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.
 
 
 
 
 
 

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