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.
 
 
 
 
 
 

2722 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. (with-eval-after-load 'git-commit
  1325. (add-hook 'git-commit-setup-hook
  1326. 'turn-off-auto-fill t))
  1327. (with-eval-after-load 'rst
  1328. (defvar rst-mode-map)
  1329. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1330. (with-eval-after-load 'jdee
  1331. (add-hook 'jdee-mode-hook
  1332. (lambda ()
  1333. (make-local-variable 'global-mode-string)
  1334. (add-to-list 'global-mode-string
  1335. mode-line-position))))
  1336. ;; Cannot enable error thrown. Why???
  1337. ;; https://github.com/m0smith/malabar-mode#Installation
  1338. ;; (when (require 'malabar-mode nil t)
  1339. ;; (add-to-list 'load-path
  1340. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1341. ;; (require 'cedet-devel-load nil t)
  1342. ;; (eval-after-init (activate-malabar-mode)))
  1343. (with-eval-after-load 'make-mode
  1344. (defvar makefile-mode-map (make-sparse-keymap))
  1345. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1346. ;; this functions is set in write-file-functions, i cannot find any
  1347. ;; good way to remove this.
  1348. (fset 'makefile-warn-suspicious-lines 'ignore))
  1349. (with-eval-after-load 'verilog-mode
  1350. (defvar verilog-mode-map (make-sparse-keymap))
  1351. (define-key verilog-mode-map ";" 'self-insert-command))
  1352. (setq diff-switches "-u")
  1353. (autoload 'diff-goto-source "diff-mode" nil t)
  1354. (with-eval-after-load 'diff-mode
  1355. ;; (when (and (eq major-mode
  1356. ;; 'diff-mode)
  1357. ;; (not buffer-file-name))
  1358. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1359. ;; (view-mode 1))
  1360. (set-face-attribute 'diff-header nil
  1361. :foreground nil
  1362. :background nil
  1363. :weight 'bold)
  1364. (set-face-attribute 'diff-file-header nil
  1365. :foreground nil
  1366. :background nil
  1367. :weight 'bold)
  1368. (set-face-foreground 'diff-index "blue")
  1369. (set-face-attribute 'diff-hunk-header nil
  1370. :foreground "cyan"
  1371. :weight 'normal)
  1372. (set-face-attribute 'diff-context nil
  1373. ;; :foreground "white"
  1374. :foreground nil
  1375. :weight 'normal)
  1376. (set-face-foreground 'diff-removed "red")
  1377. (set-face-foreground 'diff-added "green")
  1378. (set-face-background 'diff-removed nil)
  1379. (set-face-background 'diff-added nil)
  1380. (set-face-attribute 'diff-changed nil
  1381. :foreground "magenta"
  1382. :weight 'normal)
  1383. (set-face-attribute 'diff-refine-changed nil
  1384. :foreground nil
  1385. :background nil
  1386. :weight 'bold
  1387. :inverse-video t)
  1388. ;; Annoying !
  1389. ;;(diff-auto-refine-mode)
  1390. )
  1391. ;; (ffap-bindings)
  1392. (set-variable 'browse-url-browser-function
  1393. 'eww-browse-url)
  1394. (set-variable 'sh-here-document-word "__EOC__")
  1395. (with-eval-after-load 'adoc-mode
  1396. (defvar adoc-mode-map)
  1397. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1398. (when (fboundp 'adoc-mode)
  1399. (setq auto-mode-alist
  1400. `(("\\.adoc\\'" . adoc-mode)
  1401. ("\\.asciidoc\\'" . adoc-mode)
  1402. ,@auto-mode-alist)))
  1403. (with-eval-after-load 'markup-faces
  1404. ;; Is this too match ?
  1405. (set-face-foreground 'markup-meta-face
  1406. "color-245")
  1407. (set-face-foreground 'markup-meta-hide-face
  1408. "color-245")
  1409. )
  1410. ;; TODO: check if this is required
  1411. (with-eval-after-load 'groovy-mode
  1412. (defvar groovy-mode-map)
  1413. (define-key groovy-mode-map "(" 'self-insert-command)
  1414. (define-key groovy-mode-map ")" 'self-insert-command)
  1415. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1416. )
  1417. (when (fboundp 'groovy-mode)
  1418. (add-to-list 'auto-mode-alist
  1419. '("build\\.gradle\\'" . groovy-mode)))
  1420. (add-to-list 'auto-mode-alist
  1421. '("\\.gawk\\'" . awk-mode))
  1422. (with-eval-after-load 'yaml-mode
  1423. (defvar yaml-mode-map (make-sparse-keymap))
  1424. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1425. (with-eval-after-load 'html-mode
  1426. (defvar html-mode-map (make-sparse-keymap))
  1427. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1428. (with-eval-after-load 'text-mode
  1429. (define-key text-mode-map (kbd "C-m") 'newline))
  1430. (with-eval-after-load 'info
  1431. (defvar Info-additional-directory-list)
  1432. (dolist (dir (directory-files (concat user-emacs-directory
  1433. "info")
  1434. t
  1435. "^[^.].*"))
  1436. (when (file-directory-p dir)
  1437. (add-to-list 'Info-additional-directory-list
  1438. dir)))
  1439. (let ((dir (expand-file-name "~/.brew/share/info")))
  1440. (when (file-directory-p dir)
  1441. (add-to-list 'Info-additional-directory-list
  1442. dir))))
  1443. (with-eval-after-load 'apropos
  1444. (defvar apropos-mode-map (make-sparse-keymap))
  1445. (define-key apropos-mode-map "n" 'next-line)
  1446. (define-key apropos-mode-map "p" 'previous-line))
  1447. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1448. ;; (define-key isearch-mode-map
  1449. ;; (kbd "C-j") 'isearch-other-control-char)
  1450. ;; (define-key isearch-mode-map
  1451. ;; (kbd "C-k") 'isearch-other-control-char)
  1452. ;; (define-key isearch-mode-map
  1453. ;; (kbd "C-h") 'isearch-other-control-char)
  1454. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1455. (define-key isearch-mode-map (kbd "M-r")
  1456. 'isearch-query-replace-regexp)
  1457. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1458. (setq lazy-highlight-cleanup nil)
  1459. ;; face for isearch highlighing
  1460. (set-face-attribute 'lazy-highlight
  1461. nil
  1462. :foreground `unspecified
  1463. :background `unspecified
  1464. :underline t
  1465. ;; :weight `bold
  1466. )
  1467. (add-hook 'outline-mode-hook
  1468. (lambda ()
  1469. (when (string-match "\\.md\\'" buffer-file-name)
  1470. (set (make-local-variable 'outline-regexp) "#+ "))))
  1471. (add-hook 'outline-mode-hook
  1472. 'outline-show-all)
  1473. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1474. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1475. (with-eval-after-load 'markdown-mode
  1476. (defvar gfm-mode-map)
  1477. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1478. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1479. )
  1480. (when (fboundp 'gfm-mode)
  1481. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1482. (add-hook 'markdown-mode-hook
  1483. (lambda ()
  1484. (outline-minor-mode 1)
  1485. (flyspell-mode)
  1486. (set (make-local-variable 'comment-start) ";")))
  1487. )
  1488. ;; c-mode
  1489. ;; http://www.emacswiki.org/emacs/IndentingC
  1490. ;; http://en.wikipedia.org/wiki/Indent_style
  1491. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1492. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1493. (with-eval-after-load 'cc-vars
  1494. (defvar c-default-style nil)
  1495. (add-to-list 'c-default-style
  1496. '(c-mode . "k&r"))
  1497. (add-to-list 'c-default-style
  1498. '(c++-mode . "k&r")))
  1499. (with-eval-after-load 'js2-mode
  1500. ;; currently do not use js2-mode
  1501. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1502. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1503. ;; (defvar js2-mode-map (make-sparse-keymap))
  1504. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1505. ;; (interactive)
  1506. ;; (js2-enter-key)
  1507. ;; (indent-for-tab-command)))
  1508. ;; (add-hook (kill-local-variable 'before-save-hook)
  1509. ;; 'js2-before-save)
  1510. ;; (add-hook 'before-save-hook
  1511. ;; 'my-indent-buffer
  1512. ;; nil
  1513. ;; t)
  1514. )
  1515. (add-to-list 'interpreter-mode-alist
  1516. '("node" . js-mode))
  1517. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1518. (with-eval-after-load 'uniquify
  1519. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1520. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1521. (setq uniquify-min-dir-content 1))
  1522. (with-eval-after-load 'view
  1523. (defvar view-mode-map (make-sparse-keymap))
  1524. (define-key view-mode-map "j" 'scroll-up-line)
  1525. (define-key view-mode-map "k" 'scroll-down-line)
  1526. (define-key view-mode-map "v" 'toggle-read-only)
  1527. (define-key view-mode-map "q" 'bury-buffer)
  1528. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1529. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1530. ;; (define-key view-mode-map
  1531. ;; "n" 'nonincremental-repeat-search-forward)
  1532. ;; (define-key view-mode-map
  1533. ;; "N" 'nonincremental-repeat-search-backward)
  1534. ;; N conflicts with git-walktree
  1535. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1536. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1537. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1538. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1539. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1540. (global-set-key "\M-r" 'view-mode)
  1541. ;; (setq view-read-only t)
  1542. (with-eval-after-load 'term
  1543. (defvar term-raw-map (make-sparse-keymap))
  1544. (define-key term-raw-map (kbd "C-x")
  1545. (lookup-key (current-global-map)
  1546. (kbd "C-x"))))
  1547. (add-hook 'term-mode-hook
  1548. (lambda ()
  1549. ;; Stop current line highlighting
  1550. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1551. (set-variable 'scroll-margin 0 t)
  1552. ))
  1553. (set-variable 'term-buffer-maximum-size 20480)
  1554. (set-variable 'term-suppress-hard-newline t)
  1555. (add-hook 'Man-mode-hook
  1556. (lambda ()
  1557. (view-mode 1)
  1558. (setq truncate-lines nil)))
  1559. (set-variable 'Man-notify-method (if window-system
  1560. 'newframe
  1561. 'aggressive))
  1562. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1563. "woman_cache.el")))
  1564. ;; not work because man.el will be loaded when man called
  1565. (defalias 'man 'woman)
  1566. (add-to-list 'auto-mode-alist
  1567. '("tox\\.ini\\'" . conf-unix-mode))
  1568. (when (fboundp 'toml-mode)
  1569. (add-to-list 'auto-mode-alist
  1570. '("/tox\\.ini\\'" . toml-mode))
  1571. (add-to-list 'auto-mode-alist
  1572. '("/Pipfile\\'" . toml-mode))
  1573. (add-to-list 'auto-mode-alist
  1574. '("/poetry\\.lock\\'" . toml-mode))
  1575. )
  1576. (when (fboundp 'json-mode)
  1577. (add-to-list 'auto-mode-alist
  1578. '("/Pipfile\\.lock\\'" . json-mode)))
  1579. (add-hook 'go-mode-hook
  1580. (lambda()
  1581. (defvar go-mode-map)
  1582. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1583. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1584. (when (fboundp 'k8s-mode)
  1585. (add-to-list 'auto-mode-alist
  1586. '("\\.k8s\\'" . k8s-mode)))
  1587. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1588. ;; buffers
  1589. (defvar bs-configurations)
  1590. (declare-function bs-set-configuration "bs")
  1591. (declare-function bs-refresh "bs")
  1592. (declare-function bs-message-without-log "bs")
  1593. (declare-function bs--current-config-message "bs")
  1594. (with-eval-after-load 'bs
  1595. (add-to-list 'bs-configurations
  1596. '("specials" "^\\*" nil ".*" nil nil))
  1597. (add-to-list 'bs-configurations
  1598. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1599. (defvar bs-mode-map)
  1600. (defvar bs-current-configuration)
  1601. (define-key bs-mode-map (kbd "t")
  1602. ;; TODO: fix toggle feature
  1603. (lambda ()
  1604. (interactive)
  1605. (if (string= "specials"
  1606. bs-current-configuration)
  1607. (bs-set-configuration "files")
  1608. (bs-set-configuration "specials"))
  1609. (bs-refresh)
  1610. (bs-message-without-log "%s"
  1611. (bs--current-config-message))))
  1612. ;; (setq bs-configurations (list
  1613. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1614. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1615. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1616. )
  1617. (when (fboundp 'bs-show)
  1618. (defalias 'list-buffers 'bs-show)
  1619. (set-variable 'bs-default-configuration "files-and-specials")
  1620. (set-variable 'bs-default-sort-name "by nothing")
  1621. (add-hook 'bs-mode-hook
  1622. (lambda ()
  1623. (set (make-local-variable 'scroll-margin) 0))))
  1624. ;;(iswitchb-mode 1)
  1625. (icomplete-mode)
  1626. (defun iswitchb-buffer-display-other-window ()
  1627. "Do iswitchb in other window."
  1628. (interactive)
  1629. (let ((iswitchb-default-method 'display))
  1630. (call-interactively 'iswitchb-buffer)))
  1631. ;; buffer killing
  1632. ;; (defun my-delete-window-killing-buffer () nil)
  1633. (defun my-query-kill-current-buffer ()
  1634. "Interactively kill current buffer."
  1635. (interactive)
  1636. (if (y-or-n-p (concat "kill current buffer? :"))
  1637. (kill-buffer (current-buffer))))
  1638. (defun my-force-query-kill-current-buffer ()
  1639. "Interactively kill current buffer."
  1640. (interactive)
  1641. (when (y-or-n-p (concat "kill current buffer? :"))
  1642. (let ((kill-buffer-hook nil)
  1643. (kill-buffer-query-functions nil))
  1644. (kill-buffer (current-buffer)))))
  1645. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1646. ;; Originally C-x C-k -> kmacro-keymap
  1647. ;; (global-set-key "\C-x\C-k" 'kmacro-keymap)
  1648. (global-set-key (kbd "C-x C-k") 'my-query-kill-current-buffer)
  1649. (substitute-key-definition 'kill-buffer
  1650. 'my-query-kill-current-buffer
  1651. global-map)
  1652. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1653. ;; dired
  1654. (defun my-file-head (filename &optional n)
  1655. "Return list of first N lines of file FILENAME."
  1656. ;; TODO: Fix for janapese text
  1657. ;; TODO: Fix for short text
  1658. (let ((num (or n 10))
  1659. (size 100)
  1660. (beg 0)
  1661. (end 0)
  1662. (result '())
  1663. (read -1))
  1664. (with-temp-buffer
  1665. (erase-buffer)
  1666. (while (or (<= (count-lines (point-min)
  1667. (point-max))
  1668. num)
  1669. (eq read 0))
  1670. (setq end (+ beg size))
  1671. (setq read (nth 1 (insert-file-contents-literally filename
  1672. nil
  1673. beg
  1674. end)))
  1675. (goto-char (point-max))
  1676. (setq beg (+ beg size)))
  1677. (goto-char (point-min))
  1678. (while (< (length result) num)
  1679. (let ((start (point)))
  1680. (forward-line 1)
  1681. (setq result
  1682. `(,@result ,(buffer-substring-no-properties start
  1683. (point))))))
  1684. result
  1685. ;; (buffer-substring-no-properties (point-min)
  1686. ;; (progn
  1687. ;; (forward-line num)
  1688. ;; (point)))
  1689. )))
  1690. ;; (apply 'concat (my-file-head "./shrc" 10)
  1691. (defun my-dired-echo-file-head (arg)
  1692. "Echo head of current file.
  1693. ARG is num to show, or defaults to 7."
  1694. (interactive "P")
  1695. (let ((f (dired-get-filename)))
  1696. (message "%s"
  1697. (apply 'concat
  1698. (my-file-head f
  1699. 7)))))
  1700. (defun my-dired-diff ()
  1701. "Show diff of marked file and file of current line."
  1702. (interactive)
  1703. (let ((files (dired-get-marked-files nil nil nil t)))
  1704. (if (eq (car files)
  1705. t)
  1706. (diff (cadr files) (dired-get-filename))
  1707. (message "One file must be marked!"))))
  1708. (defun dired-get-file-info ()
  1709. "Print information of current line file."
  1710. (interactive)
  1711. (let* ((file (dired-get-filename t))
  1712. (quoted (shell-quote-argument file)))
  1713. (if (file-directory-p file)
  1714. (progn
  1715. (message "Calculating disk usage...")
  1716. (let ((du (or (executable-find "gdu")
  1717. (executable-find "du")
  1718. (error "du not found"))))
  1719. (shell-command (concat du
  1720. " -hsD "
  1721. quoted))))
  1722. (shell-command (concat "file "
  1723. quoted)))))
  1724. (defun my-dired-scroll-up ()
  1725. "Scroll up."
  1726. (interactive)
  1727. (my-dired-previous-line (- (window-height) 1)))
  1728. (defun my-dired-scroll-down ()
  1729. "Scroll down."
  1730. (interactive)
  1731. (my-dired-next-line (- (window-height) 1)))
  1732. ;; (defun my-dired-forward-line (arg)
  1733. ;; ""
  1734. ;; (interactive "p"))
  1735. (defun my-dired-previous-line (arg)
  1736. "Move ARG lines up."
  1737. (interactive "p")
  1738. (if (> arg 0)
  1739. (progn
  1740. (if (eq (line-number-at-pos)
  1741. 1)
  1742. (goto-char (point-max))
  1743. (forward-line -1))
  1744. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1745. (dired-get-subdir))
  1746. (- arg 1)
  1747. arg)))
  1748. (dired-move-to-filename)))
  1749. (defun my-dired-next-line (arg)
  1750. "Move ARG lines down."
  1751. (interactive "p")
  1752. (if (> arg 0)
  1753. (progn
  1754. (if (eq (point)
  1755. (point-max))
  1756. (goto-char (point-min))
  1757. (forward-line 1))
  1758. (my-dired-next-line (if (or (dired-get-filename nil t)
  1759. (dired-get-subdir))
  1760. (- arg 1)
  1761. arg)))
  1762. (dired-move-to-filename)))
  1763. (defun my-tramp-remote-find-file (f)
  1764. "Open F."
  1765. (interactive (list (read-file-name "My Find File Tramp: "
  1766. "/scp:"
  1767. nil ;; "/scp:"
  1768. (confirm-nonexistent-file-or-buffer))))
  1769. (find-file f))
  1770. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1771. (if (eq window-system 'mac)
  1772. (setq dired-listing-switches "-lhF")
  1773. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1774. )
  1775. (setq dired-listing-switches "-lhF")
  1776. ;; when using dired-find-alternate-file
  1777. ;; reuse current dired buffer for the file to open
  1778. ;; (put 'dired-find-alternate-file 'disabled nil)
  1779. (set-variable 'dired-ls-F-marks-symlinks t)
  1780. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1781. (set-variable 'ls-lisp-dirs-first t)
  1782. (set-variable 'ls-lisp-use-localized-time-format t)
  1783. (set-variable 'ls-lisp-format-time-list
  1784. '("%Y-%m-%d %H:%M"
  1785. "%Y-%m-%d "))
  1786. (set-variable 'dired-dwim-target t)
  1787. (set-variable 'dired-isearch-filenames t)
  1788. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1789. (set-variable 'dired-hide-details-hide-information-lines nil)
  1790. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1791. (set-variable 'dired-recursive-deletes 'always)
  1792. ;; (add-hook 'dired-after-readin-hook
  1793. ;; 'my-replace-nasi-none)
  1794. (with-eval-after-load 'dired
  1795. (require 'ls-lisp nil t)
  1796. (defvar dired-mode-map (make-sparse-keymap))
  1797. ;; dired-do-chgrp sometimes cause system hung
  1798. (define-key dired-mode-map "G" 'ignore)
  1799. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1800. (define-key dired-mode-map "i" 'dired-get-file-info)
  1801. ;; (define-key dired-mode-map "f" 'find-file)
  1802. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1803. (define-key dired-mode-map "z" 'fzf)
  1804. (define-key dired-mode-map "!" 'shell-command)
  1805. (define-key dired-mode-map "&" 'async-shell-command)
  1806. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1807. (define-key dired-mode-map "=" 'my-dired-diff)
  1808. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1809. (define-key dired-mode-map "b" 'gtkbm)
  1810. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1811. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1812. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1813. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1814. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1815. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1816. (substitute-key-definition 'dired-next-line
  1817. 'my-dired-next-line
  1818. dired-mode-map)
  1819. (substitute-key-definition 'dired-previous-line
  1820. 'my-dired-previous-line
  1821. dired-mode-map)
  1822. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1823. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1824. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1825. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1826. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1827. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1828. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1829. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1830. (add-hook 'dired-mode-hook
  1831. (lambda ()
  1832. (when (fboundp 'dired-hide-details-mode)
  1833. (dired-hide-details-mode t)
  1834. (local-set-key "l" 'dired-hide-details-mode))
  1835. (let ((file "._Icon\015"))
  1836. (when nil
  1837. '(file-readable-p file)
  1838. (delete-file file)))))
  1839. (when (fboundp 'pack-dired-dwim)
  1840. (with-eval-after-load 'dired
  1841. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1842. (when (fboundp 'dired-list-all-mode)
  1843. (setq dired-listing-switches "-lhF")
  1844. (with-eval-after-load 'dired
  1845. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1846. (when (fboundp 'dired-filter-mode)
  1847. (add-hook 'dired-mode-hook
  1848. 'dired-filter-mode))
  1849. (set-variable 'dired-filter-stack nil)
  1850. ;; Currently disabled in favor of dired-from-git-ls-files
  1851. ;; (define-key ctl-x-map "f" 'find-dired)
  1852. (defvar my-dired-git-ls-files-history
  1853. "History for `my-dired-git-ls-files'." nil)
  1854. (defun my-dired-git-ls-files (arg)
  1855. "Dired from git ls-files."
  1856. (interactive (list
  1857. (read-shell-command "git ls-files: "
  1858. "git ls-files -z ")))
  1859. (pop-to-buffer-same-window
  1860. (dired-noselect `(,default-directory
  1861. ,@(split-string (shell-command-to-string arg)
  1862. "\0" t))
  1863. ""))
  1864. )
  1865. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1866. (with-eval-after-load 'dired
  1867. (defvar dired-mode-map (make-sparse-keymap))
  1868. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1869. (with-eval-after-load 'pack
  1870. (set-variable 'pack-silence
  1871. t)
  1872. (defvar pack-program-alist)
  1873. (add-to-list 'pack-program-alist
  1874. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf"))
  1875. (when (executable-find "aunpack")
  1876. (add-to-list 'pack-program-alist
  1877. ' ("\\.zip\\'"
  1878. :pack ("zip" "-r" archive sources)
  1879. :pack-append ("zip" "-r" archive sources)
  1880. :unpack ("aunpack" archive))))
  1881. )
  1882. ;; (define-minor-mode my-dired-glob-filter)
  1883. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1884. ;; misc funcs
  1885. (define-key ctl-x-map "T" 'git-worktree)
  1886. (define-key ctl-x-map "W" 'git-walktree)
  1887. (when (fboundp 'browse-url-default-macosx-browser)
  1888. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1889. (defalias 'qcalc 'quick-calc)
  1890. (defun memo (&optional dir)
  1891. "Open memo.txt in DIR."
  1892. (interactive)
  1893. (pop-to-buffer (find-file-noselect (concat (if dir
  1894. (file-name-as-directory dir)
  1895. "")
  1896. "memo.txt"))))
  1897. ;; TODO: remember-projectile
  1898. (set (defvar my-privnotes-path nil
  1899. "My privnotes repository path.")
  1900. (expand-file-name "~/my/privnotes"))
  1901. (defun my-privnotes-readme (dir)
  1902. "Open my privnotes DIR."
  1903. (interactive (list
  1904. (read-file-name "Privnotes: "
  1905. (expand-file-name (format-time-string "%Y%m%d_")
  1906. my-privnotes-path))))
  1907. (let ((path (expand-file-name "README.md" dir)))
  1908. (with-current-buffer (find-file path)
  1909. (unless (file-exists-p path)
  1910. (insert (file-name-base dir)
  1911. "\n"
  1912. "=======\n"
  1913. "\n\n")))))
  1914. (define-key ctl-x-map "p" 'my-privnotes-readme)
  1915. (set (defvar my-rgrep-alist nil
  1916. "Alist of rgrep command.
  1917. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1918. condition to choose COMMAND when evaluated.")
  1919. `(
  1920. ;; ripgrep
  1921. ("rg"
  1922. (executable-find "rg")
  1923. "rg -nH --no-heading --hidden --glob '!.git/' --smart-case -M 1280 ")
  1924. ;; git grep
  1925. ("gitgrep"
  1926. (eq 0
  1927. (shell-command "git rev-parse --git-dir"))
  1928. "git --no-pager grep -nH -e ")
  1929. ;; sift
  1930. ("sift"
  1931. (executable-find "sift")
  1932. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1933. ;; the silver searcher
  1934. ("ag"
  1935. (executable-find "ag")
  1936. "ag --nogroup --nopager --filename ")
  1937. ;; ack
  1938. ("ack"
  1939. (executable-find "ack")
  1940. "ack --nogroup --nopager --with-filename ")
  1941. ;; gnu global
  1942. ("global"
  1943. (and (require 'ggtags nil t)
  1944. (executable-find "global")
  1945. (ggtags-current-project-root))
  1946. "global --result grep ")
  1947. ;; grep
  1948. ("grep"
  1949. t
  1950. ,(concat "find . "
  1951. "-path '*/.git' -prune -o "
  1952. "-path '*/.svn' -prune -o "
  1953. "-type f -print0 | "
  1954. "xargs -0 grep -nH -e "))
  1955. )
  1956. )
  1957. (defvar my-rgrep-default nil
  1958. "Default command name for my-rgrep.")
  1959. (defun my-rgrep-grep-command (&optional name alist)
  1960. "Return recursive grep command for current directory or nil.
  1961. If NAME is given, use that without testing.
  1962. Commands are searched from ALIST."
  1963. (if alist
  1964. (if name
  1965. ;; if name is given search that from alist and return the command
  1966. (nth 2 (assoc name
  1967. alist))
  1968. ;; if name is not given try test in 1th elem
  1969. (let ((car (car alist))
  1970. (cdr (cdr alist)))
  1971. (if (eval (nth 1 car))
  1972. ;; if the condition is true return the command
  1973. (nth 2 car)
  1974. ;; try next one
  1975. (and cdr
  1976. (my-rgrep-grep-command name cdr)))))
  1977. ;; if alist is not given set default value
  1978. (my-rgrep-grep-command name my-rgrep-alist)))
  1979. (defun my-rgrep (command-args)
  1980. "My recursive grep. Run COMMAND-ARGS.
  1981. If prefix argument is given, use current symbol as default search target
  1982. and search from projectile root (if projectile is available)."
  1983. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1984. nil)))
  1985. (if cmd
  1986. (list (read-shell-command "grep command: "
  1987. (concat cmd
  1988. (if current-prefix-arg
  1989. (thing-at-point 'symbol t)
  1990. ""))
  1991. 'grep-find-history))
  1992. (error "My-Rgrep: Command for rgrep not found")
  1993. )))
  1994. (if (and current-prefix-arg
  1995. (eval-and-compile (require 'projectile nil t))
  1996. (projectile-project-p))
  1997. (projectile-with-default-dir (projectile-project-root)
  1998. (compilation-start command-args
  1999. 'grep-mode))
  2000. (compilation-start command-args
  2001. 'grep-mode)))
  2002. (defun my-rgrep-thing-at-point-projectile-root ()
  2003. "My recursive grep to find thing at point from project root."
  2004. (interactive)
  2005. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  2006. nil))
  2007. (command-args
  2008. (if cmd
  2009. (concat cmd
  2010. (or (thing-at-point 'symbol t)
  2011. (error "No symbol at point")))
  2012. (error "My-Rgrep: Command for rgrep not found"))))
  2013. (if (eval-and-compile (require 'projectile nil t))
  2014. (projectile-with-default-dir (or (projectile-project-root)
  2015. default-directory)
  2016. (compilation-start command-args
  2017. 'grep-mode))
  2018. (compilation-start command-args
  2019. 'grep-mode))))
  2020. (defmacro define-my-rgrep (name)
  2021. "Define rgrep for NAME."
  2022. `(defun ,(intern (concat "my-rgrep-"
  2023. name)) ()
  2024. ,(format "My recursive grep by %s."
  2025. name)
  2026. (interactive)
  2027. (let ((my-rgrep-default ,name))
  2028. (if (called-interactively-p 'any)
  2029. (call-interactively 'my-rgrep)
  2030. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2031. )
  2032. (define-my-rgrep "ack")
  2033. (define-my-rgrep "ag")
  2034. (define-my-rgrep "rg")
  2035. (define-my-rgrep "sift")
  2036. (define-my-rgrep "gitgrep")
  2037. (define-my-rgrep "grep")
  2038. (define-my-rgrep "global")
  2039. (define-key ctl-x-map "s" 'my-rgrep)
  2040. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  2041. (defun my-occur (regexp &optional region)
  2042. "My occur command to search REGEXP."
  2043. (interactive (list (read-string "List lines matching regexp: "
  2044. (thing-at-point 'symbol t))))
  2045. (occur regexp nil region))
  2046. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  2047. (set-variable 'dumb-jump-prefer-searcher 'rg)
  2048. (defalias 'make 'compile)
  2049. (define-key ctl-x-map "c" 'compile)
  2050. (defun my-pushbullet-note (text &optional title)
  2051. "Push TEXT."
  2052. (interactive "sText to Push: ")
  2053. (pb/push-item '("") text "note" (or title "")))
  2054. ;;;;;;;;;;;;;;;;;;;;;
  2055. ;; git-bug
  2056. (defconst git-bug-ls-regexp
  2057. (eval-when-compile
  2058. (rx bol
  2059. (submatch (one-or-more alphanumeric)) ; id
  2060. ;; (one-or-more any)
  2061. (one-or-more space)
  2062. (submatch (or "open" "close")) ; status
  2063. (one-or-more space)
  2064. (submatch (maximal-match (zero-or-more print))) ; title
  2065. "\t"
  2066. (submatch (one-or-more alphanumeric)) ; user
  2067. (one-or-more space)
  2068. "C:"
  2069. (submatch (one-or-more digit)) ; Comment num
  2070. (one-or-more space)
  2071. "L:"
  2072. (submatch (one-or-more digit)) ; Label num
  2073. eol
  2074. ))
  2075. "Regexp to parse line of output of git-bug ls.
  2076. Used by `git-bug-ls'.")
  2077. (defun git-bug-bugs ()
  2078. "Get list of git-bug bugs."
  2079. (with-temp-buffer
  2080. (git-bug--call-process "bug" "ls")
  2081. (goto-char (point-min))
  2082. (let ((bugs nil))
  2083. (while (not (eq (point) (point-max)))
  2084. (save-match-data
  2085. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  2086. (setq bugs `(,@bugs
  2087. ,(list
  2088. :id (match-string 1)
  2089. :status (match-string 2)
  2090. :title (string-trim (match-string 3))
  2091. :user (match-string 4)
  2092. :comment-num (match-string 5)
  2093. :label-num (match-string 6)
  2094. )))))
  2095. (forward-line 1)
  2096. (goto-char (point-at-bol)))
  2097. bugs)))
  2098. (defun git-bug-ls ()
  2099. "Open and select git bug list buffer."
  2100. (interactive)
  2101. (pop-to-buffer (git-bug-ls-noselect)))
  2102. (defun git-bug-ls-noselect (&optional directory)
  2103. "Open git bug list buffer.
  2104. If optional arg DIRECTORY is given change current directory to there before
  2105. initializing."
  2106. (setq directory (expand-file-name (or directory
  2107. default-directory)))
  2108. (cl-assert (file-directory-p directory))
  2109. (let* ((root (git-bug--get-repository-root directory))
  2110. (name (file-name-nondirectory root))
  2111. (bname (format "*GitBug<%s>*" name)))
  2112. (with-current-buffer (get-buffer-create bname)
  2113. (cd root)
  2114. (git-bug-ls--set-tabulated-list-mode-variables)
  2115. (git-bug-ls-mode)
  2116. (current-buffer))))
  2117. (defun git-bug--get-repository-root (dir)
  2118. "Resolve repository root of DIR.
  2119. If DIR is not inside of any git repository, signal an error."
  2120. (cl-assert (file-directory-p dir))
  2121. (with-temp-buffer
  2122. (cd dir)
  2123. (git-bug--call-process "rev-parse" "--show-toplevel")
  2124. (goto-char (point-min))
  2125. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  2126. (defun git-bug--call-process (&rest args)
  2127. "Start git process synchronously with ARGS.
  2128. Raise error when git process ends with non-zero status.
  2129. Any output will be written to current buffer."
  2130. (let ((status (apply 'call-process
  2131. "git"
  2132. nil
  2133. t
  2134. nil
  2135. args)))
  2136. (cl-assert (eq status 0)
  2137. nil
  2138. (buffer-substring-no-properties (point-min) (point-max)))))
  2139. ;;;;;;;;;;;;;;;;;;;
  2140. ;; peek-file-mode
  2141. (defvar peek-file-buffers
  2142. ()
  2143. "Peek buffers.")
  2144. (defun peek-file (file)
  2145. "Peek FILE."
  2146. (interactive "fFile to peek: ")
  2147. (with-current-buffer (find-file file)
  2148. (peek-file-mode)))
  2149. (define-minor-mode peek-file-mode
  2150. "Peek file mode."
  2151. :lighter "PK"
  2152. (view-mode peek-file-mode)
  2153. (add-to-list 'peek-file-buffers
  2154. (current-buffer))
  2155. (add-hook 'switch-buffer-functions
  2156. 'peek-file-remove-buffers))
  2157. (defun peek-file-remove-buffers (&args _)
  2158. "Remove peek file buffers."
  2159. (cl-dolist (buf (cl-copy-list peek-file-buffers))
  2160. (unless (get-buffer-window buf t)
  2161. (setq peek-file-buffers
  2162. (delq buf
  2163. peek-file-buffers))
  2164. (with-current-buffer buf
  2165. (when peek-file-mode
  2166. (kill-buffer))))))
  2167. ;;;;;;;;;;;;;;;;;;;;
  2168. ;; remember-projectile
  2169. ;; TODO: Add global-minor-mode
  2170. (defvar remember-data-file)
  2171. (defun my-set-remember-data-file-buffer-local ()
  2172. "Set `remember-data-file'."
  2173. (when (require 'projectile nil t)
  2174. (setq-local remember-data-file
  2175. (expand-file-name ".remember.notes"
  2176. (projectile-project-root)))))
  2177. (add-hook 'after-change-major-mode-hook
  2178. 'my-set-remember-data-file-buffer-local)
  2179. (define-key ctl-x-map "R" 'remember)
  2180. ;; ivy
  2181. (defvar ivy-re-builders-alist)
  2182. (set-variable 'ivy-re-builders-alist
  2183. '((t . my--ivy-regex-fuzzy-ignore-order)))
  2184. (defun my--ivy-regex-fuzzy-ignore-order (str)
  2185. "Re-build regex from STR for ignore-order fuzzy match."
  2186. (let ((re-list (ivy--regex-ignore-order str)))
  2187. (if (listp re-list)
  2188. (mapcar (lambda (e)
  2189. (let ((head (car e))
  2190. (tail (cdr e)))
  2191. (if tail
  2192. (cons (ivy--regex-fuzzy head)
  2193. tail)
  2194. (cons head tail))))
  2195. re-list)
  2196. (ivy--regex-fuzzy re-list))))
  2197. ;; (my--ivy-regex-fuzzy-ignore-order "ab bc !cee")
  2198. ;; (ivy--regex-fuzzy "ab")
  2199. ;; (ivy--regex-ignore-order "a b")
  2200. (with-eval-after-load 'ivy
  2201. (defvar ivy-minibuffer-map)
  2202. (define-key ivy-minibuffer-map (kbd "C-u")
  2203. (lambda () (interactive) (delete-region (point-at-bol) (point)))))
  2204. (when (fboundp 'counsel-M-x)
  2205. (define-key esc-map "x" 'counsel-M-x)
  2206. ;; (counsel-mode 1)
  2207. ;; counsel-fzf executes fzf -f QUERY for each input
  2208. ;; (define-key ctl-x-map "f"
  2209. ;; (lambda ()
  2210. ;; (interactive
  2211. ;; (let ((process-environment (cl-copy-list process-environment)))
  2212. ;; (setenv "FZF_DEFAULT_COMMAND" nil)
  2213. ;; (counsel-fzf)))))
  2214. )
  2215. (when (and (fboundp 'ivy-read)
  2216. (locate-library "counsel"))
  2217. (defvar counsel-describe-map)
  2218. (defun my-counsel-describe-symbol ()
  2219. "Forwaord to `describe-symbol'."
  2220. (interactive)
  2221. (cl-assert (eval-and-compile (require 'help-mode nil t))) ;; describe-symbol-backends
  2222. (cl-assert (eval-and-compile (require 'counsel nil t)))
  2223. (ivy-read "Describe symbol: " obarray
  2224. ;; From describe-symbol definition
  2225. :predicate (lambda (vv)
  2226. (cl-some (lambda (x) (funcall (nth 1 x) vv))
  2227. describe-symbol-backends))
  2228. :require-match t
  2229. :history 'counsel-describe-symbol-history
  2230. :keymap counsel-describe-map
  2231. :preselect (ivy-thing-at-point)
  2232. :action (lambda (x)
  2233. (describe-symbol (intern x)))
  2234. :caller 'my-counsel-describe-symbol))
  2235. (define-key help-map "o" 'my-counsel-describe-symbol)
  2236. )
  2237. (with-eval-after-load 'ivy
  2238. (ivy-configure 'my-counsel-describe-symbol
  2239. :sort-fn #'ivy-string<)
  2240. )
  2241. (when (fboundp 'counsel-imenu)
  2242. (define-key ctl-x-map "l" 'counsel-imenu))
  2243. (when (fboundp 'swoop)
  2244. ;; (global-set-key (kbd "C-s") 'swoop)
  2245. ;; (global-set-key (kbd "C-r") 'swoop)
  2246. ;; (define-key esc-map (kbd "C-s") 'swoop-multi)
  2247. (define-key esc-map (kbd "C-s") 'swoop)
  2248. (with-eval-after-load 'swoop-lib
  2249. (defvar swoop-map)
  2250. (define-key swoop-map (kbd "C-s") 'swoop-action-goto-line-next)
  2251. (define-key swoop-map (kbd "C-r") 'swoop-action-goto-line-prev)
  2252. )
  2253. ;; TODO: case sensitive swoop
  2254. ;; Wrap swoop-async-get-match-lines-list?
  2255. ;; (with-eval-after-load 'swoop-lib
  2256. ;; (defun my-swoop-advice-smart-case (orig-func &rest args)
  2257. ;; "Function to wrap swoop function."
  2258. ;; (message "args: %S" args)
  2259. ;; (let* (
  2260. ;; (query (car args))
  2261. ;; ;; (query (plist-get args
  2262. ;; ;; :$query))
  2263. ;; (case-fold-search
  2264. ;; (let ((case-fold-search nil))
  2265. ;; (not (string-match-p (rx upper) query))))
  2266. ;; )
  2267. ;; ;; (message "case-fold-search: %S" case-fold-search)
  2268. ;; ;; (message "query: %S" query)
  2269. ;; (apply orig-func args)))
  2270. ;; (advice-add 'swoop-async-get-match-lines-list
  2271. ;; :around
  2272. ;; 'my-swoop-advice-smart-case)
  2273. ;; (set-variable 'swoop-async-get-match-lines-list
  2274. ;; (byte-compile 'swoop-async-get-match-lines-list))
  2275. ;; )
  2276. )
  2277. ;; なんかよくわからないけど頻繁に index.lock を残してしまう
  2278. (when (fboundp 'dired-k)
  2279. (set-variable 'dired-k-style 'git)
  2280. ;; What is the best way of doing this?
  2281. (with-eval-after-load 'dired-k
  2282. (fset 'dired-k--highlight-by-file-attribyte 'ignore))
  2283. ;; (set-variable 'dired-k-size-colors
  2284. ;; `((,most-positive-fixnum)))
  2285. ;; (set-variable 'dired-k-date-colors
  2286. ;; `((,most-positive-fixnum)))
  2287. ;; always execute dired-k when dired buffer is opened and reverted
  2288. ;; (add-hook 'dired-after-readin-hook #'dired-k-no-revert)
  2289. ;; This causes:
  2290. ;; fatal: Unable to create '.git/index.lock': File exist.s
  2291. ;; (add-hook 'switch-buffer-functions
  2292. ;; (lambda (prev cur)
  2293. ;; (when (derived-mode-p 'dired-mode)
  2294. ;; (dired-k-no-revert))))
  2295. )
  2296. (add-hook 'python-mode-hook
  2297. (lambda ()
  2298. ;; Currently on python-mode eldoc-mode sometimes print
  2299. ;; wired message on "from" keyword:
  2300. ;;var from = require("./from")
  2301. (eldoc-mode -1)))
  2302. ;; ?
  2303. (define-key input-decode-map "\e[1;5C" [C-right])
  2304. (define-key input-decode-map "\e[1;5D" [C-left])
  2305. ;; mozc
  2306. (global-set-key (kbd "C-c m e") 'ignore)
  2307. (global-set-key (kbd "C-c m d") 'ignore)
  2308. ;; mozc
  2309. (when (locate-library "mozc")
  2310. ;; https://tottoto.net/mac-emacs-karabiner-elements-japanese-input-method-config/
  2311. (with-eval-after-load 'mozc
  2312. ;; (define-key mozc-mode-map (kbd "C-h") 'backward-delete-char)
  2313. ;; (define-key mozc-mode-map (kbd "C-p") (kbd "<up>"))
  2314. ;; (define-key mozc-mode-map (kbd "C-n") (kbd "SPC"))
  2315. )
  2316. (setq default-input-method "japanese-mozc")
  2317. (custom-set-variables '(mozc-leim-title "あ"))
  2318. (defun turn-on-input-method ()
  2319. (interactive)
  2320. (activate-input-method default-input-method))
  2321. (defun turn-off-input-method ()
  2322. (interactive)
  2323. (deactivate-input-method))
  2324. ;; (setq mozc-candidate-style 'echo-area)
  2325. (global-set-key (kbd "C-c m e") 'turn-on-input-method)
  2326. (global-set-key (kbd "C-c m d") 'turn-off-input-method)
  2327. (require 'mozc-popup)
  2328. (set-variable 'mozc-candidate-style 'popup)
  2329. ;; これいる?
  2330. (when (require 'mozc-im nil t)
  2331. (setq default-input-method "japanese-mozc-im")
  2332. ;; (global-set-key (kbd "C-j") 'toggle-input-method)
  2333. )
  2334. )
  2335. ;; Local Variables:
  2336. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2337. ;; flycheck-checker: emacs-lisp
  2338. ;; End:
  2339. ;;; emancs.el ends here