Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

3017 Zeilen
100 KiB

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