annotate lisp/format.el @ 215:1f0dabaa0855 r20-4b6

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