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.
 
 
 
 
 
 

3007 lines
99 KiB

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