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.
 
 
 
 
 
 

3033 lines
100 KiB

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