]> code.delx.au - gnu-emacs/blob - lisp/pcvs-info.el
(Ffontset_info): Fix newline in doc.
[gnu-emacs] / lisp / pcvs-info.el
1 ;;; pcvs-info.el --- Internal representation of a fileinfo entry
2
3 ;; Copyright (C) 1991-2000 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: pcl-cvs
7 ;; Version: $Name: $
8 ;; Revision: $Id: pcvs-info.el,v 1.1 2000/03/11 03:42:29 monnier Exp $
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;; The cvs-fileinfo data structure:
30 ;;
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).
34
35 ;;; Code:
36
37 (eval-when-compile (require 'cl))
38 (require 'pcvs-util)
39 ;;(require 'pcvs-defs)
40
41 ;;;;
42 ;;;; config variables
43 ;;;;
44
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))
50
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))
59
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))
67
68
69 ;;;;
70 ;;;; Faces for fontification
71 ;;;;
72
73 (defface cvs-header-face
74 '((((class color) (background dark))
75 (:foreground "lightyellow" :bold t))
76 (((class color) (background light))
77 (:foreground "blue4" :bold t))
78 (t (:bold t)))
79 "PCL-CVS face used to highlight directory changes."
80 :group 'pcl-cvs)
81
82 (defface cvs-filename-face
83 '((((class color) (background dark))
84 (:foreground "lightblue"))
85 (((class color) (background light))
86 (:foreground "blue4"))
87 (t ()))
88 "PCL-CVS face used to highlight file names."
89 :group 'pcl-cvs)
90
91 (defface cvs-unknown-face
92 '((((class color) (background dark))
93 (:foreground "red"))
94 (((class color) (background light))
95 (:foreground "red"))
96 (t (:italic t)))
97 "PCL-CVS face used to highlight unknown file status."
98 :group 'pcl-cvs)
99
100 (defface cvs-handled-face
101 '((((class color) (background dark))
102 (:foreground "pink"))
103 (((class color) (background light))
104 (:foreground "pink"))
105 (t ()))
106 "PCL-CVS face used to highlight handled file status."
107 :group 'pcl-cvs)
108
109 (defface cvs-need-action-face
110 '((((class color) (background dark))
111 (:foreground "orange"))
112 (((class color) (background light))
113 (:foreground "orange"))
114 (t (:italic t)))
115 "PCL-CVS face used to highlight status of files needing action."
116 :group 'pcl-cvs)
117
118 (defface cvs-marked-face
119 '((((class color) (background dark))
120 (:foreground "green" :bold t))
121 (((class color) (background light))
122 (:foreground "green3" :bold t))
123 (t (:bold t)))
124 "PCL-CVS face used to highlight marked file indicator."
125 :group 'pcl-cvs)
126
127 (defface cvs-msg-face
128 '((t (:italic t)))
129 "PCL-CVS face used to highlight CVS messages."
130 :group 'pcl-cvs)
131
132
133 ;; There is normally no need to alter the following variable, but if
134 ;; your site has installed CVS in a non-standard way you might have
135 ;; to change it.
136
137 (defvar cvs-bakprefix ".#"
138 "The prefix that CVS prepends to files when rcsmerge'ing.")
139
140 (easy-mmode-defmap cvs-filename-map
141 '(([(mouse-2)] . cvs-mode-find-file))
142 "Local keymap for text properties of file names"
143 :inherit 'cvs-mode-map)
144
145 (easy-mmode-defmap cvs-status-map
146 '(([(mouse-2)] . cvs-mouse-toggle-mark))
147 "Local keymap for text properties of status"
148 :inherit 'cvs-mode-map)
149
150 (easy-mmode-defmap cvs-dirname-map
151 '(([(mouse-2)] . cvs-mode-find-file))
152 "Local keymap for text properties of directory names"
153 :inherit 'cvs-mode-map)
154
155 ;; Constructor:
156
157 (defstruct (cvs-fileinfo
158 (:constructor nil)
159 (:copier nil)
160 (:constructor -cvs-create-fileinfo (type dir file full-log
161 &key marked subtype
162 merge
163 base-rev
164 head-rev))
165 (:conc-name cvs-fileinfo->))
166 marked ;; t/nil.
167 type ;; See below
168 subtype ;; See below
169 dir ;; Relative directory the file resides in.
170 ;; (concat dir file) should give a valid path.
171 file ;; The file name sans the directory.
172 base-rev ;; During status: This is the revision that the
173 ;; working file is based on.
174 head-rev ;; During status: This is the highest revision in
175 ;; the repository.
176 merge ;; A cons cell containing the (ancestor . head) revisions
177 ;; of the merge that resulted in the current file.
178 ;;removed ;; t if the file no longer exists.
179 full-log ;; The output from cvs, unparsed.
180 ;;mod-time ;; Not used.
181
182 ;; In addition to the above, the following values can be extracted:
183
184 ;; handled ;; t if this file doesn't require further action.
185 ;; full-path ;; The complete relative filename.
186 ;; pp-name ;; The printed file name
187 ;; backup-file;; For MERGED and CONFLICT files after a \"cvs update\",
188 ;; this is a full path to the backup file where the
189 ;; untouched version resides.
190
191 ;; The meaning of the type field:
192
193 ;; Value ---Used by--- Explanation
194 ;; update status
195 ;; NEED-UPDATE x file needs update
196 ;; MODIFIED x x modified by you, unchanged in repository
197 ;; MERGED x x successful merge
198 ;; ADDED x x added by you, not yet committed
199 ;; MISSING x rm'd, but not yet `cvs remove'd
200 ;; REMOVED x x removed by you, not yet committed
201 ;; NEED-MERGE x need merge
202 ;; CONFLICT x conflict when merging
203 ;; ;;MOD-CONFLICT x removed locally, changed in repository.
204 ;; DIRCHANGE x x A change of directory.
205 ;; UNKNOWN x An unknown file.
206 ;; UP-TO-DATE x The file is up-to-date.
207 ;; UPDATED x x file copied from repository
208 ;; PATCHED x x diff applied from repository
209 ;; COMMITTED x x cvs commit'd
210 ;; DEAD An entry that should be removed
211 ;; MESSAGE x x This is a special fileinfo that is used
212 ;; to display a text that should be in
213 ;; full-log."
214 ;; TEMP A temporary message that should be removed
215 )
216 (defun cvs-create-fileinfo (type dir file msg &rest keys)
217 (cvs-check-fileinfo (apply #'-cvs-create-fileinfo type dir file msg keys)))
218
219 ;; Fake selectors:
220
221 (defun cvs-fileinfo->full-path (fileinfo)
222 "Return the full path for the file that is described in FILEINFO."
223 (let ((dir (cvs-fileinfo->dir fileinfo)))
224 (if (eq (cvs-fileinfo->type fileinfo) 'DIRCHANGE)
225 (if (string= dir "") "." (directory-file-name dir))
226 ;; Here, I use `concat' rather than `expand-file-name' because I want
227 ;; the resulting path to stay relative if `dir' is relative.
228 ;; I could also use `expand-file-name' with `default-directory = ""'
229 (concat dir (cvs-fileinfo->file fileinfo)))))
230
231 (defun cvs-fileinfo->pp-name (fi)
232 "Return the filename of FI as it should be displayed."
233 (if cvs-display-full-path
234 (cvs-fileinfo->full-path fi)
235 (cvs-fileinfo->file fi)))
236
237 (defun cvs-fileinfo->backup-file (fileinfo)
238 "Construct the file name of the backup file for FILEINFO."
239 (let* ((dir (cvs-fileinfo->dir fileinfo))
240 (file (cvs-fileinfo->file fileinfo))
241 (default-directory (file-name-as-directory (expand-file-name dir)))
242 (files (directory-files "." nil
243 (concat "^" (regexp-quote cvs-bakprefix)
244 (regexp-quote file) "\\.")))
245 bf)
246 (dolist (f files bf)
247 (when (and (file-readable-p f)
248 (or (null bf) (file-newer-than-file-p f bf)))
249 (setq bf (concat dir f))))))
250
251 ;; (defun cvs-fileinfo->handled (fileinfo)
252 ;; "Tell if this requires further action"
253 ;; (memq (cvs-fileinfo->type fileinfo) '(UP-TO-DATE DEAD)))
254
255 \f
256 ;; Predicate:
257
258 (defun boolp (x) (or (eq t x) (null x)))
259 (defun cvs-check-fileinfo (fi)
260 "Check FI's conformance to some conventions."
261 (let ((check 'none)
262 (type (cvs-fileinfo->type fi))
263 (subtype (cvs-fileinfo->subtype fi))
264 (marked (cvs-fileinfo->marked fi))
265 (dir (cvs-fileinfo->dir fi))
266 (file (cvs-fileinfo->file fi))
267 (base-rev (cvs-fileinfo->base-rev fi))
268 (head-rev (cvs-fileinfo->head-rev fi))
269 (full-log (cvs-fileinfo->full-log fi)))
270 (if (and (setq check 'marked) (boolp marked)
271 (setq check 'base-rev) (or (null base-rev) (stringp base-rev))
272 (setq check 'head-rev) (or (null head-rev) (stringp head-rev))
273 (setq check 'full-log) (stringp full-log)
274 (setq check 'dir)
275 (and (stringp dir)
276 (not (file-name-absolute-p dir))
277 (or (string= dir "")
278 (string= dir (file-name-as-directory dir))))
279 (setq check 'file)
280 (and (stringp file)
281 (string= file (file-name-nondirectory file)))
282 (setq check 'type) (symbolp type)
283 (setq check 'consistency)
284 (case type
285 (DIRCHANGE (and (null subtype) (string= "." file)))
286 ((NEED-UPDATE ADDED MISSING DEAD MODIFIED MESSAGE UP-TO-DATE
287 REMOVED NEED-MERGE CONFLICT UNKNOWN MESSAGE)
288 t)))
289 fi
290 (error "Invalid :%s in cvs-fileinfo %s" check fi))))
291
292 \f
293 ;;;;
294 ;;;; State table to indicate what you can do when.
295 ;;;;
296
297 (defconst cvs-states
298 `((NEED-UPDATE update diff)
299 (UP-TO-DATE update nil remove diff safe-rm revert)
300 (MODIFIED update commit undo remove diff merge diff-base)
301 (ADDED update commit remove)
302 (MISSING remove undo update safe-rm revert)
303 (REMOVED commit add undo safe-rm)
304 (NEED-MERGE update undo diff diff-base)
305 (CONFLICT merge remove undo commit diff diff-base)
306 (DIRCHANGE remove update diff ,(if cvs-allow-dir-commit 'commit) tag)
307 (UNKNOWN ignore add remove)
308 (DEAD )
309 (MESSAGE))
310 "Fileinfo state descriptions for pcl-cvs.
311 This is an assoc list. Each element consists of (STATE . FUNS)
312 - STATE (described in `cvs-create-fileinfo') is the key
313 - FUNS is the list of applicable operations.
314 The first one (if any) should be the \"default\" action.
315 Most of the actions have the obvious meaning.
316 `safe-rm' indicates that the file can be removed without losing
317 any information.")
318
319 ;;;;
320 ;;;; Utility functions
321 ;;;;
322
323 ;;----------
324 (defun cvs-applicable-p (fi-or-type func)
325 "Check if FUNC is applicable to FI-OR-TYPE.
326 If FUNC is nil, always return t.
327 FI-OR-TYPE can either be a symbol (a fileinfo-type) or a fileinfo."
328 (let ((type (if (symbolp fi-or-type) fi-or-type
329 (cvs-fileinfo->type fi-or-type))))
330 (and (not (eq type 'MESSAGE))
331 (eq (car (memq func (cdr (assq type cvs-states)))) func))))
332
333 ;; (defun cvs-default-action (fileinfo)
334 ;; "Return some kind of \"default\" action to be performed."
335 ;; (second (assq (cvs-fileinfo->type fileinfo) cvs-states)))
336
337 ;; fileinfo pretty-printers:
338
339 (defun cvs-add-face (str face &optional keymap)
340 (when cvs-highlight
341 (add-text-properties 0 (length str)
342 (list* 'face face
343 (when keymap
344 (list 'mouse-face 'highlight
345 'local-map keymap)))
346 str))
347 str)
348
349 ;;----------
350 (defun cvs-fileinfo-pp (fileinfo)
351 "Pretty print FILEINFO. Insert a printed representation in current buffer.
352 For use by the cookie package."
353 (cvs-check-fileinfo fileinfo)
354 (let ((type (cvs-fileinfo->type fileinfo))
355 (subtype (cvs-fileinfo->subtype fileinfo)))
356 (insert
357 (case type
358 (DIRCHANGE (concat "In directory "
359 (cvs-add-face (cvs-fileinfo->full-path fileinfo)
360 'cvs-header-face cvs-dirname-map)
361 ":"))
362 (MESSAGE
363 (cvs-add-face (format "Message: %s" (cvs-fileinfo->full-log fileinfo))
364 'cvs-msg-face))
365 (t
366 (let* ((status (if (cvs-fileinfo->marked fileinfo)
367 (cvs-add-face "*" 'cvs-marked-face)
368 " "))
369 (file (cvs-add-face (cvs-fileinfo->pp-name fileinfo)
370 'cvs-filename-face cvs-filename-map))
371 (base (or (cvs-fileinfo->base-rev fileinfo) ""))
372 (head (cvs-fileinfo->head-rev fileinfo))
373 (type
374 (let ((str (case type
375 ;;(MOD-CONFLICT "Not Removed")
376 (DEAD "")
377 (t (capitalize (symbol-name type)))))
378 (face (case type
379 (UP-TO-DATE 'cvs-handled-face)
380 (UNKNOWN 'cvs-unknown-face)
381 (t 'cvs-need-action-face))))
382 (cvs-add-face str face cvs-status-map)))
383 (side (or
384 ;; maybe a subtype
385 (when subtype (downcase (symbol-name subtype)))
386 ;; or the head-rev
387 (when (and head (not (string= head base))) head)
388 ;; or nothing
389 "")))
390 (format "%-11s %s %-11s %-11s %s"
391 side status type base file)))))))
392
393
394 (defun cvs-fileinfo-update (fi fi-new)
395 "Update FI with the information provided in FI-NEW."
396 (let ((type (cvs-fileinfo->type fi-new))
397 (merge (cvs-fileinfo->merge fi-new)))
398 (setf (cvs-fileinfo->type fi) type)
399 (setf (cvs-fileinfo->subtype fi) (cvs-fileinfo->subtype fi-new))
400 (setf (cvs-fileinfo->full-log fi) (cvs-fileinfo->full-log fi-new))
401 (setf (cvs-fileinfo->base-rev fi) (cvs-fileinfo->base-rev fi-new))
402 (setf (cvs-fileinfo->head-rev fi) (cvs-fileinfo->head-rev fi-new))
403 (cond
404 (merge (setf (cvs-fileinfo->merge fi) merge))
405 ((memq type '(UP-TO-DATE NEED-UPDATE))
406 (setf (cvs-fileinfo->merge fi) nil)))))
407
408 ;;----------
409 (defun cvs-fileinfo< (a b)
410 "Compare fileinfo A with fileinfo B and return t if A is `less'.
411 The ordering defined by this function is such that directories are
412 sorted alphabetically, and inside every directory the DIRCHANGE
413 fileinfo will appear first, followed by all files (alphabetically)."
414 (let ((subtypea (cvs-fileinfo->subtype a))
415 (subtypeb (cvs-fileinfo->subtype b)))
416 (cond
417 ;; Sort according to directories.
418 ((string< (cvs-fileinfo->dir a) (cvs-fileinfo->dir b)) t)
419 ((not (string= (cvs-fileinfo->dir a) (cvs-fileinfo->dir b))) nil)
420
421 ;; The DIRCHANGE entry is always first within the directory.
422 ((eq (cvs-fileinfo->type b) 'DIRCHANGE) nil)
423 ((eq (cvs-fileinfo->type a) 'DIRCHANGE) t)
424
425 ;; All files are sorted by file name.
426 ((string< (cvs-fileinfo->file a) (cvs-fileinfo->file b))))))
427
428 (provide 'pcvs-info)
429
430 ;;; pcl-cvs-info.el ends here