您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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