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

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