209
|
1 ;;; format.el --- read and save files in multiple formats
|
|
2
|
|
3 ;; Copyright (c) 1994, 1995, 1997 Free Software Foundation
|
|
4
|
|
5 ;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu>
|
|
6 ;; Keywords: extensions, dumped
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; 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.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
23 ;; 02111-1307, USA.
|
|
24
|
|
25 ;;; Synched up with: Emacs/Mule zeta.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This file is dumped with XEmacs.
|
|
30
|
|
31 ;; This file defines a unified mechanism for saving & loading files stored
|
|
32 ;; in different formats. `format-alist' contains information that directs
|
|
33 ;; Emacs to call an encoding or decoding function when reading or writing
|
|
34 ;; files that match certain conditions.
|
|
35 ;;
|
|
36 ;; When a file is visited, its format is determined by matching the
|
|
37 ;; beginning of the file against regular expressions stored in
|
|
38 ;; `format-alist'. If this fails, you can manually translate the buffer
|
|
39 ;; using `format-decode-buffer'. In either case, the formats used are
|
|
40 ;; listed in the variable `buffer-file-format', and become the default
|
|
41 ;; format for saving the buffer. To save a buffer in a different format,
|
|
42 ;; change this variable, or use `format-write-file'.
|
|
43 ;;
|
|
44 ;; Auto-save files are normally created in the same format as the visited
|
|
45 ;; file, but the variable `auto-save-file-format' can be set to a
|
|
46 ;; particularly fast or otherwise preferred format to be used for
|
|
47 ;; auto-saving (or nil to do no encoding on auto-save files, but then you
|
|
48 ;; risk losing any text-properties in the buffer).
|
|
49 ;;
|
|
50 ;; You can manually translate a buffer into or out of a particular format
|
|
51 ;; with the functions `format-encode-buffer' and `format-decode-buffer'.
|
|
52 ;; To translate just the region use the functions `format-encode-region'
|
|
53 ;; and `format-decode-region'.
|
|
54 ;;
|
|
55 ;; You can define a new format by writing the encoding and decoding
|
|
56 ;; functions, and adding an entry to `format-alist'. See enriched.el for
|
|
57 ;; an example of how to implement a file format. There are various
|
|
58 ;; functions defined in this file that may be useful for writing the
|
|
59 ;; encoding and decoding functions:
|
|
60 ;; * `format-annotate-region' and `format-deannotate-region' allow a
|
|
61 ;; single alist of information to be used for encoding and decoding.
|
|
62 ;; The alist defines a correspondence between strings in the file
|
|
63 ;; ("annotations") and text-properties in the buffer.
|
|
64 ;; * `format-replace-strings' is similarly useful for doing simple
|
|
65 ;; string->string translations in a reversible manner.
|
|
66
|
|
67 ;;; Code:
|
|
68
|
|
69 (put 'buffer-file-format 'permanent-local t)
|
|
70
|
|
71 (defvar format-alist
|
|
72 '((image/jpeg "JPEG image" "\377\330\377\340\000\020JFIF"
|
|
73 image-decode-jpeg nil t image-mode)
|
|
74 (image/gif "GIF image" "GIF8[79]"
|
|
75 image-decode-gif nil t image-mode)
|
|
76 (image/png "Portable Network Graphics" "\211PNG"
|
|
77 image-decode-png nil t image-mode)
|
|
78 (image/x-xpm "XPM image" "/\\* XPM \\*/"
|
|
79 image-decode-xpm nil t image-mode)
|
|
80 (text/enriched "Extended MIME text/enriched format."
|
|
81 "Content-[Tt]ype:[ \t]*text/enriched"
|
|
82 enriched-decode enriched-encode t enriched-mode)
|
|
83 (text/richtext "Extended MIME obsolete text/richtext format."
|
|
84 "Content-[Tt]ype:[ \t]*text/richtext"
|
|
85 richtext-decode richtext-encode t enriched-mode)
|
|
86 (plain "Standard ASCII format, no text properties."
|
|
87 ;; Plain only exists so that there is an obvious neutral choice in
|
|
88 ;; the completion list.
|
|
89 nil nil nil nil nil))
|
|
90 "List of information about understood file formats.
|
|
91 Elements are of the form \(NAME DOC-STR REGEXP FROM-FN TO-FN MODIFY MODE-FN).
|
|
92 NAME is a symbol, which is stored in `buffer-file-format'.
|
|
93 DOC-STR should be a single line providing more information about the
|
|
94 format. It is currently unused, but in the future will be shown to
|
|
95 the user if they ask for more information.
|
|
96 REGEXP is a regular expression to match against the beginning of the file;
|
|
97 it should match only files in that format.
|
|
98 FROM-FN is called to decode files in that format; it gets two args, BEGIN
|
|
99 and END, and can make any modifications it likes, returning the new
|
|
100 end. It must make sure that the beginning of the file no longer
|
|
101 matches REGEXP, or else it will get called again.
|
|
102 TO-FN is called to encode a region into that format; it is passed three
|
|
103 arguments: BEGIN, END, and BUFFER. BUFFER is the original buffer that
|
|
104 the data being written came from, which the function could use, for
|
|
105 example, to find the values of local variables. TO-FN should either
|
|
106 return a list of annotations like `write-region-annotate-functions',
|
|
107 or modify the region and return the new end.
|
|
108 MODIFY, if non-nil, means the TO-FN wants to modify the region. If nil,
|
|
109 TO-FN will not make any changes but will instead return a list of
|
|
110 annotations.
|
|
111 MODE-FN, if specified, is called when visiting a file with that format.")
|
|
112
|
|
113 ;;; Basic Functions (called from Lisp)
|
|
114
|
|
115 (defun format-annotate-function (format from to)
|
|
116 "Returns annotations for writing region as FORMAT.
|
|
117 FORMAT is a symbol naming one of the formats defined in `format-alist',
|
|
118 it must be a single symbol, not a list like `buffer-file-format'.
|
|
119 FROM and TO delimit the region to be operated on in the current buffer.
|
|
120 This function works like a function on `write-region-annotate-functions':
|
|
121 it either returns a list of annotations, or returns with a different buffer
|
|
122 current, which contains the modified text to write.
|
|
123
|
|
124 For most purposes, consider using `format-encode-region' instead."
|
|
125 ;; This function is called by write-region (actually build-annotations)
|
|
126 ;; for each element of buffer-file-format.
|
|
127 (let* ((info (assq format format-alist))
|
|
128 (to-fn (nth 4 info))
|
|
129 (modify (nth 5 info)))
|
|
130 (if to-fn
|
|
131 (if modify
|
|
132 ;; To-function wants to modify region. Copy to safe place.
|
|
133 (let ((copy-buf (get-buffer-create " *Format Temp*")))
|
|
134 (copy-to-buffer copy-buf from to)
|
|
135 (set-buffer copy-buf)
|
|
136 (format-insert-annotations write-region-annotations-so-far from)
|
|
137 (funcall to-fn (point-min) (point-max))
|
|
138 nil)
|
|
139 ;; Otherwise just call function, it will return annotations.
|
|
140 (funcall to-fn from to)))))
|
|
141
|
|
142 (defun format-decode (format length &optional visit-flag)
|
|
143 ;; This function is called by insert-file-contents whenever a file is read.
|
|
144 "Decode text from any known FORMAT.
|
|
145 FORMAT is a symbol appearing in `format-alist' or a list of such symbols,
|
|
146 or nil, in which case this function tries to guess the format of the data by
|
|
147 matching against the regular expressions in `format-alist'. After a match is
|
|
148 found and the region decoded, the alist is searched again from the beginning
|
|
149 for another match.
|
|
150
|
|
151 Second arg LENGTH is the number of characters following point to operate on.
|
|
152 If optional third arg VISIT-FLAG is true, set `buffer-file-format'
|
|
153 to the list of formats used, and call any mode functions defined for those
|
|
154 formats.
|
|
155
|
|
156 Returns the new length of the decoded region.
|
|
157
|
|
158 For most purposes, consider using `format-decode-region' instead."
|
|
159 (let ((mod (buffer-modified-p))
|
|
160 (begin (point))
|
|
161 (end (+ (point) length)))
|
|
162 (if (null format)
|
|
163 ;; Figure out which format it is in, remember list in `format'.
|
|
164 (let ((try format-alist))
|
|
165 (while try
|
|
166 (let* ((f (car try))
|
|
167 (regexp (nth 2 f))
|
|
168 (p (point)))
|
|
169 (if (and regexp (looking-at regexp)
|
|
170 (< (match-end 0) (+ begin length)))
|
|
171 (progn
|
|
172 (setq format (cons (car f) format))
|
|
173 ;; Decode it
|
|
174 (if (nth 3 f) (setq end (funcall (nth 3 f) begin end)))
|
|
175 ;; Call visit function if required
|
|
176 (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1))
|
|
177 ;; Safeguard against either of the functions changing pt.
|
|
178 (goto-char p)
|
|
179 ;; Rewind list to look for another format
|
|
180 (setq try format-alist))
|
|
181 (setq try (cdr try))))))
|
|
182 ;; Deal with given format(s)
|
|
183 (or (listp format) (setq format (list format)))
|
|
184 (let ((do format) f)
|
|
185 (while do
|
|
186 (or (setq f (assq (car do) format-alist))
|
|
187 (error "Unknown format" (car do)))
|
|
188 ;; Decode:
|
|
189 (if (nth 3 f) (setq end (funcall (nth 3 f) begin end)))
|
|
190 ;; Call visit function if required
|
|
191 (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1))
|
|
192 (setq do (cdr do)))))
|
|
193 (if visit-flag
|
|
194 (setq buffer-file-format format))
|
|
195 (set-buffer-modified-p mod)
|
|
196 ;; Return new length of region
|
|
197 (- end begin)))
|
|
198
|
|
199 ;;;
|
|
200 ;;; Interactive functions & entry points
|
|
201 ;;;
|
|
202
|
|
203 (defun format-decode-buffer (&optional format)
|
|
204 "Translate the buffer from some FORMAT.
|
|
205 If the format is not specified, this function attempts to guess.
|
|
206 `buffer-file-format' is set to the format used, and any mode-functions
|
|
207 for the format are called."
|
|
208 (interactive
|
|
209 (list (format-read "Translate buffer from format (default: guess): ")))
|
|
210 (save-excursion
|
|
211 (goto-char (point-min))
|
|
212 (format-decode format (buffer-size) t)))
|
|
213
|
|
214 (defun format-decode-region (from to &optional format)
|
|
215 "Decode the region from some format.
|
|
216 Arg FORMAT is optional; if omitted the format will be determined by looking
|
|
217 for identifying regular expressions at the beginning of the region."
|
|
218 (interactive
|
|
219 (list (region-beginning) (region-end)
|
|
220 (format-read "Translate region from format (default: guess): ")))
|
|
221 (save-excursion
|
|
222 (goto-char from)
|
|
223 (format-decode format (- to from) nil)))
|
|
224
|
|
225 (defun format-encode-buffer (&optional format)
|
|
226 "Translate the buffer into FORMAT.
|
|
227 FORMAT defaults to `buffer-file-format'. It is a symbol naming one of the
|
|
228 formats defined in `format-alist', or a list of such symbols."
|
|
229 (interactive
|
|
230 (list (format-read (format "Translate buffer to format (default %s): "
|
|
231 buffer-file-format))))
|
|
232 (format-encode-region (point-min) (point-max) format))
|
|
233
|
|
234 (defun format-encode-region (beg end &optional format)
|
|
235 "Translate the region into some FORMAT.
|
|
236 FORMAT defaults to `buffer-file-format', it is a symbol naming
|
|
237 one of the formats defined in `format-alist', or a list of such symbols."
|
|
238 (interactive
|
|
239 (list (region-beginning) (region-end)
|
|
240 (format-read (format "Translate region to format (default %s): "
|
|
241 buffer-file-format))))
|
|
242 (if (null format) (setq format buffer-file-format))
|
|
243 (if (symbolp format) (setq format (list format)))
|
|
244 (save-excursion
|
|
245 (goto-char end)
|
|
246 (let ( ; (cur-buf (current-buffer))
|
|
247 (end (point-marker)))
|
|
248 (while format
|
|
249 (let* ((info (assq (car format) format-alist))
|
|
250 (to-fn (nth 4 info))
|
|
251 (modify (nth 5 info))
|
|
252 ;; result
|
|
253 )
|
|
254 (if to-fn
|
|
255 (if modify
|
|
256 (setq end (funcall to-fn beg end (current-buffer)))
|
|
257 (format-insert-annotations
|
|
258 (funcall to-fn beg end (current-buffer)))))
|
|
259 (setq format (cdr format)))))))
|
|
260
|
|
261 (defun format-write-file (filename format)
|
|
262 "Write current buffer into a FILE using some FORMAT.
|
|
263 Makes buffer visit that file and sets the format as the default for future
|
|
264 saves. If the buffer is already visiting a file, you can specify a directory
|
|
265 name as FILE, to write a file of the same old name in that directory."
|
|
266 (interactive
|
|
267 ;; Same interactive spec as write-file, plus format question.
|
|
268 (let* ((file (if buffer-file-name
|
|
269 (read-file-name "Write file: "
|
|
270 nil nil nil nil)
|
|
271 (read-file-name "Write file: "
|
|
272 (cdr (assq 'default-directory
|
|
273 (buffer-local-variables)))
|
|
274 nil nil (buffer-name))))
|
|
275 (fmt (format-read (format "Write file `%s' in format: "
|
|
276 (file-name-nondirectory file)))))
|
|
277 (list file fmt)))
|
|
278 (setq buffer-file-format format)
|
|
279 (write-file filename))
|
|
280
|
|
281 (defun format-find-file (filename format)
|
|
282 "Find the file FILE using data format FORMAT.
|
|
283 If FORMAT is nil then do not do any format conversion."
|
|
284 (interactive
|
|
285 ;; Same interactive spec as write-file, plus format question.
|
|
286 (let* ((file (read-file-name "Find file: "))
|
|
287 (fmt (format-read (format "Read file `%s' in format: "
|
|
288 (file-name-nondirectory file)))))
|
|
289 (list file fmt)))
|
|
290 (let ((format-alist nil))
|
|
291 (find-file filename))
|
|
292 (if format
|
|
293 (format-decode-buffer format)))
|
|
294
|
|
295 (defun format-insert-file (filename format &optional beg end)
|
|
296 "Insert the contents of file FILE using data format FORMAT.
|
|
297 If FORMAT is nil then do not do any format conversion.
|
|
298 The optional third and fourth arguments BEG and END specify
|
|
299 the part of the file to read.
|
|
300
|
|
301 The return value is like the value of `insert-file-contents':
|
|
302 a list (ABSOLUTE-FILE-NAME . SIZE)."
|
|
303 (interactive
|
|
304 ;; Same interactive spec as write-file, plus format question.
|
|
305 (let* ((file (read-file-name "Find file: "))
|
|
306 (fmt (format-read (format "Read file `%s' in format: "
|
|
307 (file-name-nondirectory file)))))
|
|
308 (list file fmt)))
|
|
309 (let (value size)
|
|
310 (let ((format-alist nil))
|
|
311 (setq value (insert-file-contents filename nil beg end))
|
|
312 (setq size (nth 1 value)))
|
|
313 (if format
|
|
314 (setq size (format-decode format size)
|
|
315 value (cons (car value) size)))
|
|
316 value))
|
|
317
|
|
318 (defun format-read (&optional prompt)
|
|
319 "Read and return the name of a format.
|
|
320 Return value is a list, like `buffer-file-format'; it may be nil.
|
|
321 Formats are defined in `format-alist'. Optional arg is the PROMPT to use."
|
|
322 (let* ((table (mapcar (lambda (x) (list (symbol-name (car x))))
|
|
323 format-alist))
|
|
324 (ans (completing-read (or prompt "Format: ") table nil t)))
|
|
325 (if (not (equal "" ans)) (list (intern ans)))))
|
|
326
|
|
327
|
|
328 ;;;
|
|
329 ;;; Below are some functions that may be useful in writing encoding and
|
|
330 ;;; decoding functions for use in format-alist.
|
|
331 ;;;
|
|
332
|
|
333 (defun format-replace-strings (alist &optional reverse beg end)
|
|
334 "Do multiple replacements on the buffer.
|
|
335 ALIST is a list of (from . to) pairs, which should be proper arguments to
|
|
336 `search-forward' and `replace-match' respectively.
|
|
337 Optional 2nd arg REVERSE, if non-nil, means the pairs are (to . from), so that
|
|
338 you can use the same list in both directions if it contains only literal
|
|
339 strings.
|
|
340 Optional args BEGIN and END specify a region of the buffer to operate on."
|
|
341 (save-excursion
|
|
342 (save-restriction
|
|
343 (or beg (setq beg (point-min)))
|
|
344 (if end (narrow-to-region (point-min) end))
|
|
345 (while alist
|
|
346 (let ((from (if reverse (cdr (car alist)) (car (car alist))))
|
|
347 (to (if reverse (car (cdr alist)) (cdr (car alist)))))
|
|
348 (goto-char beg)
|
|
349 (while (search-forward from nil t)
|
|
350 (goto-char (match-beginning 0))
|
|
351 (insert to)
|
|
352 (set-text-properties (- (point) (length to)) (point)
|
|
353 (text-properties-at (point)))
|
|
354 (delete-region (point) (+ (point) (- (match-end 0)
|
|
355 (match-beginning 0)))))
|
|
356 (setq alist (cdr alist)))))))
|
|
357
|
|
358 ;;; Some list-manipulation functions that we need.
|
|
359
|
|
360 (defun format-delq-cons (cons list)
|
|
361 "Remove the given CONS from LIST by side effect,
|
|
362 and return the new LIST. Since CONS could be the first element
|
|
363 of LIST, write `\(setq foo \(format-delq-cons element foo))' to be sure of
|
|
364 changing the value of `foo'."
|
|
365 (if (eq cons list)
|
|
366 (cdr list)
|
|
367 (let ((p list))
|
|
368 (while (not (eq (cdr p) cons))
|
|
369 (if (null p) (error "format-delq-cons: not an element."))
|
|
370 (setq p (cdr p)))
|
|
371 ;; Now (cdr p) is the cons to delete
|
|
372 (setcdr p (cdr cons))
|
|
373 list)))
|
|
374
|
|
375 (defun format-make-relatively-unique (a b)
|
|
376 "Delete common elements of lists A and B, return as pair.
|
|
377 Compares using `equal'."
|
|
378 (let* ((acopy (copy-sequence a))
|
|
379 (bcopy (copy-sequence b))
|
|
380 (tail acopy))
|
|
381 (while tail
|
|
382 (let ((dup (member (car tail) bcopy))
|
|
383 (next (cdr tail)))
|
|
384 (if dup (setq acopy (format-delq-cons tail acopy)
|
|
385 bcopy (format-delq-cons dup bcopy)))
|
|
386 (setq tail next)))
|
|
387 (cons acopy bcopy)))
|
|
388
|
|
389 (defun format-common-tail (a b)
|
|
390 "Given two lists that have a common tail, return it.
|
|
391 Compares with `equal', and returns the part of A that is equal to the
|
|
392 equivalent part of B. If even the last items of the two are not equal,
|
|
393 returns nil."
|
|
394 (let ((la (length a))
|
|
395 (lb (length b)))
|
|
396 ;; Make sure they are the same length
|
|
397 (if (> la lb)
|
|
398 (setq a (nthcdr (- la lb) a))
|
|
399 (setq b (nthcdr (- lb la) b))))
|
|
400 (while (not (equal a b))
|
|
401 (setq a (cdr a)
|
|
402 b (cdr b)))
|
|
403 a)
|
|
404
|
|
405 (defun format-reorder (items order)
|
|
406 "Arrange ITEMS to following partial ORDER.
|
|
407 Elements of ITEMS equal to elements of ORDER will be rearranged to follow the
|
|
408 ORDER. Unmatched items will go last."
|
|
409 (if order
|
|
410 (let ((item (member (car order) items)))
|
|
411 (if item
|
|
412 (cons (car item)
|
|
413 (format-reorder (format-delq-cons item items)
|
|
414 (cdr order)))
|
|
415 (format-reorder items (cdr order))))
|
|
416 items))
|
|
417
|
|
418 (put 'face 'format-list-valued t) ; These text-properties take values
|
|
419 (put 'unknown 'format-list-valued t) ; that are lists, the elements of which
|
|
420 ; should be considered separately.
|
|
421 ; See format-deannotate-region and
|
|
422 ; format-annotate-region.
|
|
423
|
|
424 ;;;
|
|
425 ;;; Decoding
|
|
426 ;;;
|
|
427
|
|
428 (defun format-deannotate-region (from to translations next-fn)
|
|
429 "Translate annotations in the region into text properties.
|
|
430 This sets text properties between FROM to TO as directed by the
|
|
431 TRANSLATIONS and NEXT-FN arguments.
|
|
432
|
|
433 NEXT-FN is a function that searches forward from point for an annotation.
|
|
434 It should return a list of 4 elements: \(BEGIN END NAME POSITIVE). BEGIN and
|
|
435 END are buffer positions bounding the annotation, NAME is the name searched
|
|
436 for in TRANSLATIONS, and POSITIVE should be non-nil if this annotation marks
|
|
437 the beginning of a region with some property, or nil if it ends the region.
|
|
438 NEXT-FN should return nil if there are no annotations after point.
|
|
439
|
|
440 The basic format of the TRANSLATIONS argument is described in the
|
|
441 documentation for the `format-annotate-region' function. There are some
|
|
442 additional things to keep in mind for decoding, though:
|
|
443
|
|
444 When an annotation is found, the TRANSLATIONS list is searched for a
|
|
445 text-property name and value that corresponds to that annotation. If the
|
|
446 text-property has several annotations associated with it, it will be used only
|
|
447 if the other annotations are also in effect at that point. The first match
|
|
448 found whose annotations are all present is used.
|
|
449
|
|
450 The text property thus determined is set to the value over the region between
|
|
451 the opening and closing annotations. However, if the text-property name has a
|
|
452 non-nil `format-list-valued' property, then the value will be consed onto the
|
|
453 surrounding value of the property, rather than replacing that value.
|
|
454
|
|
455 There are some special symbols that can be used in the \"property\" slot of
|
|
456 the TRANSLATIONS list: PARAMETER and FUNCTION \(spelled in uppercase).
|
|
457 Annotations listed under the pseudo-property PARAMETER are considered to be
|
|
458 arguments of the immediately surrounding annotation; the text between the
|
|
459 opening and closing parameter annotations is deleted from the buffer but saved
|
|
460 as a string. The surrounding annotation should be listed under the
|
|
461 pseudo-property FUNCTION. Instead of inserting a text-property for this
|
|
462 annotation, the function listed in the VALUE slot is called to make whatever
|
|
463 changes are appropriate. The function's first two arguments are the START and
|
|
464 END locations, and the rest of the arguments are any PARAMETERs found in that
|
|
465 region.
|
|
466
|
|
467 Any annotations that are found by NEXT-FN but not defined by TRANSLATIONS
|
|
468 are saved as values of the `unknown' text-property \(which is list-valued).
|
|
469 The TRANSLATIONS list should usually contain an entry of the form
|
|
470 \(unknown \(nil format-annotate-value))
|
|
471 to write these unknown annotations back into the file."
|
|
472 (save-excursion
|
|
473 (save-restriction
|
|
474 (narrow-to-region (point-min) to)
|
|
475 (goto-char from)
|
|
476 (let (next open-ans todo
|
|
477 ;; loc
|
|
478 unknown-ans)
|
|
479 (while (setq next (funcall next-fn))
|
|
480 (let* ((loc (nth 0 next))
|
|
481 (end (nth 1 next))
|
|
482 (name (nth 2 next))
|
|
483 (positive (nth 3 next))
|
|
484 (found nil))
|
|
485
|
|
486 ;; Delete the annotation
|
|
487 (delete-region loc end)
|
|
488 (if positive
|
|
489 ;; Positive annotations are stacked, remembering location
|
|
490 (setq open-ans (cons (list name loc) open-ans))
|
|
491 ;; It is a negative annotation:
|
|
492 ;; Close the top annotation & add its text property.
|
|
493 ;; If the file's nesting is messed up, the close might not match
|
|
494 ;; the top thing on the open-annotations stack.
|
|
495 ;; If no matching annotation is open, just ignore the close.
|
|
496 (if (not (assoc name open-ans))
|
|
497 (message "Extra closing annotation (%s) in file" name)
|
|
498 ;; If one is open, but not on the top of the stack, close
|
|
499 ;; the things in between as well. Set `found' when the real
|
|
500 ;; one is closed.
|
|
501 (while (not found)
|
|
502 (let* ((top (car open-ans)) ; first on stack: should match.
|
|
503 (top-name (car top))
|
|
504 (start (car (cdr top))) ; location of start
|
|
505 (params (cdr (cdr top))) ; parameters
|
|
506 (aalist translations)
|
|
507 (matched nil))
|
|
508 (if (equal name top-name)
|
|
509 (setq found t)
|
|
510 (message "Improper nesting in file."))
|
|
511 ;; Look through property names in TRANSLATIONS
|
|
512 (while aalist
|
|
513 (let ((prop (car (car aalist)))
|
|
514 (alist (cdr (car aalist))))
|
|
515 ;; And look through values for each property
|
|
516 (while alist
|
|
517 (let ((value (car (car alist)))
|
|
518 (ans (cdr (car alist))))
|
|
519 (if (member top-name ans)
|
|
520 ;; This annotation is listed, but still have to
|
|
521 ;; check if multiple annotations are satisfied
|
|
522 (if (member 'nil (mapcar
|
|
523 (lambda (r)
|
|
524 (assoc r open-ans))
|
|
525 ans))
|
|
526 nil ; multiple ans not satisfied
|
|
527 ;; Yes, all set.
|
|
528 ;; If there are multiple annotations going
|
|
529 ;; into one text property, adjust the
|
|
530 ;; begin points of the other annotations
|
|
531 ;; so that we don't get double marking.
|
|
532 (let ((to-reset ans)
|
|
533 this-one)
|
|
534 (while to-reset
|
|
535 (setq this-one
|
|
536 (assoc (car to-reset)
|
|
537 (cdr open-ans)))
|
|
538 (if this-one
|
|
539 (setcar (cdr this-one) loc))
|
|
540 (setq to-reset (cdr to-reset))))
|
|
541 ;; Set loop variables to nil so loop
|
|
542 ;; will exit.
|
|
543 (setq alist nil aalist nil matched t
|
|
544 ;; pop annotation off stack.
|
|
545 open-ans (cdr open-ans))
|
|
546 (cond
|
|
547 ;; Check for pseudo-properties
|
|
548 ((eq prop 'PARAMETER)
|
|
549 ;; This is a parameter of the top open ann:
|
|
550 ;; delete text and use as arg.
|
|
551 (if open-ans
|
|
552 ;; (If nothing open, discard).
|
|
553 (setq open-ans
|
|
554 (cons (append (car open-ans)
|
|
555 (list
|
|
556 (buffer-substring
|
|
557 start loc)))
|
|
558 (cdr open-ans))))
|
|
559 (delete-region start loc))
|
|
560 ((eq prop 'FUNCTION)
|
|
561 ;; Not a property, but a function to call.
|
|
562 (let ((rtn (apply value start loc params)))
|
|
563 (if rtn (setq todo (cons rtn todo)))))
|
|
564 (t
|
|
565 ;; Normal property/value pair
|
|
566 (setq todo
|
|
567 (cons (list start loc prop value)
|
|
568 todo)))))))
|
|
569 (setq alist (cdr alist))))
|
|
570 (setq aalist (cdr aalist)))
|
|
571 (if matched
|
|
572 nil
|
|
573 ;; Didn't find any match for the annotation:
|
|
574 ;; Store as value of text-property `unknown'.
|
|
575 (setq open-ans (cdr open-ans))
|
|
576 (setq todo (cons (list start loc 'unknown top-name)
|
|
577 todo))
|
|
578 (setq unknown-ans (cons name unknown-ans)))))))))
|
|
579
|
|
580 ;; Once entire file has been scanned, add the properties.
|
|
581 (while todo
|
|
582 (let* ((item (car todo))
|
|
583 (from (nth 0 item))
|
|
584 (to (nth 1 item))
|
|
585 (prop (nth 2 item))
|
|
586 (val (nth 3 item)))
|
|
587
|
|
588 (put-text-property
|
|
589 from to prop
|
|
590 (cond ((numberp val) ; add to ambient value if numeric
|
|
591 (+ val (or (get-text-property from prop) 0)))
|
|
592 ((get prop 'format-list-valued) ; value gets consed onto
|
|
593 ; list-valued properties
|
|
594 (let ((prev (get-text-property from prop)))
|
|
595 (cons val (if (listp prev) prev (list prev)))))
|
|
596 (t val)))) ; normally, just set to val.
|
|
597 (setq todo (cdr todo)))
|
|
598
|
|
599 (if unknown-ans
|
|
600 (message "Unknown annotations: %s" unknown-ans))))))
|
|
601
|
|
602 ;;;
|
|
603 ;;; Encoding
|
|
604 ;;;
|
|
605
|
|
606 (defun format-insert-annotations (list &optional offset)
|
|
607 "Apply list of annotations to buffer as `write-region' would.
|
|
608 Inserts each element of the given LIST of buffer annotations at its
|
|
609 appropriate place. Use second arg OFFSET if the annotations' locations are
|
|
610 not relative to the beginning of the buffer: annotations will be inserted
|
|
611 at their location-OFFSET+1 \(ie, the offset is treated as the character number
|
|
612 of the first character in the buffer)."
|
|
613 (if (not offset)
|
|
614 (setq offset 0)
|
|
615 (setq offset (1- offset)))
|
|
616 (let ((l (reverse list)))
|
|
617 (while l
|
|
618 (goto-char (- (car (car l)) offset))
|
|
619 (insert (cdr (car l)))
|
|
620 (setq l (cdr l)))))
|
|
621
|
|
622 (defun format-annotate-value (old new)
|
|
623 "Return OLD and NEW as a \(close . open) annotation pair.
|
|
624 Useful as a default function for TRANSLATIONS alist when the value of the text
|
|
625 property is the name of the annotation that you want to use, as it is for the
|
|
626 `unknown' text property."
|
|
627 (cons (if old (list old))
|
|
628 (if new (list new))))
|
|
629
|
|
630 (defun format-annotate-region (from to trans format-fn ignore)
|
|
631 "Generate annotations for text properties in the region.
|
|
632 Searches for changes between FROM and TO, and describes them with a list of
|
|
633 annotations as defined by alist TRANSLATIONS and FORMAT-FN. IGNORE lists text
|
|
634 properties not to consider; any text properties that are neither ignored nor
|
|
635 listed in TRANSLATIONS are warned about.
|
|
636 If you actually want to modify the region, give the return value of this
|
|
637 function to `format-insert-annotations'.
|
|
638
|
|
639 Format of the TRANSLATIONS argument:
|
|
640
|
|
641 Each element is a list whose car is a PROPERTY, and the following
|
|
642 elements are VALUES of that property followed by the names of zero or more
|
|
643 ANNOTATIONS. Whenever the property takes on that value, the annotations
|
|
644 \(as formatted by FORMAT-FN) are inserted into the file.
|
|
645 When the property stops having that value, the matching negated annotation
|
|
646 will be inserted \(it may actually be closed earlier and reopened, if
|
|
647 necessary, to keep proper nesting).
|
|
648
|
|
649 If the property's value is a list, then each element of the list is dealt with
|
|
650 separately.
|
|
651
|
|
652 If a VALUE is numeric, then it is assumed that there is a single annotation
|
|
653 and each occurrence of it increments the value of the property by that number.
|
|
654 Thus, given the entry \(left-margin \(4 \"indent\")), if the left margin
|
|
655 changes from 4 to 12, two <indent> annotations will be generated.
|
|
656
|
|
657 If the VALUE is nil, then instead of annotations, a function should be
|
|
658 specified. This function is used as a default: it is called for all
|
|
659 transitions not explicitly listed in the table. The function is called with
|
|
660 two arguments, the OLD and NEW values of the property. It should return
|
|
661 lists of annotations like `format-annotate-location' does.
|
|
662
|
|
663 The same structure can be used in reverse for reading files."
|
|
664 (let ((all-ans nil) ; All annotations - becomes return value
|
|
665 (open-ans nil) ; Annotations not yet closed
|
|
666 (loc nil) ; Current location
|
|
667 (not-found nil)) ; Properties that couldn't be saved
|
|
668 (while (or (null loc)
|
|
669 (and (setq loc (next-property-change loc nil to))
|
|
670 (< loc to)))
|
|
671 (or loc (setq loc from))
|
|
672 (let* ((ans (format-annotate-location loc (= loc from) ignore trans))
|
|
673 (neg-ans (format-reorder (aref ans 0) open-ans))
|
|
674 (pos-ans (aref ans 1))
|
|
675 (ignored (aref ans 2)))
|
|
676 (setq not-found (append ignored not-found)
|
|
677 ignore (append ignored ignore))
|
|
678 ;; First do the negative (closing) annotations
|
|
679 (while neg-ans
|
|
680 ;; Check if it's missing. This can happen (eg, a numeric property
|
|
681 ;; going negative can generate closing annotations before there are
|
|
682 ;; any open). Warn user & ignore.
|
|
683 (if (not (member (car neg-ans) open-ans))
|
|
684 (message "Can't close %s: not open." (car neg-ans))
|
|
685 (while (not (equal (car neg-ans) (car open-ans)))
|
|
686 ;; To close anno. N, need to first close ans 1 to N-1,
|
|
687 ;; remembering to re-open them later.
|
|
688 (setq pos-ans (cons (car open-ans) pos-ans))
|
|
689 (setq all-ans
|
|
690 (cons (cons loc (funcall format-fn (car open-ans) nil))
|
|
691 all-ans))
|
|
692 (setq open-ans (cdr open-ans)))
|
|
693 ;; Now remove the one we're really interested in from open list.
|
|
694 (setq open-ans (cdr open-ans))
|
|
695 ;; And put the closing annotation here.
|
|
696 (setq all-ans
|
|
697 (cons (cons loc (funcall format-fn (car neg-ans) nil))
|
|
698 all-ans)))
|
|
699 (setq neg-ans (cdr neg-ans)))
|
|
700 ;; Now deal with positive (opening) annotations
|
|
701 (let ( ; (p pos-ans)
|
|
702 )
|
|
703 (while pos-ans
|
|
704 (setq open-ans (cons (car pos-ans) open-ans))
|
|
705 (setq all-ans
|
|
706 (cons (cons loc (funcall format-fn (car pos-ans) t))
|
|
707 all-ans))
|
|
708 (setq pos-ans (cdr pos-ans))))))
|
|
709
|
|
710 ;; Close any annotations still open
|
|
711 (while open-ans
|
|
712 (setq all-ans
|
|
713 (cons (cons to (funcall format-fn (car open-ans) nil))
|
|
714 all-ans))
|
|
715 (setq open-ans (cdr open-ans)))
|
|
716 (if not-found
|
|
717 (message "These text properties could not be saved:\n %s"
|
|
718 not-found))
|
|
719 (nreverse all-ans)))
|
|
720
|
|
721 ;;; Internal functions for format-annotate-region.
|
|
722
|
|
723 (defun format-annotate-location (loc all ignore trans)
|
|
724 "Return annotation(s) needed at LOCATION.
|
|
725 This includes any properties that change between LOC-1 and LOC.
|
|
726 If ALL is true, don't look at previous location, but generate annotations for
|
|
727 all non-nil properties.
|
|
728 Third argument IGNORE is a list of text-properties not to consider.
|
|
729
|
|
730 Return value is a vector of 3 elements:
|
|
731 1. List of names of the annotations to close
|
|
732 2. List of the names of annotations to open.
|
|
733 3. List of properties that were ignored or couldn't be annotated."
|
|
734 (let* ((prev-loc (1- loc))
|
|
735 (before-plist (if all nil (text-properties-at prev-loc)))
|
|
736 (after-plist (text-properties-at loc))
|
|
737 p negatives positives prop props not-found)
|
|
738 ;; make list of all property names involved
|
|
739 (setq p before-plist)
|
|
740 (while p
|
|
741 (if (not (memq (car p) props))
|
|
742 (setq props (cons (car p) props)))
|
|
743 (setq p (cdr (cdr p))))
|
|
744 (setq p after-plist)
|
|
745 (while p
|
|
746 (if (not (memq (car p) props))
|
|
747 (setq props (cons (car p) props)))
|
|
748 (setq p (cdr (cdr p))))
|
|
749
|
|
750 (while props
|
|
751 (setq prop (car props)
|
|
752 props (cdr props))
|
|
753 (if (memq prop ignore)
|
|
754 nil ; If it's been ignored before, ignore it now.
|
|
755 (let ((before (if all nil (car (cdr (memq prop before-plist)))))
|
|
756 (after (car (cdr (memq prop after-plist)))))
|
|
757 (if (equal before after)
|
|
758 nil ; no change; ignore
|
|
759 (let ((result (format-annotate-single-property-change
|
|
760 prop before after trans)))
|
|
761 (if (not result)
|
|
762 (setq not-found (cons prop not-found))
|
|
763 (setq negatives (nconc negatives (car result))
|
|
764 positives (nconc positives (cdr result)))))))))
|
|
765 (vector negatives positives not-found)))
|
|
766
|
|
767 (defun format-annotate-single-property-change (prop old new trans)
|
|
768 "Return annotations for PROPERTY changing from OLD to NEW.
|
|
769 These are searched for in the TRANSLATIONS alist.
|
|
770 If NEW does not appear in the list, but there is a default function, then that
|
|
771 function is called.
|
|
772 Annotations to open and to close are returned as a dotted pair."
|
|
773 (let ((prop-alist (cdr (assoc prop trans)))
|
|
774 ;; default
|
|
775 )
|
|
776 (if (not prop-alist)
|
|
777 nil
|
|
778 ;; If property is numeric, nil means 0
|
|
779 (cond ((and (numberp old) (null new))
|
|
780 (setq new 0))
|
|
781 ((and (numberp new) (null old))
|
|
782 (setq old 0)))
|
|
783 ;; If either old or new is a list, have to treat both that way.
|
|
784 (if (or (consp old) (consp new))
|
|
785 (let* ((old (if (listp old) old (list old)))
|
|
786 (new (if (listp new) new (list new)))
|
|
787 ;; (tail (format-common-tail old new))
|
|
788 close open)
|
|
789 (while old
|
|
790 (setq close
|
|
791 (append (car (format-annotate-atomic-property-change
|
|
792 prop-alist (car old) nil))
|
|
793 close)
|
|
794 old (cdr old)))
|
|
795 (while new
|
|
796 (setq open
|
|
797 (append (cdr (format-annotate-atomic-property-change
|
|
798 prop-alist nil (car new)))
|
|
799 open)
|
|
800 new (cdr new)))
|
|
801 (format-make-relatively-unique close open))
|
|
802 (format-annotate-atomic-property-change prop-alist old new)))))
|
|
803
|
|
804 (defun format-annotate-atomic-property-change (prop-alist old new)
|
|
805 "Internal function annotate a single property change.
|
|
806 PROP-ALIST is the relevant segment of a TRANSLATIONS list.
|
|
807 OLD and NEW are the values."
|
|
808 (cond
|
|
809 ;; Numerical annotation - use difference
|
|
810 ((and (numberp old) (numberp new))
|
|
811 (let* ((entry (progn
|
|
812 (while (and (car (car prop-alist))
|
|
813 (not (numberp (car (car prop-alist)))))
|
|
814 (setq prop-alist (cdr prop-alist)))
|
|
815 (car prop-alist)))
|
|
816 (increment (car (car prop-alist)))
|
|
817 (n (ceiling (/ (float (- new old)) (float increment))))
|
|
818 (anno (car (cdr (car prop-alist)))))
|
|
819 (if (> n 0)
|
|
820 (cons nil (make-list n anno))
|
|
821 (cons (make-list (- n) anno) nil))))
|
|
822
|
|
823 ;; Standard annotation
|
|
824 (t (let ((close (and old (cdr (assoc old prop-alist))))
|
|
825 (open (and new (cdr (assoc new prop-alist)))))
|
|
826 (if (or close open)
|
|
827 (format-make-relatively-unique close open)
|
|
828 ;; Call "Default" function, if any
|
|
829 (let ((default (assq nil prop-alist)))
|
|
830 (if default
|
|
831 (funcall (car (cdr default)) old new))))))))
|
|
832
|
|
833 ;;; format.el ends here
|