Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

2151 строка
71 KiB

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