You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2971 lines
98 KiB

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