Mercurial > emacs
annotate lisp/textmodes/enriched.el @ 61527:020a384cb96d
(DONTCOMPILE): Remove list.
(compile, compile-always): Fix comments.
(update-authors): Load the library in which batch-update-authors is defined.
author | Lute Kamstra <lute@gnu.org> |
---|---|
date | Wed, 13 Apr 2005 09:44:52 +0000 |
parents | 3801e0f5830b |
children | 8b5102566971 02f1dbc4a199 |
rev | line source |
---|---|
51349 | 1 ;;; enriched.el --- read and save files in text/enriched format |
2 | |
61287
3801e0f5830b
(enriched-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
57318
diff
changeset
|
3 ;; Copyright (c) 1994, 1995, 1996, 2002, 2004, 2005 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 | |
57248
213428cc8e39
(enriched-translations): Replace defconst with defvar.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57203
diff
changeset
|
105 (defvar enriched-translations |
51349 | 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 | |
57203
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
157 ;; The next variable is buffer local if and only if Enriched mode is |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
158 ;; enabled. The buffer local value records whether |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
159 ;; `default-text-properties' should remain buffer local when disabling |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
160 ;; Enriched mode. For technical reasons, the default value should be t. |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
161 (defvar enriched-default-text-properties-local-flag t) |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
162 |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
163 ;; 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
|
164 ;; 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
|
165 ;; variables and to correctly update `enriched-old-bindings'. |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
166 (defvar enriched-rerun-flag nil) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
167 |
51349 | 168 ;;; |
169 ;;; Define the mode | |
170 ;;; | |
171 | |
172 (put 'enriched-mode 'permanent-local t) | |
173 ;;;###autoload | |
174 (define-minor-mode enriched-mode | |
175 "Minor mode for editing text/enriched files. | |
176 These are files with embedded formatting information in the MIME standard | |
177 text/enriched format. | |
57203
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
178 Turning the mode on or off runs `enriched-mode-hook'. |
51349 | 179 |
180 More information about Enriched mode is available in the file | |
181 etc/enriched.doc in the Emacs distribution directory. | |
182 | |
183 Commands: | |
184 | |
185 \\{enriched-mode-map}" | |
61287
3801e0f5830b
(enriched-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
57318
diff
changeset
|
186 :group 'enriched :lighter " Enriched" |
51349 | 187 (cond ((null enriched-mode) |
188 ;; Turn mode off | |
189 (setq buffer-file-format (delq 'text/enriched buffer-file-format)) | |
190 ;; restore old variable values | |
191 (while enriched-old-bindings | |
57203
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
192 (set (pop enriched-old-bindings) (pop enriched-old-bindings))) |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
193 (unless enriched-default-text-properties-local-flag |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
194 (kill-local-variable 'default-text-properties)) |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
195 (kill-local-variable 'enriched-default-text-properties-local-flag) |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
196 (unless use-hard-newlines (use-hard-newlines 0))) |
51349 | 197 |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
198 ((and (memq 'text/enriched buffer-file-format) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
199 (not enriched-rerun-flag)) |
51349 | 200 ;; Mode already on; do nothing. |
201 nil) | |
202 | |
203 (t ; Turn mode on | |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
204 (add-to-list 'buffer-file-format 'text/enriched) |
51349 | 205 ;; Save old variable values before we change them. |
206 ;; These will be restored if we exit Enriched mode. | |
207 (setq enriched-old-bindings | |
208 (list 'buffer-display-table buffer-display-table | |
57203
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
209 'default-text-properties default-text-properties |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
210 'use-hard-newlines use-hard-newlines)) |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
211 (make-local-variable 'enriched-default-text-properties-local-flag) |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
212 (setq enriched-default-text-properties-local-flag |
6cbbae74d7ca
(enriched-default-text-properties-local-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
57147
diff
changeset
|
213 (local-variable-p 'default-text-properties)) |
51349 | 214 (make-local-variable 'default-text-properties) |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
215 (setq buffer-display-table enriched-display-table) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
216 (use-hard-newlines 1 (if enriched-rerun-flag 'never nil)) |
51349 | 217 (let ((sticky (plist-get default-text-properties 'front-sticky)) |
218 (p enriched-par-props)) | |
219 (dolist (x p) | |
220 (add-to-list 'sticky x)) | |
221 (if sticky | |
222 (setq default-text-properties | |
223 (plist-put default-text-properties | |
224 'front-sticky sticky))))))) | |
225 | |
57147
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
226 (defun enriched-before-change-major-mode () |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
227 (when enriched-mode |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
228 (while enriched-old-bindings |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
229 (set (pop enriched-old-bindings) (pop enriched-old-bindings))))) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
230 |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
231 (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
|
232 |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
233 (defun enriched-after-change-major-mode () |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
234 (when enriched-mode |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
235 (let ((enriched-rerun-flag t)) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
236 (enriched-mode 1)))) |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
237 |
049bdfb5d92f
(enriched-rerun-flag): New variable.
Luc Teirlinck <teirllm@auburn.edu>
parents:
56323
diff
changeset
|
238 (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
|
239 |
51349 | 240 ;;; |
241 ;;; Keybindings | |
242 ;;; | |
243 | |
244 (defvar enriched-mode-map nil | |
245 "Keymap for Enriched mode.") | |
246 | |
247 (if (null enriched-mode-map) | |
248 (fset 'enriched-mode-map (setq enriched-mode-map (make-sparse-keymap)))) | |
249 | |
250 (if (not (assq 'enriched-mode minor-mode-map-alist)) | |
251 (setq minor-mode-map-alist | |
252 (cons (cons 'enriched-mode enriched-mode-map) | |
253 minor-mode-map-alist))) | |
254 | |
255 (define-key enriched-mode-map "\C-a" 'beginning-of-line-text) | |
256 (define-key enriched-mode-map "\C-m" 'reindent-then-newline-and-indent) | |
257 (define-key enriched-mode-map "\C-j" 'reindent-then-newline-and-indent) | |
258 (define-key enriched-mode-map "\M-j" 'facemenu-justification-menu) | |
259 (define-key enriched-mode-map "\M-S" 'set-justification-center) | |
260 (define-key enriched-mode-map "\C-x\t" 'increase-left-margin) | |
57318
99238df8b246
(enriched-mode-map): Give `set-left-margin' and `set-right-margin'
Luc Teirlinck <teirllm@auburn.edu>
parents:
57248
diff
changeset
|
261 (define-key enriched-mode-map "\C-c[" 'set-left-margin) |
99238df8b246
(enriched-mode-map): Give `set-left-margin' and `set-right-margin'
Luc Teirlinck <teirllm@auburn.edu>
parents:
57248
diff
changeset
|
262 (define-key enriched-mode-map "\C-c]" 'set-right-margin) |
51349 | 263 |
264 ;;; | |
265 ;;; Some functions dealing with text-properties, especially indentation | |
266 ;;; | |
267 | |
268 (defun enriched-map-property-regions (prop func &optional from to) | |
269 "Apply a function to regions of the buffer based on a text property. | |
270 For each contiguous region of the buffer for which the value of PROPERTY is | |
271 eq, the FUNCTION will be called. Optional arguments FROM and TO specify the | |
272 region over which to scan. | |
273 | |
274 The specified function receives three arguments: the VALUE of the property in | |
275 the region, and the START and END of each region." | |
276 (save-excursion | |
277 (save-restriction | |
278 (if to (narrow-to-region (point-min) to)) | |
279 (goto-char (or from (point-min))) | |
280 (let ((begin (point)) | |
281 end | |
282 (marker (make-marker)) | |
283 (val (get-text-property (point) prop))) | |
284 (while (setq end (text-property-not-all begin (point-max) prop val)) | |
285 (move-marker marker end) | |
286 (funcall func val begin (marker-position marker)) | |
287 (setq begin (marker-position marker) | |
288 val (get-text-property marker prop))) | |
289 (if (< begin (point-max)) | |
290 (funcall func val begin (point-max))))))) | |
291 | |
292 (put 'enriched-map-property-regions 'lisp-indent-hook 1) | |
293 | |
294 (defun enriched-insert-indentation (&optional from to) | |
295 "Indent and justify each line in the region." | |
296 (save-excursion | |
297 (save-restriction | |
298 (if to (narrow-to-region (point-min) to)) | |
299 (goto-char (or from (point-min))) | |
300 (if (not (bolp)) (forward-line 1)) | |
301 (while (not (eobp)) | |
302 (if (eolp) | |
303 nil ; skip blank lines | |
304 (indent-to (current-left-margin)) | |
305 (justify-current-line t nil t)) | |
306 (forward-line 1))))) | |
307 | |
308 ;;; | |
309 ;;; Encoding Files | |
310 ;;; | |
311 | |
312 ;;;###autoload | |
313 (defun enriched-encode (from to orig-buf) | |
314 (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
|
315 (let ((inhibit-read-only t)) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
316 (save-restriction |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
317 (narrow-to-region from to) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
318 (delete-to-left-margin) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
319 (unjustify-region) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
320 (goto-char from) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
321 (format-replace-strings '(("<" . "<<"))) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
322 (format-insert-annotations |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
323 (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
|
324 'enriched-make-annotation enriched-ignore)) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
325 (goto-char from) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
326 (insert (if (stringp enriched-initial-annotation) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
327 enriched-initial-annotation |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
328 (save-excursion |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
329 ;; 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
|
330 ;; 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
|
331 ;; information, since we were looking at local |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
332 ;; variables in the wrong buffer. |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
333 (if orig-buf (set-buffer orig-buf)) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
334 (funcall enriched-initial-annotation)))) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
335 (enriched-map-property-regions 'hard |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
336 (lambda (v b e) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
337 (if (and v (= ?\n (char-after b))) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
338 (progn (goto-char b) (insert "\n")))) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
339 (point) nil) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
340 (if enriched-verbose (message nil)) |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
341 ;; Return new end. |
a712704c769c
(enriched-encode): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
342 (point-max)))) |
51349 | 343 |
344 (defun enriched-make-annotation (internal-ann positive) | |
345 "Format an annotation INTERNAL-ANN. | |
346 INTERNAL-ANN may be a string, for a flag, or a list of the form (PARAM VALUE). | |
347 If POSITIVE is non-nil, this is the opening annotation; | |
348 if nil, the matching close." | |
349 (cond ((stringp internal-ann) | |
350 (format enriched-annotation-format (if positive "" "/") internal-ann)) | |
351 ;; Otherwise it is an annotation with parameters, represented as a list | |
352 (positive | |
353 (let ((item (car internal-ann)) | |
354 (params (cdr internal-ann))) | |
355 (concat (format enriched-annotation-format "" item) | |
356 (mapconcat (lambda (i) (concat "<param>" i "</param>")) | |
357 params "")))) | |
358 (t (format enriched-annotation-format "/" (car internal-ann))))) | |
359 | |
360 (defun enriched-encode-other-face (old new) | |
361 "Generate annotations for random face change. | |
362 One annotation each for foreground color, background color, italic, etc." | |
363 (cons (and old (enriched-face-ans old)) | |
364 (and new (enriched-face-ans new)))) | |
365 | |
366 (defun enriched-face-ans (face) | |
367 "Return annotations specifying FACE. | |
368 FACE may be a list of faces instead of a single face; | |
369 it can also be anything allowed as an element of a list | |
370 which can be the value of the `face' text property." | |
371 (cond ((and (consp face) (eq (car face) 'foreground-color)) | |
372 (list (list "x-color" (cdr face)))) | |
373 ((and (consp face) (eq (car face) 'background-color)) | |
374 (list (list "x-bg-color" (cdr face)))) | |
375 ((and (listp face) (eq (car face) :foreground)) | |
376 (list (list "x-color" (cadr face)))) | |
377 ((and (listp face) (eq (car face) :background)) | |
378 (list (list "x-bg-color" (cadr face)))) | |
379 ((listp face) | |
380 (apply 'append (mapcar 'enriched-face-ans face))) | |
381 ((let* ((fg (face-attribute face :foreground)) | |
382 (bg (face-attribute face :background)) | |
383 (props (face-font face t)) | |
384 (ans (cdr (format-annotate-single-property-change | |
385 'face nil props enriched-translations)))) | |
386 (unless (eq fg 'unspecified) | |
387 (setq ans (cons (list "x-color" fg) ans))) | |
388 (unless (eq bg 'unspecified) | |
389 (setq ans (cons (list "x-bg-color" bg) ans))) | |
390 ans)))) | |
391 | |
392 ;;; | |
393 ;;; Decoding files | |
394 ;;; | |
395 | |
396 ;;;###autoload | |
397 (defun enriched-decode (from to) | |
398 (if enriched-verbose (message "Enriched: decoding document...")) | |
399 (use-hard-newlines 1 'never) | |
400 (save-excursion | |
401 (save-restriction | |
402 (narrow-to-region from to) | |
403 (goto-char from) | |
404 | |
405 ;; Deal with header | |
406 (let ((file-width (enriched-get-file-width))) | |
407 (enriched-remove-header) | |
408 | |
409 ;; Deal with newlines | |
410 (while (search-forward-regexp "\n\n+" nil t) | |
411 (if (current-justification) | |
412 (delete-char -1)) | |
413 (set-hard-newline-properties (match-beginning 0) (point))) | |
414 | |
415 ;; Translate annotations | |
416 (format-deannotate-region from (point-max) enriched-translations | |
417 'enriched-next-annotation) | |
418 | |
419 ;; Indent or fill the buffer | |
420 (cond (file-width ; File was filled to this width | |
421 (setq fill-column file-width) | |
422 (if enriched-verbose (message "Indenting...")) | |
423 (enriched-insert-indentation)) | |
424 (t ; File was not filled. | |
425 (if enriched-verbose (message "Filling paragraphs...")) | |
426 (fill-region (point-min) (point-max)))) | |
427 (if enriched-verbose (message nil))) | |
428 (point-max)))) | |
429 | |
430 (defun enriched-next-annotation () | |
431 "Find and return next text/enriched annotation. | |
432 Any \"<<\" strings encountered are converted to \"<\". | |
433 Return value is \(begin end name positive-p), or nil if none was found." | |
434 (while (and (search-forward "<" nil 1) | |
435 (progn (goto-char (match-beginning 0)) | |
436 (not (looking-at enriched-annotation-regexp)))) | |
437 (forward-char 1) | |
438 (if (= ?< (char-after (point))) | |
439 (delete-char 1) | |
440 ;; A single < that does not start an annotation is an error, | |
441 ;; which we note and then ignore. | |
442 (message "Warning: malformed annotation in file at %s" | |
443 (1- (point))))) | |
444 (if (not (eobp)) | |
445 (let* ((beg (match-beginning 0)) | |
446 (end (match-end 0)) | |
447 (name (downcase (buffer-substring | |
448 (match-beginning 2) (match-end 2)))) | |
449 (pos (not (match-beginning 1)))) | |
450 (list beg end name pos)))) | |
451 | |
452 (defun enriched-get-file-width () | |
453 "Look for file width information on this line." | |
454 (save-excursion | |
455 (if (search-forward "Text-Width: " (+ (point) 1000) t) | |
456 (read (current-buffer))))) | |
457 | |
458 (defun enriched-remove-header () | |
459 "Remove file-format header at point." | |
460 (while (looking-at "^[-A-Za-z]+: .*\n") | |
461 (delete-region (point) (match-end 0))) | |
462 (if (looking-at "^\n") | |
463 (delete-char 1))) | |
464 | |
465 (defun enriched-decode-foreground (from to &optional color) | |
466 (if color | |
467 (list from to 'face (list ':foreground color)) | |
468 (message "Warning: no color specified for <x-color>") | |
469 nil)) | |
470 | |
471 (defun enriched-decode-background (from to &optional color) | |
472 (if color | |
473 (list from to 'face (list ':background color)) | |
474 (message "Warning: no color specified for <x-bg-color>") | |
475 nil)) | |
476 | |
477 ;;; Handling the `display' property. | |
478 | |
479 | |
480 (defun enriched-handle-display-prop (old new) | |
481 "Return a list of annotations for a change in the `display' property. | |
482 OLD is the old value of the property, NEW is the new value. Value | |
483 is a list `(CLOSE OPEN)', where CLOSE is a list of annotations to | |
484 close and OPEN a list of annotations to open. Each of these lists | |
485 has the form `(ANNOTATION PARAM ...)'." | |
486 (let ((annotation "x-display") | |
487 (param (prin1-to-string (or old new)))) | |
488 (if (null old) | |
489 (cons nil (list (list annotation param))) | |
490 (cons (list (list annotation param)) nil)))) | |
491 | |
492 (defun enriched-decode-display-prop (start end &optional param) | |
493 "Decode a `display' property for text between START and END. | |
494 PARAM is a `<param>' found for the property. | |
495 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
|
496 the range of text to assign text property SYMBOL with value VALUE." |
51349 | 497 (let ((prop (when (stringp param) |
498 (condition-case () | |
499 (car (read-from-string param)) | |
500 (error nil))))) | |
501 (unless prop | |
502 (message "Warning: invalid <x-display> parameter %s" param)) | |
503 (list start end 'display prop))) | |
504 | |
52401 | 505 ;;; arch-tag: 05cae488-3fea-45cd-ac29-5b02cb64e42b |
51349 | 506 ;;; enriched.el ends here |