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.
 
 
 
 
 
 

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