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