vc.el 138 KB
Newer Older
1
;;; vc.el --- drive a version-control system from within Emacs
Eric S. Raymond's avatar
Eric S. Raymond committed
2

Dave Love's avatar
Dave Love committed
3
;; Copyright (C) 1992,93,94,95,96,97,98,2000,2001  Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

5 6
;; Author:     FSF (see below for full credits)
;; Maintainer: Andre Spiegel <spiegel@gnu.org>
Gerd Moellmann's avatar
Gerd Moellmann committed
7
;; Keywords: tools
Eric S. Raymond's avatar
Eric S. Raymond committed
8

9
;; $Id: vc.el,v 1.313 2001/10/21 23:31:45 spiegel Exp $
10

Eric S. Raymond's avatar
Eric S. Raymond committed
11 12 13 14 15 16 17 18 19 20 21 22 23
;; This file is part of GNU Emacs.

;; 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 2, 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
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
24 25 26
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Eric S. Raymond's avatar
Eric S. Raymond committed
27

28 29 30 31 32 33 34 35 36
;;; Credits:

;; VC was initially designed and implemented by Eric S. Raymond
;; <esr@snark.thyrsus.com>.  Over the years, many people have
;; contributed substantial amounts of work to VC.  These include:
;;   Per Cederqvist <ceder@lysator.liu.se>
;;   Paul Eggert <eggert@twinsun.com>
;;   Sebastian Kremer <sk@thp.uni-koeln.de>
;;   Martin Lorentzson <martinl@gnu.org>
Dave Love's avatar
Dave Love committed
37
;;   Dave Love <fx@gnu.org>
38 39 40 41 42
;;   Stefan Monnier <monnier@cs.yale.edu>
;;   Andre Spiegel <spiegel@gnu.org>
;;   Richard Stallman <rms@gnu.org>
;;   ttn@netcom.com

Eric S. Raymond's avatar
Eric S. Raymond committed
43 44
;;; Commentary:

