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