Commit | Line | Data |
---|---|---|
92d1eebf ER |
1 | ;;; vc-dispatcher.el -- generic command-dispatcher facility. |
2 | ||
3 | ;; Copyright (C) 2008 | |
4 | ;; Free Software Foundation, Inc. | |
5 | ||
6 | ;; Author: FSF (see below for full credits) | |
7 | ;; Maintainer: Eric S. Raymond <esr@thyrsus.com> | |
8 | ;; Keywords: tools | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
92d1eebf | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
92d1eebf ER |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
92d1eebf ER |
24 | |
25 | ;;; Credits: | |
26 | ||
27 | ;; Designed and implemented by Eric S. Raymond, originally as part of VC mode. | |
dbf2c858 | 28 | ;; Stefan Monnier and Dan Nicolaescu contributed substantial work on the |
821677fb | 29 | ;; vc-dir front end. |
92d1eebf ER |
30 | |
31 | ;;; Commentary: | |
32 | ||
33 | ;; Goals: | |
34 | ;; | |
35 | ;; There is a class of front-ending problems that Emacs might be used | |
36 | ;; to address that involves selecting sets of files, or possibly | |
37 | ;; directories, and passing the selection set to slave commands. The | |
38 | ;; prototypical example, from which this code is derived, is talking | |
39 | ;; to version-control systems. | |
40 | ;; | |
41 | ;; vc-dispatcher.el is written to decouple the UI issues in such front | |
42 | ;; ends from their application-specific logic. It also provides a | |
43 | ;; service layer for running the slave commands either synchronously | |
44 | ;; or asynchronously and managing the message/error logs from the | |
45 | ;; command runs. | |
46 | ;; | |
47 | ;; Similar UI problems can be expected to come up in applications | |
48 | ;; areas other than VCSes; IDEs and document search are two obvious ones. | |
49 | ;; This mode is intended to ensure that the Emacs interfaces for all such | |
50 | ;; beasts are consistent and carefully designed. But even if nothing | |
51 | ;; but VC ever uses it, getting the layer separation right will be | |
52 | ;; a valuable thing. | |
53 | ||
54 | ;; Dispatcher's universe: | |
55 | ;; | |
56 | ;; The universe consists of the file tree rooted at the current | |
57 | ;; directory. The dispatcher's upper layer deduces some subset | |
58 | ;; of the file tree from the state of the currently visited buffer | |
59 | ;; and returns that subset, presumably to a client mode. | |
60 | ;; | |
61 | ;; The user may be attempting to select one of three contexts: an | |
62 | ;; explicitly selected fileset, the current working directory, or a | |
63 | ;; global (null) context. The user may be looking at either of two | |
64 | ;; different views; a buffer visiting a file, or a directory buffer | |
65 | ;; generated by vc-dispatcher. The main UI problem connected with | |
66 | ;; this mode is that the user may need to be able to select any of | |
67 | ;; these three contexts from either view. | |
68 | ;; | |
69 | ;; The lower layer of this mode runs commands in subprocesses, either | |
70 | ;; synchronously or asynchronously. Commands may be launched in one | |
71 | ;; of two ways: they may be run immediately, or the calling mode can | |
72 | ;; create a closure associated with a text-entry buffer, to be | |
73 | ;; executed when the user types C-c to ship the buffer contents. In | |
74 | ;; either case the command messages and error (if any) will remain | |
75 | ;; available in a status buffer. | |
76 | ||
d99d06ec ER |
77 | ;; Special behavior of dispatcher directory buffers: |
78 | ;; | |
038608c7 | 79 | ;; In dispatcher directory buffers, facilities to perform basic |
d99d06ec ER |
80 | ;; navigation and selection operations are provided by keymap and menu |
81 | ;; entries that dispatcher sets up itself, so they'll be uniform | |
038608c7 | 82 | ;; across all dispatcher-using client modes. Client modes are |
d99d06ec ER |
83 | ;; expected to append to these to provide mode-specific bindings. |
84 | ;; | |
85 | ;; The standard map associates a 'state' slot (that the client mode | |
86 | ;; may set) with each directory entry. The dispatcher knows nothing | |
87 | ;; about the semantics of individual states, but mark and unmark commands | |
038608c7 ER |
88 | ;; treat all entries with the same state as the currently selected one as |
89 | ;; a unit. | |
821677fb ER |
90 | |
91 | ;; The interface | |
92 | ;; | |
93 | ;; The main interface to the lower level is vc-do-command. This launches a | |
94 | ;; comand, synchronously or asynchronously, making the output available | |
95 | ;; in a command log buffer. Two other functions, (vc-start-annotation) and | |
96 | ;; (vc-finish-logentry), allow you to associate a command closure with an | |
97 | ;; abbotation buffer so that when the user confirms the comment the closure | |
98 | ;; is run (with the comment as part of its context). | |
99 | ;; | |
100 | ;; The interface to the upper level has the two main entry points (vc-dir) | |
101 | ;; and (vc-dispatcher-selection-set) and a couple of convenience functions. | |
102 | ;; (vc-dir) sets up a dispatcher browsing buffer; (vc-dispatcher-selection-set) | |
103 | ;; returns a selection set of files, either the marked files in a browsing | |
104 | ;; buffer or the singleton set consisting of the file visited by the current | |
105 | ;; buffer (when that is appropriate). | |
106 | ||
d99d06ec ER |
107 | ;; To do: |
108 | ;; | |
109 | ;; - vc-dir-kill-dir-status-process should not be specific to dir-status, | |
110 | ;; it should work for other async commands as well (pull/push/...). | |
111 | ;; | |
112 | ;; - the *VC-log* buffer needs font-locking. | |
113 | ;; | |
114 | ;; - Set `vc-dir-insert-directories' to t and check what operations | |
115 | ;; and backends do not support directory arguments and fix them. | |
116 | ;; | |
117 | ;; - vc-dir needs mouse bindings. | |
118 | ;; | |
119 | ;; - vc-dir needs more key bindings for VC actions. | |
120 | ;; | |
121 | ;; - vc-dir toolbar needs more icons. | |
122 | ;; | |
123 | ;; - vc-dir-next-line should not print an "end of buffer" message when | |
124 | ;; invoked with the cursor on the last file. | |
125 | ;; | |
126 | ;; - add commands to move to the prev/next directory in vc-dir. | |
127 | ;; | |
128 | ;; - document vc-dir in the manual. | |
129 | ;; | |
130 | ||
92d1eebf ER |
131 | (provide 'vc-dispatcher) |
132 | ||
4f61cc3e ER |
133 | (eval-when-compile |
134 | (require 'cl) | |
135 | (require 'dired) ; for dired-map-over-marks macro | |
136 | (require 'dired-aux)) ; for dired-kill-{line,tree} | |
137 | ||
83affd96 ER |
138 | ;; General customization |
139 | ||
140 | (defcustom vc-logentry-check-hook nil | |
141 | "Normal hook run by `vc-finish-logentry'. | |
142 | Use this to impose your own rules on the entry in addition to any the | |
4f61cc3e | 143 | dispatcher client mode imposes itself." |
83affd96 ER |
144 | :type 'hook |
145 | :group 'vc) | |
146 | ||
17f039f3 ER |
147 | (defcustom vc-delete-logbuf-window t |
148 | "If non-nil, delete the *VC-log* buffer and window after each logical action. | |
149 | If nil, bury that buffer instead. | |
150 | This is most useful if you have multiple windows on a frame and would like to | |
151 | preserve the setting." | |
152 | :type 'boolean | |
153 | :group 'vc) | |
154 | ||
155 | (defcustom vc-command-messages nil | |
156 | "If non-nil, display run messages from back-end commands." | |
157 | :type 'boolean | |
158 | :group 'vc) | |
159 | ||
821677fb ER |
160 | (defcustom vc-suppress-confirm nil |
161 | "If non-nil, treat user as expert; suppress yes-no prompts on some things." | |
162 | :type 'boolean | |
163 | :group 'vc) | |
164 | ||
83affd96 | 165 | ;; Variables the user doesn't need to know about. |
17f039f3 | 166 | |
83affd96 ER |
167 | (defvar vc-log-operation nil) |
168 | (defvar vc-log-after-operation-hook nil) | |
169 | (defvar vc-log-fileset) | |
170 | (defvar vc-log-extra) | |
171 | ||
172 | ;; In a log entry buffer, this is a local variable | |
173 | ;; that points to the buffer for which it was made | |
174 | ;; (either a file, or a VC dired buffer). | |
175 | (defvar vc-parent-buffer nil) | |
176 | (put 'vc-parent-buffer 'permanent-local t) | |
177 | (defvar vc-parent-buffer-name nil) | |
178 | (put 'vc-parent-buffer-name 'permanent-local t) | |
179 | ||
92d1eebf ER |
180 | ;; Common command execution logic |
181 | ||
182 | (defun vc-process-filter (p s) | |
183 | "An alternative output filter for async process P. | |
184 | One difference with the default filter is that this inserts S after markers. | |
185 | Another is that undo information is not kept." | |
186 | (let ((buffer (process-buffer p))) | |
187 | (when (buffer-live-p buffer) | |
188 | (with-current-buffer buffer | |
189 | (save-excursion | |
190 | (let ((buffer-undo-list t) | |
191 | (inhibit-read-only t)) | |
192 | (goto-char (process-mark p)) | |
193 | (insert s) | |
194 | (set-marker (process-mark p) (point)))))))) | |
195 | ||
7265c6e8 ER |
196 | (defun vc-setup-buffer (buf) |
197 | "Prepare BUF for executing a slave command and make it current." | |
92d1eebf ER |
198 | (let ((camefrom (current-buffer)) |
199 | (olddir default-directory)) | |
200 | (set-buffer (get-buffer-create buf)) | |
201 | (kill-all-local-variables) | |
202 | (set (make-local-variable 'vc-parent-buffer) camefrom) | |
203 | (set (make-local-variable 'vc-parent-buffer-name) | |
204 | (concat " from " (buffer-name camefrom))) | |
205 | (setq default-directory olddir) | |
206 | (let ((buffer-undo-list t) | |
207 | (inhibit-read-only t)) | |
208 | (erase-buffer)))) | |
209 | ||
210 | (defvar vc-sentinel-movepoint) ;Dynamically scoped. | |
211 | ||
212 | (defun vc-process-sentinel (p s) | |
213 | (let ((previous (process-get p 'vc-previous-sentinel)) | |
214 | (buf (process-buffer p))) | |
215 | ;; Impatient users sometime kill "slow" buffers; check liveness | |
216 | ;; to avoid "error in process sentinel: Selecting deleted buffer". | |
217 | (when (buffer-live-p buf) | |
218 | (when previous (funcall previous p s)) | |
219 | (with-current-buffer buf | |
220 | (setq mode-line-process | |
221 | (let ((status (process-status p))) | |
222 | ;; Leave mode-line uncluttered, normally. | |
223 | (unless (eq 'exit status) | |
224 | (format " (%s)" status)))) | |
225 | (let (vc-sentinel-movepoint) | |
226 | ;; Normally, we want async code such as sentinels to not move point. | |
227 | (save-excursion | |
228 | (goto-char (process-mark p)) | |
229 | (let ((cmds (process-get p 'vc-sentinel-commands))) | |
230 | (process-put p 'vc-sentinel-commands nil) | |
231 | (dolist (cmd cmds) | |
232 | ;; Each sentinel may move point and the next one should be run | |
233 | ;; at that new point. We could get the same result by having | |
234 | ;; each sentinel read&set process-mark, but since `cmd' needs | |
235 | ;; to work both for async and sync processes, this would be | |
236 | ;; difficult to achieve. | |
237 | (vc-exec-after cmd)))) | |
238 | ;; But sometimes the sentinels really want to move point. | |
239 | (when vc-sentinel-movepoint | |
240 | (let ((win (get-buffer-window (current-buffer) 0))) | |
241 | (if (not win) | |
242 | (goto-char vc-sentinel-movepoint) | |
243 | (with-selected-window win | |
244 | (goto-char vc-sentinel-movepoint)))))))))) | |
245 | ||
246 | (defun vc-set-mode-line-busy-indicator () | |
247 | (setq mode-line-process | |
248 | (concat " " (propertize "[waiting...]" | |
249 | 'face 'mode-line-emphasis | |
250 | 'help-echo | |
251 | "A VC command is in progress in this buffer")))) | |
252 | ||
253 | (defun vc-exec-after (code) | |
254 | "Eval CODE when the current buffer's process is done. | |
255 | If the current buffer has no process, just evaluate CODE. | |
256 | Else, add CODE to the process' sentinel." | |
257 | (let ((proc (get-buffer-process (current-buffer)))) | |
258 | (cond | |
259 | ;; If there's no background process, just execute the code. | |
260 | ;; We used to explicitly call delete-process on exited processes, | |
261 | ;; but this led to timing problems causing process output to be | |
262 | ;; lost. Terminated processes get deleted automatically | |
263 | ;; anyway. -- cyd | |
264 | ((or (null proc) (eq (process-status proc) 'exit)) | |
265 | ;; Make sure we've read the process's output before going further. | |
266 | (when proc (accept-process-output proc)) | |
267 | (eval code)) | |
268 | ;; If a process is running, add CODE to the sentinel | |
269 | ((eq (process-status proc) 'run) | |
270 | (vc-set-mode-line-busy-indicator) | |
271 | (let ((previous (process-sentinel proc))) | |
272 | (unless (eq previous 'vc-process-sentinel) | |
273 | (process-put proc 'vc-previous-sentinel previous)) | |
274 | (set-process-sentinel proc 'vc-process-sentinel)) | |
275 | (process-put proc 'vc-sentinel-commands | |
276 | ;; We keep the code fragments in the order given | |
277 | ;; so that vc-diff-finish's message shows up in | |
278 | ;; the presence of non-nil vc-command-messages. | |
279 | (append (process-get proc 'vc-sentinel-commands) | |
280 | (list code)))) | |
281 | (t (error "Unexpected process state")))) | |
282 | nil) | |
283 | ||
284 | (defvar vc-post-command-functions nil | |
285 | "Hook run at the end of `vc-do-command'. | |
286 | Each function is called inside the buffer in which the command was run | |
287 | and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.") | |
288 | ||
289 | (defvar w32-quote-process-args) | |
290 | ||
291 | (defun vc-delistify (filelist) | |
292 | "Smash a FILELIST into a file list string suitable for info messages." | |
293 | ;; FIXME what about file names with spaces? | |
294 | (if (not filelist) "." (mapconcat 'identity filelist " "))) | |
295 | ||
296 | ;;;###autoload | |
297 | (defun vc-do-command (buffer okstatus command file-or-list &rest flags) | |
298 | "Execute a VC command, notifying user and checking for errors. | |
299 | Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the | |
300 | current buffer if BUFFER is t. If the destination buffer is not | |
301 | already current, set it up properly and erase it. The command is | |
302 | considered successful if its exit status does not exceed OKSTATUS (if | |
303 | OKSTATUS is nil, that means to ignore error status, if it is `async', that | |
304 | means not to wait for termination of the subprocess; if it is t it means to | |
305 | ignore all execution errors). FILE-OR-LIST is the name of a working file; | |
306 | it may be a list of files or be nil (to execute commands that don't expect | |
307 | a file name or set of files). If an optional list of FLAGS is present, | |
308 | that is inserted into the command line before the filename." | |
309 | ;; FIXME: file-relative-name can return a bogus result because | |
310 | ;; it doesn't look at the actual file-system to see if symlinks | |
311 | ;; come into play. | |
312 | (let* ((files | |
313 | (mapcar (lambda (f) (file-relative-name (expand-file-name f))) | |
314 | (if (listp file-or-list) file-or-list (list file-or-list)))) | |
315 | (full-command | |
316 | ;; What we're doing here is preparing a version of the command | |
317 | ;; for display in a debug-progess message. If it's fewer than | |
318 | ;; 20 characters display the entire command (without trailing | |
319 | ;; newline). Otherwise display the first 20 followed by an ellipsis. | |
320 | (concat (if (string= (substring command -1) "\n") | |
321 | (substring command 0 -1) | |
322 | command) | |
323 | " " | |
324 | (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags)) | |
325 | " " (vc-delistify files)))) | |
326 | (save-current-buffer | |
327 | (unless (or (eq buffer t) | |
328 | (and (stringp buffer) | |
329 | (string= (buffer-name) buffer)) | |
330 | (eq buffer (current-buffer))) | |
b1ddeeb7 | 331 | (vc-setup-buffer (or buffer "*vc*"))) |
92d1eebf ER |
332 | ;; If there's some previous async process still running, just kill it. |
333 | (let ((oldproc (get-buffer-process (current-buffer)))) | |
334 | ;; If we wanted to wait for oldproc to finish before doing | |
335 | ;; something, we'd have used vc-eval-after. | |
336 | ;; Use `delete-process' rather than `kill-process' because we don't | |
337 | ;; want any of its output to appear from now on. | |
338 | (if oldproc (delete-process oldproc))) | |
339 | (let ((squeezed (remq nil flags)) | |
340 | (inhibit-read-only t) | |
341 | (status 0)) | |
342 | (when files | |
343 | (setq squeezed (nconc squeezed files))) | |
344 | (let ((exec-path (append vc-path exec-path)) | |
345 | ;; Add vc-path to PATH for the execution of this command. | |
346 | (process-environment | |
347 | (cons (concat "PATH=" (getenv "PATH") | |
348 | path-separator | |
349 | (mapconcat 'identity vc-path path-separator)) | |
350 | process-environment)) | |
351 | (w32-quote-process-args t)) | |
352 | (when (and (eq okstatus 'async) (file-remote-p default-directory)) | |
353 | ;; start-process does not support remote execution | |
354 | (setq okstatus nil)) | |
355 | (if (eq okstatus 'async) | |
356 | ;; Run asynchronously. | |
357 | (let ((proc | |
358 | (let ((process-connection-type nil)) | |
359 | (apply 'start-file-process command (current-buffer) | |
360 | command squeezed)))) | |
361 | (if vc-command-messages | |
362 | (message "Running %s in background..." full-command)) | |
363 | ;;(set-process-sentinel proc (lambda (p msg) (delete-process p))) | |
364 | (set-process-filter proc 'vc-process-filter) | |
365 | (vc-exec-after | |
366 | `(if vc-command-messages | |
367 | (message "Running %s in background... done" ',full-command)))) | |
368 | ;; Run synchrously | |
369 | (when vc-command-messages | |
370 | (message "Running %s in foreground..." full-command)) | |
371 | (let ((buffer-undo-list t)) | |
372 | (setq status (apply 'process-file command nil t nil squeezed))) | |
373 | (when (and (not (eq t okstatus)) | |
374 | (or (not (integerp status)) | |
375 | (and okstatus (< okstatus status)))) | |
376 | (unless (eq ?\s (aref (buffer-name (current-buffer)) 0)) | |
377 | (pop-to-buffer (current-buffer)) | |
378 | (goto-char (point-min)) | |
379 | (shrink-window-if-larger-than-buffer)) | |
380 | (error "Running %s...FAILED (%s)" full-command | |
381 | (if (integerp status) (format "status %d" status) status)))) | |
382 | ;; We're done. But don't emit a status message if running | |
383 | ;; asychronously, it would just mislead. | |
384 | (if (and vc-command-messages (not (eq okstatus 'async))) | |
385 | (message "Running %s...OK = %d" full-command status))) | |
386 | (vc-exec-after | |
387 | `(run-hook-with-args 'vc-post-command-functions | |
388 | ',command ',file-or-list ',flags)) | |
389 | status)))) | |
390 | ||
17f039f3 ER |
391 | ;; These functions are used to ensure that the view the user sees is up to date |
392 | ;; even if the dispatcher client mode has messed with file contents (as in, | |
393 | ;; for example, VCS keyword expansion). | |
394 | ||
395 | (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win)) | |
396 | ||
397 | (defun vc-position-context (posn) | |
398 | "Save a bit of the text around POSN in the current buffer. | |
399 | Used to help us find the corresponding position again later | |
400 | if markers are destroyed or corrupted." | |
401 | ;; A lot of this was shamelessly lifted from Sebastian Kremer's | |
402 | ;; rcs.el mode. | |
403 | (list posn | |
404 | (buffer-size) | |
405 | (buffer-substring posn | |
406 | (min (point-max) (+ posn 100))))) | |
407 | ||
408 | (defun vc-find-position-by-context (context) | |
409 | "Return the position of CONTEXT in the current buffer. | |
410 | If CONTEXT cannot be found, return nil." | |
411 | (let ((context-string (nth 2 context))) | |
412 | (if (equal "" context-string) | |
413 | (point-max) | |
414 | (save-excursion | |
415 | (let ((diff (- (nth 1 context) (buffer-size)))) | |
416 | (when (< diff 0) (setq diff (- diff))) | |
417 | (goto-char (nth 0 context)) | |
418 | (if (or (search-forward context-string nil t) | |
419 | ;; Can't use search-backward since the match may continue | |
420 | ;; after point. | |
421 | (progn (goto-char (- (point) diff (length context-string))) | |
422 | ;; goto-char doesn't signal an error at | |
423 | ;; beginning of buffer like backward-char would | |
424 | (search-forward context-string nil t))) | |
425 | ;; to beginning of OSTRING | |
426 | (- (point) (length context-string)))))))) | |
427 | ||
428 | (defun vc-context-matches-p (posn context) | |
429 | "Return t if POSN matches CONTEXT, nil otherwise." | |
430 | (let* ((context-string (nth 2 context)) | |
431 | (len (length context-string)) | |
432 | (end (+ posn len))) | |
433 | (if (> end (1+ (buffer-size))) | |
434 | nil | |
435 | (string= context-string (buffer-substring posn end))))) | |
436 | ||
437 | (defun vc-buffer-context () | |
438 | "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE). | |
439 | Used by `vc-restore-buffer-context' to later restore the context." | |
440 | (let ((point-context (vc-position-context (point))) | |
441 | ;; Use mark-marker to avoid confusion in transient-mark-mode. | |
442 | (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer)) | |
443 | (vc-position-context (mark-marker)))) | |
444 | ;; Make the right thing happen in transient-mark-mode. | |
445 | (mark-active nil) | |
446 | ;; The new compilation code does not use compilation-error-list any | |
447 | ;; more, so the code below is now ineffective and might as well | |
448 | ;; be disabled. -- Stef | |
449 | ;; ;; We may want to reparse the compilation buffer after revert | |
450 | ;; (reparse (and (boundp 'compilation-error-list) ;compile loaded | |
451 | ;; ;; Construct a list; each elt is nil or a buffer | |
452 | ;; ;; if that buffer is a compilation output buffer | |
453 | ;; ;; that contains markers into the current buffer. | |
454 | ;; (save-current-buffer | |
455 | ;; (mapcar (lambda (buffer) | |
456 | ;; (set-buffer buffer) | |
457 | ;; (let ((errors (or | |
458 | ;; compilation-old-error-list | |
459 | ;; compilation-error-list)) | |
460 | ;; (buffer-error-marked-p nil)) | |
461 | ;; (while (and (consp errors) | |
462 | ;; (not buffer-error-marked-p)) | |
463 | ;; (and (markerp (cdr (car errors))) | |
464 | ;; (eq buffer | |
465 | ;; (marker-buffer | |
466 | ;; (cdr (car errors)))) | |
467 | ;; (setq buffer-error-marked-p t)) | |
468 | ;; (setq errors (cdr errors))) | |
469 | ;; (if buffer-error-marked-p buffer))) | |
470 | ;; (buffer-list))))) | |
471 | (reparse nil)) | |
472 | (list point-context mark-context reparse))) | |
473 | ||
474 | (defun vc-restore-buffer-context (context) | |
475 | "Restore point/mark, and reparse any affected compilation buffers. | |
476 | CONTEXT is that which `vc-buffer-context' returns." | |
477 | (let ((point-context (nth 0 context)) | |
478 | (mark-context (nth 1 context)) | |
479 | ;; (reparse (nth 2 context)) | |
480 | ) | |
481 | ;; The new compilation code does not use compilation-error-list any | |
482 | ;; more, so the code below is now ineffective and might as well | |
483 | ;; be disabled. -- Stef | |
484 | ;; ;; Reparse affected compilation buffers. | |
485 | ;; (while reparse | |
486 | ;; (if (car reparse) | |
487 | ;; (with-current-buffer (car reparse) | |
488 | ;; (let ((compilation-last-buffer (current-buffer)) ;select buffer | |
489 | ;; ;; Record the position in the compilation buffer of | |
490 | ;; ;; the last error next-error went to. | |
491 | ;; (error-pos (marker-position | |
492 | ;; (car (car-safe compilation-error-list))))) | |
493 | ;; ;; Reparse the error messages as far as they were parsed before. | |
494 | ;; (compile-reinitialize-errors '(4) compilation-parsing-end) | |
495 | ;; ;; Move the pointer up to find the error we were at before | |
496 | ;; ;; reparsing. Now next-error should properly go to the next one. | |
497 | ;; (while (and compilation-error-list | |
498 | ;; (/= error-pos (car (car compilation-error-list)))) | |
499 | ;; (setq compilation-error-list (cdr compilation-error-list)))))) | |
500 | ;; (setq reparse (cdr reparse))) | |
501 | ||
502 | ;; if necessary, restore point and mark | |
503 | (if (not (vc-context-matches-p (point) point-context)) | |
504 | (let ((new-point (vc-find-position-by-context point-context))) | |
505 | (when new-point (goto-char new-point)))) | |
506 | (and mark-active | |
507 | mark-context | |
508 | (not (vc-context-matches-p (mark) mark-context)) | |
509 | (let ((new-mark (vc-find-position-by-context mark-context))) | |
510 | (when new-mark (set-mark new-mark)))))) | |
511 | ||
512 | (defun vc-revert-buffer-internal (&optional arg no-confirm) | |
513 | "Revert buffer, keeping point and mark where user expects them. | |
514 | Try to be clever in the face of changes due to expanded version-control | |
515 | key words. This is important for typeahead to work as expected. | |
516 | ARG and NO-CONFIRM are passed on to `revert-buffer'." | |
517 | (interactive "P") | |
518 | (widen) | |
519 | (let ((context (vc-buffer-context))) | |
520 | ;; Use save-excursion here, because it may be able to restore point | |
521 | ;; and mark properly even in cases where vc-restore-buffer-context | |
522 | ;; would fail. However, save-excursion might also get it wrong -- | |
523 | ;; in this case, vc-restore-buffer-context gives it a second try. | |
524 | (save-excursion | |
525 | ;; t means don't call normal-mode; | |
526 | ;; that's to preserve various minor modes. | |
527 | (revert-buffer arg no-confirm t)) | |
528 | (vc-restore-buffer-context context))) | |
529 | ||
530 | (defun vc-resynch-window (file &optional keep noquery) | |
531 | "If FILE is in the current buffer, either revert or unvisit it. | |
532 | The choice between revert (to see expanded keywords) and unvisit | |
533 | depends on KEEP. NOQUERY if non-nil inhibits confirmation for | |
534 | reverting. NOQUERY should be t *only* if it is known the only | |
535 | difference between the buffer and the file is due to | |
536 | modifications by the dispatcher client code, rather than user | |
537 | editing!" | |
538 | (and (string= buffer-file-name file) | |
539 | (if keep | |
540 | (progn | |
541 | (vc-revert-buffer-internal t noquery) | |
542 | ;; TODO: Adjusting view mode might no longer be necessary | |
543 | ;; after RMS change to files.el of 1999-08-08. Investigate | |
544 | ;; this when we install the new VC. | |
545 | (and view-read-only | |
546 | (if (file-writable-p file) | |
547 | (and view-mode | |
548 | (let ((view-old-buffer-read-only nil)) | |
549 | (view-mode-exit))) | |
550 | (and (not view-mode) | |
551 | (not (eq (get major-mode 'mode-class) 'special)) | |
552 | (view-mode-enter)))) | |
553 | ;; FIXME: Call into vc.el | |
554 | (vc-mode-line buffer-file-name)) | |
555 | (kill-buffer (current-buffer))))) | |
556 | ||
557 | (defun vc-resynch-buffer (file &optional keep noquery) | |
558 | "If FILE is currently visited, resynch its buffer." | |
559 | (if (string= buffer-file-name file) | |
560 | (vc-resynch-window file keep noquery) | |
561 | (let ((buffer (get-file-buffer file))) | |
562 | (when buffer | |
563 | (with-current-buffer buffer | |
564 | (vc-resynch-window file keep noquery))))) | |
17f039f3 ER |
565 | (vc-directory-resynch-file file) |
566 | (when (memq 'vc-dir-mark-buffer-changed after-save-hook) | |
567 | (let ((buffer (get-file-buffer file))) | |
17f039f3 ER |
568 | (vc-dir-mark-buffer-changed file)))) |
569 | ||
83affd96 ER |
570 | ;; Command closures |
571 | ||
572 | (defun vc-start-logentry (files extra comment initial-contents msg action &optional after-hook) | |
573 | "Accept a comment for an operation on FILES with extra data EXTRA. | |
574 | If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the | |
575 | action on close to ACTION. If COMMENT is a string and | |
576 | INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial | |
577 | contents of the log entry buffer. If COMMENT is a string and | |
578 | INITIAL-CONTENTS is nil, do action immediately as if the user had | |
579 | entered COMMENT. If COMMENT is t, also do action immediately with an | |
580 | empty comment. Remember the file's buffer in `vc-parent-buffer' | |
581 | \(current one if no file). AFTER-HOOK specifies the local value | |
582 | for `vc-log-after-operation-hook'." | |
583 | (let ((parent | |
584 | (if (or (eq major-mode 'vc-dired-mode) (eq major-mode 'vc-dir-mode)) | |
585 | ;; If we are called from VC dired, the parent buffer is | |
586 | ;; the current buffer. | |
587 | (current-buffer) | |
588 | (if (and files (equal (length files) 1)) | |
589 | (get-file-buffer (car files)) | |
590 | (current-buffer))))) | |
83affd96 ER |
591 | (if (and comment (not initial-contents)) |
592 | (set-buffer (get-buffer-create "*VC-log*")) | |
593 | (pop-to-buffer (get-buffer-create "*VC-log*"))) | |
594 | (set (make-local-variable 'vc-parent-buffer) parent) | |
595 | (set (make-local-variable 'vc-parent-buffer-name) | |
596 | (concat " from " (buffer-name vc-parent-buffer))) | |
83affd96 ER |
597 | (vc-log-edit files) |
598 | (make-local-variable 'vc-log-after-operation-hook) | |
599 | (when after-hook | |
600 | (setq vc-log-after-operation-hook after-hook)) | |
601 | (setq vc-log-operation action) | |
602 | (setq vc-log-extra extra) | |
603 | (when comment | |
604 | (erase-buffer) | |
605 | (when (stringp comment) (insert comment))) | |
606 | (if (or (not comment) initial-contents) | |
607 | (message "%s Type C-c C-c when done" msg) | |
608 | (vc-finish-logentry (eq comment t))))) | |
609 | ||
610 | (defun vc-finish-logentry (&optional nocomment) | |
611 | "Complete the operation implied by the current log entry. | |
612 | Use the contents of the current buffer as a check-in or registration | |
613 | comment. If the optional arg NOCOMMENT is non-nil, then don't check | |
614 | the buffer contents as a comment." | |
615 | (interactive) | |
616 | ;; Check and record the comment, if any. | |
617 | (unless nocomment | |
618 | (run-hooks 'vc-logentry-check-hook)) | |
619 | ;; Sync parent buffer in case the user modified it while editing the comment. | |
620 | ;; But not if it is a vc-dired buffer. | |
621 | (with-current-buffer vc-parent-buffer | |
622 | (or vc-dired-mode (eq major-mode 'vc-dir-mode) (vc-buffer-sync))) | |
623 | (unless vc-log-operation | |
624 | (error "No log operation is pending")) | |
625 | ;; save the parameters held in buffer-local variables | |
626 | (let ((log-operation vc-log-operation) | |
627 | (log-fileset vc-log-fileset) | |
628 | (log-extra vc-log-extra) | |
629 | (log-entry (buffer-string)) | |
630 | (after-hook vc-log-after-operation-hook) | |
631 | (tmp-vc-parent-buffer vc-parent-buffer)) | |
632 | (pop-to-buffer vc-parent-buffer) | |
633 | ;; OK, do it to it | |
634 | (save-excursion | |
635 | (funcall log-operation | |
636 | log-fileset | |
637 | log-extra | |
638 | log-entry)) | |
639 | ;; Remove checkin window (after the checkin so that if that fails | |
640 | ;; we don't zap the *VC-log* buffer and the typing therein). | |
641 | ;; -- IMO this should be replaced with quit-window | |
642 | (let ((logbuf (get-buffer "*VC-log*"))) | |
643 | (cond ((and logbuf vc-delete-logbuf-window) | |
644 | (delete-windows-on logbuf (selected-frame)) | |
645 | ;; Kill buffer and delete any other dedicated windows/frames. | |
646 | (kill-buffer logbuf)) | |
647 | (logbuf (pop-to-buffer "*VC-log*") | |
648 | (bury-buffer) | |
649 | (pop-to-buffer tmp-vc-parent-buffer)))) | |
650 | ;; Now make sure we see the expanded headers | |
651 | (when log-fileset | |
652 | (mapc | |
653 | (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t)) | |
654 | log-fileset)) | |
655 | (when vc-dired-mode | |
656 | (dired-move-to-filename)) | |
657 | (when (eq major-mode 'vc-dir-mode) | |
658 | (vc-dir-move-to-goal-column)) | |
659 | (run-hooks after-hook 'vc-finish-logentry-hook))) | |
660 | ||
783b505b ER |
661 | ;; VC-Dired mode |
662 | ;; FIXME: to be removed when vc-dir support is finished | |
4f61cc3e ER |
663 | |
664 | (defcustom vc-dired-listing-switches "-al" | |
665 | "Switches passed to `ls' for vc-dired. MUST contain the `l' option." | |
666 | :type 'string | |
667 | :group 'vc | |
668 | :version "21.1") | |
669 | ||
670 | (defcustom vc-dired-recurse t | |
671 | "If non-nil, show directory trees recursively in VC Dired." | |
672 | :type 'boolean | |
673 | :group 'vc | |
674 | :version "20.3") | |
675 | ||
676 | (defcustom vc-dired-terse-display t | |
677 | "If non-nil, show only locked or locally modified files in VC Dired." | |
678 | :type 'boolean | |
679 | :group 'vc | |
680 | :version "20.3") | |
681 | ||
682 | (defvar vc-dired-mode nil) | |
683 | (defvar vc-dired-window-configuration) | |
4f61cc3e ER |
684 | (defvar vc-dired-switches) |
685 | (defvar vc-dired-terse-mode) | |
686 | ||
783b505b ER |
687 | (make-variable-buffer-local 'vc-dired-mode) |
688 | ||
4f61cc3e ER |
689 | (defvar vc-dired-mode-map |
690 | (let ((map (make-sparse-keymap)) | |
691 | (vmap (make-sparse-keymap))) | |
692 | (define-key map "\C-xv" vmap) | |
693 | (define-key map "v" vmap) | |
694 | (set-keymap-parent vmap vc-prefix-map) | |
695 | (define-key vmap "t" 'vc-dired-toggle-terse-mode) | |
696 | map)) | |
697 | ||
698 | (define-derived-mode vc-dired-mode dired-mode "Dired under VC" | |
699 | "The major mode used in VC directory buffers. | |
700 | ||
701 | It works like Dired, but lists only files under version control, with | |
702 | the current VC state of each file being indicated in the place of the | |
703 | file's link count, owner, group and size. Subdirectories are also | |
704 | listed, and you may insert them into the buffer as desired, like in | |
705 | Dired. | |
706 | ||
707 | All Dired commands operate normally, with the exception of `v', which | |
708 | is redefined as the version control prefix, so that you can type | |
709 | `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on | |
710 | the file named in the current Dired buffer line. `vv' invokes | |
711 | `vc-next-action' on this file, or on all files currently marked. | |
712 | There is a special command, `*l', to mark all files currently locked." | |
713 | ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20. | |
714 | ;; We do it here because dired might not be loaded yet | |
715 | ;; when vc-dired-mode-map is initialized. | |
716 | (set-keymap-parent vc-dired-mode-map dired-mode-map) | |
717 | (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t) | |
718 | ;; The following is slightly modified from files.el, | |
719 | ;; because file lines look a bit different in vc-dired-mode | |
720 | ;; (the column before the date does not end in a digit). | |
721 | ;; albinus: It should be done in the original declaration. Problem | |
722 | ;; is the optional empty state-info; otherwise ")" would be good | |
723 | ;; enough as delimeter. | |
724 | (set (make-local-variable 'directory-listing-before-filename-regexp) | |
725 | (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)") | |
726 | ;; In some locales, month abbreviations are as short as 2 letters, | |
727 | ;; and they can be followed by ".". | |
728 | (month (concat l l "+\\.?")) | |
729 | (s " ") | |
730 | (yyyy "[0-9][0-9][0-9][0-9]") | |
731 | (dd "[ 0-3][0-9]") | |
732 | (HH:MM "[ 0-2][0-9]:[0-5][0-9]") | |
733 | (seconds "[0-6][0-9]\\([.,][0-9]+\\)?") | |
734 | (zone "[-+][0-2][0-9][0-5][0-9]") | |
735 | (iso-mm-dd "[01][0-9]-[0-3][0-9]") | |
736 | (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?")) | |
737 | (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time | |
738 | "\\|" yyyy "-" iso-mm-dd "\\)")) | |
739 | (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)" | |
740 | s "+" | |
741 | "\\(" HH:MM "\\|" yyyy "\\)")) | |
742 | (western-comma (concat month s "+" dd "," s "+" yyyy)) | |
743 | ;; Japanese MS-Windows ls-lisp has one-digit months, and | |
744 | ;; omits the Kanji characters after month and day-of-month. | |
745 | (mm "[ 0-1]?[0-9]") | |
746 | (japanese | |
747 | (concat mm l "?" s dd l "?" s "+" | |
748 | "\\(" HH:MM "\\|" yyyy l "?" "\\)"))) | |
749 | ;; the .* below ensures that we find the last match on a line | |
750 | (concat ".*" s | |
751 | "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)" | |
752 | s "+"))) | |
753 | (and (boundp 'vc-dired-switches) | |
754 | vc-dired-switches | |
755 | (set (make-local-variable 'dired-actual-switches) | |
756 | vc-dired-switches)) | |
757 | (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display) | |
758 | ;;(let ((backend-name (symbol-name (vc-responsible-backend | |
759 | ;; default-directory)))) | |
760 | ;; (setq mode-name (concat mode-name backend-name)) | |
761 | ;; ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent. | |
762 | ;; (let ((vc-dire-menu-map (copy-keymap vc-menu-map))) | |
763 | ;; (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc] | |
764 | ;; (cons backend-name vc-dire-menu-map) 'subdir))) | |
765 | (setq vc-dired-mode t)) | |
766 | ||
767 | (defun vc-dired-toggle-terse-mode () | |
768 | "Toggle terse display in VC Dired." | |
769 | (interactive) | |
770 | (if (not vc-dired-mode) | |
771 | nil | |
772 | (setq vc-dired-terse-mode (not vc-dired-terse-mode)) | |
773 | (if vc-dired-terse-mode | |
774 | (vc-dired-hook) | |
775 | (revert-buffer)))) | |
776 | ||
777 | (defun vc-dired-mark-locked () | |
778 | "Mark all files currently locked." | |
779 | (interactive) | |
780 | (dired-mark-if (let ((f (dired-get-filename nil t))) | |
781 | (and f | |
782 | (not (file-directory-p f)) | |
783 | (not (vc-up-to-date-p f)))) | |
784 | "locked file")) | |
785 | ||
786 | (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked) | |
787 | ||
788 | (defun vc-dired-reformat-line (vc-info) | |
789 | "Reformat a directory-listing line. | |
790 | Replace various columns with version control information, VC-INFO. | |
791 | This code, like dired, assumes UNIX -l format." | |
792 | (beginning-of-line) | |
793 | (when (re-search-forward | |
794 | ;; Match link count, owner, group, size. Group may be missing, | |
795 | ;; and only the size is present in OS/2 -l format. | |
796 | "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) " | |
797 | (line-end-position) t) | |
798 | (replace-match (substring (concat vc-info " ") 0 10) | |
799 | t t nil 1))) | |
800 | ||
801 | (defun vc-dired-ignorable-p (filename) | |
802 | "Should FILENAME be ignored in VC-Dired listings?" | |
803 | (catch t | |
804 | ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.) | |
805 | (dolist (ignorable completion-ignored-extensions) | |
806 | (let ((ext (substring filename | |
807 | (- (length filename) | |
808 | (length ignorable))))) | |
809 | (if (string= ignorable ext) (throw t t)))) | |
810 | ;; Ignore Makefiles derived from something else | |
811 | (when (string= (file-name-nondirectory filename) "Makefile") | |
812 | (let* ((dir (file-name-directory filename)) | |
813 | (peers (directory-files (or dir default-directory)))) | |
814 | (if (or (member "Makefile.in" peers) (member "Makefile.am" peers)) | |
815 | (throw t t)))) | |
816 | nil)) | |
817 | ||
818 | (defun vc-dired-purge () | |
819 | "Remove empty subdirs." | |
820 | (goto-char (point-min)) | |
821 | (while (dired-get-subdir) | |
822 | (forward-line 2) | |
823 | (if (dired-get-filename nil t) | |
824 | (if (not (dired-next-subdir 1 t)) | |
825 | (goto-char (point-max))) | |
826 | (forward-line -2) | |
827 | (if (not (string= (dired-current-directory) default-directory)) | |
828 | (dired-do-kill-lines t "") | |
829 | ;; We cannot remove the top level directory. | |
830 | ;; Just make it look a little nicer. | |
831 | (forward-line 1) | |
832 | (or (eobp) (kill-line)) | |
833 | (if (not (dired-next-subdir 1 t)) | |
834 | (goto-char (point-max)))))) | |
835 | (goto-char (point-min))) | |
836 | ||
837 | (defun vc-dired-buffers-for-dir (dir) | |
838 | "Return a list of all vc-dired buffers that currently display DIR." | |
839 | (let (result) | |
840 | ;; Check whether dired is loaded. | |
841 | (when (fboundp 'dired-buffers-for-dir) | |
842 | (dolist (buffer (dired-buffers-for-dir dir)) | |
843 | (with-current-buffer buffer | |
844 | (when vc-dired-mode | |
845 | (push buffer result))))) | |
846 | (nreverse result))) | |
847 | ||
848 | (defun vc-directory-resynch-file (file) | |
849 | "Update the entries for FILE in any VC Dired buffers that list it." | |
850 | ;;FIXME This needs to be implemented so it works for vc-dir | |
851 | (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file)))) | |
852 | (when buffers | |
853 | (mapcar (lambda (buffer) | |
854 | (with-current-buffer buffer | |
855 | (when (dired-goto-file file) | |
856 | ;; bind vc-dired-terse-mode to nil so that | |
857 | ;; files won't vanish when they are checked in | |
858 | (let ((vc-dired-terse-mode nil)) | |
859 | (dired-do-redisplay 1))))) | |
860 | buffers)))) | |
861 | ||
862 | ;;;###autoload | |
863 | (defun vc-directory (dir read-switches) | |
864 | "Create a buffer in VC Dired Mode for directory DIR. | |
865 | ||
866 | See Info node `VC Dired Mode'. | |
867 | ||
868 | With prefix arg READ-SWITCHES, specify a value to override | |
869 | `dired-listing-switches' when generating the listing." | |
870 | (interactive "DDired under VC (directory): \nP") | |
871 | (let ((vc-dired-switches (concat vc-dired-listing-switches | |
872 | (if vc-dired-recurse "R" "")))) | |
873 | (if read-switches | |
874 | (setq vc-dired-switches | |
875 | (read-string "Dired listing switches: " | |
876 | vc-dired-switches))) | |
877 | (require 'dired) | |
878 | (require 'dired-aux) | |
879 | (switch-to-buffer | |
880 | (dired-internal-noselect (expand-file-name (file-name-as-directory dir)) | |
881 | vc-dired-switches | |
882 | 'vc-dired-mode)))) | |
883 | ||
783b505b ER |
884 | ;; The ewoc-based vc-directory implementation |
885 | ||
886 | (defcustom vc-dir-mode-hook nil | |
887 | "Normal hook run by `vc-dir-mode'. | |
888 | See `run-hooks'." | |
889 | :type 'hook | |
890 | :group 'vc) | |
891 | ||
892 | ;; Used to store information for the files displayed in the *VC status* buffer. | |
893 | ;; Each item displayed corresponds to one of these defstructs. | |
894 | (defstruct (vc-dir-fileinfo | |
895 | (:copier nil) | |
896 | (:type list) ;So we can use `member' on lists of FIs. | |
897 | (:constructor | |
898 | ;; We could define it as an alias for `list'. | |
899 | vc-dir-create-fileinfo (name state &optional extra marked directory)) | |
900 | (:conc-name vc-dir-fileinfo->)) | |
901 | name ;Keep it as first, for `member'. | |
902 | state | |
903 | ;; For storing client-mode specific information. | |
904 | extra | |
905 | marked | |
906 | ;; To keep track of not updated files during a global refresh | |
907 | needs-update | |
908 | ;; To distinguish files and directories. | |
909 | directory) | |
910 | ||
cb625535 ER |
911 | ;; Used to describe a dispatcher client mode. |
912 | (defstruct (vc-client-object | |
913 | (:copier nil) | |
914 | (:constructor | |
915 | vc-create-client-object (name | |
916 | headers | |
917 | file-to-info | |
918 | file-to-state | |
919 | file-to-extra | |
920 | updater)) | |
921 | (:conc-name vc-client-object->)) | |
922 | name | |
923 | headers | |
924 | file-to-info | |
925 | file-to-state | |
926 | file-to-extra | |
927 | updater) | |
928 | ||
783b505b ER |
929 | (defvar vc-ewoc nil) |
930 | (defvar vc-dir-process-buffer nil | |
931 | "The buffer used for the asynchronous call that computes the VC status.") | |
932 | ||
933 | (defun vc-dir-move-to-goal-column () | |
934 | ;; Used to keep the cursor on the file name column. | |
935 | (beginning-of-line) | |
936 | ;; Must be in sync with vc-default-status-printer. | |
937 | (forward-char 25)) | |
938 | ||
939 | (defun vc-dir-prepare-status-buffer (dir &optional create-new) | |
940 | "Find a *vc-dir* buffer showing DIR, or create a new one." | |
941 | (setq dir (expand-file-name dir)) | |
942 | (let* ((bname "*vc-dir*") | |
943 | ;; Look for another *vc-dir* buffer visiting the same directory. | |
944 | (buf (save-excursion | |
945 | (unless create-new | |
946 | (dolist (buffer (buffer-list)) | |
947 | (set-buffer buffer) | |
948 | (when (and (eq major-mode 'vc-dir-mode) | |
949 | (string= (expand-file-name default-directory) dir)) | |
950 | (return buffer))))))) | |
951 | (or buf | |
952 | ;; Create a new *vc-dir* buffer. | |
953 | (with-current-buffer (create-file-buffer bname) | |
954 | (cd dir) | |
955 | (vc-setup-buffer (current-buffer)) | |
956 | ;; Reset the vc-parent-buffer-name so that it does not appear | |
957 | ;; in the mode-line. | |
958 | (setq vc-parent-buffer-name nil) | |
959 | (current-buffer))))) | |
960 | ||
961 | (defvar vc-dir-menu-map | |
962 | (let ((map (make-sparse-keymap "VC-dir"))) | |
963 | (define-key map [quit] | |
964 | '(menu-item "Quit" quit-window | |
965 | :help "Quit")) | |
966 | (define-key map [kill] | |
967 | '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process | |
968 | :enable (vc-dir-busy) | |
969 | :help "Kill the command that updates VC status buffer")) | |
970 | (define-key map [refresh] | |
971 | '(menu-item "Refresh" vc-dir-refresh | |
972 | :enable (not (vc-dir-busy)) | |
973 | :help "Refresh the contents of the VC status buffer")) | |
974 | ;; Movement. | |
975 | (define-key map [sepmv] '("--")) | |
976 | (define-key map [next-line] | |
977 | '(menu-item "Next line" vc-dir-next-line | |
978 | :help "Go to the next line" :keys "n")) | |
979 | (define-key map [previous-line] | |
980 | '(menu-item "Previous line" vc-dir-previous-line | |
981 | :help "Go to the previous line")) | |
982 | ;; Marking. | |
983 | (define-key map [sepmrk] '("--")) | |
984 | (define-key map [unmark-all] | |
985 | '(menu-item "Unmark All" vc-dir-unmark-all-files | |
986 | :help "Unmark all files that are in the same state as the current file\ | |
987 | \nWith prefix argument unmark all files")) | |
988 | (define-key map [unmark-previous] | |
989 | '(menu-item "Unmark previous " vc-dir-unmark-file-up | |
990 | :help "Move to the previous line and unmark the file")) | |
991 | ||
992 | (define-key map [mark-all] | |
993 | '(menu-item "Mark All" vc-dir-mark-all-files | |
994 | :help "Mark all files that are in the same state as the current file\ | |
995 | \nWith prefix argument mark all files")) | |
996 | (define-key map [unmark] | |
997 | '(menu-item "Unmark" vc-dir-unmark | |
998 | :help "Unmark the current file or all files in the region")) | |
999 | ||
1000 | (define-key map [mark] | |
1001 | '(menu-item "Mark" vc-dir-mark | |
1002 | :help "Mark the current file or all files in the region")) | |
1003 | ||
1004 | (define-key map [sepopn] '("--")) | |
1005 | (define-key map [open-other] | |
1006 | '(menu-item "Open in other window" vc-dir-find-file-other-window | |
1007 | :help "Find the file on the current line, in another window")) | |
1008 | (define-key map [open] | |
1009 | '(menu-item "Open file" vc-dir-find-file | |
1010 | :help "Find the file on the current line")) | |
1011 | ;; FIXME: Stuff starting here should be appended by vc | |
1012 | ;; VC info details | |
1013 | (define-key map [sepvcdet] '("--")) | |
1014 | (define-key map [remup] | |
1015 | '(menu-item "Hide up-to-date" vc-dir-hide-up-to-date | |
1016 | :help "Hide up-to-date items from display")) | |
1017 | ;; FIXME: This needs a key binding. And maybe a better name | |
1018 | ;; ("Insert" like PCL-CVS uses does not sound that great either)... | |
1019 | (define-key map [ins] | |
1020 | '(menu-item "Show File" vc-dir-show-fileentry | |
1021 | :help "Show a file in the VC status listing even though it might be up to date")) | |
1022 | (define-key map [annotate] | |
1023 | '(menu-item "Annotate" vc-annotate | |
1024 | :help "Display the edit history of the current file using colors")) | |
1025 | (define-key map [diff] | |
1026 | '(menu-item "Compare with Base Version" vc-diff | |
1027 | :help "Compare file set with the base version")) | |
1028 | (define-key map [log] | |
1029 | '(menu-item "Show history" vc-print-log | |
1030 | :help "List the change log of the current file set in a window")) | |
1031 | ;; VC commands. | |
1032 | (define-key map [sepvccmd] '("--")) | |
1033 | (define-key map [update] | |
1034 | '(menu-item "Update to latest version" vc-update | |
1035 | :help "Update the current fileset's files to their tip revisions")) | |
1036 | (define-key map [revert] | |
1037 | '(menu-item "Revert to base version" vc-revert | |
1038 | :help "Revert working copies of the selected fileset to their repository contents.")) | |
1039 | (define-key map [next-action] | |
1040 | ;; FIXME: This really really really needs a better name! | |
1041 | ;; And a key binding too. | |
1042 | '(menu-item "Check In/Out" vc-next-action | |
1043 | :help "Do the next logical version control operation on the current fileset")) | |
1044 | (define-key map [register] | |
1045 | '(menu-item "Register" vc-dir-register | |
1046 | :help "Register file set into the version control system")) | |
1047 | map) | |
1048 | "Menu for VC status") | |
1049 | ||
1050 | (defalias 'vc-dir-menu-map vc-dir-menu-map) | |
1051 | ||
1052 | (defvar vc-dir-mode-map | |
1053 | (let ((map (make-keymap))) | |
1054 | (suppress-keymap map) | |
1055 | ;; Marking. | |
1056 | (define-key map "m" 'vc-dir-mark) | |
1057 | (define-key map "M" 'vc-dir-mark-all-files) | |
1058 | (define-key map "u" 'vc-dir-unmark) | |
1059 | (define-key map "U" 'vc-dir-unmark-all-files) | |
1060 | (define-key map "\C-?" 'vc-dir-unmark-file-up) | |
1061 | (define-key map "\M-\C-?" 'vc-dir-unmark-all-files) | |
1062 | ;; Movement. | |
1063 | (define-key map "n" 'vc-dir-next-line) | |
1064 | (define-key map " " 'vc-dir-next-line) | |
1065 | (define-key map "\t" 'vc-dir-next-line) | |
1066 | (define-key map "p" 'vc-dir-previous-line) | |
1067 | (define-key map [backtab] 'vc-dir-previous-line) | |
783b505b ER |
1068 | ;; The remainder. |
1069 | (define-key map "f" 'vc-dir-find-file) | |
1070 | (define-key map "\C-m" 'vc-dir-find-file) | |
1071 | (define-key map "o" 'vc-dir-find-file-other-window) | |
783b505b ER |
1072 | (define-key map "q" 'quit-window) |
1073 | (define-key map "g" 'vc-dir-refresh) | |
1074 | (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process) | |
1075 | (define-key map [(down-mouse-3)] 'vc-dir-menu) | |
1076 | (define-key map [(mouse-2)] 'vc-dir-toggle-mark) | |
1077 | ||
cb625535 | 1078 | ;; FIXME: Calls back into vc.el |
783b505b ER |
1079 | ;; Hook up the menu. |
1080 | (define-key map [menu-bar vc-dir-mode] | |
1081 | '(menu-item | |
1082 | ;; This is used so that client modes can add mode-specific | |
1083 | ;; menu items to vc-dir-menu-map. | |
1084 | "VC Status" vc-dir-menu-map :filter vc-dir-menu-map-filter)) | |
1085 | map) | |
1086 | "Keymap for VC status") | |
1087 | ||
1088 | (defmacro vc-at-event (event &rest body) | |
1089 | "Evaluate `body' wich point located at event-start of `event'. | |
1090 | If `body' uses `event', it should be a variable, | |
1091 | otherwise it will be evaluated twice." | |
1092 | (let ((posn (gensym "vc-at-event-posn"))) | |
1093 | `(let ((,posn (event-start ,event))) | |
1094 | (save-excursion | |
1095 | (set-buffer (window-buffer (posn-window ,posn))) | |
1096 | (goto-char (posn-point ,posn)) | |
1097 | ,@body)))) | |
1098 | ||
1099 | (defun vc-dir-menu (e) | |
1100 | "Popup the VC status menu." | |
1101 | (interactive "e") | |
1102 | (vc-at-event e (popup-menu vc-dir-menu-map e))) | |
1103 | ||
1104 | (defvar vc-dir-tool-bar-map | |
1105 | (let ((map (make-sparse-keymap))) | |
1106 | (tool-bar-local-item-from-menu 'vc-dir-find-file "open" | |
1107 | map vc-dir-mode-map) | |
1108 | (tool-bar-local-item "bookmark_add" | |
1109 | 'vc-dir-toggle-mark 'vc-dir-toggle-mark map | |
1110 | :help "Toggle mark on current item") | |
1111 | (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow" | |
1112 | map vc-dir-mode-map | |
1113 | :rtl "right-arrow") | |
1114 | (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow" | |
1115 | map vc-dir-mode-map | |
1116 | :rtl "left-arrow") | |
1117 | (tool-bar-local-item-from-menu 'vc-print-log "info" | |
1118 | map vc-dir-mode-map) | |
1119 | (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh" | |
1120 | map vc-dir-mode-map) | |
1121 | (tool-bar-local-item-from-menu 'nonincremental-search-forward | |
1122 | "search" map) | |
1123 | (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel" | |
1124 | map vc-dir-mode-map) | |
1125 | (tool-bar-local-item-from-menu 'quit-window "exit" | |
1126 | map vc-dir-mode-map) | |
1127 | map)) | |
1128 | ||
1129 | ;; t if directories should be shown in vc-dir. | |
1130 | ;; WORK IN PROGRESS! DO NOT SET this! ONLY set it if you want to help | |
1131 | ;; write code for this feature. This variable will likely disappear | |
1132 | ;; when the work is done. | |
1133 | (defvar vc-dir-insert-directories nil) | |
1134 | ||
1135 | (defun vc-dir-update (entries buffer &optional noinsert) | |
1136 | "Update BUFFER's ewoc from the list of ENTRIES. | |
1137 | If NOINSERT, ignore elements on ENTRIES which are not in the ewoc." | |
1138 | ;; Add ENTRIES to the vc-dir buffer BUFFER. | |
1139 | (with-current-buffer buffer | |
1140 | ;; Insert the entries sorted by name into the ewoc. | |
1141 | ;; We assume the ewoc is sorted too, which should be the | |
1142 | ;; case if we always add entries with vc-dir-update. | |
1143 | (setq entries | |
1144 | ;; Sort: first files and then subdirectories. | |
1145 | ;; XXX: this is VERY inefficient, it computes the directory | |
1146 | ;; names too many times | |
1147 | (sort entries | |
1148 | (lambda (entry1 entry2) | |
1149 | (let ((dir1 (file-name-directory (expand-file-name (car entry1)))) | |
1150 | (dir2 (file-name-directory (expand-file-name (car entry2))))) | |
1151 | (cond | |
1152 | ((string< dir1 dir2) t) | |
1153 | ((not (string= dir1 dir2)) nil) | |
1154 | ((string< (car entry1) (car entry2)))))))) | |
1155 | (if (not vc-dir-insert-directories) | |
1156 | (let ((entry (car entries)) | |
1157 | (node (ewoc-nth vc-ewoc 0))) | |
1158 | (while (and entry node) | |
1159 | (let ((entryfile (car entry)) | |
1160 | (nodefile (vc-dir-fileinfo->name (ewoc-data node)))) | |
1161 | (cond | |
1162 | ((string-lessp nodefile entryfile) | |
1163 | (setq node (ewoc-next vc-ewoc node))) | |
1164 | ((string-lessp entryfile nodefile) | |
1165 | (unless noinsert | |
1166 | (ewoc-enter-before vc-ewoc node | |
1167 | (apply 'vc-dir-create-fileinfo entry))) | |
1168 | (setq entries (cdr entries) entry (car entries))) | |
1169 | (t | |
1170 | (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry)) | |
1171 | (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry)) | |
1172 | (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil) | |
1173 | (ewoc-invalidate vc-ewoc node) | |
1174 | (setq entries (cdr entries) entry (car entries)) | |
1175 | (setq node (ewoc-next vc-ewoc node)))))) | |
1176 | (unless (or node noinsert) | |
1177 | ;; We're past the last node, all remaining entries go to the end. | |
1178 | (while entries | |
1179 | (ewoc-enter-last vc-ewoc | |
1180 | (apply 'vc-dir-create-fileinfo (pop entries)))))) | |
1181 | ;; Insert directory entries in the right places. | |
1182 | (let ((entry (car entries)) | |
1183 | (node (ewoc-nth vc-ewoc 0))) | |
1184 | ;; Insert . if it is not present. | |
1185 | (unless node | |
1186 | (let ((rd (file-relative-name default-directory))) | |
1187 | (ewoc-enter-last | |
1188 | vc-ewoc (vc-dir-create-fileinfo | |
1189 | rd nil nil nil (expand-file-name default-directory)))) | |
1190 | (setq node (ewoc-nth vc-ewoc 0))) | |
1191 | ||
1192 | (while (and entry node) | |
1193 | (let* ((entryfile (car entry)) | |
1194 | (entrydir (file-name-directory (expand-file-name entryfile))) | |
1195 | (nodedir | |
1196 | (or (vc-dir-fileinfo->directory (ewoc-data node)) | |
1197 | (file-name-directory | |
1198 | (expand-file-name | |
1199 | (vc-dir-fileinfo->name (ewoc-data node))))))) | |
1200 | (cond | |
1201 | ;; First try to find the directory. | |
1202 | ((string-lessp nodedir entrydir) | |
1203 | (setq node (ewoc-next vc-ewoc node))) | |
1204 | ((string-equal nodedir entrydir) | |
1205 | ;; Found the directory, find the place for the file name. | |
1206 | (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node)))) | |
1207 | (cond | |
1208 | ((string-lessp nodefile entryfile) | |
1209 | (setq node (ewoc-next vc-ewoc node))) | |
1210 | ((string-equal nodefile entryfile) | |
1211 | (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry)) | |
1212 | (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry)) | |
1213 | (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil) | |
1214 | (ewoc-invalidate vc-ewoc node) | |
1215 | (setq entries (cdr entries) entry (car entries)) | |
1216 | (setq node (ewoc-next vc-ewoc node))) | |
1217 | (t | |
1218 | (ewoc-enter-before vc-ewoc node | |
1219 | (apply 'vc-dir-create-fileinfo entry)) | |
1220 | (setq entries (cdr entries) entry (car entries)))))) | |
1221 | (t | |
1222 | ;; We need to insert a directory node | |
1223 | (let ((rd (file-relative-name entrydir))) | |
1224 | (ewoc-enter-last | |
1225 | vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))) | |
1226 | ;; Now insert the node itself. | |
1227 | (ewoc-enter-before vc-ewoc node | |
1228 | (apply 'vc-dir-create-fileinfo entry)) | |
1229 | (setq entries (cdr entries) entry (car entries)))))) | |
1230 | ;; We're past the last node, all remaining entries go to the end. | |
1231 | (unless (or node noinsert) | |
1232 | (let* ((lastnode (ewoc-nth vc-ewoc -1)) | |
1233 | (lastdir | |
1234 | (or (vc-dir-fileinfo->directory (ewoc-data lastnode)) | |
1235 | (file-name-directory | |
1236 | (expand-file-name | |
1237 | (vc-dir-fileinfo->name (ewoc-data lastnode))))))) | |
1238 | (dolist (entry entries) | |
1239 | (let ((entrydir (file-name-directory (expand-file-name (car entry))))) | |
1240 | ;; Insert a directory node if needed. | |
1241 | (unless (string-equal lastdir entrydir) | |
1242 | (setq lastdir entrydir) | |
1243 | (let ((rd (file-relative-name entrydir))) | |
1244 | (ewoc-enter-last | |
1245 | vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))) | |
1246 | ;; Now insert the node itself. | |
1247 | (ewoc-enter-last vc-ewoc | |
1248 | (apply 'vc-dir-create-fileinfo entry)))))))))) | |
1249 | ||
1250 | (defun vc-dir-busy () | |
1251 | (and (buffer-live-p vc-dir-process-buffer) | |
1252 | (get-buffer-process vc-dir-process-buffer))) | |
1253 | ||
1254 | (defun vc-dir-kill-dir-status-process () | |
1255 | "Kill the temporary buffer and associated process." | |
1256 | (interactive) | |
1257 | (when (buffer-live-p vc-dir-process-buffer) | |
1258 | (let ((proc (get-buffer-process vc-dir-process-buffer))) | |
1259 | (when proc (delete-process proc)) | |
1260 | (setq vc-dir-process-buffer nil) | |
1261 | (setq mode-line-process nil)))) | |
1262 | ||
1263 | (defun vc-dir-kill-query () | |
1264 | ;; Make sure that when the VC status buffer is killed the update | |
1265 | ;; process running in background is also killed. | |
1266 | (if (vc-dir-busy) | |
1267 | (when (y-or-n-p "Status update process running, really kill status buffer?") | |
1268 | (vc-dir-kill-dir-status-process) | |
1269 | t) | |
1270 | t)) | |
1271 | ||
1272 | (defun vc-dir-next-line (arg) | |
1273 | "Go to the next line. | |
1274 | If a prefix argument is given, move by that many lines." | |
1275 | (interactive "p") | |
1276 | (ewoc-goto-next vc-ewoc arg) | |
1277 | (vc-dir-move-to-goal-column)) | |
1278 | ||
1279 | (defun vc-dir-previous-line (arg) | |
1280 | "Go to the previous line. | |
1281 | If a prefix argument is given, move by that many lines." | |
1282 | (interactive "p") | |
1283 | (ewoc-goto-prev vc-ewoc arg) | |
1284 | (vc-dir-move-to-goal-column)) | |
1285 | ||
1286 | (defun vc-dir-mark-unmark (mark-unmark-function) | |
1287 | (if (use-region-p) | |
1288 | (let ((firstl (line-number-at-pos (region-beginning))) | |
1289 | (lastl (line-number-at-pos (region-end)))) | |
1290 | (save-excursion | |
1291 | (goto-char (region-beginning)) | |
1292 | (while (<= (line-number-at-pos) lastl) | |
1293 | (funcall mark-unmark-function)))) | |
1294 | (funcall mark-unmark-function))) | |
1295 | ||
1296 | (defun vc-dir-parent-marked-p (arg) | |
1297 | (when vc-dir-insert-directories | |
1298 | ;; Return nil if none of the parent directories of arg is marked. | |
1299 | (let* ((argdata (ewoc-data arg)) | |
1300 | (argdir | |
1301 | (let ((crtdir (vc-dir-fileinfo->directory argdata))) | |
1302 | (if crtdir | |
1303 | crtdir | |
1304 | (file-name-directory (expand-file-name | |
1305 | (vc-dir-fileinfo->name argdata)))))) | |
1306 | (arglen (length argdir)) | |
1307 | (crt arg) | |
1308 | data dir) | |
1309 | ;; Go through the predecessors, checking if any directory that is | |
1310 | ;; a parent is marked. | |
1311 | (while (setq crt (ewoc-prev vc-ewoc crt)) | |
1312 | (setq data (ewoc-data crt)) | |
1313 | (setq dir | |
1314 | (let ((crtdir (vc-dir-fileinfo->directory data))) | |
1315 | (if crtdir | |
1316 | crtdir | |
1317 | (file-name-directory (expand-file-name | |
1318 | (vc-dir-fileinfo->name data)))))) | |
1319 | ||
1320 | (when (and (vc-dir-fileinfo->directory data) | |
1321 | (string-equal (substring argdir 0 (length dir)) dir)) | |
1322 | (when (vc-dir-fileinfo->marked data) | |
1323 | (error "Cannot mark `%s', parent directory `%s' marked" | |
1324 | (vc-dir-fileinfo->name argdata) | |
1325 | (vc-dir-fileinfo->name data))))) | |
1326 | nil))) | |
1327 | ||
1328 | (defun vc-dir-children-marked-p (arg) | |
1329 | ;; Return nil if none of the children of arg is marked. | |
1330 | (when vc-dir-insert-directories | |
1331 | (let* ((argdata (ewoc-data arg)) | |
1332 | (argdir (vc-dir-fileinfo->directory argdata)) | |
1333 | (arglen (length argdir)) | |
1334 | (is-child t) | |
1335 | (crt arg) | |
1336 | data dir) | |
1337 | (while (and is-child (setq crt (ewoc-next vc-ewoc crt))) | |
1338 | (setq data (ewoc-data crt)) | |
1339 | (setq dir | |
1340 | (let ((crtdir (vc-dir-fileinfo->directory data))) | |
1341 | (if crtdir | |
1342 | crtdir | |
1343 | (file-name-directory (expand-file-name | |
1344 | (vc-dir-fileinfo->name data)))))) | |
1345 | (if (string-equal argdir (substring dir 0 arglen)) | |
1346 | (when (vc-dir-fileinfo->marked data) | |
1347 | (error "Cannot mark `%s', child `%s' marked" | |
1348 | (vc-dir-fileinfo->name argdata) | |
1349 | (vc-dir-fileinfo->name data))) | |
1350 | ;; We are done, we got to an entry that is not a child of `arg'. | |
1351 | (setq is-child nil))) | |
1352 | nil))) | |
1353 | ||
1354 | (defun vc-dir-mark-file (&optional arg) | |
1355 | ;; Mark ARG or the current file and move to the next line. | |
1356 | (let* ((crt (or arg (ewoc-locate vc-ewoc))) | |
1357 | (file (ewoc-data crt)) | |
1358 | (isdir (vc-dir-fileinfo->directory file))) | |
1359 | (when (or (and isdir (not (vc-dir-children-marked-p crt))) | |
1360 | (and (not isdir) (not (vc-dir-parent-marked-p crt)))) | |
1361 | (setf (vc-dir-fileinfo->marked file) t) | |
1362 | (ewoc-invalidate vc-ewoc crt) | |
1363 | (unless (or arg (mouse-event-p last-command-event)) | |
1364 | (vc-dir-next-line 1))))) | |
1365 | ||
1366 | (defun vc-dir-mark () | |
1367 | "Mark the current file or all files in the region. | |
1368 | If the region is active, mark all the files in the region. | |
1369 | Otherwise mark the file on the current line and move to the next | |
1370 | line." | |
1371 | (interactive) | |
1372 | (vc-dir-mark-unmark 'vc-dir-mark-file)) | |
1373 | ||
1374 | (defun vc-dir-mark-all-files (arg) | |
1375 | "Mark all files with the same state as the current one. | |
1376 | With a prefix argument mark all files. | |
1377 | If the current entry is a directory, mark all child files. | |
1378 | ||
1379 | The VC commands operate on files that are on the same state. | |
1380 | This command is intended to make it easy to select all files that | |
1381 | share the same state." | |
1382 | (interactive "P") | |
1383 | (if arg | |
1384 | ;; Mark all files. | |
1385 | (progn | |
1386 | ;; First check that no directory is marked, we can't mark | |
1387 | ;; files in that case. | |
1388 | (ewoc-map | |
1389 | (lambda (filearg) | |
1390 | (when (and (vc-dir-fileinfo->directory filearg) | |
1391 | (vc-dir-fileinfo->directory filearg)) | |
1392 | (error "Cannot mark all files, directory `%s' marked" | |
1393 | (vc-dir-fileinfo->name filearg)))) | |
1394 | vc-ewoc) | |
1395 | (ewoc-map | |
1396 | (lambda (filearg) | |
1397 | (unless (vc-dir-fileinfo->marked filearg) | |
1398 | (setf (vc-dir-fileinfo->marked filearg) t) | |
1399 | t)) | |
1400 | vc-ewoc)) | |
1401 | (let ((data (ewoc-data (ewoc-locate vc-ewoc)))) | |
1402 | (if (vc-dir-fileinfo->directory data) | |
1403 | ;; It's a directory, mark child files. | |
1404 | (let ((crt (ewoc-locate vc-ewoc))) | |
1405 | (unless (vc-dir-children-marked-p crt) | |
1406 | (while (setq crt (ewoc-next vc-ewoc crt)) | |
1407 | (let ((crt-data (ewoc-data crt))) | |
1408 | (unless (vc-dir-fileinfo->directory crt-data) | |
1409 | (setf (vc-dir-fileinfo->marked crt-data) t) | |
1410 | (ewoc-invalidate vc-ewoc crt)))))) | |
1411 | ;; It's a file | |
1412 | (let ((state (vc-dir-fileinfo->state data)) | |
1413 | (crt (ewoc-nth vc-ewoc 0))) | |
1414 | (while crt | |
1415 | (let ((crt-data (ewoc-data crt))) | |
1416 | (when (and (not (vc-dir-fileinfo->marked crt-data)) | |
1417 | (eq (vc-dir-fileinfo->state crt-data) state) | |
1418 | (not (vc-dir-fileinfo->directory crt-data))) | |
1419 | (vc-dir-mark-file crt))) | |
1420 | (setq crt (ewoc-next vc-ewoc crt)))))))) | |
1421 | ||
1422 | (defun vc-dir-unmark-file () | |
1423 | ;; Unmark the current file and move to the next line. | |
1424 | (let* ((crt (ewoc-locate vc-ewoc)) | |
1425 | (file (ewoc-data crt))) | |
1426 | (setf (vc-dir-fileinfo->marked file) nil) | |
1427 | (ewoc-invalidate vc-ewoc crt) | |
1428 | (unless (mouse-event-p last-command-event) | |
1429 | (vc-dir-next-line 1)))) | |
1430 | ||
1431 | (defun vc-dir-unmark () | |
1432 | "Unmark the current file or all files in the region. | |
1433 | If the region is active, unmark all the files in the region. | |
1434 | Otherwise mark the file on the current line and move to the next | |
1435 | line." | |
1436 | (interactive) | |
1437 | (vc-dir-mark-unmark 'vc-dir-unmark-file)) | |
1438 | ||
1439 | (defun vc-dir-unmark-file-up () | |
1440 | "Move to the previous line and unmark the file." | |
1441 | (interactive) | |
1442 | ;; If we're on the first line, we won't move up, but we will still | |
1443 | ;; remove the mark. This seems a bit odd but it is what buffer-menu | |
1444 | ;; does. | |
1445 | (let* ((prev (ewoc-goto-prev vc-ewoc 1)) | |
1446 | (file (ewoc-data prev))) | |
1447 | (setf (vc-dir-fileinfo->marked file) nil) | |
1448 | (ewoc-invalidate vc-ewoc prev) | |
1449 | (vc-dir-move-to-goal-column))) | |
1450 | ||
1451 | (defun vc-dir-unmark-all-files (arg) | |
1452 | "Unmark all files with the same state as the current one. | |
1453 | With a prefix argument unmark all files. | |
1454 | If the current entry is a directory, unmark all the child files. | |
1455 | ||
1456 | The VC commands operate on files that are on the same state. | |
1457 | This command is intended to make it easy to deselect all files | |
1458 | that share the same state." | |
1459 | (interactive "P") | |
1460 | (if arg | |
1461 | (ewoc-map | |
1462 | (lambda (filearg) | |
1463 | (when (vc-dir-fileinfo->marked filearg) | |
1464 | (setf (vc-dir-fileinfo->marked filearg) nil) | |
1465 | t)) | |
1466 | vc-ewoc) | |
1467 | (let* ((crt (ewoc-locate vc-ewoc)) | |
1468 | (data (ewoc-data crt))) | |
1469 | (if (vc-dir-fileinfo->directory data) | |
1470 | ;; It's a directory, unmark child files. | |
1471 | (while (setq crt (ewoc-next vc-ewoc crt)) | |
1472 | (let ((crt-data (ewoc-data crt))) | |
1473 | (unless (vc-dir-fileinfo->directory crt-data) | |
1474 | (setf (vc-dir-fileinfo->marked crt-data) nil) | |
1475 | (ewoc-invalidate vc-ewoc crt)))) | |
1476 | ;; It's a file | |
1477 | (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt)))) | |
1478 | (ewoc-map | |
1479 | (lambda (filearg) | |
1480 | (when (and (vc-dir-fileinfo->marked filearg) | |
1481 | (eq (vc-dir-fileinfo->state filearg) crt-state)) | |
1482 | (setf (vc-dir-fileinfo->marked filearg) nil) | |
1483 | t)) | |
1484 | vc-ewoc)))))) | |
1485 | ||
1486 | (defun vc-dir-toggle-mark-file () | |
1487 | (let* ((crt (ewoc-locate vc-ewoc)) | |
1488 | (file (ewoc-data crt))) | |
1489 | (if (vc-dir-fileinfo->marked file) | |
1490 | (vc-dir-unmark-file) | |
1491 | (vc-dir-mark-file)))) | |
1492 | ||
1493 | (defun vc-dir-toggle-mark (e) | |
1494 | (interactive "e") | |
1495 | (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file))) | |
1496 | ||
1497 | (defun vc-dir-delete-file () | |
1498 | "Delete the marked files, or the current file if no marks." | |
1499 | (interactive) | |
1500 | (mapc 'vc-delete-file (or (vc-dir-marked-files) | |
1501 | (list (vc-dir-current-file))))) | |
1502 | ||
1503 | (defun vc-dir-find-file () | |
1504 | "Find the file on the current line." | |
1505 | (interactive) | |
1506 | (find-file (vc-dir-current-file))) | |
1507 | ||
1508 | (defun vc-dir-find-file-other-window () | |
1509 | "Find the file on the current line, in another window." | |
1510 | (interactive) | |
1511 | (find-file-other-window (vc-dir-current-file))) | |
1512 | ||
1513 | (defun vc-dir-current-file () | |
1514 | (let ((node (ewoc-locate vc-ewoc))) | |
1515 | (unless node | |
1516 | (error "No file available.")) | |
1517 | (expand-file-name (vc-dir-fileinfo->name (ewoc-data node))))) | |
1518 | ||
1519 | (defun vc-dir-marked-files () | |
1520 | "Return the list of marked files." | |
1521 | (mapcar | |
1522 | (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem))) | |
1523 | (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked))) | |
1524 | ||
1525 | (defun vc-dir-marked-only-files () | |
cb625535 | 1526 | "Return the list of marked files, For marked directories return child files." |
783b505b ER |
1527 | (let ((crt (ewoc-nth vc-ewoc 0)) |
1528 | result) | |
1529 | (while crt | |
1530 | (let ((crt-data (ewoc-data crt))) | |
1531 | (if (vc-dir-fileinfo->marked crt-data) | |
1532 | (if (vc-dir-fileinfo->directory crt-data) | |
1533 | (let* ((dir (vc-dir-fileinfo->directory crt-data)) | |
1534 | (dirlen (length dir)) | |
1535 | data) | |
1536 | (while | |
1537 | (and (setq crt (ewoc-next vc-ewoc crt)) | |
1538 | (string-equal | |
1539 | (substring | |
1540 | (progn | |
1541 | (setq data (ewoc-data crt)) | |
1542 | (let ((crtdir (vc-dir-fileinfo->directory data))) | |
1543 | (if crtdir | |
1544 | crtdir | |
1545 | (file-name-directory | |
1546 | (expand-file-name | |
1547 | (vc-dir-fileinfo->name data)))))) | |
1548 | 0 dirlen) | |
1549 | dir)) | |
1550 | (unless (vc-dir-fileinfo->directory data) | |
1551 | (push (vc-dir-fileinfo->name data) result)))) | |
1552 | (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result) | |
1553 | (setq crt (ewoc-next vc-ewoc crt))) | |
1554 | (setq crt (ewoc-next vc-ewoc crt))))) | |
1555 | result)) | |
038608c7 | 1556 | |
cb625535 ER |
1557 | (defun vc-dir-mark-buffer-changed (&optional fname) |
1558 | (let* ((file (or fname (expand-file-name buffer-file-name))) | |
1559 | (found-vc-dir-buf nil)) | |
1560 | (save-excursion | |
1561 | (dolist (status-buf (buffer-list)) | |
1562 | (set-buffer status-buf) | |
1563 | ;; look for a vc-dir buffer that might show this file. | |
1564 | (when (eq major-mode 'vc-dir-mode) | |
1565 | (setq found-vc-dir-buf t) | |
1566 | (let ((ddir (expand-file-name default-directory))) | |
1567 | ;; This test is cvs-string-prefix-p | |
1568 | (when (eq t (compare-strings file nil (length ddir) ddir nil nil)) | |
1569 | (let* | |
1570 | ((file-short (substring file (length ddir))) | |
6494957a SM |
1571 | (state |
1572 | (funcall (vc-client-object->file-to-state vc-client-mode) | |
038608c7 | 1573 | file)) |
cb625535 | 1574 | (extra |
6494957a | 1575 | (funcall (vc-client-object->file-to-extra vc-client-mode) |
038608c7 | 1576 | file)) |
cb625535 ER |
1577 | (entry |
1578 | (list file-short state extra))) | |
1579 | (vc-dir-update (list entry) status-buf)))))) | |
1580 | ;; We didn't find any vc-dir buffers, remove the hook, it is | |
1581 | ;; not needed. | |
1582 | (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-dir-mark-buffer-changed))))) | |
1583 | ||
1584 | (defun vc-dir-mode (client-object) | |
1585 | "Major mode for showing the VC status for a directory. | |
1586 | Marking/Unmarking key bindings and actions: | |
1587 | m - marks a file/directory or if the region is active, mark all the files | |
1588 | in region. | |
1589 | Restrictions: - a file cannot be marked if any parent directory is marked | |
1590 | - a directory cannot be marked if any child file or | |
1591 | directory is marked | |
1592 | u - marks a file/directory or if the region is active, unmark all the files | |
1593 | in region. | |
1594 | M - if the cursor is on a file: mark all the files with the same VC state as | |
1595 | the current file | |
1596 | - if the cursor is on a directory: mark all child files | |
1597 | - with a prefix argument: mark all files | |
1598 | U - if the cursor is on a file: unmark all the files with the same VC state | |
1599 | as the current file | |
1600 | - if the cursor is on a directory: unmark all child files | |
1601 | - with a prefix argument: unmark all files | |
1602 | ||
1603 | ||
1604 | \\{vc-dir-mode-map}" | |
1605 | (setq mode-name (vc-client-object->name client-object)) | |
1606 | (setq major-mode 'vc-dir-mode) | |
1607 | (setq buffer-read-only t) | |
1608 | (use-local-map vc-dir-mode-map) | |
1609 | (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map) | |
6494957a | 1610 | (set (make-local-variable 'vc-client-mode) client-object) |
cb625535 ER |
1611 | (let ((buffer-read-only nil)) |
1612 | (erase-buffer) | |
1613 | (set (make-local-variable 'vc-dir-process-buffer) nil) | |
1614 | (set (make-local-variable 'vc-ewoc) | |
1615 | (ewoc-create (vc-client-object->file-to-info client-object) | |
1616 | (vc-client-object->headers client-object))) | |
1617 | (add-hook 'after-save-hook 'vc-dir-mark-buffer-changed) | |
1618 | ;; Make sure that if the VC status buffer is killed, the update | |
1619 | ;; process running in the background is also killed. | |
1620 | (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t) | |
1621 | (funcall (vc-client-object->updater client-object))) | |
1622 | (run-hooks 'vc-dir-mode-hook)) | |
1623 | ||
1624 | (put 'vc-dir-mode 'mode-class 'special) | |
1625 | ||
be636037 ER |
1626 | (defun vc-buffer-sync (&optional not-urgent) |
1627 | "Make sure the current buffer and its working file are in sync. | |
1628 | NOT-URGENT means it is ok to continue if the user says not to save." | |
1629 | (when (buffer-modified-p) | |
1630 | (if (or vc-suppress-confirm | |
1631 | (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name)))) | |
1632 | (save-buffer) | |
1633 | (unless not-urgent | |
1634 | (error "Aborted"))))) | |
1635 | ||
b236ab0d ER |
1636 | (defun vc-dispatcher-browsing () |
1637 | "Are we in a directory browser buffer?" | |
1638 | (or vc-dired-mode (eq major-mode 'vc-dir-mode))) | |
1639 | ||
1640 | (defun vc-dispatcher-selection-set (eligible | |
1641 | &optional | |
1642 | allow-directory-wildcard | |
1643 | allow-inegible | |
1644 | include-files-not-directories) | |
1645 | "Deduce a set of files to which to apply an operation. Return the fileset. | |
1646 | If we're in VC-dired mode, the fileset is the list of marked files. | |
1647 | Otherwise, if we're looking at a buffer for which ELIGIBLE returns non-NIL, | |
1648 | the fileset is a singleton containing this file. | |
1649 | If neither of these things is true, but ALLOW-DIRECTORY-WILDCARD is on | |
1650 | and we're in a dired buffer, select the current directory. | |
1651 | If none of these conditions is met, but ALLOW-INELIGIBLE is on and the | |
1652 | visited file is not registered, return a singleton fileset containing it. | |
1653 | If INCLUDE-FILES-NOT-DIRECTORIES then if directories are marked, | |
1654 | return the list of VC files in those directories instead of | |
1655 | the directories themselves. | |
1656 | Otherwise, throw an error." | |
be636037 | 1657 | (let ((files |
b236ab0d ER |
1658 | (cond |
1659 | ;; Browsing with dired | |
1660 | (vc-dired-mode | |
1661 | (let ((marked (dired-map-over-marks (dired-get-filename) nil))) | |
1662 | (if marked | |
1663 | marked | |
1664 | (error "No files have been selected.")))) | |
1665 | ;; Browsing with vc-dir | |
1666 | ((eq major-mode 'vc-dir-mode) | |
1667 | (or | |
1668 | (if include-files-not-directories | |
1669 | (vc-dir-marked-only-files) | |
1670 | (vc-dir-marked-files)) | |
1671 | (list (vc-dir-current-file)))) | |
1672 | ;; Visiting an eligible file | |
1673 | ((funcall eligible buffer-file-name) | |
1674 | (list buffer-file-name)) | |
1675 | ;; No eligible file -- if there's a parent buffer, deuce from there | |
1676 | ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer) | |
1677 | (with-current-buffer vc-parent-buffer | |
1678 | (vc-dispatcher-browsing)))) | |
1679 | (progn | |
1680 | (set-buffer vc-parent-buffer) | |
1681 | (vc-dispatcher-selection-set))) | |
1682 | ;; No parent buffer, we may want to select entire directory | |
1683 | ;; | |
1684 | ;; This is guarded by an enabling arg so users won't potentially | |
1685 | ;; shoot themselves in the foot by modifying a fileset they can't | |
1686 | ;; verify by eyeball. Allow it for nondestructive commands like | |
1687 | ;; making diffs, or possibly for destructive ones that have | |
1688 | ;; confirmation prompts. | |
1689 | ((and allow-directory-wildcard | |
1690 | ;; I think this is a misfeature. For now, I'll leave it in, but | |
1691 | ;; I'll disable it anywhere else than in dired buffers. --Stef | |
1692 | (and (derived-mode-p 'dired-mode) | |
1693 | (equal buffer-file-name nil) | |
1694 | (equal list-buffers-directory default-directory))) | |
1695 | (progn | |
1696 | (message "All eligible files below %s selected." | |
1697 | default-directory) | |
1698 | (list default-directory))) | |
1699 | ;; Last, if we're allowing ineligible files and visiting one, select it. | |
1700 | ((and allow-ineligible (not (eligible buffer-file-name))) | |
1701 | (list buffer-file-name)) | |
1702 | ;; No good set here, throw error | |
be636037 ER |
1703 | (t (error "No fileset is available here."))))) |
1704 | ;; We assume, in order to avoid unpleasant surprises to the user, | |
1705 | ;; that a fileset is not in good shape to be handed to the user if the | |
1706 | ;; buffers visting the fileset don't match the on-disk contents. | |
1707 | (dolist (file files) | |
1708 | (let ((visited (get-file-buffer file))) | |
1709 | (when visited | |
1710 | (if (or vc-dired-mode (eq major-mode 'vc-dir-mode)) | |
1711 | (switch-to-buffer-other-window visited) | |
1712 | (set-buffer visited)) | |
1713 | ;; Check relation of buffer and file, and make sure | |
1714 | ;; user knows what he's doing. First, finding the file | |
1715 | ;; will check whether the file on disk is newer. | |
1716 | ;; Ignore buffer-read-only during this test, and | |
1717 | ;; preserve find-file-literally. | |
1718 | (let ((buffer-read-only (not (file-writable-p file)))) | |
1719 | (find-file-noselect file nil find-file-literally)) | |
1720 | (if (not (verify-visited-file-modtime (current-buffer))) | |
1721 | (if (yes-or-no-p (format "Replace %s on disk with buffer contents? " file)) | |
1722 | (write-file buffer-file-name) | |
1723 | (error "Aborted")) | |
1724 | ;; Now, check if we have unsaved changes. | |
1725 | (vc-buffer-sync t) | |
1726 | (when (buffer-modified-p) | |
1727 | (or (y-or-n-p (message "Use %s on disk, keeping modified buffer? " file)) | |
1728 | (error "Aborted"))))))) | |
1729 | files)) | |
b236ab0d | 1730 | |
3eb20677 | 1731 | ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246 |
b1ddeeb7 | 1732 | ;;; vc-dispatcher.el ends here |