Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

2849 строки
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 5 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. ;; Bookmark feature does not work well with multiple emacs instances...
  991. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  992. (set-variable 'bookmark-default-file
  993. (expand-file-name (concat user-emacs-directory
  994. "bmk")))
  995. (defun my-bookmark-set ()
  996. "My `bookmark-set'."
  997. (interactive)
  998. (cl-assert (or buffer-file-name
  999. default-directory))
  1000. (let ((name (file-name-nondirectory (or buffer-file-name
  1001. (directory-file-name default-directory))))
  1002. (linenum (count-lines (point-min)
  1003. (point)))
  1004. (linetext (buffer-substring-no-properties (point-at-bol)
  1005. (point-at-eol))))
  1006. (bookmark-set (format "%s:%d:%s"
  1007. name linenum linetext)
  1008. nil)))
  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. ;; vc
  1017. (set-variable 'vc-handled-backends '(RCS))
  1018. (set-variable 'vc-rcs-register-switches "-l")
  1019. (set-variable 'vc-rcs-checkin-switches "-l")
  1020. (set-variable 'vc-command-messages t)
  1021. (when (fboundp 'neotree)
  1022. (define-key ctl-x-map (kbd "C-n") 'neotree))
  1023. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1024. ;; share clipboard with x
  1025. ;; this page describes this in details, but only these sexps seem to be needed
  1026. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  1027. (and nil
  1028. (not window-system)
  1029. (not (eq window-system 'mac))
  1030. (getenv "DISPLAY")
  1031. (not (equal (getenv "DISPLAY") ""))
  1032. (executable-find "xclip")
  1033. ;; (< emacs-major-version 24)
  1034. '(require 'xclip nil t)
  1035. nil
  1036. (turn-on-xclip))
  1037. (and (eq system-type 'darwin)
  1038. (require 'pasteboard nil t)
  1039. (turn-on-pasteboard))
  1040. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1041. ;; some modes and hooks
  1042. ;; Include some extra modes
  1043. (require 'generic-x)
  1044. ;; Derived from https://github.com/ensime/ensime-emacs/issues/591#issuecomment-291916753
  1045. (defun my-scalafmt ()
  1046. (interactive)
  1047. (cl-assert buffer-file-name)
  1048. (cl-assert (not (buffer-modified-p)))
  1049. (let* ((configdir (locate-dominating-file default-directory ".scalafmt.conf"))
  1050. (configoption (if configdir
  1051. (concat " --config "
  1052. (shell-quote-argument (expand-file-name configdir))
  1053. ".scalafmt.conf"
  1054. )
  1055. ""))
  1056. (str (concat "scalafmt -f "
  1057. (shell-quote-argument buffer-file-name)
  1058. configoption
  1059. " -i --exclude ensime")))
  1060. (message str)
  1061. (shell-command-to-string str))
  1062. (message "scalafmt done")
  1063. (revert-buffer nil t))
  1064. (when (fboundp 'web-mode)
  1065. (add-to-list 'auto-mode-alist
  1066. '("\\.html\\.j2\\'" . web-mode))
  1067. (add-to-list 'auto-mode-alist
  1068. ;; Django Template Language
  1069. '("\\.dtl\\'" . web-mode))
  1070. )
  1071. (when (locate-library "wgrep")
  1072. (set-variable 'wgrep-auto-save-buffer t)
  1073. (with-eval-after-load 'grep
  1074. (defvar grep-mode-map)
  1075. (define-key grep-mode-map
  1076. "e"
  1077. 'wgrep-change-to-wgrep-mode)))
  1078. (when (fboundp 'grep-context-mode)
  1079. (add-hook 'compilation-mode-hook #'grep-context-mode))
  1080. (with-eval-after-load 'remember
  1081. (defvar remember-mode-map)
  1082. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  1083. (set-variable 'remember-notes-initial-major-mode
  1084. 'change-log-mode)
  1085. (with-eval-after-load 'magit-files
  1086. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  1087. ;; existing keybindings.
  1088. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  1089. ;; to disable thie mode here, but do anyway.
  1090. ;; See also https://github.com/magit/magit/issues/3517
  1091. (global-magit-file-mode -1))
  1092. (with-eval-after-load 'magit-section
  1093. (set-face-background 'magit-section-highlight
  1094. nil))
  1095. ;; Sane colors
  1096. (with-eval-after-load 'magit-diff
  1097. (set-face-background 'magit-diff-context nil)
  1098. (set-face-background 'magit-diff-context-highlight nil)
  1099. (set-face-foreground 'magit-diff-hunk-heading nil)
  1100. (set-face-background 'magit-diff-hunk-heading nil)
  1101. (set-face-foreground 'magit-diff-hunk-heading-highlight nil)
  1102. (set-face-background 'magit-diff-hunk-heading-highlight nil)
  1103. ;; https://blog.shibayu36.org/entry/2016/03/27/220552
  1104. (set-face-foreground 'magit-diff-added "green")
  1105. (set-face-background 'magit-diff-added nil)
  1106. (set-face-foreground 'magit-diff-added-highlight "green")
  1107. (set-face-background 'magit-diff-added-highlight nil)
  1108. (set-face-foreground 'magit-diff-removed "red")
  1109. (set-face-background 'magit-diff-removed nil)
  1110. (set-face-foreground 'magit-diff-removed-highlight "red")
  1111. (set-face-background 'magit-diff-removed-highlight nil)
  1112. (set-face-background 'magit-diff-lines-boundary "blue")
  1113. )
  1114. (defun my-magit-messenger (file line)
  1115. "Magit messenger."
  1116. (interactive (list buffer-file-name
  1117. (line-number-at-pos)))
  1118. (cl-assert file)
  1119. (cl-assert line)
  1120. (let* ((blame-args '("-w"))
  1121. (id (with-temp-buffer
  1122. (let ((exit (apply 'call-process
  1123. "git" ;; PROGRAM
  1124. nil ;; INFILE
  1125. t ;; DESTINATION
  1126. nil ;; DISPLAY
  1127. "--no-pager" ;; ARGS
  1128. "blame"
  1129. "-L"
  1130. (format "%d,+1" line)
  1131. "--porcelain"
  1132. file
  1133. blame-args
  1134. )))
  1135. (goto-char (point-min))
  1136. (cl-assert (eq exit 0)
  1137. "Failed: %s" (buffer-substring (point)
  1138. (point-at-eol)))
  1139. (save-match-data
  1140. (re-search-forward (rx buffer-start
  1141. (one-or-more hex-digit)))
  1142. (match-string 0))))))
  1143. (magit-show-commit id)))
  1144. (when (boundp 'git-rebase-filename-regexp)
  1145. (add-to-list 'auto-mode-alist
  1146. `(,git-rebase-filename-regexp . text-mode)))
  1147. (when (fboundp 'global-aggressive-indent-mode)
  1148. (add-hook 'after-first-visit-hook
  1149. 'global-aggressive-indent-mode))
  1150. (with-eval-after-load 'aggressive-indent
  1151. (defvar aggressive-indent-excluded-modes)
  1152. (set-variable 'aggressive-indent-excluded-modes
  1153. `(web-mode
  1154. diff-mode
  1155. toml-mode
  1156. conf-mode
  1157. dockerfile-mode
  1158. groovy-mode
  1159. scala-mode
  1160. ,@aggressive-indent-excluded-modes)))
  1161. (when (fboundp 'ggtags-mode)
  1162. (add-hook 'c-mode-common-hook
  1163. 'ggtags-mode)
  1164. (add-hook 'python-mode-hook
  1165. 'ggtags-mode)
  1166. (add-hook 'js-mode-hook
  1167. 'ggtags-mode)
  1168. (add-hook 'scheme-mode-hook
  1169. 'ggtags-mode)
  1170. )
  1171. (when (fboundp 'imenu-list-minor-mode)
  1172. (defvar imenu-list-buffer-name)
  1173. (defun my-imenu-list-toggle ()
  1174. "My 'imenu-list` toggle."
  1175. (interactive)
  1176. (require 'imenu-list)
  1177. (if (eq (window-buffer)
  1178. (get-buffer imenu-list-buffer-name))
  1179. (imenu-list-minor-mode -1)
  1180. (imenu-list-minor-mode 1)))
  1181. ;; (set-variable 'imenu-list-auto-resize t)
  1182. (set-variable 'imenu-list-focus-after-activation t)
  1183. (define-key ctl-x-map (kbd "C-l") 'my-imenu-list-toggle))
  1184. (add-hook 'emacs-lisp-mode-hook
  1185. (lambda ()
  1186. (setq imenu-generic-expression
  1187. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  1188. ,@imenu-generic-expression))))
  1189. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  1190. (with-eval-after-load 'compile
  1191. (defvar compilation-filter-start)
  1192. (defvar compilation-error-regexp-alist)
  1193. (require 'ansi-color)
  1194. (add-hook 'compilation-filter-hook
  1195. (lambda ()
  1196. (let ((inhibit-read-only t))
  1197. (ansi-color-apply-on-region compilation-filter-start
  1198. (point)))))
  1199. (add-to-list 'compilation-error-regexp-alist
  1200. ;; ansible-lint
  1201. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2))
  1202. (add-to-list 'compilation-error-regexp-alist
  1203. ;; pydocstyle
  1204. '("^\\([^ \n]+\\):\\([0-9]+\\) " 1 2))
  1205. )
  1206. (when (fboundp 'global-company-mode)
  1207. (add-hook 'after-first-visit-hook
  1208. 'global-company-mode))
  1209. (with-eval-after-load 'company
  1210. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1211. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1212. (set-variable 'company-idle-delay nil)
  1213. (set-variable 'company-minimum-prefix-length 2)
  1214. (set-variable 'company-selection-wrap-around t)
  1215. (defvar company-mode-map)
  1216. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1217. ;; (with-eval-after-load 'python
  1218. ;; (defvar python-indent-trigger-commands)
  1219. ;; ;; TODO: This disables completion in puthon?
  1220. ;; (add-to-list 'python-indent-trigger-commands
  1221. ;; 'company-indent-or-complete-common))
  1222. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1223. (defvar company-active-map)
  1224. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1225. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1226. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1227. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1228. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1229. (defvar company-mode)
  1230. (defvar company-candidates)
  1231. (defvar company-candidates-length)
  1232. ;; (popup-tip "Hello, World!")
  1233. (defun my-company-lighter-current-length ()
  1234. "Get current candidate length."
  1235. (interactive)
  1236. (let ((l nil)
  1237. (inhibit-message t))
  1238. (when (and company-mode
  1239. (not (minibufferp))
  1240. ;; Do nothing when already in company completion
  1241. (not company-candidates))
  1242. ;; FIXME: Somehow it cannto catch errors from ggtags
  1243. (ignore-errors
  1244. ;; (company-auto-begin)
  1245. (company-manual-begin)
  1246. (setq l company-candidates-length)
  1247. (company-cancel)))
  1248. (if l
  1249. (format "[%d]" l)
  1250. "")))
  1251. (defvar company-lighter)
  1252. (set-variable 'company-lighter-base "Cmp")
  1253. ;; (add-to-list 'company-lighter
  1254. ;; '(:eval (my-company-lighter-current-length))
  1255. ;; t)
  1256. ;; This breaks japanese text input
  1257. ;; (set-variable 'my-company-length-popup-tip-timer
  1258. ;; (run-with-idle-timer 0.2 t
  1259. ;; 'my-company-length-popup-tip))
  1260. ;; (current-active-maps)
  1261. ;; (lookup-key)
  1262. '(mapcar (lambda (map)
  1263. (lookup-key map (kbd "C-i")))
  1264. (current-active-maps))
  1265. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1266. (set-face-attribute 'company-tooltip nil
  1267. :foreground "black" :background "lightgrey")
  1268. (set-face-attribute 'company-tooltip-common nil
  1269. :foreground "black" :background "lightgrey")
  1270. (set-face-attribute 'company-tooltip-common-selection nil
  1271. :foreground "white" :background "steelblue")
  1272. (set-face-attribute 'company-tooltip-selection nil
  1273. :foreground "black" :background "steelblue")
  1274. (set-face-attribute 'company-preview-common nil
  1275. :background nil :foreground "lightgrey" :underline t)
  1276. (set-face-attribute 'company-scrollbar-fg nil
  1277. :background "orange")
  1278. (set-face-attribute 'company-scrollbar-bg nil
  1279. :background "gray40")
  1280. )
  1281. ;; https://github.com/lunaryorn/flycheck
  1282. ;; TODO: Any way to disable auto check?
  1283. ;; Update flycheck-hooks-alist?
  1284. (when (fboundp 'global-flycheck-mode)
  1285. (add-hook 'after-first-visit-hook
  1286. 'global-flycheck-mode))
  1287. ;; (set-variable 'flycheck-display-errors-delay 2.0)
  1288. ;; (fset 'flycheck-display-error-at-point-soon 'ignore)
  1289. ;; (with-eval-after-load 'flycheck
  1290. ;; (when (fboundp 'flycheck-black-check-setup)
  1291. ;; (flycheck-black-check-setup)))
  1292. (when (fboundp 'ilookup-open-word)
  1293. (define-key ctl-x-map "d" 'ilookup-open-word))
  1294. (set-variable 'ac-ignore-case nil)
  1295. (when (fboundp 'term-run-shell-command)
  1296. (define-key ctl-x-map "t" 'term-run-shell-command))
  1297. (add-to-list 'safe-local-variable-values
  1298. '(encoding utf-8))
  1299. (setq enable-local-variables :safe)
  1300. ;; Detect file type from shebang and set major-mode.
  1301. (add-to-list 'interpreter-mode-alist
  1302. '("python3" . python-mode))
  1303. (add-to-list 'interpreter-mode-alist
  1304. '("python2" . python-mode))
  1305. (with-eval-after-load 'python
  1306. (defvar python-mode-map (make-sparse-keymap))
  1307. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1308. ;; I want to use this, but this breaks normal self-insert-command
  1309. ;; (set-variable 'py-indent-list-style
  1310. ;; 'one-level-to-beginning-of-statement)
  1311. (set-variable 'pydoc-command
  1312. "python3 -m pydoc")
  1313. (with-eval-after-load 'pydoc
  1314. (when (require 'with-venv nil t)
  1315. (with-venv-advice-add 'pydoc)))
  1316. (set-variable 'flycheck-python-mypy-ini ".mypy.ini")
  1317. (set-variable 'flycheck-flake8rc "setup.cfg")
  1318. (set-variable 'flycheck-python-pylint-executable "python3")
  1319. (set-variable 'flycheck-python-pycompile-executable "python3")
  1320. (set-variable 'python-indent-guess-indent-offset nil)
  1321. (with-eval-after-load 'blacken
  1322. (when (require 'with-venv nil t)
  1323. (with-venv-advice-add 'blacken-buffer)))
  1324. (with-eval-after-load 'ansible-doc
  1325. (when (require 'with-venv nil t)
  1326. (with-venv-advice-add 'ansible-doc)))
  1327. ;; `isortify-buffer' breaks buffer when it contains japanese text
  1328. (defun my-isortify ()
  1329. (interactive)
  1330. (cl-assert buffer-file-name)
  1331. (cl-assert (not (buffer-modified-p)))
  1332. (call-process "python" ;; PROGRAM
  1333. nil ;; INFILE
  1334. nil ;; DESTINATION
  1335. nil ;; DISPLAY
  1336. "-m" "isort" buffer-file-name)
  1337. (message "isortify done")
  1338. (revert-buffer nil t))
  1339. (when (fboundp 'with-venv-advice-add)
  1340. ;; TODO: Lazy load with-venv
  1341. (with-venv-advice-add 'my-isortify))
  1342. ;; https://github.com/lunaryorn/old-emacs-configuration/blob/master/lisp/flycheck-virtualenv.el
  1343. (defun my-set-venv-flycheck-executable-find ()
  1344. "Set flycheck executabie find."
  1345. (interactive)
  1346. (set-variable 'flycheck-executable-find
  1347. '(lambda (e)
  1348. (with-venv
  1349. (executable-find e)))
  1350. t))
  1351. (defun my-update-flycheck-flake8-error-level-alist ()
  1352. "Update `flycheck-flake8-error-level-alist'."
  1353. (defvar flycheck-flake8-error-level-alist)
  1354. ;; (add-to-list 'flycheck-flake8-error-level-alist
  1355. ;; '("^D.*$" . warning))
  1356. (set-variable 'flycheck-flake8-error-level-alist
  1357. nil)
  1358. )
  1359. (add-hook 'python-mode-hook
  1360. 'my-set-venv-flycheck-executable-find)
  1361. (add-hook 'python-mode-hook
  1362. 'my-update-flycheck-flake8-error-level-alist)
  1363. (when (fboundp 'with-venv-info-mode)
  1364. (add-hook 'python-mode-hook
  1365. 'with-venv-info-mode))
  1366. ;; Run multiple chekcers
  1367. ;; https://github.com/flycheck/flycheck/issues/186
  1368. ;; http://fukuyama.co/foreign-regexp
  1369. '(and (require 'foreign-regexp nil t)
  1370. (progn
  1371. (setq foreign-regexp/regexp-type 'perl)
  1372. '(setq reb-re-syntax 'foreign-regexp)
  1373. ))
  1374. (with-eval-after-load 'sql
  1375. (require 'sql-indent nil t))
  1376. (add-to-list 'auto-mode-alist
  1377. '("\\.hql\\'" . sql-mode))
  1378. (when (fboundp 'git-command)
  1379. (define-key ctl-x-map "g" 'git-command))
  1380. (when (fboundp 'gited-list)
  1381. (defalias 'gited 'gited-list))
  1382. (when (fboundp 'global-git-commit-mode)
  1383. (add-hook 'after-first-visit-hook
  1384. 'global-git-commit-mode)
  1385. (add-hook 'after-first-visit-hook
  1386. 'git-commit-setup-check-buffer))
  1387. (with-eval-after-load 'git-commit
  1388. (add-hook 'git-commit-setup-hook
  1389. 'turn-off-auto-fill t))
  1390. (with-eval-after-load 'rst
  1391. (defvar rst-mode-map)
  1392. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1393. (with-eval-after-load 'jdee
  1394. (add-hook 'jdee-mode-hook
  1395. (lambda ()
  1396. (make-local-variable 'global-mode-string)
  1397. (add-to-list 'global-mode-string
  1398. mode-line-position))))
  1399. ;; Cannot enable error thrown. Why???
  1400. ;; https://github.com/m0smith/malabar-mode#Installation
  1401. ;; (when (require 'malabar-mode nil t)
  1402. ;; (add-to-list 'load-path
  1403. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1404. ;; (require 'cedet-devel-load nil t)
  1405. ;; (eval-after-init (activate-malabar-mode)))
  1406. (with-eval-after-load 'make-mode
  1407. (defvar makefile-mode-map (make-sparse-keymap))
  1408. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1409. ;; this functions is set in write-file-functions, i cannot find any
  1410. ;; good way to remove this.
  1411. (fset 'makefile-warn-suspicious-lines 'ignore))
  1412. (with-eval-after-load 'verilog-mode
  1413. (defvar verilog-mode-map (make-sparse-keymap))
  1414. (define-key verilog-mode-map ";" 'self-insert-command))
  1415. (setq diff-switches "-u")
  1416. (autoload 'diff-goto-source "diff-mode" nil t)
  1417. (with-eval-after-load 'diff-mode
  1418. ;; (when (and (eq major-mode
  1419. ;; 'diff-mode)
  1420. ;; (not buffer-file-name))
  1421. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1422. ;; (view-mode 1))
  1423. (set-face-attribute 'diff-header nil
  1424. :foreground nil
  1425. :background nil
  1426. :weight 'bold)
  1427. (set-face-attribute 'diff-file-header nil
  1428. :foreground nil
  1429. :background nil
  1430. :weight 'bold)
  1431. (set-face-foreground 'diff-index "blue")
  1432. (set-face-attribute 'diff-hunk-header nil
  1433. :foreground "cyan"
  1434. :weight 'normal)
  1435. (set-face-attribute 'diff-context nil
  1436. ;; :foreground "white"
  1437. :foreground nil
  1438. :weight 'normal)
  1439. (set-face-foreground 'diff-removed "red")
  1440. (set-face-foreground 'diff-added "green")
  1441. (set-face-background 'diff-removed nil)
  1442. (set-face-background 'diff-added nil)
  1443. (set-face-attribute 'diff-changed nil
  1444. :foreground "magenta"
  1445. :weight 'normal)
  1446. (set-face-attribute 'diff-refine-changed nil
  1447. :foreground nil
  1448. :background nil
  1449. :weight 'bold
  1450. :inverse-video t)
  1451. ;; Annoying !
  1452. ;;(diff-auto-refine-mode)
  1453. )
  1454. ;; (ffap-bindings)
  1455. (set-variable 'browse-url-browser-function
  1456. 'eww-browse-url)
  1457. (set-variable 'sh-here-document-word "__EOC__")
  1458. (with-eval-after-load 'adoc-mode
  1459. (defvar adoc-mode-map)
  1460. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1461. (when (fboundp 'adoc-mode)
  1462. (setq auto-mode-alist
  1463. `(("\\.adoc\\'" . adoc-mode)
  1464. ("\\.asciidoc\\'" . adoc-mode)
  1465. ,@auto-mode-alist)))
  1466. (with-eval-after-load 'markup-faces
  1467. ;; Is this too match ?
  1468. (set-face-foreground 'markup-meta-face
  1469. "color-245")
  1470. (set-face-foreground 'markup-meta-hide-face
  1471. "color-245")
  1472. )
  1473. ;; TODO: check if this is required
  1474. (with-eval-after-load 'groovy-mode
  1475. (defvar groovy-mode-map)
  1476. (define-key groovy-mode-map "(" 'self-insert-command)
  1477. (define-key groovy-mode-map ")" 'self-insert-command)
  1478. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1479. )
  1480. (when (fboundp 'groovy-mode)
  1481. (add-to-list 'auto-mode-alist
  1482. '("build\\.gradle\\'" . groovy-mode)))
  1483. (add-to-list 'auto-mode-alist
  1484. '("\\.gawk\\'" . awk-mode))
  1485. (with-eval-after-load 'yaml-mode
  1486. (defvar yaml-mode-map (make-sparse-keymap))
  1487. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1488. (with-eval-after-load 'html-mode
  1489. (defvar html-mode-map (make-sparse-keymap))
  1490. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1491. (with-eval-after-load 'text-mode
  1492. (define-key text-mode-map (kbd "C-m") 'newline))
  1493. (with-eval-after-load 'info
  1494. (defvar Info-additional-directory-list)
  1495. (dolist (dir (directory-files (concat user-emacs-directory
  1496. "info")
  1497. t
  1498. "^[^.].*"))
  1499. (when (file-directory-p dir)
  1500. (add-to-list 'Info-additional-directory-list
  1501. dir)))
  1502. (let ((dir (expand-file-name "~/.brew/share/info")))
  1503. (when (file-directory-p dir)
  1504. (add-to-list 'Info-additional-directory-list
  1505. dir))))
  1506. (with-eval-after-load 'apropos
  1507. (defvar apropos-mode-map (make-sparse-keymap))
  1508. (define-key apropos-mode-map "n" 'next-line)
  1509. (define-key apropos-mode-map "p" 'previous-line))
  1510. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1511. ;; (define-key isearch-mode-map
  1512. ;; (kbd "C-j") 'isearch-other-control-char)
  1513. ;; (define-key isearch-mode-map
  1514. ;; (kbd "C-k") 'isearch-other-control-char)
  1515. ;; (define-key isearch-mode-map
  1516. ;; (kbd "C-h") 'isearch-other-control-char)
  1517. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1518. (define-key isearch-mode-map (kbd "M-r")
  1519. 'isearch-query-replace-regexp)
  1520. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1521. (setq lazy-highlight-cleanup nil)
  1522. ;; face for isearch highlighing
  1523. (set-face-attribute 'lazy-highlight
  1524. nil
  1525. :foreground `unspecified
  1526. :background `unspecified
  1527. :underline t
  1528. ;; :weight `bold
  1529. )
  1530. (add-hook 'outline-mode-hook
  1531. (lambda ()
  1532. (when (string-match "\\.md\\'" buffer-file-name)
  1533. (setq-local outline-regexp "#+ "))))
  1534. (add-hook 'outline-mode-hook
  1535. 'outline-show-all)
  1536. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1537. (with-eval-after-load 'markdown-mode
  1538. (defvar gfm-mode-map)
  1539. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1540. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1541. )
  1542. (when (fboundp 'gfm-mode)
  1543. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1544. (add-hook 'markdown-mode-hook
  1545. 'outline-minor-mode)
  1546. (add-hook 'markdown-mode-hook
  1547. (lambda ()
  1548. (setq-local comment-start ";")))
  1549. )
  1550. ;; http://keisanbutsuriya.hateblo.jp/entry/2015/02/10/152543
  1551. ;; M-$ to ispell word
  1552. ;; M-x flyspell-buffer to highlight all suspicious words
  1553. (when (executable-find "aspell")
  1554. (set-variable 'ispell-program-name "aspell")
  1555. (set-variable 'ispell-extra-args '("--lang=en_US")))
  1556. (with-eval-after-load 'ispell
  1557. (add-to-list 'ispell-skip-region-alist '("[^\000-\377]+")))
  1558. (when (fboundp 'flyspell-mode)
  1559. (add-hook 'text-mode-hook
  1560. 'flyspell-mode))
  1561. (when (fboundp 'flyspell-prog-mode)
  1562. (add-hook 'prog-mode-hook
  1563. 'flyspell-prog-mode))
  1564. ;; c-mode
  1565. ;; http://www.emacswiki.org/emacs/IndentingC
  1566. ;; http://en.wikipedia.org/wiki/Indent_style
  1567. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1568. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1569. (with-eval-after-load 'cc-vars
  1570. (defvar c-default-style nil)
  1571. (add-to-list 'c-default-style
  1572. '(c-mode . "k&r"))
  1573. (add-to-list 'c-default-style
  1574. '(c++-mode . "k&r")))
  1575. (with-eval-after-load 'js2-mode
  1576. ;; currently do not use js2-mode
  1577. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1578. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1579. ;; (defvar js2-mode-map (make-sparse-keymap))
  1580. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1581. ;; (interactive)
  1582. ;; (js2-enter-key)
  1583. ;; (indent-for-tab-command)))
  1584. ;; (add-hook (kill-local-variable 'before-save-hook)
  1585. ;; 'js2-before-save)
  1586. ;; (add-hook 'before-save-hook
  1587. ;; 'my-indent-buffer
  1588. ;; nil
  1589. ;; t)
  1590. )
  1591. (add-to-list 'interpreter-mode-alist
  1592. '("node" . js-mode))
  1593. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1594. (with-eval-after-load 'uniquify
  1595. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1596. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1597. (setq uniquify-min-dir-content 1))
  1598. (with-eval-after-load 'view
  1599. (defvar view-mode-map (make-sparse-keymap))
  1600. (define-key view-mode-map "j" 'scroll-up-line)
  1601. (define-key view-mode-map "k" 'scroll-down-line)
  1602. (define-key view-mode-map "v" 'toggle-read-only)
  1603. (define-key view-mode-map "q" 'bury-buffer)
  1604. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1605. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1606. ;; (define-key view-mode-map
  1607. ;; "n" 'nonincremental-repeat-search-forward)
  1608. ;; (define-key view-mode-map
  1609. ;; "N" 'nonincremental-repeat-search-backward)
  1610. ;; N conflicts with git-walktree
  1611. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1612. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1613. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1614. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1615. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1616. (global-set-key "\M-r" 'view-mode)
  1617. ;; (setq view-read-only t)
  1618. (with-eval-after-load 'term
  1619. (defvar term-raw-map (make-sparse-keymap))
  1620. (define-key term-raw-map (kbd "C-x")
  1621. (lookup-key (current-global-map)
  1622. (kbd "C-x"))))
  1623. (add-hook 'term-mode-hook
  1624. (lambda ()
  1625. ;; Stop current line highlighting
  1626. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1627. (set-variable 'scroll-margin 0 t)
  1628. ))
  1629. (set-variable 'term-buffer-maximum-size 20480)
  1630. (set-variable 'term-suppress-hard-newline t)
  1631. (add-hook 'Man-mode-hook
  1632. (lambda ()
  1633. (view-mode 1)
  1634. (setq truncate-lines nil)))
  1635. (set-variable 'Man-notify-method (if window-system
  1636. 'newframe
  1637. 'aggressive))
  1638. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1639. "woman_cache.el")))
  1640. ;; not work because man.el will be loaded when man called
  1641. (defalias 'man 'woman)
  1642. (add-to-list 'auto-mode-alist
  1643. '("tox\\.ini\\'" . conf-unix-mode))
  1644. (when (fboundp 'toml-mode)
  1645. (add-to-list 'auto-mode-alist
  1646. '("/tox\\.ini\\'" . toml-mode))
  1647. (add-to-list 'auto-mode-alist
  1648. '("/Pipfile\\'" . toml-mode))
  1649. (add-to-list 'auto-mode-alist
  1650. '("/poetry\\.lock\\'" . toml-mode))
  1651. )
  1652. (when (fboundp 'json-mode)
  1653. (add-to-list 'auto-mode-alist
  1654. '("/Pipfile\\.lock\\'" . json-mode)))
  1655. (add-hook 'go-mode-hook
  1656. (lambda()
  1657. (defvar go-mode-map)
  1658. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1659. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1660. (when (fboundp 'k8s-mode)
  1661. (add-to-list 'auto-mode-alist
  1662. '("\\.k8s\\'" . k8s-mode)))
  1663. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1664. ;; buffers
  1665. (defvar bs-configurations)
  1666. (declare-function bs-set-configuration "bs")
  1667. (declare-function bs-refresh "bs")
  1668. (declare-function bs-message-without-log "bs")
  1669. (declare-function bs--current-config-message "bs")
  1670. (with-eval-after-load 'bs
  1671. (add-to-list 'bs-configurations
  1672. '("specials" "^\\*" nil ".*" nil nil))
  1673. (add-to-list 'bs-configurations
  1674. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1675. (defvar bs-mode-map)
  1676. (defvar bs-current-configuration)
  1677. (define-key bs-mode-map (kbd "t")
  1678. ;; TODO: fix toggle feature
  1679. (lambda ()
  1680. (interactive)
  1681. (if (string= "specials"
  1682. bs-current-configuration)
  1683. (bs-set-configuration "files")
  1684. (bs-set-configuration "specials"))
  1685. (bs-refresh)
  1686. (bs-message-without-log "%s"
  1687. (bs--current-config-message))))
  1688. ;; (setq bs-configurations (list
  1689. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1690. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1691. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1692. )
  1693. (when (fboundp 'bs-show)
  1694. (defalias 'list-buffers 'bs-show)
  1695. (set-variable 'bs-default-configuration "files-and-specials")
  1696. (set-variable 'bs-default-sort-name "by nothing")
  1697. (add-hook 'bs-mode-hook
  1698. (lambda ()
  1699. (set (make-local-variable 'scroll-margin) 0))))
  1700. ;;(iswitchb-mode 1)
  1701. (icomplete-mode)
  1702. (defun iswitchb-buffer-display-other-window ()
  1703. "Do iswitchb in other window."
  1704. (interactive)
  1705. (let ((iswitchb-default-method 'display))
  1706. (call-interactively 'iswitchb-buffer)))
  1707. ;; buffer killing
  1708. ;; (defun my-delete-window-killing-buffer () nil)
  1709. (defun my-query-kill-current-buffer ()
  1710. "Interactively kill current buffer."
  1711. (interactive)
  1712. (if (y-or-n-p (concat "kill current buffer? :"))
  1713. (kill-buffer (current-buffer))))
  1714. (defun my-force-query-kill-current-buffer ()
  1715. "Interactively kill current buffer."
  1716. (interactive)
  1717. (when (y-or-n-p (concat "kill current buffer? :"))
  1718. (let ((kill-buffer-hook nil)
  1719. (kill-buffer-query-functions nil))
  1720. (kill-buffer (current-buffer)))))
  1721. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1722. ;; Originally C-x C-k -> kmacro-keymap
  1723. ;; (global-set-key "\C-x\C-k" 'kmacro-keymap)
  1724. (global-set-key (kbd "C-x C-k") 'my-query-kill-current-buffer)
  1725. (substitute-key-definition 'kill-buffer
  1726. 'my-query-kill-current-buffer
  1727. global-map)
  1728. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1729. ;; dired
  1730. (defun my-file-head (filename &optional n)
  1731. "Return list of first N lines of file FILENAME."
  1732. ;; TODO: Fix for janapese text
  1733. ;; TODO: Fix for short text
  1734. (let ((num (or n 10))
  1735. (size 100)
  1736. (beg 0)
  1737. (end 0)
  1738. (result '())
  1739. (read -1))
  1740. (with-temp-buffer
  1741. (erase-buffer)
  1742. (while (or (<= (count-lines (point-min)
  1743. (point-max))
  1744. num)
  1745. (eq read 0))
  1746. (setq end (+ beg size))
  1747. (setq read (nth 1 (insert-file-contents-literally filename
  1748. nil
  1749. beg
  1750. end)))
  1751. (goto-char (point-max))
  1752. (setq beg (+ beg size)))
  1753. (goto-char (point-min))
  1754. (while (< (length result) num)
  1755. (let ((start (point)))
  1756. (forward-line 1)
  1757. (setq result
  1758. `(,@result ,(buffer-substring-no-properties start
  1759. (point))))))
  1760. result
  1761. ;; (buffer-substring-no-properties (point-min)
  1762. ;; (progn
  1763. ;; (forward-line num)
  1764. ;; (point)))
  1765. )))
  1766. ;; (apply 'concat (my-file-head "./shrc" 10)
  1767. (declare-function dired-get-filename "dired" t)
  1768. (defun my-dired-echo-file-head (arg)
  1769. "Echo head of current file.
  1770. ARG is num to show, or defaults to 7."
  1771. (interactive "P")
  1772. (let ((f (dired-get-filename)))
  1773. (message "%s"
  1774. (apply 'concat
  1775. (my-file-head f
  1776. 7)))))
  1777. (defun my-dired-diff ()
  1778. "Show diff of marked file and file of current line."
  1779. (interactive)
  1780. (let ((files (dired-get-marked-files nil nil nil t)))
  1781. (if (eq (car files)
  1782. t)
  1783. (diff (cadr files) (dired-get-filename))
  1784. (message "One file must be marked!"))))
  1785. (defun dired-get-file-info ()
  1786. "Print information of current line file."
  1787. (interactive)
  1788. (let* ((file (dired-get-filename t))
  1789. (quoted (shell-quote-argument file)))
  1790. (if (file-directory-p file)
  1791. (progn
  1792. (message "Calculating disk usage...")
  1793. (let ((du (or (executable-find "gdu")
  1794. (executable-find "du")
  1795. (error "du not found"))))
  1796. (shell-command (concat du
  1797. " -hsD "
  1798. quoted))))
  1799. (shell-command (concat "file "
  1800. quoted)))))
  1801. (defun my-dired-scroll-up ()
  1802. "Scroll up."
  1803. (interactive)
  1804. (my-dired-previous-line (- (window-height) 1)))
  1805. (defun my-dired-scroll-down ()
  1806. "Scroll down."
  1807. (interactive)
  1808. (my-dired-next-line (- (window-height) 1)))
  1809. ;; (defun my-dired-forward-line (arg)
  1810. ;; ""
  1811. ;; (interactive "p"))
  1812. (defun my-dired-previous-line (arg)
  1813. "Move ARG lines up."
  1814. (interactive "p")
  1815. (if (> arg 0)
  1816. (progn
  1817. (if (eq (line-number-at-pos)
  1818. 1)
  1819. (goto-char (point-max))
  1820. (forward-line -1))
  1821. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1822. (dired-get-subdir))
  1823. (- arg 1)
  1824. arg)))
  1825. (dired-move-to-filename)))
  1826. (defun my-dired-next-line (arg)
  1827. "Move ARG lines down."
  1828. (interactive "p")
  1829. (if (> arg 0)
  1830. (progn
  1831. (if (eq (point)
  1832. (point-max))
  1833. (goto-char (point-min))
  1834. (forward-line 1))
  1835. (my-dired-next-line (if (or (dired-get-filename nil t)
  1836. (dired-get-subdir))
  1837. (- arg 1)
  1838. arg)))
  1839. (dired-move-to-filename)))
  1840. (defun my-tramp-remote-find-file (f)
  1841. "Open F."
  1842. (interactive (list (read-file-name "My Find File Tramp: "
  1843. "/scp:"
  1844. nil ;; "/scp:"
  1845. (confirm-nonexistent-file-or-buffer))))
  1846. (find-file f))
  1847. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1848. (if (eq window-system 'mac)
  1849. (setq dired-listing-switches "-lhF")
  1850. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1851. )
  1852. (setq dired-listing-switches "-lhF")
  1853. ;; when using dired-find-alternate-file
  1854. ;; reuse current dired buffer for the file to open
  1855. ;; (put 'dired-find-alternate-file 'disabled nil)
  1856. (set-variable 'dired-ls-F-marks-symlinks t)
  1857. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1858. (set-variable 'ls-lisp-dirs-first t)
  1859. (set-variable 'ls-lisp-use-localized-time-format t)
  1860. (set-variable 'ls-lisp-format-time-list
  1861. '("%Y-%m-%d %H:%M"
  1862. "%Y-%m-%d "))
  1863. (set-variable 'dired-dwim-target t)
  1864. (set-variable 'dired-isearch-filenames t)
  1865. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1866. (set-variable 'dired-hide-details-hide-information-lines nil)
  1867. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1868. (set-variable 'dired-recursive-deletes 'always)
  1869. ;; (add-hook 'dired-after-readin-hook
  1870. ;; 'my-replace-nasi-none)
  1871. (with-eval-after-load 'dired
  1872. (require 'ls-lisp nil t)
  1873. (defvar dired-mode-map (make-sparse-keymap))
  1874. ;; dired-do-chgrp sometimes cause system hung
  1875. (define-key dired-mode-map "G" 'ignore)
  1876. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1877. (define-key dired-mode-map "i" 'dired-get-file-info)
  1878. ;; (define-key dired-mode-map "f" 'find-file)
  1879. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1880. (define-key dired-mode-map "z" 'fzf)
  1881. (define-key dired-mode-map "!" 'shell-command)
  1882. (define-key dired-mode-map "&" 'async-shell-command)
  1883. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1884. (define-key dired-mode-map "=" 'my-dired-diff)
  1885. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1886. (define-key dired-mode-map "b" 'gtkbm)
  1887. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1888. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1889. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1890. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1891. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1892. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1893. (substitute-key-definition 'dired-next-line
  1894. 'my-dired-next-line
  1895. dired-mode-map)
  1896. (substitute-key-definition 'dired-previous-line
  1897. 'my-dired-previous-line
  1898. dired-mode-map)
  1899. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1900. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1901. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1902. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1903. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1904. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1905. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1906. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1907. (add-hook 'dired-mode-hook
  1908. (lambda ()
  1909. (when (fboundp 'dired-hide-details-mode)
  1910. (dired-hide-details-mode t)
  1911. (local-set-key "l" 'dired-hide-details-mode))
  1912. (let ((file "._Icon\015"))
  1913. (when nil
  1914. '(file-readable-p file)
  1915. (delete-file file)))))
  1916. (when (fboundp 'pack-dired-dwim)
  1917. (with-eval-after-load 'dired
  1918. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1919. (when (fboundp 'dired-list-all-mode)
  1920. (setq dired-listing-switches "-lhF")
  1921. (with-eval-after-load 'dired
  1922. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1923. (when (fboundp 'dired-filter-mode)
  1924. (add-hook 'dired-mode-hook
  1925. 'dired-filter-mode))
  1926. (set-variable 'dired-filter-stack nil)
  1927. ;; Currently disabled in favor of dired-from-git-ls-files
  1928. ;; (define-key ctl-x-map "f" 'find-dired)
  1929. (defvar my-dired-git-ls-files-history
  1930. "History for `my-dired-git-ls-files'." nil)
  1931. (defun my-dired-git-ls-files (arg)
  1932. "Dired from git ls-files."
  1933. (interactive (list
  1934. (read-shell-command "git ls-files: "
  1935. "git ls-files -z ")))
  1936. (pop-to-buffer-same-window
  1937. (dired-noselect `(,default-directory
  1938. ,@(split-string (shell-command-to-string arg)
  1939. "\0" t))
  1940. ""))
  1941. )
  1942. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1943. (with-eval-after-load 'dired
  1944. (defvar dired-mode-map (make-sparse-keymap))
  1945. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1946. (with-eval-after-load 'pack
  1947. (set-variable 'pack-silence
  1948. t)
  1949. (defvar pack-program-alist)
  1950. (add-to-list 'pack-program-alist
  1951. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf"))
  1952. (when (executable-find "aunpack")
  1953. (add-to-list 'pack-program-alist
  1954. ' ("\\.zip\\'"
  1955. :pack ("zip" "-r" archive sources)
  1956. :pack-append ("zip" "-r" archive sources)
  1957. :unpack ("aunpack" archive))))
  1958. )
  1959. ;; (define-minor-mode my-dired-glob-filter)
  1960. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1961. ;; misc funcs
  1962. (define-key ctl-x-map "T" 'git-worktree)
  1963. (define-key ctl-x-map "W" 'git-walktree)
  1964. (when (fboundp 'browse-url-default-macosx-browser)
  1965. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1966. (defalias 'qcalc 'quick-calc)
  1967. (defun memo (&optional dir)
  1968. "Open memo.txt in DIR."
  1969. (interactive)
  1970. (pop-to-buffer (find-file-noselect (concat (if dir
  1971. (file-name-as-directory dir)
  1972. "")
  1973. "memo.txt"))))
  1974. ;; TODO: remember-projectile
  1975. (set (defvar my-privnotes-path nil
  1976. "My privnotes repository path.")
  1977. (expand-file-name "~/my/privnotes"))
  1978. (defun my-privnotes-readme (dir)
  1979. "Open my privnotes DIR."
  1980. (interactive (list
  1981. (read-file-name "Privnotes: "
  1982. (expand-file-name (format-time-string "%Y%m%d_")
  1983. my-privnotes-path))))
  1984. (let ((path (expand-file-name "README.md" dir)))
  1985. (with-current-buffer (find-file path)
  1986. (unless (file-exists-p path)
  1987. (insert (file-name-base dir)
  1988. "\n"
  1989. "=======\n"
  1990. "\n\n")))))
  1991. (define-key ctl-x-map "p" 'my-privnotes-readme)
  1992. (set (defvar my-rgrep-alist nil
  1993. "Alist of rgrep command.
  1994. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1995. condition to choose COMMAND when evaluated.")
  1996. `(
  1997. ;; ripgrep
  1998. ("rg"
  1999. (executable-find "rg")
  2000. "rg -nH --no-heading --hidden --glob '!.git/' --smart-case -M 1280 ")
  2001. ;; git grep
  2002. ("gitgrep"
  2003. (eq 0
  2004. (shell-command "git rev-parse --git-dir"))
  2005. "git --no-pager grep -nH -e ")
  2006. ;; sift
  2007. ("sift"
  2008. (executable-find "sift")
  2009. ("sift --binary-skip --filename --line-number --git --smart-case "))
  2010. ;; the silver searcher
  2011. ("ag"
  2012. (executable-find "ag")
  2013. "ag --nogroup --nopager --filename ")
  2014. ;; ack
  2015. ("ack"
  2016. (executable-find "ack")
  2017. "ack --nogroup --nopager --with-filename ")
  2018. ;; gnu global
  2019. ("global"
  2020. (and (require 'ggtags nil t)
  2021. (executable-find "global")
  2022. (ggtags-current-project-root))
  2023. "global --result grep ")
  2024. ;; grep
  2025. ("grep"
  2026. t
  2027. ,(concat "find . "
  2028. "-path '*/.git' -prune -o "
  2029. "-path '*/.svn' -prune -o "
  2030. "-type f -print0 | "
  2031. "xargs -0 grep -nH -e "))
  2032. )
  2033. )
  2034. (defvar my-rgrep-default nil
  2035. "Default command name for my-rgrep.")
  2036. (defun my-rgrep-grep-command (&optional name alist)
  2037. "Return recursive grep command for current directory or nil.
  2038. If NAME is given, use that without testing.
  2039. Commands are searched from ALIST."
  2040. (if alist
  2041. (if name
  2042. ;; if name is given search that from alist and return the command
  2043. (nth 2 (assoc name
  2044. alist))
  2045. ;; if name is not given try test in 1th elem
  2046. (let ((car (car alist))
  2047. (cdr (cdr alist)))
  2048. (if (eval (nth 1 car))
  2049. ;; if the condition is true return the command
  2050. (nth 2 car)
  2051. ;; try next one
  2052. (and cdr
  2053. (my-rgrep-grep-command name cdr)))))
  2054. ;; if alist is not given set default value
  2055. (my-rgrep-grep-command name my-rgrep-alist)))
  2056. (defun my-rgrep (command-args)
  2057. "My recursive grep. Run COMMAND-ARGS.
  2058. If prefix argument is given, use current symbol as default search target
  2059. and search from projectile root (if projectile is available)."
  2060. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2061. nil)))
  2062. (if cmd
  2063. (list (read-shell-command "grep command: "
  2064. (concat cmd
  2065. (if current-prefix-arg
  2066. (thing-at-point 'symbol t)
  2067. ""))
  2068. 'grep-find-history))
  2069. (error "My-Rgrep: Command for rgrep not found")
  2070. )))
  2071. (if (and current-prefix-arg
  2072. (eval-and-compile (require 'projectile nil t))
  2073. (projectile-project-p))
  2074. (projectile-with-default-dir (projectile-project-root)
  2075. (compilation-start command-args
  2076. 'grep-mode))
  2077. (compilation-start command-args
  2078. 'grep-mode)))
  2079. (defun my-rgrep-thing-at-point-projectile-root ()
  2080. "My recursive grep to find thing at point from project root."
  2081. (interactive)
  2082. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  2083. nil))
  2084. (command-args
  2085. (if cmd
  2086. (concat cmd
  2087. (or (thing-at-point 'symbol t)
  2088. (error "No symbol at point")))
  2089. (error "My-Rgrep: Command for rgrep not found"))))
  2090. (if (eval-and-compile (require 'projectile nil t))
  2091. (projectile-with-default-dir (or (projectile-project-root)
  2092. default-directory)
  2093. (compilation-start command-args
  2094. 'grep-mode))
  2095. (compilation-start command-args
  2096. 'grep-mode))))
  2097. (defmacro define-my-rgrep (name)
  2098. "Define rgrep for NAME."
  2099. `(defun ,(intern (concat "my-rgrep-"
  2100. name)) ()
  2101. ,(format "My recursive grep by %s."
  2102. name)
  2103. (interactive)
  2104. (let ((my-rgrep-default ,name))
  2105. (if (called-interactively-p 'any)
  2106. (call-interactively 'my-rgrep)
  2107. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2108. )
  2109. (define-my-rgrep "ack")
  2110. (define-my-rgrep "ag")
  2111. (define-my-rgrep "rg")
  2112. (define-my-rgrep "sift")
  2113. (define-my-rgrep "gitgrep")
  2114. (define-my-rgrep "grep")
  2115. (define-my-rgrep "global")
  2116. (define-key ctl-x-map "s" 'my-rgrep)
  2117. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  2118. (defun my-occur (regexp &optional region)
  2119. "My occur command to search REGEXP."
  2120. (interactive (list (read-string "List lines matching regexp: "
  2121. (thing-at-point 'symbol t))))
  2122. (occur regexp nil region))
  2123. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  2124. (set-variable 'dumb-jump-prefer-searcher 'rg)
  2125. (defalias 'make 'compile)
  2126. (define-key ctl-x-map "c" 'compile)
  2127. (defun my-pushbullet-note (text &optional title)
  2128. "Push TEXT."
  2129. (interactive "sText to Push: ")
  2130. (pb/push-item '("") text "note" (or title "")))
  2131. ;;;;;;;;;;;;;;;;;;;;;
  2132. ;; git-bug
  2133. (defconst git-bug-ls-regexp
  2134. (eval-when-compile
  2135. (rx bol
  2136. (submatch (one-or-more alphanumeric)) ; id
  2137. ;; (one-or-more any)
  2138. (one-or-more space)
  2139. (submatch (or "open" "close")) ; status
  2140. (one-or-more space)
  2141. (submatch (maximal-match (zero-or-more print))) ; title
  2142. "\t"
  2143. (submatch (one-or-more alphanumeric)) ; user
  2144. (one-or-more space)
  2145. "C:"
  2146. (submatch (one-or-more digit)) ; Comment num
  2147. (one-or-more space)
  2148. "L:"
  2149. (submatch (one-or-more digit)) ; Label num
  2150. eol
  2151. ))
  2152. "Regexp to parse line of output of git-bug ls.
  2153. Used by `git-bug-ls'.")
  2154. (defun git-bug-bugs ()
  2155. "Get list of git-bug bugs."
  2156. (with-temp-buffer
  2157. (git-bug--call-process "bug" "ls")
  2158. (goto-char (point-min))
  2159. (let ((bugs nil))
  2160. (while (not (eq (point) (point-max)))
  2161. (save-match-data
  2162. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  2163. (setq bugs `(,@bugs
  2164. ,(list
  2165. :id (match-string 1)
  2166. :status (match-string 2)
  2167. :title (string-trim (match-string 3))
  2168. :user (match-string 4)
  2169. :comment-num (match-string 5)
  2170. :label-num (match-string 6)
  2171. )))))
  2172. (forward-line 1)
  2173. (goto-char (point-at-bol)))
  2174. bugs)))
  2175. (defun git-bug-ls ()
  2176. "Open and select git bug list buffer."
  2177. (interactive)
  2178. (pop-to-buffer (git-bug-ls-noselect)))
  2179. (defun git-bug-ls-noselect (&optional directory)
  2180. "Open git bug list buffer.
  2181. If optional arg DIRECTORY is given change current directory to there before
  2182. initializing."
  2183. (setq directory (expand-file-name (or directory
  2184. default-directory)))
  2185. (cl-assert (file-directory-p directory))
  2186. (let* ((root (git-bug--get-repository-root directory))
  2187. (name (file-name-nondirectory root))
  2188. (bname (format "*GitBug<%s>*" name)))
  2189. (with-current-buffer (get-buffer-create bname)
  2190. (cd root)
  2191. (git-bug-ls--set-tabulated-list-mode-variables)
  2192. (git-bug-ls-mode)
  2193. (current-buffer))))
  2194. (defun git-bug--get-repository-root (dir)
  2195. "Resolve repository root of DIR.
  2196. If DIR is not inside of any git repository, signal an error."
  2197. (cl-assert (file-directory-p dir))
  2198. (with-temp-buffer
  2199. (cd dir)
  2200. (git-bug--call-process "rev-parse" "--show-toplevel")
  2201. (goto-char (point-min))
  2202. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  2203. (defun git-bug--call-process (&rest args)
  2204. "Start git process synchronously with ARGS.
  2205. Raise error when git process ends with non-zero status.
  2206. Any output will be written to current buffer."
  2207. (let ((status (apply 'call-process
  2208. "git"
  2209. nil
  2210. t
  2211. nil
  2212. args)))
  2213. (cl-assert (eq status 0)
  2214. nil
  2215. (buffer-substring-no-properties (point-min) (point-max)))))
  2216. ;;;;;;;;;;;;;;;;;;;
  2217. ;; peek-file-mode
  2218. (defvar peek-file-buffers
  2219. ()
  2220. "Peek buffers.")
  2221. (defun peek-file (file)
  2222. "Peek FILE."
  2223. (interactive "fFile to peek: ")
  2224. (with-current-buffer (find-file file)
  2225. (peek-file-mode)))
  2226. (define-minor-mode peek-file-mode
  2227. "Peek file mode."
  2228. :lighter "PK"
  2229. (view-mode peek-file-mode)
  2230. (add-to-list 'peek-file-buffers
  2231. (current-buffer))
  2232. (add-hook 'switch-buffer-functions
  2233. 'peek-file-remove-buffers))
  2234. (defun peek-file-remove-buffers (&args _)
  2235. "Remove peek file buffers."
  2236. (cl-dolist (buf (cl-copy-list peek-file-buffers))
  2237. (unless (get-buffer-window buf t)
  2238. (setq peek-file-buffers
  2239. (delq buf
  2240. peek-file-buffers))
  2241. (with-current-buffer buf
  2242. (when peek-file-mode
  2243. (kill-buffer))))))
  2244. ;;;;;;;;;;;;;;;;;;;;
  2245. ;; remember-projectile
  2246. ;; TODO: Add global-minor-mode
  2247. (defvar remember-data-file)
  2248. (defun my-set-remember-data-file-buffer-local ()
  2249. "Set `remember-data-file'."
  2250. (when (require 'projectile nil t)
  2251. (setq-local remember-data-file
  2252. (expand-file-name ".remember.notes"
  2253. (projectile-project-root)))))
  2254. (add-hook 'after-change-major-mode-hook
  2255. 'my-set-remember-data-file-buffer-local)
  2256. (define-key ctl-x-map "R" 'remember)
  2257. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2258. ;; ivy
  2259. ;; (set-variable 'enable-recursive-minibuffers t)
  2260. ;; (minibuffer-depth-indicate-mode 1)
  2261. (defun my--ivy-regex-fuzzy-ignore-order (str)
  2262. "Re-build regex from STR for ignore-order fuzzy match."
  2263. (let ((re-list (ivy--regex-ignore-order str)))
  2264. (if (listp re-list)
  2265. (mapcar (lambda (e)
  2266. (let ((head (car e))
  2267. (tail (cdr e)))
  2268. (if tail
  2269. (cons (ivy--regex-fuzzy head)
  2270. tail)
  2271. (cons head tail))))
  2272. re-list)
  2273. (ivy--regex-fuzzy re-list))))
  2274. ;; (my--ivy-regex-fuzzy-ignore-order "ab bc !cee")
  2275. ;; (ivy--regex-fuzzy "ab")
  2276. ;; (ivy--regex-ignore-order "a b")
  2277. (set-variable 'ivy-re-builders-alist
  2278. '((t . my--ivy-regex-fuzzy-ignore-order)))
  2279. ;; (set-variable 'ivy-format-functions-alist
  2280. ;; '((t . ivy-format-function-arrow)))
  2281. (set-variable 'ivy-format-functions-alist
  2282. '((t . (lambda (cands) (ivy--format-function-generic
  2283. (lambda (str)
  2284. (concat "-> "
  2285. (ivy--add-face str 'ivy-current-match)
  2286. ))
  2287. (lambda (str)
  2288. (concat "| " str))
  2289. cands
  2290. "\n")))))
  2291. (set-variable 'ivy-wrap t)
  2292. (set-variable 'ivy-sort-max-size 500)
  2293. (when (fboundp 'ivy-rich-mode)
  2294. (ivy-rich-mode 1))
  2295. (with-eval-after-load 'ivy
  2296. (defvar ivy-minibuffer-map)
  2297. (define-key ivy-minibuffer-map (kbd "C-u")
  2298. (lambda () (interactive) (delete-region (point-at-bol) (point)))))
  2299. (set-variable 'ivy-on-del-error-function 'ignore)
  2300. (when (fboundp 'counsel-M-x)
  2301. (define-key esc-map "x" 'counsel-M-x)
  2302. ;; (counsel-mode 1)
  2303. ;; counsel-fzf executes fzf -f QUERY for each input
  2304. ;; (define-key ctl-x-map "f"
  2305. ;; (lambda ()
  2306. ;; (interactive
  2307. ;; (let ((process-environment (cl-copy-list process-environment)))
  2308. ;; (setenv "FZF_DEFAULT_COMMAND" nil)
  2309. ;; (counsel-fzf)))))
  2310. )
  2311. ;; (when (fboundp 'counsel-switch-buffer)
  2312. ;; (define-key ctl-x-map (kbd "C-b") 'counsel-switch-buffer))
  2313. (when (and (fboundp 'ivy-read)
  2314. (locate-library "counsel"))
  2315. (defvar counsel-describe-map)
  2316. (defun my-counsel-describe-symbol ()
  2317. "Forwaord to `describe-symbol'."
  2318. (interactive)
  2319. (cl-assert (eval-and-compile (require 'help-mode nil t))) ;; describe-symbol-backends
  2320. (cl-assert (eval-and-compile (require 'counsel nil t)))
  2321. (ivy-read "Describe symbol: " obarray
  2322. ;; From describe-symbol definition
  2323. :predicate (lambda (vv)
  2324. (cl-some (lambda (x) (funcall (nth 1 x) vv))
  2325. describe-symbol-backends))
  2326. :require-match t
  2327. :history 'counsel-describe-symbol-history
  2328. :keymap counsel-describe-map
  2329. :preselect (ivy-thing-at-point)
  2330. :action (lambda (x)
  2331. (describe-symbol (intern x)))
  2332. :caller 'my-counsel-describe-symbol))
  2333. (define-key help-map "o" 'my-counsel-describe-symbol)
  2334. )
  2335. ;; (defun my-ivy-length (x y)
  2336. ;; "Ivy sort to order by string length."
  2337. ;; (<= (length (if (consp x) (car x) x))
  2338. ;; (length (if (consp y) (car y) y))))
  2339. ;; (my-ivy-length "a" (cons "bc" t))
  2340. ;; (my-ivy-length "a" (cons "c" t))
  2341. (with-eval-after-load 'ivy
  2342. ;; (ivy-configure 'my-counsel-describe-symbol
  2343. ;; :sort-fn 'my-ivy-length)
  2344. (ivy-configure 'counsel-M-x
  2345. :initial-input ""
  2346. ;; :sort-fn 'my-ivy-length
  2347. )
  2348. (defvar ivy-sort-matches-functions-alist)
  2349. (add-to-list 'ivy-sort-matches-functions-alist
  2350. '(my-counsel-describe-symbol . ivy--shorter-matches-first))
  2351. (add-to-list 'ivy-sort-matches-functions-alist
  2352. '(counsel-M-x . ivy--shorter-matches-first))
  2353. )
  2354. (when (fboundp 'counsel-imenu)
  2355. (define-key ctl-x-map "l" 'counsel-imenu))
  2356. (when (fboundp 'swoop)
  2357. ;; (global-set-key (kbd "C-s") 'swoop)
  2358. ;; (global-set-key (kbd "C-r") 'swoop)
  2359. ;; (define-key esc-map (kbd "C-s") 'swoop-multi)
  2360. (define-key esc-map (kbd "C-s") 'swoop)
  2361. (with-eval-after-load 'swoop-lib
  2362. (defvar swoop-map)
  2363. (define-key swoop-map (kbd "C-s") 'swoop-action-goto-line-next)
  2364. (define-key swoop-map (kbd "C-r") 'swoop-action-goto-line-prev)
  2365. )
  2366. ;; TODO: case sensitive swoop
  2367. ;; Wrap swoop-async-get-match-lines-list?
  2368. ;; (with-eval-after-load 'swoop-lib
  2369. ;; (defun my-swoop-advice-smart-case (orig-func &rest args)
  2370. ;; "Function to wrap swoop function."
  2371. ;; (message "args: %S" args)
  2372. ;; (let* (
  2373. ;; (query (car args))
  2374. ;; ;; (query (plist-get args
  2375. ;; ;; :$query))
  2376. ;; (case-fold-search
  2377. ;; (let ((case-fold-search nil))
  2378. ;; (not (string-match-p (rx upper) query))))
  2379. ;; )
  2380. ;; ;; (message "case-fold-search: %S" case-fold-search)
  2381. ;; ;; (message "query: %S" query)
  2382. ;; (apply orig-func args)))
  2383. ;; (advice-add 'swoop-async-get-match-lines-list
  2384. ;; :around
  2385. ;; 'my-swoop-advice-smart-case)
  2386. ;; (set-variable 'swoop-async-get-match-lines-list
  2387. ;; (byte-compile 'swoop-async-get-match-lines-list))
  2388. ;; )
  2389. )
  2390. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2391. ;; dired-k
  2392. ;; なんかよくわからないけど頻繁に index.lock を残してしまう
  2393. (when (fboundp 'dired-k)
  2394. (set-variable 'dired-k-style 'git)
  2395. ;; What is the best way of doing this?
  2396. (with-eval-after-load 'dired-k
  2397. (fset 'dired-k--highlight-by-file-attribyte 'ignore))
  2398. ;; (set-variable 'dired-k-size-colors
  2399. ;; `((,most-positive-fixnum)))
  2400. ;; (set-variable 'dired-k-date-colors
  2401. ;; `((,most-positive-fixnum)))
  2402. ;; always execute dired-k when dired buffer is opened and reverted
  2403. ;; (add-hook 'dired-after-readin-hook #'dired-k-no-revert)
  2404. ;; This causes:
  2405. ;; fatal: Unable to create '.git/index.lock': File exist.s
  2406. ;; (add-hook 'switch-buffer-functions
  2407. ;; (lambda (prev cur)
  2408. ;; (when (derived-mode-p 'dired-mode)
  2409. ;; (dired-k-no-revert))))
  2410. )
  2411. (add-hook 'python-mode-hook
  2412. (lambda ()
  2413. ;; Currently on python-mode eldoc-mode sometimes print
  2414. ;; wired message on "from" keyword:
  2415. ;;var from = require("./from")
  2416. (eldoc-mode -1)))
  2417. ;; ?
  2418. (define-key input-decode-map "\e[1;5C" [C-right])
  2419. (define-key input-decode-map "\e[1;5D" [C-left])
  2420. ;;;;;;;;;;;;;;;;;;;;;;;;
  2421. ;; mozc
  2422. (global-set-key (kbd "C-c m e") 'ignore)
  2423. (global-set-key (kbd "C-c m d") 'ignore)
  2424. ;; mozc
  2425. (when (locate-library "mozc")
  2426. ;; https://tottoto.net/mac-emacs-karabiner-elements-japanese-input-method-config/
  2427. (with-eval-after-load 'mozc
  2428. ;; (define-key mozc-mode-map (kbd "C-h") 'backward-delete-char)
  2429. ;; (define-key mozc-mode-map (kbd "C-p") (kbd "<up>"))
  2430. ;; (define-key mozc-mode-map (kbd "C-n") (kbd "SPC"))
  2431. )
  2432. (setq default-input-method "japanese-mozc")
  2433. (custom-set-variables '(mozc-leim-title "あ"))
  2434. (defun turn-on-input-method ()
  2435. (interactive)
  2436. (activate-input-method default-input-method))
  2437. (defun turn-off-input-method ()
  2438. (interactive)
  2439. (deactivate-input-method))
  2440. ;; (setq mozc-candidate-style 'echo-area)
  2441. (global-set-key (kbd "C-c m e") 'turn-on-input-method)
  2442. (global-set-key (kbd "C-c m d") 'turn-off-input-method)
  2443. (require 'mozc-popup)
  2444. (set-variable 'mozc-candidate-style 'popup)
  2445. ;; これいる?
  2446. (when (require 'mozc-im nil t)
  2447. (setq default-input-method "japanese-mozc-im")
  2448. ;; (global-set-key (kbd "C-j") 'toggle-input-method)
  2449. )
  2450. )
  2451. ;; Local Variables:
  2452. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2453. ;; flycheck-checker: emacs-lisp
  2454. ;; End:
  2455. ;;; emancs.el ends here