Re-add the interactive merge
[stgit] / contrib / stgit.el
1 ;; stgit.el: An emacs mode for StGit
2 ;;
3 ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se>
4 ;;
5 ;; To install: put this file on the load-path and place the following
6 ;; in your .emacs file:
7 ;;
8 ;; (require 'stgit)
9 ;;
10 ;; To start: `M-x stgit'
11
12 (defun stgit (dir)
13 "Manage stgit patches"
14 (interactive "DDirectory: \n")
15 (switch-to-stgit-buffer dir)
16 (stgit-refresh))
17
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)))
22 (while (and buffers
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
28 (car buffers)
29 (create-stgit-buffer dir)))))
30
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)
38 (stgit-mode)
39 (setq buffer-read-only t))
40 buf))
41
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
48 (erase-buffer)
49 (setq default-directory stgit-dir)
50 (setq buffer-read-only t))
51 (let ((standard-output output-buf))
52 ,@body)
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)
59
60 (defun stgit-run (&rest args)
61 (apply 'call-process "stg" nil standard-output nil args))
62
63 (defun stgit-refresh ()
64 "Update the contents of the stgit buffer"
65 (interactive)
66 (let ((inhibit-read-only t)
67 (curline (line-number-at-pos))
68 (curpatch (stgit-patch-at-point)))
69 (erase-buffer)
70 (insert "Branch: ")
71 (stgit-run "branch")
72 (stgit-run "series" "--description")
73 (stgit-rehighlight (point-min) (point-max))
74 (if curpatch
75 (stgit-goto-patch curpatch)
76 (goto-line curline))))
77
78 (defface stgit-description-face
79 '((((background dark)) (:foreground "tan"))
80 (((background light)) (:foreground "dark red")))
81 "The face used for StGit desriptions")
82
83 (defface stgit-top-patch-face
84 '((((background dark)) (:weight bold :foreground "yellow"))
85 (((background light)) (:weight bold :foreground "purple"))
86 (t (:weight bold)))
87 "The face used for the top patch names")
88
89 (defface stgit-applied-patch-face
90 '((((background dark)) (:foreground "light yellow"))
91 (((background light)) (:foreground "purple"))
92 (t ()))
93 "The face used for applied patch names")
94
95 (defface stgit-unapplied-patch-face
96 '((((background dark)) (:foreground "gray80"))
97 (((background light)) (:foreground "orchid"))
98 (t ()))
99 "The face used for unapplied patch names")
100
101 (defun stgit-rehighlight (start end)
102 "Refresh fontification of region between START and END."
103 (save-excursion
104 (goto-char start)
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)))
110 (put-text-property
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))))
117 (forward-line 1))))
118
119 (defvar stgit-mode-hook nil
120 "Run after `stgit-mode' is setup.")
121
122 (defvar stgit-mode-map nil
123 "Keymap for StGit major mode.")
124
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 "N" 'stgit-new)
136 (define-key stgit-mode-map "\C-r" 'stgit-repair)
137 (define-key stgit-mode-map "C" 'stgit-commit)
138 (define-key stgit-mode-map "U" 'stgit-uncommit)
139 (define-key stgit-mode-map ">" 'stgit-push-next)
140 (define-key stgit-mode-map "<" 'stgit-pop-next)
141 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
142 (define-key stgit-mode-map "G" 'stgit-goto)
143 (define-key stgit-mode-map "=" 'stgit-show))
144
145 (defun stgit-mode ()
146 "Major mode for interacting with StGit.
147 Commands:
148 \\{stgit-mode-map}"
149 (kill-all-local-variables)
150 (buffer-disable-undo)
151 (setq mode-name "StGit"
152 major-mode 'stgit-mode
153 goal-column 2)
154 (use-local-map stgit-mode-map)
155 (set (make-local-variable 'list-buffers-directory) default-directory)
156 (set-variable 'truncate-lines 't)
157 (run-hooks 'stgit-mode-hook))
158
159 (defun stgit-patch-at-point ()
160 "Return the patch name on the current line"
161 (save-excursion
162 (beginning-of-line)
163 (if (looking-at "[>+-] \\([^ ]*\\)")
164 (match-string-no-properties 1)
165 nil)))
166
167 (defun stgit-goto-patch (patch)
168 "Move point to the line containing PATCH"
169 (let ((p (point)))
170 (goto-char (point-min))
171 (if (re-search-forward (concat "[>+-] " (regexp-quote patch) " ") nil t)
172 (progn (move-to-column goal-column)
173 t)
174 (goto-char p)
175 nil)))
176
177 (defun stgit-rename (name)
178 "Rename the patch under point"
179 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
180 (let ((old-name (stgit-patch-at-point)))
181 (unless old-name
182 (error "No patch on this line"))
183 (stgit-capture-output nil
184 (stgit-run "rename" old-name name))
185 (stgit-refresh)
186 (stgit-goto-patch name)))
187
188 (defun stgit-repair ()
189 "Run stg repair"
190 (interactive)
191 (stgit-capture-output nil
192 (stgit-run "repair"))
193 (stgit-refresh))
194
195 (defun stgit-commit ()
196 "Run stg commit."
197 (interactive)
198 (stgit-capture-output nil (stgit-run "commit"))
199 (stgit-refresh))
200
201 (defun stgit-uncommit (arg)
202 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
203 (interactive "p")
204 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
205 (stgit-refresh))
206
207 (defun stgit-push-next ()
208 "Push the first unapplied patch"
209 (interactive)
210 (stgit-capture-output nil (stgit-run "push"))
211 (stgit-refresh))
212
213 (defun stgit-pop-next ()
214 "Pop the topmost applied patch"
215 (interactive)
216 (stgit-capture-output nil (stgit-run "pop"))
217 (stgit-refresh))
218
219 (defun stgit-applied-at-point ()
220 "Is the patch on the current line applied?"
221 (save-excursion
222 (beginning-of-line)
223 (looking-at "[>+]")))
224
225 (defun stgit-push-or-pop ()
226 "Push or pop the patch on the current line"
227 (interactive)
228 (let ((patch (stgit-patch-at-point))
229 (applied (stgit-applied-at-point)))
230 (stgit-capture-output nil
231 (stgit-run (if applied "pop" "push") patch))
232 (stgit-refresh)))
233
234 (defun stgit-goto ()
235 "Go to the patch on the current line"
236 (interactive)
237 (let ((patch (stgit-patch-at-point)))
238 (stgit-capture-output nil
239 (stgit-run "goto" patch))
240 (stgit-refresh)))
241
242 (defun stgit-show ()
243 "Show the patch on the current line"
244 (interactive)
245 (stgit-capture-output "*StGit patch*"
246 (stgit-run "show" (stgit-patch-at-point))
247 (with-current-buffer standard-output
248 (goto-char (point-min))
249 (diff-mode))))
250
251 (defun stgit-edit ()
252 "Edit the patch on the current line"
253 (interactive)
254 (let ((patch (if (stgit-applied-at-point)
255 (stgit-patch-at-point)
256 (error "This patch is not applied")))
257 (edit-buf (get-buffer-create "*stgit edit*"))
258 (dir default-directory))
259 (log-edit 'stgit-confirm-edit t nil edit-buf)
260 (set (make-local-variable 'stgit-edit-patch) patch)
261 (setq default-directory dir)
262 (let ((standard-output edit-buf))
263 (stgit-run "edit" "--save-template=-" patch))))
264
265 (defun stgit-confirm-edit ()
266 (interactive)
267 (let ((file (make-temp-file "stgit-edit-")))
268 (write-region (point-min) (point-max) file)
269 (stgit-capture-output nil
270 (stgit-run "edit" "-f" file stgit-edit-patch))
271 (with-current-buffer log-edit-parent-buffer
272 (stgit-refresh))))
273
274 (defun stgit-new ()
275 "Create a new patch"
276 (interactive)
277 (let ((edit-buf (get-buffer-create "*stgit edit*")))
278 (log-edit 'stgit-confirm-new t nil edit-buf)))
279
280 (defun stgit-confirm-new ()
281 (interactive)
282 (let ((file (make-temp-file "stgit-edit-"))
283 (patch (stgit-create-patch-name
284 (buffer-substring (point-min)
285 (save-excursion (goto-char (point-min))
286 (end-of-line)
287 (point))))))
288 (write-region (point-min) (point-max) file)
289 (stgit-capture-output nil
290 (stgit-run "new" "-m" "placeholder" patch)
291 (stgit-run "edit" "-f" file patch))
292 (with-current-buffer log-edit-parent-buffer
293 (stgit-refresh))))
294
295 (defun stgit-create-patch-name (description)
296 "Create a patch name from a long description"
297 (let ((patch ""))
298 (while (> (length description) 0)
299 (cond ((string-match "\\`[a-zA-Z_-]+" description)
300 (setq patch (downcase (concat patch (match-string 0 description))))
301 (setq description (substring description (match-end 0))))
302 ((string-match "\\` +" description)
303 (setq patch (concat patch "-"))
304 (setq description (substring description (match-end 0))))
305 ((string-match "\\`[^a-zA-Z_-]+" description)
306 (setq description (substring description (match-end 0))))))
307 (cond ((= (length patch) 0)
308 "patch")
309 ((> (length patch) 20)
310 (substring patch 0 20))
311 (t patch))))
312
313 (defun stgit-help ()
314 "Display help for the StGit mode."
315 (interactive)
316 (describe-function 'stgit-mode))
317
318 (provide 'stgit)