25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

2156 satır
71 KiB

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