(forward-word): Allow 0 args.
[bpt/emacs.git] / lisp / vc-cvs.el
index b9b9979..0c1e6bc 100644 (file)
@@ -1,11 +1,11 @@
 ;;; vc-cvs.el --- non-resident support for CVS version-control
 
-;; Copyright (C) 1995,98,99,2000,2001,2002  Free Software Foundation, Inc.
+;; Copyright (C) 1995,98,99,2000,2001,02,2003  Free Software Foundation, Inc.
 
 ;; Author:      FSF (see vc.el for full credits)
 ;; Maintainer:  Andre Spiegel <spiegel@gnu.org>
 
-;; $Id: vc-cvs.el,v 1.56 2003/04/23 13:14:16 spiegel Exp $
+;; $Id: vc-cvs.el,v 1.67 2004/01/20 17:41:18 uid65624 Exp $
 
 ;; This file is part of GNU Emacs.
 
@@ -191,7 +191,7 @@ See also variable `vc-cvs-sticky-date-format-string'."
 
 (defun vc-cvs-state (file)
   "CVS-specific version of `vc-state'."
-  (if (vc-cvs-stay-local-p file)
+  (if (vc-stay-local-p file)
       (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.
@@ -217,7 +217,7 @@ See also variable `vc-cvs-sticky-date-format-string'."
   "Find the CVS state of all files in DIR."
   ;; if DIR is not under CVS control, don't do anything.
   (when (file-readable-p (expand-file-name "CVS/Entries" dir))
-    (if (vc-cvs-stay-local-p 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
@@ -257,31 +257,20 @@ See also variable `vc-cvs-sticky-date-format-string'."
 Compared to the default implementation, this function does two things:
 Handle the special case of a CVS file that is added but not yet
 committed and support display of sticky tags."
-  (let* ((state   (vc-state file))
-        (rev     (vc-workfile-version file))
-        (sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
-        (sticky-tag-printable (and sticky-tag
-                                   (not (string= sticky-tag ""))
-                                   (concat "[" sticky-tag "]"))))
-    (cond ((string= rev "0")
-          ;; A file that is added but not yet committed.
-          "CVS @@")
-         ((or (eq state 'up-to-date)
-              (eq state 'needs-patch))
-          (concat "CVS-" rev sticky-tag-printable))
-          ((stringp state)
-          (concat "CVS:" state ":" rev sticky-tag-printable))
-          (t
-           ;; Not just for the 'edited state, but also a fallback
-           ;; for all other states.  Think about different symbols
-           ;; for 'needs-patch and 'needs-merge.
-           (concat "CVS:" rev sticky-tag-printable)))))
+  (let ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
+       (string (if (string= (vc-workfile-version file) "0")
+                   ;; A file that is added but not yet committed.
+                   "CVS @@"
+                 (vc-default-mode-line-string 'CVS file))))
+    (if (zerop (length sticky-tag))
+       string
+      (concat string "[" sticky-tag "]"))))
 
 (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-workfile-version) "0")
+          (if (equal (vc-workfile-version file) "0")
               "(added)" "(modified)"))
          ((eq cvs-state 'needs-patch) "(patch)")
          ((eq cvs-state 'needs-merge) "(merge)"))))
@@ -297,19 +286,15 @@ COMMENT can be used to provide an initial description of FILE.
 
 `vc-register-switches' and `vc-cvs-register-switches' are passed to
 the CVS command (in that order)."
-  (let ((switches (append
-                  (if (stringp vc-register-switches)
-                      (list vc-register-switches)
-                    vc-register-switches)
-                  (if (stringp vc-cvs-register-switches)
-                      (list vc-cvs-register-switches)
-                    vc-cvs-register-switches))))
-
-    (apply 'vc-cvs-command nil 0 file
-          "add"
-          (and comment (string-match "[^\t\n ]" comment)
-               (concat "-m" comment))
-          switches)))
+  (when (and (not (vc-cvs-responsible-p file))
+            (vc-cvs-could-register file))
+    ;; Register the directory if needed.
+    (vc-cvs-register (directory-file-name (file-name-directory file))))
+  (apply 'vc-cvs-command nil 0 file
+        "add"
+        (and comment (string-match "[^\t\n ]" comment)
+             (concat "-m" comment))
+        (vc-switches 'CVS 'register)))
 
 (defun vc-cvs-responsible-p (file)
   "Return non-nil if CVS thinks it is responsible for FILE."
@@ -318,32 +303,33 @@ the CVS command (in that order)."
                                          file
                                        (file-name-directory file)))))
 
-(defalias 'vc-cvs-could-register 'vc-cvs-responsible-p
+(defun vc-cvs-could-register (file)
   "Return non-nil if FILE could be registered in CVS.
-This is only possible if CVS is responsible for FILE's directory.")
+This is only possible if CVS is managing FILE's directory or one of
+its parents."
+  (let ((dir file))
+    (while (and (stringp dir)
+                (not (equal dir (setq dir (file-name-directory dir))))
+                dir)
+      (setq dir (if (file-directory-p
+                     (expand-file-name "CVS/Entries" dir))
+                    t (directory-file-name dir))))
+    (eq dir t)))
 
 (defun vc-cvs-checkin (file rev comment)
   "CVS-specific version of `vc-backend-checkin'."
-  (let ((switches (if (stringp vc-checkin-switches)
-                     (list vc-checkin-switches)
-                   vc-checkin-switches))
-       status)
-    (if (or (not rev) (vc-cvs-valid-version-number-p rev))
-        (setq status (apply 'vc-cvs-command nil 1 file
-                            "ci" (if rev (concat "-r" rev))
-                            (concat "-m" comment)
-                            switches))
-      (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
-          (error "%s is not a valid symbolic tag name" rev)
-        ;; If the input revison is a valid symbolic tag name, we create it
-        ;; as a branch, commit and switch to it.
-        (apply 'vc-cvs-command nil 0 file "tag" "-b" (list rev))
-        (apply 'vc-cvs-command nil 0 file "update" "-r" (list rev))
-        (setq status (apply 'vc-cvs-command nil 1 file
-                            "ci"
-                            (concat "-m" comment)
-                            switches))
-        (vc-file-setprop file 'vc-cvs-sticky-tag rev)))
+  (unless (or (not rev) (vc-cvs-valid-version-number-p rev))
+    (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
+       (error "%s is not a valid symbolic tag name" rev)
+      ;; If the input revison is a valid symbolic tag name, we create it
+      ;; as a branch, commit and switch to it.
+      (apply 'vc-cvs-command nil 0 file "tag" "-b" (list rev))
+      (apply 'vc-cvs-command nil 0 file "update" "-r" (list rev))
+      (vc-file-setprop file 'vc-cvs-sticky-tag rev)))
+  (let ((status (apply 'vc-cvs-command nil 1 file
+                      "ci" (if rev (concat "-r" rev))
+                      (concat "-m" comment)
+                      (vc-switches 'CVS 'checkin))))
     (set-buffer "*vc*")
     (goto-char (point-min))
     (when (not (zerop status))
@@ -382,9 +368,7 @@ This is only possible if CVS is responsible for FILE's directory.")
         (and rev (not (string= rev ""))
              (concat "-r" rev))
         "-p"
-        (if (stringp vc-checkout-switches)
-            (list vc-checkout-switches)
-          vc-checkout-switches)))
+        (vc-switches 'CVS 'checkout)))
 
 (defun vc-cvs-checkout (file &optional editable rev workfile)
   "Retrieve a revision of FILE into a WORKFILE.
@@ -397,9 +381,7 @@ REV is the revision to check out into WORKFILE."
     (save-excursion
       ;; Change buffers to get local value of vc-checkout-switches.
       (if file-buffer (set-buffer file-buffer))
-      (setq switches (if (stringp vc-checkout-switches)
-                        (list vc-checkout-switches)
-                      vc-checkout-switches))
+      (setq switches (vc-switches 'CVS 'checkout))
       ;; Save this buffer's default-directory
       ;; and use save-excursion to make sure it is restored
       ;; in the same buffer it was saved in.
@@ -445,7 +427,7 @@ REV is the revision to check out into WORKFILE."
            (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 file) 'implicit))
                      (if vc-cvs-use-edit
                          (vc-cvs-command nil 0 file "edit")
                        (set-file-modes file (logior (file-modes file) 128))
@@ -459,15 +441,21 @@ REV is the revision to check out into WORKFILE."
                                        'implicit)))
                           "-w")
                      "update"
-                     ;; default for verbose checkout: clear the sticky tag so
-                     ;; that the actual update will get the head of the trunk
-                     (if (or (not rev) (eq rev t) (string= rev ""))
-                         "-A"
-                       (concat "-r" rev))
+                     (when rev
+                       (unless (eq rev t)
+                         ;; default for verbose checkout: clear the
+                         ;; sticky tag so that the actual update will
+                         ;; get the head of the trunk
+                         (if (string= rev "")
+                             "-A"
+                           (concat "-r" rev))))
                      switches))))
        (vc-mode-line file)
        (message "Checking out %s...done" filename)))))
 
+(defun vc-cvs-delete-file (file)
+  (vc-cvs-command nil 0 file "remove" "-f"))
+
 (defun vc-cvs-revert (file &optional contents-done)
   "Revert FILE to the version it was based on."
   (unless contents-done
@@ -545,47 +533,41 @@ The changes are between FIRST-VERSION and SECOND-VERSION."
 ;;; History functions
 ;;;
 
-(defun vc-cvs-print-log (file)
+(defun vc-cvs-print-log (file &optional buffer)
   "Get change log associated with FILE."
   (vc-cvs-command
-   nil
-   (if (and (vc-cvs-stay-local-p file) (fboundp 'start-process)) 'async 0)
+   buffer
+   (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
    file "log"))
 
-(defun vc-cvs-diff (file &optional oldvers newvers)
+(defun vc-cvs-diff (file &optional oldvers newvers buffer)
   "Get a difference report using CVS between two versions of FILE."
-  (let (status (diff-switches-list (vc-diff-switches-list 'CVS)))
-    (if (string= (vc-workfile-version file) "0")
-       ;; This file is added but not yet committed; there is no master file.
-       (if (or oldvers newvers)
-           (error "No revisions of %s exist" file)
-         ;; We regard this as "changed".
-         ;; Diff it against /dev/null.
-          ;; Note: this is NOT a "cvs diff".
-          (apply 'vc-do-command "*vc-diff*"
-                 1 "diff" file
-                 (append diff-switches-list '("/dev/null")))
-         ;; Even if it's empty, it's locally modified.
-         1)
-      (setq status
-            (apply 'vc-cvs-command "*vc-diff*"
-                   (if (and (vc-cvs-stay-local-p file)
-                           (fboundp 'start-process))
-                      'async
-                    1)
-                   file "diff"
-                   (and oldvers (concat "-r" oldvers))
-                   (and newvers (concat "-r" newvers))
-                   diff-switches-list))
-      (if (vc-cvs-stay-local-p file)
-          1 ;; async diff, pessimistic assumption
-        status))))
+  (if (string= (vc-workfile-version file) "0")
+      ;; This file is added but not yet committed; there is no master file.
+      (if (or oldvers newvers)
+         (error "No revisions of %s exist" file)
+       ;; We regard this as "changed".
+       ;; Diff it against /dev/null.
+       ;; Note: this is NOT a "cvs diff".
+       (apply 'vc-do-command (or buffer "*vc-diff*")
+              1 "diff" file
+              (append (vc-switches nil 'diff) '("/dev/null")))
+       ;; Even if it's empty, it's locally modified.
+       1)
+    (let* ((async (and (vc-stay-local-p file) (fboundp 'start-process)))
+          (status (apply 'vc-cvs-command (or buffer "*vc-diff*")
+                         (if async 'async 1)
+                         file "diff"
+                         (and oldvers (concat "-r" oldvers))
+                         (and newvers (concat "-r" newvers))
+                         (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-cvs-stay-local-p dir)
+    (if (vc-stay-local-p dir)
         ;; local diff: do it filewise, and only for files that are modified
         (vc-file-tree-walk
          dir
@@ -603,13 +585,12 @@ The changes are between FIRST-VERSION and SECOND-VERSION."
         (apply 'vc-cvs-command "*vc-diff*" 1 nil "diff"
                (and rev1 (concat "-r" rev1))
                (and rev2 (concat "-r" rev2))
-               (vc-diff-switches-list 'CVS))))))
+               (vc-switches 'CVS 'diff))))))
 
 (defun vc-cvs-annotate-command (file buffer &optional version)
   "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
 Optional arg VERSION is a version to annotate from."
-  (vc-cvs-command buffer 0 file "annotate" (if version
-                                                    (concat "-r" version))))
+  (vc-cvs-command buffer 0 file "annotate" (if version (concat "-r" version))))
 
 (defun vc-cvs-annotate-current-time ()
   "Return the current time, based at midnight of the current day, and
@@ -644,6 +625,14 @@ systime, or nil if there is none."
          (beginning-of-line nil)
            (vc-cvs-annotate-time))))))
 
+(defun vc-cvs-annotate-extract-revision-at-line ()
+  (save-excursion
+    (beginning-of-line)
+    (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
+                          (line-end-position) t)
+       (match-string-no-properties 1)
+      nil)))
+
 ;;;
 ;;; Snapshot system
 ;;;
@@ -696,7 +685,7 @@ If UPDATE is non-nil, then update (resynch) any affected buffers."
 ;;; Miscellaneous
 ;;;
 
-(defalias 'vc-cvs-make-version-backups-p 'vc-cvs-stay-local-p
+(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-check-headers ()
@@ -721,56 +710,21 @@ and that it passes `vc-cvs-global-switches' to it before FLAGS."
            (append vc-cvs-global-switches
                    flags))))
 
-(defun vc-cvs-stay-local-p (file)
-  "Return non-nil if VC should stay local when handling FILE.
-See `vc-cvs-stay-local'."
-  (when vc-cvs-stay-local
-    (let* ((dirname (if (file-directory-p file)
-                       (directory-file-name file)
-                     (file-name-directory file)))
-          (prop
-           (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
-               (vc-file-setprop
-                dirname 'vc-cvs-stay-local-p
-                (let ((rootname (expand-file-name "CVS/Root" dirname)))
-                  (when (file-readable-p rootname)
-                    (with-temp-buffer
-                      (let ((coding-system-for-read
-                             (or file-name-coding-system
-                                 default-file-name-coding-system)))
-                        (vc-insert-file rootname))
-                      (goto-char (point-min))
-                      (let* ((cvs-root-members
-                              (vc-cvs-parse-root
-                               (buffer-substring (point)
-                                                 (line-end-position))))
-                             (hostname (nth 2 cvs-root-members)))
-                        (if (not hostname)
-                            'no
-                          (let* ((stay-local t)
-                                 (rx
-                                  (cond
-                                   ;; vc-cvs-stay-local: rx
-                                   ((stringp vc-cvs-stay-local)
-                                    vc-cvs-stay-local)
-                                   ;; vc-cvs-stay-local: '( [except] rx ... )
-                                   ((consp vc-cvs-stay-local)
-                                    (mapconcat
-                                     'identity
-                                     (if (not (eq (car vc-cvs-stay-local)
-                                                  'except))
-                                         vc-cvs-stay-local
-                                       (setq stay-local nil)
-                                       (cdr vc-cvs-stay-local))
-                                     "\\|")))))
-                            (if (not rx)
-                                'yes
-                              (if (not (string-match rx hostname))
-                                  (setq stay-local (not stay-local)))
-                              (if stay-local
-                                  'yes
-                                'no))))))))))))
-      (if (eq prop 'yes) t nil))))
+(defalias 'vc-cvs-stay-local-p 'vc-stay-local-p)  ;Back-compatibility.
+
+(defun vc-cvs-repository-hostname (dirname)
+  "Hostname of the CVS server associated to workarea DIRNAME."
+  (let ((rootname (expand-file-name "CVS/Root" dirname)))
+    (when (file-readable-p rootname)
+      (with-temp-buffer
+       (let ((coding-system-for-read
+              (or file-name-coding-system
+                  default-file-name-coding-system)))
+         (vc-insert-file rootname))
+       (goto-char (point-min))
+       (nth 2 (vc-cvs-parse-root
+               (buffer-substring (point)
+                                 (line-end-position))))))))
 
 (defun vc-cvs-parse-root (root)
   "Split CVS ROOT specification string into a list of fields.
@@ -975,22 +929,26 @@ is non-nil."
             "\\(.*\\)"))               ;Sticky tag
     (vc-file-setprop file 'vc-workfile-version (match-string 1))
     (vc-file-setprop file 'vc-cvs-sticky-tag
-                    (vc-cvs-parse-sticky-tag (match-string 4) (match-string 5)))
-    ;; compare checkout time and modification time
-    (let* ((mtime (nth 5 (file-attributes file)))
-          (system-time-locale "C")
-          (mtstr (format-time-string "%c" mtime 'utc)))
-      ;; Solaris sometimes uses "Wed Sep 05" instead of  "Wed Sep  5".
-      ;; See "grep '[^a-z_]ctime' cvs/src/*.c" for reference.
-      (if (= (aref mtstr 8) ?0)
-         (setq mtstr (concat (substring mtstr 0 8) " " (substring mtstr 9))))
-      (cond ((equal mtstr (match-string 2))
-            (vc-file-setprop file 'vc-checkout-time mtime)
-            (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
-           (t
-            (vc-file-setprop file 'vc-checkout-time 0)
-            (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
+                    (vc-cvs-parse-sticky-tag (match-string 4) 
+                                              (match-string 5)))
+    ;; Compare checkout time and modification time.
+    ;; This is intentionally different from the algorithm that CVS uses
+    ;; (which is based on textual comparison), because there can be problems
+    ;; generating a time string that looks exactly like the one from CVS.
+    (let ((mtime (nth 5 (file-attributes file))))
+      (require 'parse-time)
+      (let ((parsed-time
+            (parse-time-string (concat (match-string 2) " +0000"))))
+       (cond ((and (not (string-match "\\+" (match-string 2)))
+                   (car parsed-time)
+                   (equal mtime (apply 'encode-time parsed-time)))
+              (vc-file-setprop file 'vc-checkout-time mtime)
+              (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
+             (t
+              (vc-file-setprop file 'vc-checkout-time 0)
+              (if set-state (vc-file-setprop file 'vc-state 'edited)))))))))
 
 (provide 'vc-cvs)
 
+;;; arch-tag: 60e1402a-aa53-4607-927a-cf74f144b432
 ;;; vc-cvs.el ends here