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.
 
 
 
 
 
 

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