chiark / gitweb /
Emacs mode: Add mark command
[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 dir)
16   (stgit-refresh))
17
18 (defun switch-to-stgit-buffer (dir)
19   "Switch to a (possibly new) buffer displaying StGit patches for DIR"
20   (setq dir (file-name-as-directory dir))
21   (let ((buffers (buffer-list)))
22     (while (and buffers
23                 (not (with-current-buffer (car buffers)
24                        (and (eq major-mode 'stgit-mode)
25                             (string= default-directory dir)))))
26       (setq buffers (cdr buffers)))
27     (switch-to-buffer (if buffers
28                           (car buffers)
29                         (create-stgit-buffer dir)))))
30
31 (defun create-stgit-buffer (dir)
32   "Create a buffer for showing StGit patches.
33 Argument DIR is the repository path."
34   (let ((buf (create-file-buffer (concat dir "*stgit*")))
35         (inhibit-read-only t))
36     (with-current-buffer buf
37       (setq default-directory dir)
38       (stgit-mode)
39       (setq buffer-read-only t))
40     buf))
41
42 (defmacro stgit-capture-output (name &rest body)
43   "Capture StGit output and show it in a window at the end"
44   `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
45          (stgit-dir default-directory)
46          (inhibit-read-only t))
47      (with-current-buffer output-buf
48        (erase-buffer)
49        (setq default-directory stgit-dir)
50        (setq buffer-read-only t))
51      (let ((standard-output output-buf))
52        ,@body)
53      (with-current-buffer output-buf
54        (set-buffer-modified-p nil)
55        (setq buffer-read-only t)
56        (if (< (point-min) (point-max))
57            (display-buffer output-buf t)))))
58 (put 'stgit-capture-output 'lisp-indent-function 1)
59
60 (defun stgit-run (&rest args)
61   (apply 'call-process "stg" nil standard-output nil args))
62
63 (defun stgit-refresh ()
64   "Update the contents of the stgit buffer"
65   (interactive)
66   (let ((inhibit-read-only t)
67         (curline (line-number-at-pos))
68         (curpatch (stgit-patch-at-point)))
69     (erase-buffer)
70     (insert "Branch: ")
71     (stgit-run "branch")
72     (stgit-run "series" "--description")
73     (stgit-rescan)
74     (if curpatch
75         (stgit-goto-patch curpatch)
76       (goto-line curline))))
77
78 (defface stgit-description-face
79   '((((background dark)) (:foreground "tan"))
80     (((background light)) (:foreground "dark red")))
81   "The face used for StGit desriptions")
82
83 (defface stgit-top-patch-face
84   '((((background dark)) (:weight bold :foreground "yellow"))
85     (((background light)) (:weight bold :foreground "purple"))
86     (t (:weight bold)))
87   "The face used for the top patch names")
88
89 (defface stgit-applied-patch-face
90   '((((background dark)) (:foreground "light yellow"))
91     (((background light)) (:foreground "purple"))
92     (t ()))
93   "The face used for applied patch names")
94
95 (defface stgit-unapplied-patch-face
96   '((((background dark)) (:foreground "gray80"))
97     (((background light)) (:foreground "orchid"))
98     (t ()))
99   "The face used for unapplied patch names")
100
101 (defun stgit-rescan ()
102   "Rescan the status buffer."
103   (save-excursion
104     (let ((marked ()))
105       (goto-char (point-min))
106       (while (not (eobp))
107         (cond ((looking-at "Branch: \\(.*\\)")
108                (put-text-property (match-beginning 1) (match-end 1)
109                                   'face 'bold))
110               ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *| \\(.*\\)")
111                (let ((state (match-string 1))
112                      (patchsym (intern (match-string 3))))
113                  (put-text-property
114                   (match-beginning 3) (match-end 3) 'face
115                   (cond ((string= state ">") 'stgit-top-patch-face)
116                         ((string= state "+") 'stgit-applied-patch-face)
117                         ((string= state "-") 'stgit-unapplied-patch-face)))
118                  (put-text-property (match-beginning 4) (match-end 4)
119                                     'face 'stgit-description-face)
120                  (when (memq patchsym stgit-marked-patches)
121                    (replace-match "*" nil nil nil 2)
122                    (setq marked (cons patchsym marked))))))
123         (forward-line 1))
124       (setq stgit-marked-patches (nreverse marked)))))
125
126 (defvar stgit-mode-hook nil
127   "Run after `stgit-mode' is setup.")
128
129 (defvar stgit-mode-map nil
130   "Keymap for StGit major mode.")
131
132 (unless stgit-mode-map
133   (setq stgit-mode-map (make-keymap))
134   (suppress-keymap stgit-mode-map)
135   (define-key stgit-mode-map " "   'stgit-mark)
136   (define-key stgit-mode-map "\d" 'stgit-unmark)
137   (define-key stgit-mode-map "?"   'stgit-help)
138   (define-key stgit-mode-map "h"   'stgit-help)
139   (define-key stgit-mode-map "p"   'previous-line)
140   (define-key stgit-mode-map "n"   'next-line)
141   (define-key stgit-mode-map "g"   'stgit-refresh)
142   (define-key stgit-mode-map "r"   'stgit-rename)
143   (define-key stgit-mode-map "e"   'stgit-edit)
144   (define-key stgit-mode-map "N"   'stgit-new)
145   (define-key stgit-mode-map "\C-r"   'stgit-repair)
146   (define-key stgit-mode-map "C"   'stgit-commit)
147   (define-key stgit-mode-map "U"   'stgit-uncommit)
148   (define-key stgit-mode-map ">"   'stgit-push-next)
149   (define-key stgit-mode-map "<"   'stgit-pop-next)
150   (define-key stgit-mode-map "P"   'stgit-push-or-pop)
151   (define-key stgit-mode-map "G"   'stgit-goto)
152   (define-key stgit-mode-map "="   'stgit-show))
153
154 (defun stgit-mode ()
155   "Major mode for interacting with StGit.
156 Commands:
157 \\{stgit-mode-map}"
158   (kill-all-local-variables)
159   (buffer-disable-undo)
160   (setq mode-name "StGit"
161         major-mode 'stgit-mode
162         goal-column 2)
163   (use-local-map stgit-mode-map)
164   (set (make-local-variable 'list-buffers-directory) default-directory)
165   (set (make-local-variable 'stgit-marked-patches) nil)
166   (set-variable 'truncate-lines 't)
167   (run-hooks 'stgit-mode-hook))
168
169 (defun stgit-add-mark (patch)
170   (let ((patchsym (intern patch)))
171     (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
172
173 (defun stgit-remove-mark (patch)
174   (let ((patchsym (intern patch)))
175     (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
176
177 (defun stgit-marked-patches ()
178   "Return the names of the marked patches."
179   (mapcar 'symbol-name stgit-marked-patches))
180
181 (defun stgit-patch-at-point ()
182   "Return the patch name on the current line"
183   (save-excursion
184     (beginning-of-line)
185     (if (looking-at "[>+-][ *]\\([^ ]*\\)")
186         (match-string-no-properties 1)
187       nil)))
188
189 (defun stgit-goto-patch (patch)
190   "Move point to the line containing PATCH"
191   (let ((p (point)))
192     (goto-char (point-min))
193     (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
194         (progn (move-to-column goal-column)
195                t)
196       (goto-char p)
197       nil)))
198
199 (defun stgit-mark ()
200   "Mark the patch under point"
201   (interactive)
202   (let ((patch (stgit-patch-at-point)))
203     (stgit-add-mark patch)
204     (stgit-refresh))
205   (next-line))
206
207 (defun stgit-unmark ()
208   "Mark the patch on the previous line"
209   (interactive)
210   (forward-line -1)
211   (let ((patch (stgit-patch-at-point)))
212     (stgit-remove-mark patch)
213     (stgit-refresh)))
214
215 (defun stgit-rename (name)
216   "Rename the patch under point"
217   (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
218   (let ((old-name (stgit-patch-at-point)))
219     (unless old-name
220       (error "No patch on this line"))
221     (stgit-capture-output nil
222       (stgit-run "rename" old-name name))
223     (stgit-refresh)
224     (stgit-goto-patch name)))
225
226 (defun stgit-repair ()
227   "Run stg repair"
228   (interactive)
229   (stgit-capture-output nil
230    (stgit-run "repair"))
231   (stgit-refresh))
232
233 (defun stgit-commit ()
234   "Run stg commit."
235   (interactive)
236   (stgit-capture-output nil (stgit-run "commit"))
237   (stgit-refresh))
238
239 (defun stgit-uncommit (arg)
240   "Run stg uncommit. Numeric arg determines number of patches to uncommit."
241   (interactive "p")
242   (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
243   (stgit-refresh))
244
245 (defun stgit-push-next ()
246   "Push the first unapplied patch"
247   (interactive)
248   (stgit-capture-output nil (stgit-run "push"))
249   (stgit-refresh))
250
251 (defun stgit-pop-next ()
252   "Pop the topmost applied patch"
253   (interactive)
254   (stgit-capture-output nil (stgit-run "pop"))
255   (stgit-refresh))
256
257 (defun stgit-applied-at-point ()
258   "Is the patch on the current line applied?"
259   (save-excursion
260     (beginning-of-line)
261     (looking-at "[>+]")))
262
263 (defun stgit-push-or-pop ()
264   "Push or pop the patch on the current line"
265   (interactive)
266   (let ((patch (stgit-patch-at-point))
267         (applied (stgit-applied-at-point)))
268     (stgit-capture-output nil
269        (stgit-run (if applied "pop" "push") patch))
270     (stgit-refresh)))
271
272 (defun stgit-goto ()
273   "Go to the patch on the current line"
274   (interactive)
275   (let ((patch (stgit-patch-at-point)))
276     (stgit-capture-output nil
277        (stgit-run "goto" patch))
278     (stgit-refresh)))
279
280 (defun stgit-show ()
281   "Show the patch on the current line"
282   (interactive)
283   (stgit-capture-output "*StGit patch*"
284     (stgit-run "show" (stgit-patch-at-point))
285     (with-current-buffer standard-output
286       (goto-char (point-min))
287       (diff-mode))))
288
289 (defun stgit-edit ()
290   "Edit the patch on the current line"
291   (interactive)
292   (let ((patch (if (stgit-applied-at-point)
293                    (stgit-patch-at-point)
294                  (error "This patch is not applied")))
295         (edit-buf (get-buffer-create "*stgit edit*"))
296         (dir default-directory))
297     (log-edit 'stgit-confirm-edit t nil edit-buf)
298     (set (make-local-variable 'stgit-edit-patch) patch)
299     (setq default-directory dir)
300     (let ((standard-output edit-buf))
301       (stgit-run "edit" "--save-template=-" patch))))
302
303 (defun stgit-confirm-edit ()
304   (interactive)
305   (let ((file (make-temp-file "stgit-edit-")))
306     (write-region (point-min) (point-max) file)
307     (stgit-capture-output nil
308       (stgit-run "edit" "-f" file stgit-edit-patch))
309     (with-current-buffer log-edit-parent-buffer
310       (stgit-refresh))))
311
312 (defun stgit-new ()
313   "Create a new patch"
314   (interactive)
315   (let ((edit-buf (get-buffer-create "*stgit edit*")))
316     (log-edit 'stgit-confirm-new t nil edit-buf)))
317
318 (defun stgit-confirm-new ()
319   (interactive)
320   (let ((file (make-temp-file "stgit-edit-"))
321         (patch (stgit-create-patch-name
322                 (buffer-substring (point-min)
323                                   (save-excursion (goto-char (point-min))
324                                                   (end-of-line)
325                                                   (point))))))
326     (write-region (point-min) (point-max) file)
327     (stgit-capture-output nil
328       (stgit-run "new" "-m" "placeholder" patch)
329       (stgit-run "edit" "-f" file patch))
330     (with-current-buffer log-edit-parent-buffer
331       (stgit-refresh))))
332
333 (defun stgit-create-patch-name (description)
334   "Create a patch name from a long description"
335   (let ((patch ""))
336     (while (> (length description) 0)
337       (cond ((string-match "\\`[a-zA-Z_-]+" description)
338              (setq patch (downcase (concat patch (match-string 0 description))))
339              (setq description (substring description (match-end 0))))
340             ((string-match "\\` +" description)
341              (setq patch (concat patch "-"))
342              (setq description (substring description (match-end 0))))
343             ((string-match "\\`[^a-zA-Z_-]+" description)
344              (setq description (substring description (match-end 0))))))
345     (cond ((= (length patch) 0)
346            "patch")
347           ((> (length patch) 20)
348            (substring patch 0 20))
349           (t patch))))
350
351 (defun stgit-help ()
352   "Display help for the StGit mode."
353   (interactive)
354   (describe-function 'stgit-mode))
355
356 (provide 'stgit)