25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3005 lines
100 KiB

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