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.
 
 
 
 
 
 

2770 lines
90 KiB

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