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.
 
 
 
 
 
 

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