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.
 
 
 
 
 
 

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