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