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.
 
 
 
 
 
 

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