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