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.
 
 
 
 
 
 

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