Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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