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