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