pcvs.el 85.8 KB
Newer Older
1
;;; pcvs.el --- a front-end to CVS
Stefan Monnier's avatar
Stefan Monnier committed
2

3
;; Copyright (C) 1991,92,93,94,95,95,97,98,99,2000,02,2003
Stefan Monnier's avatar
Stefan Monnier committed
4
;; 		 Free Software Foundation, Inc.
Stefan Monnier's avatar
Stefan Monnier committed
5 6 7 8 9 10 11

;; Author: (The PCL-CVS Trust) pcl-cvs@cyclic.com
;;	(Per Cederqvist) ceder@lysator.liu.se
;;	(Greg A. Woods) woods@weird.com
;;	(Jim Blandy) jimb@cyclic.com
;;	(Karl Fogel) kfogel@floss.red-bean.com
;;	(Jim Kingdon) kingdon@cyclic.com
12 13 14
;;	(Stefan Monnier) monnier@cs.yale.edu
;;	(Greg Klanderman) greg@alphatech.com
;;	(Jari Aalto+mail.emacs) jari.aalto@poboxes.com
Stefan Monnier's avatar
Stefan Monnier committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
;; Maintainer: (Stefan Monnier) monnier+lists/cvs/pcl@flint.cs.yale.edu
;; Keywords: CVS, version control, release management

;; 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
;; 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.

;;; Commentary:

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
;; PCL-CVS is a front-end to the CVS version control system.  For people
;; familiar with VC, it is somewhat like VC-dired: it presents the status of
;; all the files in your working area and allows you to commit/update several
;; of them at a time.  Compared to VC-dired, it is considerably better and
;; faster (but only for CVS).

;; PCL-CVS was originally written by Per Cederqvist many years ago.  This
;; version derives from the XEmacs-21 version, itself based on the 2.0b2
;; version (last release from Per).  It is a thorough rework.

;; Contrary to what you'd expect, PCL-CVS is not a replacement for VC but only
;; for VC-dired.  As such, I've tried to make PCL-CVS and VC interoperate
;; seamlessly (I also use VC).

;; To use PCL-CVS just use `M-x cvs-examine RET <dir> RET'.
52
;; There is a TeXinfo manual, which can be helpful to get started.
53

54 55
;;; Bugs:

Stefan Monnier's avatar
Stefan Monnier committed
56 57
;; - Extracting an old version seems not to recognize encoding correctly.
;;   That's probably because it's done via a process rather than a file.
58

Stefan Monnier's avatar
Stefan Monnier committed
59 60
;;; Todo:

61
;; ******** FIX THE DOCUMENTATION *********
62
;;
63
;; - rework the displaying of error messages.
64 65 66
;; - allow to flush messages only
;; - allow to protect files like ChangeLog from flushing
;; - automatically cvs-mode-insert files from find-file-hook
67
;;   (and don't flush them as long as they are visited)
68 69 70 71 72 73
;; - query the user for cvs-get-marked (for some cmds or if nothing's selected)
;; - don't return the first (resp last) FI if the cursor is before
;;   (resp after) it.
;; - allow cvs-confirm-removals to force always confirmation.
;; - cvs-checkout should ask for a revision (with completion).
;; - removal confirmation should allow specifying another file name.
74
;;
75
;; - hide fileinfos without getting rid of them (will require ewok work).
76 77 78 79
;; - add toolbar entries
;; - marking
;;    marking directories should jump to just after the dir.
;;    allow (un)marking directories at a time with the mouse.
80 81
;;    allow cvs-cmd-do to either clear the marks or not.
;;    add a "marks active" notion, like transient-mark-mode does.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
;; - liveness indicator
;; - indicate in docstring if the cmd understands the `b' prefix(es).
;; - call smerge-mode when opening CONFLICT files.
;; - have vc-checkin delegate to cvs-mode-commit when applicable
;; - higher-level CVS operations
;;    cvs-mode-rename
;;    cvs-mode-branch
;; - module-level commands
;;    add support for parsing 'modules' file ("cvs co -c")
;;    cvs-mode-rcs2log
;;    cvs-rdiff
;;    cvs-release
;;    cvs-import
;;    C-u M-x cvs-checkout should ask for a cvsroot
;;    cvs-mode-handle-new-vendor-version
Stefan Monnier's avatar
Stefan Monnier committed
97 98
;; 	- checks out module, or alternately does update join
;; 	- does "cvs -n tag LAST_VENDOR" to find old files into *cvs*
99
;;    cvs-export
100
;; 	(with completion on tag names and hooks to help generate full releases)
101 102
;; - display stickiness information.  And current CVS/Tag as well.
;; - write 'cvs-mode-admin' to do arbitrary 'cvs admin' commands
103 104
;;   Most interesting would be version removal and log message replacement.
;;   The last one would be neat when called from log-view-mode.
105
;; - cvs-mode-incorporate
106
;; 	It would merge in the status from one *cvs* buffer into another.
Stefan Monnier's avatar
Stefan Monnier committed
107 108
;; 	This would be used to populate such a buffer that had been created with
;; 	a `cvs {update,status,checkout} -l'.
109 110
;; - cvs-mode-(i)diff-other-{file,buffer,cvs-buffer}
;; - offer the choice to kill the process when the user kills the cvs buffer.
Stefan Monnier's avatar
Stefan Monnier committed
111
;; 	right now, it's killed without further ado.
112
;; - make `cvs-mode-ignore' allow manually entering a pattern.
Stefan Monnier's avatar
Stefan Monnier committed
113
;; 	to which dir should it apply ?
114 115 116
;; - cvs-mode-ignore should try to remove duplicate entries.
;; - maybe poll/check CVS/Entries files to react to external `cvs' commands ?
;; - some kind of `cvs annotate' support ?
Stefan Monnier's avatar
Stefan Monnier committed
117
;; 	but vc-annotate can be used instead.
118 119
;; - proper `g' that passes safe args and uses either cvs-status or cvs-examine
;;   maybe also use cvs-update depending on I-don't-know-what.
120
;; - add message-levels so that we can hide some levels of messages
Stefan Monnier's avatar
Stefan Monnier committed
121 122 123

;;; Code:

124
(eval-when-compile (require 'cl))
Stefan Monnier's avatar
Stefan Monnier committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
(require 'ewoc)				;Ewoc was once cookie
(require 'pcvs-defs)
(require 'pcvs-util)
(require 'pcvs-parse)
(require 'pcvs-info)


;;;;
;;;; global vars
;;;;

(defvar cvs-cookies) ;;nil
  ;;"Handle for the cookie structure that is displayed in the *cvs* buffer.")
;;(make-variable-buffer-local 'cvs-cookies)

;;;;
;;;; Dynamically scoped variables
;;;;

(defvar cvs-from-vc nil "Bound to t inside VC advice.")

146
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
147
;;;; flags variables
148
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
149 150 151 152

(defun cvs-defaults (&rest defs)
  (let ((defs (cvs-first defs cvs-shared-start)))
    (append defs
153
	    (make-list (- cvs-shared-start (length defs)) (car defs))
Stefan Monnier's avatar
Stefan Monnier committed
154 155 156 157 158 159 160 161 162 163
	    cvs-shared-flags)))

;; For cvs flags, we need to add "-f" to override the cvsrc settings
;; we also want to evict the annoying -q and -Q options that hide useful
;; information from pcl-cvs.
(cvs-flags-define cvs-cvs-flags '(("-f")))

(cvs-flags-define cvs-checkout-flags (cvs-defaults '("-P")))
(cvs-flags-define cvs-status-flags (cvs-defaults '("-v") nil))
(cvs-flags-define cvs-log-flags (cvs-defaults nil))
164
(cvs-flags-define cvs-diff-flags (cvs-defaults '("-u" "-N") '("-c" "-N") '("-u" "-b")))
Stefan Monnier's avatar
Stefan Monnier committed
165 166 167 168 169 170 171 172 173 174
(cvs-flags-define cvs-tag-flags (cvs-defaults nil))
(cvs-flags-define cvs-add-flags (cvs-defaults nil))
(cvs-flags-define cvs-commit-flags (cvs-defaults nil))
(cvs-flags-define cvs-remove-flags (cvs-defaults nil))
;;(cvs-flags-define cvs-undo-flags (cvs-defaults nil))
(cvs-flags-define cvs-update-flags (cvs-defaults '("-d" "-P")))

(defun cvs-reread-cvsrc ()
  "Reset the default arguments to those in the `cvs-cvsrc-file'."
  (interactive)
