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.
 
 
 
 
 
 

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