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