Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

2996 řádky
99 KiB

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