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

2971 rivejä
98 KiB

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