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.
 
 
 
 
 
 

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