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.
 
 
 
 
 
 

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