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") |
56d81fe5 DK |
73 | (if curpatch |
74 | (stgit-goto-patch curpatch) | |
75 | (goto-line curline)))) | |
76 | ||
77 | (defvar stgit-mode-hook nil | |
78 | "Run after `stgit-mode' is setup.") | |
79 | ||
80 | (defvar stgit-mode-map nil | |
81 | "Keymap for StGit major mode.") | |
82 | ||
83 | (unless stgit-mode-map | |
84 | (setq stgit-mode-map (make-keymap)) | |
85 | (suppress-keymap stgit-mode-map) | |
0663524d KH |
86 | (define-key stgit-mode-map "?" 'stgit-help) |
87 | (define-key stgit-mode-map "h" 'stgit-help) | |
9a3bb1f1 DK |
88 | (define-key stgit-mode-map "p" 'previous-line) |
89 | (define-key stgit-mode-map "n" 'next-line) | |
56d81fe5 DK |
90 | (define-key stgit-mode-map "g" 'stgit-refresh) |
91 | (define-key stgit-mode-map "r" 'stgit-rename) | |
26201d96 | 92 | (define-key stgit-mode-map "\C-r" 'stgit-repair) |
c4aad9a7 DK |
93 | (define-key stgit-mode-map "C" 'stgit-commit) |
94 | (define-key stgit-mode-map "U" 'stgit-uncommit) | |
56d81fe5 | 95 | (define-key stgit-mode-map ">" 'stgit-push-next) |
82a12e6e | 96 | (define-key stgit-mode-map "<" 'stgit-pop-next) |
f9182fca | 97 | (define-key stgit-mode-map "P" 'stgit-push-or-pop) |
c7adf5ef | 98 | (define-key stgit-mode-map "G" 'stgit-goto) |
56d81fe5 DK |
99 | (define-key stgit-mode-map "=" 'stgit-show)) |
100 | ||
101 | (defun stgit-mode () | |
102 | "Major mode for interacting with StGit. | |
103 | Commands: | |
104 | \\{stgit-mode-map}" | |
105 | (kill-all-local-variables) | |
106 | (buffer-disable-undo) | |
107 | (setq mode-name "StGit" | |
108 | major-mode 'stgit-mode | |
109 | goal-column 2) | |
110 | (use-local-map stgit-mode-map) | |
111 | (set (make-local-variable 'list-buffers-directory) default-directory) | |
2870f8b8 | 112 | (set-variable 'truncate-lines 't) |
56d81fe5 DK |
113 | (run-hooks 'stgit-mode-hook)) |
114 | ||
115 | (defun stgit-patch-at-point () | |
116 | "Return the patch name on the current line" | |
117 | (save-excursion | |
118 | (beginning-of-line) | |
2870f8b8 | 119 | (if (looking-at "[>+-] \\([^ ]*\\)") |
56d81fe5 DK |
120 | (match-string 1) |
121 | nil))) | |
122 | ||
123 | (defun stgit-goto-patch (patch) | |
124 | "Move point to the line containing PATCH" | |
125 | (let ((p (point))) | |
126 | (goto-char (point-min)) | |
2870f8b8 | 127 | (if (re-search-forward (concat "[>+-] " (regexp-quote patch) " ") nil t) |
56d81fe5 DK |
128 | (progn (move-to-column goal-column) |
129 | t) | |
130 | (goto-char p) | |
131 | nil))) | |
132 | ||
133 | (defun stgit-rename (name) | |
134 | "Rename the patch under point" | |
135 | (interactive (list (read-string "Patch name: " (stgit-patch-at-point)))) | |
136 | (let ((old-name (stgit-patch-at-point))) | |
137 | (unless old-name | |
138 | (error "No patch on this line")) | |
139 | (stgit-capture-output nil | |
140 | (stgit-run "rename" old-name name)) | |
141 | (stgit-refresh) | |
142 | (stgit-goto-patch name))) | |
143 | ||
26201d96 DK |
144 | (defun stgit-repair () |
145 | "Run stg repair" | |
146 | (interactive) | |
147 | (stgit-capture-output nil | |
148 | (stgit-run "repair")) | |
149 | (stgit-refresh)) | |
150 | ||
c4aad9a7 DK |
151 | (defun stgit-commit () |
152 | "Run stg commit." | |
153 | (interactive) | |
154 | (stgit-capture-output nil (stgit-run "commit")) | |
155 | (stgit-refresh)) | |
156 | ||
157 | (defun stgit-uncommit (arg) | |
158 | "Run stg uncommit. Numeric arg determines number of patches to uncommit." | |
159 | (interactive "p") | |
160 | (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg))) | |
161 | (stgit-refresh)) | |
162 | ||
56d81fe5 | 163 | (defun stgit-push-next () |
82a12e6e | 164 | "Push the first unapplied patch" |
56d81fe5 | 165 | (interactive) |
82a12e6e KH |
166 | (stgit-capture-output nil (stgit-run "push")) |
167 | (stgit-refresh)) | |
56d81fe5 | 168 | |
82a12e6e KH |
169 | (defun stgit-pop-next () |
170 | "Pop the topmost applied patch" | |
56d81fe5 | 171 | (interactive) |
82a12e6e KH |
172 | (stgit-capture-output nil (stgit-run "pop")) |
173 | (stgit-refresh)) | |
56d81fe5 | 174 | |
f9182fca KH |
175 | (defun stgit-applied-at-point () |
176 | "Is the patch on the current line applied?" | |
177 | (save-excursion | |
178 | (beginning-of-line) | |
179 | (looking-at "[>+]"))) | |
180 | ||
181 | (defun stgit-push-or-pop () | |
182 | "Push or pop the patch on the current line" | |
183 | (interactive) | |
184 | (let ((patch (stgit-patch-at-point)) | |
185 | (applied (stgit-applied-at-point))) | |
186 | (stgit-capture-output nil | |
187 | (stgit-run (if applied "pop" "push") patch)) | |
188 | (stgit-refresh))) | |
189 | ||
c7adf5ef KH |
190 | (defun stgit-goto () |
191 | "Go to the patch on the current line" | |
192 | (interactive) | |
193 | (let ((patch (stgit-patch-at-point))) | |
194 | (stgit-capture-output nil | |
195 | (stgit-run "goto" patch)) | |
196 | (stgit-refresh))) | |
197 | ||
56d81fe5 DK |
198 | (defun stgit-show () |
199 | "Show the patch on the current line" | |
200 | (interactive) | |
201 | (stgit-capture-output "*StGit patch*" | |
202 | (stgit-run "show" (stgit-patch-at-point)) | |
203 | (with-current-buffer standard-output | |
204 | (goto-char (point-min)) | |
205 | (diff-mode)))) | |
0663524d KH |
206 | |
207 | (defun stgit-help () | |
208 | "Display help for the StGit mode." | |
209 | (interactive) | |
210 | (describe-function 'stgit-mode)) | |
3a59f3db KH |
211 | |
212 | (provide 'stgit) |