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