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