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.
 
 
 
 
 
 

2707 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. ;; TODO: Remove: Just checking fboundp should be enough for most cases
  41. (defmacro safe-require-or-eval (feature)
  42. "Require FEATURE if available.
  43. At compile time the feature will be loaded immediately."
  44. `(eval-and-compile
  45. (message "safe-require-or-eval: Trying to require %s" ,feature)
  46. (require ,feature nil t)))
  47. ;; TODO: Remove: Defining autoload is no more needed for most cases
  48. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  49. "Define autoloading FEATURE that defines FUNCTIONS.
  50. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  51. the function same as FEATURE is defined as autoloaded function. BODY is passed
  52. to `eval-after-load'.
  53. After this macro is expanded, this returns the path to library if FEATURE
  54. found, otherwise returns nil."
  55. (declare (indent 2) (debug t))
  56. (let* ((libname (symbol-name (eval feature)))
  57. (libpath (locate-library libname)))
  58. `(progn
  59. (when (locate-library ,libname)
  60. ,@(mapcar (lambda (f)
  61. `(unless (fboundp ',f)
  62. (progn
  63. (message "Autoloaded function `%S' defined (%s)"
  64. (quote ,f)
  65. ,libpath)
  66. (autoload (quote ,f)
  67. ,libname
  68. ,(concat "Autoloaded function defined in \""
  69. libpath
  70. "\".")
  71. t))))
  72. (or (eval functions)
  73. `(,(eval feature)))))
  74. (eval-after-load ,feature
  75. (quote (progn
  76. ,@body)))
  77. (locate-library ,libname))))
  78. (when (autoload-eval-lazily 'tetris nil
  79. (message "Tetris loaded!"))
  80. (message "Tetris found!"))
  81. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  82. ;; package
  83. (set (defvar 10sr-package-list)
  84. '(
  85. vimrc-mode
  86. markdown-mode
  87. yaml-mode
  88. gnuplot-mode
  89. php-mode
  90. erlang
  91. js2-mode
  92. js-doc
  93. git-commit
  94. gitignore-mode
  95. adoc-mode
  96. go-mode
  97. ;; It seems malabar has been merged into jdee and this package
  98. ;; already removed
  99. ;; malabar-mode
  100. gosh-mode
  101. scala-mode
  102. web-mode
  103. color-moccur
  104. ggtags
  105. flycheck
  106. auto-highlight-symbol
  107. hl-todo
  108. ;; Currently not available
  109. ;; pp-c-l
  110. xclip
  111. foreign-regexp
  112. multi-term
  113. term-run
  114. editorconfig
  115. git-ps1-mode
  116. restart-emacs
  117. fill-column-indicator
  118. pkgbuild-mode
  119. minibuffer-line
  120. which-key
  121. ;; I think this works in place of my autosave lib
  122. super-save
  123. pipenv
  124. imenu-list
  125. page-break-lines
  126. aggressive-indent
  127. dired-filter
  128. wgrep
  129. magit
  130. git-gutter
  131. end-mark
  132. sl
  133. ;; TODO: Configure pony-tpl-mode
  134. pony-mode
  135. gited
  136. highlight-indentation
  137. diminish
  138. fzf
  139. fic-mode
  140. term-cursor
  141. pydoc
  142. swoop
  143. color-identifiers-mode
  144. dired-k
  145. blacken
  146. editorconfig
  147. editorconfig-custom-majormode
  148. git-command
  149. prompt-text
  150. ;; 10sr repository
  151. ;; 10sr-extras
  152. terminal-title
  153. dired-list-all-mode
  154. pack
  155. set-modeline-color
  156. read-only-only-mode
  157. smart-revert
  158. autosave
  159. ;;window-organizer
  160. ilookup
  161. pasteboard
  162. awk-preview
  163. recently
  164. ))
  165. (when (safe-require-or-eval 'package)
  166. (setq package-archives
  167. `(,@package-archives
  168. ("melpa" . "https://melpa.org/packages/")
  169. ;; Somehow fails to download via https
  170. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  171. (package-initialize)
  172. (defun my-auto-install-package ()
  173. "Install packages semi-automatically."
  174. (interactive)
  175. (package-refresh-contents)
  176. (mapc (lambda (pkg)
  177. (or (package-installed-p pkg)
  178. (package-install pkg)))
  179. 10sr-package-list))
  180. )
  181. ;; (lazy-load-eval 'sudoku)
  182. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  183. ;; my-idle-hook
  184. (defvar my-idle-hook nil
  185. "Hook run when idle for several secs.")
  186. (defvar my-idle-hook-sec 5
  187. "Second to run `my-idle-hook'.")
  188. (run-with-idle-timer my-idle-hook-sec
  189. t
  190. (lambda ()
  191. (run-hooks 'my-idle-hook)))
  192. ;; (add-hook 'my-idle-hook
  193. ;; (lambda ()
  194. ;; (message "idle hook message")))
  195. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  196. ;; start and quit
  197. (setq inhibit-startup-message t)
  198. (setq initial-buffer-choice 'messages-buffer)
  199. (setq confirm-kill-emacs 'y-or-n-p)
  200. (setq gc-cons-threshold (* 1024 1024 16))
  201. (setq garbage-collection-messages nil)
  202. (when window-system
  203. (add-to-list 'default-frame-alist '(cursor-type . box))
  204. (add-to-list 'default-frame-alist '(background-color . "white"))
  205. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  206. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  207. ;; does not work?
  208. )
  209. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  210. (menu-bar-mode 1)
  211. (define-key ctl-x-map "m" 'menu-bar-open)
  212. (and (fboundp 'tool-bar-mode)
  213. (tool-bar-mode 0))
  214. (and (fboundp 'set-scroll-bar-mode)
  215. (set-scroll-bar-mode nil))
  216. (eval-after-init
  217. (message "%s %s" invocation-name emacs-version)
  218. (message "Invocation directory: %s" default-directory)
  219. (message "%s was taken to initialize emacs." (emacs-init-time))
  220. ;; (view-echo-area-messages)
  221. ;; (view-emacs-news)
  222. )
  223. (with-current-buffer "*Messages*"
  224. (emacs-lock-mode 'kill))
  225. (cd ".") ; when using windows use / instead of \ in `default-directory'
  226. ;; locale
  227. (set-language-environment "Japanese")
  228. (set-default-coding-systems 'utf-8-unix)
  229. (prefer-coding-system 'utf-8-unix)
  230. (setq system-time-locale "C")
  231. ;; my prefix map
  232. (defvar my-prefix-map nil
  233. "My prefix map.")
  234. (define-prefix-command 'my-prefix-map)
  235. (global-set-key (kbd "C-^") 'my-prefix-map)
  236. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  237. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  238. ;; (comint-show-maximum-output)
  239. ;; kill scratch
  240. (eval-after-init
  241. (let ((buf (get-buffer "*scratch*")))
  242. (when buf
  243. (kill-buffer buf))))
  244. ;; modifier keys
  245. ;; (setq mac-option-modifier 'control)
  246. ;; display
  247. (setq visible-bell t)
  248. (setq ring-bell-function 'ignore)
  249. (mouse-avoidance-mode 'banish)
  250. (setq echo-keystrokes 0.1)
  251. (defun reload-init-file ()
  252. "Reload Emacs init file."
  253. (interactive)
  254. (when (and user-init-file
  255. (file-readable-p user-init-file))
  256. (load-file user-init-file)))
  257. (safe-require-or-eval 'session)
  258. ;; server
  259. (set-variable 'server-name (concat "server"
  260. (number-to-string (emacs-pid))))
  261. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  262. ;; In Darwin environment, init fails with message like 'Service name too long'
  263. ;; when server-use-tcp is nil
  264. (when (or (eq system-type
  265. 'cygwin)
  266. (eq system-type
  267. 'darwin))
  268. (set-variable 'server-use-tcp t))
  269. ;; MSYS2 fix
  270. (when (eq system-type
  271. 'windows-nt)
  272. (setq shell-file-name
  273. (executable-find "bash"))
  274. '(setq function-key-map
  275. `(,@function-key-map ([pause] . [?\C-c])
  276. ))
  277. (define-key key-translation-map
  278. (kbd "<pause>")
  279. (kbd "C-c"))
  280. '(keyboard-translate [pause]
  281. (kbd "C-c")p)
  282. ;; TODO: move to other place later
  283. (when (not window-system)
  284. (setq interprogram-paste-function nil)
  285. (setq interprogram-cut-function nil)))
  286. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  287. ;; global keys
  288. (global-set-key (kbd "<up>") 'scroll-down-line)
  289. (global-set-key (kbd "<down>") 'scroll-up-line)
  290. (global-set-key (kbd "<left>") 'scroll-down)
  291. (global-set-key (kbd "<right>") 'scroll-up)
  292. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  293. (global-set-key (kbd "C-\\") help-map)
  294. (define-key ctl-x-map (kbd "DEL") help-map)
  295. (define-key ctl-x-map (kbd "C-h") help-map)
  296. (define-key help-map "a" 'apropos)
  297. ;; disable annoying keys
  298. (global-set-key [prior] 'ignore)
  299. (global-set-key (kbd "<next>") 'ignore)
  300. (global-set-key [menu] 'ignore)
  301. (global-set-key [down-mouse-1] 'ignore)
  302. (global-set-key [down-mouse-2] 'ignore)
  303. (global-set-key [down-mouse-3] 'ignore)
  304. (global-set-key [mouse-1] 'ignore)
  305. (global-set-key [mouse-2] 'ignore)
  306. (global-set-key [mouse-3] 'ignore)
  307. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  308. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  309. ;; Interactively evaluate Emacs Lisp expressions
  310. (define-key ctl-x-map "i" 'ielm)
  311. (when (safe-require-or-eval 'which-key)
  312. (which-key-mode))
  313. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  314. ;; editor
  315. ;; Basically it should not set globally (instead use something like file local
  316. ;; variables or editorconfig), but for most cases I just need this...
  317. (defun my-set-require-final-newline ()
  318. "Set `require-final-newline'."
  319. (set (make-local-variable 'require-final-newline)
  320. mode-require-final-newline))
  321. (add-hook 'prog-mode-hook
  322. 'my-set-require-final-newline)
  323. (add-hook 'text-mode-hook
  324. 'my-set-require-final-newline)
  325. (add-hook 'conf-mode-hook
  326. 'my-set-require-final-newline)
  327. ;; Used from term-cursor
  328. ;; hbar is too hard to find...
  329. (defun my-cursor-type-change (&rest args)
  330. "ARGS are discarded."
  331. ;; TODO: Support wdired and wgrep
  332. (if buffer-read-only
  333. (setq cursor-type 'hbar)
  334. (setq cursor-type 'box)))
  335. ;; (add-hook 'switch-buffer-functions
  336. ;; 'my-cursor-type-change)
  337. ;; (add-hook 'read-only-mode-hook
  338. ;; 'my-cursor-type-change)
  339. ;; (when (fboundp 'global-term-cursor-mode)
  340. ;; (global-term-cursor-mode 1))
  341. ;; ;; (term-cursor--eval)
  342. (setq kill-whole-line t)
  343. (setq scroll-conservatively 35
  344. scroll-margin 2)
  345. (setq-default major-mode 'text-mode)
  346. (setq next-line-add-newlines nil)
  347. (setq kill-read-only-ok t)
  348. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  349. ;; (setq-default line-spacing 0.2)
  350. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  351. ;; (setq-default tab-width 4)
  352. (setq-default indent-tabs-mode nil)
  353. (setq-default indent-line-function 'indent-to-left-margin)
  354. ;; (setq-default indent-line-function nil)
  355. (setq-default truncate-lines nil)
  356. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  357. (delete-selection-mode 1)
  358. (cua-mode 0)
  359. (setq line-move-visual nil)
  360. (setq create-lockfiles nil)
  361. (add-hook 'before-save-hook
  362. 'time-stamp)
  363. ;; Add Time-stamp: <> to insert timestamp there
  364. (set-variable 'time-stamp-format
  365. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  366. ;; key bindings
  367. ;; moving around
  368. ;;(keyboard-translate ?\M-j ?\C-j)
  369. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  370. (define-key esc-map "p" 'backward-paragraph)
  371. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  372. (define-key esc-map "n" 'forward-paragraph)
  373. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  374. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  375. (global-set-key (kbd "C-<left>") 'scroll-down)
  376. (global-set-key (kbd "C-<right>") 'scroll-up)
  377. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  378. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  379. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  380. ;; C-h and DEL
  381. (global-set-key (kbd "C-h") (kbd "DEL"))
  382. ;; (normal-erase-is-backspace-mode 1)
  383. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  384. (global-set-key (kbd "C-m") 'newline-and-indent)
  385. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  386. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  387. (define-key esc-map "u" 'undo)
  388. (define-key esc-map "i" (kbd "ESC TAB"))
  389. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  390. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  391. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  392. (when (fboundp 'undo-fu-only-undo)
  393. (global-set-key (kbd "C-_") 'undo-fu-only-undo))
  394. (when (fboundp 'undo-fu-only-redo)
  395. (global-set-key (kbd "C-M-_") 'undo-fu-only-redo))
  396. (require 'page-ext nil t)
  397. (when (safe-require-or-eval 'page-break-lines)
  398. (global-page-break-lines-mode 1))
  399. (when (safe-require-or-eval 'git-gutter)
  400. (declare-function global-git-gutter-mode "git-gutter")
  401. (custom-set-variables
  402. '(git-gutter:lighter " Gttr"))
  403. (custom-set-variables
  404. '(git-gutter:update-interval 2))
  405. (custom-set-variables
  406. '(git-gutter:unchanged-sign " "))
  407. (when (>= (display-color-cells)
  408. 256)
  409. (let ((c "color-233"))
  410. (set-face-background 'git-gutter:modified c)
  411. (set-face-background 'git-gutter:added c)
  412. (set-face-background 'git-gutter:deleted c)
  413. (set-face-background 'git-gutter:unchanged c)))
  414. (global-git-gutter-mode 1)
  415. )
  416. ;; (when (safe-require-or-eval 'fancy-narrow)
  417. ;; (fancy-narrow-mode 1))
  418. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  419. ;; title and mode-line
  420. (when (safe-require-or-eval 'terminal-title)
  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 (safe-require-or-eval 'diminish)
  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 (safe-require-or-eval 'minibuffer-line)
  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 (safe-require-or-eval 'prompt-text)
  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. (autoload-eval-lazily 'helm nil
  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 (safe-require-or-eval 'paren)
  623. (show-paren-mode 1)
  624. (setq show-paren-delay 0.5
  625. show-paren-style 'parenthesis) ; mixed is hard to read
  626. ;; (set-face-background 'show-paren-match
  627. ;; "black")
  628. ;; ;; (face-foreground 'default))
  629. ;; (set-face-foreground 'show-paren-match
  630. ;; "white")
  631. ;; (set-face-inverse-video-p 'show-paren-match
  632. ;; t)
  633. )
  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 (safe-require-or-eval 'whitespace)
  657. (add-to-list 'whitespace-display-mappings
  658. ;; We need t since last one takes precedence
  659. `(tab-mark ?\t ,(vconcat "|>\t")) t)
  660. ;; (add-to-list 'whitespace-display-mappings
  661. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  662. (setq whitespace-style '(face
  663. trailing ; trailing blanks
  664. ;; tabs
  665. ;; spaces
  666. ;; lines
  667. lines-tail ; lines over 80
  668. newline ; newlines
  669. ;; empty ; empty lines at beg or end of buffer
  670. ;; big-indent
  671. ;; space-mark
  672. tab-mark
  673. newline-mark ; use display table for newline
  674. ))
  675. ;; (setq whitespace-newline 'font-lock-comment-face)
  676. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  677. (defun my-whitesspace-mode-reload ()
  678. "Reload whitespace-mode config."
  679. (interactive)
  680. (when whitespace-mode
  681. (whitespace-mode 0)
  682. (whitespace-mode 1)))
  683. (set-variable 'whitespace-line-column nil)
  684. (global-whitespace-mode t)
  685. (add-hook 'dired-mode-hook
  686. (lambda ()
  687. (set (make-local-variable 'whitespace-style) nil)))
  688. (if (>= (display-color-cells)
  689. 256)
  690. (set-face-foreground 'whitespace-newline "color-109")
  691. ;; (progn
  692. ;; (set-face-bold-p 'whitespace-newline
  693. ;; t))
  694. ))
  695. (and nil
  696. '(safe-require-or-eval 'fill-column-indicator)
  697. (setq fill-column-indicator))
  698. (defun my-gen-hl-line-color-dark ()
  699. "Generate color for current line in black background."
  700. (let* ((candidates (mapcar 'number-to-string (number-sequence 1 6)))
  701. (limit (length candidates))
  702. (r 0) (g 0) (b 0))
  703. (while (and (<= (abs (- r g)) 1)
  704. (<= (abs (- g b)) 1)
  705. (<= (abs (- b r)) 1))
  706. (setq r (random limit))
  707. (setq g (random limit))
  708. (setq b (random limit)))
  709. (format "#%s%s%s"
  710. (nth r candidates)
  711. (nth g candidates)
  712. (nth b candidates)
  713. )))
  714. ;; (my-gen-hl-line-color-dark)
  715. ;; highlight current line
  716. ;; http://wiki.riywo.com/index.php?Meadow
  717. (face-spec-set 'hl-line
  718. `((((min-colors 256)
  719. (background dark))
  720. ;; Rotate midnightblue
  721. (:background ,(my-gen-hl-line-color-dark)))
  722. (((min-colors 256)
  723. (background light))
  724. ;; TODO: What is should be?
  725. (:background "color-234"))
  726. (t
  727. (:underline "black"))))
  728. (set-variable 'hl-line-global-modes
  729. '(not
  730. term-mode))
  731. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  732. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  733. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  734. ;;(safe-require-or-eval 'set-modeline-color)
  735. ;; (let ((fg (face-foreground 'default))
  736. ;; (bg (face-background 'default)))
  737. ;; (set-face-background 'mode-line-inactive
  738. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  739. ;; (set-face-foreground 'mode-line-inactive
  740. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  741. ;; (set-face-underline 'mode-line-inactive
  742. ;; t)
  743. ;; (set-face-underline 'vertical-border
  744. ;; nil)
  745. ;; (when (safe-require-or-eval 'end-mark)
  746. ;; (global-end-mark-mode))
  747. ;; M-x highlight-* to highlight things
  748. (global-hi-lock-mode 1)
  749. (unless (fboundp 'highlight-region-text)
  750. (defun highlight-region-text (beg end)
  751. "Highlight text between BEG and END."
  752. (interactive "r")
  753. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  754. end)))
  755. (setq deactivate-mark t)))
  756. (when (safe-require-or-eval 'auto-highlight-symbol)
  757. (set-variable 'ahs-idle-interval 0.6)
  758. (global-auto-highlight-symbol-mode 1))
  759. (when (safe-require-or-eval 'highlight-indentation)
  760. (set-face-background 'highlight-indentation-face "color-236")
  761. (dolist (hook
  762. '(
  763. prog-mode-hook
  764. text-mode-hook
  765. ))
  766. (add-hook hook
  767. 'highlight-indentation-mode)))
  768. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  769. (when (fboundp 'fic-mode)
  770. (add-hook 'prog-mode-hook
  771. 'fic-mode))
  772. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  773. ;; file handling
  774. (auto-insert-mode 1)
  775. ;; fzf
  776. ;; Too slow in term buffer!
  777. ;; (set-variable 'fzf/executable "sk")
  778. ;; (set-variable 'fzf/args "--color bw --print-query")
  779. ;; Modified from hardcoded default to include:
  780. ;; - directories
  781. ;; - hidden files
  782. ;; - root directory (.)
  783. ;; - parent directory (..)
  784. ;; ripgrep cannot list directories...
  785. ;; (setenv "FZF_DEFAULT_COMMAND" "rg --files --hidden --follow --glob '!.git/*' --no-ignore")
  786. (let* ((find (if (executable-find "bfs")
  787. ;; Breadth-first find https://github.com/tavianator/bfs
  788. "bfs"
  789. ;; Use gfind if available?
  790. "find"))
  791. (findcmd (concat "set -eu; set -o pipefail; "
  792. "echo .; "
  793. "echo ..; "
  794. "command " find " -L . "
  795. "-mindepth 1 "
  796. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  797. "-o -print "
  798. "2> /dev/null "
  799. "| "
  800. "cut -b3-"))
  801. (fdcmd (concat "set -eu; set -o pipefail; "
  802. "echo .; "
  803. "echo ..; "
  804. "command fd "
  805. "--follow --hidden --no-ignore "
  806. "--color always "
  807. "2>/dev/null")))
  808. (if (executable-find "fd")
  809. (setenv "FZF_DEFAULT_COMMAND" fdcmd)
  810. (setenv "FZF_DEFAULT_COMMAND" findcmd)))
  811. (set-variable 'fzf/window-height 45)
  812. (set-variable 'fzf/args "--print-query --ansi --color='bg+:-1' --inline-info --cycle")
  813. ;; (set-variable 'fzf/args "--print-query --ansi --inline-info --cycle")
  814. ;; (set-variable 'fzf/args "--print-query --ansi --color=bw --inline-info --cycle")
  815. (defun my-fzf-or-find-file ()
  816. "Call fzf if usable or call find-file."
  817. (declare (interactive-only t))
  818. (interactive)
  819. (if (and (executable-find "fzf")
  820. (fboundp 'fzf)
  821. (not (file-remote-p default-directory)))
  822. (fzf)
  823. (call-interactively 'find-file)))
  824. (define-key ctl-x-map "f" 'my-fzf-or-find-file)
  825. (defun my-fzf-all-lines ()
  826. "Fzf all lines."
  827. (interactive)
  828. (let ((process-environment (cl-copy-list process-environment)))
  829. (setenv "FZF_DEFAULT_COMMAND" "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^")
  830. (fzf)))
  831. (define-key ctl-x-map "S" 'my-fzf-all-lines)
  832. ;; recently
  833. (when (safe-require-or-eval 'recently)
  834. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  835. (set-variable 'recently-max 1000)
  836. (defvar recently-excludes)
  837. (add-to-list 'recently-excludes
  838. (rx-to-string (list 'and
  839. 'string-start
  840. (expand-file-name package-user-dir))
  841. t))
  842. (recently-mode 1))
  843. (defvar my-cousel-recently-history nil "History of `my-counsel-recently'.")
  844. (when (and (require 'recently nil t)
  845. (fboundp 'ivy-read))
  846. (defun my-counsel-recently ()
  847. "Consel `recently'."
  848. (interactive)
  849. (ivy-read "Recently: " (recently-list)
  850. :require-match t
  851. :history 'my-cousel-recently-history
  852. :preselect default-directory
  853. :action (lambda (x) (find-file x))
  854. :caller 'my-counsel-recently))
  855. (define-key ctl-x-map (kbd "C-r") 'my-counsel-recently)
  856. )
  857. (when (fboundp 'editorconfig-mode)
  858. (add-hook 'after-first-visit-hook
  859. 'editorconfig-mode))
  860. (set-variable 'editorconfig-get-properties-function
  861. 'editorconfig-core-get-properties-hash)
  862. (set-variable 'editorconfig-mode-lighter "")
  863. (when (fboundp 'ws-butler-mode)
  864. (set-variable 'editorconfig-trim-whitespaces-mode
  865. 'ws-butler-mode))
  866. (with-eval-after-load 'org-src
  867. ;; [*.org\[\*Org Src*\[ c \]*\]]
  868. (add-hook 'org-src-mode-hook
  869. 'editorconfig-mode-apply t))
  870. (when (fboundp 'editorconfig-custom-majormode)
  871. (add-hook 'editorconfig-after-apply-functions
  872. 'editorconfig-custom-majormode))
  873. ;; Add readonly=true to set read-only-mode
  874. (add-hook 'editorconfig-after-apply-functions
  875. (lambda (props)
  876. (let ((r (gethash 'readonly props)))
  877. (when (and (string= r "true")
  878. (not buffer-read-only))
  879. (read-only-mode 1)))))
  880. (add-hook 'editorconfig-hack-properties-functions
  881. '(lambda (props)
  882. (when (derived-mode-p 'makefile-mode)
  883. (puthash 'indent_style "tab" props))
  884. (when (derived-mode-p 'diff-mode)
  885. (puthash 'trim_trailing_whitespace "false" props)
  886. (puthash 'insert_final_newline "false" props)
  887. )
  888. ))
  889. (when (fboundp 'editorconfig-auto-apply-enable)
  890. (add-hook 'editorconfig-conf-mode-hook
  891. 'editorconfig-auto-apply-enable))
  892. ;; (when (fboundp 'editorconfig-charset-extras)
  893. ;; (add-hook 'editorconfig-custom-hooks
  894. ;; 'editorconfig-charset-extras))
  895. (setq revert-without-query '(".+"))
  896. ;; save cursor position
  897. (when (safe-require-or-eval 'saveplace)
  898. (setq-default save-place t)
  899. (setq save-place-file (concat user-emacs-directory
  900. "places")))
  901. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  902. (setq make-backup-files t)
  903. (setq vc-make-backup-files t)
  904. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  905. (setq backup-directory-alist
  906. (cons (cons "." (expand-file-name (concat user-emacs-directory
  907. "backup")))
  908. backup-directory-alist))
  909. (setq version-control 't)
  910. (setq delete-old-versions t)
  911. (setq kept-new-versions 20)
  912. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  913. "auto-save/")))
  914. ;; (setq delete-auto-save-files t)
  915. (setq auto-save-visited-interval 8)
  916. (auto-save-visited-mode 1)
  917. ;; (add-to-list 'auto-save-file-name-transforms
  918. ;; `(".*" ,(concat user-emacs-directory "auto-save-dir") t))
  919. ;; (setq auto-save-interval 3)
  920. ;; (auto-save-mode 1)
  921. (add-to-list 'completion-ignored-extensions ".bak")
  922. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  923. (setq delete-by-moving-to-trash t)
  924. ;; trash-directory "~/.emacs.d/trash")
  925. (add-hook 'after-save-hook
  926. 'executable-make-buffer-file-executable-if-script-p)
  927. (set-variable 'bookmark-default-file
  928. (expand-file-name (concat user-emacs-directory
  929. "bmk")))
  930. (set-variable 'bookmark-save-flag
  931. 1)
  932. (with-eval-after-load 'recentf
  933. (defvar recentf-exclude)
  934. (defvar bookmark-default-file)
  935. (add-to-list 'recentf-exclude
  936. (regexp-quote bookmark-default-file)))
  937. (when (safe-require-or-eval 'smart-revert)
  938. (smart-revert-on))
  939. ;; autosave
  940. ;; auto-save-visited-mode can be used instead?
  941. ;; (when (safe-require-or-eval 'autosave)
  942. ;; (autosave-set 8))
  943. ;; bookmarks
  944. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  945. ;; vc
  946. (set-variable 'vc-handled-backends '(RCS))
  947. (set-variable 'vc-rcs-register-switches "-l")
  948. (set-variable 'vc-rcs-checkin-switches "-l")
  949. (set-variable 'vc-command-messages t)
  950. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  951. ;; share clipboard with x
  952. ;; this page describes this in details, but only these sexps seem to be needed
  953. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  954. (and nil
  955. (not window-system)
  956. (not (eq window-system 'mac))
  957. (getenv "DISPLAY")
  958. (not (equal (getenv "DISPLAY") ""))
  959. (executable-find "xclip")
  960. ;; (< emacs-major-version 24)
  961. '(safe-require-or-eval 'xclip)
  962. nil
  963. (turn-on-xclip))
  964. (and (eq system-type 'darwin)
  965. (safe-require-or-eval 'pasteboard)
  966. (turn-on-pasteboard))
  967. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  968. ;; some modes and hooks
  969. ;; Include some extra modes
  970. (require 'generic-x)
  971. ;; Derived from https://github.com/ensime/ensime-emacs/issues/591#issuecomment-291916753
  972. (defun my-scalafmt ()
  973. (interactive)
  974. (cl-assert buffer-file-name)
  975. (cl-assert (not (buffer-modified-p)))
  976. (let* ((configdir (locate-dominating-file default-directory ".scalafmt.conf"))
  977. (configoption (if configdir
  978. (concat " --config "
  979. (shell-quote-argument (expand-file-name configdir))
  980. ".scalafmt.conf"
  981. )
  982. ""))
  983. (str (concat "scalafmt -f "
  984. (shell-quote-argument buffer-file-name)
  985. configoption
  986. " -i --exclude ensime")))
  987. (message str)
  988. (shell-command-to-string str))
  989. (message "scalafmt done")
  990. (revert-buffer nil t))
  991. (when (fboundp 'web-mode)
  992. (add-to-list 'auto-mode-alist
  993. '("\\.html\\.j2\\'" . web-mode))
  994. (add-to-list 'auto-mode-alist
  995. ;; Django Template Language
  996. '("\\.dtl\\'" . web-mode))
  997. )
  998. (when (autoload-eval-lazily 'wgrep)
  999. (set-variable 'wgrep-auto-save-buffer t)
  1000. (with-eval-after-load 'grep
  1001. (defvar grep-mode-map)
  1002. (define-key grep-mode-map
  1003. "e"
  1004. 'wgrep-change-to-wgrep-mode)))
  1005. (when (fboundp 'grep-context-mode)
  1006. (add-hook 'compilation-mode-hook #'grep-context-mode))
  1007. (with-eval-after-load 'remember
  1008. (defvar remember-mode-map)
  1009. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  1010. (set-variable 'remember-notes-initial-major-mode
  1011. 'change-log-mode)
  1012. (with-eval-after-load 'magit-files
  1013. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  1014. ;; existing keybindings.
  1015. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  1016. ;; to disable thie mode here, but do anyway.
  1017. ;; See also https://github.com/magit/magit/issues/3517
  1018. (global-magit-file-mode -1))
  1019. (with-eval-after-load 'magit-section
  1020. (set-face-background 'magit-section-highlight
  1021. nil))
  1022. ;; Sane colors
  1023. (with-eval-after-load 'magit-diff
  1024. (set-face-background 'magit-diff-context nil)
  1025. (set-face-background 'magit-diff-context-highlight nil)
  1026. (set-face-foreground 'magit-diff-hunk-heading nil)
  1027. (set-face-background 'magit-diff-hunk-heading nil)
  1028. (set-face-foreground 'magit-diff-hunk-heading-highlight nil)
  1029. (set-face-background 'magit-diff-hunk-heading-highlight nil)
  1030. ;; https://blog.shibayu36.org/entry/2016/03/27/220552
  1031. (set-face-foreground 'magit-diff-added "green")
  1032. (set-face-background 'magit-diff-added nil)
  1033. (set-face-foreground 'magit-diff-added-highlight "green")
  1034. (set-face-background 'magit-diff-added-highlight nil)
  1035. (set-face-foreground 'magit-diff-removed "red")
  1036. (set-face-background 'magit-diff-removed nil)
  1037. (set-face-foreground 'magit-diff-removed-highlight "red")
  1038. (set-face-background 'magit-diff-removed-highlight nil)
  1039. (set-face-background 'magit-diff-lines-boundary "blue")
  1040. )
  1041. (defun my-magit-messenger (file line)
  1042. "Magit messenger."
  1043. (interactive (list buffer-file-name
  1044. (line-number-at-pos)))
  1045. (cl-assert file)
  1046. (cl-assert line)
  1047. (let* ((blame-args '("-w"))
  1048. (id (with-temp-buffer
  1049. (let ((exit (apply 'call-process
  1050. "git" ;; PROGRAM
  1051. nil ;; INFILE
  1052. t ;; DESTINATION
  1053. nil ;; DISPLAY
  1054. "--no-pager" ;; ARGS
  1055. "blame"
  1056. "-L"
  1057. (format "%d,+1" line)
  1058. "--porcelain"
  1059. file
  1060. blame-args
  1061. )))
  1062. (goto-char (point-min))
  1063. (cl-assert (eq exit 0)
  1064. "Failed: %s" (buffer-substring (point)
  1065. (point-at-eol)))
  1066. (save-match-data
  1067. (re-search-forward (rx buffer-start
  1068. (one-or-more hex-digit)))
  1069. (match-string 0))))))
  1070. (magit-show-commit id)))
  1071. (when (boundp 'git-rebase-filename-regexp)
  1072. (add-to-list 'auto-mode-alist
  1073. `(,git-rebase-filename-regexp . text-mode)))
  1074. (when (safe-require-or-eval 'aggressive-indent)
  1075. (defvar aggressive-indent-excluded-modes)
  1076. (setq aggressive-indent-excluded-modes
  1077. `(web-mode
  1078. diff-mode
  1079. toml-mode
  1080. conf-mode
  1081. dockerfile-mode
  1082. groovy-mode
  1083. scala-mode
  1084. ,@aggressive-indent-excluded-modes))
  1085. (global-aggressive-indent-mode 1))
  1086. (when (fboundp 'ggtags-mode)
  1087. (add-hook 'c-mode-common-hook
  1088. 'ggtags-mode)
  1089. (add-hook 'python-mode-hook
  1090. 'ggtags-mode)
  1091. (add-hook 'js-mode-hook
  1092. 'ggtags-mode)
  1093. (add-hook 'scheme-mode-hook
  1094. 'ggtags-mode)
  1095. )
  1096. (when (fboundp 'imenu-list-minor-mode)
  1097. (defvar imenu-list-buffer-name)
  1098. (defun my-imenu-list-toggle ()
  1099. "My 'imenu-list` toggle."
  1100. (interactive)
  1101. (require 'imenu-list)
  1102. (if (eq (window-buffer)
  1103. (get-buffer imenu-list-buffer-name))
  1104. (imenu-list-minor-mode -1)
  1105. (imenu-list-minor-mode 1)))
  1106. ;; (set-variable 'imenu-list-auto-resize t)
  1107. (set-variable 'imenu-list-focus-after-activation t)
  1108. (define-key ctl-x-map (kbd "C-l") 'my-imenu-list-toggle))
  1109. (add-hook 'emacs-lisp-mode-hook
  1110. (lambda ()
  1111. (setq imenu-generic-expression
  1112. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  1113. ,@imenu-generic-expression))))
  1114. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  1115. (with-eval-after-load 'compile
  1116. (defvar compilation-filter-start)
  1117. (defvar compilation-error-regexp-alist)
  1118. (require 'ansi-color)
  1119. (add-hook 'compilation-filter-hook
  1120. (lambda ()
  1121. (let ((inhibit-read-only t))
  1122. (ansi-color-apply-on-region compilation-filter-start
  1123. (point)))))
  1124. (add-to-list 'compilation-error-regexp-alist
  1125. ;; ansible-lint
  1126. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2))
  1127. (add-to-list 'compilation-error-regexp-alist
  1128. ;; pydocstyle
  1129. '("^\\([^ \n]+\\):\\([0-9]+\\) " 1 2))
  1130. )
  1131. (when (safe-require-or-eval 'company)
  1132. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1133. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1134. (global-company-mode)
  1135. (set-variable 'company-idle-delay nil)
  1136. (set-variable 'company-minimum-prefix-length 2)
  1137. (set-variable 'company-selection-wrap-around t)
  1138. (defvar company-mode-map)
  1139. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1140. ;; (with-eval-after-load 'python
  1141. ;; (defvar python-indent-trigger-commands)
  1142. ;; ;; TODO: This disables completion in puthon?
  1143. ;; (add-to-list 'python-indent-trigger-commands
  1144. ;; 'company-indent-or-complete-common))
  1145. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1146. (defvar company-active-map)
  1147. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1148. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1149. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1150. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1151. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1152. (defvar company-mode)
  1153. (defvar company-candidates)
  1154. (defvar company-candidates-length)
  1155. ;; (popup-tip "Hello, World!")
  1156. (defun my-company-lighter-current-length ()
  1157. "Get current candidate length."
  1158. (interactive)
  1159. (let ((l nil)
  1160. (inhibit-message t))
  1161. (when (and company-mode
  1162. (not (minibufferp))
  1163. ;; Do nothing when already in company completion
  1164. (not company-candidates))
  1165. ;; FIXME: Somehow it cannto catch errors from ggtags
  1166. (ignore-errors
  1167. ;; (company-auto-begin)
  1168. (company-manual-begin)
  1169. (setq l company-candidates-length)
  1170. (company-cancel)))
  1171. (if l
  1172. (format "[%d]" l)
  1173. "")))
  1174. (defvar company-lighter)
  1175. (set-variable 'company-lighter-base "Cmp")
  1176. ;; (add-to-list 'company-lighter
  1177. ;; '(:eval (my-company-lighter-current-length))
  1178. ;; t)
  1179. ;; This breaks japanese text input
  1180. ;; (set-variable 'my-company-length-popup-tip-timer
  1181. ;; (run-with-idle-timer 0.2 t
  1182. ;; 'my-company-length-popup-tip))
  1183. ;; (current-active-maps)
  1184. ;; (lookup-key)
  1185. '(mapcar (lambda (map)
  1186. (lookup-key map (kbd "C-i")))
  1187. (current-active-maps))
  1188. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1189. (set-face-attribute 'company-tooltip nil
  1190. :foreground "black" :background "lightgrey")
  1191. (set-face-attribute 'company-tooltip-common nil
  1192. :foreground "black" :background "lightgrey")
  1193. (set-face-attribute 'company-tooltip-common-selection nil
  1194. :foreground "white" :background "steelblue")
  1195. (set-face-attribute 'company-tooltip-selection nil
  1196. :foreground "black" :background "steelblue")
  1197. (set-face-attribute 'company-preview-common nil
  1198. :background nil :foreground "lightgrey" :underline t)
  1199. (set-face-attribute 'company-scrollbar-fg nil
  1200. :background "orange")
  1201. (set-face-attribute 'company-scrollbar-bg nil
  1202. :background "gray40")
  1203. )
  1204. ;; https://github.com/lunaryorn/flycheck
  1205. ;; TODO: Any way to disable auto check?
  1206. ;; Update flycheck-hooks-alist?
  1207. (when (safe-require-or-eval 'flycheck)
  1208. (eval-after-init (global-flycheck-mode)))
  1209. ;; (set-variable 'flycheck-display-errors-delay 2.0)
  1210. ;; (fset 'flycheck-display-error-at-point-soon 'ignore)
  1211. ;; (with-eval-after-load 'flycheck
  1212. ;; (when (fboundp 'flycheck-black-check-setup)
  1213. ;; (flycheck-black-check-setup)))
  1214. (when (autoload-eval-lazily 'ilookup)
  1215. (define-key ctl-x-map "d" 'ilookup-open-word))
  1216. (set-variable 'ac-ignore-case nil)
  1217. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  1218. (define-key ctl-x-map "t" 'term-run-shell-command))
  1219. (add-to-list 'safe-local-variable-values
  1220. '(encoding utf-8))
  1221. (setq enable-local-variables :safe)
  1222. ;; Detect file type from shebang and set major-mode.
  1223. (add-to-list 'interpreter-mode-alist
  1224. '("python3" . python-mode))
  1225. (add-to-list 'interpreter-mode-alist
  1226. '("python2" . python-mode))
  1227. (with-eval-after-load 'python
  1228. (defvar python-mode-map (make-sparse-keymap))
  1229. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1230. ;; I want to use this, but this breaks normal self-insert-command
  1231. ;; (set-variable 'py-indent-list-style
  1232. ;; 'one-level-to-beginning-of-statement)
  1233. (set-variable 'pydoc-command
  1234. "python3 -m pydoc")
  1235. (with-eval-after-load 'pydoc
  1236. (when (require 'with-venv nil t)
  1237. (with-venv-advice-add 'pydoc)))
  1238. (set-variable 'flycheck-python-mypy-ini ".mypy.ini")
  1239. (set-variable 'flycheck-flake8rc "setup.cfg")
  1240. (set-variable 'flycheck-python-pylint-executable "python3")
  1241. (set-variable 'flycheck-python-pycompile-executable "python3")
  1242. (set-variable 'python-indent-guess-indent-offset nil)
  1243. (with-eval-after-load 'blacken
  1244. (when (require 'with-venv nil t)
  1245. (with-venv-advice-add 'blacken-buffer)))
  1246. (with-eval-after-load 'ansible-doc
  1247. (when (require 'with-venv nil t)
  1248. (with-venv-advice-add 'ansible-doc)))
  1249. ;; `isortify-buffer' breaks buffer when it contains japanese text
  1250. (defun my-isortify ()
  1251. (interactive)
  1252. (cl-assert buffer-file-name)
  1253. (cl-assert (not (buffer-modified-p)))
  1254. (call-process "python" ;; PROGRAM
  1255. nil ;; INFILE
  1256. nil ;; DESTINATION
  1257. nil ;; DISPLAY
  1258. "-m" "isort" buffer-file-name)
  1259. (message "isortify done")
  1260. (revert-buffer nil t))
  1261. (when (fboundp 'with-venv-advice-add)
  1262. ;; TODO: Lazy load with-venv
  1263. (with-venv-advice-add 'my-isortify))
  1264. ;; https://github.com/lunaryorn/old-emacs-configuration/blob/master/lisp/flycheck-virtualenv.el
  1265. (defun my-set-venv-flycheck-executable-find ()
  1266. "Set flycheck executabie find."
  1267. (interactive)
  1268. (set-variable 'flycheck-executable-find
  1269. '(lambda (e)
  1270. (with-venv
  1271. (executable-find e)))
  1272. t))
  1273. (defun my-update-flycheck-flake8-error-level-alist ()
  1274. "Update `flycheck-flake8-error-level-alist'."
  1275. (defvar flycheck-flake8-error-level-alist)
  1276. ;; (add-to-list 'flycheck-flake8-error-level-alist
  1277. ;; '("^D.*$" . warning))
  1278. (set-variable 'flycheck-flake8-error-level-alist
  1279. nil)
  1280. )
  1281. (add-hook 'python-mode-hook
  1282. 'my-set-venv-flycheck-executable-find)
  1283. (add-hook 'python-mode-hook
  1284. 'my-update-flycheck-flake8-error-level-alist)
  1285. (when (fboundp 'with-venv-info-mode)
  1286. (add-hook 'python-mode-hook
  1287. 'with-venv-info-mode))
  1288. ;; Run multiple chekcers
  1289. ;; https://github.com/flycheck/flycheck/issues/186
  1290. ;; http://fukuyama.co/foreign-regexp
  1291. '(and (safe-require-or-eval 'foreign-regexp)
  1292. (progn
  1293. (setq foreign-regexp/regexp-type 'perl)
  1294. '(setq reb-re-syntax 'foreign-regexp)
  1295. ))
  1296. (autoload-eval-lazily 'sql '(sql-mode)
  1297. (require 'sql-indent nil t))
  1298. (add-to-list 'auto-mode-alist
  1299. '("\\.hql\\'" . sql-mode))
  1300. (when (autoload-eval-lazily 'git-command)
  1301. (define-key ctl-x-map "g" 'git-command))
  1302. ;; This keybind cause unexpected call really many many times
  1303. ;; (when (autoload-eval-lazily 'gited)
  1304. ;; (define-key ctl-x-map (kbd "C-g") 'gited-list))
  1305. (defalias 'gited 'gited-list)
  1306. (when (safe-require-or-eval 'git-commit)
  1307. (global-git-commit-mode 1))
  1308. (with-eval-after-load 'git-commit
  1309. (add-hook 'git-commit-setup-hook
  1310. 'turn-off-auto-fill t))
  1311. (autoload-eval-lazily 'sl)
  1312. (with-eval-after-load 'rst
  1313. (defvar rst-mode-map)
  1314. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1315. (with-eval-after-load 'jdee
  1316. (add-hook 'jdee-mode-hook
  1317. (lambda ()
  1318. (make-local-variable 'global-mode-string)
  1319. (add-to-list 'global-mode-string
  1320. mode-line-position))))
  1321. ;; Cannot enable error thrown. Why???
  1322. ;; https://github.com/m0smith/malabar-mode#Installation
  1323. ;; (when (autoload-eval-lazily 'malabar-mode)
  1324. ;; (add-to-list 'load-path
  1325. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1326. ;; (safe-require-or-eval 'cedet-devel-load)
  1327. ;; (eval-after-init (activate-malabar-mode)))
  1328. (with-eval-after-load 'make-mode
  1329. (defvar makefile-mode-map (make-sparse-keymap))
  1330. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1331. ;; this functions is set in write-file-functions, i cannot find any
  1332. ;; good way to remove this.
  1333. (fset 'makefile-warn-suspicious-lines 'ignore))
  1334. (with-eval-after-load 'verilog-mode
  1335. (defvar verilog-mode-map (make-sparse-keymap))
  1336. (define-key verilog-mode-map ";" 'self-insert-command))
  1337. (setq diff-switches "-u")
  1338. (autoload 'diff-goto-source "diff-mode")
  1339. (with-eval-after-load 'diff-mode
  1340. ;; (when (and (eq major-mode
  1341. ;; 'diff-mode)
  1342. ;; (not buffer-file-name))
  1343. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1344. ;; (view-mode 1))
  1345. (set-face-attribute 'diff-header nil
  1346. :foreground nil
  1347. :background nil
  1348. :weight 'bold)
  1349. (set-face-attribute 'diff-file-header nil
  1350. :foreground nil
  1351. :background nil
  1352. :weight 'bold)
  1353. (set-face-foreground 'diff-index "blue")
  1354. (set-face-attribute 'diff-hunk-header nil
  1355. :foreground "cyan"
  1356. :weight 'normal)
  1357. (set-face-attribute 'diff-context nil
  1358. ;; :foreground "white"
  1359. :foreground nil
  1360. :weight 'normal)
  1361. (set-face-foreground 'diff-removed "red")
  1362. (set-face-foreground 'diff-added "green")
  1363. (set-face-background 'diff-removed nil)
  1364. (set-face-background 'diff-added nil)
  1365. (set-face-attribute 'diff-changed nil
  1366. :foreground "magenta"
  1367. :weight 'normal)
  1368. (set-face-attribute 'diff-refine-changed nil
  1369. :foreground nil
  1370. :background nil
  1371. :weight 'bold
  1372. :inverse-video t)
  1373. ;; Annoying !
  1374. ;;(diff-auto-refine-mode)
  1375. )
  1376. ;; (ffap-bindings)
  1377. (set-variable 'browse-url-browser-function
  1378. 'eww-browse-url)
  1379. (set-variable 'sh-here-document-word "__EOC__")
  1380. (when (autoload-eval-lazily 'adoc-mode
  1381. nil
  1382. (defvar adoc-mode-map (make-sparse-keymap))
  1383. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1384. (setq auto-mode-alist
  1385. `(("\\.adoc\\'" . adoc-mode)
  1386. ("\\.asciidoc\\'" . adoc-mode)
  1387. ,@auto-mode-alist)))
  1388. (with-eval-after-load 'markup-faces
  1389. ;; Is this too match ?
  1390. (set-face-foreground 'markup-meta-face
  1391. "color-245")
  1392. (set-face-foreground 'markup-meta-hide-face
  1393. "color-245")
  1394. )
  1395. ;; TODO: check if this is required
  1396. (when (autoload-eval-lazily 'groovy-mode nil
  1397. (defvar groovy-mode-map (make-sparse-keymap))
  1398. (define-key groovy-mode-map "(" 'self-insert-command)
  1399. (define-key groovy-mode-map ")" 'self-insert-command)
  1400. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1401. )
  1402. (add-to-list 'auto-mode-alist
  1403. '("build\\.gradle\\'" . groovy-mode)))
  1404. (add-to-list 'auto-mode-alist
  1405. '("\\.gawk\\'" . awk-mode))
  1406. (with-eval-after-load 'yaml-mode
  1407. (defvar yaml-mode-map (make-sparse-keymap))
  1408. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1409. (with-eval-after-load 'html-mode
  1410. (defvar html-mode-map (make-sparse-keymap))
  1411. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1412. (with-eval-after-load 'text-mode
  1413. (define-key text-mode-map (kbd "C-m") 'newline))
  1414. (autoload-eval-lazily 'info nil
  1415. (defvar Info-additional-directory-list)
  1416. (dolist (dir (directory-files (concat user-emacs-directory
  1417. "info")
  1418. t
  1419. "^[^.].*"))
  1420. (when (file-directory-p dir)
  1421. (add-to-list 'Info-additional-directory-list
  1422. dir)))
  1423. (let ((dir (expand-file-name "~/.brew/share/info")))
  1424. (when (file-directory-p dir)
  1425. (add-to-list 'Info-additional-directory-list
  1426. dir))))
  1427. (with-eval-after-load 'apropos
  1428. (defvar apropos-mode-map (make-sparse-keymap))
  1429. (define-key apropos-mode-map "n" 'next-line)
  1430. (define-key apropos-mode-map "p" 'previous-line))
  1431. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1432. ;; (define-key isearch-mode-map
  1433. ;; (kbd "C-j") 'isearch-other-control-char)
  1434. ;; (define-key isearch-mode-map
  1435. ;; (kbd "C-k") 'isearch-other-control-char)
  1436. ;; (define-key isearch-mode-map
  1437. ;; (kbd "C-h") 'isearch-other-control-char)
  1438. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1439. (define-key isearch-mode-map (kbd "M-r")
  1440. 'isearch-query-replace-regexp)
  1441. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1442. (setq lazy-highlight-cleanup nil)
  1443. ;; face for isearch highlighing
  1444. (set-face-attribute 'lazy-highlight
  1445. nil
  1446. :foreground `unspecified
  1447. :background `unspecified
  1448. :underline t
  1449. ;; :weight `bold
  1450. )
  1451. (add-hook 'outline-mode-hook
  1452. (lambda ()
  1453. (when (string-match "\\.md\\'" buffer-file-name)
  1454. (set (make-local-variable 'outline-regexp) "#+ "))))
  1455. (add-hook 'outline-mode-hook
  1456. 'outline-show-all)
  1457. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1458. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1459. (when (autoload-eval-lazily 'markdown-mode
  1460. '(markdown-mode gfm-mode)
  1461. (defvar gfm-mode-map (make-sparse-keymap))
  1462. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1463. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1464. )
  1465. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1466. (set-variable 'markdown-command (or (executable-find "markdown")
  1467. (executable-find "markdown.pl")
  1468. ""))
  1469. (add-hook 'markdown-mode-hook
  1470. (lambda ()
  1471. (outline-minor-mode 1)
  1472. (flyspell-mode)
  1473. (set (make-local-variable 'comment-start) ";")))
  1474. )
  1475. ;; c-mode
  1476. ;; http://www.emacswiki.org/emacs/IndentingC
  1477. ;; http://en.wikipedia.org/wiki/Indent_style
  1478. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1479. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1480. (with-eval-after-load 'cc-vars
  1481. (defvar c-default-style nil)
  1482. (add-to-list 'c-default-style
  1483. '(c-mode . "k&r"))
  1484. (add-to-list 'c-default-style
  1485. '(c++-mode . "k&r")))
  1486. (autoload-eval-lazily 'js2-mode nil
  1487. ;; currently do not use js2-mode
  1488. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1489. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1490. ;; (defvar js2-mode-map (make-sparse-keymap))
  1491. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1492. ;; (interactive)
  1493. ;; (js2-enter-key)
  1494. ;; (indent-for-tab-command)))
  1495. ;; (add-hook (kill-local-variable 'before-save-hook)
  1496. ;; 'js2-before-save)
  1497. ;; (add-hook 'before-save-hook
  1498. ;; 'my-indent-buffer
  1499. ;; nil
  1500. ;; t)
  1501. )
  1502. (add-to-list 'interpreter-mode-alist
  1503. '("node" . js-mode))
  1504. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1505. (with-eval-after-load 'uniquify
  1506. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1507. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1508. (setq uniquify-min-dir-content 1))
  1509. (with-eval-after-load 'view
  1510. (defvar view-mode-map (make-sparse-keymap))
  1511. (define-key view-mode-map "j" 'scroll-up-line)
  1512. (define-key view-mode-map "k" 'scroll-down-line)
  1513. (define-key view-mode-map "v" 'toggle-read-only)
  1514. (define-key view-mode-map "q" 'bury-buffer)
  1515. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1516. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1517. ;; (define-key view-mode-map
  1518. ;; "n" 'nonincremental-repeat-search-forward)
  1519. ;; (define-key view-mode-map
  1520. ;; "N" 'nonincremental-repeat-search-backward)
  1521. ;; N conflicts with git-walktree
  1522. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1523. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1524. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1525. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1526. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1527. (global-set-key "\M-r" 'view-mode)
  1528. ;; (setq view-read-only t)
  1529. (with-eval-after-load 'term
  1530. (defvar term-raw-map (make-sparse-keymap))
  1531. (define-key term-raw-map (kbd "C-x")
  1532. (lookup-key (current-global-map)
  1533. (kbd "C-x"))))
  1534. (add-hook 'term-mode-hook
  1535. (lambda ()
  1536. ;; Stop current line highlighting
  1537. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1538. (set-variable 'scroll-margin 0 t)
  1539. ))
  1540. (set-variable 'term-buffer-maximum-size 20480)
  1541. (set-variable 'term-suppress-hard-newline t)
  1542. (add-hook 'Man-mode-hook
  1543. (lambda ()
  1544. (view-mode 1)
  1545. (setq truncate-lines nil)))
  1546. (set-variable 'Man-notify-method (if window-system
  1547. 'newframe
  1548. 'aggressive))
  1549. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1550. "woman_cache.el")))
  1551. ;; not work because man.el will be loaded when man called
  1552. (defalias 'man 'woman)
  1553. (add-to-list 'auto-mode-alist
  1554. '("tox\\.ini\\'" . conf-unix-mode))
  1555. (when (autoload-eval-lazily 'toml-mode)
  1556. (add-to-list 'auto-mode-alist
  1557. '("/tox\\.ini\\'" . toml-mode))
  1558. (add-to-list 'auto-mode-alist
  1559. '("/Pipfile\\'" . toml-mode))
  1560. (add-to-list 'auto-mode-alist
  1561. '("/poetry\\.lock\\'" . toml-mode))
  1562. )
  1563. (when (autoload-eval-lazily 'json-mode)
  1564. (add-to-list 'auto-mode-alist
  1565. '("/Pipfile\\.lock\\'" . json-mode)))
  1566. (add-hook 'go-mode-hook
  1567. (lambda()
  1568. (defvar go-mode-map)
  1569. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1570. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1571. (when (autoload-eval-lazily 'k8s-mode)
  1572. (add-to-list 'auto-mode-alist
  1573. '("\\.k8s\\'" . k8s-mode)))
  1574. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1575. ;; buffers
  1576. (defvar bs-configurations)
  1577. (declare-function bs-set-configuration "bs")
  1578. (declare-function bs-refresh "bs")
  1579. (declare-function bs-message-without-log "bs")
  1580. (declare-function bs--current-config-message "bs")
  1581. (when (autoload-eval-lazily 'bs '(bs-show)
  1582. (add-to-list 'bs-configurations
  1583. '("specials" "^\\*" nil ".*" nil nil))
  1584. (add-to-list 'bs-configurations
  1585. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1586. (defvar bs-mode-map)
  1587. (defvar bs-current-configuration)
  1588. (define-key bs-mode-map (kbd "t")
  1589. ;; TODO: fix toggle feature
  1590. (lambda ()
  1591. (interactive)
  1592. (if (string= "specials"
  1593. bs-current-configuration)
  1594. (bs-set-configuration "files")
  1595. (bs-set-configuration "specials"))
  1596. (bs-refresh)
  1597. (bs-message-without-log "%s"
  1598. (bs--current-config-message))))
  1599. ;; (setq bs-configurations (list
  1600. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1601. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1602. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1603. )
  1604. (defalias 'list-buffers 'bs-show)
  1605. (set-variable 'bs-default-configuration "files-and-specials")
  1606. (set-variable 'bs-default-sort-name "by nothing")
  1607. (add-hook 'bs-mode-hook
  1608. (lambda ()
  1609. (set (make-local-variable 'scroll-margin) 0))))
  1610. ;;(iswitchb-mode 1)
  1611. (icomplete-mode)
  1612. (defun iswitchb-buffer-display-other-window ()
  1613. "Do iswitchb in other window."
  1614. (interactive)
  1615. (let ((iswitchb-default-method 'display))
  1616. (call-interactively 'iswitchb-buffer)))
  1617. ;; buffer killing
  1618. ;; (defun my-delete-window-killing-buffer () nil)
  1619. (defun my-query-kill-current-buffer ()
  1620. "Interactively kill current buffer."
  1621. (interactive)
  1622. (if (y-or-n-p (concat "kill current buffer? :"))
  1623. (kill-buffer (current-buffer))))
  1624. (defun my-force-query-kill-current-buffer ()
  1625. "Interactively kill current buffer."
  1626. (interactive)
  1627. (when (y-or-n-p (concat "kill current buffer? :"))
  1628. (let ((kill-buffer-hook nil)
  1629. (kill-buffer-query-functions nil))
  1630. (kill-buffer (current-buffer)))))
  1631. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1632. ;; Originally C-x C-k -> kmacro-keymap
  1633. ;; (global-set-key "\C-x\C-k" 'kmacro-keymap)
  1634. (global-set-key (kbd "C-x C-k") 'my-query-kill-current-buffer)
  1635. (substitute-key-definition 'kill-buffer
  1636. 'my-query-kill-current-buffer
  1637. global-map)
  1638. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1639. ;; dired
  1640. (defun my-file-head (filename &optional n)
  1641. "Return list of first N lines of file FILENAME."
  1642. ;; TODO: Fix for janapese text
  1643. ;; TODO: Fix for short text
  1644. (let ((num (or n 10))
  1645. (size 100)
  1646. (beg 0)
  1647. (end 0)
  1648. (result '())
  1649. (read -1))
  1650. (with-temp-buffer
  1651. (erase-buffer)
  1652. (while (or (<= (count-lines (point-min)
  1653. (point-max))
  1654. num)
  1655. (eq read 0))
  1656. (setq end (+ beg size))
  1657. (setq read (nth 1 (insert-file-contents-literally filename
  1658. nil
  1659. beg
  1660. end)))
  1661. (goto-char (point-max))
  1662. (setq beg (+ beg size)))
  1663. (goto-char (point-min))
  1664. (while (< (length result) num)
  1665. (let ((start (point)))
  1666. (forward-line 1)
  1667. (setq result
  1668. `(,@result ,(buffer-substring-no-properties start
  1669. (point))))))
  1670. result
  1671. ;; (buffer-substring-no-properties (point-min)
  1672. ;; (progn
  1673. ;; (forward-line num)
  1674. ;; (point)))
  1675. )))
  1676. ;; (apply 'concat (my-file-head "./shrc" 10)
  1677. (defun my-dired-echo-file-head (arg)
  1678. "Echo head of current file.
  1679. ARG is num to show, or defaults to 7."
  1680. (interactive "P")
  1681. (let ((f (dired-get-filename)))
  1682. (message "%s"
  1683. (apply 'concat
  1684. (my-file-head f
  1685. 7)))))
  1686. (defun my-dired-diff ()
  1687. "Show diff of marked file and file of current line."
  1688. (interactive)
  1689. (let ((files (dired-get-marked-files nil nil nil t)))
  1690. (if (eq (car files)
  1691. t)
  1692. (diff (cadr files) (dired-get-filename))
  1693. (message "One file must be marked!"))))
  1694. (defun dired-get-file-info ()
  1695. "Print information of current line file."
  1696. (interactive)
  1697. (let* ((file (dired-get-filename t))
  1698. (quoted (shell-quote-argument file)))
  1699. (if (file-directory-p file)
  1700. (progn
  1701. (message "Calculating disk usage...")
  1702. (let ((du (or (executable-find "gdu")
  1703. (executable-find "du")
  1704. (error "du not found"))))
  1705. (shell-command (concat du
  1706. " -hsD "
  1707. quoted))))
  1708. (shell-command (concat "file "
  1709. quoted)))))
  1710. (defun my-dired-scroll-up ()
  1711. "Scroll up."
  1712. (interactive)
  1713. (my-dired-previous-line (- (window-height) 1)))
  1714. (defun my-dired-scroll-down ()
  1715. "Scroll down."
  1716. (interactive)
  1717. (my-dired-next-line (- (window-height) 1)))
  1718. ;; (defun my-dired-forward-line (arg)
  1719. ;; ""
  1720. ;; (interactive "p"))
  1721. (defun my-dired-previous-line (arg)
  1722. "Move ARG lines up."
  1723. (interactive "p")
  1724. (if (> arg 0)
  1725. (progn
  1726. (if (eq (line-number-at-pos)
  1727. 1)
  1728. (goto-char (point-max))
  1729. (forward-line -1))
  1730. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1731. (dired-get-subdir))
  1732. (- arg 1)
  1733. arg)))
  1734. (dired-move-to-filename)))
  1735. (defun my-dired-next-line (arg)
  1736. "Move ARG lines down."
  1737. (interactive "p")
  1738. (if (> arg 0)
  1739. (progn
  1740. (if (eq (point)
  1741. (point-max))
  1742. (goto-char (point-min))
  1743. (forward-line 1))
  1744. (my-dired-next-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-tramp-remote-find-file (f)
  1750. "Open F."
  1751. (interactive (list (read-file-name "My Find File Tramp: "
  1752. "/scp:"
  1753. nil ;; "/scp:"
  1754. (confirm-nonexistent-file-or-buffer))))
  1755. (find-file f))
  1756. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1757. (if (eq window-system 'mac)
  1758. (setq dired-listing-switches "-lhF")
  1759. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1760. )
  1761. (setq dired-listing-switches "-lhF")
  1762. ;; when using dired-find-alternate-file
  1763. ;; reuse current dired buffer for the file to open
  1764. ;; (put 'dired-find-alternate-file 'disabled nil)
  1765. (set-variable 'dired-ls-F-marks-symlinks t)
  1766. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1767. (set-variable 'ls-lisp-dirs-first t)
  1768. (set-variable 'ls-lisp-use-localized-time-format t)
  1769. (set-variable 'ls-lisp-format-time-list
  1770. '("%Y-%m-%d %H:%M"
  1771. "%Y-%m-%d "))
  1772. (set-variable 'dired-dwim-target t)
  1773. (set-variable 'dired-isearch-filenames t)
  1774. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1775. (set-variable 'dired-hide-details-hide-information-lines nil)
  1776. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1777. (set-variable 'dired-recursive-deletes 'always)
  1778. ;; (add-hook 'dired-after-readin-hook
  1779. ;; 'my-replace-nasi-none)
  1780. (with-eval-after-load 'dired
  1781. (safe-require-or-eval 'ls-lisp)
  1782. (defvar dired-mode-map (make-sparse-keymap))
  1783. ;; dired-do-chgrp sometimes cause system hung
  1784. (define-key dired-mode-map "G" 'ignore)
  1785. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1786. (define-key dired-mode-map "i" 'dired-get-file-info)
  1787. ;; (define-key dired-mode-map "f" 'find-file)
  1788. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1789. (define-key dired-mode-map "z" 'fzf)
  1790. (define-key dired-mode-map "!" 'shell-command)
  1791. (define-key dired-mode-map "&" 'async-shell-command)
  1792. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1793. (define-key dired-mode-map "=" 'my-dired-diff)
  1794. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1795. (define-key dired-mode-map "b" 'gtkbm)
  1796. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1797. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1798. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1799. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1800. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1801. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1802. (substitute-key-definition 'dired-next-line
  1803. 'my-dired-next-line
  1804. dired-mode-map)
  1805. (substitute-key-definition 'dired-previous-line
  1806. 'my-dired-previous-line
  1807. dired-mode-map)
  1808. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1809. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1810. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1811. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1812. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1813. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1814. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1815. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1816. (add-hook 'dired-mode-hook
  1817. (lambda ()
  1818. (when (fboundp 'dired-hide-details-mode)
  1819. (dired-hide-details-mode t)
  1820. (local-set-key "l" 'dired-hide-details-mode))
  1821. (let ((file "._Icon\015"))
  1822. (when nil
  1823. '(file-readable-p file)
  1824. (delete-file file)))))
  1825. (when (fboundp 'pack-dired-dwim)
  1826. (with-eval-after-load 'dired
  1827. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1828. (when (autoload-eval-lazily 'dired-list-all-mode)
  1829. (setq dired-listing-switches "-lhF")
  1830. (with-eval-after-load 'dired
  1831. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1832. (when (autoload-eval-lazily 'dired-filter)
  1833. (add-hook 'dired-mode-hook
  1834. 'dired-filter-mode))
  1835. (set-variable 'dired-filter-stack nil)
  1836. ;; Currently disabled in favor of dired-from-git-ls-files
  1837. ;; (define-key ctl-x-map "f" 'find-dired)
  1838. (defvar my-dired-git-ls-files-history
  1839. "History for `my-dired-git-ls-files'." nil)
  1840. (defun my-dired-git-ls-files (arg)
  1841. "Dired from git ls-files."
  1842. (interactive (list
  1843. (read-shell-command "git ls-files: "
  1844. "git ls-files -z ")))
  1845. (pop-to-buffer-same-window
  1846. (dired-noselect `(,default-directory
  1847. ,@(split-string (shell-command-to-string arg)
  1848. "\0" t))
  1849. ""))
  1850. )
  1851. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1852. (with-eval-after-load 'dired
  1853. (defvar dired-mode-map (make-sparse-keymap))
  1854. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1855. (with-eval-after-load 'pack
  1856. (set-variable 'pack-silence
  1857. t)
  1858. (defvar pack-program-alist)
  1859. (add-to-list 'pack-program-alist
  1860. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf"))
  1861. (when (executable-find "aunpack")
  1862. (add-to-list 'pack-program-alist
  1863. ' ("\\.zip\\'"
  1864. :pack ("zip" "-r" archive sources)
  1865. :pack-append ("zip" "-r" archive sources)
  1866. :unpack ("aunpack" archive))))
  1867. )
  1868. ;; (define-minor-mode my-dired-glob-filter)
  1869. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1870. ;; misc funcs
  1871. (define-key ctl-x-map "T" 'git-worktree)
  1872. (define-key ctl-x-map "W" 'git-walktree)
  1873. (when (fboundp 'browse-url-default-macosx-browser)
  1874. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1875. (defalias 'qcalc 'quick-calc)
  1876. (defun memo (&optional dir)
  1877. "Open memo.txt in DIR."
  1878. (interactive)
  1879. (pop-to-buffer (find-file-noselect (concat (if dir
  1880. (file-name-as-directory dir)
  1881. "")
  1882. "memo.txt"))))
  1883. ;; TODO: remember-projectile
  1884. (set (defvar my-privnotes-path nil
  1885. "My privnotes repository path.")
  1886. (expand-file-name "~/my/privnotes"))
  1887. (defun my-privnotes-readme (dir)
  1888. "Open my privnotes DIR."
  1889. (interactive (list
  1890. (read-file-name "Privnotes: "
  1891. (expand-file-name (format-time-string "%Y%m%d_")
  1892. my-privnotes-path))))
  1893. (let ((path (expand-file-name "README.md" dir)))
  1894. (with-current-buffer (find-file path)
  1895. (unless (file-exists-p path)
  1896. (insert (file-name-base dir)
  1897. "\n"
  1898. "=======\n"
  1899. "\n\n")))))
  1900. (define-key ctl-x-map "p" 'my-privnotes-readme)
  1901. (set (defvar my-rgrep-alist nil
  1902. "Alist of rgrep command.
  1903. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1904. condition to choose COMMAND when evaluated.")
  1905. `(
  1906. ;; ripgrep
  1907. ("rg"
  1908. (executable-find "rg")
  1909. "rg -nH --no-heading --hidden --glob '!.git/' --smart-case -M 1280 ")
  1910. ;; git grep
  1911. ("gitgrep"
  1912. (eq 0
  1913. (shell-command "git rev-parse --git-dir"))
  1914. "git --no-pager grep -nH -e ")
  1915. ;; sift
  1916. ("sift"
  1917. (executable-find "sift")
  1918. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1919. ;; the silver searcher
  1920. ("ag"
  1921. (executable-find "ag")
  1922. "ag --nogroup --nopager --filename ")
  1923. ;; ack
  1924. ("ack"
  1925. (executable-find "ack")
  1926. "ack --nogroup --nopager --with-filename ")
  1927. ;; gnu global
  1928. ("global"
  1929. (and (require 'ggtags nil t)
  1930. (executable-find "global")
  1931. (ggtags-current-project-root))
  1932. "global --result grep ")
  1933. ;; grep
  1934. ("grep"
  1935. t
  1936. ,(concat "find . "
  1937. "-path '*/.git' -prune -o "
  1938. "-path '*/.svn' -prune -o "
  1939. "-type f -print0 | "
  1940. "xargs -0 grep -nH -e "))
  1941. )
  1942. )
  1943. (defvar my-rgrep-default nil
  1944. "Default command name for my-rgrep.")
  1945. (defun my-rgrep-grep-command (&optional name alist)
  1946. "Return recursive grep command for current directory or nil.
  1947. If NAME is given, use that without testing.
  1948. Commands are searched from ALIST."
  1949. (if alist
  1950. (if name
  1951. ;; if name is given search that from alist and return the command
  1952. (nth 2 (assoc name
  1953. alist))
  1954. ;; if name is not given try test in 1th elem
  1955. (let ((car (car alist))
  1956. (cdr (cdr alist)))
  1957. (if (eval (nth 1 car))
  1958. ;; if the condition is true return the command
  1959. (nth 2 car)
  1960. ;; try next one
  1961. (and cdr
  1962. (my-rgrep-grep-command name cdr)))))
  1963. ;; if alist is not given set default value
  1964. (my-rgrep-grep-command name my-rgrep-alist)))
  1965. (defun my-rgrep (command-args)
  1966. "My recursive grep. Run COMMAND-ARGS.
  1967. If prefix argument is given, use current symbol as default search target
  1968. and search from projectile root (if projectile is available)."
  1969. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1970. nil)))
  1971. (if cmd
  1972. (list (read-shell-command "grep command: "
  1973. (concat cmd
  1974. (if current-prefix-arg
  1975. (thing-at-point 'symbol t)
  1976. ""))
  1977. 'grep-find-history))
  1978. (error "My-Rgrep: Command for rgrep not found")
  1979. )))
  1980. (if (and current-prefix-arg
  1981. (safe-require-or-eval 'projectile)
  1982. (projectile-project-p))
  1983. (projectile-with-default-dir (projectile-project-root)
  1984. (compilation-start command-args
  1985. 'grep-mode))
  1986. (compilation-start command-args
  1987. 'grep-mode)))
  1988. (defun my-rgrep-thing-at-point-projectile-root ()
  1989. "My recursive grep to find thing at point from project root."
  1990. (interactive)
  1991. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1992. nil))
  1993. (command-args
  1994. (if cmd
  1995. (concat cmd
  1996. (or (thing-at-point 'symbol t)
  1997. (error "No symbol at point")))
  1998. (error "My-Rgrep: Command for rgrep not found"))))
  1999. (if (safe-require-or-eval 'projectile)
  2000. (projectile-with-default-dir (or (projectile-project-root)
  2001. default-directory)
  2002. (compilation-start command-args
  2003. 'grep-mode))
  2004. (compilation-start command-args
  2005. 'grep-mode))))
  2006. (defmacro define-my-rgrep (name)
  2007. "Define rgrep for NAME."
  2008. `(defun ,(intern (concat "my-rgrep-"
  2009. name)) ()
  2010. ,(format "My recursive grep by %s."
  2011. name)
  2012. (interactive)
  2013. (let ((my-rgrep-default ,name))
  2014. (if (called-interactively-p 'any)
  2015. (call-interactively 'my-rgrep)
  2016. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2017. )
  2018. (define-my-rgrep "ack")
  2019. (define-my-rgrep "ag")
  2020. (define-my-rgrep "rg")
  2021. (define-my-rgrep "sift")
  2022. (define-my-rgrep "gitgrep")
  2023. (define-my-rgrep "grep")
  2024. (define-my-rgrep "global")
  2025. (define-key ctl-x-map "s" 'my-rgrep)
  2026. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  2027. (defun my-occur (regexp &optional region)
  2028. "My occur command to search REGEXP."
  2029. (interactive (list (read-string "List lines matching regexp: "
  2030. (thing-at-point 'symbol t))))
  2031. (occur regexp nil region))
  2032. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  2033. (set-variable 'dumb-jump-prefer-searcher 'rg)
  2034. (defalias 'make 'compile)
  2035. (define-key ctl-x-map "c" 'compile)
  2036. (defun my-pushbullet-note (text &optional title)
  2037. "Push TEXT."
  2038. (interactive "sText to Push: ")
  2039. (pb/push-item '("") text "note" (or title "")))
  2040. ;;;;;;;;;;;;;;;;;;;;;
  2041. ;; git-bug
  2042. (defconst git-bug-ls-regexp
  2043. (eval-when-compile
  2044. (rx bol
  2045. (submatch (one-or-more alphanumeric)) ; id
  2046. ;; (one-or-more any)
  2047. (one-or-more space)
  2048. (submatch (or "open" "close")) ; status
  2049. (one-or-more space)
  2050. (submatch (maximal-match (zero-or-more print))) ; title
  2051. "\t"
  2052. (submatch (one-or-more alphanumeric)) ; user
  2053. (one-or-more space)
  2054. "C:"
  2055. (submatch (one-or-more digit)) ; Comment num
  2056. (one-or-more space)
  2057. "L:"
  2058. (submatch (one-or-more digit)) ; Label num
  2059. eol
  2060. ))
  2061. "Regexp to parse line of output of git-bug ls.
  2062. Used by `git-bug-ls'.")
  2063. (defun git-bug-bugs ()
  2064. "Get list of git-bug bugs."
  2065. (with-temp-buffer
  2066. (git-bug--call-process "bug" "ls")
  2067. (goto-char (point-min))
  2068. (let ((bugs nil))
  2069. (while (not (eq (point) (point-max)))
  2070. (save-match-data
  2071. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  2072. (setq bugs `(,@bugs
  2073. ,(list
  2074. :id (match-string 1)
  2075. :status (match-string 2)
  2076. :title (string-trim (match-string 3))
  2077. :user (match-string 4)
  2078. :comment-num (match-string 5)
  2079. :label-num (match-string 6)
  2080. )))))
  2081. (forward-line 1)
  2082. (goto-char (point-at-bol)))
  2083. bugs)))
  2084. (defun git-bug-ls ()
  2085. "Open and select git bug list buffer."
  2086. (interactive)
  2087. (pop-to-buffer (git-bug-ls-noselect)))
  2088. (defun git-bug-ls-noselect (&optional directory)
  2089. "Open git bug list buffer.
  2090. If optional arg DIRECTORY is given change current directory to there before
  2091. initializing."
  2092. (setq directory (expand-file-name (or directory
  2093. default-directory)))
  2094. (cl-assert (file-directory-p directory))
  2095. (let* ((root (git-bug--get-repository-root directory))
  2096. (name (file-name-nondirectory root))
  2097. (bname (format "*GitBug<%s>*" name)))
  2098. (with-current-buffer (get-buffer-create bname)
  2099. (cd root)
  2100. (git-bug-ls--set-tabulated-list-mode-variables)
  2101. (git-bug-ls-mode)
  2102. (current-buffer))))
  2103. (defun git-bug--get-repository-root (dir)
  2104. "Resolve repository root of DIR.
  2105. If DIR is not inside of any git repository, signal an error."
  2106. (cl-assert (file-directory-p dir))
  2107. (with-temp-buffer
  2108. (cd dir)
  2109. (git-bug--call-process "rev-parse" "--show-toplevel")
  2110. (goto-char (point-min))
  2111. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  2112. (defun git-bug--call-process (&rest args)
  2113. "Start git process synchronously with ARGS.
  2114. Raise error when git process ends with non-zero status.
  2115. Any output will be written to current buffer."
  2116. (let ((status (apply 'call-process
  2117. "git"
  2118. nil
  2119. t
  2120. nil
  2121. args)))
  2122. (cl-assert (eq status 0)
  2123. nil
  2124. (buffer-substring-no-properties (point-min) (point-max)))))
  2125. ;;;;;;;;;;;;;;;;;;;
  2126. ;; peek-file-mode
  2127. (defvar peek-file-buffers
  2128. ()
  2129. "Peek buffers.")
  2130. (defun peek-file (file)
  2131. "Peek FILE."
  2132. (interactive "fFile to peek: ")
  2133. (with-current-buffer (find-file file)
  2134. (peek-file-mode)))
  2135. (define-minor-mode peek-file-mode
  2136. "Peek file mode."
  2137. :lighter "PK"
  2138. (view-mode peek-file-mode)
  2139. (add-to-list 'peek-file-buffers
  2140. (current-buffer))
  2141. (add-hook 'switch-buffer-functions
  2142. 'peek-file-remove-buffers))
  2143. (defun peek-file-remove-buffers (&args _)
  2144. "Remove peek file buffers."
  2145. (cl-dolist (buf (cl-copy-list peek-file-buffers))
  2146. (unless (get-buffer-window buf t)
  2147. (setq peek-file-buffers
  2148. (delq buf
  2149. peek-file-buffers))
  2150. (with-current-buffer buf
  2151. (when peek-file-mode
  2152. (kill-buffer))))))
  2153. ;;;;;;;;;;;;;;;;;;;;
  2154. ;; remember-projectile
  2155. ;; TODO: Add global-minor-mode
  2156. (defvar remember-data-file)
  2157. (defun my-set-remember-data-file-buffer-local ()
  2158. "Set `remember-data-file'."
  2159. (when (require 'projectile nil t)
  2160. (setq-local remember-data-file
  2161. (expand-file-name ".remember.notes"
  2162. (projectile-project-root)))))
  2163. (add-hook 'after-change-major-mode-hook
  2164. 'my-set-remember-data-file-buffer-local)
  2165. (define-key ctl-x-map "R" 'remember)
  2166. ;; ivy
  2167. (defvar ivy-re-builders-alist)
  2168. (set-variable 'ivy-re-builders-alist
  2169. '((t . my--ivy-regex-fuzzy-ignore-order)))
  2170. (defun my--ivy-regex-fuzzy-ignore-order (str)
  2171. "Re-build regex from STR for ignore-order fuzzy match."
  2172. (let ((re-list (ivy--regex-ignore-order str)))
  2173. (if (listp re-list)
  2174. (mapcar (lambda (e)
  2175. (let ((head (car e))
  2176. (tail (cdr e)))
  2177. (if tail
  2178. (cons (ivy--regex-fuzzy head)
  2179. tail)
  2180. (cons head tail))))
  2181. re-list)
  2182. (ivy--regex-fuzzy re-list))))
  2183. ;; (my--ivy-regex-fuzzy-ignore-order "ab bc !cee")
  2184. ;; (ivy--regex-fuzzy "ab")
  2185. ;; (ivy--regex-ignore-order "a b")
  2186. (with-eval-after-load 'ivy
  2187. (defvar ivy-minibuffer-map)
  2188. (define-key ivy-minibuffer-map (kbd "C-u")
  2189. (lambda () (interactive) (delete-region (point-at-bol) (point)))))
  2190. (when (fboundp 'counsel-M-x)
  2191. (define-key esc-map "x" 'counsel-M-x)
  2192. ;; (counsel-mode 1)
  2193. ;; counsel-fzf executes fzf -f QUERY for each input
  2194. ;; (define-key ctl-x-map "f"
  2195. ;; (lambda ()
  2196. ;; (interactive
  2197. ;; (let ((process-environment (cl-copy-list process-environment)))
  2198. ;; (setenv "FZF_DEFAULT_COMMAND" nil)
  2199. ;; (counsel-fzf)))))
  2200. )
  2201. (when (and (fboundp 'ivy-read)
  2202. (locate-library "counsel"))
  2203. (defvar counsel-describe-map)
  2204. (defun my-counsel-describe-symbol ()
  2205. "Forwaord to `describe-symbol'."
  2206. (interactive)
  2207. (cl-assert (eval-and-compile (require 'help-mode nil t))) ;; describe-symbol-backends
  2208. (cl-assert (eval-and-compile (require 'counsel nil t)))
  2209. (ivy-read "Describe symbol: " obarray
  2210. ;; From describe-symbol definition
  2211. :predicate (lambda (vv)
  2212. (cl-some (lambda (x) (funcall (nth 1 x) vv))
  2213. describe-symbol-backends))
  2214. :require-match t
  2215. :history 'counsel-describe-symbol-history
  2216. :keymap counsel-describe-map
  2217. :preselect (ivy-thing-at-point)
  2218. :action (lambda (x)
  2219. (describe-symbol (intern x)))
  2220. :caller 'my-counsel-describe-symbol))
  2221. (define-key help-map "o" 'my-counsel-describe-symbol)
  2222. )
  2223. (with-eval-after-load 'ivy
  2224. (ivy-configure 'my-counsel-describe-symbol
  2225. :sort-fn #'ivy-string<)
  2226. )
  2227. (when (fboundp 'counsel-imenu)
  2228. (define-key ctl-x-map "l" 'counsel-imenu))
  2229. (when (fboundp 'swoop)
  2230. ;; (global-set-key (kbd "C-s") 'swoop)
  2231. ;; (global-set-key (kbd "C-r") 'swoop)
  2232. ;; (define-key esc-map (kbd "C-s") 'swoop-multi)
  2233. (define-key esc-map (kbd "C-s") 'swoop)
  2234. (with-eval-after-load 'swoop-lib
  2235. (defvar swoop-map)
  2236. (define-key swoop-map (kbd "C-s") 'swoop-action-goto-line-next)
  2237. (define-key swoop-map (kbd "C-r") 'swoop-action-goto-line-prev)
  2238. )
  2239. ;; TODO: case sensitive swoop
  2240. ;; Wrap swoop-async-get-match-lines-list?
  2241. ;; (with-eval-after-load 'swoop-lib
  2242. ;; (defun my-swoop-advice-smart-case (orig-func &rest args)
  2243. ;; "Function to wrap swoop function."
  2244. ;; (message "args: %S" args)
  2245. ;; (let* (
  2246. ;; (query (car args))
  2247. ;; ;; (query (plist-get args
  2248. ;; ;; :$query))
  2249. ;; (case-fold-search
  2250. ;; (let ((case-fold-search nil))
  2251. ;; (not (string-match-p (rx upper) query))))
  2252. ;; )
  2253. ;; ;; (message "case-fold-search: %S" case-fold-search)
  2254. ;; ;; (message "query: %S" query)
  2255. ;; (apply orig-func args)))
  2256. ;; (advice-add 'swoop-async-get-match-lines-list
  2257. ;; :around
  2258. ;; 'my-swoop-advice-smart-case)
  2259. ;; (set-variable 'swoop-async-get-match-lines-list
  2260. ;; (byte-compile 'swoop-async-get-match-lines-list))
  2261. ;; )
  2262. )
  2263. ;; なんかよくわからないけど頻繁に index.lock を残してしまう
  2264. (when (fboundp 'dired-k)
  2265. (set-variable 'dired-k-style 'git)
  2266. ;; What is the best way of doing this?
  2267. (with-eval-after-load 'dired-k
  2268. (fset 'dired-k--highlight-by-file-attribyte 'ignore))
  2269. ;; (set-variable 'dired-k-size-colors
  2270. ;; `((,most-positive-fixnum)))
  2271. ;; (set-variable 'dired-k-date-colors
  2272. ;; `((,most-positive-fixnum)))
  2273. ;; always execute dired-k when dired buffer is opened and reverted
  2274. ;; (add-hook 'dired-after-readin-hook #'dired-k-no-revert)
  2275. ;; This causes:
  2276. ;; fatal: Unable to create '.git/index.lock': File exist.s
  2277. ;; (add-hook 'switch-buffer-functions
  2278. ;; (lambda (prev cur)
  2279. ;; (when (derived-mode-p 'dired-mode)
  2280. ;; (dired-k-no-revert))))
  2281. )
  2282. (add-hook 'python-mode-hook
  2283. (lambda ()
  2284. ;; Currently on python-mode eldoc-mode sometimes print
  2285. ;; wired message on "from" keyword:
  2286. ;;var from = require("./from")
  2287. (eldoc-mode -1)))
  2288. ;; ?
  2289. (define-key input-decode-map "\e[1;5C" [C-right])
  2290. (define-key input-decode-map "\e[1;5D" [C-left])
  2291. ;; mozc
  2292. (global-set-key (kbd "C-c m e") 'ignore)
  2293. (global-set-key (kbd "C-c m d") 'ignore)
  2294. ;; mozc
  2295. (when (locate-library "mozc")
  2296. ;; https://tottoto.net/mac-emacs-karabiner-elements-japanese-input-method-config/
  2297. (with-eval-after-load 'mozc
  2298. ;; (define-key mozc-mode-map (kbd "C-h") 'backward-delete-char)
  2299. ;; (define-key mozc-mode-map (kbd "C-p") (kbd "<up>"))
  2300. ;; (define-key mozc-mode-map (kbd "C-n") (kbd "SPC"))
  2301. )
  2302. (setq default-input-method "japanese-mozc")
  2303. (custom-set-variables '(mozc-leim-title "あ"))
  2304. (defun turn-on-input-method ()
  2305. (interactive)
  2306. (activate-input-method default-input-method))
  2307. (defun turn-off-input-method ()
  2308. (interactive)
  2309. (deactivate-input-method))
  2310. ;; (setq mozc-candidate-style 'echo-area)
  2311. (global-set-key (kbd "C-c m e") 'turn-on-input-method)
  2312. (global-set-key (kbd "C-c m d") 'turn-off-input-method)
  2313. (require 'mozc-popup)
  2314. (set-variable 'mozc-candidate-style 'popup)
  2315. ;; これいる?
  2316. (when (require 'mozc-im nil t)
  2317. (setq default-input-method "japanese-mozc-im")
  2318. ;; (global-set-key (kbd "C-j") 'toggle-input-method)
  2319. )
  2320. )
  2321. ;; Local Variables:
  2322. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2323. ;; flycheck-checker: emacs-lisp
  2324. ;; End:
  2325. ;;; emancs.el ends here