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