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.
 
 
 
 
 
 

2055 lines
68 KiB

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