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.
 
 
 
 
 
 

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