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

3052 regels
102 KiB

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