Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

2086 rader
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. (when (safe-require-or-eval 'amx)
  480. (amx-mode 1)
  481. (defvar amx-map)
  482. (define-key amx-map (kbd "C-h") (kbd "DEL")))
  483. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  484. ;; letters, font-lock mode and fonts
  485. (setq text-quoting-style 'grave)
  486. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  487. ;; (set-window-margins (selected-window) 1 1)
  488. (unless window-system
  489. (setq frame-background-mode 'dark))
  490. (and (or (eq system-type 'Darwin)
  491. (eq system-type 'darwin))
  492. (fboundp 'mac-set-input-method-parameter)
  493. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  494. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  495. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  496. (boundp 'input-method-inactivate-hook))
  497. (add-hook 'input-method-activate-hook
  498. (lambda () (set-cursor-color "red")))
  499. (add-hook 'input-method-inactivate-hook
  500. (lambda () (set-cursor-color "black"))))
  501. (when (safe-require-or-eval 'paren)
  502. (show-paren-mode 1)
  503. (setq show-paren-delay 0.5
  504. show-paren-style 'parenthesis) ; mixed is hard to read
  505. ;; (set-face-background 'show-paren-match
  506. ;; "black")
  507. ;; ;; (face-foreground 'default))
  508. ;; (set-face-foreground 'show-paren-match
  509. ;; "white")
  510. ;; (set-face-inverse-video-p 'show-paren-match
  511. ;; t)
  512. )
  513. (transient-mark-mode 1)
  514. (global-font-lock-mode 1)
  515. (setq font-lock-global-modes
  516. '(not
  517. help-mode
  518. eshell-mode
  519. ;;term-mode
  520. Man-mode
  521. magit-diff-mode
  522. magit-revision-mode))
  523. ;; (standard-display-ascii ?\n "$\n")
  524. ;; (defvar my-eol-face
  525. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  526. ;; )
  527. ;; (defvar my-tab-face
  528. ;; '(("\t" . '(0 highlight t nil))))
  529. (defvar my-jspace-face
  530. '(("\u3000" . '(0 highlight t nil))))
  531. (add-hook 'font-lock-mode-hook
  532. (lambda ()
  533. ;; (font-lock-add-keywords nil my-eol-face)
  534. (font-lock-add-keywords nil my-jspace-face)
  535. ))
  536. (when (safe-require-or-eval 'whitespace)
  537. (add-to-list 'whitespace-display-mappings
  538. ;; We need t since last one takes precedence
  539. `(tab-mark ?\t ,(vconcat "|>\t")) t)
  540. ;; (add-to-list 'whitespace-display-mappings
  541. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  542. (setq whitespace-style '(face
  543. trailing ; trailing blanks
  544. ;; tabs
  545. ;; spaces
  546. ;; lines
  547. lines-tail ; lines over 80
  548. newline ; newlines
  549. empty ; empty lines at beg or end of buffer
  550. ;; big-indent
  551. ;; space-mark
  552. tab-mark
  553. newline-mark ; use display table for newline
  554. ))
  555. ;; (setq whitespace-newline 'font-lock-comment-face)
  556. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  557. (defun my-whitesspace-mode-reload ()
  558. "Reload whitespace-mode config."
  559. (interactive)
  560. (when whitespace-mode
  561. (whitespace-mode 0)
  562. (whitespace-mode 1)))
  563. (set-variable 'whitespace-line-column nil)
  564. (global-whitespace-mode t)
  565. (add-hook 'dired-mode-hook
  566. (lambda ()
  567. (set (make-local-variable 'whitespace-style) nil)))
  568. (if (>= (display-color-cells)
  569. 256)
  570. (set-face-foreground 'whitespace-newline "color-109")
  571. ;; (progn
  572. ;; (set-face-bold-p 'whitespace-newline
  573. ;; t))
  574. ))
  575. (and nil
  576. '(safe-require-or-eval 'fill-column-indicator)
  577. (setq fill-column-indicator))
  578. ;; highlight current line
  579. ;; http://wiki.riywo.com/index.php?Meadow
  580. (face-spec-set 'hl-line
  581. '((((min-colors 256)
  582. (background dark))
  583. (:background "color-234"))
  584. (((min-colors 256)
  585. (background light))
  586. (:background "color-234"))
  587. (t
  588. (:underline "black"))))
  589. (set-variable 'hl-line-global-modes
  590. '(not
  591. term-mode))
  592. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  593. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  594. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  595. ;;(safe-require-or-eval 'set-modeline-color)
  596. ;; (let ((fg (face-foreground 'default))
  597. ;; (bg (face-background 'default)))
  598. ;; (set-face-background 'mode-line-inactive
  599. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  600. ;; (set-face-foreground 'mode-line-inactive
  601. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  602. ;; (set-face-underline 'mode-line-inactive
  603. ;; t)
  604. ;; (set-face-underline 'vertical-border
  605. ;; nil)
  606. ;; (when (safe-require-or-eval 'end-mark)
  607. ;; (global-end-mark-mode))
  608. ;; M-x highlight-* to highlight things
  609. (global-hi-lock-mode 1)
  610. (unless (fboundp 'highlight-region-text)
  611. (defun highlight-region-text (beg end)
  612. "Highlight text between BEG and END."
  613. (interactive "r")
  614. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  615. end)))
  616. (setq deactivate-mark t)))
  617. (when (safe-require-or-eval 'auto-highlight-symbol)
  618. (set-variable 'ahs-idle-interval 0.6)
  619. (global-auto-highlight-symbol-mode 1))
  620. (when (safe-require-or-eval 'highlight-indentation)
  621. (set-face-background 'highlight-indentation-face "color-236")
  622. (dolist (hook
  623. '(
  624. prog-mode-hook
  625. ))
  626. (add-hook hook
  627. 'highlight-indentation-mode)))
  628. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  629. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  630. ;; file handling
  631. ;; fzf
  632. ;; Too slow in term buffer!
  633. ;; (set-variable 'fzf/executable "sk")
  634. ;; (set-variable 'fzf/args "--color bw --print-query")
  635. ;; Modified from hardcoded default to include:
  636. ;; - directories
  637. ;; - hidden files
  638. ;; - root directory (.)
  639. ;; - parent directory (..)
  640. ;; ripgrep cannot list directories...
  641. ;; (setenv "FZF_DEFAULT_COMMAND" "rg --files --hidden --follow --glob '!.git/*' --no-ignore")
  642. (let* ((find (if (executable-find "bfs")
  643. ;; Breadth-first find https://github.com/tavianator/bfs
  644. "bfs"
  645. ;; Use gfind if available?
  646. "find"))
  647. (findcmd (concat "set -eu; set -o pipefail; "
  648. "echo .; "
  649. "echo ..; "
  650. "command " find " -L . "
  651. "-mindepth 1 "
  652. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  653. "-o -print "
  654. "2> /dev/null "
  655. "| "
  656. "cut -b3-"))
  657. (fdcmd (concat "set -eu; set -o pipefail; "
  658. "echo .; "
  659. "echo ..; "
  660. "command fd "
  661. "--follow --hidden --no-ignore "
  662. "--color always "
  663. "2>/dev/null")))
  664. (if (executable-find "fd")
  665. (setenv "FZF_DEFAULT_COMMAND" fdcmd)
  666. (setenv "FZF_DEFAULT_COMMAND" findcmd)))
  667. (set-variable 'fzf/window-height 45)
  668. (set-variable 'fzf/args "--print-query --ansi --color='bg+:-1' --inline-info --cycle")
  669. ;; (set-variable 'fzf/args "--print-query --ansi --inline-info --cycle")
  670. ;; (set-variable 'fzf/args "--print-query --ansi --color=bw --inline-info --cycle")
  671. (defun my-fzf-or-find-file ()
  672. "Call fzf if usable or call find-file."
  673. (declare (interactive-only t))
  674. (interactive)
  675. (if (and (executable-find "fzf")
  676. (fboundp 'fzf)
  677. (not (file-remote-p default-directory)))
  678. (fzf)
  679. (call-interactively 'find-file)))
  680. (define-key ctl-x-map "f" 'my-fzf-or-find-file)
  681. (defun my-fzf-all-lines ()
  682. "Fzf all lines."
  683. (interactive)
  684. (let ((process-environment (cl-copy-list process-environment)))
  685. (setenv "FZF_DEFAULT_COMMAND" "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^")
  686. (fzf)))
  687. (define-key ctl-x-map "S" 'my-fzf-all-lines)
  688. ;; recently
  689. (when (safe-require-or-eval 'recently)
  690. (set-variable 'recently-max 1000)
  691. (recently-mode 1))
  692. (when (safe-require-or-eval 'editorconfig)
  693. (set-variable 'editorconfig-get-properties-function
  694. 'editorconfig-core-get-properties-hash)
  695. (editorconfig-mode 1)
  696. (set-variable 'editorconfig-mode-lighter "")
  697. (when (fboundp 'ws-butler-mode)
  698. (set-variable 'editorconfig-trim-whitespaces-mode
  699. 'ws-butler-mode))
  700. (with-eval-after-load 'org-src
  701. ;; [*.org\[\*Org Src*\[ c \]*\]]
  702. (add-hook 'org-src-mode-hook
  703. 'editorconfig-mode-apply t)))
  704. (when (fboundp 'editorconfig-custom-majormode)
  705. (add-hook 'editorconfig-after-apply-functions
  706. 'editorconfig-custom-majormode))
  707. ;; Add readonly=true to set read-only-mode
  708. (add-hook 'editorconfig-after-apply-functions
  709. (lambda (props)
  710. (let ((r (gethash 'readonly props)))
  711. (when (and (string= r "true")
  712. (not buffer-read-only))
  713. (read-only-mode 1)))))
  714. (add-hook 'editorconfig-hack-properties-functions
  715. '(lambda (props)
  716. (when (derived-mode-p 'makefile-mode)
  717. (puthash 'indent_style "tab" props))))
  718. ;; (when (fboundp 'editorconfig-charset-extras)
  719. ;; (add-hook 'editorconfig-custom-hooks
  720. ;; 'editorconfig-charset-extras))
  721. (setq revert-without-query '(".+"))
  722. ;; save cursor position
  723. (when (safe-require-or-eval 'saveplace)
  724. (setq-default save-place t)
  725. (setq save-place-file (concat user-emacs-directory
  726. "places")))
  727. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  728. (setq make-backup-files t)
  729. (setq vc-make-backup-files t)
  730. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  731. (setq backup-directory-alist
  732. (cons (cons "." (expand-file-name (concat user-emacs-directory
  733. "backup")))
  734. backup-directory-alist))
  735. (setq version-control 't)
  736. (setq delete-old-versions t)
  737. (setq kept-new-versions 20)
  738. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  739. "auto-save/")))
  740. ;; (setq delete-auto-save-files t)
  741. (setq auto-save-visited-interval 8)
  742. (auto-save-visited-mode 1)
  743. (add-to-list 'completion-ignored-extensions ".bak")
  744. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  745. (setq delete-by-moving-to-trash t)
  746. ;; trash-directory "~/.emacs.d/trash")
  747. (add-hook 'after-save-hook
  748. 'executable-make-buffer-file-executable-if-script-p)
  749. (set-variable 'bookmark-default-file
  750. (expand-file-name (concat user-emacs-directory
  751. "bmk")))
  752. (set-variable 'bookmark-save-flag
  753. 1)
  754. (with-eval-after-load 'recentf
  755. (defvar recentf-exclude)
  756. (defvar bookmark-default-file)
  757. (add-to-list 'recentf-exclude
  758. (regexp-quote bookmark-default-file)))
  759. (when (safe-require-or-eval 'smart-revert)
  760. (smart-revert-on))
  761. ;; autosave
  762. ;; auto-save-visited-mode can be used instead?
  763. ;; (when (safe-require-or-eval 'autosave)
  764. ;; (autosave-set 8))
  765. ;; bookmarks
  766. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  767. ;; vc
  768. (set-variable 'vc-handled-backends '(RCS))
  769. (set-variable 'vc-rcs-register-switches "-l")
  770. (set-variable 'vc-rcs-checkin-switches "-l")
  771. (set-variable 'vc-command-messages t)
  772. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  773. ;; share clipboard with x
  774. ;; this page describes this in details, but only these sexps seem to be needed
  775. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  776. (and nil
  777. (not window-system)
  778. (not (eq window-system 'mac))
  779. (getenv "DISPLAY")
  780. (not (equal (getenv "DISPLAY") ""))
  781. (executable-find "xclip")
  782. ;; (< emacs-major-version 24)
  783. '(safe-require-or-eval 'xclip)
  784. nil
  785. (turn-on-xclip))
  786. (and (eq system-type 'darwin)
  787. (safe-require-or-eval 'pasteboard)
  788. (turn-on-pasteboard))
  789. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  790. ;; some modes and hooks
  791. ;; Include some extra modes
  792. (require 'generic-x)
  793. (when (autoload-eval-lazily 'wgrep)
  794. (set-variable 'wgrep-auto-save-buffer t)
  795. (with-eval-after-load 'grep
  796. (defvar grep-mode-map)
  797. (define-key grep-mode-map
  798. "e"
  799. 'wgrep-change-to-wgrep-mode)))
  800. (with-eval-after-load 'remember
  801. (defvar remember-mode-map (make-sparse-keymap))
  802. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  803. (with-eval-after-load 'magit-files
  804. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  805. ;; existing keybindings.
  806. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  807. ;; to disable thie mode here, but do anyway.
  808. ;; See also https://github.com/magit/magit/issues/3517
  809. (global-magit-file-mode -1))
  810. (with-eval-after-load 'magit-section
  811. (set-face-background 'magit-section-highlight
  812. nil))
  813. (with-eval-after-load 'magit-diff
  814. (set-face-background 'magit-diff-added-highlight
  815. nil)
  816. (set-face-background 'magit-diff-removed-highlight
  817. nil)
  818. (set-face-background 'magit-diff-context-highlight
  819. nil)
  820. )
  821. (when (boundp 'git-rebase-filename-regexp)
  822. (add-to-list 'auto-mode-alist
  823. `(,git-rebase-filename-regexp . text-mode)))
  824. (when (safe-require-or-eval 'aggressive-indent)
  825. (defvar aggressive-indent-excluded-modes)
  826. (setq aggressive-indent-excluded-modes
  827. `(diff-mode
  828. toml-mode
  829. conf-mode
  830. dockerfile-mode
  831. groovy-mode
  832. scala-mode
  833. ,@aggressive-indent-excluded-modes))
  834. (global-aggressive-indent-mode 1))
  835. (when (autoload-eval-lazily 'ggtags)
  836. (add-hook 'c-mode-common-hook
  837. (lambda ()
  838. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  839. (ggtags-mode 1))))
  840. (add-hook 'python-mode-hook
  841. (lambda ()
  842. (ggtags-mode 1))))
  843. (when (autoload-eval-lazily 'imenu-list)
  844. ;; (set-variable 'imenu-list-auto-resize t)
  845. (set-variable 'imenu-list-focus-after-activation t)
  846. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  847. (add-hook 'emacs-lisp-mode-hook
  848. (lambda ()
  849. (setq imenu-generic-expression
  850. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  851. ,@imenu-generic-expression))))
  852. (with-eval-after-load 'compile
  853. (defvar compilation-filter-start)
  854. (defvar compilation-error-regexp-alist)
  855. (require 'ansi-color)
  856. (add-hook 'compilation-filter-hook
  857. (lambda ()
  858. (let ((inhibit-read-only t))
  859. (ansi-color-apply-on-region compilation-filter-start
  860. (point)))))
  861. (add-to-list 'compilation-error-regexp-alist
  862. ;; ansible-lint
  863. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  864. ;; Workaround to avoid ensime error
  865. (defvar ensime-mode-key-prefix nil)
  866. (when (safe-require-or-eval 'company)
  867. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  868. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  869. (global-company-mode)
  870. (set-variable 'company-idle-delay nil)
  871. (set-variable 'company-minimum-prefix-length 2)
  872. (set-variable 'company-selection-wrap-around t)
  873. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  874. (defvar company-active-map)
  875. (define-key company-active-map (kbd "C-n") 'company-select-next)
  876. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  877. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  878. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  879. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  880. )
  881. ;; https://github.com/lunaryorn/flycheck
  882. (when (safe-require-or-eval 'flycheck)
  883. (call-after-init 'global-flycheck-mode))
  884. (when (autoload-eval-lazily 'ilookup)
  885. (define-key ctl-x-map "d" 'ilookup-open-word))
  886. (set-variable 'ac-ignore-case nil)
  887. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  888. (define-key ctl-x-map "t" 'term-run-shell-command))
  889. (add-to-list 'safe-local-variable-values
  890. '(encoding utf-8))
  891. (setq enable-local-variables :safe)
  892. ;; Detect file type from shebang and set major-mode.
  893. (add-to-list 'interpreter-mode-alist
  894. '("python3" . python-mode))
  895. (add-to-list 'interpreter-mode-alist
  896. '("python2" . python-mode))
  897. (with-eval-after-load 'python
  898. (defvar python-mode-map (make-sparse-keymap))
  899. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  900. (when (autoload-eval-lazily 'pipenv)
  901. (add-hook 'python-mode-hook
  902. (lambda ()
  903. (pipenv-mode 1)
  904. (pipenv-projectile-after-switch-default)))
  905. )
  906. (set-variable 'flycheck-python-pycompile-executable "python3")
  907. (set-variable 'python-indent-guess-indent-offset nil)
  908. (with-eval-after-load 'blacken
  909. (when (require 'with-venv nil t)
  910. (with-venv-advice-add 'blacken-buffer)))
  911. ;; http://fukuyama.co/foreign-regexp
  912. '(and (safe-require-or-eval 'foreign-regexp)
  913. (progn
  914. (setq foreign-regexp/regexp-type 'perl)
  915. '(setq reb-re-syntax 'foreign-regexp)
  916. ))
  917. (autoload-eval-lazily 'sql '(sql-mode)
  918. (require 'sql-indent nil t))
  919. (add-to-list 'auto-mode-alist
  920. '("\\.hql\\'" . sql-mode))
  921. (when (autoload-eval-lazily 'git-command)
  922. (define-key ctl-x-map "g" 'git-command))
  923. (when (autoload-eval-lazily 'gited)
  924. (define-key ctl-x-map (kbd "C-g") 'gited-list))
  925. (when (safe-require-or-eval 'git-commit)
  926. (global-git-commit-mode 1))
  927. (with-eval-after-load 'git-commit
  928. (add-hook 'git-commit-setup-hook
  929. 'turn-off-auto-fill t))
  930. (autoload-eval-lazily 'sl)
  931. (with-eval-after-load 'rst
  932. (defvar rst-mode-map)
  933. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  934. (with-eval-after-load 'jdee
  935. (add-hook 'jdee-mode-hook
  936. (lambda ()
  937. (make-local-variable 'global-mode-string)
  938. (add-to-list 'global-mode-string
  939. mode-line-position))))
  940. ;; Cannot enable error thrown. Why???
  941. ;; https://github.com/m0smith/malabar-mode#Installation
  942. ;; (when (autoload-eval-lazily 'malabar-mode)
  943. ;; (add-to-list 'load-path
  944. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  945. ;; (safe-require-or-eval 'cedet-devel-load)
  946. ;; (call-after-init 'activate-malabar-mode))
  947. (with-eval-after-load 'make-mode
  948. (defvar makefile-mode-map (make-sparse-keymap))
  949. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  950. ;; this functions is set in write-file-functions, i cannot find any
  951. ;; good way to remove this.
  952. (fset 'makefile-warn-suspicious-lines 'ignore))
  953. (with-eval-after-load 'verilog-mode
  954. (defvar verilog-mode-map (make-sparse-keymap))
  955. (define-key verilog-mode-map ";" 'self-insert-command))
  956. (setq diff-switches "-u")
  957. (with-eval-after-load 'diff-mode
  958. ;; (when (and (eq major-mode
  959. ;; 'diff-mode)
  960. ;; (not buffer-file-name))
  961. ;; ;; do not pass when major-mode is derived mode of diff-mode
  962. ;; (view-mode 1))
  963. (set-face-attribute 'diff-header nil
  964. :foreground nil
  965. :background nil
  966. :weight 'bold)
  967. (set-face-attribute 'diff-file-header nil
  968. :foreground nil
  969. :background nil
  970. :weight 'bold)
  971. (set-face-foreground 'diff-index "blue")
  972. (set-face-attribute 'diff-hunk-header nil
  973. :foreground "cyan"
  974. :weight 'normal)
  975. (set-face-attribute 'diff-context nil
  976. ;; :foreground "white"
  977. :foreground nil
  978. :weight 'normal)
  979. (set-face-foreground 'diff-removed "red")
  980. (set-face-foreground 'diff-added "green")
  981. (set-face-background 'diff-removed nil)
  982. (set-face-background 'diff-added nil)
  983. (set-face-attribute 'diff-changed nil
  984. :foreground "magenta"
  985. :weight 'normal)
  986. (set-face-attribute 'diff-refine-changed nil
  987. :foreground nil
  988. :background nil
  989. :weight 'bold
  990. :inverse-video t)
  991. ;; Annoying !
  992. ;;(diff-auto-refine-mode)
  993. )
  994. ;; (ffap-bindings)
  995. (set-variable 'browse-url-browser-function
  996. 'eww-browse-url)
  997. (set-variable 'sh-here-document-word "__EOC__")
  998. (when (autoload-eval-lazily 'adoc-mode
  999. nil
  1000. (defvar adoc-mode-map (make-sparse-keymap))
  1001. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1002. (setq auto-mode-alist
  1003. `(("\\.adoc\\'" . adoc-mode)
  1004. ("\\.asciidoc\\'" . adoc-mode)
  1005. ,@auto-mode-alist)))
  1006. (with-eval-after-load 'markup-faces
  1007. ;; Is this too match ?
  1008. (set-face-foreground 'markup-meta-face
  1009. "color-245")
  1010. (set-face-foreground 'markup-meta-hide-face
  1011. "color-245")
  1012. )
  1013. ;; TODO: check if this is required
  1014. (when (autoload-eval-lazily 'groovy-mode nil
  1015. (defvar groovy-mode-map (make-sparse-keymap))
  1016. (define-key groovy-mode-map "(" 'self-insert-command)
  1017. (define-key groovy-mode-map ")" 'self-insert-command)
  1018. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1019. )
  1020. (add-to-list 'auto-mode-alist
  1021. '("build\\.gradle\\'" . groovy-mode)))
  1022. (add-to-list 'auto-mode-alist
  1023. '("\\.gawk\\'" . awk-mode))
  1024. (with-eval-after-load 'yaml-mode
  1025. (defvar yaml-mode-map (make-sparse-keymap))
  1026. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1027. (with-eval-after-load 'html-mode
  1028. (defvar html-mode-map (make-sparse-keymap))
  1029. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1030. (with-eval-after-load 'text-mode
  1031. (define-key text-mode-map (kbd "C-m") 'newline))
  1032. (autoload-eval-lazily 'info nil
  1033. (defvar Info-additional-directory-list)
  1034. (dolist (dir (directory-files (concat user-emacs-directory
  1035. "info")
  1036. t
  1037. "^[^.].*"))
  1038. (when (file-directory-p dir)
  1039. (add-to-list 'Info-additional-directory-list
  1040. dir)))
  1041. (let ((dir (expand-file-name "~/.brew/share/info")))
  1042. (when (file-directory-p dir)
  1043. (add-to-list 'Info-additional-directory-list
  1044. dir))))
  1045. (with-eval-after-load 'apropos
  1046. (defvar apropos-mode-map (make-sparse-keymap))
  1047. (define-key apropos-mode-map "n" 'next-line)
  1048. (define-key apropos-mode-map "p" 'previous-line))
  1049. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1050. ;; (define-key isearch-mode-map
  1051. ;; (kbd "C-j") 'isearch-other-control-char)
  1052. ;; (define-key isearch-mode-map
  1053. ;; (kbd "C-k") 'isearch-other-control-char)
  1054. ;; (define-key isearch-mode-map
  1055. ;; (kbd "C-h") 'isearch-other-control-char)
  1056. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1057. (define-key isearch-mode-map (kbd "M-r")
  1058. 'isearch-query-replace-regexp)
  1059. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1060. (setq lazy-highlight-cleanup nil)
  1061. ;; face for isearch highlighing
  1062. (set-face-attribute 'lazy-highlight
  1063. nil
  1064. :foreground `unspecified
  1065. :background `unspecified
  1066. :underline t
  1067. ;; :weight `bold
  1068. )
  1069. (add-hook 'outline-mode-hook
  1070. (lambda ()
  1071. (when (string-match "\\.md\\'" buffer-file-name)
  1072. (set (make-local-variable 'outline-regexp) "#+ "))))
  1073. (add-hook 'outline-mode-hook
  1074. 'outline-show-all)
  1075. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1076. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1077. (when (autoload-eval-lazily 'markdown-mode
  1078. '(markdown-mode gfm-mode)
  1079. (defvar gfm-mode-map (make-sparse-keymap))
  1080. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  1081. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1082. (set-variable 'markdown-command (or (executable-find "markdown")
  1083. (executable-find "markdown.pl")
  1084. ""))
  1085. (add-hook 'markdown-mode-hook
  1086. (lambda ()
  1087. (outline-minor-mode 1)
  1088. (flyspell-mode)
  1089. (set (make-local-variable 'comment-start) ";")))
  1090. )
  1091. ;; c-mode
  1092. ;; http://www.emacswiki.org/emacs/IndentingC
  1093. ;; http://en.wikipedia.org/wiki/Indent_style
  1094. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1095. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1096. (with-eval-after-load 'cc-vars
  1097. (defvar c-default-style nil)
  1098. (add-to-list 'c-default-style
  1099. '(c-mode . "k&r"))
  1100. (add-to-list 'c-default-style
  1101. '(c++-mode . "k&r")))
  1102. (autoload-eval-lazily 'js2-mode nil
  1103. ;; currently do not use js2-mode
  1104. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1105. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1106. ;; (defvar js2-mode-map (make-sparse-keymap))
  1107. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1108. ;; (interactive)
  1109. ;; (js2-enter-key)
  1110. ;; (indent-for-tab-command)))
  1111. ;; (add-hook (kill-local-variable 'before-save-hook)
  1112. ;; 'js2-before-save)
  1113. ;; (add-hook 'before-save-hook
  1114. ;; 'my-indent-buffer
  1115. ;; nil
  1116. ;; t)
  1117. )
  1118. (add-to-list 'interpreter-mode-alist
  1119. '("node" . js-mode))
  1120. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1121. (with-eval-after-load 'uniquify
  1122. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1123. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1124. (setq uniquify-min-dir-content 1))
  1125. (with-eval-after-load 'view
  1126. (defvar view-mode-map (make-sparse-keymap))
  1127. (define-key view-mode-map "j" 'scroll-up-line)
  1128. (define-key view-mode-map "k" 'scroll-down-line)
  1129. (define-key view-mode-map "v" 'toggle-read-only)
  1130. (define-key view-mode-map "q" 'bury-buffer)
  1131. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1132. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1133. ;; (define-key view-mode-map
  1134. ;; "n" 'nonincremental-repeat-search-forward)
  1135. ;; (define-key view-mode-map
  1136. ;; "N" 'nonincremental-repeat-search-backward)
  1137. ;; N conflicts with git-walktree
  1138. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1139. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1140. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1141. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1142. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1143. (global-set-key "\M-r" 'view-mode)
  1144. ;; (setq view-read-only t)
  1145. (with-eval-after-load 'term
  1146. (defvar term-raw-map (make-sparse-keymap))
  1147. (define-key term-raw-map (kbd "C-x")
  1148. (lookup-key (current-global-map)
  1149. (kbd "C-x"))))
  1150. (add-hook 'term-mode-hook
  1151. (lambda ()
  1152. ;; Stop current line highlighting
  1153. (set (make-local-variable (defvar hl-line-range-function))
  1154. (lambda () '(0 . 0)))
  1155. (set (make-local-variable 'scroll-margin)
  1156. 0)
  1157. (set-variable 'term-buffer-maximum-size 20480)
  1158. ))
  1159. (add-hook 'Man-mode-hook
  1160. (lambda ()
  1161. (view-mode 1)
  1162. (setq truncate-lines nil)))
  1163. (set-variable 'Man-notify-method (if window-system
  1164. 'newframe
  1165. 'aggressive))
  1166. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1167. "woman_cache.el")))
  1168. ;; not work because man.el will be loaded when man called
  1169. (defalias 'man 'woman)
  1170. (add-to-list 'auto-mode-alist
  1171. '("tox\\.ini\\'" . conf-unix-mode))
  1172. (when (autoload-eval-lazily 'toml-mode)
  1173. (add-to-list 'auto-mode-alist
  1174. '("/tox\\.ini\\'" . toml-mode))
  1175. (add-to-list 'auto-mode-alist
  1176. '("/Pipfile\\'" . toml-mode))
  1177. (add-to-list 'auto-mode-alist
  1178. '("/poetry\\.lock\\'" . toml-mode))
  1179. )
  1180. (when (autoload-eval-lazily 'json-mode)
  1181. (add-to-list 'auto-mode-alist
  1182. '("/Pipfile\\.lock\\'" . json-mode)))
  1183. (add-hook 'go-mode-hook
  1184. (lambda()
  1185. (defvar go-mode-map)
  1186. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1187. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1188. (when (autoload-eval-lazily 'k8s-mode)
  1189. (add-to-list 'auto-mode-alist
  1190. '("\\.k8s\\'" . k8s-mode)))
  1191. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1192. ;; buffers
  1193. (defvar bs-configurations)
  1194. (when (autoload-eval-lazily 'bs '(bs-show)
  1195. (add-to-list 'bs-configurations
  1196. '("specials" "^\\*" nil ".*" nil nil))
  1197. (add-to-list 'bs-configurations
  1198. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1199. (defvar bs-mode-map)
  1200. (defvar bs-current-configuration)
  1201. (define-key bs-mode-map (kbd "t")
  1202. ;; TODO: fix toggle feature
  1203. (lambda ()
  1204. (interactive)
  1205. (if (string= "specials"
  1206. bs-current-configuration)
  1207. (bs-set-configuration "files")
  1208. (bs-set-configuration "specials"))
  1209. (bs-refresh)
  1210. (bs-message-without-log "%s"
  1211. (bs--current-config-message))))
  1212. ;; (setq bs-configurations (list
  1213. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1214. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1215. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1216. )
  1217. (defalias 'list-buffers 'bs-show)
  1218. (set-variable 'bs-default-configuration "files-and-specials")
  1219. (set-variable 'bs-default-sort-name "by nothing")
  1220. (add-hook 'bs-mode-hook
  1221. (lambda ()
  1222. (set (make-local-variable 'scroll-margin) 0))))
  1223. ;;(iswitchb-mode 1)
  1224. (icomplete-mode)
  1225. (defun iswitchb-buffer-display-other-window ()
  1226. "Do iswitchb in other window."
  1227. (interactive)
  1228. (let ((iswitchb-default-method 'display))
  1229. (call-interactively 'iswitchb-buffer)))
  1230. ;; buffer killing
  1231. ;; (defun my-delete-window-killing-buffer () nil)
  1232. (defun my-query-kill-current-buffer ()
  1233. "Interactively kill current buffer."
  1234. (interactive)
  1235. (if (y-or-n-p (concat "kill current buffer? :"))
  1236. (kill-buffer (current-buffer))))
  1237. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1238. (substitute-key-definition 'kill-buffer
  1239. 'my-query-kill-current-buffer
  1240. global-map)
  1241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1242. ;; recentf-mode
  1243. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1244. "recentf")))
  1245. (set-variable 'recentf-max-menu-items 20)
  1246. (set-variable 'recentf-max-saved-items 30)
  1247. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1248. (set-variable 'recentf-auto-cleanup 3)
  1249. ;; (safe-require-or-eval 'sync-recentf)
  1250. ;; (when (safe-require-or-eval 'recentf)
  1251. ;; (add-to-list 'recentf-exclude
  1252. ;; (regexp-quote recentf-save-file))
  1253. ;; (add-to-list 'recentf-exclude
  1254. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1255. ;; (add-to-list 'recentf-exclude
  1256. ;; "/sync-recentf-marker\\'")
  1257. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1258. ;; (remove-hook 'find-file-hook
  1259. ;; 'recentf-track-opened-file)
  1260. ;; (defun my-recentf-load-track-save-list ()
  1261. ;; "Load current recentf list from file, track current visiting file, then save
  1262. ;; the list."
  1263. ;; (recentf-load-list)
  1264. ;; (recentf-track-opened-file)
  1265. ;; (recentf-save-list))
  1266. ;; (add-hook 'find-file-hook
  1267. ;; 'my-recentf-load-track-save-list)
  1268. ;; (add-hook 'kill-emacs-hook
  1269. ;; 'recentf-load-list)
  1270. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1271. ;; ;; (add-hook 'find-file-hook
  1272. ;; ;; (lambda ()
  1273. ;; ;; (recentf-add-file default-directory)))
  1274. ;; (when (autoload-eval-lazily 'recentf-show)
  1275. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1276. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1277. ;; ;; 'recentf-load-list)
  1278. ;; )
  1279. ;; (recentf-mode 1)
  1280. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1281. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1282. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1283. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1284. ;; (add-hook 'recentf-dialog-mode-hook
  1285. ;; (lambda ()
  1286. ;; ;; (recentf-save-list)
  1287. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1288. ;; ;; 'my-recentf-cd-and-find-file)
  1289. ;; (cd "~/"))))
  1290. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1291. ;; dired
  1292. (defun my-file-head (filename &optional n)
  1293. "Return list of first N lines of file FILENAME."
  1294. ;; TODO: Fix for janapese text
  1295. ;; TODO: Fix for short text
  1296. (let ((num (or n 10))
  1297. (size 100)
  1298. (beg 0)
  1299. (end 0)
  1300. (result '())
  1301. (read -1))
  1302. (with-temp-buffer
  1303. (erase-buffer)
  1304. (while (or (<= (count-lines (point-min)
  1305. (point-max))
  1306. num)
  1307. (eq read 0))
  1308. (setq end (+ beg size))
  1309. (setq read (nth 1 (insert-file-contents-literally filename
  1310. nil
  1311. beg
  1312. end)))
  1313. (goto-char (point-max))
  1314. (setq beg (+ beg size)))
  1315. (goto-char (point-min))
  1316. (while (< (length result) num)
  1317. (let ((start (point)))
  1318. (forward-line 1)
  1319. (setq result
  1320. `(,@result ,(buffer-substring-no-properties start
  1321. (point))))))
  1322. result
  1323. ;; (buffer-substring-no-properties (point-min)
  1324. ;; (progn
  1325. ;; (forward-line num)
  1326. ;; (point)))
  1327. )))
  1328. ;; (apply 'concat (my-file-head "./shrc" 10)
  1329. (defun my-dired-echo-file-head (arg)
  1330. "Echo head of current file.
  1331. ARG is num to show, or defaults to 7."
  1332. (interactive "P")
  1333. (let ((f (dired-get-filename)))
  1334. (message "%s"
  1335. (apply 'concat
  1336. (my-file-head f
  1337. 7)))))
  1338. (defun my-dired-diff ()
  1339. "Show diff of marked file and file of current line."
  1340. (interactive)
  1341. (let ((files (dired-get-marked-files nil nil nil t)))
  1342. (if (eq (car files)
  1343. t)
  1344. (diff (cadr files) (dired-get-filename))
  1345. (message "One file must be marked!"))))
  1346. (defun dired-get-file-info ()
  1347. "Print information of current line file."
  1348. (interactive)
  1349. (let ((f (shell-quote-argument (dired-get-filename t))))
  1350. (if (file-directory-p f)
  1351. (progn
  1352. (message "Calculating disk usage...")
  1353. (shell-command (concat "du -hsD "
  1354. f)))
  1355. (shell-command (concat "file "
  1356. f)))))
  1357. (defun my-dired-scroll-up ()
  1358. "Scroll up."
  1359. (interactive)
  1360. (my-dired-previous-line (- (window-height) 1)))
  1361. (defun my-dired-scroll-down ()
  1362. "Scroll down."
  1363. (interactive)
  1364. (my-dired-next-line (- (window-height) 1)))
  1365. ;; (defun my-dired-forward-line (arg)
  1366. ;; ""
  1367. ;; (interactive "p"))
  1368. (defun my-dired-previous-line (arg)
  1369. "Move ARG lines up."
  1370. (interactive "p")
  1371. (if (> arg 0)
  1372. (progn
  1373. (if (eq (line-number-at-pos)
  1374. 1)
  1375. (goto-char (point-max))
  1376. (forward-line -1))
  1377. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1378. (dired-get-subdir))
  1379. (- arg 1)
  1380. arg)))
  1381. (dired-move-to-filename)))
  1382. (defun my-dired-next-line (arg)
  1383. "Move ARG lines down."
  1384. (interactive "p")
  1385. (if (> arg 0)
  1386. (progn
  1387. (if (eq (point)
  1388. (point-max))
  1389. (goto-char (point-min))
  1390. (forward-line 1))
  1391. (my-dired-next-line (if (or (dired-get-filename nil t)
  1392. (dired-get-subdir))
  1393. (- arg 1)
  1394. arg)))
  1395. (dired-move-to-filename)))
  1396. (defun my-tramp-remote-find-file (f)
  1397. "Open F."
  1398. (interactive (list (read-file-name "My Find File Tramp: "
  1399. "/scp:"
  1400. nil ;; "/scp:"
  1401. (confirm-nonexistent-file-or-buffer))))
  1402. (find-file f))
  1403. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1404. (if (eq window-system 'mac)
  1405. (setq dired-listing-switches "-lhF")
  1406. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1407. )
  1408. (setq dired-listing-switches "-lhF")
  1409. ;; when using dired-find-alternate-file
  1410. ;; reuse current dired buffer for the file to open
  1411. ;; (put 'dired-find-alternate-file 'disabled nil)
  1412. (set-variable 'dired-ls-F-marks-symlinks t)
  1413. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1414. (set-variable 'ls-lisp-dirs-first t)
  1415. (set-variable 'ls-lisp-use-localized-time-format t)
  1416. (set-variable 'ls-lisp-format-time-list
  1417. '("%Y-%m-%d %H:%M"
  1418. "%Y-%m-%d "))
  1419. (set-variable 'dired-dwim-target t)
  1420. (set-variable 'dired-isearch-filenames t)
  1421. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1422. (set-variable 'dired-hide-details-hide-information-lines nil)
  1423. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1424. (set-variable 'dired-recursive-deletes 'always)
  1425. ;; (add-hook 'dired-after-readin-hook
  1426. ;; 'my-replace-nasi-none)
  1427. (with-eval-after-load 'dired
  1428. (safe-require-or-eval 'ls-lisp)
  1429. (defvar dired-mode-map (make-sparse-keymap))
  1430. ;; dired-do-chgrp sometimes cause system hung
  1431. (define-key dired-mode-map "G" 'ignore)
  1432. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1433. (define-key dired-mode-map "i" 'dired-get-file-info)
  1434. ;; (define-key dired-mode-map "f" 'find-file)
  1435. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1436. (define-key dired-mode-map "z" 'fzf)
  1437. (define-key dired-mode-map "!" 'shell-command)
  1438. (define-key dired-mode-map "&" 'async-shell-command)
  1439. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1440. (define-key dired-mode-map "=" 'my-dired-diff)
  1441. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1442. (define-key dired-mode-map "b" 'gtkbm)
  1443. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1444. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1445. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1446. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1447. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1448. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1449. (substitute-key-definition 'dired-next-line
  1450. 'my-dired-next-line
  1451. dired-mode-map)
  1452. (substitute-key-definition 'dired-previous-line
  1453. 'my-dired-previous-line
  1454. dired-mode-map)
  1455. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1456. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1457. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1458. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1459. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1460. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1461. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1462. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1463. (add-hook 'dired-mode-hook
  1464. (lambda ()
  1465. (when (fboundp 'dired-hide-details-mode)
  1466. (dired-hide-details-mode t)
  1467. (local-set-key "l" 'dired-hide-details-mode))
  1468. (let ((file "._Icon\015"))
  1469. (when nil
  1470. '(file-readable-p file)
  1471. (delete-file file)))))
  1472. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack)
  1473. (defvar pack-program-alist)
  1474. (add-to-list 'pack-program-alist
  1475. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf")))
  1476. (set-variable 'pack-silence
  1477. t)
  1478. (with-eval-after-load 'dired
  1479. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1480. (when (autoload-eval-lazily 'dired-list-all-mode)
  1481. (setq dired-listing-switches "-lhF")
  1482. (with-eval-after-load 'dired
  1483. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1484. (when (autoload-eval-lazily 'dired-filter)
  1485. (add-hook 'dired-mode-hook
  1486. 'dired-filter-mode))
  1487. (set-variable 'dired-filter-stack nil)
  1488. ;; Currently disabled in favor of dired-from-git-ls-files
  1489. ;; (define-key ctl-x-map "f" 'find-dired)
  1490. (defvar my-dired-git-ls-files-history
  1491. "History for `my-dired-git-ls-files'." nil)
  1492. (defun my-dired-git-ls-files (arg)
  1493. "Dired from git ls-files."
  1494. (interactive (list
  1495. (read-shell-command "git ls-files: "
  1496. "git ls-files -z ")))
  1497. (pop-to-buffer-same-window
  1498. (dired-noselect `(,default-directory
  1499. ,@(split-string (shell-command-to-string arg)
  1500. "\0" t))
  1501. ""))
  1502. )
  1503. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1504. (with-eval-after-load 'dired
  1505. (defvar dired-mode-map (make-sparse-keymap))
  1506. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1507. ;; (define-minor-mode my-dired-glob-filter)
  1508. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1509. ;; misc funcs
  1510. (when (fboundp 'browse-url-default-macosx-browser)
  1511. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1512. (defalias 'qcalc 'quick-calc)
  1513. (defun memo (&optional dir)
  1514. "Open memo.txt in DIR."
  1515. (interactive)
  1516. (pop-to-buffer (find-file-noselect (concat (if dir
  1517. (file-name-as-directory dir)
  1518. "")
  1519. "memo.txt"))))
  1520. (set (defvar my-rgrep-alist nil
  1521. "Alist of rgrep command.
  1522. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1523. condition to choose COMMAND when evaluated.")
  1524. `(
  1525. ;; ripgrep
  1526. ("rg"
  1527. (executable-find "rg")
  1528. "rg -nH --no-heading --color=always --hidden --glob '!.git/' --smart-case -M 1280 ")
  1529. ;; git grep
  1530. ("gitgrep"
  1531. (eq 0
  1532. (shell-command "git rev-parse --git-dir"))
  1533. "git --no-pager -c color.grep=always grep -nH -e ")
  1534. ;; sift
  1535. ("sift"
  1536. (executable-find "sift")
  1537. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1538. ;; the silver searcher
  1539. ("ag"
  1540. (executable-find "ag")
  1541. "ag --nogroup --nopager --filename ")
  1542. ;; ack
  1543. ("ack"
  1544. (executable-find "ack")
  1545. "ack --nogroup --nopager --with-filename ")
  1546. ;; gnu global
  1547. ("global"
  1548. (and (require 'ggtags nil t)
  1549. (executable-find "global")
  1550. (ggtags-current-project-root))
  1551. "global --result grep ")
  1552. ;; grep
  1553. ("grep"
  1554. t
  1555. ,(concat "find . "
  1556. "-path '*/.git' -prune -o "
  1557. "-path '*/.svn' -prune -o "
  1558. "-type f -print0 | "
  1559. "xargs -0 grep -nH -e "))
  1560. )
  1561. )
  1562. (defvar my-rgrep-default nil
  1563. "Default command name for my-rgrep.")
  1564. (defun my-rgrep-grep-command (&optional name alist)
  1565. "Return recursive grep command for current directory or nil.
  1566. If NAME is given, use that without testing.
  1567. Commands are searched from ALIST."
  1568. (if alist
  1569. (if name
  1570. ;; if name is given search that from alist and return the command
  1571. (nth 2 (assoc name
  1572. alist))
  1573. ;; if name is not given try test in 1th elem
  1574. (let ((car (car alist))
  1575. (cdr (cdr alist)))
  1576. (if (eval (nth 1 car))
  1577. ;; if the condition is true return the command
  1578. (nth 2 car)
  1579. ;; try next one
  1580. (and cdr
  1581. (my-rgrep-grep-command name cdr)))))
  1582. ;; if alist is not given set default value
  1583. (my-rgrep-grep-command name my-rgrep-alist)))
  1584. (defun my-rgrep (command-args)
  1585. "My recursive grep. Run COMMAND-ARGS.
  1586. If prefix argument is given, use current symbol as default search target
  1587. and search from projectile root (if projectile is available)."
  1588. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1589. nil)))
  1590. (if cmd
  1591. (list (read-shell-command "grep command: "
  1592. (concat cmd
  1593. (if current-prefix-arg
  1594. (thing-at-point 'symbol t)
  1595. ""))
  1596. 'grep-find-history))
  1597. (error "My-Rgrep: Command for rgrep not found")
  1598. )))
  1599. (if (and current-prefix-arg
  1600. (safe-require-or-eval 'projectile)
  1601. (projectile-project-p))
  1602. (projectile-with-default-dir (projectile-project-root)
  1603. (compilation-start command-args
  1604. 'grep-mode))
  1605. (compilation-start command-args
  1606. 'grep-mode)))
  1607. (defun my-rgrep-thing-at-point-projectile-root ()
  1608. "My recursive grep to find thing at point from project root."
  1609. (interactive)
  1610. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1611. nil))
  1612. (command-args
  1613. (if cmd
  1614. (concat cmd
  1615. (or (thing-at-point 'symbol t)
  1616. (error "No symbol at point")))
  1617. (error "My-Rgrep: Command for rgrep not found"))))
  1618. (if (safe-require-or-eval 'projectile)
  1619. (projectile-with-default-dir (or (projectile-project-root)
  1620. default-directory)
  1621. (compilation-start command-args
  1622. 'grep-mode))
  1623. (compilation-start command-args
  1624. 'grep-mode))))
  1625. (defmacro define-my-rgrep (name)
  1626. "Define rgrep for NAME."
  1627. `(defun ,(intern (concat "my-rgrep-"
  1628. name)) ()
  1629. ,(format "My recursive grep by %s."
  1630. name)
  1631. (interactive)
  1632. (let ((my-rgrep-default ,name))
  1633. (if (called-interactively-p 'any)
  1634. (call-interactively 'my-rgrep)
  1635. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1636. )
  1637. (define-my-rgrep "ack")
  1638. (define-my-rgrep "ag")
  1639. (define-my-rgrep "rg")
  1640. (define-my-rgrep "sift")
  1641. (define-my-rgrep "gitgrep")
  1642. (define-my-rgrep "grep")
  1643. (define-my-rgrep "global")
  1644. (define-key ctl-x-map "s" 'my-rgrep)
  1645. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1646. (defun my-occur (regexp &optional region)
  1647. "My occur command to search REGEXP."
  1648. (interactive (list (read-string "List lines matching regexp: "
  1649. (thing-at-point 'symbol t))))
  1650. (occur regexp nil region))
  1651. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1652. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1653. (defalias 'make 'compile)
  1654. (define-key ctl-x-map "c" 'compile)
  1655. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1656. ;; editorconfig-auto-apply
  1657. (define-minor-mode editorconfig-auto-apply-mode
  1658. "When saving .editorconfig file update buffer configs."
  1659. :lighter " ECAA"
  1660. (if editorconfig-auto-apply-mode
  1661. (add-hook 'after-save-hook
  1662. 'editorconfig-auto-apply-mode--run nil t)
  1663. (remove-hook 'after-save-hook
  1664. 'editorconfig-auto-apply-mode--run t)))
  1665. (defun editorconfig-auto-apply-enable ()
  1666. "Turn on `editorconfig-auto-apply-mode'."
  1667. (unless editorconfig-auto-apply-mode
  1668. (editorconfig-auto-apply-mode 1)))
  1669. (defun editorconfig-auto-apply-disable ()
  1670. "Turn off `editorconfig-auto-apply-mode'."
  1671. (when editorconfig-auto-apply-mode
  1672. (editorconfig-auto-apply-mode -1)))
  1673. (defun editorconfig-auto-apply-mode--run ()
  1674. "When saving .editorconfig file walk all buffers and update configs."
  1675. (when (eq major-mode
  1676. 'editorconfig-conf-mode)
  1677. (let ((dir (file-name-directory buffer-file-name)))
  1678. (cl-dolist (buf (buffer-list))
  1679. (when (and (buffer-file-name buf)
  1680. (file-in-directory-p (buffer-file-name buf)
  1681. dir))
  1682. (with-current-buffer buf
  1683. (editorconfig-mode-apply)))))))
  1684. (add-hook 'editorconfig-conf-mode-hook
  1685. 'editorconfig-auto-apply-enable)
  1686. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1687. (define-key ctl-x-map "T" 'git-worktree)
  1688. (define-key ctl-x-map "W" 'git-walktree)
  1689. ;;;;;;;;;;;;;;;;
  1690. ;; flychcek-black
  1691. ;; TODO: Move to https://github.com/10sr/flycheck-black-check
  1692. (require 'flycheck)
  1693. (flycheck-define-checker python-black-check
  1694. "A Python style checker."
  1695. :command ("python3" "-m" "black"
  1696. "--check"
  1697. (config-file "--config" flycheck-black)
  1698. source)
  1699. :error-parser flycheck-parse-black-check
  1700. ;; :error-patterns
  1701. ;; (
  1702. ;; (error line-start "error: cannot format " (file-name) ": " (message) ": " line ":" column ": " (one-or-more any) line-end)
  1703. ;; (error line-start (message) " " (file-name) line-end)
  1704. ;; )
  1705. :enabled (lambda ()
  1706. (or (not (flycheck-python-needs-module-p 'python-black-check))
  1707. (flycheck-python-find-module 'python-black-check "black")))
  1708. :verify (lambda (_) (flycheck-python-verify-module 'python-black-check "black"))
  1709. :modes python-mode)
  1710. ;; (flycheck-define-checker python-black-diff
  1711. ;; "A Python style checker."
  1712. ;; :command ("python3"
  1713. ;; "-m" "black"
  1714. ;; (config-file "--config" flycheck-black)
  1715. ;; "--diff" source)
  1716. ;; :error-parser my-flycheck-parse-unified-diff
  1717. ;; :enabled (lambda ()
  1718. ;; (or (not (flycheck-python-needs-module-p 'python-black))
  1719. ;; (flycheck-python-find-module 'python-black "black")))
  1720. ;; :verify (lambda (_) (flycheck-python-verify-module 'python-black "black"))
  1721. ;; :modes python-mode)
  1722. (flycheck-def-config-file-var flycheck-black python-black-check "pyproject.toml"
  1723. :safe #'stringp)
  1724. (add-to-list 'flycheck-checkers
  1725. 'python-black-check)
  1726. (defun flycheck-parse-black-check (output checker buffer)
  1727. "Flycheck parser to check if reformat is required."
  1728. (let ((result nil))
  1729. (with-temp-buffer
  1730. (insert output)
  1731. (save-match-data
  1732. (goto-char (point-min))
  1733. (when (re-search-forward "^would reformat .*$" nil t)
  1734. (add-to-list 'result (flycheck-error-new-at
  1735. (point-min)
  1736. nil
  1737. 'error
  1738. ;;(format "Black: %s" (match-string 0))
  1739. "Black: would reformat"
  1740. :buffer buffer
  1741. :checker checker)))
  1742. (goto-char (point-min))
  1743. (when (re-search-forward "^error: .*$" nil t)
  1744. (add-to-list 'result (flycheck-error-new-at
  1745. (point-min)
  1746. nil
  1747. 'error
  1748. ;; Fix not to include absolute file path
  1749. (format "Black: %s" (match-string 0))
  1750. :buffer buffer
  1751. :checker checker)))))
  1752. result))
  1753. (defun my-flycheck-parse-unified-diff (output checker buffer)
  1754. "Flycheck parser to parse diff output."
  1755. (let ((source-line 0)
  1756. (result ())
  1757. (hunk "HUNK"))
  1758. (with-temp-buffer
  1759. (insert output)
  1760. (goto-char (point-min))
  1761. (while (not (eq (point) (point-max)))
  1762. ;; FIXME: Do not stop when no result
  1763. (while (not (re-search-forward "^@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*$" (point-at-eol) t))
  1764. (forward-line 1)
  1765. (goto-char (point-at-bol)))
  1766. (setq source-line
  1767. (string-to-number (match-string 1)))
  1768. ;; TODO: Add filename support
  1769. (setq hunk
  1770. (match-string 0))
  1771. ;;(while (not (rearch-forward "^\\(-\\|\\+\\)")))
  1772. )
  1773. (add-to-list 'result
  1774. (flycheck-error-new-at
  1775. 0
  1776. nil
  1777. 'error
  1778. "MESSAGE"
  1779. :buffer buffer
  1780. :checker checker
  1781. :group hunk)))
  1782. result))
  1783. (set-variable 'flycheck-python-mypy-ini
  1784. ".mypy.ini")
  1785. ;; Local Variables:
  1786. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1787. ;; flycheck-checker: emacs-lisp
  1788. ;; End:
  1789. ;;; emancs.el ends here