Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

2843 rindas
93 KiB

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