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.
 
 
 
 
 
 

2167 lines
71 KiB

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