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.
 
 
 
 
 
 

3094 lines
103 KiB

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