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