Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

2993 linhas
99 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (load "bytecomp") ;; Required for WSL environment
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  7. ;; TODO: Use custom-set-variables in place of set-variable
  8. (setq debug-on-error t)
  9. ;; make directories
  10. (unless (file-directory-p (expand-file-name user-emacs-directory))
  11. (make-directory (expand-file-name user-emacs-directory)))
  12. (require 'cl-lib)
  13. (require 'simple)
  14. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  15. ;; Some macros for internals
  16. (defvar after-first-visit-hook nil
  17. "Run only once at the first visit of file.")
  18. (defvar after-first-visit-hook--done nil
  19. "Non-nil when `after-first-visit-hook' has already been called.")
  20. (defun after-first-visit-hook-run ()
  21. "Run `after-first-visit-hook' and clear its config."
  22. (when (not after-first-visit-hook--done)
  23. (run-hooks 'after-first-visit-hook))
  24. (setq after-first-visit-hook--done t)
  25. (remove-hook 'find-file-hook
  26. 'after-first-visit-hook-run))
  27. (add-hook 'find-file-hook
  28. 'after-first-visit-hook-run)
  29. (defmacro eval-after-init (&rest body)
  30. "If `after-init-hook' has been run, run BODY immediately.
  31. Otherwize hook it."
  32. (declare (indent 0) (debug t))
  33. `(if after-init-time
  34. ;; Currently after-init-hook is run just after setting after-init-hook
  35. (progn
  36. ,@body)
  37. (add-hook 'after-init-hook
  38. (lambda ()
  39. ,@body))))
  40. ;; (defmacro safe-require-or-eval (feature)
  41. ;; "Require FEATURE if available.
  42. ;; At compile time the feature will be loaded immediately."
  43. ;; `(eval-and-compile
  44. ;; (message "safe-require-or-eval: Trying to require %s" ,feature)
  45. ;; (require ,feature nil t)))
  46. ;; (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  47. ;; "Define autoloading FEATURE that defines FUNCTIONS.
  48. ;; FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  49. ;; the function same as FEATURE is defined as autoloaded function. BODY is passed
  50. ;; to `eval-after-load'.
  51. ;; After this macro is expanded, this returns the path to library if FEATURE
  52. ;; found, otherwise returns nil."
  53. ;; (declare (indent 2) (debug t))
  54. ;; (let* ((libname (symbol-name (eval feature)))
  55. ;; (libpath (locate-library libname)))
  56. ;; `(progn
  57. ;; (when (locate-library ,libname)
  58. ;; ,@(mapcar (lambda (f)
  59. ;; `(unless (fboundp ',f)
  60. ;; (progn
  61. ;; (message "Autoloaded function `%S' defined (%s)"
  62. ;; (quote ,f)
  63. ;; ,libpath)
  64. ;; (autoload (quote ,f)
  65. ;; ,libname
  66. ;; ,(concat "Autoloaded function defined in \""
  67. ;; libpath
  68. ;; "\".")
  69. ;; t))))
  70. ;; (or (eval functions)
  71. ;; `(,(eval feature)))))
  72. ;; (eval-after-load ,feature
  73. ;; (quote (progn
  74. ;; ,@body)))
  75. ;; (locate-library ,libname))))
  76. ;; (when (autoload-eval-lazily 'tetris nil
  77. ;; (message "Tetris loaded!"))
  78. ;; (message "Tetris found!"))
  79. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  80. ;; package
  81. (set (defvar 10sr-package-list)
  82. '(
  83. vimrc-mode
  84. markdown-mode
  85. yaml-mode
  86. gnuplot-mode
  87. php-mode
  88. erlang
  89. js2-mode
  90. js-doc
  91. git-commit
  92. gitignore-mode
  93. adoc-mode
  94. go-mode
  95. ;; It seems malabar has been merged into jdee and this package
  96. ;; already removed
  97. ;; malabar-mode
  98. gosh-mode
  99. scala-mode
  100. web-mode
  101. toml-mode
  102. json-mode
  103. color-moccur
  104. ggtags
  105. flycheck
  106. auto-highlight-symbol
  107. hl-todo
  108. ;; Currently not available
  109. ;; pp-c-l
  110. xclip
  111. foreign-regexp
  112. multi-term
  113. term-run
  114. editorconfig
  115. git-ps1-mode
  116. restart-emacs
  117. fill-column-indicator
  118. pkgbuild-mode
  119. minibuffer-line
  120. which-key
  121. ;; I think this works in place of my autosave lib
  122. super-save
  123. pipenv
  124. imenu-list
  125. page-break-lines
  126. aggressive-indent
  127. dired-filter
  128. wgrep
  129. magit
  130. git-gutter
  131. end-mark
  132. sl
  133. ;; TODO: Configure pony-tpl-mode
  134. pony-mode
  135. gited
  136. highlight-indentation
  137. diminish
  138. fzf
  139. fic-mode
  140. term-cursor
  141. pydoc
  142. color-identifiers-mode
  143. dired-k
  144. blacken
  145. back-button
  146. with-venv
  147. nyan-mode
  148. editorconfig
  149. editorconfig-custom-majormode
  150. git-command
  151. prompt-text
  152. ;; 10sr repository
  153. ;; 10sr-extras
  154. terminal-title
  155. dired-list-all-mode
  156. pack
  157. set-modeline-color
  158. read-only-only-mode
  159. smart-revert
  160. autosave
  161. ;;window-organizer
  162. ilookup
  163. pasteboard
  164. awk-preview
  165. recently
  166. ))
  167. (require 'package)
  168. (set-variable 'package-archives
  169. `(,@package-archives
  170. ("melpa" . "https://melpa.org/packages/")
  171. ;; Somehow fails to download via https
  172. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  173. (package-initialize)
  174. (defun my-auto-install-package ()
  175. "Install packages semi-automatically."
  176. (interactive)
  177. (package-refresh-contents)
  178. (mapc (lambda (pkg)
  179. (or (package-installed-p pkg)
  180. (package-install pkg)))
  181. 10sr-package-list))
  182. ;; (lazy-load-eval 'sudoku)
  183. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  184. ;; my-idle-hook
  185. (defvar my-idle-hook nil
  186. "Hook run when idle for several secs.")
  187. (defvar my-idle-hook-sec 5
  188. "Second to run `my-idle-hook'.")
  189. (run-with-idle-timer my-idle-hook-sec
  190. t
  191. (lambda ()
  192. (run-hooks 'my-idle-hook)))
  193. ;; (add-hook 'my-idle-hook
  194. ;; (lambda ()
  195. ;; (message "idle hook message")))
  196. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  197. ;; start and quit
  198. (setq inhibit-startup-message t)
  199. (setq initial-buffer-choice 'messages-buffer)
  200. (setq confirm-kill-emacs 'y-or-n-p)
  201. (setq gc-cons-threshold (* 1024 1024 16))
  202. (setq garbage-collection-messages nil)
  203. (when window-system
  204. (add-to-list 'default-frame-alist '(cursor-type . box))
  205. (add-to-list 'default-frame-alist '(background-color . "white"))
  206. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  207. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  208. ;; does not work?
  209. )
  210. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  211. (menu-bar-mode 1)
  212. (define-key ctl-x-map "M" 'menu-bar-open)
  213. (defalias 'menu 'menu-bar-open)
  214. (and (fboundp 'tool-bar-mode)
  215. (tool-bar-mode 0))
  216. (and (fboundp 'set-scroll-bar-mode)
  217. (set-scroll-bar-mode nil))
  218. (eval-after-init
  219. (message "%s %s" invocation-name emacs-version)
  220. (message "Invocation directory: %s" default-directory)
  221. (message "%s was taken to initialize emacs." (emacs-init-time))
  222. ;; (view-echo-area-messages)
  223. ;; (view-emacs-news)
  224. )
  225. (with-current-buffer "*Messages*"
  226. (emacs-lock-mode 'kill))
  227. (cd ".") ; when using windows use / instead of \ in `default-directory'
  228. ;; locale
  229. (set-language-environment "Japanese")
  230. (set-default-coding-systems 'utf-8-unix)
  231. (prefer-coding-system 'utf-8-unix)
  232. (setq system-time-locale "C")
  233. ;; my prefix map
  234. (defvar my-prefix-map nil
  235. "My prefix map.")
  236. (define-prefix-command 'my-prefix-map)
  237. (global-set-key (kbd "C-^") 'my-prefix-map)
  238. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  239. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  240. ;; (comint-show-maximum-output)
  241. ;; kill scratch
  242. (eval-after-init
  243. (let ((buf (get-buffer "*scratch*")))
  244. (when buf
  245. (kill-buffer buf))))
  246. ;; modifier keys
  247. ;; (setq mac-option-modifier 'control)
  248. ;; display
  249. (setq visible-bell t)
  250. (setq ring-bell-function 'ignore)
  251. (mouse-avoidance-mode 'banish)
  252. (setq echo-keystrokes 0.1)
  253. (defun reload-init-file ()
  254. "Reload Emacs init file."
  255. (interactive)
  256. (when (and user-init-file
  257. (file-readable-p user-init-file))
  258. (load-file user-init-file)))
  259. (require 'session nil t)
  260. ;; server
  261. (set-variable 'server-name (concat "server"
  262. (number-to-string (emacs-pid))))
  263. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  264. ;; In Darwin environment, init fails with message like 'Service name too long'
  265. ;; when server-use-tcp is nil
  266. (when (or (eq system-type
  267. 'cygwin)
  268. (eq system-type
  269. 'darwin))
  270. (set-variable 'server-use-tcp t))
  271. ;; MSYS2 fix
  272. (when (eq system-type
  273. 'windows-nt)
  274. (setq shell-file-name
  275. (executable-find "bash"))
  276. '(setq function-key-map
  277. `(,@function-key-map ([pause] . [?\C-c])
  278. ))
  279. (define-key key-translation-map
  280. (kbd "<pause>")
  281. (kbd "C-c"))
  282. '(keyboard-translate [pause]
  283. (kbd "C-c")p)
  284. ;; TODO: move to other place later
  285. (when (not window-system)
  286. (setq interprogram-paste-function nil)
  287. (setq interprogram-cut-function nil)))
  288. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  289. ;; global keys
  290. (global-set-key (kbd "<up>") 'scroll-down-line)
  291. (global-set-key (kbd "<down>") 'scroll-up-line)
  292. (global-set-key (kbd "<left>") 'scroll-down)
  293. (global-set-key (kbd "<right>") 'scroll-up)
  294. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  295. ;; (global-set-key (kbd "C-\\") help-map)
  296. (define-key ctl-x-map (kbd "DEL") help-map)
  297. (define-key ctl-x-map (kbd "C-h") help-map)
  298. ;; This is often fired mistakenly
  299. (define-key ctl-x-map "h" 'ignore) ;; Previously mark-whole-buffer
  300. (define-key help-map "a" 'apropos)
  301. ;; disable annoying keys
  302. (global-set-key [prior] 'ignore)
  303. (global-set-key (kbd "<next>") 'ignore)
  304. (global-set-key [menu] 'ignore)
  305. (global-set-key [down-mouse-1] 'ignore)
  306. (global-set-key [down-mouse-2] 'ignore)
  307. (global-set-key [down-mouse-3] 'ignore)
  308. (global-set-key [mouse-1] 'ignore)
  309. (global-set-key [mouse-2] 'ignore)
  310. (global-set-key [mouse-3] 'ignore)
  311. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  312. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  313. ;; Interactively evaluate Emacs Lisp expressions
  314. (define-key ctl-x-map "i" 'ielm)
  315. (when (fboundp 'which-key-mode)
  316. (which-key-mode))
  317. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  318. ;; editor
  319. ;; Basically it should not set globally (instead use something like file local
  320. ;; variables or editorconfig), but for most cases I just need this...
  321. (defun my-set-require-final-newline ()
  322. "Set `require-final-newline'."
  323. (set (make-local-variable 'require-final-newline)
  324. mode-require-final-newline))
  325. (add-hook 'prog-mode-hook
  326. 'my-set-require-final-newline)
  327. (add-hook 'text-mode-hook
  328. 'my-set-require-final-newline)
  329. (add-hook 'conf-mode-hook
  330. 'my-set-require-final-newline)
  331. ;; Used from term-cursor
  332. ;; hbar is too hard to find...
  333. (defun my-cursor-type-change (&rest args)
  334. "ARGS are discarded."
  335. ;; TODO: Support wdired and wgrep
  336. (if buffer-read-only
  337. (setq cursor-type 'hbar)
  338. (setq cursor-type 'box)))
  339. ;; (add-hook 'switch-buffer-functions
  340. ;; 'my-cursor-type-change)
  341. ;; (add-hook 'read-only-mode-hook
  342. ;; 'my-cursor-type-change)
  343. ;; (when (fboundp 'global-term-cursor-mode)
  344. ;; (global-term-cursor-mode 1))
  345. ;; ;; (term-cursor--eval)
  346. (setq kill-whole-line t)
  347. (setq scroll-conservatively 35
  348. scroll-margin 2)
  349. (setq-default major-mode 'text-mode)
  350. (setq next-line-add-newlines nil)
  351. (setq kill-read-only-ok t)
  352. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  353. ;; (setq-default line-spacing 0.2)
  354. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  355. ;; (setq-default tab-width 4)
  356. (setq-default indent-tabs-mode nil)
  357. (setq-default indent-line-function 'indent-to-left-margin)
  358. ;; (setq-default indent-line-function nil)
  359. (setq-default truncate-lines nil)
  360. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  361. (delete-selection-mode 1)
  362. (cua-mode 0)
  363. (setq line-move-visual nil)
  364. (setq create-lockfiles nil)
  365. (setq set-mark-command-repeat-pop t)
  366. (add-hook 'before-save-hook
  367. 'time-stamp)
  368. ;; Add Time-stamp: <> to insert timestamp there
  369. (set-variable 'time-stamp-format
  370. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  371. ;; key bindings
  372. ;; moving around
  373. ;;(keyboard-translate ?\M-j ?\C-j)
  374. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  375. (define-key esc-map "p" 'backward-paragraph)
  376. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  377. (define-key esc-map "n" 'forward-paragraph)
  378. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  379. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  380. (global-set-key (kbd "C-<left>") 'scroll-down)
  381. (global-set-key (kbd "C-<right>") 'scroll-up)
  382. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  383. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  384. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  385. ;; C-h and DEL
  386. (global-set-key (kbd "C-h") (kbd "DEL"))
  387. ;; (normal-erase-is-backspace-mode 1)
  388. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  389. (global-set-key (kbd "C-m") 'newline-and-indent)
  390. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  391. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  392. (define-key esc-map "u" 'undo)
  393. (define-key esc-map "i" (kbd "ESC TAB"))
  394. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  395. ;; (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  396. ;; (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  397. (set-variable 'search-default-mode t)
  398. ;; TODO: Do not depend on ivy function
  399. ;; (set-variable 'search-default-mode
  400. ;; (lambda (str lax)
  401. ;; (ivy--regex-fuzzy (replace-regexp-in-string (rx (one-or-more whitespace))
  402. ;; ""
  403. ;; str))))
  404. (when (fboundp 'undo-fu-only-undo)
  405. (global-set-key (kbd "C-_") 'undo-fu-only-undo))
  406. (when (fboundp 'undo-fu-only-redo)
  407. (global-set-key (kbd "C-M-_") 'undo-fu-only-redo))
  408. (require 'page-ext nil t)
  409. (when (fboundp 'global-page-break-lines-mode)
  410. (add-hook 'after-first-visit-hook
  411. 'global-page-break-lines-mode))
  412. (with-eval-after-load 'page-break-lines
  413. (set-face-foreground 'page-break-lines
  414. "cyan")
  415. )
  416. (defun my-insert-page-break ()
  417. "Insert ^L."
  418. (interactive)
  419. (insert "\^L\n"))
  420. (when (fboundp 'global-git-gutter-mode)
  421. (add-hook 'after-first-visit-hook
  422. 'global-git-gutter-mode))
  423. (with-eval-after-load 'git-gutter
  424. (declare-function global-git-gutter-mode "git-gutter")
  425. (custom-set-variables
  426. '(git-gutter:lighter " Gttr"))
  427. (custom-set-variables
  428. '(git-gutter:update-interval 2))
  429. (custom-set-variables
  430. '(git-gutter:unchanged-sign " "))
  431. (when (>= (display-color-cells)
  432. 256)
  433. (let ((c "color-233"))
  434. (set-face-background 'git-gutter:modified c)
  435. (set-face-background 'git-gutter:added c)
  436. (set-face-background 'git-gutter:deleted c)
  437. (set-face-background 'git-gutter:unchanged c))))
  438. ;; (when (fboundp 'fancy-narrow-mode)
  439. ;; (add-hook 'after-first-visit-hook
  440. ;; 'fancy-narrow-mode))
  441. ;; https://solist.work/blog/posts/mark-ring/
  442. (defun my-exchange-point-and-mark ()
  443. "`exchange-point-and-mark' without mark activation."
  444. (interactive)
  445. (exchange-point-and-mark)
  446. (deactivate-mark))
  447. (define-key ctl-x-map (kbd "C-x") 'my-exchange-point-and-mark)
  448. (when (fboundp 'counsel-mark-ring)
  449. (define-key ctl-x-map "m" 'counsel-mark-ring))
  450. (with-eval-after-load 'ivy
  451. (defvar ivy-sort-functions-alist)
  452. (add-to-list 'ivy-sort-functions-alist
  453. '(counsel-mark-ring)))
  454. (run-with-idle-timer 10 t
  455. (lambda ()
  456. (push-mark)
  457. ;; (when (fboundp 'visible-mark-move-overlays)
  458. ;; (visible-mark-move-overlays))
  459. ))
  460. (add-hook '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. (declare-function company-cancel "company")
  1292. (when (fboundp 'global-company-mode)
  1293. (add-hook 'after-first-visit-hook
  1294. 'global-company-mode))
  1295. (with-eval-after-load 'company
  1296. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1297. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1298. (set-variable 'company-idle-delay nil)
  1299. (set-variable 'company-minimum-prefix-length 2)
  1300. (set-variable 'company-selection-wrap-around t)
  1301. (defvar company-mode-map)
  1302. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1303. ;; (with-eval-after-load 'python
  1304. ;; (defvar python-indent-trigger-commands)
  1305. ;; ;; TODO: This disables completion in puthon?
  1306. ;; (add-to-list 'python-indent-trigger-commands
  1307. ;; 'company-indent-or-complete-common))
  1308. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1309. (defvar company-active-map)
  1310. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1311. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1312. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1313. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1314. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1315. (defvar company-mode)
  1316. (defvar company-candidates)
  1317. (defvar company-candidates-length)
  1318. ;; (popup-tip "Hello, World!")
  1319. (defun my-company-lighter-current-length ()
  1320. "Get current candidate length."
  1321. (interactive)
  1322. (let ((l nil)
  1323. (inhibit-message t))
  1324. (when (and company-mode
  1325. (not (minibufferp))
  1326. ;; Do nothing when already in company completion
  1327. (not company-candidates))
  1328. ;; FIXME: Somehow it cannto catch errors from ggtags
  1329. (ignore-errors
  1330. ;; (company-auto-begin)
  1331. (company-manual-begin)
  1332. (setq l company-candidates-length)
  1333. (company-cancel)))
  1334. (if l
  1335. (format "[%d]" l)
  1336. "")))
  1337. (defvar company-lighter)
  1338. (set-variable 'company-lighter-base "Cmp")
  1339. ;; (add-to-list 'company-lighter
  1340. ;; '(:eval (my-company-lighter-current-length))
  1341. ;; t)
  1342. ;; This breaks japanese text input
  1343. ;; (set-variable 'my-company-length-popup-tip-timer
  1344. ;; (run-with-idle-timer 0.2 t
  1345. ;; 'my-company-length-popup-tip))
  1346. ;; (current-active-maps)
  1347. ;; (lookup-key)
  1348. '(mapcar (lambda (map)
  1349. (lookup-key map (kbd "C-i")))
  1350. (current-active-maps))
  1351. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1352. (set-face-attribute 'company-tooltip nil
  1353. :foreground "black" :background "lightgrey")
  1354. (set-face-attribute 'company-tooltip-common nil
  1355. :foreground "black" :background "lightgrey")
  1356. (set-face-attribute 'company-tooltip-common-selection nil
  1357. :foreground "white" :background "steelblue")
  1358. (set-face-attribute 'company-tooltip-selection nil
  1359. :foreground "black" :background "steelblue")
  1360. (set-face-attribute 'company-preview-common nil
  1361. :background nil :foreground "lightgrey" :underline t)
  1362. (set-face-attribute 'company-scrollbar-fg nil
  1363. :background "orange")
  1364. (set-face-attribute 'company-scrollbar-bg nil
  1365. :background "gray40")
  1366. )
  1367. ;; https://github.com/lunaryorn/flycheck
  1368. ;; TODO: Any way to disable auto check?
  1369. ;; Update flycheck-hooks-alist?
  1370. (when (fboundp 'global-flycheck-mode)
  1371. (add-hook 'after-first-visit-hook
  1372. 'global-flycheck-mode))
  1373. ;; (set-variable 'flycheck-display-errors-delay 2.0)
  1374. ;; (fset 'flycheck-display-error-at-point-soon 'ignore)
  1375. ;; (with-eval-after-load 'flycheck
  1376. ;; (when (fboundp 'flycheck-black-check-setup)
  1377. ;; (flycheck-black-check-setup)))
  1378. (when (fboundp 'ilookup-open-word)
  1379. (define-key ctl-x-map "d" 'ilookup-open-word))
  1380. (set-variable 'ac-ignore-case nil)
  1381. (when (fboundp 'term-run-shell-command)
  1382. (define-key ctl-x-map "t" 'term-run-shell-command))
  1383. (add-to-list 'safe-local-variable-values
  1384. '(encoding utf-8))
  1385. (setq enable-local-variables :safe)
  1386. ;; Detect file type from shebang and set major-mode.
  1387. (add-to-list 'interpreter-mode-alist
  1388. '("python3" . python-mode))
  1389. (add-to-list 'interpreter-mode-alist
  1390. '("python2" . python-mode))
  1391. (with-eval-after-load 'python
  1392. (defvar python-mode-map (make-sparse-keymap))
  1393. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1394. ;; I want to use this, but this breaks normal self-insert-command
  1395. ;; (set-variable 'py-indent-list-style
  1396. ;; 'one-level-to-beginning-of-statement)
  1397. (set-variable 'pydoc-command
  1398. "python3 -m pydoc")
  1399. (with-eval-after-load 'pydoc
  1400. (when (require 'with-venv nil t)
  1401. (with-venv-advice-add 'pydoc)))
  1402. (set-variable 'flycheck-python-mypy-ini ".mypy.ini")
  1403. (set-variable 'flycheck-flake8rc "setup.cfg")
  1404. (set-variable 'flycheck-python-pylint-executable "python3")
  1405. (set-variable 'flycheck-python-pycompile-executable "python3")
  1406. (set-variable 'python-indent-guess-indent-offset nil)
  1407. (with-eval-after-load 'blacken
  1408. (when (require 'with-venv nil t)
  1409. (with-venv-advice-add 'blacken-buffer)))
  1410. (with-eval-after-load 'ansible-doc
  1411. (when (require 'with-venv nil t)
  1412. (with-venv-advice-add 'ansible-doc)))
  1413. ;; `isortify-buffer' breaks buffer when it contains japanese text
  1414. (defun my-isortify ()
  1415. (interactive)
  1416. (cl-assert buffer-file-name)
  1417. (cl-assert (not (buffer-modified-p)))
  1418. (call-process "python" ;; PROGRAM
  1419. nil ;; INFILE
  1420. nil ;; DESTINATION
  1421. nil ;; DISPLAY
  1422. "-m" "isort" buffer-file-name)
  1423. (message "isortify done")
  1424. (revert-buffer nil t))
  1425. (when (fboundp 'with-venv-advice-add)
  1426. ;; TODO: Lazy load with-venv
  1427. (with-venv-advice-add 'my-isortify))
  1428. ;; https://github.com/lunaryorn/old-emacs-configuration/blob/master/lisp/flycheck-virtualenv.el
  1429. (defun my-set-venv-flycheck-executable-find ()
  1430. "Set flycheck executabie find."
  1431. (interactive)
  1432. (when (fboundp 'with-venv)
  1433. (set-variable 'flycheck-executable-find
  1434. '(lambda (e)
  1435. (with-venv
  1436. (executable-find e)))
  1437. t)))
  1438. (defun my-update-flycheck-flake8-error-level-alist ()
  1439. "Update `flycheck-flake8-error-level-alist'."
  1440. (defvar flycheck-flake8-error-level-alist)
  1441. ;; (add-to-list 'flycheck-flake8-error-level-alist
  1442. ;; '("^D.*$" . warning))
  1443. (set-variable 'flycheck-flake8-error-level-alist
  1444. nil)
  1445. )
  1446. (add-hook 'python-mode-hook
  1447. 'my-set-venv-flycheck-executable-find)
  1448. (add-hook 'python-mode-hook
  1449. 'my-update-flycheck-flake8-error-level-alist)
  1450. (when (fboundp 'with-venv-info-mode)
  1451. (add-hook 'python-mode-hook
  1452. 'with-venv-info-mode))
  1453. ;; Run multiple chekcers
  1454. ;; https://github.com/flycheck/flycheck/issues/186
  1455. (add-hook 'python-mode-hook
  1456. (lambda ()
  1457. ;; Currently on python-mode eldoc-mode sometimes print
  1458. ;; wired message on "from" keyword:
  1459. ;;var from = require("./from")
  1460. (eldoc-mode -1)))
  1461. ;; http://fukuyama.co/foreign-regexp
  1462. '(and (require 'foreign-regexp nil t)
  1463. (progn
  1464. (setq foreign-regexp/regexp-type 'perl)
  1465. '(setq reb-re-syntax 'foreign-regexp)
  1466. ))
  1467. (with-eval-after-load 'sql
  1468. (require 'sql-indent nil t))
  1469. (add-to-list 'auto-mode-alist
  1470. '("\\.hql\\'" . sql-mode))
  1471. (when (fboundp 'git-command)
  1472. (define-key ctl-x-map "g" 'git-command))
  1473. (when (fboundp 'gited-list)
  1474. (defalias 'gited 'gited-list))
  1475. (when (fboundp 'global-git-commit-mode)
  1476. (add-hook 'after-first-visit-hook
  1477. 'global-git-commit-mode)
  1478. (add-hook 'after-first-visit-hook
  1479. 'git-commit-setup-check-buffer))
  1480. (with-eval-after-load 'git-commit
  1481. (add-hook 'git-commit-setup-hook
  1482. 'turn-off-auto-fill t))
  1483. (with-eval-after-load 'rst
  1484. (defvar rst-mode-map)
  1485. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1486. (with-eval-after-load 'jdee
  1487. (add-hook 'jdee-mode-hook
  1488. (lambda ()
  1489. (make-local-variable 'global-mode-string)
  1490. (add-to-list 'global-mode-string
  1491. mode-line-position))))
  1492. ;; Cannot enable error thrown. Why???
  1493. ;; https://github.com/m0smith/malabar-mode#Installation
  1494. ;; (when (require 'malabar-mode nil t)
  1495. ;; (add-to-list 'load-path
  1496. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1497. ;; (require 'cedet-devel-load nil t)
  1498. ;; (eval-after-init (activate-malabar-mode)))
  1499. (with-eval-after-load 'make-mode
  1500. (defvar makefile-mode-map (make-sparse-keymap))
  1501. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1502. ;; this functions is set in write-file-functions, i cannot find any
  1503. ;; good way to remove this.
  1504. (fset 'makefile-warn-suspicious-lines 'ignore))
  1505. (with-eval-after-load 'verilog-mode
  1506. (defvar verilog-mode-map (make-sparse-keymap))
  1507. (define-key verilog-mode-map ";" 'self-insert-command))
  1508. (setq diff-switches "-u")
  1509. (autoload 'diff-goto-source "diff-mode" nil t)
  1510. (with-eval-after-load 'diff-mode
  1511. ;; (when (and (eq major-mode
  1512. ;; 'diff-mode)
  1513. ;; (not buffer-file-name))
  1514. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1515. ;; (view-mode 1))
  1516. (set-face-attribute 'diff-header nil
  1517. :foreground nil
  1518. :background nil
  1519. :weight 'bold)
  1520. (set-face-attribute 'diff-file-header nil
  1521. :foreground nil
  1522. :background nil
  1523. :weight 'bold)
  1524. (set-face-foreground 'diff-index "blue")
  1525. (set-face-attribute 'diff-hunk-header nil
  1526. :foreground "cyan"
  1527. :weight 'normal)
  1528. (set-face-attribute 'diff-context nil
  1529. ;; :foreground "white"
  1530. :foreground nil
  1531. :weight 'normal)
  1532. (set-face-foreground 'diff-removed "red")
  1533. (set-face-foreground 'diff-added "green")
  1534. (set-face-background 'diff-removed nil)
  1535. (set-face-background 'diff-added nil)
  1536. (set-face-attribute 'diff-changed nil
  1537. :foreground "magenta"
  1538. :weight 'normal)
  1539. (set-face-attribute 'diff-refine-changed nil
  1540. :foreground nil
  1541. :background nil
  1542. :weight 'bold
  1543. :inverse-video t)
  1544. ;; Annoying !
  1545. ;;(diff-auto-refine-mode)
  1546. )
  1547. ;; (ffap-bindings)
  1548. (set-variable 'browse-url-browser-function
  1549. 'eww-browse-url)
  1550. (set-variable 'sh-here-document-word "__EOC__")
  1551. (with-eval-after-load 'adoc-mode
  1552. (defvar adoc-mode-map)
  1553. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1554. (when (fboundp 'adoc-mode)
  1555. (setq auto-mode-alist
  1556. `(("\\.adoc\\'" . adoc-mode)
  1557. ("\\.asciidoc\\'" . adoc-mode)
  1558. ,@auto-mode-alist)))
  1559. (with-eval-after-load 'markup-faces
  1560. ;; Is this too match ?
  1561. (set-face-foreground 'markup-meta-face
  1562. "color-245")
  1563. (set-face-foreground 'markup-meta-hide-face
  1564. "color-245")
  1565. )
  1566. ;; TODO: check if this is required
  1567. (with-eval-after-load 'groovy-mode
  1568. (defvar groovy-mode-map)
  1569. (define-key groovy-mode-map "(" 'self-insert-command)
  1570. (define-key groovy-mode-map ")" 'self-insert-command)
  1571. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1572. )
  1573. (when (fboundp 'groovy-mode)
  1574. (add-to-list 'auto-mode-alist
  1575. '("build\\.gradle\\'" . groovy-mode)))
  1576. (add-to-list 'auto-mode-alist
  1577. '("\\.gawk\\'" . awk-mode))
  1578. (with-eval-after-load 'yaml-mode
  1579. (defvar yaml-mode-map (make-sparse-keymap))
  1580. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1581. (with-eval-after-load 'html-mode
  1582. (defvar html-mode-map (make-sparse-keymap))
  1583. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1584. (with-eval-after-load 'text-mode
  1585. (define-key text-mode-map (kbd "C-m") 'newline))
  1586. (with-eval-after-load 'info
  1587. (defvar Info-additional-directory-list)
  1588. (dolist (dir (directory-files (concat user-emacs-directory
  1589. "info")
  1590. t
  1591. "^[^.].*"))
  1592. (when (file-directory-p dir)
  1593. (add-to-list 'Info-additional-directory-list
  1594. dir)))
  1595. (let ((dir (expand-file-name "~/.brew/share/info")))
  1596. (when (file-directory-p dir)
  1597. (add-to-list 'Info-additional-directory-list
  1598. dir))))
  1599. (with-eval-after-load 'apropos
  1600. (defvar apropos-mode-map (make-sparse-keymap))
  1601. (define-key apropos-mode-map "n" 'next-line)
  1602. (define-key apropos-mode-map "p" 'previous-line))
  1603. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1604. ;; (define-key isearch-mode-map
  1605. ;; (kbd "C-j") 'isearch-other-control-char)
  1606. ;; (define-key isearch-mode-map
  1607. ;; (kbd "C-k") 'isearch-other-control-char)
  1608. ;; (define-key isearch-mode-map
  1609. ;; (kbd "C-h") 'isearch-other-control-char)
  1610. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1611. (define-key isearch-mode-map (kbd "M-r")
  1612. 'isearch-query-replace-regexp)
  1613. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1614. (setq lazy-highlight-cleanup nil)
  1615. ;; face for isearch highlighing
  1616. (set-face-attribute 'lazy-highlight
  1617. nil
  1618. :foreground `unspecified
  1619. :background `unspecified
  1620. :underline t
  1621. ;; :weight `bold
  1622. )
  1623. (add-hook 'outline-mode-hook
  1624. (lambda ()
  1625. (when (string-match "\\.md\\'" buffer-file-name)
  1626. (setq-local outline-regexp "#+ "))))
  1627. (add-hook 'outline-mode-hook
  1628. 'outline-show-all)
  1629. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1630. (with-eval-after-load 'markdown-mode
  1631. (defvar gfm-mode-map)
  1632. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1633. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1634. )
  1635. (when (fboundp 'gfm-mode)
  1636. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1637. (add-hook 'markdown-mode-hook
  1638. 'outline-minor-mode)
  1639. (add-hook 'markdown-mode-hook
  1640. (lambda ()
  1641. (setq-local comment-start ";")))
  1642. )
  1643. ;; http://keisanbutsuriya.hateblo.jp/entry/2015/02/10/152543
  1644. ;; M-$ to ispell word
  1645. ;; M-x flyspell-buffer to highlight all suspicious words
  1646. (when (executable-find "aspell")
  1647. (set-variable 'ispell-program-name "aspell")
  1648. (set-variable 'ispell-extra-args '("--lang=en_US")))
  1649. (with-eval-after-load 'ispell
  1650. (add-to-list 'ispell-skip-region-alist '("[^\000-\377]+")))
  1651. (when (fboundp 'flyspell-mode)
  1652. (add-hook 'text-mode-hook
  1653. 'flyspell-mode))
  1654. (when (fboundp 'flyspell-prog-mode)
  1655. (add-hook 'prog-mode-hook
  1656. 'flyspell-prog-mode))
  1657. ;; c-mode
  1658. ;; http://www.emacswiki.org/emacs/IndentingC
  1659. ;; http://en.wikipedia.org/wiki/Indent_style
  1660. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1661. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1662. (with-eval-after-load 'cc-vars
  1663. (defvar c-default-style nil)
  1664. (add-to-list 'c-default-style
  1665. '(c-mode . "k&r"))
  1666. (add-to-list 'c-default-style
  1667. '(c++-mode . "k&r")))
  1668. (with-eval-after-load 'js2-mode
  1669. ;; currently do not use js2-mode
  1670. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1671. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1672. ;; (defvar js2-mode-map (make-sparse-keymap))
  1673. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1674. ;; (interactive)
  1675. ;; (js2-enter-key)
  1676. ;; (indent-for-tab-command)))
  1677. ;; (add-hook (kill-local-variable 'before-save-hook)
  1678. ;; 'js2-before-save)
  1679. ;; (add-hook 'before-save-hook
  1680. ;; 'my-indent-buffer
  1681. ;; nil
  1682. ;; t)
  1683. )
  1684. (add-to-list 'interpreter-mode-alist
  1685. '("node" . js-mode))
  1686. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1687. (with-eval-after-load 'uniquify
  1688. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1689. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1690. (setq uniquify-min-dir-content 1))
  1691. (with-eval-after-load 'view
  1692. (defvar view-mode-map (make-sparse-keymap))
  1693. (define-key view-mode-map "j" 'scroll-up-line)
  1694. (define-key view-mode-map "k" 'scroll-down-line)
  1695. (define-key view-mode-map "v" 'toggle-read-only)
  1696. (define-key view-mode-map "q" 'bury-buffer)
  1697. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1698. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1699. ;; (define-key view-mode-map
  1700. ;; "n" 'nonincremental-repeat-search-forward)
  1701. ;; (define-key view-mode-map
  1702. ;; "N" 'nonincremental-repeat-search-backward)
  1703. ;; N conflicts with git-walktree
  1704. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1705. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1706. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1707. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1708. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1709. (global-set-key "\M-r" 'view-mode)
  1710. ;; (setq view-read-only t)
  1711. (with-eval-after-load 'term
  1712. (defvar term-raw-map (make-sparse-keymap))
  1713. (define-key term-raw-map (kbd "C-x")
  1714. (lookup-key (current-global-map)
  1715. (kbd "C-x"))))
  1716. (add-hook 'term-mode-hook
  1717. (lambda ()
  1718. ;; Stop current line highlighting
  1719. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1720. (set-variable 'scroll-margin 0 t)
  1721. ))
  1722. (set-variable 'term-buffer-maximum-size 20480)
  1723. (set-variable 'term-suppress-hard-newline t)
  1724. (add-hook 'Man-mode-hook
  1725. (lambda ()
  1726. (view-mode 1)
  1727. (setq truncate-lines nil)))
  1728. (set-variable 'Man-notify-method (if window-system
  1729. 'newframe
  1730. 'aggressive))
  1731. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1732. "woman_cache.el")))
  1733. ;; not work because man.el will be loaded when man called
  1734. (defalias 'man 'woman)
  1735. (add-to-list 'auto-mode-alist
  1736. '("tox\\.ini\\'" . conf-unix-mode))
  1737. (when (fboundp 'toml-mode)
  1738. (add-to-list 'auto-mode-alist
  1739. '("/tox\\.ini\\'" . toml-mode))
  1740. (add-to-list 'auto-mode-alist
  1741. '("/Pipfile\\'" . toml-mode))
  1742. (add-to-list 'auto-mode-alist
  1743. '("/poetry\\.lock\\'" . toml-mode))
  1744. )
  1745. (when (fboundp 'json-mode)
  1746. (add-to-list 'auto-mode-alist
  1747. '("/Pipfile\\.lock\\'" . json-mode)))
  1748. (add-hook 'go-mode-hook
  1749. (lambda()
  1750. (defvar go-mode-map)
  1751. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1752. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1753. (when (fboundp 'k8s-mode)
  1754. (add-to-list 'auto-mode-alist
  1755. '("\\.k8s\\'" . k8s-mode)))
  1756. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1757. ;; buffers
  1758. (defvar bs-configurations)
  1759. (declare-function bs-set-configuration "bs")
  1760. (declare-function bs-refresh "bs")
  1761. (declare-function bs-message-without-log "bs")
  1762. (declare-function bs--current-config-message "bs")
  1763. (with-eval-after-load 'bs
  1764. (add-to-list 'bs-configurations
  1765. '("specials" "^\\*" nil ".*" nil nil))
  1766. (add-to-list 'bs-configurations
  1767. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1768. (defvar bs-mode-map)
  1769. (defvar bs-current-configuration)
  1770. (define-key bs-mode-map (kbd "t")
  1771. ;; TODO: fix toggle feature
  1772. (lambda ()
  1773. (interactive)
  1774. (if (string= "specials"
  1775. bs-current-configuration)
  1776. (bs-set-configuration "files")
  1777. (bs-set-configuration "specials"))
  1778. (bs-refresh)
  1779. (bs-message-without-log "%s"
  1780. (bs--current-config-message))))
  1781. ;; (setq bs-configurations (list
  1782. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1783. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1784. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1785. )
  1786. (when (fboundp 'bs-show)
  1787. (defalias 'list-buffers 'bs-show)
  1788. (set-variable 'bs-default-configuration "files-and-specials")
  1789. (set-variable 'bs-default-sort-name "by nothing")
  1790. (add-hook 'bs-mode-hook
  1791. (lambda ()
  1792. (setq-local scroll-margin 0)
  1793. (setq-local header-line-format nil)
  1794. (setq-local mode-line-format nil)
  1795. )))
  1796. ;;(iswitchb-mode 1)
  1797. (icomplete-mode)
  1798. (defun iswitchb-buffer-display-other-window ()
  1799. "Do iswitchb in other window."
  1800. (interactive)
  1801. (let ((iswitchb-default-method 'display))
  1802. (call-interactively 'iswitchb-buffer)))
  1803. ;; buffer killing
  1804. ;; (defun my-delete-window-killing-buffer () nil)
  1805. (defun my-query-kill-current-buffer ()
  1806. "Interactively kill current buffer."
  1807. (interactive)
  1808. (if (y-or-n-p (concat "kill current buffer? :"))
  1809. (kill-buffer (current-buffer))))
  1810. (defun my-force-query-kill-current-buffer ()
  1811. "Interactively kill current buffer."
  1812. (interactive)
  1813. (when (y-or-n-p (concat "kill current buffer? :"))
  1814. (let ((kill-buffer-hook nil)
  1815. (kill-buffer-query-functions nil))
  1816. (kill-buffer (current-buffer)))))
  1817. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1818. ;; Originally C-x C-k -> kmacro-keymap
  1819. ;; (global-set-key "\C-x\C-k" 'kmacro-keymap)
  1820. (global-set-key (kbd "C-x C-k") 'my-query-kill-current-buffer)
  1821. (substitute-key-definition 'kill-buffer
  1822. 'my-query-kill-current-buffer
  1823. global-map)
  1824. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1825. ;; dired
  1826. (defun my-file-head (filename &optional n)
  1827. "Return list of first N lines of file FILENAME."
  1828. ;; TODO: Fix for janapese text
  1829. ;; TODO: Fix for short text
  1830. (let ((num (or n 10))
  1831. (size 100)
  1832. (beg 0)
  1833. (end 0)
  1834. (result '())
  1835. (read -1))
  1836. (with-temp-buffer
  1837. (erase-buffer)
  1838. (while (or (<= (count-lines (point-min)
  1839. (point-max))
  1840. num)
  1841. (eq read 0))
  1842. (setq end (+ beg size))
  1843. (setq read (nth 1 (insert-file-contents-literally filename
  1844. nil
  1845. beg
  1846. end)))
  1847. (goto-char (point-max))
  1848. (setq beg (+ beg size)))
  1849. (goto-char (point-min))
  1850. (while (< (length result) num)
  1851. (let ((start (point)))
  1852. (forward-line 1)
  1853. (setq result
  1854. `(,@result ,(buffer-substring-no-properties start
  1855. (point))))))
  1856. result
  1857. ;; (buffer-substring-no-properties (point-min)
  1858. ;; (progn
  1859. ;; (forward-line num)
  1860. ;; (point)))
  1861. )))
  1862. ;; (apply 'concat (my-file-head "./shrc" 10)
  1863. (declare-function dired-get-filename "dired" t)
  1864. (defun my-dired-echo-file-head (arg)
  1865. "Echo head of current file.
  1866. ARG is num to show, or defaults to 7."
  1867. (interactive "P")
  1868. (let ((f (dired-get-filename)))
  1869. (message "%s"
  1870. (apply 'concat
  1871. (my-file-head f
  1872. 7)))))
  1873. (declare-function dired-get-marked-files "dired")
  1874. (defun my-dired-diff ()
  1875. "Show diff of marked file and file of current line."
  1876. (interactive)
  1877. (let ((files (dired-get-marked-files nil nil nil t)))
  1878. (if (eq (car files)
  1879. t)
  1880. (diff (cadr files) (dired-get-filename))
  1881. (message "One file must be marked!"))))
  1882. (defun dired-get-file-info ()
  1883. "Print information of current line file."
  1884. (interactive)
  1885. (let* ((file (dired-get-filename t))
  1886. (quoted (shell-quote-argument file)))
  1887. (if (file-directory-p file)
  1888. (progn
  1889. (message "Calculating disk usage...")
  1890. (let ((du (or (executable-find "gdu")
  1891. (executable-find "du")
  1892. (error "du not found"))))
  1893. (shell-command (concat du
  1894. " -hsD "
  1895. quoted))))
  1896. (shell-command (concat "file "
  1897. quoted)))))
  1898. (defun my-dired-scroll-up ()
  1899. "Scroll up."
  1900. (interactive)
  1901. (my-dired-previous-line (- (window-height) 1)))
  1902. (defun my-dired-scroll-down ()
  1903. "Scroll down."
  1904. (interactive)
  1905. (my-dired-next-line (- (window-height) 1)))
  1906. ;; (defun my-dired-forward-line (arg)
  1907. ;; ""
  1908. ;; (interactive "p"))
  1909. (declare-function dired-get-subdir "dired")
  1910. (declare-function dired-move-to-filename "dired")
  1911. (defun my-dired-previous-line (arg)
  1912. "Move ARG lines up."
  1913. (interactive "p")
  1914. (if (> arg 0)
  1915. (progn
  1916. (if (eq (line-number-at-pos)
  1917. 1)
  1918. (goto-char (point-max))
  1919. (forward-line -1))
  1920. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1921. (dired-get-subdir))
  1922. (- arg 1)
  1923. arg)))
  1924. (dired-move-to-filename)))
  1925. (defun my-dired-next-line (arg)
  1926. "Move ARG lines down."
  1927. (interactive "p")
  1928. (if (> arg 0)
  1929. (progn
  1930. (if (eq (point)
  1931. (point-max))
  1932. (goto-char (point-min))
  1933. (forward-line 1))
  1934. (my-dired-next-line (if (or (dired-get-filename nil t)
  1935. (dired-get-subdir))
  1936. (- arg 1)
  1937. arg)))
  1938. (dired-move-to-filename)))
  1939. (defun my-tramp-remote-find-file (f)
  1940. "Open F."
  1941. (interactive (list (read-file-name "My Find File Tramp: "
  1942. "/scp:"
  1943. nil ;; "/scp:"
  1944. (confirm-nonexistent-file-or-buffer))))
  1945. (find-file f))
  1946. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1947. (if (eq window-system 'mac)
  1948. (setq dired-listing-switches "-lhF")
  1949. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1950. )
  1951. (setq dired-listing-switches "-lhF")
  1952. ;; when using dired-find-alternate-file
  1953. ;; reuse current dired buffer for the file to open
  1954. ;; (put 'dired-find-alternate-file 'disabled nil)
  1955. (set-variable 'dired-ls-F-marks-symlinks t)
  1956. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1957. (set-variable 'ls-lisp-dirs-first t)
  1958. (set-variable 'ls-lisp-use-localized-time-format t)
  1959. (set-variable 'ls-lisp-format-time-list
  1960. '("%Y-%m-%d %H:%M"
  1961. "%Y-%m-%d "))
  1962. (set-variable 'dired-dwim-target t)
  1963. (set-variable 'dired-isearch-filenames t)
  1964. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1965. (set-variable 'dired-hide-details-hide-information-lines nil)
  1966. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1967. (set-variable 'dired-recursive-deletes 'always)
  1968. ;; (add-hook 'dired-after-readin-hook
  1969. ;; 'my-replace-nasi-none)
  1970. (with-eval-after-load 'dired
  1971. (require 'ls-lisp nil t)
  1972. (defvar dired-mode-map (make-sparse-keymap))
  1973. ;; dired-do-chgrp sometimes cause system hung
  1974. (define-key dired-mode-map "G" 'ignore)
  1975. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1976. (define-key dired-mode-map "i" 'dired-get-file-info)
  1977. ;; (define-key dired-mode-map "f" 'find-file)
  1978. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1979. (define-key dired-mode-map "z" 'fzf)
  1980. (define-key dired-mode-map "!" 'shell-command)
  1981. (define-key dired-mode-map "&" 'async-shell-command)
  1982. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1983. (define-key dired-mode-map "=" 'my-dired-diff)
  1984. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1985. (define-key dired-mode-map "b" 'gtkbm)
  1986. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1987. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1988. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1989. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1990. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1991. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1992. (substitute-key-definition 'dired-next-line
  1993. 'my-dired-next-line
  1994. dired-mode-map)
  1995. (substitute-key-definition 'dired-previous-line
  1996. 'my-dired-previous-line
  1997. dired-mode-map)
  1998. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1999. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  2000. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  2001. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  2002. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  2003. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  2004. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  2005. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  2006. (add-hook 'dired-mode-hook
  2007. (lambda ()
  2008. (when (fboundp 'dired-hide-details-mode)
  2009. (dired-hide-details-mode t)
  2010. (local-set-key "l" 'dired-hide-details-mode))
  2011. (let ((file "._Icon\015"))
  2012. (when nil
  2013. '(file-readable-p file)
  2014. (delete-file file)))))
  2015. (when (fboundp 'pack-dired-dwim)
  2016. (with-eval-after-load 'dired
  2017. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  2018. (when (fboundp 'dired-list-all-mode)
  2019. (setq dired-listing-switches "-lhF")
  2020. (with-eval-after-load 'dired
  2021. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  2022. (when (fboundp 'dired-filter-mode)
  2023. (add-hook 'dired-mode-hook
  2024. 'dired-filter-mode))
  2025. (set-variable 'dired-filter-stack nil)
  2026. ;; Currently disabled in favor of dired-from-git-ls-files
  2027. ;; (define-key ctl-x-map "f" 'find-dired)
  2028. (defvar my-dired-git-ls-files-history
  2029. "History for `my-dired-git-ls-files'." nil)
  2030. (defun my-dired-git-ls-files (arg)
  2031. "Dired from git ls-files."
  2032. (interactive (list
  2033. (read-shell-command "git ls-files: "
  2034. "git ls-files -z ")))
  2035. (pop-to-buffer-same-window
  2036. (dired-noselect `(,default-directory
  2037. ,@(split-string (shell-command-to-string arg)
  2038. "\0" t))
  2039. ""))
  2040. )
  2041. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  2042. (with-eval-after-load 'dired
  2043. (defvar dired-mode-map (make-sparse-keymap))
  2044. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  2045. (with-eval-after-load 'pack
  2046. (set-variable 'pack-silence
  2047. t)
  2048. (defvar pack-program-alist)
  2049. (add-to-list 'pack-program-alist
  2050. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf"))
  2051. (when (executable-find "aunpack")
  2052. (add-to-list 'pack-program-alist
  2053. ' ("\\.zip\\'"
  2054. :pack ("zip" "-r" archive sources)
  2055. :pack-append ("zip" "-r" archive sources)
  2056. :unpack ("aunpack" archive))))
  2057. )
  2058. ;; dired-k
  2059. ;; Current HEAD of original repo is broken
  2060. ;; https://github.com/syohex/emacs-dired-k/issues/45
  2061. (when (fboundp 'dired-k)
  2062. (set-variable 'dired-k-style 'git)
  2063. ;; What is the best way of doing this?
  2064. (with-eval-after-load 'dired-k
  2065. (fset 'dired-k--highlight-by-file-attribyte 'ignore))
  2066. ;; (set-variable 'dired-k-size-colors
  2067. ;; `((,most-positive-fixnum)))
  2068. ;; (set-variable 'dired-k-date-colors
  2069. ;; `((,most-positive-fixnum)))
  2070. ;; always execute dired-k when dired buffer is opened and reverted
  2071. (add-hook 'dired-after-readin-hook #'dired-k-no-revert)
  2072. (add-hook 'switch-buffer-functions
  2073. (lambda (prev cur)
  2074. (when (derived-mode-p 'dired-mode)
  2075. (dired-k-no-revert))))
  2076. )
  2077. ;; (define-minor-mode my-dired-glob-filter)
  2078. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2079. ;; misc funcs
  2080. (define-key ctl-x-map "T" 'git-worktree)
  2081. (define-key ctl-x-map "W" 'git-walktree)
  2082. (when (fboundp 'browse-url-default-macosx-browser)
  2083. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  2084. (defalias 'qcalc 'quick-calc)
  2085. (defun memo (&optional dir)
  2086. "Open memo.txt in DIR."
  2087. (interactive)
  2088. (pop-to-buffer (find-file-noselect (concat (if dir
  2089. (file-name-as-directory dir)
  2090. "")
  2091. "memo.txt"))))
  2092. ;; TODO: remember-projectile
  2093. (set (defvar my-privnotes-path nil
  2094. "My privnotes repository path.")
  2095. (expand-file-name "~/my/privnotes"))
  2096. (defun my-privnotes-readme (dir)
  2097. "Open my privnotes DIR."
  2098. (interactive (list
  2099. (read-file-name "Privnotes: "
  2100. (expand-file-name (format-time-string "%Y%m%d_")
  2101. my-privnotes-path))))
  2102. (let ((path (expand-file-name "README.md" dir)))
  2103. (with-current-buffer (find-file path)
  2104. (unless (file-exists-p path)
  2105. (insert (file-name-base dir)
  2106. "\n"
  2107. "=======\n"
  2108. "\n\n")))))
  2109. (define-key ctl-x-map "p" 'my-privnotes-readme)
  2110. (set (defvar my-rgrep-alist nil
  2111. "Alist of rgrep command.
  2112. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2113. condition to choose COMMAND when evaluated.")
  2114. `(
  2115. ;; ripgrep
  2116. ("rg"
  2117. (executable-find "rg")
  2118. "rg -nH --no-heading --hidden --glob '!.git/' --smart-case -M 1280 ")
  2119. ;; git grep
  2120. ("gitgrep"
  2121. (eq 0
  2122. (shell-command "git rev-parse --git-dir"))
  2123. "git --no-pager grep -nH -e ")
  2124. ;; sift
  2125. ("sift"
  2126. (executable-find "sift")
  2127. ("sift --binary-skip --filename --line-number --git --smart-case "))
  2128. ;; the silver searcher
  2129. ("ag"
  2130. (executable-find "ag")
  2131. "ag --nogroup --nopager --filename ")
  2132. ;; ack
  2133. ("ack"
  2134. (executable-find "ack")
  2135. "ack --nogroup --nopager --with-filename ")
  2136. ;; gnu global
  2137. ("global"
  2138. (and (require 'ggtags nil t)
  2139. (executable-find "global")
  2140. (ggtags-current-project-root))
  2141. "global --result grep ")
  2142. ;; grep
  2143. ("grep"
  2144. t
  2145. ,(concat "find . "
  2146. "-path '*/.git' -prune -o "
  2147. "-path '*/.svn' -prune -o "
  2148. "-type f -print0 | "
  2149. "xargs -0 grep -nH -e "))
  2150. )
  2151. )
  2152. (defvar my-rgrep-default nil
  2153. "Default command name for my-rgrep.")
  2154. (defun my-rgrep-grep-command (&optional name alist)
  2155. "Return recursive grep command for current directory or nil.
  2156. If NAME is given, use that without testing.
  2157. Commands are searched from ALIST."
  2158. (if alist
  2159. (if name
  2160. ;; if name is given search that from alist and return the command
  2161. (nth 2 (assoc name
  2162. alist))
  2163. ;; if name is not given try test in 1th elem
  2164. (let ((car (car alist))
  2165. (cdr (cdr alist)))
  2166. (if (eval (nth 1 car))
  2167. ;; if the condition is true return the command
  2168. (nth 2 car)
  2169. ;; try next one
  2170. (and cdr
  2171. (my-rgrep-grep-command name cdr)))))
  2172. ;; if alist is not given set default value
  2173. (my-rgrep-grep-command name my-rgrep-alist)))
  2174. (defun my-rgrep (command-args)
  2175. "My recursive grep. Run COMMAND-ARGS.
  2176. If prefix argument is given, use current symbol as default search target
  2177. and search from projectile root (if projectile is available)."
  2178. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2179. nil)))
  2180. (if cmd
  2181. (list (read-shell-command "grep command: "
  2182. (concat cmd
  2183. (if current-prefix-arg
  2184. (thing-at-point 'symbol t)
  2185. ""))
  2186. 'grep-find-history))
  2187. (error "My-Rgrep: Command for rgrep not found")
  2188. )))
  2189. (if (and current-prefix-arg
  2190. (eval-and-compile (require 'projectile nil t))
  2191. (projectile-project-p))
  2192. (projectile-with-default-dir (projectile-project-root)
  2193. (compilation-start command-args
  2194. 'grep-mode))
  2195. (compilation-start command-args
  2196. 'grep-mode)))
  2197. (defun my-rgrep-thing-at-point-projectile-root ()
  2198. "My recursive grep to find thing at point from project root."
  2199. (interactive)
  2200. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  2201. nil))
  2202. (command-args
  2203. (if cmd
  2204. (concat cmd
  2205. (or (thing-at-point 'symbol t)
  2206. (error "No symbol at point")))
  2207. (error "My-Rgrep: Command for rgrep not found"))))
  2208. (if (eval-and-compile (require 'projectile nil t))
  2209. (projectile-with-default-dir (or (projectile-project-root)
  2210. default-directory)
  2211. (compilation-start command-args
  2212. 'grep-mode))
  2213. (compilation-start command-args
  2214. 'grep-mode))))
  2215. (defmacro define-my-rgrep (name)
  2216. "Define rgrep for NAME."
  2217. `(defun ,(intern (concat "my-rgrep-"
  2218. name)) ()
  2219. ,(format "My recursive grep by %s."
  2220. name)
  2221. (interactive)
  2222. (let ((my-rgrep-default ,name))
  2223. (if (called-interactively-p 'any)
  2224. (call-interactively 'my-rgrep)
  2225. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2226. )
  2227. (define-my-rgrep "ack")
  2228. (define-my-rgrep "ag")
  2229. (define-my-rgrep "rg")
  2230. (define-my-rgrep "sift")
  2231. (define-my-rgrep "gitgrep")
  2232. (define-my-rgrep "grep")
  2233. (define-my-rgrep "global")
  2234. (define-key ctl-x-map "s" 'my-rgrep)
  2235. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  2236. (defun my-occur (regexp &optional region)
  2237. "My occur command to search REGEXP to search REGION."
  2238. (interactive (list (read-string "List lines matching regexp: "
  2239. (thing-at-point 'symbol t))))
  2240. (occur regexp nil region))
  2241. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  2242. (set-variable 'dumb-jump-prefer-searcher 'rg)
  2243. (defalias 'make 'compile)
  2244. (define-key ctl-x-map "c" 'compile)
  2245. (autoload 'pb/push-item "pushbullet")
  2246. (defun my-pushbullet-note (text &optional title)
  2247. "Push TEXT with optional TITLE."
  2248. (interactive "sText to Push: ")
  2249. (pb/push-item '("") text "note" (or title "")))
  2250. ;;;;;;;;;;;;;;;;;;;;;
  2251. ;; git-bug
  2252. (defconst git-bug-ls-regexp
  2253. (eval-when-compile
  2254. (rx bol
  2255. (submatch (one-or-more alphanumeric)) ; id
  2256. ;; (one-or-more any)
  2257. (one-or-more space)
  2258. (submatch (or "open" "close")) ; status
  2259. (one-or-more space)
  2260. (submatch (maximal-match (zero-or-more print))) ; title
  2261. "\t"
  2262. (submatch (one-or-more alphanumeric)) ; user
  2263. (one-or-more space)
  2264. "C:"
  2265. (submatch (one-or-more digit)) ; Comment num
  2266. (one-or-more space)
  2267. "L:"
  2268. (submatch (one-or-more digit)) ; Label num
  2269. eol
  2270. ))
  2271. "Regexp to parse line of output of git-bug ls.
  2272. Used by `git-bug-ls'.")
  2273. (declare-function string-trim "subr-x")
  2274. (defun git-bug-bugs ()
  2275. "Get list of git-bug bugs."
  2276. (with-temp-buffer
  2277. (git-bug--call-process "bug" "ls")
  2278. (goto-char (point-min))
  2279. (let ((bugs nil))
  2280. (while (not (eq (point) (point-max)))
  2281. (save-match-data
  2282. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  2283. (setq bugs `(,@bugs
  2284. ,(list
  2285. :id (match-string 1)
  2286. :status (match-string 2)
  2287. :title (string-trim (match-string 3))
  2288. :user (match-string 4)
  2289. :comment-num (match-string 5)
  2290. :label-num (match-string 6)
  2291. )))))
  2292. (forward-line 1)
  2293. (goto-char (point-at-bol)))
  2294. bugs)))
  2295. (defun git-bug-ls ()
  2296. "Open and select git bug list buffer."
  2297. (interactive)
  2298. (pop-to-buffer (git-bug-ls-noselect)))
  2299. (defun git-bug-ls--set-tabulated-list-mode-variables ()
  2300. "Not implemented.")
  2301. (defun git-bug-ls-mode ()
  2302. "Not implemented.")
  2303. (defun git-bug-ls-noselect (&optional directory)
  2304. "Open git bug list buffer.
  2305. If optional arg DIRECTORY is given change current directory to there before
  2306. initializing."
  2307. (setq directory (expand-file-name (or directory
  2308. default-directory)))
  2309. (cl-assert (file-directory-p directory))
  2310. (let* ((root (git-bug--get-repository-root directory))
  2311. (name (file-name-nondirectory root))
  2312. (bname (format "*GitBug<%s>*" name)))
  2313. (with-current-buffer (get-buffer-create bname)
  2314. (cd root)
  2315. (git-bug-ls--set-tabulated-list-mode-variables)
  2316. (git-bug-ls-mode)
  2317. (current-buffer))))
  2318. (defun git-bug--get-repository-root (dir)
  2319. "Resolve repository root of DIR.
  2320. If DIR is not inside of any git repository, signal an error."
  2321. (cl-assert (file-directory-p dir))
  2322. (with-temp-buffer
  2323. (cd dir)
  2324. (git-bug--call-process "rev-parse" "--show-toplevel")
  2325. (goto-char (point-min))
  2326. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  2327. (defun git-bug--call-process (&rest args)
  2328. "Start git process synchronously with ARGS.
  2329. Raise error when git process ends with non-zero status.
  2330. Any output will be written to current buffer."
  2331. (let ((status (apply 'call-process
  2332. "git"
  2333. nil
  2334. t
  2335. nil
  2336. args)))
  2337. (cl-assert (eq status 0)
  2338. nil
  2339. (buffer-substring-no-properties (point-min) (point-max)))))
  2340. ;;;;;;;;;;;;;;;;;;;
  2341. ;; peek-file-mode
  2342. (defvar peek-file-buffers
  2343. ()
  2344. "Peek buffers.")
  2345. (defun peek-file (file)
  2346. "Peek FILE."
  2347. (interactive "fFile to peek: ")
  2348. (with-current-buffer (find-file file)
  2349. (peek-file-mode)))
  2350. (define-minor-mode peek-file-mode
  2351. "Peek file mode."
  2352. :lighter "PK"
  2353. (view-mode peek-file-mode)
  2354. (add-to-list 'peek-file-buffers
  2355. (current-buffer))
  2356. (add-hook 'switch-buffer-functions
  2357. 'peek-file-remove-buffers))
  2358. (defun peek-file-remove-buffers (&args _)
  2359. "Remove peek file buffers."
  2360. (cl-dolist (buf (cl-copy-list peek-file-buffers))
  2361. (unless (get-buffer-window buf t)
  2362. (setq peek-file-buffers
  2363. (delq buf
  2364. peek-file-buffers))
  2365. (with-current-buffer buf
  2366. (when peek-file-mode
  2367. (kill-buffer))))))
  2368. ;;;;;;;;;;;;;;;;;;;;
  2369. ;; remember-projectile
  2370. ;; TODO: Add global-minor-mode
  2371. (defvar remember-data-file)
  2372. (defun my-set-remember-data-file-buffer-local ()
  2373. "Set `remember-data-file'."
  2374. (when (require 'projectile nil t)
  2375. (setq-local remember-data-file
  2376. (expand-file-name ".remember.notes"
  2377. (projectile-project-root)))))
  2378. (add-hook 'after-change-major-mode-hook
  2379. 'my-set-remember-data-file-buffer-local)
  2380. (define-key ctl-x-map "R" 'remember)
  2381. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2382. ;; ivy
  2383. ;; (set-variable 'enable-recursive-minibuffers t)
  2384. ;; (minibuffer-depth-indicate-mode 1)
  2385. (declare-function ivy--regex-ignore-order "ivy")
  2386. (declare-function ivy--regex-fuzzy "ivy")
  2387. (defun my--ivy-regex-fuzzy-ignore-order (str)
  2388. "Re-build regex from STR for ignore-order fuzzy match."
  2389. (let ((re-list (ivy--regex-ignore-order str)))
  2390. (if (listp re-list)
  2391. (mapcar (lambda (e)
  2392. (let ((head (car e))
  2393. (tail (cdr e)))
  2394. (if tail
  2395. (cons (ivy--regex-fuzzy head)
  2396. tail)
  2397. (cons head tail))))
  2398. re-list)
  2399. (ivy--regex-fuzzy re-list))))
  2400. ;; (my--ivy-regex-fuzzy-ignore-order "ab bc !cee")
  2401. ;; (ivy--regex-fuzzy "ab")
  2402. ;; (ivy--regex-ignore-order "a b")
  2403. (set-variable 'ivy-re-builders-alist
  2404. '((t . my--ivy-regex-fuzzy-ignore-order)))
  2405. ;; (set-variable 'ivy-format-functions-alist
  2406. ;; '((t . ivy-format-function-arrow)))
  2407. (set-variable 'ivy-format-functions-alist
  2408. '((t . (lambda (cands) (ivy--format-function-generic
  2409. (lambda (str)
  2410. (concat "+> "
  2411. (ivy--add-face str 'ivy-current-match)
  2412. ))
  2413. (lambda (str)
  2414. (concat "| " str))
  2415. cands
  2416. "\n")))))
  2417. (set-variable 'ivy-wrap t)
  2418. (set-variable 'ivy-sort-max-size 500)
  2419. (when (fboundp 'ivy-rich-mode)
  2420. (ivy-rich-mode 1))
  2421. (with-eval-after-load 'ivy
  2422. (defvar ivy-minibuffer-map)
  2423. (define-key ivy-minibuffer-map (kbd "C-u")
  2424. (lambda () (interactive) (delete-region (point-at-bol) (point)))))
  2425. (set-variable 'ivy-on-del-error-function 'ignore)
  2426. (when (fboundp 'counsel-M-x)
  2427. (define-key esc-map "x" 'counsel-M-x)
  2428. ;; (counsel-mode 1)
  2429. ;; counsel-fzf executes fzf -f QUERY for each input
  2430. ;; (define-key ctl-x-map "f"
  2431. ;; (lambda ()
  2432. ;; (interactive
  2433. ;; (let ((process-environment (cl-copy-list process-environment)))
  2434. ;; (setenv "FZF_DEFAULT_COMMAND" nil)
  2435. ;; (counsel-fzf)))))
  2436. )
  2437. ;; (when (fboundp 'counsel-switch-buffer)
  2438. ;; (define-key ctl-x-map (kbd "C-b") 'counsel-switch-buffer))
  2439. (when (and (fboundp 'ivy-read)
  2440. (locate-library "counsel"))
  2441. (defvar counsel-describe-map)
  2442. (defun my-counsel-describe-symbol ()
  2443. "Forwaord to `describe-symbol'."
  2444. (interactive)
  2445. (cl-assert (eval-and-compile (require 'help-mode nil t))) ;; describe-symbol-backends
  2446. (cl-assert (eval-and-compile (require 'counsel nil t)))
  2447. (ivy-read "Describe symbol: " obarray
  2448. ;; From describe-symbol definition
  2449. :predicate (lambda (vv)
  2450. (cl-some (lambda (x) (funcall (nth 1 x) vv))
  2451. describe-symbol-backends))
  2452. :require-match t
  2453. :history 'counsel-describe-symbol-history
  2454. :keymap counsel-describe-map
  2455. :preselect (ivy-thing-at-point)
  2456. :action (lambda (x)
  2457. (describe-symbol (intern x)))
  2458. :caller 'my-counsel-describe-symbol))
  2459. (define-key help-map "o" 'my-counsel-describe-symbol)
  2460. )
  2461. ;; (defun my-ivy-length (x y)
  2462. ;; "Ivy sort to order by string length."
  2463. ;; (<= (length (if (consp x) (car x) x))
  2464. ;; (length (if (consp y) (car y) y))))
  2465. ;; (my-ivy-length "a" (cons "bc" t))
  2466. ;; (my-ivy-length "a" (cons "c" t))
  2467. (with-eval-after-load 'ivy
  2468. ;; (ivy-configure 'my-counsel-describe-symbol
  2469. ;; :sort-fn 'my-ivy-length)
  2470. (ivy-configure 'counsel-M-x
  2471. :initial-input ""
  2472. ;; :sort-fn 'my-ivy-length
  2473. )
  2474. (defvar ivy-sort-matches-functions-alist)
  2475. (add-to-list 'ivy-sort-matches-functions-alist
  2476. '(my-counsel-describe-symbol . ivy--shorter-matches-first))
  2477. (add-to-list 'ivy-sort-matches-functions-alist
  2478. '(counsel-M-x . ivy--shorter-matches-first))
  2479. )
  2480. (when (fboundp 'counsel-imenu)
  2481. (define-key ctl-x-map "l" 'counsel-imenu))
  2482. (when (fboundp 'swiper)
  2483. (define-key esc-map (kbd "C-s") 'swiper))
  2484. ;; ?
  2485. (define-key input-decode-map "\e[1;5C" [C-right])
  2486. (define-key input-decode-map "\e[1;5D" [C-left])
  2487. ;;;;;;;;;;;;;;;;;;;;;;;;
  2488. ;; mozc
  2489. (global-set-key (kbd "C-c m e") 'ignore)
  2490. (global-set-key (kbd "C-c m d") 'ignore)
  2491. ;; mozc
  2492. (when (locate-library "mozc")
  2493. ;; https://tottoto.net/mac-emacs-karabiner-elements-japanese-input-method-config/
  2494. (with-eval-after-load 'mozc
  2495. ;; (define-key mozc-mode-map (kbd "C-h") 'backward-delete-char)
  2496. ;; (define-key mozc-mode-map (kbd "C-p") (kbd "<up>"))
  2497. ;; (define-key mozc-mode-map (kbd "C-n") (kbd "SPC"))
  2498. )
  2499. (setq default-input-method "japanese-mozc")
  2500. (custom-set-variables '(mozc-leim-title "あ"))
  2501. (defun turn-on-input-method ()
  2502. (interactive)
  2503. (activate-input-method default-input-method))
  2504. (defun turn-off-input-method ()
  2505. (interactive)
  2506. (deactivate-input-method))
  2507. ;; (setq mozc-candidate-style 'echo-area)
  2508. (global-set-key (kbd "C-c m e") 'turn-on-input-method)
  2509. (global-set-key (kbd "C-c m d") 'turn-off-input-method)
  2510. (require 'mozc-popup)
  2511. (set-variable 'mozc-candidate-style 'popup)
  2512. ;; これいる?
  2513. (when (require 'mozc-im nil t)
  2514. (setq default-input-method "japanese-mozc-im")
  2515. ;; (global-set-key (kbd "C-j") 'toggle-input-method)
  2516. )
  2517. )
  2518. ;;;;;;;;;;;;;;
  2519. ;; mmv
  2520. ;; https://www.emacswiki.org/emacs/MakingMarkVisible
  2521. ;;;; Make the mark visible, and the visibility toggleable. ('mmv' means 'make
  2522. ;;;; mark visible'.) By Patrick Gundlach, Teemu Leisti, and Stefan.
  2523. (defgroup mmv nil
  2524. "Make mark visible."
  2525. :group 'tools)
  2526. (defvar mmv-face-foreground
  2527. (face-foreground 'hi-yellow)
  2528. "Foreground color for `mmv-face'.")
  2529. (defvar mmv-face-background
  2530. (face-background 'hi-yellow)
  2531. "Background color for `mmv-face'.")
  2532. (defface mmv-face
  2533. `((t :background ,mmv-face-background :foreground ,mmv-face-foreground))
  2534. "Face used for showing the mark's position."
  2535. :group 'mmv)
  2536. (defvar-local mmv-mark-overlay nil
  2537. "The overlay for showing the mark's position.")
  2538. (defvar-local mmv-is-mark-visible t
  2539. "The overlay is visible only when this variable's value is t.")
  2540. (defun mmv-draw-mark (&rest _)
  2541. "Make the mark's position stand out by means of a one-character-long overlay.
  2542. If the value of variable `mmv-is-mark-visible' is nil, the mark will be
  2543. invisible."
  2544. (unless mmv-mark-overlay
  2545. (setq mmv-mark-overlay (make-overlay 0 0 nil t))
  2546. (overlay-put mmv-mark-overlay 'face 'mmv-face))
  2547. (let ((mark-position (mark t)))
  2548. (cond
  2549. ((null mark-position) (delete-overlay mmv-mark-overlay))
  2550. ((and (< mark-position (point-max))
  2551. (not (eq ?\n (char-after mark-position))))
  2552. (overlay-put mmv-mark-overlay 'after-string nil)
  2553. (move-overlay mmv-mark-overlay mark-position (1+ mark-position)))
  2554. (t
  2555. ;; This branch is called when the mark is at the end of a line or at the
  2556. ;; end of the buffer. We use a bit of trickery to avoid the higlight
  2557. ;; extending from the mark all the way to the right end of the frame.
  2558. (overlay-put mmv-mark-overlay 'after-string
  2559. (propertize " " 'face (overlay-get mmv-mark-overlay 'face)))
  2560. (move-overlay mmv-mark-overlay mark-position mark-position)))))
  2561. (add-hook 'pre-redisplay-functions #'mmv-draw-mark)
  2562. (defun mmv-toggle-mark-visibility ()
  2563. "Toggles the mark's visiblity and redraws it (whether invisible or visible)."
  2564. (interactive)
  2565. (setq mmv-is-mark-visible (not mmv-is-mark-visible))
  2566. (if mmv-is-mark-visible
  2567. (set-face-attribute 'mmv-face nil :background mmv-face-background :foreground mmv-face-foreground)
  2568. (set-face-attribute 'mmv-face nil :background 'unspecified :foreground 'unspecified))
  2569. (mmv-draw-mark))
  2570. ;; Local Variables:
  2571. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2572. ;; flycheck-checker: emacs-lisp
  2573. ;; End:
  2574. ;;; emancs.el ends here