Merged in changes from CVS trunk.
[bpt/emacs.git] / lisp / cvs-status.el
CommitLineData
5ebfa0ab 1;;; cvs-status.el --- major mode for browsing `cvs status' output -*- coding: utf-8 -*-
5b467bf4 2
0efd0b3d 3;; Copyright (C) 1999, 2000, 03, 2004 Free Software Foundation, Inc.
5b467bf4
SM
4
5;; Author: Stefan Monnier <monnier@cs.yale.edu>
337bfec7 6;; Keywords: pcl-cvs cvs status tree tools
5b467bf4
SM
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;; Todo:
28
5b467bf4
SM
29;; - Somehow allow cvs-status-tree to work on-the-fly
30
31;;; Code:
32
33(eval-when-compile (require 'cl))
34(require 'pcvs-util)
35
36;;;
37
38(defgroup cvs-status nil
39 "Major mode for browsing `cvs status' output."
40 :group 'pcl-cvs
41 :prefix "cvs-status-")
42
43(easy-mmode-defmap cvs-status-mode-map
44 '(("n" . next-line)
5b467bf4 45 ("p" . previous-line)
184c5091
SM
46 ("N" . cvs-status-next)
47 ("P" . cvs-status-prev)
48 ("\M-n" . cvs-status-next)
49 ("\M-p" . cvs-status-prev)
5b467bf4
SM
50 ("t" . cvs-status-cvstrees)
51 ("T" . cvs-status-trees))
52 "CVS-Status' keymap."
53 :group 'cvs-status
54 :inherit 'cvs-mode-map)
55
56;;(easy-menu-define cvs-status-menu cvs-status-mode-map
57;; "Menu for `cvs-status-mode'."
58;; '("CVS-Status"
59;; ["Show Tag Trees" cvs-status-tree t]
60;; ))
61
62(defvar cvs-status-mode-hook nil
63 "Hook run at the end of `cvs-status-mode'.")
64
65(defconst cvs-status-tags-leader-re "^ Existing Tags:$")
ad710d9f
SM
66(defconst cvs-status-entry-leader-re
67 "^File:\\s-+\\(?:no file \\)?\\(.*\\S-\\)\\s-+Status: \\(.+\\)$")
5b467bf4
SM
68(defconst cvs-status-dir-re "^cvs[.ex]* [a-z]+: Examining \\(.+\\)$")
69(defconst cvs-status-rev-re "[0-9][.0-9]*\\.[.0-9]*[0-9]")
70(defconst cvs-status-tag-re "[ \t]\\([a-zA-Z][^ \t\n.]*\\)")
71
72(defconst cvs-status-font-lock-keywords
73 `((,cvs-status-entry-leader-re
74 (1 'cvs-filename-face)
75 (2 'cvs-need-action-face))
76 (,cvs-status-tags-leader-re
77 (,cvs-status-rev-re
78 (save-excursion (re-search-forward "^\n" nil 'move) (point))
79 (progn (re-search-backward cvs-status-tags-leader-re nil t)
80 (forward-line 1))
81 (0 font-lock-comment-face))
82 (,cvs-status-tag-re
83 (save-excursion (re-search-forward "^\n" nil 'move) (point))
84 (progn (re-search-backward cvs-status-tags-leader-re nil t)
85 (forward-line 1))
86 (1 font-lock-function-name-face)))))
87(defconst cvs-status-font-lock-defaults
c68088c2 88 '(cvs-status-font-lock-keywords t nil nil nil (font-lock-multiline . t)))
71296446 89
5b467bf4
SM
90
91(put 'cvs-status-mode 'mode-class 'special)
92;;;###autoload
43e56cba 93(define-derived-mode cvs-status-mode fundamental-mode "CVS-Status"
5b467bf4
SM
94 "Mode used for cvs status output."
95 (set (make-local-variable 'font-lock-defaults) cvs-status-font-lock-defaults)
96 (set (make-local-variable 'cvs-minor-wrap-function) 'cvs-status-minor-wrap))
97
184c5091
SM
98;; Define cvs-status-next and cvs-status-prev
99(easy-mmode-define-navigation cvs-status cvs-status-entry-leader-re "entry")
5b467bf4
SM
100
101(defun cvs-status-current-file ()
102 (save-excursion
103 (forward-line 1)
104 (or (re-search-backward cvs-status-entry-leader-re nil t)
105 (re-search-forward cvs-status-entry-leader-re))
106 (let* ((file (match-string 1))
107 (cvsdir (and (re-search-backward cvs-status-dir-re nil t)
108 (match-string 1)))
109 (pcldir (and (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
110 (match-string 1)))
111 (dir ""))
112 (let ((default-directory ""))
113 (when pcldir (setq dir (expand-file-name pcldir dir)))
114 (when cvsdir (setq dir (expand-file-name cvsdir dir)))
115 (expand-file-name file dir)))))
116
117(defun cvs-status-current-tag ()
118 (save-excursion
119 (let ((pt (point))
120 (col (current-column))
121 (start (progn (re-search-backward cvs-status-tags-leader-re nil t) (point)))
122 (end (progn (re-search-forward "^$" nil t) (point))))
123 (when (and (< start pt) (> end pt))
124 (goto-char pt)
125 (end-of-line)
126 (let ((tag nil) (dist pt) (end (point)))
127 (beginning-of-line)
128 (while (re-search-forward cvs-status-tag-re end t)
129 (let* ((cole (current-column))
130 (colb (save-excursion
131 (goto-char (match-beginning 1)) (current-column)))
132 (ndist (min (abs (- cole col)) (abs (- colb col)))))
133 (when (< ndist dist)
134 (setq dist ndist)
135 (setq tag (match-string 1)))))
136 tag)))))
137
138(defun cvs-status-minor-wrap (buf f)
139 (let ((data (with-current-buffer buf
140 (cons
141 (cons (cvs-status-current-file)
142 (cvs-status-current-tag))
befe763f 143 (when mark-active
5b467bf4
SM
144 (save-excursion
145 (goto-char (mark))
146 (cons (cvs-status-current-file)
147 (cvs-status-current-tag))))))))
148 (let ((cvs-branch-prefix (cdar data))
149 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
150 (cvs-minor-current-files
151 (cons (caar data)
152 (when (and (cadr data) (not (equal (caar data) (cadr data))))
153 (list (cadr data)))))
154 ;; FIXME: I need to force because the fileinfos are UNKNOWN
155 (cvs-force-command "/F"))
156 (funcall f))))
157
158;;
159;; Tagelt, tag element
160;;
161
162(defstruct (cvs-tag
163 (:constructor nil)
164 (:constructor cvs-tag-make
165 (vlist &optional name type))
166 (:conc-name cvs-tag->))
167 vlist
168 name
169 type)
170
171(defsubst cvs-status-vl-to-str (vl) (mapconcat 'number-to-string vl "."))
172
173(defun cvs-tag->string (tag)
174 (if (stringp tag) tag
175 (let ((name (cvs-tag->name tag))
176 (vl (cvs-tag->vlist tag)))
177 (if (null name) (cvs-status-vl-to-str vl)
178 (let ((rev (if vl (concat " (" (cvs-status-vl-to-str vl) ")") "")))
179 (if (consp name) (mapcar (lambda (name) (concat name rev)) name)
180 (concat name rev)))))))
181
182(defun cvs-tag-compare-1 (vl1 vl2)
183 (cond
184 ((and (null vl1) (null vl2)) 'equal)
185 ((null vl1) 'more2)
186 ((null vl2) 'more1)
187 (t (let ((v1 (car vl1))
188 (v2 (car vl2)))
189 (cond
190 ((> v1 v2) 'more1)
191 ((< v1 v2) 'more2)
192 (t (cvs-tag-compare-1 (cdr vl1) (cdr vl2))))))))
193
194(defsubst cvs-tag-compare (tag1 tag2)
195 (cvs-tag-compare-1 (cvs-tag->vlist tag1) (cvs-tag->vlist tag2)))
196
197(defun cvs-tag-merge (tag1 tag2)
198 "Merge TAG1 and TAG2 into one."
199 (let ((type1 (cvs-tag->type tag1))
200 (type2 (cvs-tag->type tag2))
201 (name1 (cvs-tag->name tag1))
202 (name2 (cvs-tag->name tag2)))
203 (unless (equal (cvs-tag->vlist tag1) (cvs-tag->vlist tag2))
204 (setf (cvs-tag->vlist tag1) nil))
205 (if type1
206 (unless (or (not type2) (equal type1 type2))
207 (setf (cvs-tag->type tag1) nil))
208 (setf (cvs-tag->type tag1) type2))
209 (if name1
210 (setf (cvs-tag->name tag1) (cvs-append name1 name2))
211 (setf (cvs-tag->name tag1) name2))
212 tag1))
213
214(defun cvs-tree-print (tags printer column)
215 "Print the tree of TAGS where each tag's string is given by PRINTER.
216PRINTER should accept both a tag (in which case it should return a string)
217or a string (in which case it should simply return its argument).
218A tag cannot be a CONS. The return value can also be a list of strings,
219if several nodes where merged into one.
220The tree will be printed no closer than column COLUMN."
71296446 221
5b467bf4
SM
222 (let* ((eol (save-excursion (end-of-line) (current-column)))
223 (column (max (+ eol 2) column)))
224 (if (null tags) column
225 ;;(move-to-column-force column)
226 (let* ((rev (cvs-car tags))
227 (name (funcall printer (cvs-car rev)))
228 (rest (append (cvs-cdr name) (cvs-cdr tags)))
229 (prefix
230 (save-excursion
231 (or (= (forward-line 1) 0) (insert "\n"))
232 (cvs-tree-print rest printer column))))
233 (assert (>= prefix column))
234 (move-to-column prefix t)
235 (assert (eolp))
236 (insert (cvs-car name))
237 (dolist (br (cvs-cdr rev))
238 (let* ((column (current-column))
239 (brrev (funcall printer (cvs-car br)))
240 (brlength (length (cvs-car brrev)))
241 (brfill (concat (make-string (/ brlength 2) ? ) "|"))
242 (prefix
243 (save-excursion
244 (insert " -- ")
245 (cvs-tree-print (cvs-append brrev brfill (cvs-cdr br))
246 printer (current-column)))))
247 (delete-region (save-excursion (move-to-column prefix) (point))
248 (point))
249 (insert " " (make-string (- prefix column 2) ?-) " ")
250 (end-of-line)))
251 prefix))))
252
253(defun cvs-tree-merge (tree1 tree2)
254 "Merge tags trees TREE1 and TREE2 into one.
255BEWARE: because of stability issues, this is not a symetric operation."
256 (assert (and (listp tree1) (listp tree2)))
257 (cond
258 ((null tree1) tree2)
259 ((null tree2) tree1)
260 (t
261 (let* ((rev1 (car tree1))
262 (tag1 (cvs-car rev1))
263 (vl1 (cvs-tag->vlist tag1))
264 (l1 (length vl1))
265 (rev2 (car tree2))
266 (tag2 (cvs-car rev2))
267 (vl2 (cvs-tag->vlist tag2))
268 (l2 (length vl2)))
269 (cond
270 ((= l1 l2)
271 (case (cvs-tag-compare tag1 tag2)
272 (more1 (list* rev2 (cvs-tree-merge tree1 (cdr tree2))))
273 (more2 (list* rev1 (cvs-tree-merge (cdr tree1) tree2)))
274 (equal
275 (cons (cons (cvs-tag-merge tag1 tag2)
276 (cvs-tree-merge (cvs-cdr rev1) (cvs-cdr rev2)))
277 (cvs-tree-merge (cdr tree1) (cdr tree2))))))
278 ((> l1 l2)
c68088c2
SM
279 (cvs-tree-merge
280 (list (cons (cvs-tag-make (cvs-butlast vl1)) tree1)) tree2))
5b467bf4 281 ((< l1 l2)
c68088c2
SM
282 (cvs-tree-merge
283 tree1 (list (cons (cvs-tag-make (cvs-butlast vl2)) tree2)))))))))
5b467bf4
SM
284
285(defun cvs-tag-make-tag (tag)
dedffa6a
GM
286 (let ((vl (mapcar 'string-to-number (split-string (nth 2 tag) "\\."))))
287 (cvs-tag-make vl (nth 0 tag) (intern (nth 1 tag)))))
5b467bf4
SM
288
289(defun cvs-tags->tree (tags)
290 "Make a tree out of a list of TAGS."
291 (let ((tags
c68088c2
SM
292 (mapcar
293 (lambda (tag)
294 (let ((tag (cvs-tag-make-tag tag)))
295 (list (if (not (eq (cvs-tag->type tag) 'branch)) tag
296 (list (cvs-tag-make (cvs-butlast (cvs-tag->vlist tag)))
297 tag)))))
298 tags)))
5b467bf4
SM
299 (while (cdr tags)
300 (let (tl)
301 (while tags
302 (push (cvs-tree-merge (pop tags) (pop tags)) tl))
303 (setq tags (nreverse tl))))
304 (car tags)))
305
306(defun cvs-status-get-tags ()
307 "Look for a list of tags, read them in and delete them.
f0529b5b 308Return nil if there was an empty list of tags and t if there wasn't
5b467bf4
SM
309even a list. Else, return the list of tags where each element of
310the list is a three-string list TAG, KIND, REV."
311 (let ((tags nil))
312 (if (not (re-search-forward cvs-status-tags-leader-re nil t)) t
313 (forward-char 1)
314 (let ((pt (point))
315 (lastrev nil)
316 (case-fold-search t))
317 (or
318 (looking-at "\\s-+no\\s-+tags")
319
320 (progn ; normal listing
321 (while (looking-at "^[ \t]+\\([^ \t\n]+\\)[ \t]+(\\([a-z]+\\): \\(.+\\))$")
322 (push (list (match-string 1) (match-string 2) (match-string 3)) tags)
323 (forward-line 1))
324 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
325 tags)
326
327 (progn ; cvstree-style listing
328 (while (or (looking-at "^ .+\\(.\\) \\([0-9.]+\\): \\([^\n\t .0-9][^\n\t ]*\\)?$")
329 (and lastrev
330 (looking-at "^ .+\\(\\) \\(8\\)? \\([^\n\t .0-9][^\n\t ]*\\)$")))
331 (setq lastrev (or (match-string 2) lastrev))
332 (push (list (match-string 3)
333 (if (equal (match-string 1) " ") "branch" "revision")
334 lastrev) tags)
335 (forward-line 1))
336 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
337 (setq tags (nreverse tags)))
338
339 (progn ; new tree style listing
c68088c2 340 (let* ((re-lead "[ \t]*\\(-+\\)?\\(|\n?[ \t]+\\)*")
5b467bf4
SM
341 (re3 (concat re-lead "\\(\\.\\)?\\(" cvs-status-rev-re "\\)"))
342 (re2 (concat re-lead cvs-status-tag-re "\\(\\)"))
343 (re1 (concat re-lead cvs-status-tag-re
344 " (\\(" cvs-status-rev-re "\\))")))
345 (while (or (looking-at re1) (looking-at re2) (looking-at re3))
346 (push (list (match-string 3)
347 (if (match-string 1) "branch" "revision")
348 (match-string 4)) tags)
349 (goto-char (match-end 0))
350 (when (eolp) (forward-char 1))))
351 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
352 (setq tags (nreverse tags))))
353
354 (delete-region pt (point)))
355 tags)))
356
357(defvar font-lock-mode)
358(defun cvs-refontify (beg end)
359 (when (and (boundp 'font-lock-mode)
360 font-lock-mode
361 (fboundp 'font-lock-fontify-region))
362 (font-lock-fontify-region (1- beg) (1+ end))))
363
364(defun cvs-status-trees ()
365 "Look for a lists of tags, and replace them with trees."
366 (interactive)
367 (save-excursion
368 (goto-char (point-min))
369 (let ((inhibit-read-only t)
370 (tags nil))
371 (while (listp (setq tags (cvs-status-get-tags)))
372 ;;(let ((pt (save-excursion (forward-line -1) (point))))
373 (save-restriction
374 (narrow-to-region (point) (point))
375 ;;(newline)
c68088c2
SM
376 (combine-after-change-calls
377 (cvs-tree-print (cvs-tags->tree tags) 'cvs-tag->string 3)))
5b467bf4 378 ;;(cvs-refontify pt (point))
c68088c2 379 ;;(sit-for 0)
5b467bf4
SM
380 ;;)
381 ))))
382
c68088c2 383;;;;
5b467bf4 384;;;; CVSTree-style trees
c68088c2
SM
385;;;;
386
5ebfa0ab
SM
387(defvar cvs-tree-use-jisx0208 nil) ;Old compat var.
388(defvar cvs-tree-use-charset
389 (cond
390 (cvs-tree-use-jisx0208 'jisx0208)
391 ((char-displayable-p ?━) 'unicode)
392 ((char-displayable-p (make-char 'japanese-jisx0208 40 44)) 'jisx0208))
c68088c2
SM
393 "*Non-nil if we should use the graphical glyphs from `japanese-jisx0208'.
394Otherwise, default to ASCII chars like +, - and |.")
395
396(defconst cvs-tree-char-space
5ebfa0ab
SM
397 (case cvs-tree-use-charset
398 (jisx0208 (make-char 'japanese-jisx0208 33 33))
399 (unicode " ")
400 (t " ")))
c68088c2 401(defconst cvs-tree-char-hbar
5ebfa0ab
SM
402 (case cvs-tree-use-charset
403 (jisx0208 (make-char 'japanese-jisx0208 40 44))
404 (unicode "━")
405 (t "--")))
c68088c2 406(defconst cvs-tree-char-vbar
5ebfa0ab
SM
407 (case cvs-tree-use-charset
408 (jisx0208 (make-char 'japanese-jisx0208 40 45))
409 (unicode "┃")
410 (t "| ")))
c68088c2 411(defconst cvs-tree-char-branch
5ebfa0ab
SM
412 (case cvs-tree-use-charset
413 (jisx0208 (make-char 'japanese-jisx0208 40 50))
414 (unicode "┣")
415 (t "+-")))
c68088c2 416(defconst cvs-tree-char-eob ;end of branch
5ebfa0ab
SM
417 (case cvs-tree-use-charset
418 (jisx0208 (make-char 'japanese-jisx0208 40 49))
419 (unicode "┗")
420 (t "`-")))
c68088c2 421(defconst cvs-tree-char-bob ;beginning of branch
5ebfa0ab
SM
422 (case cvs-tree-use-charset
423 (jisx0208 (make-char 'japanese-jisx0208 40 51))
424 (unicode "┳")
425 (t "+-")))
5b467bf4
SM
426
427(defun cvs-tag-lessp (tag1 tag2)
428 (eq (cvs-tag-compare tag1 tag2) 'more2))
429
184c5091 430(defvar cvs-tree-nomerge nil)
5b467bf4
SM
431
432(defun cvs-status-cvstrees (&optional arg)
433 "Look for a list of tags, and replace it with a tree.
434Optional prefix ARG chooses between two representations."
435 (interactive "P")
5ebfa0ab 436 (when (and cvs-tree-use-charset
c68088c2
SM
437 (not enable-multibyte-characters))
438 ;; We need to convert the buffer from unibyte to multibyte
439 ;; since we'll use multibyte chars for the tree.
440 (let ((modified (buffer-modified-p))
441 (inhibit-read-only t)
442 (inhibit-modification-hooks t))
443 (unwind-protect
444 (progn
445 (decode-coding-region (point-min) (point-max) 'undecided)
446 (set-buffer-multibyte t))
447 (restore-buffer-modified-p modified))))
5b467bf4
SM
448 (save-excursion
449 (goto-char (point-min))
450 (let ((inhibit-read-only t)
451 (tags nil)
452 (cvs-tree-nomerge (if arg (not cvs-tree-nomerge) cvs-tree-nomerge)))
453 (while (listp (setq tags (cvs-status-get-tags)))
454 (let ((tags (mapcar 'cvs-tag-make-tag tags))
455 ;;(pt (save-excursion (forward-line -1) (point)))
456 )
457 (setq tags (sort tags 'cvs-tag-lessp))
7382bcae 458 (let* ((first (car tags))
5b467bf4 459 (prev (if (cvs-tag-p first)
7382bcae 460 (list (car (cvs-tag->vlist first))) nil)))
c68088c2
SM
461 (combine-after-change-calls
462 (cvs-tree-tags-insert tags prev))
5b467bf4 463 ;;(cvs-refontify pt (point))
c68088c2
SM
464 ;;(sit-for 0)
465 ))))))
5b467bf4
SM
466
467(defun cvs-tree-tags-insert (tags prev)
468 (when tags
469 (let* ((tag (car tags))
470 (vlist (cvs-tag->vlist tag))
471 (nprev ;"next prev"
472 (let* ((next (cvs-car (cadr tags)))
473 (nprev (if (and cvs-tree-nomerge next
474 (equal vlist (cvs-tag->vlist next)))
475 prev vlist)))
476 (cvs-map (lambda (v p) v) nprev prev)))
477 (after (save-excursion
478 (newline)
479 (cvs-tree-tags-insert (cdr tags) nprev)))
480 (pe t) ;"prev equal"
481 (nas nil)) ;"next afters" to be returned
482 (insert " ")
483 (do* ((vs vlist (cdr vs))
484 (ps prev (cdr ps))
485 (as after (cdr as)))
486 ((and (null as) (null vs) (null ps))
487 (let ((revname (cvs-status-vl-to-str vlist)))
488 (if (cvs-every 'identity (cvs-map 'equal prev vlist))
489 (insert (make-string (+ 4 (length revname)) ? )
490 (or (cvs-tag->name tag) ""))
491 (insert " " revname ": " (or (cvs-tag->name tag) "")))))
492 (let* ((eq (and pe (equal (car ps) (car vs))))
493 (next-eq (equal (cadr ps) (cadr vs))))
494 (let* ((na+char
495 (if (car as)
496 (if eq
c68088c2
SM
497 (if next-eq (cons t cvs-tree-char-vbar)
498 (cons t cvs-tree-char-branch))
499 (cons nil cvs-tree-char-bob))
5b467bf4 500 (if eq
c68088c2
SM
501 (if next-eq (cons nil cvs-tree-char-space)
502 (cons t cvs-tree-char-eob))
5b467bf4
SM
503 (cons nil (if (and (eq (cvs-tag->type tag) 'branch)
504 (cvs-every 'null as))
c68088c2
SM
505 cvs-tree-char-space
506 cvs-tree-char-hbar))))))
5b467bf4
SM
507 (insert (cdr na+char))
508 (push (car na+char) nas))
509 (setq pe eq)))
510 (nreverse nas))))
511
71296446 512;;;;
5b467bf4 513;;;; Merged trees from different files
71296446 514;;;;
5b467bf4
SM
515
516(defun cvs-tree-fuzzy-merge-1 (trees tree prev)
517 )
518
519(defun cvs-tree-fuzzy-merge (trees tree)
520 "Do the impossible: merge TREE into TREES."
521 ())
522
523(defun cvs-tree ()
524 "Get tags from the status output and merge tham all into a big tree."
525 (save-excursion
526 (goto-char (point-min))
527 (let ((inhibit-read-only t)
528 (trees (make-vector 31 0)) tree)
529 (while (listp (setq tree (cvs-tags->tree (cvs-status-get-tags))))
530 (cvs-tree-fuzzy-merge trees tree))
531 (erase-buffer)
532 (let ((cvs-tag-print-rev nil))
533 (cvs-tree-print tree 'cvs-tag->string 3)))))
71296446 534
5b467bf4
SM
535
536(provide 'cvs-status)
537
ab5796a9 538;;; arch-tag: db8b5094-d02a-473e-a476-544e89ff5ad0
5b467bf4 539;;; cvs-status.el ends here