No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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