Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / format.el
CommitLineData
a8581027 1;;; format.el --- read and save files in multiple formats
b578f267 2
c90f2757 3;; Copyright (C) 1994, 1995, 1997, 1999, 2001, 2002, 2003, 2004,
409cc4a3 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
029894b9 5
823139fb 6;; Author: Boris Goldowsky <boris@gnu.org>
029894b9
BG
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
b4aa6026 12;; the Free Software Foundation; either version 3, or (at your option)
029894b9 13;; any later version.
b578f267 14
029894b9
BG
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.
b578f267 19
029894b9 20;; You should have received a copy of the GNU General Public License
b578f267 21;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
029894b9
BG
24
25;;; Commentary:
b578f267
EN
26
27;; This file defines a unified mechanism for saving & loading files stored
28;; in different formats. `format-alist' contains information that directs
029894b9 29;; Emacs to call an encoding or decoding function when reading or writing
823139fb 30;; files that match certain conditions.
029894b9 31;;
b578f267
EN
32;; When a file is visited, its format is determined by matching the
33;; beginning of the file against regular expressions stored in
34;; `format-alist'. If this fails, you can manually translate the buffer
35;; using `format-decode-buffer'. In either case, the formats used are
36;; listed in the variable `buffer-file-format', and become the default
37;; format for saving the buffer. To save a buffer in a different format,
38;; change this variable, or use `format-write-file'.
029894b9
BG
39;;
40;; Auto-save files are normally created in the same format as the visited
4a2255ad 41;; file, but the variable `buffer-auto-save-file-format' can be set to a
b578f267
EN
42;; particularly fast or otherwise preferred format to be used for
43;; auto-saving (or nil to do no encoding on auto-save files, but then you
44;; risk losing any text-properties in the buffer).
029894b9 45;;
b578f267
EN
46;; You can manually translate a buffer into or out of a particular format
47;; with the functions `format-encode-buffer' and `format-decode-buffer'.
48;; To translate just the region use the functions `format-encode-region'
823139fb 49;; and `format-decode-region'.
029894b9 50;;
b578f267
EN
51;; You can define a new format by writing the encoding and decoding
52;; functions, and adding an entry to `format-alist'. See enriched.el for
53;; an example of how to implement a file format. There are various
54;; functions defined in this file that may be useful for writing the
55;; encoding and decoding functions:
56;; * `format-annotate-region' and `format-deannotate-region' allow a
57;; single alist of information to be used for encoding and decoding.
58;; The alist defines a correspondence between strings in the file
59;; ("annotations") and text-properties in the buffer.
029894b9
BG
60;; * `format-replace-strings' is similarly useful for doing simple
61;; string->string translations in a reversible manner.
62
b578f267
EN
63;;; Code:
64
029894b9 65(put 'buffer-file-format 'permanent-local t)
4a2255ad 66(put 'buffer-auto-save-file-format 'permanent-local t)
029894b9 67
823139fb 68(defvar format-alist
8d0dd9d2 69 '((text/enriched "Extended MIME text/enriched format."
029894b9
BG
70 "Content-[Tt]ype:[ \t]*text/enriched"
71 enriched-decode enriched-encode t enriched-mode)
c84cf2bf 72 (plain "ISO 8859-1 standard format, no text properties."
029894b9
BG
73 ;; Plain only exists so that there is an obvious neutral choice in
74 ;; the completion list.
c84cf2bf 75 nil nil nil nil nil)
823139fb
DL
76 (TeX "TeX (encoding)"
77 nil
c84cf2bf 78 iso-tex2iso iso-iso2tex t nil)
823139fb
DL
79 (gtex "German TeX (encoding)"
80 nil
c84cf2bf 81 iso-gtex2iso iso-iso2gtex t nil)
f1d6fe69 82 (html "HTML/SGML \"ISO 8879:1986//ENTITIES Added Latin 1//EN\" (encoding)"
823139fb 83 nil
f1d6fe69 84 iso-sgml2iso iso-iso2sgml t nil)
823139fb
DL
85 (rot13 "rot13"
86 nil
c84cf2bf 87 "tr a-mn-z n-za-m" "tr a-mn-z n-za-m" t nil)
823139fb
DL
88 (duden "Duden Ersatzdarstellung"
89 nil
90 "diac" iso-iso2duden t nil)
91 (de646 "German ASCII (ISO 646)"
92 nil
271af233 93 "recode -f iso646-ge:latin1" "recode -f latin1:iso646-ge" t nil)
823139fb
DL
94 (denet "net German"
95 nil
c84cf2bf 96 iso-german iso-cvt-read-only t nil)
823139fb
DL
97 (esnet "net Spanish"
98 nil
8d0dd9d2 99 iso-spanish iso-cvt-read-only t nil))
029894b9
BG
100 "List of information about understood file formats.
101Elements are of the form \(NAME DOC-STR REGEXP FROM-FN TO-FN MODIFY MODE-FN).
d1ae737b 102
029894b9 103NAME is a symbol, which is stored in `buffer-file-format'.
d1ae737b 104
029894b9
BG
105DOC-STR should be a single line providing more information about the
106 format. It is currently unused, but in the future will be shown to
107 the user if they ask for more information.
d1ae737b 108
029894b9 109REGEXP is a regular expression to match against the beginning of the file;
823139fb 110 it should match only files in that format. Use nil to avoid
12008438 111 matching at all for formats for which it isn't appropriate to
823139fb 112 require explicit encoding/decoding.
d1ae737b 113
12008438 114FROM-FN is called to decode files in that format; it takes two args, BEGIN
029894b9
BG
115 and END, and can make any modifications it likes, returning the new
116 end. It must make sure that the beginning of the file no longer
117 matches REGEXP, or else it will get called again.
d1ae737b
RS
118 Alternatively, FROM-FN can be a string, which specifies a shell command
119 (including options) to be used as a filter to perform the conversion.
120
12008438 121TO-FN is called to encode a region into that format; it takes three
d1abb42f
RS
122 arguments: BEGIN, END, and BUFFER. BUFFER is the original buffer that
123 the data being written came from, which the function could use, for
124 example, to find the values of local variables. TO-FN should either
125 return a list of annotations like `write-region-annotate-functions',
126 or modify the region and return the new end.
d1ae737b
RS
127 Alternatively, TO-FN can be a string, which specifies a shell command
128 (including options) to be used as a filter to perform the conversion.
129
d1abb42f
RS
130MODIFY, if non-nil, means the TO-FN wants to modify the region. If nil,
131 TO-FN will not make any changes but will instead return a list of
823139fb 132 annotations.
d1ae737b 133
3e224645
EZ
134MODE-FN, if specified, is called when visiting a file with that format.
135 It is called with a single positive argument, on the assumption
12008438 136 that this would turn on some minor mode.
34f30f79
RS
137
138PRESERVE, if non-nil, means that `format-write-file' should not remove
ec8dda2f 139 this format from `buffer-file-format'.")
029894b9
BG
140
141;;; Basic Functions (called from Lisp)
142
c84cf2bf 143(defun format-encode-run-method (method from to &optional buffer)
12008438
TTN
144 "Translate using METHOD the text from FROM to TO.
145If METHOD is a string, it is a shell command (including options);
c84cf2bf
RS
146otherwise, it should be a Lisp function.
147BUFFER should be the buffer that the output originally came from."
148 (if (stringp method)
823139fb 149 (let ((error-buff (get-buffer-create "*Format Errors*"))
7cc45a35 150 (coding-system-for-read 'no-conversion)
823139fb
DL
151 format-alist)
152 (with-current-buffer error-buff
153 (widen)
154 (erase-buffer))
10fa0073
GM
155 (if (and (zerop (save-window-excursion
156 (shell-command-on-region from to method t t
157 error-buff)))
823139fb
DL
158 ;; gzip gives zero exit status with bad args, for instance.
159 (zerop (with-current-buffer error-buff
160 (buffer-size))))
161 (bury-buffer error-buff)
162 (switch-to-buffer-other-window error-buff)
10fa0073 163 (error "Format encoding failed")))
c84cf2bf
RS
164 (funcall method from to buffer)))
165
166(defun format-decode-run-method (method from to &optional buffer)
12008438
TTN
167 "Decode using METHOD the text from FROM to TO.
168If METHOD is a string, it is a shell command (including options); otherwise,
169it should be a Lisp function. Decoding is done for the given BUFFER."
c84cf2bf 170 (if (stringp method)
823139fb 171 (let ((error-buff (get-buffer-create "*Format Errors*"))
7cc45a35 172 (coding-system-for-write 'no-conversion)
823139fb
DL
173 format-alist)
174 (with-current-buffer error-buff
175 (widen)
176 (erase-buffer))
177 ;; We should perhaps go via a temporary buffer and copy it
178 ;; back, in case of errors.
179 (if (and (zerop (save-window-excursion
180 (shell-command-on-region (point-min) (point-max)
181 method t t
182 error-buff)))
183 ;; gzip gives zero exit status with bad args, for instance.
184 (zerop (with-current-buffer error-buff
185 (buffer-size))))
186 (bury-buffer error-buff)
187 (switch-to-buffer-other-window error-buff)
188 (error "Format decoding failed"))
c84cf2bf
RS
189 (point))
190 (funcall method from to)))
191
10fa0073 192(defun format-annotate-function (format from to orig-buf format-count)
823139fb 193 "Return annotations for writing region as FORMAT.
12008438
TTN
194FORMAT is a symbol naming one of the formats defined in `format-alist'.
195It must be a single symbol, not a list like `buffer-file-format'.
d1abb42f
RS
196FROM and TO delimit the region to be operated on in the current buffer.
197ORIG-BUF is the original buffer that the data came from.
10fa0073
GM
198
199FORMAT-COUNT is an integer specifying how many times this function has
200been called in the process of decoding ORIG-BUF.
201
12008438 202This function works like a function in `write-region-annotate-functions':
029894b9 203it either returns a list of annotations, or returns with a different buffer
10fa0073
GM
204current, which contains the modified text to write. In the latter case,
205this function's value is nil.
029894b9
BG
206
207For most purposes, consider using `format-encode-region' instead."
10fa0073
GM
208 ;; This function is called by write-region (actually
209 ;; build_annotations) for each element of buffer-file-format.
029894b9
BG
210 (let* ((info (assq format format-alist))
211 (to-fn (nth 4 info))
212 (modify (nth 5 info)))
213 (if to-fn
214 (if modify
215 ;; To-function wants to modify region. Copy to safe place.
10fa0073 216 (let ((copy-buf (get-buffer-create (format " *Format Temp %d*"
8a2f1176
RS
217 format-count)))
218 (sel-disp selective-display)
5258c763
JB
219 (multibyte enable-multibyte-characters)
220 (coding-system buffer-file-coding-system))
8a2f1176
RS
221 (with-current-buffer copy-buf
222 (setq selective-display sel-disp)
5258c763
JB
223 (set-buffer-multibyte multibyte)
224 (setq buffer-file-coding-system coding-system))
029894b9
BG
225 (copy-to-buffer copy-buf from to)
226 (set-buffer copy-buf)
227 (format-insert-annotations write-region-annotations-so-far from)
c84cf2bf 228 (format-encode-run-method to-fn (point-min) (point-max) orig-buf)
029894b9
BG
229 nil)
230 ;; Otherwise just call function, it will return annotations.
d1abb42f 231 (funcall to-fn from to orig-buf)))))
029894b9
BG
232
233(defun format-decode (format length &optional visit-flag)
234 ;; This function is called by insert-file-contents whenever a file is read.
235 "Decode text from any known FORMAT.
823139fb 236FORMAT is a symbol appearing in `format-alist' or a list of such symbols,
029894b9
BG
237or nil, in which case this function tries to guess the format of the data by
238matching against the regular expressions in `format-alist'. After a match is
239found and the region decoded, the alist is searched again from the beginning
240for another match.
241
242Second arg LENGTH is the number of characters following point to operate on.
243If optional third arg VISIT-FLAG is true, set `buffer-file-format'
be227f22
SM
244to the reverted list of formats used, and call any mode functions defined
245for those formats.
029894b9 246
12008438 247Return the new length of the decoded region.
029894b9
BG
248
249For most purposes, consider using `format-decode-region' instead."
250 (let ((mod (buffer-modified-p))
10fa0073 251 (begin (point))
029894b9 252 (end (+ (point) length)))
10fa0073
GM
253 (unwind-protect
254 (progn
255 ;; Don't record undo information for the decoding.
71296446 256
10fa0073
GM
257 (if (null format)
258 ;; Figure out which format it is in, remember list in `format'.
259 (let ((try format-alist))
260 (while try
261 (let* ((f (car try))
262 (regexp (nth 2 f))
263 (p (point)))
264 (if (and regexp (looking-at regexp)
265 (< (match-end 0) (+ begin length)))
266 (progn
be227f22 267 (push (car f) format)
10fa0073
GM
268 ;; Decode it
269 (if (nth 3 f)
270 (setq end (format-decode-run-method (nth 3 f) begin end)))
271 ;; Call visit function if required
272 (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1))
273 ;; Safeguard against either of the functions changing pt.
274 (goto-char p)
275 ;; Rewind list to look for another format
276 (setq try format-alist))
277 (setq try (cdr try))))))
278 ;; Deal with given format(s)
279 (or (listp format) (setq format (list format)))
280 (let ((do format) f)
281 (while do
282 (or (setq f (assq (car do) format-alist))
6b61353c 283 (error "Unknown format %s" (car do)))
10fa0073
GM
284 ;; Decode:
285 (if (nth 3 f)
286 (setq end (format-decode-run-method (nth 3 f) begin end)))
287 ;; Call visit function if required
288 (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1))
be227f22
SM
289 (setq do (cdr do))))
290 ;; Encode in the opposite order.
291 (setq format (reverse format)))
10fa0073
GM
292 (if visit-flag
293 (setq buffer-file-format format)))
71296446 294
021cfbea
GM
295 (set-buffer-modified-p mod))
296
10fa0073 297 ;; Return new length of region
029894b9
BG
298 (- end begin)))
299
300;;;
301;;; Interactive functions & entry points
302;;;
303
304(defun format-decode-buffer (&optional format)
305 "Translate the buffer from some FORMAT.
12008438
TTN
306If the format is not specified, attempt a regexp-based guess.
307Set `buffer-file-format' to the format used, and call any
308format-specific mode functions."
029894b9 309 (interactive
5b76833f 310 (list (format-read "Translate buffer from format (default guess): ")))
029894b9
BG
311 (save-excursion
312 (goto-char (point-min))
313 (format-decode format (buffer-size) t)))
314
315(defun format-decode-region (from to &optional format)
316 "Decode the region from some format.
317Arg FORMAT is optional; if omitted the format will be determined by looking
318for identifying regular expressions at the beginning of the region."
319 (interactive
823139fb 320 (list (region-beginning) (region-end)
5b76833f 321 (format-read "Translate region from format (default guess): ")))
029894b9
BG
322 (save-excursion
323 (goto-char from)
324 (format-decode format (- to from) nil)))
325
326(defun format-encode-buffer (&optional format)
327 "Translate the buffer into FORMAT.
328FORMAT defaults to `buffer-file-format'. It is a symbol naming one of the
329formats defined in `format-alist', or a list of such symbols."
330 (interactive
331 (list (format-read (format "Translate buffer to format (default %s): "
332 buffer-file-format))))
333 (format-encode-region (point-min) (point-max) format))
334
335(defun format-encode-region (beg end &optional format)
823139fb 336 "Translate the region into some FORMAT.
12008438 337FORMAT defaults to `buffer-file-format'. It is a symbol naming
029894b9 338one of the formats defined in `format-alist', or a list of such symbols."
823139fb
DL
339 (interactive
340 (list (region-beginning) (region-end)
341 (format-read (format "Translate region to format (default %s): "
342 buffer-file-format))))
343 (if (null format) (setq format buffer-file-format))
344 (if (symbolp format) (setq format (list format)))
345 (save-excursion
346 (goto-char end)
347 (let ((cur-buf (current-buffer))
348 (end (point-marker)))
349 (while format
350 (let* ((info (assq (car format) format-alist))
351 (to-fn (nth 4 info))
352 (modify (nth 5 info))
353 result)
354 (if to-fn
355 (if modify
356 (setq end (format-encode-run-method to-fn beg end
357 (current-buffer)))
358 (format-insert-annotations
359 (funcall to-fn beg end (current-buffer)))))
360 (setq format (cdr format)))))))
029894b9 361
6b61353c 362(defun format-write-file (filename format &optional confirm)
823139fb 363 "Write current buffer into file FILENAME using some FORMAT.
6b61353c 364Make buffer visit that file and set the format as the default for future
029894b9 365saves. If the buffer is already visiting a file, you can specify a directory
6b61353c
KH
366name as FILENAME, to write a file of the same old name in that directory.
367
12008438
TTN
368If optional third arg CONFIRM is non-nil, ask for confirmation before
369overwriting an existing file. Interactively, confirmation is required
370unless you supply a prefix argument."
029894b9
BG
371 (interactive
372 ;; Same interactive spec as write-file, plus format question.
373 (let* ((file (if buffer-file-name
374 (read-file-name "Write file: "
375 nil nil nil nil)
376 (read-file-name "Write file: "
377 (cdr (assq 'default-directory
378 (buffer-local-variables)))
379 nil nil (buffer-name))))
823139fb 380 (fmt (format-read (format "Write file `%s' in format: "
029894b9 381 (file-name-nondirectory file)))))
6b61353c 382 (list file fmt (not current-prefix-arg))))
34f30f79
RS
383 (let ((old-formats buffer-file-format)
384 preserve-formats)
385 (dolist (fmt old-formats)
386 (let ((aelt (assq fmt format-alist)))
387 (if (nth 7 aelt)
388 (push fmt preserve-formats))))
389 (setq buffer-file-format format)
390 (dolist (fmt preserve-formats)
391 (unless (memq fmt buffer-file-format)
392 (setq buffer-file-format (append buffer-file-format (list fmt))))))
6b61353c 393 (write-file filename confirm))
029894b9 394
ce6af52b 395(defun format-find-file (filename format)
823139fb 396 "Find the file FILENAME using data format FORMAT.
ce6af52b
KH
397If FORMAT is nil then do not do any format conversion."
398 (interactive
399 ;; Same interactive spec as write-file, plus format question.
400 (let* ((file (read-file-name "Find file: "))
823139fb 401 (fmt (format-read (format "Read file `%s' in format: "
ce6af52b
KH
402 (file-name-nondirectory file)))))
403 (list file fmt)))
404 (let ((format-alist nil))
405 (find-file filename))
406 (if format
407 (format-decode-buffer format)))
408
409(defun format-insert-file (filename format &optional beg end)
823139fb 410 "Insert the contents of file FILENAME using data format FORMAT.
ce6af52b
KH
411If FORMAT is nil then do not do any format conversion.
412The optional third and fourth arguments BEG and END specify
12008438 413the part (in bytes) of the file to read.
ce6af52b
KH
414
415The return value is like the value of `insert-file-contents':
6b61353c 416a list (ABSOLUTE-FILE-NAME SIZE)."
ce6af52b
KH
417 (interactive
418 ;; Same interactive spec as write-file, plus format question.
419 (let* ((file (read-file-name "Find file: "))
823139fb 420 (fmt (format-read (format "Read file `%s' in format: "
ce6af52b
KH
421 (file-name-nondirectory file)))))
422 (list file fmt)))
f70aa678
MR
423 (let (value size old-undo)
424 ;; Record only one undo entry for the insertion. Inhibit point-motion and
425 ;; modification hooks as with `insert-file-contents'.
426 (let ((inhibit-point-motion-hooks t)
427 (inhibit-modification-hooks t))
428 ;; Don't bind `buffer-undo-list' to t here to assert that
429 ;; `insert-file-contents' may record whether the buffer was unmodified
430 ;; before.
431 (let ((format-alist nil))
432 (setq value (insert-file-contents filename nil beg end))
433 (setq size (nth 1 value)))
434 (when (consp buffer-undo-list)
435 (let ((head (car buffer-undo-list)))
436 (when (and (consp head)
437 (equal (car head) (point))
438 (equal (cdr head) (+ (point) size)))
439 ;; Remove first entry from `buffer-undo-list', we shall insert
440 ;; another one below.
441 (setq old-undo (cdr buffer-undo-list)))))
442 (when format
443 (let ((buffer-undo-list t))
444 (setq size (format-decode format size)
445 value (list (car value) size)))
446 (unless (eq buffer-undo-list t)
447 (setq buffer-undo-list
448 (cons (cons (point) (+ (point) size)) old-undo)))))
449 (unless inhibit-modification-hooks
450 (run-hook-with-args 'after-change-functions (point) (+ (point) size) 0))
ce6af52b
KH
451 value))
452
029894b9
BG
453(defun format-read (&optional prompt)
454 "Read and return the name of a format.
455Return value is a list, like `buffer-file-format'; it may be nil.
456Formats are defined in `format-alist'. Optional arg is the PROMPT to use."
457 (let* ((table (mapcar (lambda (x) (list (symbol-name (car x))))
458 format-alist))
459 (ans (completing-read (or prompt "Format: ") table nil t)))
460 (if (not (equal "" ans)) (list (intern ans)))))
461
462
463;;;
464;;; Below are some functions that may be useful in writing encoding and
465;;; decoding functions for use in format-alist.
466;;;
467
468(defun format-replace-strings (alist &optional reverse beg end)
469 "Do multiple replacements on the buffer.
d0fc7e3b 470ALIST is a list of (FROM . TO) pairs, which should be proper arguments to
12008438
TTN
471`search-forward' and `replace-match', respectively.
472Optional second arg REVERSE, if non-nil, means the pairs are (TO . FROM),
473so that you can use the same list in both directions if it contains only
474literal strings.
823139fb 475Optional args BEG and END specify a region of the buffer on which to operate."
029894b9
BG
476 (save-excursion
477 (save-restriction
478 (or beg (setq beg (point-min)))
479 (if end (narrow-to-region (point-min) end))
480 (while alist
481 (let ((from (if reverse (cdr (car alist)) (car (car alist))))
2c3ac81d 482 (to (if reverse (car (car alist)) (cdr (car alist)))))
029894b9
BG
483 (goto-char beg)
484 (while (search-forward from nil t)
485 (goto-char (match-beginning 0))
486 (insert to)
487 (set-text-properties (- (point) (length to)) (point)
488 (text-properties-at (point)))
489 (delete-region (point) (+ (point) (- (match-end 0)
490 (match-beginning 0)))))
491 (setq alist (cdr alist)))))))
492
493;;; Some list-manipulation functions that we need.
494
495(defun format-delq-cons (cons list)
823139fb
DL
496 "Remove the given CONS from LIST by side effect and return the new LIST.
497Since CONS could be the first element of LIST, write
498`\(setq foo \(format-delq-cons element foo))' to be sure of changing
499the value of `foo'."
029894b9
BG
500 (if (eq cons list)
501 (cdr list)
502 (let ((p list))
503 (while (not (eq (cdr p) cons))
e8af40ee 504 (if (null p) (error "format-delq-cons: not an element"))
029894b9
BG
505 (setq p (cdr p)))
506 ;; Now (cdr p) is the cons to delete
507 (setcdr p (cdr cons))
508 list)))
71296446 509
029894b9
BG
510(defun format-make-relatively-unique (a b)
511 "Delete common elements of lists A and B, return as pair.
12008438 512Compare using `equal'."
029894b9
BG
513 (let* ((acopy (copy-sequence a))
514 (bcopy (copy-sequence b))
515 (tail acopy))
516 (while tail
517 (let ((dup (member (car tail) bcopy))
518 (next (cdr tail)))
519 (if dup (setq acopy (format-delq-cons tail acopy)
520 bcopy (format-delq-cons dup bcopy)))
521 (setq tail next)))
522 (cons acopy bcopy)))
523
524(defun format-common-tail (a b)
525 "Given two lists that have a common tail, return it.
12008438 526Compare with `equal', and return the part of A that is equal to the
029894b9 527equivalent part of B. If even the last items of the two are not equal,
12008438 528return nil."
029894b9
BG
529 (let ((la (length a))
530 (lb (length b)))
531 ;; Make sure they are the same length
823139fb 532 (if (> la lb)
029894b9
BG
533 (setq a (nthcdr (- la lb) a))
534 (setq b (nthcdr (- lb la) b))))
535 (while (not (equal a b))
536 (setq a (cdr a)
537 b (cdr b)))
538 a)
539
f0a6c717
GM
540(defun format-proper-list-p (list)
541 "Return t if LIST is a proper list.
542A proper list is a list ending with a nil cdr, not with an atom "
543 (when (listp list)
544 (while (consp list)
545 (setq list (cdr list)))
546 (null list)))
547
029894b9 548(defun format-reorder (items order)
12008438
TTN
549 "Arrange ITEMS to follow partial ORDER.
550Elements of ITEMS equal to elements of ORDER will be rearranged
551to follow the ORDER. Unmatched items will go last."
029894b9
BG
552 (if order
553 (let ((item (member (car order) items)))
554 (if item
823139fb 555 (cons (car item)
029894b9
BG
556 (format-reorder (format-delq-cons item items)
557 (cdr order)))
558 (format-reorder items (cdr order))))
559 items))
560
561(put 'face 'format-list-valued t) ; These text-properties take values
562(put 'unknown 'format-list-valued t) ; that are lists, the elements of which
563 ; should be considered separately.
564 ; See format-deannotate-region and
565 ; format-annotate-region.
566
f3bbef87
GM
567;; This text property has list values, but they are treated atomically.
568
569(put 'display 'format-list-atomic-p t)
570
029894b9
BG
571;;;
572;;; Decoding
573;;;
574
575(defun format-deannotate-region (from to translations next-fn)
576 "Translate annotations in the region into text properties.
823139fb 577This sets text properties between FROM to TO as directed by the
029894b9
BG
578TRANSLATIONS and NEXT-FN arguments.
579
580NEXT-FN is a function that searches forward from point for an annotation.
581It should return a list of 4 elements: \(BEGIN END NAME POSITIVE). BEGIN and
582END are buffer positions bounding the annotation, NAME is the name searched
583for in TRANSLATIONS, and POSITIVE should be non-nil if this annotation marks
584the beginning of a region with some property, or nil if it ends the region.
585NEXT-FN should return nil if there are no annotations after point.
586
587The basic format of the TRANSLATIONS argument is described in the
588documentation for the `format-annotate-region' function. There are some
589additional things to keep in mind for decoding, though:
590
591When an annotation is found, the TRANSLATIONS list is searched for a
592text-property name and value that corresponds to that annotation. If the
593text-property has several annotations associated with it, it will be used only
594if the other annotations are also in effect at that point. The first match
595found whose annotations are all present is used.
596
597The text property thus determined is set to the value over the region between
598the opening and closing annotations. However, if the text-property name has a
599non-nil `format-list-valued' property, then the value will be consed onto the
600surrounding value of the property, rather than replacing that value.
601
602There are some special symbols that can be used in the \"property\" slot of
603the TRANSLATIONS list: PARAMETER and FUNCTION \(spelled in uppercase).
604Annotations listed under the pseudo-property PARAMETER are considered to be
605arguments of the immediately surrounding annotation; the text between the
606opening and closing parameter annotations is deleted from the buffer but saved
92308d3a
RS
607as a string.
608
609The surrounding annotation should be listed under the pseudo-property
610FUNCTION. Instead of inserting a text-property for this annotation,
611the function listed in the VALUE slot is called to make whatever
612changes are appropriate. It can also return a list of the form
613\(START LOC PROP VALUE) which specifies a property to put on. The
614function's first two arguments are the START and END locations, and
615the rest of the arguments are any PARAMETERs found in that region.
029894b9
BG
616
617Any annotations that are found by NEXT-FN but not defined by TRANSLATIONS
618are saved as values of the `unknown' text-property \(which is list-valued).
619The TRANSLATIONS list should usually contain an entry of the form
620 \(unknown \(nil format-annotate-value))
621to write these unknown annotations back into the file."
622 (save-excursion
623 (save-restriction
624 (narrow-to-region (point-min) to)
625 (goto-char from)
626 (let (next open-ans todo loc unknown-ans)
627 (while (setq next (funcall next-fn))
628 (let* ((loc (nth 0 next))
629 (end (nth 1 next))
630 (name (nth 2 next))
631 (positive (nth 3 next))
632 (found nil))
633
634 ;; Delete the annotation
635 (delete-region loc end)
22828206
RS
636 (cond
637 ;; Positive annotations are stacked, remembering location
be227f22 638 (positive (push `(,name ((,loc . nil))) open-ans))
22828206
RS
639 ;; It is a negative annotation:
640 ;; Close the top annotation & add its text property.
641 ;; If the file's nesting is messed up, the close might not match
642 ;; the top thing on the open-annotations stack.
643 ;; If no matching annotation is open, just ignore the close.
644 ((not (assoc name open-ans))
645 (message "Extra closing annotation (%s) in file" name))
646 ;; If one is open, but not on the top of the stack, close
647 ;; the things in between as well. Set `found' when the real
648 ;; one is closed.
649 (t
650 (while (not found)
651 (let* ((top (car open-ans)) ; first on stack: should match.
652 (top-name (car top)) ; text property name
653 (top-extents (nth 1 top)) ; property regions
654 (params (cdr (cdr top))) ; parameters
655 (aalist translations)
656 (matched nil))
657 (if (equal name top-name)
658 (setq found t)
659 (message "Improper nesting in file."))
660 ;; Look through property names in TRANSLATIONS
661 (while aalist
662 (let ((prop (car (car aalist)))
663 (alist (cdr (car aalist))))
664 ;; And look through values for each property
665 (while alist
666 (let ((value (car (car alist)))
667 (ans (cdr (car alist))))
668 (if (member top-name ans)
669 ;; This annotation is listed, but still have to
670 ;; check if multiple annotations are satisfied
671 (if (member nil (mapcar (lambda (r)
672 (assoc r open-ans))
673 ans))
674 nil ; multiple ans not satisfied
675 ;; If there are multiple annotations going
676 ;; into one text property, split up the other
677 ;; annotations so they apply individually to
678 ;; the other regions.
679 (setcdr (car top-extents) loc)
680 (let ((to-split ans) this-one extents)
681 (while to-split
682 (setq this-one
683 (assoc (car to-split) open-ans)
684 extents (nth 1 this-one))
685 (if (not (eq this-one top))
686 (setcar (cdr this-one)
687 (format-subtract-regions
688 extents top-extents)))
689 (setq to-split (cdr to-split))))
690 ;; Set loop variables to nil so loop
691 ;; will exit.
692 (setq alist nil aalist nil matched t
693 ;; pop annotation off stack.
694 open-ans (cdr open-ans))
695 (let ((extents top-extents)
696 (start (car (car top-extents)))
697 (loc (cdr (car top-extents))))
698 (while extents
699 (cond
700 ;; Check for pseudo-properties
701 ((eq prop 'PARAMETER)
702 ;; A parameter of the top open ann:
703 ;; delete text and use as arg.
704 (if open-ans
705 ;; (If nothing open, discard).
706 (setq open-ans
707 (cons
708 (append (car open-ans)
709 (list
710 (buffer-substring
711 start loc)))
712 (cdr open-ans))))
713 (delete-region start loc))
714 ((eq prop 'FUNCTION)
715 ;; Not a property, but a function.
716 (let ((rtn
717 (apply value start loc params)))
be227f22 718 (if rtn (push rtn todo))))
22828206
RS
719 (t
720 ;; Normal property/value pair
721 (setq todo
722 (cons (list start loc prop value)
723 todo))))
724 (setq extents (cdr extents)
725 start (car (car extents))
726 loc (cdr (car extents))))))))
727 (setq alist (cdr alist))))
728 (setq aalist (cdr aalist)))
729 (if (not matched)
029894b9
BG
730 ;; Didn't find any match for the annotation:
731 ;; Store as value of text-property `unknown'.
22828206
RS
732 (let ((extents top-extents)
733 (start (car (car top-extents)))
0b140219 734 (loc (or (cdr (car top-extents)) loc)))
22828206
RS
735 (while extents
736 (setq open-ans (cdr open-ans)
737 todo (cons (list start loc 'unknown top-name)
738 todo)
739 unknown-ans (cons name unknown-ans)
740 extents (cdr extents)
741 start (car (car extents))
742 loc (cdr (car extents))))))))))))
029894b9
BG
743
744 ;; Once entire file has been scanned, add the properties.
745 (while todo
746 (let* ((item (car todo))
747 (from (nth 0 item))
748 (to (nth 1 item))
749 (prop (nth 2 item))
750 (val (nth 3 item)))
22828206
RS
751
752 (if (numberp val) ; add to ambient value if numeric
753 (format-property-increment-region from to prop val 0)
754 (put-text-property
029894b9 755 from to prop
22828206 756 (cond ((get prop 'format-list-valued) ; value gets consed onto
029894b9
BG
757 ; list-valued properties
758 (let ((prev (get-text-property from prop)))
759 (cons val (if (listp prev) prev (list prev)))))
22828206 760 (t val))))) ; normally, just set to val.
029894b9 761 (setq todo (cdr todo)))
22828206 762
029894b9
BG
763 (if unknown-ans
764 (message "Unknown annotations: %s" unknown-ans))))))
765
22828206 766(defun format-subtract-regions (minu subtra)
9c3054da 767 "Remove from the regions in MINUEND the regions in SUBTRAHEND.
d0fc7e3b 768A region is a dotted pair (FROM . TO). Both parameters are lists of
823139fb
DL
769regions. Each list must contain nonoverlapping, noncontiguous
770regions, in descending order. The result is also nonoverlapping,
771noncontiguous, and in descending order. The first element of MINUEND
772can have a cdr of nil, indicating that the end of that region is not
9c3054da
JB
773yet known.
774
775\(fn MINUEND SUBTRAHEND)"
22828206
RS
776 (let* ((minuend (copy-alist minu))
777 (subtrahend (copy-alist subtra))
778 (m (car minuend))
779 (s (car subtrahend))
780 results)
781 (while (and minuend subtrahend)
823139fb 782 (cond
22828206
RS
783 ;; The minuend starts after the subtrahend ends; keep it.
784 ((> (car m) (cdr s))
be227f22
SM
785 (push m results)
786 (setq minuend (cdr minuend)
22828206
RS
787 m (car minuend)))
788 ;; The minuend extends beyond the end of the subtrahend. Chop it off.
789 ((or (null (cdr m)) (> (cdr m) (cdr s)))
be227f22 790 (push (cons (1+ (cdr s)) (cdr m)) results)
22828206
RS
791 (setcdr m (cdr s)))
792 ;; The subtrahend starts after the minuend ends; throw it away.
793 ((< (cdr m) (car s))
794 (setq subtrahend (cdr subtrahend) s (car subtrahend)))
795 ;; The subtrahend extends beyond the end of the minuend. Chop it off.
796 (t ;(<= (cdr m) (cdr s)))
797 (if (>= (car m) (car s))
798 (setq minuend (cdr minuend) m (car minuend))
799 (setcdr m (1- (car s)))
800 (setq subtrahend (cdr subtrahend) s (car subtrahend))))))
801 (nconc (nreverse results) minuend)))
802
803;; This should probably go somewhere other than format.el. Then again,
804;; indent.el has alter-text-property. NOTE: We can also use
805;; next-single-property-change instead of text-property-not-all, but then
806;; we have to see if we passed TO.
807(defun format-property-increment-region (from to prop delta default)
12008438 808 "In the region from FROM to TO increment property PROP by amount DELTA.
823139fb 809DELTA may be negative. If property PROP is nil anywhere
22828206
RS
810in the region, it is treated as though it were DEFAULT."
811 (let ((cur from) val newval next)
812 (while cur
813 (setq val (get-text-property cur prop)
814 newval (+ (or val default) delta)
815 next (text-property-not-all cur to prop val))
816 (put-text-property cur (or next to) prop newval)
817 (setq cur next))))
818
029894b9
BG
819;;;
820;;; Encoding
821;;;
822
823(defun format-insert-annotations (list &optional offset)
824 "Apply list of annotations to buffer as `write-region' would.
12008438 825Insert each element of the given LIST of buffer annotations at its
029894b9
BG
826appropriate place. Use second arg OFFSET if the annotations' locations are
827not relative to the beginning of the buffer: annotations will be inserted
9c3054da
JB
828at their location-OFFSET+1 \(ie, the offset is treated as the position of
829the first character in the buffer)."
823139fb 830 (if (not offset)
029894b9
BG
831 (setq offset 0)
832 (setq offset (1- offset)))
833 (let ((l (reverse list)))
834 (while l
835 (goto-char (- (car (car l)) offset))
836 (insert (cdr (car l)))
837 (setq l (cdr l)))))
838
839(defun format-annotate-value (old new)
0b1ce6ba 840 "Return OLD and NEW as a \(CLOSE . OPEN) annotation pair.
029894b9
BG
841Useful as a default function for TRANSLATIONS alist when the value of the text
842property is the name of the annotation that you want to use, as it is for the
843`unknown' text property."
844 (cons (if old (list old))
845 (if new (list new))))
846
823139fb 847(defun format-annotate-region (from to translations format-fn ignore)
029894b9 848 "Generate annotations for text properties in the region.
12008438 849Search for changes between FROM and TO, and describe them with a list of
029894b9
BG
850annotations as defined by alist TRANSLATIONS and FORMAT-FN. IGNORE lists text
851properties not to consider; any text properties that are neither ignored nor
852listed in TRANSLATIONS are warned about.
853If you actually want to modify the region, give the return value of this
854function to `format-insert-annotations'.
855
856Format of the TRANSLATIONS argument:
857
858Each element is a list whose car is a PROPERTY, and the following
d0fc7e3b
RS
859elements have the form (VALUE ANNOTATIONS...).
860Whenever the property takes on the value VALUE, the annotations
029894b9
BG
861\(as formatted by FORMAT-FN) are inserted into the file.
862When the property stops having that value, the matching negated annotation
863will be inserted \(it may actually be closed earlier and reopened, if
823139fb 864necessary, to keep proper nesting).
029894b9 865
d0fc7e3b 866If VALUE is a list, then each element of the list is dealt with
029894b9
BG
867separately.
868
869If a VALUE is numeric, then it is assumed that there is a single annotation
870and each occurrence of it increments the value of the property by that number.
871Thus, given the entry \(left-margin \(4 \"indent\")), if the left margin
872changes from 4 to 12, two <indent> annotations will be generated.
873
874If the VALUE is nil, then instead of annotations, a function should be
875specified. This function is used as a default: it is called for all
876transitions not explicitly listed in the table. The function is called with
877two arguments, the OLD and NEW values of the property. It should return
d0fc7e3b 878a cons cell (CLOSE . OPEN) as `format-annotate-single-property-change' does.
029894b9 879
d0fc7e3b 880The same TRANSLATIONS structure can be used in reverse for reading files."
029894b9
BG
881 (let ((all-ans nil) ; All annotations - becomes return value
882 (open-ans nil) ; Annotations not yet closed
883 (loc nil) ; Current location
884 (not-found nil)) ; Properties that couldn't be saved
885 (while (or (null loc)
886 (and (setq loc (next-property-change loc nil to))
887 (< loc to)))
888 (or loc (setq loc from))
823139fb 889 (let* ((ans (format-annotate-location loc (= loc from) ignore translations))
029894b9
BG
890 (neg-ans (format-reorder (aref ans 0) open-ans))
891 (pos-ans (aref ans 1))
892 (ignored (aref ans 2)))
893 (setq not-found (append ignored not-found)
894 ignore (append ignored ignore))
895 ;; First do the negative (closing) annotations
896 (while neg-ans
897 ;; Check if it's missing. This can happen (eg, a numeric property
898 ;; going negative can generate closing annotations before there are
899 ;; any open). Warn user & ignore.
900 (if (not (member (car neg-ans) open-ans))
901 (message "Can't close %s: not open." (car neg-ans))
902 (while (not (equal (car neg-ans) (car open-ans)))
903 ;; To close anno. N, need to first close ans 1 to N-1,
904 ;; remembering to re-open them later.
be227f22 905 (push (car open-ans) pos-ans)
823139fb 906 (setq all-ans
029894b9
BG
907 (cons (cons loc (funcall format-fn (car open-ans) nil))
908 all-ans))
909 (setq open-ans (cdr open-ans)))
910 ;; Now remove the one we're really interested in from open list.
911 (setq open-ans (cdr open-ans))
912 ;; And put the closing annotation here.
be227f22
SM
913 (push (cons loc (funcall format-fn (car neg-ans) nil))
914 all-ans))
029894b9
BG
915 (setq neg-ans (cdr neg-ans)))
916 ;; Now deal with positive (opening) annotations
917 (let ((p pos-ans))
918 (while pos-ans
be227f22
SM
919 (push (car pos-ans) open-ans)
920 (push (cons loc (funcall format-fn (car pos-ans) t))
921 all-ans)
029894b9
BG
922 (setq pos-ans (cdr pos-ans))))))
923
924 ;; Close any annotations still open
925 (while open-ans
823139fb 926 (setq all-ans
029894b9
BG
927 (cons (cons to (funcall format-fn (car open-ans) nil))
928 all-ans))
929 (setq open-ans (cdr open-ans)))
930 (if not-found
931 (message "These text properties could not be saved:\n %s"
932 not-found))
933 (nreverse all-ans)))
934
935;;; Internal functions for format-annotate-region.
936
823139fb
DL
937(defun format-annotate-location (loc all ignore translations)
938 "Return annotation(s) needed at location LOC.
9c3054da 939This includes any properties that change between LOC - 1 and LOC.
029894b9
BG
940If ALL is true, don't look at previous location, but generate annotations for
941all non-nil properties.
942Third argument IGNORE is a list of text-properties not to consider.
d0fc7e3b 943Use the TRANSLATIONS alist (see `format-annotate-region' for doc).
029894b9
BG
944
945Return value is a vector of 3 elements:
d0fc7e3b
RS
9461. List of annotations to close
9472. List of annotations to open.
9483. List of properties that were ignored or couldn't be annotated.
949
950The annotations in lists 1 and 2 need not be strings.
951They can be whatever the FORMAT-FN in `format-annotate-region'
952can handle. If that is `enriched-make-annotation', they can be
953either strings, or lists of the form (PARAMETER VALUE)."
029894b9
BG
954 (let* ((prev-loc (1- loc))
955 (before-plist (if all nil (text-properties-at prev-loc)))
956 (after-plist (text-properties-at loc))
957 p negatives positives prop props not-found)
958 ;; make list of all property names involved
959 (setq p before-plist)
960 (while p
961 (if (not (memq (car p) props))
be227f22 962 (push (car p) props))
029894b9
BG
963 (setq p (cdr (cdr p))))
964 (setq p after-plist)
965 (while p
966 (if (not (memq (car p) props))
be227f22 967 (push (car p) props))
029894b9
BG
968 (setq p (cdr (cdr p))))
969
970 (while props
be227f22 971 (setq prop (pop props))
029894b9
BG
972 (if (memq prop ignore)
973 nil ; If it's been ignored before, ignore it now.
974 (let ((before (if all nil (car (cdr (memq prop before-plist)))))
975 (after (car (cdr (memq prop after-plist)))))
976 (if (equal before after)
977 nil ; no change; ignore
978 (let ((result (format-annotate-single-property-change
823139fb 979 prop before after translations)))
029894b9 980 (if (not result)
be227f22 981 (push prop not-found)
029894b9
BG
982 (setq negatives (nconc negatives (car result))
983 positives (nconc positives (cdr result)))))))))
984 (vector negatives positives not-found)))
985
d0fc7e3b 986(defun format-annotate-single-property-change (prop old new translations)
823139fb 987 "Return annotations for property PROP changing from OLD to NEW.
d0fc7e3b
RS
988These are searched for in the translations alist TRANSLATIONS
989 (see `format-annotate-region' for the format).
12008438
TTN
990If NEW does not appear in the list, but there is a default function,
991then call that function.
992Return a cons of the form (CLOSE . OPEN)
d0fc7e3b
RS
993where CLOSE is a list of annotations to close
994and OPEN is a list of annotations to open.
995
996The annotations in CLOSE and OPEN need not be strings.
997They can be whatever the FORMAT-FN in `format-annotate-region'
998can handle. If that is `enriched-make-annotation', they can be
999either strings, or lists of the form (PARAMETER VALUE)."
1000
1001 (let ((prop-alist (cdr (assoc prop translations)))
029894b9
BG
1002 default)
1003 (if (not prop-alist)
1004 nil
029894b9 1005 ;; If either old or new is a list, have to treat both that way.
f0a6c717 1006 (if (and (or (listp old) (listp new))
f3bbef87 1007 (not (get prop 'format-list-atomic-p)))
f0a6c717
GM
1008 (if (or (not (format-proper-list-p old))
1009 (not (format-proper-list-p new)))
1010 (format-annotate-atomic-property-change prop-alist old new)
1011 (let* ((old (if (listp old) old (list old)))
1012 (new (if (listp new) new (list new)))
1013 (tail (format-common-tail old new))
1014 close open)
1015 (while old
1016 (setq close
1017 (append (car (format-annotate-atomic-property-change
1018 prop-alist (car old) nil))
1019 close)
1020 old (cdr old)))
1021 (while new
1022 (setq open
1023 (append (cdr (format-annotate-atomic-property-change
1024 prop-alist nil (car new)))
1025 open)
1026 new (cdr new)))
1027 (format-make-relatively-unique close open)))
029894b9
BG
1028 (format-annotate-atomic-property-change prop-alist old new)))))
1029
1030(defun format-annotate-atomic-property-change (prop-alist old new)
12008438 1031 "Internal function to annotate a single property change.
d0fc7e3b 1032PROP-ALIST is the relevant element of a TRANSLATIONS list.
029894b9 1033OLD and NEW are the values."
e5a60108
RS
1034 (let (num-ann)
1035 ;; If old and new values are numbers,
1036 ;; look for a number in PROP-ALIST.
5f525595
RS
1037 (if (and (or (null old) (numberp old))
1038 (or (null new) (numberp new)))
e5a60108
RS
1039 (progn
1040 (setq num-ann prop-alist)
1041 (while (and num-ann (not (numberp (car (car num-ann)))))
1042 (setq num-ann (cdr num-ann)))))
1043 (if num-ann
5f525595 1044 ;; Numerical annotation - use difference
34fbecde
RS
1045 (progn
1046 ;; If property is numeric, nil means 0
1047 (cond ((and (numberp old) (null new))
1048 (setq new 0))
1049 ((and (numberp new) (null old))
1050 (setq old 0)))
1051
1052 (let* ((entry (car num-ann))
1053 (increment (car entry))
1054 (n (ceiling (/ (float (- new old)) (float increment))))
1055 (anno (car (cdr entry))))
1056 (if (> n 0)
1057 (cons nil (make-list n anno))
1058 (cons (make-list (- n) anno) nil))))
e5a60108
RS
1059
1060 ;; Standard annotation
1061 (let ((close (and old (cdr (assoc old prop-alist))))
029894b9
BG
1062 (open (and new (cdr (assoc new prop-alist)))))
1063 (if (or close open)
1064 (format-make-relatively-unique close open)
1065 ;; Call "Default" function, if any
1066 (let ((default (assq nil prop-alist)))
1067 (if default
1068 (funcall (car (cdr default)) old new))))))))
1069
c84cf2bf 1070(provide 'format)
823139fb 1071
cbee283d 1072;; arch-tag: c387e9c7-a93d-47bf-89bc-8ca67e96755a
823139fb 1073;;; format.el ends here