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