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.
 
 
 
 
 
 

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