]> code.delx.au - gnu-emacs/blob - lisp/vc/cvs-status.el
cf1f49cbeee71f549362efe77cf037013e54b6ac
[gnu-emacs] / lisp / vc / cvs-status.el
1 ;;; cvs-status.el --- major mode for browsing `cvs status' output -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1999-2015 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-lib))
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 (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)
95
96 (put 'cvs-status-mode 'mode-class 'special)
97 ;;;###autoload
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))
102
103 ;; Define cvs-status-next and cvs-status-prev
104 (easy-mmode-define-navigation cvs-status cvs-status-entry-leader-re "entry")
105
106 (defun cvs-status-current-file ()
107 (save-excursion
108 (forward-line 1)
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)
113 (match-string 1)))
114 (pcldir (and (if (boundp 'cvs-pcl-cvs-dirchange-re)
115 (re-search-backward cvs-pcl-cvs-dirchange-re nil t))
116 (match-string 1)))
117 (dir ""))
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)))))
122
123 (defun cvs-status-current-tag ()
124 (save-excursion
125 (let ((pt (point))
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))
130 (goto-char pt)
131 (end-of-line)
132 (let ((tag nil) (dist pt) (end (point)))
133 (beginning-of-line)
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)))))
139 (when (< ndist dist)
140 (setq dist ndist)
141 (setq tag (match-string 1)))))
142 tag)))))
143
144 (defun cvs-status-minor-wrap (buf f)
145 (let ((data (with-current-buffer buf
146 (cons
147 (cons (cvs-status-current-file)
148 (cvs-status-current-tag))
149 (when mark-active
150 (save-excursion
151 (goto-char (mark))
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
157 (cons (caar data)
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"))
162 (funcall f))))
163
164 ;;
165 ;; Tagelt, tag element
166 ;;
167
168 (cl-defstruct (cvs-tag
169 (:constructor nil)
170 (:constructor cvs-tag-make
171 (vlist &optional name type))
172 (:conc-name cvs-tag->))
173 vlist
174 name
175 type)
176
177 (defsubst cvs-status-vl-to-str (vl) (mapconcat 'number-to-string vl "."))
178
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)))))))
187
188 (defun cvs-tag-compare-1 (vl1 vl2)
189 (cond
190 ((and (null vl1) (null vl2)) 'equal)
191 ((null vl1) 'more2)
192 ((null vl2) 'more1)
193 (t (let ((v1 (car vl1))
194 (v2 (car vl2)))
195 (cond
196 ((> v1 v2) 'more1)
197 ((< v1 v2) 'more2)
198 (t (cvs-tag-compare-1 (cdr vl1) (cdr vl2))))))))
199
200 (defsubst cvs-tag-compare (tag1 tag2)
201 (cvs-tag-compare-1 (cvs-tag->vlist tag1) (cvs-tag->vlist tag2)))
202
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))
211 (if type1
212 (unless (or (not type2) (equal type1 type2))
213 (setf (cvs-tag->type tag1) nil))
214 (setf (cvs-tag->type tag1) type2))
215 (if name1
216 (setf (cvs-tag->name tag1) (cvs-append name1 name2))
217 (setf (cvs-tag->name tag1) name2))
218 tag1))
219
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."
227
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)))
234 (prefix
235 (save-excursion
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)
240 (cl-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 symmetric operation."
261 (cl-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 (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)))
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
285 (list (cons (cvs-tag-make (butlast vl1)) tree1)) tree2))
286 ((< l1 l2)
287 (cvs-tree-merge
288 tree1 (list (cons (cvs-tag-make (butlast vl2)) tree2)))))))))
289
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)))))
293
294 (defun cvs-tags->tree (tags)
295 "Make a tree out of a list of TAGS."
296 (let ((tags
297 (mapcar
298 (lambda (tag)
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)))
302 tag)))))
303 tags)))
304 (while (cdr tags)
305 (let (tl)
306 (while tags
307 (push (cvs-tree-merge (pop tags) (pop tags)) tl))
308 (setq tags (nreverse tl))))
309 (car tags)))
310
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."
316 (let ((tags nil))
317 (if (not (re-search-forward cvs-status-tags-leader-re nil t)) t
318 (forward-char 1)
319 (let ((pt (point))
320 (lastrev nil)
321 (case-fold-search t))
322 (or
323 (looking-at "\\s-+no\\s-+tags")
324
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)
328 (forward-line 1))
329 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
330 tags)
331
332 (progn ; cvstree-style listing
333 (while (or (looking-at "^ .+\\(.\\) \\([0-9.]+\\): \\([^\n\t .0-9][^\n\t ]*\\)?$")
334 (and lastrev
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")
339 lastrev) tags)
340 (forward-line 1))
341 (unless (looking-at "^$") (setq tags nil) (goto-char pt))
342 (setq tags (nreverse tags)))
343
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))))
358
359 (delete-region pt (point)))
360 tags)))
361
362 (defvar font-lock-mode)
363 ;; (defun cvs-refontify (beg end)
364 ;; (when (and (boundp 'font-lock-mode)
365 ;; font-lock-mode
366 ;; (fboundp 'font-lock-fontify-region))
367 ;; (font-lock-fontify-region (1- beg) (1+ end))))
368
369 (defun cvs-status-trees ()
370 "Look for a lists of tags, and replace them with trees."
371 (interactive)
372 (save-excursion
373 (goto-char (point-min))
374 (let ((inhibit-read-only t)
375 (tags nil))
376 (while (listp (setq tags (cvs-status-get-tags)))
377 ;;(let ((pt (save-excursion (forward-line -1) (point))))
378 (save-restriction
379 (narrow-to-region (point) (point))
380 ;;(newline)
381 (combine-after-change-calls
382 (cvs-tree-print (cvs-tags->tree tags) 'cvs-tag->string 3)))
383 ;;(cvs-refontify pt (point))
384 ;;(sit-for 0)
385 ;;)
386 ))))
387
388 ;;;;
389 ;;;; CVSTree-style trees
390 ;;;;
391
392 (defvar cvs-tree-use-jisx0208 nil) ;Old compat var.
393 (defvar cvs-tree-use-charset
394 (cond
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 |.")
400
401 (defconst cvs-tree-char-space
402 (pcase cvs-tree-use-charset
403 (`jisx0208 (make-char 'japanese-jisx0208 33 33))
404 (`unicode " ")
405 (_ " ")))
406 (defconst cvs-tree-char-hbar
407 (pcase cvs-tree-use-charset
408 (`jisx0208 (make-char 'japanese-jisx0208 40 44))
409 (`unicode "━")
410 (_ "--")))
411 (defconst cvs-tree-char-vbar
412 (pcase cvs-tree-use-charset
413 (`jisx0208 (make-char 'japanese-jisx0208 40 45))
414 (`unicode "┃")
415 (_ "| ")))
416 (defconst cvs-tree-char-branch
417 (pcase cvs-tree-use-charset
418 (`jisx0208 (make-char 'japanese-jisx0208 40 50))
419 (`unicode "┣")
420 (_ "+-")))
421 (defconst cvs-tree-char-eob ;end of branch
422 (pcase cvs-tree-use-charset
423 (`jisx0208 (make-char 'japanese-jisx0208 40 49))
424 (`unicode "┗")
425 (_ "`-")))
426 (defconst cvs-tree-char-bob ;beginning of branch
427 (pcase cvs-tree-use-charset
428 (`jisx0208 (make-char 'japanese-jisx0208 40 51))
429 (`unicode "┳")
430 (_ "+-")))
431
432 (defun cvs-tag-lessp (tag1 tag2)
433 (eq (cvs-tag-compare tag1 tag2) 'more2))
434
435 (defvar cvs-tree-nomerge nil)
436
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."
440 (interactive "P")
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))
448 (unwind-protect
449 (progn
450 (decode-coding-region (point-min) (point-max) 'undecided)
451 (set-buffer-multibyte t))
452 (restore-buffer-modified-p modified))))
453 (save-excursion
454 (goto-char (point-min))
455 (let ((inhibit-read-only t)
456 (tags nil)
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)))
461 )
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))
469 ;;(sit-for 0)
470 ))))))
471
472 (defun cvs-tree-tags-insert (tags prev)
473 (when tags
474 (let* ((tag (car tags))
475 (vlist (cvs-tag->vlist tag))
476 (nprev ;"next prev"
477 (let* ((next (cvs-car (cadr tags)))
478 (nprev (if (and cvs-tree-nomerge next
479 (equal vlist (cvs-tag->vlist next)))
480 prev vlist)))
481 (cvs-map (lambda (v _p) v) nprev prev)))
482 (after (save-excursion
483 (newline)
484 (cvs-tree-tags-insert (cdr tags) nprev)))
485 (pe t) ;"prev equal"
486 (nas nil)) ;"next afters" to be returned
487 (insert " ")
488 (cl-do* ((vs vlist (cdr vs))
489 (ps prev (cdr ps))
490 (as after (cdr as)))
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))))
499 (let* ((na+char
500 (if (car as)
501 (if eq
502 (if next-eq (cons t cvs-tree-char-vbar)
503 (cons t cvs-tree-char-branch))
504 (cons nil cvs-tree-char-bob))
505 (if eq
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))
510 cvs-tree-char-space
511 cvs-tree-char-hbar))))))
512 (insert (cdr na+char))
513 (push (car na+char) nas))
514 (setq pe eq)))
515 (nreverse nas))))
516
517 ;;;;
518 ;;;; Merged trees from different files
519 ;;;;
520
521 ;; (defun cvs-tree-fuzzy-merge-1 (trees tree prev)
522 ;; )
523
524 ;; (defun cvs-tree-fuzzy-merge (trees tree)
525 ;; "Do the impossible: merge TREE into TREES."
526 ;; ())
527
528 ;; (defun cvs-tree ()
529 ;; "Get tags from the status output and merge them all into a big tree."
530 ;; (save-excursion
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))
536 ;; (erase-buffer)
537 ;; (let ((cvs-tag-print-rev nil))
538 ;; (cvs-tree-print tree 'cvs-tag->string 3)))))
539
540
541 (provide 'cvs-status)
542
543 ;;; cvs-status.el ends here