stgit.el: Add 'q' for stgit-quit
[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 (git-get-top-dir dir))
16 (stgit-reload))
17
18 (defun git-get-top-dir (dir)
19 "Retrieve the top-level directory of a git tree."
20 (let ((cdup (with-output-to-string
21 (with-current-buffer standard-output
22 (cd dir)
23 (unless (eq 0 (call-process "git" nil t nil
24 "rev-parse" "--show-cdup"))
25 (error "cannot find top-level git tree for %s." dir))))))
26 (expand-file-name (concat (file-name-as-directory dir)
27 (car (split-string cdup "\n"))))))
28
29 (defun switch-to-stgit-buffer (dir)
30 "Switch to a (possibly new) buffer displaying StGit patches for DIR"
31 (setq dir (file-name-as-directory dir))
32 (let ((buffers (buffer-list)))
33 (while (and buffers
34 (not (with-current-buffer (car buffers)
35 (and (eq major-mode 'stgit-mode)
36 (string= default-directory dir)))))
37 (setq buffers (cdr buffers)))
38 (switch-to-buffer (if buffers
39 (car buffers)
40 (create-stgit-buffer dir)))))
41
42 (defun create-stgit-buffer (dir)
43 "Create a buffer for showing StGit patches.
44 Argument DIR is the repository path."
45 (let ((buf (create-file-buffer (concat dir "*stgit*")))
46 (inhibit-read-only t))
47 (with-current-buffer buf
48 (setq default-directory dir)
49 (stgit-mode)
50 (setq buffer-read-only t))
51 buf))
52
53 (defmacro stgit-capture-output (name &rest body)
54 "Capture StGit output and show it in a window at the end"
55 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
56 (stgit-dir default-directory)
57 (inhibit-read-only t))
58 (with-current-buffer output-buf
59 (erase-buffer)
60 (setq default-directory stgit-dir)
61 (setq buffer-read-only t))
62 (let ((standard-output output-buf))
63 ,@body)
64 (with-current-buffer output-buf
65 (set-buffer-modified-p nil)
66 (setq buffer-read-only t)
67 (if (< (point-min) (point-max))
68 (display-buffer output-buf t)))))
69 (put 'stgit-capture-output 'lisp-indent-function 1)
70
71 (defun stgit-run-silent (&rest args)
72 (apply 'call-process "stg" nil standard-output nil args))
73
74 (defun stgit-run (&rest args)
75 (let ((msgcmd (mapconcat #'identity args " ")))
76 (message "Running stg %s..." msgcmd)
77 (apply 'call-process "stg" nil standard-output nil args)
78 (message "Running stg %s...done" msgcmd)))
79
80 (defun stgit-reload ()
81 "Update the contents of the stgit buffer"
82 (interactive)
83 (let ((inhibit-read-only t)
84 (curline (line-number-at-pos))
85 (curpatch (stgit-patch-at-point)))
86 (erase-buffer)
87 (insert "Branch: ")
88 (stgit-run-silent "branch")
89 (stgit-run-silent "series" "--description")
90 (stgit-rescan)
91 (if curpatch
92 (stgit-goto-patch curpatch)
93 (goto-line curline))))
94
95 (defface stgit-description-face
96 '((((background dark)) (:foreground "tan"))
97 (((background light)) (:foreground "dark red")))
98 "The face used for StGit desriptions")
99
100 (defface stgit-top-patch-face
101 '((((background dark)) (:weight bold :foreground "yellow"))
102 (((background light)) (:weight bold :foreground "purple"))
103 (t (:weight bold)))
104 "The face used for the top patch names")
105
106 (defface stgit-applied-patch-face
107 '((((background dark)) (:foreground "light yellow"))
108 (((background light)) (:foreground "purple"))
109 (t ()))
110 "The face used for applied patch names")
111
112 (defface stgit-unapplied-patch-face
113 '((((background dark)) (:foreground "gray80"))
114 (((background light)) (:foreground "orchid"))
115 (t ()))
116 "The face used for unapplied patch names")
117
118 (defun stgit-rescan ()
119 "Rescan the status buffer."
120 (save-excursion
121 (let ((marked ()))
122 (goto-char (point-min))
123 (while (not (eobp))
124 (cond ((looking-at "Branch: \\(.*\\)")
125 (put-text-property (match-beginning 1) (match-end 1)
126 'face 'bold))
127 ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
128 (let ((state (match-string 1))
129 (patchsym (intern (match-string 3))))
130 (put-text-property
131 (match-beginning 3) (match-end 3) 'face
132 (cond ((string= state ">") 'stgit-top-patch-face)
133 ((string= state "+") 'stgit-applied-patch-face)
134 ((string= state "-") 'stgit-unapplied-patch-face)))
135 (put-text-property (match-beginning 4) (match-end 4)
136 'face 'stgit-description-face)
137 (when (memq patchsym stgit-marked-patches)
138 (replace-match "*" nil nil nil 2)
139 (setq marked (cons patchsym marked)))))
140 ((or (looking-at "stg series: Branch \".*\" not initialised")
141 (looking-at "stg series: .*: branch not initialized"))
142 (forward-line 1)
143 (insert "Run M-x stgit-init to initialise")))
144 (forward-line 1))
145 (setq stgit-marked-patches (nreverse marked)))))
146
147 (defun stgit-quit ()
148 "Hide the stgit buffer"
149 (interactive)
150 (bury-buffer))
151
152 (defvar stgit-mode-hook nil
153 "Run after `stgit-mode' is setup.")
154
155 (defvar stgit-mode-map nil
156 "Keymap for StGit major mode.")
157
158 (unless stgit-mode-map
159 (setq stgit-mode-map (make-keymap))
160 (suppress-keymap stgit-mode-map)
161 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
162 '((" " . stgit-mark)
163 ("\d" . stgit-unmark)
164 ("?" . stgit-help)
165 ("h" . stgit-help)
166 ("p" . previous-line)
167 ("n" . next-line)
168 ("g" . stgit-reload)
169 ("r" . stgit-refresh)
170 ("\C-c\C-r" . stgit-rename)
171 ("e" . stgit-edit)
172 ("c" . stgit-coalesce)
173 ("N" . stgit-new)
174 ("R" . stgit-repair)
175 ("C" . stgit-commit)
176 ("U" . stgit-uncommit)
177 (">" . stgit-push-next)
178 ("<" . stgit-pop-next)
179 ("P" . stgit-push-or-pop)
180 ("G" . stgit-goto)
181 ("=" . stgit-show)
182 ("D" . stgit-delete)
183 ([(control ?/)] . stgit-undo)
184 ("\C-_" . stgit-undo)
185 ("q" . stgit-quit))))
186
187 (defun stgit-mode ()
188 "Major mode for interacting with StGit.
189 Commands:
190 \\{stgit-mode-map}"
191 (kill-all-local-variables)
192 (buffer-disable-undo)
193 (setq mode-name "StGit"
194 major-mode 'stgit-mode
195 goal-column 2)
196 (use-local-map stgit-mode-map)
197 (set (make-local-variable 'list-buffers-directory) default-directory)
198 (set (make-local-variable 'stgit-marked-patches) nil)
199 (set-variable 'truncate-lines 't)
200 (run-hooks 'stgit-mode-hook))
201
202 (defun stgit-add-mark (patch)
203 (let ((patchsym (intern patch)))
204 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
205
206 (defun stgit-remove-mark (patch)
207 (let ((patchsym (intern patch)))
208 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
209
210 (defun stgit-clear-marks ()
211 (setq stgit-marked-patches '()))
212
213 (defun stgit-marked-patches ()
214 "Return the names of the marked patches."
215 (mapcar 'symbol-name stgit-marked-patches))
216
217 (defun stgit-patch-at-point ()
218 "Return the patch name on the current line"
219 (save-excursion
220 (beginning-of-line)
221 (if (looking-at "[>+-][ *]\\([^ ]*\\)")
222 (match-string-no-properties 1)
223 nil)))
224
225 (defun stgit-patches-marked-or-at-point ()
226 "Return the names of the marked patches, or the patch on the current line."
227 (if stgit-marked-patches
228 (stgit-marked-patches)
229 (let ((patch (stgit-patch-at-point)))
230 (if patch
231 (list patch)
232 '()))))
233
234 (defun stgit-goto-patch (patch)
235 "Move point to the line containing PATCH"
236 (let ((p (point)))
237 (goto-char (point-min))
238 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
239 (progn (move-to-column goal-column)
240 t)
241 (goto-char p)
242 nil)))
243
244 (defun stgit-init ()
245 "Run stg init"
246 (interactive)
247 (stgit-capture-output nil
248 (stgit-run "init"))
249 (stgit-reload))
250
251 (defun stgit-mark ()
252 "Mark the patch under point"
253 (interactive)
254 (let ((patch (stgit-patch-at-point)))
255 (stgit-add-mark patch)
256 (stgit-reload))
257 (next-line))
258
259 (defun stgit-unmark ()
260 "Mark the patch on the previous line"
261 (interactive)
262 (forward-line -1)
263 (let ((patch (stgit-patch-at-point)))
264 (stgit-remove-mark patch)
265 (stgit-reload)))
266
267 (defun stgit-rename (name)
268 "Rename the patch under point"
269 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
270 (let ((old-name (stgit-patch-at-point)))
271 (unless old-name
272 (error "No patch on this line"))
273 (stgit-capture-output nil
274 (stgit-run "rename" old-name name))
275 (stgit-reload)
276 (stgit-goto-patch name)))
277
278 (defun stgit-repair ()
279 "Run stg repair"
280 (interactive)
281 (stgit-capture-output nil
282 (stgit-run "repair"))
283 (stgit-reload))
284
285 (defun stgit-commit ()
286 "Run stg commit."
287 (interactive)
288 (stgit-capture-output nil (stgit-run "commit"))
289 (stgit-reload))
290
291 (defun stgit-uncommit (arg)
292 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
293 (interactive "p")
294 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
295 (stgit-reload))
296
297 (defun stgit-push-next (npatches)
298 "Push the first unapplied patch.
299 With numeric prefix argument, push that many patches."
300 (interactive "p")
301 (stgit-capture-output nil (stgit-run "push" "-n"
302 (number-to-string npatches)))
303 (stgit-reload))
304
305 (defun stgit-pop-next (npatches)
306 "Pop the topmost applied patch.
307 With numeric prefix argument, pop that many patches."
308 (interactive "p")
309 (stgit-capture-output nil (stgit-run "pop" "-n" (number-to-string npatches)))
310 (stgit-reload))
311
312 (defun stgit-applied-at-point ()
313 "Is the patch on the current line applied?"
314 (save-excursion
315 (beginning-of-line)
316 (looking-at "[>+]")))
317
318 (defun stgit-push-or-pop ()
319 "Push or pop the patch on the current line"
320 (interactive)
321 (let ((patch (stgit-patch-at-point))
322 (applied (stgit-applied-at-point)))
323 (stgit-capture-output nil
324 (stgit-run (if applied "pop" "push") patch))
325 (stgit-reload)))
326
327 (defun stgit-goto ()
328 "Go to the patch on the current line"
329 (interactive)
330 (let ((patch (stgit-patch-at-point)))
331 (stgit-capture-output nil
332 (stgit-run "goto" patch))
333 (stgit-reload)))
334
335 (defun stgit-show ()
336 "Show the patch on the current line"
337 (interactive)
338 (stgit-capture-output "*StGit patch*"
339 (stgit-run "show" (stgit-patch-at-point))
340 (with-current-buffer standard-output
341 (goto-char (point-min))
342 (diff-mode))))
343
344 (defun stgit-edit ()
345 "Edit the patch on the current line"
346 (interactive)
347 (let ((patch (stgit-patch-at-point))
348 (edit-buf (get-buffer-create "*StGit edit*"))
349 (dir default-directory))
350 (log-edit 'stgit-confirm-edit t nil edit-buf)
351 (set (make-local-variable 'stgit-edit-patch) patch)
352 (setq default-directory dir)
353 (let ((standard-output edit-buf))
354 (stgit-run-silent "edit" "--save-template=-" patch))))
355
356 (defun stgit-confirm-edit ()
357 (interactive)
358 (let ((file (make-temp-file "stgit-edit-")))
359 (write-region (point-min) (point-max) file)
360 (stgit-capture-output nil
361 (stgit-run "edit" "-f" file stgit-edit-patch))
362 (with-current-buffer log-edit-parent-buffer
363 (stgit-reload))))
364
365 (defun stgit-new ()
366 "Create a new patch"
367 (interactive)
368 (let ((edit-buf (get-buffer-create "*StGit edit*")))
369 (log-edit 'stgit-confirm-new t nil edit-buf)))
370
371 (defun stgit-confirm-new ()
372 (interactive)
373 (let ((file (make-temp-file "stgit-edit-")))
374 (write-region (point-min) (point-max) file)
375 (stgit-capture-output nil
376 (stgit-run "new" "-f" file))
377 (with-current-buffer log-edit-parent-buffer
378 (stgit-reload))))
379
380 (defun stgit-create-patch-name (description)
381 "Create a patch name from a long description"
382 (let ((patch ""))
383 (while (> (length description) 0)
384 (cond ((string-match "\\`[a-zA-Z_-]+" description)
385 (setq patch (downcase (concat patch (match-string 0 description))))
386 (setq description (substring description (match-end 0))))
387 ((string-match "\\` +" description)
388 (setq patch (concat patch "-"))
389 (setq description (substring description (match-end 0))))
390 ((string-match "\\`[^a-zA-Z_-]+" description)
391 (setq description (substring description (match-end 0))))))
392 (cond ((= (length patch) 0)
393 "patch")
394 ((> (length patch) 20)
395 (substring patch 0 20))
396 (t patch))))
397
398 (defun stgit-delete (patch-names)
399 "Delete the named patches"
400 (interactive (list (stgit-patches-marked-or-at-point)))
401 (if (zerop (length patch-names))
402 (error "No patches to delete")
403 (when (yes-or-no-p (format "Really delete %d patches? "
404 (length patch-names)))
405 (stgit-capture-output nil
406 (apply 'stgit-run "delete" patch-names))
407 (stgit-reload))))
408
409 (defun stgit-coalesce (patch-names)
410 "Run stg coalesce on the named patches"
411 (interactive (list (stgit-marked-patches)))
412 (let ((edit-buf (get-buffer-create "*StGit edit*"))
413 (dir default-directory))
414 (log-edit 'stgit-confirm-coalesce t nil edit-buf)
415 (set (make-local-variable 'stgit-patches) patch-names)
416 (setq default-directory dir)
417 (let ((standard-output edit-buf))
418 (apply 'stgit-run-silent "coalesce" "--save-template=-" patch-names))))
419
420 (defun stgit-confirm-coalesce ()
421 (interactive)
422 (let ((file (make-temp-file "stgit-edit-")))
423 (write-region (point-min) (point-max) file)
424 (stgit-capture-output nil
425 (apply 'stgit-run "coalesce" "-f" file stgit-patches))
426 (with-current-buffer log-edit-parent-buffer
427 (stgit-clear-marks)
428 ;; Go to first marked patch and stay there
429 (goto-char (point-min))
430 (re-search-forward (concat "^[>+-]\\*") nil t)
431 (move-to-column goal-column)
432 (let ((pos (point)))
433 (stgit-reload)
434 (goto-char pos)))))
435
436 (defun stgit-help ()
437 "Display help for the StGit mode."
438 (interactive)
439 (describe-function 'stgit-mode))
440
441 (defun stgit-undo (&optional arg)
442 "Run stg undo.
443 With prefix argument, run it with the --hard flag."
444 (interactive "P")
445 (stgit-capture-output nil
446 (if arg
447 (stgit-run "undo" "--hard")
448 (stgit-run "undo")))
449 (stgit-reload))
450
451 (defun stgit-refresh (&optional arg)
452 "Run stg refresh.
453 With prefix argument, refresh the patch under point."
454 (interactive "P")
455 (let ((patchargs (if arg
456 (let ((patches (stgit-patches-marked-or-at-point)))
457 (cond ((null patches)
458 (error "no patch to update"))
459 ((> (length patches) 1)
460 (error "too many patches selected"))
461 (t
462 (cons "-p" patches))))
463 nil)))
464 (stgit-capture-output nil
465 (apply 'stgit-run "refresh" patchargs)))
466 (stgit-reload))
467
468 (provide 'stgit)