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