(gnus-configure-windows): switch-to-buffer -> set-buffer.
[emacs.git] / lisp / pcvs-info.el
bloba1e09a70e0d990113246d5f1684c0b6fe6361b60
1 ;;; pcvs-info.el --- Internal representation of a fileinfo entry
3 ;; Copyright (C) 1991-2000 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: pcl-cvs
7 ;; Version: $Name: $
8 ;; Revision: $Id: pcvs-info.el,v 1.3 2000/06/12 04:37:50 monnier Exp $
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; The cvs-fileinfo data structure:
31 ;; When the `cvs update' is ready we parse the output. Every file
32 ;; that is affected in some way is added to the cookie collection as
33 ;; a "fileinfo" (as defined below in cvs-create-fileinfo).
35 ;;; Code:
37 (eval-when-compile (require 'cl))
38 (require 'pcvs-util)
39 ;;(require 'pcvs-defs)
41 ;;;;
42 ;;;; config variables
43 ;;;;
45 (defcustom cvs-display-full-path t
46 "*Specifies how the filenames should look like in the listing.
47 If t, their full path name will be displayed, else only the filename."
48 :group 'pcl-cvs
49 :type '(boolean))
51 (defvar global-font-lock-mode)
52 (defvar font-lock-auto-fontify)
53 (defcustom cvs-highlight
54 (or (and (boundp 'font-lock-auto-fontify) font-lock-auto-fontify)
55 (and (boundp 'global-font-lock-mode) global-font-lock-mode))
56 "*Whether to use text highlighting (à la font-lock) or not."
57 :group 'pcl-cvs
58 :type '(boolean))
60 (defcustom cvs-allow-dir-commit nil
61 "*Allow `cvs-mode-commit' on directories.
62 If you commit without any marked file and with the cursor positioned
63 on a directory entry, cvs would commit the whole directory. This seems
64 to confuse some users sometimes."
65 :group 'pcl-cvs
66 :type '(boolean))
68 ;;;;
69 ;;;; Faces for fontification
70 ;;;;
72 (defface cvs-header-face
73 '((((class color) (background dark))
74 (:foreground "lightyellow" :bold t))
75 (((class color) (background light))
76 (:foreground "blue4" :bold t))
77 (t (:bold t)))
78 "PCL-CVS face used to highlight directory changes."
79 :group 'pcl-cvs)
81 (defface cvs-filename-face
82 '((((class color) (background dark))
83 (:foreground "lightblue"))
84 (((class color) (background light))
85 (:foreground "blue4"))
86 (t ()))
87 "PCL-CVS face used to highlight file names."
88 :group 'pcl-cvs)
90 (defface cvs-unknown-face
91 '((((class color) (background dark))
92 (:foreground "red"))
93 (((class color) (background light))
94 (:foreground "red"))
95 (t (:italic t)))
96 "PCL-CVS face used to highlight unknown file status."
97 :group 'pcl-cvs)
99 (defface cvs-handled-face
100 '((((class color) (background dark))
101 (:foreground "pink"))
102 (((class color) (background light))
103 (:foreground "pink"))
104 (t ()))
105 "PCL-CVS face used to highlight handled file status."
106 :group 'pcl-cvs)
108 (defface cvs-need-action-face
109 '((((class color) (background dark))
110 (:foreground "orange"))
111 (((class color) (background light))
112 (:foreground "orange"))
113 (t (:italic t)))
114 "PCL-CVS face used to highlight status of files needing action."
115 :group 'pcl-cvs)
117 (defface cvs-marked-face
118 '((((class color) (background dark))
119 (:foreground "green" :bold t))
120 (((class color) (background light))
121 (:foreground "green3" :bold t))
122 (t (:bold t)))
123 "PCL-CVS face used to highlight marked file indicator."
124 :group 'pcl-cvs)
126 (defface cvs-msg-face
127 '((t (:italic t)))
128 "PCL-CVS face used to highlight CVS messages."
129 :group 'pcl-cvs)
131 (defvar cvs-fi-up-to-date-face 'cvs-handled-face)
132 (defvar cvs-fi-unknown-face 'cvs-unknown-face)
133 (defvar cvs-fi-conflict-face 'font-lock-warning-face)
135 ;; There is normally no need to alter the following variable, but if
136 ;; your site has installed CVS in a non-standard way you might have
137 ;; to change it.
139 (defvar cvs-bakprefix ".#"
140 "The prefix that CVS prepends to files when rcsmerge'ing.")
142 (easy-mmode-defmap cvs-status-map
143 '(([(mouse-2)] . cvs-mouse-toggle-mark))
144 "Local keymap for text properties of status")
146 ;; Constructor:
148 (defstruct (cvs-fileinfo
149 (:constructor nil)
150 (:copier nil)
151 (:constructor -cvs-create-fileinfo (type dir file full-log
152 &key marked subtype
153 merge
154 base-rev
155 head-rev))
156 (:conc-name cvs-fileinfo->))
157 marked ;; t/nil.
158 type ;; See below
159 subtype ;; See below
160 dir ;; Relative directory the file resides in.
161 ;; (concat dir file) should give a valid path.
162 file ;; The file name sans the directory.
163 base-rev ;; During status: This is the revision that the
164 ;; working file is based on.
165 head-rev ;; During status: This is the highest revision in
166 ;; the repository.
167 merge ;; A cons cell containing the (ancestor . head) revisions
168 ;; of the merge that resulted in the current file.
169 ;;removed ;; t if the file no longer exists.
170 full-log ;; The output from cvs, unparsed.
171 ;;mod-time ;; Not used.
173 ;; In addition to the above, the following values can be extracted:
175 ;; handled ;; t if this file doesn't require further action.
176 ;; full-path ;; The complete relative filename.
177 ;; pp-name ;; The printed file name
178 ;; backup-file;; For MERGED and CONFLICT files after a \"cvs update\",
179 ;; this is a full path to the backup file where the
180 ;; untouched version resides.
182 ;; The meaning of the type field:
184 ;; Value ---Used by--- Explanation
185 ;; update status
186 ;; NEED-UPDATE x file needs update
187 ;; MODIFIED x x modified by you, unchanged in repository
188 ;; MERGED x x successful merge
189 ;; ADDED x x added by you, not yet committed
190 ;; MISSING x rm'd, but not yet `cvs remove'd
191 ;; REMOVED x x removed by you, not yet committed
192 ;; NEED-MERGE x need merge
193 ;; CONFLICT x conflict when merging
194 ;; ;;MOD-CONFLICT x removed locally, changed in repository.
195 ;; DIRCHANGE x x A change of directory.
196 ;; UNKNOWN x An unknown file.
197 ;; UP-TO-DATE x The file is up-to-date.
198 ;; UPDATED x x file copied from repository
199 ;; PATCHED x x diff applied from repository
200 ;; COMMITTED x x cvs commit'd
201 ;; DEAD An entry that should be removed
202 ;; MESSAGE x x This is a special fileinfo that is used
203 ;; to display a text that should be in
204 ;; full-log."
205 ;; TEMP A temporary message that should be removed
207 (defun cvs-create-fileinfo (type dir file msg &rest keys)
208 (cvs-check-fileinfo (apply #'-cvs-create-fileinfo type dir file msg keys)))
210 ;; Fake selectors:
212 (defun cvs-fileinfo->full-path (fileinfo)
213 "Return the full path for the file that is described in FILEINFO."
214 (let ((dir (cvs-fileinfo->dir fileinfo)))
215 (if (eq (cvs-fileinfo->type fileinfo) 'DIRCHANGE)
216 (if (string= dir "") "." (directory-file-name dir))
217 ;; Here, I use `concat' rather than `expand-file-name' because I want
218 ;; the resulting path to stay relative if `dir' is relative.
219 (concat dir (cvs-fileinfo->file fileinfo)))))
221 (defun cvs-fileinfo->pp-name (fi)
222 "Return the filename of FI as it should be displayed."
223 (if cvs-display-full-path
224 (cvs-fileinfo->full-path fi)
225 (cvs-fileinfo->file fi)))
227 (defun cvs-fileinfo->backup-file (fileinfo)
228 "Construct the file name of the backup file for FILEINFO."
229 (let* ((dir (cvs-fileinfo->dir fileinfo))
230 (file (cvs-fileinfo->file fileinfo))
231 (default-directory (file-name-as-directory (expand-file-name dir)))
232 (files (directory-files "." nil
233 (concat "^" (regexp-quote cvs-bakprefix)
234 (regexp-quote file) "\\.")))
236 (dolist (f files bf)
237 (when (and (file-readable-p f)
238 (or (null bf) (file-newer-than-file-p f bf)))
239 (setq bf (concat dir f))))))
241 ;; (defun cvs-fileinfo->handled (fileinfo)
242 ;; "Tell if this requires further action"
243 ;; (memq (cvs-fileinfo->type fileinfo) '(UP-TO-DATE DEAD)))
246 ;; Predicate:
248 (defun boolp (x) (or (eq t x) (null x)))
249 (defun cvs-check-fileinfo (fi)
250 "Check FI's conformance to some conventions."
251 (let ((check 'none)
252 (type (cvs-fileinfo->type fi))
253 (subtype (cvs-fileinfo->subtype fi))
254 (marked (cvs-fileinfo->marked fi))
255 (dir (cvs-fileinfo->dir fi))
256 (file (cvs-fileinfo->file fi))
257 (base-rev (cvs-fileinfo->base-rev fi))
258 (head-rev (cvs-fileinfo->head-rev fi))
259 (full-log (cvs-fileinfo->full-log fi)))
260 (if (and (setq check 'marked) (boolp marked)
261 (setq check 'base-rev) (or (null base-rev) (stringp base-rev))
262 (setq check 'head-rev) (or (null head-rev) (stringp head-rev))
263 (setq check 'full-log) (stringp full-log)
264 (setq check 'dir)
265 (and (stringp dir)
266 (not (file-name-absolute-p dir))
267 (or (string= dir "")
268 (string= dir (file-name-as-directory dir))))
269 (setq check 'file)
270 (and (stringp file)
271 (string= file (file-name-nondirectory file)))
272 (setq check 'type) (symbolp type)
273 (setq check 'consistency)
274 (case type
275 (DIRCHANGE (and (null subtype) (string= "." file)))
276 ((NEED-UPDATE ADDED MISSING DEAD MODIFIED MESSAGE UP-TO-DATE
277 REMOVED NEED-MERGE CONFLICT UNKNOWN MESSAGE)
278 t)))
280 (error "Invalid :%s in cvs-fileinfo %s" check fi))))
283 ;;;;
284 ;;;; State table to indicate what you can do when.
285 ;;;;
287 (defconst cvs-states
288 `((NEED-UPDATE update diff)
289 (UP-TO-DATE update nil remove diff safe-rm revert)
290 (MODIFIED update commit undo remove diff merge diff-base)
291 (ADDED update commit remove)
292 (MISSING remove undo update safe-rm revert)
293 (REMOVED commit add undo safe-rm)
294 (NEED-MERGE update undo diff diff-base)
295 (CONFLICT merge remove undo commit diff diff-base)
296 (DIRCHANGE remove update diff ,(if cvs-allow-dir-commit 'commit) tag)
297 (UNKNOWN ignore add remove)
298 (DEAD )
299 (MESSAGE))
300 "Fileinfo state descriptions for pcl-cvs.
301 This is an assoc list. Each element consists of (STATE . FUNS)
302 - STATE (described in `cvs-create-fileinfo') is the key
303 - FUNS is the list of applicable operations.
304 The first one (if any) should be the \"default\" action.
305 Most of the actions have the obvious meaning.
306 `safe-rm' indicates that the file can be removed without losing
307 any information.")
309 ;;;;
310 ;;;; Utility functions
311 ;;;;
313 (defun cvs-applicable-p (fi-or-type func)
314 "Check if FUNC is applicable to FI-OR-TYPE.
315 If FUNC is nil, always return t.
316 FI-OR-TYPE can either be a symbol (a fileinfo-type) or a fileinfo."
317 (let ((type (if (symbolp fi-or-type) fi-or-type
318 (cvs-fileinfo->type fi-or-type))))
319 (and (not (eq type 'MESSAGE))
320 (eq (car (memq func (cdr (assq type cvs-states)))) func))))
322 (defun cvs-add-face (str face &optional keymap)
323 (when cvs-highlight
324 (add-text-properties 0 (length str)
325 (list* 'face face
326 (when keymap
327 (list* 'mouse-face 'highlight
328 (when (keymapp keymap)
329 (list 'keymap keymap)))))
330 str))
331 str)
333 (defun cvs-fileinfo-pp (fileinfo)
334 "Pretty print FILEINFO. Insert a printed representation in current buffer.
335 For use by the cookie package."
336 (cvs-check-fileinfo fileinfo)
337 (let ((type (cvs-fileinfo->type fileinfo))
338 (subtype (cvs-fileinfo->subtype fileinfo)))
339 (insert
340 (case type
341 (DIRCHANGE (concat "In directory "
342 (cvs-add-face (cvs-fileinfo->full-path fileinfo)
343 'cvs-header-face t)
344 ":"))
345 (MESSAGE
346 (cvs-add-face (format "Message: %s" (cvs-fileinfo->full-log fileinfo))
347 'cvs-msg-face))
349 (let* ((status (if (cvs-fileinfo->marked fileinfo)
350 (cvs-add-face "*" 'cvs-marked-face)
351 " "))
352 (file (cvs-add-face (cvs-fileinfo->pp-name fileinfo)
353 'cvs-filename-face t))
354 (base (or (cvs-fileinfo->base-rev fileinfo) ""))
355 (head (cvs-fileinfo->head-rev fileinfo))
356 (type
357 (let ((str (case type
358 ;;(MOD-CONFLICT "Not Removed")
359 (DEAD "")
360 (t (capitalize (symbol-name type)))))
361 (face (let ((sym (intern
362 (concat "cvs-fi-"
363 (downcase (symbol-name type))
364 "-face"))))
365 (or (and (boundp sym) (symbol-value sym))
366 'cvs-need-action-face))))
367 (cvs-add-face str face cvs-status-map)))
368 (side (or
369 ;; maybe a subtype
370 (when subtype (downcase (symbol-name subtype)))
371 ;; or the head-rev
372 (when (and head (not (string= head base))) head)
373 ;; or nothing
374 "")))
375 (format "%-11s %s %-11s %-11s %s"
376 side status type base file)))))))
379 (defun cvs-fileinfo-update (fi fi-new)
380 "Update FI with the information provided in FI-NEW."
381 (let ((type (cvs-fileinfo->type fi-new))
382 (merge (cvs-fileinfo->merge fi-new)))
383 (setf (cvs-fileinfo->type fi) type)
384 (setf (cvs-fileinfo->subtype fi) (cvs-fileinfo->subtype fi-new))
385 (setf (cvs-fileinfo->full-log fi) (cvs-fileinfo->full-log fi-new))
386 (setf (cvs-fileinfo->base-rev fi) (cvs-fileinfo->base-rev fi-new))
387 (setf (cvs-fileinfo->head-rev fi) (cvs-fileinfo->head-rev fi-new))
388 (cond
389 (merge (setf (cvs-fileinfo->merge fi) merge))
390 ((memq type '(UP-TO-DATE NEED-UPDATE))
391 (setf (cvs-fileinfo->merge fi) nil)))))
393 (defun cvs-fileinfo< (a b)
394 "Compare fileinfo A with fileinfo B and return t if A is `less'.
395 The ordering defined by this function is such that directories are
396 sorted alphabetically, and inside every directory the DIRCHANGE
397 fileinfo will appear first, followed by all files (alphabetically)."
398 (let ((subtypea (cvs-fileinfo->subtype a))
399 (subtypeb (cvs-fileinfo->subtype b)))
400 (cond
401 ;; Sort according to directories.
402 ((string< (cvs-fileinfo->dir a) (cvs-fileinfo->dir b)) t)
403 ((not (string= (cvs-fileinfo->dir a) (cvs-fileinfo->dir b))) nil)
405 ;; The DIRCHANGE entry is always first within the directory.
406 ((eq (cvs-fileinfo->type b) 'DIRCHANGE) nil)
407 ((eq (cvs-fileinfo->type a) 'DIRCHANGE) t)
409 ;; All files are sorted by file name.
410 ((string< (cvs-fileinfo->file a) (cvs-fileinfo->file b))))))
413 ;;; Look at CVS/Entries to quickly find a first approximation of the status
416 (defun cvs-fileinfo-from-entries (dir &optional all)
417 "List of fileinfos for DIR, extracted from CVS/Entries.
418 Unless ALL is optional, returns only the files that are not up-to-date.
419 DIR can also be a file."
420 (let* ((singlefile
421 (cond
422 ((equal dir "") nil)
423 ((file-directory-p dir) (setq dir (file-name-as-directory dir)) nil)
424 (t (prog1 (file-name-nondirectory dir)
425 (setq dir (or (file-name-directory dir) ""))))))
426 (file (expand-file-name "CVS/Entries" dir))
427 (fis nil))
428 (if (not (file-readable-p file))
429 (push (cvs-create-fileinfo (if singlefile 'UNKNOWN 'DIRCHANGE)
430 dir (or singlefile ".") "") fis)
431 (with-temp-buffer
432 (insert-file-contents file)
433 (goto-char (point-min))
434 ;; Select the single file entry in case we're only interested in a file.
435 (cond
436 ((not singlefile)
437 (push (cvs-create-fileinfo 'DIRCHANGE dir "." "") fis))
438 ((re-search-forward
439 (concat "^[^/]*/" (regexp-quote singlefile) "/.*") nil t)
440 (setq all t)
441 (goto-char (match-beginning 0))
442 (narrow-to-region (point) (match-end 0)))
444 (push (cvs-create-fileinfo 'UNKNOWN dir singlefile "") fis)
445 (narrow-to-region (point-min) (point-min))))
446 (while (looking-at "\\([^/]*\\)/\\([^/]*\\)/\\([^/]*\\)/\\([^/]*\\)/")
447 (if (/= (match-beginning 1) (match-end 1))
448 (setq fis (append (cvs-fileinfo-from-entries
449 (concat dir (file-name-as-directory
450 (match-string 2)))
451 all)
452 fis))
453 (let ((f (match-string 2))
454 (rev (match-string 3))
455 (date (match-string 4))
456 timestamp
457 (type 'MODIFIED)
458 (subtype nil))
459 (cond
460 ((equal (substring rev 0 1) "-")
461 (setq type 'REMOVED rev (substring rev 1)))
462 ((not (file-exists-p (concat dir f))) (setq type 'MISSING))
463 ((equal rev "0") (setq type 'ADDED rev nil))
464 ((equal date "Result of merge") (setq subtype 'MERGED))
465 ((let ((mtime (nth 5 (file-attributes (concat dir f))))
466 (system-time-locale "C"))
467 (equal (setq timestamp (format-time-string "%c" mtime 'utc))
468 date))
469 (setq type (if all 'UP-TO-DATE)))
470 ((equal date (concat "Result of merge+" timestamp))
471 (setq type 'CONFLICT)))
472 (when type
473 (push (cvs-create-fileinfo type dir f ""
474 :base-rev rev :subtype subtype)
475 fis))))
476 (forward-line 1))))
477 fis))
479 (provide 'pcvs-info)
481 ;;; pcl-cvs-info.el ends here