You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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