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

2792 lines
92 KiB

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