25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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