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.
 
 
 
 
 
 

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