選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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