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")
74 (stgit-goto-patch curpatch)
75 (goto-line curline))))
77 (defvar stgit-mode-hook nil
78 "Run after `stgit-mode' is setup.")
80 (defvar stgit-mode-map nil
81 "Keymap for StGit major mode.")
83 (unless stgit-mode-map
84 (setq stgit-mode-map (make-keymap))
85 (suppress-keymap stgit-mode-map)
86 (define-key stgit-mode-map "?" 'stgit-help)
87 (define-key stgit-mode-map "h" 'stgit-help)
88 (define-key stgit-mode-map "p" 'previous-line)
89 (define-key stgit-mode-map "n" 'next-line)
90 (define-key stgit-mode-map "g" 'stgit-refresh)
91 (define-key stgit-mode-map "r" 'stgit-rename)
92 (define-key stgit-mode-map "e" 'stgit-edit)
93 (define-key stgit-mode-map "\C-r" 'stgit-repair)
94 (define-key stgit-mode-map "C" 'stgit-commit)
95 (define-key stgit-mode-map "U" 'stgit-uncommit)
96 (define-key stgit-mode-map ">" 'stgit-push-next)
97 (define-key stgit-mode-map "<" 'stgit-pop-next)
98 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
99 (define-key stgit-mode-map "G" 'stgit-goto)
100 (define-key stgit-mode-map "=" 'stgit-show))
103 "Major mode for interacting with StGit.
106 (kill-all-local-variables)
107 (buffer-disable-undo)
108 (setq mode-name "StGit"
109 major-mode 'stgit-mode
111 (use-local-map stgit-mode-map)
112 (set (make-local-variable 'list-buffers-directory) default-directory)
113 (set-variable 'truncate-lines 't)
114 (run-hooks 'stgit-mode-hook))
116 (defun stgit-patch-at-point ()
117 "Return the patch name on the current line"
120 (if (looking-at "[>+-] \\([^ ]*\\)")
124 (defun stgit-goto-patch (patch)
125 "Move point to the line containing PATCH"
127 (goto-char (point-min))
128 (if (re-search-forward (concat "[>+-] " (regexp-quote patch) " ") nil t)
129 (progn (move-to-column goal-column)
134 (defun stgit-rename (name)
135 "Rename the patch under point"
136 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
137 (let ((old-name (stgit-patch-at-point)))
139 (error "No patch on this line"))
140 (stgit-capture-output nil
141 (stgit-run "rename" old-name name))
143 (stgit-goto-patch name)))
145 (defun stgit-repair ()
148 (stgit-capture-output nil
149 (stgit-run "repair"))
152 (defun stgit-commit ()
155 (stgit-capture-output nil (stgit-run "commit"))
158 (defun stgit-uncommit (arg)
159 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
161 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
164 (defun stgit-push-next ()
165 "Push the first unapplied patch"
167 (stgit-capture-output nil (stgit-run "push"))
170 (defun stgit-pop-next ()
171 "Pop the topmost applied patch"
173 (stgit-capture-output nil (stgit-run "pop"))
176 (defun stgit-applied-at-point ()
177 "Is the patch on the current line applied?"
180 (looking-at "[>+]")))
182 (defun stgit-push-or-pop ()
183 "Push or pop the patch on the current line"
185 (let ((patch (stgit-patch-at-point))
186 (applied (stgit-applied-at-point)))
187 (stgit-capture-output nil
188 (stgit-run (if applied "pop" "push") patch))
192 "Go to the patch on the current line"
194 (let ((patch (stgit-patch-at-point)))
195 (stgit-capture-output nil
196 (stgit-run "goto" patch))
200 "Show the patch on the current line"
202 (stgit-capture-output "*StGit patch*"
203 (stgit-run "show" (stgit-patch-at-point))
204 (with-current-buffer standard-output
205 (goto-char (point-min))
209 "Edit the patch on the current line"
211 (let ((patch (if (stgit-applied-at-point)
212 (stgit-patch-at-point)
213 (error "This patch is not applied")))
214 (edit-buf (get-buffer-create "*stgit edit*"))
215 (dir default-directory))
216 (log-edit 'stgit-confirm-edit t nil edit-buf)
217 (set (make-local-variable 'stgit-edit-patch) patch)
218 (setq default-directory dir)
219 (let ((standard-output edit-buf))
220 (stgit-run "edit" "--save-template=-" patch))))
222 (defun stgit-confirm-edit ()
224 (let ((file (make-temp-file "stgit-edit-")))
225 (write-region (point-min) (point-max) file)
226 (stgit-capture-output nil
227 (stgit-run "edit" "-f" file stgit-edit-patch))
228 (with-current-buffer log-edit-parent-buffer
233 "Display help for the StGit mode."
235 (describe-function 'stgit-mode))