45 46
;; This mode is fully documented in the Emacs user's manual.
;;
47
;; Supported version-control systems presently include SCCS, RCS, and CVS.
48 49 50
;;
;; Some features will not work with old RCS versions.  Where
;; appropriate, VC finds out which version you have, and allows or
51
;; disallows those features (stealing locks, for example, works only
52
;; from 5.6.2 onwards).
53 54
;; Even initial checkins will fail if your RCS version is so old that ci
;; doesn't understand -t-; this has been known to happen to people running
55
;; NExTSTEP 3.0.
Eric S. Raymond's avatar
Eric S. Raymond committed
56
;;
57
;; You can support the RCS -x option by customizing vc-rcs-master-templates.
Eric S. Raymond's avatar
Eric S. Raymond committed
58 59 60 61
;;
;; Proper function of the SCCS diff commands requires the shellscript vcdiff
;; to be installed somewhere on Emacs's path for executables.
;;
62
;; If your site uses the ChangeLog convention supported by Emacs, the
63
;; function vc-comment-to-change-log should prove a useful checkin hook.
64
;;
Eric S. Raymond's avatar
Eric S. Raymond committed
65 66
;; The vc code maintains some internal state in order to reduce expensive
;; version-control operations to a minimum.  Some names are only computed
67
;; once.  If you perform version control operations with RCS/SCCS/CVS while
Eric S. Raymond's avatar
Eric S. Raymond committed
68 69 70 71 72
;; vc's back is turned, or move/rename master files while vc is running,
;; vc may get seriously confused.  Don't do these things!
;;
;; Developer's notes on some concurrency issues are included at the end of
;; the file.
73
;;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
;; ADDING SUPPORT FOR OTHER BACKENDS
;;
;; VC can use arbitrary version control systems as a backend.  To add
;; support for a new backend named SYS, write a library vc-sys.el that
;; contains functions of the form `vc-sys-...' (note that SYS is in lower
;; case for the function and library names).  VC will use that library if
;; you put the symbol SYS somewhere into the list of
;; `vc-handled-backends'.  Then, for example, if `vc-sys-registered'
;; returns non-nil for a file, all SYS-specific versions of VC commands
;; will be available for that file.
;;
;; VC keeps some per-file information in the form of properties (see
;; vc-file-set/getprop in vc-hooks.el).  The backend-specific functions
;; do not generally need to be aware of these properties.  For example,
;; `vc-sys-workfile-version' should compute the workfile version and
;; return it; it should not look it up in the property, and it needn't
;; store it there either.  However, if a backend-specific function does
;; store a value in a property, that value takes precedence over any
;; value that the generic code might want to set (check for uses of 
;; the macro `with-vc-properties' in vc.el).
;;
;; In the list of functions below, each identifier needs to be prepended
;; with `vc-sys-'.  Some of the functions are mandatory (marked with a
;; `*'), others are optional (`-').
;;
;; STATE-QUERYING FUNCTIONS
;;
101
;; * registered (file)
102 103 104 105 106 107 108 109 110 111 112
;;
;;   Return non-nil if FILE is registered in this backend.
;;
;; * state (file) 
;;
;;   Return the current version control state of FILE.  For a list of
;;   possible values, see `vc-state'.  This function should do a full and
;;   reliable state computation; it is usually called immediately after
;;   C-x v v.  If you want to use a faster heuristic when visiting a
;;   file, put that into `state-heuristic' below.
;;
113
;; - state-heuristic (file)
114 115 116 117 118 119
;;
;;   If provided, this function is used to estimate the version control
;;   state of FILE at visiting time.  It should be considerably faster
;;   than the implementation of `state'.  For a list of possible values,
;;   see the doc string of `vc-state'.
;;
120
;; - dir-state (dir)
121 122 123 124 125 126
;;
;;   If provided, this function is used to find the version control state
;;   of all files in DIR in a fast way.  The function should not return
;;   anything, but rather store the files' states into the corresponding
;;   `vc-state' properties.
;;
127
;; * workfile-version (file)
128 129 130 131 132 133 134 135 136 137
;;
;;   Return the current workfile version of FILE.
;;
;; - latest-on-branch-p (file)
;;
;;   Return non-nil if the current workfile version of FILE is the latest
;;   on its branch.  The default implementation always returns t, which
;;   means that working with non-current versions is not supported by
;;   default.
;;
138
;; * checkout-model (file)
139 140 141 142
;;
;;   Indicate whether FILE needs to be "checked out" before it can be
;;   edited.  See `vc-checkout-model' for a list of possible values.
;;
143
;; - workfile-unchanged-p (file)
144 145 146 147 148 149 150 151
;;
;;   Return non-nil if FILE is unchanged from its current workfile
;;   version.  This function should do a brief comparison of FILE's
;;   contents with those of the master version.  If the backend does not
;;   have such a brief-comparison feature, the default implementation of
;;   this function can be used, which delegates to a full
;;   vc-BACKEND-diff.
;;
152
;; - mode-line-string (file)
153 154 155 156 157
;;
;;   If provided, this function should return the VC-specific mode line
;;   string for FILE.  The default implementation deals well with all
;;   states that `vc-state' can return.
;;
158
;; - dired-state-info (file)
159 160 161 162 163 164 165 166 167 168 169 170
;;
;;   Translate the `vc-state' property of FILE into a string that can be
;;   used in a vc-dired buffer.  The default implementation deals well
;;   with all states that `vc-state' can return.
;;
;; STATE-CHANGING FUNCTIONS
;;
;; * register (file &optional rev comment)
;;
;;   Register FILE in this backend.  Optionally, an initial revision REV
;;   and an initial description of the file, COMMENT, may be specified.
;;
171
;; - responsible-p (file)
172 173 174 175 176 177 178
;;
;;   Return non-nil if this backend considers itself "responsible" for
;;   FILE, which can also be a directory.  This function is used to find
;;   out what backend to use for registration of new files and for things
;;   like change log generation.  The default implementation always
;;   returns nil.
;;
179
;; - could-register (file)
180 181 182 183
;;
;;   Return non-nil if FILE could be registered under this backend.  The
;;   default implementation always returns t.
;;
184
;; - receive-file (file rev)
185 186 187 188 189 190 191 192 193 194 195 196
;;
;;   Let this backend "receive" a file that is already registered under
;;   another backend.  The default implementation simply calls `register'
;;   for FILE, but it can be overridden to do something more specific,
;;   e.g. keep revision numbers consistent or choose editing modes for
;;   FILE that resemble those of the other backend.
;;
;; - unregister (file)
;;
;;   Unregister FILE from this backend.  This is only needed if this
;;   backend may be used as a "more local" backend for temporary editing.
;;
197
;; * checkin (file rev comment)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
;;
;;   Commit changes in FILE to this backend.  If REV is non-nil, that
;;   should become the new revision number.  COMMENT is used as a
;;   check-in comment.
;;
;; * checkout (file &optional editable rev destfile)
;;
;;   Check out revision REV of FILE into the working area.  If EDITABLE
;;   is non-nil, FILE should be writable by the user and if locking is
;;   used for FILE, a lock should also be set.  If REV is non-nil, that
;;   is the revision to check out (default is current workfile version);
;;   if REV is the empty string, that means to check out the head of the
;;   trunk.  If optional arg DESTFILE is given, it is an alternate
;;   filename to write the contents to.
;;
213
;; * revert (file &optional contents-done)
214
;;
215 216 217 218
;;   Revert FILE back to the current workfile version.  If optional
;;   arg CONTENTS-DONE is non-nil, then the contents of FILE have
;;   already been reverted from a version backup, and this function
;;   only needs to update the status of FILE within the backend.
219 220 221 222 223 224 225 226 227
;;
;; - cancel-version (file editable)
;;
;;   Cancel the current workfile version of FILE, i.e. remove it from the
;;   master.  EDITABLE non-nil means that FILE should be writable
;;   afterwards, and if locking is used for FILE, then a lock should also
;;   be set.  If this function is not provided, trying to cancel a
;;   version is caught as an error.
;;
228
;; - merge (file rev1 rev2)
229 230 231
;;
;;   Merge the changes between REV1 and REV2 into the current working file.
;;
232
;; - merge-news (file)
233 234 235
;;
;;   Merge recent changes from the current branch into FILE.
;;
236
;; - steal-lock (file &optional version)
237 238 239 240 241 242 243 244
;;
;;   Steal any lock on the current workfile version of FILE, or on
;;   VERSION if that is provided.  This function is only needed if
;;   locking is used for files under this backend, and if files can
;;   indeed be locked by other users.
;;
;; HISTORY FUNCTIONS
;;
245
;; * print-log (file)
246
;;
247
;;   Insert the revision log of FILE into the *vc* buffer.
248
;;
249
;; - show-log-entry (version)
250 251 252 253 254
;;
;;   If provided, search the log entry for VERSION in the current buffer,
;;   and make sure it is displayed in the buffer's window.  The default
;;   implementation of this function works for RCS-style logs.
;;
255
;; - wash-log (file)
256 257 258 259
;;
;;   Remove all non-comment information from the output of print-log.  The
;;   default implementation of this function works for RCS-style logs.
;;
260
;; - logentry-check ()
261 262 263 264 265 266
;;
;;   If defined, this function is run to find out whether the user
;;   entered a valid log entry for check-in.  The log entry is in the
;;   current buffer, and if it is not a valid one, the function should
;;   throw an error.
;;
267
;; - comment-history (file)
268 269 270 271 272 273 274
;;
;;   Return a string containing all log entries that were made for FILE.
;;   This is used for transferring a file from one backend to another,
;;   retaining comment information.  The default implementation of this
;;   function does this by calling print-log and then wash-log, and
;;   returning the resulting buffer contents as a string.
;;
275
;; - update-changelog (files)
276 277 278 279 280 281
;;
;;   Using recent log entries, create ChangeLog entries for FILES, or for
;;   all files at or below the default-directory if FILES is nil.  The
;;   default implementation runs rcs2log, which handles RCS- and
;;   CVS-style logs.
;;
282
;; * diff (file &optional rev1 rev2)
283
;;
284
;;   Insert the diff for FILE into the *vc-diff* buffer.  If REV1 and REV2
285 286 287 288 289 290 291
;;   are non-nil, report differences from REV1 to REV2.  If REV1 is nil,
;;   use the current workfile version (as found in the repository) as the
;;   older version; if REV2 is nil, use the current workfile contents as
;;   the newer version.  This function should return a status of either 0
;;   (no differences found), or 1 (either non-empty diff or the diff is
;;   run asynchronously).
;;
292 293 294 295 296 297 298
;; - diff-tree (dir &optional rev1 rev2) 
;;
;;   Insert the diff for all files at and below DIR into the *vc-diff*
;;   buffer.  The meaning of REV1 and REV2 is the same as for 
;;   vc-BACKEND-diff.  The default implementation does an explicit tree
;;   walk, calling vc-BACKEND-diff for each individual file.
;;
299
;; - annotate-command (file buf rev)
300 301 302 303 304
;;
;;   If this function is provided, it should produce an annotated version
;;   of FILE in BUF, relative to version REV.  This is currently only
;;   implemented for CVS, using the `cvs annotate' command.
;;
305
;; - annotate-time ()
306 307
;;
;;   Only required if `annotate-command' is defined for the backend.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
;;   Return the time of the next line of annotation at or after point,
;;   as a floating point fractional number of days.  The helper
;;   function `vc-annotate-convert-time' may be useful for converting
;;   multi-part times as returned by `current-time' and `encode-time'
;;   to this format.  Return NIL if no more lines of annotation appear
;;   in the buffer.  You can safely assume that point is placed at the
;;   beginning of each line, starting at `point-min'.  The buffer that
;;   point is placed in is the Annotate output, as defined by the
;;   relevant backend.
;;
;; - annotate-current-time ()
;;
;;   Only required if `annotate-command' is defined for the backend,
;;   AND you'd like the current time considered to be anything besides
;;   (vs-annotate-convert-time (current-time)) -- i.e. the current
;;   time with hours, minutes, and seconds included.  Probably safe to
;;   ignore.  Return the current-time, in units of fractional days.
325 326 327
;;
;; SNAPSHOT SYSTEM
;;
328
;; - create-snapshot (dir name branchp)
329 330 331 332 333 334 335 336 337 338 339 340 341 342
;;
;;   Take a snapshot of the current state of files under DIR and name it
;;   NAME.  This should make sure that files are up-to-date before
;;   proceeding with the action.  DIR can also be a file and if BRANCHP
;;   is specified, NAME should be created as a branch and DIR should be
;;   checked out under this new branch.  The default implementation does
;;   not support branches but does a sanity check, a tree traversal and
;;   for each file calls `assign-name'.
;;
;; - assign-name (file name)
;;
;;   Give name NAME to the current version of FILE, assuming it is
;;   up-to-date.  Only used by the default version of `create-snapshot'.
;;
343
;; - retrieve-snapshot (dir name update)
344 345 346 347 348 349 350 351 352 353
;;
;;   Retrieve a named snapshot of all registered files at or below DIR.
;;   If UPDATE is non-nil, then update buffers of any files in the
;;   snapshot that are currently visited.  The default implementation
;;   does a sanity check whether there aren't any uncommitted changes at
;;   or below DIR, and then performs a tree walk, using the `checkout'
;;   function to retrieve the corresponding versions.
;;
;; MISCELLANEOUS
;;
354
;; - make-version-backups-p (file)
355 356 357 358 359 360
;;
;;   Return non-nil if unmodified repository versions of FILE should be
;;   backed up locally.  If this is done, VC can perform `diff' and
;;   `revert' operations itself, without calling the backend system.  The
;;   default implementation always returns nil.
;;
361
;; - check-headers ()
362 363 364
;;
;;   Return non-nil if the current buffer contains any version headers.
;;
365
;; - clear-headers ()
366 367 368 369 370 371 372 373
;;
;;   In the current buffer, reset all version headers to their unexpanded
;;   form.  This function should be provided if the state-querying code
;;   for this backend uses the version headers to determine the state of
;;   a file.  This function will then be called whenever VC changes the
;;   version control state in such a way that the headers would give
;;   wrong information.
;;
374
;; - rename-file (old new)
375 376 377 378
;;
;;   Rename file OLD to NEW, both in the working area and in the
;;   repository.  If this function is not provided, the command
;;   `vc-rename-file' will signal an error.
379

