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 "N" 'stgit-new)
145 (define-key stgit-mode-map "\C-r" 'stgit-repair)
146 (define-key stgit-mode-map "C" 'stgit-commit)
147 (define-key stgit-mode-map "U" 'stgit-uncommit)
148 (define-key stgit-mode-map ">" 'stgit-push-next)
149 (define-key stgit-mode-map "<" 'stgit-pop-next)
150 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
151 (define-key stgit-mode-map "G" 'stgit-goto)
152 (define-key stgit-mode-map "=" 'stgit-show))
155 "Major mode for interacting with StGit.
158 (kill-all-local-variables)
159 (buffer-disable-undo)
160 (setq mode-name "StGit"
161 major-mode 'stgit-mode
163 (use-local-map stgit-mode-map)
164 (set (make-local-variable 'list-buffers-directory) default-directory)
165 (set (make-local-variable 'stgit-marked-patches) nil)
166 (set-variable 'truncate-lines 't)
167 (run-hooks 'stgit-mode-hook))
169 (defun stgit-add-mark (patch)
170 (let ((patchsym (intern patch)))
171 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
173 (defun stgit-remove-mark (patch)
174 (let ((patchsym (intern patch)))
175 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
177 (defun stgit-marked-patches ()
178 "Return the names of the marked patches."
179 (mapcar 'symbol-name stgit-marked-patches))
181 (defun stgit-patch-at-point ()
182 "Return the patch name on the current line"
185 (if (looking-at "[>+-][ *]\\([^ ]*\\)")
186 (match-string-no-properties 1)
189 (defun stgit-goto-patch (patch)
190 "Move point to the line containing PATCH"
192 (goto-char (point-min))
193 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
194 (progn (move-to-column goal-column)
200 "Mark the patch under point"
202 (let ((patch (stgit-patch-at-point)))
203 (stgit-add-mark patch)
207 (defun stgit-unmark ()
208 "Mark the patch on the previous line"
211 (let ((patch (stgit-patch-at-point)))
212 (stgit-remove-mark patch)
215 (defun stgit-rename (name)
216 "Rename the patch under point"
217 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
218 (let ((old-name (stgit-patch-at-point)))
220 (error "No patch on this line"))
221 (stgit-capture-output nil
222 (stgit-run "rename" old-name name))
224 (stgit-goto-patch name)))
226 (defun stgit-repair ()
229 (stgit-capture-output nil
230 (stgit-run "repair"))
233 (defun stgit-commit ()
236 (stgit-capture-output nil (stgit-run "commit"))
239 (defun stgit-uncommit (arg)
240 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
242 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
245 (defun stgit-push-next ()
246 "Push the first unapplied patch"
248 (stgit-capture-output nil (stgit-run "push"))
251 (defun stgit-pop-next ()
252 "Pop the topmost applied patch"
254 (stgit-capture-output nil (stgit-run "pop"))
257 (defun stgit-applied-at-point ()
258 "Is the patch on the current line applied?"
261 (looking-at "[>+]")))
263 (defun stgit-push-or-pop ()
264 "Push or pop the patch on the current line"
266 (let ((patch (stgit-patch-at-point))
267 (applied (stgit-applied-at-point)))
268 (stgit-capture-output nil
269 (stgit-run (if applied "pop" "push") patch))
273 "Go to the patch on the current line"
275 (let ((patch (stgit-patch-at-point)))
276 (stgit-capture-output nil
277 (stgit-run "goto" patch))
281 "Show the patch on the current line"
283 (stgit-capture-output "*StGit patch*"
284 (stgit-run "show" (stgit-patch-at-point))
285 (with-current-buffer standard-output
286 (goto-char (point-min))
290 "Edit the patch on the current line"
292 (let ((patch (if (stgit-applied-at-point)
293 (stgit-patch-at-point)
294 (error "This patch is not applied")))
295 (edit-buf (get-buffer-create "*stgit edit*"))
296 (dir default-directory))
297 (log-edit 'stgit-confirm-edit t nil edit-buf)
298 (set (make-local-variable 'stgit-edit-patch) patch)
299 (setq default-directory dir)
300 (let ((standard-output edit-buf))
301 (stgit-run "edit" "--save-template=-" patch))))
303 (defun stgit-confirm-edit ()
305 (let ((file (make-temp-file "stgit-edit-")))
306 (write-region (point-min) (point-max) file)
307 (stgit-capture-output nil
308 (stgit-run "edit" "-f" file stgit-edit-patch))
309 (with-current-buffer log-edit-parent-buffer
315 (let ((edit-buf (get-buffer-create "*stgit edit*")))
316 (log-edit 'stgit-confirm-new t nil edit-buf)))
318 (defun stgit-confirm-new ()
320 (let ((file (make-temp-file "stgit-edit-"))
321 (patch (stgit-create-patch-name
322 (buffer-substring (point-min)
323 (save-excursion (goto-char (point-min))
326 (write-region (point-min) (point-max) file)
327 (stgit-capture-output nil
328 (stgit-run "new" "-m" "placeholder" patch)
329 (stgit-run "edit" "-f" file patch))
330 (with-current-buffer log-edit-parent-buffer
333 (defun stgit-create-patch-name (description)
334 "Create a patch name from a long description"
336 (while (> (length description) 0)
337 (cond ((string-match "\\`[a-zA-Z_-]+" description)
338 (setq patch (downcase (concat patch (match-string 0 description))))
339 (setq description (substring description (match-end 0))))
340 ((string-match "\\` +" description)
341 (setq patch (concat patch "-"))
342 (setq description (substring description (match-end 0))))
343 ((string-match "\\`[^a-zA-Z_-]+" description)
344 (setq description (substring description (match-end 0))))))
345 (cond ((= (length patch) 0)
347 ((> (length patch) 20)
348 (substring patch 0 20))
352 "Display help for the StGit mode."
354 (describe-function 'stgit-mode))