1 ;; stgit.el: An emacs mode for StGit
3 ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se>
5 ;; To install: put this file on the load-path and place the following
6 ;; in your .emacs file:
10 ;; To start: `M-x stgit'
13 "Manage stgit patches"
14 (interactive "DDirectory: \n")
15 (switch-to-stgit-buffer dir)
18 (defun switch-to-stgit-buffer (dir)
19 "Switch to a (possibly new) buffer displaying StGit patches for DIR"
20 (setq dir (file-name-as-directory dir))
21 (let ((buffers (buffer-list)))
23 (not (with-current-buffer (car buffers)
24 (and (eq major-mode 'stgit-mode)
25 (string= default-directory dir)))))
26 (setq buffers (cdr buffers)))
27 (switch-to-buffer (if buffers
29 (create-stgit-buffer dir)))))
31 (defun create-stgit-buffer (dir)
32 "Create a buffer for showing StGit patches.
33 Argument DIR is the repository path."
34 (let ((buf (create-file-buffer (concat dir "*stgit*")))
35 (inhibit-read-only t))
36 (with-current-buffer buf
37 (setq default-directory dir)
39 (setq buffer-read-only t))
42 (defmacro stgit-capture-output (name &rest body)
43 "Capture StGit output and show it in a window at the end"
44 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
45 (stgit-dir default-directory)
46 (inhibit-read-only t))
47 (with-current-buffer output-buf
49 (setq default-directory stgit-dir)
50 (setq buffer-read-only t))
51 (let ((standard-output output-buf))
53 (with-current-buffer output-buf
54 (set-buffer-modified-p nil)
55 (setq buffer-read-only t)
56 (if (< (point-min) (point-max))
57 (display-buffer output-buf t)))))
58 (put 'stgit-capture-output 'lisp-indent-function 1)
60 (defun stgit-run (&rest args)
61 (apply 'call-process "stg" nil standard-output nil args))
63 (defun stgit-refresh ()
64 "Update the contents of the stgit buffer"
66 (let ((inhibit-read-only t)
67 (curline (line-number-at-pos))
68 (curpatch (stgit-patch-at-point)))
72 (stgit-run "series" "--description")
73 (stgit-rehighlight (point-min) (point-max))
75 (stgit-goto-patch curpatch)
76 (goto-line curline))))
78 (defface stgit-description-face
79 '((((background dark)) (:foreground "tan"))
80 (((background light)) (:foreground "dark red")))
81 "The face used for StGit desriptions")
83 (defface stgit-top-patch-face
84 '((((background dark)) (:weight bold :foreground "yellow"))
85 (((background light)) (:weight bold :foreground "purple"))
87 "The face used for the top patch names")
89 (defface stgit-applied-patch-face
90 '((((background dark)) (:foreground "light yellow"))
91 (((background light)) (:foreground "purple"))
93 "The face used for applied patch names")
95 (defface stgit-unapplied-patch-face
96 '((((background dark)) (:foreground "gray80"))
97 (((background light)) (:foreground "orchid"))
99 "The face used for unapplied patch names")
101 (defun stgit-rehighlight (start end)
102 "Refresh fontification of region between START and END."
105 (while (< (point) end)
106 (cond ((looking-at "Branch: \\(.*\\)")
107 (put-text-property (match-beginning 1) (match-end 1) 'face 'bold))
108 ((looking-at "\\([>+-]\\) \\([^ ]+\\) *| \\(.*\\)")
109 (let ((state (match-string 1)))
111 (match-beginning 2) (match-end 2)
112 'face (cond ((string= state ">") 'stgit-top-patch-face)
113 ((string= state "+") 'stgit-applied-patch-face)
114 ((string= state "-") 'stgit-unapplied-patch-face)))
115 (put-text-property (match-beginning 3) (match-end 3)
116 'face 'stgit-description-face))))
119 (defvar stgit-mode-hook nil
120 "Run after `stgit-mode' is setup.")
122 (defvar stgit-mode-map nil
123 "Keymap for StGit major mode.")
125 (unless stgit-mode-map
126 (setq stgit-mode-map (make-keymap))
127 (suppress-keymap stgit-mode-map)
128 (define-key stgit-mode-map "?" 'stgit-help)
129 (define-key stgit-mode-map "h" 'stgit-help)
130 (define-key stgit-mode-map "p" 'previous-line)
131 (define-key stgit-mode-map "n" 'next-line)
132 (define-key stgit-mode-map "g" 'stgit-refresh)
133 (define-key stgit-mode-map "r" 'stgit-rename)
134 (define-key stgit-mode-map "e" 'stgit-edit)
135 (define-key stgit-mode-map "\C-r" 'stgit-repair)
136 (define-key stgit-mode-map "C" 'stgit-commit)
137 (define-key stgit-mode-map "U" 'stgit-uncommit)
138 (define-key stgit-mode-map ">" 'stgit-push-next)
139 (define-key stgit-mode-map "<" 'stgit-pop-next)
140 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
141 (define-key stgit-mode-map "G" 'stgit-goto)
142 (define-key stgit-mode-map "=" 'stgit-show))
145 "Major mode for interacting with StGit.
148 (kill-all-local-variables)
149 (buffer-disable-undo)
150 (setq mode-name "StGit"
151 major-mode 'stgit-mode
153 (use-local-map stgit-mode-map)
154 (set (make-local-variable 'list-buffers-directory) default-directory)
155 (set-variable 'truncate-lines 't)
156 (run-hooks 'stgit-mode-hook))
158 (defun stgit-patch-at-point ()
159 "Return the patch name on the current line"
162 (if (looking-at "[>+-] \\([^ ]*\\)")
163 (match-string-no-properties 1)
166 (defun stgit-goto-patch (patch)
167 "Move point to the line containing PATCH"
169 (goto-char (point-min))
170 (if (re-search-forward (concat "[>+-] " (regexp-quote patch) " ") nil t)
171 (progn (move-to-column goal-column)
176 (defun stgit-rename (name)
177 "Rename the patch under point"
178 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
179 (let ((old-name (stgit-patch-at-point)))
181 (error "No patch on this line"))
182 (stgit-capture-output nil
183 (stgit-run "rename" old-name name))
185 (stgit-goto-patch name)))
187 (defun stgit-repair ()
190 (stgit-capture-output nil
191 (stgit-run "repair"))
194 (defun stgit-commit ()
197 (stgit-capture-output nil (stgit-run "commit"))
200 (defun stgit-uncommit (arg)
201 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
203 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
206 (defun stgit-push-next ()
207 "Push the first unapplied patch"
209 (stgit-capture-output nil (stgit-run "push"))
212 (defun stgit-pop-next ()
213 "Pop the topmost applied patch"
215 (stgit-capture-output nil (stgit-run "pop"))
218 (defun stgit-applied-at-point ()
219 "Is the patch on the current line applied?"
222 (looking-at "[>+]")))
224 (defun stgit-push-or-pop ()
225 "Push or pop the patch on the current line"
227 (let ((patch (stgit-patch-at-point))
228 (applied (stgit-applied-at-point)))
229 (stgit-capture-output nil
230 (stgit-run (if applied "pop" "push") patch))
234 "Go to the patch on the current line"
236 (let ((patch (stgit-patch-at-point)))
237 (stgit-capture-output nil
238 (stgit-run "goto" patch))
242 "Show the patch on the current line"
244 (stgit-capture-output "*StGit patch*"
245 (stgit-run "show" (stgit-patch-at-point))
246 (with-current-buffer standard-output
247 (goto-char (point-min))
251 "Edit the patch on the current line"
253 (let ((patch (if (stgit-applied-at-point)
254 (stgit-patch-at-point)
255 (error "This patch is not applied")))
256 (edit-buf (get-buffer-create "*stgit edit*"))
257 (dir default-directory))
258 (log-edit 'stgit-confirm-edit t nil edit-buf)
259 (set (make-local-variable 'stgit-edit-patch) patch)
260 (setq default-directory dir)
261 (let ((standard-output edit-buf))
262 (stgit-run "edit" "--save-template=-" patch))))
264 (defun stgit-confirm-edit ()
266 (let ((file (make-temp-file "stgit-edit-")))
267 (write-region (point-min) (point-max) file)
268 (stgit-capture-output nil
269 (stgit-run "edit" "-f" file stgit-edit-patch))
270 (with-current-buffer log-edit-parent-buffer
275 "Display help for the StGit mode."
277 (describe-function 'stgit-mode))