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

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