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

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