(cl-do-arglist): Use plist-get and plist-member instead of memq.
[bpt/emacs.git] / lisp / pcvs.el
CommitLineData
5b467bf4
SM
1;;; pcvs.el -- A Front-end to CVS.
2
3;; Copyright (C) 1991-2000 Free Software Foundation, Inc.
4
5;; Author: (The PCL-CVS Trust) pcl-cvs@cyclic.com
6;; (Per Cederqvist) ceder@lysator.liu.se
7;; (Greg A. Woods) woods@weird.com
8;; (Jim Blandy) jimb@cyclic.com
9;; (Karl Fogel) kfogel@floss.red-bean.com
10;; (Jim Kingdon) kingdon@cyclic.com
a7996e05
SM
11;; (Stefan Monnier) monnier@cs.yale.edu
12;; (Greg Klanderman) greg@alphatech.com
13;; (Jari Aalto+mail.emacs) jari.aalto@poboxes.com
5b467bf4
SM
14;; Maintainer: (Stefan Monnier) monnier+lists/cvs/pcl@flint.cs.yale.edu
15;; Keywords: CVS, version control, release management
16;; Version: $Name: $
3c7fafc7 17;; Revision: $Id: pcvs.el,v 1.12 2000/10/08 19:11:34 monnier Exp $
5b467bf4
SM
18
19;; This file is part of GNU Emacs.
20
21;; GNU Emacs is free software; you can redistribute it and/or modify
22;; it under the terms of the GNU General Public License as published by
23;; the Free Software Foundation; either version 2, or (at your option)
24;; any later version.
25
26;; GNU Emacs is distributed in the hope that it will be useful,
27;; but WITHOUT ANY WARRANTY; without even the implied warranty of
28;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29;; GNU General Public License for more details.
30
31;; You should have received a copy of the GNU General Public License
32;; along with GNU Emacs; see the file COPYING. If not, write to the
33;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
34;; Boston, MA 02111-1307, USA.
35
36;;; Commentary:
37
cb3430a1
SM
38;; PCL-CVS is a front-end to the CVS version control system. For people
39;; familiar with VC, it is somewhat like VC-dired: it presents the status of
40;; all the files in your working area and allows you to commit/update several
41;; of them at a time. Compared to VC-dired, it is considerably better and
42;; faster (but only for CVS).
43
44;; PCL-CVS was originally written by Per Cederqvist many years ago. This
45;; version derives from the XEmacs-21 version, itself based on the 2.0b2
46;; version (last release from Per). It is a thorough rework.
47
48;; Contrary to what you'd expect, PCL-CVS is not a replacement for VC but only
49;; for VC-dired. As such, I've tried to make PCL-CVS and VC interoperate
50;; seamlessly (I also use VC).
51
52;; To use PCL-CVS just use `M-x cvs-examine RET <dir> RET'.
96190aa1 53;; There is a TeXinfo manual, which can be helpful to get started.
cb3430a1 54
5b467bf4
SM
55;;; Todo:
56
cb3430a1 57;; ******** FIX THE DOCUMENTATION *********
96190aa1 58;;
eed914af
SM
59;; - use UP-TO-DATE rather than DEAD when cleaning before `examine'.
60;;
96190aa1 61;; - hide fileinfos without getting rid of them (will require ewok work).
cb3430a1
SM
62;; - add toolbar entries
63;; - marking
64;; marking directories should jump to just after the dir.
65;; allow (un)marking directories at a time with the mouse.
cb3430a1
SM
66;; - liveness indicator
67;; - indicate in docstring if the cmd understands the `b' prefix(es).
68;; - call smerge-mode when opening CONFLICT files.
cb3430a1
SM
69;; - have vc-checkin delegate to cvs-mode-commit when applicable
70;; - higher-level CVS operations
71;; cvs-mode-rename
72;; cvs-mode-branch
73;; - module-level commands
74;; add support for parsing 'modules' file ("cvs co -c")
75;; cvs-mode-rcs2log
76;; cvs-rdiff
77;; cvs-release
78;; cvs-import
79;; C-u M-x cvs-checkout should ask for a cvsroot
80;; cvs-mode-handle-new-vendor-version
5b467bf4
SM
81;; - checks out module, or alternately does update join
82;; - does "cvs -n tag LAST_VENDOR" to find old files into *cvs*
cb3430a1 83;; cvs-export
44946a4c 84;; (with completion on tag names and hooks to help generate full releases)
cb3430a1 85;; - allow cvs-cmd-do to either clear the marks or not.
cb3430a1
SM
86;; - display stickiness information. And current CVS/Tag as well.
87;; - write 'cvs-mode-admin' to do arbitrary 'cvs admin' commands
6dc7d3d5
SM
88;; Most interesting would be version removal and log message replacement.
89;; The last one would be neat when called from log-view-mode.
cb3430a1 90;; - cvs-mode-incorporate
6dc7d3d5 91;; It would merge in the status from one *cvs* buffer into another.
5b467bf4
SM
92;; This would be used to populate such a buffer that had been created with
93;; a `cvs {update,status,checkout} -l'.
cb3430a1
SM
94;; - cvs-mode-(i)diff-other-{file,buffer,cvs-buffer}
95;; - offer the choice to kill the process when the user kills the cvs buffer.
5b467bf4 96;; right now, it's killed without further ado.
cb3430a1 97;; - make `cvs-mode-ignore' allow manually entering a pattern.
5b467bf4 98;; to which dir should it apply ?
cb3430a1
SM
99;; - cvs-mode-ignore should try to remove duplicate entries.
100;; - maybe poll/check CVS/Entries files to react to external `cvs' commands ?
101;; - some kind of `cvs annotate' support ?
5b467bf4 102;; but vc-annotate can be used instead.
44946a4c
SM
103;; - proper `g' that passes safe args and uses either cvs-status or cvs-examine
104;; maybe also use cvs-update depending on I-don't-know-what.
cb3430a1 105;; - add message-levels so that we can hide some levels of messages
5b467bf4
SM
106
107;;; Code:
108
3db3a13f 109(eval-when-compile (require 'cl))
5b467bf4
SM
110(require 'ewoc) ;Ewoc was once cookie
111(require 'pcvs-defs)
112(require 'pcvs-util)
113(require 'pcvs-parse)
114(require 'pcvs-info)
115
116\f
117;;;;
118;;;; global vars
119;;;;
120
121(defvar cvs-cookies) ;;nil
122 ;;"Handle for the cookie structure that is displayed in the *cvs* buffer.")
123;;(make-variable-buffer-local 'cvs-cookies)
124
125;;;;
126;;;; Dynamically scoped variables
127;;;;
128
129(defvar cvs-from-vc nil "Bound to t inside VC advice.")
130
027b73ac 131;;;;
5b467bf4 132;;;; flags variables
027b73ac 133;;;;
5b467bf4
SM
134
135(defun cvs-defaults (&rest defs)
136 (let ((defs (cvs-first defs cvs-shared-start)))
137 (append defs
44946a4c 138 (make-list (- cvs-shared-start (length defs)) (car defs))
5b467bf4
SM
139 cvs-shared-flags)))
140
141;; For cvs flags, we need to add "-f" to override the cvsrc settings
142;; we also want to evict the annoying -q and -Q options that hide useful
143;; information from pcl-cvs.
144(cvs-flags-define cvs-cvs-flags '(("-f")))
145
146(cvs-flags-define cvs-checkout-flags (cvs-defaults '("-P")))
147(cvs-flags-define cvs-status-flags (cvs-defaults '("-v") nil))
148(cvs-flags-define cvs-log-flags (cvs-defaults nil))
6dc7d3d5 149(cvs-flags-define cvs-diff-flags (cvs-defaults '("-u" "-N") '("-c" "-N") '("-u" "-b")))
5b467bf4
SM
150(cvs-flags-define cvs-tag-flags (cvs-defaults nil))
151(cvs-flags-define cvs-add-flags (cvs-defaults nil))
152(cvs-flags-define cvs-commit-flags (cvs-defaults nil))
153(cvs-flags-define cvs-remove-flags (cvs-defaults nil))
154;;(cvs-flags-define cvs-undo-flags (cvs-defaults nil))
155(cvs-flags-define cvs-update-flags (cvs-defaults '("-d" "-P")))
156
157(defun cvs-reread-cvsrc ()
158 "Reset the default arguments to those in the `cvs-cvsrc-file'."
159 (interactive)
160 (let ((cvsrc (cvs-file-to-string cvs-cvsrc-file)))
161 (when (stringp cvsrc)
162 ;; fetch the values
163 (dolist (cmd '("cvs" "checkout" "status" "log" "diff" "tag"
164 "add" "commit" "remove" "update"))
165 (let* ((sym (intern (concat "cvs-" cmd "-flags")))
166 (val (when (string-match (concat "^" cmd "\\s-\\(.*\\)$") cvsrc)
167 (cvs-string->strings (match-string 1 cvsrc)))))
168 (cvs-flags-set sym 0 val)))
169 ;; ensure that cvs doesn't have -q or -Q
170 (cvs-flags-set 'cvs-cvs-flags 0
171 (cons "-f"
172 (cdr (cvs-partition
173 (lambda (x) (member x '("-q" "-Q")))
174 (cvs-flags-query 'cvs-cvs-flags
175 nil 'noquery))))))))
176
177;; initialize to cvsrc's default values
178(cvs-reread-cvsrc)
179
180\f
181;;;;
182;;;; Mouse bindings and mode motion
183;;;;
184
185(defun cvs-menu (e)
186 "Popup the CVS menu."
187 (interactive "e")
96190aa1
SM
188 (let ((cvs-minor-current-files
189 (list (ewoc-data (ewoc-locate
190 cvs-cookies (posn-point (event-end e)))))))
191 (popup-menu cvs-menu-map e)))
5b467bf4
SM
192
193(defvar cvs-mode-line-process nil
194 "Mode-line control for displaying info on cvs process status.")
195
196
027b73ac 197;;;;
5b467bf4 198;;;; Query-Type-Descriptor for Tags
027b73ac 199;;;;
5b467bf4
SM
200
201(autoload 'cvs-status-get-tags "cvs-status")
202(defun cvs-tags-list ()
203 "Return a list of acceptable tags, ready for completions."
204 (assert (cvs-buffer-p))
205 (let ((marked (cvs-get-marked)))
206 (list* '("BASE") '("HEAD")
207 (when marked
208 (with-temp-buffer
209 (call-process cvs-program
210 nil ;no input
211 t ;output to current-buffer
212 nil ;don't update display while running
213 "status"
214 "-v"
215 (cvs-fileinfo->full-path (car marked)))
216 (goto-char (point-min))
217 (let ((tags (cvs-status-get-tags)))
218 (when (listp tags) tags)))))))
219
220(defvar cvs-tag-history nil)
221(defconst cvs-qtypedesc-tag
222 (cvs-qtypedesc-create 'identity 'identity 'cvs-tags-list 'cvs-tag-history))
223
027b73ac 224;;;;
5b467bf4
SM
225
226(defun cvs-mode! (&optional -cvs-mode!-fun -cvs-mode!-noerror)
227 "Switch to the *cvs* buffer.
228If -CVS-MODE!-FUN is provided, it is executed *cvs* being the current buffer
229 and with its window selected. Else, the *cvs* buffer is simply selected.
230If -CVS-MODE!-NOERROR is non-nil, then failure to find a *cvs* buffer does
231 not generate an error and the current buffer is kept selected.
232-CVS-MODE!-FUN is called interactively if applicable and else with no argument."
233 (let* ((-cvs-mode!-buf (current-buffer))
234 (cvsbuf (cond ((cvs-buffer-p) (current-buffer))
235 ((and cvs-buffer (cvs-buffer-p cvs-buffer)) cvs-buffer)
236 (-cvs-mode!-noerror (current-buffer))
237 (t (error "can't find the *cvs* buffer."))))
238 (-cvs-mode!-wrapper cvs-minor-wrap-function)
239 (-cvs-mode!-cont (lambda ()
240 (save-current-buffer
241 (if (commandp -cvs-mode!-fun)
242 (call-interactively -cvs-mode!-fun)
243 (funcall -cvs-mode!-fun))))))
244 (if (not -cvs-mode!-fun) (set-buffer cvsbuf)
245 (let ((cvs-mode!-buf (current-buffer))
246 (cvs-mode!-owin (selected-window))
247 (cvs-mode!-nwin (get-buffer-window cvsbuf 'visible)))
248 (unwind-protect
249 (progn
250 (set-buffer cvsbuf)
251 (when cvs-mode!-nwin (select-window cvs-mode!-nwin))
252 (if -cvs-mode!-wrapper
253 (funcall -cvs-mode!-wrapper -cvs-mode!-buf -cvs-mode!-cont)
254 (funcall -cvs-mode!-cont)))
255 (set-buffer cvs-mode!-buf)
256 (when (and cvs-mode!-nwin (eq cvs-mode!-nwin (selected-window)))
257 ;; the selected window has not been changed by FUN
258 (select-window cvs-mode!-owin)))))))
259
027b73ac 260;;;;
5b467bf4 261;;;; Prefixes
027b73ac 262;;;;
5b467bf4
SM
263
264(defvar cvs-branches (list cvs-vendor-branch "HEAD" "HEAD"))
265(cvs-prefix-define cvs-branch-prefix
266 "Current selected branch."
267 "version"
268 (cons cvs-vendor-branch cvs-branches)
269 cvs-qtypedesc-tag)
270
271(defun cvs-set-branch-prefix (arg)
272 "Set the branch prefix to take action at the next command.
273See `cvs-prefix-set' for a further the description of the behavior.
274\\[universal-argument] 1 selects the vendor branch
275and \\[universal-argument] 2 selects the HEAD."
276 (interactive "P")
277 (cvs-mode!)
278 (cvs-prefix-set 'cvs-branch-prefix arg))
279
280(defun cvs-add-branch-prefix (flags &optional arg)
281 "Add branch selection argument if the branch prefix was set.
282The argument is added (or not) to the list of FLAGS and is constructed
283by appending the branch to ARG which defaults to \"-r\"."
284 (let ((branch (cvs-prefix-get 'cvs-branch-prefix)))
285 ;; deactivate the secondary prefix, even if not used.
286 (cvs-prefix-get 'cvs-secondary-branch-prefix)
287 (if branch (cons (concat (or arg "-r") branch) flags) flags)))
288
289(cvs-prefix-define cvs-secondary-branch-prefix
290 "Current secondary selected branch."
291 "version"
292 (cons cvs-vendor-branch cvs-branches)
293 cvs-qtypedesc-tag)
294
295(defun cvs-set-secondary-branch-prefix (arg)
296 "Set the branch prefix to take action at the next command.
297See `cvs-prefix-set' for a further the description of the behavior.
298\\[universal-argument] 1 selects the vendor branch
299and \\[universal-argument] 2 selects the HEAD."
300 (interactive "P")
301 (cvs-mode!)
302 (cvs-prefix-set 'cvs-secondary-branch-prefix arg))
303
304(defun cvs-add-secondary-branch-prefix (flags &optional arg)
305 "Add branch selection argument if the secondary branch prefix was set.
306The argument is added (or not) to the list of FLAGS and is constructed
307by appending the branch to ARG which defaults to \"-r\".
308Since the `cvs-secondary-branch-prefix' is only active if the primary
309prefix is active, it is important to read the secondary prefix before
310the primay since reading the primary can deactivate it."
311 (let ((branch (and (cvs-prefix-get 'cvs-branch-prefix 'read-only)
312 (cvs-prefix-get 'cvs-secondary-branch-prefix))))
313 (if branch (cons (concat (or arg "-r") branch) flags) flags)))
314
027b73ac 315;;;;
5b467bf4
SM
316
317(define-minor-mode
318 cvs-minor-mode
319 "
320This mode is used for buffers related to a main *cvs* buffer.
321All the `cvs-mode' buffer operations are simply rebound under
322the \\[cvs-mode-map] prefix.
323"
324 nil " CVS")
325(put 'cvs-minor-mode 'permanent-local t)
326
327
328(defvar cvs-temp-buffers nil)
329(defun cvs-temp-buffer (&optional cmd normal nosetup)
330 "Create a temporary buffer to run CMD in.
331If CMD is a string, use it to lookup `cvs-buffer-name-alist' to find
332the buffer name to be used and its `major-mode'.
333
334The selected window will not be changed. The new buffer will not maintain undo
335information and will be read-only unless NORMAL is non-nil. It will be emptied
336\(unless NOSETUP is non-nil\) and its `default-directory' will be inherited
337from the current buffer."
338 (let* ((cvs-buf (current-buffer))
339 (info (cdr (assoc cmd cvs-buffer-name-alist)))
dedffa6a
GM
340 (name (eval (nth 0 info)))
341 (mode (nth 1 info))
5b467bf4
SM
342 (dir default-directory)
343 (buf (cond
344 (name (cvs-get-buffer-create name))
345 ((and (bufferp cvs-temp-buffer) (buffer-name cvs-temp-buffer))
346 cvs-temp-buffer)
347 (t
348 (set (make-local-variable 'cvs-temp-buffer)
349 (cvs-get-buffer-create
350 (eval cvs-temp-buffer-name) 'noreuse))))))
027b73ac 351
5b467bf4
SM
352 ;; handle the potential pre-existing process
353 (let ((proc (get-buffer-process buf)))
354 (when (and (not normal) (processp proc)
355 (memq (process-status proc) '(run stop)))
356 (error "Can not run two cvs processes simultaneously")))
357
358 (if (not name) (kill-local-variable 'other-window-scroll-buffer)
359 ;; Strangely, if no window is created, `display-buffer' ends up
360 ;; doing a `switch-to-buffer' which does a `set-buffer', hence
361 ;; the need for `save-excursion'.
362 (unless nosetup (save-excursion (display-buffer buf)))
363 ;; FIXME: this doesn't do the right thing if the user later on
364 ;; does a `find-file-other-window' and `scroll-other-window'
365 (set (make-local-variable 'other-window-scroll-buffer) buf))
366
367 (add-to-list 'cvs-temp-buffers buf)
368
369 (with-current-buffer buf
370 (setq buffer-read-only nil)
371 (setq default-directory dir)
372 (unless nosetup (erase-buffer))
373 (set (make-local-variable 'cvs-buffer) cvs-buf)
374 ;;(cvs-minor-mode 1)
375 (let ((lbd list-buffers-directory))
376 (if (fboundp mode) (funcall mode) (fundamental-mode))
377 (when lbd (set (make-local-variable 'list-buffers-directory) lbd)))
378 (cvs-minor-mode 1)
379 ;;(set (make-local-variable 'cvs-buffer) cvs-buf)
380 (unless normal
381 (setq buffer-read-only t)
382 (buffer-disable-undo))
383 buf)))
384
385(defun cvs-mode-kill-buffers ()
386 "Kill all the \"temporary\" buffers created by the *cvs* buffer."
387 (interactive)
388 (dolist (buf cvs-temp-buffers) (ignore-errors (kill-buffer buf))))
389
390(defun cvs-make-cvs-buffer (dir &optional new)
391 "Create the *cvs* buffer for directory DIR.
392If non-nil, NEW means to create a new buffer no matter what."
393 ;; the real cvs-buffer creation
394 (setq dir (cvs-expand-dir-name dir))
395 (let* ((buffer-name (eval cvs-buffer-name))
396 (buffer
397 (or (and (not new)
398 (eq cvs-reuse-cvs-buffer 'current)
399 (cvs-buffer-p) ;reuse the current buffer if possible
400 (current-buffer))
401 ;; look for another cvs buffer visiting the same directory
402 (save-excursion
403 (unless new
404 (dolist (buffer (cons (current-buffer) (buffer-list)))
405 (set-buffer buffer)
406 (and (cvs-buffer-p)
407 (case cvs-reuse-cvs-buffer
408 (always t)
409 (subdir
410 (or (cvs-string-prefix-p default-directory dir)
411 (cvs-string-prefix-p dir default-directory)))
412 (samedir (string= default-directory dir)))
413 (return buffer)))))
414 ;; we really have to create a new buffer:
415 ;; we temporarily bind cwd to "" to prevent
416 ;; create-file-buffer from using directory info
417 ;; unless it is explicitly in the cvs-buffer-name.
418 (cvs-get-buffer-create buffer-name new))))
419 (with-current-buffer buffer
420 (or
421 (and (string= dir default-directory) (cvs-buffer-p)
422 ;; just a refresh
423 (ignore-errors
424 (cvs-cleanup-collection cvs-cookies nil nil t)
425 (current-buffer)))
426 ;; setup from scratch
427 (progn
428 (setq default-directory dir)
429 (setq buffer-read-only nil)
430 (erase-buffer)
cb3430a1
SM
431 (insert "\
432Repository : " (directory-file-name (cvs-get-cvsroot)) "
433Module : " (cvs-get-module) "
434Working dir: " (abbreviate-file-name dir) "
435
436")
5b467bf4
SM
437 (setq buffer-read-only t)
438 (cvs-mode)
439 (set (make-local-variable 'list-buffers-directory) buffer-name)
440 ;;(set (make-local-variable 'cvs-temp-buffer) (cvs-temp-buffer))
cb3430a1 441 (let ((cookies (ewoc-create 'cvs-fileinfo-pp "\n" "")))
5b467bf4 442 (set (make-local-variable 'cvs-cookies) cookies)
5b467bf4
SM
443 (add-hook 'kill-buffer-hook
444 (lambda ()
445 (ignore-errors (kill-buffer cvs-temp-buffer)))
446 nil t)
447 ;;(set-buffer buf)
448 buffer))))))
449
450(defun* cvs-cmd-do (cmd dir flags fis new
451 &key cvsargs noexist dont-change-disc noshow)
452 (let* ((dir (file-name-as-directory
453 (abbreviate-file-name (expand-file-name dir))))
454 (cvsbuf (cvs-make-cvs-buffer dir new)))
455 ;; Check that dir is under CVS control.
456 (unless (file-directory-p dir)
6dc7d3d5 457 (error "%s is not a directory" dir))
5b467bf4 458 (unless (or noexist (file-directory-p (expand-file-name "CVS" dir)))
6dc7d3d5 459 (error "%s does not contain CVS controlled files" dir))
5b467bf4
SM
460
461 (set-buffer cvsbuf)
462 (cvs-mode-run cmd flags fis
463 :cvsargs cvsargs :dont-change-disc dont-change-disc)
464
465 (if noshow cvsbuf
466 (let ((pop-up-windows nil)) (pop-to-buffer cvsbuf)))))
467;; (funcall (if (and (boundp 'pop-up-frames) pop-up-frames)
468;; 'pop-to-buffer 'switch-to-buffer)
469;; cvsbuf))))
470
5b467bf4
SM
471(defun cvs-run-process (args fis postprocess &optional single-dir)
472 (assert (cvs-buffer-p cvs-buffer))
473 (save-current-buffer
474 (let ((procbuf (current-buffer))
475 (cvsbuf cvs-buffer)
476 (single-dir (or single-dir (eq cvs-execute-single-dir t))))
027b73ac 477
5b467bf4
SM
478 (set-buffer procbuf)
479 (goto-char (point-max))
480 (unless (bolp) (let ((inhibit-read-only t)) (insert "\n")))
481 ;; find the set of files we'll process in this round
482 (let* ((dir+files+rest
483 (if (or (null fis) (not single-dir))
484 ;; not single-dir mode: just process the whole thing
485 (list "" (mapcar 'cvs-fileinfo->full-path fis) nil)
486 ;; single-dir mode: extract the same-dir-elements
487 (let ((dir (cvs-fileinfo->dir (car fis))))
488 ;; output the concerned dir so the parser can translate paths
489 (let ((inhibit-read-only t))
490 (insert "pcl-cvs: descending directory " dir "\n"))
491 ;; loop to find the same-dir-elems
492 (do* ((files () (cons (cvs-fileinfo->file fi) files))
493 (fis fis (cdr fis))
494 (fi (car fis) (car fis)))
495 ((not (and fis (string= dir (cvs-fileinfo->dir fi))))
496 (list dir files fis))))))
dedffa6a
GM
497 (dir (nth 0 dir+files+rest))
498 (files (nth 1 dir+files+rest))
499 (rest (nth 2 dir+files+rest)))
027b73ac 500
5b467bf4
SM
501 ;; setup the (current) process buffer
502 (set (make-local-variable 'cvs-postprocess)
503 (if (null rest)
504 ;; this is the last invocation
505 postprocess
506 ;; else, we have to register ourselves to be rerun on the rest
507 `(cvs-run-process ',args ',rest ',postprocess ',single-dir)))
5b467bf4
SM
508 (add-hook 'kill-buffer-hook
509 (lambda ()
510 (let ((proc (get-buffer-process (current-buffer))))
511 (when (processp proc)
512 (set-process-filter proc nil)
513 (set-process-sentinel proc nil)
514 (delete-process proc))))
515 nil t)
516
517 ;; create the new process and setup the procbuffer correspondingly
518 (let* ((args (append (cvs-flags-query 'cvs-cvs-flags nil 'noquery)
519 (if cvs-cvsroot (list "-d" cvs-cvsroot))
520 args
521 files))
522 (process-connection-type nil) ; Use a pipe, not a pty.
523 (process
524 ;; the process will be run in the selected dir
525 (let ((default-directory (cvs-expand-dir-name dir)))
526 (apply 'start-process "cvs" procbuf cvs-program args))))
527 (set-process-sentinel process 'cvs-sentinel)
528 (set-process-filter process 'cvs-update-filter)
529 (set-marker (process-mark process) (point-max))
530 (ignore-errors (process-send-eof process)) ;close its stdin to avoid hangs
027b73ac 531
5b467bf4
SM
532 ;; now finish setting up the cvs-buffer
533 (set-buffer cvsbuf)
534 (setq cvs-mode-line-process (symbol-name (process-status process)))
535 (force-mode-line-update)))))
536
537 ;; The following line is said to improve display updates on some
538 ;; emacsen. It shouldn't be needed, but it does no harm.
539 (sit-for 0))
540
541(defun cvs-update-header (args fis) ; inline
542 (let* ((lastarg nil)
543 ;; filter out the largish commit message
544 (args (mapcar (lambda (arg)
545 (cond
546 ((and (eq lastarg nil) (string= arg "commit"))
547 (setq lastarg 'commit) arg)
548 ((and (eq lastarg 'commit) (string= arg "-m"))
549 (setq lastarg '-m) arg)
550 ((eq lastarg '-m)
551 (setq lastarg 'done) "<log message>")
552 (t arg)))
553 args))
554 ;; turn them into a string
555 (arg (cvs-strings->string
556 (append (cvs-flags-query 'cvs-cvs-flags nil 'noquery)
557 (if cvs-cvsroot (list "-d" cvs-cvsroot))
558 args
559 (mapcar 'cvs-fileinfo->full-path fis))))
560 (str (if args (concat "-- Running " cvs-program " " arg " ...\n")
561 "\n")))
562 (if nil (insert str) ;inline
563 ;;(with-current-buffer cvs-buffer
cb3430a1
SM
564 (let* ((prev-msg (car (ewoc-get-hf cvs-cookies)))
565 (tin (ewoc-nth cvs-cookies 0)))
5b467bf4
SM
566 ;; look for the first *real* fileinfo (to determine emptyness)
567 (while
568 (and tin
569 (memq (cvs-fileinfo->type (ewoc-data tin))
570 '(MESSAGE DIRCHANGE)))
571 (setq tin (ewoc-next cvs-cookies tin)))
572 ;; cleanup the prev-msg
573 (when (string-match "Running \\(.*\\) ...\n" prev-msg)
574 (setq prev-msg
575 (concat
576 "-- last cmd: "
577 (match-string 1 prev-msg)
578 " --")))
579 ;; set the new header and footer
cb3430a1
SM
580 (ewoc-set-hf cvs-cookies
581 str (concat "\n--------------------- "
582 (if tin "End" "Empty")
583 " ---------------------\n"
584 prev-msg))))))
5b467bf4
SM
585
586
5b467bf4
SM
587(defun cvs-sentinel (proc msg)
588 "Sentinel for the cvs update process.
589This is responsible for parsing the output from the cvs update when
590it is finished."
591 (when (memq (process-status proc) '(signal exit))
592 (if (null (buffer-name (process-buffer proc)))
593 ;;(set-process-buffer proc nil)
594 (error "cvs' process buffer was killed")
595 (let* ((obuf (current-buffer))
596 (procbuffer (process-buffer proc)))
597 (set-buffer (with-current-buffer procbuffer cvs-buffer))
598 (setq cvs-mode-line-process (symbol-name (process-status proc)))
599 (force-mode-line-update)
600 (set-buffer procbuffer)
601 (let ((cvs-postproc cvs-postprocess))
602 ;; Since the buffer and mode line will show that the
603 ;; process is dead, we can delete it now. Otherwise it
604 ;; will stay around until M-x list-processes.
605 (delete-process proc)
606 (setq cvs-postprocess nil)
607 ;; do the postprocessing like parsing and such
608 (save-excursion (eval cvs-postproc))
609 ;; check whether something is left
610 (unless cvs-postprocess
611 (buffer-enable-undo)
612 (with-current-buffer cvs-buffer
613 (cvs-update-header nil nil) ;FIXME: might need to be inline
614 (message "CVS process has completed"))))
615 ;; This might not even be necessary
616 (set-buffer obuf)))))
617
5b467bf4
SM
618(defun cvs-parse-process (dcd &optional subdir)
619 "FIXME: bad name, no doc"
620 (let* ((from-buf (current-buffer))
621 (fileinfos (cvs-parse-buffer 'cvs-parse-table dcd subdir))
622 (_ (set-buffer cvs-buffer))
623 last
624 (from-pt (point)))
625 ;; add the new fileinfos
626 (dolist (fi fileinfos)
627 (setq last (cvs-addto-collection cvs-cookies fi last)))
628 (cvs-cleanup-collection cvs-cookies
629 (eq cvs-auto-remove-handled t)
630 cvs-auto-remove-directories
631 nil)
632 ;; update the display (might be unnecessary)
6dc7d3d5 633 ;;(ewoc-refresh cvs-cookies)
5b467bf4
SM
634 ;; revert buffers if necessary
635 (when (and cvs-auto-revert (not dcd) (not cvs-from-vc))
636 (cvs-revert-if-needed fileinfos))
637 ;; get back to where we were. `save-excursion' doesn't seem to
638 ;; work in this case, probably because the buffer is reconstructed
639 ;; by the cookie code.
640 (goto-char from-pt)
641 (set-buffer from-buf)))
642
643(defmacro defun-cvs-mode (fun args docstring interact &rest body)
644 "Define a function to be used in a *cvs* buffer.
645This will look for a *cvs* buffer and execute BODY in it.
646Since the interactive arguments might need to be queried after
647switching to the *cvs* buffer, the generic code is rather ugly,
648but luckily we can often use simpler alternatives.
649
650FUN can be either a symbol (i.e. STYLE is nil) or a cons (FUN . STYLE).
651ARGS and DOCSTRING are the normal argument list.
652INTERACT is the interactive specification or nil for non-commands.
653
654STYLE can be either SIMPLE, NOARGS or DOUBLE. It's an error for it
655to have any other value, unless other details of the function make it
656clear what alternative to use.
657- SIMPLE will get all the interactive arguments from the original buffer.
658- NOARGS will get all the arguments from the *cvs* buffer and will
659 always behave as if called interactively.
660- DOUBLE is the generic case."
661 (let ((style (cvs-cdr fun))
662 (fun (cvs-car fun)))
663 (cond
664 ;; a trivial interaction, no need to move it
665 ((or (eq style 'SIMPLE)
dedffa6a
GM
666 (null (nth 1 interact))
667 (stringp (nth 1 interact)))
5b467bf4
SM
668 `(defun ,fun ,args ,docstring ,interact
669 (cvs-mode! (lambda () ,@body))))
670
671 ;; fun is only called interactively: move all the args to the inner fun
672 ((eq style 'NOARGS)
673 `(defun ,fun () ,docstring (interactive)
674 (cvs-mode! (lambda ,args ,interact ,@body))))
675
676 ;; bad case
677 ((eq style 'DOUBLE)
678 (string-match ".*" docstring)
679 (let ((line1 (match-string 0 docstring))
680 (restdoc (substring docstring (match-end 0)))
681 (fun-1 (intern (concat (symbol-name fun) "-1"))))
682 `(progn
683 (defun ,fun-1 ,args
684 ,(concat docstring "\nThis function only works within a *cvs* buffer.
685For interactive use, use `" (symbol-name fun) "' instead.")
686 ,interact
687 ,@body)
688 (defun ,fun ()
689 ,(concat line1 "\nWrapper function that switches to a *cvs* buffer
690before calling the real function `" (symbol-name fun-1) "'.\n")
691 (interactive)
692 (cvs-mode! ',fun-1)))))
693
694 (t (error "unknown style %s in `defun-cvs-mode'" style)))))
695(def-edebug-spec defun-cvs-mode (&define sexp lambda-list stringp ("interactive" interactive) def-body))
696
697(defun-cvs-mode cvs-mode-kill-process ()
698 "Kill the temporary buffer and associated process."
699 (interactive)
700 (when (and (bufferp cvs-temp-buffer) (buffer-name cvs-temp-buffer))
701 (let ((proc (get-buffer-process cvs-temp-buffer)))
702 (when proc (delete-process proc)))))
703
704;;;
705;;; Maintaining the collection in the face of updates
706;;;
707
708(defun cvs-addto-collection (c fi &optional tin)
3c7fafc7 709 "Add FI to C and return FI's corresponding tin.
5b467bf4
SM
710FI is inserted in its proper place or maybe even merged with a preexisting
711 fileinfo if applicable.
712TIN specifies an optional starting point."
713 (unless tin (setq tin (ewoc-nth c 0)))
714 (while (and tin (cvs-fileinfo< fi (ewoc-data tin)))
715 (setq tin (ewoc-prev c tin)))
3c7fafc7 716 (if (null tin) (ewoc-enter-first c fi) ;empty collection
5b467bf4
SM
717 (assert (not (cvs-fileinfo< fi (ewoc-data tin))))
718 (let ((next-tin (ewoc-next c tin)))
719 (while (not (or (null next-tin)
720 (cvs-fileinfo< fi (ewoc-data next-tin))))
721 (setq tin next-tin next-tin (ewoc-next c next-tin)))
722 (if (cvs-fileinfo< (ewoc-data tin) fi)
723 ;; tin < fi < next-tin
724 (ewoc-enter-after c tin fi)
725 ;; fi == tin
726 (cvs-fileinfo-update (ewoc-data tin) fi)
3c7fafc7
SM
727 (ewoc-invalidate c tin)
728 tin))))
5b467bf4 729
6dc7d3d5
SM
730(defcustom cvs-cleanup-functions nil
731 "Functions to tweak the cleanup process.
732The functions are called with a single argument (a FILEINFO) and should
733return a non-nil value if that fileinfo should be removed."
734 :group 'pcl-cvs
735 :type '(hook :options (cvs-cleanup-removed)))
736
737(defun cvs-cleanup-removed (fi)
738 "Non-nil if FI has been cvs-removed but still exists.
739This is intended for use on `cvs-cleanup-functions' when you have cvs-removed
740automatically generated files (which should hence not be under CVS control)
741but can't commit the removal because the repository's owner doesn't understand
742the problem."
743 (and (or (eq (cvs-fileinfo->type fi) 'REMOVED)
744 (and (eq (cvs-fileinfo->type fi) 'CONFLICT)
745 (eq (cvs-fileinfo->subtype fi) 'REMOVED)))
746 (file-exists-p (cvs-fileinfo->full-path fi))))
747
5b467bf4
SM
748;; called at the following times:
749;; - postparse ((eq cvs-auto-remove-handled t) cvs-auto-remove-directories nil)
750;; - pre-run ((eq cvs-auto-remove-handled 'delayed) nil t)
751;; - remove-handled (t (or cvs-auto-remove-directories 'handled) t)
752;; - cvs-cmd-do (nil nil t)
753;; - post-ignore (nil nil nil)
754;; - acknowledge (nil nil nil)
755;; - remove (nil nil nil)
756(defun cvs-cleanup-collection (c rm-handled rm-dirs rm-msgs)
757 "Remove undesired entries.
758C is the collection
759RM-HANDLED if non-nil means remove handled entries.
760RM-DIRS behaves like `cvs-auto-remove-directories'.
761RM-MSGS if non-nil means remove messages."
762 (let (last-fi first-dir (rerun t))
763 (while rerun
764 (setq rerun nil)
765 (setq first-dir t)
766 (setq last-fi (cvs-create-fileinfo 'DEAD "../" "" "")) ;place-holder
767 (ewoc-filter
768 c (lambda (fi)
769 (let* ((type (cvs-fileinfo->type fi))
770 (subtype (cvs-fileinfo->subtype fi))
771 (keep
772 (case type
773 ;; remove temp messages and keep the others
b15b5618 774 (MESSAGE (not (or rm-msgs (eq subtype 'TEMP))))
5b467bf4
SM
775 ;; remove entries
776 (DEAD nil)
777 ;; handled also?
778 (UP-TO-DATE (not rm-handled))
779 ;; keep the rest
6dc7d3d5
SM
780 (t (not (run-hook-with-args-until-success
781 'cvs-cleanup-functions fi))))))
027b73ac 782
5b467bf4
SM
783 ;; mark dirs for removal
784 (when (and keep rm-dirs
785 (eq (cvs-fileinfo->type last-fi) 'DIRCHANGE)
786 (not (when first-dir (setq first-dir nil) t))
787 (or (eq rm-dirs 'all)
788 (not (cvs-string-prefix-p
789 (cvs-fileinfo->dir last-fi)
790 (cvs-fileinfo->dir fi)))
791 (and (eq type 'DIRCHANGE) (eq rm-dirs 'empty))
792 (eq subtype 'FOOTER)))
793 (setf (cvs-fileinfo->type last-fi) 'DEAD)
794 (setq rerun t))
b15b5618
SM
795 (when keep (setq last-fi fi)))))
796 ;; remove empty last dir
797 (when (and rm-dirs
798 (not first-dir)
799 (eq (cvs-fileinfo->type last-fi) 'DIRCHANGE))
800 (setf (cvs-fileinfo->type last-fi) 'DEAD)
801 (setq rerun t)))))
5b467bf4
SM
802
803(defun cvs-get-cvsroot ()
804 "Gets the CVSROOT for DIR."
805 (let ((cvs-cvsroot-file (expand-file-name "Root" "CVS")))
806 (or (cvs-file-to-string cvs-cvsroot-file t)
807 cvs-cvsroot
808 (getenv "CVSROOT")
809 "?????")))
810
811(defun cvs-get-module ()
812 "Return the current CVS module.
813This usually doesn't really work but is a handy initval in a prompt."
814 (let* ((repfile (expand-file-name "Repository" "CVS"))
815 (rep (cvs-file-to-string repfile t)))
816 (cond
817 ((null rep) "")
818 ((not (file-name-absolute-p rep)) rep)
819 (t
820 (let* ((root (cvs-get-cvsroot))
821 (str (concat (file-name-as-directory (or root "/")) " || " rep)))
822 (if (and root (string-match "\\(.*\\) || \\1\\(.*\\)\\'" str))
823 (match-string 2 str)
824 (file-name-nondirectory rep)))))))
825
826
827\f
828;;;;
829;;;; running a "cvs checkout".
830;;;;
831
832;;;###autoload
833(defun cvs-checkout (modules dir flags)
834 "Run a 'cvs checkout MODULES' in DIR.
835Feed the output to a *cvs* buffer, display it in the current window,
836and run `cvs-mode' on it.
837
838With a prefix argument, prompt for cvs FLAGS to use."
839 (interactive
840 (list (cvs-string->strings (read-string "Module(s): " (cvs-get-module)))
841 (read-file-name "CVS Checkout Directory: "
842 nil default-directory nil)
843 (cvs-add-branch-prefix
844 (cvs-flags-query 'cvs-checkout-flags "cvs checkout flags"))))
845 (when (eq flags t)
846 (setf flags (cvs-flags-query 'cvs-checkout-flags nil 'noquery)))
847 (cvs-cmd-do "checkout" (or dir default-directory)
848 (append flags modules) nil 'new
849 :noexist t))
850
851\f
027b73ac 852;;;;
5b467bf4 853;;;; The code for running a "cvs update" and friends in various ways.
027b73ac 854;;;;
5b467bf4
SM
855
856(defun-cvs-mode (cvs-mode-revert-buffer . SIMPLE)
857 (&optional ignore-auto noconfirm)
44946a4c 858 "Rerun `cvs-examine' on the current directory with the defauls flags."
5b467bf4
SM
859 (interactive)
860 (cvs-examine default-directory t))
861
862(defun cvs-query-directory (msg)
863 ;; last-command-char = ?\r hints that the command was run via M-x
864 (if (and (cvs-buffer-p)
865 (not current-prefix-arg)
866 (not (eq last-command-char ?\r)))
867 default-directory
868 (read-file-name msg nil default-directory nil)))
869
6dc7d3d5
SM
870;;;###autoload
871(defun cvs-quickdir (dir &optional flags noshow)
872 "Open a *cvs* buffer on DIR without running cvs.
873With a prefix argument, prompt for a directory to use.
874A prefix arg >8 (ex: \\[universal-argument] \\[universal-argument]),
875 prevents reuse of an existing *cvs* buffer.
876Optional argument NOSHOW if non-nil means not to display the buffer.
877FLAGS is ignored."
878 (interactive (list (cvs-query-directory "CVS quickdir (directory): ")))
879 ;; FIXME: code duplication with cvs-cmd-do and cvs-parse-process
880 (let* ((dir (file-name-as-directory
881 (abbreviate-file-name (expand-file-name dir))))
882 (new (> (prefix-numeric-value current-prefix-arg) 8))
883 (cvsbuf (cvs-make-cvs-buffer dir new))
884 last)
885 ;; Check that dir is under CVS control.
886 (unless (file-directory-p dir)
887 (error "%s is not a directory" dir))
888 (unless (file-directory-p (expand-file-name "CVS" dir))
889 (error "%s does not contain CVS controlled files" dir))
890 (set-buffer cvsbuf)
891 (dolist (fi (cvs-fileinfo-from-entries ""))
892 (setq last (cvs-addto-collection cvs-cookies fi last)))
893 (cvs-cleanup-collection cvs-cookies
894 (eq cvs-auto-remove-handled t)
895 cvs-auto-remove-directories
896 nil)
897 (if noshow cvsbuf
898 (let ((pop-up-windows nil)) (pop-to-buffer cvsbuf)))))
5b467bf4
SM
899
900;;;###autoload
901(defun cvs-examine (directory flags &optional noshow)
902 "Run a `cvs -n update' in the specified DIRECTORY.
903That is, check what needs to be done, but don't change the disc.
904Feed the output to a *cvs* buffer and run `cvs-mode' on it.
905With a prefix argument, prompt for a directory and cvs FLAGS to use.
906A prefix arg >8 (ex: \\[universal-argument] \\[universal-argument]),
907 prevents reuse of an existing *cvs* buffer.
908Optional argument NOSHOW if non-nil means not to display the buffer."
909 (interactive (list (cvs-query-directory "CVS Examine (directory): ")
910 (cvs-flags-query 'cvs-update-flags "cvs -n update flags")))
911 (when (eq flags t)
912 (setf flags (cvs-flags-query 'cvs-update-flags nil 'noquery)))
913 (cvs-cmd-do "update" directory flags nil
914 (> (prefix-numeric-value current-prefix-arg) 8)
915 :cvsargs '("-n")
916 :noshow noshow
917 :dont-change-disc t))
918
919
920;;;###autoload
921(defun cvs-update (directory flags)
922 "Run a `cvs update' in the current working DIRECTORY.
923Feed the output to a *cvs* buffer and run `cvs-mode' on it.
924With a prefix argument, prompt for a directory and cvs FLAGS to use.
925A prefix arg >8 (ex: \\[universal-argument] \\[universal-argument]),
926 prevents reuse of an existing *cvs* buffer."
927 (interactive (list (cvs-query-directory "CVS Update (directory): ")
928 (cvs-flags-query 'cvs-update-flags "cvs update flags")))
929 (when (eq flags t)
930 (setf flags (cvs-flags-query 'cvs-update-flags nil 'noquery)))
931 (cvs-cmd-do "update" directory flags nil
932 (> (prefix-numeric-value current-prefix-arg) 8)))
933
934
935;;;###autoload
936(defun cvs-status (directory flags &optional noshow)
937 "Run a `cvs status' in the current working DIRECTORY.
938Feed the output to a *cvs* buffer and run `cvs-mode' on it.
939With a prefix argument, prompt for a directory and cvs FLAGS to use.
940A prefix arg >8 (ex: \\[universal-argument] \\[universal-argument]),
941 prevents reuse of an existing *cvs* buffer.
942Optional argument NOSHOW if non-nil means not to display the buffer."
943 (interactive (list (cvs-query-directory "CVS Status (directory): ")
944 (cvs-flags-query 'cvs-status-flags "cvs status flags")))
945 (when (eq flags t)
946 (setf flags (cvs-flags-query 'cvs-status-flags nil 'noquery)))
947 (cvs-cmd-do "status" directory flags nil
948 (> (prefix-numeric-value current-prefix-arg) 8)
949 :noshow noshow :dont-change-disc t))
950
5b467bf4
SM
951(defun cvs-update-filter (proc string)
952 "Filter function for pcl-cvs.
953This function gets the output that CVS sends to stdout. It inserts
954the STRING into (process-buffer PROC) but it also checks if CVS is waiting
955for a lock file. If so, it inserts a message cookie in the *cvs* buffer."
956 (save-match-data
957 (with-current-buffer (process-buffer proc)
958 (let ((inhibit-read-only t))
959 (save-excursion
960 ;; Insert the text, moving the process-marker.
961 (goto-char (process-mark proc))
962 (insert string)
963 (set-marker (process-mark proc) (point))
964 ;; FIXME: Delete any old lock message
965 ;;(if (tin-nth cookies 1)
966 ;; (tin-delete cookies
967 ;; (tin-nth cookies 1)))
968 ;; Check if CVS is waiting for a lock.
969 (beginning-of-line 0) ;Move to beginning of last complete line.
970 (when (looking-at "^[ a-z]+: \\(.*waiting for .*lock in \\(.*\\)\\)$")
971 (let ((msg (match-string 1))
972 (lock (match-string 2)))
973 (with-current-buffer cvs-buffer
974 (set (make-local-variable 'cvs-lock-file) lock)
975 ;; display the lock situation in the *cvs* buffer:
976 (ewoc-enter-last
977 cvs-cookies
978 (cvs-create-fileinfo
979 'MESSAGE "" " "
980 (concat msg
027b73ac 981 (substitute-command-keys
5b467bf4
SM
982 "\n\t(type \\[cvs-mode-delete-lock] to delete it)"))
983 :subtype 'TEMP))
984 (pop-to-buffer (current-buffer))
985 (goto-char (point-max))
986 (beep)))))))))
987
988\f
989;;;;
990;;;; The cvs-mode and its associated commands.
991;;;;
992
993(cvs-prefix-define cvs-force-command "" "" '("/F") cvs-qtypedesc-string1)
994(defun-cvs-mode cvs-mode-force-command (arg)
995 "Force the next cvs command to operate on all the selected files.
996By default, cvs commands only operate on files on which the command
997\"makes sense\". This overrides the safety feature on the next cvs command.
998It actually behaves as a toggle. If prefixed by \\[universal-argument] \\[universal-argument],
999the override will persist until the next toggle."
1000 (interactive "P")
1001 (cvs-prefix-set 'cvs-force-command arg))
1002
5b467bf4 1003(put 'cvs-mode 'mode-class 'special)
cb3430a1 1004(define-derived-mode cvs-mode fundamental-mode "CVS"
5b467bf4 1005 "Mode used for PCL-CVS, a frontend to CVS.
cb3430a1 1006Full documentation is in the Texinfo file."
5b467bf4
SM
1007 (setq mode-line-process
1008 '("" cvs-force-command cvs-ignore-marks-modif
1009 ":" (cvs-branch-prefix
1010 ("" cvs-branch-prefix (cvs-secondary-branch-prefix
1011 ("->" cvs-secondary-branch-prefix))))
1012 " " cvs-mode-line-process))
1013 (buffer-disable-undo (current-buffer))
1014 ;;(set (make-local-variable 'goal-column) cvs-cursor-column)
1015 (set (make-local-variable 'revert-buffer-function) 'cvs-mode-revert-buffer)
cb3430a1 1016 (setq truncate-lines t)
5b467bf4
SM
1017 (cvs-prefix-make-local 'cvs-branch-prefix)
1018 (cvs-prefix-make-local 'cvs-secondary-branch-prefix)
1019 (cvs-prefix-make-local 'cvs-force-command)
1020 (cvs-prefix-make-local 'cvs-ignore-marks-modif)
1021 (make-local-variable 'cvs-mode-line-process)
1022 (make-local-variable 'cvs-temp-buffers))
1023
1024
1025(defun cvs-buffer-p (&optional buffer)
1026 "Return whether the (by default current) BUFFER is a `cvs-mode' buffer."
1027 (save-excursion
1028 (if buffer (set-buffer buffer))
1029 (and (eq major-mode 'cvs-mode))))
1030
1031(defun cvs-buffer-check ()
1032 "Check that the current buffer follows cvs-buffer's conventions."
1033 (let ((buf (current-buffer))
1034 (check 'none))
1035 (or (and (setq check 'collection)
1036 (eq (ewoc-buffer cvs-cookies) buf)
1037 (setq check 'cvs-temp-buffer)
1038 (or (null cvs-temp-buffer)
1039 (null (buffer-name cvs-temp-buffer))
1040 (and (eq (with-current-buffer cvs-temp-buffer cvs-buffer) buf)
1041 (equal (with-current-buffer cvs-temp-buffer
1042 default-directory)
1043 default-directory)))
1044 t)
1045 (error "Inconsistent %s in buffer %s" check (buffer-name buf)))))
1046
1047
1048(defun-cvs-mode cvs-mode-quit ()
1049 "Quit PCL-CVS, killing the *cvs* buffer."
1050 (interactive)
1051 (and (y-or-n-p "Quit pcl-cvs? ") (kill-buffer (current-buffer))))
1052
1053;; Give help....
1054
1055(defun cvs-help ()
1056 "Display help for various PCL-CVS commands."
1057 (interactive)
1058 (if (eq last-command 'cvs-help)
1059 (describe-function 'cvs-mode) ; would need to use minor-mode for cvs-edit-mode
1060 (message
1061 (substitute-command-keys
1062 "`\\[cvs-help]':help `\\[cvs-mode-add]':add `\\[cvs-mode-commit]':commit \
1063`\\[cvs-mode-diff-map]':diff* `\\[cvs-mode-log]':log \
1064`\\[cvs-mode-remove]':remove `\\[cvs-mode-status]':status \
1065`\\[cvs-mode-undo]':undo"))))
1066
1067(defun cvs-mode-diff-help ()
1068 "Display help for various PCL-CVS diff commands."
1069 (interactive)
1070 (if (eq last-command 'cvs-mode-diff-help)
1071 (describe-function 'cvs-mode) ; no better docs for diff stuff?
1072 (message
1073 (substitute-command-keys
1074 "`\\[cvs-mode-diff]':diff `\\[cvs-mode-idiff]':idiff \
1075`\\[cvs-mode-diff-head]':head `\\[cvs-mode-diff-vendor]':vendor \
1076`\\[cvs-mode-diff-backup]':backup `\\[cvs-mode-idiff-other]':other \
1077`\\[cvs-mode-imerge]':imerge"))))
1078
1079;; Move around in the buffer
1080
1081(defun-cvs-mode cvs-mode-previous-line (arg)
1082 "Go to the previous line.
1083If a prefix argument is given, move by that many lines."
1084 (interactive "p")
44946a4c 1085 (ewoc-goto-prev cvs-cookies arg))
5b467bf4
SM
1086
1087(defun-cvs-mode cvs-mode-next-line (arg)
1088 "Go to the next line.
1089If a prefix argument is given, move by that many lines."
1090 (interactive "p")
44946a4c 1091 (ewoc-goto-next cvs-cookies arg))
5b467bf4 1092
027b73ac 1093;;;;
5b467bf4 1094;;;; Mark handling
027b73ac 1095;;;;
5b467bf4
SM
1096
1097(defun-cvs-mode cvs-mode-mark (&optional arg)
1098 "Mark the fileinfo on the current line.
1099If the fileinfo is a directory, all the contents of that directory are
1100marked instead. A directory can never be marked."
1101 (interactive)
44946a4c 1102 (let* ((tin (ewoc-locate cvs-cookies))
5b467bf4
SM
1103 (fi (ewoc-data tin)))
1104 (if (eq (cvs-fileinfo->type fi) 'DIRCHANGE)
1105 ;; it's a directory: let's mark all files inside
1106 (ewoc-map
1107 (lambda (f dir)
1108 (when (cvs-dir-member-p f dir)
1109 (setf (cvs-fileinfo->marked f)
1110 (not (if (eq arg 'toggle) (cvs-fileinfo->marked f) arg)))
1111 t)) ;Tell cookie to redisplay this cookie.
1112 cvs-cookies
1113 (cvs-fileinfo->dir fi))
1114 ;; not a directory: just do the obvious
1115 (setf (cvs-fileinfo->marked fi)
1116 (not (if (eq arg 'toggle) (cvs-fileinfo->marked fi) arg)))
1117 (ewoc-invalidate cvs-cookies tin)
1118 (cvs-mode-next-line 1))))
1119
1120(defun cvs-mouse-toggle-mark (e)
1121 "Toggle the mark of the entry under the mouse."
1122 (interactive "e")
44946a4c
SM
1123 (save-excursion
1124 (mouse-set-point e)
1125 (cvs-mode-mark 'toggle)))
5b467bf4
SM
1126
1127(defun-cvs-mode cvs-mode-unmark ()
1128 "Unmark the fileinfo on the current line."
1129 (interactive)
1130 (cvs-mode-mark t))
1131
1132(defun-cvs-mode cvs-mode-mark-all-files ()
1133 "Mark all files."
1134 (interactive)
1135 (ewoc-map (lambda (cookie)
1136 (unless (eq (cvs-fileinfo->type cookie) 'DIRCHANGE)
1137 (setf (cvs-fileinfo->marked cookie) t)))
1138 cvs-cookies))
1139
1140(defun-cvs-mode cvs-mode-mark-matching-files (regex)
1141 "Mark all files matching REGEX."
1142 (interactive "sMark files matching: ")
1143 (ewoc-map (lambda (cookie)
1144 (when (and (not (eq (cvs-fileinfo->type cookie) 'DIRCHANGE))
1145 (string-match regex (cvs-fileinfo->file cookie)))
1146 (setf (cvs-fileinfo->marked cookie) t)))
1147 cvs-cookies))
1148
1149(defun-cvs-mode cvs-mode-unmark-all-files ()
1150 "Unmark all files.
1151Directories are also unmarked, but that doesn't matter, since
1152they should always be unmarked."
1153 (interactive)
1154 (ewoc-map (lambda (cookie)
1155 (setf (cvs-fileinfo->marked cookie) nil)
1156 t)
1157 cvs-cookies))
1158
1159(defun-cvs-mode cvs-mode-unmark-up ()
1160 "Unmark the file on the previous line."
1161 (interactive)
44946a4c 1162 (let ((tin (ewoc-goto-prev cvs-cookies 1)))
5b467bf4
SM
1163 (when tin
1164 (setf (cvs-fileinfo->marked (ewoc-data tin)) nil)
1165 (ewoc-invalidate cvs-cookies tin))))
1166
1167(defconst cvs-ignore-marks-alternatives
1168 '(("toggle-marks" . "/TM")
1169 ("force-marks" . "/FM")
1170 ("ignore-marks" . "/IM")))
1171
1172(cvs-prefix-define cvs-ignore-marks-modif
1173 "Prefix to decide whether to ignore marks or not."
1174 "active"
1175 (mapcar 'cdr cvs-ignore-marks-alternatives)
1176 (cvs-qtypedesc-create
1177 (lambda (str) (cdr (assoc str cvs-ignore-marks-alternatives)))
1178 (lambda (obj) (caar (member* obj cvs-ignore-marks-alternatives :key 'cdr)))
1179 (lambda () cvs-ignore-marks-alternatives)
1180 nil t))
027b73ac 1181
5b467bf4
SM
1182(defun-cvs-mode cvs-mode-toggle-marks (arg)
1183 "Toggle whether the next CVS command uses marks.
1184See `cvs-prefix-set' for further description of the behavior.
1185\\[universal-argument] 1 selects `force-marks',
1186\\[universal-argument] 2 selects `ignore-marks',
1187\\[universal-argument] 3 selects `toggle-marks'."
1188 (interactive "P")
1189 (cvs-prefix-set 'cvs-ignore-marks-modif arg))
027b73ac 1190
5b467bf4
SM
1191(defun cvs-ignore-marks-p (cmd &optional read-only)
1192 (let ((default (if (member cmd cvs-invert-ignore-marks)
1193 (not cvs-default-ignore-marks)
1194 cvs-default-ignore-marks))
1195 (modif (cvs-prefix-get 'cvs-ignore-marks-modif read-only)))
1196 (cond
1197 ((equal modif "/IM") t)
1198 ((equal modif "/TM") (not default))
1199 ((equal modif "/FM") nil)
1200 (t default))))
1201
1202(defun cvs-mode-mark-get-modif (cmd)
1203 (if (cvs-ignore-marks-p cmd 'read-only) "/IM" "/FM"))
1204
1205(defvar cvs-minor-current-files)
1206(defun cvs-get-marked (&optional ignore-marks ignore-contents)
1207 "Return a list of all selected fileinfos.
1208If there are any marked tins, and IGNORE-MARKS is nil, return them.
1209Otherwise, if the cursor selects a directory, and IGNORE-CONTENTS is
1210nil, return all files in it, else return just the directory.
1211Otherwise return (a list containing) the file the cursor points to, or
1212an empty list if it doesn't point to a file at all.
1213
1214Args: &optional IGNORE-MARKS IGNORE-CONTENTS."
1215
1216 (let ((fis nil))
96190aa1
SM
1217 (dolist (fi (if (and (boundp 'cvs-minor-current-files)
1218 (consp cvs-minor-current-files))
5b467bf4
SM
1219 (mapcar
1220 (lambda (f)
96190aa1
SM
1221 (if (cvs-fileinfo-p f) f
1222 (let ((f (file-relative-name f)))
1223 (if (file-directory-p f)
1224 (cvs-create-fileinfo
1225 'DIRCHANGE (file-name-as-directory f) "." "")
1226 (let ((dir (file-name-directory f))
1227 (file (file-name-nondirectory f)))
1228 (cvs-create-fileinfo
1229 'UNKNOWN (or dir "") file ""))))))
5b467bf4
SM
1230 cvs-minor-current-files)
1231 (or (and (not ignore-marks)
1232 (ewoc-collect cvs-cookies
1233 'cvs-fileinfo->marked))
44946a4c 1234 (list (ewoc-data (ewoc-locate cvs-cookies))))))
027b73ac 1235
5b467bf4
SM
1236 (if (or ignore-contents (not (eq (cvs-fileinfo->type fi) 'DIRCHANGE)))
1237 (push fi fis)
1238 ;; If a directory is selected, return members, if any.
1239 (setq fis
1240 (append (ewoc-collect cvs-cookies
1241 'cvs-dir-member-p
1242 (cvs-fileinfo->dir fi))
1243 fis))))
1244 (nreverse fis)))
1245
1246(defun* cvs-mode-marked (filter &optional (cmd (symbol-name filter))
1247 &key read-only one file)
1248 "Get the list of marked FIS.
1249CMD is used to determine whether to use the marks or not.
1250Only files for which FILTER is applicable are returned.
1251If READ-ONLY is non-nil, the current toggling is left intact.
1252If ONE is non-nil, marks are ignored and a single FI is returned.
1253If FILE is non-nil, directory entries won't be selected."
1254 (let* ((fis (cvs-get-marked (or one (cvs-ignore-marks-p cmd read-only))
1255 (and (not file)
1256 (cvs-applicable-p 'DIRCHANGE filter))))
1257 (force (cvs-prefix-get 'cvs-force-command))
1258 (fis (car (cvs-partition
1259 (lambda (fi) (cvs-applicable-p fi (and (not force) filter)))
1260 fis))))
1261 (cond
1262 ((null fis)
1263 (error "`%s' is not applicable to any of the selected files." filter))
1264 ((and one (cdr fis))
1265 (error "`%s' is only applicable to a single file." cmd))
1266 (one (car fis))
1267 (t fis))))
1268
1269(defun cvs-enabledp (filter)
1270 "Determine whether FILTER applies to at least one of the selected files."
1271 (ignore-errors (cvs-mode-marked filter nil :read-only t)))
1272
1273(defun cvs-mode-files (&rest -cvs-mode-files-args)
1274 (cvs-mode!
1275 (lambda ()
1276 (mapcar 'cvs-fileinfo->full-path
1277 (apply 'cvs-mode-marked -cvs-mode-files-args)))))
1278
1279;;;
1280;;; Interface between CVS-Edit and PCL-CVS
1281;;;
1282
1283(defun cvs-mode-commit-setup ()
1284 "Run `cvs-mode-commit' with setup."
1285 (interactive)
1286 (cvs-mode-commit 'force))
1287
1288(defun cvs-mode-commit (setup)
1289 "Check in all marked files, or the current file.
1290The user will be asked for a log message in a buffer.
1291The buffer's mode and name is determined by the \"message\" setting
1292 of `cvs-buffer-name-alist'.
1293The POSTPROC specified there (typically `cvs-edit') is then called,
1294 passing it the SETUP argument."
1295 (interactive "P")
1296 ;; It seems that the save-excursion that happens if I use the better
1297 ;; form of `(cvs-mode! (lambda ...))' screws up a couple things which
1298 ;; end up being rather annoying (like cvs-edit-mode's message being
1299 ;; displayed in the wrong minibuffer).
1300 (cvs-mode!)
1301 (pop-to-buffer (cvs-temp-buffer "message" 'normal 'nosetup))
1302 (set (make-local-variable 'cvs-minor-wrap-function) 'cvs-commit-minor-wrap)
1303 (let ((lbd list-buffers-directory)
dedffa6a 1304 (setupfun (or (nth 2 (cdr (assoc "message" cvs-buffer-name-alist)))
5b467bf4
SM
1305 'cvs-edit)))
1306 (funcall setupfun 'cvs-do-commit setup 'cvs-commit-filelist)
1307 (set (make-local-variable 'list-buffers-directory) lbd)))
1308
1309(defun cvs-commit-minor-wrap (buf f)
1310 (let ((cvs-ignore-marks-modif (cvs-mode-mark-get-modif "commit")))
1311 (funcall f)))
1312
1313(defun cvs-commit-filelist () (cvs-mode-files 'commit nil :read-only t :file t))
1314
1315(defun cvs-do-commit (flags)
1316 "Do the actual commit, using the current buffer as the log message."
1317 (interactive (list (cvs-flags-query 'cvs-commit-flags "cvs commit flags")))
027b73ac 1318 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
5b467bf4
SM
1319 (cvs-mode!)
1320 ;;(pop-to-buffer cvs-buffer)
1321 (cvs-mode-do "commit" (list* "-m" msg flags) 'commit)))
1322
1323
027b73ac 1324;;;;
5b467bf4 1325;;;; CVS Mode commands
027b73ac 1326;;;;
5b467bf4
SM
1327
1328(defun-cvs-mode (cvs-mode-insert . NOARGS) (file)
1329 "Insert an entry for a specific file."
1330 (interactive
1331 (list (read-file-name "File to insert: " nil nil nil
96190aa1
SM
1332 ;; Can't use ignore-errors here because interactive
1333 ;; specs aren't byte-compiled.
1334 (condition-case nil
1335 (cvs-fileinfo->dir
1336 (car (cvs-mode-marked nil nil :read-only t)))
1337 (error nil)))))
6dc7d3d5
SM
1338 (let ((file (file-relative-name (directory-file-name file))) last)
1339 (dolist (fi (cvs-fileinfo-from-entries file))
3c7fafc7
SM
1340 (setq last (cvs-addto-collection cvs-cookies fi last)))
1341 ;; There should have been at least one entry.
1342 (goto-char (ewoc-location last))))
5b467bf4
SM
1343
1344(defun-cvs-mode (cvs-mode-add . SIMPLE) (flags)
1345 "Add marked files to the cvs repository.
1346With prefix argument, prompt for cvs flags."
1347 (interactive (list (cvs-flags-query 'cvs-add-flags "cvs add flags")))
1348 (let ((fis (cvs-mode-marked 'add))
1349 (needdesc nil) (dirs nil))
1350 ;; find directories and look for fis needing a description
1351 (dolist (fi fis)
1352 (cond
1353 ((file-directory-p (cvs-fileinfo->full-path fi)) (push fi dirs))
1354 ((eq (cvs-fileinfo->type fi) 'UNKNOWN) (setq needdesc t))))
1355 ;; prompt for description if necessary
1356 (let* ((msg (if (and needdesc
1357 (or current-prefix-arg (not cvs-add-default-message)))
1358 (read-from-minibuffer "Enter description: ")
1359 (or cvs-add-default-message "")))
1360 (flags (list* "-m" msg flags))
1361 (postproc
1362 ;; setup postprocessing for the directory entries
1363 (when dirs
1364 `((cvs-run-process (list "-n" "update")
1365 ',dirs
1366 '(cvs-parse-process t))
1367 (dolist (fi ',dirs) (setf (cvs-fileinfo->type fi) 'DEAD))))))
1368 (cvs-mode-run "add" flags fis :postproc postproc))))
1369
5b467bf4
SM
1370(defun-cvs-mode (cvs-mode-diff . DOUBLE) (flags)
1371 "Diff the selected files against the repository.
1372This command compares the files in your working area against the
1373revision which they are based upon."
1374 (interactive
1375 (list (cvs-add-branch-prefix
1376 (cvs-add-secondary-branch-prefix
1377 (cvs-flags-query 'cvs-diff-flags "cvs diff flags")))))
1378 (cvs-mode-do "diff" flags 'diff
1379 :show t)) ;; :ignore-exit t
1380
5b467bf4
SM
1381(defun-cvs-mode (cvs-mode-diff-head . SIMPLE) (flags)
1382 "Diff the selected files against the head of the current branch.
1383See ``cvs-mode-diff'' for more info."
1384 (interactive (list (cvs-flags-query 'cvs-diff-flags "cvs diff flags")))
1385 (cvs-mode-diff-1 (cons "-rHEAD" flags)))
1386
5b467bf4
SM
1387(defun-cvs-mode (cvs-mode-diff-vendor . SIMPLE) (flags)
1388 "Diff the selected files against the head of the vendor branch.
1389See ``cvs-mode-diff'' for more info."
1390 (interactive (list (cvs-flags-query 'cvs-diff-flags "cvs diff flags")))
1391 (cvs-mode-diff-1 (cons (concat "-r" cvs-vendor-branch) flags)))
1392
5b467bf4
SM
1393;; sadly, this is not provided by cvs, so we have to roll our own
1394(defun-cvs-mode (cvs-mode-diff-backup . SIMPLE) (flags)
1395 "Diff the files against the backup file.
1396This command can be used on files that are marked with \"Merged\"
1397or \"Conflict\" in the *cvs* buffer."
1398 (interactive (list (cvs-flags-query 'cvs-diff-flags "diff flags")))
1399 (unless (listp flags) (error "flags should be a list of strings."))
1400 (save-some-buffers)
1401 (let* ((filter 'diff)
1402 (marked (cvs-get-marked (cvs-ignore-marks-p "diff")))
1403 ;;(tins (cvs-filter-applicable filter marked))
1404 (fis (delete-if-not 'cvs-fileinfo->backup-file marked)))
1405 (unless (consp fis)
1406 (error "No files with a backup file selected!"))
1407 ;; let's extract some info into the environment for `buffer-name'
1408 (let* ((dir (cvs-fileinfo->dir (car fis)))
1409 (file (cvs-fileinfo->file (car fis))))
1410 (set-buffer (cvs-temp-buffer "diff")))
1411 (message "cvs diff backup...")
1412 (cvs-execute-single-file-list fis 'cvs-diff-backup-extractor
1413 cvs-diff-program flags))
1414 (message "cvs diff backup... Done."))
1415
5b467bf4
SM
1416(defun cvs-diff-backup-extractor (fileinfo)
1417 "Return the filename and the name of the backup file as a list.
1418Signal an error if there is no backup file."
1419 (let ((backup-file (cvs-fileinfo->backup-file fileinfo)))
1420 (unless backup-file
1421 (error "%s has no backup file." (cvs-fileinfo->full-path fileinfo)))
1422 (list backup-file (cvs-fileinfo->file fileinfo))))
1423
1424;;
1425;; Emerge support
1426;;
1427(defun cvs-emerge-diff (b1 b2) (emerge-buffers b1 b2 b1))
1428(defun cvs-emerge-merge (b1 b2 base out)
1429 (emerge-buffers-with-ancestor b1 b2 base (find-file-noselect out)))
1430
1431;;
1432;; Ediff support
027b73ac 1433;;
5b467bf4
SM
1434
1435(defvar ediff-after-quit-destination-buffer)
1436(defvar cvs-transient-buffers)
1437(defun cvs-ediff-startup-hook ()
1438 (add-hook 'ediff-after-quit-hook-internal
1439 `(lambda ()
1440 (cvs-ediff-exit-hook
1441 ',ediff-after-quit-destination-buffer ',cvs-transient-buffers))
1442 nil 'local))
1443
1444(defun cvs-ediff-exit-hook (cvs-buf tmp-bufs)
1445 ;; kill the temp buffers (and their associated windows)
1446 (dolist (tb tmp-bufs)
1447 (when (and tb (buffer-live-p tb) (not (buffer-modified-p tb)))
1448 (let ((win (get-buffer-window tb t)))
a7996e05 1449 (kill-buffer tb)
eed914af 1450 (when (window-live-p win) (ignore-errors (delete-window win))))))
5b467bf4
SM
1451 ;; switch back to the *cvs* buffer
1452 (when (and cvs-buf (buffer-live-p cvs-buf)
1453 (not (get-buffer-window cvs-buf t)))
1454 (ignore-errors (switch-to-buffer cvs-buf))))
1455
1456(defun cvs-ediff-diff (b1 b2)
1457 (let ((ediff-after-quit-destination-buffer (current-buffer))
1458 (startup-hook '(cvs-ediff-startup-hook)))
a7996e05 1459 (ediff-buffers b1 b2 startup-hook 'ediff-revision)))
5b467bf4
SM
1460
1461(defun cvs-ediff-merge (b1 b2 base out)
1462 (let ((ediff-after-quit-destination-buffer (current-buffer))
1463 (startup-hook '(cvs-ediff-startup-hook)))
1464 (ediff-merge-buffers-with-ancestor
1465 b1 b2 base startup-hook
1466 'ediff-merge-revisions-with-ancestor
1467 out)))
1468
1469;;
1470;; Interactive merge/diff support.
1471;;
1472
1473(defun cvs-retrieve-revision (fileinfo rev)
1474 "Retrieve the given REVision of the file in FILEINFO into a new buffer."
eed914af
SM
1475 (let* ((file (cvs-fileinfo->full-path fileinfo))
1476 (buffile (concat file "." rev)))
1477 (or (find-buffer-visiting buffile)
1478 (with-current-buffer (create-file-buffer buffile)
1479 (message "Retrieving revision %s..." rev)
1480 (let ((res (call-process cvs-program nil t nil
1481 "-q" "update" "-p" "-r" rev file)))
1482 (when (and res (not (and (equal 0 res))))
1483 (error "Something went wrong retrieving revision %s: %s" rev res))
1484 (set-buffer-modified-p nil)
1485 (let ((buffer-file-name (expand-file-name file)))
1486 (after-find-file))
1487 (toggle-read-only 1)
1488 (message "Retrieving revision %s... Done" rev)
1489 (current-buffer))))))
5b467bf4 1490
6dc7d3d5 1491(eval-and-compile (autoload 'smerge-ediff "smerge-mode"))
5b467bf4 1492
6dc7d3d5
SM
1493;; FIXME: The user should be able to specify ancestor/head/backup and we should
1494;; provide sensible defaults when merge info is unavailable (rather than rely
1495;; on smerge-ediff). Also provide sane defaults for need-merge files.
5b467bf4
SM
1496(defun-cvs-mode cvs-mode-imerge ()
1497 "Merge interactively appropriate revisions of the selected file."
1498 (interactive)
1499 (let ((fi (cvs-mode-marked 'merge nil :one t :file t)))
1500 (let ((merge (cvs-fileinfo->merge fi))
1501 (file (cvs-fileinfo->full-path fi))
1502 (backup-file (cvs-fileinfo->backup-file fi)))
1503 (if (not (and merge backup-file))
1504 (let ((buf (find-file-noselect file)))
1505 (message "Missing merge info or backup file, using VC.")
6dc7d3d5
SM
1506 (with-current-buffer buf
1507 (smerge-ediff)))
5b467bf4
SM
1508 (let* ((ancestor-buf (cvs-retrieve-revision fi (car merge)))
1509 (head-buf (cvs-retrieve-revision fi (cdr merge)))
1510 (backup-buf (let ((auto-mode-alist nil))
1511 (find-file-noselect backup-file)))
1512 ;; this binding is used by cvs-ediff-startup-hook
1513 (cvs-transient-buffers (list ancestor-buf backup-buf head-buf)))
1514 (with-current-buffer backup-buf
1515 (let ((buffer-file-name (expand-file-name file)))
1516 (after-find-file)))
1517 (funcall (cdr cvs-idiff-imerge-handlers)
1518 backup-buf head-buf ancestor-buf file))))))
1519
1520(cvs-flags-define cvs-idiff-version
1521 (list "BASE" cvs-vendor-branch cvs-vendor-branch "BASE" "BASE")
1522 "version: " cvs-qtypedesc-tag)
1523
1524(defun-cvs-mode (cvs-mode-idiff . NOARGS) (&optional rev1 rev2)
1525 "Diff interactively current file to revisions."
1526 (interactive
1527 (let* ((rev1 (cvs-prefix-get 'cvs-branch-prefix))
1528 (rev2 (and rev1 (cvs-prefix-get 'cvs-secondary-branch-prefix))))
1529 (list (or rev1 (cvs-flags-query 'cvs-idiff-version))
1530 rev2)))
1531 (let ((fi (cvs-mode-marked 'diff "idiff" :one t :file t)))
1532 (let* ((file (cvs-fileinfo->full-path fi))
1533 (rev1-buf (cvs-retrieve-revision fi (or rev1 "BASE")))
1534 (rev2-buf (if rev2 (cvs-retrieve-revision fi rev2)))
1535 ;; this binding is used by cvs-ediff-startup-hook
1536 (cvs-transient-buffers (list rev1-buf rev2-buf)))
1537 (funcall (car cvs-idiff-imerge-handlers)
1538 rev1-buf (or rev2-buf (find-file-noselect file))))))
1539
1540(defun-cvs-mode (cvs-mode-idiff-other . NOARGS) ()
1541 "Diff interactively current file to revisions."
1542 (interactive)
1543 (let* ((rev1 (cvs-prefix-get 'cvs-branch-prefix))
1544 (rev2 (and rev1 (cvs-prefix-get 'cvs-secondary-branch-prefix)))
1545 (fis (cvs-mode-marked 'diff "idiff" :file t)))
1546 (when (> (length fis) 2)
1547 (error "idiff-other cannot be applied to more than 2 files at a time."))
44946a4c 1548 (let* ((fi1 (car fis))
5b467bf4
SM
1549 (rev1-buf (if rev1 (cvs-retrieve-revision fi1 rev1)
1550 (find-file-noselect (cvs-fileinfo->full-path fi1))))
1551 rev2-buf)
1552 (if (cdr fis)
dedffa6a 1553 (let ((fi2 (nth 1 fis)))
5b467bf4
SM
1554 (setq rev2-buf
1555 (if rev2 (cvs-retrieve-revision fi2 rev2)
1556 (find-file-noselect (cvs-fileinfo->full-path fi2)))))
1557 (error "idiff-other doesn't know what other file/buffer to use."))
1558 (let* (;; this binding is used by cvs-ediff-startup-hook
1559 (cvs-transient-buffers (list rev1-buf rev2-buf)))
1560 (funcall (car cvs-idiff-imerge-handlers)
1561 rev1-buf rev2-buf)))))
1562
1563
1564(defun cvs-fileinfo-kill (c fi)
1565 "Mark a fileinfo xor its members (in case of a directory) as dead."
1566 (if (eq (cvs-fileinfo->type fi) 'DIRCHANGE)
1567 (dolist (fi (ewoc-collect c 'cvs-dir-member-p
1568 (cvs-fileinfo->dir fi)))
1569 (setf (cvs-fileinfo->type fi) 'DEAD))
1570 (setf (cvs-fileinfo->type fi) 'DEAD)))
1571
cb3430a1
SM
1572(defun cvs-is-within-p (fis dir)
1573 "Non-nil is buffer is inside one of FIS (in DIR)."
1574 (when (stringp buffer-file-name)
1575 (setq buffer-file-name (expand-file-name buffer-file-name))
1576 (let (ret)
1577 (dolist (fi (or fis (list (cvs-create-fileinfo 'DIRCHANGE "" "." ""))))
1578 (when (cvs-string-prefix-p
1579 (expand-file-name (cvs-fileinfo->full-path fi) dir)
1580 buffer-file-name)
1581 (setq ret t)))
1582 ret)))
1583
5b467bf4
SM
1584(defun* cvs-mode-run (cmd flags fis
1585 &key (buf (cvs-temp-buffer))
1586 dont-change-disc cvsargs postproc)
1587 "Generic cvs-mode-<foo> function.
1588Executes `cvs CVSARGS CMD FLAGS FIS'.
1589BUF is the buffer to be used for cvs' output.
1590DONT-CHANGE-DISC non-nil indicates that the command will not change the
1591 contents of files. This is only used by the parser.
1592POSTPROC is a list of expressions to be evaluated at the very end (after
1593 parsing if applicable). It will be prepended with `progn' is necessary."
cb3430a1
SM
1594 (let ((def-dir default-directory))
1595 ;; Save the relevant buffers
1596 (save-some-buffers nil (lambda () (cvs-is-within-p fis def-dir))))
5b467bf4
SM
1597 (unless (listp flags) (error "flags should be a list of strings"))
1598 (let* ((cvs-buf (current-buffer))
1599 (single-dir (or (not (listp cvs-execute-single-dir))
1600 (member cmd cvs-execute-single-dir)))
1601 (parse (member cmd cvs-parse-known-commands))
1602 (args (append cvsargs (list cmd) flags))
dedffa6a 1603 (after-mode (nth 2 (cdr (assoc cmd cvs-buffer-name-alist)))))
5b467bf4
SM
1604 (cvs-cleanup-collection cvs-cookies ;cleanup remaining messages
1605 (eq cvs-auto-remove-handled 'delayed) nil t)
1606 (when (fboundp after-mode)
1607 (setq postproc (append postproc `((,after-mode)))))
1608 (when parse (push `(cvs-parse-process ',dont-change-disc) postproc))
1609 (when (member cmd '("status" "update")) ;FIXME: Yuck!!
1610 ;; absence of `cvs update' output has a specific meaning.
1611 (push
1612 `(dolist (fi ',(or fis
1613 (list (cvs-create-fileinfo 'DIRCHANGE "" "." ""))))
1614 (cvs-fileinfo-kill ',cvs-cookies fi))
1615 postproc))
1616 (setq postproc (if (cdr postproc) (cons 'progn postproc) (car postproc)))
1617 (cvs-update-header args fis)
1618 (with-current-buffer buf
1619 ;;(set (make-local-variable 'cvs-buffer) cvs-buf)
1620 (let ((inhibit-read-only t)) (erase-buffer))
1621 (message "Running cvs %s ..." cmd)
1622 (cvs-run-process args fis postproc single-dir))))
1623
1624
1625(defun* cvs-mode-do (cmd flags filter
1626 &key show dont-change-disc parse cvsargs postproc)
1627 "Generic cvs-mode-<foo> function.
1628Executes `cvs CVSARGS CMD FLAGS' on the selected files.
1629FILTER is passed to `cvs-applicable-p' to only apply the command to
1630 files for which it makes sense.
1631SHOW indicates that CMD should be not be run in the default temp buffer and
1632 should be shown to the user. The buffer and mode to be used is determined
1633 by `cvs-buffer-name-alist'.
1634DONT-CHANGE-DISC non-nil indicates that the command will not change the
1635 contents of files. This is only used by the parser."
1636 (cvs-mode-run cmd flags (cvs-mode-marked filter cmd)
1637 :buf (cvs-temp-buffer (when show cmd))
1638 :dont-change-disc dont-change-disc
1639 :cvsargs cvsargs
1640 :postproc postproc))
1641
1642(defun-cvs-mode (cvs-mode-status . SIMPLE) (flags)
1643 "Show cvs status for all marked files.
1644With prefix argument, prompt for cvs flags."
1645 (interactive (list (cvs-flags-query 'cvs-status-flags "cvs status flags")))
1646 (cvs-mode-do "status" flags nil :dont-change-disc t :show t
1647 :postproc (when (eq cvs-auto-remove-handled 'status)
1648 '((with-current-buffer ,(current-buffer)
1649 (cvs-mode-remove-handled))))))
1650
1651(defun-cvs-mode (cvs-mode-tree . SIMPLE) (flags)
1652 "Call cvstree using the file under the point as a keyfile."
1653 (interactive (list (cvs-flags-query 'cvs-status-flags "cvs status flags")))
1654 (cvs-mode-run "status" (cons "-v" flags) (cvs-mode-marked nil "status")
1655 :buf (cvs-temp-buffer "tree")
1656 :dont-change-disc t
1657 :postproc '((cvs-status-trees))))
1658
1659;; cvs log
1660
1661(defun-cvs-mode (cvs-mode-log . NOARGS) (flags)
1662 "Display the cvs log of all selected files.
1663With prefix argument, prompt for cvs flags."
1664 (interactive (list (cvs-add-branch-prefix
1665 (cvs-flags-query 'cvs-log-flags "cvs log flags"))))
1666 (cvs-mode-do "log" flags nil :show t))
1667
1668
1669(defun-cvs-mode (cvs-mode-update . NOARGS) (flags)
1670 "Update all marked files.
1671With a prefix argument, prompt for cvs flags."
1672 (interactive
1673 (list (cvs-add-branch-prefix
1674 (cvs-add-secondary-branch-prefix
1675 (cvs-flags-query 'cvs-update-flags "cvs update flags")
1676 "-j") "-j")))
1677 (cvs-mode-do "update" flags 'update))
1678
1679
1680(defun-cvs-mode (cvs-mode-examine . NOARGS) (flags)
1681 "Re-examine all marked files.
1682With a prefix argument, prompt for cvs flags."
1683 (interactive
1684 (list (cvs-add-branch-prefix
1685 (cvs-add-secondary-branch-prefix
1686 (cvs-flags-query 'cvs-update-flags "cvs -n update flags")
1687 "-j") "-j")))
1688 (cvs-mode-do "update" flags nil :cvsargs '("-n") :dont-change-disc t))
1689
1690
1691(defun-cvs-mode cvs-mode-ignore (&optional pattern)
1692 "Arrange so that CVS ignores the selected files.
1693This command ignores files that are not flagged as `Unknown'."
1694 (interactive)
1695 (dolist (fi (cvs-mode-marked 'ignore))
1696 (cvs-append-to-ignore (cvs-fileinfo->dir fi) (cvs-fileinfo->file fi))
1697 (setf (cvs-fileinfo->type fi) 'DEAD))
1698 (cvs-cleanup-collection cvs-cookies nil nil nil))
1699
1700
1701(defun cvs-append-to-ignore (dir str)
1702 "Add STR to the .cvsignore file in DIR."
1703 (save-window-excursion
1704 (set-buffer (find-file-noselect (expand-file-name ".cvsignore" dir)))
1705 (when (ignore-errors
1706 (and buffer-read-only
1707 (eq 'CVS (vc-backend buffer-file-name))
d15c2aaa 1708 (not (vc-editable-p buffer-file-name))))
5b467bf4
SM
1709 ;; CVSREAD=on special case
1710 (vc-toggle-read-only))
1711 (goto-char (point-max))
1712 (unless (zerop (current-column)) (insert "\n"))
1713 (insert str "\n")
1714 (if cvs-sort-ignore-file (sort-lines nil (point-min) (point-max)))
1715 (save-buffer)))
1716
1717
1718(defun cvs-mode-find-file-other-window (e)
1719 "Select a buffer containing the file in another window."
1720 (interactive (list last-input-event))
1721 (cvs-mode-find-file e t))
1722
1723
1724(defun cvs-find-modif (fi)
1725 (with-temp-buffer
1726 (call-process cvs-program nil (current-buffer) nil
1727 "-f" "diff" (cvs-fileinfo->file fi))
1728 (goto-char (point-min))
1729 (if (re-search-forward "^\\([0-9]+\\)" nil t)
1730 (string-to-number (match-string 1))
1731 1)))
1732
1733
1734(defun cvs-mode-find-file (e &optional other)
1735 "Select a buffer containing the file.
1736With a prefix, opens the buffer in an OTHER window."
1737 (interactive (list last-input-event current-prefix-arg))
6dc7d3d5
SM
1738 (when (ignore-errors (mouse-set-point e) t) ;for invocation via the mouse
1739 (unless (memq (get-text-property (point) 'face)
1740 '(cvs-dirname-face cvs-filename-face))
1741 (error "Not a file name")))
5b467bf4
SM
1742 (cvs-mode!
1743 (lambda (&optional rev)
1744 (interactive (list (cvs-prefix-get 'cvs-branch-prefix)))
1745 (let* ((cvs-buf (current-buffer))
1746 (fi (cvs-mode-marked nil nil :one t)))
1747 (if (eq (cvs-fileinfo->type fi) 'DIRCHANGE)
1748 (let ((odir default-directory))
1749 (setq default-directory
1750 (cvs-expand-dir-name (cvs-fileinfo->dir fi)))
1751 (if other
1752 (dired-other-window default-directory)
1753 (dired default-directory))
1754 (set-buffer cvs-buf)
1755 (setq default-directory odir))
1756 (let ((buf (if rev (cvs-retrieve-revision fi rev)
1757 (find-file-noselect (cvs-fileinfo->full-path fi)))))
1758 (funcall (if other 'switch-to-buffer-other-window 'switch-to-buffer)
1759 buf)
1760 (when (and cvs-find-file-and-jump (cvs-applicable-p fi 'diff-base))
1761 (goto-line (cvs-find-modif fi)))
1762 buf))))))
1763
1764
1765(defun-cvs-mode (cvs-mode-undo . SIMPLE) (flags)
1766 "Undo local changes to all marked files.
1767The file is removed and `cvs update FILE' is run."
1768 ;;"With prefix argument, prompt for cvs FLAGS."
1769 (interactive (list nil));; (cvs-flags-query 'cvs-undo-flags "undo flags")
1770 (if current-prefix-arg (call-interactively 'cvs-mode-revert-to-rev)
1771 (let* ((fis (cvs-do-removal 'undo "update" 'all))
1772 (removedp (lambda (fi) (eq (cvs-fileinfo->type fi) 'REMOVED)))
1773 (fis-split (cvs-partition removedp fis))
1774 (fis-removed (car fis-split))
1775 (fis-other (cdr fis-split)))
1776 (if (null fis-other)
1777 (when fis-removed (cvs-mode-run "add" nil fis-removed))
1778 (cvs-mode-run "update" flags fis-other
1779 :postproc
1780 (when fis-removed
1781 `((with-current-buffer ,(current-buffer)
1782 (cvs-mode-run "add" nil ',fis-removed)))))))))
1783
1784
1785(defun-cvs-mode (cvs-mode-revert-to-rev . NOARGS) (rev)
1786 "Revert the selected files to an old revision."
1787 (interactive
1788 (list (or (cvs-prefix-get 'cvs-branch-prefix)
1789 (let ((current-prefix-arg '(4)))
1790 (cvs-flags-query 'cvs-idiff-version)))))
1791 (let* ((fis (cvs-mode-marked 'revert "revert" :file t))
1792 (tag (concat "tmp_pcl_tag_" (make-temp-name "")))
1793 (untag `((with-current-buffer ,(current-buffer)
1794 (cvs-mode-run "tag" (list "-d" ',tag) ',fis))))
1795 (update `((with-current-buffer ,(current-buffer)
1796 (cvs-mode-run "update" (list "-j" ',tag "-j" ',rev) ',fis
1797 :postproc ',untag)))))
1798 (cvs-mode-run "tag" (list tag) fis :postproc update)))
1799
1800
1801(defun-cvs-mode cvs-mode-delete-lock ()
1802 "Delete the lock file that CVS is waiting for.
1803Note that this can be dangerous. You should only do this
1804if you are convinced that the process that created the lock is dead."
1805 (interactive)
1806 (let* ((default-directory (cvs-expand-dir-name cvs-lock-file))
1807 (locks (directory-files default-directory nil cvs-lock-file-regexp)))
1808 (cond
1809 ((not locks) (error "No lock files found."))
1810 ((yes-or-no-p (concat "Really delete locks in " cvs-lock-file "? "))
1811 (dolist (lock locks)
1812 (cond ((file-directory-p lock) (delete-directory lock))
1813 ((file-exists-p lock) (delete-file lock))))))))
1814
1815
1816(defun-cvs-mode cvs-mode-remove-handled ()
1817 "Remove all lines that are handled.
1818Empty directories are removed."
1819 (interactive)
1820 (cvs-cleanup-collection cvs-cookies
1821 t (or cvs-auto-remove-directories 'handled) t))
1822
1823
1824(defun-cvs-mode cvs-mode-acknowledge ()
1825 "Remove all marked files from the buffer."
1826 (interactive)
1827 (dolist (fi (cvs-get-marked (cvs-ignore-marks-p "acknowledge") t))
1828 (setf (cvs-fileinfo->type fi) 'DEAD))
1829 (cvs-cleanup-collection cvs-cookies nil nil nil))
1830
5b467bf4
SM
1831(defun cvs-do-removal (filter &optional cmd all)
1832 "Remove files.
1833Returns a list of FIS that should be `cvs remove'd."
1834 (let* ((files (cvs-mode-marked filter cmd :file t :read-only t))
1835 (fis (delete-if (lambda (fi) (eq (cvs-fileinfo->type fi) 'UNKNOWN))
1836 (cvs-mode-marked filter cmd)))
1837 (silent (or (not cvs-confirm-removals)
1838 (cvs-every (lambda (fi)
1839 (or (not (file-exists-p
1840 (cvs-fileinfo->full-path fi)))
1841 (cvs-applicable-p fi 'safe-rm)))
1842 files))))
1843 (when (and (not silent) (equal cvs-confirm-removals 'list))
1844 (save-excursion
1845 (pop-to-buffer (cvs-temp-buffer))
1846 (dolist (fi fis)
1847 (insert (cvs-fileinfo->full-path fi) "\n"))))
1848 (if (not (or silent
1849 (yes-or-no-p (format "Delete %d files? " (length files)))))
1850 (progn (message "Aborting") nil)
1851 (dolist (fi files)
1852 (let* ((type (cvs-fileinfo->type fi))
1853 (file (cvs-fileinfo->full-path fi)))
1854 (when (or all (eq type 'UNKNOWN))
1855 (when (file-exists-p file) (delete-file file))
1856 (unless all (setf (cvs-fileinfo->type fi) 'DEAD) t))))
1857 fis)))
1858
1859(defun-cvs-mode (cvs-mode-remove . SIMPLE) (flags)
1860 "Remove all marked files.
1861With prefix argument, prompt for cvs flags."
1862 (interactive (list (cvs-flags-query 'cvs-remove-flags "cvs remove flags")))
1863 (let ((fis (cvs-do-removal 'remove)))
1864 (if fis (cvs-mode-run "remove" (cons "-f" flags) fis)
1865 (cvs-cleanup-collection cvs-cookies nil nil nil))))
1866
1867
1868(defvar cvs-tag-name "")
1869(defun-cvs-mode (cvs-mode-tag . SIMPLE) (tag &optional flags)
1870 "Run `cvs tag TAG' on all selected files.
1871With prefix argument, prompt for cvs flags."
1872 (interactive
1873 (list (setq cvs-tag-name
1874 (cvs-query-read cvs-tag-name "Tag name: " cvs-qtypedesc-tag))
1875 (cvs-flags-query 'cvs-tag-flags "tag flags")))
1876 (cvs-mode-do "tag" (append flags (list tag))
1877 (when cvs-force-dir-tag 'tag)))
1878
1879(defun-cvs-mode (cvs-mode-untag . SIMPLE) (tag &optional flags)
1880 "Run `cvs tag -d TAG' on all selected files.
1881With prefix argument, prompt for cvs flags."
1882 (interactive
1883 (list (setq cvs-tag-name
1884 (cvs-query-read cvs-tag-name "Tag to delete: " cvs-qtypedesc-tag))
1885 (cvs-flags-query 'cvs-tag-flags "tag flags")))
1886 (cvs-mode-do "tag" (append '("-d") flags (list tag))
1887 (when cvs-force-dir-tag 'tag)))
027b73ac 1888
5b467bf4
SM
1889
1890;; Byte compile files.
1891
1892(defun-cvs-mode cvs-mode-byte-compile-files ()
1893 "Run byte-compile-file on all selected files that end in '.el'."
1894 (interactive)
1895 (let ((marked (cvs-get-marked (cvs-ignore-marks-p "byte-compile"))))
1896 (dolist (fi marked)
1897 (let ((filename (cvs-fileinfo->full-path fi)))
1898 (when (string-match "\\.el\\'" filename)
1899 (byte-compile-file filename))))))
1900
1901;; ChangeLog support.
1902
5b467bf4
SM
1903(defun-cvs-mode cvs-mode-add-change-log-entry-other-window ()
1904 "Add a ChangeLog entry in the ChangeLog of the current directory."
1905 (interactive)
1906 (let* ((fi (cvs-mode-marked nil nil :one t))
1907 (default-directory (cvs-expand-dir-name (cvs-fileinfo->dir fi)))
1908 (buffer-file-name (expand-file-name (cvs-fileinfo->file fi))))
1909 ;; This `save-excursion' is necessary because of interaction between
1910 ;; dynamic scoping and buffer-local variables:
1911 ;; the above binding of `buffer-file-name' has temporarily changed the
1912 ;; buffer-local variable (same thing for `default-directory'), so we
1913 ;; need to switch back to the original buffer before the unbinding
1914 ;; restores the old value.
1915 (save-excursion (add-change-log-entry-other-window))))
1916
1917;; interactive commands to set optional flags
1918
1919(defun cvs-mode-set-flags (flag)
1920 "Ask for new setting of cvs-FLAG-flags."
1921 (interactive
1922 (list (completing-read
1923 "Which flag: "
1924 (mapcar 'list '("cvs" "diff" "update" "status" "log" "tag" ;"rtag"
1925 "commit" "remove" "undo" "checkout"))
1926 nil t)))
1927 (let* ((sym (intern (concat "cvs-" flag "-flags"))))
1928 (let ((current-prefix-arg '(16)))
1929 (cvs-flags-query sym (concat flag " flags")))))
1930
1931\f
1932;;;;
1933;;;; Utilities for the *cvs* buffer
1934;;;;
1935
5b467bf4
SM
1936(defun cvs-dir-member-p (fileinfo dir)
1937 "Return true if FILEINFO represents a file in directory DIR."
1938 (and (not (eq (cvs-fileinfo->type fileinfo) 'DIRCHANGE))
b15b5618 1939 (cvs-string-prefix-p dir (cvs-fileinfo->dir fileinfo))))
5b467bf4
SM
1940
1941(defun cvs-execute-single-file (fi extractor program constant-args)
1942 "Internal function for `cvs-execute-single-file-list'."
1943 (let* ((cur-dir (cvs-fileinfo->dir fi))
1944 (default-directory (cvs-expand-dir-name cur-dir))
1945 (inhibit-read-only t)
1946 (arg-list (funcall extractor fi)))
027b73ac 1947
5b467bf4
SM
1948 ;; Execute the command unless extractor returned t.
1949 (when (listp arg-list)
1950 (let* ((args (append constant-args arg-list)))
027b73ac 1951
5b467bf4
SM
1952 (insert (format "=== cd %s\n=== %s %s\n\n"
1953 cur-dir program (cvs-strings->string args)))
027b73ac 1954
5b467bf4
SM
1955 ;; FIXME: return the exit status?
1956 (apply 'call-process program nil t t args)
1957 (goto-char (point-max))))))
1958
1959;; FIXME: make this run in the background ala cvs-run-process...
1960(defun cvs-execute-single-file-list (fis extractor program constant-args)
1961 "Run PROGRAM on all elements on FIS.
1962The PROGRAM will be called with pwd set to the directory the files
1963reside in. CONSTANT-ARGS is a list of strings to pass as arguments to
1964PROGRAM. The arguments given to the program will be CONSTANT-ARGS
1965followed by the list that EXTRACTOR returns.
1966
1967EXTRACTOR will be called once for each file on FIS. It is given
1968one argument, the cvs-fileinfo. It can return t, which means ignore
1969this file, or a list of arguments to send to the program."
1970 (dolist (fi fis)
1971 (cvs-execute-single-file fi extractor program constant-args)))
1972
1973\f
1974(defun cvs-revert-if-needed (fis)
1975 (dolist (fileinfo fis)
1976 (let* ((file (cvs-fileinfo->full-path fileinfo))
1977 (buffer (find-buffer-visiting file)))
1978 ;; For a revert to happen the user must be editing the file...
1979 (unless (or (null buffer)
1980 (eq (cvs-fileinfo->type fileinfo) 'MESSAGE)
1981 ;; FIXME: check whether revert is really needed.
1982 ;; `(verify-visited-file-modtime buffer)' doesn't cut it
1983 ;; because it only looks at the time stamp (it ignores
1984 ;; read-write changes) which is not changed by `commit'.
1985 (buffer-modified-p buffer))
1986 (with-current-buffer buffer
a7996e05
SM
1987 (ignore-errors
1988 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)
1989 ;; `preserve-modes' avoids changing the (minor) modes. But we
1990 ;; do want to reset the mode for VC, so we do it explicitly.
1991 (vc-find-file-hook)
1992 (when (eq (cvs-fileinfo->type fileinfo) 'CONFLICT)
1993 (smerge-mode 1))))))))
5b467bf4
SM
1994
1995\f
1996(defun cvs-change-cvsroot (newroot)
1997 "Change the cvsroot."
1998 (interactive "DNew repository: ")
1999 (if (or (file-directory-p (expand-file-name "CVSROOT" newroot))
2000 (y-or-n-p (concat "Warning: no CVSROOT found inside repository."
2001 " Change cvs-cvsroot anyhow?")))
2002 (setq cvs-cvsroot newroot)))
2003
2004;;;;
2005;;;; useful global settings
2006;;;;
2007
2008;;;###autoload
2009(add-to-list 'completion-ignored-extensions "CVS/")
2010
2011;;
2012;; Hook to allow calling PCL-CVS by visiting the /CVS subdirectory
2013;;
2014
6dc7d3d5 2015;;;###autoload
eed914af 2016(defcustom cvs-dired-action 'cvs-quickdir
6dc7d3d5
SM
2017 "The action to be performed when opening a CVS directory.
2018Sensible values are `cvs-examine', `cvs-status' and `cvs-quickdir'."
2019 :group 'pcl-cvs
2020 :type '(choice (const cvs-examine) (const cvs-status) (const cvs-quickdir)))
2021
5b467bf4
SM
2022;;;###autoload
2023(defcustom cvs-dired-use-hook '(4)
2024 "Whether or not opening a CVS directory should run PCL-CVS.
2025NIL means never do it.
2026ALWAYS means to always do it unless a prefix argument is given to the
2027 command that prompted the opening of the directory.
2028Anything else means to do it only if the prefix arg is equal to this value."
2029 :group 'pcl-cvs
2030 :type '(choice (const :tag "Never" nil)
2031 (const :tag "Always" always)
2032 (const :tag "Prefix" (4))))
2033
2034;;;###autoload
b15b5618 2035(progn (defun cvs-dired-noselect (dir)
5b467bf4
SM
2036 "Run `cvs-examine' if DIR is a CVS administrative directory.
2037The exact behavior is determined also by `cvs-dired-use-hook'."
2038 (when (stringp dir)
2039 (setq dir (directory-file-name dir))
2040 (when (and (string= "CVS" (file-name-nondirectory dir))
2041 (file-readable-p (expand-file-name "Entries" dir))
2042 cvs-dired-use-hook
2043 (if (eq cvs-dired-use-hook 'always)
2044 (not current-prefix-arg)
2045 (equal current-prefix-arg cvs-dired-use-hook)))
2046 (save-excursion
6dc7d3d5 2047 (funcall cvs-dired-action (file-name-directory dir) t t))))))
5b467bf4
SM
2048
2049;;
2050;; hook into VC
2051;;
2052
d15c2aaa
SM
2053(add-hook 'vc-post-command-functions 'cvs-vc-command-advice)
2054
2055(defun cvs-vc-command-advice (command file flags)
2056 (when (and (equal command "cvs")
5b467bf4 2057 ;; don't parse output we don't understand.
d15c2aaa 2058 (member (car flags) cvs-parse-known-commands))
5b467bf4 2059 (save-excursion
d15c2aaa
SM
2060 (let ((buffer (current-buffer))
2061 (dir default-directory)
5b467bf4
SM
2062 (cvs-from-vc t))
2063 (dolist (cvs-buf (buffer-list))
2064 (set-buffer cvs-buf)
2065 ;; look for a corresponding pcl-cvs buffer
2066 (when (and (eq major-mode 'cvs-mode)
2067 (cvs-string-prefix-p default-directory dir))
2068 (let ((subdir (substring dir (length default-directory))))
2069 (set-buffer buffer)
2070 (set (make-local-variable 'cvs-buffer) cvs-buf)
2071 ;; VC never (?) does `cvs -n update' so dcd=nil
2072 ;; should probably always be the right choice.
2073 (cvs-parse-process nil subdir))))))))
2074
2075;;
2076;; Hook into write-buffer
2077;;
2078
2079(defun cvs-mark-buffer-changed ()
2080 (let* ((file (expand-file-name buffer-file-name))
2081 (version (and (fboundp 'vc-backend)
2082 (eq (vc-backend file) 'CVS)
2083 (vc-workfile-version file))))
2084 (when version
2085 (save-excursion
2086 (dolist (cvs-buf (buffer-list))
2087 (set-buffer cvs-buf)
2088 ;; look for a corresponding pcl-cvs buffer
2089 (when (and (eq major-mode 'cvs-mode)
2090 (cvs-string-prefix-p default-directory file))
2091 (let* ((file (substring file (length default-directory)))
2092 (fi (cvs-create-fileinfo
2093 (if (string= "0" version)
2094 'ADDED 'MODIFIED)
2095 (or (file-name-directory file) "")
2096 (file-name-nondirectory file)
2097 "cvs-mark-buffer-changed")))
2098 (cvs-addto-collection cvs-cookies fi))))))))
2099
2100(add-hook 'after-save-hook 'cvs-mark-buffer-changed)
2101
2102;;
2103;; hook into uniquify
2104;;
2105
2106(defadvice uniquify-buffer-file-name (after pcl-cvs-uniquify activate)
2107 (or ad-return-value
2108 (save-excursion
2109 (set-buffer (ad-get-arg 0))
2110 (when (eq major-mode 'cvs-mode)
2111 (setq ad-return-value list-buffers-directory)))))
2112
2113\f
2114(provide 'pcvs)
2115
2116;;; pcvs.el ends here