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.
 
 
 
 
 
 

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