Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

2089 рядки
69 KiB

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