stgit.el: Fix some indentation
[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 ("m" . stgit-mark)
164 ("\d" . stgit-unmark-up)
165 ("u" . stgit-unmark-down)
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)
186 ("\C-_" . stgit-undo)
187 ("q" . stgit-quit))))
188
189 (defun stgit-mode ()
190 "Major mode for interacting with StGit.
191 Commands:
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)
200 (set (make-local-variable 'stgit-marked-patches) nil)
201 (set-variable 'truncate-lines 't)
202 (run-hooks 'stgit-mode-hook))
203
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
212 (defun stgit-clear-marks ()
213 (setq stgit-marked-patches '()))
214
215 (defun stgit-marked-patches ()
216 "Return the names of the marked patches."
217 (mapcar 'symbol-name stgit-marked-patches))
218
219 (defun stgit-patch-at-point ()
220 "Return the patch name on the current line"
221 (save-excursion
222 (beginning-of-line)
223 (if (looking-at "[>+-][ *]\\([^ ]*\\)")
224 (match-string-no-properties 1)
225 nil)))
226
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
236 (defun stgit-goto-patch (patch)
237 "Move point to the line containing PATCH"
238 (let ((p (point)))
239 (goto-char (point-min))
240 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
241 (progn (move-to-column goal-column)
242 t)
243 (goto-char p)
244 nil)))
245
246 (defun stgit-init ()
247 "Run stg init"
248 (interactive)
249 (stgit-capture-output nil
250 (stgit-run "init"))
251 (stgit-reload))
252
253 (defun stgit-mark ()
254 "Mark the patch under point"
255 (interactive)
256 (let ((patch (stgit-patch-at-point)))
257 (stgit-add-mark patch)
258 (stgit-reload))
259 (next-line))
260
261 (defun stgit-unmark-up ()
262 "Remove mark from the patch on the previous line"
263 (interactive)
264 (forward-line -1)
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))
274
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))
283 (stgit-reload)
284 (stgit-goto-patch name)))
285
286 (defun stgit-repair ()
287 "Run stg repair"
288 (interactive)
289 (stgit-capture-output nil
290 (stgit-run "repair"))
291 (stgit-reload))
292
293 (defun stgit-commit ()
294 "Run stg commit."
295 (interactive)
296 (stgit-capture-output nil (stgit-run "commit"))
297 (stgit-reload))
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)))
303 (stgit-reload))
304
305 (defun stgit-push-next (npatches)
306 "Push the first unapplied patch.
307 With numeric prefix argument, push that many patches."
308 (interactive "p")
309 (stgit-capture-output nil (stgit-run "push" "-n"
310 (number-to-string npatches)))
311 (stgit-reload))
312
313 (defun stgit-pop-next (npatches)
314 "Pop the topmost applied patch.
315 With numeric prefix argument, pop that many patches."
316 (interactive "p")
317 (stgit-capture-output nil (stgit-run "pop" "-n" (number-to-string npatches)))
318 (stgit-reload))
319
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))
333 (stgit-reload)))
334
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))
341 (stgit-reload)))
342
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))))
351
352 (defun stgit-edit ()
353 "Edit the patch on the current line"
354 (interactive)
355 (let ((patch (stgit-patch-at-point))
356 (edit-buf (get-buffer-create "*StGit edit*"))
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))
362 (stgit-run-silent "edit" "--save-template=-" patch))))
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
371 (stgit-reload))))
372
373 (defun stgit-new ()
374 "Create a new patch"
375 (interactive)
376 (let ((edit-buf (get-buffer-create "*StGit edit*")))
377 (log-edit 'stgit-confirm-new t nil edit-buf)))
378
379 (defun stgit-confirm-new ()
380 (interactive)
381 (let ((file (make-temp-file "stgit-edit-")))
382 (write-region (point-min) (point-max) file)
383 (stgit-capture-output nil
384 (stgit-run "new" "-f" file))
385 (with-current-buffer log-edit-parent-buffer
386 (stgit-reload))))
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))))
405
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))
415 (stgit-reload))))
416
417 (defun stgit-coalesce (patch-names)
418 "Run stg coalesce on the named patches"
419 (interactive (list (stgit-marked-patches)))
420 (let ((edit-buf (get-buffer-create "*StGit edit*"))
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))
426 (apply 'stgit-run-silent "coalesce" "--save-template=-" patch-names))))
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
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)))
441 (stgit-reload)
442 (goto-char pos)))))
443
444 (defun stgit-help ()
445 "Display help for the StGit mode."
446 (interactive)
447 (describe-function 'stgit-mode))
448
449 (defun stgit-undo (&optional arg)
450 "Run stg undo.
451 With 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")))
457 (stgit-reload))
458
459 (defun stgit-refresh (&optional arg)
460 "Run stg refresh.
461 With 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
476 (provide 'stgit)