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") | |
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" | |
34afb86c DK |
44 | `(let ((output-buf (get-buffer-create ,(or name "*StGit output*"))) |
45 | (stgit-dir default-directory) | |
46 | (inhibit-read-only t)) | |
56d81fe5 | 47 | (with-current-buffer output-buf |
34afb86c DK |
48 | (erase-buffer) |
49 | (setq default-directory stgit-dir) | |
50 | (setq buffer-read-only t)) | |
56d81fe5 DK |
51 | (let ((standard-output output-buf)) |
52 | ,@body) | |
34afb86c DK |
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))))) | |
56d81fe5 DK |
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") | |
2870f8b8 | 72 | (stgit-run "series" "--description") |
6df83d42 | 73 | (stgit-rescan) |
56d81fe5 DK |
74 | (if curpatch |
75 | (stgit-goto-patch curpatch) | |
76 | (goto-line curline)))) | |
77 | ||
07f464e0 DK |
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 | ||
6df83d42 DK |
101 | (defun stgit-rescan () |
102 | "Rescan the status buffer." | |
07f464e0 | 103 | (save-excursion |
6df83d42 DK |
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))))) | |
07f464e0 | 125 | |
56d81fe5 DK |
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) | |
6df83d42 DK |
135 | (define-key stgit-mode-map " " 'stgit-mark) |
136 | (define-key stgit-mode-map "\d" 'stgit-unmark) | |
0663524d KH |
137 | (define-key stgit-mode-map "?" 'stgit-help) |
138 | (define-key stgit-mode-map "h" 'stgit-help) | |
9a3bb1f1 DK |
139 | (define-key stgit-mode-map "p" 'previous-line) |
140 | (define-key stgit-mode-map "n" 'next-line) | |
56d81fe5 DK |
141 | (define-key stgit-mode-map "g" 'stgit-refresh) |
142 | (define-key stgit-mode-map "r" 'stgit-rename) | |
0bca35c8 | 143 | (define-key stgit-mode-map "e" 'stgit-edit) |
ea0def18 | 144 | (define-key stgit-mode-map "c" 'stgit-coalesce) |
64c097a0 | 145 | (define-key stgit-mode-map "N" 'stgit-new) |
b4d7f9ae | 146 | (define-key stgit-mode-map "R" 'stgit-repair) |
c4aad9a7 DK |
147 | (define-key stgit-mode-map "C" 'stgit-commit) |
148 | (define-key stgit-mode-map "U" 'stgit-uncommit) | |
56d81fe5 | 149 | (define-key stgit-mode-map ">" 'stgit-push-next) |
82a12e6e | 150 | (define-key stgit-mode-map "<" 'stgit-pop-next) |
f9182fca | 151 | (define-key stgit-mode-map "P" 'stgit-push-or-pop) |
c7adf5ef | 152 | (define-key stgit-mode-map "G" 'stgit-goto) |
56d81fe5 DK |
153 | (define-key stgit-mode-map "=" 'stgit-show)) |
154 | ||
155 | (defun stgit-mode () | |
156 | "Major mode for interacting with StGit. | |
157 | Commands: | |
158 | \\{stgit-mode-map}" | |
159 | (kill-all-local-variables) | |
160 | (buffer-disable-undo) | |
161 | (setq mode-name "StGit" | |
162 | major-mode 'stgit-mode | |
163 | goal-column 2) | |
164 | (use-local-map stgit-mode-map) | |
165 | (set (make-local-variable 'list-buffers-directory) default-directory) | |
6df83d42 | 166 | (set (make-local-variable 'stgit-marked-patches) nil) |
2870f8b8 | 167 | (set-variable 'truncate-lines 't) |
56d81fe5 DK |
168 | (run-hooks 'stgit-mode-hook)) |
169 | ||
6df83d42 DK |
170 | (defun stgit-add-mark (patch) |
171 | (let ((patchsym (intern patch))) | |
172 | (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))) | |
173 | ||
174 | (defun stgit-remove-mark (patch) | |
175 | (let ((patchsym (intern patch))) | |
176 | (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))) | |
177 | ||
178 | (defun stgit-marked-patches () | |
179 | "Return the names of the marked patches." | |
180 | (mapcar 'symbol-name stgit-marked-patches)) | |
181 | ||
56d81fe5 DK |
182 | (defun stgit-patch-at-point () |
183 | "Return the patch name on the current line" | |
184 | (save-excursion | |
185 | (beginning-of-line) | |
6df83d42 | 186 | (if (looking-at "[>+-][ *]\\([^ ]*\\)") |
07f464e0 | 187 | (match-string-no-properties 1) |
56d81fe5 DK |
188 | nil))) |
189 | ||
190 | (defun stgit-goto-patch (patch) | |
191 | "Move point to the line containing PATCH" | |
192 | (let ((p (point))) | |
193 | (goto-char (point-min)) | |
6df83d42 | 194 | (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t) |
56d81fe5 DK |
195 | (progn (move-to-column goal-column) |
196 | t) | |
197 | (goto-char p) | |
198 | nil))) | |
199 | ||
6df83d42 DK |
200 | (defun stgit-mark () |
201 | "Mark the patch under point" | |
202 | (interactive) | |
203 | (let ((patch (stgit-patch-at-point))) | |
204 | (stgit-add-mark patch) | |
205 | (stgit-refresh)) | |
206 | (next-line)) | |
207 | ||
208 | (defun stgit-unmark () | |
209 | "Mark the patch on the previous line" | |
210 | (interactive) | |
211 | (forward-line -1) | |
212 | (let ((patch (stgit-patch-at-point))) | |
213 | (stgit-remove-mark patch) | |
214 | (stgit-refresh))) | |
215 | ||
56d81fe5 DK |
216 | (defun stgit-rename (name) |
217 | "Rename the patch under point" | |
218 | (interactive (list (read-string "Patch name: " (stgit-patch-at-point)))) | |
219 | (let ((old-name (stgit-patch-at-point))) | |
220 | (unless old-name | |
221 | (error "No patch on this line")) | |
222 | (stgit-capture-output nil | |
223 | (stgit-run "rename" old-name name)) | |
224 | (stgit-refresh) | |
225 | (stgit-goto-patch name))) | |
226 | ||
26201d96 DK |
227 | (defun stgit-repair () |
228 | "Run stg repair" | |
229 | (interactive) | |
230 | (stgit-capture-output nil | |
231 | (stgit-run "repair")) | |
232 | (stgit-refresh)) | |
233 | ||
c4aad9a7 DK |
234 | (defun stgit-commit () |
235 | "Run stg commit." | |
236 | (interactive) | |
237 | (stgit-capture-output nil (stgit-run "commit")) | |
238 | (stgit-refresh)) | |
239 | ||
240 | (defun stgit-uncommit (arg) | |
241 | "Run stg uncommit. Numeric arg determines number of patches to uncommit." | |
242 | (interactive "p") | |
243 | (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg))) | |
244 | (stgit-refresh)) | |
245 | ||
56d81fe5 | 246 | (defun stgit-push-next () |
82a12e6e | 247 | "Push the first unapplied patch" |
56d81fe5 | 248 | (interactive) |
82a12e6e KH |
249 | (stgit-capture-output nil (stgit-run "push")) |
250 | (stgit-refresh)) | |
56d81fe5 | 251 | |
82a12e6e KH |
252 | (defun stgit-pop-next () |
253 | "Pop the topmost applied patch" | |
56d81fe5 | 254 | (interactive) |
82a12e6e KH |
255 | (stgit-capture-output nil (stgit-run "pop")) |
256 | (stgit-refresh)) | |
56d81fe5 | 257 | |
f9182fca KH |
258 | (defun stgit-applied-at-point () |
259 | "Is the patch on the current line applied?" | |
260 | (save-excursion | |
261 | (beginning-of-line) | |
262 | (looking-at "[>+]"))) | |
263 | ||
264 | (defun stgit-push-or-pop () | |
265 | "Push or pop the patch on the current line" | |
266 | (interactive) | |
267 | (let ((patch (stgit-patch-at-point)) | |
268 | (applied (stgit-applied-at-point))) | |
269 | (stgit-capture-output nil | |
270 | (stgit-run (if applied "pop" "push") patch)) | |
271 | (stgit-refresh))) | |
272 | ||
c7adf5ef KH |
273 | (defun stgit-goto () |
274 | "Go to the patch on the current line" | |
275 | (interactive) | |
276 | (let ((patch (stgit-patch-at-point))) | |
277 | (stgit-capture-output nil | |
278 | (stgit-run "goto" patch)) | |
279 | (stgit-refresh))) | |
280 | ||
56d81fe5 DK |
281 | (defun stgit-show () |
282 | "Show the patch on the current line" | |
283 | (interactive) | |
284 | (stgit-capture-output "*StGit patch*" | |
285 | (stgit-run "show" (stgit-patch-at-point)) | |
286 | (with-current-buffer standard-output | |
287 | (goto-char (point-min)) | |
288 | (diff-mode)))) | |
0663524d | 289 | |
0bca35c8 DK |
290 | (defun stgit-edit () |
291 | "Edit the patch on the current line" | |
292 | (interactive) | |
8ae7dc9d | 293 | (let ((patch (stgit-patch-at-point)) |
0780be79 | 294 | (edit-buf (get-buffer-create "*StGit edit*")) |
0bca35c8 DK |
295 | (dir default-directory)) |
296 | (log-edit 'stgit-confirm-edit t nil edit-buf) | |
297 | (set (make-local-variable 'stgit-edit-patch) patch) | |
298 | (setq default-directory dir) | |
299 | (let ((standard-output edit-buf)) | |
300 | (stgit-run "edit" "--save-template=-" patch)))) | |
301 | ||
302 | (defun stgit-confirm-edit () | |
303 | (interactive) | |
304 | (let ((file (make-temp-file "stgit-edit-"))) | |
305 | (write-region (point-min) (point-max) file) | |
306 | (stgit-capture-output nil | |
307 | (stgit-run "edit" "-f" file stgit-edit-patch)) | |
308 | (with-current-buffer log-edit-parent-buffer | |
309 | (stgit-refresh)))) | |
310 | ||
64c097a0 DK |
311 | (defun stgit-new () |
312 | "Create a new patch" | |
313 | (interactive) | |
0780be79 | 314 | (let ((edit-buf (get-buffer-create "*StGit edit*"))) |
64c097a0 DK |
315 | (log-edit 'stgit-confirm-new t nil edit-buf))) |
316 | ||
317 | (defun stgit-confirm-new () | |
318 | (interactive) | |
319 | (let ((file (make-temp-file "stgit-edit-")) | |
320 | (patch (stgit-create-patch-name | |
321 | (buffer-substring (point-min) | |
322 | (save-excursion (goto-char (point-min)) | |
323 | (end-of-line) | |
324 | (point)))))) | |
325 | (write-region (point-min) (point-max) file) | |
326 | (stgit-capture-output nil | |
327 | (stgit-run "new" "-m" "placeholder" patch) | |
328 | (stgit-run "edit" "-f" file patch)) | |
329 | (with-current-buffer log-edit-parent-buffer | |
330 | (stgit-refresh)))) | |
331 | ||
332 | (defun stgit-create-patch-name (description) | |
333 | "Create a patch name from a long description" | |
334 | (let ((patch "")) | |
335 | (while (> (length description) 0) | |
336 | (cond ((string-match "\\`[a-zA-Z_-]+" description) | |
337 | (setq patch (downcase (concat patch (match-string 0 description)))) | |
338 | (setq description (substring description (match-end 0)))) | |
339 | ((string-match "\\` +" description) | |
340 | (setq patch (concat patch "-")) | |
341 | (setq description (substring description (match-end 0)))) | |
342 | ((string-match "\\`[^a-zA-Z_-]+" description) | |
343 | (setq description (substring description (match-end 0)))))) | |
344 | (cond ((= (length patch) 0) | |
345 | "patch") | |
346 | ((> (length patch) 20) | |
347 | (substring patch 0 20)) | |
348 | (t patch)))) | |
0bca35c8 | 349 | |
ea0def18 DK |
350 | (defun stgit-coalesce (patch-names) |
351 | "Run stg coalesce on the named patches" | |
352 | (interactive (list (stgit-marked-patches))) | |
0780be79 | 353 | (let ((edit-buf (get-buffer-create "*StGit edit*")) |
ea0def18 DK |
354 | (dir default-directory)) |
355 | (log-edit 'stgit-confirm-coalesce t nil edit-buf) | |
356 | (set (make-local-variable 'stgit-patches) patch-names) | |
357 | (setq default-directory dir) | |
358 | (let ((standard-output edit-buf)) | |
359 | (apply 'stgit-run "coalesce" "--save-template=-" patch-names)))) | |
360 | ||
361 | (defun stgit-confirm-coalesce () | |
362 | (interactive) | |
363 | (let ((file (make-temp-file "stgit-edit-"))) | |
364 | (write-region (point-min) (point-max) file) | |
365 | (stgit-capture-output nil | |
366 | (apply 'stgit-run "coalesce" "-f" file stgit-patches)) | |
367 | (with-current-buffer log-edit-parent-buffer | |
368 | (stgit-refresh)))) | |
369 | ||
0663524d KH |
370 | (defun stgit-help () |
371 | "Display help for the StGit mode." | |
372 | (interactive) | |
373 | (describe-function 'stgit-mode)) | |
3a59f3db KH |
374 | |
375 | (provide 'stgit) |