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.
 
 
 
 
 
 

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