1 ;;; cvs-status.el --- major mode for browsing `cvs status' output -*- coding: utf-8; lexical-binding: t -*-
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: pcl-cvs cvs status tree vc tools
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;; - Somehow allow cvs-status-tree to work on-the-fly
31 (eval-when-compile (require 'cl-lib))
36 (defgroup cvs-status nil
37 "Major mode for browsing `cvs status' output."
39 :prefix "cvs-status-")
41 (easy-mmode-defmap cvs-status-mode-map
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))
53 :inherit 'cvs-mode-map)
55 ;;(easy-menu-define cvs-status-menu cvs-status-mode-map
56 ;; "Menu for `cvs-status-mode'."
58 ;; ["Show Tag Trees" cvs-status-tree t]
61 (defvar cvs-status-mode-hook nil
62 "Hook run at the end of `cvs-status-mode'.")
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.]*\\)")
71 (defconst cvs-status-font-lock-keywords
72 `((,cvs-status-entry-leader-re
75 (,cvs-status-tags-leader-re
77 (save-excursion (re-search-forward "^\n" nil 'move) (point))
78 (progn (re-search-backward cvs-status-tags-leader-re nil t)
80 (0 font-lock-comment-face))
82 (save-excursion (re-search-forward "^\n" nil 'move) (point))
83 (progn (re-search-backward cvs-status-tags-leader-re nil t)
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)))
89 (defvar cvs-minor-wrap-function)
90 (defvar cvs-force-command)
91 (defvar cvs-minor-current-files)
92 (defvar cvs-secondary-branch-prefix)
93 (defvar cvs-branch-prefix)
94 (defvar cvs-tag-print-rev)
96 (put 'cvs-status-mode 'mode-class 'special)
98 (define-derived-mode cvs-status-mode fundamental-mode "CVS-Status"
99 "Mode used for cvs status output."
100 (set (make-local-variable 'font-lock-defaults) cvs-status-font-lock-defaults)
101 (set (make-local-variable 'cvs-minor-wrap-function) 'cvs-status-minor-wrap))
103 ;; Define cvs-status-next and cvs-status-prev
104 (easy-mmode-define-navigation cvs-status cvs-status-entry-leader-re "entry")
106 (defun cvs-status-current-file ()
109 (or (re-search-backward cvs-status-entry-leader-re nil t)
110 (re-search-forward cvs-status-entry-leader-re))
111 (let* ((file (match-string 1))
112 (cvsdir (and (re-search-backward cvs-status-dir-re nil t)
114 (pcldir (and (if (boundp 'cvs-pcl-cvs-dirchange-re)
115 (re-search-backward cvs-pcl-cvs-dirchange-re nil t))
118 (let ((default-directory ""))
119 (when pcldir (setq dir (expand-file-name pcldir dir)))
120 (when cvsdir (setq dir (expand-file-name cvsdir dir)))
121 (expand-file-name file dir)))))
123 (defun cvs-status-current-tag ()
126 (col (current-column))
127 (start (progn (re-search-backward cvs-status-tags-leader-re nil t) (point)))
128 (end (progn (re-search-forward "^$" nil t) (point))))
129 (when (and (< start pt) (> end pt))
132 (let ((tag nil) (dist pt) (end (point)))
134 (while (re-search-forward cvs-status-tag-re end t)
135 (let* ((cole (current-column))
136 (colb (save-excursion
137 (goto-char (match-beginning 1)) (current-column)))
138 (ndist (min (abs (- cole col)) (abs (- colb col)))))
141 (setq tag (match-string 1)))))
144 (defun cvs-status-minor-wrap (buf f)
145 (let ((data (with-current-buffer buf
147 (cons (cvs-status-current-file)
148 (cvs-status-current-tag))
152 (cons (cvs-status-current-file)
153 (cvs-status-current-tag))))))))
154 (let ((cvs-branch-prefix (cdar data))
155 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
156 (cvs-minor-current-files
158 (when (and (cadr data) (not (equal (caar data) (cadr data))))
159 (list (cadr data)))))
160 ;; FIXME: I need to force because the fileinfos are UNKNOWN
161 (cvs-force-command "/F"))
165 ;; Tagelt, tag element
168 (cl-defstruct (cvs-tag
170 (:constructor cvs-tag-make
171 (vlist &optional name type))
172 (:conc-name cvs-tag->))
177 (defsubst cvs-status-vl-to-str (vl) (mapconcat 'number-to-string vl "."))
179 (defun cvs-tag->string (tag)
180 (if (stringp tag) tag
181 (let ((name (cvs-tag->name tag))
182 (vl (cvs-tag->vlist tag)))
183 (if (null name) (cvs-status-vl-to-str vl)
184 (let ((rev (if vl (concat " (" (cvs-status-vl-to-str vl) ")") "")))
185 (if (consp name) (mapcar (lambda (name) (concat name rev)) name)
186 (concat name rev)))))))
188 (defun cvs-tag-compare-1 (vl1 vl2)
190 ((and (null vl1) (null vl2)) 'equal)
193 (t (let ((v1 (car vl1))
198 (t (cvs-tag-compare-1 (cdr vl1) (cdr vl2))))))))
200 (defsubst cvs-tag-compare (tag1 tag2)
201 (cvs-tag-compare-1 (cvs-tag->vlist tag1) (cvs-tag->vlist tag2)))
203 (defun cvs-tag-merge (tag1 tag2)
204 "Merge TAG1 and TAG2 into one."
205 (let ((type1 (cvs-tag->type tag1))
206 (type2 (cvs-tag->type tag2))
207 (name1 (cvs-tag->name tag1))
208 (name2 (cvs-tag->name tag2)))
209 (unless (equal (cvs-tag->vlist tag1) (cvs-tag->vlist tag2))
210 (setf (cvs-tag->vlist tag1) nil))
212 (unless (or (not type2) (equal type1 type2))
213 (setf (cvs-tag->type tag1) nil))
214 (setf (cvs-tag->type tag1) type2))
216 (setf (cvs-tag->name tag1) (cvs-append name1 name2))
217 (setf (cvs-tag->name tag1) name2))
220 (defun cvs-tree-print (tags printer column)
221 "Print the tree of TAGS where each tag's string is given by PRINTER.
222 PRINTER should accept both a tag (in which case it should return a string)
223 or a string (in which case it should simply return its argument).
224 A tag cannot be a CONS. The return value can also be a list of strings,
225 if several nodes where merged into one.
226 The tree will be printed no closer than column COLUMN."
228 (let* ((eol (save-excursion (end-of-line) (current-column)))
229 (column (max (+ eol 2) column)))
230 (if (null tags) 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 (cl-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 symmetric operation."
261 (cl-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 (pcase (cvs-tag-compare tag1 tag2)
277 (`more1 (cons rev2 (cvs-tree-merge tree1 (cdr tree2))))
278 (`more2 (cons 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))))))
285 (list (cons (cvs-tag-make (butlast vl1)) tree1)) tree2))
288 tree1 (list (cons (cvs-tag-make (butlast vl2)) tree2)))))))))
290 (defun cvs-tag-make-tag (tag)
291 (let ((vl (mapcar 'string-to-number (split-string (nth 2 tag) "\\."))))
292 (cvs-tag-make vl (nth 0 tag) (intern (nth 1 tag)))))
294 (defun cvs-tags->tree (tags)
295 "Make a tree out of a list of TAGS."
299 (let ((tag (cvs-tag-make-tag tag)))
300 (list (if (not (eq (cvs-tag->type tag) 'branch)) tag
301 (list (cvs-tag-make (butlast (cvs-tag->vlist tag)))
307 (push (cvs-tree-merge (pop tags) (pop tags)) tl))
308 (setq tags (nreverse tl))))
311 (defun cvs-status-get-tags ()
312 "Look for a list of tags, read them in and delete them.
313 Return nil if there was an empty list of tags and t if there wasn't
314 even a list. Else, return the list of tags where each element of
315 the list is a three-string list TAG, KIND, REV."
317 (if (not (re-search-forward cvs-status-tags-leader-re nil t)) t
321 (case-fold-search t))
323 (looking-at "\\s-+no\\s-+tags")
325 (progn ; normal listing
326 (while (looking-at "^[ \t]+\\([^ \t\n]+\\)[ \t]+(\\([a-z]+\\): \\(.+\\))$")
327 (push (list (match-string 1) (match-string 2) (match-string 3)) tags)
329 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
332 (progn ; cvstree-style listing
333 (while (or (looking-at "^ .+\\(.\\) \\([0-9.]+\\): \\([^\n\t .0-9][^\n\t ]*\\)?$")
335 (looking-at "^ .+\\(\\) \\(8\\)? \\([^\n\t .0-9][^\n\t ]*\\)$")))
336 (setq lastrev (or (match-string 2) lastrev))
337 (push (list (match-string 3)
338 (if (equal (match-string 1) " ") "branch" "revision")
341 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
342 (setq tags (nreverse tags)))
344 (progn ; new tree style listing
345 (let* ((re-lead "[ \t]*\\(-+\\)?\\(|\n?[ \t]+\\)*")
346 (re3 (concat re-lead "\\(\\.\\)?\\(" cvs-status-rev-re "\\)"))
347 (re2 (concat re-lead cvs-status-tag-re "\\(\\)"))
348 (re1 (concat re-lead cvs-status-tag-re
349 " (\\(" cvs-status-rev-re "\\))")))
350 (while (or (looking-at re1) (looking-at re2) (looking-at re3))
351 (push (list (match-string 3)
352 (if (match-string 1) "branch" "revision")
353 (match-string 4)) tags)
354 (goto-char (match-end 0))
355 (when (eolp) (forward-char 1))))
356 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
357 (setq tags (nreverse tags))))
359 (delete-region pt (point)))
362 (defvar font-lock-mode)
363 ;; (defun cvs-refontify (beg end)
364 ;; (when (and (boundp 'font-lock-mode)
366 ;; (fboundp 'font-lock-fontify-region))
367 ;; (font-lock-fontify-region (1- beg) (1+ end))))
369 (defun cvs-status-trees ()
370 "Look for a lists of tags, and replace them with trees."
373 (goto-char (point-min))
374 (let ((inhibit-read-only t)
376 (while (listp (setq tags (cvs-status-get-tags)))
377 ;;(let ((pt (save-excursion (forward-line -1) (point))))
379 (narrow-to-region (point) (point))
381 (combine-after-change-calls
382 (cvs-tree-print (cvs-tags->tree tags) 'cvs-tag->string 3)))
383 ;;(cvs-refontify pt (point))
389 ;;;; CVSTree-style trees
392 (defvar cvs-tree-use-jisx0208 nil) ;Old compat var.
393 (defvar cvs-tree-use-charset
395 (cvs-tree-use-jisx0208 'jisx0208)
396 ((char-displayable-p ?━) 'unicode)
397 ((char-displayable-p (make-char 'japanese-jisx0208 40 44)) 'jisx0208))
398 "Non-nil if we should use the graphical glyphs from `japanese-jisx0208'.
399 Otherwise, default to ASCII chars like +, - and |.")
401 (defconst cvs-tree-char-space
402 (pcase cvs-tree-use-charset
403 (`jisx0208 (make-char 'japanese-jisx0208 33 33))
406 (defconst cvs-tree-char-hbar
407 (pcase cvs-tree-use-charset
408 (`jisx0208 (make-char 'japanese-jisx0208 40 44))
411 (defconst cvs-tree-char-vbar
412 (pcase cvs-tree-use-charset
413 (`jisx0208 (make-char 'japanese-jisx0208 40 45))
416 (defconst cvs-tree-char-branch
417 (pcase cvs-tree-use-charset
418 (`jisx0208 (make-char 'japanese-jisx0208 40 50))
421 (defconst cvs-tree-char-eob ;end of branch
422 (pcase cvs-tree-use-charset
423 (`jisx0208 (make-char 'japanese-jisx0208 40 49))
426 (defconst cvs-tree-char-bob ;beginning of branch
427 (pcase cvs-tree-use-charset
428 (`jisx0208 (make-char 'japanese-jisx0208 40 51))
432 (defun cvs-tag-lessp (tag1 tag2)
433 (eq (cvs-tag-compare tag1 tag2) 'more2))
435 (defvar cvs-tree-nomerge nil)
437 (defun cvs-status-cvstrees (&optional arg)
438 "Look for a list of tags, and replace it with a tree.
439 Optional prefix ARG chooses between two representations."
441 (when (and cvs-tree-use-charset
442 (not enable-multibyte-characters))
443 ;; We need to convert the buffer from unibyte to multibyte
444 ;; since we'll use multibyte chars for the tree.
445 (let ((modified (buffer-modified-p))
446 (inhibit-read-only t)
447 (inhibit-modification-hooks t))
450 (decode-coding-region (point-min) (point-max) 'undecided)
451 (set-buffer-multibyte t))
452 (restore-buffer-modified-p modified))))
454 (goto-char (point-min))
455 (let ((inhibit-read-only t)
457 (cvs-tree-nomerge (if arg (not cvs-tree-nomerge) cvs-tree-nomerge)))
458 (while (listp (setq tags (cvs-status-get-tags)))
459 (let ((tags (mapcar 'cvs-tag-make-tag tags))
460 ;;(pt (save-excursion (forward-line -1) (point)))
462 (setq tags (sort tags 'cvs-tag-lessp))
463 (let* ((first (car tags))
464 (prev (if (cvs-tag-p first)
465 (list (car (cvs-tag->vlist first))) nil)))
466 (combine-after-change-calls
467 (cvs-tree-tags-insert tags prev))
468 ;;(cvs-refontify pt (point))
472 (defun cvs-tree-tags-insert (tags prev)
474 (let* ((tag (car tags))
475 (vlist (cvs-tag->vlist tag))
477 (let* ((next (cvs-car (cadr tags)))
478 (nprev (if (and cvs-tree-nomerge next
479 (equal vlist (cvs-tag->vlist next)))
481 (cvs-map (lambda (v _p) v) nprev prev)))
482 (after (save-excursion
484 (cvs-tree-tags-insert (cdr tags) nprev)))
486 (nas nil)) ;"next afters" to be returned
488 (cl-do* ((vs vlist (cdr vs))
491 ((and (null as) (null vs) (null ps))
492 (let ((revname (cvs-status-vl-to-str vlist)))
493 (if (cvs-every 'identity (cvs-map 'equal prev vlist))
494 (insert (make-string (+ 4 (length revname)) ? )
495 (or (cvs-tag->name tag) ""))
496 (insert " " revname ": " (or (cvs-tag->name tag) "")))))
497 (let* ((eq (and pe (equal (car ps) (car vs))))
498 (next-eq (equal (cadr ps) (cadr vs))))
502 (if next-eq (cons t cvs-tree-char-vbar)
503 (cons t cvs-tree-char-branch))
504 (cons nil cvs-tree-char-bob))
506 (if next-eq (cons nil cvs-tree-char-space)
507 (cons t cvs-tree-char-eob))
508 (cons nil (if (and (eq (cvs-tag->type tag) 'branch)
509 (cvs-every 'null as))
511 cvs-tree-char-hbar))))))
512 (insert (cdr na+char))
513 (push (car na+char) nas))
518 ;;;; Merged trees from different files
521 ;; (defun cvs-tree-fuzzy-merge-1 (trees tree prev)
524 ;; (defun cvs-tree-fuzzy-merge (trees tree)
525 ;; "Do the impossible: merge TREE into TREES."
528 ;; (defun cvs-tree ()
529 ;; "Get tags from the status output and merge them all into a big tree."
531 ;; (goto-char (point-min))
532 ;; (let ((inhibit-read-only t)
533 ;; (trees (make-vector 31 0)) tree)
534 ;; (while (listp (setq tree (cvs-tags->tree (cvs-status-get-tags))))
535 ;; (cvs-tree-fuzzy-merge trees tree))
537 ;; (let ((cvs-tag-print-rev nil))
538 ;; (cvs-tree-print tree 'cvs-tag->string 3)))))
541 (provide 'cvs-status)
543 ;;; cvs-status.el ends here