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