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