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.
 
 
 
 
 
 

2109 rivejä
70 KiB

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