Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

3014 wiersze
100 KiB

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