annotate lisp/format.el @ 390:c6012109f545 r21-2-10

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