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