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

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