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.
 
 
 
 
 
 

2027 lines
67 KiB

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