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.
 
 
 
 
 
 

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