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.
 
 
 
 
 
 

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