380
;;; Code:
381

Eric S. Raymond's avatar
Eric S. Raymond committed
382
(require 'vc-hooks)
383
(require 'ring)
384
(eval-when-compile
385
  (require 'cl)
386 387 388
  (require 'compile)
  (require 'dired)      ; for dired-map-over-marks macro
  (require 'dired-aux))	; for dired-kill-{line,tree}
389 390 391 392 393

(if (not (assoc 'vc-parent-buffer minor-mode-alist))
    (setq minor-mode-alist
	  (cons '(vc-parent-buffer vc-parent-buffer-name)
		minor-mode-alist)))
Eric S. Raymond's avatar
Eric S. Raymond committed
394 395 396

;; General customization

397 398 399 400 401 402 403 404 405
(defgroup vc nil
  "Version-control system in Emacs."
  :group 'tools)

(defcustom vc-suppress-confirm nil
  "*If non-nil, treat user as expert; suppress yes-no prompts on some things."
  :type 'boolean
  :group 'vc)

406 407 408 409 410 411 412 413
(defcustom vc-delete-logbuf-window t
  "*If non-nil, delete the *VC-log* buffer and window after each logical action.
If nil, bury that buffer instead.
This is most useful if you have multiple windows on a frame and would like to
preserve the setting."
  :type 'boolean
  :group 'vc)

414 415 416 417 418
(defcustom vc-initial-comment nil
  "*If non-nil, prompt for initial comment when a file is registered."
  :type 'boolean
  :group 'vc)

419 420
(defcustom vc-default-init-version "1.1"
  "*A string used as the default version number when a new file is registered.
421
This can be overridden by giving a prefix argument to \\[vc-register]."
422
  :type 'string
Dan Nicolaescu's avatar
Dan Nicolaescu committed
423 424
  :group 'vc
  :version "20.3")
425

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
(defcustom vc-command-messages nil
  "*If non-nil, display run messages from back-end commands."
  :type 'boolean
  :group 'vc)

(defcustom vc-checkin-switches nil
  "*A string or list of strings specifying extra switches for checkin.
These are passed to the checkin program by \\[vc-checkin]."
  :type '(choice (const :tag "None" nil)
		 (string :tag "Argument String")
		 (repeat :tag "Argument List"
			 :value ("")
			 string))
  :group 'vc)

(defcustom vc-checkout-switches nil
  "*A string or list of strings specifying extra switches for checkout.
These are passed to the checkout program by \\[vc-checkout]."
  :type '(choice (const :tag "None" nil)
		 (string :tag "Argument String")
		 (repeat :tag "Argument List"
			 :value ("")
			 string))
  :group 'vc)

(defcustom vc-register-switches nil
  "*A string or list of strings; extra switches for registering a file.
These are passed to the checkin program by \\[vc-register]."
  :type '(choice (const :tag "None" nil)
		 (string :tag "Argument String")
		 (repeat :tag "Argument List"
			 :value ("")
			 string))
  :group 'vc)

461 462 463 464
(defcustom vc-dired-listing-switches "-al"
  "*Switches passed to `ls' for vc-dired.  MUST contain the `l' option."
  :type 'string
  :group 'vc
465
  :version "21.1")
466

467 468 469 470 471 472 473 474 475 476 477 478
(defcustom vc-dired-recurse t
  "*If non-nil, show directory trees recursively in VC Dired."
  :type 'boolean
  :group 'vc
  :version "20.3")

(defcustom vc-dired-terse-display t
  "*If non-nil, show only locked files in VC Dired."
  :type 'boolean
  :group 'vc
  :version "20.3")

479 480 481 482
(defcustom vc-directory-exclusion-list '("SCCS" "RCS" "CVS")
  "*List of directory names to be ignored while recursively walking file trees."
  :type '(repeat string)
  :group 'vc)
483

484 485 486
(defconst vc-maximum-comment-ring-size 32
  "Maximum number of saved comments in the comment ring.")

487 488 489 490 491 492 493 494 495 496 497 498
(defcustom vc-diff-switches nil
  "*A string or list of strings specifying switches for diff under VC.
There is also an option vc-BACKEND-diff-switches for each BACKEND that
VC can handle."
  :type '(choice (const :tag "None" nil)
		 (string :tag "Argument String")
		 (repeat :tag "Argument List"
			 :value ("")
			 string))
  :group 'vc
  :version "21.1")

André Spiegel's avatar
André Spiegel committed
499 500 501 502 503 504 505 506
;;;###autoload
(defcustom vc-checkout-hook nil
  "*Normal hook (list of functions) run after a file has been checked out.
See `run-hooks'."
  :type 'hook
  :group 'vc
  :version "21.1")

507 508 509 510 511 512 513 514 515
(defcustom vc-annotate-display-mode nil
  "Which mode to color the annotations with by default."
  :type '(choice (const :tag "Default" nil)
		 (const :tag "Scale to Oldest" scale)
		 (const :tag "Scale Oldest->Newest" fullscale)
		 (number :tag "Specify Fractional Number of Days"
			 :value "20.5"))
  :group 'vc)

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
;;;###autoload
(defcustom vc-checkin-hook nil
  "*Normal hook (list of functions) run after a checkin is done.
See `run-hooks'."
  :type 'hook
  :options '(vc-comment-to-change-log)
  :group 'vc)

;;;###autoload
(defcustom vc-before-checkin-hook nil
  "*Normal hook (list of functions) run before a file gets checked in.
See `run-hooks'."
  :type 'hook
  :group 'vc)

(defcustom vc-logentry-check-hook nil
  "*Normal hook run by `vc-backend-logentry-check'.
Use this to impose your own rules on the entry in addition to any the
version control backend imposes itself."
  :type 'hook
  :group 'vc)
537

538
;; Annotate customization
539
(defcustom vc-annotate-color-map
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  '(( 20. . "#FF0000")
    ( 40. . "#FF3800")
    ( 60. . "#FF7000")
    ( 80. . "#FFA800")
    (100. . "#FFE000")
    (120. . "#E7FF00")
    (140. . "#AFFF00")
    (160. . "#77FF00")
    (180. . "#3FFF00")
    (200. . "#07FF00")
    (220. . "#00FF31")
    (240. . "#00FF69")
    (260. . "#00FFA1")
    (280. . "#00FFD9")
    (300. . "#00EEFF")
    (320. . "#00B6FF")
    (340. . "#007EFF"))
  "*ASSOCIATION list of age versus color, for \\[vc-annotate].  
Ages are given in units of fractional days.  Default is eighteen steps
using a twenty day increment."
560
  :type 'alist
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  :group 'vc)

(defcustom vc-annotate-very-old-color "#0046FF"
  "*Color for lines older than CAR of last cons in `vc-annotate-color-map'."
  :type 'string
  :group 'vc)

(defcustom vc-annotate-background "black"
  "*Background color for \\[vc-annotate].
Default color is used if nil."
  :type 'string
  :group 'vc)

(defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
  "*Menu elements for the mode-specific menu of VC-Annotate mode.
List of factors, used to expand/compress the time scale.  See `vc-annotate'."
577
  :type '(repeat number)
578 579
  :group 'vc)

580 581 582
;; vc-annotate functionality (CVS only).
(defvar vc-annotate-mode nil
  "Variable indicating if VC-Annotate mode is active.")
583

584 585 586 587 588
(defvar vc-annotate-mode-map
  (let ((m (make-sparse-keymap)))
    (define-key m [menu-bar] (make-sparse-keymap "VC-Annotate"))
    m)
  "Local keymap used for VC-Annotate mode.")
589

590 591
(defvar vc-annotate-mode-menu nil
  "Local keymap used for VC-Annotate mode's menu bar menu.")
592

Eric S. Raymond's avatar
Eric S. Raymond committed
593 594
;; Header-insertion hair

595
(defcustom vc-static-header-alist
Eric S. Raymond's avatar
Eric S. Raymond committed
596 597
  '(("\\.c$" .
     "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
598 599
  "*Associate static header string templates with file types.
A \%s in the template is replaced with the first string associated with
600
the file's version control type in `vc-header-alist'."
601 602 603 604
  :type '(repeat (cons :format "%v"
		       (regexp :tag "File Type")
		       (string :tag "Header String")))
  :group 'vc)
605

606
(defcustom vc-comment-alist
Eric S. Raymond's avatar
Eric S. Raymond committed
607 608
  '((nroff-mode ".\\\"" ""))
  "*Special comment delimiters to be used in generating vc headers only.
609 610
Add an entry in this list if you need to override the normal `comment-start'
and `comment-end' variables.  This will only be necessary if the mode language
611 612 613 614 615 616
is sensitive to blank lines."
  :type '(repeat (list :format "%v"
		       (symbol :tag "Mode")
		       (string :tag "Comment Start")
		       (string :tag "Comment End")))
  :group 'vc)
Eric S. Raymond's avatar
Eric S. Raymond committed
617

618
;; Default is to be extra careful for super-user.
619 620
;; TODO: This variable is no longer used; the corresponding checks
;;       are always done now.  If that turns out to be fast enough,
621
;;       the variable can be obsoleted.
622
(defcustom vc-checkout-carefully (= (user-uid) 0)
623 624
  "*Non-nil means be extra-careful in checkout.
Verify that the file really is not locked
625 626 627
and that its contents match what the master file says."
  :type 'boolean
  :group 'vc)
628

629

630
;; The main keymap
631

632 633 634 635 636 637 638 639 640 641 642
;; Initialization code, to be done just once at load-time
(defvar vc-log-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\M-n" 'vc-next-comment)
    (define-key map "\M-p" 'vc-previous-comment)
    (define-key map "\M-r" 'vc-comment-search-reverse)
    (define-key map "\M-s" 'vc-comment-search-forward)
    (define-key map "\C-c\C-c" 'vc-finish-logentry)
    map))
;; Compatibility with old name.  Should we bother ?
(defvar vc-log-entry-mode vc-log-mode-map)
643

644

Eric S. Raymond's avatar
Eric S. Raymond committed
645 646
;; Variables the user doesn't need to know about.
(defvar vc-log-operation nil)
647
(defvar vc-log-after-operation-hook nil)
648
(defvar vc-annotate-buffers nil
649 650
  "Alist of current \"Annotate\" buffers and their corresponding backends.
The keys are \(BUFFER . BACKEND\).  See also `vc-annotate-get-backend'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
651 652 653
;; In a log entry buffer, this is a local variable
;; that points to the buffer for which it was made
;; (either a file, or a VC dired buffer).
654
(defvar vc-parent-buffer nil)
655
(put 'vc-parent-buffer 'permanent-local t)
656
(defvar vc-parent-buffer-name nil)
657
(put 'vc-parent-buffer-name 'permanent-local t)
Eric S. Raymond's avatar
Eric S. Raymond committed
658

659 660 661
(defvar vc-log-file)
(defvar vc-log-version)

662
(defvar vc-dired-mode nil)
663 664
(make-variable-buffer-local 'vc-dired-mode)

665
(defvar vc-comment-ring (make-ring vc-maximum-comment-ring-size))
666
(defvar vc-comment-ring-index nil)
667
(defvar vc-last-comment-match "")
668

669 670 671
;; functions that operate on RCS revision numbers.  This code should
;; also be moved into the backends.  It stays for now, however, since
;; it is used in code below.
672
(defun vc-trunk-p (rev)
673
  "Return t if REV is a revision on the trunk."
674 675
  (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))

676
(defun vc-branch-p (rev)
677
  "Return t if REV is a branch revision."
678 679
  (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))

680
(defun vc-branch-part (rev)
681
  "Return the branch part of a revision number REV."
682 683
  (substring rev 0 (string-match "\\.[0-9]+\\'" rev)))

684
(defun vc-minor-part (rev)
685
  "Return the minor version number of a revision number REV."
686 687 688 689
  (string-match "[0-9]+\\'" rev)
  (substring rev (match-beginning 0) (match-end 0)))

(defun vc-previous-version (rev)
690
  "Guess the version number immediately preceding REV."
691 692 693 694 695 696 697 698 699 700 701 702 703
  (let ((branch (vc-branch-part rev))
        (minor-num (string-to-number (vc-minor-part rev))))
    (if (> minor-num 1)
        ;; version does probably not start a branch or release
        (concat branch "." (number-to-string (1- minor-num)))
      (if (vc-trunk-p rev)
          ;; we are at the beginning of the trunk --
          ;; don't know anything to return here
          ""
        ;; we are at the beginning of a branch --
        ;; return version of starting point
        (vc-branch-part branch)))))

Eric S. Raymond's avatar
Eric S. Raymond committed
704 705
;; File property caching

706 707 708 709 710 711
(defun vc-clear-context ()
  "Clear all cached file properties and the comment ring."
  (interactive)
  (fillarray vc-file-prop-obarray nil)
  ;; Note: there is potential for minor lossage here if there is an open
  ;; log buffer with a nonzero local value of vc-comment-ring-index.
712
  (setq vc-comment-ring (make-ring vc-maximum-comment-ring-size)))
713

714
(defmacro with-vc-properties (file form settings)
715 716 717 718
  "Execute FORM, then set per-file properties for FILE,
but only those that have not been set during the execution of FORM.
SETTINGS is a list of two-element lists, each of which has the
  form (PROPERTY . VALUE)."
719 720 721 722
  `(let ((vc-touched-properties (list t))
	 (filename ,file))
     ,form
     (mapcar (lambda (setting)
723
	       (let ((property (car setting)))
724
		 (unless (memq property vc-touched-properties)
725
		   (put (intern filename vc-file-prop-obarray)
726
			property (cdr setting)))))
727 728
	     ,settings)))

Eric S. Raymond's avatar
Eric S. Raymond committed
729 730
;; Random helper functions

731 732
(defsubst vc-editable-p (file)
  (or (eq (vc-checkout-model file) 'implicit)
733
      (memq (vc-state file) '(edited needs-merge))))
734

735
;; Two macros for elisp programming
736 737
;;;###autoload
(defmacro with-vc-file (file comment &rest body)
738 739 740 741
  "Check out a writable copy of FILE if necessary and execute the body.
Check in FILE with COMMENT (a string) after BODY has been executed.
FILE is passed through `expand-file-name'; BODY executed within
`save-excursion'.  If FILE is not under version control, or locked by
742 743
somebody else, signal error."
  `(let ((file (expand-file-name ,file)))
744
     (or (vc-backend file)
745
	 (error (format "File not under version control: `%s'" file)))
746 747 748 749
     (unless (vc-editable-p file)
       (let ((state (vc-state file)))
	 (if (stringp state) (error (format "`%s' is locking `%s'" state file))
	   (vc-checkout file t))))
750 751 752
     (save-excursion
       ,@body)
     (vc-checkin file nil ,comment)))
753
(put 'with-vc-file 'indent-function 1)
754 755 756

;;;###autoload
(defmacro edit-vc-file (file comment &rest body)
757 758
  "Edit FILE under version control, executing body.
Checkin with COMMENT after executing BODY.
759 760 761 762
This macro uses `with-vc-file', passing args to it.
However, before executing BODY, find FILE, and after BODY, save buffer."
  `(with-vc-file
    ,file ,comment
763
    (set-buffer (find-file-noselect ,file))
764 765
    ,@body
    (save-buffer)))
766
(put 'edit-vc-file 'indent-function 1)
767

768
(defun vc-ensure-vc-buffer ()
769
  "Make sure that the current buffer visits a version-controlled file."
770 771 772 773 774 775 776 777
  (if vc-dired-mode
      (set-buffer (find-file-noselect (dired-get-filename)))
    (while vc-parent-buffer
      (pop-to-buffer vc-parent-buffer))
    (if (not (buffer-file-name))
	(error "Buffer %s is not associated with a file" (buffer-name))
      (if (not (vc-backend (buffer-file-name)))
	  (error "File %s is not under version control" (buffer-file-name))))))
778

Eric S. Raymond's avatar
Eric S. Raymond committed
779
(defvar vc-binary-assoc nil)
780 781 782 783
(defvar vc-binary-suffixes
  (if (memq system-type '(ms-dos windows-nt))
      '(".exe" ".com" ".bat" ".cmd" ".btm" "")
    '("")))
784 785

(defun vc-process-filter (p s)
786
  "An alternative output filter for async process P.
787 788 789 790 791 792 793 794 795
The only difference with the default filter is to insert S after markers."
  (with-current-buffer (process-buffer p)
    (save-excursion
      (let ((inhibit-read-only t))
	(goto-char (process-mark p))
	(insert s)
	(set-marker (process-mark p) (point))))))

(defun vc-setup-buffer (&optional buf)
796
  "Prepare BUF for executing a VC command and make it the current buffer.
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
BUF defaults to \"*vc*\", can be a string and will be created if necessary."
  (unless buf (setq buf "*vc*"))
  (let ((camefrom (current-buffer))
	(olddir default-directory))
    (set-buffer (get-buffer-create buf))
    (kill-all-local-variables)
    (set (make-local-variable 'vc-parent-buffer) camefrom)
    (set (make-local-variable 'vc-parent-buffer-name)
	 (concat " from " (buffer-name camefrom)))
    (setq default-directory olddir)
    (let ((inhibit-read-only t))
      (erase-buffer))))

(defun vc-exec-after (code)
  "Eval CODE when the current buffer's process is done.
If the current buffer has no process, just evaluate CODE.
Else, add CODE to the process' sentinel."
  (let ((proc (get-buffer-process (current-buffer))))
    (cond
     ;; If there's no background process, just execute the code.
     ((null proc) (eval code))
     ;; If the background process has exited, reap it and try again
     ((eq (process-status proc) 'exit)
      (delete-process proc)
      (vc-exec-after code))
     ;; If a process is running, add CODE to the sentinel
     ((eq (process-status proc) 'run)
      (let ((sentinel (process-sentinel proc)))
	(set-process-sentinel proc
	  `(lambda (p s)
	     (with-current-buffer ',(current-buffer)
	       (goto-char (process-mark p))
829
	       ,@(append (cdr (cdr (cdr ;strip off `with-current-buffer buf
830 831 832 833 834 835 836 837 838 839 840 841
                                        ;             (goto-char...)'
			   (car (cdr (cdr ;strip off `lambda (p s)'
			    sentinel))))))
			 (list `(vc-exec-after ',code))))))))
     (t (error "Unexpected process state"))))
  nil)

(defvar vc-post-command-functions nil
  "Hook run at the end of `vc-do-command'.
Each function is called inside the buffer in which the command was run
and is passed 3 argument: the COMMAND, the FILE and the FLAGS.")

Stefan Monnier's avatar
Stefan Monnier committed
842
;;;###autoload
843
(defun vc-do-command (buffer okstatus command file &rest flags)
844
  "Execute a version control command, notifying user and checking for errors.
845 846 847 848 849 850 851 852
Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
current buffer if BUFFER is t.  If the destination buffer is not
already current, set it up properly and erase it.  The command is
considered successful if its exit status does not exceed OKSTATUS (if
OKSTATUS is nil, that means to ignore errors, if it is 'async, that
means not to wait for termination of the subprocess).  FILE is the
name of the working file (may also be nil, to execute commands that
don't expect a file name).  If an optional list of FLAGS is present,
853
that is inserted into the command line before the filename."
854
  (and file (setq file (expand-file-name file)))
Eric S. Raymond's avatar
Eric S. Raymond committed
855
  (if vc-command-messages
856
      (message "Running %s on %s..." command file))
857
  (save-current-buffer
858 859 860 861 862
    (unless (or (eq buffer t)
                (and (stringp buffer)
                     (string= (buffer-name) buffer))
                (eq buffer (current-buffer)))
      (vc-setup-buffer buffer))
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
    (let ((squeezed nil)
	  (inhibit-read-only t)
	  (status 0))
      (setq squeezed (delq nil (copy-sequence flags)))
      (when file
	;; FIXME: file-relative-name can return a bogus result because
	;; it doesn't look at the actual file-system to see if symlinks
	;; come into play.
	(setq squeezed (append squeezed (list (file-relative-name file)))))
      (let ((exec-path (append vc-path exec-path))
	    ;; Add vc-path to PATH for the execution of this command.
	    (process-environment
	     (cons (concat "PATH=" (getenv "PATH")
			   path-separator
			   (mapconcat 'identity vc-path path-separator))
		   process-environment))
	    (w32-quote-process-args t))
	(if (eq okstatus 'async)
	    (let ((proc (apply 'start-process command (current-buffer) command
			       squeezed)))
883 884
              (unless (active-minibuffer-window)
                (message "Running %s in the background..." command))
885 886 887
	      ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
	      (set-process-filter proc 'vc-process-filter)
	      (vc-exec-after
888 889
	       `(unless (active-minibuffer-window)
                  (message "Running %s in the background... done" ',command))))
890 891 892 893 894 895 896 897 898 899 900 901
	  (setq status (apply 'call-process command nil t nil squeezed))
	  (when (or (not (integerp status)) (and okstatus (< okstatus status)))
	    (pop-to-buffer (current-buffer))
	    (goto-char (point-min))
	    (shrink-window-if-larger-than-buffer)
	    (error "Running %s...FAILED (%s)" command
		   (if (integerp status) (format "status %d" status) status))))
	(if vc-command-messages
	    (message "Running %s...OK" command)))
      (vc-exec-after
       `(run-hook-with-args 'vc-post-command-functions ',command ',file ',flags))
      status)))
Eric S. Raymond's avatar
Eric S. Raymond committed
902

903
(defun vc-position-context (posn)
904 905 906
  "Save a bit of the text around POSN in the current buffer.
Used to help us find the corresponding position again later
if markers are destroyed or corrupted."
907 908
  ;; A lot of this was shamelessly lifted from Sebastian Kremer's
  ;; rcs.el mode.
909 910 911 912 913 914
  (list posn
	(buffer-size)
	(buffer-substring posn
			  (min (point-max) (+ posn 100)))))

(defun vc-find-position-by-context (context)
915
  "Return the position of CONTEXT in the current buffer, or nil if not found."
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
  (let ((context-string (nth 2 context)))
    (if (equal "" context-string)
	(point-max)
      (save-excursion
	(let ((diff (- (nth 1 context) (buffer-size))))
	  (if (< diff 0) (setq diff (- diff)))
	  (goto-char (nth 0 context))
	  (if (or (search-forward context-string nil t)
		  ;; Can't use search-backward since the match may continue
		  ;; after point.
		  (progn (goto-char (- (point) diff (length context-string)))
			 ;; goto-char doesn't signal an error at
			 ;; beginning of buffer like backward-char would
			 (search-forward context-string nil t)))
	      ;; to beginning of OSTRING
	      (- (point) (length context-string))))))))

933
(defun vc-context-matches-p (posn context)
934
  "Return t if POSN matches CONTEXT, nil otherwise."
935 936 937 938 939 940 941
  (let* ((context-string (nth 2 context))
	 (len (length context-string))
	 (end (+ posn len)))
    (if (> end (1+ (buffer-size)))
	nil
      (string= context-string (buffer-substring posn end)))))

942
(defun vc-buffer-context ()
943 944
  "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
Used by `vc-restore-buffer-context' to later restore the context."
945
  (let ((point-context (vc-position-context (point)))
946 947 948 949
	;; Use mark-marker to avoid confusion in transient-mark-mode.
	(mark-context  (if (eq (marker-buffer (mark-marker)) (current-buffer))
			   (vc-position-context (mark-marker))))
	;; Make the right thing happen in transient-mark-mode.
950 951 952 953 954 955 956 957
	(mark-active nil)
	;; We may want to reparse the compilation buffer after revert
	(reparse (and (boundp 'compilation-error-list) ;compile loaded
		      (let ((curbuf (current-buffer)))
			;; Construct a list; each elt is nil or a buffer
			;; iff that buffer is a compilation output buffer
			;; that contains markers into the current buffer.
			(save-excursion
958
			  (mapcar (lambda (buffer)
959 960 961 962 963
				    (set-buffer buffer)
				    (let ((errors (or
						   compilation-old-error-list
						   compilation-error-list))
					  (buffer-error-marked-p nil))
964
				      (while (and (consp errors)
965
						  (not buffer-error-marked-p))
966
					(and (markerp (cdr (car errors)))
967 968
					     (eq buffer
						 (marker-buffer
969
						  (cdr (car errors))))
970
					     (setq buffer-error-marked-p t))
971
					(setq errors (cdr errors)))
972
				      (if buffer-error-marked-p buffer)))
973
				  (buffer-list)))))))
974 975 976
    (list point-context mark-context reparse)))

(defun vc-restore-buffer-context (context)
977
  "Restore point/mark, and reparse any affected compilation buffers.
978
CONTEXT is that which `vc-buffer-context' returns."
979 980 981
  (let ((point-context (nth 0 context))
	(mark-context (nth 1 context))
	(reparse (nth 2 context)))
982 983 984
    ;; Reparse affected compilation buffers.
    (while reparse
      (if (car reparse)
985
	  (with-current-buffer (car reparse)
986 987 988 989 990 991 992 993 994 995
	    (let ((compilation-last-buffer (current-buffer)) ;select buffer
		  ;; Record the position in the compilation buffer of
		  ;; the last error next-error went to.
		  (error-pos (marker-position
			      (car (car-safe compilation-error-list)))))
	      ;; Reparse the error messages as far as they were parsed before.
	      (compile-reinitialize-errors '(4) compilation-parsing-end)
	      ;; Move the pointer up to find the error we were at before
	      ;; reparsing.  Now next-error should properly go to the next one.
	      (while (and compilation-error-list
996
			  (/= error-pos (car (car compilation-error-list))))
997 998
		(setq compilation-error-list (cdr compilation-error-list))))))
      (setq reparse (cdr reparse)))
999

1000 1001 1002 1003
    ;; if necessary, restore point and mark
    (if (not (vc-context-matches-p (point) point-context))
	(let ((new-point (vc-find-position-by-context point-context)))
	  (if new-point (goto-char new-point))))
1004 1005 1006 1007 1008
    (and mark-active
         mark-context
         (not (vc-context-matches-p (mark) mark-context))
         (let ((new-mark (vc-find-position-by-context mark-context)))
           (if new-mark (set-mark new-mark))))))
1009

1010
(defun vc-revert-buffer1 (&optional arg no-confirm)
1011 1012 1013 1014
  "Revert buffer, trying to keep point and mark where user expects them.
Tries to be clever in the face of changes due to expanded version control
key words.  This is important for typeahead to work as expected.
ARG and NO-CONFIRM are passed on to `revert-buffer'."
1015 1016 1017
  (interactive "P")
  (widen)
  (let ((context (vc-buffer-context)))
1018 1019
    ;; Use save-excursion here, because it may be able to restore point
    ;; and mark properly even in cases where vc-restore-buffer-context
1020
    ;; would fail.  However, save-excursion might also get it wrong --
1021 1022
    ;; in this case, vc-restore-buffer-context gives it a second try.
    (save-excursion
1023
      ;; t means don't call normal-mode;
1024 1025
      ;; that's to preserve various minor modes.
      (revert-buffer arg no-confirm t))
1026 1027
    (vc-restore-buffer-context context)))

Eric S. Raymond's avatar
Eric S. Raymond committed
1028

1029
(defun vc-buffer-sync (&optional not-urgent)
1030
  "Make sure the current buffer and its working file are in sync.
1031
NOT-URGENT means it is ok to continue if the user says not to save."
1032
  (if (buffer-modified-p)
1033 1034 1035
      (if (or vc-suppress-confirm
	      (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
	  (save-buffer)
1036
	(unless not-urgent
1037 1038
	  (error "Aborted")))))

André Spiegel's avatar
André Spiegel committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
(defun vc-workfile-unchanged-p (file)
  "Has FILE changed since last checkout?"
  (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
        (lastmod (nth 5 (file-attributes file))))
    (if checkout-time
        (equal checkout-time lastmod)
      (let ((unchanged (vc-call workfile-unchanged-p file)))
        (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
        unchanged))))

1049
(defun vc-default-workfile-unchanged-p (backend file)
André Spiegel's avatar
André Spiegel committed
1050 1051 1052
  "Default check whether FILE is unchanged: diff against master version."
  (zerop (vc-call diff file (vc-workfile-version file))))

1053
(defun vc-default-latest-on-branch-p (backend file)
1054 1055 1056 1057
  "Default check whether the current workfile version of FILE is the 
latest on its branch."
  t)

1058 1059 1060 1061 1062
(defun vc-recompute-state (file)
  "Force a recomputation of the version control state of FILE.
The state is computed using the exact, and possibly expensive
function `vc-BACKEND-state', not the heuristic."
  (vc-file-setprop file 'vc-state (vc-call state file)))
1063

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
(defun vc-next-action-on-file (file verbose &optional comment)
  "Do The Right Thing for a given version-controlled FILE.
If COMMENT is specified, it will be used as an admin or checkin comment.
If VERBOSE is non-nil, query the user rather than using default parameters."
  (let ((visited (get-file-buffer file))
	state version)
    (when visited
      ;; Check relation of buffer and file, and make sure
      ;; user knows what he's doing.  First, finding the file
      ;; will check whether the file on disk is newer.
      (if vc-dired-mode
	  (find-file-other-window file)
1076
	(set-buffer (find-file-noselect file)))
1077 1078 1079 1080 1081 1082 1083 1084 1085
      (if (not (verify-visited-file-modtime (current-buffer)))
	  (if (yes-or-no-p "Replace file on disk with buffer contents? ")
	      (write-file (buffer-file-name))
	    (error "Aborted"))
	;; Now, check if we have unsaved changes.
	(vc-buffer-sync t)
	(if (buffer-modified-p)
	    (or (y-or-n-p "Operate on disk file, keeping modified buffer? ")
		(error "Aborted")))))
1086

1087
    ;; Do the right thing
1088
    (if (not (vc-registered file))
1089 1090
	(vc-register verbose comment)
      (vc-recompute-state file)