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