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.
 
 
 
 
 
 

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