Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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