Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

3061 Zeilen
102 KiB

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