Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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