Unbreak bootstrap.
[bpt/emacs.git] / lisp / vc-dispatcher.el
CommitLineData
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
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
4f61cc3e
ER
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
83affd96
ER
84;; General customization
85
86(defcustom vc-logentry-check-hook nil
87 "Normal hook run by `vc-finish-logentry'.
88Use this to impose your own rules on the entry in addition to any the
4f61cc3e 89dispatcher client mode imposes itself."
83affd96
ER
90 :type 'hook
91 :group 'vc)
92
17f039f3
ER
93(defcustom vc-delete-logbuf-window t
94 "If non-nil, delete the *VC-log* buffer and window after each logical action.
95If nil, bury that buffer instead.
96This is most useful if you have multiple windows on a frame and would like to
97preserve the setting."
98 :type 'boolean
99 :group 'vc)
100
101(defcustom vc-command-messages nil
102 "If non-nil, display run messages from back-end commands."
103 :type 'boolean
104 :group 'vc)
105
83affd96 106;; Variables the user doesn't need to know about.
17f039f3 107
83affd96
ER
108(defvar vc-log-operation nil)
109(defvar vc-log-after-operation-hook nil)
110(defvar vc-log-fileset)
111(defvar vc-log-extra)
112
113;; In a log entry buffer, this is a local variable
114;; that points to the buffer for which it was made
115;; (either a file, or a VC dired buffer).
116(defvar vc-parent-buffer nil)
117(put 'vc-parent-buffer 'permanent-local t)
118(defvar vc-parent-buffer-name nil)
119(put 'vc-parent-buffer-name 'permanent-local t)
120
92d1eebf
ER
121;; Common command execution logic
122
123(defun vc-process-filter (p s)
124 "An alternative output filter for async process P.
125One difference with the default filter is that this inserts S after markers.
126Another is that undo information is not kept."
127 (let ((buffer (process-buffer p)))
128 (when (buffer-live-p buffer)
129 (with-current-buffer buffer
130 (save-excursion
131 (let ((buffer-undo-list t)
132 (inhibit-read-only t))
133 (goto-char (process-mark p))
134 (insert s)
135 (set-marker (process-mark p) (point))))))))
136
7265c6e8
ER
137(defun vc-setup-buffer (buf)
138 "Prepare BUF for executing a slave command and make it current."
92d1eebf
ER
139 (let ((camefrom (current-buffer))
140 (olddir default-directory))
141 (set-buffer (get-buffer-create buf))
142 (kill-all-local-variables)
143 (set (make-local-variable 'vc-parent-buffer) camefrom)
144 (set (make-local-variable 'vc-parent-buffer-name)
145 (concat " from " (buffer-name camefrom)))
146 (setq default-directory olddir)
147 (let ((buffer-undo-list t)
148 (inhibit-read-only t))
149 (erase-buffer))))
150
151(defvar vc-sentinel-movepoint) ;Dynamically scoped.
152
153(defun vc-process-sentinel (p s)
154 (let ((previous (process-get p 'vc-previous-sentinel))
155 (buf (process-buffer p)))
156 ;; Impatient users sometime kill "slow" buffers; check liveness
157 ;; to avoid "error in process sentinel: Selecting deleted buffer".
158 (when (buffer-live-p buf)
159 (when previous (funcall previous p s))
160 (with-current-buffer buf
161 (setq mode-line-process
162 (let ((status (process-status p)))
163 ;; Leave mode-line uncluttered, normally.
164 (unless (eq 'exit status)
165 (format " (%s)" status))))
166 (let (vc-sentinel-movepoint)
167 ;; Normally, we want async code such as sentinels to not move point.
168 (save-excursion
169 (goto-char (process-mark p))
170 (let ((cmds (process-get p 'vc-sentinel-commands)))
171 (process-put p 'vc-sentinel-commands nil)
172 (dolist (cmd cmds)
173 ;; Each sentinel may move point and the next one should be run
174 ;; at that new point. We could get the same result by having
175 ;; each sentinel read&set process-mark, but since `cmd' needs
176 ;; to work both for async and sync processes, this would be
177 ;; difficult to achieve.
178 (vc-exec-after cmd))))
179 ;; But sometimes the sentinels really want to move point.
180 (when vc-sentinel-movepoint
181 (let ((win (get-buffer-window (current-buffer) 0)))
182 (if (not win)
183 (goto-char vc-sentinel-movepoint)
184 (with-selected-window win
185 (goto-char vc-sentinel-movepoint))))))))))
186
187(defun vc-set-mode-line-busy-indicator ()
188 (setq mode-line-process
189 (concat " " (propertize "[waiting...]"
190 'face 'mode-line-emphasis
191 'help-echo
192 "A VC command is in progress in this buffer"))))
193
194(defun vc-exec-after (code)
195 "Eval CODE when the current buffer's process is done.
196If the current buffer has no process, just evaluate CODE.
197Else, add CODE to the process' sentinel."
198 (let ((proc (get-buffer-process (current-buffer))))
199 (cond
200 ;; If there's no background process, just execute the code.
201 ;; We used to explicitly call delete-process on exited processes,
202 ;; but this led to timing problems causing process output to be
203 ;; lost. Terminated processes get deleted automatically
204 ;; anyway. -- cyd
205 ((or (null proc) (eq (process-status proc) 'exit))
206 ;; Make sure we've read the process's output before going further.
207 (when proc (accept-process-output proc))
208 (eval code))
209 ;; If a process is running, add CODE to the sentinel
210 ((eq (process-status proc) 'run)
211 (vc-set-mode-line-busy-indicator)
212 (let ((previous (process-sentinel proc)))
213 (unless (eq previous 'vc-process-sentinel)
214 (process-put proc 'vc-previous-sentinel previous))
215 (set-process-sentinel proc 'vc-process-sentinel))
216 (process-put proc 'vc-sentinel-commands
217 ;; We keep the code fragments in the order given
218 ;; so that vc-diff-finish's message shows up in
219 ;; the presence of non-nil vc-command-messages.
220 (append (process-get proc 'vc-sentinel-commands)
221 (list code))))
222 (t (error "Unexpected process state"))))
223 nil)
224
225(defvar vc-post-command-functions nil
226 "Hook run at the end of `vc-do-command'.
227Each function is called inside the buffer in which the command was run
228and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
229
230(defvar w32-quote-process-args)
231
232(defun vc-delistify (filelist)
233 "Smash a FILELIST into a file list string suitable for info messages."
234 ;; FIXME what about file names with spaces?
235 (if (not filelist) "." (mapconcat 'identity filelist " ")))
236
237;;;###autoload
238(defun vc-do-command (buffer okstatus command file-or-list &rest flags)
239 "Execute a VC command, notifying user and checking for errors.
240Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
241current buffer if BUFFER is t. If the destination buffer is not
242already current, set it up properly and erase it. The command is
243considered successful if its exit status does not exceed OKSTATUS (if
244OKSTATUS is nil, that means to ignore error status, if it is `async', that
245means not to wait for termination of the subprocess; if it is t it means to
246ignore all execution errors). FILE-OR-LIST is the name of a working file;
247it may be a list of files or be nil (to execute commands that don't expect
248a file name or set of files). If an optional list of FLAGS is present,
249that is inserted into the command line before the filename."
250 ;; FIXME: file-relative-name can return a bogus result because
251 ;; it doesn't look at the actual file-system to see if symlinks
252 ;; come into play.
253 (let* ((files
254 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
255 (if (listp file-or-list) file-or-list (list file-or-list))))
256 (full-command
257 ;; What we're doing here is preparing a version of the command
258 ;; for display in a debug-progess message. If it's fewer than
259 ;; 20 characters display the entire command (without trailing
260 ;; newline). Otherwise display the first 20 followed by an ellipsis.
261 (concat (if (string= (substring command -1) "\n")
262 (substring command 0 -1)
263 command)
264 " "
265 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
266 " " (vc-delistify files))))
267 (save-current-buffer
268 (unless (or (eq buffer t)
269 (and (stringp buffer)
270 (string= (buffer-name) buffer))
271 (eq buffer (current-buffer)))
b1ddeeb7 272 (vc-setup-buffer (or buffer "*vc*")))
92d1eebf
ER
273 ;; If there's some previous async process still running, just kill it.
274 (let ((oldproc (get-buffer-process (current-buffer))))
275 ;; If we wanted to wait for oldproc to finish before doing
276 ;; something, we'd have used vc-eval-after.
277 ;; Use `delete-process' rather than `kill-process' because we don't
278 ;; want any of its output to appear from now on.
279 (if oldproc (delete-process oldproc)))
280 (let ((squeezed (remq nil flags))
281 (inhibit-read-only t)
282 (status 0))
283 (when files
284 (setq squeezed (nconc squeezed files)))
285 (let ((exec-path (append vc-path exec-path))
286 ;; Add vc-path to PATH for the execution of this command.
287 (process-environment
288 (cons (concat "PATH=" (getenv "PATH")
289 path-separator
290 (mapconcat 'identity vc-path path-separator))
291 process-environment))
292 (w32-quote-process-args t))
293 (when (and (eq okstatus 'async) (file-remote-p default-directory))
294 ;; start-process does not support remote execution
295 (setq okstatus nil))
296 (if (eq okstatus 'async)
297 ;; Run asynchronously.
298 (let ((proc
299 (let ((process-connection-type nil))
300 (apply 'start-file-process command (current-buffer)
301 command squeezed))))
302 (if vc-command-messages
303 (message "Running %s in background..." full-command))
304 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
305 (set-process-filter proc 'vc-process-filter)
306 (vc-exec-after
307 `(if vc-command-messages
308 (message "Running %s in background... done" ',full-command))))
309 ;; Run synchrously
310 (when vc-command-messages
311 (message "Running %s in foreground..." full-command))
312 (let ((buffer-undo-list t))
313 (setq status (apply 'process-file command nil t nil squeezed)))
314 (when (and (not (eq t okstatus))
315 (or (not (integerp status))
316 (and okstatus (< okstatus status))))
317 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
318 (pop-to-buffer (current-buffer))
319 (goto-char (point-min))
320 (shrink-window-if-larger-than-buffer))
321 (error "Running %s...FAILED (%s)" full-command
322 (if (integerp status) (format "status %d" status) status))))
323 ;; We're done. But don't emit a status message if running
324 ;; asychronously, it would just mislead.
325 (if (and vc-command-messages (not (eq okstatus 'async)))
326 (message "Running %s...OK = %d" full-command status)))
327 (vc-exec-after
328 `(run-hook-with-args 'vc-post-command-functions
329 ',command ',file-or-list ',flags))
330 status))))
331
17f039f3
ER
332;; These functions are used to ensure that the view the user sees is up to date
333;; even if the dispatcher client mode has messed with file contents (as in,
334;; for example, VCS keyword expansion).
335
336(declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
337
338(defun vc-position-context (posn)
339 "Save a bit of the text around POSN in the current buffer.
340Used to help us find the corresponding position again later
341if markers are destroyed or corrupted."
342 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
343 ;; rcs.el mode.
344 (list posn
345 (buffer-size)
346 (buffer-substring posn
347 (min (point-max) (+ posn 100)))))
348
349(defun vc-find-position-by-context (context)
350 "Return the position of CONTEXT in the current buffer.
351If CONTEXT cannot be found, return nil."
352 (let ((context-string (nth 2 context)))
353 (if (equal "" context-string)
354 (point-max)
355 (save-excursion
356 (let ((diff (- (nth 1 context) (buffer-size))))
357 (when (< diff 0) (setq diff (- diff)))
358 (goto-char (nth 0 context))
359 (if (or (search-forward context-string nil t)
360 ;; Can't use search-backward since the match may continue
361 ;; after point.
362 (progn (goto-char (- (point) diff (length context-string)))
363 ;; goto-char doesn't signal an error at
364 ;; beginning of buffer like backward-char would
365 (search-forward context-string nil t)))
366 ;; to beginning of OSTRING
367 (- (point) (length context-string))))))))
368
369(defun vc-context-matches-p (posn context)
370 "Return t if POSN matches CONTEXT, nil otherwise."
371 (let* ((context-string (nth 2 context))
372 (len (length context-string))
373 (end (+ posn len)))
374 (if (> end (1+ (buffer-size)))
375 nil
376 (string= context-string (buffer-substring posn end)))))
377
378(defun vc-buffer-context ()
379 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
380Used by `vc-restore-buffer-context' to later restore the context."
381 (let ((point-context (vc-position-context (point)))
382 ;; Use mark-marker to avoid confusion in transient-mark-mode.
383 (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
384 (vc-position-context (mark-marker))))
385 ;; Make the right thing happen in transient-mark-mode.
386 (mark-active nil)
387 ;; The new compilation code does not use compilation-error-list any
388 ;; more, so the code below is now ineffective and might as well
389 ;; be disabled. -- Stef
390 ;; ;; We may want to reparse the compilation buffer after revert
391 ;; (reparse (and (boundp 'compilation-error-list) ;compile loaded
392 ;; ;; Construct a list; each elt is nil or a buffer
393 ;; ;; if that buffer is a compilation output buffer
394 ;; ;; that contains markers into the current buffer.
395 ;; (save-current-buffer
396 ;; (mapcar (lambda (buffer)
397 ;; (set-buffer buffer)
398 ;; (let ((errors (or
399 ;; compilation-old-error-list
400 ;; compilation-error-list))
401 ;; (buffer-error-marked-p nil))
402 ;; (while (and (consp errors)
403 ;; (not buffer-error-marked-p))
404 ;; (and (markerp (cdr (car errors)))
405 ;; (eq buffer
406 ;; (marker-buffer
407 ;; (cdr (car errors))))
408 ;; (setq buffer-error-marked-p t))
409 ;; (setq errors (cdr errors)))
410 ;; (if buffer-error-marked-p buffer)))
411 ;; (buffer-list)))))
412 (reparse nil))
413 (list point-context mark-context reparse)))
414
415(defun vc-restore-buffer-context (context)
416 "Restore point/mark, and reparse any affected compilation buffers.
417CONTEXT is that which `vc-buffer-context' returns."
418 (let ((point-context (nth 0 context))
419 (mark-context (nth 1 context))
420 ;; (reparse (nth 2 context))
421 )
422 ;; The new compilation code does not use compilation-error-list any
423 ;; more, so the code below is now ineffective and might as well
424 ;; be disabled. -- Stef
425 ;; ;; Reparse affected compilation buffers.
426 ;; (while reparse
427 ;; (if (car reparse)
428 ;; (with-current-buffer (car reparse)
429 ;; (let ((compilation-last-buffer (current-buffer)) ;select buffer
430 ;; ;; Record the position in the compilation buffer of
431 ;; ;; the last error next-error went to.
432 ;; (error-pos (marker-position
433 ;; (car (car-safe compilation-error-list)))))
434 ;; ;; Reparse the error messages as far as they were parsed before.
435 ;; (compile-reinitialize-errors '(4) compilation-parsing-end)
436 ;; ;; Move the pointer up to find the error we were at before
437 ;; ;; reparsing. Now next-error should properly go to the next one.
438 ;; (while (and compilation-error-list
439 ;; (/= error-pos (car (car compilation-error-list))))
440 ;; (setq compilation-error-list (cdr compilation-error-list))))))
441 ;; (setq reparse (cdr reparse)))
442
443 ;; if necessary, restore point and mark
444 (if (not (vc-context-matches-p (point) point-context))
445 (let ((new-point (vc-find-position-by-context point-context)))
446 (when new-point (goto-char new-point))))
447 (and mark-active
448 mark-context
449 (not (vc-context-matches-p (mark) mark-context))
450 (let ((new-mark (vc-find-position-by-context mark-context)))
451 (when new-mark (set-mark new-mark))))))
452
453(defun vc-revert-buffer-internal (&optional arg no-confirm)
454 "Revert buffer, keeping point and mark where user expects them.
455Try to be clever in the face of changes due to expanded version-control
456key words. This is important for typeahead to work as expected.
457ARG and NO-CONFIRM are passed on to `revert-buffer'."
458 (interactive "P")
459 (widen)
460 (let ((context (vc-buffer-context)))
461 ;; Use save-excursion here, because it may be able to restore point
462 ;; and mark properly even in cases where vc-restore-buffer-context
463 ;; would fail. However, save-excursion might also get it wrong --
464 ;; in this case, vc-restore-buffer-context gives it a second try.
465 (save-excursion
466 ;; t means don't call normal-mode;
467 ;; that's to preserve various minor modes.
468 (revert-buffer arg no-confirm t))
469 (vc-restore-buffer-context context)))
470
471(defun vc-resynch-window (file &optional keep noquery)
472 "If FILE is in the current buffer, either revert or unvisit it.
473The choice between revert (to see expanded keywords) and unvisit
474depends on KEEP. NOQUERY if non-nil inhibits confirmation for
475reverting. NOQUERY should be t *only* if it is known the only
476difference between the buffer and the file is due to
477modifications by the dispatcher client code, rather than user
478editing!"
479 (and (string= buffer-file-name file)
480 (if keep
481 (progn
482 (vc-revert-buffer-internal t noquery)
483 ;; TODO: Adjusting view mode might no longer be necessary
484 ;; after RMS change to files.el of 1999-08-08. Investigate
485 ;; this when we install the new VC.
486 (and view-read-only
487 (if (file-writable-p file)
488 (and view-mode
489 (let ((view-old-buffer-read-only nil))
490 (view-mode-exit)))
491 (and (not view-mode)
492 (not (eq (get major-mode 'mode-class) 'special))
493 (view-mode-enter))))
494 ;; FIXME: Call into vc.el
495 (vc-mode-line buffer-file-name))
496 (kill-buffer (current-buffer)))))
497
498(defun vc-resynch-buffer (file &optional keep noquery)
499 "If FILE is currently visited, resynch its buffer."
500 (if (string= buffer-file-name file)
501 (vc-resynch-window file keep noquery)
502 (let ((buffer (get-file-buffer file)))
503 (when buffer
504 (with-current-buffer buffer
505 (vc-resynch-window file keep noquery)))))
506 ;; FIME: Call into vc.el
507 (vc-directory-resynch-file file)
508 (when (memq 'vc-dir-mark-buffer-changed after-save-hook)
509 (let ((buffer (get-file-buffer file)))
510 ;; FIME: Call into vc.el
511 (vc-dir-mark-buffer-changed file))))
512
83affd96
ER
513;; Command closures
514
515(defun vc-start-logentry (files extra comment initial-contents msg action &optional after-hook)
516 "Accept a comment for an operation on FILES with extra data EXTRA.
517If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
518action on close to ACTION. If COMMENT is a string and
519INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
520contents of the log entry buffer. If COMMENT is a string and
521INITIAL-CONTENTS is nil, do action immediately as if the user had
522entered COMMENT. If COMMENT is t, also do action immediately with an
523empty comment. Remember the file's buffer in `vc-parent-buffer'
524\(current one if no file). AFTER-HOOK specifies the local value
525for `vc-log-after-operation-hook'."
526 (let ((parent
527 (if (or (eq major-mode 'vc-dired-mode) (eq major-mode 'vc-dir-mode))
528 ;; If we are called from VC dired, the parent buffer is
529 ;; the current buffer.
530 (current-buffer)
531 (if (and files (equal (length files) 1))
532 (get-file-buffer (car files))
533 (current-buffer)))))
83affd96
ER
534 (if (and comment (not initial-contents))
535 (set-buffer (get-buffer-create "*VC-log*"))
536 (pop-to-buffer (get-buffer-create "*VC-log*")))
537 (set (make-local-variable 'vc-parent-buffer) parent)
538 (set (make-local-variable 'vc-parent-buffer-name)
539 (concat " from " (buffer-name vc-parent-buffer)))
83affd96
ER
540 (vc-log-edit files)
541 (make-local-variable 'vc-log-after-operation-hook)
542 (when after-hook
543 (setq vc-log-after-operation-hook after-hook))
544 (setq vc-log-operation action)
545 (setq vc-log-extra extra)
546 (when comment
547 (erase-buffer)
548 (when (stringp comment) (insert comment)))
549 (if (or (not comment) initial-contents)
550 (message "%s Type C-c C-c when done" msg)
551 (vc-finish-logentry (eq comment t)))))
552
553(defun vc-finish-logentry (&optional nocomment)
554 "Complete the operation implied by the current log entry.
555Use the contents of the current buffer as a check-in or registration
556comment. If the optional arg NOCOMMENT is non-nil, then don't check
557the buffer contents as a comment."
558 (interactive)
559 ;; Check and record the comment, if any.
560 (unless nocomment
561 (run-hooks 'vc-logentry-check-hook))
562 ;; Sync parent buffer in case the user modified it while editing the comment.
563 ;; But not if it is a vc-dired buffer.
564 (with-current-buffer vc-parent-buffer
565 (or vc-dired-mode (eq major-mode 'vc-dir-mode) (vc-buffer-sync)))
566 (unless vc-log-operation
567 (error "No log operation is pending"))
568 ;; save the parameters held in buffer-local variables
569 (let ((log-operation vc-log-operation)
570 (log-fileset vc-log-fileset)
571 (log-extra vc-log-extra)
572 (log-entry (buffer-string))
573 (after-hook vc-log-after-operation-hook)
574 (tmp-vc-parent-buffer vc-parent-buffer))
575 (pop-to-buffer vc-parent-buffer)
576 ;; OK, do it to it
577 (save-excursion
578 (funcall log-operation
579 log-fileset
580 log-extra
581 log-entry))
582 ;; Remove checkin window (after the checkin so that if that fails
583 ;; we don't zap the *VC-log* buffer and the typing therein).
584 ;; -- IMO this should be replaced with quit-window
585 (let ((logbuf (get-buffer "*VC-log*")))
586 (cond ((and logbuf vc-delete-logbuf-window)
587 (delete-windows-on logbuf (selected-frame))
588 ;; Kill buffer and delete any other dedicated windows/frames.
589 (kill-buffer logbuf))
590 (logbuf (pop-to-buffer "*VC-log*")
591 (bury-buffer)
592 (pop-to-buffer tmp-vc-parent-buffer))))
593 ;; Now make sure we see the expanded headers
594 (when log-fileset
595 (mapc
596 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
597 log-fileset))
598 (when vc-dired-mode
599 (dired-move-to-filename))
600 (when (eq major-mode 'vc-dir-mode)
601 (vc-dir-move-to-goal-column))
602 (run-hooks after-hook 'vc-finish-logentry-hook)))
603
4f61cc3e
ER
604;; VC-Dired mode (to be removed when vc-dir support is finished)
605
606(defcustom vc-dired-listing-switches "-al"
607 "Switches passed to `ls' for vc-dired. MUST contain the `l' option."
608 :type 'string
609 :group 'vc
610 :version "21.1")
611
612(defcustom vc-dired-recurse t
613 "If non-nil, show directory trees recursively in VC Dired."
614 :type 'boolean
615 :group 'vc
616 :version "20.3")
617
618(defcustom vc-dired-terse-display t
619 "If non-nil, show only locked or locally modified files in VC Dired."
620 :type 'boolean
621 :group 'vc
622 :version "20.3")
623
624(defvar vc-dired-mode nil)
625(defvar vc-dired-window-configuration)
626
627(make-variable-buffer-local 'vc-dired-mode)
628
629;; The VC directory major mode. Coopt Dired for this.
630;; All VC commands get mapped into logical equivalents.
631
632(defvar vc-dired-switches)
633(defvar vc-dired-terse-mode)
634
635(defvar vc-dired-mode-map
636 (let ((map (make-sparse-keymap))
637 (vmap (make-sparse-keymap)))
638 (define-key map "\C-xv" vmap)
639 (define-key map "v" vmap)
640 (set-keymap-parent vmap vc-prefix-map)
641 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
642 map))
643
644(define-derived-mode vc-dired-mode dired-mode "Dired under VC"
645 "The major mode used in VC directory buffers.
646
647It works like Dired, but lists only files under version control, with
648the current VC state of each file being indicated in the place of the
649file's link count, owner, group and size. Subdirectories are also
650listed, and you may insert them into the buffer as desired, like in
651Dired.
652
653All Dired commands operate normally, with the exception of `v', which
654is redefined as the version control prefix, so that you can type
655`vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
656the file named in the current Dired buffer line. `vv' invokes
657`vc-next-action' on this file, or on all files currently marked.
658There is a special command, `*l', to mark all files currently locked."
659 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
660 ;; We do it here because dired might not be loaded yet
661 ;; when vc-dired-mode-map is initialized.
662 (set-keymap-parent vc-dired-mode-map dired-mode-map)
663 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
664 ;; The following is slightly modified from files.el,
665 ;; because file lines look a bit different in vc-dired-mode
666 ;; (the column before the date does not end in a digit).
667 ;; albinus: It should be done in the original declaration. Problem
668 ;; is the optional empty state-info; otherwise ")" would be good
669 ;; enough as delimeter.
670 (set (make-local-variable 'directory-listing-before-filename-regexp)
671 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
672 ;; In some locales, month abbreviations are as short as 2 letters,
673 ;; and they can be followed by ".".
674 (month (concat l l "+\\.?"))
675 (s " ")
676 (yyyy "[0-9][0-9][0-9][0-9]")
677 (dd "[ 0-3][0-9]")
678 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
679 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
680 (zone "[-+][0-2][0-9][0-5][0-9]")
681 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
682 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
683 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
684 "\\|" yyyy "-" iso-mm-dd "\\)"))
685 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
686 s "+"
687 "\\(" HH:MM "\\|" yyyy "\\)"))
688 (western-comma (concat month s "+" dd "," s "+" yyyy))
689 ;; Japanese MS-Windows ls-lisp has one-digit months, and
690 ;; omits the Kanji characters after month and day-of-month.
691 (mm "[ 0-1]?[0-9]")
692 (japanese
693 (concat mm l "?" s dd l "?" s "+"
694 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
695 ;; the .* below ensures that we find the last match on a line
696 (concat ".*" s
697 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
698 s "+")))
699 (and (boundp 'vc-dired-switches)
700 vc-dired-switches
701 (set (make-local-variable 'dired-actual-switches)
702 vc-dired-switches))
703 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
704 ;;(let ((backend-name (symbol-name (vc-responsible-backend
705 ;; default-directory))))
706 ;; (setq mode-name (concat mode-name backend-name))
707 ;; ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent.
708 ;; (let ((vc-dire-menu-map (copy-keymap vc-menu-map)))
709 ;; (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc]
710 ;; (cons backend-name vc-dire-menu-map) 'subdir)))
711 (setq vc-dired-mode t))
712
713(defun vc-dired-toggle-terse-mode ()
714 "Toggle terse display in VC Dired."
715 (interactive)
716 (if (not vc-dired-mode)
717 nil
718 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
719 (if vc-dired-terse-mode
720 (vc-dired-hook)
721 (revert-buffer))))
722
723(defun vc-dired-mark-locked ()
724 "Mark all files currently locked."
725 (interactive)
726 (dired-mark-if (let ((f (dired-get-filename nil t)))
727 (and f
728 (not (file-directory-p f))
729 (not (vc-up-to-date-p f))))
730 "locked file"))
731
732(define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
733
734(defun vc-dired-reformat-line (vc-info)
735 "Reformat a directory-listing line.
736Replace various columns with version control information, VC-INFO.
737This code, like dired, assumes UNIX -l format."
738 (beginning-of-line)
739 (when (re-search-forward
740 ;; Match link count, owner, group, size. Group may be missing,
741 ;; and only the size is present in OS/2 -l format.
742 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
743 (line-end-position) t)
744 (replace-match (substring (concat vc-info " ") 0 10)
745 t t nil 1)))
746
747(defun vc-dired-ignorable-p (filename)
748 "Should FILENAME be ignored in VC-Dired listings?"
749 (catch t
750 ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.)
751 (dolist (ignorable completion-ignored-extensions)
752 (let ((ext (substring filename
753 (- (length filename)
754 (length ignorable)))))
755 (if (string= ignorable ext) (throw t t))))
756 ;; Ignore Makefiles derived from something else
757 (when (string= (file-name-nondirectory filename) "Makefile")
758 (let* ((dir (file-name-directory filename))
759 (peers (directory-files (or dir default-directory))))
760 (if (or (member "Makefile.in" peers) (member "Makefile.am" peers))
761 (throw t t))))
762 nil))
763
764(defun vc-dired-purge ()
765 "Remove empty subdirs."
766 (goto-char (point-min))
767 (while (dired-get-subdir)
768 (forward-line 2)
769 (if (dired-get-filename nil t)
770 (if (not (dired-next-subdir 1 t))
771 (goto-char (point-max)))
772 (forward-line -2)
773 (if (not (string= (dired-current-directory) default-directory))
774 (dired-do-kill-lines t "")
775 ;; We cannot remove the top level directory.
776 ;; Just make it look a little nicer.
777 (forward-line 1)
778 (or (eobp) (kill-line))
779 (if (not (dired-next-subdir 1 t))
780 (goto-char (point-max))))))
781 (goto-char (point-min)))
782
783(defun vc-dired-buffers-for-dir (dir)
784 "Return a list of all vc-dired buffers that currently display DIR."
785 (let (result)
786 ;; Check whether dired is loaded.
787 (when (fboundp 'dired-buffers-for-dir)
788 (dolist (buffer (dired-buffers-for-dir dir))
789 (with-current-buffer buffer
790 (when vc-dired-mode
791 (push buffer result)))))
792 (nreverse result)))
793
794(defun vc-directory-resynch-file (file)
795 "Update the entries for FILE in any VC Dired buffers that list it."
796 ;;FIXME This needs to be implemented so it works for vc-dir
797 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
798 (when buffers
799 (mapcar (lambda (buffer)
800 (with-current-buffer buffer
801 (when (dired-goto-file file)
802 ;; bind vc-dired-terse-mode to nil so that
803 ;; files won't vanish when they are checked in
804 (let ((vc-dired-terse-mode nil))
805 (dired-do-redisplay 1)))))
806 buffers))))
807
808;;;###autoload
809(defun vc-directory (dir read-switches)
810 "Create a buffer in VC Dired Mode for directory DIR.
811
812See Info node `VC Dired Mode'.
813
814With prefix arg READ-SWITCHES, specify a value to override
815`dired-listing-switches' when generating the listing."
816 (interactive "DDired under VC (directory): \nP")
817 (let ((vc-dired-switches (concat vc-dired-listing-switches
818 (if vc-dired-recurse "R" ""))))
819 (if read-switches
820 (setq vc-dired-switches
821 (read-string "Dired listing switches: "
822 vc-dired-switches)))
823 (require 'dired)
824 (require 'dired-aux)
825 (switch-to-buffer
826 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
827 vc-dired-switches
828 'vc-dired-mode))))
829
b1ddeeb7 830;;; vc-dispatcher.el ends here