Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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