選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

2026 行
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. (when (require 'with-venv nil t)
  816. (with-venv-advice-add 'blacken-buffer)))
  817. ;; http://fukuyama.co/foreign-regexp
  818. '(and (safe-require-or-eval 'foreign-regexp)
  819. (progn
  820. (setq foreign-regexp/regexp-type 'perl)
  821. '(setq reb-re-syntax 'foreign-regexp)
  822. ))
  823. (autoload-eval-lazily 'sql '(sql-mode)
  824. (require 'sql-indent nil t))
  825. (when (autoload-eval-lazily 'git-command)
  826. (define-key ctl-x-map "g" 'git-command))
  827. (when (safe-require-or-eval 'git-commit)
  828. (global-git-commit-mode 1))
  829. (with-eval-after-load 'git-commit
  830. (add-hook 'git-commit-setup-hook
  831. 'turn-off-auto-fill t))
  832. (autoload-eval-lazily 'sl)
  833. (with-eval-after-load 'rst
  834. (defvar rst-mode-map)
  835. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  836. (with-eval-after-load 'jdee
  837. (add-hook 'jdee-mode-hook
  838. (lambda ()
  839. (make-local-variable 'global-mode-string)
  840. (add-to-list 'global-mode-string
  841. mode-line-position))))
  842. ;; Cannot enable error thrown. Why???
  843. ;; https://github.com/m0smith/malabar-mode#Installation
  844. ;; (when (autoload-eval-lazily 'malabar-mode)
  845. ;; (add-to-list 'load-path
  846. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  847. ;; (safe-require-or-eval 'cedet-devel-load)
  848. ;; (call-after-init 'activate-malabar-mode))
  849. (with-eval-after-load 'make-mode
  850. (defvar makefile-mode-map (make-sparse-keymap))
  851. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  852. ;; this functions is set in write-file-functions, i cannot find any
  853. ;; good way to remove this.
  854. (fset 'makefile-warn-suspicious-lines 'ignore))
  855. (with-eval-after-load 'verilog-mode
  856. (defvar verilog-mode-map (make-sparse-keymap))
  857. (define-key verilog-mode-map ";" 'self-insert-command))
  858. (setq diff-switches "-u")
  859. (with-eval-after-load 'diff-mode
  860. ;; (when (and (eq major-mode
  861. ;; 'diff-mode)
  862. ;; (not buffer-file-name))
  863. ;; ;; do not pass when major-mode is derived mode of diff-mode
  864. ;; (view-mode 1))
  865. (set-face-attribute 'diff-header nil
  866. :foreground nil
  867. :background nil
  868. :weight 'bold)
  869. (set-face-attribute 'diff-file-header nil
  870. :foreground nil
  871. :background nil
  872. :weight 'bold)
  873. (set-face-foreground 'diff-index "blue")
  874. (set-face-attribute 'diff-hunk-header nil
  875. :foreground "cyan"
  876. :weight 'normal)
  877. (set-face-attribute 'diff-context nil
  878. ;; :foreground "white"
  879. :foreground nil
  880. :weight 'normal)
  881. (set-face-foreground 'diff-removed "red")
  882. (set-face-foreground 'diff-added "green")
  883. (set-face-background 'diff-removed nil)
  884. (set-face-background 'diff-added nil)
  885. (set-face-attribute 'diff-changed nil
  886. :foreground "magenta"
  887. :weight 'normal)
  888. (set-face-attribute 'diff-refine-changed nil
  889. :foreground nil
  890. :background nil
  891. :weight 'bold
  892. :inverse-video t)
  893. ;; Annoying !
  894. ;;(diff-auto-refine-mode)
  895. )
  896. ;; (ffap-bindings)
  897. (set-variable 'browse-url-browser-function
  898. 'eww-browse-url)
  899. (set-variable 'sh-here-document-word "__EOC__")
  900. (when (autoload-eval-lazily 'adoc-mode
  901. nil
  902. (defvar adoc-mode-map (make-sparse-keymap))
  903. (define-key adoc-mode-map (kbd "C-m") 'newline))
  904. (setq auto-mode-alist
  905. `(("\\.adoc\\'" . adoc-mode)
  906. ("\\.asciidoc\\'" . adoc-mode)
  907. ,@auto-mode-alist)))
  908. (with-eval-after-load 'markup-faces
  909. ;; Is this too match ?
  910. (set-face-foreground 'markup-meta-face
  911. "color-245")
  912. (set-face-foreground 'markup-meta-hide-face
  913. "color-245")
  914. )
  915. ;; TODO: check if this is required
  916. (when (autoload-eval-lazily 'groovy-mode nil
  917. (defvar groovy-mode-map (make-sparse-keymap))
  918. (define-key groovy-mode-map "(" 'self-insert-command)
  919. (define-key groovy-mode-map ")" 'self-insert-command)
  920. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  921. )
  922. (add-to-list 'auto-mode-alist
  923. '("build\\.gradle\\'" . groovy-mode)))
  924. (add-to-list 'auto-mode-alist
  925. '("\\.gawk\\'" . awk-mode))
  926. (with-eval-after-load 'yaml-mode
  927. (defvar yaml-mode-map (make-sparse-keymap))
  928. (define-key yaml-mode-map (kbd "C-m") 'newline))
  929. (with-eval-after-load 'html-mode
  930. (defvar html-mode-map (make-sparse-keymap))
  931. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  932. (with-eval-after-load 'text-mode
  933. (define-key text-mode-map (kbd "C-m") 'newline))
  934. (autoload-eval-lazily 'info nil
  935. (defvar Info-additional-directory-list)
  936. (dolist (dir (directory-files (concat user-emacs-directory
  937. "info")
  938. t
  939. "^[^.].*"))
  940. (when (file-directory-p dir)
  941. (add-to-list 'Info-additional-directory-list
  942. dir)))
  943. (let ((dir (expand-file-name "~/.brew/share/info")))
  944. (when (file-directory-p dir)
  945. (add-to-list 'Info-additional-directory-list
  946. dir))))
  947. (with-eval-after-load 'apropos
  948. (defvar apropos-mode-map (make-sparse-keymap))
  949. (define-key apropos-mode-map "n" 'next-line)
  950. (define-key apropos-mode-map "p" 'previous-line))
  951. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  952. ;; (define-key isearch-mode-map
  953. ;; (kbd "C-j") 'isearch-other-control-char)
  954. ;; (define-key isearch-mode-map
  955. ;; (kbd "C-k") 'isearch-other-control-char)
  956. ;; (define-key isearch-mode-map
  957. ;; (kbd "C-h") 'isearch-other-control-char)
  958. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  959. (define-key isearch-mode-map (kbd "M-r")
  960. 'isearch-query-replace-regexp)
  961. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  962. (setq lazy-highlight-cleanup nil)
  963. ;; face for isearch highlighing
  964. (set-face-attribute 'lazy-highlight
  965. nil
  966. :foreground `unspecified
  967. :background `unspecified
  968. :underline t
  969. ;; :weight `bold
  970. )
  971. (add-hook 'outline-mode-hook
  972. (lambda ()
  973. (when (string-match "\\.md\\'" buffer-file-name)
  974. (set (make-local-variable 'outline-regexp) "#+ "))))
  975. (add-hook 'outline-mode-hook
  976. 'outline-show-all)
  977. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  978. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  979. (when (autoload-eval-lazily 'markdown-mode
  980. '(markdown-mode gfm-mode)
  981. (defvar gfm-mode-map (make-sparse-keymap))
  982. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  983. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  984. (set-variable 'markdown-command (or (executable-find "markdown")
  985. (executable-find "markdown.pl")
  986. ""))
  987. (add-hook 'markdown-mode-hook
  988. (lambda ()
  989. (outline-minor-mode 1)
  990. (flyspell-mode)
  991. (set (make-local-variable 'comment-start) ";")))
  992. )
  993. ;; c-mode
  994. ;; http://www.emacswiki.org/emacs/IndentingC
  995. ;; http://en.wikipedia.org/wiki/Indent_style
  996. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  997. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  998. (with-eval-after-load 'cc-vars
  999. (defvar c-default-style nil)
  1000. (add-to-list 'c-default-style
  1001. '(c-mode . "k&r"))
  1002. (add-to-list 'c-default-style
  1003. '(c++-mode . "k&r")))
  1004. (autoload-eval-lazily 'js2-mode nil
  1005. ;; currently do not use js2-mode
  1006. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1007. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1008. ;; (defvar js2-mode-map (make-sparse-keymap))
  1009. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1010. ;; (interactive)
  1011. ;; (js2-enter-key)
  1012. ;; (indent-for-tab-command)))
  1013. ;; (add-hook (kill-local-variable 'before-save-hook)
  1014. ;; 'js2-before-save)
  1015. ;; (add-hook 'before-save-hook
  1016. ;; 'my-indent-buffer
  1017. ;; nil
  1018. ;; t)
  1019. )
  1020. (add-to-list 'interpreter-mode-alist
  1021. '("node" . js-mode))
  1022. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1023. (with-eval-after-load 'uniquify
  1024. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1025. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1026. (setq uniquify-min-dir-content 1))
  1027. (with-eval-after-load 'view
  1028. (defvar view-mode-map (make-sparse-keymap))
  1029. (define-key view-mode-map "j" 'scroll-up-line)
  1030. (define-key view-mode-map "k" 'scroll-down-line)
  1031. (define-key view-mode-map "v" 'toggle-read-only)
  1032. (define-key view-mode-map "q" 'bury-buffer)
  1033. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1034. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1035. ;; (define-key view-mode-map
  1036. ;; "n" 'nonincremental-repeat-search-forward)
  1037. ;; (define-key view-mode-map
  1038. ;; "N" 'nonincremental-repeat-search-backward)
  1039. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1040. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1041. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1042. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1043. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1044. (global-set-key "\M-r" 'view-mode)
  1045. ;; (setq view-read-only t)
  1046. (with-eval-after-load 'term
  1047. (defvar term-raw-map (make-sparse-keymap))
  1048. (define-key term-raw-map (kbd "C-x")
  1049. (lookup-key (current-global-map)
  1050. (kbd "C-x"))))
  1051. (add-hook 'term-mode-hook
  1052. (lambda ()
  1053. ;; Stop current line highlighting
  1054. (set (make-local-variable (defvar hl-line-range-function))
  1055. (lambda () '(0 . 0)))
  1056. (set (make-local-variable 'scroll-margin)
  1057. 0)))
  1058. (add-hook 'Man-mode-hook
  1059. (lambda ()
  1060. (view-mode 1)
  1061. (setq truncate-lines nil)))
  1062. (set-variable 'Man-notify-method (if window-system
  1063. 'newframe
  1064. 'aggressive))
  1065. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1066. "woman_cache.el")))
  1067. ;; not work because man.el will be loaded when man called
  1068. (defalias 'man 'woman)
  1069. (add-to-list 'auto-mode-alist
  1070. '("tox\\.ini\\'" . conf-unix-mode))
  1071. (when (autoload-eval-lazily 'toml-mode)
  1072. (add-to-list 'auto-mode-alist
  1073. '("/tox\\.ini\\'" . toml-mode))
  1074. (add-to-list 'auto-mode-alist
  1075. '("/Pipfile\\'" . toml-mode))
  1076. (add-to-list 'auto-mode-alist
  1077. '("/poetry\\.lock\\'" . toml-mode))
  1078. )
  1079. (when (autoload-eval-lazily 'json-mode)
  1080. (add-to-list 'auto-mode-alist
  1081. '("/Pipfile\\.lock\\'" . json-mode)))
  1082. (add-hook 'go-mode-hook
  1083. (lambda()
  1084. (defvar go-mode-map)
  1085. (add-hook 'before-save-hook' 'gofmt-before-save)
  1086. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1087. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1088. ;; buffers
  1089. (defvar bs-configurations)
  1090. (when (autoload-eval-lazily 'bs '(bs-show)
  1091. (add-to-list 'bs-configurations
  1092. '("specials" "^\\*" nil ".*" nil nil))
  1093. (add-to-list 'bs-configurations
  1094. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1095. (defvar bs-mode-map)
  1096. (defvar bs-current-configuration)
  1097. (define-key bs-mode-map (kbd "t")
  1098. ;; TODO: fix toggle feature
  1099. (lambda ()
  1100. (interactive)
  1101. (if (string= "specials"
  1102. bs-current-configuration)
  1103. (bs-set-configuration "files")
  1104. (bs-set-configuration "specials"))
  1105. (bs-refresh)
  1106. (bs-message-without-log "%s"
  1107. (bs--current-config-message))))
  1108. ;; (setq bs-configurations (list
  1109. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1110. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1111. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1112. )
  1113. (defalias 'list-buffers 'bs-show)
  1114. (set-variable 'bs-default-configuration "files-and-specials")
  1115. (set-variable 'bs-default-sort-name "by nothing")
  1116. (add-hook 'bs-mode-hook
  1117. (lambda ()
  1118. (set (make-local-variable 'scroll-margin) 0))))
  1119. ;;(iswitchb-mode 1)
  1120. (icomplete-mode)
  1121. (defun iswitchb-buffer-display-other-window ()
  1122. "Do iswitchb in other window."
  1123. (interactive)
  1124. (let ((iswitchb-default-method 'display))
  1125. (call-interactively 'iswitchb-buffer)))
  1126. ;; buffer killing
  1127. ;; (defun my-delete-window-killing-buffer () nil)
  1128. (defun my-query-kill-current-buffer ()
  1129. "Interactively kill current buffer."
  1130. (interactive)
  1131. (if (y-or-n-p (concat "kill current buffer? :"))
  1132. (kill-buffer (current-buffer))))
  1133. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1134. (substitute-key-definition 'kill-buffer
  1135. 'my-query-kill-current-buffer
  1136. global-map)
  1137. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1138. ;; recentf-mode
  1139. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1140. "recentf")))
  1141. (set-variable 'recentf-max-menu-items 20)
  1142. (set-variable 'recentf-max-saved-items 30)
  1143. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1144. (set-variable 'recentf-auto-cleanup 3)
  1145. ;; (safe-require-or-eval 'sync-recentf)
  1146. ;; (when (safe-require-or-eval 'recentf)
  1147. ;; (add-to-list 'recentf-exclude
  1148. ;; (regexp-quote recentf-save-file))
  1149. ;; (add-to-list 'recentf-exclude
  1150. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1151. ;; (add-to-list 'recentf-exclude
  1152. ;; "/sync-recentf-marker\\'")
  1153. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1154. ;; (remove-hook 'find-file-hook
  1155. ;; 'recentf-track-opened-file)
  1156. ;; (defun my-recentf-load-track-save-list ()
  1157. ;; "Load current recentf list from file, track current visiting file, then save
  1158. ;; the list."
  1159. ;; (recentf-load-list)
  1160. ;; (recentf-track-opened-file)
  1161. ;; (recentf-save-list))
  1162. ;; (add-hook 'find-file-hook
  1163. ;; 'my-recentf-load-track-save-list)
  1164. ;; (add-hook 'kill-emacs-hook
  1165. ;; 'recentf-load-list)
  1166. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1167. ;; ;; (add-hook 'find-file-hook
  1168. ;; ;; (lambda ()
  1169. ;; ;; (recentf-add-file default-directory)))
  1170. ;; (when (autoload-eval-lazily 'recentf-show)
  1171. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1172. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1173. ;; ;; 'recentf-load-list)
  1174. ;; )
  1175. ;; (recentf-mode 1)
  1176. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1177. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1178. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1179. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1180. ;; (add-hook 'recentf-dialog-mode-hook
  1181. ;; (lambda ()
  1182. ;; ;; (recentf-save-list)
  1183. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1184. ;; ;; 'my-recentf-cd-and-find-file)
  1185. ;; (cd "~/"))))
  1186. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1187. ;; dired
  1188. (defun my-file-head (filename &optional n)
  1189. "Return list of first N lines of file FILENAME."
  1190. ;; TODO: Fix for janapese text
  1191. ;; TODO: Fix for short text
  1192. (let ((num (or n 10))
  1193. (size 100)
  1194. (beg 0)
  1195. (end 0)
  1196. (result '())
  1197. (read -1))
  1198. (with-temp-buffer
  1199. (erase-buffer)
  1200. (while (or (<= (count-lines (point-min)
  1201. (point-max))
  1202. num)
  1203. (eq read 0))
  1204. (setq end (+ beg size))
  1205. (setq read (nth 1 (insert-file-contents-literally filename
  1206. nil
  1207. beg
  1208. end)))
  1209. (goto-char (point-max))
  1210. (setq beg (+ beg size)))
  1211. (goto-char (point-min))
  1212. (while (< (length result) num)
  1213. (let ((start (point)))
  1214. (forward-line 1)
  1215. (setq result
  1216. `(,@result ,(buffer-substring-no-properties start
  1217. (point))))))
  1218. result
  1219. ;; (buffer-substring-no-properties (point-min)
  1220. ;; (progn
  1221. ;; (forward-line num)
  1222. ;; (point)))
  1223. )))
  1224. ;; (apply 'concat (my-file-head "./shrc" 10)
  1225. (defun my-dired-echo-file-head (arg)
  1226. "Echo head of current file.
  1227. ARG is num to show, or defaults to 7."
  1228. (interactive "P")
  1229. (let ((f (dired-get-filename)))
  1230. (message "%s"
  1231. (apply 'concat
  1232. (my-file-head f
  1233. 7)))))
  1234. (defun my-dired-diff ()
  1235. "Show diff of marked file and file of current line."
  1236. (interactive)
  1237. (let ((files (dired-get-marked-files nil nil nil t)))
  1238. (if (eq (car files)
  1239. t)
  1240. (diff (cadr files) (dired-get-filename))
  1241. (message "One file must be marked!"))))
  1242. (defun dired-get-file-info ()
  1243. "Print information of current line file."
  1244. (interactive)
  1245. (let ((f (shell-quote-argument (dired-get-filename t))))
  1246. (if (file-directory-p f)
  1247. (progn
  1248. (message "Calculating disk usage...")
  1249. (shell-command (concat "du -hsD "
  1250. f)))
  1251. (shell-command (concat "file "
  1252. f)))))
  1253. (defun my-dired-scroll-up ()
  1254. "Scroll up."
  1255. (interactive)
  1256. (my-dired-previous-line (- (window-height) 1)))
  1257. (defun my-dired-scroll-down ()
  1258. "Scroll down."
  1259. (interactive)
  1260. (my-dired-next-line (- (window-height) 1)))
  1261. ;; (defun my-dired-forward-line (arg)
  1262. ;; ""
  1263. ;; (interactive "p"))
  1264. (defun my-dired-previous-line (arg)
  1265. "Move ARG lines up."
  1266. (interactive "p")
  1267. (if (> arg 0)
  1268. (progn
  1269. (if (eq (line-number-at-pos)
  1270. 1)
  1271. (goto-char (point-max))
  1272. (forward-line -1))
  1273. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1274. (dired-get-subdir))
  1275. (- arg 1)
  1276. arg)))
  1277. (dired-move-to-filename)))
  1278. (defun my-dired-next-line (arg)
  1279. "Move ARG lines down."
  1280. (interactive "p")
  1281. (if (> arg 0)
  1282. (progn
  1283. (if (eq (point)
  1284. (point-max))
  1285. (goto-char (point-min))
  1286. (forward-line 1))
  1287. (my-dired-next-line (if (or (dired-get-filename nil t)
  1288. (dired-get-subdir))
  1289. (- arg 1)
  1290. arg)))
  1291. (dired-move-to-filename)))
  1292. (defun my-tramp-remote-find-file (f)
  1293. "Open F."
  1294. (interactive (list (read-file-name "My Find File Tramp: "
  1295. "/scp:"
  1296. nil ;; "/scp:"
  1297. (confirm-nonexistent-file-or-buffer))))
  1298. (find-file f))
  1299. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1300. (if (eq window-system 'mac)
  1301. (setq dired-listing-switches "-lhF")
  1302. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1303. )
  1304. (setq dired-listing-switches "-lhF")
  1305. ;; when using dired-find-alternate-file
  1306. ;; reuse current dired buffer for the file to open
  1307. ;; (put 'dired-find-alternate-file 'disabled nil)
  1308. (set-variable 'dired-ls-F-marks-symlinks t)
  1309. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1310. (set-variable 'ls-lisp-dirs-first t)
  1311. (set-variable 'ls-lisp-use-localized-time-format t)
  1312. (set-variable 'ls-lisp-format-time-list
  1313. '("%Y-%m-%d %H:%M"
  1314. "%Y-%m-%d "))
  1315. (set-variable 'dired-dwim-target t)
  1316. (set-variable 'dired-isearch-filenames t)
  1317. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1318. (set-variable 'dired-hide-details-hide-information-lines nil)
  1319. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1320. (set-variable 'dired-recursive-deletes 'always)
  1321. ;; (add-hook 'dired-after-readin-hook
  1322. ;; 'my-replace-nasi-none)
  1323. (with-eval-after-load 'dired
  1324. (safe-require-or-eval 'ls-lisp)
  1325. (defvar dired-mode-map (make-sparse-keymap))
  1326. ;; dired-do-chgrp sometimes cause system hung
  1327. (define-key dired-mode-map "G" 'ignore)
  1328. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1329. (define-key dired-mode-map "i" 'dired-get-file-info)
  1330. (define-key dired-mode-map "f" 'find-file)
  1331. (define-key dired-mode-map "!" 'shell-command)
  1332. (define-key dired-mode-map "&" 'async-shell-command)
  1333. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1334. (define-key dired-mode-map "=" 'my-dired-diff)
  1335. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1336. (define-key dired-mode-map "b" 'gtkbm)
  1337. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1338. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1339. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1340. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1341. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1342. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1343. (substitute-key-definition 'dired-next-line
  1344. 'my-dired-next-line
  1345. dired-mode-map)
  1346. (substitute-key-definition 'dired-previous-line
  1347. 'my-dired-previous-line
  1348. dired-mode-map)
  1349. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1350. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1351. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1352. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1353. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1354. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1355. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1356. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1357. (add-hook 'dired-mode-hook
  1358. (lambda ()
  1359. (when (fboundp 'dired-hide-details-mode)
  1360. (dired-hide-details-mode t)
  1361. (local-set-key "l" 'dired-hide-details-mode))
  1362. (let ((file "._Icon\015"))
  1363. (when nil
  1364. '(file-readable-p file)
  1365. (delete-file file)))))
  1366. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1367. (with-eval-after-load 'dired
  1368. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1369. (when (autoload-eval-lazily 'dired-list-all-mode)
  1370. (setq dired-listing-switches "-lhF")
  1371. (with-eval-after-load 'dired
  1372. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1373. (when (autoload-eval-lazily 'dired-filter)
  1374. (add-hook 'dired-mode-hook
  1375. 'dired-filter-mode))
  1376. (set-variable 'dired-filter-stack nil)
  1377. ;; Currently disabled in favor of dired-from-git-ls-files
  1378. ;; (define-key ctl-x-map "f" 'find-dired)
  1379. ;; It works!
  1380. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1381. (defun my-dired-git-ls-files (args)
  1382. "Dired from git ls-files."
  1383. (interactive "sgit ls-files args: ")
  1384. (pop-to-buffer-same-window
  1385. (dired-noselect `(,default-directory
  1386. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1387. "\0" t))
  1388. ""))
  1389. )
  1390. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1391. (with-eval-after-load 'dired
  1392. (defvar dired-mode-map (make-sparse-keymap))
  1393. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1394. ;; (define-minor-mode my-dired-glob-filter)
  1395. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1396. ;; misc funcs
  1397. (defalias 'qcalc 'quick-calc)
  1398. (defun memo (&optional dir)
  1399. "Open memo.txt in DIR."
  1400. (interactive)
  1401. (pop-to-buffer (find-file-noselect (concat (if dir
  1402. (file-name-as-directory dir)
  1403. "")
  1404. "memo.txt"))))
  1405. (set (defvar my-rgrep-alist nil
  1406. "Alist of rgrep command.
  1407. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1408. condition to choose COMMAND when evaluated.")
  1409. `(
  1410. ;; ripgrep
  1411. ("rg"
  1412. (executable-find "rg")
  1413. "rg --hidden --no-heading --smart-case ")
  1414. ;; git grep
  1415. ("gitgrep"
  1416. (eq 0
  1417. (shell-command "git rev-parse --git-dir"))
  1418. "git --no-pager -c color.grep=always grep -nH -e ")
  1419. ;; sift
  1420. ("sift"
  1421. (executable-find "sift")
  1422. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1423. ;; the silver searcher
  1424. ("ag"
  1425. (executable-find "ag")
  1426. "ag --nogroup --nopager --filename ")
  1427. ;; ack
  1428. ("ack"
  1429. (executable-find "ack")
  1430. "ack --nogroup --nopager --with-filename ")
  1431. ;; gnu global
  1432. ("global"
  1433. (and (require 'ggtags nil t)
  1434. (executable-find "global")
  1435. (ggtags-current-project-root))
  1436. "global --result grep ")
  1437. ;; grep
  1438. ("grep"
  1439. t
  1440. ,(concat "find . "
  1441. "-path '*/.git' -prune -o "
  1442. "-path '*/.svn' -prune -o "
  1443. "-type f -print0 | "
  1444. "xargs -0 grep -nH -e "))
  1445. )
  1446. )
  1447. (defvar my-rgrep-default nil
  1448. "Default command name for my-rgrep.")
  1449. (defun my-rgrep-grep-command (&optional name alist)
  1450. "Return recursive grep command for current directory or nil.
  1451. If NAME is given, use that without testing.
  1452. Commands are searched from ALIST."
  1453. (if alist
  1454. (if name
  1455. ;; if name is given search that from alist and return the command
  1456. (nth 2 (assoc name
  1457. alist))
  1458. ;; if name is not given try test in 1th elem
  1459. (let ((car (car alist))
  1460. (cdr (cdr alist)))
  1461. (if (eval (nth 1 car))
  1462. ;; if the condition is true return the command
  1463. (nth 2 car)
  1464. ;; try next one
  1465. (and cdr
  1466. (my-rgrep-grep-command name cdr)))))
  1467. ;; if alist is not given set default value
  1468. (my-rgrep-grep-command name my-rgrep-alist)))
  1469. (defun my-rgrep (command-args)
  1470. "My recursive grep. Run COMMAND-ARGS.
  1471. If prefix argument is given, use current symbol as default search target
  1472. and search from projectile root (if projectile is available)."
  1473. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1474. nil)))
  1475. (if cmd
  1476. (list (read-shell-command "grep command: "
  1477. (concat cmd
  1478. (if current-prefix-arg
  1479. (thing-at-point 'symbol t)
  1480. ""))
  1481. 'grep-find-history))
  1482. (error "My-Rgrep: Command for rgrep not found")
  1483. )))
  1484. (if (and current-prefix-arg
  1485. (safe-require-or-eval 'projectile)
  1486. (projectile-project-p))
  1487. (projectile-with-default-dir (projectile-project-root)
  1488. (compilation-start command-args
  1489. 'grep-mode))
  1490. (compilation-start command-args
  1491. 'grep-mode)))
  1492. (defun my-rgrep-thing-at-point-projectile-root ()
  1493. "My recursive grep to find thing at point from project root."
  1494. (interactive)
  1495. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1496. nil))
  1497. (command-args
  1498. (if cmd
  1499. (concat cmd
  1500. (or (thing-at-point 'symbol t)
  1501. (error "No symbol at point")))
  1502. (error "My-Rgrep: Command for rgrep not found"))))
  1503. (if (safe-require-or-eval 'projectile)
  1504. (projectile-with-default-dir (or (projectile-project-root)
  1505. default-directory)
  1506. (compilation-start command-args
  1507. 'grep-mode))
  1508. (compilation-start command-args
  1509. 'grep-mode))))
  1510. (defmacro define-my-rgrep (name)
  1511. "Define rgrep for NAME."
  1512. `(defun ,(intern (concat "my-rgrep-"
  1513. name)) ()
  1514. ,(format "My recursive grep by %s."
  1515. name)
  1516. (interactive)
  1517. (let ((my-rgrep-default ,name))
  1518. (if (called-interactively-p 'any)
  1519. (call-interactively 'my-rgrep)
  1520. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1521. )
  1522. (define-my-rgrep "ack")
  1523. (define-my-rgrep "ag")
  1524. (define-my-rgrep "rg")
  1525. (define-my-rgrep "sift")
  1526. (define-my-rgrep "gitgrep")
  1527. (define-my-rgrep "grep")
  1528. (define-my-rgrep "global")
  1529. (define-key ctl-x-map "s" 'my-rgrep)
  1530. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1531. (defun my-occur (regexp &optional region)
  1532. "My occur command to search REGEXP."
  1533. (interactive (list (read-string "List lines matching regexp: "
  1534. (thing-at-point 'symbol t))))
  1535. (occur regexp nil region))
  1536. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1537. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1538. (defalias 'make 'compile)
  1539. (define-key ctl-x-map "c" 'compile)
  1540. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1541. ;; editorconfig-auto-apply
  1542. (define-minor-mode editorconfig-auto-apply-mode
  1543. "When saving .editorconfig file update buffer configs."
  1544. :global t
  1545. :lighter ""
  1546. (if editorconfig-auto-apply-mode
  1547. (add-hook 'after-save-hook
  1548. 'editorconfig-auto-apply-mode--run)
  1549. (remove-hook 'after-save-hook
  1550. 'editorconfig-auto-apply-mode--run)))
  1551. (defun editorconfig-auto-apply-mode--run ()
  1552. "When saving .editorconfig file walk all buffers and update configs."
  1553. (when (eq major-mode
  1554. 'editorconfig-conf-mode)
  1555. (let ((dir (file-name-directory buffer-file-name)))
  1556. (cl-dolist (buf (buffer-list))
  1557. (when (and (buffer-file-name buf)
  1558. (file-in-directory-p (buffer-file-name buf)
  1559. dir))
  1560. (with-current-buffer buf
  1561. (editorconfig-mode-apply)))))))
  1562. (editorconfig-auto-apply-mode 1)
  1563. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1564. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1565. ;; git-worktree
  1566. (defun git-worktree-get-current-trees ()
  1567. "Get current worktree list."
  1568. (with-temp-buffer
  1569. (git-worktree--call-process "worktree" "list" "--porcelain")
  1570. (goto-char (point-min))
  1571. (let ((trees nil))
  1572. (save-match-data
  1573. (while (not (eq (point) (point-max)))
  1574. (let ((worktree nil)
  1575. (head nil)
  1576. (branch nil))
  1577. (while (re-search-forward "^\\([^ ]+\\) \\(.*\\)$" (point-at-eol) t)
  1578. (pcase (match-string 1)
  1579. ("worktree" (setq worktree (match-string 2)))
  1580. ("HEAD" (setq head (match-string 2)))
  1581. ("branch" (setq branch (match-string 2))))
  1582. (forward-line 1)
  1583. (goto-char (point-at-bol)))
  1584. (setq trees `(,@trees
  1585. (:worktree ,worktree :head ,head :branch ,branch)))
  1586. (forward-line 1)
  1587. (goto-char (point-at-bol)))
  1588. ))
  1589. trees)))
  1590. (defun git-worktree--call-process (&rest args)
  1591. "Start git process synchronously with ARGS.
  1592. Raise error when git process ends with non-zero status.
  1593. Any output will be written to current buffer."
  1594. (let ((status (apply 'call-process
  1595. "git"
  1596. nil
  1597. t
  1598. nil
  1599. args)))
  1600. (cl-assert (eq status 0)
  1601. nil
  1602. (buffer-substring-no-properties (point-min) (point-max)))))
  1603. (defun git-worktree--get-repository-root (dir)
  1604. "Resolve repository root of DIR.
  1605. If DIR is not inside of any git repository, signal an error."
  1606. (cl-assert (file-directory-p dir))
  1607. (with-temp-buffer
  1608. (cd dir)
  1609. (git-worktree--call-process "rev-parse" "--show-toplevel")
  1610. (goto-char (point-min))
  1611. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  1612. ;;(git-worktree--get-repository-root default-directory)
  1613. (defun git-worktree-open-noselect (&optional directory)
  1614. "Open git worktree list buffer.
  1615. If optional arg DIRECTORY is given change current directory to there before
  1616. initializing."
  1617. (setq directory (expand-file-name (or directory
  1618. default-directory)))
  1619. (cl-assert (file-directory-p directory))
  1620. (let* ((root (git-worktree--get-repository-root directory))
  1621. (name (file-name-nondirectory root))
  1622. (bname (format "*GitWorktree<%s>*" name)))
  1623. (with-current-buffer (get-buffer-create bname)
  1624. (cd root)
  1625. (git-worktree--set-tabulated-list-mode-variables)
  1626. (git-worktree-mode)
  1627. (current-buffer))))
  1628. ;; ((:worktree "/Users/10sr/.dotfiles" :head "5e7457a8d49ef6a517cdf39d038ba5fdf98dc68e" :branch "refs/heads/master") (:worktree "/Users/10sr/.dotfiles/b1" :head "fa7d868076d807692e35f82ae23596c903fd1117" :branch "refs/heads/b1"))
  1629. (defun git-worktree--set-tabulated-list-mode-variables ()
  1630. "Set variables for `tabulated-list-mode'."
  1631. (let ((trees (git-worktree-get-current-trees)))
  1632. (setq tabulated-list-entries
  1633. (mapcar (lambda (e)
  1634. (list e
  1635. (vector
  1636. (concat (file-relative-name (plist-get e :worktree))
  1637. "/")
  1638. (or (plist-get e :branch) "")
  1639. (plist-get e :head)
  1640. )))
  1641. trees))
  1642. (let ((branch-max-size
  1643. (apply 'max
  1644. (cl-loop for e in tabulated-list-entries
  1645. collect (length (elt (cadr e) 1)))))
  1646. (worktree-max-size
  1647. (apply 'max
  1648. (length "Worktree")
  1649. (cl-loop for e in tabulated-list-entries
  1650. collect (length (elt (cadr e) 0))))))
  1651. (setq tabulated-list-format
  1652. `[
  1653. ("Worktree" ,worktree-max-size t)
  1654. ("Branch" ,branch-max-size t)
  1655. ("Head" -1 t)
  1656. ]))))
  1657. (defun git-worktree-open (&optional directory)
  1658. "Open git worktree list buffer.
  1659. If optional arg DIRECTORY is given change current directory to there before
  1660. initializing."
  1661. (interactive)
  1662. (let ((bf (git-worktree-open-noselect directory)))
  1663. (pop-to-buffer bf)))
  1664. (defalias 'git-worktree 'git-worktree-open)
  1665. (defun git-worktree-mode-go ()
  1666. "Go to worktree directory at point."
  1667. (interactive)
  1668. (let* ((id (tabulated-list-get-id))
  1669. (path (plist-get id :worktree)))
  1670. (cl-assert path nil "No worktree info at point")
  1671. (cl-assert (file-directory-p path) t "Directory not found")
  1672. (dired path)))
  1673. (defun git-worktree-mode-move ()
  1674. "Move worktree at point to a new location."
  1675. (interactive)
  1676. (let* ((id (tabulated-list-get-id))
  1677. (path (plist-get id :worktree)))
  1678. (cl-assert path nil "No worktree info at point")
  1679. (cl-assert (file-directory-p path) t "Directory not found")
  1680. (let ((new (read-file-name (format "New name for worktree \"%s\": "
  1681. path))))
  1682. (with-temp-buffer
  1683. (git-worktree--call-process "worktree"
  1684. "move"
  1685. path
  1686. (expand-file-name new)))
  1687. (revert-buffer))))
  1688. (defun git-worktree-mode-add ()
  1689. "Add new git worktree."
  1690. (interactive)
  1691. (let* ((path (read-file-name "Path of new worktree: "))
  1692. (commitish (read-string (format "Commitish to checkout to \"%s\" (Empty to omit): "
  1693. path)))
  1694. (args (append '("worktree" "add")
  1695. (if (string= "" commitish)
  1696. (list (expand-file-name path))
  1697. (list (expand-file-name path) commitish)))))
  1698. (with-temp-buffer
  1699. (apply 'git-worktree--call-process args))
  1700. (revert-buffer)))
  1701. (defun git-worktree-mode-remove ()
  1702. "Remove worktree at point."
  1703. (interactive)
  1704. (let* ((id (tabulated-list-get-id))
  1705. (path (plist-get id :worktree)))
  1706. (cl-assert path nil "No worktree info at point")
  1707. (cl-assert (file-directory-p path) t "Directory not found")
  1708. (when (yes-or-no-p (format "Remove workking directory \"%s\": "
  1709. path))
  1710. (with-temp-buffer
  1711. (git-worktree--call-process "worktree"
  1712. "remove"
  1713. path))
  1714. (revert-buffer))))
  1715. (defvar git-worktree-mode-map
  1716. (let ((map (make-sparse-keymap)))
  1717. (suppress-keymap map)
  1718. (define-key map "A" 'git-worktree-mode-add)
  1719. (define-key map (kbd "C-m") 'git-worktree-mode-go)
  1720. (define-key map "R" 'git-worktree-mode-move)
  1721. (define-key map "D" 'git-worktree-mode-remove)
  1722. ;; (define-key map (kbd "C-g") 'git-worktree-mode-close)
  1723. (define-key map "/" 'isearch-forward)
  1724. map))
  1725. (define-derived-mode git-worktree-mode tabulated-list-mode "Git-Worktrees"
  1726. "Major mode for browsing recently opened files and directories."
  1727. (setq tabulated-list-padding 2)
  1728. (add-hook 'tabulated-list-revert-hook
  1729. 'git-worktree--set-tabulated-list-mode-variables
  1730. nil
  1731. t)
  1732. (tabulated-list-init-header)
  1733. (tabulated-list-print nil nil))
  1734. ;; Local Variables:
  1735. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1736. ;; flycheck-checker: emacs-lisp
  1737. ;; End:
  1738. ;;; emancs.el ends here