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