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