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.
 
 
 
 
 
 

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