1 ;;; cvs-status.el --- Major mode for browsing `cvs status' output
3 ;; Copyright (C) 1999-2000 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: pcl-cvs cvs status tree
8 ;; Revision: $Id: cvs-status.el,v 1.14 2000/03/05 21:32:21 monnier Exp $
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
31 ;; - Rename to cvs-status-mode.el
32 ;; - Somehow allow cvs-status-tree to work on-the-fly
36 (eval-when-compile (require 'cl))
41 (defgroup cvs-status nil
42 "Major mode for browsing `cvs status' output."
44 :prefix "cvs-status-")
46 (easy-mmode-defmap cvs-status-mode-map
48 ("N" . cvs-status-next-entry)
49 ("\M-n" . cvs-status-next-entry)
51 ("P" . cvs-status-prev-entry)
52 ("\M-p" . cvs-status-prev-entry)
53 ("t" . cvs-status-cvstrees)
54 ("T" . cvs-status-trees))
57 :inherit 'cvs-mode-map)
59 ;;(easy-menu-define cvs-status-menu cvs-status-mode-map
60 ;; "Menu for `cvs-status-mode'."
62 ;; ["Show Tag Trees" cvs-status-tree t]
65 (defvar cvs-status-mode-hook nil
66 "Hook run at the end of `cvs-status-mode'.")
68 (defconst cvs-status-tags-leader-re "^ Existing Tags:$")
69 (defconst cvs-status-entry-leader-re "^File: \\(\\S-+\\)\\s-+Status: \\(.+\\)$")
70 (defconst cvs-status-dir-re "^cvs[.ex]* [a-z]+: Examining \\(.+\\)$")
71 (defconst cvs-status-rev-re "[0-9][.0-9]*\\.[.0-9]*[0-9]")
72 (defconst cvs-status-tag-re "[ \t]\\([a-zA-Z][^ \t\n.]*\\)")
74 (defconst cvs-status-font-lock-keywords
75 `((,cvs-status-entry-leader-re
76 (1 'cvs-filename-face)
77 (2 'cvs-need-action-face))
78 (,cvs-status-tags-leader-re
80 (save-excursion (re-search-forward "^\n" nil 'move) (point))
81 (progn (re-search-backward cvs-status-tags-leader-re nil t)
83 (0 font-lock-comment-face))
85 (save-excursion (re-search-forward "^\n" nil 'move) (point))
86 (progn (re-search-backward cvs-status-tags-leader-re nil t)
88 (1 font-lock-function-name-face)))))
89 (defconst cvs-status-font-lock-defaults
90 '(cvs-status-font-lock-keywords t nil nil nil))
93 (put 'cvs-status-mode 'mode-class 'special)
95 (autoload 'cvs-status-mode "cvs-status" "Mode used for cvs status output." t)
96 (eval-when-compile (autoload 'easy-mmode-define-derived-mode "easy-mmode"))
97 (easy-mmode-define-derived-mode cvs-status-mode fundamental-mode "CVS-Status"
98 "Mode used for cvs status output."
99 (set (make-local-variable 'font-lock-defaults) cvs-status-font-lock-defaults)
100 (set (make-local-variable 'cvs-minor-wrap-function) 'cvs-status-minor-wrap))
103 (defun cvs-status-next-entry (n)
104 "Go to the N'th next cvs status entry."
106 (if (< n 0) (cvs-status-prev-entry (- n))
108 (re-search-forward cvs-status-entry-leader-re nil t n)
109 (beginning-of-line)))
111 (defun cvs-status-prev-entry (n)
112 "Go to the N'th previous cvs status entry."
114 (if (< n 0) (cvs-status-next-entry (- n))
116 (re-search-backward cvs-status-entry-leader-re nil t n)
117 (beginning-of-line)))
119 (defun cvs-status-current-file ()
122 (or (re-search-backward cvs-status-entry-leader-re nil t)
123 (re-search-forward cvs-status-entry-leader-re))
124 (let* ((file (match-string 1))
125 (cvsdir (and (re-search-backward cvs-status-dir-re nil t)
127 (pcldir (and (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
130 (let ((default-directory ""))
131 (when pcldir (setq dir (expand-file-name pcldir dir)))
132 (when cvsdir (setq dir (expand-file-name cvsdir dir)))
133 (expand-file-name file dir)))))
135 (defun cvs-status-current-tag ()
138 (col (current-column))
139 (start (progn (re-search-backward cvs-status-tags-leader-re nil t) (point)))
140 (end (progn (re-search-forward "^$" nil t) (point))))
141 (when (and (< start pt) (> end pt))
144 (let ((tag nil) (dist pt) (end (point)))
146 (while (re-search-forward cvs-status-tag-re end t)
147 (let* ((cole (current-column))
148 (colb (save-excursion
149 (goto-char (match-beginning 1)) (current-column)))
150 (ndist (min (abs (- cole col)) (abs (- colb col)))))
153 (setq tag (match-string 1)))))
156 (defun cvs-status-minor-wrap (buf f)
157 (let ((data (with-current-buffer buf
159 (cons (cvs-status-current-file)
160 (cvs-status-current-tag))
161 (when (ignore-errors (mark))
162 ;; `mark-active' is not provided by XEmacs :-(
165 (cons (cvs-status-current-file)
166 (cvs-status-current-tag))))))))
167 (let ((cvs-branch-prefix (cdar data))
168 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
169 (cvs-minor-current-files
171 (when (and (cadr data) (not (equal (caar data) (cadr data))))
172 (list (cadr data)))))
173 ;; FIXME: I need to force because the fileinfos are UNKNOWN
174 (cvs-force-command "/F"))
178 ;; Tagelt, tag element
183 (:constructor cvs-tag-make
184 (vlist &optional name type))
185 (:conc-name cvs-tag->))
190 (defsubst cvs-status-vl-to-str (vl) (mapconcat 'number-to-string vl "."))
192 (defun cvs-tag->string (tag)
193 (if (stringp tag) tag
194 (let ((name (cvs-tag->name tag))
195 (vl (cvs-tag->vlist tag)))
196 (if (null name) (cvs-status-vl-to-str vl)
197 (let ((rev (if vl (concat " (" (cvs-status-vl-to-str vl) ")") "")))
198 (if (consp name) (mapcar (lambda (name) (concat name rev)) name)
199 (concat name rev)))))))
201 (defun cvs-tag-compare-1 (vl1 vl2)
203 ((and (null vl1) (null vl2)) 'equal)
206 (t (let ((v1 (car vl1))
211 (t (cvs-tag-compare-1 (cdr vl1) (cdr vl2))))))))
213 (defsubst cvs-tag-compare (tag1 tag2)
214 (cvs-tag-compare-1 (cvs-tag->vlist tag1) (cvs-tag->vlist tag2)))
216 (defun cvs-tag-merge (tag1 tag2)
217 "Merge TAG1 and TAG2 into one."
218 (let ((type1 (cvs-tag->type tag1))
219 (type2 (cvs-tag->type tag2))
220 (name1 (cvs-tag->name tag1))
221 (name2 (cvs-tag->name tag2)))
222 (unless (equal (cvs-tag->vlist tag1) (cvs-tag->vlist tag2))
223 (setf (cvs-tag->vlist tag1) nil))
225 (unless (or (not type2) (equal type1 type2))
226 (setf (cvs-tag->type tag1) nil))
227 (setf (cvs-tag->type tag1) type2))
229 (setf (cvs-tag->name tag1) (cvs-append name1 name2))
230 (setf (cvs-tag->name tag1) name2))
233 (defun cvs-tree-print (tags printer column)
234 "Print the tree of TAGS where each tag's string is given by PRINTER.
235 PRINTER should accept both a tag (in which case it should return a string)
236 or a string (in which case it should simply return its argument).
237 A tag cannot be a CONS. The return value can also be a list of strings,
238 if several nodes where merged into one.
239 The tree will be printed no closer than column COLUMN."
241 (let* ((eol (save-excursion (end-of-line) (current-column)))
242 (column (max (+ eol 2) column)))
243 (if (null tags) column
244 ;;(move-to-column-force column)
245 (let* ((rev (cvs-car tags))
246 (name (funcall printer (cvs-car rev)))
247 (rest (append (cvs-cdr name) (cvs-cdr tags)))
250 (or (= (forward-line 1) 0) (insert "\n"))
251 (cvs-tree-print rest printer column))))
252 (assert (>= prefix column))
253 (move-to-column prefix t)
255 (insert (cvs-car name))
256 (dolist (br (cvs-cdr rev))
257 (let* ((column (current-column))
258 (brrev (funcall printer (cvs-car br)))
259 (brlength (length (cvs-car brrev)))
260 (brfill (concat (make-string (/ brlength 2) ? ) "|"))
264 (cvs-tree-print (cvs-append brrev brfill (cvs-cdr br))
265 printer (current-column)))))
266 (delete-region (save-excursion (move-to-column prefix) (point))
268 (insert " " (make-string (- prefix column 2) ?-) " ")
272 (defun cvs-tree-merge (tree1 tree2)
273 "Merge tags trees TREE1 and TREE2 into one.
274 BEWARE: because of stability issues, this is not a symetric operation."
275 (assert (and (listp tree1) (listp tree2)))
280 (let* ((rev1 (car tree1))
281 (tag1 (cvs-car rev1))
282 (vl1 (cvs-tag->vlist tag1))
285 (tag2 (cvs-car rev2))
286 (vl2 (cvs-tag->vlist tag2))
290 (case (cvs-tag-compare tag1 tag2)
291 (more1 (list* rev2 (cvs-tree-merge tree1 (cdr tree2))))
292 (more2 (list* rev1 (cvs-tree-merge (cdr tree1) tree2)))
294 (cons (cons (cvs-tag-merge tag1 tag2)
295 (cvs-tree-merge (cvs-cdr rev1) (cvs-cdr rev2)))
296 (cvs-tree-merge (cdr tree1) (cdr tree2))))))
298 (cvs-tree-merge (list (cons (cvs-tag-make (butlast vl1)) tree1)) tree2))
300 (cvs-tree-merge tree1 (list (cons (cvs-tag-make (butlast vl2)) tree2)))))))))
302 (defun cvs-tag-make-tag (tag)
303 (let ((vl (mapcar 'string-to-number (split-string (third tag) "\\."))))
304 (cvs-tag-make vl (first tag) (intern (second tag)))))
306 (defun cvs-tags->tree (tags)
307 "Make a tree out of a list of TAGS."
309 (mapcar (lambda (tag)
310 (let ((tag (cvs-tag-make-tag tag)))
311 (list (if (not (eq (cvs-tag->type tag) 'branch)) tag
312 (list (cvs-tag-make (butlast (cvs-tag->vlist tag)))
318 (push (cvs-tree-merge (pop tags) (pop tags)) tl))
319 (setq tags (nreverse tl))))
322 (defun cvs-status-get-tags ()
323 "Look for a list of tags, read them in and delete them.
324 Returns NIL if there was an empty list of tags and T if there wasn't
325 even a list. Else, return the list of tags where each element of
326 the list is a three-string list TAG, KIND, REV."
328 (if (not (re-search-forward cvs-status-tags-leader-re nil t)) t
332 (case-fold-search t))
334 (looking-at "\\s-+no\\s-+tags")
336 (progn ; normal listing
337 (while (looking-at "^[ \t]+\\([^ \t\n]+\\)[ \t]+(\\([a-z]+\\): \\(.+\\))$")
338 (push (list (match-string 1) (match-string 2) (match-string 3)) tags)
340 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
343 (progn ; cvstree-style listing
344 (while (or (looking-at "^ .+\\(.\\) \\([0-9.]+\\): \\([^\n\t .0-9][^\n\t ]*\\)?$")
346 (looking-at "^ .+\\(\\) \\(8\\)? \\([^\n\t .0-9][^\n\t ]*\\)$")))
347 (setq lastrev (or (match-string 2) lastrev))
348 (push (list (match-string 3)
349 (if (equal (match-string 1) " ") "branch" "revision")
352 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
353 (setq tags (nreverse tags)))
355 (progn ; new tree style listing
356 (let* ((re-lead "[ \t]*\\(-+\\)?\\(|\n?[ \t]+\\)?")
357 (re3 (concat re-lead "\\(\\.\\)?\\(" cvs-status-rev-re "\\)"))
358 (re2 (concat re-lead cvs-status-tag-re "\\(\\)"))
359 (re1 (concat re-lead cvs-status-tag-re
360 " (\\(" cvs-status-rev-re "\\))")))
361 (while (or (looking-at re1) (looking-at re2) (looking-at re3))
362 (push (list (match-string 3)
363 (if (match-string 1) "branch" "revision")
364 (match-string 4)) tags)
365 (goto-char (match-end 0))
366 (when (eolp) (forward-char 1))))
367 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
368 (setq tags (nreverse tags))))
370 (delete-region pt (point)))
373 (defvar font-lock-mode)
374 (defun cvs-refontify (beg end)
375 (when (and (boundp 'font-lock-mode)
377 (fboundp 'font-lock-fontify-region))
378 (font-lock-fontify-region (1- beg) (1+ end))))
380 (defun cvs-status-trees ()
381 "Look for a lists of tags, and replace them with trees."
384 (goto-char (point-min))
385 (let ((inhibit-read-only t)
387 (while (listp (setq tags (cvs-status-get-tags)))
388 ;;(let ((pt (save-excursion (forward-line -1) (point))))
390 (narrow-to-region (point) (point))
392 (cvs-tree-print (cvs-tags->tree tags) 'cvs-tag->string 3))
393 ;;(cvs-refontify pt (point))
399 ;;;; CVSTree-style trees
402 ;; chars sets. Ripped from cvstree
403 (defvar cvstree-dstr-2byte-ready
404 (when (featurep 'mule)
405 (if (boundp 'current-language-environment)
406 (string= current-language-environment "Japanese")
408 "*Variable that specifies characters set used in cvstree tree graph.
409 If non-nil, 2byte (Japanese?) characters set is used.
410 If nil, 1byte characters set is used.
411 2byte characters might be available with Mule or Emacs with Mule extension.")
413 (defconst cvstree-dstr-char-space
414 (if cvstree-dstr-2byte-ready "
\e$B!!
\e(B" " "))
415 (defconst cvstree-dstr-char-hbar
416 (if cvstree-dstr-2byte-ready "
\e$B(,
\e(B" "-"))
417 (defconst cvstree-dstr-char-vbar
418 (if cvstree-dstr-2byte-ready "
\e$B(-
\e(B" "|"))
419 (defconst cvstree-dstr-char-branch
420 (if cvstree-dstr-2byte-ready "
\e$B(2
\e(B" "+"))
421 (defconst cvstree-dstr-char-eob ;end of branch
422 (if cvstree-dstr-2byte-ready "
\e$B(1
\e(B" "`"))
423 (defconst cvstree-dstr-char-bob ;beginning of branch
424 (if cvstree-dstr-2byte-ready "
\e$B(3
\e(B" "+"))
426 (defun cvs-tag-lessp (tag1 tag2)
427 (eq (cvs-tag-compare tag1 tag2) 'more2))
429 (defvar cvs-tree-nomerge t)
431 (defun cvs-status-cvstrees (&optional arg)
432 "Look for a list of tags, and replace it with a tree.
433 Optional prefix ARG chooses between two representations."
436 (goto-char (point-min))
437 (let ((inhibit-read-only t)
439 (cvs-tree-nomerge (if arg (not cvs-tree-nomerge) cvs-tree-nomerge)))
440 (while (listp (setq tags (cvs-status-get-tags)))
441 (let ((tags (mapcar 'cvs-tag-make-tag tags))
442 ;;(pt (save-excursion (forward-line -1) (point)))
444 (setq tags (sort tags 'cvs-tag-lessp))
445 (let* ((first (first tags))
446 (prev (if (cvs-tag-p first)
447 (list (first (cvs-tag->vlist first))) nil)))
448 (cvs-tree-tags-insert tags prev)
449 ;;(cvs-refontify pt (point))
452 (defun cvs-tree-tags-insert (tags prev)
454 (let* ((tag (car tags))
455 (vlist (cvs-tag->vlist tag))
457 (let* ((next (cvs-car (cadr tags)))
458 (nprev (if (and cvs-tree-nomerge next
459 (equal vlist (cvs-tag->vlist next)))
461 (cvs-map (lambda (v p) v) nprev prev)))
462 (after (save-excursion
464 (cvs-tree-tags-insert (cdr tags) nprev)))
466 (nas nil)) ;"next afters" to be returned
468 (do* ((vs vlist (cdr vs))
471 ((and (null as) (null vs) (null ps))
472 (let ((revname (cvs-status-vl-to-str vlist)))
473 (if (cvs-every 'identity (cvs-map 'equal prev vlist))
474 (insert (make-string (+ 4 (length revname)) ? )
475 (or (cvs-tag->name tag) ""))
476 (insert " " revname ": " (or (cvs-tag->name tag) "")))))
477 (let* ((eq (and pe (equal (car ps) (car vs))))
478 (next-eq (equal (cadr ps) (cadr vs))))
482 (if next-eq (cons t cvstree-dstr-char-vbar)
483 (cons t cvstree-dstr-char-branch))
484 (cons nil cvstree-dstr-char-bob))
486 (if next-eq (cons nil cvstree-dstr-char-space)
487 (cons t cvstree-dstr-char-eob))
488 (cons nil (if (and (eq (cvs-tag->type tag) 'branch)
489 (cvs-every 'null as))
490 cvstree-dstr-char-space
491 cvstree-dstr-char-hbar))))))
492 (insert (cdr na+char))
493 (push (car na+char) nas))
498 ;;;; Merged trees from different files
501 (defun cvs-tree-fuzzy-merge-1 (trees tree prev)
504 (defun cvs-tree-fuzzy-merge (trees tree)
505 "Do the impossible: merge TREE into TREES."
509 "Get tags from the status output and merge tham all into a big tree."
511 (goto-char (point-min))
512 (let ((inhibit-read-only t)
513 (trees (make-vector 31 0)) tree)
514 (while (listp (setq tree (cvs-tags->tree (cvs-status-get-tags))))
515 (cvs-tree-fuzzy-merge trees tree))
517 (let ((cvs-tag-print-rev nil))
518 (cvs-tree-print tree 'cvs-tag->string 3)))))
521 (provide 'cvs-status)
523 ;;; cvs-status.el ends here