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