]> code.delx.au - gnu-emacs/blob - lisp/cvs-status.el
(cvs-status-(prev|next)): Rename from
[gnu-emacs] / lisp / cvs-status.el
1 ;;; cvs-status.el --- Major mode for browsing `cvs status' output
2
3 ;; Copyright (C) 1999-2000 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: pcl-cvs cvs status tree
7 ;; Version: $Name: $
8 ;; Revision: $Id: cvs-status.el,v 1.1 2000/03/11 03:42:28 monnier Exp $
9
10 ;; This file is part of GNU Emacs.
11
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)
15 ;; any later version.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;; Todo:
30
31 ;; - Rename to cvs-status-mode.el
32 ;; - Somehow allow cvs-status-tree to work on-the-fly
33
34 ;;; Code:
35
36 (eval-when-compile (require 'cl))
37 (require 'pcvs-util)
38
39 ;;;
40
41 (defgroup cvs-status nil
42 "Major mode for browsing `cvs status' output."
43 :group 'pcl-cvs
44 :prefix "cvs-status-")
45
46 (easy-mmode-defmap cvs-status-mode-map
47 '(("n" . next-line)
48 ("p" . previous-line)
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))
55 "CVS-Status' keymap."
56 :group 'cvs-status
57 :inherit 'cvs-mode-map)
58
59 ;;(easy-menu-define cvs-status-menu cvs-status-mode-map
60 ;; "Menu for `cvs-status-mode'."
61 ;; '("CVS-Status"
62 ;; ["Show Tag Trees" cvs-status-tree t]
63 ;; ))
64
65 (defvar cvs-status-mode-hook nil
66 "Hook run at the end of `cvs-status-mode'.")
67
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.]*\\)")
73
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
79 (,cvs-status-rev-re
80 (save-excursion (re-search-forward "^\n" nil 'move) (point))
81 (progn (re-search-backward cvs-status-tags-leader-re nil t)
82 (forward-line 1))
83 (0 font-lock-comment-face))
84 (,cvs-status-tag-re
85 (save-excursion (re-search-forward "^\n" nil 'move) (point))
86 (progn (re-search-backward cvs-status-tags-leader-re nil t)
87 (forward-line 1))
88 (1 font-lock-function-name-face)))))
89 (defconst cvs-status-font-lock-defaults
90 '(cvs-status-font-lock-keywords t nil nil nil))
91
92
93 (put 'cvs-status-mode 'mode-class 'special)
94 ;;;###autoload
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))
101
102 ;; Define cvs-status-next and cvs-status-prev
103 (easy-mmode-define-navigation cvs-status cvs-status-entry-leader-re "entry")
104
105 (defun cvs-status-current-file ()
106 (save-excursion
107 (forward-line 1)
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)
112 (match-string 1)))
113 (pcldir (and (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
114 (match-string 1)))
115 (dir ""))
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)))))
120
121 (defun cvs-status-current-tag ()
122 (save-excursion
123 (let ((pt (point))
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))
128 (goto-char pt)
129 (end-of-line)
130 (let ((tag nil) (dist pt) (end (point)))
131 (beginning-of-line)
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)))))
137 (when (< ndist dist)
138 (setq dist ndist)
139 (setq tag (match-string 1)))))
140 tag)))))
141
142 (defun cvs-status-minor-wrap (buf f)
143 (let ((data (with-current-buffer buf
144 (cons
145 (cons (cvs-status-current-file)
146 (cvs-status-current-tag))
147 (when (ignore-errors (mark))
148 ;; `mark-active' is not provided by XEmacs :-(
149 (save-excursion
150 (goto-char (mark))
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
156 (cons (caar data)
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"))
161 (funcall f))))
162
163 ;;
164 ;; Tagelt, tag element
165 ;;
166
167 (defstruct (cvs-tag
168 (:constructor nil)
169 (:constructor cvs-tag-make
170 (vlist &optional name type))
171 (:conc-name cvs-tag->))
172 vlist
173 name
174 type)
175
176 (defsubst cvs-status-vl-to-str (vl) (mapconcat 'number-to-string vl "."))
177
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)))))))
186
187 (defun cvs-tag-compare-1 (vl1 vl2)
188 (cond
189 ((and (null vl1) (null vl2)) 'equal)
190 ((null vl1) 'more2)
191 ((null vl2) 'more1)
192 (t (let ((v1 (car vl1))
193 (v2 (car vl2)))
194 (cond
195 ((> v1 v2) 'more1)
196 ((< v1 v2) 'more2)
197 (t (cvs-tag-compare-1 (cdr vl1) (cdr vl2))))))))
198
199 (defsubst cvs-tag-compare (tag1 tag2)
200 (cvs-tag-compare-1 (cvs-tag->vlist tag1) (cvs-tag->vlist tag2)))
201
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))
210 (if type1
211 (unless (or (not type2) (equal type1 type2))
212 (setf (cvs-tag->type tag1) nil))
213 (setf (cvs-tag->type tag1) type2))
214 (if name1
215 (setf (cvs-tag->name tag1) (cvs-append name1 name2))
216 (setf (cvs-tag->name tag1) name2))
217 tag1))
218
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."
226
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)))
234 (prefix
235 (save-excursion
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)
240 (assert (eolp))
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) ? ) "|"))
247 (prefix
248 (save-excursion
249 (insert " -- ")
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))
253 (point))
254 (insert " " (make-string (- prefix column 2) ?-) " ")
255 (end-of-line)))
256 prefix))))
257
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)))
262 (cond
263 ((null tree1) tree2)
264 ((null tree2) tree1)
265 (t
266 (let* ((rev1 (car tree1))
267 (tag1 (cvs-car rev1))
268 (vl1 (cvs-tag->vlist tag1))
269 (l1 (length vl1))
270 (rev2 (car tree2))
271 (tag2 (cvs-car rev2))
272 (vl2 (cvs-tag->vlist tag2))
273 (l2 (length vl2)))
274 (cond
275 ((= l1 l2)
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)))
279 (equal
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))))))
283 ((> l1 l2)
284 (cvs-tree-merge (list (cons (cvs-tag-make (butlast vl1)) tree1)) tree2))
285 ((< l1 l2)
286 (cvs-tree-merge tree1 (list (cons (cvs-tag-make (butlast vl2)) tree2)))))))))
287
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)))))
291
292 (defun cvs-tags->tree (tags)
293 "Make a tree out of a list of TAGS."
294 (let ((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)))
299 tag)))))
300 tags)))
301 (while (cdr tags)
302 (let (tl)
303 (while tags
304 (push (cvs-tree-merge (pop tags) (pop tags)) tl))
305 (setq tags (nreverse tl))))
306 (car tags)))
307
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."
313 (let ((tags nil))
314 (if (not (re-search-forward cvs-status-tags-leader-re nil t)) t
315 (forward-char 1)
316 (let ((pt (point))
317 (lastrev nil)
318 (case-fold-search t))
319 (or
320 (looking-at "\\s-+no\\s-+tags")
321
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)
325 (forward-line 1))
326 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
327 tags)
328
329 (progn ; cvstree-style listing
330 (while (or (looking-at "^ .+\\(.\\) \\([0-9.]+\\): \\([^\n\t .0-9][^\n\t ]*\\)?$")
331 (and lastrev
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")
336 lastrev) tags)
337 (forward-line 1))
338 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
339 (setq tags (nreverse tags)))
340
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))))
355
356 (delete-region pt (point)))
357 tags)))
358
359 (defvar font-lock-mode)
360 (defun cvs-refontify (beg end)
361 (when (and (boundp 'font-lock-mode)
362 font-lock-mode
363 (fboundp 'font-lock-fontify-region))
364 (font-lock-fontify-region (1- beg) (1+ end))))
365
366 (defun cvs-status-trees ()
367 "Look for a lists of tags, and replace them with trees."
368 (interactive)
369 (save-excursion
370 (goto-char (point-min))
371 (let ((inhibit-read-only t)
372 (tags nil))
373 (while (listp (setq tags (cvs-status-get-tags)))
374 ;;(let ((pt (save-excursion (forward-line -1) (point))))
375 (save-restriction
376 (narrow-to-region (point) (point))
377 ;;(newline)
378 (cvs-tree-print (cvs-tags->tree tags) 'cvs-tag->string 3))
379 ;;(cvs-refontify pt (point))
380 (sit-for 0)
381 ;;)
382 ))))
383
384 ;;;;
385 ;;;; CVSTree-style trees
386 ;;;;
387
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")
393 t)) ; mule/emacs-19
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.")
398
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" "+-"))
411
412 (defun cvs-tag-lessp (tag1 tag2)
413 (eq (cvs-tag-compare tag1 tag2) 'more2))
414
415 (defvar cvs-tree-nomerge nil)
416
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."
420 (interactive "P")
421 (save-excursion
422 (goto-char (point-min))
423 (let ((inhibit-read-only t)
424 (tags nil)
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)))
429 )
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))
436 (sit-for 0)))))))
437
438 (defun cvs-tree-tags-insert (tags prev)
439 (when tags
440 (let* ((tag (car tags))
441 (vlist (cvs-tag->vlist tag))
442 (nprev ;"next prev"
443 (let* ((next (cvs-car (cadr tags)))
444 (nprev (if (and cvs-tree-nomerge next
445 (equal vlist (cvs-tag->vlist next)))
446 prev vlist)))
447 (cvs-map (lambda (v p) v) nprev prev)))
448 (after (save-excursion
449 (newline)
450 (cvs-tree-tags-insert (cdr tags) nprev)))
451 (pe t) ;"prev equal"
452 (nas nil)) ;"next afters" to be returned
453 (insert " ")
454 (do* ((vs vlist (cdr vs))
455 (ps prev (cdr ps))
456 (as after (cdr as)))
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))))
465 (let* ((na+char
466 (if (car as)
467 (if eq
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))
471 (if eq
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))
480 (setq pe eq)))
481 (nreverse nas))))
482
483 ;;;;
484 ;;;; Merged trees from different files
485 ;;;;
486
487 (defun cvs-tree-fuzzy-merge-1 (trees tree prev)
488 )
489
490 (defun cvs-tree-fuzzy-merge (trees tree)
491 "Do the impossible: merge TREE into TREES."
492 ())
493
494 (defun cvs-tree ()
495 "Get tags from the status output and merge tham all into a big tree."
496 (save-excursion
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))
502 (erase-buffer)
503 (let ((cvs-tag-print-rev nil))
504 (cvs-tree-print tree 'cvs-tag->string 3)))))
505
506
507 (provide 'cvs-status)
508
509 ;;; Change Log:
510 ;; $Log$
511
512 ;;; cvs-status.el ends here