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")
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-rescan ()
102 "Rescan the status buffer."
105 (goto-char (point-min))
107 (cond ((looking-at "Branch: \\(.*\\)")
108 (put-text-property (match-beginning 1) (match-end 1)
110 ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
111 (let ((state (match-string 1))
112 (patchsym (intern (match-string 3))))
114 (match-beginning 3) (match-end 3) 'face
115 (cond ((string= state ">") 'stgit-top-patch-face)
116 ((string= state "+") 'stgit-applied-patch-face)
117 ((string= state "-") 'stgit-unapplied-patch-face)))
118 (put-text-property (match-beginning 4) (match-end 4)
119 'face 'stgit-description-face)
120 (when (memq patchsym stgit-marked-patches)
121 (replace-match "*" nil nil nil 2)
122 (setq marked (cons patchsym marked))))))
124 (setq stgit-marked-patches (nreverse marked)))))
126 (defvar stgit-mode-hook nil
127 "Run after `stgit-mode' is setup.")
129 (defvar stgit-mode-map nil
130 "Keymap for StGit major mode.")
132 (unless stgit-mode-map
133 (setq stgit-mode-map (make-keymap))
134 (suppress-keymap stgit-mode-map)
135 (define-key stgit-mode-map " " 'stgit-mark)
136 (define-key stgit-mode-map "\d" 'stgit-unmark)
137 (define-key stgit-mode-map "?" 'stgit-help)
138 (define-key stgit-mode-map "h" 'stgit-help)
139 (define-key stgit-mode-map "p" 'previous-line)
140 (define-key stgit-mode-map "n" 'next-line)
141 (define-key stgit-mode-map "g" 'stgit-refresh)
142 (define-key stgit-mode-map "r" 'stgit-rename)
143 (define-key stgit-mode-map "e" 'stgit-edit)
144 (define-key stgit-mode-map "c" 'stgit-coalesce)
145 (define-key stgit-mode-map "N" 'stgit-new)
146 (define-key stgit-mode-map "R" 'stgit-repair)
147 (define-key stgit-mode-map "C" 'stgit-commit)
148 (define-key stgit-mode-map "U" 'stgit-uncommit)
149 (define-key stgit-mode-map ">" 'stgit-push-next)
150 (define-key stgit-mode-map "<" 'stgit-pop-next)
151 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
152 (define-key stgit-mode-map "G" 'stgit-goto)
153 (define-key stgit-mode-map "=" 'stgit-show))
156 "Major mode for interacting with StGit.
159 (kill-all-local-variables)
160 (buffer-disable-undo)
161 (setq mode-name "StGit"
162 major-mode 'stgit-mode
164 (use-local-map stgit-mode-map)
165 (set (make-local-variable 'list-buffers-directory) default-directory)
166 (set (make-local-variable 'stgit-marked-patches) nil)
167 (set-variable 'truncate-lines 't)
168 (run-hooks 'stgit-mode-hook))
170 (defun stgit-add-mark (patch)
171 (let ((patchsym (intern patch)))
172 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
174 (defun stgit-remove-mark (patch)
175 (let ((patchsym (intern patch)))
176 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
178 (defun stgit-marked-patches ()
179 "Return the names of the marked patches."
180 (mapcar 'symbol-name stgit-marked-patches))
182 (defun stgit-patch-at-point ()
183 "Return the patch name on the current line"
186 (if (looking-at "[>+-][ *]\\([^ ]*\\)")
187 (match-string-no-properties 1)
190 (defun stgit-goto-patch (patch)
191 "Move point to the line containing PATCH"
193 (goto-char (point-min))
194 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
195 (progn (move-to-column goal-column)
201 "Mark the patch under point"
203 (let ((patch (stgit-patch-at-point)))
204 (stgit-add-mark patch)
208 (defun stgit-unmark ()
209 "Mark the patch on the previous line"
212 (let ((patch (stgit-patch-at-point)))
213 (stgit-remove-mark patch)
216 (defun stgit-rename (name)
217 "Rename the patch under point"
218 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
219 (let ((old-name (stgit-patch-at-point)))
221 (error "No patch on this line"))
222 (stgit-capture-output nil
223 (stgit-run "rename" old-name name))
225 (stgit-goto-patch name)))
227 (defun stgit-repair ()
230 (stgit-capture-output nil
231 (stgit-run "repair"))
234 (defun stgit-commit ()
237 (stgit-capture-output nil (stgit-run "commit"))
240 (defun stgit-uncommit (arg)
241 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
243 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
246 (defun stgit-push-next ()
247 "Push the first unapplied patch"
249 (stgit-capture-output nil (stgit-run "push"))
252 (defun stgit-pop-next ()
253 "Pop the topmost applied patch"
255 (stgit-capture-output nil (stgit-run "pop"))
258 (defun stgit-applied-at-point ()
259 "Is the patch on the current line applied?"
262 (looking-at "[>+]")))
264 (defun stgit-push-or-pop ()
265 "Push or pop the patch on the current line"
267 (let ((patch (stgit-patch-at-point))
268 (applied (stgit-applied-at-point)))
269 (stgit-capture-output nil
270 (stgit-run (if applied "pop" "push") patch))
274 "Go to the patch on the current line"
276 (let ((patch (stgit-patch-at-point)))
277 (stgit-capture-output nil
278 (stgit-run "goto" patch))
282 "Show the patch on the current line"
284 (stgit-capture-output "*StGit patch*"
285 (stgit-run "show" (stgit-patch-at-point))
286 (with-current-buffer standard-output
287 (goto-char (point-min))
291 "Edit the patch on the current line"
293 (let ((patch (stgit-patch-at-point))
294 (edit-buf (get-buffer-create "*StGit edit*"))
295 (dir default-directory))
296 (log-edit 'stgit-confirm-edit t nil edit-buf)
297 (set (make-local-variable 'stgit-edit-patch) patch)
298 (setq default-directory dir)
299 (let ((standard-output edit-buf))
300 (stgit-run "edit" "--save-template=-" patch))))
302 (defun stgit-confirm-edit ()
304 (let ((file (make-temp-file "stgit-edit-")))
305 (write-region (point-min) (point-max) file)
306 (stgit-capture-output nil
307 (stgit-run "edit" "-f" file stgit-edit-patch))
308 (with-current-buffer log-edit-parent-buffer
314 (let ((edit-buf (get-buffer-create "*StGit edit*")))
315 (log-edit 'stgit-confirm-new t nil edit-buf)))
317 (defun stgit-confirm-new ()
319 (let ((file (make-temp-file "stgit-edit-"))
320 (patch (stgit-create-patch-name
321 (buffer-substring (point-min)
322 (save-excursion (goto-char (point-min))
325 (write-region (point-min) (point-max) file)
326 (stgit-capture-output nil
327 (stgit-run "new" "-m" "placeholder" patch)
328 (stgit-run "edit" "-f" file patch))
329 (with-current-buffer log-edit-parent-buffer
332 (defun stgit-create-patch-name (description)
333 "Create a patch name from a long description"
335 (while (> (length description) 0)
336 (cond ((string-match "\\`[a-zA-Z_-]+" description)
337 (setq patch (downcase (concat patch (match-string 0 description))))
338 (setq description (substring description (match-end 0))))
339 ((string-match "\\` +" description)
340 (setq patch (concat patch "-"))
341 (setq description (substring description (match-end 0))))
342 ((string-match "\\`[^a-zA-Z_-]+" description)
343 (setq description (substring description (match-end 0))))))
344 (cond ((= (length patch) 0)
346 ((> (length patch) 20)
347 (substring patch 0 20))
350 (defun stgit-coalesce (patch-names)
351 "Run stg coalesce on the named patches"
352 (interactive (list (stgit-marked-patches)))
353 (let ((edit-buf (get-buffer-create "*StGit edit*"))
354 (dir default-directory))
355 (log-edit 'stgit-confirm-coalesce t nil edit-buf)
356 (set (make-local-variable 'stgit-patches) patch-names)
357 (setq default-directory dir)
358 (let ((standard-output edit-buf))
359 (apply 'stgit-run "coalesce" "--save-template=-" patch-names))))
361 (defun stgit-confirm-coalesce ()
363 (let ((file (make-temp-file "stgit-edit-")))
364 (write-region (point-min) (point-max) file)
365 (stgit-capture-output nil
366 (apply 'stgit-run "coalesce" "-f" file stgit-patches))
367 (with-current-buffer log-edit-parent-buffer
371 "Display help for the StGit mode."
373 (describe-function 'stgit-mode))