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.
 
 
 
 
 
 

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