Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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