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