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.
 
 
 
 
 
 

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