175 176 177 178 179 180 181
  (condition-case nil
      (with-temp-buffer
	(insert-file-contents cvs-cvsrc-file)
	;; fetch the values
	(dolist (cmd '("cvs" "checkout" "status" "log" "diff" "tag"
		       "add" "commit" "remove" "update"))
	  (goto-char (point-min))
182 183 184 185 186
	  (when (re-search-forward
		 (concat "^" cmd "\\(\\s-+\\(.*\\)\\)?$") nil t)
	    (let* ((sym (intern (concat "cvs-" cmd "-flags")))
		   (val (cvs-string->strings (or (match-string 2) ""))))
	      (cvs-flags-set sym 0 val))))
187 188 189 190
	;; ensure that cvs doesn't have -q or -Q
	(cvs-flags-set 'cvs-cvs-flags 0
		       (cons "-f"
			     (cdr (cvs-partition
191
				   (lambda (x) (member x '("-q" "-Q" "-f")))
192 193 194
				   (cvs-flags-query 'cvs-cvs-flags
						    nil 'noquery))))))
      (file-error nil)))
Stefan Monnier's avatar
Stefan Monnier committed
195 196 197 198 199 200 201 202 203

;; initialize to cvsrc's default values
(cvs-reread-cvsrc)


;;;;
;;;; Mouse bindings and mode motion
;;;;

204 205
(defvar cvs-minor-current-files)

Stefan Monnier's avatar
Stefan Monnier committed
206 207 208
(defun cvs-menu (e)
  "Popup the CVS menu."
  (interactive "e")
209 210 211
  (let ((cvs-minor-current-files
	 (list (ewoc-data (ewoc-locate
			   cvs-cookies (posn-point (event-end e)))))))
212
    (popup-menu cvs-menu e)))
Stefan Monnier's avatar
Stefan Monnier committed
213 214 215 216 217

(defvar cvs-mode-line-process nil
  "Mode-line control for displaying info on cvs process status.")


218
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
219
;;;; Query-Type-Descriptor for Tags
220
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

(autoload 'cvs-status-get-tags "cvs-status")
(defun cvs-tags-list ()
  "Return a list of acceptable tags, ready for completions."
  (assert (cvs-buffer-p))
  (let ((marked (cvs-get-marked)))
    (list* '("BASE") '("HEAD")
	   (when marked
	     (with-temp-buffer
	       (call-process cvs-program
			     nil	;no input
			     t		;output to current-buffer
			     nil	;don't update display while running
			     "status"
			     "-v"
			     (cvs-fileinfo->full-path (car marked)))
	       (goto-char (point-min))
	       (let ((tags (cvs-status-get-tags)))
		 (when (listp tags) tags)))))))

(defvar cvs-tag-history nil)
(defconst cvs-qtypedesc-tag
  (cvs-qtypedesc-create 'identity 'identity 'cvs-tags-list 'cvs-tag-history))

245
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
246

247
(defun cvs-mode! (&optional -cvs-mode!-fun)
Stefan Monnier's avatar
Stefan Monnier committed
248 249 250 251 252 253 254
  "Switch to the *cvs* buffer.
If -CVS-MODE!-FUN is provided, it is executed *cvs* being the current buffer
  and with its window selected.  Else, the *cvs* buffer is simply selected.
-CVS-MODE!-FUN is called interactively if applicable and else with no argument."
  (let* ((-cvs-mode!-buf (current-buffer))
	 (cvsbuf (cond ((cvs-buffer-p) (current-buffer))
		       ((and cvs-buffer (cvs-buffer-p cvs-buffer)) cvs-buffer)
255
		       (t (error "can't find the *cvs* buffer"))))
Stefan Monnier's avatar
Stefan Monnier committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	 (-cvs-mode!-wrapper cvs-minor-wrap-function)
	 (-cvs-mode!-cont (lambda ()
			    (save-current-buffer
			      (if (commandp -cvs-mode!-fun)
				  (call-interactively -cvs-mode!-fun)
				(funcall -cvs-mode!-fun))))))
    (if (not -cvs-mode!-fun) (set-buffer cvsbuf)
      (let ((cvs-mode!-buf (current-buffer))
	    (cvs-mode!-owin (selected-window))
	    (cvs-mode!-nwin (get-buffer-window cvsbuf 'visible)))
	(unwind-protect
	    (progn
	      (set-buffer cvsbuf)
	      (when cvs-mode!-nwin (select-window cvs-mode!-nwin))
	      (if -cvs-mode!-wrapper
		  (funcall -cvs-mode!-wrapper -cvs-mode!-buf -cvs-mode!-cont)
		(funcall -cvs-mode!-cont)))
	  (set-buffer cvs-mode!-buf)
	  (when (and cvs-mode!-nwin (eq cvs-mode!-nwin (selected-window)))
	    ;; the selected window has not been changed by FUN
	    (select-window cvs-mode!-owin)))))))

278
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
279
;;;; Prefixes
280
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

(defvar cvs-branches (list cvs-vendor-branch "HEAD" "HEAD"))
(cvs-prefix-define cvs-branch-prefix
  "Current selected branch."
  "version"
  (cons cvs-vendor-branch cvs-branches)
  cvs-qtypedesc-tag)

(defun cvs-set-branch-prefix (arg)
  "Set the branch prefix to take action at the next command.
See `cvs-prefix-set' for a further the description of the behavior.
\\[universal-argument] 1 selects the vendor branch
and \\[universal-argument] 2 selects the HEAD."
  (interactive "P")
  (cvs-mode!)
  (cvs-prefix-set 'cvs-branch-prefix arg))

(defun cvs-add-branch-prefix (flags &optional arg)
  "Add branch selection argument if the branch prefix was set.
The argument is added (or not) to the list of FLAGS and is constructed
by appending the branch to ARG which defaults to \"-r\"."
  (let ((branch (cvs-prefix-get 'cvs-branch-prefix)))
    ;; deactivate the secondary prefix, even if not used.
    (cvs-prefix-get 'cvs-secondary-branch-prefix)
    (if branch (cons (concat (or arg "-r") branch) flags) flags)))

(cvs-prefix-define cvs-secondary-branch-prefix
  "Current secondary selected branch."
  "version"
  (cons cvs-vendor-branch cvs-branches)
  cvs-qtypedesc-tag)

(defun cvs-set-secondary-branch-prefix (arg)
  "Set the branch prefix to take action at the next command.
See `cvs-prefix-set' for a further the description of the behavior.
\\[universal-argument] 1 selects the vendor branch
and \\[universal-argument] 2 selects the HEAD."
  (interactive "P")
  (cvs-mode!)
  (cvs-prefix-set 'cvs-secondary-branch-prefix arg))

(defun cvs-add-secondary-branch-prefix (flags &optional arg)
  "Add branch selection argument if the secondary branch prefix was set.
The argument is added (or not) to the list of FLAGS and is constructed
by appending the branch to ARG which defaults to \"-r\".
Since the `cvs-secondary-branch-prefix' is only active if the primary
prefix is active, it is important to read the secondary prefix before
the primay since reading the primary can deactivate it."
  (let ((branch (and (cvs-prefix-get 'cvs-branch-prefix 'read-only)
		     (cvs-prefix-get 'cvs-secondary-branch-prefix))))
    (if branch (cons (concat (or arg "-r") branch) flags) flags)))

333
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
334

335 336
(define-minor-mode cvs-minor-mode
  "This mode is used for buffers related to a main *cvs* buffer.
Stefan Monnier's avatar
Stefan Monnier committed
337
All the `cvs-mode' buffer operations are simply rebound under
338
the \\[cvs-mode-map] prefix."
339 340
  nil " CVS"
  :group 'pcl-cvs)
Stefan Monnier's avatar
Stefan Monnier committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
(put 'cvs-minor-mode 'permanent-local t)


(defvar cvs-temp-buffers nil)
(defun cvs-temp-buffer (&optional cmd normal nosetup)
  "Create a temporary buffer to run CMD in.
If CMD is a string, use it to lookup `cvs-buffer-name-alist' to find
the buffer name to be used and its `major-mode'.

The selected window will not be changed.  The new buffer will not maintain undo
information and will be read-only unless NORMAL is non-nil.  It will be emptied
\(unless NOSETUP is non-nil\) and its `default-directory' will be inherited
from the current buffer."
  (let* ((cvs-buf (current-buffer))
	 (info (cdr (assoc cmd cvs-buffer-name-alist)))
356 357
	 (name (eval (nth 0 info)))
	 (mode (nth 1 info))
Stefan Monnier's avatar
Stefan Monnier committed
358 359 360 361 362 363 364 365 366
	 (dir default-directory)
	 (buf (cond
	       (name (cvs-get-buffer-create name))
	       ((and (bufferp cvs-temp-buffer) (buffer-name cvs-temp-buffer))
		cvs-temp-buffer)
	       (t
		(set (make-local-variable 'cvs-temp-buffer)
		     (cvs-get-buffer-create
		      (eval cvs-temp-buffer-name) 'noreuse))))))
367

Stefan Monnier's avatar
Stefan Monnier committed
368 369 370 371
    ;; handle the potential pre-existing process
    (let ((proc (get-buffer-process buf)))
      (when (and (not normal) (processp proc)
		 (memq (process-status proc) '(run stop)))
372 373 374 375 376 377 378 379
	(if cmd
	    ;; When CMD is specified, the buffer is normally shown to the
	    ;; user, so interrupting the process is not harmful.
	    ;; Use `delete-process' rather than `kill-process' otherwise
	    ;; the pending output of the process will still get inserted
	    ;; after we erase the buffer.
	    (delete-process proc)
	  (error "Can not run two cvs processes simultaneously"))))
Stefan Monnier's avatar
Stefan Monnier committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 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

    (if (not name) (kill-local-variable 'other-window-scroll-buffer)
      ;; Strangely, if no window is created, `display-buffer' ends up
      ;; doing a `switch-to-buffer' which does a `set-buffer', hence
      ;; the need for `save-excursion'.
      (unless nosetup (save-excursion (display-buffer buf)))
      ;; FIXME: this doesn't do the right thing if the user later on
      ;; does a `find-file-other-window' and `scroll-other-window'
      (set (make-local-variable 'other-window-scroll-buffer) buf))

    (add-to-list 'cvs-temp-buffers buf)

    (with-current-buffer buf
      (setq buffer-read-only nil)
      (setq default-directory dir)
      (unless nosetup (erase-buffer))
      (set (make-local-variable 'cvs-buffer) cvs-buf)
      ;;(cvs-minor-mode 1)
      (let ((lbd list-buffers-directory))
	(if (fboundp mode) (funcall mode) (fundamental-mode))
	(when lbd (set (make-local-variable 'list-buffers-directory) lbd)))
      (cvs-minor-mode 1)
      ;;(set (make-local-variable 'cvs-buffer) cvs-buf)
      (unless normal
	(setq buffer-read-only t)
	(buffer-disable-undo))
      buf)))

(defun cvs-mode-kill-buffers ()
  "Kill all the \"temporary\" buffers created by the *cvs* buffer."
  (interactive)
  (dolist (buf cvs-temp-buffers) (ignore-errors (kill-buffer buf))))

(defun cvs-make-cvs-buffer (dir &optional new)
  "Create the *cvs* buffer for directory DIR.
If non-nil, NEW means to create a new buffer no matter what."
  ;; the real cvs-buffer creation
  (setq dir (cvs-expand-dir-name dir))
  (let* ((buffer-name (eval cvs-buffer-name))
	 (buffer
	  (or (and (not new)
		   (eq cvs-reuse-cvs-buffer 'current)
		   (cvs-buffer-p)	;reuse the current buffer if possible
		   (current-buffer))
	      ;; look for another cvs buffer visiting the same directory
	      (save-excursion
		(unless new
		  (dolist (buffer (cons (current-buffer) (buffer-list)))
		    (set-buffer buffer)
		    (and (cvs-buffer-p)
			 (case cvs-reuse-cvs-buffer
			   (always t)
			   (subdir
			    (or (cvs-string-prefix-p default-directory dir)
				(cvs-string-prefix-p dir default-directory)))
			   (samedir (string= default-directory dir)))
			 (return buffer)))))
	      ;; we really have to create a new buffer:
	      ;; we temporarily bind cwd to "" to prevent
	      ;; create-file-buffer from using directory info
	      ;; unless it is explicitly in the cvs-buffer-name.
	      (cvs-get-buffer-create buffer-name new))))
    (with-current-buffer buffer
      (or
       (and (string= dir default-directory) (cvs-buffer-p)
	    ;; just a refresh
	    (ignore-errors
	      (cvs-cleanup-collection cvs-cookies nil nil t)
	      (current-buffer)))
       ;; setup from scratch
       (progn
	 (setq default-directory dir)
	 (setq buffer-read-only nil)
	 (erase-buffer)
454 455 456 457 458 459 460 461 462 463
	 (insert "Repository : " (directory-file-name (cvs-get-cvsroot))
		 "\nModule     : " (cvs-get-module)
		 "\nWorking dir: " (abbreviate-file-name dir)
		 (if (not (file-readable-p "CVS/Tag")) "\n"
		   (let ((tag (cvs-file-to-string "CVS/Tag")))
		     (cond
		      ((string-match "\\`T" tag)
		       (concat "\nTag        : " (substring tag 1)))
		      ((string-match "\\`D" tag)
		       (concat "\nDate       : " (substring tag 1)))
464 465
		      ("\n"))))
		 "\n")
Stefan Monnier's avatar
Stefan Monnier committed
466 467 468 469
	 (setq buffer-read-only t)
	 (cvs-mode)
	 (set (make-local-variable 'list-buffers-directory) buffer-name)
	 ;;(set (make-local-variable 'cvs-temp-buffer) (cvs-temp-buffer))
470
	 (let ((cookies (ewoc-create 'cvs-fileinfo-pp "\n" "")))
Stefan Monnier's avatar
Stefan Monnier committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	   (set (make-local-variable 'cvs-cookies) cookies)
	   (add-hook 'kill-buffer-hook
		     (lambda ()
		       (ignore-errors (kill-buffer cvs-temp-buffer)))
		     nil t)
	   ;;(set-buffer buf)
	   buffer))))))

(defun* cvs-cmd-do (cmd dir flags fis new
			&key cvsargs noexist dont-change-disc noshow)
  (let* ((dir (file-name-as-directory
	       (abbreviate-file-name (expand-file-name dir))))
	 (cvsbuf (cvs-make-cvs-buffer dir new)))
    ;; Check that dir is under CVS control.
    (unless (file-directory-p dir)
486
      (error "%s is not a directory" dir))
487 488
    (unless (or noexist (file-directory-p (expand-file-name "CVS" dir))
		(file-expand-wildcards (expand-file-name "*/CVS" dir)))
489
      (error "%s does not contain CVS controlled files" dir))
Stefan Monnier's avatar
Stefan Monnier committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

    (set-buffer cvsbuf)
    (cvs-mode-run cmd flags fis
		  :cvsargs cvsargs :dont-change-disc dont-change-disc)

    (if noshow cvsbuf
      (let ((pop-up-windows nil)) (pop-to-buffer cvsbuf)))))
;;      (funcall (if (and (boundp 'pop-up-frames) pop-up-frames)
;;		   'pop-to-buffer 'switch-to-buffer)
;;	       cvsbuf))))

(defun cvs-run-process (args fis postprocess &optional single-dir)
  (assert (cvs-buffer-p cvs-buffer))
  (save-current-buffer
    (let ((procbuf (current-buffer))
	  (cvsbuf cvs-buffer)
	  (single-dir (or single-dir (eq cvs-execute-single-dir t))))
507

Stefan Monnier's avatar
Stefan Monnier committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
      (set-buffer procbuf)
      (goto-char (point-max))
      (unless (bolp) (let ((inhibit-read-only t)) (insert "\n")))
      ;; find the set of files we'll process in this round
      (let* ((dir+files+rest
	      (if (or (null fis) (not single-dir))
		  ;; not single-dir mode: just process the whole thing
		  (list "" (mapcar 'cvs-fileinfo->full-path fis) nil)
		;; single-dir mode: extract the same-dir-elements
		(let ((dir (cvs-fileinfo->dir (car fis))))
		  ;; output the concerned dir so the parser can translate paths
		  (let ((inhibit-read-only t))
		    (insert "pcl-cvs: descending directory " dir "\n"))
		  ;; loop to find the same-dir-elems
		  (do* ((files () (cons (cvs-fileinfo->file fi) files))
			(fis fis (cdr fis))
			(fi (car fis) (car fis)))
		      ((not (and fis (string= dir (cvs-fileinfo->dir fi))))
		       (list dir files fis))))))
527 528 529
	     (dir (nth 0 dir+files+rest))
	     (files (nth 1 dir+files+rest))
	     (rest (nth 2 dir+files+rest)))
530

Stefan Monnier's avatar
Stefan Monnier committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
	;; setup the (current) process buffer
	(set (make-local-variable 'cvs-postprocess)
	     (if (null rest)
		 ;; this is the last invocation
		 postprocess
	       ;; else, we have to register ourselves to be rerun on the rest
	       `(cvs-run-process ',args ',rest ',postprocess ',single-dir)))
	(add-hook 'kill-buffer-hook
		  (lambda ()
		    (let ((proc (get-buffer-process (current-buffer))))
		      (when (processp proc)
			(set-process-filter proc nil)
			(set-process-sentinel proc nil)
			(delete-process proc))))
		  nil t)

	;; create the new process and setup the procbuffer correspondingly
	(let* ((args (append (cvs-flags-query 'cvs-cvs-flags nil 'noquery)
			     (if cvs-cvsroot (list "-d" cvs-cvsroot))
			     args
			     files))
552 553 554
	       ;; If process-connection-type is nil and the repository
	       ;; is accessed via SSH, a bad interaction between libc,
	       ;; CVS and SSH can lead to garbled output.
Stefan Monnier's avatar
Stefan Monnier committed
555 556
	       ;; It might be a glibc-specific problem (but it also happens
	       ;; under Mac OS X, it seems).
557 558 559
	       ;; Until the problem is cleared, we'll use a pty rather than
	       ;; a pipe.
	       ;; (process-connection-type nil) ; Use a pipe, not a pty.
Stefan Monnier's avatar
Stefan Monnier committed
560 561 562 563 564 565 566 567
	       (process
		;; the process will be run in the selected dir
		(let ((default-directory (cvs-expand-dir-name dir)))
		  (apply 'start-process "cvs" procbuf cvs-program args))))
	  (set-process-sentinel process 'cvs-sentinel)
	  (set-process-filter process 'cvs-update-filter)
	  (set-marker (process-mark process) (point-max))
	  (ignore-errors (process-send-eof process)) ;close its stdin to avoid hangs
568

Stefan Monnier's avatar
Stefan Monnier committed
569 570 571 572 573 574 575 576 577 578 579 580 581
	  ;; now finish setting up the cvs-buffer
	  (set-buffer cvsbuf)
	  (setq cvs-mode-line-process (symbol-name (process-status process)))
	  (force-mode-line-update)))))

  ;; The following line is said to improve display updates on some
  ;; emacsen. It shouldn't be needed, but it does no harm.
  (sit-for 0))

(defun cvs-update-header (args fis) ; inline
  (let* ((lastarg nil)
	 (args (mapcar (lambda (arg)
			 (cond
582
			  ;; filter out the largish commit message
Stefan Monnier's avatar
Stefan Monnier committed
583 584 585 586 587 588
			  ((and (eq lastarg nil) (string= arg "commit"))
			   (setq lastarg 'commit) arg)
			  ((and (eq lastarg 'commit) (string= arg "-m"))
			   (setq lastarg '-m) arg)
			  ((eq lastarg '-m)
			   (setq lastarg 'done) "<log message>")
589 590 591 592 593 594 595 596
			  ;; filter out the largish `admin -mrev:msg' message
			  ((and (eq lastarg nil) (string= arg "admin"))
			   (setq lastarg 'admin) arg)
			  ((and (eq lastarg 'admin)
				(string-match "\\`-m[^:]*:" arg))
			   (setq lastarg 'done)
			   (concat (match-string 0 arg) "<log message>"))
			  ;; Keep the rest as is.
Stefan Monnier's avatar
Stefan Monnier committed
597 598 599 600 601 602 603 604 605 606
			  (t arg)))
		       args))
	 ;; turn them into a string
	 (arg (cvs-strings->string
	       (append (cvs-flags-query 'cvs-cvs-flags nil 'noquery)
		       (if cvs-cvsroot (list "-d" cvs-cvsroot))
		       args
		       (mapcar 'cvs-fileinfo->full-path fis))))
	 (str (if args (concat "-- Running " cvs-program " " arg " ...\n")
		"\n")))
607
    (if nil (insert str)		;inline
Stefan Monnier's avatar
Stefan Monnier committed
608
      ;;(with-current-buffer cvs-buffer
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
      (let* ((prev-msg (car (ewoc-get-hf cvs-cookies)))
	     (tin (ewoc-nth cvs-cookies 0)))
	;; look for the first *real* fileinfo (to determine emptyness)
	(while
	    (and tin
		 (memq (cvs-fileinfo->type (ewoc-data tin))
		       '(MESSAGE DIRCHANGE)))
	  (setq tin (ewoc-next cvs-cookies tin)))
	;; cleanup the prev-msg
	(when (string-match "Running \\(.*\\) ...\n" prev-msg)
	  (setq prev-msg
		(concat
		 "-- last cmd: "
		 (match-string 1 prev-msg)
		 " --")))
	;; set the new header and footer
	(ewoc-set-hf cvs-cookies
		     str (concat "\n--------------------- "
				 (if tin "End" "Empty")
				 " ---------------------\n"
				 prev-msg))))))
Stefan Monnier's avatar
Stefan Monnier committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655


(defun cvs-sentinel (proc msg)
  "Sentinel for the cvs update process.
This is responsible for parsing the output from the cvs update when
it is finished."
  (when (memq (process-status proc) '(signal exit))
    (if (null (buffer-name (process-buffer proc)))
	;;(set-process-buffer proc nil)
	(error "cvs' process buffer was killed")
      (let* ((obuf (current-buffer))
	     (procbuffer (process-buffer proc)))
	(set-buffer (with-current-buffer procbuffer cvs-buffer))
	(setq cvs-mode-line-process (symbol-name (process-status proc)))
	(force-mode-line-update)
	(set-buffer procbuffer)
	(let ((cvs-postproc cvs-postprocess))
	  ;; Since the buffer and mode line will show that the
	  ;; process is dead, we can delete it now.  Otherwise it
	  ;; will stay around until M-x list-processes.
	  (delete-process proc)
	  (setq cvs-postprocess nil)
	  ;; do the postprocessing like parsing and such
	  (save-excursion (eval cvs-postproc))
	  ;; check whether something is left
	  (unless cvs-postprocess
656 657 658
	    ;; IIRC, we enable undo again once the process is finished
	    ;; for cases where the output was inserted in *vc-diff* or
	    ;; in a file-like buffer.  -stef
Stefan Monnier's avatar
Stefan Monnier committed
659 660 661
	    (buffer-enable-undo)
	    (with-current-buffer cvs-buffer
	      (cvs-update-header nil nil) ;FIXME: might need to be inline
662
	      (message "CVS process has completed in %s" (buffer-name)))))
Stefan Monnier's avatar
Stefan Monnier committed
663 664 665
	;; This might not even be necessary
	(set-buffer obuf)))))

Stefan Monnier's avatar
Stefan Monnier committed
666 667 668 669 670 671
(defun cvs-parse-process (dcd &optional subdir old-fis)
  "Parse the output of a cvs process.
DCD is the `dont-change-disc' flag to use when parsing that output.
SUBDIR is the subdirectory (if any) where this command was run.
OLD-FIS is the list of fileinfos on which the cvs command was applied and
  which should be considered up-to-date if they are missing from the output."
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
  (let* ((fileinfos (cvs-parse-buffer 'cvs-parse-table dcd subdir))
	 last)
    (with-current-buffer cvs-buffer
      ;; Expand OLD-FIS to actual files.
      (let ((fis nil))
	(dolist (fi old-fis)
	  (setq fis (if (eq (cvs-fileinfo->type fi) 'DIRCHANGE)
			(nconc (ewoc-collect cvs-cookies 'cvs-dir-member-p
					     (cvs-fileinfo->dir fi))
			       fis)
		      (cons fi fis))))
	(setq old-fis fis))
      ;; Drop OLD-FIS which were already up-to-date.
      (let ((fis nil))
	(dolist (fi old-fis)
	  (unless (eq (cvs-fileinfo->type fi) 'UP-TO-DATE) (push fi fis)))
	(setq old-fis fis))
      ;; Add the new fileinfos to the ewoc.
      (dolist (fi fileinfos)
	(setq last (cvs-addto-collection cvs-cookies fi last))
	;; This FI was in the output, so remove it from OLD-FIS.
	(setq old-fis (delq (ewoc-data last) old-fis)))
      ;; Process the "silent output" (i.e. absence means up-to-date).
Stefan Monnier's avatar
Stefan Monnier committed
695
      (dolist (fi old-fis)
696 697 698 699 700 701 702 703 704 705 706
	(setf (cvs-fileinfo->type fi) 'UP-TO-DATE)
	(setq last (cvs-addto-collection cvs-cookies fi last)))
      (setq fileinfos (nconc old-fis fileinfos))
      ;; Clean up the ewoc as requested by the user.
      (cvs-cleanup-collection cvs-cookies
			      (eq cvs-auto-remove-handled t)
			      cvs-auto-remove-directories
			      nil)
      ;; Revert buffers if necessary.
      (when (and cvs-auto-revert (not dcd) (not cvs-from-vc))
	(cvs-revert-if-needed fileinfos)))))
Stefan Monnier's avatar
Stefan Monnier committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

(defmacro defun-cvs-mode (fun args docstring interact &rest body)
  "Define a function to be used in a *cvs* buffer.
This will look for a *cvs* buffer and execute BODY in it.
Since the interactive arguments might need to be queried after
switching to the *cvs* buffer, the generic code is rather ugly,
but luckily we can often use simpler alternatives.

FUN can be either a symbol (i.e. STYLE is nil) or a cons (FUN . STYLE).
ARGS and DOCSTRING are the normal argument list.
INTERACT is the interactive specification or nil for non-commands.

STYLE can be either SIMPLE, NOARGS or DOUBLE.  It's an error for it
to have any other value, unless other details of the function make it
clear what alternative to use.
- SIMPLE will get all the interactive arguments from the original buffer.
- NOARGS will get all the arguments from the *cvs* buffer and will
  always behave as if called interactively.
- DOUBLE is the generic case."
726
  (declare (debug (&define sexp lambda-list stringp ("interactive" interactive) def-body)))
Stefan Monnier's avatar
Stefan Monnier committed
727 728 729 730 731
  (let ((style (cvs-cdr fun))
	(fun (cvs-car fun)))
    (cond
     ;; a trivial interaction, no need to move it
     ((or (eq style 'SIMPLE)
732 733
	  (null (nth 1 interact))
	  (stringp (nth 1 interact)))
Stefan Monnier's avatar
Stefan Monnier committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
      `(defun ,fun ,args ,docstring ,interact
	 (cvs-mode! (lambda () ,@body))))

     ;; fun is only called interactively:  move all the args to the inner fun
     ((eq style 'NOARGS)
      `(defun ,fun () ,docstring (interactive)
	 (cvs-mode! (lambda ,args ,interact ,@body))))

     ;; bad case
     ((eq style 'DOUBLE)
      (string-match ".*" docstring)
      (let ((line1 (match-string 0 docstring))
	    (fun-1 (intern (concat (symbol-name fun) "-1"))))
	`(progn
	   (defun ,fun-1 ,args
	     ,(concat docstring "\nThis function only works within a *cvs* buffer.
For interactive use, use `" (symbol-name fun) "' instead.")
	     ,interact
	     ,@body)
	   (defun ,fun ()
	     ,(concat line1 "\nWrapper function that switches to a *cvs* buffer
before calling the real function `" (symbol-name fun-1) "'.\n")
	     (interactive)
	     (cvs-mode! ',fun-1)))))

     (t (error "unknown style %s in `defun-cvs-mode'" style)))))

(defun-cvs-mode cvs-mode-kill-process ()
  "Kill the temporary buffer and associated process."
  (interactive)
  (when (and (bufferp cvs-temp-buffer) (buffer-name cvs-temp-buffer))
    (let ((proc (get-buffer-process cvs-temp-buffer)))
      (when proc (delete-process proc)))))

768 769 770
;;
;; Maintaining the collection in the face of updates
;;
Stefan Monnier's avatar
Stefan Monnier committed
771 772

(defun cvs-addto-collection (c fi &optional tin)
773
  "Add FI to C and return FI's corresponding tin.
Stefan Monnier's avatar
Stefan Monnier committed
774 775 776 777 778 779
FI is inserted in its proper place or maybe even merged with a preexisting
  fileinfo if applicable.
TIN specifies an optional starting point."
  (unless tin (setq tin (ewoc-nth c 0)))
  (while (and tin (cvs-fileinfo< fi (ewoc-data tin)))
    (setq tin (ewoc-prev c tin)))
780
  (if (null tin) (ewoc-enter-first c fi) ;empty collection
Stefan Monnier's avatar
Stefan Monnier committed
781 782 783 784 785
    (assert (not (cvs-fileinfo< fi (ewoc-data tin))))
    (let ((next-tin (ewoc-next c tin)))
      (while (not (or (null next-tin)
		      (cvs-fileinfo< fi (ewoc-data next-tin))))
	(setq tin next-tin next-tin (ewoc-next c next-tin)))
786 787
      (if (or (cvs-fileinfo< (ewoc-data tin) fi)
	      (eq (cvs-fileinfo->type  fi) 'MESSAGE))
Stefan Monnier's avatar
Stefan Monnier committed
788 789 790 791
	  ;; tin < fi < next-tin
	  (ewoc-enter-after c tin fi)
	;; fi == tin
	(cvs-fileinfo-update (ewoc-data tin) fi)
792
	(ewoc-invalidate c tin)
793 794
	;; Move cursor back to where it belongs.
	(when (bolp) (cvs-move-to-goal-column))
795
	tin))))
Stefan Monnier's avatar
Stefan Monnier committed
796

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
(defcustom cvs-cleanup-functions nil
  "Functions to tweak the cleanup process.
The functions are called with a single argument (a FILEINFO) and should
return a non-nil value if that fileinfo should be removed."
  :group 'pcl-cvs
  :type '(hook :options (cvs-cleanup-removed)))

(defun cvs-cleanup-removed (fi)
  "Non-nil if FI has been cvs-removed but still exists.
This is intended for use on `cvs-cleanup-functions' when you have cvs-removed
automatically generated files (which should hence not be under CVS control)
but can't commit the removal because the repository's owner doesn't understand
the problem."
  (and (or (eq (cvs-fileinfo->type fi) 'REMOVED)
	   (and (eq (cvs-fileinfo->type fi) 'CONFLICT)
		(eq (cvs-fileinfo->subtype fi) 'REMOVED)))
       (file-exists-p (cvs-fileinfo->full-path fi))))

Stefan Monnier's avatar
Stefan Monnier committed
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
;; called at the following times:
;; - postparse  ((eq cvs-auto-remove-handled t) cvs-auto-remove-directories nil)
;; - pre-run    ((eq cvs-auto-remove-handled 'delayed) nil t)
;; - remove-handled (t (or cvs-auto-remove-directories 'handled) t)
;; - cvs-cmd-do (nil nil t)
;; - post-ignore (nil nil nil)
;; - acknowledge (nil nil nil)
;; - remove     (nil nil nil)
(defun cvs-cleanup-collection (c rm-handled rm-dirs rm-msgs)
  "Remove undesired entries.
C is the collection
RM-HANDLED if non-nil means remove handled entries.
RM-DIRS behaves like `cvs-auto-remove-directories'.
RM-MSGS if non-nil means remove messages."
  (let (last-fi first-dir (rerun t))
    (while rerun
      (setq rerun nil)
      (setq first-dir t)
      (setq last-fi (cvs-create-fileinfo 'DEAD "../" "" "")) ;place-holder
      (ewoc-filter
       c (lambda (fi)
	   (let* ((type (cvs-fileinfo->type fi))
		  (subtype (cvs-fileinfo->subtype fi))
		  (keep
		   (case type
		     ;; remove temp messages and keep the others
841
		     (MESSAGE (not (or rm-msgs (eq subtype 'TEMP))))
Stefan Monnier's avatar
Stefan Monnier committed
842 843 844 845 846
		     ;; remove entries
		     (DEAD nil)
		     ;; handled also?
		     (UP-TO-DATE (not rm-handled))
		     ;; keep the rest
847 848
		     (t (not (run-hook-with-args-until-success
			      'cvs-cleanup-functions fi))))))
849

Stefan Monnier's avatar
Stefan Monnier committed
850 851 852 853 854 855 856 857 858 859 860 861
	     ;; mark dirs for removal
	     (when (and keep rm-dirs
			(eq (cvs-fileinfo->type last-fi) 'DIRCHANGE)
			(not (when first-dir (setq first-dir nil) t))
			(or (eq rm-dirs 'all)
			    (not (cvs-string-prefix-p
				  (cvs-fileinfo->dir last-fi)
				  (cvs-fileinfo->dir fi)))
			    (and (eq type 'DIRCHANGE) (eq rm-dirs 'empty))
			    (eq subtype 'FOOTER)))
	       (setf (cvs-fileinfo->type last-fi) 'DEAD)
	       (setq rerun t))
862 863 864 865 866 867 868
	     (when keep (setq last-fi fi)))))
      ;; remove empty last dir
      (when (and rm-dirs
		 (not first-dir)
		 (eq (cvs-fileinfo->type last-fi) 'DIRCHANGE))
	(setf (cvs-fileinfo->type last-fi) 'DEAD)
	(setq rerun t)))))
Stefan Monnier's avatar
Stefan Monnier committed
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907

(defun cvs-get-cvsroot ()
  "Gets the CVSROOT for DIR."
  (let ((cvs-cvsroot-file (expand-file-name "Root" "CVS")))
    (or (cvs-file-to-string cvs-cvsroot-file t)
	cvs-cvsroot
	(getenv "CVSROOT")
	"?????")))

(defun cvs-get-module ()
  "Return the current CVS module.
This usually doesn't really work but is a handy initval in a prompt."
  (let* ((repfile (expand-file-name "Repository" "CVS"))
	 (rep (cvs-file-to-string repfile t)))
    (cond
     ((null rep) "")
     ((not (file-name-absolute-p rep)) rep)
     (t
      (let* ((root (cvs-get-cvsroot))
	     (str (concat (file-name-as-directory (or root "/")) " || " rep)))
	(if (and root (string-match "\\(.*\\) || \\1\\(.*\\)\\'" str))
	    (match-string 2 str)
	  (file-name-nondirectory rep)))))))



;;;;
;;;; running a "cvs checkout".
;;;;

;;;###autoload
(defun cvs-checkout (modules dir flags)
  "Run a 'cvs checkout MODULES' in DIR.
Feed the output to a *cvs* buffer, display it in the current window,
and run `cvs-mode' on it.

With a prefix argument, prompt for cvs FLAGS to use."
  (interactive
   (list (cvs-string->strings (read-string "Module(s): " (cvs-get-module)))
908
	 (read-directory-name "CVS Checkout Directory: "
Stefan Monnier's avatar
Stefan Monnier committed
909 910 911 912 913 914 915 916 917 918
			 nil default-directory nil)
	 (cvs-add-branch-prefix
	  (cvs-flags-query 'cvs-checkout-flags "cvs checkout flags"))))
  (when (eq flags t)
    (setf flags (cvs-flags-query 'cvs-checkout-flags nil 'noquery)))
  (cvs-cmd-do "checkout" (or dir default-directory)
	      (append flags modules) nil 'new
	      :noexist t))


919
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
920
;;;; The code for running a "cvs update" and friends in various ways.
921
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
922 923 924

(defun-cvs-mode (cvs-mode-revert-buffer . SIMPLE)
                (&optional ignore-auto noconfirm)
Dave Love's avatar
Dave Love committed
925
  "Rerun `cvs-examine' on the current directory with the default flags."
Stefan Monnier's avatar
Stefan Monnier committed
926 927 928 929 930 931 932 933 934
  (interactive)
  (cvs-examine default-directory t))

(defun cvs-query-directory (msg)
  ;; last-command-char = ?\r hints that the command was run via M-x
  (if (and (cvs-buffer-p)
	   (not current-prefix-arg)
	   (not (eq last-command-char ?\r)))
      default-directory
935
    (read-directory-name msg nil default-directory nil)))
Stefan Monnier's avatar
Stefan Monnier committed
936

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
;;;###autoload
(defun cvs-quickdir (dir &optional flags noshow)
  "Open a *cvs* buffer on DIR without running cvs.
With a prefix argument, prompt for a directory to use.
A prefix arg >8 (ex: \\[universal-argument] \\[universal-argument]),
  prevents reuse of an existing *cvs* buffer.
Optional argument NOSHOW if non-nil means not to display the buffer.
FLAGS is ignored."
  (interactive (list (cvs-query-directory "CVS quickdir (directory): ")))
  ;; FIXME: code duplication with cvs-cmd-do and cvs-parse-process
  (let* ((dir (file-name-as-directory
	       (abbreviate-file-name (expand-file-name dir))))
	 (new (> (prefix-numeric-value current-prefix-arg) 8))
	 (cvsbuf (cvs-make-cvs-buffer dir new))
	 last)
    ;; Check that dir is under CVS control.
    (unless (file-directory-p dir)
      (error "%s is not a directory" dir))
    (unless (file-directory-p (expand-file-name "CVS" dir))
      (error "%s does not contain CVS controlled files" dir))
    (set-buffer cvsbuf)
    (dolist (fi (cvs-fileinfo-from-entries ""))
      (setq last (cvs-addto-collection cvs-cookies fi last)))
    (cvs-cleanup-collection cvs-cookies
			    (eq cvs-auto-remove-handled t)
			    cvs-auto-remove-directories
			    nil)
    (if noshow cvsbuf
      (let ((pop-up-windows nil)) (pop-to-buffer cvsbuf)))))
Stefan Monnier's avatar
Stefan Monnier committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979

;;;###autoload
(defun cvs-examine (directory flags &optional noshow)
  "Run a `cvs -n update' in the specified DIRECTORY.
That is, check what needs to be done, but don't change the disc.
Feed the output to a *cvs* buffer and run `cvs-mode' on it.
With a prefix argument, prompt for a directory and cvs FLAGS to use.
A prefix arg >8 (ex: \\[universal-argument] \\[universal-argument]),
  prevents reuse of an existing *cvs* buffer.
Optional argument NOSHOW if non-nil means not to display the buffer."
  (interactive (list (cvs-query-directory "CVS Examine (directory): ")
		     (cvs-flags-query 'cvs-update-flags "cvs -n update flags")))
  (when (eq flags t)
    (setf flags (cvs-flags-query 'cvs-update-flags nil 'noquery)))
980
  (when find-file-visit-truename (setq directory (file-truename directory)))
Stefan Monnier's avatar
Stefan Monnier committed
981 982 983 984 985 986 987 988 989 990 991
  (cvs-cmd-do "update" directory flags nil
	      (> (prefix-numeric-value current-prefix-arg) 8)
	      :cvsargs '("-n")
	      :noshow noshow
	      :dont-change-disc t))


;;;###autoload
(defun cvs-update (directory flags)
  "Run a `cvs update' in the current working DIRECTORY.
Feed the output to a *cvs* buffer and run `cvs-mode' on it.
992
With a \\[universal-argument] prefix argument, prompt for a directory to use.
Stefan Monnier's avatar
Stefan Monnier committed
993
A prefix arg >8 (ex: \\[universal-argument] \\[universal-argument]),
994 995 996
  prevents reuse of an existing *cvs* buffer.
The prefix is also passed to `cvs-flags-query' to select the FLAGS
  passed to cvs."
Stefan Monnier's avatar
Stefan Monnier committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  (interactive (list (cvs-query-directory "CVS Update (directory): ")
		     (cvs-flags-query 'cvs-update-flags "cvs update flags")))
  (when (eq flags t)
    (setf flags (cvs-flags-query 'cvs-update-flags nil 'noquery)))
  (cvs-cmd-do "update" directory flags nil
	      (> (prefix-numeric-value current-prefix-arg) 8)))


;;;###autoload
(defun cvs-status (directory flags &optional noshow)
  "Run a `cvs status' in the current working DIRECTORY.
Feed the output to a *cvs* buffer and run `cvs-mode' on it.
With a prefix argument, prompt for a directory and cvs FLAGS to use.
A prefix arg >8 (ex: \\[universal-argument] \\[universal-argument]),
  prevents reuse of an existing *cvs* buffer.
Optional argument NOSHOW if non-nil means not to display the buffer."
  (interactive (list (cvs-query-directory "CVS Status (directory): ")
		     (cvs-flags-query 'cvs-status-flags "cvs status flags")))
  (when (eq flags t)
    (setf flags (cvs-flags-query 'cvs-status-flags nil 'noquery)))
  (cvs-cmd-do "status" directory flags nil
	      (> (prefix-numeric-value current-prefix-arg) 8)
	      :noshow noshow :dont-change-disc t))

(defun cvs-update-filter (proc string)
  "Filter function for pcl-cvs.
This function gets the output that CVS sends to stdout.  It inserts
the STRING into (process-buffer PROC) but it also checks if CVS is waiting
for a lock file.  If so, it inserts a message cookie in the *cvs* buffer."
  (save-match-data
    (with-current-buffer (process-buffer proc)
      (let ((inhibit-read-only t))
	(save-excursion
	  ;; Insert the text, moving the process-marker.
	  (goto-char (process-mark proc))
	  (insert string)
	  (set-marker (process-mark proc) (point))
	  ;; FIXME: Delete any old lock message
	  ;;(if (tin-nth cookies 1)
	  ;;  (tin-delete cookies
	  ;;	      (tin-nth cookies 1)))
	  ;; Check if CVS is waiting for a lock.
1039
	  (beginning-of-line 0)	      ;Move to beginning of last complete line.
Stefan Monnier's avatar
Stefan Monnier committed
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	  (when (looking-at "^[ a-z]+: \\(.*waiting for .*lock in \\(.*\\)\\)$")
	    (let ((msg (match-string 1))
		  (lock (match-string 2)))
	      (with-current-buffer cvs-buffer
		(set (make-local-variable 'cvs-lock-file) lock)
		;; display the lock situation in the *cvs* buffer:
		(ewoc-enter-last
		 cvs-cookies
		 (cvs-create-fileinfo
		  'MESSAGE "" " "
		  (concat msg
1051 1052 1053
			  (when (file-exists-p lock)
			    (substitute-command-keys
			     "\n\t(type \\[cvs-mode-delete-lock] to delete it)")))
Stefan Monnier's avatar
Stefan Monnier committed
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
		  :subtype 'TEMP))
		(pop-to-buffer (current-buffer))
		(goto-char (point-max))
		(beep)))))))))


;;;;
;;;; The cvs-mode and its associated commands.
;;;;

(cvs-prefix-define cvs-force-command "" "" '("/F") cvs-qtypedesc-string1)
(defun-cvs-mode cvs-mode-force-command (arg)
  "Force the next cvs command to operate on all the selected files.
By default, cvs commands only operate on files on which the command
\"makes sense\".  This overrides the safety feature on the next cvs command.
It actually behaves as a toggle.  If prefixed by \\[universal-argument] \\[universal-argument],
the override will persist until the next toggle."
  (interactive "P")
  (cvs-prefix-set 'cvs-force-command arg))

(put 'cvs-mode 'mode-class 'special)
1075
(define-derived-mode cvs-mode nil "CVS"
Stefan Monnier's avatar
Stefan Monnier committed
1076
  "Mode used for PCL-CVS, a frontend to CVS.
1077
Full documentation is in the Texinfo file."
Stefan Monnier's avatar
Stefan Monnier committed
1078 1079 1080 1081 1082 1083
  (setq mode-line-process
	'("" cvs-force-command cvs-ignore-marks-modif
	  ":" (cvs-branch-prefix
	       ("" cvs-branch-prefix (cvs-secondary-branch-prefix
				      ("->" cvs-secondary-branch-prefix))))
	  " " cvs-mode-line-process))
1084 1085
  (if buffer-file-name
      (error "Use M-x cvs-quickdir to get a *cvs* buffer."))
1086
  (buffer-disable-undo)
Stefan Monnier's avatar
Stefan Monnier committed
1087 1088
  ;;(set (make-local-variable 'goal-column) cvs-cursor-column)
  (set (make-local-variable 'revert-buffer-function) 'cvs-mode-revert-buffer)
1089
  (setq truncate-lines t)
Stefan Monnier's avatar
Stefan Monnier committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  (cvs-prefix-make-local 'cvs-branch-prefix)
  (cvs-prefix-make-local 'cvs-secondary-branch-prefix)
  (cvs-prefix-make-local 'cvs-force-command)
  (cvs-prefix-make-local 'cvs-ignore-marks-modif)
  (make-local-variable 'cvs-mode-line-process)
  (make-local-variable 'cvs-temp-buffers))


(defun cvs-buffer-p (&optional buffer)
  "Return whether the (by default current) BUFFER is a `cvs-mode' buffer."
  (save-excursion
    (if buffer (set-buffer buffer))
    (and (eq major-mode 'cvs-mode))))

(defun cvs-buffer-check ()
  "Check that the current buffer follows cvs-buffer's conventions."
  (let ((buf (current-buffer))
	(check 'none))
    (or (and (setq check 'collection)
	     (eq (ewoc-buffer cvs-cookies) buf)
	     (setq check 'cvs-temp-buffer)
	     (or (null cvs-temp-buffer)
		 (null (buffer-name cvs-temp-buffer))
		 (and (eq (with-current-buffer cvs-temp-buffer cvs-buffer) buf)
		      (equal (with-current-buffer cvs-temp-buffer
			       default-directory)
			     default-directory)))
	     t)
	(error "Inconsistent %s in buffer %s" check (buffer-name buf)))))


1121
(defun cvs-mode-quit ()
Stefan Monnier's avatar
Stefan Monnier committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
  "Quit PCL-CVS, killing the *cvs* buffer."
  (interactive)
  (and (y-or-n-p "Quit pcl-cvs? ") (kill-buffer (current-buffer))))

;; Give help....

(defun cvs-help ()
  "Display help for various PCL-CVS commands."
  (interactive)
  (if (eq last-command 'cvs-help)
1132
      (describe-function 'cvs-mode)   ; would need minor-mode for log-edit-mode
Stefan Monnier's avatar
Stefan Monnier committed
1133 1134 1135 1136 1137 1138 1139 1140 1141
    (message
     (substitute-command-keys
      "`\\[cvs-help]':help `\\[cvs-mode-add]':add `\\[cvs-mode-commit]':commit \
`\\[cvs-mode-diff-map]':diff* `\\[cvs-mode-log]':log \
`\\[cvs-mode-remove]':remove `\\[cvs-mode-status]':status \
`\\[cvs-mode-undo]':undo"))))

;; Move around in the buffer

1142 1143 1144 1145 1146 1147
(defun cvs-move-to-goal-column ()
  (let* ((eol (line-end-position))
	 (fpos (next-single-property-change (point) 'cvs-goal-column nil eol)))
    (when (< fpos eol)
      (goto-char fpos))))

Stefan Monnier's avatar
Stefan Monnier committed
1148 1149 1150 1151
(defun-cvs-mode cvs-mode-previous-line (arg)
  "Go to the previous line.
If a prefix argument is given, move by that many lines."
  (interactive "p")
1152
  (ewoc-goto-prev cvs-cookies arg)
1153
  (cvs-move-to-goal-column))
Stefan Monnier's avatar
Stefan Monnier committed
1154 1155 1156 1157 1158

(defun-cvs-mode cvs-mode-next-line (arg)
  "Go to the next line.
If a prefix argument is given, move by that many lines."
  (interactive "p")
1159
  (ewoc-goto-next cvs-cookies arg)
1160
  (cvs-move-to-goal-column))
Stefan Monnier's avatar
Stefan Monnier committed
1161

1162
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
1163
;;;; Mark handling
1164
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
1165 1166 1167 1168 1169 1170

(defun-cvs-mode cvs-mode-mark (&optional arg)
  "Mark the fileinfo on the current line.
If the fileinfo is a directory, all the contents of that directory are
marked instead. A directory can never be marked."
  (interactive)
1171
  (let* ((tin (ewoc-locate cvs-cookies))
Stefan Monnier's avatar
Stefan Monnier committed
1172 1173 1174 1175 1176 1177 1178 1179
	 (fi (ewoc-data tin)))
    (if (eq (cvs-fileinfo->type fi) 'DIRCHANGE)
	;; it's a directory: let's mark all files inside
	(ewoc-map
	 (lambda (f dir)
	   (when (cvs-dir-member-p f dir)
	     (setf (cvs-fileinfo->marked f)
		   (not (if (eq arg 'toggle) (cvs-fileinfo->marked f) arg)))
1180
	     t))			;Tell cookie to redisplay this cookie.
Stefan Monnier's avatar
Stefan Monnier committed
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
	 cvs-cookies
	 (cvs-fileinfo->dir fi))
      ;; not a directory: just do the obvious
      (setf (cvs-fileinfo->marked fi)
	    (not (if (eq arg 'toggle) (cvs-fileinfo->marked fi) arg)))
      (ewoc-invalidate cvs-cookies tin)
      (cvs-mode-next-line 1))))

(defun cvs-mouse-toggle-mark (e)
  "Toggle the mark of the entry under the mouse."
  (interactive "e")
1192 1193 1194
  (save-excursion
    (mouse-set-point e)
    (cvs-mode-mark 'toggle)))
Stefan Monnier's avatar
Stefan Monnier committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

(defun-cvs-mode cvs-mode-unmark ()
  "Unmark the fileinfo on the current line."
  (interactive)
  (cvs-mode-mark t))

(defun-cvs-mode cvs-mode-mark-all-files ()
  "Mark all files."
  (interactive)
  (ewoc-map (lambda (cookie)
1205 1206 1207
	      (unless (eq (cvs-fileinfo->type cookie) 'DIRCHANGE)
		(setf (cvs-fileinfo->marked cookie) t)))
	    cvs-cookies))
Stefan Monnier's avatar
Stefan Monnier committed
1208

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
(defun-cvs-mode (cvs-mode-mark-on-state . SIMPLE) (state)
  "Mark all files in state STATE."
  (interactive
   (list
    (let ((default
	    (condition-case nil
		(downcase
		 (symbol-name
		  (cvs-fileinfo->type
		   (cvs-mode-marked nil nil :read-only t :one t :noquery t))))
	      (error nil))))
      (intern
       (upcase
	(completing-read
	 (concat
	  "Mark files in state" (if default (concat " [" default "]")) ": ")
	 (mapcar (lambda (x)
		   (list (downcase (symbol-name (car x)))))
		 cvs-states)
	 nil t nil nil default))))))
  (ewoc-map (lambda (fi)
	      (when (eq (cvs-fileinfo->type fi) state)
		(setf (cvs-fileinfo->marked fi) t)))
	    cvs-cookies))

Stefan Monnier's avatar
Stefan Monnier committed
1234 1235 1236 1237
(defun-cvs-mode cvs-mode-mark-matching-files (regex)
  "Mark all files matching REGEX."
  (interactive "sMark files matching: ")
  (ewoc-map (lambda (cookie)
1238 1239 1240 1241
	      (when (and (not (eq (cvs-fileinfo->type cookie) 'DIRCHANGE))
			 (string-match regex (cvs-fileinfo->file cookie)))
		(setf (cvs-fileinfo->marked cookie) t)))
	    cvs-cookies))
Stefan Monnier's avatar
Stefan Monnier committed
1242 1243 1244 1245 1246 1247 1248

(defun-cvs-mode cvs-mode-unmark-all-files ()
  "Unmark all files.
Directories are also unmarked, but that doesn't matter, since
they should always be unmarked."
  (interactive)
  (ewoc-map (lambda (cookie)
1249 1250 1251
	      (setf (cvs-fileinfo->marked cookie) nil)
	      t)
	    cvs-cookies))
Stefan Monnier's avatar
Stefan Monnier committed
1252 1253 1254 1255

(defun-cvs-mode cvs-mode-unmark-up ()
  "Unmark the file on the previous line."
  (interactive)
1256
  (let ((tin (ewoc-goto-prev cvs-cookies 1)))
Stefan Monnier's avatar
Stefan Monnier committed
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
    (when tin
      (setf (cvs-fileinfo->marked (ewoc-data tin)) nil)
      (ewoc-invalidate cvs-cookies tin))))

(defconst cvs-ignore-marks-alternatives
  '(("toggle-marks"	. "/TM")
    ("force-marks"	. "/FM")
    ("ignore-marks"	. "/IM")))

(cvs-prefix-define cvs-ignore-marks-modif
  "Prefix to decide whether to ignore marks or not."
  "active"
  (mapcar 'cdr cvs-ignore-marks-alternatives)
  (cvs-qtypedesc-create
   (lambda (str) (cdr (assoc str cvs-ignore-marks-alternatives)))
1272
   (lambda (obj) (car (rassoc obj cvs-ignore-marks-alternatives)))
Stefan Monnier's avatar
Stefan Monnier committed
1273 1274
   (lambda () cvs-ignore-marks-alternatives)
   nil t))
1275

Stefan Monnier's avatar
Stefan Monnier committed
1276 1277 1278 1279 1280 1281 1282 1283
(defun-cvs-mode cvs-mode-toggle-marks (arg)
  "Toggle whether the next CVS command uses marks.
See `cvs-prefix-set' for further description of the behavior.
\\[universal-argument] 1 selects `force-marks',
\\[universal-argument] 2 selects `ignore-marks',
\\[universal-argument] 3 selects `toggle-marks'."
  (interactive "P")
  (cvs-prefix-set 'cvs-ignore-marks-modif arg))
1284

Stefan Monnier's avatar
Stefan Monnier committed
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
(defun cvs-ignore-marks-p (cmd &optional read-only)
  (let ((default (if (member cmd cvs-invert-ignore-marks)
		     (not cvs-default-ignore-marks)
		   cvs-default-ignore-marks))
	(modif (cvs-prefix-get 'cvs-ignore-marks-modif read-only)))
    (cond
     ((equal modif "/IM") t)
     ((equal modif "/TM") (not default))
     ((equal modif "/FM") nil)
     (t default))))

(defun cvs-mode-mark-get-modif (cmd)
  (if (cvs-ignore-marks-p cmd 'read-only) "/IM" "/FM"))

(defun cvs-get-marked (&optional ignore-marks ignore-contents)
  "Return a list of all selected fileinfos.
If there are any marked tins, and IGNORE-MARKS is nil, return them.
Otherwise, if the cursor selects a directory, and IGNORE-CONTENTS is
nil, return all files in it, else return just the directory.
Otherwise return (a list containing) the file the cursor points to, or
an empty list if it doesn't point to a file at all.

Args: &optional IGNORE-MARKS IGNORE-CONTENTS."

  (let ((fis nil))
1310 1311
    (dolist (fi (if (and (boundp 'cvs-minor-current-files)
			 (consp cvs-minor-current-files))
Stefan Monnier's avatar
Stefan Monnier committed
1312 1313
		    (mapcar
		     (lambda (f)
1314 1315 1316 1317 1318 1319 1320 1321 1322
		       (if (cvs-fileinfo-p f) f
			 (let ((f (file-relative-name f)))
			   (if (file-directory-p f)
			       (cvs-create-fileinfo
				'DIRCHANGE (file-name-as-directory f) "." "")
			     (let ((dir (file-name-directory f))
				   (file (file-name-nondirectory f)))
			       (cvs-create-fileinfo
				'UNKNOWN (or dir "") file ""))))))
Stefan Monnier's avatar
Stefan Monnier committed
1323 1324
		     cvs-minor-current-files)
		  (or (and (not ignore-marks)
1325
			   (ewoc-collect cvs-cookies 'cvs-fileinfo->marked))
1326
		      (list (ewoc-data (ewoc-locate cvs-cookies))))))
1327

Stefan Monnier's avatar
Stefan Monnier committed
1328 1329 1330 1331
      (if (or ignore-contents (not (eq (cvs-fileinfo->type fi) 'DIRCHANGE)))
	  (push fi fis)
	;; If a directory is selected, return members, if any.
	(setq fis
1332 1333
	      (append (ewoc-collect
		       cvs-cookies 'cvs-dir-member-p (cvs-fileinfo->dir fi))
Stefan Monnier's avatar
Stefan Monnier committed
1334 1335 1336
		      fis))))
    (nreverse fis)))

1337
(defun* cvs-mode-marked (filter &optional cmd
1338
				&key read-only one file noquery)
Stefan Monnier's avatar
Stefan Monnier committed
1339 1340 1341 1342 1343 1344
  "Get the list of marked FIS.
CMD is used to determine whether to use the marks or not.
Only files for which FILTER is applicable are returned.
If READ-ONLY is non-nil, the current toggling is left intact.
If ONE is non-nil, marks are ignored and a single FI is returned.
If FILE is non-nil, directory entries won't be selected."
1345
  (unless cmd (setq cmd (symbol-name filter)))
Stefan Monnier's avatar
Stefan Monnier committed
1346
  (let* ((fis (cvs-get-marked (or one (cvs-ignore-marks-p cmd read-only))
1347 1348
			      (and (not file)
				   (cvs-applicable-p 'DIRCHANGE filter))))
Stefan Monnier's avatar
Stefan Monnier committed
1349 1350 1351 1352
	 (force (cvs-prefix-get 'cvs-force-command))
	 (fis (car (cvs-partition
		    (lambda (fi) (cvs-applicable-p fi (and (not force) filter)))
		    fis))))
1353 1354 1355 1356
    (when (and (or (null fis) (and one (cdr fis))) (not noquery))
      (message (if (null fis)
		   "`%s' is not applicable to any of the selected files."
		 "`%s' is only applicable to a single file.") cmd)
1357
      (sit-for 1)
Stefan Monnier's avatar