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

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