Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

3050 linhas
102 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (load "bytecomp") ;; Required for WSL environment
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  7. ;; TODO: Use custom-set-variables in place of set-variable
  8. (setq debug-on-error t)
  9. ;; make directories
  10. (unless (file-directory-p (expand-file-name user-emacs-directory))
  11. (make-directory (expand-file-name user-emacs-directory)))
  12. ;; Custom file
  13. (setq custom-file (expand-file-name "custom.el" user-emacs-directory))
  14. (when (file-readable-p custom-file)
  15. (load custom-file))
  16. (require 'cl-lib)
  17. (require 'simple)
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19. ;; Some macros for internals
  20. (defvar after-first-visit-hook nil
  21. "Run only once at the first visit of file.")
  22. (defvar after-first-visit-hook--done nil
  23. "Non-nil when `after-first-visit-hook' has already been called.")
  24. (defun after-first-visit-hook-run ()
  25. "Run `after-first-visit-hook' and clear its config."
  26. (when (not after-first-visit-hook--done)
  27. (run-hooks 'after-first-visit-hook))
  28. (setq after-first-visit-hook--done t)
  29. (remove-hook 'find-file-hook
  30. 'after-first-visit-hook-run))
  31. (add-hook 'find-file-hook
  32. 'after-first-visit-hook-run)
  33. (defmacro eval-after-init (&rest body)
  34. "If `after-init-hook' has been run, run BODY immediately.
  35. Otherwize hook it."
  36. (declare (indent 0) (debug t))
  37. `(if after-init-time
  38. ;; Currently after-init-hook is run just after setting after-init-hook
  39. (progn
  40. ,@body)
  41. (add-hook 'after-init-hook
  42. (lambda ()
  43. ,@body))))
  44. ;; (defmacro safe-require-or-eval (feature)
  45. ;; "Require FEATURE if available.
  46. ;; At compile time the feature will be loaded immediately."
  47. ;; `(eval-and-compile
  48. ;; (message "safe-require-or-eval: Trying to require %s" ,feature)
  49. ;; (require ,feature nil t)))
  50. ;; (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  51. ;; "Define autoloading FEATURE that defines FUNCTIONS.
  52. ;; FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  53. ;; the function same as FEATURE is defined as autoloaded function. BODY is passed
  54. ;; to `eval-after-load'.
  55. ;; After this macro is expanded, this returns the path to library if FEATURE
  56. ;; found, otherwise returns nil."
  57. ;; (declare (indent 2) (debug t))
  58. ;; (let* ((libname (symbol-name (eval feature)))
  59. ;; (libpath (locate-library libname)))
  60. ;; `(progn
  61. ;; (when (locate-library ,libname)
  62. ;; ,@(mapcar (lambda (f)
  63. ;; `(unless (fboundp ',f)
  64. ;; (progn
  65. ;; (message "Autoloaded function `%S' defined (%s)"
  66. ;; (quote ,f)
  67. ;; ,libpath)
  68. ;; (autoload (quote ,f)
  69. ;; ,libname
  70. ;; ,(concat "Autoloaded function defined in \""
  71. ;; libpath
  72. ;; "\".")
  73. ;; t))))
  74. ;; (or (eval functions)
  75. ;; `(,(eval feature)))))
  76. ;; (eval-after-load ,feature
  77. ;; (quote (progn
  78. ;; ,@body)))
  79. ;; (locate-library ,libname))))
  80. ;; (when (autoload-eval-lazily 'tetris nil
  81. ;; (message "Tetris loaded!"))
  82. ;; (message "Tetris found!"))
  83. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  84. ;; package
  85. (require 'package)
  86. (set-variable 'package-archives
  87. `(,@package-archives
  88. ("melpa" . "https://melpa.org/packages/")
  89. ;; Somehow fails to download via https
  90. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  91. (package-initialize)
  92. ;; Use package-install-selected-packages to install these
  93. (let ((my '(
  94. vimrc-mode
  95. markdown-mode
  96. yaml-mode
  97. gnuplot-mode
  98. php-mode
  99. erlang
  100. js2-mode
  101. js-doc
  102. git-commit
  103. gitignore-mode
  104. adoc-mode
  105. go-mode
  106. ;; It seems malabar has been merged into jdee and this package
  107. ;; already removed
  108. ;; malabar-mode
  109. gosh-mode
  110. scala-mode
  111. web-mode
  112. toml-mode
  113. json-mode
  114. color-moccur
  115. ggtags
  116. flycheck
  117. auto-highlight-symbol
  118. hl-todo
  119. ;; Currently not available
  120. ;; pp-c-l
  121. xclip
  122. foreign-regexp
  123. multi-term
  124. term-run
  125. editorconfig
  126. git-ps1-mode
  127. restart-emacs
  128. fill-column-indicator
  129. pkgbuild-mode
  130. minibuffer-line
  131. which-key
  132. ;; I think this works in place of my autosave lib
  133. super-save
  134. pipenv
  135. imenu-list
  136. page-break-lines
  137. ;; aggressive-indent
  138. dired-filter
  139. wgrep
  140. magit
  141. git-gutter
  142. end-mark
  143. sl
  144. ;; TODO: Configure pony-tpl-mode
  145. pony-mode
  146. gited
  147. highlight-indentation
  148. diminish
  149. fzf
  150. fic-mode
  151. term-cursor
  152. pydoc
  153. color-identifiers-mode
  154. dired-k
  155. blacken
  156. back-button
  157. with-venv
  158. nyan-mode
  159. diredfl
  160. editorconfig
  161. editorconfig-custom-majormode
  162. git-command
  163. prompt-text
  164. ;; 10sr repository
  165. ;; 10sr-extras
  166. terminal-title
  167. dired-list-all-mode
  168. pack
  169. set-modeline-color
  170. read-only-only-mode
  171. smart-revert
  172. autosave
  173. ;;window-organizer
  174. ilookup
  175. pasteboard
  176. awk-preview
  177. recently
  178. )))
  179. (set-variable 'package-selected-packages
  180. (cl-remove-duplicates (append package-selected-packages
  181. my
  182. ()))))
  183. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  184. ;; my-idle-hook
  185. (defvar my-idle-hook nil
  186. "Hook run when idle for several secs.")
  187. (defvar my-idle-hook-sec 5
  188. "Second to run `my-idle-hook'.")
  189. (run-with-idle-timer my-idle-hook-sec
  190. t
  191. (lambda ()
  192. (run-hooks 'my-idle-hook)))
  193. ;; (add-hook 'my-idle-hook
  194. ;; (lambda ()
  195. ;; (message "idle hook message")))
  196. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  197. ;; start and quit
  198. (setq inhibit-startup-message t)
  199. (setq initial-buffer-choice 'messages-buffer)
  200. (setq confirm-kill-emacs 'y-or-n-p)
  201. (setq gc-cons-threshold (* 1024 1024 16))
  202. (setq garbage-collection-messages nil)
  203. (when window-system
  204. (add-to-list 'default-frame-alist '(cursor-type . box))
  205. (add-to-list 'default-frame-alist '(background-color . "white"))
  206. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  207. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  208. ;; does not work?
  209. )
  210. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  211. (menu-bar-mode 1)
  212. (define-key ctl-x-map "M" 'menu-bar-open)
  213. (defalias 'menu 'menu-bar-open)
  214. (and (fboundp 'tool-bar-mode)
  215. (tool-bar-mode 0))
  216. (and (fboundp 'set-scroll-bar-mode)
  217. (set-scroll-bar-mode nil))
  218. (eval-after-init
  219. (message "%s %s" invocation-name emacs-version)
  220. (message "Invocation directory: %s" default-directory)
  221. (message "%s was taken to initialize emacs." (emacs-init-time))
  222. ;; (view-echo-area-messages)
  223. ;; (view-emacs-news)
  224. )
  225. (with-current-buffer "*Messages*"
  226. (emacs-lock-mode 'kill))
  227. (cd ".") ; when using windows use / instead of \ in `default-directory'
  228. ;; locale
  229. (set-language-environment "Japanese")
  230. (set-default-coding-systems 'utf-8-unix)
  231. (prefer-coding-system 'utf-8-unix)
  232. (setq system-time-locale "C")
  233. ;; my prefix map
  234. (defvar my-prefix-map nil
  235. "My prefix map.")
  236. (define-prefix-command 'my-prefix-map)
  237. (global-set-key (kbd "C-^") 'my-prefix-map)
  238. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  239. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  240. ;; (comint-show-maximum-output)
  241. ;; kill scratch
  242. (eval-after-init
  243. (let ((buf (get-buffer "*scratch*")))
  244. (when buf
  245. (kill-buffer buf))))
  246. ;; modifier keys
  247. ;; (setq mac-option-modifier 'control)
  248. ;; display
  249. (setq visible-bell t)
  250. (setq ring-bell-function 'ignore)
  251. (mouse-avoidance-mode 'banish)
  252. (setq echo-keystrokes 0.1)
  253. (defun reload-init-file ()
  254. "Reload Emacs init file."
  255. (interactive)
  256. (when (and user-init-file
  257. (file-readable-p user-init-file))
  258. (load-file user-init-file)))
  259. (require 'session nil t)
  260. ;; server
  261. (set-variable 'server-name (concat "server"
  262. (number-to-string (emacs-pid))))
  263. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  264. ;; In Darwin environment, init fails with message like 'Service name too long'
  265. ;; when server-use-tcp is nil
  266. (when (or (eq system-type
  267. 'cygwin)
  268. (eq system-type
  269. 'darwin))
  270. (set-variable 'server-use-tcp t))
  271. ;; MSYS2 fix
  272. (when (eq system-type
  273. 'windows-nt)
  274. (setq shell-file-name
  275. (executable-find "bash"))
  276. '(setq function-key-map
  277. `(,@function-key-map ([pause] . [?\C-c])
  278. ))
  279. (define-key key-translation-map
  280. (kbd "<pause>")
  281. (kbd "C-c"))
  282. '(keyboard-translate [pause]
  283. (kbd "C-c")p)
  284. ;; TODO: move to other place later
  285. (when (not window-system)
  286. (setq interprogram-paste-function nil)
  287. (setq interprogram-cut-function nil)))
  288. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  289. ;; global keys
  290. (global-set-key (kbd "<up>") 'scroll-down-line)
  291. (global-set-key (kbd "<down>") 'scroll-up-line)
  292. (global-set-key (kbd "<left>") 'scroll-down)
  293. (global-set-key (kbd "<right>") 'scroll-up)
  294. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  295. ;; (global-set-key (kbd "C-\\") help-map)
  296. (define-key ctl-x-map (kbd "DEL") help-map)
  297. (define-key ctl-x-map (kbd "C-h") help-map)
  298. ;; This is often fired mistakenly
  299. (define-key ctl-x-map "h" 'ignore) ;; Previously mark-whole-buffer
  300. (define-key help-map "a" 'apropos)
  301. ;; disable annoying keys
  302. (global-set-key [prior] 'ignore)
  303. (global-set-key (kbd "<next>") 'ignore)
  304. (global-set-key [menu] 'ignore)
  305. (global-set-key [down-mouse-1] 'ignore)
  306. (global-set-key [down-mouse-2] 'ignore)
  307. (global-set-key [down-mouse-3] 'ignore)
  308. (global-set-key [mouse-1] 'ignore)
  309. (global-set-key [mouse-2] 'ignore)
  310. (global-set-key [mouse-3] 'ignore)
  311. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  312. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  313. ;; Interactively evaluate Emacs Lisp expressions
  314. (define-key ctl-x-map "i" 'ielm)
  315. (when (fboundp 'which-key-mode)
  316. (which-key-mode))
  317. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  318. ;; editor
  319. ;; Basically it should not set globally (instead use something like file local
  320. ;; variables or editorconfig), but for most cases I just need this...
  321. (defun my-set-require-final-newline ()
  322. "Set `require-final-newline'."
  323. (set (make-local-variable 'require-final-newline)
  324. mode-require-final-newline))
  325. (add-hook 'prog-mode-hook
  326. 'my-set-require-final-newline)
  327. (add-hook 'text-mode-hook
  328. 'my-set-require-final-newline)
  329. (add-hook 'conf-mode-hook
  330. 'my-set-require-final-newline)
  331. ;; Used from term-cursor
  332. ;; hbar is too hard to find...
  333. (defun my-cursor-type-change (&rest args)
  334. "ARGS are discarded."
  335. ;; TODO: Support wdired and wgrep
  336. (if buffer-read-only
  337. (setq cursor-type 'hbar)
  338. (setq cursor-type 'box)))
  339. ;; (add-hook 'switch-buffer-functions
  340. ;; 'my-cursor-type-change)
  341. ;; (add-hook 'read-only-mode-hook
  342. ;; 'my-cursor-type-change)
  343. ;; (when (fboundp 'global-term-cursor-mode)
  344. ;; (global-term-cursor-mode 1))
  345. ;; ;; (term-cursor--eval)
  346. (setq kill-whole-line t)
  347. (setq scroll-conservatively 35
  348. scroll-margin 2)
  349. (setq-default major-mode 'text-mode)
  350. (setq next-line-add-newlines nil)
  351. (setq kill-read-only-ok t)
  352. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  353. ;; (setq-default line-spacing 0.2)
  354. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  355. ;; (setq-default tab-width 4)
  356. (setq-default indent-tabs-mode nil)
  357. (setq-default indent-line-function 'indent-to-left-margin)
  358. ;; (setq-default indent-line-function nil)
  359. (setq-default truncate-lines nil)
  360. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  361. (delete-selection-mode 1)
  362. (cua-mode 0)
  363. (setq line-move-visual nil)
  364. (setq create-lockfiles nil)
  365. (setq set-mark-command-repeat-pop t)
  366. (add-hook 'before-save-hook
  367. 'time-stamp)
  368. ;; Add Time-stamp: <> to insert timestamp there
  369. (set-variable 'time-stamp-format
  370. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  371. ;; key bindings
  372. ;; moving around
  373. ;;(keyboard-translate ?\M-j ?\C-j)
  374. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  375. (define-key esc-map "p" 'backward-paragraph)
  376. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  377. (define-key esc-map "n" 'forward-paragraph)
  378. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  379. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  380. (global-set-key (kbd "C-<left>") 'scroll-down)
  381. (global-set-key (kbd "C-<right>") 'scroll-up)
  382. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  383. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  384. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  385. ;; C-h and DEL
  386. (global-set-key (kbd "C-h") (kbd "DEL"))
  387. ;; (normal-erase-is-backspace-mode 1)
  388. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  389. (global-set-key (kbd "C-m") 'newline-and-indent)
  390. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  391. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  392. (define-key esc-map "u" 'undo)
  393. (define-key esc-map "i" (kbd "ESC TAB"))
  394. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  395. ;; (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  396. ;; (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  397. (set-variable 'search-default-mode t)
  398. ;; TODO: Do not depend on ivy function
  399. ;; (set-variable 'search-default-mode
  400. ;; (lambda (str lax)
  401. ;; (ivy--regex-fuzzy (replace-regexp-in-string (rx (one-or-more whitespace))
  402. ;; ""
  403. ;; str))))
  404. (when (fboundp 'undo-fu-only-undo)
  405. (global-set-key (kbd "C-_") 'undo-fu-only-undo))
  406. (when (fboundp 'undo-fu-only-redo)
  407. (global-set-key (kbd "C-M-_") 'undo-fu-only-redo))
  408. (require 'page-ext nil t)
  409. (when (fboundp 'global-page-break-lines-mode)
  410. (add-hook 'after-first-visit-hook
  411. 'global-page-break-lines-mode))
  412. (with-eval-after-load 'page-break-lines
  413. (set-face-foreground 'page-break-lines
  414. "cyan")
  415. )
  416. (defun my-insert-page-break ()
  417. "Insert ^L."
  418. (interactive)
  419. (insert "\^L\n"))
  420. (when (fboundp 'global-git-gutter-mode)
  421. (add-hook 'after-first-visit-hook
  422. 'global-git-gutter-mode))
  423. (with-eval-after-load 'git-gutter
  424. (declare-function global-git-gutter-mode "git-gutter")
  425. (custom-set-variables
  426. '(git-gutter:lighter " Gttr"))
  427. (custom-set-variables
  428. '(git-gutter:update-interval 2))
  429. (custom-set-variables
  430. '(git-gutter:unchanged-sign " "))
  431. (when (>= (display-color-cells)
  432. 256)
  433. (let ((c "color-233"))
  434. (set-face-background 'git-gutter:modified c)
  435. (set-face-background 'git-gutter:added c)
  436. (set-face-background 'git-gutter:deleted c)
  437. (set-face-background 'git-gutter:unchanged c)))
  438. (set-face-background 'git-gutter:modified "magenta")
  439. (set-face-background 'git-gutter:added "green")
  440. (set-face-background 'git-gutter:deleted "red")
  441. )
  442. ;; (when (fboundp 'fancy-narrow-mode)
  443. ;; (add-hook 'after-first-visit-hook
  444. ;; 'fancy-narrow-mode))
  445. ;; https://solist.work/blog/posts/mark-ring/
  446. (defun my-exchange-point-and-mark ()
  447. "`exchange-point-and-mark' without mark activation."
  448. (interactive)
  449. (exchange-point-and-mark)
  450. (deactivate-mark))
  451. (define-key ctl-x-map (kbd "C-x") 'my-exchange-point-and-mark)
  452. (when (fboundp 'counsel-mark-ring)
  453. (define-key ctl-x-map "m" 'counsel-mark-ring))
  454. (with-eval-after-load 'ivy
  455. (defvar ivy-sort-functions-alist)
  456. (add-to-list 'ivy-sort-functions-alist
  457. '(counsel-mark-ring)))
  458. (run-with-idle-timer 10 t
  459. (lambda ()
  460. (push-mark)
  461. ;; (when (fboundp 'visible-mark-move-overlays)
  462. ;; (visible-mark-move-overlays))
  463. ))
  464. (add-hook 'switch-buffer-functions
  465. (lambda (&rest _)
  466. (unless (or (mark t)
  467. (minibufferp))
  468. (push-mark))))
  469. (add-hook 'switch-buffer-functions
  470. (lambda (&rest _)
  471. (when (minibufferp)
  472. ;; Remove mark in minibuffer
  473. (set-mark nil))))
  474. (when (fboundp 'back-button-mode)
  475. (back-button-mode 1))
  476. (when (fboundp 'back-button-local-forward)
  477. (global-set-key (kbd "<right>") 'back-button-local-forward))
  478. (when (fboundp 'back-button-local-backward)
  479. (global-set-key (kbd "<left>") 'back-button-local-backward))
  480. (when (fboundp 'global-visible-mark-mode)
  481. (set-variable 'visible-mark-max 2)
  482. ;; (set-variable 'visible-mark-faces '(visible-mark-face1 visible-mark-face2))
  483. ;; http://emacs.rubikitch.com/visible-mark/
  484. ;; transient-mark-modeでC-SPC C-SPC、あるいはC-SPC C-gすると消えるバグ修正
  485. (defun visible-mark-move-overlays--avoid-disappear (&rest them)
  486. "Fix.
  487. THEM are function and its args."
  488. (let ((mark-active t)) (apply them)))
  489. (advice-add 'visible-mark-move-overlays
  490. :around
  491. 'visible-mark-move-overlays--avoid-disappear)
  492. ;; (global-visible-mark-mode 1)
  493. )
  494. ;; visible-mark-mode
  495. ;; visible-mark-overlays
  496. ;; mark-ring
  497. ;; (equal mark-ring (cl-copy-list mark-ring))
  498. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  499. ;; title and mode-line
  500. (when (fboundp 'terminal-title-mode)
  501. ;; if TERM is not screen use default value
  502. (if (getenv "TMUX")
  503. ;; if use tmux locally just basename of current dir
  504. (set-variable 'terminal-title-format
  505. '((file-name-nondirectory (directory-file-name
  506. default-directory))))
  507. (if (and (let ((tty-type (frame-parameter nil
  508. 'tty-type)))
  509. (and tty-type
  510. (equal (car (split-string tty-type
  511. "-"))
  512. "screen")))
  513. (not (getenv "SSH_CONNECTION")))
  514. (set-variable 'terminal-title-format
  515. '((file-name-nondirectory (directory-file-name
  516. default-directory))))
  517. ;; seems that TMUX is used locally and ssh to remote host
  518. (set-variable 'terminal-title-format
  519. `("em:"
  520. ,user-login-name
  521. "@"
  522. ,(car (split-string (system-name)
  523. "\\."))
  524. ":"
  525. default-directory))
  526. )
  527. )
  528. (terminal-title-mode))
  529. (setq eol-mnemonic-dos "\\r\\n")
  530. (setq eol-mnemonic-mac "\\r")
  531. (setq eol-mnemonic-unix "")
  532. (which-function-mode 1)
  533. (line-number-mode 0)
  534. (column-number-mode 0)
  535. (size-indication-mode 0)
  536. (setq mode-line-position
  537. '(:eval (format ":%%l:%%c /%d%s"
  538. (count-lines (point-max)
  539. (point-min))
  540. (if (buffer-narrowed-p)
  541. "[N]"
  542. "")
  543. )))
  544. (when (fboundp 'diminish)
  545. (eval-after-init
  546. (diminish 'recently-mode)
  547. (diminish 'editorconfig-mode)
  548. (diminish 'which-key-mode)
  549. )
  550. (with-eval-after-load 'whitespace
  551. (diminish 'global-whitespace-mode))
  552. (with-eval-after-load 'page-break-lines
  553. (diminish 'page-break-lines-mode))
  554. (with-eval-after-load 'auto-highlight-symbol
  555. (diminish 'auto-highlight-symbol-mode))
  556. (with-eval-after-load 'color-identifiers-mode
  557. (diminish 'color-identifiers-mode))
  558. (with-eval-after-load 'highlight-indentation
  559. (diminish 'highlight-indentation-mode))
  560. (with-eval-after-load 'back-button
  561. (diminish 'back-button-mode))
  562. (with-eval-after-load 'git-gutter
  563. (diminish 'git-gutter-mode))
  564. (with-eval-after-load 'autorevert
  565. (diminish 'auto-revert-mode))
  566. )
  567. (setq mode-line-front-space "")
  568. ;; (setq mode-line-end-spaces "")
  569. ;; Set current frame name to empty string
  570. (setq-default mode-line-format
  571. (let* ((l mode-line-format)
  572. (l (cl-substitute " " " "
  573. l
  574. :test 'equal))
  575. (l (cl-substitute " " " "
  576. l
  577. :test 'equal))
  578. )
  579. l))
  580. (set-frame-parameter nil 'name "")
  581. ;; See color-name-rgb-alist for available color names
  582. ;; http://www.raebear.net/computers/emacs-colors/
  583. ;; https://www.emacswiki.org/emacs/ListColors
  584. ;; (list-colors-display is not a complete list)
  585. (defconst my-mode-line-background-default
  586. (face-background 'mode-line)
  587. "Default color of mode-line at init.")
  588. (defun my-mode-line-color-update (&rest args)
  589. "ARGS are discarded"
  590. (let ((ro "skyblue")
  591. (rw my-mode-line-background-default))
  592. (if (or (not buffer-read-only)
  593. (and (eq major-mode 'wdired-mode)))
  594. (set-face-background 'mode-line
  595. rw)
  596. (set-face-background 'mode-line
  597. ro))))
  598. (add-hook 'switch-buffer-functions
  599. 'my-mode-line-color-update)
  600. (add-hook 'read-only-mode-hook
  601. 'my-mode-line-color-update)
  602. (add-hook 'wdired-mode-hook
  603. 'my-mode-line-color-update)
  604. (advice-add 'wdired-change-to-dired-mode
  605. :after
  606. 'my-mode-line-color-update)
  607. (advice-add 'wgrep-change-to-wgrep-mode
  608. :after
  609. 'my-mode-line-color-update)
  610. (advice-add 'wgrep-to-original-mode
  611. :after
  612. 'my-mode-line-color-update)
  613. (set-face-background 'header-line
  614. my-mode-line-background-default)
  615. ;; sky-color-clock
  616. ;; https://tsuu32.hatenablog.com/entry/2019/11/07/020005
  617. (declare-function sky-color-clock "sky-color-clock")
  618. (declare-function sky-color-clock-initialize "sky-color-clock")
  619. (defun sky-color-clock--form ()
  620. "Gen string for right aligned form."
  621. (let* ((sky-color-clock-str
  622. (propertize (sky-color-clock) 'help-echo (format-time-string "Sky color clock\n%F (%a)")))
  623. (mode-line-right-margin
  624. (propertize " " 'display `(space :align-to (- right-fringe ,(length sky-color-clock-str))))))
  625. (concat mode-line-right-margin sky-color-clock-str)))
  626. (when (require 'sky-color-clock nil t)
  627. (sky-color-clock-initialize 35) ; Tokyo, Japan
  628. (set-variable 'sky-color-clock-format "%H:%M")
  629. (set-variable 'sky-color-clock-enable-emoji-icon nil)
  630. (setq mode-line-end-spaces '(:eval (sky-color-clock--form))))
  631. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  632. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  633. ;; minibuffer
  634. (setq insert-default-directory t)
  635. (setq completion-ignore-case t
  636. read-file-name-completion-ignore-case t
  637. read-buffer-completion-ignore-case t)
  638. (setq resize-mini-windows t)
  639. (temp-buffer-resize-mode 1)
  640. (savehist-mode 1)
  641. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  642. (set-variable 'help-at-pt-display-when-idle t)
  643. (fset 'yes-or-no-p 'y-or-n-p)
  644. ;; complete symbol when `eval'
  645. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  646. (define-key minibuffer-local-map (kbd "C-u")
  647. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  648. ;; I dont know these bindings are good
  649. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  650. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  651. (with-eval-after-load 'minibuffer-line
  652. (set-face-underline 'minibuffer-line nil)
  653. )
  654. (when (fboundp 'minibuffer-line-mode)
  655. (set-variable 'minibuffer-line-refresh-interval
  656. 25)
  657. ;; Set idle timer
  658. (defvar my-minibuffer-line--idle-timer nil)
  659. (defvar minibuffer-line-mode)
  660. (add-hook 'minibuffer-line-mode-hook
  661. (lambda ()
  662. (when my-minibuffer-line--idle-timer
  663. (cancel-timer my-minibuffer-line--idle-timer)
  664. (setq my-minibuffer-line--idle-timer nil))
  665. (when minibuffer-line-mode
  666. (setq my-minibuffer-line--idle-timer
  667. (run-with-idle-timer 0.5
  668. t
  669. 'minibuffer-line--update)))))
  670. (set-variable 'minibuffer-line-format
  671. `(,(concat user-login-name
  672. "@"
  673. (car (split-string (system-name)
  674. "\\."))
  675. ":")
  676. (:eval (abbreviate-file-name (or buffer-file-name
  677. default-directory)))
  678. (:eval (and (fboundp 'git-ps1-mode-get-current)
  679. (git-ps1-mode-get-current " [GIT:%s]")))
  680. " "
  681. (:eval (format-time-string display-time-format))))
  682. (minibuffer-line-mode 1)
  683. )
  684. (when (fboundp 'prompt-text-mode)
  685. (set-variable 'prompt-text-format
  686. `(,(concat ""
  687. user-login-name
  688. "@"
  689. (car (split-string (system-name)
  690. "\\."))
  691. ":")
  692. (:eval (abbreviate-file-name (or buffer-file-name
  693. default-directory)))
  694. (:eval (and (fboundp 'git-ps1-mode-get-current)
  695. (git-ps1-mode-get-current " [GIT:%s]")))
  696. " "
  697. (:eval (format-time-string display-time-format))
  698. "\n"
  699. (:eval (symbol-name this-command))
  700. ": "))
  701. (prompt-text-mode 1))
  702. (with-eval-after-load 'helm
  703. (defvar helm-map)
  704. (define-key helm-map (kbd "C-h") (kbd "DEL")))
  705. (setq-default header-line-format
  706. '(:eval (let ((f (or (buffer-file-name)
  707. default-directory)))
  708. (when f
  709. (abbreviate-file-name f)))))
  710. (when (eval-and-compile (require 'nyan-mode nil t))
  711. (setq-default header-line-format
  712. '(
  713. ;; (:eval (file-name-nondirectory(directory-file-name (or buffer-file-name
  714. ;; default-directory))))
  715. ;; "["
  716. (:eval (progn
  717. (list (nyan-create))))
  718. ;; "]"
  719. ;; (:eval (let ((f (or (buffer-file-name)
  720. ;; default-directory)))
  721. ;; (when f
  722. ;; (abbreviate-file-name f))))
  723. ))
  724. (defun my-nyan-set-length (&rest _)
  725. "Set `nyan-mode' length to window width."
  726. (set-variable 'nyan-bar-length
  727. (- (window-size nil t) 4)
  728. t))
  729. (add-hook 'window-configuration-change-hook
  730. 'my-nyan-set-length)
  731. (add-hook 'switch-buffer-functions
  732. 'my-nyan-set-length)
  733. )
  734. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  735. ;; letters, font-lock mode and fonts
  736. (when (fboundp 'color-identifiers-mode)
  737. (add-hook 'prog-mode-hook
  738. 'color-identifiers-mode))
  739. (setq text-quoting-style 'grave)
  740. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  741. ;; (set-window-margins (selected-window) 1 1)
  742. (unless window-system
  743. (setq frame-background-mode 'dark))
  744. (and (or (eq system-type 'Darwin)
  745. (eq system-type 'darwin))
  746. (fboundp 'mac-set-input-method-parameter)
  747. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  748. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  749. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  750. (boundp 'input-method-inactivate-hook))
  751. (add-hook 'input-method-activate-hook
  752. (lambda () (set-cursor-color "red")))
  753. (add-hook 'input-method-inactivate-hook
  754. (lambda () (set-cursor-color "black"))))
  755. (when (fboundp 'show-paren-mode)
  756. (add-hook 'after-first-visit-hook
  757. 'show-paren-mode))
  758. (set-variable 'show-paren-delay 0.5)
  759. (set-variable 'show-paren-style 'parenthesis) ; mixed is hard to read
  760. ;; (set-face-background 'show-paren-match
  761. ;; "black")
  762. ;; ;; (face-foreground 'default))
  763. ;; (set-face-foreground 'show-paren-match
  764. ;; "white")
  765. ;; (set-face-inverse-video-p 'show-paren-match
  766. ;; t)
  767. (transient-mark-mode 1)
  768. (global-font-lock-mode 1)
  769. (setq font-lock-global-modes
  770. '(not
  771. help-mode
  772. eshell-mode
  773. ;;term-mode
  774. Man-mode
  775. ))
  776. ;; (standard-display-ascii ?\n "$\n")
  777. ;; (defvar my-eol-face
  778. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  779. ;; )
  780. ;; (defvar my-tab-face
  781. ;; '(("\t" . '(0 highlight t nil))))
  782. (defvar my-jspace-face
  783. '(("\u3000" . '(0 highlight t nil))))
  784. (add-hook 'font-lock-mode-hook
  785. (lambda ()
  786. ;; (font-lock-add-keywords nil my-eol-face)
  787. (font-lock-add-keywords nil my-jspace-face)
  788. ))
  789. (when (fboundp 'global-whitespace-mode)
  790. (add-hook 'after-first-visit-hook
  791. 'global-whitespace-mode))
  792. (add-hook 'dired-mode-hook
  793. ;; Other way to disable in dired buffers?
  794. (lambda () (set-variable 'whitespace-style nil t)))
  795. (with-eval-after-load 'whitespace
  796. (defvar whitespace-display-mappings)
  797. (defvar whitespace-mode)
  798. (add-to-list 'whitespace-display-mappings
  799. ;; We need t since last one takes precedence
  800. `(tab-mark ?\t ,(vconcat "|>\t")) t)
  801. ;; (add-to-list 'whitespace-display-mappings
  802. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  803. (set-variable 'whitespace-style '(face
  804. trailing ; trailing blanks
  805. ;; tabs
  806. ;; spaces
  807. ;; lines
  808. lines-tail ; lines over 80
  809. newline ; newlines
  810. ;; empty ; empty lines at beg or end of buffer
  811. ;; big-indent
  812. ;; space-mark
  813. tab-mark
  814. newline-mark ; use display table for newline
  815. ))
  816. ;; (setq whitespace-newline 'font-lock-comment-face)
  817. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  818. (defun my-whitesspace-mode-reload ()
  819. "Reload whitespace-mode config."
  820. (interactive)
  821. (when whitespace-mode
  822. (whitespace-mode 0)
  823. (whitespace-mode 1)))
  824. (set-variable 'whitespace-line-column nil)
  825. (if (>= (display-color-cells)
  826. 256)
  827. (set-face-foreground 'whitespace-newline "color-109")
  828. ;; (progn
  829. ;; (set-face-bold-p 'whitespace-newline
  830. ;; t))
  831. ))
  832. (and nil
  833. '(require 'fill-column-indicator nil t)
  834. (setq fill-column-indicator))
  835. (defun my-gen-hl-line-color-dark ()
  836. "Generate color for current line in black background."
  837. (let* ((candidates (mapcar 'number-to-string (number-sequence 1 6)))
  838. (limit (length candidates))
  839. (r 0) (g 0) (b 0))
  840. (while (and (<= (abs (- r g)) 1)
  841. (<= (abs (- g b)) 1)
  842. (<= (abs (- b r)) 1))
  843. (setq r (random limit))
  844. (setq g (random limit))
  845. (setq b (random limit)))
  846. (format "#%s%s%s"
  847. (nth r candidates)
  848. (nth g candidates)
  849. (nth b candidates)
  850. )))
  851. ;; (my-gen-hl-line-color-dark)
  852. ;; highlight current line
  853. ;; http://wiki.riywo.com/index.php?Meadow
  854. (face-spec-set 'hl-line
  855. `((((min-colors 256)
  856. (background dark))
  857. ;; Rotate midnightblue
  858. (:background ,(my-gen-hl-line-color-dark)))
  859. (((min-colors 256)
  860. (background light))
  861. ;; TODO: What is should be?
  862. (:background "color-234"))
  863. (t
  864. (:underline "black"))))
  865. (set-variable 'hl-line-global-modes
  866. '(not
  867. term-mode))
  868. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  869. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  870. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  871. ;;(require 'set-modeline-color nil t)
  872. ;; (let ((fg (face-foreground 'default))
  873. ;; (bg (face-background 'default)))
  874. ;; (set-face-background 'mode-line-inactive
  875. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  876. ;; (set-face-foreground 'mode-line-inactive
  877. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  878. ;; (set-face-underline 'mode-line-inactive
  879. ;; t)
  880. ;; (set-face-underline 'vertical-border
  881. ;; nil)
  882. ;; (when (require 'end-mark nil t)
  883. ;; (global-end-mark-mode))
  884. ;; M-x highlight-* to highlight things
  885. (global-hi-lock-mode 1)
  886. (unless (fboundp 'highlight-region-text)
  887. (defun highlight-region-text (beg end)
  888. "Highlight text between BEG and END."
  889. (interactive "r")
  890. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  891. end)))
  892. (setq deactivate-mark t)))
  893. (when (fboundp 'auto-highlight-symbol-mode)
  894. (add-hook 'prog-mode-hook
  895. 'auto-highlight-symbol-mode))
  896. ;; Not work in combination with flyspell-mode
  897. ;; (when (fboundp 'global-auto-highlight-symbol-mode)
  898. ;; (add-hook 'after-first-visit-hook
  899. ;; 'global-auto-highlight-symbol-mode))
  900. (set-variable 'ahs-idle-interval 0.6)
  901. (when (fboundp 'highlight-indentation-mode)
  902. (dolist (hook
  903. '(
  904. prog-mode-hook
  905. text-mode-hook
  906. ))
  907. (add-hook hook
  908. 'highlight-indentation-mode)))
  909. (with-eval-after-load 'highlight-indentation
  910. (set-face-background 'highlight-indentation-face "color-236"))
  911. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  912. (when (fboundp 'fic-mode)
  913. (add-hook 'prog-mode-hook
  914. 'fic-mode))
  915. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  916. ;; file handling
  917. (auto-insert-mode 1)
  918. ;; fzf
  919. ;; Too slow in term buffer!
  920. ;; (set-variable 'fzf/executable "sk")
  921. ;; (set-variable 'fzf/args "--color bw --print-query")
  922. ;; Modified from hardcoded default to include:
  923. ;; - directories
  924. ;; - hidden files
  925. ;; - root directory (.)
  926. ;; - parent directory (..)
  927. ;; ripgrep cannot list directories...
  928. ;; (setenv "FZF_DEFAULT_COMMAND" "rg --files --hidden --follow --glob '!.git/*' --no-ignore")
  929. (defvar my-fzf-default-command nil
  930. "My fzf FZF_DEFAULT_COMMAND.")
  931. (set-variable 'my-fzf-default-command
  932. (let ((find (or (executable-find "bfs") ;; Breadth-first find https://github.com/tavianator/bfs
  933. ;; Use gfind if available?
  934. "find"))
  935. (fd (or (executable-find "fdfind")
  936. (executable-find "fd"))))
  937. (if fd
  938. (concat "set -eu; set -o pipefail; "
  939. "echo .; "
  940. "echo ..; "
  941. "command " fd " "
  942. "--follow --hidden --no-ignore "
  943. "--color always "
  944. "2>/dev/null")
  945. (concat "set -eu; set -o pipefail; "
  946. "echo .; "
  947. "echo ..; "
  948. "command " find " -L . "
  949. "-mindepth 1 "
  950. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  951. "-o -print "
  952. "2> /dev/null "
  953. "| "
  954. "cut -b3-"))))
  955. (set-variable 'fzf/window-height 45)
  956. (set-variable 'fzf/args "--print-query --ansi --color='bg+:-1' --inline-info --cycle")
  957. ;; (set-variable 'fzf/args "--print-query --ansi --inline-info --cycle")
  958. ;; (set-variable 'fzf/args "--print-query --ansi --color=bw --inline-info --cycle")
  959. (declare-function fzf "fzf" t)
  960. (defun my-fzf-or-find-file ()
  961. "Call fzf if usable or call `find-file'."
  962. (declare (interactive-only t))
  963. (interactive)
  964. (if (and (executable-find "fzf")
  965. (fboundp 'fzf)
  966. (not (file-remote-p default-directory)))
  967. (let ((process-environment (cl-copy-list process-environment)))
  968. (setenv "FZF_DEFAULT_COMMAND" my-fzf-default-command)
  969. (fzf))
  970. (call-interactively 'find-file)))
  971. (define-key ctl-x-map "f" 'my-fzf-or-find-file)
  972. (defun my-fzf-all-lines ()
  973. "Fzf all lines."
  974. (interactive)
  975. (let ((process-environment (cl-copy-list process-environment)))
  976. (setenv "FZF_DEFAULT_COMMAND" "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^")
  977. (fzf)))
  978. (define-key ctl-x-map "S" 'my-fzf-all-lines)
  979. ;; recently
  980. ;; TODO: Enable after first visit file?
  981. (with-eval-after-load 'recently
  982. (defvar recently-excludes)
  983. (add-to-list 'recently-excludes
  984. (rx-to-string (list 'and
  985. 'string-start
  986. (expand-file-name package-user-dir))
  987. t)))
  988. (when (fboundp 'recently-mode)
  989. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  990. (set-variable 'recently-max 1000)
  991. (recently-mode 1))
  992. (defvar my-cousel-recently-history nil "History of `my-counsel-recently'.")
  993. (declare-function recently-list "recently" t)
  994. (when (and (require 'recently nil t)
  995. (fboundp 'ivy-read))
  996. (defun my-counsel-recently ()
  997. "Consel `recently'."
  998. (interactive)
  999. (ivy-read "Recently: " (mapcar 'abbreviate-file-name (recently-list))
  1000. :require-match t
  1001. :history 'my-cousel-recently-history
  1002. :preselect default-directory
  1003. :action (lambda (x) (find-file x))
  1004. :caller 'my-counsel-recently))
  1005. (define-key ctl-x-map (kbd "C-r") 'my-counsel-recently)
  1006. )
  1007. (when (fboundp 'editorconfig-mode)
  1008. (add-hook 'after-first-visit-hook
  1009. 'editorconfig-mode)
  1010. (add-hook 'after-first-visit-hook
  1011. 'editorconfig-mode-apply
  1012. t)) ;; Do after enabling editorconfig-mode
  1013. (set-variable 'editorconfig-get-properties-function
  1014. 'editorconfig-core-get-properties-hash)
  1015. (set-variable 'editorconfig-mode-lighter "")
  1016. (when (fboundp 'ws-butler-mode)
  1017. (set-variable 'editorconfig-trim-whitespaces-mode
  1018. 'ws-butler-mode))
  1019. (with-eval-after-load 'org-src
  1020. ;; [*.org\[\*Org Src*\[ c \]*\]]
  1021. (add-hook 'org-src-mode-hook
  1022. 'editorconfig-mode-apply t))
  1023. (when (fboundp 'editorconfig-custom-majormode)
  1024. (add-hook 'editorconfig-after-apply-functions
  1025. 'editorconfig-custom-majormode))
  1026. ;; Add readonly=true to set read-only-mode
  1027. (add-hook 'editorconfig-after-apply-functions
  1028. (lambda (props)
  1029. (let ((r (gethash 'readonly props)))
  1030. (when (and (string= r "true")
  1031. (not buffer-read-only))
  1032. (read-only-mode 1)))))
  1033. (add-hook 'editorconfig-hack-properties-functions
  1034. '(lambda (props)
  1035. (when (derived-mode-p 'makefile-mode)
  1036. (puthash 'indent_style "tab" props))
  1037. (when (derived-mode-p 'diff-mode)
  1038. (puthash 'trim_trailing_whitespace "false" props)
  1039. (puthash 'insert_final_newline "false" props)
  1040. )
  1041. ))
  1042. (when (fboundp 'editorconfig-auto-apply-enable)
  1043. (add-hook 'editorconfig-conf-mode-hook
  1044. 'editorconfig-auto-apply-enable))
  1045. ;; (when (fboundp 'editorconfig-charset-extras)
  1046. ;; (add-hook 'editorconfig-custom-hooks
  1047. ;; 'editorconfig-charset-extras))
  1048. (setq revert-without-query '(".+"))
  1049. ;; save cursor position
  1050. (when (fboundp 'save-place-mode)
  1051. (autoload 'save-place-find-file-hook "saveplace")
  1052. (add-hook 'after-first-visit-hook
  1053. 'save-place-mode)
  1054. (add-hook 'after-first-visit-hook
  1055. 'save-place-find-file-hook
  1056. t))
  1057. (set-variable 'save-place-file (concat user-emacs-directory
  1058. "places"))
  1059. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  1060. (setq make-backup-files t)
  1061. (setq vc-make-backup-files t)
  1062. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  1063. (setq backup-directory-alist
  1064. (cons (cons "." (expand-file-name (concat user-emacs-directory
  1065. "backup")))
  1066. backup-directory-alist))
  1067. (setq version-control 't)
  1068. (setq delete-old-versions t)
  1069. (setq kept-new-versions 20)
  1070. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  1071. "auto-save/")))
  1072. ;; (setq delete-auto-save-files t)
  1073. (setq auto-save-visited-interval 8)
  1074. (auto-save-visited-mode 1)
  1075. ;; (add-to-list 'auto-save-file-name-transforms
  1076. ;; `(".*" ,(concat user-emacs-directory "auto-save-dir") t))
  1077. ;; (setq auto-save-interval 3)
  1078. ;; (auto-save-mode 1)
  1079. (add-to-list 'completion-ignored-extensions ".bak")
  1080. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  1081. (setq delete-by-moving-to-trash t)
  1082. ;; trash-directory "~/.emacs.d/trash")
  1083. (add-hook 'after-save-hook
  1084. 'executable-make-buffer-file-executable-if-script-p)
  1085. (when (fboundp 'smart-revert-on)
  1086. (smart-revert-on))
  1087. ;; autosave
  1088. ;; auto-save-visited-mode can be used instead?
  1089. ;; (when (require 'autosave nil t)
  1090. ;; (autosave-set 8))
  1091. ;; bookmarks
  1092. (set-variable 'bookmark-default-file
  1093. (expand-file-name (concat user-emacs-directory
  1094. "bmk")))
  1095. (set-variable 'bookmark-sort-flag nil)
  1096. (defun my-bookmark-set ()
  1097. "My `bookmark-set'."
  1098. (interactive)
  1099. (cl-assert (or buffer-file-name
  1100. default-directory))
  1101. (let ((name (file-name-nondirectory (or buffer-file-name
  1102. (directory-file-name default-directory))))
  1103. (linenum (count-lines (point-min)
  1104. (point)))
  1105. (linetext (buffer-substring-no-properties (point-at-bol)
  1106. (point-at-eol))))
  1107. (bookmark-set (format "%s:%d:%s"
  1108. name linenum linetext)
  1109. nil)))
  1110. ;; Done by advice instead
  1111. ;; (set-variable 'bookmark-save-flag
  1112. ;; 1)
  1113. (with-eval-after-load 'recentf
  1114. (defvar recentf-exclude)
  1115. (defvar bookmark-default-file)
  1116. (add-to-list 'recentf-exclude
  1117. (regexp-quote bookmark-default-file)))
  1118. (defvar bookmark-default-file)
  1119. (defun my-bookmark-set--advice (orig-func &rest args)
  1120. "Function for `bookmark-set-internal'.
  1121. ORIG-FUNC is the target function, and ARGS is the argument when it is called."
  1122. (bookmark-load bookmark-default-file)
  1123. (apply orig-func args)
  1124. (bookmark-save))
  1125. (with-eval-after-load 'bookmark
  1126. (advice-add 'bookmark-set-internal
  1127. :around
  1128. 'my-bookmark-set--advice))
  1129. (define-key ctl-x-map "b" 'list-bookmarks)
  1130. (when (fboundp 'counsel-bookmark)
  1131. (define-key ctl-x-map "b" 'counsel-bookmark))
  1132. (define-key ctl-x-map "B" 'my-bookmark-set)
  1133. ;; vc
  1134. (set-variable 'vc-handled-backends '(RCS))
  1135. (set-variable 'vc-rcs-register-switches "-l")
  1136. (set-variable 'vc-rcs-checkin-switches "-l")
  1137. (set-variable 'vc-command-messages t)
  1138. (when (fboundp 'neotree)
  1139. (define-key ctl-x-map (kbd "C-n") 'neotree))
  1140. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1141. ;; share clipboard with x
  1142. ;; this page describes this in details, but only these sexps seem to be needed
  1143. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  1144. (and nil
  1145. (not window-system)
  1146. (not (eq window-system 'mac))
  1147. (getenv "DISPLAY")
  1148. (not (equal (getenv "DISPLAY") ""))
  1149. (executable-find "xclip")
  1150. ;; (< emacs-major-version 24)
  1151. '(require 'xclip nil t)
  1152. nil
  1153. (turn-on-xclip))
  1154. (declare-function turn-on-pasteboard "pasteboard")
  1155. (and (eq system-type 'darwin)
  1156. (require 'pasteboard nil t)
  1157. (turn-on-pasteboard))
  1158. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1159. ;; some modes and hooks
  1160. ;; Include some extra modes
  1161. (require 'generic-x)
  1162. ;; Derived from https://github.com/ensime/ensime-emacs/issues/591#issuecomment-291916753
  1163. (defun my-scalafmt ()
  1164. (interactive)
  1165. (cl-assert buffer-file-name)
  1166. (cl-assert (not (buffer-modified-p)))
  1167. (let* ((configdir (locate-dominating-file default-directory ".scalafmt.conf"))
  1168. (configoption (if configdir
  1169. (concat " --config "
  1170. (shell-quote-argument (expand-file-name configdir))
  1171. ".scalafmt.conf"
  1172. )
  1173. ""))
  1174. (str (concat "scalafmt -f "
  1175. (shell-quote-argument buffer-file-name)
  1176. configoption
  1177. " -i --exclude ensime")))
  1178. (message str)
  1179. (shell-command-to-string str))
  1180. (message "scalafmt done")
  1181. (revert-buffer nil t))
  1182. (when (fboundp 'web-mode)
  1183. (add-to-list 'auto-mode-alist
  1184. '("\\.html\\.j2\\'" . web-mode))
  1185. (add-to-list 'auto-mode-alist
  1186. ;; Django Template Language
  1187. '("\\.dtl\\'" . web-mode))
  1188. )
  1189. (when (locate-library "wgrep")
  1190. (set-variable 'wgrep-auto-save-buffer t)
  1191. (with-eval-after-load 'grep
  1192. (defvar grep-mode-map)
  1193. (define-key grep-mode-map
  1194. "e"
  1195. 'wgrep-change-to-wgrep-mode)))
  1196. (when (fboundp 'grep-context-mode)
  1197. (add-hook 'compilation-mode-hook #'grep-context-mode))
  1198. (with-eval-after-load 'remember
  1199. (defvar remember-mode-map)
  1200. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  1201. (set-variable 'remember-notes-initial-major-mode
  1202. 'change-log-mode)
  1203. (declare-function global-magit-file-mode "magit-files")
  1204. (with-eval-after-load 'magit-files
  1205. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  1206. ;; existing keybindings.
  1207. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  1208. ;; to disable thie mode here, but do anyway.
  1209. ;; See also https://github.com/magit/magit/issues/3517
  1210. (global-magit-file-mode -1))
  1211. (with-eval-after-load 'magit-section
  1212. (set-face-background 'magit-section-highlight
  1213. nil))
  1214. ;; Sane colors
  1215. (with-eval-after-load 'magit-diff
  1216. (set-face-background 'magit-diff-context nil)
  1217. (set-face-background 'magit-diff-context-highlight nil)
  1218. (set-face-foreground 'magit-diff-hunk-heading nil)
  1219. (set-face-background 'magit-diff-hunk-heading nil)
  1220. (set-face-foreground 'magit-diff-hunk-heading-highlight nil)
  1221. (set-face-background 'magit-diff-hunk-heading-highlight nil)
  1222. ;; https://blog.shibayu36.org/entry/2016/03/27/220552
  1223. (set-face-foreground 'magit-diff-added "green")
  1224. (set-face-background 'magit-diff-added nil)
  1225. (set-face-foreground 'magit-diff-added-highlight "green")
  1226. (set-face-background 'magit-diff-added-highlight nil)
  1227. (set-face-foreground 'magit-diff-removed "red")
  1228. (set-face-background 'magit-diff-removed nil)
  1229. (set-face-foreground 'magit-diff-removed-highlight "red")
  1230. (set-face-background 'magit-diff-removed-highlight nil)
  1231. (set-face-background 'magit-diff-lines-boundary "blue")
  1232. )
  1233. (declare-function magit-show-commit "magit")
  1234. (defun my-magit-messenger (file line)
  1235. "Magit messenger."
  1236. (interactive (list buffer-file-name
  1237. (line-number-at-pos)))
  1238. (cl-assert file)
  1239. (cl-assert line)
  1240. (let* ((blame-args '("-w"))
  1241. (id (with-temp-buffer
  1242. (let ((exit (apply 'call-process
  1243. "git" ;; PROGRAM
  1244. nil ;; INFILE
  1245. t ;; DESTINATION
  1246. nil ;; DISPLAY
  1247. "--no-pager" ;; ARGS
  1248. "blame"
  1249. "-L"
  1250. (format "%d,+1" line)
  1251. "--porcelain"
  1252. file
  1253. blame-args
  1254. )))
  1255. (goto-char (point-min))
  1256. (cl-assert (eq exit 0)
  1257. "Failed: %s" (buffer-substring (point)
  1258. (point-at-eol)))
  1259. (save-match-data
  1260. (re-search-forward (rx buffer-start
  1261. (one-or-more hex-digit)))
  1262. (match-string 0))))))
  1263. (magit-show-commit id)))
  1264. (when (boundp 'git-rebase-filename-regexp)
  1265. (add-to-list 'auto-mode-alist
  1266. `(,git-rebase-filename-regexp . text-mode)))
  1267. (when (fboundp 'ggtags-mode)
  1268. (add-hook 'c-mode-common-hook
  1269. 'ggtags-mode)
  1270. (add-hook 'python-mode-hook
  1271. 'ggtags-mode)
  1272. (add-hook 'js-mode-hook
  1273. 'ggtags-mode)
  1274. (add-hook 'scheme-mode-hook
  1275. 'ggtags-mode)
  1276. )
  1277. (when (fboundp 'imenu-list-minor-mode)
  1278. (defvar imenu-list-buffer-name)
  1279. (defun my-imenu-list-toggle ()
  1280. "My 'imenu-list` toggle."
  1281. (interactive)
  1282. (require 'imenu-list)
  1283. (if (eq (window-buffer)
  1284. (get-buffer imenu-list-buffer-name))
  1285. (imenu-list-minor-mode -1)
  1286. (imenu-list-minor-mode 1)))
  1287. ;; (set-variable 'imenu-list-auto-resize t)
  1288. (set-variable 'imenu-list-focus-after-activation t)
  1289. ;; (define-key ctl-x-map (kbd "C-l") 'my-imenu-list-toggle)
  1290. (define-key ctl-x-map (kbd "C-l") 'imenu-list-smart-toggle)
  1291. )
  1292. (add-hook 'emacs-lisp-mode-hook
  1293. (lambda ()
  1294. (setq imenu-generic-expression
  1295. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  1296. ,@imenu-generic-expression))))
  1297. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  1298. (with-eval-after-load 'compile
  1299. (defvar compilation-filter-start)
  1300. (defvar compilation-error-regexp-alist)
  1301. (eval-and-compile (require 'ansi-color))
  1302. (add-hook 'compilation-filter-hook
  1303. (lambda ()
  1304. (let ((inhibit-read-only t))
  1305. (ansi-color-apply-on-region compilation-filter-start
  1306. (point)))))
  1307. (add-to-list 'compilation-error-regexp-alist
  1308. ;; ansible-lint
  1309. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2))
  1310. (add-to-list 'compilation-error-regexp-alist
  1311. ;; pydocstyle
  1312. '("^\\([^ \n]+\\):\\([0-9]+\\) " 1 2))
  1313. )
  1314. (declare-function company-cancel "company")
  1315. (when (fboundp 'global-company-mode)
  1316. (add-hook 'after-first-visit-hook
  1317. 'global-company-mode))
  1318. (declare-function company-manual-begin "company")
  1319. (with-eval-after-load 'company
  1320. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1321. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1322. (set-variable 'company-idle-delay nil)
  1323. (set-variable 'company-minimum-prefix-length 2)
  1324. (set-variable 'company-selection-wrap-around t)
  1325. (defvar company-mode-map)
  1326. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1327. ;; (with-eval-after-load 'python
  1328. ;; (defvar python-indent-trigger-commands)
  1329. ;; ;; TODO: This disables completion in puthon?
  1330. ;; (add-to-list 'python-indent-trigger-commands
  1331. ;; 'company-indent-or-complete-common))
  1332. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1333. (defvar company-active-map)
  1334. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1335. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1336. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1337. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1338. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1339. (defvar company-mode)
  1340. (defvar company-candidates)
  1341. (defvar company-candidates-length)
  1342. ;; (popup-tip "Hello, World!")
  1343. (defun my-company-lighter-current-length ()
  1344. "Get current candidate length."
  1345. (interactive)
  1346. (let ((l nil)
  1347. (inhibit-message t))
  1348. (when (and company-mode
  1349. (not (minibufferp))
  1350. ;; Do nothing when already in company completion
  1351. (not company-candidates))
  1352. ;; FIXME: Somehow it cannto catch errors from ggtags
  1353. (ignore-errors
  1354. ;; (company-auto-begin)
  1355. (company-manual-begin)
  1356. (setq l company-candidates-length)
  1357. (company-cancel)))
  1358. (if l
  1359. (format "[%d]" l)
  1360. "")))
  1361. (defvar company-lighter)
  1362. (set-variable 'company-lighter-base "Cmp")
  1363. ;; (add-to-list 'company-lighter
  1364. ;; '(:eval (my-company-lighter-current-length))
  1365. ;; t)
  1366. ;; This breaks japanese text input
  1367. ;; (set-variable 'my-company-length-popup-tip-timer
  1368. ;; (run-with-idle-timer 0.2 t
  1369. ;; 'my-company-length-popup-tip))
  1370. ;; (current-active-maps)
  1371. ;; (lookup-key)
  1372. '(mapcar (lambda (map)
  1373. (lookup-key map (kbd "C-i")))
  1374. (current-active-maps))
  1375. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1376. (set-face-attribute 'company-tooltip nil
  1377. :foreground "black" :background "lightgrey")
  1378. (set-face-attribute 'company-tooltip-common nil
  1379. :foreground "black" :background "lightgrey")
  1380. (set-face-attribute 'company-tooltip-common-selection nil
  1381. :foreground "white" :background "steelblue")
  1382. (set-face-attribute 'company-tooltip-selection nil
  1383. :foreground "black" :background "steelblue")
  1384. (set-face-attribute 'company-preview-common nil
  1385. :background nil :foreground "lightgrey" :underline t)
  1386. (set-face-attribute 'company-scrollbar-fg nil
  1387. :background "orange")
  1388. (set-face-attribute 'company-scrollbar-bg nil
  1389. :background "gray40")
  1390. )
  1391. ;; https://github.com/lunaryorn/flycheck
  1392. ;; TODO: Any way to disable auto check?
  1393. ;; Update flycheck-hooks-alist?
  1394. (when (fboundp 'global-flycheck-mode)
  1395. (add-hook 'after-first-visit-hook
  1396. 'global-flycheck-mode))
  1397. ;; (set-variable 'flycheck-display-errors-delay 2.0)
  1398. ;; (fset 'flycheck-display-error-at-point-soon 'ignore)
  1399. ;; (with-eval-after-load 'flycheck
  1400. ;; (when (fboundp 'flycheck-black-check-setup)
  1401. ;; (flycheck-black-check-setup)))
  1402. (when (fboundp 'ilookup-open-word)
  1403. (define-key ctl-x-map "d" 'ilookup-open-word))
  1404. (set-variable 'ac-ignore-case nil)
  1405. (when (fboundp 'term-run-shell-command)
  1406. (define-key ctl-x-map "t" 'term-run-shell-command))
  1407. (add-to-list 'safe-local-variable-values
  1408. '(encoding utf-8))
  1409. (setq enable-local-variables :safe)
  1410. ;; Detect file type from shebang and set major-mode.
  1411. (add-to-list 'interpreter-mode-alist
  1412. '("python3" . python-mode))
  1413. (add-to-list 'interpreter-mode-alist
  1414. '("python2" . python-mode))
  1415. (with-eval-after-load 'python
  1416. (defvar python-mode-map (make-sparse-keymap))
  1417. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1418. ;; I want to use this, but this breaks normal self-insert-command
  1419. ;; (set-variable 'py-indent-list-style
  1420. ;; 'one-level-to-beginning-of-statement)
  1421. (set-variable 'pydoc-command
  1422. "python3 -m pydoc")
  1423. (declare-function with-venv-advice-add "with-venv")
  1424. (with-eval-after-load 'pydoc
  1425. (when (require 'with-venv nil t)
  1426. (with-venv-advice-add 'pydoc)))
  1427. (set-variable 'flycheck-python-mypy-ini ".mypy.ini")
  1428. (set-variable 'flycheck-flake8rc "setup.cfg")
  1429. (set-variable 'flycheck-python-pylint-executable "python3")
  1430. (set-variable 'flycheck-python-pycompile-executable "python3")
  1431. (set-variable 'python-indent-guess-indent-offset nil)
  1432. (with-eval-after-load 'blacken
  1433. (when (require 'with-venv nil t)
  1434. (with-venv-advice-add 'blacken-buffer)))
  1435. (with-eval-after-load 'ansible-doc
  1436. (when (require 'with-venv nil t)
  1437. (with-venv-advice-add 'ansible-doc)))
  1438. ;; `isortify-buffer' breaks buffer when it contains japanese text
  1439. (defun my-isortify ()
  1440. (interactive)
  1441. (cl-assert buffer-file-name)
  1442. (cl-assert (not (buffer-modified-p)))
  1443. (call-process "python" ;; PROGRAM
  1444. nil ;; INFILE
  1445. nil ;; DESTINATION
  1446. nil ;; DISPLAY
  1447. "-m" "isort" buffer-file-name)
  1448. (message "isortify done")
  1449. (revert-buffer nil t))
  1450. (when (fboundp 'with-venv-advice-add)
  1451. ;; TODO: Lazy load with-venv
  1452. (with-venv-advice-add 'my-isortify))
  1453. ;; https://github.com/lunaryorn/old-emacs-configuration/blob/master/lisp/flycheck-virtualenv.el
  1454. (defun my-set-venv-flycheck-executable-find ()
  1455. "Set flycheck executabie find."
  1456. (interactive)
  1457. (when (fboundp 'with-venv)
  1458. (set-variable 'flycheck-executable-find
  1459. '(lambda (e)
  1460. (with-venv
  1461. (executable-find e)))
  1462. t)))
  1463. (defun my-update-flycheck-flake8-error-level-alist ()
  1464. "Update `flycheck-flake8-error-level-alist'."
  1465. (defvar flycheck-flake8-error-level-alist)
  1466. ;; (add-to-list 'flycheck-flake8-error-level-alist
  1467. ;; '("^D.*$" . warning))
  1468. (set-variable 'flycheck-flake8-error-level-alist
  1469. nil)
  1470. )
  1471. (add-hook 'python-mode-hook
  1472. 'my-set-venv-flycheck-executable-find)
  1473. (add-hook 'python-mode-hook
  1474. 'my-update-flycheck-flake8-error-level-alist)
  1475. (when (fboundp 'with-venv-info-mode)
  1476. (add-hook 'python-mode-hook
  1477. 'with-venv-info-mode))
  1478. ;; Run multiple chekcers
  1479. ;; https://github.com/flycheck/flycheck/issues/186
  1480. (add-hook 'python-mode-hook
  1481. (lambda ()
  1482. ;; Currently on python-mode eldoc-mode sometimes print
  1483. ;; wired message on "from" keyword:
  1484. ;;var from = require("./from")
  1485. (eldoc-mode -1)))
  1486. ;; http://fukuyama.co/foreign-regexp
  1487. '(and (require 'foreign-regexp nil t)
  1488. (progn
  1489. (setq foreign-regexp/regexp-type 'perl)
  1490. '(setq reb-re-syntax 'foreign-regexp)
  1491. ))
  1492. (with-eval-after-load 'sql
  1493. (require 'sql-indent nil t))
  1494. (add-to-list 'auto-mode-alist
  1495. '("\\.hql\\'" . sql-mode))
  1496. (when (fboundp 'git-command)
  1497. (define-key ctl-x-map "g" 'git-command))
  1498. (when (fboundp 'gited-list)
  1499. (defalias 'gited 'gited-list))
  1500. (when (fboundp 'global-git-commit-mode)
  1501. (add-hook 'after-first-visit-hook
  1502. 'global-git-commit-mode)
  1503. (add-hook 'after-first-visit-hook
  1504. 'git-commit-setup-check-buffer))
  1505. (with-eval-after-load 'git-commit
  1506. (add-hook 'git-commit-setup-hook
  1507. 'turn-off-auto-fill t))
  1508. (with-eval-after-load 'rst
  1509. (defvar rst-mode-map)
  1510. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1511. (with-eval-after-load 'jdee
  1512. (add-hook 'jdee-mode-hook
  1513. (lambda ()
  1514. (make-local-variable 'global-mode-string)
  1515. (add-to-list 'global-mode-string
  1516. mode-line-position))))
  1517. ;; Cannot enable error thrown. Why???
  1518. ;; https://github.com/m0smith/malabar-mode#Installation
  1519. ;; (when (require 'malabar-mode nil t)
  1520. ;; (add-to-list 'load-path
  1521. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1522. ;; (require 'cedet-devel-load nil t)
  1523. ;; (eval-after-init (activate-malabar-mode)))
  1524. (with-eval-after-load 'make-mode
  1525. (defvar makefile-mode-map (make-sparse-keymap))
  1526. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1527. ;; this functions is set in write-file-functions, i cannot find any
  1528. ;; good way to remove this.
  1529. (fset 'makefile-warn-suspicious-lines 'ignore))
  1530. (with-eval-after-load 'verilog-mode
  1531. (defvar verilog-mode-map (make-sparse-keymap))
  1532. (define-key verilog-mode-map ";" 'self-insert-command))
  1533. (setq diff-switches "-u")
  1534. (autoload 'diff-goto-source "diff-mode" nil t)
  1535. (with-eval-after-load 'diff-mode
  1536. ;; (when (and (eq major-mode
  1537. ;; 'diff-mode)
  1538. ;; (not buffer-file-name))
  1539. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1540. ;; (view-mode 1))
  1541. (set-face-attribute 'diff-header nil
  1542. :foreground nil
  1543. :background nil
  1544. :weight 'bold)
  1545. (set-face-attribute 'diff-file-header nil
  1546. :foreground nil
  1547. :background nil
  1548. :weight 'bold)
  1549. (set-face-foreground 'diff-index "blue")
  1550. (set-face-attribute 'diff-hunk-header nil
  1551. :foreground "cyan"
  1552. :weight 'normal)
  1553. (set-face-attribute 'diff-context nil
  1554. ;; :foreground "white"
  1555. :foreground nil
  1556. :weight 'normal)
  1557. (set-face-foreground 'diff-removed "red")
  1558. (set-face-foreground 'diff-added "green")
  1559. (set-face-background 'diff-removed nil)
  1560. (set-face-background 'diff-added nil)
  1561. (set-face-attribute 'diff-changed nil
  1562. :foreground "magenta"
  1563. :weight 'normal)
  1564. (set-face-attribute 'diff-refine-changed nil
  1565. :foreground nil
  1566. :background nil
  1567. :weight 'bold
  1568. :inverse-video t)
  1569. ;; Annoying !
  1570. ;;(diff-auto-refine-mode)
  1571. )
  1572. ;; (ffap-bindings)
  1573. (set-variable 'browse-url-browser-function
  1574. 'eww-browse-url)
  1575. (set-variable 'sh-here-document-word "__EOC__")
  1576. (with-eval-after-load 'adoc-mode
  1577. (defvar adoc-mode-map)
  1578. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1579. (when (fboundp 'adoc-mode)
  1580. (setq auto-mode-alist
  1581. `(("\\.adoc\\'" . adoc-mode)
  1582. ("\\.asciidoc\\'" . adoc-mode)
  1583. ,@auto-mode-alist)))
  1584. (with-eval-after-load 'markup-faces
  1585. ;; Is this too match ?
  1586. (set-face-foreground 'markup-meta-face
  1587. "color-245")
  1588. (set-face-foreground 'markup-meta-hide-face
  1589. "color-245")
  1590. )
  1591. ;; TODO: check if this is required
  1592. (with-eval-after-load 'groovy-mode
  1593. (defvar groovy-mode-map)
  1594. (define-key groovy-mode-map "(" 'self-insert-command)
  1595. (define-key groovy-mode-map ")" 'self-insert-command)
  1596. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1597. )
  1598. (when (fboundp 'groovy-mode)
  1599. (add-to-list 'auto-mode-alist
  1600. '("build\\.gradle\\'" . groovy-mode)))
  1601. (add-to-list 'auto-mode-alist
  1602. '("\\.gawk\\'" . awk-mode))
  1603. (with-eval-after-load 'yaml-mode
  1604. (defvar yaml-mode-map (make-sparse-keymap))
  1605. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1606. (with-eval-after-load 'html-mode
  1607. (defvar html-mode-map (make-sparse-keymap))
  1608. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1609. (with-eval-after-load 'text-mode
  1610. (define-key text-mode-map (kbd "C-m") 'newline))
  1611. (with-eval-after-load 'info
  1612. (defvar Info-additional-directory-list)
  1613. (dolist (dir (directory-files (concat user-emacs-directory
  1614. "info")
  1615. t
  1616. "^[^.].*"))
  1617. (when (file-directory-p dir)
  1618. (add-to-list 'Info-additional-directory-list
  1619. dir)))
  1620. (let ((dir (expand-file-name "~/.brew/share/info")))
  1621. (when (file-directory-p dir)
  1622. (add-to-list 'Info-additional-directory-list
  1623. dir))))
  1624. (with-eval-after-load 'apropos
  1625. (defvar apropos-mode-map (make-sparse-keymap))
  1626. (define-key apropos-mode-map "n" 'next-line)
  1627. (define-key apropos-mode-map "p" 'previous-line))
  1628. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1629. ;; (define-key isearch-mode-map
  1630. ;; (kbd "C-j") 'isearch-other-control-char)
  1631. ;; (define-key isearch-mode-map
  1632. ;; (kbd "C-k") 'isearch-other-control-char)
  1633. ;; (define-key isearch-mode-map
  1634. ;; (kbd "C-h") 'isearch-other-control-char)
  1635. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1636. (define-key isearch-mode-map (kbd "M-r")
  1637. 'isearch-query-replace-regexp)
  1638. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1639. (setq lazy-highlight-cleanup nil)
  1640. ;; face for isearch highlighing
  1641. (set-face-attribute 'lazy-highlight
  1642. nil
  1643. :foreground `unspecified
  1644. :background `unspecified
  1645. :underline t
  1646. ;; :weight `bold
  1647. )
  1648. (add-hook 'outline-mode-hook
  1649. (lambda ()
  1650. (when (string-match "\\.md\\'" buffer-file-name)
  1651. (setq-local outline-regexp "#+ "))))
  1652. (add-hook 'outline-mode-hook
  1653. 'outline-show-all)
  1654. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1655. (with-eval-after-load 'markdown-mode
  1656. (defvar gfm-mode-map)
  1657. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1658. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1659. )
  1660. (when (fboundp 'gfm-mode)
  1661. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1662. (add-hook 'markdown-mode-hook
  1663. 'outline-minor-mode)
  1664. (add-hook 'markdown-mode-hook
  1665. (lambda ()
  1666. (setq-local comment-start ";")))
  1667. )
  1668. ;; http://keisanbutsuriya.hateblo.jp/entry/2015/02/10/152543
  1669. ;; M-$ to ispell word
  1670. ;; M-x flyspell-buffer to highlight all suspicious words
  1671. (when (executable-find "aspell")
  1672. (set-variable 'ispell-program-name "aspell")
  1673. (set-variable 'ispell-extra-args '("--lang=en_US")))
  1674. (with-eval-after-load 'ispell
  1675. (add-to-list 'ispell-skip-region-alist '("[^\000-\377]+")))
  1676. (when (fboundp 'flyspell-mode)
  1677. (add-hook 'text-mode-hook
  1678. 'flyspell-mode))
  1679. (when (fboundp 'flyspell-prog-mode)
  1680. (add-hook 'prog-mode-hook
  1681. 'flyspell-prog-mode))
  1682. ;; c-mode
  1683. ;; http://www.emacswiki.org/emacs/IndentingC
  1684. ;; http://en.wikipedia.org/wiki/Indent_style
  1685. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1686. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1687. (with-eval-after-load 'cc-vars
  1688. (defvar c-default-style nil)
  1689. (add-to-list 'c-default-style
  1690. '(c-mode . "k&r"))
  1691. (add-to-list 'c-default-style
  1692. '(c++-mode . "k&r")))
  1693. (with-eval-after-load 'js2-mode
  1694. ;; currently do not use js2-mode
  1695. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1696. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1697. ;; (defvar js2-mode-map (make-sparse-keymap))
  1698. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1699. ;; (interactive)
  1700. ;; (js2-enter-key)
  1701. ;; (indent-for-tab-command)))
  1702. ;; (add-hook (kill-local-variable 'before-save-hook)
  1703. ;; 'js2-before-save)
  1704. ;; (add-hook 'before-save-hook
  1705. ;; 'my-indent-buffer
  1706. ;; nil
  1707. ;; t)
  1708. )
  1709. (add-to-list 'interpreter-mode-alist
  1710. '("node" . js-mode))
  1711. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1712. (with-eval-after-load 'uniquify
  1713. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1714. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1715. (setq uniquify-min-dir-content 1))
  1716. (with-eval-after-load 'view
  1717. (defvar view-mode-map (make-sparse-keymap))
  1718. (define-key view-mode-map "j" 'scroll-up-line)
  1719. (define-key view-mode-map "k" 'scroll-down-line)
  1720. (define-key view-mode-map "v" 'toggle-read-only)
  1721. (define-key view-mode-map "q" 'bury-buffer)
  1722. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1723. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1724. ;; (define-key view-mode-map
  1725. ;; "n" 'nonincremental-repeat-search-forward)
  1726. ;; (define-key view-mode-map
  1727. ;; "N" 'nonincremental-repeat-search-backward)
  1728. ;; N conflicts with git-walktree
  1729. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1730. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1731. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1732. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1733. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1734. (global-set-key "\M-r" 'view-mode)
  1735. ;; (setq view-read-only t)
  1736. (with-eval-after-load 'term
  1737. (defvar term-raw-map (make-sparse-keymap))
  1738. (define-key term-raw-map (kbd "C-x")
  1739. (lookup-key (current-global-map)
  1740. (kbd "C-x"))))
  1741. (add-hook 'term-mode-hook
  1742. (lambda ()
  1743. ;; Stop current line highlighting
  1744. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1745. (set-variable 'scroll-margin 0 t)
  1746. ))
  1747. (set-variable 'term-buffer-maximum-size 20480)
  1748. (set-variable 'term-suppress-hard-newline t)
  1749. (add-hook 'Man-mode-hook
  1750. (lambda ()
  1751. (view-mode 1)
  1752. (setq truncate-lines nil)))
  1753. (set-variable 'Man-notify-method (if window-system
  1754. 'newframe
  1755. 'aggressive))
  1756. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1757. "woman_cache.el")))
  1758. ;; not work because man.el will be loaded when man called
  1759. (defalias 'man 'woman)
  1760. (add-to-list 'auto-mode-alist
  1761. '("tox\\.ini\\'" . conf-unix-mode))
  1762. (when (fboundp 'toml-mode)
  1763. (add-to-list 'auto-mode-alist
  1764. '("/tox\\.ini\\'" . toml-mode))
  1765. (add-to-list 'auto-mode-alist
  1766. '("/Pipfile\\'" . toml-mode))
  1767. (add-to-list 'auto-mode-alist
  1768. '("/poetry\\.lock\\'" . toml-mode))
  1769. )
  1770. (when (fboundp 'json-mode)
  1771. (add-to-list 'auto-mode-alist
  1772. '("/Pipfile\\.lock\\'" . json-mode)))
  1773. (add-hook 'go-mode-hook
  1774. (lambda()
  1775. (defvar go-mode-map)
  1776. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1777. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1778. (when (fboundp 'k8s-mode)
  1779. (add-to-list 'auto-mode-alist
  1780. '("\\.k8s\\'" . k8s-mode)))
  1781. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1782. ;; buffers
  1783. (defvar bs-configurations)
  1784. (declare-function bs-set-configuration "bs")
  1785. (declare-function bs-refresh "bs")
  1786. (declare-function bs-message-without-log "bs")
  1787. (declare-function bs--current-config-message "bs")
  1788. (with-eval-after-load 'bs
  1789. (add-to-list 'bs-configurations
  1790. '("specials" "^\\*" nil ".*" nil nil))
  1791. (add-to-list 'bs-configurations
  1792. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1793. (defvar bs-mode-map)
  1794. (defvar bs-current-configuration)
  1795. (define-key bs-mode-map (kbd "t")
  1796. ;; TODO: fix toggle feature
  1797. (lambda ()
  1798. (interactive)
  1799. (if (string= "specials"
  1800. bs-current-configuration)
  1801. (bs-set-configuration "files")
  1802. (bs-set-configuration "specials"))
  1803. (bs-refresh)
  1804. (bs-message-without-log "%s"
  1805. (bs--current-config-message))))
  1806. ;; (setq bs-configurations (list
  1807. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1808. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1809. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1810. )
  1811. (when (fboundp 'bs-show)
  1812. (defalias 'list-buffers 'bs-show)
  1813. (set-variable 'bs-default-configuration "files-and-specials")
  1814. (set-variable 'bs-default-sort-name "by nothing")
  1815. (add-hook 'bs-mode-hook
  1816. (lambda ()
  1817. (setq-local scroll-margin 0)
  1818. (setq-local header-line-format nil)
  1819. (setq-local mode-line-format nil)
  1820. )))
  1821. ;;(iswitchb-mode 1)
  1822. (icomplete-mode)
  1823. (defun iswitchb-buffer-display-other-window ()
  1824. "Do iswitchb in other window."
  1825. (interactive)
  1826. (let ((iswitchb-default-method 'display))
  1827. (call-interactively 'iswitchb-buffer)))
  1828. ;; buffer killing
  1829. ;; (defun my-delete-window-killing-buffer () nil)
  1830. (defun my-query-kill-current-buffer ()
  1831. "Interactively kill current buffer."
  1832. (interactive)
  1833. (if (y-or-n-p (concat "kill current buffer? :"))
  1834. (kill-buffer (current-buffer))))
  1835. (defun my-force-query-kill-current-buffer ()
  1836. "Interactively kill current buffer."
  1837. (interactive)
  1838. (when (y-or-n-p (concat "kill current buffer? :"))
  1839. (let ((kill-buffer-hook nil)
  1840. (kill-buffer-query-functions nil))
  1841. (kill-buffer (current-buffer)))))
  1842. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1843. ;; Originally C-x C-k -> kmacro-keymap
  1844. ;; (global-set-key "\C-x\C-k" 'kmacro-keymap)
  1845. (global-set-key (kbd "C-x C-k") 'my-query-kill-current-buffer)
  1846. (substitute-key-definition 'kill-buffer
  1847. 'my-query-kill-current-buffer
  1848. global-map)
  1849. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1850. ;; dired
  1851. (defun my-file-head (filename &optional n)
  1852. "Return list of first N lines of file FILENAME."
  1853. ;; TODO: Fix for janapese text
  1854. ;; TODO: Fix for short text
  1855. (let ((num (or n 10))
  1856. (size 100)
  1857. (beg 0)
  1858. (end 0)
  1859. (result '())
  1860. (read -1))
  1861. (with-temp-buffer
  1862. (erase-buffer)
  1863. (while (or (<= (count-lines (point-min)
  1864. (point-max))
  1865. num)
  1866. (eq read 0))
  1867. (setq end (+ beg size))
  1868. (setq read (nth 1 (insert-file-contents-literally filename
  1869. nil
  1870. beg
  1871. end)))
  1872. (goto-char (point-max))
  1873. (setq beg (+ beg size)))
  1874. (goto-char (point-min))
  1875. (while (< (length result) num)
  1876. (let ((start (point)))
  1877. (forward-line 1)
  1878. (setq result
  1879. `(,@result ,(buffer-substring-no-properties start
  1880. (point))))))
  1881. result
  1882. ;; (buffer-substring-no-properties (point-min)
  1883. ;; (progn
  1884. ;; (forward-line num)
  1885. ;; (point)))
  1886. )))
  1887. ;; (apply 'concat (my-file-head "./shrc" 10)
  1888. (declare-function dired-get-filename "dired" t)
  1889. (defun my-dired-echo-file-head (arg)
  1890. "Echo head of current file.
  1891. ARG is num to show, or defaults to 7."
  1892. (interactive "P")
  1893. (let ((f (dired-get-filename)))
  1894. (message "%s"
  1895. (apply 'concat
  1896. (my-file-head f
  1897. 7)))))
  1898. (declare-function dired-get-marked-files "dired")
  1899. (defun my-dired-diff ()
  1900. "Show diff of marked file and file of current line."
  1901. (interactive)
  1902. (let ((files (dired-get-marked-files nil nil nil t)))
  1903. (if (eq (car files)
  1904. t)
  1905. (diff (cadr files) (dired-get-filename))
  1906. (message "One file must be marked!"))))
  1907. (defun dired-get-file-info ()
  1908. "Print information of current line file."
  1909. (interactive)
  1910. (let* ((file (dired-get-filename t))
  1911. (quoted (shell-quote-argument file)))
  1912. (if (file-directory-p file)
  1913. (progn
  1914. (message "Calculating disk usage...")
  1915. (let ((du (or (executable-find "gdu")
  1916. (executable-find "du")
  1917. (error "du not found"))))
  1918. (shell-command (concat du
  1919. " -hsD "
  1920. quoted))))
  1921. (shell-command (concat "file "
  1922. quoted)))))
  1923. (defun my-dired-scroll-up ()
  1924. "Scroll up."
  1925. (interactive)
  1926. (my-dired-previous-line (- (window-height) 1)))
  1927. (defun my-dired-scroll-down ()
  1928. "Scroll down."
  1929. (interactive)
  1930. (my-dired-next-line (- (window-height) 1)))
  1931. ;; (defun my-dired-forward-line (arg)
  1932. ;; ""
  1933. ;; (interactive "p"))
  1934. (declare-function dired-get-subdir "dired")
  1935. (declare-function dired-move-to-filename "dired")
  1936. (defun my-dired-previous-line (arg)
  1937. "Move ARG lines up."
  1938. (interactive "p")
  1939. (if (> arg 0)
  1940. (progn
  1941. (if (eq (line-number-at-pos)
  1942. 1)
  1943. (goto-char (point-max))
  1944. (forward-line -1))
  1945. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1946. (dired-get-subdir))
  1947. (- arg 1)
  1948. arg)))
  1949. (dired-move-to-filename)))
  1950. (defun my-dired-next-line (arg)
  1951. "Move ARG lines down."
  1952. (interactive "p")
  1953. (if (> arg 0)
  1954. (progn
  1955. (if (eq (point)
  1956. (point-max))
  1957. (goto-char (point-min))
  1958. (forward-line 1))
  1959. (my-dired-next-line (if (or (dired-get-filename nil t)
  1960. (dired-get-subdir))
  1961. (- arg 1)
  1962. arg)))
  1963. (dired-move-to-filename)))
  1964. (defun my-tramp-remote-find-file (f)
  1965. "Open F."
  1966. (interactive (list (read-file-name "My Find File Tramp: "
  1967. "/scp:"
  1968. nil ;; "/scp:"
  1969. (confirm-nonexistent-file-or-buffer))))
  1970. (find-file f))
  1971. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1972. (if (eq window-system 'mac)
  1973. (setq dired-listing-switches "-lhF")
  1974. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1975. )
  1976. (setq dired-listing-switches "-lhF")
  1977. ;; when using dired-find-alternate-file
  1978. ;; reuse current dired buffer for the file to open
  1979. ;; (put 'dired-find-alternate-file 'disabled nil)
  1980. (set-variable 'dired-ls-F-marks-symlinks t)
  1981. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1982. (set-variable 'ls-lisp-dirs-first t)
  1983. (set-variable 'ls-lisp-use-localized-time-format t)
  1984. (set-variable 'ls-lisp-format-time-list
  1985. '("%Y-%m-%d %H:%M"
  1986. "%Y-%m-%d "))
  1987. (set-variable 'dired-dwim-target t)
  1988. (set-variable 'dired-isearch-filenames t)
  1989. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1990. (set-variable 'dired-hide-details-hide-information-lines nil)
  1991. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1992. (set-variable 'dired-recursive-deletes 'always)
  1993. ;; (add-hook 'dired-after-readin-hook
  1994. ;; 'my-replace-nasi-none)
  1995. (with-eval-after-load 'dired
  1996. (require 'ls-lisp nil t)
  1997. (defvar dired-mode-map (make-sparse-keymap))
  1998. ;; dired-do-chgrp sometimes cause system hung
  1999. (define-key dired-mode-map "G" 'ignore)
  2000. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  2001. (define-key dired-mode-map "i" 'dired-get-file-info)
  2002. ;; (define-key dired-mode-map "f" 'find-file)
  2003. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  2004. (define-key dired-mode-map "z" 'fzf)
  2005. (define-key dired-mode-map "!" 'shell-command)
  2006. (define-key dired-mode-map "&" 'async-shell-command)
  2007. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  2008. (define-key dired-mode-map "=" 'my-dired-diff)
  2009. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  2010. (define-key dired-mode-map "b" 'gtkbm)
  2011. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  2012. (define-key dired-mode-map (kbd "TAB") 'other-window)
  2013. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  2014. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  2015. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  2016. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  2017. (substitute-key-definition 'dired-next-line
  2018. 'my-dired-next-line
  2019. dired-mode-map)
  2020. (substitute-key-definition 'dired-previous-line
  2021. 'my-dired-previous-line
  2022. dired-mode-map)
  2023. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  2024. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  2025. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  2026. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  2027. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  2028. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  2029. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  2030. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  2031. (add-hook 'dired-mode-hook
  2032. (lambda ()
  2033. (when (fboundp 'dired-hide-details-mode)
  2034. (dired-hide-details-mode t)
  2035. (local-set-key "l" 'dired-hide-details-mode))
  2036. (let ((file "._Icon\015"))
  2037. (when nil
  2038. '(file-readable-p file)
  2039. (delete-file file)))))
  2040. (when (fboundp 'pack-dired-dwim)
  2041. (with-eval-after-load 'dired
  2042. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  2043. (when (fboundp 'dired-list-all-mode)
  2044. (setq dired-listing-switches "-lhF")
  2045. (with-eval-after-load 'dired
  2046. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  2047. (when (fboundp 'dired-filter-mode)
  2048. (add-hook 'dired-mode-hook
  2049. 'dired-filter-mode))
  2050. (set-variable 'dired-filter-stack nil)
  2051. ;; Currently disabled in favor of dired-from-git-ls-files
  2052. ;; (define-key ctl-x-map "f" 'find-dired)
  2053. (defvar my-dired-git-ls-files-history
  2054. "History for `my-dired-git-ls-files'." nil)
  2055. (defun my-dired-git-ls-files (arg)
  2056. "Dired from git ls-files."
  2057. (interactive (list
  2058. (read-shell-command "git ls-files: "
  2059. "git ls-files -z ")))
  2060. (pop-to-buffer-same-window
  2061. (dired-noselect `(,default-directory
  2062. ,@(split-string (shell-command-to-string arg)
  2063. "\0" t))
  2064. ""))
  2065. )
  2066. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  2067. (with-eval-after-load 'dired
  2068. (defvar dired-mode-map (make-sparse-keymap))
  2069. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  2070. (with-eval-after-load 'pack
  2071. (set-variable 'pack-silence
  2072. t)
  2073. (defvar pack-program-alist)
  2074. (add-to-list 'pack-program-alist
  2075. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf"))
  2076. (when (executable-find "aunpack")
  2077. (add-to-list 'pack-program-alist
  2078. ' ("\\.zip\\'"
  2079. :pack ("zip" "-r" archive sources)
  2080. :pack-append ("zip" "-r" archive sources)
  2081. :unpack ("aunpack" archive))))
  2082. )
  2083. ;; dired-k
  2084. ;; Current HEAD of original repo is broken
  2085. ;; https://github.com/syohex/emacs-dired-k/issues/45
  2086. (declare-function dired-k-no-revert "dired-k")
  2087. (when (fboundp 'dired-k)
  2088. (set-variable 'dired-k-style 'git)
  2089. ;; What is the best way of doing this?
  2090. (with-eval-after-load 'dired-k
  2091. (fset 'dired-k--highlight-by-file-attribyte 'ignore))
  2092. ;; (set-variable 'dired-k-size-colors
  2093. ;; `((,most-positive-fixnum)))
  2094. ;; (set-variable 'dired-k-date-colors
  2095. ;; `((,most-positive-fixnum)))
  2096. (let* ((pkg (cadr (assq 'dired-k
  2097. package-alist)))
  2098. (version-str (package-version-join (package-desc-version pkg))))
  2099. ;; Currently dired-k HEAD of original repository is broken so do not use that
  2100. (when (string= version-str
  2101. "20171017.1228")
  2102. ;; always execute dired-k when dired buffer is opened and reverted
  2103. (add-hook 'dired-after-readin-hook #'dired-k-no-revert)
  2104. ;; This still create index.lock files...........................
  2105. ;; (add-hook 'switch-buffer-functions
  2106. ;; (lambda (prev cur)
  2107. ;; (when (derived-mode-p 'dired-mode)
  2108. ;; (dired-k-no-revert))))
  2109. )))
  2110. ;; (when (eval-and-compile (require 'dired-rainbow nil t))
  2111. ;; (dired-rainbow-define gtags "brightblack" "GTAGS"))
  2112. ;; ?
  2113. ;; (set-variable 'dired-omit-files
  2114. ;; (rx (or (regexp "^\\.?#")
  2115. ;; (regexp "^\\.$")
  2116. ;; (regexp "^\\.\\.$")
  2117. ;; (regexp "^GPATH$")
  2118. ;; (regexp "^GRTAGS$")
  2119. ;; (regexp "^GTAGS$")
  2120. ;; )))
  2121. (with-eval-after-load 'diredfl
  2122. (set-face-foreground 'diredfl-file-name nil)
  2123. )
  2124. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2125. ;; misc funcs
  2126. (define-key ctl-x-map "T" 'git-worktree)
  2127. (define-key ctl-x-map "W" 'git-walktree)
  2128. (when (fboundp 'browse-url-default-macosx-browser)
  2129. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  2130. (defalias 'qcalc 'quick-calc)
  2131. (defun memo (&optional dir)
  2132. "Open memo.txt in DIR."
  2133. (interactive)
  2134. (pop-to-buffer (find-file-noselect (concat (if dir
  2135. (file-name-as-directory dir)
  2136. "")
  2137. "memo.txt"))))
  2138. ;; TODO: remember-projectile
  2139. (set (defvar my-privnotes-path nil
  2140. "My privnotes repository path.")
  2141. (expand-file-name "~/my/privnotes"))
  2142. (defun my-privnotes-readme (dir)
  2143. "Open my privnotes DIR."
  2144. (interactive (list
  2145. (read-file-name "Privnotes: "
  2146. (expand-file-name (format-time-string "%Y%m%d_")
  2147. my-privnotes-path))))
  2148. (let ((path (expand-file-name "README.md" dir)))
  2149. (with-current-buffer (find-file path)
  2150. (unless (file-exists-p path)
  2151. (insert (file-name-base dir)
  2152. "\n"
  2153. "=======\n"
  2154. "\n\n")))))
  2155. (define-key ctl-x-map "p" 'my-privnotes-readme)
  2156. (set (defvar my-rgrep-alist nil
  2157. "Alist of rgrep command.
  2158. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2159. condition to choose COMMAND when evaluated.")
  2160. `(
  2161. ;; ripgrep
  2162. ("rg"
  2163. (executable-find "rg")
  2164. "rg -nH --no-heading --hidden --glob '!.git/' --smart-case -M 1280 ")
  2165. ;; git grep
  2166. ("gitgrep"
  2167. (eq 0
  2168. (shell-command "git rev-parse --git-dir"))
  2169. "git --no-pager grep -nH -e ")
  2170. ;; sift
  2171. ("sift"
  2172. (executable-find "sift")
  2173. ("sift --binary-skip --filename --line-number --git --smart-case "))
  2174. ;; the silver searcher
  2175. ("ag"
  2176. (executable-find "ag")
  2177. "ag --nogroup --nopager --filename ")
  2178. ;; ack
  2179. ("ack"
  2180. (executable-find "ack")
  2181. "ack --nogroup --nopager --with-filename ")
  2182. ;; gnu global
  2183. ("global"
  2184. (and (require 'ggtags nil t)
  2185. (executable-find "global")
  2186. (ggtags-current-project-root))
  2187. "global --result grep ")
  2188. ;; grep
  2189. ("grep"
  2190. t
  2191. ,(concat "find . "
  2192. "-path '*/.git' -prune -o "
  2193. "-path '*/.svn' -prune -o "
  2194. "-type f -print0 | "
  2195. "xargs -0 grep -nH -e "))
  2196. )
  2197. )
  2198. (defvar my-rgrep-default nil
  2199. "Default command name for my-rgrep.")
  2200. (defun my-rgrep-grep-command (&optional name alist)
  2201. "Return recursive grep command for current directory or nil.
  2202. If NAME is given, use that without testing.
  2203. Commands are searched from ALIST."
  2204. (if alist
  2205. (if name
  2206. ;; if name is given search that from alist and return the command
  2207. (nth 2 (assoc name
  2208. alist))
  2209. ;; if name is not given try test in 1th elem
  2210. (let ((car (car alist))
  2211. (cdr (cdr alist)))
  2212. (if (eval (nth 1 car))
  2213. ;; if the condition is true return the command
  2214. (nth 2 car)
  2215. ;; try next one
  2216. (and cdr
  2217. (my-rgrep-grep-command name cdr)))))
  2218. ;; if alist is not given set default value
  2219. (my-rgrep-grep-command name my-rgrep-alist)))
  2220. (declare-function projectile-project-p "projectile")
  2221. (declare-function projectile-with-default-dir "projectile")
  2222. (declare-function projectile-project-root "projectile")
  2223. (defun my-rgrep (command-args)
  2224. "My recursive grep. Run COMMAND-ARGS.
  2225. If prefix argument is given, use current symbol as default search target
  2226. and search from projectile root (if projectile is available)."
  2227. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2228. nil)))
  2229. (if cmd
  2230. (list (read-shell-command "grep command: "
  2231. (concat cmd
  2232. (if current-prefix-arg
  2233. (thing-at-point 'symbol t)
  2234. ""))
  2235. 'grep-find-history))
  2236. (error "My-Rgrep: Command for rgrep not found")
  2237. )))
  2238. (if (and current-prefix-arg
  2239. (eval-and-compile (require 'projectile nil t))
  2240. (projectile-project-p))
  2241. (projectile-with-default-dir (projectile-project-root)
  2242. (compilation-start command-args
  2243. 'grep-mode))
  2244. (compilation-start command-args
  2245. 'grep-mode)))
  2246. (defun my-rgrep-thing-at-point-projectile-root ()
  2247. "My recursive grep to find thing at point from project root."
  2248. (interactive)
  2249. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  2250. nil))
  2251. (command-args
  2252. (if cmd
  2253. (concat cmd
  2254. (or (thing-at-point 'symbol t)
  2255. (error "No symbol at point")))
  2256. (error "My-Rgrep: Command for rgrep not found"))))
  2257. (if (eval-and-compile (require 'projectile nil t))
  2258. (projectile-with-default-dir (or (projectile-project-root)
  2259. default-directory)
  2260. (compilation-start command-args
  2261. 'grep-mode))
  2262. (compilation-start command-args
  2263. 'grep-mode))))
  2264. (defmacro define-my-rgrep (name)
  2265. "Define rgrep for NAME."
  2266. `(defun ,(intern (concat "my-rgrep-"
  2267. name)) ()
  2268. ,(format "My recursive grep by %s."
  2269. name)
  2270. (interactive)
  2271. (let ((my-rgrep-default ,name))
  2272. (if (called-interactively-p 'any)
  2273. (call-interactively 'my-rgrep)
  2274. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2275. )
  2276. (define-my-rgrep "ack")
  2277. (define-my-rgrep "ag")
  2278. (define-my-rgrep "rg")
  2279. (define-my-rgrep "sift")
  2280. (define-my-rgrep "gitgrep")
  2281. (define-my-rgrep "grep")
  2282. (define-my-rgrep "global")
  2283. (define-key ctl-x-map "s" 'my-rgrep)
  2284. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  2285. (defun my-occur (regexp &optional region)
  2286. "My occur command to search REGEXP to search REGION."
  2287. (interactive (list (read-string "List lines matching regexp: "
  2288. (thing-at-point 'symbol t))))
  2289. (occur regexp nil region))
  2290. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  2291. (set-variable 'dumb-jump-prefer-searcher 'rg)
  2292. (defalias 'make 'compile)
  2293. (define-key ctl-x-map "c" 'compile)
  2294. (autoload 'pb/push-item "pushbullet")
  2295. (defun my-pushbullet-note (text &optional title)
  2296. "Push TEXT with optional TITLE."
  2297. (interactive "sText to Push: ")
  2298. (pb/push-item '("") text "note" (or title "")))
  2299. ;;;;;;;;;;;;;;;;;;;;;
  2300. ;; git-bug
  2301. (defconst git-bug-ls-regexp
  2302. (eval-when-compile
  2303. (rx bol
  2304. (submatch (one-or-more alphanumeric)) ; id
  2305. ;; (one-or-more any)
  2306. (one-or-more space)
  2307. (submatch (or "open" "close")) ; status
  2308. (one-or-more space)
  2309. (submatch (maximal-match (zero-or-more print))) ; title
  2310. "\t"
  2311. (submatch (one-or-more alphanumeric)) ; user
  2312. (one-or-more space)
  2313. "C:"
  2314. (submatch (one-or-more digit)) ; Comment num
  2315. (one-or-more space)
  2316. "L:"
  2317. (submatch (one-or-more digit)) ; Label num
  2318. eol
  2319. ))
  2320. "Regexp to parse line of output of git-bug ls.
  2321. Used by `git-bug-ls'.")
  2322. (declare-function string-trim "subr-x")
  2323. (defun git-bug-bugs ()
  2324. "Get list of git-bug bugs."
  2325. (with-temp-buffer
  2326. (git-bug--call-process "bug" "ls")
  2327. (goto-char (point-min))
  2328. (let ((bugs nil))
  2329. (while (not (eq (point) (point-max)))
  2330. (save-match-data
  2331. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  2332. (setq bugs `(,@bugs
  2333. ,(list
  2334. :id (match-string 1)
  2335. :status (match-string 2)
  2336. :title (string-trim (match-string 3))
  2337. :user (match-string 4)
  2338. :comment-num (match-string 5)
  2339. :label-num (match-string 6)
  2340. )))))
  2341. (forward-line 1)
  2342. (goto-char (point-at-bol)))
  2343. bugs)))
  2344. (defun git-bug-ls ()
  2345. "Open and select git bug list buffer."
  2346. (interactive)
  2347. (pop-to-buffer (git-bug-ls-noselect)))
  2348. (defun git-bug-ls--set-tabulated-list-mode-variables ()
  2349. "Not implemented.")
  2350. (defun git-bug-ls-mode ()
  2351. "Not implemented.")
  2352. (defun git-bug-ls-noselect (&optional directory)
  2353. "Open git bug list buffer.
  2354. If optional arg DIRECTORY is given change current directory to there before
  2355. initializing."
  2356. (setq directory (expand-file-name (or directory
  2357. default-directory)))
  2358. (cl-assert (file-directory-p directory))
  2359. (let* ((root (git-bug--get-repository-root directory))
  2360. (name (file-name-nondirectory root))
  2361. (bname (format "*GitBug<%s>*" name)))
  2362. (with-current-buffer (get-buffer-create bname)
  2363. (cd root)
  2364. (git-bug-ls--set-tabulated-list-mode-variables)
  2365. (git-bug-ls-mode)
  2366. (current-buffer))))
  2367. (defun git-bug--get-repository-root (dir)
  2368. "Resolve repository root of DIR.
  2369. If DIR is not inside of any git repository, signal an error."
  2370. (cl-assert (file-directory-p dir))
  2371. (with-temp-buffer
  2372. (cd dir)
  2373. (git-bug--call-process "rev-parse" "--show-toplevel")
  2374. (goto-char (point-min))
  2375. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  2376. (defun git-bug--call-process (&rest args)
  2377. "Start git process synchronously with ARGS.
  2378. Raise error when git process ends with non-zero status.
  2379. Any output will be written to current buffer."
  2380. (let ((status (apply 'call-process
  2381. "git"
  2382. nil
  2383. t
  2384. nil
  2385. args)))
  2386. (cl-assert (eq status 0)
  2387. nil
  2388. (buffer-substring-no-properties (point-min) (point-max)))))
  2389. ;;;;;;;;;;;;;;;;;;;
  2390. ;; peek-file-mode
  2391. (defvar peek-file-buffers
  2392. ()
  2393. "Peek buffers.")
  2394. (defun peek-file (file)
  2395. "Peek FILE."
  2396. (interactive "fFile to peek: ")
  2397. (with-current-buffer (find-file file)
  2398. (peek-file-mode)))
  2399. (define-minor-mode peek-file-mode
  2400. "Peek file mode."
  2401. :lighter "PK"
  2402. (view-mode peek-file-mode)
  2403. (add-to-list 'peek-file-buffers
  2404. (current-buffer))
  2405. (add-hook 'switch-buffer-functions
  2406. 'peek-file-remove-buffers))
  2407. (defun peek-file-remove-buffers (&args _)
  2408. "Remove peek file buffers."
  2409. (cl-dolist (buf (cl-copy-list peek-file-buffers))
  2410. (unless (get-buffer-window buf t)
  2411. (setq peek-file-buffers
  2412. (delq buf
  2413. peek-file-buffers))
  2414. (with-current-buffer buf
  2415. (when peek-file-mode
  2416. (kill-buffer))))))
  2417. ;;;;;;;;;;;;;;;;;;;;
  2418. ;; remember-projectile
  2419. ;; TODO: Add global-minor-mode
  2420. (defvar remember-data-file)
  2421. (defun my-set-remember-data-file-buffer-local ()
  2422. "Set `remember-data-file'."
  2423. (when (require 'projectile nil t)
  2424. (setq-local remember-data-file
  2425. (expand-file-name ".remember.notes"
  2426. (projectile-project-root)))))
  2427. (add-hook 'after-change-major-mode-hook
  2428. 'my-set-remember-data-file-buffer-local)
  2429. (define-key ctl-x-map "R" 'remember)
  2430. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2431. ;; ivy
  2432. ;; (set-variable 'enable-recursive-minibuffers t)
  2433. ;; (minibuffer-depth-indicate-mode 1)
  2434. (declare-function ivy--regex-ignore-order "ivy")
  2435. (declare-function ivy--regex-fuzzy "ivy")
  2436. (defun my--ivy-regex-fuzzy-ignore-order (str)
  2437. "Re-build regex from STR for ignore-order fuzzy match."
  2438. (let ((re-list (ivy--regex-ignore-order str)))
  2439. (if (listp re-list)
  2440. (mapcar (lambda (e)
  2441. (let ((head (car e))
  2442. (tail (cdr e)))
  2443. (if tail
  2444. (cons (ivy--regex-fuzzy head)
  2445. tail)
  2446. (cons head tail))))
  2447. re-list)
  2448. (ivy--regex-fuzzy re-list))))
  2449. ;; (my--ivy-regex-fuzzy-ignore-order "ab bc !cee")
  2450. ;; (ivy--regex-fuzzy "ab")
  2451. ;; (ivy--regex-ignore-order "a b")
  2452. (set-variable 'ivy-re-builders-alist
  2453. '((t . my--ivy-regex-fuzzy-ignore-order)))
  2454. ;; (set-variable 'ivy-format-functions-alist
  2455. ;; '((t . ivy-format-function-arrow)))
  2456. (set-variable 'ivy-format-functions-alist
  2457. '((t . (lambda (cands) (ivy--format-function-generic
  2458. (lambda (str)
  2459. (concat "+> "
  2460. (ivy--add-face str 'ivy-current-match)
  2461. ))
  2462. (lambda (str)
  2463. (concat "| " str))
  2464. cands
  2465. "\n")))))
  2466. (set-variable 'ivy-wrap t)
  2467. (set-variable 'ivy-sort-max-size 500)
  2468. (when (fboundp 'ivy-rich-mode)
  2469. (ivy-rich-mode 1))
  2470. (with-eval-after-load 'ivy
  2471. (defvar ivy-minibuffer-map)
  2472. (define-key ivy-minibuffer-map (kbd "C-u")
  2473. (lambda () (interactive) (delete-region (point-at-bol) (point)))))
  2474. (set-variable 'ivy-on-del-error-function 'ignore)
  2475. (when (fboundp 'counsel-M-x)
  2476. (define-key esc-map "x" 'counsel-M-x)
  2477. ;; (counsel-mode 1)
  2478. ;; counsel-fzf executes fzf -f QUERY for each input
  2479. ;; (define-key ctl-x-map "f"
  2480. ;; (lambda ()
  2481. ;; (interactive
  2482. ;; (let ((process-environment (cl-copy-list process-environment)))
  2483. ;; (setenv "FZF_DEFAULT_COMMAND" nil)
  2484. ;; (counsel-fzf)))))
  2485. )
  2486. ;; (when (fboundp 'counsel-switch-buffer)
  2487. ;; (define-key ctl-x-map (kbd "C-b") 'counsel-switch-buffer))
  2488. (declare-function ivy-thing-at-point "ivy")
  2489. (when (and (fboundp 'ivy-read)
  2490. (locate-library "counsel"))
  2491. (defvar counsel-describe-map)
  2492. (defun my-counsel-describe-symbol ()
  2493. "Forwaord to `describe-symbol'."
  2494. (interactive)
  2495. (cl-assert (eval-and-compile (require 'help-mode nil t))) ;; describe-symbol-backends
  2496. (cl-assert (eval-and-compile (require 'counsel nil t)))
  2497. (ivy-read "Describe symbol: " obarray
  2498. ;; From describe-symbol definition
  2499. :predicate (lambda (vv)
  2500. (cl-some (lambda (x) (funcall (nth 1 x) vv))
  2501. describe-symbol-backends))
  2502. :require-match t
  2503. :history 'counsel-describe-symbol-history
  2504. :keymap counsel-describe-map
  2505. :preselect (ivy-thing-at-point)
  2506. :action (lambda (x)
  2507. (describe-symbol (intern x)))
  2508. :caller 'my-counsel-describe-symbol))
  2509. (define-key help-map "o" 'my-counsel-describe-symbol)
  2510. )
  2511. ;; (defun my-ivy-length (x y)
  2512. ;; "Ivy sort to order by string length."
  2513. ;; (<= (length (if (consp x) (car x) x))
  2514. ;; (length (if (consp y) (car y) y))))
  2515. ;; (my-ivy-length "a" (cons "bc" t))
  2516. ;; (my-ivy-length "a" (cons "c" t))
  2517. (declare-function ivy-configure "ivy")
  2518. (with-eval-after-load 'counsel ;; Hook to counsel, not ivy
  2519. ;; (ivy-configure 'my-counsel-describe-symbol
  2520. ;; :sort-fn 'my-ivy-length)
  2521. (ivy-configure 'counsel-M-x
  2522. :initial-input ""
  2523. ;; :sort-fn 'my-ivy-length
  2524. )
  2525. (defvar ivy-sort-matches-functions-alist)
  2526. (add-to-list 'ivy-sort-matches-functions-alist
  2527. '(my-counsel-describe-symbol . ivy--shorter-matches-first))
  2528. (add-to-list 'ivy-sort-matches-functions-alist
  2529. '(counsel-M-x . ivy--shorter-matches-first))
  2530. )
  2531. (when (fboundp 'counsel-imenu)
  2532. (define-key ctl-x-map "l" 'counsel-imenu))
  2533. (when (fboundp 'swiper)
  2534. (define-key esc-map (kbd "C-s") 'swiper))
  2535. ;; ?
  2536. (define-key input-decode-map "\e[1;5C" [C-right])
  2537. (define-key input-decode-map "\e[1;5D" [C-left])
  2538. ;;;;;;;;;;;;;;;;;;;;;;;;
  2539. ;; mozc
  2540. (global-set-key (kbd "C-c m e") 'ignore)
  2541. (global-set-key (kbd "C-c m d") 'ignore)
  2542. ;; mozc
  2543. (when (locate-library "mozc")
  2544. ;; https://tottoto.net/mac-emacs-karabiner-elements-japanese-input-method-config/
  2545. (with-eval-after-load 'mozc
  2546. ;; (define-key mozc-mode-map (kbd "C-h") 'backward-delete-char)
  2547. ;; (define-key mozc-mode-map (kbd "C-p") (kbd "<up>"))
  2548. ;; (define-key mozc-mode-map (kbd "C-n") (kbd "SPC"))
  2549. )
  2550. (setq default-input-method "japanese-mozc")
  2551. (custom-set-variables '(mozc-leim-title "あ"))
  2552. (defun turn-on-input-method ()
  2553. (interactive)
  2554. (activate-input-method default-input-method))
  2555. (defun turn-off-input-method ()
  2556. (interactive)
  2557. (deactivate-input-method))
  2558. ;; (setq mozc-candidate-style 'echo-area)
  2559. (global-set-key (kbd "C-c m e") 'turn-on-input-method)
  2560. (global-set-key (kbd "C-c m d") 'turn-off-input-method)
  2561. (global-set-key (kbd "<f7>") 'turn-off-input-method)
  2562. (global-set-key (kbd "<f8>") 'turn-on-input-method)
  2563. (require 'mozc-popup)
  2564. (set-variable 'mozc-candidate-style 'popup)
  2565. ;; これいる?
  2566. (when (require 'mozc-im nil t)
  2567. (setq default-input-method "japanese-mozc-im")
  2568. ;; (global-set-key (kbd "C-j") 'toggle-input-method)
  2569. )
  2570. )
  2571. ;;;;;;;;;;;;;;
  2572. ;; mmv
  2573. ;; https://www.emacswiki.org/emacs/MakingMarkVisible
  2574. ;;;; Make the mark visible, and the visibility toggleable. ('mmv' means 'make
  2575. ;;;; mark visible'.) By Patrick Gundlach, Teemu Leisti, and Stefan.
  2576. (defgroup mmv nil
  2577. "Make mark visible."
  2578. :group 'tools)
  2579. (defvar mmv-face-foreground
  2580. (face-foreground 'hi-yellow)
  2581. "Foreground color for `mmv-face'.")
  2582. (defvar mmv-face-background
  2583. (face-background 'hi-yellow)
  2584. "Background color for `mmv-face'.")
  2585. (defface mmv-face
  2586. `((t :background ,mmv-face-background :foreground ,mmv-face-foreground))
  2587. "Face used for showing the mark's position."
  2588. :group 'mmv)
  2589. (defvar-local mmv-mark-overlay nil
  2590. "The overlay for showing the mark's position.")
  2591. (defvar-local mmv-is-mark-visible t
  2592. "The overlay is visible only when this variable's value is t.")
  2593. (defun mmv-draw-mark (&rest _)
  2594. "Make the mark's position stand out by means of a one-character-long overlay.
  2595. If the value of variable `mmv-is-mark-visible' is nil, the mark will be
  2596. invisible."
  2597. (unless mmv-mark-overlay
  2598. (setq mmv-mark-overlay (make-overlay 0 0 nil t))
  2599. (overlay-put mmv-mark-overlay 'face 'mmv-face)
  2600. (overlay-put mmv-mark-overlay 'priority 10)) ;; bigger than highlight-indentation-current-column-overlay-priority
  2601. (let ((mark-position (mark t)))
  2602. (cond
  2603. ((null mark-position) (delete-overlay mmv-mark-overlay))
  2604. ((and (< mark-position (point-max))
  2605. (not (eq ?\n (char-after mark-position))))
  2606. (overlay-put mmv-mark-overlay 'after-string nil)
  2607. (move-overlay mmv-mark-overlay mark-position (1+ mark-position)))
  2608. (t
  2609. ;; This branch is called when the mark is at the end of a line or at the
  2610. ;; end of the buffer. We use a bit of trickery to avoid the higlight
  2611. ;; extending from the mark all the way to the right end of the frame.
  2612. (overlay-put mmv-mark-overlay 'after-string
  2613. (propertize " " 'face (overlay-get mmv-mark-overlay 'face)))
  2614. (move-overlay mmv-mark-overlay mark-position mark-position)))))
  2615. (add-hook 'pre-redisplay-functions #'mmv-draw-mark)
  2616. (defun mmv-toggle-mark-visibility ()
  2617. "Toggles the mark's visiblity and redraws it (whether invisible or visible)."
  2618. (interactive)
  2619. (setq mmv-is-mark-visible (not mmv-is-mark-visible))
  2620. (if mmv-is-mark-visible
  2621. (set-face-attribute 'mmv-face nil :background mmv-face-background :foreground mmv-face-foreground)
  2622. (set-face-attribute 'mmv-face nil :background 'unspecified :foreground 'unspecified))
  2623. (mmv-draw-mark))
  2624. ;; Local Variables:
  2625. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2626. ;; flycheck-checker: emacs-lisp
  2627. ;; End:
  2628. ;;; emancs.el ends here