Commit | Line | Data |
---|---|---|
08a1ccb0 | 1 | ;;; vc-dispatcher.el -- generic command-dispatcher facility. |
92d1eebf | 2 | |
114f9c96 | 3 | ;; Copyright (C) 2008, 2009, 2010 |
92d1eebf ER |
4 | ;; Free Software Foundation, Inc. |
5 | ||
6 | ;; Author: FSF (see below for full credits) | |
7 | ;; Maintainer: Eric S. Raymond <esr@thyrsus.com> | |
9766adfb | 8 | ;; Keywords: vc tools |
bd78fa1d | 9 | ;; Package: vc |
92d1eebf ER |
10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
eb3fa2cf | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
92d1eebf | 14 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
92d1eebf ER |
17 | |
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
92d1eebf ER |
25 | |
26 | ;;; Credits: | |
27 | ||
28 | ;; Designed and implemented by Eric S. Raymond, originally as part of VC mode. | |
dbf2c858 | 29 | ;; Stefan Monnier and Dan Nicolaescu contributed substantial work on the |
821677fb | 30 | ;; vc-dir front end. |
92d1eebf ER |
31 | |
32 | ;;; Commentary: | |
33 | ||
34 | ;; Goals: | |
35 | ;; | |
36 | ;; There is a class of front-ending problems that Emacs might be used | |
37 | ;; to address that involves selecting sets of files, or possibly | |
38 | ;; directories, and passing the selection set to slave commands. The | |
39 | ;; prototypical example, from which this code is derived, is talking | |
b1d87c10 | 40 | ;; to version-control systems. |
92d1eebf ER |
41 | ;; |
42 | ;; vc-dispatcher.el is written to decouple the UI issues in such front | |
b1d87c10 | 43 | ;; ends from their application-specific logic. It also provides a |
92d1eebf ER |
44 | ;; service layer for running the slave commands either synchronously |
45 | ;; or asynchronously and managing the message/error logs from the | |
46 | ;; command runs. | |
47 | ;; | |
48 | ;; Similar UI problems can be expected to come up in applications | |
49 | ;; areas other than VCSes; IDEs and document search are two obvious ones. | |
50 | ;; This mode is intended to ensure that the Emacs interfaces for all such | |
51 | ;; beasts are consistent and carefully designed. But even if nothing | |
52 | ;; but VC ever uses it, getting the layer separation right will be | |
53 | ;; a valuable thing. | |
54 | ||
55 | ;; Dispatcher's universe: | |
56 | ;; | |
57 | ;; The universe consists of the file tree rooted at the current | |
b1d87c10 GM |
58 | ;; directory. The dispatcher's upper layer deduces some subset |
59 | ;; of the file tree from the state of the currently visited buffer | |
92d1eebf ER |
60 | ;; and returns that subset, presumably to a client mode. |
61 | ;; | |
06ad7cd8 ER |
62 | ;; The user may be looking at either of two different views; a buffer |
63 | ;; visiting a file, or a directory buffer generated by vc-dispatcher. | |
92d1eebf ER |
64 | ;; |
65 | ;; The lower layer of this mode runs commands in subprocesses, either | |
66 | ;; synchronously or asynchronously. Commands may be launched in one | |
67 | ;; of two ways: they may be run immediately, or the calling mode can | |
68 | ;; create a closure associated with a text-entry buffer, to be | |
b1d87c10 | 69 | ;; executed when the user types C-c to ship the buffer contents. In |
92d1eebf ER |
70 | ;; either case the command messages and error (if any) will remain |
71 | ;; available in a status buffer. | |
72 | ||
d99d06ec ER |
73 | ;; Special behavior of dispatcher directory buffers: |
74 | ;; | |
038608c7 | 75 | ;; In dispatcher directory buffers, facilities to perform basic |
d99d06ec ER |
76 | ;; navigation and selection operations are provided by keymap and menu |
77 | ;; entries that dispatcher sets up itself, so they'll be uniform | |
038608c7 | 78 | ;; across all dispatcher-using client modes. Client modes are |
d99d06ec ER |
79 | ;; expected to append to these to provide mode-specific bindings. |
80 | ;; | |
81 | ;; The standard map associates a 'state' slot (that the client mode | |
82 | ;; may set) with each directory entry. The dispatcher knows nothing | |
83 | ;; about the semantics of individual states, but mark and unmark commands | |
b1d87c10 | 84 | ;; treat all entries with the same state as the currently selected one as |
038608c7 | 85 | ;; a unit. |
821677fb | 86 | |
dab68082 | 87 | ;; The interface: |
821677fb ER |
88 | ;; |
89 | ;; The main interface to the lower level is vc-do-command. This launches a | |
b1d87c10 | 90 | ;; command, synchronously or asynchronously, making the output available |
4096c5f2 | 91 | ;; in a command log buffer. Two other functions, (vc-start-logentry) and |
821677fb | 92 | ;; (vc-finish-logentry), allow you to associate a command closure with an |
b1d87c10 | 93 | ;; annotation buffer so that when the user confirms the comment the closure |
821677fb ER |
94 | ;; is run (with the comment as part of its context). |
95 | ;; | |
96 | ;; The interface to the upper level has the two main entry points (vc-dir) | |
97 | ;; and (vc-dispatcher-selection-set) and a couple of convenience functions. | |
98 | ;; (vc-dir) sets up a dispatcher browsing buffer; (vc-dispatcher-selection-set) | |
99 | ;; returns a selection set of files, either the marked files in a browsing | |
100 | ;; buffer or the singleton set consisting of the file visited by the current | |
b1d87c10 GM |
101 | ;; buffer (when that is appropriate). It also does what is needed to ensure |
102 | ;; that on-disk files and the contents of their visiting Emacs buffers | |
c8d4f1a0 ER |
103 | ;; coincide. |
104 | ;; | |
8c6eab5a | 105 | ;; When the client mode adds a local vc-mode-line-hook to a buffer, it |
c8d4f1a0 ER |
106 | ;; will be called with the buffer file name as argument whenever the |
107 | ;; dispatcher resynchs the buffer. | |
821677fb | 108 | |
d99d06ec ER |
109 | ;; To do: |
110 | ;; | |
373b6519 | 111 | ;; - log buffers need font-locking. |
d99d06ec | 112 | ;; |
83affd96 | 113 | |
1915fb44 | 114 | ;; General customization |
83affd96 ER |
115 | (defcustom vc-logentry-check-hook nil |
116 | "Normal hook run by `vc-finish-logentry'. | |
117 | Use this to impose your own rules on the entry in addition to any the | |
4f61cc3e | 118 | dispatcher client mode imposes itself." |
83affd96 ER |
119 | :type 'hook |
120 | :group 'vc) | |
121 | ||
17f039f3 | 122 | (defcustom vc-delete-logbuf-window t |
400aec1a | 123 | "If non-nil, delete the log buffer and window after each logical action. |
17f039f3 ER |
124 | If nil, bury that buffer instead. |
125 | This is most useful if you have multiple windows on a frame and would like to | |
126 | preserve the setting." | |
127 | :type 'boolean | |
128 | :group 'vc) | |
129 | ||
130 | (defcustom vc-command-messages nil | |
131 | "If non-nil, display run messages from back-end commands." | |
132 | :type 'boolean | |
133 | :group 'vc) | |
134 | ||
821677fb ER |
135 | (defcustom vc-suppress-confirm nil |
136 | "If non-nil, treat user as expert; suppress yes-no prompts on some things." | |
137 | :type 'boolean | |
138 | :group 'vc) | |
139 | ||
83affd96 | 140 | ;; Variables the user doesn't need to know about. |
17f039f3 | 141 | |
83affd96 ER |
142 | (defvar vc-log-operation nil) |
143 | (defvar vc-log-after-operation-hook nil) | |
144 | (defvar vc-log-fileset) | |
83affd96 ER |
145 | |
146 | ;; In a log entry buffer, this is a local variable | |
147 | ;; that points to the buffer for which it was made | |
d536a904 | 148 | ;; (either a file, or a directory buffer). |
83affd96 ER |
149 | (defvar vc-parent-buffer nil) |
150 | (put 'vc-parent-buffer 'permanent-local t) | |
151 | (defvar vc-parent-buffer-name nil) | |
152 | (put 'vc-parent-buffer-name 'permanent-local t) | |
153 | ||
92d1eebf ER |
154 | ;; Common command execution logic |
155 | ||
156 | (defun vc-process-filter (p s) | |
157 | "An alternative output filter for async process P. | |
158 | One difference with the default filter is that this inserts S after markers. | |
159 | Another is that undo information is not kept." | |
160 | (let ((buffer (process-buffer p))) | |
161 | (when (buffer-live-p buffer) | |
162 | (with-current-buffer buffer | |
163 | (save-excursion | |
164 | (let ((buffer-undo-list t) | |
165 | (inhibit-read-only t)) | |
166 | (goto-char (process-mark p)) | |
167 | (insert s) | |
168 | (set-marker (process-mark p) (point)))))))) | |
169 | ||
7265c6e8 ER |
170 | (defun vc-setup-buffer (buf) |
171 | "Prepare BUF for executing a slave command and make it current." | |
92d1eebf ER |
172 | (let ((camefrom (current-buffer)) |
173 | (olddir default-directory)) | |
174 | (set-buffer (get-buffer-create buf)) | |
175 | (kill-all-local-variables) | |
176 | (set (make-local-variable 'vc-parent-buffer) camefrom) | |
177 | (set (make-local-variable 'vc-parent-buffer-name) | |
178 | (concat " from " (buffer-name camefrom))) | |
179 | (setq default-directory olddir) | |
180 | (let ((buffer-undo-list t) | |
181 | (inhibit-read-only t)) | |
182 | (erase-buffer)))) | |
183 | ||
184 | (defvar vc-sentinel-movepoint) ;Dynamically scoped. | |
185 | ||
186 | (defun vc-process-sentinel (p s) | |
187 | (let ((previous (process-get p 'vc-previous-sentinel)) | |
188 | (buf (process-buffer p))) | |
189 | ;; Impatient users sometime kill "slow" buffers; check liveness | |
190 | ;; to avoid "error in process sentinel: Selecting deleted buffer". | |
191 | (when (buffer-live-p buf) | |
192 | (when previous (funcall previous p s)) | |
193 | (with-current-buffer buf | |
194 | (setq mode-line-process | |
195 | (let ((status (process-status p))) | |
196 | ;; Leave mode-line uncluttered, normally. | |
197 | (unless (eq 'exit status) | |
198 | (format " (%s)" status)))) | |
199 | (let (vc-sentinel-movepoint) | |
200 | ;; Normally, we want async code such as sentinels to not move point. | |
201 | (save-excursion | |
202 | (goto-char (process-mark p)) | |
203 | (let ((cmds (process-get p 'vc-sentinel-commands))) | |
204 | (process-put p 'vc-sentinel-commands nil) | |
205 | (dolist (cmd cmds) | |
206 | ;; Each sentinel may move point and the next one should be run | |
207 | ;; at that new point. We could get the same result by having | |
208 | ;; each sentinel read&set process-mark, but since `cmd' needs | |
209 | ;; to work both for async and sync processes, this would be | |
210 | ;; difficult to achieve. | |
211 | (vc-exec-after cmd)))) | |
212 | ;; But sometimes the sentinels really want to move point. | |
213 | (when vc-sentinel-movepoint | |
214 | (let ((win (get-buffer-window (current-buffer) 0))) | |
215 | (if (not win) | |
216 | (goto-char vc-sentinel-movepoint) | |
217 | (with-selected-window win | |
218 | (goto-char vc-sentinel-movepoint)))))))))) | |
219 | ||
220 | (defun vc-set-mode-line-busy-indicator () | |
221 | (setq mode-line-process | |
222 | (concat " " (propertize "[waiting...]" | |
223 | 'face 'mode-line-emphasis | |
224 | 'help-echo | |
d536a904 | 225 | "A command is in progress in this buffer")))) |
92d1eebf ER |
226 | |
227 | (defun vc-exec-after (code) | |
228 | "Eval CODE when the current buffer's process is done. | |
229 | If the current buffer has no process, just evaluate CODE. | |
230 | Else, add CODE to the process' sentinel." | |
231 | (let ((proc (get-buffer-process (current-buffer)))) | |
232 | (cond | |
233 | ;; If there's no background process, just execute the code. | |
234 | ;; We used to explicitly call delete-process on exited processes, | |
235 | ;; but this led to timing problems causing process output to be | |
236 | ;; lost. Terminated processes get deleted automatically | |
237 | ;; anyway. -- cyd | |
238 | ((or (null proc) (eq (process-status proc) 'exit)) | |
239 | ;; Make sure we've read the process's output before going further. | |
240 | (when proc (accept-process-output proc)) | |
241 | (eval code)) | |
242 | ;; If a process is running, add CODE to the sentinel | |
243 | ((eq (process-status proc) 'run) | |
244 | (vc-set-mode-line-busy-indicator) | |
245 | (let ((previous (process-sentinel proc))) | |
246 | (unless (eq previous 'vc-process-sentinel) | |
247 | (process-put proc 'vc-previous-sentinel previous)) | |
248 | (set-process-sentinel proc 'vc-process-sentinel)) | |
249 | (process-put proc 'vc-sentinel-commands | |
250 | ;; We keep the code fragments in the order given | |
251 | ;; so that vc-diff-finish's message shows up in | |
252 | ;; the presence of non-nil vc-command-messages. | |
253 | (append (process-get proc 'vc-sentinel-commands) | |
254 | (list code)))) | |
255 | (t (error "Unexpected process state")))) | |
256 | nil) | |
257 | ||
258 | (defvar vc-post-command-functions nil | |
259 | "Hook run at the end of `vc-do-command'. | |
260 | Each function is called inside the buffer in which the command was run | |
261 | and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.") | |
262 | ||
263 | (defvar w32-quote-process-args) | |
264 | ||
265 | (defun vc-delistify (filelist) | |
266 | "Smash a FILELIST into a file list string suitable for info messages." | |
267 | ;; FIXME what about file names with spaces? | |
268 | (if (not filelist) "." (mapconcat 'identity filelist " "))) | |
269 | ||
270 | ;;;###autoload | |
271 | (defun vc-do-command (buffer okstatus command file-or-list &rest flags) | |
06ad7cd8 | 272 | "Execute a slave command, notifying user and checking for errors. |
2888a97e ER |
273 | Output from COMMAND goes to BUFFER, or the current buffer if |
274 | BUFFER is t. If the destination buffer is not already current, | |
275 | set it up properly and erase it. The command is considered | |
276 | successful if its exit status does not exceed OKSTATUS (if | |
277 | OKSTATUS is nil, that means to ignore error status, if it is | |
278 | `async', that means not to wait for termination of the | |
279 | subprocess; if it is t it means to ignore all execution errors). | |
280 | FILE-OR-LIST is the name of a working file; it may be a list of | |
281 | files or be nil (to execute commands that don't expect a file | |
282 | name or set of files). If an optional list of FLAGS is present, | |
4096c5f2 DN |
283 | that is inserted into the command line before the filename. |
284 | Return the return value of the slave command in the synchronous | |
285 | case, and the process object in the asynchronous case." | |
92d1eebf ER |
286 | ;; FIXME: file-relative-name can return a bogus result because |
287 | ;; it doesn't look at the actual file-system to see if symlinks | |
288 | ;; come into play. | |
289 | (let* ((files | |
290 | (mapcar (lambda (f) (file-relative-name (expand-file-name f))) | |
291 | (if (listp file-or-list) file-or-list (list file-or-list)))) | |
292 | (full-command | |
293 | ;; What we're doing here is preparing a version of the command | |
b1d87c10 | 294 | ;; for display in a debug-progress message. If it's fewer than |
92d1eebf ER |
295 | ;; 20 characters display the entire command (without trailing |
296 | ;; newline). Otherwise display the first 20 followed by an ellipsis. | |
297 | (concat (if (string= (substring command -1) "\n") | |
298 | (substring command 0 -1) | |
299 | command) | |
300 | " " | |
301 | (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags)) | |
302 | " " (vc-delistify files)))) | |
303 | (save-current-buffer | |
304 | (unless (or (eq buffer t) | |
305 | (and (stringp buffer) | |
306 | (string= (buffer-name) buffer)) | |
307 | (eq buffer (current-buffer))) | |
2888a97e | 308 | (vc-setup-buffer buffer)) |
92d1eebf ER |
309 | ;; If there's some previous async process still running, just kill it. |
310 | (let ((oldproc (get-buffer-process (current-buffer)))) | |
311 | ;; If we wanted to wait for oldproc to finish before doing | |
312 | ;; something, we'd have used vc-eval-after. | |
313 | ;; Use `delete-process' rather than `kill-process' because we don't | |
314 | ;; want any of its output to appear from now on. | |
4096c5f2 | 315 | (when oldproc (delete-process oldproc))) |
92d1eebf ER |
316 | (let ((squeezed (remq nil flags)) |
317 | (inhibit-read-only t) | |
318 | (status 0)) | |
319 | (when files | |
320 | (setq squeezed (nconc squeezed files))) | |
b0287b39 | 321 | (let (;; Since some functions need to parse the output |
d4efdaea | 322 | ;; from external commands, set LC_MESSAGES to C. |
b0287b39 | 323 | (process-environment (cons "LC_MESSAGES=C" process-environment)) |
92d1eebf | 324 | (w32-quote-process-args t)) |
92d1eebf ER |
325 | (if (eq okstatus 'async) |
326 | ;; Run asynchronously. | |
327 | (let ((proc | |
328 | (let ((process-connection-type nil)) | |
329 | (apply 'start-file-process command (current-buffer) | |
330 | command squeezed)))) | |
4096c5f2 DN |
331 | (when vc-command-messages |
332 | (message "Running %s in background..." full-command)) | |
92d1eebf ER |
333 | ;;(set-process-sentinel proc (lambda (p msg) (delete-process p))) |
334 | (set-process-filter proc 'vc-process-filter) | |
4096c5f2 DN |
335 | (setq status proc) |
336 | (when vc-command-messages | |
337 | (vc-exec-after | |
338 | `(message "Running %s in background... done" ',full-command)))) | |
b1d87c10 | 339 | ;; Run synchronously |
92d1eebf ER |
340 | (when vc-command-messages |
341 | (message "Running %s in foreground..." full-command)) | |
342 | (let ((buffer-undo-list t)) | |
343 | (setq status (apply 'process-file command nil t nil squeezed))) | |
344 | (when (and (not (eq t okstatus)) | |
345 | (or (not (integerp status)) | |
346 | (and okstatus (< okstatus status)))) | |
347 | (unless (eq ?\s (aref (buffer-name (current-buffer)) 0)) | |
348 | (pop-to-buffer (current-buffer)) | |
349 | (goto-char (point-min)) | |
350 | (shrink-window-if-larger-than-buffer)) | |
351 | (error "Running %s...FAILED (%s)" full-command | |
4096c5f2 DN |
352 | (if (integerp status) (format "status %d" status) status))) |
353 | (when vc-command-messages | |
354 | (message "Running %s...OK = %d" full-command status)))) | |
92d1eebf ER |
355 | (vc-exec-after |
356 | `(run-hook-with-args 'vc-post-command-functions | |
357 | ',command ',file-or-list ',flags)) | |
358 | status)))) | |
359 | ||
17f039f3 | 360 | ;; These functions are used to ensure that the view the user sees is up to date |
b1d87c10 | 361 | ;; even if the dispatcher client mode has messed with file contents (as in, |
17f039f3 ER |
362 | ;; for example, VCS keyword expansion). |
363 | ||
364 | (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win)) | |
365 | ||
366 | (defun vc-position-context (posn) | |
367 | "Save a bit of the text around POSN in the current buffer. | |
368 | Used to help us find the corresponding position again later | |
369 | if markers are destroyed or corrupted." | |
370 | ;; A lot of this was shamelessly lifted from Sebastian Kremer's | |
371 | ;; rcs.el mode. | |
372 | (list posn | |
373 | (buffer-size) | |
374 | (buffer-substring posn | |
375 | (min (point-max) (+ posn 100))))) | |
376 | ||
377 | (defun vc-find-position-by-context (context) | |
378 | "Return the position of CONTEXT in the current buffer. | |
379 | If CONTEXT cannot be found, return nil." | |
380 | (let ((context-string (nth 2 context))) | |
381 | (if (equal "" context-string) | |
382 | (point-max) | |
383 | (save-excursion | |
384 | (let ((diff (- (nth 1 context) (buffer-size)))) | |
385 | (when (< diff 0) (setq diff (- diff))) | |
386 | (goto-char (nth 0 context)) | |
387 | (if (or (search-forward context-string nil t) | |
388 | ;; Can't use search-backward since the match may continue | |
389 | ;; after point. | |
390 | (progn (goto-char (- (point) diff (length context-string))) | |
391 | ;; goto-char doesn't signal an error at | |
392 | ;; beginning of buffer like backward-char would | |
393 | (search-forward context-string nil t))) | |
394 | ;; to beginning of OSTRING | |
395 | (- (point) (length context-string)))))))) | |
396 | ||
397 | (defun vc-context-matches-p (posn context) | |
398 | "Return t if POSN matches CONTEXT, nil otherwise." | |
399 | (let* ((context-string (nth 2 context)) | |
400 | (len (length context-string)) | |
401 | (end (+ posn len))) | |
402 | (if (> end (1+ (buffer-size))) | |
403 | nil | |
404 | (string= context-string (buffer-substring posn end))))) | |
405 | ||
406 | (defun vc-buffer-context () | |
407 | "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE). | |
408 | Used by `vc-restore-buffer-context' to later restore the context." | |
409 | (let ((point-context (vc-position-context (point))) | |
410 | ;; Use mark-marker to avoid confusion in transient-mark-mode. | |
9ba4a350 | 411 | (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer)) |
17f039f3 ER |
412 | (vc-position-context (mark-marker)))) |
413 | ;; Make the right thing happen in transient-mark-mode. | |
9ba4a350 ER |
414 | (mark-active nil)) |
415 | (list point-context mark-context nil))) | |
17f039f3 ER |
416 | |
417 | (defun vc-restore-buffer-context (context) | |
418 | "Restore point/mark, and reparse any affected compilation buffers. | |
419 | CONTEXT is that which `vc-buffer-context' returns." | |
420 | (let ((point-context (nth 0 context)) | |
9ba4a350 | 421 | (mark-context (nth 1 context))) |
17f039f3 ER |
422 | ;; if necessary, restore point and mark |
423 | (if (not (vc-context-matches-p (point) point-context)) | |
424 | (let ((new-point (vc-find-position-by-context point-context))) | |
425 | (when new-point (goto-char new-point)))) | |
426 | (and mark-active | |
427 | mark-context | |
428 | (not (vc-context-matches-p (mark) mark-context)) | |
429 | (let ((new-mark (vc-find-position-by-context mark-context))) | |
430 | (when new-mark (set-mark new-mark)))))) | |
431 | ||
432 | (defun vc-revert-buffer-internal (&optional arg no-confirm) | |
433 | "Revert buffer, keeping point and mark where user expects them. | |
434 | Try to be clever in the face of changes due to expanded version-control | |
435 | key words. This is important for typeahead to work as expected. | |
436 | ARG and NO-CONFIRM are passed on to `revert-buffer'." | |
437 | (interactive "P") | |
438 | (widen) | |
439 | (let ((context (vc-buffer-context))) | |
440 | ;; Use save-excursion here, because it may be able to restore point | |
441 | ;; and mark properly even in cases where vc-restore-buffer-context | |
442 | ;; would fail. However, save-excursion might also get it wrong -- | |
443 | ;; in this case, vc-restore-buffer-context gives it a second try. | |
444 | (save-excursion | |
445 | ;; t means don't call normal-mode; | |
446 | ;; that's to preserve various minor modes. | |
447 | (revert-buffer arg no-confirm t)) | |
448 | (vc-restore-buffer-context context))) | |
449 | ||
8c6eab5a SM |
450 | (defvar vc-mode-line-hook nil) |
451 | (make-variable-buffer-local 'vc-mode-line-hook) | |
452 | (put 'vc-mode-line-hook 'permanent-local t) | |
453 | ||
00fd1147 | 454 | (defun vc-resynch-window (file &optional keep noquery reset-vc-info) |
17f039f3 ER |
455 | "If FILE is in the current buffer, either revert or unvisit it. |
456 | The choice between revert (to see expanded keywords) and unvisit | |
457 | depends on KEEP. NOQUERY if non-nil inhibits confirmation for | |
458 | reverting. NOQUERY should be t *only* if it is known the only | |
459 | difference between the buffer and the file is due to | |
460 | modifications by the dispatcher client code, rather than user | |
461 | editing!" | |
462 | (and (string= buffer-file-name file) | |
463 | (if keep | |
cf885595 | 464 | (when (file-exists-p file) |
00fd1147 DN |
465 | (when reset-vc-info |
466 | (vc-file-clearprops file)) | |
17f039f3 | 467 | (vc-revert-buffer-internal t noquery) |
3c53763c DN |
468 | |
469 | ;; VC operations might toggle the read-only state. In | |
470 | ;; that case we need to adjust the `view-mode' status | |
471 | ;; when `view-read-only' is non-nil. | |
17f039f3 ER |
472 | (and view-read-only |
473 | (if (file-writable-p file) | |
474 | (and view-mode | |
475 | (let ((view-old-buffer-read-only nil)) | |
476 | (view-mode-exit))) | |
477 | (and (not view-mode) | |
478 | (not (eq (get major-mode 'mode-class) 'special)) | |
479 | (view-mode-enter)))) | |
3c53763c | 480 | |
8c6eab5a SM |
481 | ;; FIXME: Why use a hook? Why pass it buffer-file-name? |
482 | (run-hook-with-args 'vc-mode-line-hook buffer-file-name)) | |
17f039f3 ER |
483 | (kill-buffer (current-buffer))))) |
484 | ||
74d0991f | 485 | (declare-function vc-dir-resynch-file "vc-dir" (&optional fname)) |
d923f4ac DN |
486 | (declare-function vc-string-prefix-p "vc" (prefix string)) |
487 | ||
00fd1147 | 488 | (defun vc-resynch-buffers-in-directory (directory &optional keep noquery reset-vc-info) |
d923f4ac DN |
489 | "Resync all buffers that visit files in DIRECTORY." |
490 | (dolist (buffer (buffer-list)) | |
491 | (let ((fname (buffer-file-name buffer))) | |
492 | (when (and fname (vc-string-prefix-p directory fname)) | |
ba83b7b6 | 493 | (with-current-buffer buffer |
00fd1147 | 494 | (vc-resynch-buffer fname keep noquery reset-vc-info)))))) |
74d0991f | 495 | |
00fd1147 | 496 | (defun vc-resynch-buffer (file &optional keep noquery reset-vc-info) |
17f039f3 ER |
497 | "If FILE is currently visited, resynch its buffer." |
498 | (if (string= buffer-file-name file) | |
00fd1147 | 499 | (vc-resynch-window file keep noquery reset-vc-info) |
d923f4ac | 500 | (if (file-directory-p file) |
00fd1147 | 501 | (vc-resynch-buffers-in-directory file keep noquery reset-vc-info) |
d923f4ac DN |
502 | (let ((buffer (get-file-buffer file))) |
503 | (when buffer | |
504 | (with-current-buffer buffer | |
00fd1147 | 505 | (vc-resynch-window file keep noquery reset-vc-info)))))) |
e02d8ae7 DN |
506 | ;; Try to avoid unnecessary work, a *vc-dir* buffer is only present |
507 | ;; if this is true. | |
cf885595 | 508 | (when vc-dir-buffers |
e02d8ae7 | 509 | (vc-dir-resynch-file file))) |
17f039f3 | 510 | |
dab68082 ER |
511 | (defun vc-buffer-sync (&optional not-urgent) |
512 | "Make sure the current buffer and its working file are in sync. | |
513 | NOT-URGENT means it is ok to continue if the user says not to save." | |
514 | (when (buffer-modified-p) | |
515 | (if (or vc-suppress-confirm | |
516 | (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name)))) | |
517 | (save-buffer) | |
518 | (unless not-urgent | |
519 | (error "Aborted"))))) | |
520 | ||
83affd96 ER |
521 | ;; Command closures |
522 | ||
dab68082 ER |
523 | ;; Set up key bindings for use while editing log messages |
524 | ||
09158997 | 525 | (defun vc-log-edit (fileset mode) |
d536a904 | 526 | "Set up `log-edit' for use on FILE." |
dab68082 ER |
527 | (setq default-directory |
528 | (with-current-buffer vc-parent-buffer default-directory)) | |
529 | (log-edit 'vc-finish-logentry | |
530 | nil | |
e97a42c1 SM |
531 | `((log-edit-listfun . (lambda () |
532 | ;; FIXME: Should expand the list | |
533 | ;; for directories. | |
534 | (mapcar 'file-relative-name | |
535 | ',fileset))) | |
09158997 DN |
536 | (log-edit-diff-function . (lambda () (vc-diff nil)))) |
537 | nil | |
538 | mode) | |
dab68082 | 539 | (set (make-local-variable 'vc-log-fileset) fileset) |
dab68082 ER |
540 | (set-buffer-modified-p nil) |
541 | (setq buffer-file-name nil)) | |
542 | ||
e97a42c1 SM |
543 | (defun vc-start-logentry (files comment initial-contents msg logbuf mode action &optional after-hook) |
544 | "Accept a comment for an operation on FILES. | |
400aec1a | 545 | If COMMENT is nil, pop up a LOGBUF buffer, emit MSG, and set the |
83affd96 ER |
546 | action on close to ACTION. If COMMENT is a string and |
547 | INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial | |
548 | contents of the log entry buffer. If COMMENT is a string and | |
549 | INITIAL-CONTENTS is nil, do action immediately as if the user had | |
550 | entered COMMENT. If COMMENT is t, also do action immediately with an | |
551 | empty comment. Remember the file's buffer in `vc-parent-buffer' | |
549f324a GM |
552 | \(current one if no file). Puts the log-entry buffer in major-mode |
553 | MODE, defaulting to `log-edit-mode' if MODE is nil. | |
554 | AFTER-HOOK specifies the local value for `vc-log-after-operation-hook'." | |
83affd96 | 555 | (let ((parent |
4903369a ER |
556 | (if (vc-dispatcher-browsing) |
557 | ;; If we are called from a directory browser, the parent buffer is | |
83affd96 ER |
558 | ;; the current buffer. |
559 | (current-buffer) | |
560 | (if (and files (equal (length files) 1)) | |
561 | (get-file-buffer (car files)) | |
562 | (current-buffer))))) | |
83affd96 | 563 | (if (and comment (not initial-contents)) |
400aec1a ER |
564 | (set-buffer (get-buffer-create logbuf)) |
565 | (pop-to-buffer (get-buffer-create logbuf))) | |
83affd96 ER |
566 | (set (make-local-variable 'vc-parent-buffer) parent) |
567 | (set (make-local-variable 'vc-parent-buffer-name) | |
568 | (concat " from " (buffer-name vc-parent-buffer))) | |
09158997 | 569 | (vc-log-edit files mode) |
83affd96 ER |
570 | (make-local-variable 'vc-log-after-operation-hook) |
571 | (when after-hook | |
572 | (setq vc-log-after-operation-hook after-hook)) | |
573 | (setq vc-log-operation action) | |
83affd96 ER |
574 | (when comment |
575 | (erase-buffer) | |
576 | (when (stringp comment) (insert comment))) | |
577 | (if (or (not comment) initial-contents) | |
578 | (message "%s Type C-c C-c when done" msg) | |
579 | (vc-finish-logentry (eq comment t))))) | |
580 | ||
74d0991f | 581 | (declare-function vc-dir-move-to-goal-column "vc-dir" ()) |
e97a42c1 SM |
582 | ;; vc-finish-logentry is typically called from a log-edit buffer (see |
583 | ;; vc-start-logentry). | |
83affd96 ER |
584 | (defun vc-finish-logentry (&optional nocomment) |
585 | "Complete the operation implied by the current log entry. | |
586 | Use the contents of the current buffer as a check-in or registration | |
587 | comment. If the optional arg NOCOMMENT is non-nil, then don't check | |
588 | the buffer contents as a comment." | |
589 | (interactive) | |
590 | ;; Check and record the comment, if any. | |
591 | (unless nocomment | |
592 | (run-hooks 'vc-logentry-check-hook)) | |
593 | ;; Sync parent buffer in case the user modified it while editing the comment. | |
e02d8ae7 | 594 | ;; But not if it is a vc-dir buffer. |
83affd96 | 595 | (with-current-buffer vc-parent-buffer |
4903369a | 596 | (or (vc-dispatcher-browsing) (vc-buffer-sync))) |
83affd96 ER |
597 | (unless vc-log-operation |
598 | (error "No log operation is pending")) | |
09158997 | 599 | |
83affd96 | 600 | ;; save the parameters held in buffer-local variables |
400aec1a ER |
601 | (let ((logbuf (current-buffer)) |
602 | (log-operation vc-log-operation) | |
e97a42c1 SM |
603 | ;; FIXME: When coming from VC-Dir, we should check that the |
604 | ;; set of selected files is still equal to vc-log-fileset, | |
605 | ;; to avoid surprises. | |
83affd96 | 606 | (log-fileset vc-log-fileset) |
83affd96 | 607 | (log-entry (buffer-string)) |
cb4f9513 | 608 | (after-hook vc-log-after-operation-hook)) |
83affd96 ER |
609 | (pop-to-buffer vc-parent-buffer) |
610 | ;; OK, do it to it | |
611 | (save-excursion | |
612 | (funcall log-operation | |
613 | log-fileset | |
e97a42c1 | 614 | log-entry)) |
83affd96 | 615 | ;; Remove checkin window (after the checkin so that if that fails |
e6d897b3 | 616 | ;; we don't zap the log buffer and the typing therein). |
83affd96 | 617 | ;; -- IMO this should be replaced with quit-window |
400aec1a ER |
618 | (cond ((and logbuf vc-delete-logbuf-window) |
619 | (delete-windows-on logbuf (selected-frame)) | |
620 | ;; Kill buffer and delete any other dedicated windows/frames. | |
621 | (kill-buffer logbuf)) | |
cb4f9513 SM |
622 | (logbuf |
623 | (with-selected-window (or (get-buffer-window logbuf 0) | |
624 | (selected-window)) | |
625 | (with-current-buffer logbuf | |
626 | (bury-buffer))))) | |
83affd96 ER |
627 | ;; Now make sure we see the expanded headers |
628 | (when log-fileset | |
629 | (mapc | |
630 | (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t)) | |
631 | log-fileset)) | |
4903369a | 632 | (when (vc-dispatcher-browsing) |
83affd96 ER |
633 | (vc-dir-move-to-goal-column)) |
634 | (run-hooks after-hook 'vc-finish-logentry-hook))) | |
635 | ||
b236ab0d ER |
636 | (defun vc-dispatcher-browsing () |
637 | "Are we in a directory browser buffer?" | |
8243c746 SM |
638 | (derived-mode-p 'vc-dir-mode)) |
639 | ||
74d0991f DN |
640 | ;; These are unused. |
641 | ;; (defun vc-dispatcher-in-fileset-p (fileset) | |
642 | ;; (let ((member nil)) | |
643 | ;; (while (and (not member) fileset) | |
644 | ;; (let ((elem (pop fileset))) | |
645 | ;; (if (if (file-directory-p elem) | |
646 | ;; (eq t (compare-strings buffer-file-name nil (length elem) | |
647 | ;; elem nil nil)) | |
648 | ;; (eq (current-buffer) (get-file-buffer elem))) | |
649 | ;; (setq member t)))) | |
650 | ;; member)) | |
651 | ||
652 | ;; (defun vc-dispatcher-selection-set (&optional observer) | |
653 | ;; "Deduce a set of files to which to apply an operation. Return a cons | |
654 | ;; cell (SELECTION . FILESET), where SELECTION is what the user chose | |
655 | ;; and FILES is the flist with any directories replaced by the listed files | |
656 | ;; within them. | |
657 | ||
658 | ;; If we're in a directory display, the fileset is the list of marked files (if | |
659 | ;; there is one) else the file on the current line. If not in a directory | |
660 | ;; display, but the current buffer visits a file, the fileset is a singleton | |
661 | ;; containing that file. Otherwise, throw an error." | |
662 | ;; (let ((selection | |
663 | ;; (cond | |
664 | ;; ;; Browsing with vc-dir | |
665 | ;; ((vc-dispatcher-browsing) | |
666 | ;; ;; If no files are marked, temporarily mark current file | |
667 | ;; ;; and choose on that basis (so we get subordinate files) | |
668 | ;; (if (not (vc-dir-marked-files)) | |
669 | ;; (prog2 | |
670 | ;; (vc-dir-mark-file) | |
671 | ;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files)) | |
672 | ;; (vc-dir-unmark-all-files t)) | |
673 | ;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files)))) | |
674 | ;; ;; Visiting an eligible file | |
675 | ;; ((buffer-file-name) | |
676 | ;; (cons (list buffer-file-name) (list buffer-file-name))) | |
677 | ;; ;; No eligible file -- if there's a parent buffer, deduce from there | |
678 | ;; ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer) | |
679 | ;; (with-current-buffer vc-parent-buffer | |
680 | ;; (vc-dispatcher-browsing)))) | |
681 | ;; (with-current-buffer vc-parent-buffer | |
682 | ;; (vc-dispatcher-selection-set))) | |
683 | ;; ;; No good set here, throw error | |
684 | ;; (t (error "No fileset is available here"))))) | |
685 | ;; ;; We assume, in order to avoid unpleasant surprises to the user, | |
686 | ;; ;; that a fileset is not in good shape to be handed to the user if the | |
687 | ;; ;; buffers visiting the fileset don't match the on-disk contents. | |
688 | ;; (unless observer | |
689 | ;; (save-some-buffers | |
690 | ;; nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection))))) | |
691 | ;; selection)) | |
b236ab0d | 692 | |
d9a3d80e DN |
693 | (provide 'vc-dispatcher) |
694 | ||
3eb20677 | 695 | ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246 |
b1ddeeb7 | 696 | ;;; vc-dispatcher.el ends here |