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.
 
 
 
 
 
 

2066 lines
68 KiB

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