]> code.delx.au - gnu-emacs/blob - lisp/vc/cvs-status.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / vc / cvs-status.el
1 ;;; cvs-status.el --- major mode for browsing `cvs status' output -*- coding: utf-8 -*-
2
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: pcl-cvs cvs status tree vc tools
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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Todo:
26
27 ;; - Somehow allow cvs-status-tree to work on-the-fly
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (require 'pcvs-util)
33
34 ;;;
35
36 (defgroup cvs-status nil
37 "Major mode for browsing `cvs status' output."
38 :group 'pcl-cvs
39 :prefix "cvs-status-")
40
41 (easy-mmode-defmap cvs-status-mode-map
42 '(("n" . next-line)
43 ("p" . previous-line)
44 ("N" . cvs-status-next)
45 ("P" . cvs-status-prev)
46 ("\M-n" . cvs-status-next)
47 ("\M-p" . cvs-status-prev)
48 ("t" . cvs-status-cvstrees)
49 ("T" . cvs-status-trees)
50 (">" . cvs-mode-checkout))
51 "CVS-Status' keymap."
52 :group 'cvs-status
53 :inherit 'cvs-mode-map)
54
55 ;;(easy-menu-define cvs-status-menu cvs-status-mode-map
56 ;; "Menu for `cvs-status-mode'."
57 ;; '("CVS-Status"
58 ;; ["Show Tag Trees" cvs-status-tree t]
59 ;; ))
60
61 (defvar cvs-status-mode-hook nil
62 "Hook run at the end of `cvs-status-mode'.")
63
64 (defconst cvs-status-tags-leader-re "^ Existing Tags:$")
65 (defconst cvs-status-entry-leader-re
66 "^File:\\s-+\\(?:no file \\)?\\(.*\\S-\\)\\s-+Status: \\(.+\\)$")
67 (defconst cvs-status-dir-re "^cvs[.ex]* [a-z]+: Examining \\(.+\\)$")
68 (defconst cvs-status-rev-re "[0-9][.0-9]*\\.[.0-9]*[0-9]")
69 (defconst cvs-status-tag-re "[ \t]\\([a-zA-Z][^ \t\n.]*\\)")
70
71 (defconst cvs-status-font-lock-keywords
72 `((,cvs-status-entry-leader-re
73 (1 'cvs-filename)
74 (2 'cvs-need-action))
75 (,cvs-status-tags-leader-re
76 (,cvs-status-rev-re
77 (save-excursion (re-search-forward "^\n" nil 'move) (point))
78 (progn (re-search-backward cvs-status-tags-leader-re nil t)
79 (forward-line 1))
80 (0 font-lock-comment-face))
81 (,cvs-status-tag-re
82 (save-excursion (re-search-forward "^\n" nil 'move) (point))
83 (progn (re-search-backward cvs-status-tags-leader-re nil t)
84 (forward-line 1))
85 (1 font-lock-function-name-face)))))
86 (defconst cvs-status-font-lock-defaults
87 '(cvs-status-font-lock-keywords t nil nil nil (font-lock-multiline . t)))
88
89 (defvar cvs-minor-wrap-function)
90 (put 'cvs-status-mode 'mode-class 'special)
91 ;;;###autoload
92 (define-derived-mode cvs-status-mode fundamental-mode "CVS-Status"
93 "Mode used for cvs status output."
94 (set (make-local-variable 'font-lock-defaults) cvs-status-font-lock-defaults)
95 (set (make-local-variable 'cvs-minor-wrap-function) 'cvs-status-minor-wrap))
96
97 ;; Define cvs-status-next and cvs-status-prev
98 (easy-mmode-define-navigation cvs-status cvs-status-entry-leader-re "entry")
99
100 (defun cvs-status-current-file ()
101 (save-excursion
102 (forward-line 1)
103 (or (re-search-backward cvs-status-entry-leader-re nil t)
104 (re-search-forward cvs-status-entry-leader-re))
105 (let* ((file (match-string 1))
106 (cvsdir (and (re-search-backward cvs-status-dir-re nil t)
107 (match-string 1)))
108 (pcldir (and (if (boundp 'cvs-pcl-cvs-dirchange-re)
109 (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))
143 (when mark-active
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.
216 PRINTER should accept both a tag (in which case it should return a string)
217 or a string (in which case it should simply return its argument).
218 A tag cannot be a CONS. The return value can also be a list of strings,
219 if several nodes where merged into one.
220 The tree will be printed no closer than column COLUMN."
221
222 (let* ((eol (save-excursion (end-of-line) (current-column)))
223 (column (max (+ eol 2) column)))
224 (if (null tags) column
225 (let* ((rev (cvs-car tags))
226 (name (funcall printer (cvs-car rev)))
227 (rest (append (cvs-cdr name) (cvs-cdr tags)))
228 (prefix
229 (save-excursion
230 (or (= (forward-line 1) 0) (insert "\n"))
231 (cvs-tree-print rest printer column))))
232 (assert (>= prefix column))
233 (move-to-column prefix t)
234 (assert (eolp))
235 (insert (cvs-car name))
236 (dolist (br (cvs-cdr rev))
237 (let* ((column (current-column))
238 (brrev (funcall printer (cvs-car br)))
239 (brlength (length (cvs-car brrev)))
240 (brfill (concat (make-string (/ brlength 2) ? ) "|"))
241 (prefix
242 (save-excursion
243 (insert " -- ")
244 (cvs-tree-print (cvs-append brrev brfill (cvs-cdr br))
245 printer (current-column)))))
246 (delete-region (save-excursion (move-to-column prefix) (point))
247 (point))
248 (insert " " (make-string (- prefix column 2) ?-) " ")
249 (end-of-line)))
250 prefix))))
251
252 (defun cvs-tree-merge (tree1 tree2)
253 "Merge tags trees TREE1 and TREE2 into one.
254 BEWARE: because of stability issues, this is not a symetric operation."
255 (assert (and (listp tree1) (listp tree2)))
256 (cond
257 ((null tree1) tree2)
258 ((null tree2) tree1)
259 (t
260 (let* ((rev1 (car tree1))
261 (tag1 (cvs-car rev1))
262 (vl1 (cvs-tag->vlist tag1))
263 (l1 (length vl1))
264 (rev2 (car tree2))
265 (tag2 (cvs-car rev2))
266 (vl2 (cvs-tag->vlist tag2))
267 (l2 (length vl2)))
268 (cond
269 ((= l1 l2)
270 (case (cvs-tag-compare tag1 tag2)
271 (more1 (list* rev2 (cvs-tree-merge tree1 (cdr tree2))))
272 (more2 (list* rev1 (cvs-tree-merge (cdr tree1) tree2)))
273 (equal
274 (cons (cons (cvs-tag-merge tag1 tag2)
275 (cvs-tree-merge (cvs-cdr rev1) (cvs-cdr rev2)))
276 (cvs-tree-merge (cdr tree1) (cdr tree2))))))
277 ((> l1 l2)
278 (cvs-tree-merge
279 (list (cons (cvs-tag-make (butlast vl1)) tree1)) tree2))
280 ((< l1 l2)
281 (cvs-tree-merge
282 tree1 (list (cons (cvs-tag-make (butlast vl2)) tree2)))))))))
283
284 (defun cvs-tag-make-tag (tag)
285 (let ((vl (mapcar 'string-to-number (split-string (nth 2 tag) "\\."))))
286 (cvs-tag-make vl (nth 0 tag) (intern (nth 1 tag)))))
287
288 (defun cvs-tags->tree (tags)
289 "Make a tree out of a list of TAGS."
290 (let ((tags
291 (mapcar
292 (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)))
304
305 (defun cvs-status-get-tags ()
306 "Look for a list of tags, read them in and delete them.
307 Return 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))
316 (or
317 (looking-at "\\s-+no\\s-+tags")
318
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)
325
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)))
337
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))))
352
353 (delete-region pt (point)))
354 tags)))
355
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))))
362
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 (combine-after-change-calls
376 (cvs-tree-print (cvs-tags->tree tags) 'cvs-tag->string 3)))
377 ;;(cvs-refontify pt (point))
378 ;;(sit-for 0)
379 ;;)
380 ))))
381
382 ;;;;
383 ;;;; CVSTree-style trees
384 ;;;;
385
386 (defvar cvs-tree-use-jisx0208 nil) ;Old compat var.
387 (defvar cvs-tree-use-charset
388 (cond
389 (cvs-tree-use-jisx0208 'jisx0208)
390 ((char-displayable-p ?━) 'unicode)
391 ((char-displayable-p (make-char 'japanese-jisx0208 40 44)) 'jisx0208))
392 "*Non-nil if we should use the graphical glyphs from `japanese-jisx0208'.
393 Otherwise, default to ASCII chars like +, - and |.")
394
395 (defconst cvs-tree-char-space
396 (case cvs-tree-use-charset
397 (jisx0208 (make-char 'japanese-jisx0208 33 33))
398 (unicode " ")
399 (t " ")))
400 (defconst cvs-tree-char-hbar
401 (case cvs-tree-use-charset
402 (jisx0208 (make-char 'japanese-jisx0208 40 44))
403 (unicode "━")
404 (t "--")))
405 (defconst cvs-tree-char-vbar
406 (case cvs-tree-use-charset
407 (jisx0208 (make-char 'japanese-jisx0208 40 45))
408 (unicode "┃")
409 (t "| ")))
410 (defconst cvs-tree-char-branch
411 (case cvs-tree-use-charset
412 (jisx0208 (make-char 'japanese-jisx0208 40 50))
413 (unicode "┣")
414 (t "+-")))
415 (defconst cvs-tree-char-eob ;end of branch
416 (case cvs-tree-use-charset
417 (jisx0208 (make-char 'japanese-jisx0208 40 49))
418 (unicode "┗")
419 (t "`-")))
420 (defconst cvs-tree-char-bob ;beginning of branch
421 (case cvs-tree-use-charset
422 (jisx0208 (make-char 'japanese-jisx0208 40 51))
423 (unicode "┳")
424 (t "+-")))
425
426 (defun cvs-tag-lessp (tag1 tag2)
427 (eq (cvs-tag-compare tag1 tag2) 'more2))
428
429 (defvar cvs-tree-nomerge nil)
430
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."
434 (interactive "P")
435 (when (and cvs-tree-use-charset
436 (not enable-multibyte-characters))
437 ;; We need to convert the buffer from unibyte to multibyte
438 ;; since we'll use multibyte chars for the tree.
439 (let ((modified (buffer-modified-p))
440 (inhibit-read-only t)
441 (inhibit-modification-hooks t))
442 (unwind-protect
443 (progn
444 (decode-coding-region (point-min) (point-max) 'undecided)
445 (set-buffer-multibyte t))
446 (restore-buffer-modified-p modified))))
447 (save-excursion
448 (goto-char (point-min))
449 (let ((inhibit-read-only t)
450 (tags nil)
451 (cvs-tree-nomerge (if arg (not cvs-tree-nomerge) cvs-tree-nomerge)))
452 (while (listp (setq tags (cvs-status-get-tags)))
453 (let ((tags (mapcar 'cvs-tag-make-tag tags))
454 ;;(pt (save-excursion (forward-line -1) (point)))
455 )
456 (setq tags (sort tags 'cvs-tag-lessp))
457 (let* ((first (car tags))
458 (prev (if (cvs-tag-p first)
459 (list (car (cvs-tag->vlist first))) nil)))
460 (combine-after-change-calls
461 (cvs-tree-tags-insert tags prev))
462 ;;(cvs-refontify pt (point))
463 ;;(sit-for 0)
464 ))))))
465
466 (defun cvs-tree-tags-insert (tags prev)
467 (when tags
468 (let* ((tag (car tags))
469 (vlist (cvs-tag->vlist tag))
470 (nprev ;"next prev"
471 (let* ((next (cvs-car (cadr tags)))
472 (nprev (if (and cvs-tree-nomerge next
473 (equal vlist (cvs-tag->vlist next)))
474 prev vlist)))
475 (cvs-map (lambda (v p) v) nprev prev)))
476 (after (save-excursion
477 (newline)
478 (cvs-tree-tags-insert (cdr tags) nprev)))
479 (pe t) ;"prev equal"
480 (nas nil)) ;"next afters" to be returned
481 (insert " ")
482 (do* ((vs vlist (cdr vs))
483 (ps prev (cdr ps))
484 (as after (cdr as)))
485 ((and (null as) (null vs) (null ps))
486 (let ((revname (cvs-status-vl-to-str vlist)))
487 (if (cvs-every 'identity (cvs-map 'equal prev vlist))
488 (insert (make-string (+ 4 (length revname)) ? )
489 (or (cvs-tag->name tag) ""))
490 (insert " " revname ": " (or (cvs-tag->name tag) "")))))
491 (let* ((eq (and pe (equal (car ps) (car vs))))
492 (next-eq (equal (cadr ps) (cadr vs))))
493 (let* ((na+char
494 (if (car as)
495 (if eq
496 (if next-eq (cons t cvs-tree-char-vbar)
497 (cons t cvs-tree-char-branch))
498 (cons nil cvs-tree-char-bob))
499 (if eq
500 (if next-eq (cons nil cvs-tree-char-space)
501 (cons t cvs-tree-char-eob))
502 (cons nil (if (and (eq (cvs-tag->type tag) 'branch)
503 (cvs-every 'null as))
504 cvs-tree-char-space
505 cvs-tree-char-hbar))))))
506 (insert (cdr na+char))
507 (push (car na+char) nas))
508 (setq pe eq)))
509 (nreverse nas))))
510
511 ;;;;
512 ;;;; Merged trees from different files
513 ;;;;
514
515 (defun cvs-tree-fuzzy-merge-1 (trees tree prev)
516 )
517
518 (defun cvs-tree-fuzzy-merge (trees tree)
519 "Do the impossible: merge TREE into TREES."
520 ())
521
522 (defun cvs-tree ()
523 "Get tags from the status output and merge tham all into a big tree."
524 (save-excursion
525 (goto-char (point-min))
526 (let ((inhibit-read-only t)
527 (trees (make-vector 31 0)) tree)
528 (while (listp (setq tree (cvs-tags->tree (cvs-status-get-tags))))
529 (cvs-tree-fuzzy-merge trees tree))
530 (erase-buffer)
531 (let ((cvs-tag-print-rev nil))
532 (cvs-tree-print tree 'cvs-tag->string 3)))))
533
534
535 (provide 'cvs-status)
536
537 ;;; cvs-status.el ends here