Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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