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