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.
 
 
 
 
 
 

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