chiark / gitweb /
stgit.el: Move stgit-rename to C-c C-r
[stgit] / contrib / stgit.el
CommitLineData
3a59f3db
KH
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
56d81fe5
DK
12(defun stgit (dir)
13 "Manage stgit patches"
14 (interactive "DDirectory: \n")
52144ce5 15 (switch-to-stgit-buffer (git-get-top-dir dir))
1f0bf00f 16 (stgit-reload))
56d81fe5 17
52144ce5
KH
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
56d81fe5
DK
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.
44Argument 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"
34afb86c
DK
55 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
56 (stgit-dir default-directory)
57 (inhibit-read-only t))
56d81fe5 58 (with-current-buffer output-buf
34afb86c
DK
59 (erase-buffer)
60 (setq default-directory stgit-dir)
61 (setq buffer-read-only t))
56d81fe5
DK
62 (let ((standard-output output-buf))
63 ,@body)
34afb86c
DK
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)))))
56d81fe5
DK
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
1f0bf00f 74(defun stgit-reload ()
56d81fe5
DK
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")
2870f8b8 83 (stgit-run "series" "--description")
6df83d42 84 (stgit-rescan)
56d81fe5
DK
85 (if curpatch
86 (stgit-goto-patch curpatch)
87 (goto-line curline))))
88
07f464e0
DK
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
6df83d42
DK
112(defun stgit-rescan ()
113 "Rescan the status buffer."
07f464e0 114 (save-excursion
6df83d42
DK
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))
8ee1e4b4 121 ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
6df83d42
DK
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)
1c2426dc 133 (setq marked (cons patchsym marked)))))
ad80ce22
DK
134 ((or (looking-at "stg series: Branch \".*\" not initialised")
135 (looking-at "stg series: .*: branch not initialized"))
1c2426dc
DK
136 (forward-line 1)
137 (insert "Run M-x stgit-init to initialise")))
6df83d42
DK
138 (forward-line 1))
139 (setq stgit-marked-patches (nreverse marked)))))
07f464e0 140
56d81fe5
DK
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)
6df83d42
DK
150 (define-key stgit-mode-map " " 'stgit-mark)
151 (define-key stgit-mode-map "\d" 'stgit-unmark)
0663524d
KH
152 (define-key stgit-mode-map "?" 'stgit-help)
153 (define-key stgit-mode-map "h" 'stgit-help)
9a3bb1f1
DK
154 (define-key stgit-mode-map "p" 'previous-line)
155 (define-key stgit-mode-map "n" 'next-line)
1f0bf00f 156 (define-key stgit-mode-map "g" 'stgit-reload)
1d087866 157 (define-key stgit-mode-map "\C-c\C-r" 'stgit-rename)
0bca35c8 158 (define-key stgit-mode-map "e" 'stgit-edit)
ea0def18 159 (define-key stgit-mode-map "c" 'stgit-coalesce)
64c097a0 160 (define-key stgit-mode-map "N" 'stgit-new)
b4d7f9ae 161 (define-key stgit-mode-map "R" 'stgit-repair)
c4aad9a7
DK
162 (define-key stgit-mode-map "C" 'stgit-commit)
163 (define-key stgit-mode-map "U" 'stgit-uncommit)
56d81fe5 164 (define-key stgit-mode-map ">" 'stgit-push-next)
82a12e6e 165 (define-key stgit-mode-map "<" 'stgit-pop-next)
f9182fca 166 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
c7adf5ef 167 (define-key stgit-mode-map "G" 'stgit-goto)
7755d7f1 168 (define-key stgit-mode-map "=" 'stgit-show)
83e51dbf
DK
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))
56d81fe5
DK
172
173(defun stgit-mode ()
174 "Major mode for interacting with StGit.
175Commands:
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)
6df83d42 184 (set (make-local-variable 'stgit-marked-patches) nil)
2870f8b8 185 (set-variable 'truncate-lines 't)
56d81fe5
DK
186 (run-hooks 'stgit-mode-hook))
187
6df83d42
DK
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
e6b1fdae
DK
196(defun stgit-clear-marks ()
197 (setq stgit-marked-patches '()))
198
6df83d42
DK
199(defun stgit-marked-patches ()
200 "Return the names of the marked patches."
201 (mapcar 'symbol-name stgit-marked-patches))
202
56d81fe5
DK
203(defun stgit-patch-at-point ()
204 "Return the patch name on the current line"
205 (save-excursion
206 (beginning-of-line)
6df83d42 207 (if (looking-at "[>+-][ *]\\([^ ]*\\)")
07f464e0 208 (match-string-no-properties 1)
56d81fe5
DK
209 nil)))
210
7755d7f1
KH
211(defun stgit-patches-marked-or-at-point ()
212 "Return the names of the marked patches, or the patch on the current line."
213 (if stgit-marked-patches
214 (stgit-marked-patches)
215 (let ((patch (stgit-patch-at-point)))
216 (if patch
217 (list patch)
218 '()))))
219
56d81fe5
DK
220(defun stgit-goto-patch (patch)
221 "Move point to the line containing PATCH"
222 (let ((p (point)))
223 (goto-char (point-min))
6df83d42 224 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
56d81fe5
DK
225 (progn (move-to-column goal-column)
226 t)
227 (goto-char p)
228 nil)))
229
1c2426dc
DK
230(defun stgit-init ()
231 "Run stg init"
232 (interactive)
233 (stgit-capture-output nil
234 (stgit-run "init"))
1f0bf00f 235 (stgit-reload))
1c2426dc 236
6df83d42
DK
237(defun stgit-mark ()
238 "Mark the patch under point"
239 (interactive)
240 (let ((patch (stgit-patch-at-point)))
241 (stgit-add-mark patch)
1f0bf00f 242 (stgit-reload))
6df83d42
DK
243 (next-line))
244
245(defun stgit-unmark ()
246 "Mark the patch on the previous line"
247 (interactive)
248 (forward-line -1)
249 (let ((patch (stgit-patch-at-point)))
250 (stgit-remove-mark patch)
1f0bf00f 251 (stgit-reload)))
6df83d42 252
56d81fe5
DK
253(defun stgit-rename (name)
254 "Rename the patch under point"
255 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
256 (let ((old-name (stgit-patch-at-point)))
257 (unless old-name
258 (error "No patch on this line"))
259 (stgit-capture-output nil
260 (stgit-run "rename" old-name name))
1f0bf00f 261 (stgit-reload)
56d81fe5
DK
262 (stgit-goto-patch name)))
263
26201d96
DK
264(defun stgit-repair ()
265 "Run stg repair"
266 (interactive)
267 (stgit-capture-output nil
268 (stgit-run "repair"))
1f0bf00f 269 (stgit-reload))
26201d96 270
c4aad9a7
DK
271(defun stgit-commit ()
272 "Run stg commit."
273 (interactive)
274 (stgit-capture-output nil (stgit-run "commit"))
1f0bf00f 275 (stgit-reload))
c4aad9a7
DK
276
277(defun stgit-uncommit (arg)
278 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
279 (interactive "p")
280 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
1f0bf00f 281 (stgit-reload))
c4aad9a7 282
0b661144
DK
283(defun stgit-push-next (npatches)
284 "Push the first unapplied patch.
285With numeric prefix argument, push that many patches."
286 (interactive "p")
287 (stgit-capture-output nil (stgit-run "push" "-n"
288 (number-to-string npatches)))
1f0bf00f 289 (stgit-reload))
56d81fe5 290
0b661144
DK
291(defun stgit-pop-next (npatches)
292 "Pop the topmost applied patch.
293With numeric prefix argument, pop that many patches."
294 (interactive "p")
295 (stgit-capture-output nil (stgit-run "pop" "-n" (number-to-string npatches)))
1f0bf00f 296 (stgit-reload))
56d81fe5 297
f9182fca
KH
298(defun stgit-applied-at-point ()
299 "Is the patch on the current line applied?"
300 (save-excursion
301 (beginning-of-line)
302 (looking-at "[>+]")))
303
304(defun stgit-push-or-pop ()
305 "Push or pop the patch on the current line"
306 (interactive)
307 (let ((patch (stgit-patch-at-point))
308 (applied (stgit-applied-at-point)))
309 (stgit-capture-output nil
310 (stgit-run (if applied "pop" "push") patch))
1f0bf00f 311 (stgit-reload)))
f9182fca 312
c7adf5ef
KH
313(defun stgit-goto ()
314 "Go to the patch on the current line"
315 (interactive)
316 (let ((patch (stgit-patch-at-point)))
317 (stgit-capture-output nil
318 (stgit-run "goto" patch))
1f0bf00f 319 (stgit-reload)))
c7adf5ef 320
56d81fe5
DK
321(defun stgit-show ()
322 "Show the patch on the current line"
323 (interactive)
324 (stgit-capture-output "*StGit patch*"
325 (stgit-run "show" (stgit-patch-at-point))
326 (with-current-buffer standard-output
327 (goto-char (point-min))
328 (diff-mode))))
0663524d 329
0bca35c8
DK
330(defun stgit-edit ()
331 "Edit the patch on the current line"
332 (interactive)
8ae7dc9d 333 (let ((patch (stgit-patch-at-point))
0780be79 334 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
335 (dir default-directory))
336 (log-edit 'stgit-confirm-edit t nil edit-buf)
337 (set (make-local-variable 'stgit-edit-patch) patch)
338 (setq default-directory dir)
339 (let ((standard-output edit-buf))
340 (stgit-run "edit" "--save-template=-" patch))))
341
342(defun stgit-confirm-edit ()
343 (interactive)
344 (let ((file (make-temp-file "stgit-edit-")))
345 (write-region (point-min) (point-max) file)
346 (stgit-capture-output nil
347 (stgit-run "edit" "-f" file stgit-edit-patch))
348 (with-current-buffer log-edit-parent-buffer
1f0bf00f 349 (stgit-reload))))
0bca35c8 350
64c097a0
DK
351(defun stgit-new ()
352 "Create a new patch"
353 (interactive)
0780be79 354 (let ((edit-buf (get-buffer-create "*StGit edit*")))
64c097a0
DK
355 (log-edit 'stgit-confirm-new t nil edit-buf)))
356
357(defun stgit-confirm-new ()
358 (interactive)
27b0f9e4 359 (let ((file (make-temp-file "stgit-edit-")))
64c097a0
DK
360 (write-region (point-min) (point-max) file)
361 (stgit-capture-output nil
27b0f9e4 362 (stgit-run "new" "-f" file))
64c097a0 363 (with-current-buffer log-edit-parent-buffer
1f0bf00f 364 (stgit-reload))))
64c097a0
DK
365
366(defun stgit-create-patch-name (description)
367 "Create a patch name from a long description"
368 (let ((patch ""))
369 (while (> (length description) 0)
370 (cond ((string-match "\\`[a-zA-Z_-]+" description)
371 (setq patch (downcase (concat patch (match-string 0 description))))
372 (setq description (substring description (match-end 0))))
373 ((string-match "\\` +" description)
374 (setq patch (concat patch "-"))
375 (setq description (substring description (match-end 0))))
376 ((string-match "\\`[^a-zA-Z_-]+" description)
377 (setq description (substring description (match-end 0))))))
378 (cond ((= (length patch) 0)
379 "patch")
380 ((> (length patch) 20)
381 (substring patch 0 20))
382 (t patch))))
0bca35c8 383
7755d7f1
KH
384(defun stgit-delete (patch-names)
385 "Delete the named patches"
386 (interactive (list (stgit-patches-marked-or-at-point)))
387 (if (zerop (length patch-names))
388 (error "No patches to delete")
389 (when (yes-or-no-p (format "Really delete %d patches? "
390 (length patch-names)))
391 (stgit-capture-output nil
392 (apply 'stgit-run "delete" patch-names))
1f0bf00f 393 (stgit-reload))))
7755d7f1 394
ea0def18
DK
395(defun stgit-coalesce (patch-names)
396 "Run stg coalesce on the named patches"
397 (interactive (list (stgit-marked-patches)))
0780be79 398 (let ((edit-buf (get-buffer-create "*StGit edit*"))
ea0def18
DK
399 (dir default-directory))
400 (log-edit 'stgit-confirm-coalesce t nil edit-buf)
401 (set (make-local-variable 'stgit-patches) patch-names)
402 (setq default-directory dir)
403 (let ((standard-output edit-buf))
404 (apply 'stgit-run "coalesce" "--save-template=-" patch-names))))
405
406(defun stgit-confirm-coalesce ()
407 (interactive)
408 (let ((file (make-temp-file "stgit-edit-")))
409 (write-region (point-min) (point-max) file)
410 (stgit-capture-output nil
411 (apply 'stgit-run "coalesce" "-f" file stgit-patches))
412 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
413 (stgit-clear-marks)
414 ;; Go to first marked patch and stay there
415 (goto-char (point-min))
416 (re-search-forward (concat "^[>+-]\\*") nil t)
417 (move-to-column goal-column)
418 (let ((pos (point)))
1f0bf00f 419 (stgit-reload)
e6b1fdae 420 (goto-char pos)))))
ea0def18 421
0663524d
KH
422(defun stgit-help ()
423 "Display help for the StGit mode."
424 (interactive)
425 (describe-function 'stgit-mode))
3a59f3db 426
83e51dbf
DK
427(defun stgit-undo (&optional arg)
428 "Run stg undo.
429With prefix argument, run it with the --hard flag."
430 (interactive "P")
431 (stgit-capture-output nil
432 (if arg
433 (stgit-run "undo" "--hard")
434 (stgit-run "undo")))
1f0bf00f 435 (stgit-reload))
83e51dbf 436
3a59f3db 437(provide 'stgit)