chiark / gitweb /
stgit.el: Add stgit-unmark-down
[stgit] / contrib / stgit.el
CommitLineData
3a59f3db
KH
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
56d81fe5
DK
12(defun stgit (dir)
13 "Manage stgit patches"
14 (interactive "DDirectory: \n")
52144ce5 15 (switch-to-stgit-buffer (git-get-top-dir dir))
1f0bf00f 16 (stgit-reload))
56d81fe5 17
52144ce5
KH
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
56d81fe5
DK
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.
44Argument 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"
34afb86c
DK
55 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
56 (stgit-dir default-directory)
57 (inhibit-read-only t))
56d81fe5 58 (with-current-buffer output-buf
34afb86c
DK
59 (erase-buffer)
60 (setq default-directory stgit-dir)
61 (setq buffer-read-only t))
56d81fe5
DK
62 (let ((standard-output output-buf))
63 ,@body)
34afb86c
DK
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)))))
56d81fe5
DK
69(put 'stgit-capture-output 'lisp-indent-function 1)
70
9aecd505 71(defun stgit-run-silent (&rest args)
56d81fe5
DK
72 (apply 'call-process "stg" nil standard-output nil args))
73
9aecd505
DK
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
1f0bf00f 80(defun stgit-reload ()
56d81fe5
DK
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: ")
9aecd505
DK
88 (stgit-run-silent "branch")
89 (stgit-run-silent "series" "--description")
6df83d42 90 (stgit-rescan)
56d81fe5
DK
91 (if curpatch
92 (stgit-goto-patch curpatch)
93 (goto-line curline))))
94
07f464e0
DK
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
6df83d42
DK
118(defun stgit-rescan ()
119 "Rescan the status buffer."
07f464e0 120 (save-excursion
6df83d42
DK
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))
8ee1e4b4 127 ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
6df83d42
DK
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)
1c2426dc 139 (setq marked (cons patchsym marked)))))
ad80ce22
DK
140 ((or (looking-at "stg series: Branch \".*\" not initialised")
141 (looking-at "stg series: .*: branch not initialized"))
1c2426dc
DK
142 (forward-line 1)
143 (insert "Run M-x stgit-init to initialise")))
6df83d42
DK
144 (forward-line 1))
145 (setq stgit-marked-patches (nreverse marked)))))
07f464e0 146
83327d53
GH
147(defun stgit-quit ()
148 "Hide the stgit buffer"
149 (interactive)
150 (bury-buffer))
151
56d81fe5
DK
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)
022a3664
GH
161 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
162 '((" " . stgit-mark)
3dccdc9b 163 ("m" . stgit-mark)
9b151b27
GH
164 ("\d" . stgit-unmark-up)
165 ("u" . stgit-unmark-down)
022a3664
GH
166 ("?" . stgit-help)
167 ("h" . stgit-help)
168 ("p" . previous-line)
169 ("n" . next-line)
170 ("g" . stgit-reload)
171 ("r" . stgit-refresh)
172 ("\C-c\C-r" . stgit-rename)
173 ("e" . stgit-edit)
174 ("c" . stgit-coalesce)
175 ("N" . stgit-new)
176 ("R" . stgit-repair)
177 ("C" . stgit-commit)
178 ("U" . stgit-uncommit)
179 (">" . stgit-push-next)
180 ("<" . stgit-pop-next)
181 ("P" . stgit-push-or-pop)
182 ("G" . stgit-goto)
183 ("=" . stgit-show)
184 ("D" . stgit-delete)
185 ([(control ?/)] . stgit-undo)
83327d53
GH
186 ("\C-_" . stgit-undo)
187 ("q" . stgit-quit))))
56d81fe5
DK
188
189(defun stgit-mode ()
190 "Major mode for interacting with StGit.
191Commands:
192\\{stgit-mode-map}"
193 (kill-all-local-variables)
194 (buffer-disable-undo)
195 (setq mode-name "StGit"
196 major-mode 'stgit-mode
197 goal-column 2)
198 (use-local-map stgit-mode-map)
199 (set (make-local-variable 'list-buffers-directory) default-directory)
6df83d42 200 (set (make-local-variable 'stgit-marked-patches) nil)
2870f8b8 201 (set-variable 'truncate-lines 't)
56d81fe5
DK
202 (run-hooks 'stgit-mode-hook))
203
6df83d42
DK
204(defun stgit-add-mark (patch)
205 (let ((patchsym (intern patch)))
206 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
207
208(defun stgit-remove-mark (patch)
209 (let ((patchsym (intern patch)))
210 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
211
e6b1fdae
DK
212(defun stgit-clear-marks ()
213 (setq stgit-marked-patches '()))
214
6df83d42
DK
215(defun stgit-marked-patches ()
216 "Return the names of the marked patches."
217 (mapcar 'symbol-name stgit-marked-patches))
218
56d81fe5
DK
219(defun stgit-patch-at-point ()
220 "Return the patch name on the current line"
221 (save-excursion
222 (beginning-of-line)
6df83d42 223 (if (looking-at "[>+-][ *]\\([^ ]*\\)")
07f464e0 224 (match-string-no-properties 1)
56d81fe5
DK
225 nil)))
226
7755d7f1
KH
227(defun stgit-patches-marked-or-at-point ()
228 "Return the names of the marked patches, or the patch on the current line."
229 (if stgit-marked-patches
230 (stgit-marked-patches)
231 (let ((patch (stgit-patch-at-point)))
232 (if patch
233 (list patch)
234 '()))))
235
56d81fe5
DK
236(defun stgit-goto-patch (patch)
237 "Move point to the line containing PATCH"
238 (let ((p (point)))
239 (goto-char (point-min))
6df83d42 240 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
56d81fe5
DK
241 (progn (move-to-column goal-column)
242 t)
243 (goto-char p)
244 nil)))
245
1c2426dc
DK
246(defun stgit-init ()
247 "Run stg init"
248 (interactive)
249 (stgit-capture-output nil
250 (stgit-run "init"))
1f0bf00f 251 (stgit-reload))
1c2426dc 252
6df83d42
DK
253(defun stgit-mark ()
254 "Mark the patch under point"
255 (interactive)
256 (let ((patch (stgit-patch-at-point)))
257 (stgit-add-mark patch)
1f0bf00f 258 (stgit-reload))
6df83d42
DK
259 (next-line))
260
9b151b27
GH
261(defun stgit-unmark-up ()
262 "Remove mark from the patch on the previous line"
6df83d42
DK
263 (interactive)
264 (forward-line -1)
9b151b27
GH
265 (stgit-remove-mark (stgit-patch-at-point))
266 (stgit-reload))
267
268(defun stgit-unmark-down ()
269 "Remove mark from the patch on the current line"
270 (interactive)
271 (stgit-remove-mark (stgit-patch-at-point))
272 (forward-line)
273 (stgit-reload))
6df83d42 274
56d81fe5
DK
275(defun stgit-rename (name)
276 "Rename the patch under point"
277 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
278 (let ((old-name (stgit-patch-at-point)))
279 (unless old-name
280 (error "No patch on this line"))
281 (stgit-capture-output nil
282 (stgit-run "rename" old-name name))
1f0bf00f 283 (stgit-reload)
56d81fe5
DK
284 (stgit-goto-patch name)))
285
26201d96
DK
286(defun stgit-repair ()
287 "Run stg repair"
288 (interactive)
289 (stgit-capture-output nil
290 (stgit-run "repair"))
1f0bf00f 291 (stgit-reload))
26201d96 292
c4aad9a7
DK
293(defun stgit-commit ()
294 "Run stg commit."
295 (interactive)
296 (stgit-capture-output nil (stgit-run "commit"))
1f0bf00f 297 (stgit-reload))
c4aad9a7
DK
298
299(defun stgit-uncommit (arg)
300 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
301 (interactive "p")
302 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
1f0bf00f 303 (stgit-reload))
c4aad9a7 304
0b661144
DK
305(defun stgit-push-next (npatches)
306 "Push the first unapplied patch.
307With numeric prefix argument, push that many patches."
308 (interactive "p")
309 (stgit-capture-output nil (stgit-run "push" "-n"
310 (number-to-string npatches)))
1f0bf00f 311 (stgit-reload))
56d81fe5 312
0b661144
DK
313(defun stgit-pop-next (npatches)
314 "Pop the topmost applied patch.
315With numeric prefix argument, pop that many patches."
316 (interactive "p")
317 (stgit-capture-output nil (stgit-run "pop" "-n" (number-to-string npatches)))
1f0bf00f 318 (stgit-reload))
56d81fe5 319
f9182fca
KH
320(defun stgit-applied-at-point ()
321 "Is the patch on the current line applied?"
322 (save-excursion
323 (beginning-of-line)
324 (looking-at "[>+]")))
325
326(defun stgit-push-or-pop ()
327 "Push or pop the patch on the current line"
328 (interactive)
329 (let ((patch (stgit-patch-at-point))
330 (applied (stgit-applied-at-point)))
331 (stgit-capture-output nil
332 (stgit-run (if applied "pop" "push") patch))
1f0bf00f 333 (stgit-reload)))
f9182fca 334
c7adf5ef
KH
335(defun stgit-goto ()
336 "Go to the patch on the current line"
337 (interactive)
338 (let ((patch (stgit-patch-at-point)))
339 (stgit-capture-output nil
340 (stgit-run "goto" patch))
1f0bf00f 341 (stgit-reload)))
c7adf5ef 342
56d81fe5
DK
343(defun stgit-show ()
344 "Show the patch on the current line"
345 (interactive)
346 (stgit-capture-output "*StGit patch*"
347 (stgit-run "show" (stgit-patch-at-point))
348 (with-current-buffer standard-output
349 (goto-char (point-min))
350 (diff-mode))))
0663524d 351
0bca35c8
DK
352(defun stgit-edit ()
353 "Edit the patch on the current line"
354 (interactive)
8ae7dc9d 355 (let ((patch (stgit-patch-at-point))
0780be79 356 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
357 (dir default-directory))
358 (log-edit 'stgit-confirm-edit t nil edit-buf)
359 (set (make-local-variable 'stgit-edit-patch) patch)
360 (setq default-directory dir)
361 (let ((standard-output edit-buf))
9aecd505 362 (stgit-run-silent "edit" "--save-template=-" patch))))
0bca35c8
DK
363
364(defun stgit-confirm-edit ()
365 (interactive)
366 (let ((file (make-temp-file "stgit-edit-")))
367 (write-region (point-min) (point-max) file)
368 (stgit-capture-output nil
369 (stgit-run "edit" "-f" file stgit-edit-patch))
370 (with-current-buffer log-edit-parent-buffer
1f0bf00f 371 (stgit-reload))))
0bca35c8 372
64c097a0
DK
373(defun stgit-new ()
374 "Create a new patch"
375 (interactive)
0780be79 376 (let ((edit-buf (get-buffer-create "*StGit edit*")))
64c097a0
DK
377 (log-edit 'stgit-confirm-new t nil edit-buf)))
378
379(defun stgit-confirm-new ()
380 (interactive)
27b0f9e4 381 (let ((file (make-temp-file "stgit-edit-")))
64c097a0
DK
382 (write-region (point-min) (point-max) file)
383 (stgit-capture-output nil
27b0f9e4 384 (stgit-run "new" "-f" file))
64c097a0 385 (with-current-buffer log-edit-parent-buffer
1f0bf00f 386 (stgit-reload))))
64c097a0
DK
387
388(defun stgit-create-patch-name (description)
389 "Create a patch name from a long description"
390 (let ((patch ""))
391 (while (> (length description) 0)
392 (cond ((string-match "\\`[a-zA-Z_-]+" description)
393 (setq patch (downcase (concat patch (match-string 0 description))))
394 (setq description (substring description (match-end 0))))
395 ((string-match "\\` +" description)
396 (setq patch (concat patch "-"))
397 (setq description (substring description (match-end 0))))
398 ((string-match "\\`[^a-zA-Z_-]+" description)
399 (setq description (substring description (match-end 0))))))
400 (cond ((= (length patch) 0)
401 "patch")
402 ((> (length patch) 20)
403 (substring patch 0 20))
404 (t patch))))
0bca35c8 405
7755d7f1
KH
406(defun stgit-delete (patch-names)
407 "Delete the named patches"
408 (interactive (list (stgit-patches-marked-or-at-point)))
409 (if (zerop (length patch-names))
410 (error "No patches to delete")
411 (when (yes-or-no-p (format "Really delete %d patches? "
412 (length patch-names)))
413 (stgit-capture-output nil
414 (apply 'stgit-run "delete" patch-names))
1f0bf00f 415 (stgit-reload))))
7755d7f1 416
ea0def18
DK
417(defun stgit-coalesce (patch-names)
418 "Run stg coalesce on the named patches"
419 (interactive (list (stgit-marked-patches)))
0780be79 420 (let ((edit-buf (get-buffer-create "*StGit edit*"))
ea0def18
DK
421 (dir default-directory))
422 (log-edit 'stgit-confirm-coalesce t nil edit-buf)
423 (set (make-local-variable 'stgit-patches) patch-names)
424 (setq default-directory dir)
425 (let ((standard-output edit-buf))
9aecd505 426 (apply 'stgit-run-silent "coalesce" "--save-template=-" patch-names))))
ea0def18
DK
427
428(defun stgit-confirm-coalesce ()
429 (interactive)
430 (let ((file (make-temp-file "stgit-edit-")))
431 (write-region (point-min) (point-max) file)
432 (stgit-capture-output nil
433 (apply 'stgit-run "coalesce" "-f" file stgit-patches))
434 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
435 (stgit-clear-marks)
436 ;; Go to first marked patch and stay there
437 (goto-char (point-min))
438 (re-search-forward (concat "^[>+-]\\*") nil t)
439 (move-to-column goal-column)
440 (let ((pos (point)))
1f0bf00f 441 (stgit-reload)
e6b1fdae 442 (goto-char pos)))))
ea0def18 443
0663524d
KH
444(defun stgit-help ()
445 "Display help for the StGit mode."
446 (interactive)
447 (describe-function 'stgit-mode))
3a59f3db 448
83e51dbf
DK
449(defun stgit-undo (&optional arg)
450 "Run stg undo.
451With prefix argument, run it with the --hard flag."
452 (interactive "P")
453 (stgit-capture-output nil
454 (if arg
455 (stgit-run "undo" "--hard")
456 (stgit-run "undo")))
1f0bf00f 457 (stgit-reload))
83e51dbf 458
4d73c4d8
DK
459(defun stgit-refresh (&optional arg)
460 "Run stg refresh.
461With prefix argument, refresh the patch under point."
462 (interactive "P")
463 (let ((patchargs (if arg
464 (let ((patches (stgit-patches-marked-or-at-point)))
465 (cond ((null patches)
466 (error "no patch to update"))
467 ((> (length patches) 1)
468 (error "too many patches selected"))
469 (t
470 (cons "-p" patches))))
471 nil)))
472 (stgit-capture-output nil
473 (apply 'stgit-run "refresh" patchargs)))
474 (stgit-reload))
475
3a59f3db 476(provide 'stgit)