chiark / gitweb /
Print conflict details with the new infrastructure (bug #11181)
[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-refresh))
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-refresh ()
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-refresh)
157   (define-key stgit-mode-map "r"   'stgit-rename)
158   (define-key stgit-mode-map "e"   'stgit-edit)
159   (define-key stgit-mode-map "c"   'stgit-coalesce)
160   (define-key stgit-mode-map "N"   'stgit-new)
161   (define-key stgit-mode-map "R"   'stgit-repair)
162   (define-key stgit-mode-map "C"   'stgit-commit)
163   (define-key stgit-mode-map "U"   'stgit-uncommit)
164   (define-key stgit-mode-map ">"   'stgit-push-next)
165   (define-key stgit-mode-map "<"   'stgit-pop-next)
166   (define-key stgit-mode-map "P"   'stgit-push-or-pop)
167   (define-key stgit-mode-map "G"   'stgit-goto)
168   (define-key stgit-mode-map "="   'stgit-show)
169   (define-key stgit-mode-map "D"   'stgit-delete)
170   (define-key stgit-mode-map [(control ?/)] 'stgit-undo)
171   (define-key stgit-mode-map "\C-_" 'stgit-undo))
172
173 (defun stgit-mode ()
174   "Major mode for interacting with StGit.
175 Commands:
176 \\{stgit-mode-map}"
177   (kill-all-local-variables)
178   (buffer-disable-undo)
179   (setq mode-name "StGit"
180         major-mode 'stgit-mode
181         goal-column 2)
182   (use-local-map stgit-mode-map)
183   (set (make-local-variable 'list-buffers-directory) default-directory)
184   (set (make-local-variable 'stgit-marked-patches) nil)
185   (set-variable 'truncate-lines 't)
186   (run-hooks 'stgit-mode-hook))
187
188 (defun stgit-add-mark (patch)
189   (let ((patchsym (intern patch)))
190     (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
191
192 (defun stgit-remove-mark (patch)
193   (let ((patchsym (intern patch)))
194     (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
195
196 (defun stgit-marked-patches ()
197   "Return the names of the marked patches."
198   (mapcar 'symbol-name stgit-marked-patches))
199
200 (defun stgit-patch-at-point ()
201   "Return the patch name on the current line"
202   (save-excursion
203     (beginning-of-line)
204     (if (looking-at "[>+-][ *]\\([^ ]*\\)")
205         (match-string-no-properties 1)
206       nil)))
207
208 (defun stgit-patches-marked-or-at-point ()
209   "Return the names of the marked patches, or the patch on the current line."
210   (if stgit-marked-patches
211       (stgit-marked-patches)
212     (let ((patch (stgit-patch-at-point)))
213       (if patch
214           (list patch)
215         '()))))
216
217 (defun stgit-goto-patch (patch)
218   "Move point to the line containing PATCH"
219   (let ((p (point)))
220     (goto-char (point-min))
221     (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
222         (progn (move-to-column goal-column)
223                t)
224       (goto-char p)
225       nil)))
226
227 (defun stgit-init ()
228   "Run stg init"
229   (interactive)
230   (stgit-capture-output nil
231    (stgit-run "init"))
232   (stgit-refresh))
233
234 (defun stgit-mark ()
235   "Mark the patch under point"
236   (interactive)
237   (let ((patch (stgit-patch-at-point)))
238     (stgit-add-mark patch)
239     (stgit-refresh))
240   (next-line))
241
242 (defun stgit-unmark ()
243   "Mark the patch on the previous line"
244   (interactive)
245   (forward-line -1)
246   (let ((patch (stgit-patch-at-point)))
247     (stgit-remove-mark patch)
248     (stgit-refresh)))
249
250 (defun stgit-rename (name)
251   "Rename the patch under point"
252   (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
253   (let ((old-name (stgit-patch-at-point)))
254     (unless old-name
255       (error "No patch on this line"))
256     (stgit-capture-output nil
257       (stgit-run "rename" old-name name))
258     (stgit-refresh)
259     (stgit-goto-patch name)))
260
261 (defun stgit-repair ()
262   "Run stg repair"
263   (interactive)
264   (stgit-capture-output nil
265    (stgit-run "repair"))
266   (stgit-refresh))
267
268 (defun stgit-commit ()
269   "Run stg commit."
270   (interactive)
271   (stgit-capture-output nil (stgit-run "commit"))
272   (stgit-refresh))
273
274 (defun stgit-uncommit (arg)
275   "Run stg uncommit. Numeric arg determines number of patches to uncommit."
276   (interactive "p")
277   (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
278   (stgit-refresh))
279
280 (defun stgit-push-next (npatches)
281   "Push the first unapplied patch.
282 With numeric prefix argument, push that many patches."
283   (interactive "p")
284   (stgit-capture-output nil (stgit-run "push" "-n"
285                                        (number-to-string npatches)))
286   (stgit-refresh))
287
288 (defun stgit-pop-next (npatches)
289   "Pop the topmost applied patch.
290 With numeric prefix argument, pop that many patches."
291   (interactive "p")
292   (stgit-capture-output nil (stgit-run "pop" "-n" (number-to-string npatches)))
293   (stgit-refresh))
294
295 (defun stgit-applied-at-point ()
296   "Is the patch on the current line applied?"
297   (save-excursion
298     (beginning-of-line)
299     (looking-at "[>+]")))
300
301 (defun stgit-push-or-pop ()
302   "Push or pop the patch on the current line"
303   (interactive)
304   (let ((patch (stgit-patch-at-point))
305         (applied (stgit-applied-at-point)))
306     (stgit-capture-output nil
307        (stgit-run (if applied "pop" "push") patch))
308     (stgit-refresh)))
309
310 (defun stgit-goto ()
311   "Go to the patch on the current line"
312   (interactive)
313   (let ((patch (stgit-patch-at-point)))
314     (stgit-capture-output nil
315        (stgit-run "goto" patch))
316     (stgit-refresh)))
317
318 (defun stgit-show ()
319   "Show the patch on the current line"
320   (interactive)
321   (stgit-capture-output "*StGit patch*"
322     (stgit-run "show" (stgit-patch-at-point))
323     (with-current-buffer standard-output
324       (goto-char (point-min))
325       (diff-mode))))
326
327 (defun stgit-edit ()
328   "Edit the patch on the current line"
329   (interactive)
330   (let ((patch (stgit-patch-at-point))
331         (edit-buf (get-buffer-create "*StGit edit*"))
332         (dir default-directory))
333     (log-edit 'stgit-confirm-edit t nil edit-buf)
334     (set (make-local-variable 'stgit-edit-patch) patch)
335     (setq default-directory dir)
336     (let ((standard-output edit-buf))
337       (stgit-run "edit" "--save-template=-" patch))))
338
339 (defun stgit-confirm-edit ()
340   (interactive)
341   (let ((file (make-temp-file "stgit-edit-")))
342     (write-region (point-min) (point-max) file)
343     (stgit-capture-output nil
344       (stgit-run "edit" "-f" file stgit-edit-patch))
345     (with-current-buffer log-edit-parent-buffer
346       (stgit-refresh))))
347
348 (defun stgit-new ()
349   "Create a new patch"
350   (interactive)
351   (let ((edit-buf (get-buffer-create "*StGit edit*")))
352     (log-edit 'stgit-confirm-new t nil edit-buf)))
353
354 (defun stgit-confirm-new ()
355   (interactive)
356   (let ((file (make-temp-file "stgit-edit-")))
357     (write-region (point-min) (point-max) file)
358     (stgit-capture-output nil
359       (stgit-run "new" "-f" file))
360     (with-current-buffer log-edit-parent-buffer
361       (stgit-refresh))))
362
363 (defun stgit-create-patch-name (description)
364   "Create a patch name from a long description"
365   (let ((patch ""))
366     (while (> (length description) 0)
367       (cond ((string-match "\\`[a-zA-Z_-]+" description)
368              (setq patch (downcase (concat patch (match-string 0 description))))
369              (setq description (substring description (match-end 0))))
370             ((string-match "\\` +" description)
371              (setq patch (concat patch "-"))
372              (setq description (substring description (match-end 0))))
373             ((string-match "\\`[^a-zA-Z_-]+" description)
374              (setq description (substring description (match-end 0))))))
375     (cond ((= (length patch) 0)
376            "patch")
377           ((> (length patch) 20)
378            (substring patch 0 20))
379           (t patch))))
380
381 (defun stgit-delete (patch-names)
382   "Delete the named patches"
383   (interactive (list (stgit-patches-marked-or-at-point)))
384   (if (zerop (length patch-names))
385       (error "No patches to delete")
386     (when (yes-or-no-p (format "Really delete %d patches? "
387                                (length patch-names)))
388       (stgit-capture-output nil
389         (apply 'stgit-run "delete" patch-names))
390       (stgit-refresh))))
391
392 (defun stgit-coalesce (patch-names)
393   "Run stg coalesce on the named patches"
394   (interactive (list (stgit-marked-patches)))
395   (let ((edit-buf (get-buffer-create "*StGit edit*"))
396         (dir default-directory))
397     (log-edit 'stgit-confirm-coalesce t nil edit-buf)
398     (set (make-local-variable 'stgit-patches) patch-names)
399     (setq default-directory dir)
400     (let ((standard-output edit-buf))
401       (apply 'stgit-run "coalesce" "--save-template=-" patch-names))))
402
403 (defun stgit-confirm-coalesce ()
404   (interactive)
405   (let ((file (make-temp-file "stgit-edit-")))
406     (write-region (point-min) (point-max) file)
407     (stgit-capture-output nil
408       (apply 'stgit-run "coalesce" "-f" file stgit-patches))
409     (with-current-buffer log-edit-parent-buffer
410       (stgit-refresh))))
411
412 (defun stgit-help ()
413   "Display help for the StGit mode."
414   (interactive)
415   (describe-function 'stgit-mode))
416
417 (defun stgit-undo (&optional arg)
418   "Run stg undo.
419 With prefix argument, run it with the --hard flag."
420   (interactive "P")
421   (stgit-capture-output nil
422     (if arg
423         (stgit-run "undo" "--hard")
424       (stgit-run "undo")))
425   (stgit-refresh))
426
427 (provide 'stgit)