Use `nth' instead of `first', `second', and `third'.
[emacs.git] / lisp / cvs-status.el
blob4f46fb8c4ff2ec2e42a2e872053f67eb1d5ce2bb
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
7 ;; Version: $Name: $
8 ;; Revision: $Id: cvs-status.el,v 1.4 2000/05/10 22:08: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)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;; Todo:
31 ;; - Rename to cvs-status-mode.el
32 ;; - Somehow allow cvs-status-tree to work on-the-fly
34 ;;; Code:
36 (eval-when-compile (require 'cl))
37 (require 'pcvs-util)
39 ;;;
41 (defgroup cvs-status nil
42 "Major mode for browsing `cvs status' output."
43 :group 'pcl-cvs
44 :prefix "cvs-status-")
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)
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 ;; ))
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
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))
93 (put 'cvs-status-mode 'mode-class 'special)
94 ;;;###autoload
95 (define-derived-mode cvs-status-mode fundamental-mode "CVS-Status"
96 "Mode used for cvs status output."
97 (set (make-local-variable 'font-lock-defaults) cvs-status-font-lock-defaults)
98 (set (make-local-variable 'cvs-minor-wrap-function) 'cvs-status-minor-wrap))
100 ;; Define cvs-status-next and cvs-status-prev
101 (easy-mmode-define-navigation cvs-status cvs-status-entry-leader-re "entry")
103 (defun cvs-status-current-file ()
104 (save-excursion
105 (forward-line 1)
106 (or (re-search-backward cvs-status-entry-leader-re nil t)
107 (re-search-forward cvs-status-entry-leader-re))
108 (let* ((file (match-string 1))
109 (cvsdir (and (re-search-backward cvs-status-dir-re nil t)
110 (match-string 1)))
111 (pcldir (and (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
112 (match-string 1)))
113 (dir ""))
114 (let ((default-directory ""))
115 (when pcldir (setq dir (expand-file-name pcldir dir)))
116 (when cvsdir (setq dir (expand-file-name cvsdir dir)))
117 (expand-file-name file dir)))))
119 (defun cvs-status-current-tag ()
120 (save-excursion
121 (let ((pt (point))
122 (col (current-column))
123 (start (progn (re-search-backward cvs-status-tags-leader-re nil t) (point)))
124 (end (progn (re-search-forward "^$" nil t) (point))))
125 (when (and (< start pt) (> end pt))
126 (goto-char pt)
127 (end-of-line)
128 (let ((tag nil) (dist pt) (end (point)))
129 (beginning-of-line)
130 (while (re-search-forward cvs-status-tag-re end t)
131 (let* ((cole (current-column))
132 (colb (save-excursion
133 (goto-char (match-beginning 1)) (current-column)))
134 (ndist (min (abs (- cole col)) (abs (- colb col)))))
135 (when (< ndist dist)
136 (setq dist ndist)
137 (setq tag (match-string 1)))))
138 tag)))))
140 (defun cvs-status-minor-wrap (buf f)
141 (let ((data (with-current-buffer buf
142 (cons
143 (cons (cvs-status-current-file)
144 (cvs-status-current-tag))
145 (when mark-active
146 (save-excursion
147 (goto-char (mark))
148 (cons (cvs-status-current-file)
149 (cvs-status-current-tag))))))))
150 (let ((cvs-branch-prefix (cdar data))
151 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
152 (cvs-minor-current-files
153 (cons (caar data)
154 (when (and (cadr data) (not (equal (caar data) (cadr data))))
155 (list (cadr data)))))
156 ;; FIXME: I need to force because the fileinfos are UNKNOWN
157 (cvs-force-command "/F"))
158 (funcall f))))
161 ;; Tagelt, tag element
164 (defstruct (cvs-tag
165 (:constructor nil)
166 (:constructor cvs-tag-make
167 (vlist &optional name type))
168 (:conc-name cvs-tag->))
169 vlist
170 name
171 type)
173 (defsubst cvs-status-vl-to-str (vl) (mapconcat 'number-to-string vl "."))
175 (defun cvs-tag->string (tag)
176 (if (stringp tag) tag
177 (let ((name (cvs-tag->name tag))
178 (vl (cvs-tag->vlist tag)))
179 (if (null name) (cvs-status-vl-to-str vl)
180 (let ((rev (if vl (concat " (" (cvs-status-vl-to-str vl) ")") "")))
181 (if (consp name) (mapcar (lambda (name) (concat name rev)) name)
182 (concat name rev)))))))
184 (defun cvs-tag-compare-1 (vl1 vl2)
185 (cond
186 ((and (null vl1) (null vl2)) 'equal)
187 ((null vl1) 'more2)
188 ((null vl2) 'more1)
189 (t (let ((v1 (car vl1))
190 (v2 (car vl2)))
191 (cond
192 ((> v1 v2) 'more1)
193 ((< v1 v2) 'more2)
194 (t (cvs-tag-compare-1 (cdr vl1) (cdr vl2))))))))
196 (defsubst cvs-tag-compare (tag1 tag2)
197 (cvs-tag-compare-1 (cvs-tag->vlist tag1) (cvs-tag->vlist tag2)))
199 (defun cvs-tag-merge (tag1 tag2)
200 "Merge TAG1 and TAG2 into one."
201 (let ((type1 (cvs-tag->type tag1))
202 (type2 (cvs-tag->type tag2))
203 (name1 (cvs-tag->name tag1))
204 (name2 (cvs-tag->name tag2)))
205 (unless (equal (cvs-tag->vlist tag1) (cvs-tag->vlist tag2))
206 (setf (cvs-tag->vlist tag1) nil))
207 (if type1
208 (unless (or (not type2) (equal type1 type2))
209 (setf (cvs-tag->type tag1) nil))
210 (setf (cvs-tag->type tag1) type2))
211 (if name1
212 (setf (cvs-tag->name tag1) (cvs-append name1 name2))
213 (setf (cvs-tag->name tag1) name2))
214 tag1))
216 (defun cvs-tree-print (tags printer column)
217 "Print the tree of TAGS where each tag's string is given by PRINTER.
218 PRINTER should accept both a tag (in which case it should return a string)
219 or a string (in which case it should simply return its argument).
220 A tag cannot be a CONS. The return value can also be a list of strings,
221 if several nodes where merged into one.
222 The tree will be printed no closer than column COLUMN."
224 (let* ((eol (save-excursion (end-of-line) (current-column)))
225 (column (max (+ eol 2) column)))
226 (if (null tags) column
227 ;;(move-to-column-force column)
228 (let* ((rev (cvs-car tags))
229 (name (funcall printer (cvs-car rev)))
230 (rest (append (cvs-cdr name) (cvs-cdr tags)))
231 (prefix
232 (save-excursion
233 (or (= (forward-line 1) 0) (insert "\n"))
234 (cvs-tree-print rest printer column))))
235 (assert (>= prefix column))
236 (move-to-column prefix t)
237 (assert (eolp))
238 (insert (cvs-car name))
239 (dolist (br (cvs-cdr rev))
240 (let* ((column (current-column))
241 (brrev (funcall printer (cvs-car br)))
242 (brlength (length (cvs-car brrev)))
243 (brfill (concat (make-string (/ brlength 2) ? ) "|"))
244 (prefix
245 (save-excursion
246 (insert " -- ")
247 (cvs-tree-print (cvs-append brrev brfill (cvs-cdr br))
248 printer (current-column)))))
249 (delete-region (save-excursion (move-to-column prefix) (point))
250 (point))
251 (insert " " (make-string (- prefix column 2) ?-) " ")
252 (end-of-line)))
253 prefix))))
255 (defun cvs-tree-merge (tree1 tree2)
256 "Merge tags trees TREE1 and TREE2 into one.
257 BEWARE: because of stability issues, this is not a symetric operation."
258 (assert (and (listp tree1) (listp tree2)))
259 (cond
260 ((null tree1) tree2)
261 ((null tree2) tree1)
263 (let* ((rev1 (car tree1))
264 (tag1 (cvs-car rev1))
265 (vl1 (cvs-tag->vlist tag1))
266 (l1 (length vl1))
267 (rev2 (car tree2))
268 (tag2 (cvs-car rev2))
269 (vl2 (cvs-tag->vlist tag2))
270 (l2 (length vl2)))
271 (cond
272 ((= l1 l2)
273 (case (cvs-tag-compare tag1 tag2)
274 (more1 (list* rev2 (cvs-tree-merge tree1 (cdr tree2))))
275 (more2 (list* rev1 (cvs-tree-merge (cdr tree1) tree2)))
276 (equal
277 (cons (cons (cvs-tag-merge tag1 tag2)
278 (cvs-tree-merge (cvs-cdr rev1) (cvs-cdr rev2)))
279 (cvs-tree-merge (cdr tree1) (cdr tree2))))))
280 ((> l1 l2)
281 (cvs-tree-merge (list (cons (cvs-tag-make (butlast vl1)) tree1)) tree2))
282 ((< l1 l2)
283 (cvs-tree-merge tree1 (list (cons (cvs-tag-make (butlast vl2)) tree2)))))))))
285 (defun cvs-tag-make-tag (tag)
286 (let ((vl (mapcar 'string-to-number (split-string (nth 2 tag) "\\."))))
287 (cvs-tag-make vl (nth 0 tag) (intern (nth 1 tag)))))
289 (defun cvs-tags->tree (tags)
290 "Make a tree out of a list of TAGS."
291 (let ((tags
292 (mapcar (lambda (tag)
293 (let ((tag (cvs-tag-make-tag tag)))
294 (list (if (not (eq (cvs-tag->type tag) 'branch)) tag
295 (list (cvs-tag-make (butlast (cvs-tag->vlist tag)))
296 tag)))))
297 tags)))
298 (while (cdr tags)
299 (let (tl)
300 (while tags
301 (push (cvs-tree-merge (pop tags) (pop tags)) tl))
302 (setq tags (nreverse tl))))
303 (car tags)))
305 (defun cvs-status-get-tags ()
306 "Look for a list of tags, read them in and delete them.
307 Returns NIL if there was an empty list of tags and T if there wasn't
308 even a list. Else, return the list of tags where each element of
309 the list is a three-string list TAG, KIND, REV."
310 (let ((tags nil))
311 (if (not (re-search-forward cvs-status-tags-leader-re nil t)) t
312 (forward-char 1)
313 (let ((pt (point))
314 (lastrev nil)
315 (case-fold-search t))
317 (looking-at "\\s-+no\\s-+tags")
319 (progn ; normal listing
320 (while (looking-at "^[ \t]+\\([^ \t\n]+\\)[ \t]+(\\([a-z]+\\): \\(.+\\))$")
321 (push (list (match-string 1) (match-string 2) (match-string 3)) tags)
322 (forward-line 1))
323 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
324 tags)
326 (progn ; cvstree-style listing
327 (while (or (looking-at "^ .+\\(.\\) \\([0-9.]+\\): \\([^\n\t .0-9][^\n\t ]*\\)?$")
328 (and lastrev
329 (looking-at "^ .+\\(\\) \\(8\\)? \\([^\n\t .0-9][^\n\t ]*\\)$")))
330 (setq lastrev (or (match-string 2) lastrev))
331 (push (list (match-string 3)
332 (if (equal (match-string 1) " ") "branch" "revision")
333 lastrev) tags)
334 (forward-line 1))
335 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
336 (setq tags (nreverse tags)))
338 (progn ; new tree style listing
339 (let* ((re-lead "[ \t]*\\(-+\\)?\\(|\n?[ \t]+\\)?")
340 (re3 (concat re-lead "\\(\\.\\)?\\(" cvs-status-rev-re "\\)"))
341 (re2 (concat re-lead cvs-status-tag-re "\\(\\)"))
342 (re1 (concat re-lead cvs-status-tag-re
343 " (\\(" cvs-status-rev-re "\\))")))
344 (while (or (looking-at re1) (looking-at re2) (looking-at re3))
345 (push (list (match-string 3)
346 (if (match-string 1) "branch" "revision")
347 (match-string 4)) tags)
348 (goto-char (match-end 0))
349 (when (eolp) (forward-char 1))))
350 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
351 (setq tags (nreverse tags))))
353 (delete-region pt (point)))
354 tags)))
356 (defvar font-lock-mode)
357 (defun cvs-refontify (beg end)
358 (when (and (boundp 'font-lock-mode)
359 font-lock-mode
360 (fboundp 'font-lock-fontify-region))
361 (font-lock-fontify-region (1- beg) (1+ end))))
363 (defun cvs-status-trees ()
364 "Look for a lists of tags, and replace them with trees."
365 (interactive)
366 (save-excursion
367 (goto-char (point-min))
368 (let ((inhibit-read-only t)
369 (tags nil))
370 (while (listp (setq tags (cvs-status-get-tags)))
371 ;;(let ((pt (save-excursion (forward-line -1) (point))))
372 (save-restriction
373 (narrow-to-region (point) (point))
374 ;;(newline)
375 (cvs-tree-print (cvs-tags->tree tags) 'cvs-tag->string 3))
376 ;;(cvs-refontify pt (point))
377 (sit-for 0)
379 ))))
381 ;;;;
382 ;;;; CVSTree-style trees
383 ;;;;
385 ;; chars sets. Ripped from cvstree
386 (defvar cvs-tree-dstr-2byte-ready
387 (when (featurep 'mule)
388 (if (boundp 'current-language-environment)
389 (string= current-language-environment "Japanese")
390 t)) ; mule/emacs-19
391 "*Variable that specifies characters set used in cvstree tree graph.
392 If non-nil, 2byte (Japanese?) characters set is used.
393 If nil, 1byte characters set is used.
394 2byte characters might be available with Mule or Emacs with Mule extension.")
396 (defconst cvs-tree-dstr-char-space
397 (if cvs-tree-dstr-2byte-ready "\e$B!!\e(B" " "))
398 (defconst cvs-tree-dstr-char-hbar
399 (if cvs-tree-dstr-2byte-ready "\e$B(,\e(B" "--"))
400 (defconst cvs-tree-dstr-char-vbar
401 (if cvs-tree-dstr-2byte-ready "\e$B(-\e(B" "| "))
402 (defconst cvs-tree-dstr-char-branch
403 (if cvs-tree-dstr-2byte-ready "\e$B(2\e(B" "+-"))
404 (defconst cvs-tree-dstr-char-eob ;end of branch
405 (if cvs-tree-dstr-2byte-ready "\e$B(1\e(B" "`-"))
406 (defconst cvs-tree-dstr-char-bob ;beginning of branch
407 (if cvs-tree-dstr-2byte-ready "\e$B(3\e(B" "+-"))
409 (defun cvs-tag-lessp (tag1 tag2)
410 (eq (cvs-tag-compare tag1 tag2) 'more2))
412 (defvar cvs-tree-nomerge nil)
414 (defun cvs-status-cvstrees (&optional arg)
415 "Look for a list of tags, and replace it with a tree.
416 Optional prefix ARG chooses between two representations."
417 (interactive "P")
418 (save-excursion
419 (goto-char (point-min))
420 (let ((inhibit-read-only t)
421 (tags nil)
422 (cvs-tree-nomerge (if arg (not cvs-tree-nomerge) cvs-tree-nomerge)))
423 (while (listp (setq tags (cvs-status-get-tags)))
424 (let ((tags (mapcar 'cvs-tag-make-tag tags))
425 ;;(pt (save-excursion (forward-line -1) (point)))
427 (setq tags (sort tags 'cvs-tag-lessp))
428 (let* ((first (nth 0 tags))
429 (prev (if (cvs-tag-p first)
430 (list (nth 0 (cvs-tag->vlist first))) nil)))
431 (cvs-tree-tags-insert tags prev)
432 ;;(cvs-refontify pt (point))
433 (sit-for 0)))))))
435 (defun cvs-tree-tags-insert (tags prev)
436 (when tags
437 (let* ((tag (car tags))
438 (vlist (cvs-tag->vlist tag))
439 (nprev ;"next prev"
440 (let* ((next (cvs-car (cadr tags)))
441 (nprev (if (and cvs-tree-nomerge next
442 (equal vlist (cvs-tag->vlist next)))
443 prev vlist)))
444 (cvs-map (lambda (v p) v) nprev prev)))
445 (after (save-excursion
446 (newline)
447 (cvs-tree-tags-insert (cdr tags) nprev)))
448 (pe t) ;"prev equal"
449 (nas nil)) ;"next afters" to be returned
450 (insert " ")
451 (do* ((vs vlist (cdr vs))
452 (ps prev (cdr ps))
453 (as after (cdr as)))
454 ((and (null as) (null vs) (null ps))
455 (let ((revname (cvs-status-vl-to-str vlist)))
456 (if (cvs-every 'identity (cvs-map 'equal prev vlist))
457 (insert (make-string (+ 4 (length revname)) ? )
458 (or (cvs-tag->name tag) ""))
459 (insert " " revname ": " (or (cvs-tag->name tag) "")))))
460 (let* ((eq (and pe (equal (car ps) (car vs))))
461 (next-eq (equal (cadr ps) (cadr vs))))
462 (let* ((na+char
463 (if (car as)
464 (if eq
465 (if next-eq (cons t cvs-tree-dstr-char-vbar)
466 (cons t cvs-tree-dstr-char-branch))
467 (cons nil cvs-tree-dstr-char-bob))
468 (if eq
469 (if next-eq (cons nil cvs-tree-dstr-char-space)
470 (cons t cvs-tree-dstr-char-eob))
471 (cons nil (if (and (eq (cvs-tag->type tag) 'branch)
472 (cvs-every 'null as))
473 cvs-tree-dstr-char-space
474 cvs-tree-dstr-char-hbar))))))
475 (insert (cdr na+char))
476 (push (car na+char) nas))
477 (setq pe eq)))
478 (nreverse nas))))
480 ;;;;
481 ;;;; Merged trees from different files
482 ;;;;
484 (defun cvs-tree-fuzzy-merge-1 (trees tree prev)
487 (defun cvs-tree-fuzzy-merge (trees tree)
488 "Do the impossible: merge TREE into TREES."
491 (defun cvs-tree ()
492 "Get tags from the status output and merge tham all into a big tree."
493 (save-excursion
494 (goto-char (point-min))
495 (let ((inhibit-read-only t)
496 (trees (make-vector 31 0)) tree)
497 (while (listp (setq tree (cvs-tags->tree (cvs-status-get-tags))))
498 (cvs-tree-fuzzy-merge trees tree))
499 (erase-buffer)
500 (let ((cvs-tag-print-rev nil))
501 (cvs-tree-print tree 'cvs-tag->string 3)))))
504 (provide 'cvs-status)
506 ;;; Change Log:
507 ;; $Log: cvs-status.el,v $
508 ;; Revision 1.4 2000/05/10 22:08:28 monnier
509 ;; (cvs-status-minor-wrap): Use mark-active.
511 ;; Revision 1.3 2000/03/22 01:08:08 monnier
512 ;; (cvs-status-mode): Use define-derived-mode.
514 ;; Revision 1.2 2000/03/22 01:01:36 monnier
515 ;; (cvs-status-(prev|next)): Rename from
516 ;; cvs-status-(prev|next)-entry and use easy-mmode-define-navigation.
517 ;; (cvs-tree-dstr-*): Rename from cvstree-dstr-* and use two ascii chars
518 ;; to let the output "breathe" a little more (more readable).
521 ;;; cvs-status.el ends here