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.
 
 
 
 
 
 

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