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

3065 lines
102 KiB

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