(Vtruncate_partial_width_windows): Improve docstring.
[bpt/emacs.git] / lisp / vc-cvs.el
index 975a24d..55a03d7 100644 (file)
@@ -1,19 +1,17 @@
 ;;; vc-cvs.el --- non-resident support for CVS version-control
 
-;; Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003,
-;;   2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
+;;   2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 
 ;; Author:      FSF (see vc.el for full credits)
 ;; Maintainer:  Andre Spiegel <spiegel@gnu.org>
 
-;; $Id$
-
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -21,9 +19,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 ;; new functions when we reload this file.
 (put 'CVS 'vc-functions nil)
 
+;;; Properties of the backend.
+
+(defun vc-cvs-revision-granularity () 'file)
+
+(defun vc-cvs-checkout-model (files)
+  "CVS-specific version of `vc-checkout-model'."
+  (if (getenv "CVSREAD")
+      'announce
+    (let* ((file (if (consp files) (car files) files))
+           (attrib (file-attributes file)))
+      (or (vc-file-getprop file 'vc-checkout-model)
+          (vc-file-setprop
+           file 'vc-checkout-model
+           (if (and attrib ;; don't check further if FILE doesn't exist
+                    ;; If the file is not writable (despite CVSREAD being
+                    ;; undefined), this is probably because the file is being
+                    ;; "watched" by other developers.
+                    ;; (If vc-mistrust-permissions was t, we actually shouldn't
+                    ;; trust this, but there is no other way to learn this from
+                    ;; CVS at the moment (version 1.9).)
+                    (string-match "r-..-..-." (nth 8 attrib)))
+               'announce
+             'implicit))))))
+
 ;;;
 ;;; Customization options
 ;;;
 
 (defcustom vc-cvs-global-switches nil
-  "*Global switches to pass to any CVS command."
+  "Global switches to pass to any CVS command."
   :type '(choice (const :tag "None" nil)
                 (string :tag "Argument String")
                 (repeat :tag "Argument List"
   :group 'vc)
 
 (defcustom vc-cvs-register-switches nil
-  "*Extra switches for registering a file into CVS.
+  "Switches for registering a file into CVS.
 A string or list of strings passed to the checkin program by
-\\[vc-register]."
-  :type '(choice (const :tag "None" nil)
+\\[vc-register].  If nil, use the value of `vc-register-switches'.
+If t, use no switches."
+  :type '(choice (const :tag "Unspecified" nil)
+                (const :tag "None" t)
                 (string :tag "Argument String")
-                (repeat :tag "Argument List"
-                        :value ("")
-                        string))
+                (repeat :tag "Argument List" :value ("") string))
   :version "21.1"
   :group 'vc)
 
 (defcustom vc-cvs-diff-switches nil
-  "*A string or list of strings specifying extra switches for cvs diff under VC."
-    :type '(choice (const :tag "None" nil)
-                (string :tag "Argument String")
-                (repeat :tag "Argument List"
-                        :value ("")
-                        string))
+  "String or list of strings specifying switches for CVS diff under VC.
+If nil, use the value of `vc-diff-switches'.  If t, use no switches."
+  :type '(choice (const :tag "Unspecified" nil)
+                 (const :tag "None" t)
+                 (string :tag "Argument String")
+                 (repeat :tag "Argument List" :value ("") string))
   :version "21.1"
   :group 'vc)
 
 (defcustom vc-cvs-header (or (cdr (assoc 'CVS vc-header-alist)) '("\$Id\$"))
-  "*Header keywords to be inserted by `vc-insert-headers'."
+  "Header keywords to be inserted by `vc-insert-headers'."
   :version "21.1"
   :type '(repeat string)
   :group 'vc)
 
 (defcustom vc-cvs-use-edit t
-  "*Non-nil means to use `cvs edit' to \"check out\" a file.
+  "Non-nil means to use `cvs edit' to \"check out\" a file.
 This is only meaningful if you don't use the implicit checkout model
 \(i.e. if you have $CVSREAD set)."
   :type 'boolean
   :version "21.1"
   :group 'vc)
 
-(defcustom vc-cvs-stay-local t
-  "*Non-nil means use local operations when possible for remote repositories.
+(defcustom vc-cvs-stay-local 'only-file
+  "Non-nil means use local operations when possible for remote repositories.
 This avoids slow queries over the network and instead uses heuristics
 and past information to determine the current status of a file.
 
+If value is the symbol `only-file' `vc-dir' will connect to the
+server, but heuristics will be used to determine the status for
+all other VC operations.
+
 The value can also be a regular expression or list of regular
 expressions to match against the host name of a repository; then VC
 only stays local for hosts that match it.  Alternatively, the value
@@ -97,16 +121,20 @@ can be a list of regular expressions where the first element is the
 symbol `except'; then VC always stays local except for hosts matched
 by these regular expressions."
   :type '(choice (const :tag "Always stay local" t)
-                (const :tag "Don't stay local" nil)
-                 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
-                       (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
-                       (regexp :format " stay local,\n%t: %v" :tag "if it matches")
+                (const :tag "Only for file operations" only-file)
+                (const :tag "Don't stay local" nil)
+                 (list :format "\nExamine hostname and %v"
+                       :tag "Examine hostname ..."
+                       (set :format "%v" :inline t
+                            (const :format "%t" :tag "don't" except))
+                       (regexp :format " stay local,\n%t: %v"
+                               :tag "if it matches")
                        (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
-  :version "21.1"
+  :version "23.1"
   :group 'vc)
 
 (defcustom vc-cvs-sticky-date-format-string "%c"
-  "*Format string for mode-line display of sticky date.
+  "Format string for mode-line display of sticky date.
 Format is according to `format-time-string'.  Only used if
 `vc-cvs-sticky-tag-display' is t."
   :type '(string)
@@ -114,7 +142,7 @@ Format is according to `format-time-string'.  Only used if
   :group 'vc)
 
 (defcustom vc-cvs-sticky-tag-display t
-  "*Specify the mode-line display of sticky tags.
+  "Specify the mode-line display of sticky tags.
 Value t means default display, nil means no display at all.  If the
 value is a function or macro, it is called with the sticky tag and
 its' type as parameters, in that order.  TYPE can have three different
@@ -174,22 +202,21 @@ See also variable `vc-cvs-sticky-date-format-string'."
         ;; make sure that the file name is searched case-sensitively
         (case-fold-search nil))
     (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
-       (with-temp-buffer
-          (vc-cvs-get-entries dirname)
-          (goto-char (point-min))
-         (cond
-          ((re-search-forward
-            ;; CVS-removed files are not taken under VC control.
-            (concat "^/" (regexp-quote basename) "/[^/-]") nil t)
-           (beginning-of-line)
-           (vc-cvs-parse-entry file)
-           t)
-          (t nil)))
+        (or (string= basename "")
+            (with-temp-buffer
+              (vc-cvs-get-entries dirname)
+              (goto-char (point-min))
+              (cond ((re-search-forward
+                      (concat "^/" (regexp-quote basename) "/[^/]") nil t)
+                     (beginning-of-line)
+                     (vc-cvs-parse-entry file)
+                     t)
+                    (t nil))))
       nil)))
 
 (defun vc-cvs-state (file)
   "CVS-specific version of `vc-state'."
-  (if (vc-stay-local-p file)
+  (if (vc-stay-local-p file 'CVS)
       (let ((state (vc-file-getprop file 'vc-state)))
         ;; If we should stay local, use the heuristic but only if
         ;; we don't have a more precise state already available.
@@ -198,7 +225,8 @@ See also variable `vc-cvs-sticky-date-format-string'."
          state))
     (with-temp-buffer
       (cd (file-name-directory file))
-      (vc-cvs-command t 0 file "status")
+      (let (process-file-side-effects)
+       (vc-cvs-command t 0 file "status"))
       (vc-cvs-parse-status t))))
 
 (defun vc-cvs-state-heuristic (file)
@@ -207,28 +235,11 @@ See also variable `vc-cvs-sticky-date-format-string'."
   ;; Otherwise consider it `edited'.
   (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
         (lastmod (nth 5 (file-attributes file))))
-    (if (equal checkout-time lastmod)
-        'up-to-date
-      'edited)))
-
-(defun vc-cvs-dir-state (dir)
-  "Find the CVS state of all files in DIR and subdirectories."
-  ;; if DIR is not under CVS control, don't do anything.
-  (when (file-readable-p (expand-file-name "CVS/Entries" dir))
-    (if (vc-stay-local-p dir)
-       (vc-cvs-dir-state-heuristic dir)
-      (let ((default-directory dir))
-       ;; Don't specify DIR in this command, the default-directory is
-       ;; enough.  Otherwise it might fail with remote repositories.
-       (with-temp-buffer
-         (buffer-disable-undo)         ;; Because these buffers can get huge
-         (vc-cvs-command t 0 nil "status")
-         (goto-char (point-min))
-         (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t)
-           (narrow-to-region (match-beginning 0) (match-end 0))
-           (vc-cvs-parse-status)
-           (goto-char (point-max))
-           (widen)))))))
+    (cond
+     ((equal checkout-time lastmod) 'up-to-date)
+     ((string= (vc-working-revision file) "0") 'added)
+     ((null checkout-time) 'unregistered)
+     (t 'edited))))
 
 (defun vc-cvs-working-revision (file)
   "CVS-specific version of `vc-working-revision'."
@@ -238,22 +249,6 @@ See also variable `vc-cvs-sticky-date-format-string'."
   (vc-cvs-registered file)
   (vc-file-getprop file 'vc-working-revision))
 
-(defun vc-cvs-checkout-model (file)
-  "CVS-specific version of `vc-checkout-model'."
-  (if (getenv "CVSREAD")
-      'announce
-    (let ((attrib (file-attributes file)))
-      (if (and attrib ;; don't check further if FILE doesn't exist
-               ;; If the file is not writable (despite CVSREAD being
-               ;; undefined), this is probably because the file is being
-               ;; "watched" by other developers.
-               ;; (If vc-mistrust-permissions was t, we actually shouldn't
-               ;; trust this, but there is no other way to learn this from CVS
-               ;; at the moment (version 1.9).)
-               (string-match "r-..-..-." (nth 8 attrib)))
-          'announce
-        'implicit))))
-
 (defun vc-cvs-mode-line-string (file)
   "Return string for placement into the modeline for FILE.
 Compared to the default implementation, this function does two things:
@@ -261,32 +256,19 @@ Handle the special case of a CVS file that is added but not yet
 committed and support display of sticky tags."
   (let* ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
         help-echo
-        (string 
-         (if (string= (vc-working-revision file) "0")
-             ;; A file that is added but not yet committed.
-             (progn
-               (setq help-echo "Added file (needs commit) under CVS")
-               "CVS @@")
-           (let ((def-ml (vc-default-mode-line-string 'CVS file)))
-             (setq help-echo 
-                   (get-text-property 0 'help-echo def-ml))
-             def-ml))))
-    (propertize 
+        (string
+          (let ((def-ml (vc-default-mode-line-string 'CVS file)))
+            (setq help-echo
+                  (get-text-property 0 'help-echo def-ml))
+            def-ml)))
+    (propertize
      (if (zerop (length sticky-tag))
         string
-       (setq help-echo (format "%s on the '%s' branch" 
+       (setq help-echo (format "%s on the '%s' branch"
                               help-echo sticky-tag))
        (concat string "[" sticky-tag "]"))
      'help-echo help-echo)))
 
-(defun vc-cvs-dired-state-info (file)
-  "CVS-specific version of `vc-dired-state-info'."
-  (let ((cvs-state (vc-state file)))
-    (cond ((eq cvs-state 'edited)
-          (if (equal (vc-working-revision file) "0")
-              "(added)" "(modified)"))
-         (t
-          (vc-default-dired-state-info 'CVS file)))))
 
 ;;;
 ;;; State-changing functions
@@ -295,9 +277,8 @@ committed and support display of sticky tags."
 (defun vc-cvs-register (files &optional rev comment)
   "Register FILES into the CVS version-control system.
 COMMENT can be used to provide an initial description of FILES.
-
-`vc-register-switches' and `vc-cvs-register-switches' are passed to
-the CVS command (in that order)."
+Passes either `vc-cvs-register-switches' or `vc-register-switches'
+to the CVS command."
   ;; Register the directories if needed.
   (let (dirs)
     (dolist (file files)
@@ -326,9 +307,10 @@ its parents."
     (while (and (stringp dir)
                 (not (equal dir (setq dir (file-name-directory dir))))
                 dir)
-      (setq dir (if (file-directory-p
+      (setq dir (if (file-exists-p
                      (expand-file-name "CVS/Entries" dir))
-                    t (directory-file-name dir))))
+                    t
+                  (directory-file-name dir))))
     (eq dir t)))
 
 (defun vc-cvs-checkin (files rev comment)
@@ -369,7 +351,7 @@ its parents."
        (vc-file-setprop
         (car files) 'vc-working-revision
         (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
-      (mapc (lambda (file) (vc-file-clearprops file)) files))
+      (mapc 'vc-file-clearprops files))
     ;; Anyway, forget the checkout model of the file, because we might have
     ;; guessed wrong when we found the file.  After commit, we can
     ;; tell it from the permissions of the file (see
@@ -402,7 +384,7 @@ REV is the revision to check out."
     (if (and (file-exists-p file) (not rev))
         ;; If no revision was specified, just make the file writable
         ;; if necessary (using `cvs-edit' if requested).
-        (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
+        (and editable (not (eq (vc-cvs-checkout-model (list file)) 'implicit))
              (if vc-cvs-use-edit
                  (vc-cvs-command nil 0 file "edit")
                (set-file-modes file (logior (file-modes file) 128))
@@ -421,17 +403,16 @@ REV is the revision to check out."
                      "-A"
                    (concat "-r" rev))))
              (vc-switches 'CVS 'checkout)))
-    (vc-mode-line file))
+    (vc-mode-line file 'CVS))
   (message "Checking out %s...done" file))
 
 (defun vc-cvs-delete-file (file)
-  (vc-cvs-command nil 0 file "remove" "-f")
-  (vc-cvs-command nil 0 file "commit" "-mRemoved."))
+  (vc-cvs-command nil 0 file "remove" "-f"))
 
 (defun vc-cvs-revert (file &optional contents-done)
   "Revert FILE to the working revision on which it was based."
   (vc-default-revert 'CVS file contents-done)
-  (unless (eq (vc-checkout-model file) 'implicit)
+  (unless (eq (vc-cvs-checkout-model (list file)) 'implicit)
     (if vc-cvs-use-edit
         (vc-cvs-command nil 0 file "unedit")
       ;; Make the file read-only by switching off all w-bits
@@ -448,8 +429,13 @@ The changes are between FIRST-REVISION and SECOND-REVISION."
   (with-current-buffer (get-buffer "*vc*")
     (goto-char (point-min))
     (if (re-search-forward "conflicts during merge" nil t)
-        1                              ; signal error
-      0)))                             ; signal success
+       (progn
+         (vc-file-setprop file 'vc-state 'conflict)
+         ;; signal error
+         1)
+      (vc-file-setprop file 'vc-state 'edited)
+      ;; signal success
+      0)))
 
 (defun vc-cvs-merge-news (file)
   "Merge in any new changes made to FILE."
@@ -472,7 +458,9 @@ The changes are between FIRST-REVISION and SECOND-REVISION."
             0 ;; there were no news; indicate success
           (if (re-search-forward
                (concat "^\\([CMUP] \\)?"
-                       (regexp-quote (file-name-nondirectory file))
+                       (regexp-quote
+                        (substring file (length (expand-file-name
+                                                 "." default-directory))))
                        "\\( already contains the differences between \\)?")
                nil t)
               (cond
@@ -490,7 +478,7 @@ The changes are between FIRST-REVISION and SECOND-REVISION."
                 0);; indicate success to the caller
                ;; Conflicts detected!
                (t
-                (vc-file-setprop file 'vc-state 'edited)
+                (vc-file-setprop file 'vc-state 'conflict)
                 1);; signal the error to the caller
                )
             (pop-to-buffer "*vc*")
@@ -498,31 +486,36 @@ The changes are between FIRST-REVISION and SECOND-REVISION."
       (message "Merging changes into %s...done" file))))
 
 (defun vc-cvs-modify-change-comment (files rev comment)
-  "Modify the change comments for FILES on a specified REV. 
+  "Modify the change comments for FILES on a specified REV.
 Will fail unless you have administrative privileges on the repo."
-  (vc-cvs-command nil 0 files "rcs" (concat "-m" comment ":" rev)))
+  (vc-cvs-command nil 0 files "admin" (concat "-m" rev ":" comment)))
 
 ;;;
 ;;; History functions
 ;;;
 
+(declare-function vc-rcs-print-log-cleanup "vc-rcs" ())
+
 (defun vc-cvs-print-log (files &optional buffer)
   "Get change logs associated with FILES."
+  (require 'vc-rcs)
   ;; It's just the catenation of the individual logs.
   (vc-cvs-command
    buffer
-   (if (vc-stay-local-p files) 'async 0)
-   files "log"))
+   (if (vc-stay-local-p files 'CVS) 'async 0)
+   files "log")
+  (with-current-buffer buffer
+    (vc-exec-after (vc-rcs-print-log-cleanup))))
 
-(defun vc-cvs-wash-log ()
-  "Remove all non-comment information from log output."
-  (vc-call-backend 'RCS 'wash-log)
-  nil)
+(defun vc-cvs-comment-history (file)
+  "Get comment history of a file."
+  (vc-call-backend 'RCS 'comment-history file))
 
 (defun vc-cvs-diff (files &optional oldvers newvers buffer)
   "Get a difference report using CVS between two revisions of FILE."
-  (let* ((async (and (not vc-disable-async-diff)
-                    (vc-stay-local-p files)))
+  (let* (process-file-side-effects
+        (async (and (not vc-disable-async-diff)
+                    (vc-stay-local-p files 'CVS)))
         (invoke-cvs-diff-list nil)
         status)
     ;; Look through the file list and see if any files have backups
@@ -541,13 +534,12 @@ Will fail unless you have administrative privileges on the repo."
              (coding-system-for-read (vc-coding-system-for-diff file)))
          (if (and file-oldvers file-newvers)
              (progn
+               ;; This used to append diff-switches and vc-diff-switches,
+               ;; which was consistent with the vc-diff-switches doc at that
+               ;; time, but not with the actual behavior of any other VC diff.
                (apply 'vc-do-command (or buffer "*vc-diff*") 1 "diff" nil
-                      (append (if (listp diff-switches)
-                                  diff-switches
-                                (list diff-switches))
-                              (if (listp vc-diff-switches)
-                                  vc-diff-switches
-                                (list vc-diff-switches))
+                      ;; Not a CVS diff, does not use vc-cvs-diff-switches.
+                      (append (vc-switches nil 'diff)
                               (list (file-relative-name file-oldvers)
                                     (file-relative-name file-newvers))))
                (setq status 0))
@@ -561,31 +553,6 @@ Will fail unless you have administrative privileges on the repo."
                          (vc-switches 'CVS 'diff))))
     (if async 1 status))) ; async diff, pessimistic assumption
 
-
-(defun vc-cvs-diff-tree (dir &optional rev1 rev2)
-  "Diff all files at and below DIR."
-  (with-current-buffer "*vc-diff*"
-    (setq default-directory dir)
-    (if (vc-stay-local-p dir)
-        ;; local diff: do it filewise, and only for files that are modified
-        (vc-file-tree-walk
-         dir
-         (lambda (f)
-           (vc-exec-after
-            `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
-               ;; possible optimization: fetch the state of all files
-               ;; in the tree via vc-cvs-dir-state-heuristic
-               (unless (vc-up-to-date-p ',f)
-                 (message "Looking at %s" ',f)
-                 (vc-diff-internal ',f ',rev1 ',rev2))))))
-      ;; cvs diff: use a single call for the entire tree
-      (let ((coding-system-for-read
-             (or coding-system-for-read 'undecided)))
-        (apply 'vc-cvs-command "*vc-diff*" 1 nil "diff"
-               (and rev1 (concat "-r" rev1))
-               (and rev2 (concat "-r" rev2))
-               (vc-switches 'CVS 'diff))))))
-
 (defconst vc-cvs-annotate-first-line-re "^[0-9]")
 
 (defun vc-cvs-annotate-process-filter (process string)
@@ -601,7 +568,7 @@ Will fail unless you have administrative privileges on the repo."
   "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
 Optional arg REVISION is a revision to annotate from."
   (vc-cvs-command buffer
-                  (if (vc-stay-local-p file)
+                  (if (vc-stay-local-p file 'CVS)
                      'async 0)
                   file "annotate"
                   (if revision (concat "-r" revision)))
@@ -617,6 +584,8 @@ Optional arg REVISION is a revision to annotate from."
         (re-search-forward vc-cvs-annotate-first-line-re)
         (delete-region (point-min) (1- (point)))))))
 
+(declare-function vc-annotate-convert-time "vc-annotate" (time))
+
 (defun vc-cvs-annotate-current-time ()
   "Return the current time, based at midnight of the current day, and
 encoded as fractional days."
@@ -669,20 +638,30 @@ systime, or nil if there is none."
        (match-string-no-properties 1)
       nil)))
 
+(defun vc-cvs-previous-revision (file rev)
+  (vc-call-backend 'RCS 'previous-revision file rev))
+
+(defun vc-cvs-next-revision (file rev)
+  (vc-call-backend 'RCS 'next-revision file rev))
+
+;; FIXME: This should probably be replaced by code using cvs2cl.
+(defun vc-cvs-update-changelog (files)
+  (vc-call-backend 'RCS 'update-changelog files))
+
 ;;;
-;;; Snapshot system
+;;; Tag system
 ;;;
 
-(defun vc-cvs-create-snapshot (dir name branchp)
+(defun vc-cvs-create-tag (dir name branchp)
   "Assign to DIR's current revision a given NAME.
 If BRANCHP is non-nil, the name is created as a branch (and the current
 workspace is immediately moved to that new branch)."
   (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
   (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
 
-(defun vc-cvs-retrieve-snapshot (dir name update)
-  "Retrieve a snapshot at and below DIR.
-NAME is the name of the snapshot; if it is empty, do a `cvs update'.
+(defun vc-cvs-retrieve-tag (dir name update)
+  "Retrieve a tag at and below DIR.
+NAME is the name of the tag; if it is empty, do a `cvs update'.
 If UPDATE is non-nil, then update (resynch) any affected buffers."
   (with-current-buffer (get-buffer-create "*vc*")
     (let ((default-directory dir)
@@ -721,8 +700,9 @@ If UPDATE is non-nil, then update (resynch) any affected buffers."
 ;;; Miscellaneous
 ;;;
 
-(defalias 'vc-cvs-make-version-backups-p 'vc-stay-local-p
-  "Return non-nil if version backups should be made for FILE.")
+(defun vc-cvs-make-version-backups-p (file)
+  "Return non-nil if version backups should be made for FILE."
+  (vc-stay-local-p file 'CVS))
 
 (defun vc-cvs-check-headers ()
   "Check if the current file has any headers in it."
@@ -736,20 +716,18 @@ If UPDATE is non-nil, then update (resynch) any affected buffers."
 ;;; Internal functions
 ;;;
 
-(defun vc-cvs-root (dir)
-  (vc-find-root dir "CVS" t))
-
 (defun vc-cvs-command (buffer okstatus files &rest flags)
   "A wrapper around `vc-do-command' for use in vc-cvs.el.
 The difference to vc-do-command is that this function always invokes `cvs',
 and that it passes `vc-cvs-global-switches' to it before FLAGS."
-  (apply 'vc-do-command buffer okstatus "cvs" files
+  (apply 'vc-do-command (or buffer "*vc*") okstatus "cvs" files
          (if (stringp vc-cvs-global-switches)
              (cons vc-cvs-global-switches flags)
            (append vc-cvs-global-switches
                    flags))))
 
-(defalias 'vc-cvs-stay-local-p 'vc-stay-local-p)  ;Back-compatibility.
+(defun vc-cvs-stay-local-p (file)  ;Back-compatibility.
+  (vc-stay-local-p file 'CVS))
 
 (defun vc-cvs-repository-hostname (dirname)
   "Hostname of the CVS server associated to workarea DIRNAME."
@@ -765,10 +743,16 @@ and that it passes `vc-cvs-global-switches' to it before FLAGS."
                (buffer-substring (point)
                                  (line-end-position))))))))
 
+(defun vc-cvs-parse-uhp (path)
+  "parse user@host/path into (user@host /path)"
+  (if (string-match "\\([^/]+\\)\\(/.*\\)" path)
+      (list (match-string 1 path) (match-string 2 path))
+      (list nil path)))
+
 (defun vc-cvs-parse-root (root)
   "Split CVS ROOT specification string into a list of fields.
 A CVS root specification of the form
-  [:METHOD:][[USER@]HOSTNAME:]/path/to/repository
+  [:METHOD:][[USER@]HOSTNAME]:?/path/to/repository
 is converted to a normalized record with the following structure:
   \(METHOD USER HOSTNAME CVS-ROOT).
 The default METHOD for a CVS root of the form
@@ -790,17 +774,16 @@ For an empty string, nil is returned (invalid CVS root)."
             ;; Invalid CVS root
             nil)
            ((= len 1)
-            ;; Simple PATH => method `local'
-            (cons "local"
-                  (cons nil root-list)))
+            (let ((uhp (vc-cvs-parse-uhp (car root-list))))
+              (cons (if (car uhp) "ext" "local") uhp)))
            ((= len 2)
             ;; [USER@]HOST:PATH => method `ext'
             (and (not (equal (car root-list) ""))
                  (cons "ext" root-list)))
            ((= len 3)
-            ;; :METHOD:PATH
+            ;; :METHOD:PATH or :METHOD:USER@HOSTNAME/PATH
             (cons (cadr root-list)
-                  (cons nil (cddr root-list))))
+                  (vc-cvs-parse-uhp (caddr root-list))))
            (t
             ;; :METHOD:[USER@]HOST:PATH
             (cdr root-list)))))
@@ -824,56 +807,258 @@ For an empty string, nil is returned (invalid CVS root)."
           ;; Normalize CVS root record
           (list method user host root)))))
 
+;; XXX: This does not work correctly for subdirectories.  "cvs status"
+;; information is context sensitive, it contains lines like:
+;; cvs status: Examining DIRNAME
+;; and the file entries after that don't show the full path.
+;; Because of this VC directory listings only show changed files
+;; at the top level for CVS.
 (defun vc-cvs-parse-status (&optional full)
   "Parse output of \"cvs status\" command in the current buffer.
 Set file properties accordingly.  Unless FULL is t, parse only
 essential information. Note that this can never set the 'ignored
 state."
-  (let (file status)
+  (let (file status missing)
     (goto-char (point-min))
     (while (looking-at "? \\(.*\\)")
       (setq file (expand-file-name (match-string 1)))
       (vc-file-setprop file 'vc-state 'unregistered)
       (forward-line 1))
-    (if (re-search-forward "^File: " nil t)
-        (cond
-         ((looking-at "no file") nil)
-         ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
-         (setq file (expand-file-name (match-string 1)))
-          (vc-file-setprop file 'vc-backend 'CVS)
-          (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
-              (setq status "Unknown")
-            (setq status (match-string 1)))
-          (if (and full
-                   (re-search-forward
-                    "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
+    (when (re-search-forward "^File: " nil t)
+      (when (setq missing (looking-at "no file "))
+       (goto-char (match-end 0)))
+      (cond
+       ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
+       (setq file (expand-file-name (match-string 1)))
+       (setq status(if (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t)
+                        (match-string 1) "Unknown"))
+       (when (and full
+                  (re-search-forward
+                   "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
 \[\t ]+\\([0-9.]+\\)"
-                    nil t))
-              (vc-file-setprop file 'vc-latest-revision (match-string 2)))
-          (vc-file-setprop
-           file 'vc-state
-           (cond
-            ((string-match "Up-to-date" status)
-             (vc-file-setprop file 'vc-checkout-time
-                              (nth 5 (file-attributes file)))
-             'up-to-date)
-            ((string-match "Locally Modified" status)             'edited)
-            ((string-match "Needs Merge" status)                  'needs-merge)
-            ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
-            (t 'edited))))))))
-
-(defun vc-cvs-dir-state-heuristic (dir)
-  "Find the CVS state of all files in DIR, using only local information."
-  (with-temp-buffer
-    (vc-cvs-get-entries dir)
+                   nil t))
+           (vc-file-setprop file 'vc-latest-revision (match-string 2)))
+       (vc-file-setprop
+        file 'vc-state
+        (cond
+         ((string-match "Up-to-date" status)
+          (vc-file-setprop file 'vc-checkout-time
+                           (nth 5 (file-attributes file)))
+          'up-to-date)
+         ((string-match "Locally Modified" status)             'edited)
+         ((string-match "Needs Merge" status)                  'needs-merge)
+         ((string-match "Needs \\(Checkout\\|Patch\\)" status)
+          (if missing 'missing 'needs-update))
+         ((string-match "Locally Added" status)                'added)
+         ((string-match "Locally Removed" status)              'removed)
+         ((string-match "File had conflicts " status)          'conflict)
+          ((string-match "Unknown" status)                     'unregistered)
+         (t 'edited))))))))
+
+(defun vc-cvs-after-dir-status (update-function)
+  ;; Heavily inspired by vc-cvs-parse-status. AKA a quick hack.
+  ;; This needs a lot of testing.
+  (let ((status nil)
+       (status-str nil)
+       (file nil)
+       (result nil)
+       (missing nil)
+       (ignore-next nil)
+       (subdir default-directory))
     (goto-char (point-min))
-    (while (not (eobp))
-      ;; CVS-removed files are not taken under VC control.
-      (when (looking-at "/\\([^/]*\\)/[^/-]")
-       (let ((file (expand-file-name (match-string 1) dir)))
-         (unless (vc-file-getprop file 'vc-state)
-           (vc-cvs-parse-entry file t))))
-      (forward-line 1))))
+    (while
+       ;; Look for either a file entry, an unregistered file, or a
+       ;; directory change.
+       (re-search-forward
+        "\\(^=+\n\\([^=c?\n].*\n\\|\n\\)+\\)\\|\\(\\(^?? .*\n\\)+\\)\\|\\(^cvs status: \\(Examining\\|nothing\\) .*\n\\)"
+        nil t)
+      ;; FIXME: get rid of narrowing here.
+      (narrow-to-region (match-beginning 0) (match-end 0))
+      (goto-char (point-min))
+      ;; The subdir
+      (when (looking-at "cvs status: Examining \\(.+\\)")
+       (setq subdir (expand-file-name (match-string 1))))
+      ;; Unregistered files
+      (while (looking-at "? \\(.*\\)")
+       (setq file (file-relative-name
+                   (expand-file-name (match-string 1) subdir)))
+       (push (list file 'unregistered) result)
+       (forward-line 1))
+      (when (looking-at "cvs status: nothing known about")
+       ;; We asked about a non existent file.  The output looks like this:
+
+       ;; cvs status: nothing known about `lisp/v.diff'
+       ;; ===================================================================
+       ;; File: no file v.diff            Status: Unknown
+       ;;
+       ;;    Working revision:    No entry for v.diff
+       ;;    Repository revision: No revision control file
+       ;;
+
+       ;; Due to narrowing in this iteration we only see the "cvs
+       ;; status:" line, so just set a flag so that we can ignore the
+       ;; file in the next iteration.
+       (setq ignore-next t))
+      ;; A file entry.
+      (when (re-search-forward "^File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: \\(.*\\)" nil t)
+       (setq missing (match-string 1))
+       (setq file (file-relative-name
+                   (expand-file-name (match-string 2) subdir)))
+       (setq status-str (match-string 3))
+       (setq status
+             (cond
+              ((string-match "Up-to-date" status-str) 'up-to-date)
+              ((string-match "Locally Modified" status-str) 'edited)
+              ((string-match "Needs Merge" status-str) 'needs-merge)
+              ((string-match "Needs \\(Checkout\\|Patch\\)" status-str)
+               (if missing 'missing 'needs-update))
+              ((string-match "Locally Added" status-str) 'added)
+              ((string-match "Locally Removed" status-str) 'removed)
+              ((string-match "File had conflicts " status-str) 'conflict)
+              ((string-match "Unknown" status-str) 'unregistered)
+              (t 'edited)))
+       (if ignore-next
+           (setq ignore-next nil)
+         (unless (eq status 'up-to-date)
+           (push (list file status) result))))
+      (goto-char (point-max))
+      (widen))
+    (funcall update-function result))
+  ;; Alternative implementation: use the "update" command instead of
+  ;; the "status" command.
+  ;; (let ((result nil)
+  ;;   (translation '((?? . unregistered)
+  ;;                  (?A . added)
+  ;;                  (?C . conflict)
+  ;;                  (?M . edited)
+  ;;                  (?P . needs-merge)
+  ;;                  (?R . removed)
+  ;;                  (?U . needs-update))))
+  ;;   (goto-char (point-min))
+  ;;   (while (not (eobp))
+  ;;     (if (looking-at "^[ACMPRU?] \\(.*\\)$")
+  ;;     (push (list (match-string 1)
+  ;;                 (cdr (assoc (char-after) translation)))
+  ;;           result)
+  ;;   (cond
+  ;;    ((looking-at "cvs update: warning: \\(.*\\) was lost")
+  ;;     ;; Format is:
+  ;;     ;; cvs update: warning: FILENAME was lost
+  ;;     ;; U FILENAME
+  ;;     (push (list (match-string 1) 'missing) result)
+  ;;     ;; Skip the "U" line
+  ;;     (forward-line 1))
+  ;;    ((looking-at "cvs update: New directory `\\(.*\\)' -- ignored")
+  ;;     (push (list (match-string 1) 'unregistered) result))))
+  ;;     (forward-line 1))
+  ;;   (funcall update-function result)))
+  )
+
+;; Based on vc-cvs-dir-state-heuristic from Emacs 22.
+(defun vc-cvs-dir-status-heuristic (dir update-function &optional basedir)
+  "Find the CVS state of all files in DIR, using only local information."
+  (let (file basename status result dirlist)
+    (with-temp-buffer
+      (vc-cvs-get-entries dir)
+      (goto-char (point-min))
+      (while (not (eobp))
+        (if (looking-at "D/\\([^/]*\\)////")
+            (push (expand-file-name (match-string 1) dir) dirlist)
+          ;; CVS-removed files are not taken under VC control.
+          (when (looking-at "/\\([^/]*\\)/[^/-]")
+            (setq basename (match-string 1)
+                  file (expand-file-name basename dir)
+                  status (or (vc-file-getprop file 'vc-state)
+                             (vc-cvs-parse-entry file t)))
+            (unless (eq status 'up-to-date)
+              (push (list (if basedir
+                              (file-relative-name file basedir)
+                            basename)
+                          status) result))))
+        (forward-line 1)))
+    (dolist (subdir dirlist)
+      (setq result (append result
+                           (vc-cvs-dir-status-heuristic subdir nil
+                                                        (or basedir dir)))))
+    (if basedir result
+      (funcall update-function result))))
+
+(defun vc-cvs-dir-status (dir update-function)
+  "Create a list of conses (file . state) for DIR."
+  ;; FIXME check all files in DIR instead?
+  (let ((local (vc-stay-local-p dir 'CVS)))
+    (if (and local (not (eq local 'only-file)))
+       (vc-cvs-dir-status-heuristic dir update-function)
+      (vc-cvs-command (current-buffer) 'async dir "-f" "status")
+      ;; Alternative implementation: use the "update" command instead of
+      ;; the "status" command.
+      ;; (vc-cvs-command (current-buffer) 'async
+      ;;                 (file-relative-name dir)
+      ;;                 "-f" "-n" "update" "-d" "-P")
+      (vc-exec-after
+       `(vc-cvs-after-dir-status (quote ,update-function))))))
+
+(defun vc-cvs-dir-status-files (dir files default-state update-function)
+  "Create a list of conses (file . state) for DIR."
+  (apply 'vc-cvs-command (current-buffer) 'async dir "-f" "status" files)
+  (vc-exec-after
+   `(vc-cvs-after-dir-status (quote ,update-function))))
+
+(defun vc-cvs-file-to-string (file)
+  "Read the content of FILE and return it as a string."
+  (condition-case nil
+      (with-temp-buffer
+       (insert-file-contents file)
+       (goto-char (point-min))
+       (buffer-substring (point) (point-max)))
+    (file-error nil)))
+
+(defun vc-cvs-dir-extra-headers (dir)
+  "Extract and represent per-directory properties of a CVS working copy."
+  (let ((repo
+        (condition-case nil
+            (with-temp-buffer
+              (insert-file-contents "CVS/Root")
+              (goto-char (point-min))
+              (and (looking-at ":ext:") (delete-char 5))
+              (concat (buffer-substring (point) (1- (point-max))) "\n"))
+          (file-error nil)))
+       (module
+        (condition-case nil
+            (with-temp-buffer
+              (insert-file-contents "CVS/Repository")
+              (goto-char (point-min))
+              (skip-chars-forward "^\n")
+              (concat (buffer-substring (point-min) (point)) "\n"))
+          (file-error nil))))
+    (concat
+     (cond (repo
+           (concat (propertize "Repository : " 'face 'font-lock-type-face)
+                    (propertize repo 'face 'font-lock-variable-name-face)))
+          (t ""))
+     (cond (module
+           (concat (propertize "Module     : " 'face 'font-lock-type-face)
+                    (propertize module 'face 'font-lock-variable-name-face)))
+          (t ""))
+     (if (file-readable-p "CVS/Tag")
+        (let ((tag (vc-cvs-file-to-string "CVS/Tag")))
+          (cond
+           ((string-match "\\`T" tag)
+            (concat (propertize "Tag        : " 'face 'font-lock-type-face)
+                    (propertize (substring tag 1)
+                                'face 'font-lock-variable-name-face)))
+           ((string-match "\\`D" tag)
+            (concat (propertize "Date       : " 'face 'font-lock-type-face)
+                    (propertize (substring tag 1)
+                                'face 'font-lock-variable-name-face)))
+           (t ""))))
+
+     ;; In CVS, branch is a per-file property, not a per-directory property.
+     ;; We can't really do this here without making dangerous assumptions.
+     ;;(propertize "Branch:     " 'face 'font-lock-type-face)
+     ;;(propertize "ADD CODE TO PRINT THE BRANCH NAME\n"
+     ;;         'face 'font-lock-warning-face)
+     )))
 
 (defun vc-cvs-get-entries (dir)
   "Insert the CVS/Entries file from below DIR into the current buffer.
@@ -956,10 +1141,9 @@ is non-nil."
   (cond
    ;; entry for a "locally added" file (not yet committed)
    ((looking-at "/[^/]+/0/")
-    (vc-file-setprop file 'vc-backend 'CVS)
     (vc-file-setprop file 'vc-checkout-time 0)
     (vc-file-setprop file 'vc-working-revision "0")
-    (if set-state (vc-file-setprop file 'vc-state 'edited)))
+    (if set-state (vc-file-setprop file 'vc-state 'added)))
    ;; normal entry
    ((looking-at
      (concat "/[^/]+"
@@ -972,7 +1156,6 @@ is non-nil."
             ;; sticky tag
             "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
             "\\(.*\\)"))               ;Sticky tag
-    (vc-file-setprop file 'vc-backend 'CVS)
     (vc-file-setprop file 'vc-working-revision (match-string 1))
     (vc-file-setprop file 'vc-cvs-sticky-tag
                     (vc-cvs-parse-sticky-tag (match-string 4)
@@ -1000,7 +1183,8 @@ is non-nil."
 ;; tag names.
 
 (defun vc-cvs-revision-table (file)
-  (let ((default-directory (file-name-directory file))
+  (let (process-file-side-effects
+       (default-directory (file-name-directory file))
         (res nil))
     (with-temp-buffer
       (vc-cvs-command t nil file "log")
@@ -1019,7 +1203,7 @@ is non-nil."
     (setq table (lazy-completion-table
                  table (lambda () (vc-cvs-revision-table (car files)))))
     table))
-                                           
+
 
 (provide 'vc-cvs)