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