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.
 
 
 
 
 
 

2064 lines
68 KiB

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