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

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