Mercurial > emacs
annotate lisp/textmodes/enriched.el @ 83210:e2877f047180
Merged in changes from CVS trunk.
Patches applied:
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-566
Update from CVS
git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-250
author | Karoly Lorentey <lorentey@elte.hu> |
---|---|
date | Thu, 23 Sep 2004 13:21:01 +0000 |
parents | 049bdfb5d92f |
children | 6cbbae74d7ca 0796fc36c2bd |
rev | line source |
---|---|
51349 | 1 ;;; enriched.el --- read and save files in text/enriched format |
2 | |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
3 ;; Copyright (c) 1994, 1995, 1996, 2002, 2004 Free Software Foundation, Inc. |
51349 | 4 |
5 ;; Author: Boris Goldowsky <boris@gnu.org> | |
6 ;; Keywords: wp, faces | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; This file implements reading, editing, and saving files with | |
28 ;; text-properties such as faces, levels of indentation, and true line | |
29 ;; breaks distinguished from newlines just used to fit text into the window. | |
30 | |
31 ;; The file format used is the MIME text/enriched format, which is a | |
32 ;; standard format defined in internet RFC 1563. All standard annotations | |
33 ;; are supported except for <smaller> and <bigger>, which are currently not | |
34 ;; possible to display. | |
35 | |
36 ;; A separate file, enriched.doc, contains further documentation and other | |
37 ;; important information about this code. It also serves as an example | |
38 ;; file in text/enriched format. It should be in the etc directory of your | |
39 ;; emacs distribution. | |
40 | |
41 ;;; Code: | |
42 | |
43 (provide 'enriched) | |
44 | |
45 ;;; | |
46 ;;; Variables controlling the display | |
47 ;;; | |
48 | |
49 (defgroup enriched nil | |
50 "Read and save files in text/enriched format" | |
51 :group 'wp) | |
52 | |
53 (defcustom enriched-verbose t | |
54 "*If non-nil, give status messages when reading and writing files." | |
55 :type 'boolean | |
56 :group 'enriched) | |
57 | |
58 ;;; | |
59 ;;; Set up faces & display table | |
60 ;;; | |
61 | |
62 ;; Emacs doesn't have a "fixed" face by default, since all faces currently | |
63 ;; have to be fixed-width. So we just pick one that looks different from the | |
64 ;; default. | |
65 (defface fixed | |
66 '((t (:weight bold))) | |
67 "Face used for text that must be shown in fixed width. | |
68 Currently, emacs can only display fixed-width fonts, but this may change. | |
69 This face is used for text specifically marked as fixed-width, for example | |
70 in text/enriched files." | |
71 :group 'enriched) | |
72 | |
73 (defface excerpt | |
74 '((t (:slant italic))) | |
75 "Face used for text that is an excerpt from another document. | |
76 This is used in Enriched mode for text explicitly marked as an excerpt." | |
77 :group 'enriched) | |
78 | |
79 (defconst enriched-display-table (or (copy-sequence standard-display-table) | |
80 (make-display-table))) | |
81 (aset enriched-display-table ?\f (make-vector (1- (frame-width)) ?-)) | |
82 | |
83 (defconst enriched-par-props '(left-margin right-margin justification) | |
84 "Text-properties that usually apply to whole paragraphs. | |
85 These are set front-sticky everywhere except at hard newlines.") | |
86 | |
87 ;;; | |
88 ;;; Variables controlling the file format | |
89 ;;; (bidirectional) | |
90 | |
91 (defconst enriched-initial-annotation | |
92 (lambda () | |
93 (format "Content-Type: text/enriched\nText-Width: %d\n\n" | |
94 fill-column)) | |
95 "What to insert at the start of a text/enriched file. | |
96 If this is a string, it is inserted. If it is a list, it should be a lambda | |
97 expression, which is evaluated to get the string to insert.") | |
98 | |
99 (defconst enriched-annotation-format "<%s%s>" | |
100 "General format of enriched-text annotations.") | |
101 | |
102 (defconst enriched-annotation-regexp "<\\(/\\)?\\([-A-Za-z0-9]+\\)>" | |
103 "Regular expression matching enriched-text annotations.") | |
104 | |
105 (defconst enriched-translations | |
106 '((face (bold-italic "bold" "italic") | |
107 (bold "bold") | |
108 (italic "italic") | |
109 (underline "underline") | |
110 (fixed "fixed") | |
111 (excerpt "excerpt") | |
112 (default ) | |
113 (nil enriched-encode-other-face)) | |
114 (left-margin (4 "indent")) | |
115 (right-margin (4 "indentright")) | |
116 (justification (none "nofill") | |
117 (right "flushright") | |
118 (left "flushleft") | |
119 (full "flushboth") | |
120 (center "center")) | |
121 (PARAMETER (t "param")) ; Argument of preceding annotation | |
122 ;; The following are not part of the standard: | |
123 (FUNCTION (enriched-decode-foreground "x-color") | |
124 (enriched-decode-background "x-bg-color") | |
125 (enriched-decode-display-prop "x-display")) | |
126 (read-only (t "x-read-only")) | |
127 (display (nil enriched-handle-display-prop)) | |
128 (unknown (nil format-annotate-value)) | |
129 ; (font-size (2 "bigger") ; unimplemented | |
130 ; (-2 "smaller")) | |
131 ) | |
132 "List of definitions of text/enriched annotations. | |
133 See `format-annotate-region' and `format-deannotate-region' for the definition | |
134 of this structure.") | |
135 | |
136 (defconst enriched-ignore | |
137 '(front-sticky rear-nonsticky hard) | |
138 "Properties that are OK to ignore when saving text/enriched files. | |
139 Any property that is neither on this list nor dealt with by | |
140 `enriched-translations' will generate a warning.") | |
141 | |
142 ;;; Internal variables | |
143 | |
144 (defcustom enriched-mode-hook nil | |
145 "Hook run after entering/leaving Enriched mode. | |
146 If you set variables in this hook, you should arrange for them to be restored | |
147 to their old values if you leave Enriched mode. One way to do this is to add | |
148 them and their old values to `enriched-old-bindings'." | |
149 :type 'hook | |
150 :group 'enriched) | |
151 | |
152 (defvar enriched-old-bindings nil | |
153 "Store old variable values that we change when entering mode. | |
154 The value is a list of \(VAR VALUE VAR VALUE...).") | |
155 (make-variable-buffer-local 'enriched-old-bindings) | |
156 | |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
157 ;; Technical internal variable. Bound to t if `enriched-mode' is |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
158 ;; being rerun by a major mode to allow it to restore buffer-local |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
159 ;; variables and to correctly update `enriched-old-bindings'. |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
160 (defvar enriched-rerun-flag nil) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
161 |
51349 | 162 ;;; |
163 ;;; Define the mode | |
164 ;;; | |
165 | |
166 (put 'enriched-mode 'permanent-local t) | |
167 ;;;###autoload | |
168 (define-minor-mode enriched-mode | |
169 "Minor mode for editing text/enriched files. | |
170 These are files with embedded formatting information in the MIME standard | |
171 text/enriched format. | |
172 Turning the mode on runs `enriched-mode-hook'. | |
173 | |
174 More information about Enriched mode is available in the file | |
175 etc/enriched.doc in the Emacs distribution directory. | |
176 | |
177 Commands: | |
178 | |
179 \\{enriched-mode-map}" | |
180 nil " Enriched" nil | |
181 (cond ((null enriched-mode) | |
182 ;; Turn mode off | |
183 (setq buffer-file-format (delq 'text/enriched buffer-file-format)) | |
184 ;; restore old variable values | |
185 (while enriched-old-bindings | |
186 (set (pop enriched-old-bindings) (pop enriched-old-bindings)))) | |
187 | |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
188 ((and (memq 'text/enriched buffer-file-format) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
189 (not enriched-rerun-flag)) |
51349 | 190 ;; Mode already on; do nothing. |
191 nil) | |
192 | |
193 (t ; Turn mode on | |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
194 (add-to-list 'buffer-file-format 'text/enriched) |
51349 | 195 ;; Save old variable values before we change them. |
196 ;; These will be restored if we exit Enriched mode. | |
197 (setq enriched-old-bindings | |
198 (list 'buffer-display-table buffer-display-table | |
199 'default-text-properties default-text-properties)) | |
200 (make-local-variable 'default-text-properties) | |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
201 (setq buffer-display-table enriched-display-table) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
202 (use-hard-newlines 1 (if enriched-rerun-flag 'never nil)) |
51349 | 203 (let ((sticky (plist-get default-text-properties 'front-sticky)) |
204 (p enriched-par-props)) | |
205 (dolist (x p) | |
206 (add-to-list 'sticky x)) | |
207 (if sticky | |
208 (setq default-text-properties | |
209 (plist-put default-text-properties | |
210 'front-sticky sticky))))))) | |
211 | |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
212 (defun enriched-before-change-major-mode () |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
213 (when enriched-mode |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
214 (while enriched-old-bindings |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
215 (set (pop enriched-old-bindings) (pop enriched-old-bindings))))) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
216 |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
217 (add-hook 'change-major-mode-hook 'enriched-before-change-major-mode) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
218 |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
219 (defun enriched-after-change-major-mode () |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
220 (when enriched-mode |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
221 (let ((enriched-rerun-flag t)) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
222 (enriched-mode 1)))) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
223 |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
224 (add-hook 'after-change-major-mode-hook 'enriched-after-change-major-mode) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
225 |
51349 | 226 ;;; |
227 ;;; Keybindings | |
228 ;;; | |
229 | |
230 (defvar enriched-mode-map nil | |
231 "Keymap for Enriched mode.") | |
232 | |
233 (if (null enriched-mode-map) | |
234 (fset 'enriched-mode-map (setq enriched-mode-map (make-sparse-keymap)))) | |
235 | |
236 (if (not (assq 'enriched-mode minor-mode-map-alist)) | |
237 (setq minor-mode-map-alist | |
238 (cons (cons 'enriched-mode enriched-mode-map) | |
239 minor-mode-map-alist))) | |
240 | |
241 (define-key enriched-mode-map "\C-a" 'beginning-of-line-text) | |
242 (define-key enriched-mode-map "\C-m" 'reindent-then-newline-and-indent) | |
243 (define-key enriched-mode-map "\C-j" 'reindent-then-newline-and-indent) | |
244 (define-key enriched-mode-map "\M-j" 'facemenu-justification-menu) | |
245 (define-key enriched-mode-map "\M-S" 'set-justification-center) | |
246 (define-key enriched-mode-map "\C-x\t" 'increase-left-margin) | |
247 (define-key enriched-mode-map "\C-c\C-l" 'set-left-margin) | |
248 (define-key enriched-mode-map "\C-c\C-r" 'set-right-margin) | |
249 | |
250 ;;; | |
251 ;;; Some functions dealing with text-properties, especially indentation | |
252 ;;; | |
253 | |
254 (defun enriched-map-property-regions (prop func &optional from to) | |
255 "Apply a function to regions of the buffer based on a text property. | |
256 For each contiguous region of the buffer for which the value of PROPERTY is | |
257 eq, the FUNCTION will be called. Optional arguments FROM and TO specify the | |
258 region over which to scan. | |
259 | |
260 The specified function receives three arguments: the VALUE of the property in | |
261 the region, and the START and END of each region." | |
262 (save-excursion | |
263 (save-restriction | |
264 (if to (narrow-to-region (point-min) to)) | |
265 (goto-char (or from (point-min))) | |
266 (let ((begin (point)) | |
267 end | |
268 (marker (make-marker)) | |
269 (val (get-text-property (point) prop))) | |
270 (while (setq end (text-property-not-all begin (point-max) prop val)) | |
271 (move-marker marker end) | |
272 (funcall func val begin (marker-position marker)) | |
273 (setq begin (marker-position marker) | |
274 val (get-text-property marker prop))) | |
275 (if (< begin (point-max)) | |
276 (funcall func val begin (point-max))))))) | |
277 | |
278 (put 'enriched-map-property-regions 'lisp-indent-hook 1) | |
279 | |
280 (defun enriched-insert-indentation (&optional from to) | |
281 "Indent and justify each line in the region." | |
282 (save-excursion | |
283 (save-restriction | |
284 (if to (narrow-to-region (point-min) to)) | |
285 (goto-char (or from (point-min))) | |
286 (if (not (bolp)) (forward-line 1)) | |
287 (while (not (eobp)) | |
288 (if (eolp) | |
289 nil ; skip blank lines | |
290 (indent-to (current-left-margin)) | |
291 (justify-current-line t nil t)) | |
292 (forward-line 1))))) | |
293 | |
294 ;;; | |
295 ;;; Encoding Files | |
296 ;;; | |
297 | |
298 ;;;###autoload | |
299 (defun enriched-encode (from to orig-buf) | |
300 (if enriched-verbose (message "Enriched: encoding document...")) | |
56323
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
301 (let ((inhibit-read-only t)) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
302 (save-restriction |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
303 (narrow-to-region from to) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
304 (delete-to-left-margin) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
305 (unjustify-region) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
306 (goto-char from) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
307 (format-replace-strings '(("<" . "<<"))) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
308 (format-insert-annotations |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
309 (format-annotate-region from (point-max) enriched-translations |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
310 'enriched-make-annotation enriched-ignore)) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
311 (goto-char from) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
312 (insert (if (stringp enriched-initial-annotation) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
313 enriched-initial-annotation |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
314 (save-excursion |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
315 ;; Eval this in the buffer we are annotating. This |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
316 ;; fixes a bug which was saving incorrect File-Width |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
317 ;; information, since we were looking at local |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
318 ;; variables in the wrong buffer. |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
319 (if orig-buf (set-buffer orig-buf)) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
320 (funcall enriched-initial-annotation)))) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
321 (enriched-map-property-regions 'hard |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
322 (lambda (v b e) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
323 (if (and v (= ?\n (char-after b))) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
324 (progn (goto-char b) (insert "\n")))) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
325 (point) nil) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
326 (if enriched-verbose (message nil)) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
327 ;; Return new end. |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
328 (point-max)))) |
51349 | 329 |
330 (defun enriched-make-annotation (internal-ann positive) | |
331 "Format an annotation INTERNAL-ANN. | |
332 INTERNAL-ANN may be a string, for a flag, or a list of the form (PARAM VALUE). | |
333 If POSITIVE is non-nil, this is the opening annotation; | |
334 if nil, the matching close." | |
335 (cond ((stringp internal-ann) | |
336 (format enriched-annotation-format (if positive "" "/") internal-ann)) | |
337 ;; Otherwise it is an annotation with parameters, represented as a list | |
338 (positive | |
339 (let ((item (car internal-ann)) | |
340 (params (cdr internal-ann))) | |
341 (concat (format enriched-annotation-format "" item) | |
342 (mapconcat (lambda (i) (concat "<param>" i "</param>")) | |
343 params "")))) | |
344 (t (format enriched-annotation-format "/" (car internal-ann))))) | |
345 | |
346 (defun enriched-encode-other-face (old new) | |
347 "Generate annotations for random face change. | |
348 One annotation each for foreground color, background color, italic, etc." | |
349 (cons (and old (enriched-face-ans old)) | |
350 (and new (enriched-face-ans new)))) | |
351 | |
352 (defun enriched-face-ans (face) | |
353 "Return annotations specifying FACE. | |
354 FACE may be a list of faces instead of a single face; | |
355 it can also be anything allowed as an element of a list | |
356 which can be the value of the `face' text property." | |
357 (cond ((and (consp face) (eq (car face) 'foreground-color)) | |
358 (list (list "x-color" (cdr face)))) | |
359 ((and (consp face) (eq (car face) 'background-color)) | |
360 (list (list "x-bg-color" (cdr face)))) | |
361 ((and (listp face) (eq (car face) :foreground)) | |
362 (list (list "x-color" (cadr face)))) | |
363 ((and (listp face) (eq (car face) :background)) | |
364 (list (list "x-bg-color" (cadr face)))) | |
365 ((listp face) | |
366 (apply 'append (mapcar 'enriched-face-ans face))) | |
367 ((let* ((fg (face-attribute face :foreground)) | |
368 (bg (face-attribute face :background)) | |
369 (props (face-font face t)) | |
370 (ans (cdr (format-annotate-single-property-change | |
371 'face nil props enriched-translations)))) | |
372 (unless (eq fg 'unspecified) | |
373 (setq ans (cons (list "x-color" fg) ans))) | |
374 (unless (eq bg 'unspecified) | |
375 (setq ans (cons (list "x-bg-color" bg) ans))) | |
376 ans)))) | |
377 | |
378 ;;; | |
379 ;;; Decoding files | |
380 ;;; | |
381 | |
382 ;;;###autoload | |
383 (defun enriched-decode (from to) | |
384 (if enriched-verbose (message "Enriched: decoding document...")) | |
385 (use-hard-newlines 1 'never) | |
386 (save-excursion | |
387 (save-restriction | |
388 (narrow-to-region from to) | |
389 (goto-char from) | |
390 | |
391 ;; Deal with header | |
392 (let ((file-width (enriched-get-file-width))) | |
393 (enriched-remove-header) | |
394 | |
395 ;; Deal with newlines | |
396 (while (search-forward-regexp "\n\n+" nil t) | |
397 (if (current-justification) | |
398 (delete-char -1)) | |
399 (set-hard-newline-properties (match-beginning 0) (point))) | |
400 | |
401 ;; Translate annotations | |
402 (format-deannotate-region from (point-max) enriched-translations | |
403 'enriched-next-annotation) | |
404 | |
405 ;; Indent or fill the buffer | |
406 (cond (file-width ; File was filled to this width | |
407 (setq fill-column file-width) | |
408 (if enriched-verbose (message "Indenting...")) | |
409 (enriched-insert-indentation)) | |
410 (t ; File was not filled. | |
411 (if enriched-verbose (message "Filling paragraphs...")) | |
412 (fill-region (point-min) (point-max)))) | |
413 (if enriched-verbose (message nil))) | |
414 (point-max)))) | |
415 | |
416 (defun enriched-next-annotation () | |
417 "Find and return next text/enriched annotation. | |
418 Any \"<<\" strings encountered are converted to \"<\". | |
419 Return value is \(begin end name positive-p), or nil if none was found." | |
420 (while (and (search-forward "<" nil 1) | |
421 (progn (goto-char (match-beginning 0)) | |
422 (not (looking-at enriched-annotation-regexp)))) | |
423 (forward-char 1) | |
424 (if (= ?< (char-after (point))) | |
425 (delete-char 1) | |
426 ;; A single < that does not start an annotation is an error, | |
427 ;; which we note and then ignore. | |
428 (message "Warning: malformed annotation in file at %s" | |
429 (1- (point))))) | |
430 (if (not (eobp)) | |
431 (let* ((beg (match-beginning 0)) | |
432 (end (match-end 0)) | |
433 (name (downcase (buffer-substring | |
434 (match-beginning 2) (match-end 2)))) | |
435 (pos (not (match-beginning 1)))) | |
436 (list beg end name pos)))) | |
437 | |
438 (defun enriched-get-file-width () | |
439 "Look for file width information on this line." | |
440 (save-excursion | |
441 (if (search-forward "Text-Width: " (+ (point) 1000) t) | |
442 (read (current-buffer))))) | |
443 | |
444 (defun enriched-remove-header () | |
445 "Remove file-format header at point." | |
446 (while (looking-at "^[-A-Za-z]+: .*\n") | |
447 (delete-region (point) (match-end 0))) | |
448 (if (looking-at "^\n") | |
449 (delete-char 1))) | |
450 | |
451 (defun enriched-decode-foreground (from to &optional color) | |
452 (if color | |
453 (list from to 'face (list ':foreground color)) | |
454 (message "Warning: no color specified for <x-color>") | |
455 nil)) | |
456 | |
457 (defun enriched-decode-background (from to &optional color) | |
458 (if color | |
459 (list from to 'face (list ':background color)) | |
460 (message "Warning: no color specified for <x-bg-color>") | |
461 nil)) | |
462 | |
463 ;;; Handling the `display' property. | |
464 | |
465 | |
466 (defun enriched-handle-display-prop (old new) | |
467 "Return a list of annotations for a change in the `display' property. | |
468 OLD is the old value of the property, NEW is the new value. Value | |
469 is a list `(CLOSE OPEN)', where CLOSE is a list of annotations to | |
470 close and OPEN a list of annotations to open. Each of these lists | |
471 has the form `(ANNOTATION PARAM ...)'." | |
472 (let ((annotation "x-display") | |
473 (param (prin1-to-string (or old new)))) | |
474 (if (null old) | |
475 (cons nil (list (list annotation param))) | |
476 (cons (list (list annotation param)) nil)))) | |
477 | |
478 (defun enriched-decode-display-prop (start end &optional param) | |
479 "Decode a `display' property for text between START and END. | |
480 PARAM is a `<param>' found for the property. | |
481 Value is a list `(START END SYMBOL VALUE)' with START and END denoting | |
51962
d4560e85f83c
(enriched-decode-display-prop): Doc fix.
John Paul Wallington <jpw@pobox.com>
parents:
51349
diff
changeset
|
482 the range of text to assign text property SYMBOL with value VALUE." |
51349 | 483 (let ((prop (when (stringp param) |
484 (condition-case () | |
485 (car (read-from-string param)) | |
486 (error nil))))) | |
487 (unless prop | |
488 (message "Warning: invalid <x-display> parameter %s" param)) | |
489 (list start end 'display prop))) | |
490 | |
52401 | 491 ;;; arch-tag: 05cae488-3fea-45cd-ac29-5b02cb64e42b |
51349 | 492 ;;; enriched.el ends here |