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") |
07f464e0 | 73 | (stgit-rehighlight (point-min) (point-max)) |
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 | ||
101 | (defun stgit-rehighlight (start end) | |
102 | "Refresh fontification of region between START and END." | |
103 | (save-excursion | |
104 | (goto-char start) | |
105 | (while (< (point) end) | |
106 | (cond ((looking-at "Branch: \\(.*\\)") | |
107 | (put-text-property (match-beginning 1) (match-end 1) 'face 'bold)) | |
108 | ((looking-at "\\([>+-]\\) \\([^ ]+\\) *| \\(.*\\)") | |
109 | (let ((state (match-string 1))) | |
110 | (put-text-property | |
111 | (match-beginning 2) (match-end 2) | |
112 | 'face (cond ((string= state ">") 'stgit-top-patch-face) | |
113 | ((string= state "+") 'stgit-applied-patch-face) | |
114 | ((string= state "-") 'stgit-unapplied-patch-face))) | |
115 | (put-text-property (match-beginning 3) (match-end 3) | |
116 | 'face 'stgit-description-face)))) | |
117 | (forward-line 1)))) | |
118 | ||
56d81fe5 DK |
119 | (defvar stgit-mode-hook nil |
120 | "Run after `stgit-mode' is setup.") | |
121 | ||
122 | (defvar stgit-mode-map nil | |
123 | "Keymap for StGit major mode.") | |
124 | ||
125 | (unless stgit-mode-map | |
126 | (setq stgit-mode-map (make-keymap)) | |
127 | (suppress-keymap stgit-mode-map) | |
0663524d KH |
128 | (define-key stgit-mode-map "?" 'stgit-help) |
129 | (define-key stgit-mode-map "h" 'stgit-help) | |
9a3bb1f1 DK |
130 | (define-key stgit-mode-map "p" 'previous-line) |
131 | (define-key stgit-mode-map "n" 'next-line) | |
56d81fe5 DK |
132 | (define-key stgit-mode-map "g" 'stgit-refresh) |
133 | (define-key stgit-mode-map "r" 'stgit-rename) | |
0bca35c8 | 134 | (define-key stgit-mode-map "e" 'stgit-edit) |
26201d96 | 135 | (define-key stgit-mode-map "\C-r" 'stgit-repair) |
c4aad9a7 DK |
136 | (define-key stgit-mode-map "C" 'stgit-commit) |
137 | (define-key stgit-mode-map "U" 'stgit-uncommit) | |
56d81fe5 | 138 | (define-key stgit-mode-map ">" 'stgit-push-next) |
82a12e6e | 139 | (define-key stgit-mode-map "<" 'stgit-pop-next) |
f9182fca | 140 | (define-key stgit-mode-map "P" 'stgit-push-or-pop) |
c7adf5ef | 141 | (define-key stgit-mode-map "G" 'stgit-goto) |
56d81fe5 DK |
142 | (define-key stgit-mode-map "=" 'stgit-show)) |
143 | ||
144 | (defun stgit-mode () | |
145 | "Major mode for interacting with StGit. | |
146 | Commands: | |
147 | \\{stgit-mode-map}" | |
148 | (kill-all-local-variables) | |
149 | (buffer-disable-undo) | |
150 | (setq mode-name "StGit" | |
151 | major-mode 'stgit-mode | |
152 | goal-column 2) | |
153 | (use-local-map stgit-mode-map) | |
154 | (set (make-local-variable 'list-buffers-directory) default-directory) | |
2870f8b8 | 155 | (set-variable 'truncate-lines 't) |
56d81fe5 DK |
156 | (run-hooks 'stgit-mode-hook)) |
157 | ||
158 | (defun stgit-patch-at-point () | |
159 | "Return the patch name on the current line" | |
160 | (save-excursion | |
161 | (beginning-of-line) | |
2870f8b8 | 162 | (if (looking-at "[>+-] \\([^ ]*\\)") |
07f464e0 | 163 | (match-string-no-properties 1) |
56d81fe5 DK |
164 | nil))) |
165 | ||
166 | (defun stgit-goto-patch (patch) | |
167 | "Move point to the line containing PATCH" | |
168 | (let ((p (point))) | |
169 | (goto-char (point-min)) | |
2870f8b8 | 170 | (if (re-search-forward (concat "[>+-] " (regexp-quote patch) " ") nil t) |
56d81fe5 DK |
171 | (progn (move-to-column goal-column) |
172 | t) | |
173 | (goto-char p) | |
174 | nil))) | |
175 | ||
176 | (defun stgit-rename (name) | |
177 | "Rename the patch under point" | |
178 | (interactive (list (read-string "Patch name: " (stgit-patch-at-point)))) | |
179 | (let ((old-name (stgit-patch-at-point))) | |
180 | (unless old-name | |
181 | (error "No patch on this line")) | |
182 | (stgit-capture-output nil | |
183 | (stgit-run "rename" old-name name)) | |
184 | (stgit-refresh) | |
185 | (stgit-goto-patch name))) | |
186 | ||
26201d96 DK |
187 | (defun stgit-repair () |
188 | "Run stg repair" | |
189 | (interactive) | |
190 | (stgit-capture-output nil | |
191 | (stgit-run "repair")) | |
192 | (stgit-refresh)) | |
193 | ||
c4aad9a7 DK |
194 | (defun stgit-commit () |
195 | "Run stg commit." | |
196 | (interactive) | |
197 | (stgit-capture-output nil (stgit-run "commit")) | |
198 | (stgit-refresh)) | |
199 | ||
200 | (defun stgit-uncommit (arg) | |
201 | "Run stg uncommit. Numeric arg determines number of patches to uncommit." | |
202 | (interactive "p") | |
203 | (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg))) | |
204 | (stgit-refresh)) | |
205 | ||
56d81fe5 | 206 | (defun stgit-push-next () |
82a12e6e | 207 | "Push the first unapplied patch" |
56d81fe5 | 208 | (interactive) |
82a12e6e KH |
209 | (stgit-capture-output nil (stgit-run "push")) |
210 | (stgit-refresh)) | |
56d81fe5 | 211 | |
82a12e6e KH |
212 | (defun stgit-pop-next () |
213 | "Pop the topmost applied patch" | |
56d81fe5 | 214 | (interactive) |
82a12e6e KH |
215 | (stgit-capture-output nil (stgit-run "pop")) |
216 | (stgit-refresh)) | |
56d81fe5 | 217 | |
f9182fca KH |
218 | (defun stgit-applied-at-point () |
219 | "Is the patch on the current line applied?" | |
220 | (save-excursion | |
221 | (beginning-of-line) | |
222 | (looking-at "[>+]"))) | |
223 | ||
224 | (defun stgit-push-or-pop () | |
225 | "Push or pop the patch on the current line" | |
226 | (interactive) | |
227 | (let ((patch (stgit-patch-at-point)) | |
228 | (applied (stgit-applied-at-point))) | |
229 | (stgit-capture-output nil | |
230 | (stgit-run (if applied "pop" "push") patch)) | |
231 | (stgit-refresh))) | |
232 | ||
c7adf5ef KH |
233 | (defun stgit-goto () |
234 | "Go to the patch on the current line" | |
235 | (interactive) | |
236 | (let ((patch (stgit-patch-at-point))) | |
237 | (stgit-capture-output nil | |
238 | (stgit-run "goto" patch)) | |
239 | (stgit-refresh))) | |
240 | ||
56d81fe5 DK |
241 | (defun stgit-show () |
242 | "Show the patch on the current line" | |
243 | (interactive) | |
244 | (stgit-capture-output "*StGit patch*" | |
245 | (stgit-run "show" (stgit-patch-at-point)) | |
246 | (with-current-buffer standard-output | |
247 | (goto-char (point-min)) | |
248 | (diff-mode)))) | |
0663524d | 249 | |
0bca35c8 DK |
250 | (defun stgit-edit () |
251 | "Edit the patch on the current line" | |
252 | (interactive) | |
253 | (let ((patch (if (stgit-applied-at-point) | |
254 | (stgit-patch-at-point) | |
255 | (error "This patch is not applied"))) | |
256 | (edit-buf (get-buffer-create "*stgit edit*")) | |
257 | (dir default-directory)) | |
258 | (log-edit 'stgit-confirm-edit t nil edit-buf) | |
259 | (set (make-local-variable 'stgit-edit-patch) patch) | |
260 | (setq default-directory dir) | |
261 | (let ((standard-output edit-buf)) | |
262 | (stgit-run "edit" "--save-template=-" patch)))) | |
263 | ||
264 | (defun stgit-confirm-edit () | |
265 | (interactive) | |
266 | (let ((file (make-temp-file "stgit-edit-"))) | |
267 | (write-region (point-min) (point-max) file) | |
268 | (stgit-capture-output nil | |
269 | (stgit-run "edit" "-f" file stgit-edit-patch)) | |
270 | (with-current-buffer log-edit-parent-buffer | |
271 | (stgit-refresh)))) | |
272 | ||
273 | ||
0663524d KH |
274 | (defun stgit-help () |
275 | "Display help for the StGit mode." | |
276 | (interactive) | |
277 | (describe-function 'stgit-mode)) | |
3a59f3db KH |
278 | |
279 | (provide 'stgit) |