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