Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

3050 řádky
101 KiB

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