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.1 2000/03/11 03:42:28 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
49 ("N" . cvs-status-next)
50 ("P" . cvs-status-prev)
51 ("\M-n" . cvs-status-next)
52 ("\M-p" . cvs-status-prev)
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))
102 ;; Define cvs-status-next and cvs-status-prev
103 (easy-mmode-define-navigation cvs-status cvs-status-entry-leader-re "entry")
105 (defun cvs-status-current-file ()
108 (or (re-search-backward cvs-status-entry-leader-re nil t)
109 (re-search-forward cvs-status-entry-leader-re))
110 (let* ((file (match-string 1))
111 (cvsdir (and (re-search-backward cvs-status-dir-re nil t)
113 (pcldir (and (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
116 (let ((default-directory ""))
117 (when pcldir (setq dir (expand-file-name pcldir dir)))
118 (when cvsdir (setq dir (expand-file-name cvsdir dir)))
119 (expand-file-name file dir)))))
121 (defun cvs-status-current-tag ()
124 (col (current-column))
125 (start (progn (re-search-backward cvs-status-tags-leader-re nil t) (point)))
126 (end (progn (re-search-forward "^$" nil t) (point))))
127 (when (and (< start pt) (> end pt))
130 (let ((tag nil) (dist pt) (end (point)))
132 (while (re-search-forward cvs-status-tag-re end t)
133 (let* ((cole (current-column))
134 (colb (save-excursion
135 (goto-char (match-beginning 1)) (current-column)))
136 (ndist (min (abs (- cole col)) (abs (- colb col)))))
139 (setq tag (match-string 1)))))
142 (defun cvs-status-minor-wrap (buf f)
143 (let ((data (with-current-buffer buf
145 (cons (cvs-status-current-file)
146 (cvs-status-current-tag))
147 (when (ignore-errors (mark))
148 ;; `mark-active' is not provided by XEmacs :-(
151 (cons (cvs-status-current-file)
152 (cvs-status-current-tag))))))))
153 (let ((cvs-branch-prefix (cdar data))
154 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
155 (cvs-minor-current-files
157 (when (and (cadr data) (not (equal (caar data) (cadr data))))
158 (list (cadr data)))))
159 ;; FIXME: I need to force because the fileinfos are UNKNOWN
160 (cvs-force-command "/F"))
164 ;; Tagelt, tag element
169 (:constructor cvs-tag-make
170 (vlist &optional name type))
171 (:conc-name cvs-tag->))
176 (defsubst cvs-status-vl-to-str (vl) (mapconcat 'number-to-string vl "."))
178 (defun cvs-tag->string (tag)
179 (if (stringp tag) tag
180 (let ((name (cvs-tag->name tag))
181 (vl (cvs-tag->vlist tag)))
182 (if (null name) (cvs-status-vl-to-str vl)
183 (let ((rev (if vl (concat " (" (cvs-status-vl-to-str vl) ")") "")))
184 (if (consp name) (mapcar (lambda (name) (concat name rev)) name)
185 (concat name rev)))))))
187 (defun cvs-tag-compare-1 (vl1 vl2)
189 ((and (null vl1) (null vl2)) 'equal)
192 (t (let ((v1 (car vl1))
197 (t (cvs-tag-compare-1 (cdr vl1) (cdr vl2))))))))
199 (defsubst cvs-tag-compare (tag1 tag2)
200 (cvs-tag-compare-1 (cvs-tag->vlist tag1) (cvs-tag->vlist tag2)))
202 (defun cvs-tag-merge (tag1 tag2)
203 "Merge TAG1 and TAG2 into one."
204 (let ((type1 (cvs-tag->type tag1))
205 (type2 (cvs-tag->type tag2))
206 (name1 (cvs-tag->name tag1))
207 (name2 (cvs-tag->name tag2)))
208 (unless (equal (cvs-tag->vlist tag1) (cvs-tag->vlist tag2))
209 (setf (cvs-tag->vlist tag1) nil))
211 (unless (or (not type2) (equal type1 type2))
212 (setf (cvs-tag->type tag1) nil))
213 (setf (cvs-tag->type tag1) type2))
215 (setf (cvs-tag->name tag1) (cvs-append name1 name2))
216 (setf (cvs-tag->name tag1) name2))
219 (defun cvs-tree-print (tags printer column)
220 "Print the tree of TAGS where each tag's string is given by PRINTER.
221 PRINTER should accept both a tag (in which case it should return a string)
222 or a string (in which case it should simply return its argument).
223 A tag cannot be a CONS. The return value can also be a list of strings,
224 if several nodes where merged into one.
225 The tree will be printed no closer than column COLUMN."
227 (let* ((eol (save-excursion (end-of-line) (current-column)))
228 (column (max (+ eol 2) column)))
229 (if (null tags) column
230 ;;(move-to-column-force column)
231 (let* ((rev (cvs-car tags))
232 (name (funcall printer (cvs-car rev)))
233 (rest (append (cvs-cdr name) (cvs-cdr tags)))
236 (or (= (forward-line 1) 0) (insert "\n"))
237 (cvs-tree-print rest printer column))))
238 (assert (>= prefix column))
239 (move-to-column prefix t)
241 (insert (cvs-car name))
242 (dolist (br (cvs-cdr rev))
243 (let* ((column (current-column))
244 (brrev (funcall printer (cvs-car br)))
245 (brlength (length (cvs-car brrev)))
246 (brfill (concat (make-string (/ brlength 2) ? ) "|"))
250 (cvs-tree-print (cvs-append brrev brfill (cvs-cdr br))
251 printer (current-column)))))
252 (delete-region (save-excursion (move-to-column prefix) (point))
254 (insert " " (make-string (- prefix column 2) ?-) " ")
258 (defun cvs-tree-merge (tree1 tree2)
259 "Merge tags trees TREE1 and TREE2 into one.
260 BEWARE: because of stability issues, this is not a symetric operation."
261 (assert (and (listp tree1) (listp tree2)))
266 (let* ((rev1 (car tree1))
267 (tag1 (cvs-car rev1))
268 (vl1 (cvs-tag->vlist tag1))
271 (tag2 (cvs-car rev2))
272 (vl2 (cvs-tag->vlist tag2))
276 (case (cvs-tag-compare tag1 tag2)
277 (more1 (list* rev2 (cvs-tree-merge tree1 (cdr tree2))))
278 (more2 (list* rev1 (cvs-tree-merge (cdr tree1) tree2)))
280 (cons (cons (cvs-tag-merge tag1 tag2)
281 (cvs-tree-merge (cvs-cdr rev1) (cvs-cdr rev2)))
282 (cvs-tree-merge (cdr tree1) (cdr tree2))))))
284 (cvs-tree-merge (list (cons (cvs-tag-make (butlast vl1)) tree1)) tree2))
286 (cvs-tree-merge tree1 (list (cons (cvs-tag-make (butlast vl2)) tree2)))))))))
288 (defun cvs-tag-make-tag (tag)
289 (let ((vl (mapcar 'string-to-number (split-string (third tag) "\\."))))
290 (cvs-tag-make vl (first tag) (intern (second tag)))))
292 (defun cvs-tags->tree (tags)
293 "Make a tree out of a list of TAGS."
295 (mapcar (lambda (tag)
296 (let ((tag (cvs-tag-make-tag tag)))
297 (list (if (not (eq (cvs-tag->type tag) 'branch)) tag
298 (list (cvs-tag-make (butlast (cvs-tag->vlist tag)))
304 (push (cvs-tree-merge (pop tags) (pop tags)) tl))
305 (setq tags (nreverse tl))))
308 (defun cvs-status-get-tags ()
309 "Look for a list of tags, read them in and delete them.
310 Returns NIL if there was an empty list of tags and T if there wasn't
311 even a list. Else, return the list of tags where each element of
312 the list is a three-string list TAG, KIND, REV."
314 (if (not (re-search-forward cvs-status-tags-leader-re nil t)) t
318 (case-fold-search t))
320 (looking-at "\\s-+no\\s-+tags")
322 (progn ; normal listing
323 (while (looking-at "^[ \t]+\\([^ \t\n]+\\)[ \t]+(\\([a-z]+\\): \\(.+\\))$")
324 (push (list (match-string 1) (match-string 2) (match-string 3)) tags)
326 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
329 (progn ; cvstree-style listing
330 (while (or (looking-at "^ .+\\(.\\) \\([0-9.]+\\): \\([^\n\t .0-9][^\n\t ]*\\)?$")
332 (looking-at "^ .+\\(\\) \\(8\\)? \\([^\n\t .0-9][^\n\t ]*\\)$")))
333 (setq lastrev (or (match-string 2) lastrev))
334 (push (list (match-string 3)
335 (if (equal (match-string 1) " ") "branch" "revision")
338 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
339 (setq tags (nreverse tags)))
341 (progn ; new tree style listing
342 (let* ((re-lead "[ \t]*\\(-+\\)?\\(|\n?[ \t]+\\)?")
343 (re3 (concat re-lead "\\(\\.\\)?\\(" cvs-status-rev-re "\\)"))
344 (re2 (concat re-lead cvs-status-tag-re "\\(\\)"))
345 (re1 (concat re-lead cvs-status-tag-re
346 " (\\(" cvs-status-rev-re "\\))")))
347 (while (or (looking-at re1) (looking-at re2) (looking-at re3))
348 (push (list (match-string 3)
349 (if (match-string 1) "branch" "revision")
350 (match-string 4)) tags)
351 (goto-char (match-end 0))
352 (when (eolp) (forward-char 1))))
353 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
354 (setq tags (nreverse tags))))
356 (delete-region pt (point)))
359 (defvar font-lock-mode)
360 (defun cvs-refontify (beg end)
361 (when (and (boundp 'font-lock-mode)
363 (fboundp 'font-lock-fontify-region))
364 (font-lock-fontify-region (1- beg) (1+ end))))
366 (defun cvs-status-trees ()
367 "Look for a lists of tags, and replace them with trees."
370 (goto-char (point-min))
371 (let ((inhibit-read-only t)
373 (while (listp (setq tags (cvs-status-get-tags)))
374 ;;(let ((pt (save-excursion (forward-line -1) (point))))
376 (narrow-to-region (point) (point))
378 (cvs-tree-print (cvs-tags->tree tags) 'cvs-tag->string 3))
379 ;;(cvs-refontify pt (point))
385 ;;;; CVSTree-style trees
388 ;; chars sets. Ripped from cvstree
389 (defvar cvs-tree-dstr-2byte-ready
390 (when (featurep 'mule)
391 (if (boundp 'current-language-environment)
392 (string= current-language-environment "Japanese")
394 "*Variable that specifies characters set used in cvstree tree graph.
395 If non-nil, 2byte (Japanese?) characters set is used.
396 If nil, 1byte characters set is used.
397 2byte characters might be available with Mule or Emacs with Mule extension.")
399 (defconst cvs-tree-dstr-char-space
400 (if cvs-tree-dstr-2byte-ready "
\e$B!!
\e(B" " "))
401 (defconst cvs-tree-dstr-char-hbar
402 (if cvs-tree-dstr-2byte-ready "
\e$B(,
\e(B" "--"))
403 (defconst cvs-tree-dstr-char-vbar
404 (if cvs-tree-dstr-2byte-ready "
\e$B(-
\e(B" "| "))
405 (defconst cvs-tree-dstr-char-branch
406 (if cvs-tree-dstr-2byte-ready "
\e$B(2
\e(B" "+-"))
407 (defconst cvs-tree-dstr-char-eob ;end of branch
408 (if cvs-tree-dstr-2byte-ready "
\e$B(1
\e(B" "`-"))
409 (defconst cvs-tree-dstr-char-bob ;beginning of branch
410 (if cvs-tree-dstr-2byte-ready "
\e$B(3
\e(B" "+-"))
412 (defun cvs-tag-lessp (tag1 tag2)
413 (eq (cvs-tag-compare tag1 tag2) 'more2))
415 (defvar cvs-tree-nomerge nil)
417 (defun cvs-status-cvstrees (&optional arg)
418 "Look for a list of tags, and replace it with a tree.
419 Optional prefix ARG chooses between two representations."
422 (goto-char (point-min))
423 (let ((inhibit-read-only t)
425 (cvs-tree-nomerge (if arg (not cvs-tree-nomerge) cvs-tree-nomerge)))
426 (while (listp (setq tags (cvs-status-get-tags)))
427 (let ((tags (mapcar 'cvs-tag-make-tag tags))
428 ;;(pt (save-excursion (forward-line -1) (point)))
430 (setq tags (sort tags 'cvs-tag-lessp))
431 (let* ((first (first tags))
432 (prev (if (cvs-tag-p first)
433 (list (first (cvs-tag->vlist first))) nil)))
434 (cvs-tree-tags-insert tags prev)
435 ;;(cvs-refontify pt (point))
438 (defun cvs-tree-tags-insert (tags prev)
440 (let* ((tag (car tags))
441 (vlist (cvs-tag->vlist tag))
443 (let* ((next (cvs-car (cadr tags)))
444 (nprev (if (and cvs-tree-nomerge next
445 (equal vlist (cvs-tag->vlist next)))
447 (cvs-map (lambda (v p) v) nprev prev)))
448 (after (save-excursion
450 (cvs-tree-tags-insert (cdr tags) nprev)))
452 (nas nil)) ;"next afters" to be returned
454 (do* ((vs vlist (cdr vs))
457 ((and (null as) (null vs) (null ps))
458 (let ((revname (cvs-status-vl-to-str vlist)))
459 (if (cvs-every 'identity (cvs-map 'equal prev vlist))
460 (insert (make-string (+ 4 (length revname)) ? )
461 (or (cvs-tag->name tag) ""))
462 (insert " " revname ": " (or (cvs-tag->name tag) "")))))
463 (let* ((eq (and pe (equal (car ps) (car vs))))
464 (next-eq (equal (cadr ps) (cadr vs))))
468 (if next-eq (cons t cvs-tree-dstr-char-vbar)
469 (cons t cvs-tree-dstr-char-branch))
470 (cons nil cvs-tree-dstr-char-bob))
472 (if next-eq (cons nil cvs-tree-dstr-char-space)
473 (cons t cvs-tree-dstr-char-eob))
474 (cons nil (if (and (eq (cvs-tag->type tag) 'branch)
475 (cvs-every 'null as))
476 cvs-tree-dstr-char-space
477 cvs-tree-dstr-char-hbar))))))
478 (insert (cdr na+char))
479 (push (car na+char) nas))
484 ;;;; Merged trees from different files
487 (defun cvs-tree-fuzzy-merge-1 (trees tree prev)
490 (defun cvs-tree-fuzzy-merge (trees tree)
491 "Do the impossible: merge TREE into TREES."
495 "Get tags from the status output and merge tham all into a big tree."
497 (goto-char (point-min))
498 (let ((inhibit-read-only t)
499 (trees (make-vector 31 0)) tree)
500 (while (listp (setq tree (cvs-tags->tree (cvs-status-get-tags))))
501 (cvs-tree-fuzzy-merge trees tree))
503 (let ((cvs-tag-print-rev nil))
504 (cvs-tree-print tree 'cvs-tag->string 3)))))
507 (provide 'cvs-status)
512 ;;; cvs-status.el ends here