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

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