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.
 
 
 
 
 
 

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