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 | ||
0f076fe6 GH |
12 | (require 'git nil t) |
13 | ||
56d81fe5 | 14 | (defun stgit (dir) |
a53347d9 | 15 | "Manage StGit patches for the tree in DIR." |
56d81fe5 | 16 | (interactive "DDirectory: \n") |
52144ce5 | 17 | (switch-to-stgit-buffer (git-get-top-dir dir)) |
1f0bf00f | 18 | (stgit-reload)) |
56d81fe5 | 19 | |
074a4fb0 GH |
20 | (unless (fboundp 'git-get-top-dir) |
21 | (defun git-get-top-dir (dir) | |
22 | "Retrieve the top-level directory of a git tree." | |
23 | (let ((cdup (with-output-to-string | |
24 | (with-current-buffer standard-output | |
25 | (cd dir) | |
26 | (unless (eq 0 (call-process "git" nil t nil | |
27 | "rev-parse" "--show-cdup")) | |
df283a8b | 28 | (error "Cannot find top-level git tree for %s" dir)))))) |
074a4fb0 GH |
29 | (expand-file-name (concat (file-name-as-directory dir) |
30 | (car (split-string cdup "\n"))))))) | |
31 | ||
32 | (defun stgit-refresh-git-status (&optional dir) | |
33 | "If it exists, refresh the `git-status' buffer belonging to | |
34 | directory DIR or `default-directory'" | |
35 | (when (and (fboundp 'git-find-status-buffer) | |
36 | (fboundp 'git-refresh-status)) | |
37 | (let* ((top-dir (git-get-top-dir (or dir default-directory))) | |
38 | (git-status-buffer (and top-dir (git-find-status-buffer top-dir)))) | |
39 | (when git-status-buffer | |
40 | (with-current-buffer git-status-buffer | |
41 | (git-refresh-status)))))) | |
52144ce5 | 42 | |
56d81fe5 | 43 | (defun switch-to-stgit-buffer (dir) |
a53347d9 | 44 | "Switch to a (possibly new) buffer displaying StGit patches for DIR." |
56d81fe5 DK |
45 | (setq dir (file-name-as-directory dir)) |
46 | (let ((buffers (buffer-list))) | |
47 | (while (and buffers | |
48 | (not (with-current-buffer (car buffers) | |
49 | (and (eq major-mode 'stgit-mode) | |
50 | (string= default-directory dir))))) | |
51 | (setq buffers (cdr buffers))) | |
52 | (switch-to-buffer (if buffers | |
53 | (car buffers) | |
54 | (create-stgit-buffer dir))))) | |
55 | ||
56 | (defun create-stgit-buffer (dir) | |
57 | "Create a buffer for showing StGit patches. | |
58 | Argument DIR is the repository path." | |
59 | (let ((buf (create-file-buffer (concat dir "*stgit*"))) | |
60 | (inhibit-read-only t)) | |
61 | (with-current-buffer buf | |
62 | (setq default-directory dir) | |
63 | (stgit-mode) | |
64 | (setq buffer-read-only t)) | |
65 | buf)) | |
66 | ||
67 | (defmacro stgit-capture-output (name &rest body) | |
a53347d9 | 68 | "Capture StGit output and show it in a window at the end." |
34afb86c DK |
69 | `(let ((output-buf (get-buffer-create ,(or name "*StGit output*"))) |
70 | (stgit-dir default-directory) | |
71 | (inhibit-read-only t)) | |
56d81fe5 | 72 | (with-current-buffer output-buf |
34afb86c DK |
73 | (erase-buffer) |
74 | (setq default-directory stgit-dir) | |
75 | (setq buffer-read-only t)) | |
56d81fe5 DK |
76 | (let ((standard-output output-buf)) |
77 | ,@body) | |
34afb86c DK |
78 | (with-current-buffer output-buf |
79 | (set-buffer-modified-p nil) | |
80 | (setq buffer-read-only t) | |
81 | (if (< (point-min) (point-max)) | |
82 | (display-buffer output-buf t))))) | |
56d81fe5 DK |
83 | (put 'stgit-capture-output 'lisp-indent-function 1) |
84 | ||
d51722b7 GH |
85 | (defun stgit-make-run-args (args) |
86 | "Return a copy of ARGS with its elements converted to strings." | |
87 | (mapcar (lambda (x) | |
88 | ;; don't use (format "%s" ...) to limit type errors | |
89 | (cond ((stringp x) x) | |
90 | ((integerp x) (number-to-string x)) | |
91 | ((symbolp x) (symbol-name x)) | |
92 | (t | |
93 | (error "Bad element in stgit-make-run-args args: %S" x)))) | |
94 | args)) | |
95 | ||
9aecd505 | 96 | (defun stgit-run-silent (&rest args) |
d51722b7 | 97 | (setq args (stgit-make-run-args args)) |
56d81fe5 DK |
98 | (apply 'call-process "stg" nil standard-output nil args)) |
99 | ||
9aecd505 | 100 | (defun stgit-run (&rest args) |
d51722b7 | 101 | (setq args (stgit-make-run-args args)) |
9aecd505 DK |
102 | (let ((msgcmd (mapconcat #'identity args " "))) |
103 | (message "Running stg %s..." msgcmd) | |
104 | (apply 'call-process "stg" nil standard-output nil args) | |
105 | (message "Running stg %s...done" msgcmd))) | |
106 | ||
378a003d | 107 | (defun stgit-run-git (&rest args) |
d51722b7 | 108 | (setq args (stgit-make-run-args args)) |
378a003d GH |
109 | (let ((msgcmd (mapconcat #'identity args " "))) |
110 | (message "Running git %s..." msgcmd) | |
111 | (apply 'call-process "git" nil standard-output nil args) | |
112 | (message "Running git %s...done" msgcmd))) | |
113 | ||
1f60181a | 114 | (defun stgit-run-git-silent (&rest args) |
d51722b7 | 115 | (setq args (stgit-make-run-args args)) |
1f60181a GH |
116 | (apply 'call-process "git" nil standard-output nil args)) |
117 | ||
1f0bf00f | 118 | (defun stgit-reload () |
a53347d9 | 119 | "Update the contents of the StGit buffer." |
56d81fe5 DK |
120 | (interactive) |
121 | (let ((inhibit-read-only t) | |
122 | (curline (line-number-at-pos)) | |
123 | (curpatch (stgit-patch-at-point))) | |
124 | (erase-buffer) | |
125 | (insert "Branch: ") | |
9aecd505 DK |
126 | (stgit-run-silent "branch") |
127 | (stgit-run-silent "series" "--description") | |
6df83d42 | 128 | (stgit-rescan) |
56d81fe5 DK |
129 | (if curpatch |
130 | (stgit-goto-patch curpatch) | |
074a4fb0 GH |
131 | (goto-line curline))) |
132 | (stgit-refresh-git-status)) | |
56d81fe5 | 133 | |
8f40753a GH |
134 | (defgroup stgit nil |
135 | "A user interface for the StGit patch maintenance tool." | |
136 | :group 'tools) | |
137 | ||
07f464e0 DK |
138 | (defface stgit-description-face |
139 | '((((background dark)) (:foreground "tan")) | |
140 | (((background light)) (:foreground "dark red"))) | |
8f40753a GH |
141 | "The face used for StGit descriptions" |
142 | :group 'stgit) | |
07f464e0 DK |
143 | |
144 | (defface stgit-top-patch-face | |
145 | '((((background dark)) (:weight bold :foreground "yellow")) | |
146 | (((background light)) (:weight bold :foreground "purple")) | |
147 | (t (:weight bold))) | |
8f40753a GH |
148 | "The face used for the top patch names" |
149 | :group 'stgit) | |
07f464e0 DK |
150 | |
151 | (defface stgit-applied-patch-face | |
152 | '((((background dark)) (:foreground "light yellow")) | |
153 | (((background light)) (:foreground "purple")) | |
154 | (t ())) | |
8f40753a GH |
155 | "The face used for applied patch names" |
156 | :group 'stgit) | |
07f464e0 DK |
157 | |
158 | (defface stgit-unapplied-patch-face | |
159 | '((((background dark)) (:foreground "gray80")) | |
160 | (((background light)) (:foreground "orchid")) | |
161 | (t ())) | |
8f40753a GH |
162 | "The face used for unapplied patch names" |
163 | :group 'stgit) | |
07f464e0 | 164 | |
1f60181a GH |
165 | (defface stgit-modified-file-face |
166 | '((((class color) (background light)) (:foreground "purple")) | |
167 | (((class color) (background dark)) (:foreground "salmon"))) | |
168 | "StGit mode face used for modified file status" | |
169 | :group 'stgit) | |
170 | ||
171 | (defface stgit-unmerged-file-face | |
172 | '((((class color) (background light)) (:foreground "red" :bold t)) | |
173 | (((class color) (background dark)) (:foreground "red" :bold t))) | |
174 | "StGit mode face used for unmerged file status" | |
175 | :group 'stgit) | |
176 | ||
177 | (defface stgit-unknown-file-face | |
178 | '((((class color) (background light)) (:foreground "goldenrod" :bold t)) | |
179 | (((class color) (background dark)) (:foreground "goldenrod" :bold t))) | |
180 | "StGit mode face used for unknown file status" | |
181 | :group 'stgit) | |
182 | ||
a6d9a852 GH |
183 | (defface stgit-file-permission-face |
184 | '((((class color) (background light)) (:foreground "green" :bold t)) | |
185 | (((class color) (background dark)) (:foreground "green" :bold t))) | |
186 | "StGit mode face used for permission changes." | |
187 | :group 'stgit) | |
188 | ||
1f60181a GH |
189 | (defcustom stgit-expand-find-copies-harder |
190 | nil | |
191 | "Try harder to find copied files when listing patches. | |
192 | ||
193 | When not nil, runs git diff-tree with the --find-copies-harder | |
194 | flag, which reduces performance." | |
195 | :type 'boolean | |
196 | :group 'stgit) | |
197 | ||
198 | (defconst stgit-file-status-code-strings | |
199 | (mapcar (lambda (arg) | |
200 | (cons (car arg) | |
a6d9a852 GH |
201 | (propertize (cadr arg) 'face (car (cddr arg))))) |
202 | '((add "Added" stgit-modified-file-face) | |
203 | (copy "Copied" stgit-modified-file-face) | |
204 | (delete "Deleted" stgit-modified-file-face) | |
205 | (modify "Modified" stgit-modified-file-face) | |
206 | (rename "Renamed" stgit-modified-file-face) | |
207 | (mode-change "Mode change" stgit-modified-file-face) | |
208 | (unmerged "Unmerged" stgit-unmerged-file-face) | |
209 | (unknown "Unknown" stgit-unknown-file-face))) | |
1f60181a GH |
210 | "Alist of code symbols to description strings") |
211 | ||
212 | (defun stgit-file-status-code-as-string (code) | |
213 | "Return stgit status code as string" | |
a6d9a852 GH |
214 | (let ((str (assq (if (consp code) (car code) code) |
215 | stgit-file-status-code-strings))) | |
216 | (when str | |
217 | (format "%-11s " | |
218 | (if (and str (consp code) (/= (cdr code) 100)) | |
219 | (format "%s %s" (cdr str) | |
220 | (propertize (format "%d%%" (cdr code)) | |
221 | 'face 'stgit-description-face)) | |
222 | (cdr str)))))) | |
1f60181a | 223 | |
a6d9a852 | 224 | (defun stgit-file-status-code (str &optional score) |
1f60181a GH |
225 | "Return stgit status code from git status string" |
226 | (let ((code (assoc str '(("A" . add) | |
227 | ("C" . copy) | |
228 | ("D" . delete) | |
229 | ("M" . modify) | |
230 | ("R" . rename) | |
231 | ("T" . mode-change) | |
232 | ("U" . unmerged) | |
233 | ("X" . unknown))))) | |
a6d9a852 GH |
234 | (setq code (if code (cdr code) 'unknown)) |
235 | (when (stringp score) | |
236 | (if (> (length score) 0) | |
237 | (setq score (string-to-number score)) | |
238 | (setq score nil))) | |
239 | (if score (cons code score) code))) | |
240 | ||
241 | (defconst stgit-file-type-strings | |
242 | '((#o100 . "file") | |
243 | (#o120 . "symlink") | |
244 | (#o160 . "subproject")) | |
245 | "Alist of names of file types") | |
246 | ||
247 | (defun stgit-file-type-string (type) | |
47271f41 GH |
248 | "Return string describing file type TYPE (the high bits of file permission). |
249 | Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'." | |
a6d9a852 GH |
250 | (let ((type-str (assoc type stgit-file-type-strings))) |
251 | (or (and type-str (cdr type-str)) | |
252 | (format "unknown type %o" type)))) | |
253 | ||
254 | (defun stgit-file-type-change-string (old-perm new-perm) | |
47271f41 GH |
255 | "Return string describing file type change from OLD-PERM to NEW-PERM. |
256 | Cf. `stgit-file-type-string'." | |
a6d9a852 GH |
257 | (let ((old-type (lsh old-perm -9)) |
258 | (new-type (lsh new-perm -9))) | |
259 | (cond ((= old-type new-type) "") | |
260 | ((zerop new-type) "") | |
261 | ((zerop old-type) | |
262 | (if (= new-type #o100) | |
263 | "" | |
264 | (format " (%s)" (stgit-file-type-string new-type)))) | |
265 | (t (format " (%s -> %s)" | |
266 | (stgit-file-type-string old-type) | |
267 | (stgit-file-type-string new-type)))))) | |
268 | ||
269 | (defun stgit-file-mode-change-string (old-perm new-perm) | |
47271f41 GH |
270 | "Return string describing file mode change from OLD-PERM to NEW-PERM. |
271 | Cf. `stgit-file-type-change-string'." | |
a6d9a852 GH |
272 | (setq old-perm (logand old-perm #o777) |
273 | new-perm (logand new-perm #o777)) | |
274 | (if (or (= old-perm new-perm) | |
275 | (zerop old-perm) | |
276 | (zerop new-perm)) | |
277 | "" | |
278 | (let* ((modified (logxor old-perm new-perm)) | |
279 | (not-x-modified (logand (logxor old-perm new-perm) #o666))) | |
280 | (cond ((zerop modified) "") | |
281 | ((and (zerop not-x-modified) | |
282 | (or (and (eq #o111 (logand old-perm #o111)) | |
283 | (propertize "-x" 'face 'stgit-file-permission-face)) | |
284 | (and (eq #o111 (logand new-perm #o111)) | |
285 | (propertize "+x" 'face | |
286 | 'stgit-file-permission-face))))) | |
287 | (t (concat (propertize (format "%o" old-perm) | |
288 | 'face 'stgit-file-permission-face) | |
289 | (propertize " -> " | |
290 | 'face 'stgit-description-face) | |
291 | (propertize (format "%o" new-perm) | |
292 | 'face 'stgit-file-permission-face))))))) | |
1f60181a | 293 | |
378a003d | 294 | (defun stgit-expand-patch (patchsym) |
47271f41 GH |
295 | "Expand (show modification of) the patch with name PATCHSYM (a |
296 | symbol) at point. | |
297 | `stgit-expand-find-copies-harder' controls how hard to try to | |
298 | find copied files." | |
378a003d GH |
299 | (save-excursion |
300 | (forward-line) | |
1f60181a GH |
301 | (let* ((start (point)) |
302 | (result (with-output-to-string | |
303 | (stgit-run-git "diff-tree" "-r" "-z" | |
304 | (if stgit-expand-find-copies-harder | |
305 | "--find-copies-harder" | |
306 | "-C") | |
d51722b7 | 307 | (stgit-id patchsym))))) |
1f60181a | 308 | (let (mstart) |
a6d9a852 | 309 | (while (string-match "\0:\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} \\(\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\\|\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\\)" |
1f60181a | 310 | result mstart) |
a6d9a852 GH |
311 | (let ((copy-or-rename (match-string 4 result)) |
312 | (old-perm (read (format "#o%s" (match-string 1 result)))) | |
313 | (new-perm (read (format "#o%s" (match-string 2 result)))) | |
1f60181a | 314 | (line-start (point)) |
a6d9a852 GH |
315 | status |
316 | change | |
1f60181a GH |
317 | properties) |
318 | (insert " ") | |
319 | (if copy-or-rename | |
a6d9a852 GH |
320 | (let ((cr-score (match-string 5 result)) |
321 | (cr-from-file (match-string 6 result)) | |
322 | (cr-to-file (match-string 7 result))) | |
323 | (setq status (stgit-file-status-code copy-or-rename | |
324 | cr-score) | |
325 | properties (list 'stgit-old-file cr-from-file | |
326 | 'stgit-new-file cr-to-file) | |
327 | change (concat | |
328 | cr-from-file | |
329 | (propertize " -> " | |
330 | 'face 'stgit-description-face) | |
331 | cr-to-file))) | |
332 | (setq status (stgit-file-status-code (match-string 8 result)) | |
333 | properties (list 'stgit-file (match-string 9 result)) | |
334 | change (match-string 9 result))) | |
335 | ||
336 | (let ((mode-change (stgit-file-mode-change-string old-perm | |
337 | new-perm))) | |
338 | (insert (format "%-12s" (stgit-file-status-code-as-string | |
339 | status)) | |
340 | mode-change | |
341 | (if (> (length mode-change) 0) " " "") | |
342 | change | |
343 | (propertize (stgit-file-type-change-string old-perm | |
344 | new-perm) | |
345 | 'face 'stgit-description-face) | |
346 | ?\n)) | |
1f60181a GH |
347 | (add-text-properties line-start (point) properties)) |
348 | (setq mstart (match-end 0)))) | |
349 | (when (= start (point)) | |
350 | (insert " <no files>\n")) | |
d51722b7 | 351 | (put-text-property start (point) 'stgit-file-patchsym patchsym)))) |
378a003d | 352 | |
6df83d42 DK |
353 | (defun stgit-rescan () |
354 | "Rescan the status buffer." | |
07f464e0 | 355 | (save-excursion |
3a1cf814 GH |
356 | (let ((marked ()) |
357 | found-any) | |
6df83d42 DK |
358 | (goto-char (point-min)) |
359 | (while (not (eobp)) | |
360 | (cond ((looking-at "Branch: \\(.*\\)") | |
361 | (put-text-property (match-beginning 1) (match-end 1) | |
362 | 'face 'bold)) | |
8ee1e4b4 | 363 | ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)") |
3a1cf814 | 364 | (setq found-any t) |
6df83d42 DK |
365 | (let ((state (match-string 1)) |
366 | (patchsym (intern (match-string 3)))) | |
367 | (put-text-property | |
368 | (match-beginning 3) (match-end 3) 'face | |
369 | (cond ((string= state ">") 'stgit-top-patch-face) | |
370 | ((string= state "+") 'stgit-applied-patch-face) | |
371 | ((string= state "-") 'stgit-unapplied-patch-face))) | |
372 | (put-text-property (match-beginning 4) (match-end 4) | |
373 | 'face 'stgit-description-face) | |
374 | (when (memq patchsym stgit-marked-patches) | |
375 | (replace-match "*" nil nil nil 2) | |
378a003d | 376 | (setq marked (cons patchsym marked))) |
d51722b7 GH |
377 | (put-text-property (match-beginning 0) (match-end 0) |
378 | 'stgit-patchsym patchsym) | |
378a003d GH |
379 | (when (memq patchsym stgit-expanded-patches) |
380 | (stgit-expand-patch patchsym)) | |
381 | )) | |
ad80ce22 DK |
382 | ((or (looking-at "stg series: Branch \".*\" not initialised") |
383 | (looking-at "stg series: .*: branch not initialized")) | |
3a1cf814 | 384 | (setq found-any t) |
1c2426dc DK |
385 | (forward-line 1) |
386 | (insert "Run M-x stgit-init to initialise"))) | |
6df83d42 | 387 | (forward-line 1)) |
3a1cf814 GH |
388 | (setq stgit-marked-patches (nreverse marked)) |
389 | (unless found-any | |
390 | (insert "\n " | |
391 | (propertize "no patches in series" | |
392 | 'face 'stgit-description-face)))))) | |
07f464e0 | 393 | |
378a003d GH |
394 | (defun stgit-select () |
395 | "Expand or collapse the current entry" | |
396 | (interactive) | |
397 | (let ((curpatch (stgit-patch-at-point))) | |
398 | (if (not curpatch) | |
399 | (let ((patched-file (stgit-patched-file-at-point))) | |
400 | (unless patched-file | |
401 | (error "No patch or file on the current line")) | |
402 | (let ((filename (expand-file-name (cdr patched-file)))) | |
403 | (unless (file-exists-p filename) | |
404 | (error "File does not exist")) | |
405 | (find-file filename))) | |
378a003d GH |
406 | (setq stgit-expanded-patches |
407 | (if (memq curpatch stgit-expanded-patches) | |
408 | (delq curpatch stgit-expanded-patches) | |
409 | (cons curpatch stgit-expanded-patches))) | |
410 | (stgit-reload)))) | |
411 | ||
412 | (defun stgit-find-file-other-window () | |
413 | "Open file at point in other window" | |
414 | (interactive) | |
415 | (let ((patched-file (stgit-patched-file-at-point))) | |
416 | (unless patched-file | |
417 | (error "No file on the current line")) | |
418 | (let ((filename (expand-file-name (cdr patched-file)))) | |
419 | (unless (file-exists-p filename) | |
420 | (error "File does not exist")) | |
421 | (find-file-other-window filename)))) | |
422 | ||
83327d53 | 423 | (defun stgit-quit () |
a53347d9 | 424 | "Hide the stgit buffer." |
83327d53 GH |
425 | (interactive) |
426 | (bury-buffer)) | |
427 | ||
0f076fe6 | 428 | (defun stgit-git-status () |
a53347d9 | 429 | "Show status using `git-status'." |
0f076fe6 GH |
430 | (interactive) |
431 | (unless (fboundp 'git-status) | |
df283a8b | 432 | (error "The stgit-git-status command requires git-status")) |
0f076fe6 GH |
433 | (let ((dir default-directory)) |
434 | (save-selected-window | |
435 | (pop-to-buffer nil) | |
436 | (git-status dir)))) | |
437 | ||
378a003d GH |
438 | (defun stgit-next-line (&optional arg try-vscroll) |
439 | "Move cursor vertically down ARG lines" | |
440 | (interactive "p\np") | |
441 | (next-line arg try-vscroll) | |
442 | (when (looking-at " \\S-") | |
443 | (forward-char 2))) | |
444 | ||
445 | (defun stgit-previous-line (&optional arg try-vscroll) | |
446 | "Move cursor vertically up ARG lines" | |
447 | (interactive "p\np") | |
448 | (previous-line arg try-vscroll) | |
449 | (when (looking-at " \\S-") | |
450 | (forward-char 2))) | |
451 | ||
452 | (defun stgit-next-patch (&optional arg) | |
453 | "Move cursor down ARG patches" | |
454 | (interactive "p") | |
455 | (unless arg | |
456 | (setq arg 1)) | |
457 | (if (< arg 0) | |
458 | (stgit-previous-patch (- arg)) | |
459 | (while (not (zerop arg)) | |
460 | (setq arg (1- arg)) | |
461 | (while (progn (stgit-next-line) | |
462 | (not (stgit-patch-at-point))))))) | |
463 | ||
464 | (defun stgit-previous-patch (&optional arg) | |
465 | "Move cursor up ARG patches" | |
466 | (interactive "p") | |
467 | (unless arg | |
468 | (setq arg 1)) | |
469 | (if (< arg 0) | |
470 | (stgit-next-patch (- arg)) | |
471 | (while (not (zerop arg)) | |
472 | (setq arg (1- arg)) | |
473 | (while (progn (stgit-previous-line) | |
474 | (not (stgit-patch-at-point))))))) | |
475 | ||
56d81fe5 DK |
476 | (defvar stgit-mode-hook nil |
477 | "Run after `stgit-mode' is setup.") | |
478 | ||
479 | (defvar stgit-mode-map nil | |
480 | "Keymap for StGit major mode.") | |
481 | ||
482 | (unless stgit-mode-map | |
483 | (setq stgit-mode-map (make-keymap)) | |
484 | (suppress-keymap stgit-mode-map) | |
022a3664 GH |
485 | (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg))) |
486 | '((" " . stgit-mark) | |
3dccdc9b | 487 | ("m" . stgit-mark) |
9b151b27 GH |
488 | ("\d" . stgit-unmark-up) |
489 | ("u" . stgit-unmark-down) | |
022a3664 GH |
490 | ("?" . stgit-help) |
491 | ("h" . stgit-help) | |
378a003d GH |
492 | ("p" . stgit-previous-line) |
493 | ("n" . stgit-next-line) | |
494 | ("\C-p" . stgit-previous-patch) | |
495 | ("\C-n" . stgit-next-patch) | |
496 | ("\M-{" . stgit-previous-patch) | |
497 | ("\M-}" . stgit-next-patch) | |
0f076fe6 | 498 | ("s" . stgit-git-status) |
022a3664 GH |
499 | ("g" . stgit-reload) |
500 | ("r" . stgit-refresh) | |
501 | ("\C-c\C-r" . stgit-rename) | |
502 | ("e" . stgit-edit) | |
503 | ("c" . stgit-coalesce) | |
504 | ("N" . stgit-new) | |
505 | ("R" . stgit-repair) | |
506 | ("C" . stgit-commit) | |
507 | ("U" . stgit-uncommit) | |
378a003d GH |
508 | ("\r" . stgit-select) |
509 | ("o" . stgit-find-file-other-window) | |
022a3664 GH |
510 | (">" . stgit-push-next) |
511 | ("<" . stgit-pop-next) | |
512 | ("P" . stgit-push-or-pop) | |
513 | ("G" . stgit-goto) | |
514 | ("=" . stgit-show) | |
515 | ("D" . stgit-delete) | |
516 | ([(control ?/)] . stgit-undo) | |
83327d53 GH |
517 | ("\C-_" . stgit-undo) |
518 | ("q" . stgit-quit)))) | |
56d81fe5 DK |
519 | |
520 | (defun stgit-mode () | |
521 | "Major mode for interacting with StGit. | |
522 | Commands: | |
523 | \\{stgit-mode-map}" | |
524 | (kill-all-local-variables) | |
525 | (buffer-disable-undo) | |
526 | (setq mode-name "StGit" | |
527 | major-mode 'stgit-mode | |
528 | goal-column 2) | |
529 | (use-local-map stgit-mode-map) | |
530 | (set (make-local-variable 'list-buffers-directory) default-directory) | |
6df83d42 | 531 | (set (make-local-variable 'stgit-marked-patches) nil) |
378a003d | 532 | (set (make-local-variable 'stgit-expanded-patches) nil) |
2870f8b8 | 533 | (set-variable 'truncate-lines 't) |
56d81fe5 DK |
534 | (run-hooks 'stgit-mode-hook)) |
535 | ||
d51722b7 GH |
536 | (defun stgit-add-mark (patchsym) |
537 | "Mark the patch PATCHSYM." | |
538 | (setq stgit-marked-patches (cons patchsym stgit-marked-patches))) | |
6df83d42 | 539 | |
d51722b7 GH |
540 | (defun stgit-remove-mark (patchsym) |
541 | "Unmark the patch PATCHSYM." | |
542 | (setq stgit-marked-patches (delq patchsym stgit-marked-patches))) | |
6df83d42 | 543 | |
e6b1fdae | 544 | (defun stgit-clear-marks () |
47271f41 | 545 | "Unmark all patches." |
e6b1fdae DK |
546 | (setq stgit-marked-patches '())) |
547 | ||
378a003d | 548 | (defun stgit-patch-at-point (&optional cause-error allow-file) |
d51722b7 | 549 | "Return the patch name on the current line as a symbol. |
378a003d GH |
550 | If CAUSE-ERROR is not nil, signal an error if none found. |
551 | If ALLOW-FILE is not nil, also handle when point is on a file of | |
552 | a patch." | |
d51722b7 GH |
553 | (or (get-text-property (point) 'stgit-patchsym) |
554 | (and allow-file | |
555 | (get-text-property (point) 'stgit-file-patchsym)) | |
556 | (when cause-error | |
557 | (error "No patch on this line")))) | |
378a003d | 558 | |
1f60181a GH |
559 | (defun stgit-patched-file-at-point (&optional both-files) |
560 | "Returns a cons of the patchsym and file name at point. For | |
561 | copies and renames, return the new file if the patch is either | |
562 | applied. If BOTH-FILES is non-nil, return a cons of the old and | |
563 | the new file names instead of just one name." | |
d51722b7 | 564 | (let ((patchsym (get-text-property (point) 'stgit-file-patchsym)) |
1f60181a GH |
565 | (file (get-text-property (point) 'stgit-file))) |
566 | (cond ((not patchsym) nil) | |
567 | (file (cons patchsym file)) | |
568 | (both-files | |
569 | (cons patchsym (cons (get-text-property (point) 'stgit-old-file) | |
570 | (get-text-property (point) 'stgit-new-file)))) | |
571 | (t | |
572 | (let ((file-sym (save-excursion | |
573 | (stgit-previous-patch) | |
d51722b7 GH |
574 | (unless (eq (stgit-patch-at-point) |
575 | patchsym) | |
1f60181a GH |
576 | (error "Cannot find the %s patch" patchsym)) |
577 | (beginning-of-line) | |
578 | (if (= (char-after) ?-) | |
579 | 'stgit-old-file | |
580 | 'stgit-new-file)))) | |
581 | (cons patchsym (get-text-property (point) file-sym))))))) | |
56d81fe5 | 582 | |
7755d7f1 | 583 | (defun stgit-patches-marked-or-at-point () |
d51722b7 | 584 | "Return the symbols of the marked patches, or the patch on the current line." |
7755d7f1 | 585 | (if stgit-marked-patches |
d51722b7 | 586 | stgit-marked-patches |
7755d7f1 KH |
587 | (let ((patch (stgit-patch-at-point))) |
588 | (if patch | |
589 | (list patch) | |
590 | '())))) | |
591 | ||
d51722b7 GH |
592 | (defun stgit-goto-patch (patchsym) |
593 | "Move point to the line containing patch PATCHSYM. | |
594 | If that patch cannot be found, return nil." | |
595 | (let ((p (text-property-any (point-min) (point-max) | |
596 | 'stgit-patchsym patchsym))) | |
597 | (when p | |
56d81fe5 | 598 | (goto-char p) |
d51722b7 | 599 | (move-to-column goal-column)))) |
56d81fe5 | 600 | |
1c2426dc | 601 | (defun stgit-init () |
a53347d9 | 602 | "Run stg init." |
1c2426dc DK |
603 | (interactive) |
604 | (stgit-capture-output nil | |
b0424080 | 605 | (stgit-run "init")) |
1f0bf00f | 606 | (stgit-reload)) |
1c2426dc | 607 | |
6df83d42 | 608 | (defun stgit-mark () |
a53347d9 | 609 | "Mark the patch under point." |
6df83d42 | 610 | (interactive) |
018fa1ac | 611 | (let ((patch (stgit-patch-at-point t))) |
6df83d42 | 612 | (stgit-add-mark patch) |
1f0bf00f | 613 | (stgit-reload)) |
378a003d | 614 | (stgit-next-patch)) |
6df83d42 | 615 | |
9b151b27 | 616 | (defun stgit-unmark-up () |
a53347d9 | 617 | "Remove mark from the patch on the previous line." |
6df83d42 | 618 | (interactive) |
378a003d | 619 | (stgit-previous-patch) |
018fa1ac | 620 | (stgit-remove-mark (stgit-patch-at-point t)) |
9b151b27 GH |
621 | (stgit-reload)) |
622 | ||
623 | (defun stgit-unmark-down () | |
a53347d9 | 624 | "Remove mark from the patch on the current line." |
9b151b27 | 625 | (interactive) |
018fa1ac | 626 | (stgit-remove-mark (stgit-patch-at-point t)) |
1288eda2 GH |
627 | (stgit-reload) |
628 | (stgit-next-patch)) | |
6df83d42 | 629 | |
56d81fe5 | 630 | (defun stgit-rename (name) |
018fa1ac | 631 | "Rename the patch under point to NAME." |
d51722b7 GH |
632 | (interactive (list (read-string "Patch name: " |
633 | (symbol-name (stgit-patch-at-point t))))) | |
634 | (let ((old-patchsym (stgit-patch-at-point t))) | |
56d81fe5 | 635 | (stgit-capture-output nil |
d51722b7 GH |
636 | (stgit-run "rename" old-patchsym name)) |
637 | (let ((name-sym (intern name))) | |
638 | (when (memq old-patchsym stgit-expanded-patches) | |
378a003d | 639 | (setq stgit-expanded-patches |
d51722b7 GH |
640 | (cons name-sym (delq old-patchsym stgit-expanded-patches)))) |
641 | (when (memq old-patchsym stgit-marked-patches) | |
378a003d | 642 | (setq stgit-marked-patches |
d51722b7 GH |
643 | (cons name-sym (delq old-patchsym stgit-marked-patches)))) |
644 | (stgit-reload) | |
645 | (stgit-goto-patch name-sym)))) | |
56d81fe5 | 646 | |
26201d96 | 647 | (defun stgit-repair () |
a53347d9 | 648 | "Run stg repair." |
26201d96 DK |
649 | (interactive) |
650 | (stgit-capture-output nil | |
b0424080 | 651 | (stgit-run "repair")) |
1f0bf00f | 652 | (stgit-reload)) |
26201d96 | 653 | |
c4aad9a7 DK |
654 | (defun stgit-commit () |
655 | "Run stg commit." | |
656 | (interactive) | |
657 | (stgit-capture-output nil (stgit-run "commit")) | |
1f0bf00f | 658 | (stgit-reload)) |
c4aad9a7 DK |
659 | |
660 | (defun stgit-uncommit (arg) | |
661 | "Run stg uncommit. Numeric arg determines number of patches to uncommit." | |
662 | (interactive "p") | |
d51722b7 | 663 | (stgit-capture-output nil (stgit-run "uncommit" "-n" arg)) |
1f0bf00f | 664 | (stgit-reload)) |
c4aad9a7 | 665 | |
0b661144 DK |
666 | (defun stgit-push-next (npatches) |
667 | "Push the first unapplied patch. | |
668 | With numeric prefix argument, push that many patches." | |
669 | (interactive "p") | |
d51722b7 | 670 | (stgit-capture-output nil (stgit-run "push" "-n" npatches)) |
074a4fb0 GH |
671 | (stgit-reload) |
672 | (stgit-refresh-git-status)) | |
56d81fe5 | 673 | |
0b661144 DK |
674 | (defun stgit-pop-next (npatches) |
675 | "Pop the topmost applied patch. | |
676 | With numeric prefix argument, pop that many patches." | |
677 | (interactive "p") | |
d51722b7 | 678 | (stgit-capture-output nil (stgit-run "pop" "-n" npatches)) |
074a4fb0 GH |
679 | (stgit-reload) |
680 | (stgit-refresh-git-status)) | |
56d81fe5 | 681 | |
f9182fca KH |
682 | (defun stgit-applied-at-point () |
683 | "Is the patch on the current line applied?" | |
684 | (save-excursion | |
685 | (beginning-of-line) | |
686 | (looking-at "[>+]"))) | |
687 | ||
688 | (defun stgit-push-or-pop () | |
a53347d9 | 689 | "Push or pop the patch on the current line." |
f9182fca | 690 | (interactive) |
d51722b7 | 691 | (let ((patchsym (stgit-patch-at-point t)) |
f9182fca KH |
692 | (applied (stgit-applied-at-point))) |
693 | (stgit-capture-output nil | |
d51722b7 | 694 | (stgit-run (if applied "pop" "push") patchsym)) |
1f0bf00f | 695 | (stgit-reload))) |
f9182fca | 696 | |
c7adf5ef | 697 | (defun stgit-goto () |
a53347d9 | 698 | "Go to the patch on the current line." |
c7adf5ef | 699 | (interactive) |
d51722b7 | 700 | (let ((patchsym (stgit-patch-at-point t))) |
c7adf5ef | 701 | (stgit-capture-output nil |
d51722b7 | 702 | (stgit-run "goto" patchsym)) |
1f0bf00f | 703 | (stgit-reload))) |
c7adf5ef | 704 | |
d51722b7 GH |
705 | (defun stgit-id (patchsym) |
706 | "Return the git commit id for PATCHSYM." | |
378a003d | 707 | (let ((result (with-output-to-string |
d51722b7 | 708 | (stgit-run-silent "id" patchsym)))) |
378a003d | 709 | (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result) |
d51722b7 | 710 | (error "Cannot find commit id for %s" patchsym)) |
378a003d GH |
711 | (match-string 1 result))) |
712 | ||
56d81fe5 | 713 | (defun stgit-show () |
a53347d9 | 714 | "Show the patch on the current line." |
56d81fe5 DK |
715 | (interactive) |
716 | (stgit-capture-output "*StGit patch*" | |
d51722b7 GH |
717 | (let ((patchsym (stgit-patch-at-point))) |
718 | (if (not patchsym) | |
1f60181a | 719 | (let ((patched-file (stgit-patched-file-at-point t))) |
378a003d GH |
720 | (unless patched-file |
721 | (error "No patch or file at point")) | |
d51722b7 | 722 | (let ((id (stgit-id (car patched-file)))) |
5f1b0013 GH |
723 | (if (consp (cdr patched-file)) |
724 | ;; two files (copy or rename) | |
725 | (stgit-run-git "diff" "-C" "-C" (concat id "^") id "--" | |
726 | (cadr patched-file) (cddr patched-file)) | |
727 | ;; just one file | |
728 | (stgit-run-git "diff" (concat id "^") id "--" | |
729 | (cdr patched-file))))) | |
b557489f | 730 | (stgit-run "show" "-O" "--patch-with-stat" patchsym)) |
5f1b0013 GH |
731 | (with-current-buffer standard-output |
732 | (goto-char (point-min)) | |
733 | (diff-mode))))) | |
0663524d | 734 | |
0bca35c8 | 735 | (defun stgit-edit () |
a53347d9 | 736 | "Edit the patch on the current line." |
0bca35c8 | 737 | (interactive) |
d51722b7 | 738 | (let ((patchsym (stgit-patch-at-point t)) |
0780be79 | 739 | (edit-buf (get-buffer-create "*StGit edit*")) |
0bca35c8 DK |
740 | (dir default-directory)) |
741 | (log-edit 'stgit-confirm-edit t nil edit-buf) | |
d51722b7 | 742 | (set (make-local-variable 'stgit-edit-patchsym) patchsym) |
0bca35c8 DK |
743 | (setq default-directory dir) |
744 | (let ((standard-output edit-buf)) | |
d51722b7 | 745 | (stgit-run-silent "edit" "--save-template=-" patchsym)))) |
0bca35c8 DK |
746 | |
747 | (defun stgit-confirm-edit () | |
748 | (interactive) | |
749 | (let ((file (make-temp-file "stgit-edit-"))) | |
750 | (write-region (point-min) (point-max) file) | |
751 | (stgit-capture-output nil | |
d51722b7 | 752 | (stgit-run "edit" "-f" file stgit-edit-patchsym)) |
0bca35c8 | 753 | (with-current-buffer log-edit-parent-buffer |
1f0bf00f | 754 | (stgit-reload)))) |
0bca35c8 | 755 | |
aa04f831 GH |
756 | (defun stgit-new (add-sign) |
757 | "Create a new patch. | |
758 | With a prefix argument, include a \"Signed-off-by:\" line at the | |
759 | end of the patch." | |
760 | (interactive "P") | |
c5d45b92 GH |
761 | (let ((edit-buf (get-buffer-create "*StGit edit*")) |
762 | (dir default-directory)) | |
763 | (log-edit 'stgit-confirm-new t nil edit-buf) | |
aa04f831 GH |
764 | (setq default-directory dir) |
765 | (when add-sign | |
766 | (save-excursion | |
767 | (let ((standard-output (current-buffer))) | |
768 | (stgit-run-silent "new" "--sign" "--save-template=-")))))) | |
64c097a0 DK |
769 | |
770 | (defun stgit-confirm-new () | |
771 | (interactive) | |
27b0f9e4 | 772 | (let ((file (make-temp-file "stgit-edit-"))) |
64c097a0 DK |
773 | (write-region (point-min) (point-max) file) |
774 | (stgit-capture-output nil | |
27b0f9e4 | 775 | (stgit-run "new" "-f" file)) |
64c097a0 | 776 | (with-current-buffer log-edit-parent-buffer |
1f0bf00f | 777 | (stgit-reload)))) |
64c097a0 DK |
778 | |
779 | (defun stgit-create-patch-name (description) | |
780 | "Create a patch name from a long description" | |
781 | (let ((patch "")) | |
782 | (while (> (length description) 0) | |
783 | (cond ((string-match "\\`[a-zA-Z_-]+" description) | |
8439f657 GH |
784 | (setq patch (downcase (concat patch |
785 | (match-string 0 description)))) | |
64c097a0 DK |
786 | (setq description (substring description (match-end 0)))) |
787 | ((string-match "\\` +" description) | |
788 | (setq patch (concat patch "-")) | |
789 | (setq description (substring description (match-end 0)))) | |
790 | ((string-match "\\`[^a-zA-Z_-]+" description) | |
791 | (setq description (substring description (match-end 0)))))) | |
792 | (cond ((= (length patch) 0) | |
793 | "patch") | |
794 | ((> (length patch) 20) | |
795 | (substring patch 0 20)) | |
796 | (t patch)))) | |
0bca35c8 | 797 | |
d51722b7 GH |
798 | (defun stgit-delete (patchsyms) |
799 | "Delete the patches in PATCHSYMS. | |
800 | Interactively, delete the marked patches, or the patch at point." | |
7755d7f1 | 801 | (interactive (list (stgit-patches-marked-or-at-point))) |
d51722b7 GH |
802 | (let ((npatches (length patchsyms))) |
803 | (if (zerop npatches) | |
804 | (error "No patches to delete") | |
805 | (when (yes-or-no-p (format "Really delete %d patch%s? " | |
806 | npatches | |
807 | (if (= 1 npatches) "" "es"))) | |
808 | (stgit-capture-output nil | |
809 | (apply 'stgit-run "delete" patchsyms)) | |
810 | (stgit-reload))))) | |
811 | ||
812 | (defun stgit-coalesce (patchsyms) | |
813 | "Coalesce the patches in PATCHSYMS. | |
814 | Interactively, coalesce the marked patches." | |
815 | (interactive (list stgit-marked-patches)) | |
816 | (when (< (length patchsyms) 2) | |
817 | (error "Need at least two patches to coalesce")) | |
0780be79 | 818 | (let ((edit-buf (get-buffer-create "*StGit edit*")) |
ea0def18 DK |
819 | (dir default-directory)) |
820 | (log-edit 'stgit-confirm-coalesce t nil edit-buf) | |
d51722b7 | 821 | (set (make-local-variable 'stgit-patchsyms) patchsyms) |
ea0def18 DK |
822 | (setq default-directory dir) |
823 | (let ((standard-output edit-buf)) | |
d51722b7 | 824 | (apply 'stgit-run-silent "coalesce" "--save-template=-" patchsyms)))) |
ea0def18 DK |
825 | |
826 | (defun stgit-confirm-coalesce () | |
827 | (interactive) | |
828 | (let ((file (make-temp-file "stgit-edit-"))) | |
829 | (write-region (point-min) (point-max) file) | |
830 | (stgit-capture-output nil | |
d51722b7 | 831 | (apply 'stgit-run "coalesce" "-f" file stgit-patchsyms)) |
ea0def18 | 832 | (with-current-buffer log-edit-parent-buffer |
e6b1fdae DK |
833 | (stgit-clear-marks) |
834 | ;; Go to first marked patch and stay there | |
835 | (goto-char (point-min)) | |
836 | (re-search-forward (concat "^[>+-]\\*") nil t) | |
837 | (move-to-column goal-column) | |
838 | (let ((pos (point))) | |
1f0bf00f | 839 | (stgit-reload) |
e6b1fdae | 840 | (goto-char pos))))) |
ea0def18 | 841 | |
0663524d KH |
842 | (defun stgit-help () |
843 | "Display help for the StGit mode." | |
844 | (interactive) | |
845 | (describe-function 'stgit-mode)) | |
3a59f3db | 846 | |
83e51dbf DK |
847 | (defun stgit-undo (&optional arg) |
848 | "Run stg undo. | |
849 | With prefix argument, run it with the --hard flag." | |
850 | (interactive "P") | |
851 | (stgit-capture-output nil | |
852 | (if arg | |
853 | (stgit-run "undo" "--hard") | |
854 | (stgit-run "undo"))) | |
1f0bf00f | 855 | (stgit-reload)) |
83e51dbf | 856 | |
4d73c4d8 DK |
857 | (defun stgit-refresh (&optional arg) |
858 | "Run stg refresh. | |
a53347d9 | 859 | With prefix argument, refresh the marked patch or the patch under point." |
4d73c4d8 DK |
860 | (interactive "P") |
861 | (let ((patchargs (if arg | |
b0424080 GH |
862 | (let ((patches (stgit-patches-marked-or-at-point))) |
863 | (cond ((null patches) | |
df283a8b | 864 | (error "No patch to update")) |
b0424080 | 865 | ((> (length patches) 1) |
df283a8b | 866 | (error "Too many patches selected")) |
b0424080 GH |
867 | (t |
868 | (cons "-p" patches)))) | |
869 | nil))) | |
4d73c4d8 | 870 | (stgit-capture-output nil |
074a4fb0 GH |
871 | (apply 'stgit-run "refresh" patchargs)) |
872 | (stgit-refresh-git-status)) | |
4d73c4d8 DK |
873 | (stgit-reload)) |
874 | ||
3a59f3db | 875 | (provide 'stgit) |