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.
 
 
 
 
 
 

2974 regels
98 KiB

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