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.
 
 
 
 
 
 

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