Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

3068 рядки
104 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. editorconfig
  116. editorconfig-custom-majormode
  117. git-command
  118. prompt-text
  119. ;; 10sr repository
  120. ;; 10sr-extras
  121. terminal-title
  122. recentf-show
  123. dired-list-all-mode
  124. pack
  125. set-modeline-color
  126. read-only-only-mode
  127. smart-revert
  128. autosave
  129. ;;window-organizer
  130. ilookup
  131. pasteboard
  132. ))
  133. (when (safe-require-or-eval 'package)
  134. (setq package-archives
  135. `(,@package-archives
  136. ("melpa" . "https://melpa.org/packages/")
  137. ;; Somehow fails to download via https
  138. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  139. (package-initialize)
  140. (defun my-auto-install-package ()
  141. "Install packages semi-automatically."
  142. (interactive)
  143. (package-refresh-contents)
  144. (mapc (lambda (pkg)
  145. (or (package-installed-p pkg)
  146. (package-install pkg)))
  147. 10sr-package-list))
  148. )
  149. ;; (lazy-load-eval 'sudoku)
  150. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  151. ;; my-idle-hook
  152. (defvar my-idle-hook nil
  153. "Hook run when idle for several secs.")
  154. (defvar my-idle-hook-sec 5
  155. "Second to run `my-idle-hook'.")
  156. (run-with-idle-timer my-idle-hook-sec
  157. t
  158. (lambda ()
  159. (run-hooks 'my-idle-hook)))
  160. ;; (add-hook 'my-idle-hook
  161. ;; (lambda ()
  162. ;; (message "idle hook message")))
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;; start and quit
  165. (setq inhibit-startup-message t)
  166. (setq confirm-kill-emacs 'y-or-n-p)
  167. (setq gc-cons-threshold (* 1024 1024 16))
  168. (setq garbage-collection-messages nil)
  169. (when window-system
  170. (add-to-list 'default-frame-alist '(cursor-type . box))
  171. (add-to-list 'default-frame-alist '(background-color . "white"))
  172. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  173. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  174. ;; does not work?
  175. )
  176. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  177. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  178. (and (fboundp 'tool-bar-mode)
  179. (tool-bar-mode 0))
  180. (and (fboundp 'set-scroll-bar-mode)
  181. (set-scroll-bar-mode nil))
  182. (call-after-init
  183. (lambda ()
  184. (message "%s %s" invocation-name emacs-version)
  185. (message "Invocation directory: %s" default-directory)
  186. (message "%s was taken to initialize emacs." (emacs-init-time))
  187. (view-echo-area-messages)
  188. ;; (view-emacs-news)
  189. ))
  190. (cd ".") ; when using windows use / instead of \ in `default-directory'
  191. ;; locale
  192. (set-language-environment "Japanese")
  193. (set-default-coding-systems 'utf-8-unix)
  194. (prefer-coding-system 'utf-8-unix)
  195. (setq system-time-locale "C")
  196. ;; my prefix map
  197. (defvar my-prefix-map nil
  198. "My prefix map.")
  199. (define-prefix-command 'my-prefix-map)
  200. ;; (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  201. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  202. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  203. ;; (comint-show-maximum-output)
  204. ;; kill scratch
  205. (call-after-init (lambda ()
  206. (let ((buf (get-buffer "*scratch*")))
  207. (when buf
  208. (kill-buffer buf)))))
  209. ;; modifier keys
  210. ;; (setq mac-option-modifier 'control)
  211. ;; display
  212. (setq visible-bell t)
  213. (setq ring-bell-function 'ignore)
  214. (mouse-avoidance-mode 'banish)
  215. (setq echo-keystrokes 0.1)
  216. (defun reload-init-file ()
  217. "Reload Emacs init file."
  218. (interactive)
  219. (when (and user-init-file
  220. (file-readable-p user-init-file))
  221. (load-file user-init-file)))
  222. (safe-require-or-eval 'session)
  223. ;; server
  224. (set-variable 'server-name (concat "server"
  225. (number-to-string (emacs-pid))))
  226. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  227. ;; In Darwin environment, init fails with message like 'Service name too long'
  228. ;; when server-use-tcp is nil
  229. (when (or (eq system-type
  230. 'cygwin)
  231. (eq system-type
  232. 'darwin))
  233. (set-variable 'server-use-tcp t))
  234. ;; MSYS2 fix
  235. (when (eq system-type
  236. 'windows-nt)
  237. (setq shell-file-name
  238. (executable-find "bash"))
  239. '(setq function-key-map
  240. `(,@function-key-map ([pause] . [?\C-c])
  241. ))
  242. (define-key key-translation-map
  243. (kbd "<pause>")
  244. (kbd "C-c"))
  245. '(keyboard-translate [pause]
  246. (kbd "C-c")p)
  247. ;; TODO: move to other place later
  248. (when (not window-system)
  249. (setq interprogram-paste-function nil)
  250. (setq interprogram-cut-function nil)))
  251. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  252. ;; global keys
  253. (global-set-key (kbd "<up>") 'scroll-down-line)
  254. (global-set-key (kbd "<down>") 'scroll-up-line)
  255. (global-set-key (kbd "<left>") 'scroll-down)
  256. (global-set-key (kbd "<right>") 'scroll-up)
  257. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  258. (global-set-key (kbd "C-\\") help-map)
  259. (define-key ctl-x-map (kbd "DEL") help-map)
  260. (define-key ctl-x-map (kbd "C-h") help-map)
  261. (define-key help-map "a" 'apropos)
  262. ;; disable annoying keys
  263. (global-set-key [prior] 'ignore)
  264. (global-set-key (kbd "<next>") 'ignore)
  265. (global-set-key [menu] 'ignore)
  266. (global-set-key [down-mouse-1] 'ignore)
  267. (global-set-key [down-mouse-2] 'ignore)
  268. (global-set-key [down-mouse-3] 'ignore)
  269. (global-set-key [mouse-1] 'ignore)
  270. (global-set-key [mouse-2] 'ignore)
  271. (global-set-key [mouse-3] 'ignore)
  272. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  273. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  274. (when (safe-require-or-eval 'which-key)
  275. (which-key-mode))
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;; editor
  278. (setq kill-whole-line t)
  279. (setq scroll-conservatively 35
  280. scroll-margin 2)
  281. (setq-default major-mode 'text-mode)
  282. (setq next-line-add-newlines nil)
  283. (setq kill-read-only-ok t)
  284. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  285. ;; (setq-default line-spacing 0.2)
  286. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  287. ;; (setq-default tab-width 4)
  288. (setq-default indent-tabs-mode nil)
  289. (setq-default indent-line-function 'indent-to-left-margin)
  290. ;; (setq-default indent-line-function nil)
  291. (setq-default truncate-lines nil)
  292. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  293. (delete-selection-mode 1)
  294. (cua-mode 0)
  295. (setq line-move-visual nil)
  296. (setq create-lockfiles nil)
  297. (add-hook 'before-save-hook
  298. 'time-stamp)
  299. ;; Add Time-stamp: <> to insert timestamp there
  300. (set-variable 'time-stamp-format
  301. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  302. ;; key bindings
  303. ;; moving around
  304. ;;(keyboard-translate ?\M-j ?\C-j)
  305. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  306. (define-key esc-map "p" 'backward-paragraph)
  307. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  308. (define-key esc-map "n" 'forward-paragraph)
  309. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  310. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  311. (global-set-key (kbd "C-<left>") 'scroll-down)
  312. (global-set-key (kbd "C-<right>") 'scroll-up)
  313. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  314. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  315. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  316. ;; C-h and DEL
  317. (global-set-key (kbd "C-h") (kbd "DEL"))
  318. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  319. (global-set-key (kbd "C-m") 'newline-and-indent)
  320. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  321. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  322. (define-key esc-map "u" 'undo)
  323. (define-key esc-map "i" (kbd "ESC TAB"))
  324. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  325. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  326. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  327. (require 'page-ext nil t)
  328. (when (safe-require-or-eval 'page-break-lines)
  329. (global-page-break-lines-mode 1))
  330. (when (safe-require-or-eval 'git-gutter)
  331. (custom-set-variables
  332. '(git-gutter:update-interval 2))
  333. (custom-set-variables
  334. '(git-gutter:unchanged-sign " "))
  335. (when (= (display-color-cells)
  336. 256)
  337. (let ((c "color-233"))
  338. (set-face-background 'git-gutter:modified c)
  339. (set-face-background 'git-gutter:added c)
  340. (set-face-background 'git-gutter:deleted c)
  341. (set-face-background 'git-gutter:unchanged c)))
  342. (global-git-gutter-mode 1)
  343. )
  344. ;; (when (safe-require-or-eval 'fancy-narrow)
  345. ;; (fancy-narrow-mode 1))
  346. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  347. ;; title and mode-line
  348. (when (safe-require-or-eval 'terminal-title)
  349. ;; if TERM is not screen use default value
  350. (if (getenv "TMUX")
  351. ;; if use tmux locally just basename of current dir
  352. (set-variable 'terminal-title-format
  353. '((file-name-nondirectory (directory-file-name
  354. default-directory))))
  355. (if (and (let ((tty-type (frame-parameter nil
  356. 'tty-type)))
  357. (and tty-type
  358. (equal (car (split-string tty-type
  359. "-"))
  360. "screen")))
  361. (not (getenv "SSH_CONNECTION")))
  362. (set-variable 'terminal-title-format
  363. '((file-name-nondirectory (directory-file-name
  364. default-directory))))
  365. ;; seems that TMUX is used locally and ssh to remote host
  366. (set-variable 'terminal-title-format
  367. `("em:"
  368. ,user-login-name
  369. "@"
  370. ,(car (split-string (system-name)
  371. "\\."))
  372. ":"
  373. default-directory))
  374. )
  375. )
  376. (terminal-title-mode))
  377. (setq eol-mnemonic-dos "\\r\\n")
  378. (setq eol-mnemonic-mac "\\r")
  379. (setq eol-mnemonic-unix "")
  380. (which-function-mode 1)
  381. (line-number-mode 0)
  382. (column-number-mode 0)
  383. (size-indication-mode 0)
  384. (setq mode-line-position
  385. '(:eval (format "L%%l/%d%s:C%%c"
  386. (count-lines (point-max)
  387. (point-min))
  388. (if (buffer-narrowed-p)
  389. "[N]"
  390. "")
  391. )))
  392. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  394. ;; minibuffer
  395. (setq insert-default-directory t)
  396. (setq completion-ignore-case t
  397. read-file-name-completion-ignore-case t
  398. read-buffer-completion-ignore-case t)
  399. (setq resize-mini-windows t)
  400. (temp-buffer-resize-mode 1)
  401. (savehist-mode 1)
  402. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  403. (set-variable 'help-at-pt-display-when-idle t)
  404. (fset 'yes-or-no-p 'y-or-n-p)
  405. ;; complete symbol when `eval'
  406. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  407. (define-key minibuffer-local-map (kbd "C-u")
  408. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  409. ;; I dont know these bindings are good
  410. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  411. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  412. (when (safe-require-or-eval 'minibuffer-line)
  413. (set-face-underline 'minibuffer-line nil)
  414. (set-variable 'minibuffer-line-refresh-interval
  415. 25)
  416. ;; Set idle timer
  417. (defvar my-minibuffer-line--idle-timer nil)
  418. (defvar minibuffer-line-mode)
  419. (add-hook 'minibuffer-line-mode-hook
  420. (lambda ()
  421. (when my-minibuffer-line--idle-timer
  422. (cancel-timer my-minibuffer-line--idle-timer)
  423. (setq my-minibuffer-line--idle-timer nil))
  424. (when minibuffer-line-mode
  425. (setq my-minibuffer-line--idle-timer
  426. (run-with-idle-timer 0.5
  427. t
  428. 'minibuffer-line--update)))))
  429. (set-variable 'minibuffer-line-format
  430. `(,(concat user-login-name
  431. "@"
  432. (car (split-string (system-name)
  433. "\\."))
  434. ":")
  435. (:eval (abbreviate-file-name (or buffer-file-name
  436. default-directory)))
  437. (:eval (and (fboundp 'git-ps1-mode-get-current)
  438. (git-ps1-mode-get-current " [GIT:%s]")))
  439. " "
  440. (:eval (format-time-string display-time-format))))
  441. (minibuffer-line-mode 1)
  442. )
  443. (when (safe-require-or-eval 'prompt-text)
  444. (set-variable 'prompt-text-format
  445. `(,(concat ""
  446. user-login-name
  447. "@"
  448. (car (split-string (system-name)
  449. "\\."))
  450. ":")
  451. (:eval (abbreviate-file-name (or buffer-file-name
  452. default-directory)))
  453. (:eval (and (fboundp 'git-ps1-mode-get-current)
  454. (git-ps1-mode-get-current " [GIT:%s]")))
  455. " "
  456. (:eval (format-time-string display-time-format))
  457. "\n"
  458. (:eval (symbol-name this-command))
  459. ": "))
  460. (prompt-text-mode 1))
  461. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  462. ;; letters, font-lock mode and fonts
  463. (setq text-quoting-style 'grave)
  464. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  465. ;; (set-window-margins (selected-window) 1 1)
  466. (unless window-system
  467. (setq frame-background-mode 'dark))
  468. (and (or (eq system-type 'Darwin)
  469. (eq system-type 'darwin))
  470. (fboundp 'mac-set-input-method-parameter)
  471. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  472. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  473. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  474. (boundp 'input-method-inactivate-hook))
  475. (add-hook 'input-method-activate-hook
  476. (lambda () (set-cursor-color "red")))
  477. (add-hook 'input-method-inactivate-hook
  478. (lambda () (set-cursor-color "black"))))
  479. (when (safe-require-or-eval 'paren)
  480. (show-paren-mode 1)
  481. (setq show-paren-delay 0.5
  482. show-paren-style 'parenthesis) ; mixed is hard to read
  483. ;; (set-face-background 'show-paren-match
  484. ;; "black")
  485. ;; ;; (face-foreground 'default))
  486. ;; (set-face-foreground 'show-paren-match
  487. ;; "white")
  488. ;; (set-face-inverse-video-p 'show-paren-match
  489. ;; t)
  490. )
  491. (transient-mark-mode 1)
  492. (global-font-lock-mode 1)
  493. (setq font-lock-global-modes
  494. '(not
  495. help-mode
  496. eshell-mode
  497. ;;term-mode
  498. Man-mode
  499. magit-diff-mode
  500. magit-revision-mode))
  501. ;; (standard-display-ascii ?\n "$\n")
  502. ;; (defvar my-eol-face
  503. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  504. ;; )
  505. ;; (defvar my-tab-face
  506. ;; '(("\t" . '(0 highlight t nil))))
  507. (defvar my-jspace-face
  508. '(("\u3000" . '(0 highlight t nil))))
  509. (add-hook 'font-lock-mode-hook
  510. (lambda ()
  511. ;; (font-lock-add-keywords nil my-eol-face)
  512. (font-lock-add-keywords nil my-jspace-face)
  513. ))
  514. (when (safe-require-or-eval 'whitespace)
  515. (add-to-list 'whitespace-display-mappings
  516. ;; We need t since last one takes precedence
  517. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  518. ;; (add-to-list 'whitespace-display-mappings
  519. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  520. (setq whitespace-style '(face
  521. trailing ; trailing blanks
  522. ;; tabs
  523. ;; spaces
  524. ;; lines
  525. lines-tail ; lines over 80
  526. newline ; newlines
  527. empty ; empty lines at beg or end of buffer
  528. ;; big-indent
  529. ;; space-mark
  530. tab-mark
  531. newline-mark ; use display table for newline
  532. ))
  533. ;; (setq whitespace-newline 'font-lock-comment-face)
  534. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  535. (defun my-whitesspace-mode-reload ()
  536. "Reload whitespace-mode config."
  537. (interactive)
  538. (when whitespace-mode
  539. (whitespace-mode 0)
  540. (whitespace-mode 1)))
  541. (set-variable 'whitespace-line-column nil)
  542. (global-whitespace-mode t)
  543. (add-hook 'dired-mode-hook
  544. (lambda ()
  545. (set (make-local-variable 'whitespace-style) nil)))
  546. (if (= (display-color-cells)
  547. 256)
  548. (set-face-foreground 'whitespace-newline "color-109")
  549. ;; (progn
  550. ;; (set-face-bold-p 'whitespace-newline
  551. ;; t))
  552. ))
  553. (and nil
  554. '(safe-require-or-eval 'fill-column-indicator)
  555. (setq fill-column-indicator))
  556. ;; highlight current line
  557. ;; http://wiki.riywo.com/index.php?Meadow
  558. (face-spec-set 'hl-line
  559. '((((min-colors 256)
  560. (background dark))
  561. (:background "color-234"))
  562. (((min-colors 256)
  563. (background light))
  564. (:background "color-234"))
  565. (t
  566. (:underline "black"))))
  567. (set-variable 'hl-line-global-modes
  568. '(not
  569. term-mode))
  570. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  571. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  572. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  573. ;;(safe-require-or-eval 'set-modeline-color)
  574. ;; (let ((fg (face-foreground 'default))
  575. ;; (bg (face-background 'default)))
  576. ;; (set-face-background 'mode-line-inactive
  577. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  578. ;; (set-face-foreground 'mode-line-inactive
  579. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  580. ;; (set-face-underline 'mode-line-inactive
  581. ;; t)
  582. ;; (set-face-underline 'vertical-border
  583. ;; nil)
  584. ;; (when (safe-require-or-eval 'end-mark)
  585. ;; (global-end-mark-mode))
  586. ;; M-x highlight-* to highlight things
  587. (global-hi-lock-mode 1)
  588. (unless (fboundp 'highlight-region-text)
  589. (defun highlight-region-text (beg end)
  590. "Highlight text between BEG and END."
  591. (interactive "r")
  592. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  593. end)))
  594. (setq deactivate-mark t)))
  595. (when (safe-require-or-eval 'auto-highlight-symbol)
  596. (set-variable 'ahs-idle-interval 0.6)
  597. (global-auto-highlight-symbol-mode 1))
  598. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  599. ;; file handling
  600. (when (safe-require-or-eval 'editorconfig)
  601. ;; (set-variable 'editorconfig-get-properties-function
  602. ;; 'editorconfig-core-get-properties-hash)
  603. (editorconfig-mode 1)
  604. (set-variable 'editorconfig-mode-lighter " EC")
  605. (with-eval-after-load 'org-src
  606. ;; [*.org\[\*Org Src*\[ c \]*\]]
  607. (add-hook 'org-src-mode-hook
  608. 'editorconfig-mode-apply t)))
  609. (when (fboundp 'editorconfig-custom-majormode)
  610. (add-hook 'editorconfig-custom-hooks
  611. 'editorconfig-custom-majormode))
  612. ;; Add readonly=true to set read-only-mode
  613. (add-hook 'editorconfig-custom-hooks
  614. (lambda (props)
  615. (let ((r (gethash 'readonly props)))
  616. (when (and (string= r "true")
  617. (not buffer-read-only))
  618. (read-only-mode 1)))))
  619. ;; (when (fboundp 'editorconfig-charset-extras)
  620. ;; (add-hook 'editorconfig-custom-hooks
  621. ;; 'editorconfig-charset-extras))
  622. (setq revert-without-query '(".+"))
  623. ;; save cursor position
  624. (when (safe-require-or-eval 'saveplace)
  625. (setq-default save-place t)
  626. (setq save-place-file (concat user-emacs-directory
  627. "places")))
  628. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  629. (setq make-backup-files t)
  630. (setq vc-make-backup-files t)
  631. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  632. (setq backup-directory-alist
  633. (cons (cons "." (expand-file-name (concat user-emacs-directory
  634. "backup")))
  635. backup-directory-alist))
  636. (setq version-control 't)
  637. (setq delete-old-versions t)
  638. (setq kept-new-versions 20)
  639. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  640. "auto-save/")))
  641. ;; (setq delete-auto-save-files t)
  642. (setq auto-save-visited-interval 8)
  643. (auto-save-visited-mode 1)
  644. (add-to-list 'completion-ignored-extensions ".bak")
  645. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  646. (setq delete-by-moving-to-trash t)
  647. ;; trash-directory "~/.emacs.d/trash")
  648. (add-hook 'after-save-hook
  649. 'executable-make-buffer-file-executable-if-script-p)
  650. (set-variable 'bookmark-default-file
  651. (expand-file-name (concat user-emacs-directory
  652. "bmk")))
  653. (set-variable 'bookmark-save-flag
  654. 1)
  655. (with-eval-after-load 'recentf
  656. (defvar recentf-exclude)
  657. (defvar bookmark-default-file)
  658. (add-to-list 'recentf-exclude
  659. (regexp-quote bookmark-default-file)))
  660. (when (safe-require-or-eval 'smart-revert)
  661. (smart-revert-on))
  662. ;; autosave
  663. ;; auto-save-visited-mode can be used instead?
  664. ;; (when (safe-require-or-eval 'autosave)
  665. ;; (autosave-set 8))
  666. ;; bookmarks
  667. (define-key ctl-x-map "m" 'list-bookmarks)
  668. ;; vc
  669. (set-variable 'vc-handled-backends '(RCS))
  670. (set-variable 'vc-rcs-register-switches "-l")
  671. (set-variable 'vc-rcs-checkin-switches "-l")
  672. (set-variable 'vc-command-messages t)
  673. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  674. ;; share clipboard with x
  675. ;; this page describes this in details, but only these sexps seem to be needed
  676. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  677. (and nil
  678. (not window-system)
  679. (not (eq window-system 'mac))
  680. (getenv "DISPLAY")
  681. (not (equal (getenv "DISPLAY") ""))
  682. (executable-find "xclip")
  683. ;; (< emacs-major-version 24)
  684. '(safe-require-or-eval 'xclip)
  685. nil
  686. (turn-on-xclip))
  687. (and (eq system-type 'darwin)
  688. (safe-require-or-eval 'pasteboard)
  689. (turn-on-pasteboard))
  690. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  691. ;; some modes and hooks
  692. ;; Include some extra modes
  693. (require 'generic-x)
  694. (when (autoload-eval-lazily 'wgrep)
  695. (set-variable 'wgrep-auto-save-buffer t)
  696. (defvar grep-mode-map)
  697. (define-key grep-mode-map
  698. "e"
  699. 'wgrep-change-to-wgrep-mode))
  700. (with-eval-after-load 'remember
  701. (defvar remember-mode-map (make-sparse-keymap))
  702. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  703. (with-eval-after-load 'magit-files
  704. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  705. ;; existing keybindings.
  706. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  707. ;; to disable thie mode here, but do anyway.
  708. ;; See also https://github.com/magit/magit/issues/3517
  709. (global-magit-file-mode -1))
  710. (with-eval-after-load 'magit-section
  711. (set-face-background 'magit-section-highlight
  712. nil))
  713. (with-eval-after-load 'magit-diff
  714. (set-face-background 'magit-diff-added-highlight
  715. nil)
  716. (set-face-background 'magit-diff-removed-highlight
  717. nil)
  718. (set-face-background 'magit-diff-context-highlight
  719. nil)
  720. )
  721. (when (boundp 'git-rebase-filename-regexp)
  722. (add-to-list 'auto-mode-alist
  723. `(,git-rebase-filename-regexp . text-mode)))
  724. (when (safe-require-or-eval 'aggressive-indent)
  725. (defvar aggressive-indent-excluded-modes)
  726. (setq aggressive-indent-excluded-modes
  727. `(diff-mode
  728. toml-mode
  729. conf-mode
  730. dockerfile-mode
  731. groovy-mode
  732. ,@aggressive-indent-excluded-modes))
  733. (global-aggressive-indent-mode 1))
  734. (when (autoload-eval-lazily 'ggtags)
  735. (add-hook 'c-mode-common-hook
  736. (lambda ()
  737. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  738. (ggtags-mode 1))))
  739. (add-hook 'python-mode-hook
  740. (lambda ()
  741. (ggtags-mode 1))))
  742. (when (autoload-eval-lazily 'imenu-list)
  743. ;; (set-variable 'imenu-list-auto-resize t)
  744. (set-variable 'imenu-list-focus-after-activation t)
  745. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  746. (add-hook 'emacs-lisp-mode-hook
  747. (lambda ()
  748. (setq imenu-generic-expression
  749. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  750. ,@imenu-generic-expression))))
  751. (with-eval-after-load 'compile
  752. (defvar compilation-filter-start)
  753. (defvar compilation-error-regexp-alist)
  754. (require 'ansi-color)
  755. (add-hook 'compilation-filter-hook
  756. (lambda ()
  757. (let ((inhibit-read-only t))
  758. (ansi-color-apply-on-region compilation-filter-start
  759. (point)))))
  760. (add-to-list 'compilation-error-regexp-alist
  761. ;; ansible-lint
  762. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  763. ;; Workaround to avoid ensime error
  764. (defvar ensime-mode-key-prefix nil)
  765. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  766. (when (safe-require-or-eval 'company)
  767. (global-company-mode)
  768. (set-variable 'company-idle-delay 0.5)
  769. (set-variable 'company-minimum-prefix-length 2)
  770. (set-variable 'company-selection-wrap-around t))
  771. ;; https://github.com/lunaryorn/flycheck
  772. (when (safe-require-or-eval 'flycheck)
  773. (call-after-init 'global-flycheck-mode))
  774. (when (autoload-eval-lazily 'ilookup)
  775. (define-key ctl-x-map "d" 'ilookup-open-word))
  776. (set-variable 'ac-ignore-case nil)
  777. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  778. (define-key ctl-x-map "t" 'term-run-shell-command))
  779. (add-to-list 'safe-local-variable-values
  780. '(encoding utf-8))
  781. (setq enable-local-variables :safe)
  782. ;; Detect file type from shebang and set major-mode.
  783. (add-to-list 'interpreter-mode-alist
  784. '("python3" . python-mode))
  785. (add-to-list 'interpreter-mode-alist
  786. '("python2" . python-mode))
  787. (with-eval-after-load 'python
  788. (defvar python-mode-map (make-sparse-keymap))
  789. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  790. (when (autoload-eval-lazily 'pipenv)
  791. (add-hook 'python-mode-hook
  792. (lambda ()
  793. (pipenv-mode 1)
  794. (pipenv-projectile-after-switch-default)))
  795. )
  796. (set-variable 'flycheck-python-pycompile-executable "python3")
  797. ;; http://fukuyama.co/foreign-regexp
  798. '(and (safe-require-or-eval 'foreign-regexp)
  799. (progn
  800. (setq foreign-regexp/regexp-type 'perl)
  801. '(setq reb-re-syntax 'foreign-regexp)
  802. ))
  803. (autoload-eval-lazily 'sql '(sql-mode)
  804. (require 'sql-indent nil t))
  805. (when (autoload-eval-lazily 'git-command)
  806. (define-key ctl-x-map "g" 'git-command))
  807. (when (safe-require-or-eval 'git-commit)
  808. (global-git-commit-mode 1))
  809. (with-eval-after-load 'git-commit
  810. (add-hook 'git-commit-setup-hook
  811. 'turn-off-auto-fill t))
  812. (autoload-eval-lazily 'sl)
  813. (with-eval-after-load 'rst
  814. (defvar rst-mode-map)
  815. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  816. (with-eval-after-load 'jdee
  817. (add-hook 'jdee-mode-hook
  818. (lambda ()
  819. (make-local-variable 'global-mode-string)
  820. (add-to-list 'global-mode-string
  821. mode-line-position))))
  822. ;; Cannot enable error thrown. Why???
  823. ;; https://github.com/m0smith/malabar-mode#Installation
  824. ;; (when (autoload-eval-lazily 'malabar-mode)
  825. ;; (add-to-list 'load-path
  826. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  827. ;; (safe-require-or-eval 'cedet-devel-load)
  828. ;; (call-after-init 'activate-malabar-mode))
  829. (with-eval-after-load 'make-mode
  830. (defvar makefile-mode-map (make-sparse-keymap))
  831. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  832. ;; this functions is set in write-file-functions, i cannot find any
  833. ;; good way to remove this.
  834. (fset 'makefile-warn-suspicious-lines 'ignore))
  835. (with-eval-after-load 'verilog-mode
  836. (defvar verilog-mode-map (make-sparse-keymap))
  837. (define-key verilog-mode-map ";" 'self-insert-command))
  838. (setq diff-switches "-u")
  839. (with-eval-after-load 'diff-mode
  840. ;; (when (and (eq major-mode
  841. ;; 'diff-mode)
  842. ;; (not buffer-file-name))
  843. ;; ;; do not pass when major-mode is derived mode of diff-mode
  844. ;; (view-mode 1))
  845. (set-face-attribute 'diff-header nil
  846. :foreground nil
  847. :background nil
  848. :weight 'bold)
  849. (set-face-attribute 'diff-file-header nil
  850. :foreground nil
  851. :background nil
  852. :weight 'bold)
  853. (set-face-foreground 'diff-index "blue")
  854. (set-face-attribute 'diff-hunk-header nil
  855. :foreground "cyan"
  856. :weight 'normal)
  857. (set-face-attribute 'diff-context nil
  858. ;; :foreground "white"
  859. :foreground nil
  860. :weight 'normal)
  861. (set-face-foreground 'diff-removed "red")
  862. (set-face-foreground 'diff-added "green")
  863. (set-face-background 'diff-removed nil)
  864. (set-face-background 'diff-added nil)
  865. (set-face-attribute 'diff-changed nil
  866. :foreground "magenta"
  867. :weight 'normal)
  868. (set-face-attribute 'diff-refine-changed nil
  869. :foreground nil
  870. :background nil
  871. :weight 'bold
  872. :inverse-video t)
  873. ;; Annoying !
  874. ;;(diff-auto-refine-mode)
  875. )
  876. ;; (ffap-bindings)
  877. (set-variable 'browse-url-browser-function
  878. 'eww-browse-url)
  879. (set-variable 'sh-here-document-word "__EOC__")
  880. (when (autoload-eval-lazily 'adoc-mode
  881. nil
  882. (defvar adoc-mode-map (make-sparse-keymap))
  883. (define-key adoc-mode-map (kbd "C-m") 'newline))
  884. (setq auto-mode-alist
  885. `(("\\.adoc\\'" . adoc-mode)
  886. ("\\.asciidoc\\'" . adoc-mode)
  887. ,@auto-mode-alist)))
  888. (with-eval-after-load 'markup-faces
  889. ;; Is this too match ?
  890. (set-face-foreground 'markup-meta-face
  891. "color-245")
  892. (set-face-foreground 'markup-meta-hide-face
  893. "color-245")
  894. )
  895. ;; TODO: check if this is required
  896. (when (autoload-eval-lazily 'groovy-mode nil
  897. (defvar groovy-mode-map (make-sparse-keymap))
  898. (define-key groovy-mode-map "(" 'self-insert-command)
  899. (define-key groovy-mode-map ")" 'self-insert-command)
  900. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  901. )
  902. (add-to-list 'auto-mode-alist
  903. '("build\\.gradle\\'" . groovy-mode)))
  904. (with-eval-after-load 'yaml-mode
  905. (defvar yaml-mode-map (make-sparse-keymap))
  906. (define-key yaml-mode-map (kbd "C-m") 'newline))
  907. (with-eval-after-load 'html-mode
  908. (defvar html-mode-map (make-sparse-keymap))
  909. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  910. (with-eval-after-load 'text-mode
  911. (define-key text-mode-map (kbd "C-m") 'newline))
  912. (autoload-eval-lazily 'info nil
  913. (defvar Info-additional-directory-list)
  914. (dolist (dir (directory-files (concat user-emacs-directory
  915. "info")
  916. t
  917. "^[^.].*"))
  918. (when (file-directory-p dir)
  919. (add-to-list 'Info-additional-directory-list
  920. dir)))
  921. (let ((dir (expand-file-name "~/.brew/share/info")))
  922. (when (file-directory-p dir)
  923. (add-to-list 'Info-additional-directory-list
  924. dir))))
  925. (with-eval-after-load 'apropos
  926. (defvar apropos-mode-map (make-sparse-keymap))
  927. (define-key apropos-mode-map "n" 'next-line)
  928. (define-key apropos-mode-map "p" 'previous-line))
  929. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  930. ;; (define-key isearch-mode-map
  931. ;; (kbd "C-j") 'isearch-other-control-char)
  932. ;; (define-key isearch-mode-map
  933. ;; (kbd "C-k") 'isearch-other-control-char)
  934. ;; (define-key isearch-mode-map
  935. ;; (kbd "C-h") 'isearch-other-control-char)
  936. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  937. (define-key isearch-mode-map (kbd "M-r")
  938. 'isearch-query-replace-regexp)
  939. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  940. (setq lazy-highlight-cleanup nil)
  941. ;; face for isearch highlighing
  942. (set-face-attribute 'lazy-highlight
  943. nil
  944. :foreground `unspecified
  945. :background `unspecified
  946. :underline t
  947. ;; :weight `bold
  948. )
  949. (add-hook 'outline-mode-hook
  950. (lambda ()
  951. (when (string-match "\\.md\\'" buffer-file-name)
  952. (set (make-local-variable 'outline-regexp) "#+ "))))
  953. (add-hook 'outline-mode-hook
  954. 'outline-show-all)
  955. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  956. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  957. (when (autoload-eval-lazily 'markdown-mode
  958. '(markdown-mode gfm-mode)
  959. (defvar gfm-mode-map (make-sparse-keymap))
  960. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  961. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  962. (set-variable 'markdown-command (or (executable-find "markdown")
  963. (executable-find "markdown.pl")
  964. ""))
  965. (add-hook 'markdown-mode-hook
  966. (lambda ()
  967. (outline-minor-mode 1)
  968. (flyspell-mode)
  969. (set (make-local-variable 'comment-start) ";")))
  970. )
  971. ;; c-mode
  972. ;; http://www.emacswiki.org/emacs/IndentingC
  973. ;; http://en.wikipedia.org/wiki/Indent_style
  974. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  975. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  976. (with-eval-after-load 'cc-vars
  977. (defvar c-default-style nil)
  978. (add-to-list 'c-default-style
  979. '(c-mode . "k&r"))
  980. (add-to-list 'c-default-style
  981. '(c++-mode . "k&r")))
  982. (autoload-eval-lazily 'js2-mode nil
  983. ;; currently do not use js2-mode
  984. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  985. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  986. ;; (defvar js2-mode-map (make-sparse-keymap))
  987. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  988. ;; (interactive)
  989. ;; (js2-enter-key)
  990. ;; (indent-for-tab-command)))
  991. ;; (add-hook (kill-local-variable 'before-save-hook)
  992. ;; 'js2-before-save)
  993. ;; (add-hook 'before-save-hook
  994. ;; 'my-indent-buffer
  995. ;; nil
  996. ;; t)
  997. )
  998. (add-to-list 'interpreter-mode-alist
  999. '("node" . js-mode))
  1000. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1001. (with-eval-after-load 'uniquify
  1002. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1003. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1004. (setq uniquify-min-dir-content 1))
  1005. (with-eval-after-load 'view
  1006. (defvar view-mode-map (make-sparse-keymap))
  1007. (define-key view-mode-map "j" 'scroll-up-line)
  1008. (define-key view-mode-map "k" 'scroll-down-line)
  1009. (define-key view-mode-map "v" 'toggle-read-only)
  1010. (define-key view-mode-map "q" 'bury-buffer)
  1011. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1012. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1013. ;; (define-key view-mode-map
  1014. ;; "n" 'nonincremental-repeat-search-forward)
  1015. ;; (define-key view-mode-map
  1016. ;; "N" 'nonincremental-repeat-search-backward)
  1017. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1018. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1019. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1020. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1021. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1022. (global-set-key "\M-r" 'view-mode)
  1023. ;; (setq view-read-only t)
  1024. (with-eval-after-load 'term
  1025. (defvar term-raw-map (make-sparse-keymap))
  1026. (define-key term-raw-map (kbd "C-x")
  1027. (lookup-key (current-global-map)
  1028. (kbd "C-x"))))
  1029. (add-hook 'term-mode-hook
  1030. (lambda ()
  1031. ;; Stop current line highlighting
  1032. (set (make-local-variable (defvar hl-line-range-function))
  1033. (lambda () '(0 . 0)))
  1034. (set (make-local-variable 'scroll-margin)
  1035. 0)))
  1036. (add-hook 'Man-mode-hook
  1037. (lambda ()
  1038. (view-mode 1)
  1039. (setq truncate-lines nil)))
  1040. (set-variable 'Man-notify-method (if window-system
  1041. 'newframe
  1042. 'aggressive))
  1043. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1044. "woman_cache.el")))
  1045. ;; not work because man.el will be loaded when man called
  1046. (defalias 'man 'woman)
  1047. (add-to-list 'auto-mode-alist
  1048. '("tox\\.ini\\'" . conf-unix-mode))
  1049. (when (autoload-eval-lazily 'toml-mode)
  1050. (add-to-list 'auto-mode-alist
  1051. '("/tox\\.ini\\'" . toml-mode))
  1052. (add-to-list 'auto-mode-alist
  1053. '("/Pipfile\\'" . toml-mode)))
  1054. (when (autoload-eval-lazily 'json-mode)
  1055. (add-to-list 'auto-mode-alist
  1056. '("/Pipfile\\.lock\\'" . json-mode)))
  1057. (add-hook 'go-mode-hook
  1058. (lambda()
  1059. (defvar go-mode-map)
  1060. (add-hook 'before-save-hook' 'gofmt-before-save)
  1061. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1062. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1063. ;; buffers
  1064. (defvar bs-configurations)
  1065. (when (autoload-eval-lazily 'bs '(bs-show)
  1066. (add-to-list 'bs-configurations
  1067. '("specials" "^\\*" nil ".*" nil nil))
  1068. (add-to-list 'bs-configurations
  1069. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1070. (defvar bs-mode-map)
  1071. (defvar bs-current-configuration)
  1072. (define-key bs-mode-map (kbd "t")
  1073. ;; TODO: fix toggle feature
  1074. (lambda ()
  1075. (interactive)
  1076. (if (string= "specials"
  1077. bs-current-configuration)
  1078. (bs-set-configuration "files")
  1079. (bs-set-configuration "specials"))
  1080. (bs-refresh)
  1081. (bs-message-without-log "%s"
  1082. (bs--current-config-message))))
  1083. ;; (setq bs-configurations (list
  1084. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1085. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1086. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1087. )
  1088. (defalias 'list-buffers 'bs-show)
  1089. (set-variable 'bs-default-configuration "files-and-specials")
  1090. (set-variable 'bs-default-sort-name "by nothing")
  1091. (add-hook 'bs-mode-hook
  1092. (lambda ()
  1093. (set (make-local-variable 'scroll-margin) 0))))
  1094. ;;(iswitchb-mode 1)
  1095. (icomplete-mode)
  1096. (defun iswitchb-buffer-display-other-window ()
  1097. "Do iswitchb in other window."
  1098. (interactive)
  1099. (let ((iswitchb-default-method 'display))
  1100. (call-interactively 'iswitchb-buffer)))
  1101. ;; buffer killing
  1102. ;; (defun my-delete-window-killing-buffer () nil)
  1103. (defun my-query-kill-current-buffer ()
  1104. "Interactively kill current buffer."
  1105. (interactive)
  1106. (if (y-or-n-p (concat "kill current buffer? :"))
  1107. (kill-buffer (current-buffer))))
  1108. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1109. (substitute-key-definition 'kill-buffer
  1110. 'my-query-kill-current-buffer
  1111. global-map)
  1112. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1113. ;; recentf-mode
  1114. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1115. "recentf")))
  1116. (set-variable 'recentf-max-menu-items 20)
  1117. (set-variable 'recentf-max-saved-items 30)
  1118. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1119. (set-variable 'recentf-auto-cleanup 3)
  1120. ;; (safe-require-or-eval 'sync-recentf)
  1121. ;; (when (safe-require-or-eval 'recentf)
  1122. ;; (add-to-list 'recentf-exclude
  1123. ;; (regexp-quote recentf-save-file))
  1124. ;; (add-to-list 'recentf-exclude
  1125. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1126. ;; (add-to-list 'recentf-exclude
  1127. ;; "/sync-recentf-marker\\'")
  1128. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1129. ;; (remove-hook 'find-file-hook
  1130. ;; 'recentf-track-opened-file)
  1131. ;; (defun my-recentf-load-track-save-list ()
  1132. ;; "Load current recentf list from file, track current visiting file, then save
  1133. ;; the list."
  1134. ;; (recentf-load-list)
  1135. ;; (recentf-track-opened-file)
  1136. ;; (recentf-save-list))
  1137. ;; (add-hook 'find-file-hook
  1138. ;; 'my-recentf-load-track-save-list)
  1139. ;; (add-hook 'kill-emacs-hook
  1140. ;; 'recentf-load-list)
  1141. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1142. ;; ;; (add-hook 'find-file-hook
  1143. ;; ;; (lambda ()
  1144. ;; ;; (recentf-add-file default-directory)))
  1145. ;; (when (autoload-eval-lazily 'recentf-show)
  1146. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1147. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1148. ;; ;; 'recentf-load-list)
  1149. ;; )
  1150. ;; (recentf-mode 1)
  1151. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1152. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1153. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1154. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1155. ;; (add-hook 'recentf-dialog-mode-hook
  1156. ;; (lambda ()
  1157. ;; ;; (recentf-save-list)
  1158. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1159. ;; ;; 'my-recentf-cd-and-find-file)
  1160. ;; (cd "~/"))))
  1161. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1162. ;; dired
  1163. (defun my-file-head (filename &optional n)
  1164. "Return list of first N lines of file FILENAME."
  1165. ;; TODO: Fix for janapese text
  1166. ;; TODO: Fix for short text
  1167. (let ((num (or n 10))
  1168. (size 100)
  1169. (beg 0)
  1170. (end 0)
  1171. (result '())
  1172. (read -1))
  1173. (with-temp-buffer
  1174. (erase-buffer)
  1175. (while (or (<= (count-lines (point-min)
  1176. (point-max))
  1177. num)
  1178. (eq read 0))
  1179. (setq end (+ beg size))
  1180. (setq read (nth 1 (insert-file-contents-literally filename
  1181. nil
  1182. beg
  1183. end)))
  1184. (goto-char (point-max))
  1185. (setq beg (+ beg size)))
  1186. (goto-char (point-min))
  1187. (while (< (length result) num)
  1188. (let ((start (point)))
  1189. (forward-line 1)
  1190. (setq result
  1191. `(,@result ,(buffer-substring-no-properties start
  1192. (point))))))
  1193. result
  1194. ;; (buffer-substring-no-properties (point-min)
  1195. ;; (progn
  1196. ;; (forward-line num)
  1197. ;; (point)))
  1198. )))
  1199. ;; (apply 'concat (my-file-head "./shrc" 10)
  1200. (defun my-dired-echo-file-head (arg)
  1201. "Echo head of current file.
  1202. ARG is num to show, or defaults to 7."
  1203. (interactive "P")
  1204. (let ((f (dired-get-filename)))
  1205. (message "%s"
  1206. (apply 'concat
  1207. (my-file-head f
  1208. 7)))))
  1209. (defun my-dired-diff ()
  1210. "Show diff of marked file and file of current line."
  1211. (interactive)
  1212. (let ((files (dired-get-marked-files nil nil nil t)))
  1213. (if (eq (car files)
  1214. t)
  1215. (diff (cadr files) (dired-get-filename))
  1216. (message "One file must be marked!"))))
  1217. (defun dired-get-file-info ()
  1218. "Print information of current line file."
  1219. (interactive)
  1220. (let ((f (shell-quote-argument (dired-get-filename t))))
  1221. (if (file-directory-p f)
  1222. (progn
  1223. (message "Calculating disk usage...")
  1224. (shell-command (concat "du -hsD "
  1225. f)))
  1226. (shell-command (concat "file "
  1227. f)))))
  1228. (defun my-dired-scroll-up ()
  1229. "Scroll up."
  1230. (interactive)
  1231. (my-dired-previous-line (- (window-height) 1)))
  1232. (defun my-dired-scroll-down ()
  1233. "Scroll down."
  1234. (interactive)
  1235. (my-dired-next-line (- (window-height) 1)))
  1236. ;; (defun my-dired-forward-line (arg)
  1237. ;; ""
  1238. ;; (interactive "p"))
  1239. (defun my-dired-previous-line (arg)
  1240. "Move ARG lines up."
  1241. (interactive "p")
  1242. (if (> arg 0)
  1243. (progn
  1244. (if (eq (line-number-at-pos)
  1245. 1)
  1246. (goto-char (point-max))
  1247. (forward-line -1))
  1248. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1249. (dired-get-subdir))
  1250. (- arg 1)
  1251. arg)))
  1252. (dired-move-to-filename)))
  1253. (defun my-dired-next-line (arg)
  1254. "Move ARG lines down."
  1255. (interactive "p")
  1256. (if (> arg 0)
  1257. (progn
  1258. (if (eq (point)
  1259. (point-max))
  1260. (goto-char (point-min))
  1261. (forward-line 1))
  1262. (my-dired-next-line (if (or (dired-get-filename nil t)
  1263. (dired-get-subdir))
  1264. (- arg 1)
  1265. arg)))
  1266. (dired-move-to-filename)))
  1267. (defun my-tramp-remote-find-file (f)
  1268. "Open F."
  1269. (interactive (list (read-file-name "My Find File Tramp: "
  1270. "/scp:"
  1271. nil ;; "/scp:"
  1272. (confirm-nonexistent-file-or-buffer))))
  1273. (find-file f))
  1274. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1275. (if (eq window-system 'mac)
  1276. (setq dired-listing-switches "-lhF")
  1277. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1278. )
  1279. (setq dired-listing-switches "-lhF")
  1280. ;; when using dired-find-alternate-file
  1281. ;; reuse current dired buffer for the file to open
  1282. ;; (put 'dired-find-alternate-file 'disabled nil)
  1283. (set-variable 'dired-ls-F-marks-symlinks t)
  1284. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1285. (set-variable 'ls-lisp-dirs-first t)
  1286. (set-variable 'ls-lisp-use-localized-time-format t)
  1287. (set-variable 'ls-lisp-format-time-list
  1288. '("%Y-%m-%d %H:%M"
  1289. "%Y-%m-%d "))
  1290. (set-variable 'dired-dwim-target t)
  1291. (set-variable 'dired-isearch-filenames t)
  1292. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1293. (set-variable 'dired-hide-details-hide-information-lines nil)
  1294. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1295. (set-variable 'dired-recursive-deletes 'always)
  1296. ;; (add-hook 'dired-after-readin-hook
  1297. ;; 'my-replace-nasi-none)
  1298. (with-eval-after-load 'dired
  1299. (safe-require-or-eval 'ls-lisp)
  1300. (defvar dired-mode-map (make-sparse-keymap))
  1301. ;; dired-do-chgrp sometimes cause system hung
  1302. (define-key dired-mode-map "G" 'ignore)
  1303. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1304. (define-key dired-mode-map "i" 'dired-get-file-info)
  1305. (define-key dired-mode-map "f" 'find-file)
  1306. (define-key dired-mode-map "!" 'shell-command)
  1307. (define-key dired-mode-map "&" 'async-shell-command)
  1308. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1309. (define-key dired-mode-map "=" 'my-dired-diff)
  1310. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1311. (define-key dired-mode-map "b" 'gtkbm)
  1312. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1313. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1314. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1315. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1316. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1317. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1318. (substitute-key-definition 'dired-next-line
  1319. 'my-dired-next-line
  1320. dired-mode-map)
  1321. (substitute-key-definition 'dired-previous-line
  1322. 'my-dired-previous-line
  1323. dired-mode-map)
  1324. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1325. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1326. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1327. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1328. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1329. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1330. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1331. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1332. (add-hook 'dired-mode-hook
  1333. (lambda ()
  1334. (when (fboundp 'dired-hide-details-mode)
  1335. (dired-hide-details-mode t)
  1336. (local-set-key "l" 'dired-hide-details-mode))
  1337. (let ((file "._Icon\015"))
  1338. (when nil
  1339. '(file-readable-p file)
  1340. (delete-file file)))))
  1341. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1342. (with-eval-after-load 'dired
  1343. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1344. (when (autoload-eval-lazily 'dired-list-all-mode)
  1345. (setq dired-listing-switches "-lhF")
  1346. (with-eval-after-load 'dired
  1347. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1348. (when (autoload-eval-lazily 'dired-filter)
  1349. (add-hook 'dired-mode-hook
  1350. 'dired-filter-mode))
  1351. ;; Currently disabled in favor of dired-from-git-ls-files
  1352. ;; (define-key ctl-x-map "f" 'find-dired)
  1353. ;; It works!
  1354. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1355. (defun my-dired-git-ls-files (args)
  1356. "Dired from git ls-files."
  1357. (interactive "sgit ls-files args: ")
  1358. (pop-to-buffer-same-window
  1359. (dired-noselect `(,default-directory
  1360. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1361. "\0" t))
  1362. ""))
  1363. )
  1364. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1365. (with-eval-after-load 'dired
  1366. (defvar dired-mode-map (make-sparse-keymap))
  1367. (define-key dired-mode-map "f" 'my-dired-git-ls-files))
  1368. ;; (define-minor-mode my-dired-glob-filter)
  1369. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1370. ;; misc funcs
  1371. (defalias 'qcalc 'quick-calc)
  1372. (defun memo (&optional dir)
  1373. "Open memo.txt in DIR."
  1374. (interactive)
  1375. (pop-to-buffer (find-file-noselect (concat (if dir
  1376. (file-name-as-directory dir)
  1377. "")
  1378. "memo.txt"))))
  1379. (set (defvar my-rgrep-alist nil
  1380. "Alist of rgrep command.
  1381. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1382. condition to choose COMMAND when evaluated.")
  1383. `(
  1384. ;; ripgrep
  1385. ("rg"
  1386. (executable-find "rg")
  1387. "rg --hidden --no-heading --smart-case ")
  1388. ;; git grep
  1389. ("gitgrep"
  1390. (eq 0
  1391. (shell-command "git rev-parse --git-dir"))
  1392. "git --no-pager -c color.grep=always grep -nH -e ")
  1393. ;; sift
  1394. ("sift"
  1395. (executable-find "sift")
  1396. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1397. ;; the silver searcher
  1398. ("ag"
  1399. (executable-find "ag")
  1400. "ag --nogroup --nopager --filename ")
  1401. ;; ack
  1402. ("ack"
  1403. (executable-find "ack")
  1404. "ack --nogroup --nopager --with-filename ")
  1405. ;; gnu global
  1406. ("global"
  1407. (and (require 'ggtags nil t)
  1408. (executable-find "global")
  1409. (ggtags-current-project-root))
  1410. "global --result grep ")
  1411. ;; grep
  1412. ("grep"
  1413. t
  1414. ,(concat "find . "
  1415. "-path '*/.git' -prune -o "
  1416. "-path '*/.svn' -prune -o "
  1417. "-type f -print0 | "
  1418. "xargs -0 grep -nH -e "))
  1419. )
  1420. )
  1421. (defvar my-rgrep-default nil
  1422. "Default command name for my-rgrep.")
  1423. (defun my-rgrep-grep-command (&optional name alist)
  1424. "Return recursive grep command for current directory or nil.
  1425. If NAME is given, use that without testing.
  1426. Commands are searched from ALIST."
  1427. (if alist
  1428. (if name
  1429. ;; if name is given search that from alist and return the command
  1430. (nth 2 (assoc name
  1431. alist))
  1432. ;; if name is not given try test in 1th elem
  1433. (let ((car (car alist))
  1434. (cdr (cdr alist)))
  1435. (if (eval (nth 1 car))
  1436. ;; if the condition is true return the command
  1437. (nth 2 car)
  1438. ;; try next one
  1439. (and cdr
  1440. (my-rgrep-grep-command name cdr)))))
  1441. ;; if alist is not given set default value
  1442. (my-rgrep-grep-command name my-rgrep-alist)))
  1443. (defun my-rgrep (command-args)
  1444. "My recursive grep. Run COMMAND-ARGS.
  1445. If prefix argument is given, use current symbol as default search target
  1446. and search from projectile root (if projectile is available)."
  1447. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1448. nil)))
  1449. (if cmd
  1450. (list (read-shell-command "grep command: "
  1451. (concat cmd
  1452. (if current-prefix-arg
  1453. (thing-at-point 'symbol t)
  1454. ""))
  1455. 'grep-find-history))
  1456. (error "My-Rgrep: Command for rgrep not found")
  1457. )))
  1458. (if (and current-prefix-arg
  1459. (safe-require-or-eval 'projectile)
  1460. (projectile-project-p))
  1461. (projectile-with-default-dir (projectile-project-root)
  1462. (compilation-start command-args
  1463. 'grep-mode))
  1464. (compilation-start command-args
  1465. 'grep-mode)))
  1466. (defun my-rgrep-thing-at-point-projectile-root ()
  1467. "My recursive grep to find thing at point from project root."
  1468. (interactive)
  1469. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1470. nil))
  1471. (command-args
  1472. (if cmd
  1473. (concat cmd
  1474. (or (thing-at-point 'symbol t)
  1475. (error "No symbol at point")))
  1476. (error "My-Rgrep: Command for rgrep not found"))))
  1477. (if (safe-require-or-eval 'projectile)
  1478. (projectile-with-default-dir (or (projectile-project-root)
  1479. default-directory)
  1480. (compilation-start command-args
  1481. 'grep-mode))
  1482. (compilation-start command-args
  1483. 'grep-mode))))
  1484. (defmacro define-my-rgrep (name)
  1485. "Define rgrep for NAME."
  1486. `(defun ,(intern (concat "my-rgrep-"
  1487. name)) ()
  1488. ,(format "My recursive grep by %s."
  1489. name)
  1490. (interactive)
  1491. (let ((my-rgrep-default ,name))
  1492. (if (called-interactively-p 'any)
  1493. (call-interactively 'my-rgrep)
  1494. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1495. )
  1496. (define-my-rgrep "ack")
  1497. (define-my-rgrep "ag")
  1498. (define-my-rgrep "rg")
  1499. (define-my-rgrep "sift")
  1500. (define-my-rgrep "gitgrep")
  1501. (define-my-rgrep "grep")
  1502. (define-my-rgrep "global")
  1503. (define-key ctl-x-map "s" 'my-rgrep)
  1504. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1505. (defun my-occur (regexp &optional region)
  1506. "My occur command to search REGEXP."
  1507. (interactive (list (read-string "List lines matching regexp: "
  1508. (thing-at-point 'symbol t))))
  1509. (occur regexp nil region))
  1510. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1511. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1512. (defalias 'make 'compile)
  1513. (define-key ctl-x-map "c" 'compile)
  1514. ;;;;;;;;;;;;;;;;;;;;;;;
  1515. ;; adoc-simple-mode
  1516. (when (safe-require-or-eval 'adoc-mode)
  1517. (defvar adoc-simple-font-lock-keywords
  1518. nil)
  1519. (define-derived-mode adoc-simple-mode adoc-mode
  1520. "Adoc-Simple"
  1521. "Major mode for editing AsciiDoc text files.
  1522. This mode is a simplified version of `adoc-mode'."
  1523. '(set (make-local-variable 'font-lock-defaults)
  1524. '(adoc-simple-font-lock-keywords
  1525. nil nil nil nil
  1526. (font-lock-multiline . t)
  1527. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1528. (add-to-list 'auto-mode-alist
  1529. '("\\.adoc\\'" . adoc-simple-mode)))
  1530. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1531. ;; awk-preview
  1532. (defgroup awk-preview nil
  1533. "Awk previewer."
  1534. :tag "Awk Preview"
  1535. :prefix "awk-preview-"
  1536. :group 'tools)
  1537. (defcustom awk-preview-program
  1538. (or (executable-find "gawk")
  1539. (executable-find "awk")
  1540. "awk")
  1541. "Awk program to execute."
  1542. :type 'string
  1543. :group 'awk-preview)
  1544. (defcustom awk-preview-switches
  1545. ;; '("--sandbox")
  1546. nil
  1547. "String of awk options appended when running awk preview."
  1548. :type '(repeat string)
  1549. :group 'awk-preview)
  1550. (defcustom awk-preview-default-program
  1551. "# C-c C-l to update preview, C-c C-c to commit, C-c C-k to abort.
  1552. {
  1553. print NR, $0
  1554. }
  1555. "
  1556. "Default awk command."
  1557. :type 'string
  1558. :group 'awk-preview)
  1559. (cl-defstruct awk-preview--env
  1560. (running-p nil)
  1561. ;; Point of beg in source buffer
  1562. (point-beg nil)
  1563. ;; Point of end in source buffer
  1564. (point-end nil)
  1565. ;; Point of beginning
  1566. ;; Used by preview buffer and always same as awk-preview--point-beg
  1567. (preview-point-beg nil)
  1568. ;; Point of beginning
  1569. ;; Used by preview buffer and may defferent from awk-preview--point-end
  1570. (preview-point-end nil)
  1571. ;; Awk preview program file name
  1572. (program-filename nil)
  1573. ;; Source buffer
  1574. (source-buffer nil)
  1575. ;; Preview buffer
  1576. (preview-buffer nil)
  1577. ;; Program buffer
  1578. (program-buffer nil)
  1579. ;; Window configuration when entering awk-review
  1580. (window-configuration nil)
  1581. )
  1582. (defvar awk-preview--env nil
  1583. "`awk-preview--env' struct object of currently running.")
  1584. (make-variable-buffer-local 'awk-preview--env)
  1585. (defvar awk-preview--point-beg
  1586. nil
  1587. "Point of beginning.")
  1588. (make-variable-buffer-local 'awk-preview--point-beg)
  1589. (defvar awk-preview--point-end
  1590. nil
  1591. "Point of end.")
  1592. (make-variable-buffer-local 'awk-preview--point-end)
  1593. (defvar awk-preview--preview-point-beg
  1594. nil
  1595. "Point of beginning.
  1596. Used by preview buffer and always same as awk-preview--point-beg.")
  1597. (make-variable-buffer-local 'awk-preview--preview-point-beg)
  1598. (defvar awk-preview--preview-point-end
  1599. nil
  1600. "Point of beginning.
  1601. Used by preview buffer and may defferent from awk-preview--point-end.")
  1602. (make-variable-buffer-local 'awk-preview--preview-point-end)
  1603. (defvar awk-preview--program-filename
  1604. nil
  1605. "Awk preview program file name.")
  1606. (make-variable-buffer-local 'awk-preview--program-filename)
  1607. (defvar awk-preview--source-buffer
  1608. nil
  1609. "Awk source buffer.")
  1610. (make-variable-buffer-local 'awk-preview--source-buffer)
  1611. (defvar awk-preview--preview-buffer
  1612. nil
  1613. "Awk preview buffer.")
  1614. (make-variable-buffer-local 'awk-preview--preview-buffer)
  1615. (defvar awk-preview--program-buffer
  1616. nil
  1617. "Awk program buffer.")
  1618. (make-variable-buffer-local 'awk-preview--program-buffer)
  1619. (defvar awk-preview--window-configuration
  1620. nil
  1621. "Awk preview window configuration.")
  1622. (defun awk-preview--invoke-awk (buf beg end progfile output &optional delete)
  1623. "Execute PROGFILE awk process with BEG and END input and output to OUTPUT buffer.
  1624. When OUTPUT is t the result will be output to BUF.
  1625. Delete the text between BEG and END when DELETE is non-nil."
  1626. (with-current-buffer buf
  1627. (let ((status (apply 'call-process-region
  1628. beg
  1629. end
  1630. awk-preview-program
  1631. delete
  1632. output
  1633. nil
  1634. `(,@awk-preview-switches "-f" ,progfile))))
  1635. (unless (eq status
  1636. 0)
  1637. (error "awk-preview: Awk program exited abnormally."))
  1638. output)))
  1639. (defvar awk-preview-program-buffer-name
  1640. "*AWK Preview Program<%s>*"
  1641. "Buffer name of awk preview program.")
  1642. (defvar awk-preview-preview-buffer-name
  1643. "*AWK Preview<%s>*"
  1644. "Buffer name of awk preview.")
  1645. (defun awk-preview--create-program-buffer (source preview)
  1646. "Create and show awk program buffer for SOURCE and PREVIEW buffer.
  1647. Return that buffer."
  1648. (let ((source-name (buffer-name source)))
  1649. (with-current-buffer (generate-new-buffer (format awk-preview-program-buffer-name
  1650. source-name))
  1651. (erase-buffer)
  1652. (insert awk-preview-default-program)
  1653. (awk-mode)
  1654. (awk-preview-program-mode 1)
  1655. (setq awk-preview--source-buffer source)
  1656. (setq awk-preview--preview-buffer preview)
  1657. (setq awk-preview--program-buffer (current-buffer))
  1658. (setq awk-preview--program-filename (make-temp-file "awk-preview-"
  1659. nil
  1660. ".awk"))
  1661. (current-buffer))))
  1662. (defun awk-preview-program-buffer-kill-hook ()
  1663. "Cleanup for awk-preview program buffer."
  1664. (when awk-preview--program-filename
  1665. (delete-file awk-preview--program-filename)))
  1666. (add-hook 'kill-buffer-hook
  1667. 'awk-preview-program-buffer-kill-hook)
  1668. (defun awk-preview--create-preview-buffer (source)
  1669. "Create preview buffer of SOURCE buffer and return it."
  1670. (with-current-buffer source
  1671. (let ((buffer-file-name nil)
  1672. (beg awk-preview--point-beg)
  1673. (end awk-preview--point-end))
  1674. (with-current-buffer (clone-buffer (format awk-preview-preview-buffer-name
  1675. (buffer-name)))
  1676. (awk-preview-program-mode 1)
  1677. (setq awk-preview--preview-point-beg beg)
  1678. (setq awk-preview--preview-point-end end)
  1679. (setq awk-preview--source-buffer source)
  1680. (setq awk-preview--preview-buffer (current-buffer))
  1681. (goto-char end)
  1682. (setq buffer-read-only t)
  1683. (current-buffer)))))
  1684. (defun awk-preview (beg end)
  1685. "Run awk and preview result."
  1686. (interactive "r")
  1687. (setq awk-preview--point-beg beg)
  1688. (setq awk-preview--point-end end)
  1689. (setq awk-preview--source-buffer (current-buffer))
  1690. (setq awk-preview--preview-buffer
  1691. (awk-preview--create-preview-buffer (current-buffer)))
  1692. (setq awk-preview--program-buffer
  1693. (awk-preview--create-program-buffer (current-buffer)
  1694. awk-preview--preview-buffer))
  1695. ;; At creation of preview buffer, program buffer is not available so set this
  1696. ;; here
  1697. (let ((b awk-preview--program-buffer))
  1698. (with-current-buffer awk-preview--preview-buffer
  1699. (setq awk-preview--program-buffer b)))
  1700. (setq awk-preview--window-configuration
  1701. (current-window-configuration))
  1702. (set-window-buffer (get-buffer-window awk-preview--source-buffer)
  1703. awk-preview--preview-buffer)
  1704. (pop-to-buffer awk-preview--program-buffer)
  1705. (switch-to-buffer awk-preview--program-buffer)
  1706. (awk-preview-update-preview)
  1707. )
  1708. (defun awk-preview-update-preview ()
  1709. "Update awk-preview."
  1710. (interactive)
  1711. (with-current-buffer awk-preview--program-buffer
  1712. (write-region (point-min)
  1713. (point-max)
  1714. awk-preview--program-filename))
  1715. (let ((output (with-current-buffer (get-buffer-create " *awk-preview output*")
  1716. (erase-buffer)
  1717. (current-buffer)))
  1718. (progfile (with-current-buffer awk-preview--program-buffer
  1719. awk-preview--program-filename)))
  1720. (with-current-buffer awk-preview--source-buffer
  1721. (awk-preview--invoke-awk (current-buffer)
  1722. awk-preview--point-beg
  1723. awk-preview--point-end
  1724. progfile
  1725. output)
  1726. (with-current-buffer awk-preview--preview-buffer
  1727. (let ((inhibit-read-only t))
  1728. (goto-char awk-preview--preview-point-end)
  1729. (delete-region awk-preview--preview-point-beg (point))
  1730. (insert-buffer-substring output)
  1731. (setq awk-preview--preview-point-end (point))))
  1732. )))
  1733. (defun awk-preview-commit ()
  1734. "Exit awk-preview and update buffer."
  1735. (interactive)
  1736. ;; (with-current-buffer awk-preview--source-buffer
  1737. ;; (awk-preview--invoke-awk (current-buffer)
  1738. ;; awk-preview--point-beg
  1739. ;; awk-preview--point-end
  1740. ;; progfile
  1741. ;; t
  1742. ;; t))
  1743. (awk-preview--cleanup))
  1744. (defun awk-preview-abort ()
  1745. "Discard result and exit awk-preview."
  1746. (interactive)
  1747. (awk-preview--cleanup))
  1748. (defun awk-preview--cleanup()
  1749. "Cleanup awk preview buffers and variables.")
  1750. (defvar awk-preview-program-mode-map
  1751. (let ((map (make-sparse-keymap)))
  1752. (define-key map (kbd "C-c C-l") 'awk-preview-update-preview)
  1753. (define-key map (kbd "C-c C-k") 'awk-preview-abort)
  1754. (define-key map (kbd "C-c C-c") 'awk-preview-commit)
  1755. map)
  1756. "Keymap for `awk-preview-program-mode'.")
  1757. (define-minor-mode awk-preview-program-mode
  1758. "Minor mode for awk-preview program buffer."
  1759. :lighter " AWKPrev"
  1760. :keymap awk-preview-program-mode-map)
  1761. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1762. ;; editorconfig-auto-apply
  1763. (define-minor-mode editorconfig-auto-apply-mode
  1764. "When saving .editorconfig file update buffer configs."
  1765. :global t
  1766. :lighter ""
  1767. (if editorconfig-auto-apply-mode
  1768. (add-hook 'after-save-hook
  1769. 'editorconfig-auto-apply-mode--run)
  1770. (remove-hook 'after-save-hook
  1771. 'editorconfig-auto-apply-mode--run)))
  1772. (defun editorconfig-auto-apply-mode--run ()
  1773. "When saving .editorconfig file walk all buffers and update configs."
  1774. (when (eq major-mode
  1775. 'editorconfig-conf-mode)
  1776. (let ((dir (file-name-directory buffer-file-name)))
  1777. (cl-dolist (buf (buffer-list))
  1778. (when (and (buffer-file-name buf)
  1779. (file-in-directory-p (buffer-file-name buf)
  1780. dir))
  1781. (with-current-buffer buf
  1782. (editorconfig-mode-apply)))))))
  1783. (editorconfig-auto-apply-mode 1)
  1784. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1785. ;; recently
  1786. (defgroup recently nil
  1787. "Recently visited files"
  1788. :tag "Recently"
  1789. :prefix "recently-"
  1790. :group 'tools)
  1791. (defcustom recently-file
  1792. (concat user-emacs-directory
  1793. "recently.el")
  1794. "Recently file."
  1795. :type 'string
  1796. :group 'recently)
  1797. (defcustom recently-max
  1798. 100
  1799. "Recently list max length."
  1800. :type 'int
  1801. :group 'recently)
  1802. (defcustom recently-excludes
  1803. '()
  1804. "List of regexps for filenames excluded from the recent list."
  1805. :type '(repeat string)
  1806. :group 'recently)
  1807. (add-to-list 'recently-excludes
  1808. (eval-when-compile (rx "/COMMIT_EDITMSG" eot)))
  1809. (defvar recently-list
  1810. '()
  1811. "Recently list.")
  1812. (defvar recently-file-mtime
  1813. nil
  1814. "Modified time of file when last read file.")
  1815. (defun recently-write ()
  1816. "Write to file."
  1817. (with-temp-buffer
  1818. (prin1 recently-list
  1819. (current-buffer))
  1820. (write-region (point-min)
  1821. (point-max)
  1822. recently-file)))
  1823. (defun recently-read ()
  1824. "Read file."
  1825. (when (file-readable-p recently-file)
  1826. (with-temp-buffer
  1827. (insert-file-contents recently-file)
  1828. (goto-char (point-min))
  1829. (setq recently-list
  1830. (read (current-buffer))))
  1831. (setq recently-file-mtime
  1832. (nth 5
  1833. (file-attributes recently-file)))))
  1834. (defun recently-reload ()
  1835. "Reload file."
  1836. (when (and (file-readable-p recently-file)
  1837. (not (equal recently-file-mtime
  1838. (nth 5
  1839. (file-attributes recently-file)))))
  1840. (recently-read)
  1841. (cl-assert (equal recently-file-mtime
  1842. (nth 5
  1843. (file-attributes recently-file))))))
  1844. (defun recently-add (path)
  1845. "Add PATH to list."
  1846. (cl-assert (string= path
  1847. (expand-file-name path)))
  1848. (when (cl-loop for re in recently-excludes
  1849. if (string-match re path) return nil
  1850. finally return t)
  1851. (recently-reload)
  1852. (let* ((l (copy-list recently-list))
  1853. (l (delete path
  1854. l))
  1855. (l (cl-loop for e in l
  1856. unless (file-in-directory-p path e)
  1857. collect e))
  1858. (l (cons path
  1859. l))
  1860. (l (recently--truncate l
  1861. recently-max)))
  1862. (unless (equal recently-list
  1863. l)
  1864. (setq recently-list l)
  1865. (recently-write)))))
  1866. (defun recently--truncate (list len)
  1867. "Truncate LIST to LEN."
  1868. (if (> (length list)
  1869. len)
  1870. (cl-subseq list
  1871. 0
  1872. len)
  1873. list))
  1874. (defun recently-find-file-hook ()
  1875. "Add current file."
  1876. (when buffer-file-name
  1877. (recently-add buffer-file-name)))
  1878. (defun recently-dired-mode-hook ()
  1879. "Add current directory."
  1880. (recently-add (expand-file-name default-directory)))
  1881. (add-hook 'find-file-hook
  1882. 'recently-find-file-hook)
  1883. (add-hook 'dired-mode-hook
  1884. 'recently-dired-mode-hook)
  1885. ;;;;;;;;;;;;;;;;
  1886. ;; recently-show
  1887. (defvar recently-show-window-height 10
  1888. "Max height of window of `recently-show'")
  1889. ;; (defvar recently-show-mode-map
  1890. ;; (let ((map (make-sparse-keymap)))
  1891. ;; (suppress-keymap map)
  1892. ;; (define-key map "n" 'next-line)
  1893. ;; (define-key map "p" 'previous-line)
  1894. ;; (define-key map (kbd "C-m") 'recently-show-find-file)
  1895. ;; (define-key map (kbd "SPC") 'recently-show-find-file)
  1896. ;; (define-key map "v" 'recently-show-view-file)
  1897. ;; (define-key map "@" 'recently-show-dired)
  1898. ;; (define-key map "q" 'recently-show-close)
  1899. ;; (define-key map (kbd "C-g") 'recently-show-close)
  1900. ;; (define-key map "?" 'describe-mode)
  1901. ;; (define-key map "/" 'isearch-forward)
  1902. ;; map))
  1903. ;; (defvar recently-show-before-listing-hook nil
  1904. ;; "Hook run before creating buffer of `recently-show'.")
  1905. (defvar recently-show-window-configuration nil
  1906. "Used for internal")
  1907. (defvar recently-show-abbreviate t
  1908. "Non-nil means use `abbreviate-file-name' when listing recently opened files.")
  1909. ;; (define-derived-mode recently-show-mode special-mode "recently-show"
  1910. ;; "Major mode for `recently-show'."
  1911. ;; ;; (set (make-local-variable 'scroll-margin)
  1912. ;; ;; 0)
  1913. ;; )
  1914. ;;;###autoload
  1915. (defun recently-show (&optional files buffer-name)
  1916. "Show simplified list of recently opened files.
  1917. If optional argument FILES is non-nil, it is a list of recently-opened
  1918. files to choose from. It defaults to the whole recent list.
  1919. If optional argument BUFFER-NAME is non-nil, it is a buffer name to
  1920. use for the buffer. It defaults to \"*recetf-show*\"."
  1921. (interactive)
  1922. (let ((bf (recently-show--create-buffer-tabulated files buffer-name)))
  1923. (if bf
  1924. (progn
  1925. ;; (recently-save-list)
  1926. (setq recently-show-window-configuration (current-window-configuration))
  1927. ;;(pop-to-buffer bf t t)
  1928. (display-buffer bf)
  1929. ;; (set-window-text-height (selected-window)
  1930. ;; recently-show-window-height)
  1931. ;; (shrink-window-if-larger-than-buffer (selected-window))
  1932. )
  1933. (message "No recent file!"))))
  1934. (defun recently-show--create-buffer-tabulated (&optional files buffer-name)
  1935. "Create buffer listing recently files FILES."
  1936. (let ((bname (or buffer-name
  1937. "*recently-show-tabulated*"))
  1938. (list (or files
  1939. (progn
  1940. (recently-reload)
  1941. recently-list))))
  1942. (when list
  1943. (when (get-buffer bname)
  1944. (kill-buffer bname))
  1945. (with-current-buffer (get-buffer-create bname)
  1946. ;; (setq tabulated-list-sort-key (cons "Name" nil))
  1947. (setq tabulated-list-entries
  1948. (mapcar (lambda (f)
  1949. (list f
  1950. (vector (file-name-nondirectory f)
  1951. (if recently-show-abbreviate
  1952. (abbreviate-file-name f)
  1953. f))))
  1954. ;; list
  1955. recently-list
  1956. ))
  1957. (let ((max
  1958. (apply 'max
  1959. (mapcar (lambda (l)
  1960. (length (elt (cadr l) 0)))
  1961. tabulated-list-entries))))
  1962. (setq tabulated-list-format
  1963. `[("Name"
  1964. ,(min max
  1965. 30)
  1966. t)
  1967. ("Full Path" 0 t)])
  1968. )
  1969. (recently-show-tabulated-mode)
  1970. (current-buffer)))))
  1971. (defun recently-show-tabulated-find-file ()
  1972. "Find-file in `recently-show-tabulated-mode'."
  1973. (interactive)
  1974. (let ((f (tabulated-list-get-id)))
  1975. (when f
  1976. (recently-show-tabulated-close)
  1977. (find-file f))))
  1978. (defvar recently-show-tabulated-mode-map
  1979. (let ((map (make-sparse-keymap)))
  1980. (suppress-keymap map)
  1981. (define-key map (kbd "C-m") 'recently-show-tabulated-find-file)
  1982. ;; TODO: implement
  1983. (define-key map "v" 'recently-show-tabulated-view-file)
  1984. ;; TODO: implement
  1985. (define-key map "@" 'recently-show-tabulated-dired)
  1986. (define-key map (kbd "C-g") 'recently-show-tabulated-close)
  1987. (define-key map "/" 'isearch-forward)
  1988. map))
  1989. (define-derived-mode recently-show-tabulated-mode tabulated-list-mode "Recently Show"
  1990. "Major mode for browsing recently opened files and directories."
  1991. (setq tabulated-list-padding 2)
  1992. ;; TODO: Implement revert
  1993. ;; (add-hook 'tabulated-list-revert-hook 'recently-reload nil t)
  1994. (tabulated-list-init-header)
  1995. (tabulated-list-print nil nil))
  1996. ;; (defun recently-show-create-buffer (&optional files buffer-name)
  1997. ;; "Create buffer listing recently files."
  1998. ;; (let ((bname (or buffer-name
  1999. ;; "*recently-show*"))
  2000. ;; (list (or files
  2001. ;; (progn
  2002. ;; (recently-reload)
  2003. ;; recently-list))))
  2004. ;; (when list
  2005. ;; (and (get-buffer bname)
  2006. ;; (kill-buffer bname))
  2007. ;; (let ((bf (get-buffer-create bname)))
  2008. ;; (with-current-buffer bf
  2009. ;; (recently-show-mode)
  2010. ;; (let ((inhibit-read-only t))
  2011. ;; (mapc (lambda (f)
  2012. ;; (insert (if recently-show-abbreviate
  2013. ;; (abbreviate-file-name f)
  2014. ;; f)
  2015. ;; "\n"))
  2016. ;; list))
  2017. ;; (goto-char (point-min))
  2018. ;; ;; (setq buffer-read-only t)
  2019. ;; )
  2020. ;; bf))))
  2021. ;; (defun recently-show-close ()
  2022. ;; "Close recently-show window."
  2023. ;; (interactive)
  2024. ;; (kill-buffer (current-buffer))
  2025. ;; (set-window-configuration recently-show-window-configuration))
  2026. (defun recently-show-tabulated-close ()
  2027. "Close recently-show window."
  2028. (interactive)
  2029. (kill-buffer (current-buffer))
  2030. (set-window-configuration recently-show-window-configuration))
  2031. ;; (defun recently-show-find-file ()
  2032. ;; "Fine file of current line."
  2033. ;; (interactive)
  2034. ;; (let ((f (recently-show-get-filename)))
  2035. ;; (recently-show-close)
  2036. ;; (find-file f)))
  2037. ;; (defun recently-show-view-file ()
  2038. ;; "view file of current line."
  2039. ;; (interactive)
  2040. ;; (let ((f (recently-show-get-filename)))
  2041. ;; (recently-show-close)
  2042. ;; (view-file f)))
  2043. ;; (defun recently-show-get-filename ()
  2044. ;; "Get filename of current line."
  2045. ;; (buffer-substring-no-properties (point-at-bol)
  2046. ;; (point-at-eol)))
  2047. ;; (defun recently-show-dired()
  2048. ;; "Open dired buffer of directory containing file of current line."
  2049. ;; (interactive)
  2050. ;; (let ((f (recently-show-get-filename)))
  2051. ;; (recently-show-close)
  2052. ;; (dired (if (file-directory-p f)
  2053. ;; f
  2054. ;; (or (file-name-directory f)
  2055. ;; ".")))))
  2056. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  2057. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2058. ;; git walktree
  2059. (defgroup git-walktree nil
  2060. "Git Walktree."
  2061. :tag "GitWalktree"
  2062. :prefix "git-walktree-"
  2063. :group 'tools)
  2064. (defvar git-walktree-current-committish nil
  2065. "Committish name of currently browsing.")
  2066. (make-variable-buffer-local 'git-walktree-current-committish)
  2067. (defvar git-walktree-current-path nil
  2068. "Path name currently visiting without leading and trailing slash.
  2069. This path is always relative to repository root.")
  2070. (make-variable-buffer-local 'git-walktree-current-path)
  2071. (defvar git-walktree-buffer-file-name nil
  2072. "Psudo filename of current buffer.")
  2073. (make-variable-buffer-local 'git-walktree-buffer-file-name)
  2074. (defvar git-walktree-object-full-sha1 nil
  2075. "Object name in full sha1 format of current buffer.")
  2076. (make-variable-buffer-local 'git-walktree-object-full-sha1)
  2077. (defvar git-walktree-repository-root nil
  2078. "Repository root path of current buffer.")
  2079. (make-variable-buffer-local 'git-walktree-repository-root)
  2080. (put 'git-walktree-repository-root
  2081. 'permanent-local
  2082. t)
  2083. (defun git-walktree--committish-fordisplay (committish)
  2084. "Convert COMMITTISH and return is a suitable format for displaying."
  2085. (if (and committish
  2086. (string-match-p "\\`[0-9a-f]+\\'"
  2087. committish)
  2088. (>= (length committish) 32))
  2089. (git-walktree--git-plumbing "rev-parse"
  2090. "--short"
  2091. committish)
  2092. committish))
  2093. (defun git-walktree--create-buffer (committish name type)
  2094. "Create and return buffer for COMMITTISH:NAME.
  2095. TYPE is target object type."
  2096. (let* ((root (git-walktree--git-plumbing "rev-parse"
  2097. "--show-toplevel"))
  2098. (committish-display (git-walktree--committish-fordisplay committish))
  2099. (name (format "%s:%s"
  2100. (or committish-display "")
  2101. name)))
  2102. (if (and git-walktree-reuse-tree-buffer
  2103. (string= type "tree"))
  2104. (with-current-buffer (or git-walktree-tree-buffer-for-reuse
  2105. (setq git-walktree-tree-buffer-for-reuse
  2106. (generate-new-buffer "gitwalktreebuf")))
  2107. (setq git-walktree-repository-root root)
  2108. (rename-buffer name t)
  2109. (current-buffer))
  2110. (with-current-buffer (get-buffer-create name)
  2111. (if git-walktree-repository-root
  2112. (if (string= root
  2113. git-walktree-repository-root)
  2114. (current-buffer)
  2115. ;; If the buffer is for another repository, create new buffer
  2116. (with-current-buffer (generate-new-buffer name)
  2117. (setq git-walktree-repository-root root)
  2118. (current-buffer)))
  2119. ;; New buffer
  2120. (setq git-walktree-repository-root root)
  2121. (current-buffer))))))
  2122. (defun git-walktree--replace-into-buffer (target)
  2123. "Replace TARGET buffer contents with that of current buffer.
  2124. It also copy text overlays."
  2125. (let ((src (current-buffer)))
  2126. (with-current-buffer target
  2127. (replace-buffer-contents src)))
  2128. ;; Copy color overlays
  2129. (let ((overlays (overlays-in (point-min) (point-max))))
  2130. (dolist (o overlays)
  2131. (let ((beg (overlay-start o))
  2132. (end (overlay-end o)))
  2133. (move-overlay (copy-overlay o)
  2134. beg
  2135. end
  2136. target)))))
  2137. (require 'ansi-color)
  2138. (defun git-walktree--open-treeish (committish path treeish)
  2139. "Open git tree buffer of COMMITISH:PATH.
  2140. TREEISH should be a tree-ish object full-sha1 of COMMITISH:PATH."
  2141. (cl-assert path)
  2142. (cl-assert treeish)
  2143. (let* (point-tree-start
  2144. (type (git-walktree--git-plumbing "cat-file"
  2145. "-t"
  2146. treeish))
  2147. (buf (git-walktree--create-buffer committish path type))
  2148. )
  2149. (cl-assert (member type
  2150. '("commit" "tree")))
  2151. (with-current-buffer buf
  2152. (unless (and (string= treeish
  2153. git-walktree-object-full-sha1)
  2154. (or (eq committish
  2155. git-walktree-current-committish)
  2156. (string= committish
  2157. git-walktree-current-committish)))
  2158. (buffer-disable-undo)
  2159. ;; For running git command go back to repository root
  2160. (cd git-walktree-repository-root)
  2161. (save-excursion
  2162. (let ((inhibit-read-only t))
  2163. ;; Remove existing overlays generated by ansi-color-apply-on-region
  2164. (remove-overlays)
  2165. (with-temp-buffer
  2166. (if committish
  2167. (progn (git-walktree--call-process nil
  2168. "show"
  2169. ;; TODO: Make this args configurable
  2170. ;; "--no-patch"
  2171. "--color=always"
  2172. "--pretty=short"
  2173. "--decorate"
  2174. "--stat"
  2175. committish)
  2176. (ansi-color-apply-on-region (point-min)
  2177. (point))
  2178. (insert "\n")
  2179. (insert (format "Contents of '%s:%s':\n"
  2180. (git-walktree--committish-fordisplay committish)
  2181. path)))
  2182. (insert (format "Contents of treeish object '%s:\n"
  2183. treeish)))
  2184. (setq point-tree-start (point))
  2185. (git-walktree--call-process nil
  2186. "ls-tree"
  2187. ;; "-r"
  2188. "--abbrev"
  2189. treeish)
  2190. (git-walktree--replace-into-buffer buf))
  2191. ))
  2192. (git-walktree-mode)
  2193. (set-buffer-modified-p nil)
  2194. (setq git-walktree-current-committish committish)
  2195. (setq git-walktree-current-path path)
  2196. (setq git-walktree-object-full-sha1 treeish)
  2197. (let ((dir (expand-file-name path git-walktree-repository-root)))
  2198. (when (and git-walktree-try-cd
  2199. (file-directory-p dir))
  2200. (cd dir)))
  2201. (when (= (point) (point-min))
  2202. (goto-char point-tree-start)
  2203. (git-walktree-mode--move-to-file)
  2204. )
  2205. ))
  2206. buf))
  2207. (defun git-walktree--call-process (&optional infile &rest args)
  2208. "Call git command with input from INFILE and args ARGS.
  2209. Result will be inserted into current buffer."
  2210. (let ((status (apply 'call-process
  2211. git-walktree-git-executable
  2212. infile
  2213. t
  2214. nil
  2215. args)))
  2216. (unless (eq 0
  2217. status)
  2218. (error "Failed to call git process %S %S"
  2219. infile
  2220. args))))
  2221. ?w
  2222. (defun git-walktree--open-blob (committish path blob)
  2223. "Open blob object of COMMITISH:PATH.
  2224. BLOB should be a object full sha1 of COMMITISH:PATH."
  2225. (cl-assert committish)
  2226. (cl-assert path)
  2227. (cl-assert blob)
  2228. (let* ((type (git-walktree--git-plumbing "cat-file"
  2229. "-t"
  2230. blob))
  2231. (buf (git-walktree--create-buffer committish path type)))
  2232. (cl-assert (string= type "blob"))
  2233. (with-current-buffer buf
  2234. (unless (string= blob
  2235. git-walktree-object-full-sha1)
  2236. ;; For running git command go back to repository root
  2237. (cd git-walktree-repository-root)
  2238. (let ((inhibit-read-only t))
  2239. (with-temp-buffer
  2240. (git-walktree--call-process nil
  2241. "cat-file"
  2242. "-p"
  2243. blob)
  2244. (git-walktree--replace-into-buffer buf)))
  2245. (setq git-walktree-buffer-file-name
  2246. (concat git-walktree-repository-root "/git@" committish ":" path))
  2247. (setq buffer-file-name
  2248. (concat git-walktree-repository-root "/" path))
  2249. (normal-mode t)
  2250. ;; For asking filename when C-xC-s
  2251. (setq buffer-file-name nil)
  2252. (set-buffer-modified-p t)
  2253. (setq git-walktree-current-committish committish)
  2254. (setq git-walktree-current-path path)
  2255. (setq git-walktree-object-full-sha1 blob)
  2256. (let ((dir (expand-file-name (or (file-name-directory path)
  2257. ".")
  2258. git-walktree-repository-root)))
  2259. (when (and git-walktree-try-cd
  2260. (file-directory-p dir))
  2261. (cd dir)))
  2262. (view-mode 1)
  2263. ))
  2264. buf))
  2265. (defun git-walktree--open-noselect-safe-path (committish &optional path)
  2266. "Open git object of COMMITTISH:PATH.
  2267. If PATH not found in COMMITTISH tree, go up path and try again until found.
  2268. When PATH is omitted or nil, it is calculated from current file or directory."
  2269. (cl-assert committish)
  2270. (let ((type (git-walktree--git-plumbing "cat-file"
  2271. "-t"
  2272. committish)))
  2273. (cl-assert (string= type "commit")))
  2274. (setq path
  2275. (or path
  2276. (git-walktree--path-in-repository (or buffer-file-name
  2277. default-directory))))
  2278. ;; PATH must not start with and end with slashes
  2279. (cl-assert (not (string-match-p "\\`/" path)))
  2280. (cl-assert (not (string-match-p "/\\'" path)))
  2281. (let ((obj (git-walktree--resolve-object committish path)))
  2282. (while (not obj)
  2283. (setq path
  2284. (git-walktree--parent-directory path))
  2285. (setq obj
  2286. (git-walktree--resolve-object committish path)))
  2287. (git-walktree--open-noselect committish
  2288. path
  2289. obj)))
  2290. ;; TODO: Store view history
  2291. (defun git-walktree--open-noselect (committish path object)
  2292. "Open buffer to view git object of COMMITTISH:PATH.
  2293. When PATH was given and non-nil open that, otherwise open root tree.
  2294. When OBJECT was given and non-nil, assume that is the object full sha1 of
  2295. COMMITTISH:PATH without checking it."
  2296. (cl-assert committish)
  2297. (let ((type (git-walktree--git-plumbing "cat-file"
  2298. "-t"
  2299. committish)))
  2300. (cl-assert (string= type "commit")))
  2301. (setq path (or path
  2302. "."))
  2303. ;; PATH must not start with and end with slashes
  2304. (cl-assert (not (string-match-p "\\`/" path)))
  2305. (cl-assert (not (string-match-p "/\\'" path)))
  2306. (setq object (or object
  2307. (git-walktree--resolve-object committish path)))
  2308. (setq object (git-walktree--git-plumbing "rev-parse"
  2309. object))
  2310. (cl-assert object)
  2311. (let ((type (git-walktree--git-plumbing "cat-file"
  2312. "-t"
  2313. object)))
  2314. (pcase type
  2315. ((or "commit" "tree")
  2316. (git-walktree--open-treeish committish path object))
  2317. ("blob"
  2318. (git-walktree--open-blob committish path object))
  2319. (_
  2320. (error "Type cannot handle: %s" type)))))
  2321. (defun git-walktree--resolve-object (committish path)
  2322. "Return object full sha1 name of COMMITISIH:PATH.
  2323. If path is equal to \".\" return COMMITTISH's root tree object.
  2324. PATH will be always treated as relative to repository root."
  2325. (cl-assert committish)
  2326. (cl-assert path)
  2327. (cl-assert (not (string-match-p "\\`/" path)))
  2328. (cl-assert (not (string-match-p "/\\'" path)))
  2329. (if (string= path ".")
  2330. (git-walktree--git-plumbing "show"
  2331. "--no-patch"
  2332. "--pretty=format:%T"
  2333. committish)
  2334. (let ((info (git-walktree--parse-lstree-line (git-walktree--git-plumbing "ls-tree"
  2335. "--full-tree"
  2336. committish
  2337. path))))
  2338. (plist-get info :object))))
  2339. (defun git-walktree-open (committish &optional path)
  2340. "Open git tree buffer of COMMITTISH.
  2341. When PATH was given and non-nil open that, otherwise try to open current path.
  2342. If target path is not found in COMMITISH tree, go up path and try again until found."
  2343. ;; TODO: Add fallback method for cases where magit is not available
  2344. (interactive (list (magit-read-branch-or-commit "Revision: ")))
  2345. (switch-to-buffer (git-walktree--open-noselect-safe-path committish path)))
  2346. (defalias 'git-walktree 'git-walktree-open)
  2347. (defun git-walktree--path-in-repository (path)
  2348. "Convert PATH into relative path to repository root.
  2349. Result will not have leading and trailing slashes."
  2350. (with-temp-buffer
  2351. (cd (if (file-directory-p path)
  2352. path
  2353. (file-name-directory path)))
  2354. (let ((root (git-walktree--git-plumbing "rev-parse"
  2355. "--show-toplevel")))
  2356. (file-relative-name (directory-file-name path)
  2357. root))))
  2358. (defcustom git-walktree-git-executable "git"
  2359. "Git executable."
  2360. :type 'string
  2361. :group 'git-walktree)
  2362. (defcustom git-walktree-try-cd t
  2363. "Try to cd if directory exists in current working directory if non-nil.
  2364. Otherwise use repository root for gitwalktree buffer's `default-directory'."
  2365. :type 'boolean
  2366. :group 'git-walktree)
  2367. (defcustom git-walktree-reuse-tree-buffer t
  2368. "Non-nil to reuse buffer for treeish object."
  2369. :type 'boolean
  2370. :group 'git-walktree)
  2371. (defvar git-walktree-tree-buffer-for-reuse nil
  2372. "Buffer to use when `git-walktree-reuse-tree-buffer' is non-nil.")
  2373. (defun git-walktree--git-plumbing (&rest args)
  2374. "Run git plubming command with ARGS.
  2375. Returns first line of output without newline."
  2376. (with-temp-buffer
  2377. (let ((status (apply 'call-process
  2378. git-walktree-git-executable
  2379. nil
  2380. t
  2381. nil
  2382. args)))
  2383. (unless (eq 0
  2384. status)
  2385. (error "Faild to run git %S:\n%s"
  2386. args
  2387. (buffer-substring-no-properties (point-min)
  2388. (point-max))))
  2389. (buffer-substring-no-properties (point-min)
  2390. (progn
  2391. (goto-char (point-min))
  2392. (point-at-eol))))))
  2393. (defconst git-walktree-ls-tree-line-regexp
  2394. "^\\([0-9]\\{6\\}\\) \\(\\w+\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2395. "Regexp for one line of output of git ls-tree.")
  2396. (defconst git-walktree-ls-tree-line-tree-regexp
  2397. "^\\([0-9]\\{6\\}\\) \\(tree\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2398. "Regexp for tree line of output of git ls-tree.")
  2399. (defconst git-walktree-ls-tree-line-commit-regexp
  2400. "^\\([0-9]\\{6\\}\\) \\(commit\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2401. "Regexp for commit line of output of git ls-tree.")
  2402. (defun git-walktree--parse-lstree-line (str)
  2403. "Extract object info from STR.
  2404. STR should be a string like following without newline.:
  2405. 100644 blob 6fd4d58202d0b46547c6fe43de0f8c878456f966 .editorconfig
  2406. Returns property list like (:mode MODE :type TYPE :object OBJECT :file FILE)."
  2407. (let (result mode type object file)
  2408. (save-match-data
  2409. (with-temp-buffer
  2410. (insert str)
  2411. (goto-char (point-min))
  2412. (and (re-search-forward git-walktree-ls-tree-line-regexp
  2413. nil
  2414. t)
  2415. (list :mode (match-string 1)
  2416. :type (match-string 2)
  2417. :object (match-string 3)
  2418. :file (match-string 4)))))))
  2419. (defun git-walktree-mode-open-this ()
  2420. "Open git object of current line."
  2421. (interactive)
  2422. (let ((info (git-walktree--parse-lstree-line (buffer-substring-no-properties (point-at-bol)
  2423. (point-at-eol)))))
  2424. (if info
  2425. (switch-to-buffer
  2426. (if (string= (plist-get info
  2427. :type)
  2428. "commit")
  2429. ;; For submodule cd to that directory and intialize
  2430. ;; TODO: Provide way to go back to known "parent" repository
  2431. (with-temp-buffer
  2432. (cd (plist-get info :file))
  2433. (git-walktree--open-noselect (plist-get info
  2434. :object)
  2435. nil
  2436. (plist-get info
  2437. :object)))
  2438. (git-walktree--open-noselect git-walktree-current-committish
  2439. (git-walktree--join-path (plist-get info
  2440. :file))
  2441. (plist-get info
  2442. :object))))
  2443. (message "No object on current line."))))
  2444. (defun git-walktree--join-path (name &optional base)
  2445. "Make path from NAME and BASE.
  2446. If base is omitted or nil use value of `git-walktree-current-path'."
  2447. (setq base (or base
  2448. git-walktree-current-path))
  2449. (cl-assert base)
  2450. (if (string= base ".")
  2451. name
  2452. (concat base "/" name)))
  2453. (defun git-walktree--parent-directory (path)
  2454. "Return parent directory of PATH without trailing slash.
  2455. For root directory return \".\".
  2456. If PATH is equal to \".\", return nil."
  2457. (if (string-match-p "/" path)
  2458. (directory-file-name (file-name-directory path))
  2459. (if (string= "." path)
  2460. nil
  2461. ".")))
  2462. (defun git-walktree-up (&optional committish path)
  2463. "Open parent directory of COMMITTISH and PATH.
  2464. If not given, value of current buffer will be used."
  2465. (interactive)
  2466. (setq committish
  2467. (or committish git-walktree-current-committish))
  2468. (setq path
  2469. (or path git-walktree-current-path))
  2470. (let ((parent (git-walktree--parent-directory path)))
  2471. (if parent
  2472. (switch-to-buffer (git-walktree--open-noselect committish
  2473. parent
  2474. nil))
  2475. (message "Cannot find parent directory for current tree."))))
  2476. (defun git-walktree-mode--move-to-file ()
  2477. "Move point to file field of ls-tree output in current line.
  2478. This function do nothing when current line is not ls-tree output."
  2479. (interactive)
  2480. (save-match-data
  2481. (when (save-excursion
  2482. (goto-char (point-at-bol))
  2483. (re-search-forward git-walktree-ls-tree-line-regexp
  2484. (point-at-eol) t))
  2485. (goto-char (match-beginning 4)))))
  2486. (defun git-walktree-mode-next-line (&optional arg try-vscroll)
  2487. "Move cursor vertically down ARG lines and move to file field if found."
  2488. (interactive "^p\np")
  2489. (or arg (setq arg 1))
  2490. (line-move arg nil nil try-vscroll)
  2491. (git-walktree-mode--move-to-file)
  2492. )
  2493. (defun git-walktree-mode-previous-line (&optional arg try-vscroll)
  2494. "Move cursor vertically up ARG lines and move to file field if found."
  2495. (interactive "^p\np")
  2496. (or arg (setq arg 1))
  2497. (line-move (- arg) nil nil try-vscroll)
  2498. (git-walktree-mode--move-to-file)
  2499. )
  2500. (defgroup git-walktree-faces nil
  2501. "Faces used by git-walktree."
  2502. :group 'git-walktree
  2503. :group 'faces)
  2504. (defface git-walktree-tree-face
  2505. ;; Same as dired-directory
  2506. '((t (:inherit font-lock-function-name-face)))
  2507. "Face used for tree objects."
  2508. :group 'git-walktree-faces)
  2509. (defface git-walktree-commit-face
  2510. ;; Same as dired-symlink face
  2511. '((t (:inherit font-lock-keyword-face)))
  2512. "Face used for commit objects."
  2513. :group 'git-walktree-faces)
  2514. (defvar git-walktree-known-child-revisions (make-hash-table :test 'equal)
  2515. "Hash of already known pair of commitid -> list of child commitid.
  2516. Both values should be object full sha1 names.")
  2517. (defun git-walktree--put-child (parent child)
  2518. "Register PARENT and CHILD relationship.
  2519. PARENT should be a full sha1 object name."
  2520. ;; Any way to check if PARENT is a full SHA-1 object name?
  2521. (let ((current (gethash parent git-walktree-known-child-revisions)))
  2522. (unless (member child current)
  2523. (puthash parent
  2524. (cons child
  2525. current)
  2526. git-walktree-known-child-revisions))))
  2527. ;; TODO: Add aggressive search mode
  2528. ;; https://stackoverflow.com/a/9870218
  2529. ;; git log --reverse --pretty=format:%H -n 1 --ancestry-path <PARENT>..HEAD
  2530. (defun git-walktree--get-children (parent)
  2531. "Get known children list of PARENT commit.
  2532. PARENT should be a full sha1 object name."
  2533. (gethash parent git-walktree-known-child-revisions))
  2534. (defun git-walktree--choose-committish (prompt-format collection)
  2535. "Emit PROMPT-FORMAT and ask user to which committish of COLLECTION to use.
  2536. When collection has just one element, return the first element without asking."
  2537. (cl-assert collection)
  2538. (if (< (length collection) 2)
  2539. (car collection)
  2540. (completing-read (format prompt-format
  2541. (mapconcat 'git-walktree--committish-fordisplay
  2542. collection
  2543. " "))
  2544. collection
  2545. nil
  2546. t)))
  2547. (defun git-walktree-parent-revision ()
  2548. "Open parent revision of current path.
  2549. If current path was not found in the parent revision try to go up path."
  2550. (interactive)
  2551. (cl-assert git-walktree-current-committish)
  2552. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2553. git-walktree-current-committish))
  2554. (parents (git-walktree--parent-full-sha1 commit-full-sha1)))
  2555. (dolist (parent parents)
  2556. (git-walktree--put-child parent
  2557. commit-full-sha1))
  2558. (if (< (length parents)
  2559. 1)
  2560. (message "This revision has no parent revision")
  2561. (let* ((parent (git-walktree--choose-committish "This revision has multiple parents. Which to open? (%s) "
  2562. parents))
  2563. (path git-walktree-current-path))
  2564. (cl-assert path)
  2565. (switch-to-buffer (git-walktree--open-noselect-safe-path parent
  2566. path))))))
  2567. (defun git-walktree--parent-full-sha1 (committish)
  2568. "Return list of parent commits of COMMITTISH in sha1 string."
  2569. (let ((type (git-walktree--git-plumbing "cat-file"
  2570. "-t"
  2571. committish)))
  2572. (cl-assert (string= type "commit")))
  2573. (let ((parents (git-walktree--git-plumbing "show"
  2574. "--no-patch"
  2575. "--pretty=format:%P"
  2576. committish)))
  2577. (split-string parents)))
  2578. (defun git-walktree-known-child-revision ()
  2579. "Open known revision of current path."
  2580. (interactive)
  2581. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2582. git-walktree-current-committish))
  2583. (children (git-walktree--get-children commit-full-sha1)))
  2584. (if (< (length children)
  2585. 1)
  2586. (message "There are no known child revision")
  2587. (let* ((child (git-walktree--choose-committish "There are multiple known childrens. Which to open? (%s)"
  2588. children))
  2589. (path git-walktree-current-path))
  2590. (cl-assert path)
  2591. (switch-to-buffer (git-walktree--open-noselect-safe-path child
  2592. path))))))
  2593. (defvar git-walktree-mode-map
  2594. (let ((map (make-sparse-keymap)))
  2595. (define-key map "n" 'git-walktree-mode-next-line)
  2596. (define-key map "p" 'git-walktree-mode-previous-line)
  2597. (define-key map (kbd "C-n") 'git-walktree-mode-next-line)
  2598. (define-key map (kbd "C-p") 'git-walktree-mode-previous-line)
  2599. ;; TODO: Review keybind
  2600. (define-key map "P" 'git-walktree-parent-revision)
  2601. (define-key map "N" 'git-walktree-known-child-revision)
  2602. (define-key map "^" 'git-walktree-up)
  2603. ;; TODO: implement
  2604. (define-key map (kbd "DEL") 'git-walktree-back)
  2605. (define-key map (kbd "C-m") 'git-walktree-mode-open-this)
  2606. map))
  2607. (defvar git-walktree-mode-font-lock-keywords
  2608. `(
  2609. (,git-walktree-ls-tree-line-regexp . (
  2610. (1 'shadow)
  2611. (3 'shadow)
  2612. ))
  2613. (,git-walktree-ls-tree-line-tree-regexp . (
  2614. (2 'git-walktree-tree-face)
  2615. (4 'git-walktree-tree-face)
  2616. ))
  2617. (,git-walktree-ls-tree-line-commit-regexp . (
  2618. (2 'git-walktree-commit-face)
  2619. (4 'git-walktree-commit-face)
  2620. ))
  2621. )
  2622. "Syntax highlighting for git-walktree mode.")
  2623. (define-derived-mode git-walktree-mode special-mode "GitWalktree"
  2624. "Major-mode for `git-walktree-open'."
  2625. (set (make-local-variable 'font-lock-defaults)
  2626. '(git-walktree-mode-font-lock-keywords
  2627. nil nil nil nil
  2628. ))
  2629. )
  2630. (require 'magit nil t)
  2631. ;; (git-revision--git-plumbing "cat-file" "-t" "HEAD")
  2632. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2633. ;; git-worktree
  2634. (defun git-worktree-get-current-list ()
  2635. "Get current worktree list."
  2636. (with-temp-buffer
  2637. (let ((trees nil)
  2638. (status (call-process "git"
  2639. nil
  2640. t
  2641. nil
  2642. "worktree" "list" "--porcelain")))
  2643. (cl-assert (eq status 0))
  2644. (goto-char (point-min))
  2645. (save-match-data
  2646. (while (not (eq (point) (point-max)))
  2647. (let ((worktree nil)
  2648. (head nil)
  2649. (branch nil))
  2650. (while (re-search-forward "^\\([^ ]\+\\) \\(.*\\)$" (point-at-eol) t)
  2651. (pcase (match-string 1)
  2652. ("worktree" (setq worktree (match-string 2)))
  2653. ("HEAD" (setq head (match-string 2)))
  2654. ("branch" (setq branch (match-string 2))))
  2655. (forward-line 1)
  2656. (goto-char (point-at-bol)))
  2657. (setq trees `(,@trees
  2658. (:worktree ,worktree :head ,head :branch ,branch)))
  2659. (forward-line 1)
  2660. (goto-char (point-at-bol)))
  2661. ))
  2662. trees)))
  2663. ;; Local Variables:
  2664. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2665. ;; flycheck-checker: emacs-lisp
  2666. ;; End:
  2667. ;;; emancs.el ends here