You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2136 lines
70 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  4. ;; SETUP_LOAD: (and (file-readable-p file)
  5. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  6. (setq debug-on-error t)
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (require 'cl-lib)
  11. (require 'simple)
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;; Some macros for internals
  14. (defmacro call-after-init (&rest body)
  15. "If `after-init-hook' has been run, run BODY immediately.
  16. Otherwize hook it."
  17. (declare (indent 0) (debug t))
  18. `(if after-init-time
  19. ;; Currently after-init-hook is run just after setting after-init-hook
  20. (progn
  21. ,@body)
  22. (add-hook 'after-init-hook
  23. (lambda ()
  24. ,@body))))
  25. (defmacro safe-require-or-eval (feature)
  26. "Require FEATURE if available.
  27. At compile time the feature will be loaded immediately."
  28. `(eval-and-compile
  29. (message "safe-require-or-eval: Trying to require %s" ,feature)
  30. (require ,feature nil t)))
  31. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  32. "Define autoloading FEATURE that defines FUNCTIONS.
  33. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  34. the function same as FEATURE is defined as autoloaded function. BODY is passed
  35. to `eval-after-load'.
  36. After this macro is expanded, this returns the path to library if FEATURE
  37. found, otherwise returns nil."
  38. (declare (indent 2) (debug t))
  39. (let* ((libname (symbol-name (eval feature)))
  40. (libpath (locate-library libname)))
  41. `(progn
  42. (when (locate-library ,libname)
  43. ,@(mapcar (lambda (f)
  44. `(unless (fboundp ',f)
  45. (progn
  46. (message "Autoloaded function `%S' defined (%s)"
  47. (quote ,f)
  48. ,libpath)
  49. (autoload (quote ,f)
  50. ,libname
  51. ,(concat "Autoloaded function defined in \""
  52. libpath
  53. "\".")
  54. t))))
  55. (or (eval functions)
  56. `(,(eval feature)))))
  57. (eval-after-load ,feature
  58. (quote (progn
  59. ,@body)))
  60. (locate-library ,libname))))
  61. (when (autoload-eval-lazily 'tetris nil
  62. (message "Tetris loaded!"))
  63. (message "Tetris found!"))
  64. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  65. ;; package
  66. (set (defvar 10sr-package-list)
  67. '(
  68. vimrc-mode
  69. markdown-mode
  70. yaml-mode
  71. gnuplot-mode
  72. php-mode
  73. erlang
  74. js2-mode
  75. js-doc
  76. git-commit
  77. gitignore-mode
  78. adoc-mode
  79. go-mode
  80. ;; It seems malabar has been merged into jdee and this package
  81. ;; already removed
  82. ;; malabar-mode
  83. gosh-mode
  84. scala-mode
  85. ;;ensime
  86. color-moccur
  87. ggtags
  88. flycheck
  89. auto-highlight-symbol
  90. hl-todo
  91. ;; Currently not available
  92. ;; pp-c-l
  93. xclip
  94. foreign-regexp
  95. multi-term
  96. term-run
  97. editorconfig
  98. git-ps1-mode
  99. restart-emacs
  100. fill-column-indicator
  101. pkgbuild-mode
  102. minibuffer-line
  103. which-key
  104. ;; I think this works in place of my autosave lib
  105. super-save
  106. pipenv
  107. imenu-list
  108. page-break-lines
  109. aggressive-indent
  110. dired-filter
  111. wgrep
  112. magit
  113. git-gutter
  114. end-mark
  115. sl
  116. ;; TODO: Configure pony-tpl-mode
  117. pony-mode
  118. gited
  119. highlight-indentation
  120. diminish
  121. fzf
  122. editorconfig
  123. editorconfig-custom-majormode
  124. git-command
  125. prompt-text
  126. ;; 10sr repository
  127. ;; 10sr-extras
  128. terminal-title
  129. dired-list-all-mode
  130. pack
  131. set-modeline-color
  132. read-only-only-mode
  133. smart-revert
  134. autosave
  135. ;;window-organizer
  136. ilookup
  137. pasteboard
  138. awk-preview
  139. recently
  140. ))
  141. (when (safe-require-or-eval 'package)
  142. (setq package-archives
  143. `(,@package-archives
  144. ("melpa" . "https://melpa.org/packages/")
  145. ;; Somehow fails to download via https
  146. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  147. (package-initialize)
  148. (defun my-auto-install-package ()
  149. "Install packages semi-automatically."
  150. (interactive)
  151. (package-refresh-contents)
  152. (mapc (lambda (pkg)
  153. (or (package-installed-p pkg)
  154. (package-install pkg)))
  155. 10sr-package-list))
  156. )
  157. ;; (lazy-load-eval 'sudoku)
  158. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  159. ;; my-idle-hook
  160. (defvar my-idle-hook nil
  161. "Hook run when idle for several secs.")
  162. (defvar my-idle-hook-sec 5
  163. "Second to run `my-idle-hook'.")
  164. (run-with-idle-timer my-idle-hook-sec
  165. t
  166. (lambda ()
  167. (run-hooks 'my-idle-hook)))
  168. ;; (add-hook 'my-idle-hook
  169. ;; (lambda ()
  170. ;; (message "idle hook message")))
  171. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  172. ;; start and quit
  173. (setq inhibit-startup-message t)
  174. (setq confirm-kill-emacs 'y-or-n-p)
  175. (setq gc-cons-threshold (* 1024 1024 16))
  176. (setq garbage-collection-messages nil)
  177. (when window-system
  178. (add-to-list 'default-frame-alist '(cursor-type . box))
  179. (add-to-list 'default-frame-alist '(background-color . "white"))
  180. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  181. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  182. ;; does not work?
  183. )
  184. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  185. (menu-bar-mode 1)
  186. (define-key ctl-x-map "m" 'menu-bar-open)
  187. (and (fboundp 'tool-bar-mode)
  188. (tool-bar-mode 0))
  189. (and (fboundp 'set-scroll-bar-mode)
  190. (set-scroll-bar-mode nil))
  191. (call-after-init
  192. (message "%s %s" invocation-name emacs-version)
  193. (message "Invocation directory: %s" default-directory)
  194. (message "%s was taken to initialize emacs." (emacs-init-time))
  195. (view-echo-area-messages)
  196. ;; (view-emacs-news)
  197. )
  198. (with-current-buffer "*Messages*"
  199. (emacs-lock-mode 'kill))
  200. (cd ".") ; when using windows use / instead of \ in `default-directory'
  201. ;; locale
  202. (set-language-environment "Japanese")
  203. (set-default-coding-systems 'utf-8-unix)
  204. (prefer-coding-system 'utf-8-unix)
  205. (setq system-time-locale "C")
  206. ;; my prefix map
  207. (defvar my-prefix-map nil
  208. "My prefix map.")
  209. (define-prefix-command 'my-prefix-map)
  210. (global-set-key (kbd "C-^") 'my-prefix-map)
  211. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  212. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  213. ;; (comint-show-maximum-output)
  214. ;; kill scratch
  215. (call-after-init
  216. (let ((buf (get-buffer "*scratch*")))
  217. (when buf
  218. (kill-buffer buf))))
  219. ;; modifier keys
  220. ;; (setq mac-option-modifier 'control)
  221. ;; display
  222. (setq visible-bell t)
  223. (setq ring-bell-function 'ignore)
  224. (mouse-avoidance-mode 'banish)
  225. (setq echo-keystrokes 0.1)
  226. (defun reload-init-file ()
  227. "Reload Emacs init file."
  228. (interactive)
  229. (when (and user-init-file
  230. (file-readable-p user-init-file))
  231. (load-file user-init-file)))
  232. (safe-require-or-eval 'session)
  233. ;; server
  234. (set-variable 'server-name (concat "server"
  235. (number-to-string (emacs-pid))))
  236. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  237. ;; In Darwin environment, init fails with message like 'Service name too long'
  238. ;; when server-use-tcp is nil
  239. (when (or (eq system-type
  240. 'cygwin)
  241. (eq system-type
  242. 'darwin))
  243. (set-variable 'server-use-tcp t))
  244. ;; MSYS2 fix
  245. (when (eq system-type
  246. 'windows-nt)
  247. (setq shell-file-name
  248. (executable-find "bash"))
  249. '(setq function-key-map
  250. `(,@function-key-map ([pause] . [?\C-c])
  251. ))
  252. (define-key key-translation-map
  253. (kbd "<pause>")
  254. (kbd "C-c"))
  255. '(keyboard-translate [pause]
  256. (kbd "C-c")p)
  257. ;; TODO: move to other place later
  258. (when (not window-system)
  259. (setq interprogram-paste-function nil)
  260. (setq interprogram-cut-function nil)))
  261. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  262. ;; global keys
  263. (global-set-key (kbd "<up>") 'scroll-down-line)
  264. (global-set-key (kbd "<down>") 'scroll-up-line)
  265. (global-set-key (kbd "<left>") 'scroll-down)
  266. (global-set-key (kbd "<right>") 'scroll-up)
  267. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  268. (global-set-key (kbd "C-\\") help-map)
  269. (define-key ctl-x-map (kbd "DEL") help-map)
  270. (define-key ctl-x-map (kbd "C-h") help-map)
  271. (define-key help-map "a" 'apropos)
  272. ;; disable annoying keys
  273. (global-set-key [prior] 'ignore)
  274. (global-set-key (kbd "<next>") 'ignore)
  275. (global-set-key [menu] 'ignore)
  276. (global-set-key [down-mouse-1] 'ignore)
  277. (global-set-key [down-mouse-2] 'ignore)
  278. (global-set-key [down-mouse-3] 'ignore)
  279. (global-set-key [mouse-1] 'ignore)
  280. (global-set-key [mouse-2] 'ignore)
  281. (global-set-key [mouse-3] 'ignore)
  282. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  283. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  284. ;; Interactively evaluate Emacs Lisp expressions
  285. (define-key ctl-x-map "i" 'ielm)
  286. (when (safe-require-or-eval 'which-key)
  287. (which-key-mode))
  288. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  289. ;; editor
  290. (setq kill-whole-line t)
  291. (setq scroll-conservatively 35
  292. scroll-margin 2)
  293. (setq-default major-mode 'text-mode)
  294. (setq next-line-add-newlines nil)
  295. (setq kill-read-only-ok t)
  296. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  297. ;; (setq-default line-spacing 0.2)
  298. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  299. ;; (setq-default tab-width 4)
  300. (setq-default indent-tabs-mode nil)
  301. (setq-default indent-line-function 'indent-to-left-margin)
  302. ;; (setq-default indent-line-function nil)
  303. (setq-default truncate-lines nil)
  304. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  305. (delete-selection-mode 1)
  306. (cua-mode 0)
  307. (setq line-move-visual nil)
  308. (setq create-lockfiles nil)
  309. (add-hook 'before-save-hook
  310. 'time-stamp)
  311. ;; Add Time-stamp: <> to insert timestamp there
  312. (set-variable 'time-stamp-format
  313. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  314. ;; key bindings
  315. ;; moving around
  316. ;;(keyboard-translate ?\M-j ?\C-j)
  317. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  318. (define-key esc-map "p" 'backward-paragraph)
  319. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  320. (define-key esc-map "n" 'forward-paragraph)
  321. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  322. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  323. (global-set-key (kbd "C-<left>") 'scroll-down)
  324. (global-set-key (kbd "C-<right>") 'scroll-up)
  325. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  326. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  327. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  328. ;; C-h and DEL
  329. (global-set-key (kbd "C-h") (kbd "DEL"))
  330. ;; (normal-erase-is-backspace-mode 1)
  331. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  332. (global-set-key (kbd "C-m") 'newline-and-indent)
  333. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  334. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  335. (define-key esc-map "u" 'undo)
  336. (define-key esc-map "i" (kbd "ESC TAB"))
  337. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  338. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  339. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  340. (require 'page-ext nil t)
  341. (when (safe-require-or-eval 'page-break-lines)
  342. (global-page-break-lines-mode 1))
  343. (when (safe-require-or-eval 'git-gutter)
  344. (declare-function global-git-gutter-mode "git-gutter")
  345. (custom-set-variables
  346. '(git-gutter:lighter " Gttr"))
  347. (custom-set-variables
  348. '(git-gutter:update-interval 2))
  349. (custom-set-variables
  350. '(git-gutter:unchanged-sign " "))
  351. (when (>= (display-color-cells)
  352. 256)
  353. (let ((c "color-233"))
  354. (set-face-background 'git-gutter:modified c)
  355. (set-face-background 'git-gutter:added c)
  356. (set-face-background 'git-gutter:deleted c)
  357. (set-face-background 'git-gutter:unchanged c)))
  358. (global-git-gutter-mode 1)
  359. )
  360. ;; (when (safe-require-or-eval 'fancy-narrow)
  361. ;; (fancy-narrow-mode 1))
  362. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  363. ;; title and mode-line
  364. (when (safe-require-or-eval 'terminal-title)
  365. ;; if TERM is not screen use default value
  366. (if (getenv "TMUX")
  367. ;; if use tmux locally just basename of current dir
  368. (set-variable 'terminal-title-format
  369. '((file-name-nondirectory (directory-file-name
  370. default-directory))))
  371. (if (and (let ((tty-type (frame-parameter nil
  372. 'tty-type)))
  373. (and tty-type
  374. (equal (car (split-string tty-type
  375. "-"))
  376. "screen")))
  377. (not (getenv "SSH_CONNECTION")))
  378. (set-variable 'terminal-title-format
  379. '((file-name-nondirectory (directory-file-name
  380. default-directory))))
  381. ;; seems that TMUX is used locally and ssh to remote host
  382. (set-variable 'terminal-title-format
  383. `("em:"
  384. ,user-login-name
  385. "@"
  386. ,(car (split-string (system-name)
  387. "\\."))
  388. ":"
  389. default-directory))
  390. )
  391. )
  392. (terminal-title-mode))
  393. (setq eol-mnemonic-dos "\\r\\n")
  394. (setq eol-mnemonic-mac "\\r")
  395. (setq eol-mnemonic-unix "")
  396. (which-function-mode 1)
  397. (line-number-mode 0)
  398. (column-number-mode 0)
  399. (size-indication-mode 0)
  400. (setq mode-line-position
  401. '(:eval (format "L%%l/%d%s:C%%c"
  402. (count-lines (point-max)
  403. (point-min))
  404. (if (buffer-narrowed-p)
  405. "[N]"
  406. "")
  407. )))
  408. (when (safe-require-or-eval 'diminish)
  409. ;; FIXME: Eval after enabling mode
  410. (call-after-init
  411. (diminish 'recently-mode)
  412. (diminish 'editorconfig-mode)
  413. (diminish 'auto-highlight-symbol-mode)
  414. (diminish 'global-whitespace-mode)
  415. (diminish 'which-key-mode)
  416. (diminish 'page-break-lines-mode)
  417. (diminish 'highlight-indentation-mode)))
  418. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  419. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  420. ;; minibuffer
  421. (setq insert-default-directory t)
  422. (setq completion-ignore-case t
  423. read-file-name-completion-ignore-case t
  424. read-buffer-completion-ignore-case t)
  425. (setq resize-mini-windows t)
  426. (temp-buffer-resize-mode 1)
  427. (savehist-mode 1)
  428. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  429. (set-variable 'help-at-pt-display-when-idle t)
  430. (fset 'yes-or-no-p 'y-or-n-p)
  431. ;; complete symbol when `eval'
  432. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  433. (define-key minibuffer-local-map (kbd "C-u")
  434. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  435. ;; I dont know these bindings are good
  436. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  437. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  438. (when (safe-require-or-eval 'minibuffer-line)
  439. (set-face-underline 'minibuffer-line nil)
  440. (set-variable 'minibuffer-line-refresh-interval
  441. 25)
  442. ;; Set idle timer
  443. (defvar my-minibuffer-line--idle-timer nil)
  444. (defvar minibuffer-line-mode)
  445. (add-hook 'minibuffer-line-mode-hook
  446. (lambda ()
  447. (when my-minibuffer-line--idle-timer
  448. (cancel-timer my-minibuffer-line--idle-timer)
  449. (setq my-minibuffer-line--idle-timer nil))
  450. (when minibuffer-line-mode
  451. (setq my-minibuffer-line--idle-timer
  452. (run-with-idle-timer 0.5
  453. t
  454. 'minibuffer-line--update)))))
  455. (set-variable 'minibuffer-line-format
  456. `(,(concat user-login-name
  457. "@"
  458. (car (split-string (system-name)
  459. "\\."))
  460. ":")
  461. (:eval (abbreviate-file-name (or buffer-file-name
  462. default-directory)))
  463. (:eval (and (fboundp 'git-ps1-mode-get-current)
  464. (git-ps1-mode-get-current " [GIT:%s]")))
  465. " "
  466. (:eval (format-time-string display-time-format))))
  467. (minibuffer-line-mode 1)
  468. )
  469. (when (safe-require-or-eval 'prompt-text)
  470. (set-variable 'prompt-text-format
  471. `(,(concat ""
  472. user-login-name
  473. "@"
  474. (car (split-string (system-name)
  475. "\\."))
  476. ":")
  477. (:eval (abbreviate-file-name (or buffer-file-name
  478. default-directory)))
  479. (:eval (and (fboundp 'git-ps1-mode-get-current)
  480. (git-ps1-mode-get-current " [GIT:%s]")))
  481. " "
  482. (:eval (format-time-string display-time-format))
  483. "\n"
  484. (:eval (symbol-name this-command))
  485. ": "))
  486. (prompt-text-mode 1))
  487. (autoload-eval-lazily 'helm nil
  488. (defvar helm-map)
  489. (define-key helm-map (kbd "C-h") (kbd "DEL")))
  490. (setq-default header-line-format
  491. '(:eval (let ((f (or (buffer-file-name)
  492. default-directory)))
  493. (when f
  494. (abbreviate-file-name f)))))
  495. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  496. ;; letters, font-lock mode and fonts
  497. (setq text-quoting-style 'grave)
  498. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  499. ;; (set-window-margins (selected-window) 1 1)
  500. (unless window-system
  501. (setq frame-background-mode 'dark))
  502. (and (or (eq system-type 'Darwin)
  503. (eq system-type 'darwin))
  504. (fboundp 'mac-set-input-method-parameter)
  505. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  506. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  507. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  508. (boundp 'input-method-inactivate-hook))
  509. (add-hook 'input-method-activate-hook
  510. (lambda () (set-cursor-color "red")))
  511. (add-hook 'input-method-inactivate-hook
  512. (lambda () (set-cursor-color "black"))))
  513. (when (safe-require-or-eval 'paren)
  514. (show-paren-mode 1)
  515. (setq show-paren-delay 0.5
  516. show-paren-style 'parenthesis) ; mixed is hard to read
  517. ;; (set-face-background 'show-paren-match
  518. ;; "black")
  519. ;; ;; (face-foreground 'default))
  520. ;; (set-face-foreground 'show-paren-match
  521. ;; "white")
  522. ;; (set-face-inverse-video-p 'show-paren-match
  523. ;; t)
  524. )
  525. (transient-mark-mode 1)
  526. (global-font-lock-mode 1)
  527. (setq font-lock-global-modes
  528. '(not
  529. help-mode
  530. eshell-mode
  531. ;;term-mode
  532. Man-mode
  533. magit-diff-mode
  534. magit-revision-mode))
  535. ;; (standard-display-ascii ?\n "$\n")
  536. ;; (defvar my-eol-face
  537. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  538. ;; )
  539. ;; (defvar my-tab-face
  540. ;; '(("\t" . '(0 highlight t nil))))
  541. (defvar my-jspace-face
  542. '(("\u3000" . '(0 highlight t nil))))
  543. (add-hook 'font-lock-mode-hook
  544. (lambda ()
  545. ;; (font-lock-add-keywords nil my-eol-face)
  546. (font-lock-add-keywords nil my-jspace-face)
  547. ))
  548. (when (safe-require-or-eval 'whitespace)
  549. (add-to-list 'whitespace-display-mappings
  550. ;; We need t since last one takes precedence
  551. `(tab-mark ?\t ,(vconcat "|>\t")) t)
  552. ;; (add-to-list 'whitespace-display-mappings
  553. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  554. (setq whitespace-style '(face
  555. trailing ; trailing blanks
  556. ;; tabs
  557. ;; spaces
  558. ;; lines
  559. lines-tail ; lines over 80
  560. newline ; newlines
  561. empty ; empty lines at beg or end of buffer
  562. ;; big-indent
  563. ;; space-mark
  564. tab-mark
  565. newline-mark ; use display table for newline
  566. ))
  567. ;; (setq whitespace-newline 'font-lock-comment-face)
  568. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  569. (defun my-whitesspace-mode-reload ()
  570. "Reload whitespace-mode config."
  571. (interactive)
  572. (when whitespace-mode
  573. (whitespace-mode 0)
  574. (whitespace-mode 1)))
  575. (set-variable 'whitespace-line-column nil)
  576. (global-whitespace-mode t)
  577. (add-hook 'dired-mode-hook
  578. (lambda ()
  579. (set (make-local-variable 'whitespace-style) nil)))
  580. (if (>= (display-color-cells)
  581. 256)
  582. (set-face-foreground 'whitespace-newline "color-109")
  583. ;; (progn
  584. ;; (set-face-bold-p 'whitespace-newline
  585. ;; t))
  586. ))
  587. (and nil
  588. '(safe-require-or-eval 'fill-column-indicator)
  589. (setq fill-column-indicator))
  590. ;; highlight current line
  591. ;; http://wiki.riywo.com/index.php?Meadow
  592. (face-spec-set 'hl-line
  593. '((((min-colors 256)
  594. (background dark))
  595. (:background "color-234"))
  596. (((min-colors 256)
  597. (background light))
  598. (:background "color-234"))
  599. (t
  600. (:underline "black"))))
  601. (set-variable 'hl-line-global-modes
  602. '(not
  603. term-mode))
  604. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  605. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  606. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  607. ;;(safe-require-or-eval 'set-modeline-color)
  608. ;; (let ((fg (face-foreground 'default))
  609. ;; (bg (face-background 'default)))
  610. ;; (set-face-background 'mode-line-inactive
  611. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  612. ;; (set-face-foreground 'mode-line-inactive
  613. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  614. ;; (set-face-underline 'mode-line-inactive
  615. ;; t)
  616. ;; (set-face-underline 'vertical-border
  617. ;; nil)
  618. ;; (when (safe-require-or-eval 'end-mark)
  619. ;; (global-end-mark-mode))
  620. ;; M-x highlight-* to highlight things
  621. (global-hi-lock-mode 1)
  622. (unless (fboundp 'highlight-region-text)
  623. (defun highlight-region-text (beg end)
  624. "Highlight text between BEG and END."
  625. (interactive "r")
  626. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  627. end)))
  628. (setq deactivate-mark t)))
  629. (when (safe-require-or-eval 'auto-highlight-symbol)
  630. (set-variable 'ahs-idle-interval 0.6)
  631. (global-auto-highlight-symbol-mode 1))
  632. (when (safe-require-or-eval 'highlight-indentation)
  633. (set-face-background 'highlight-indentation-face "color-236")
  634. (dolist (hook
  635. '(
  636. prog-mode-hook
  637. text-mode-hook
  638. ))
  639. (add-hook hook
  640. 'highlight-indentation-mode)))
  641. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  642. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  643. ;; file handling
  644. ;; fzf
  645. ;; Too slow in term buffer!
  646. ;; (set-variable 'fzf/executable "sk")
  647. ;; (set-variable 'fzf/args "--color bw --print-query")
  648. ;; Modified from hardcoded default to include:
  649. ;; - directories
  650. ;; - hidden files
  651. ;; - root directory (.)
  652. ;; - parent directory (..)
  653. ;; ripgrep cannot list directories...
  654. ;; (setenv "FZF_DEFAULT_COMMAND" "rg --files --hidden --follow --glob '!.git/*' --no-ignore")
  655. (let* ((find (if (executable-find "bfs")
  656. ;; Breadth-first find https://github.com/tavianator/bfs
  657. "bfs"
  658. ;; Use gfind if available?
  659. "find"))
  660. (findcmd (concat "set -eu; set -o pipefail; "
  661. "echo .; "
  662. "echo ..; "
  663. "command " find " -L . "
  664. "-mindepth 1 "
  665. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  666. "-o -print "
  667. "2> /dev/null "
  668. "| "
  669. "cut -b3-"))
  670. (fdcmd (concat "set -eu; set -o pipefail; "
  671. "echo .; "
  672. "echo ..; "
  673. "command fd "
  674. "--follow --hidden --no-ignore "
  675. "--color always "
  676. "2>/dev/null")))
  677. (if (executable-find "fd")
  678. (setenv "FZF_DEFAULT_COMMAND" fdcmd)
  679. (setenv "FZF_DEFAULT_COMMAND" findcmd)))
  680. (set-variable 'fzf/window-height 45)
  681. (set-variable 'fzf/args "--print-query --ansi --color='bg+:-1' --inline-info --cycle")
  682. ;; (set-variable 'fzf/args "--print-query --ansi --inline-info --cycle")
  683. ;; (set-variable 'fzf/args "--print-query --ansi --color=bw --inline-info --cycle")
  684. (defun my-fzf-or-find-file ()
  685. "Call fzf if usable or call find-file."
  686. (declare (interactive-only t))
  687. (interactive)
  688. (if (and (executable-find "fzf")
  689. (fboundp 'fzf)
  690. (not (file-remote-p default-directory)))
  691. (fzf)
  692. (call-interactively 'find-file)))
  693. (define-key ctl-x-map "f" 'my-fzf-or-find-file)
  694. (defun my-fzf-all-lines ()
  695. "Fzf all lines."
  696. (interactive)
  697. (let ((process-environment (cl-copy-list process-environment)))
  698. (setenv "FZF_DEFAULT_COMMAND" "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^")
  699. (fzf)))
  700. (define-key ctl-x-map "S" 'my-fzf-all-lines)
  701. ;; recently
  702. (when (safe-require-or-eval 'recently)
  703. (set-variable 'recently-max 1000)
  704. (recently-mode 1))
  705. (when (safe-require-or-eval 'editorconfig)
  706. (set-variable 'editorconfig-get-properties-function
  707. 'editorconfig-core-get-properties-hash)
  708. (editorconfig-mode 1)
  709. (set-variable 'editorconfig-mode-lighter "")
  710. (when (fboundp 'ws-butler-mode)
  711. (set-variable 'editorconfig-trim-whitespaces-mode
  712. 'ws-butler-mode))
  713. (with-eval-after-load 'org-src
  714. ;; [*.org\[\*Org Src*\[ c \]*\]]
  715. (add-hook 'org-src-mode-hook
  716. 'editorconfig-mode-apply t)))
  717. (when (fboundp 'editorconfig-custom-majormode)
  718. (add-hook 'editorconfig-after-apply-functions
  719. 'editorconfig-custom-majormode))
  720. ;; Add readonly=true to set read-only-mode
  721. (add-hook 'editorconfig-after-apply-functions
  722. (lambda (props)
  723. (let ((r (gethash 'readonly props)))
  724. (when (and (string= r "true")
  725. (not buffer-read-only))
  726. (read-only-mode 1)))))
  727. (add-hook 'editorconfig-hack-properties-functions
  728. '(lambda (props)
  729. (when (derived-mode-p 'makefile-mode)
  730. (puthash 'indent_style "tab" props))))
  731. ;; (when (fboundp 'editorconfig-charset-extras)
  732. ;; (add-hook 'editorconfig-custom-hooks
  733. ;; 'editorconfig-charset-extras))
  734. (setq revert-without-query '(".+"))
  735. ;; save cursor position
  736. (when (safe-require-or-eval 'saveplace)
  737. (setq-default save-place t)
  738. (setq save-place-file (concat user-emacs-directory
  739. "places")))
  740. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  741. (setq make-backup-files t)
  742. (setq vc-make-backup-files t)
  743. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  744. (setq backup-directory-alist
  745. (cons (cons "." (expand-file-name (concat user-emacs-directory
  746. "backup")))
  747. backup-directory-alist))
  748. (setq version-control 't)
  749. (setq delete-old-versions t)
  750. (setq kept-new-versions 20)
  751. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  752. "auto-save/")))
  753. ;; (setq delete-auto-save-files t)
  754. (setq auto-save-visited-interval 8)
  755. (auto-save-visited-mode 1)
  756. (add-to-list 'completion-ignored-extensions ".bak")
  757. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  758. (setq delete-by-moving-to-trash t)
  759. ;; trash-directory "~/.emacs.d/trash")
  760. (add-hook 'after-save-hook
  761. 'executable-make-buffer-file-executable-if-script-p)
  762. (set-variable 'bookmark-default-file
  763. (expand-file-name (concat user-emacs-directory
  764. "bmk")))
  765. (set-variable 'bookmark-save-flag
  766. 1)
  767. (with-eval-after-load 'recentf
  768. (defvar recentf-exclude)
  769. (defvar bookmark-default-file)
  770. (add-to-list 'recentf-exclude
  771. (regexp-quote bookmark-default-file)))
  772. (when (safe-require-or-eval 'smart-revert)
  773. (smart-revert-on))
  774. ;; autosave
  775. ;; auto-save-visited-mode can be used instead?
  776. ;; (when (safe-require-or-eval 'autosave)
  777. ;; (autosave-set 8))
  778. ;; bookmarks
  779. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  780. ;; vc
  781. (set-variable 'vc-handled-backends '(RCS))
  782. (set-variable 'vc-rcs-register-switches "-l")
  783. (set-variable 'vc-rcs-checkin-switches "-l")
  784. (set-variable 'vc-command-messages t)
  785. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  786. ;; share clipboard with x
  787. ;; this page describes this in details, but only these sexps seem to be needed
  788. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  789. (and nil
  790. (not window-system)
  791. (not (eq window-system 'mac))
  792. (getenv "DISPLAY")
  793. (not (equal (getenv "DISPLAY") ""))
  794. (executable-find "xclip")
  795. ;; (< emacs-major-version 24)
  796. '(safe-require-or-eval 'xclip)
  797. nil
  798. (turn-on-xclip))
  799. (and (eq system-type 'darwin)
  800. (safe-require-or-eval 'pasteboard)
  801. (turn-on-pasteboard))
  802. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  803. ;; some modes and hooks
  804. ;; Include some extra modes
  805. (require 'generic-x)
  806. (when (autoload-eval-lazily 'wgrep)
  807. (set-variable 'wgrep-auto-save-buffer t)
  808. (with-eval-after-load 'grep
  809. (defvar grep-mode-map)
  810. (define-key grep-mode-map
  811. "e"
  812. 'wgrep-change-to-wgrep-mode)))
  813. (with-eval-after-load 'remember
  814. (defvar remember-mode-map (make-sparse-keymap))
  815. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  816. (with-eval-after-load 'magit-files
  817. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  818. ;; existing keybindings.
  819. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  820. ;; to disable thie mode here, but do anyway.
  821. ;; See also https://github.com/magit/magit/issues/3517
  822. (global-magit-file-mode -1))
  823. (with-eval-after-load 'magit-section
  824. (set-face-background 'magit-section-highlight
  825. nil))
  826. (with-eval-after-load 'magit-diff
  827. (set-face-background 'magit-diff-added-highlight
  828. nil)
  829. (set-face-background 'magit-diff-removed-highlight
  830. nil)
  831. (set-face-background 'magit-diff-context-highlight
  832. nil)
  833. )
  834. (when (boundp 'git-rebase-filename-regexp)
  835. (add-to-list 'auto-mode-alist
  836. `(,git-rebase-filename-regexp . text-mode)))
  837. (when (safe-require-or-eval 'aggressive-indent)
  838. (defvar aggressive-indent-excluded-modes)
  839. (setq aggressive-indent-excluded-modes
  840. `(diff-mode
  841. toml-mode
  842. conf-mode
  843. dockerfile-mode
  844. groovy-mode
  845. scala-mode
  846. ,@aggressive-indent-excluded-modes))
  847. (global-aggressive-indent-mode 1))
  848. (when (autoload-eval-lazily 'ggtags)
  849. (add-hook 'c-mode-common-hook
  850. (lambda ()
  851. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  852. (ggtags-mode 1))))
  853. (add-hook 'python-mode-hook
  854. (lambda ()
  855. (ggtags-mode 1))))
  856. (when (autoload-eval-lazily 'imenu-list)
  857. ;; (set-variable 'imenu-list-auto-resize t)
  858. (set-variable 'imenu-list-focus-after-activation t)
  859. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  860. (add-hook 'emacs-lisp-mode-hook
  861. (lambda ()
  862. (setq imenu-generic-expression
  863. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  864. ,@imenu-generic-expression))))
  865. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  866. (with-eval-after-load 'compile
  867. (defvar compilation-filter-start)
  868. (defvar compilation-error-regexp-alist)
  869. (require 'ansi-color)
  870. (add-hook 'compilation-filter-hook
  871. (lambda ()
  872. (let ((inhibit-read-only t))
  873. (ansi-color-apply-on-region compilation-filter-start
  874. (point)))))
  875. (add-to-list 'compilation-error-regexp-alist
  876. ;; ansible-lint
  877. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  878. ;; Workaround to avoid ensime error
  879. (defvar ensime-mode-key-prefix nil)
  880. (when (safe-require-or-eval 'company)
  881. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  882. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  883. (global-company-mode)
  884. ;; TODO: Show length of candidate list on cursor?
  885. (set-variable 'company-idle-delay nil)
  886. (set-variable 'company-minimum-prefix-length 2)
  887. (set-variable 'company-selection-wrap-around t)
  888. (defvar company-mode-map)
  889. ;; TODO: It seems sometimes this indent is a bit different from original C-i command
  890. ;; For example python-mode?
  891. ;; TODO: Set python-indent-trigger-commands
  892. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  893. ;; (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  894. (defvar company-active-map)
  895. (define-key company-active-map (kbd "C-n") 'company-select-next)
  896. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  897. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  898. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  899. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  900. (defvar company-mode)
  901. (defvar company-candidates)
  902. (defvar company-candidates-length)
  903. ;; (popup-tip "Hello, World!")
  904. (defun my-company-length-popup-tip ()
  905. "Show tooltip of candidate length."
  906. (interactive)
  907. (when (and (require 'popup nil t)
  908. company-mode
  909. ;; Do nothing when already in company completion
  910. (not company-candidates))
  911. (let ((l nil))
  912. (unwind-protect
  913. (progn
  914. (company-manual-begin)
  915. (setq l company-candidates-length))
  916. (company-cancel))
  917. (when l
  918. (popup-tip (format "(%d)" l))))))
  919. (set-variable 'my-company-length-popup-tip-timer
  920. (run-with-idle-timer 0.5 t
  921. 'my-company-length-popup-tip))
  922. ;; (current-active-maps)
  923. ;; (lookup-key)
  924. '(mapcar (lambda (map)
  925. (lookup-key map (kbd "C-i")))
  926. (current-active-maps))
  927. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  928. (set-face-attribute 'company-tooltip nil
  929. :foreground "black" :background "lightgrey")
  930. (set-face-attribute 'company-tooltip-common nil
  931. :foreground "black" :background "lightgrey")
  932. (set-face-attribute 'company-tooltip-common-selection nil
  933. :foreground "white" :background "steelblue")
  934. (set-face-attribute 'company-tooltip-selection nil
  935. :foreground "black" :background "steelblue")
  936. (set-face-attribute 'company-preview-common nil
  937. :background nil :foreground "lightgrey" :underline t)
  938. (set-face-attribute 'company-scrollbar-fg nil
  939. :background "orange")
  940. (set-face-attribute 'company-scrollbar-bg nil
  941. :background "gray40")
  942. )
  943. ;; https://github.com/lunaryorn/flycheck
  944. (when (safe-require-or-eval 'flycheck)
  945. (call-after-init (global-flycheck-mode)))
  946. (when (autoload-eval-lazily 'ilookup)
  947. (define-key ctl-x-map "d" 'ilookup-open-word))
  948. (set-variable 'ac-ignore-case nil)
  949. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  950. (define-key ctl-x-map "t" 'term-run-shell-command))
  951. (add-to-list 'safe-local-variable-values
  952. '(encoding utf-8))
  953. (setq enable-local-variables :safe)
  954. ;; Detect file type from shebang and set major-mode.
  955. (add-to-list 'interpreter-mode-alist
  956. '("python3" . python-mode))
  957. (add-to-list 'interpreter-mode-alist
  958. '("python2" . python-mode))
  959. (with-eval-after-load 'python
  960. (defvar python-mode-map (make-sparse-keymap))
  961. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  962. (when (autoload-eval-lazily 'pipenv)
  963. (declare-function pipenv-projectile-after-switch-default "pipenv")
  964. (add-hook 'python-mode-hook
  965. (lambda ()
  966. (pipenv-mode 1)
  967. (pipenv-projectile-after-switch-default)))
  968. )
  969. (set-variable 'flycheck-python-pycompile-executable "python3")
  970. (set-variable 'python-indent-guess-indent-offset nil)
  971. (with-eval-after-load 'blacken
  972. (when (require 'with-venv nil t)
  973. (with-venv-advice-add 'blacken-buffer)))
  974. ;; http://fukuyama.co/foreign-regexp
  975. '(and (safe-require-or-eval 'foreign-regexp)
  976. (progn
  977. (setq foreign-regexp/regexp-type 'perl)
  978. '(setq reb-re-syntax 'foreign-regexp)
  979. ))
  980. (autoload-eval-lazily 'sql '(sql-mode)
  981. (require 'sql-indent nil t))
  982. (add-to-list 'auto-mode-alist
  983. '("\\.hql\\'" . sql-mode))
  984. (when (autoload-eval-lazily 'git-command)
  985. (define-key ctl-x-map "g" 'git-command))
  986. (when (autoload-eval-lazily 'gited)
  987. (define-key ctl-x-map (kbd "C-g") 'gited-list))
  988. (when (safe-require-or-eval 'git-commit)
  989. (global-git-commit-mode 1))
  990. (with-eval-after-load 'git-commit
  991. (add-hook 'git-commit-setup-hook
  992. 'turn-off-auto-fill t))
  993. (autoload-eval-lazily 'sl)
  994. (with-eval-after-load 'rst
  995. (defvar rst-mode-map)
  996. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  997. (with-eval-after-load 'jdee
  998. (add-hook 'jdee-mode-hook
  999. (lambda ()
  1000. (make-local-variable 'global-mode-string)
  1001. (add-to-list 'global-mode-string
  1002. mode-line-position))))
  1003. ;; Cannot enable error thrown. Why???
  1004. ;; https://github.com/m0smith/malabar-mode#Installation
  1005. ;; (when (autoload-eval-lazily 'malabar-mode)
  1006. ;; (add-to-list 'load-path
  1007. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1008. ;; (safe-require-or-eval 'cedet-devel-load)
  1009. ;; (call-after-init (activate-malabar-mode)))
  1010. (with-eval-after-load 'make-mode
  1011. (defvar makefile-mode-map (make-sparse-keymap))
  1012. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1013. ;; this functions is set in write-file-functions, i cannot find any
  1014. ;; good way to remove this.
  1015. (fset 'makefile-warn-suspicious-lines 'ignore))
  1016. (with-eval-after-load 'verilog-mode
  1017. (defvar verilog-mode-map (make-sparse-keymap))
  1018. (define-key verilog-mode-map ";" 'self-insert-command))
  1019. (setq diff-switches "-u")
  1020. (with-eval-after-load 'diff-mode
  1021. ;; (when (and (eq major-mode
  1022. ;; 'diff-mode)
  1023. ;; (not buffer-file-name))
  1024. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1025. ;; (view-mode 1))
  1026. (set-face-attribute 'diff-header nil
  1027. :foreground nil
  1028. :background nil
  1029. :weight 'bold)
  1030. (set-face-attribute 'diff-file-header nil
  1031. :foreground nil
  1032. :background nil
  1033. :weight 'bold)
  1034. (set-face-foreground 'diff-index "blue")
  1035. (set-face-attribute 'diff-hunk-header nil
  1036. :foreground "cyan"
  1037. :weight 'normal)
  1038. (set-face-attribute 'diff-context nil
  1039. ;; :foreground "white"
  1040. :foreground nil
  1041. :weight 'normal)
  1042. (set-face-foreground 'diff-removed "red")
  1043. (set-face-foreground 'diff-added "green")
  1044. (set-face-background 'diff-removed nil)
  1045. (set-face-background 'diff-added nil)
  1046. (set-face-attribute 'diff-changed nil
  1047. :foreground "magenta"
  1048. :weight 'normal)
  1049. (set-face-attribute 'diff-refine-changed nil
  1050. :foreground nil
  1051. :background nil
  1052. :weight 'bold
  1053. :inverse-video t)
  1054. ;; Annoying !
  1055. ;;(diff-auto-refine-mode)
  1056. )
  1057. ;; (ffap-bindings)
  1058. (set-variable 'browse-url-browser-function
  1059. 'eww-browse-url)
  1060. (set-variable 'sh-here-document-word "__EOC__")
  1061. (when (autoload-eval-lazily 'adoc-mode
  1062. nil
  1063. (defvar adoc-mode-map (make-sparse-keymap))
  1064. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1065. (setq auto-mode-alist
  1066. `(("\\.adoc\\'" . adoc-mode)
  1067. ("\\.asciidoc\\'" . adoc-mode)
  1068. ,@auto-mode-alist)))
  1069. (with-eval-after-load 'markup-faces
  1070. ;; Is this too match ?
  1071. (set-face-foreground 'markup-meta-face
  1072. "color-245")
  1073. (set-face-foreground 'markup-meta-hide-face
  1074. "color-245")
  1075. )
  1076. ;; TODO: check if this is required
  1077. (when (autoload-eval-lazily 'groovy-mode nil
  1078. (defvar groovy-mode-map (make-sparse-keymap))
  1079. (define-key groovy-mode-map "(" 'self-insert-command)
  1080. (define-key groovy-mode-map ")" 'self-insert-command)
  1081. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1082. )
  1083. (add-to-list 'auto-mode-alist
  1084. '("build\\.gradle\\'" . groovy-mode)))
  1085. (add-to-list 'auto-mode-alist
  1086. '("\\.gawk\\'" . awk-mode))
  1087. (with-eval-after-load 'yaml-mode
  1088. (defvar yaml-mode-map (make-sparse-keymap))
  1089. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1090. (with-eval-after-load 'html-mode
  1091. (defvar html-mode-map (make-sparse-keymap))
  1092. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1093. (with-eval-after-load 'text-mode
  1094. (define-key text-mode-map (kbd "C-m") 'newline))
  1095. (autoload-eval-lazily 'info nil
  1096. (defvar Info-additional-directory-list)
  1097. (dolist (dir (directory-files (concat user-emacs-directory
  1098. "info")
  1099. t
  1100. "^[^.].*"))
  1101. (when (file-directory-p dir)
  1102. (add-to-list 'Info-additional-directory-list
  1103. dir)))
  1104. (let ((dir (expand-file-name "~/.brew/share/info")))
  1105. (when (file-directory-p dir)
  1106. (add-to-list 'Info-additional-directory-list
  1107. dir))))
  1108. (with-eval-after-load 'apropos
  1109. (defvar apropos-mode-map (make-sparse-keymap))
  1110. (define-key apropos-mode-map "n" 'next-line)
  1111. (define-key apropos-mode-map "p" 'previous-line))
  1112. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1113. ;; (define-key isearch-mode-map
  1114. ;; (kbd "C-j") 'isearch-other-control-char)
  1115. ;; (define-key isearch-mode-map
  1116. ;; (kbd "C-k") 'isearch-other-control-char)
  1117. ;; (define-key isearch-mode-map
  1118. ;; (kbd "C-h") 'isearch-other-control-char)
  1119. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1120. (define-key isearch-mode-map (kbd "M-r")
  1121. 'isearch-query-replace-regexp)
  1122. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1123. (setq lazy-highlight-cleanup nil)
  1124. ;; face for isearch highlighing
  1125. (set-face-attribute 'lazy-highlight
  1126. nil
  1127. :foreground `unspecified
  1128. :background `unspecified
  1129. :underline t
  1130. ;; :weight `bold
  1131. )
  1132. (add-hook 'outline-mode-hook
  1133. (lambda ()
  1134. (when (string-match "\\.md\\'" buffer-file-name)
  1135. (set (make-local-variable 'outline-regexp) "#+ "))))
  1136. (add-hook 'outline-mode-hook
  1137. 'outline-show-all)
  1138. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1139. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1140. (when (autoload-eval-lazily 'markdown-mode
  1141. '(markdown-mode gfm-mode)
  1142. (defvar gfm-mode-map (make-sparse-keymap))
  1143. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  1144. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1145. (set-variable 'markdown-command (or (executable-find "markdown")
  1146. (executable-find "markdown.pl")
  1147. ""))
  1148. (add-hook 'markdown-mode-hook
  1149. (lambda ()
  1150. (outline-minor-mode 1)
  1151. (flyspell-mode)
  1152. (set (make-local-variable 'comment-start) ";")))
  1153. )
  1154. ;; c-mode
  1155. ;; http://www.emacswiki.org/emacs/IndentingC
  1156. ;; http://en.wikipedia.org/wiki/Indent_style
  1157. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1158. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1159. (with-eval-after-load 'cc-vars
  1160. (defvar c-default-style nil)
  1161. (add-to-list 'c-default-style
  1162. '(c-mode . "k&r"))
  1163. (add-to-list 'c-default-style
  1164. '(c++-mode . "k&r")))
  1165. (autoload-eval-lazily 'js2-mode nil
  1166. ;; currently do not use js2-mode
  1167. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1168. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1169. ;; (defvar js2-mode-map (make-sparse-keymap))
  1170. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1171. ;; (interactive)
  1172. ;; (js2-enter-key)
  1173. ;; (indent-for-tab-command)))
  1174. ;; (add-hook (kill-local-variable 'before-save-hook)
  1175. ;; 'js2-before-save)
  1176. ;; (add-hook 'before-save-hook
  1177. ;; 'my-indent-buffer
  1178. ;; nil
  1179. ;; t)
  1180. )
  1181. (add-to-list 'interpreter-mode-alist
  1182. '("node" . js-mode))
  1183. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1184. (with-eval-after-load 'uniquify
  1185. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1186. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1187. (setq uniquify-min-dir-content 1))
  1188. (with-eval-after-load 'view
  1189. (defvar view-mode-map (make-sparse-keymap))
  1190. (define-key view-mode-map "j" 'scroll-up-line)
  1191. (define-key view-mode-map "k" 'scroll-down-line)
  1192. (define-key view-mode-map "v" 'toggle-read-only)
  1193. (define-key view-mode-map "q" 'bury-buffer)
  1194. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1195. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1196. ;; (define-key view-mode-map
  1197. ;; "n" 'nonincremental-repeat-search-forward)
  1198. ;; (define-key view-mode-map
  1199. ;; "N" 'nonincremental-repeat-search-backward)
  1200. ;; N conflicts with git-walktree
  1201. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1202. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1203. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1204. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1205. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1206. (global-set-key "\M-r" 'view-mode)
  1207. ;; (setq view-read-only t)
  1208. (with-eval-after-load 'term
  1209. (defvar term-raw-map (make-sparse-keymap))
  1210. (define-key term-raw-map (kbd "C-x")
  1211. (lookup-key (current-global-map)
  1212. (kbd "C-x"))))
  1213. (add-hook 'term-mode-hook
  1214. (lambda ()
  1215. ;; Stop current line highlighting
  1216. (set (make-local-variable (defvar hl-line-range-function))
  1217. (lambda () '(0 . 0)))
  1218. (set (make-local-variable 'scroll-margin)
  1219. 0)
  1220. (set-variable 'term-buffer-maximum-size 20480)
  1221. ))
  1222. (add-hook 'Man-mode-hook
  1223. (lambda ()
  1224. (view-mode 1)
  1225. (setq truncate-lines nil)))
  1226. (set-variable 'Man-notify-method (if window-system
  1227. 'newframe
  1228. 'aggressive))
  1229. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1230. "woman_cache.el")))
  1231. ;; not work because man.el will be loaded when man called
  1232. (defalias 'man 'woman)
  1233. (add-to-list 'auto-mode-alist
  1234. '("tox\\.ini\\'" . conf-unix-mode))
  1235. (when (autoload-eval-lazily 'toml-mode)
  1236. (add-to-list 'auto-mode-alist
  1237. '("/tox\\.ini\\'" . toml-mode))
  1238. (add-to-list 'auto-mode-alist
  1239. '("/Pipfile\\'" . toml-mode))
  1240. (add-to-list 'auto-mode-alist
  1241. '("/poetry\\.lock\\'" . toml-mode))
  1242. )
  1243. (when (autoload-eval-lazily 'json-mode)
  1244. (add-to-list 'auto-mode-alist
  1245. '("/Pipfile\\.lock\\'" . json-mode)))
  1246. (add-hook 'go-mode-hook
  1247. (lambda()
  1248. (defvar go-mode-map)
  1249. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1250. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1251. (when (autoload-eval-lazily 'k8s-mode)
  1252. (add-to-list 'auto-mode-alist
  1253. '("\\.k8s\\'" . k8s-mode)))
  1254. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1255. ;; buffers
  1256. (defvar bs-configurations)
  1257. (declare-function bs-set-configuration "bs")
  1258. (declare-function bs-refresh "bs")
  1259. (declare-function bs-message-without-log "bs")
  1260. (declare-function bs--current-config-message "bs")
  1261. (when (autoload-eval-lazily 'bs '(bs-show)
  1262. (add-to-list 'bs-configurations
  1263. '("specials" "^\\*" nil ".*" nil nil))
  1264. (add-to-list 'bs-configurations
  1265. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1266. (defvar bs-mode-map)
  1267. (defvar bs-current-configuration)
  1268. (define-key bs-mode-map (kbd "t")
  1269. ;; TODO: fix toggle feature
  1270. (lambda ()
  1271. (interactive)
  1272. (if (string= "specials"
  1273. bs-current-configuration)
  1274. (bs-set-configuration "files")
  1275. (bs-set-configuration "specials"))
  1276. (bs-refresh)
  1277. (bs-message-without-log "%s"
  1278. (bs--current-config-message))))
  1279. ;; (setq bs-configurations (list
  1280. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1281. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1282. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1283. )
  1284. (defalias 'list-buffers 'bs-show)
  1285. (set-variable 'bs-default-configuration "files-and-specials")
  1286. (set-variable 'bs-default-sort-name "by nothing")
  1287. (add-hook 'bs-mode-hook
  1288. (lambda ()
  1289. (set (make-local-variable 'scroll-margin) 0))))
  1290. ;;(iswitchb-mode 1)
  1291. (icomplete-mode)
  1292. (defun iswitchb-buffer-display-other-window ()
  1293. "Do iswitchb in other window."
  1294. (interactive)
  1295. (let ((iswitchb-default-method 'display))
  1296. (call-interactively 'iswitchb-buffer)))
  1297. ;; buffer killing
  1298. ;; (defun my-delete-window-killing-buffer () nil)
  1299. (defun my-query-kill-current-buffer ()
  1300. "Interactively kill current buffer."
  1301. (interactive)
  1302. (if (y-or-n-p (concat "kill current buffer? :"))
  1303. (kill-buffer (current-buffer))))
  1304. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1305. (substitute-key-definition 'kill-buffer
  1306. 'my-query-kill-current-buffer
  1307. global-map)
  1308. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1309. ;; dired
  1310. (defun my-file-head (filename &optional n)
  1311. "Return list of first N lines of file FILENAME."
  1312. ;; TODO: Fix for janapese text
  1313. ;; TODO: Fix for short text
  1314. (let ((num (or n 10))
  1315. (size 100)
  1316. (beg 0)
  1317. (end 0)
  1318. (result '())
  1319. (read -1))
  1320. (with-temp-buffer
  1321. (erase-buffer)
  1322. (while (or (<= (count-lines (point-min)
  1323. (point-max))
  1324. num)
  1325. (eq read 0))
  1326. (setq end (+ beg size))
  1327. (setq read (nth 1 (insert-file-contents-literally filename
  1328. nil
  1329. beg
  1330. end)))
  1331. (goto-char (point-max))
  1332. (setq beg (+ beg size)))
  1333. (goto-char (point-min))
  1334. (while (< (length result) num)
  1335. (let ((start (point)))
  1336. (forward-line 1)
  1337. (setq result
  1338. `(,@result ,(buffer-substring-no-properties start
  1339. (point))))))
  1340. result
  1341. ;; (buffer-substring-no-properties (point-min)
  1342. ;; (progn
  1343. ;; (forward-line num)
  1344. ;; (point)))
  1345. )))
  1346. ;; (apply 'concat (my-file-head "./shrc" 10)
  1347. (defun my-dired-echo-file-head (arg)
  1348. "Echo head of current file.
  1349. ARG is num to show, or defaults to 7."
  1350. (interactive "P")
  1351. (let ((f (dired-get-filename)))
  1352. (message "%s"
  1353. (apply 'concat
  1354. (my-file-head f
  1355. 7)))))
  1356. (defun my-dired-diff ()
  1357. "Show diff of marked file and file of current line."
  1358. (interactive)
  1359. (let ((files (dired-get-marked-files nil nil nil t)))
  1360. (if (eq (car files)
  1361. t)
  1362. (diff (cadr files) (dired-get-filename))
  1363. (message "One file must be marked!"))))
  1364. (defun dired-get-file-info ()
  1365. "Print information of current line file."
  1366. (interactive)
  1367. (let ((f (shell-quote-argument (dired-get-filename t))))
  1368. (if (file-directory-p f)
  1369. (progn
  1370. (message "Calculating disk usage...")
  1371. (shell-command (concat "du -hsD "
  1372. f)))
  1373. (shell-command (concat "file "
  1374. f)))))
  1375. (defun my-dired-scroll-up ()
  1376. "Scroll up."
  1377. (interactive)
  1378. (my-dired-previous-line (- (window-height) 1)))
  1379. (defun my-dired-scroll-down ()
  1380. "Scroll down."
  1381. (interactive)
  1382. (my-dired-next-line (- (window-height) 1)))
  1383. ;; (defun my-dired-forward-line (arg)
  1384. ;; ""
  1385. ;; (interactive "p"))
  1386. (defun my-dired-previous-line (arg)
  1387. "Move ARG lines up."
  1388. (interactive "p")
  1389. (if (> arg 0)
  1390. (progn
  1391. (if (eq (line-number-at-pos)
  1392. 1)
  1393. (goto-char (point-max))
  1394. (forward-line -1))
  1395. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1396. (dired-get-subdir))
  1397. (- arg 1)
  1398. arg)))
  1399. (dired-move-to-filename)))
  1400. (defun my-dired-next-line (arg)
  1401. "Move ARG lines down."
  1402. (interactive "p")
  1403. (if (> arg 0)
  1404. (progn
  1405. (if (eq (point)
  1406. (point-max))
  1407. (goto-char (point-min))
  1408. (forward-line 1))
  1409. (my-dired-next-line (if (or (dired-get-filename nil t)
  1410. (dired-get-subdir))
  1411. (- arg 1)
  1412. arg)))
  1413. (dired-move-to-filename)))
  1414. (defun my-tramp-remote-find-file (f)
  1415. "Open F."
  1416. (interactive (list (read-file-name "My Find File Tramp: "
  1417. "/scp:"
  1418. nil ;; "/scp:"
  1419. (confirm-nonexistent-file-or-buffer))))
  1420. (find-file f))
  1421. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1422. (if (eq window-system 'mac)
  1423. (setq dired-listing-switches "-lhF")
  1424. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1425. )
  1426. (setq dired-listing-switches "-lhF")
  1427. ;; when using dired-find-alternate-file
  1428. ;; reuse current dired buffer for the file to open
  1429. ;; (put 'dired-find-alternate-file 'disabled nil)
  1430. (set-variable 'dired-ls-F-marks-symlinks t)
  1431. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1432. (set-variable 'ls-lisp-dirs-first t)
  1433. (set-variable 'ls-lisp-use-localized-time-format t)
  1434. (set-variable 'ls-lisp-format-time-list
  1435. '("%Y-%m-%d %H:%M"
  1436. "%Y-%m-%d "))
  1437. (set-variable 'dired-dwim-target t)
  1438. (set-variable 'dired-isearch-filenames t)
  1439. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1440. (set-variable 'dired-hide-details-hide-information-lines nil)
  1441. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1442. (set-variable 'dired-recursive-deletes 'always)
  1443. ;; (add-hook 'dired-after-readin-hook
  1444. ;; 'my-replace-nasi-none)
  1445. (with-eval-after-load 'dired
  1446. (safe-require-or-eval 'ls-lisp)
  1447. (defvar dired-mode-map (make-sparse-keymap))
  1448. ;; dired-do-chgrp sometimes cause system hung
  1449. (define-key dired-mode-map "G" 'ignore)
  1450. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1451. (define-key dired-mode-map "i" 'dired-get-file-info)
  1452. ;; (define-key dired-mode-map "f" 'find-file)
  1453. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1454. (define-key dired-mode-map "z" 'fzf)
  1455. (define-key dired-mode-map "!" 'shell-command)
  1456. (define-key dired-mode-map "&" 'async-shell-command)
  1457. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1458. (define-key dired-mode-map "=" 'my-dired-diff)
  1459. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1460. (define-key dired-mode-map "b" 'gtkbm)
  1461. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1462. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1463. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1464. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1465. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1466. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1467. (substitute-key-definition 'dired-next-line
  1468. 'my-dired-next-line
  1469. dired-mode-map)
  1470. (substitute-key-definition 'dired-previous-line
  1471. 'my-dired-previous-line
  1472. dired-mode-map)
  1473. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1474. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1475. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1476. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1477. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1478. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1479. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1480. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1481. (add-hook 'dired-mode-hook
  1482. (lambda ()
  1483. (when (fboundp 'dired-hide-details-mode)
  1484. (dired-hide-details-mode t)
  1485. (local-set-key "l" 'dired-hide-details-mode))
  1486. (let ((file "._Icon\015"))
  1487. (when nil
  1488. '(file-readable-p file)
  1489. (delete-file file)))))
  1490. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack)
  1491. (defvar pack-program-alist)
  1492. (add-to-list 'pack-program-alist
  1493. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf")))
  1494. (set-variable 'pack-silence
  1495. t)
  1496. (with-eval-after-load 'dired
  1497. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1498. (when (autoload-eval-lazily 'dired-list-all-mode)
  1499. (setq dired-listing-switches "-lhF")
  1500. (with-eval-after-load 'dired
  1501. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1502. (when (autoload-eval-lazily 'dired-filter)
  1503. (add-hook 'dired-mode-hook
  1504. 'dired-filter-mode))
  1505. (set-variable 'dired-filter-stack nil)
  1506. ;; Currently disabled in favor of dired-from-git-ls-files
  1507. ;; (define-key ctl-x-map "f" 'find-dired)
  1508. (defvar my-dired-git-ls-files-history
  1509. "History for `my-dired-git-ls-files'." nil)
  1510. (defun my-dired-git-ls-files (arg)
  1511. "Dired from git ls-files."
  1512. (interactive (list
  1513. (read-shell-command "git ls-files: "
  1514. "git ls-files -z ")))
  1515. (pop-to-buffer-same-window
  1516. (dired-noselect `(,default-directory
  1517. ,@(split-string (shell-command-to-string arg)
  1518. "\0" t))
  1519. ""))
  1520. )
  1521. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1522. (with-eval-after-load 'dired
  1523. (defvar dired-mode-map (make-sparse-keymap))
  1524. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1525. ;; (define-minor-mode my-dired-glob-filter)
  1526. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1527. ;; misc funcs
  1528. (when (fboundp 'browse-url-default-macosx-browser)
  1529. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1530. (defalias 'qcalc 'quick-calc)
  1531. (defun memo (&optional dir)
  1532. "Open memo.txt in DIR."
  1533. (interactive)
  1534. (pop-to-buffer (find-file-noselect (concat (if dir
  1535. (file-name-as-directory dir)
  1536. "")
  1537. "memo.txt"))))
  1538. (set (defvar my-privnotes-path nil
  1539. "My privnotes repository path.")
  1540. (expand-file-name "~/my/privnotes"))
  1541. (defun my-privnotes-readme (dir)
  1542. "Open my privnotes DIR."
  1543. (interactive (list
  1544. (read-file-name "Privnotes: "
  1545. (expand-file-name (format-time-string "%Y%m%d_")
  1546. my-privnotes-path))))
  1547. (let ((path (expand-file-name "README.md" dir)))
  1548. (with-current-buffer (find-file path)
  1549. (unless (file-exists-p path)
  1550. (insert (file-name-base dir)
  1551. "\n"
  1552. "=======\n"
  1553. "\n\n")))))
  1554. (define-key ctl-x-map "p" 'my-privnotes-readme)
  1555. (set (defvar my-rgrep-alist nil
  1556. "Alist of rgrep command.
  1557. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1558. condition to choose COMMAND when evaluated.")
  1559. `(
  1560. ;; ripgrep
  1561. ("rg"
  1562. (executable-find "rg")
  1563. "rg -nH --no-heading --color=always --hidden --glob '!.git/' --smart-case -M 1280 ")
  1564. ;; git grep
  1565. ("gitgrep"
  1566. (eq 0
  1567. (shell-command "git rev-parse --git-dir"))
  1568. "git --no-pager -c color.grep=always grep -nH -e ")
  1569. ;; sift
  1570. ("sift"
  1571. (executable-find "sift")
  1572. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1573. ;; the silver searcher
  1574. ("ag"
  1575. (executable-find "ag")
  1576. "ag --nogroup --nopager --filename ")
  1577. ;; ack
  1578. ("ack"
  1579. (executable-find "ack")
  1580. "ack --nogroup --nopager --with-filename ")
  1581. ;; gnu global
  1582. ("global"
  1583. (and (require 'ggtags nil t)
  1584. (executable-find "global")
  1585. (ggtags-current-project-root))
  1586. "global --result grep ")
  1587. ;; grep
  1588. ("grep"
  1589. t
  1590. ,(concat "find . "
  1591. "-path '*/.git' -prune -o "
  1592. "-path '*/.svn' -prune -o "
  1593. "-type f -print0 | "
  1594. "xargs -0 grep -nH -e "))
  1595. )
  1596. )
  1597. (defvar my-rgrep-default nil
  1598. "Default command name for my-rgrep.")
  1599. (defun my-rgrep-grep-command (&optional name alist)
  1600. "Return recursive grep command for current directory or nil.
  1601. If NAME is given, use that without testing.
  1602. Commands are searched from ALIST."
  1603. (if alist
  1604. (if name
  1605. ;; if name is given search that from alist and return the command
  1606. (nth 2 (assoc name
  1607. alist))
  1608. ;; if name is not given try test in 1th elem
  1609. (let ((car (car alist))
  1610. (cdr (cdr alist)))
  1611. (if (eval (nth 1 car))
  1612. ;; if the condition is true return the command
  1613. (nth 2 car)
  1614. ;; try next one
  1615. (and cdr
  1616. (my-rgrep-grep-command name cdr)))))
  1617. ;; if alist is not given set default value
  1618. (my-rgrep-grep-command name my-rgrep-alist)))
  1619. (defun my-rgrep (command-args)
  1620. "My recursive grep. Run COMMAND-ARGS.
  1621. If prefix argument is given, use current symbol as default search target
  1622. and search from projectile root (if projectile is available)."
  1623. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1624. nil)))
  1625. (if cmd
  1626. (list (read-shell-command "grep command: "
  1627. (concat cmd
  1628. (if current-prefix-arg
  1629. (thing-at-point 'symbol t)
  1630. ""))
  1631. 'grep-find-history))
  1632. (error "My-Rgrep: Command for rgrep not found")
  1633. )))
  1634. (if (and current-prefix-arg
  1635. (safe-require-or-eval 'projectile)
  1636. (projectile-project-p))
  1637. (projectile-with-default-dir (projectile-project-root)
  1638. (compilation-start command-args
  1639. 'grep-mode))
  1640. (compilation-start command-args
  1641. 'grep-mode)))
  1642. (defun my-rgrep-thing-at-point-projectile-root ()
  1643. "My recursive grep to find thing at point from project root."
  1644. (interactive)
  1645. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1646. nil))
  1647. (command-args
  1648. (if cmd
  1649. (concat cmd
  1650. (or (thing-at-point 'symbol t)
  1651. (error "No symbol at point")))
  1652. (error "My-Rgrep: Command for rgrep not found"))))
  1653. (if (safe-require-or-eval 'projectile)
  1654. (projectile-with-default-dir (or (projectile-project-root)
  1655. default-directory)
  1656. (compilation-start command-args
  1657. 'grep-mode))
  1658. (compilation-start command-args
  1659. 'grep-mode))))
  1660. (defmacro define-my-rgrep (name)
  1661. "Define rgrep for NAME."
  1662. `(defun ,(intern (concat "my-rgrep-"
  1663. name)) ()
  1664. ,(format "My recursive grep by %s."
  1665. name)
  1666. (interactive)
  1667. (let ((my-rgrep-default ,name))
  1668. (if (called-interactively-p 'any)
  1669. (call-interactively 'my-rgrep)
  1670. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1671. )
  1672. (define-my-rgrep "ack")
  1673. (define-my-rgrep "ag")
  1674. (define-my-rgrep "rg")
  1675. (define-my-rgrep "sift")
  1676. (define-my-rgrep "gitgrep")
  1677. (define-my-rgrep "grep")
  1678. (define-my-rgrep "global")
  1679. (define-key ctl-x-map "s" 'my-rgrep)
  1680. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1681. (defun my-occur (regexp &optional region)
  1682. "My occur command to search REGEXP."
  1683. (interactive (list (read-string "List lines matching regexp: "
  1684. (thing-at-point 'symbol t))))
  1685. (occur regexp nil region))
  1686. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1687. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1688. (defalias 'make 'compile)
  1689. (define-key ctl-x-map "c" 'compile)
  1690. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1691. (define-key ctl-x-map "T" 'git-worktree)
  1692. (define-key ctl-x-map "W" 'git-walktree)
  1693. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1694. ;; editorconfig-auto-apply
  1695. (define-minor-mode editorconfig-auto-apply-mode
  1696. "When saving .editorconfig file update buffer configs."
  1697. :lighter " ECAA"
  1698. (if editorconfig-auto-apply-mode
  1699. (add-hook 'after-save-hook
  1700. 'editorconfig-auto-apply-mode--run nil t)
  1701. (remove-hook 'after-save-hook
  1702. 'editorconfig-auto-apply-mode--run t)))
  1703. (defun editorconfig-auto-apply-enable ()
  1704. "Turn on `editorconfig-auto-apply-mode'."
  1705. (unless editorconfig-auto-apply-mode
  1706. (editorconfig-auto-apply-mode 1)))
  1707. (defun editorconfig-auto-apply-disable ()
  1708. "Turn off `editorconfig-auto-apply-mode'."
  1709. (when editorconfig-auto-apply-mode
  1710. (editorconfig-auto-apply-mode -1)))
  1711. (defun editorconfig-auto-apply-mode--run ()
  1712. "When saving .editorconfig file walk all buffers and update configs."
  1713. (when (eq major-mode
  1714. 'editorconfig-conf-mode)
  1715. (let ((dir (file-name-directory buffer-file-name)))
  1716. (cl-dolist (buf (buffer-list))
  1717. (when (and (buffer-file-name buf)
  1718. (file-in-directory-p (buffer-file-name buf)
  1719. dir))
  1720. (with-current-buffer buf
  1721. (editorconfig-mode-apply)))))))
  1722. (add-hook 'editorconfig-conf-mode-hook
  1723. 'editorconfig-auto-apply-enable)
  1724. ;;;;;;;;;;;;;;;;
  1725. ;; flychcek-black
  1726. ;; TODO: Move to https://github.com/10sr/flycheck-black-check
  1727. (require 'flycheck)
  1728. (flycheck-define-checker python-black-check
  1729. "A Python style checker."
  1730. :command ("python3" "-m" "black"
  1731. "--check"
  1732. (config-file "--config" flycheck-black)
  1733. source)
  1734. :error-parser flycheck-parse-black-check
  1735. ;; :error-patterns
  1736. ;; (
  1737. ;; (error line-start "error: cannot format " (file-name) ": " (message) ": " line ":" column ": " (one-or-more any) line-end)
  1738. ;; (error line-start (message) " " (file-name) line-end)
  1739. ;; )
  1740. :enabled (lambda ()
  1741. (or (not (flycheck-python-needs-module-p 'python-black-check))
  1742. (flycheck-python-find-module 'python-black-check "black")))
  1743. :verify (lambda (_) (flycheck-python-verify-module 'python-black-check "black"))
  1744. :modes python-mode)
  1745. ;; (flycheck-define-checker python-black-diff
  1746. ;; "A Python style checker."
  1747. ;; :command ("python3"
  1748. ;; "-m" "black"
  1749. ;; (config-file "--config" flycheck-black)
  1750. ;; "--diff" source)
  1751. ;; :error-parser my-flycheck-parse-unified-diff
  1752. ;; :enabled (lambda ()
  1753. ;; (or (not (flycheck-python-needs-module-p 'python-black))
  1754. ;; (flycheck-python-find-module 'python-black "black")))
  1755. ;; :verify (lambda (_) (flycheck-python-verify-module 'python-black "black"))
  1756. ;; :modes python-mode)
  1757. (flycheck-def-config-file-var flycheck-black python-black-check "pyproject.toml"
  1758. :safe #'stringp)
  1759. (add-to-list 'flycheck-checkers
  1760. 'python-black-check)
  1761. (defun flycheck-parse-black-check (output checker buffer)
  1762. "Flycheck parser to check if reformat is required."
  1763. (let ((result nil))
  1764. (with-temp-buffer
  1765. (insert output)
  1766. (save-match-data
  1767. (goto-char (point-min))
  1768. (when (re-search-forward "^would reformat .*$" nil t)
  1769. (add-to-list 'result (flycheck-error-new-at
  1770. (point-min)
  1771. nil
  1772. 'error
  1773. ;;(format "Black: %s" (match-string 0))
  1774. "Black: would reformat"
  1775. :buffer buffer
  1776. :checker checker)))
  1777. (goto-char (point-min))
  1778. (when (re-search-forward "^error: .*$" nil t)
  1779. (add-to-list 'result (flycheck-error-new-at
  1780. (point-min)
  1781. nil
  1782. 'error
  1783. ;; Fix not to include absolute file path
  1784. (format "Black: %s" (match-string 0))
  1785. :buffer buffer
  1786. :checker checker)))))
  1787. result))
  1788. (defun my-flycheck-parse-unified-diff (output checker buffer)
  1789. "Flycheck parser to parse diff output."
  1790. (let ((source-line 0)
  1791. (result ())
  1792. (hunk "HUNK"))
  1793. (with-temp-buffer
  1794. (insert output)
  1795. (goto-char (point-min))
  1796. (while (not (eq (point) (point-max)))
  1797. ;; FIXME: Do not stop when no result
  1798. (while (not (re-search-forward "^@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*$" (point-at-eol) t))
  1799. (forward-line 1)
  1800. (goto-char (point-at-bol)))
  1801. (setq source-line
  1802. (string-to-number (match-string 1)))
  1803. ;; TODO: Add filename support
  1804. (setq hunk
  1805. (match-string 0))
  1806. ;;(while (not (rearch-forward "^\\(-\\|\\+\\)")))
  1807. )
  1808. (add-to-list 'result
  1809. (flycheck-error-new-at
  1810. 0
  1811. nil
  1812. 'error
  1813. "MESSAGE"
  1814. :buffer buffer
  1815. :checker checker
  1816. :group hunk)))
  1817. result))
  1818. (set-variable 'flycheck-python-mypy-ini
  1819. ".mypy.ini")
  1820. ;;;;;;;;;;;;;;;;;;;
  1821. ;; peek-file-mode
  1822. (defun peek-file (file)
  1823. "Peek FILE."
  1824. (interactive)
  1825. (message "%s" file))
  1826. ;; Local Variables:
  1827. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1828. ;; flycheck-checker: emacs-lisp
  1829. ;; End:
  1830. ;;; emancs.el ends here