Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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