1 ;;; enriched.el --- read and save files in text/enriched format
3 ;; Copyright (C) 1994-1996, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This file implements reading, editing, and saving files with
26 ;; text-properties such as faces, levels of indentation, and true line
27 ;; breaks distinguished from newlines just used to fit text into the window.
29 ;; The file format used is the MIME text/enriched format, which is a
30 ;; standard format defined in internet RFC 1563. All standard annotations
31 ;; are supported except for <smaller> and <bigger>, which are currently not
32 ;; possible to display.
34 ;; A separate file, enriched.doc, contains further documentation and other
35 ;; important information about this code. It also serves as an example
36 ;; file in text/enriched format. It should be in the etc directory of your
37 ;; emacs distribution.
44 ;;; Variables controlling the display
47 (defgroup enriched nil
48 "Read and save files in text/enriched format."
51 (defcustom enriched-verbose t
52 "If non-nil, give status messages when reading and writing files."
57 ;;; Set up faces & display table
60 ;; Emacs doesn't have a "fixed" face by default, since all faces currently
61 ;; have to be fixed-width. So we just pick one that looks different from the
65 "Face used for text that must be shown in fixed width.
66 Currently, Emacs can only display fixed-width fonts, but this may change.
67 This face is used for text specifically marked as fixed-width, for example
68 in text/enriched files."
72 '((t (:slant italic)))
73 "Face used for text that is an excerpt from another document.
74 This is used in Enriched mode for text explicitly marked as an excerpt."
77 (defconst enriched-display-table (or (copy-sequence standard-display-table)
78 (make-display-table)))
79 (aset enriched-display-table ?\f (make-vector (1- (frame-width)) ?-))
81 (defconst enriched-par-props '(left-margin right-margin justification)
82 "Text-properties that usually apply to whole paragraphs.
83 These are set front-sticky everywhere except at hard newlines.")
86 ;;; Variables controlling the file format
89 (defconst enriched-initial-annotation
91 (format "Content-Type: text/enriched\nText-Width: %d\n\n"
93 "What to insert at the start of a text/enriched file.
94 If this is a string, it is inserted. If it is a list, it should be a lambda
95 expression, which is evaluated to get the string to insert.")
97 (defconst enriched-annotation-format "<%s%s>"
98 "General format of enriched-text annotations.")
100 (defconst enriched-annotation-regexp "<\\(/\\)?\\([-A-Za-z0-9]+\\)>"
101 "Regular expression matching enriched-text annotations.")
103 (defvar enriched-translations
104 '((face (bold-italic "bold" "italic")
107 (underline "underline")
111 (nil enriched-encode-other-face))
112 (left-margin (4 "indent"))
113 (right-margin (4 "indentright"))
114 (justification (none "nofill")
119 (PARAMETER (t "param")) ; Argument of preceding annotation
120 ;; The following are not part of the standard:
121 (FUNCTION (enriched-decode-foreground "x-color")
122 (enriched-decode-background "x-bg-color")
123 (enriched-decode-display-prop "x-display"))
124 (read-only (t "x-read-only"))
125 (display (nil enriched-handle-display-prop))
126 (unknown (nil format-annotate-value))
127 ; (font-size (2 "bigger") ; unimplemented
130 "List of definitions of text/enriched annotations.
131 See `format-annotate-region' and `format-deannotate-region' for the definition
134 (defconst enriched-ignore
135 '(front-sticky rear-nonsticky hard)
136 "Properties that are OK to ignore when saving text/enriched files.
137 Any property that is neither on this list nor dealt with by
138 `enriched-translations' will generate a warning.")
140 ;;; Internal variables
142 (defcustom enriched-mode-hook nil
143 "Hook run after entering/leaving Enriched mode.
144 If you set variables in this hook, you should arrange for them to be restored
145 to their old values if you leave Enriched mode. One way to do this is to add
146 them and their old values to `enriched-old-bindings'."
150 (defvar enriched-old-bindings nil
151 "Store old variable values that we change when entering mode.
152 The value is a list of \(VAR VALUE VAR VALUE...).")
153 (make-variable-buffer-local 'enriched-old-bindings)
155 ;; The next variable is buffer local if and only if Enriched mode is
156 ;; enabled. The buffer local value records whether
157 ;; `default-text-properties' should remain buffer local when disabling
158 ;; Enriched mode. For technical reasons, the default value should be t.
159 (defvar enriched-default-text-properties-local-flag t)
161 ;; Technical internal variable. Bound to t if `enriched-mode' is
162 ;; being rerun by a major mode to allow it to restore buffer-local
163 ;; variables and to correctly update `enriched-old-bindings'.
164 (defvar enriched-rerun-flag nil)
170 (put 'enriched-mode 'permanent-local t)
172 (define-minor-mode enriched-mode
173 "Minor mode for editing text/enriched files.
174 These are files with embedded formatting information in the MIME standard
175 text/enriched format.
176 Turning the mode on or off runs `enriched-mode-hook'.
178 More information about Enriched mode is available in the file
179 etc/enriched.doc in the Emacs distribution directory.
183 \\{enriched-mode-map}"
184 :group 'enriched :lighter " Enriched"
185 (cond ((null enriched-mode)
187 (setq buffer-file-format (delq 'text/enriched buffer-file-format))
188 ;; restore old variable values
189 (while enriched-old-bindings
190 (set (pop enriched-old-bindings) (pop enriched-old-bindings)))
191 (unless enriched-default-text-properties-local-flag
192 (kill-local-variable 'default-text-properties))
193 (kill-local-variable 'enriched-default-text-properties-local-flag)
194 (unless use-hard-newlines (use-hard-newlines 0)))
196 ((and (memq 'text/enriched buffer-file-format)
197 (not enriched-rerun-flag))
198 ;; Mode already on; do nothing.
202 (add-to-list 'buffer-file-format 'text/enriched)
203 ;; Save old variable values before we change them.
204 ;; These will be restored if we exit Enriched mode.
205 (setq enriched-old-bindings
206 (list 'buffer-display-table buffer-display-table
207 'default-text-properties default-text-properties
208 'use-hard-newlines use-hard-newlines))
209 (make-local-variable 'enriched-default-text-properties-local-flag)
210 (setq enriched-default-text-properties-local-flag
211 (local-variable-p 'default-text-properties))
212 (make-local-variable 'default-text-properties)
213 (setq buffer-display-table enriched-display-table)
214 (use-hard-newlines 1 (if enriched-rerun-flag 'never nil))
215 (let ((sticky (plist-get default-text-properties 'front-sticky))
216 (p enriched-par-props))
218 (add-to-list 'sticky x))
220 (setq default-text-properties
221 (plist-put default-text-properties
222 'front-sticky sticky)))))))
224 (defun enriched-before-change-major-mode ()
226 (while enriched-old-bindings
227 (set (pop enriched-old-bindings) (pop enriched-old-bindings)))))
229 (add-hook 'change-major-mode-hook 'enriched-before-change-major-mode)
231 (defun enriched-after-change-major-mode ()
233 (let ((enriched-rerun-flag t))
236 (add-hook 'after-change-major-mode-hook 'enriched-after-change-major-mode)
242 (defvar enriched-mode-map nil
243 "Keymap for Enriched mode.")
245 (if (null enriched-mode-map)
246 (fset 'enriched-mode-map (setq enriched-mode-map (make-sparse-keymap))))
248 (if (not (assq 'enriched-mode minor-mode-map-alist))
249 (setq minor-mode-map-alist
250 (cons (cons 'enriched-mode enriched-mode-map)
251 minor-mode-map-alist)))
253 (define-key enriched-mode-map [remap move-beginning-of-line] 'beginning-of-line-text)
254 (define-key enriched-mode-map "\C-m" 'reindent-then-newline-and-indent)
255 (define-key enriched-mode-map
256 [remap newline-and-indent] '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)
260 (define-key enriched-mode-map "\C-c[" 'set-left-margin)
261 (define-key enriched-mode-map "\C-c]" 'set-right-margin)
264 ;;; Some functions dealing with text-properties, especially indentation
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.
273 The specified function receives three arguments: the VALUE of the property in
274 the region, and the START and END of each region."
277 (if to (narrow-to-region (point-min) to))
278 (goto-char (or from (point-min)))
279 (let ((begin (point))
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)))))))
291 (put 'enriched-map-property-regions 'lisp-indent-hook 1)
293 (defun enriched-insert-indentation (&optional from to)
294 "Indent and justify each line in the region."
297 (if to (narrow-to-region (point-min) to))
298 (goto-char (or from (point-min)))
299 (if (not (bolp)) (forward-line 1))
302 nil ; skip blank lines
303 (indent-to (current-left-margin))
304 (justify-current-line t nil t))
312 (defun enriched-encode (from to orig-buf)
313 (if enriched-verbose (message "Enriched: encoding document..."))
314 (let ((inhibit-read-only t))
316 (narrow-to-region from to)
317 (delete-to-left-margin)
320 (format-replace-strings '(("<" . "<<")))
321 (format-insert-annotations
322 (format-annotate-region from (point-max) enriched-translations
323 'enriched-make-annotation enriched-ignore))
325 (insert (if (stringp enriched-initial-annotation)
326 enriched-initial-annotation
328 ;; Eval this in the buffer we are annotating. This
329 ;; fixes a bug which was saving incorrect File-Width
330 ;; information, since we were looking at local
331 ;; variables in the wrong buffer.
332 (if orig-buf (set-buffer orig-buf))
333 (funcall enriched-initial-annotation))))
334 (enriched-map-property-regions 'hard
336 (if (and v (= ?\n (char-after b)))
337 (progn (goto-char b) (insert "\n"))))
339 (if enriched-verbose (message nil))
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
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>"))
357 (t (format enriched-annotation-format "/" (car internal-ann)))))
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))))
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))))
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)))
396 (defun enriched-decode (from to)
397 (if enriched-verbose (message "Enriched: decoding document..."))
398 (use-hard-newlines 1 'never)
401 (narrow-to-region from to)
405 (let ((file-width (enriched-get-file-width)))
406 (enriched-remove-header)
408 ;; Deal with newlines
409 (while (search-forward-regexp "\n\n+" nil t)
410 (if (current-justification)
412 (set-hard-newline-properties (match-beginning 0) (point)))
414 ;; Translate annotations
415 (format-deannotate-region from (point-max) enriched-translations
416 'enriched-next-annotation)
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)))
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))))
437 (if (= ?< (char-after (point)))
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"
444 (let* ((beg (match-beginning 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))))
451 (defun enriched-get-file-width ()
452 "Look for file width information on this line."
454 (if (search-forward "Text-Width: " (+ (point) 1000) t)
455 (read (current-buffer)))))
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")
464 (defun enriched-decode-foreground (from to &optional color)
466 (list from to 'face (list ':foreground color))
467 (message "Warning: no color specified for <x-color>")
470 (defun enriched-decode-background (from to &optional color)
472 (list from to 'face (list ':background color))
473 (message "Warning: no color specified for <x-bg-color>")
476 ;;; Handling the `display' property.
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))))
488 (cons nil (list (list annotation param)))
489 (cons (list (list annotation param)) nil))))
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
495 the range of text to assign text property SYMBOL with value VALUE."
496 (let ((prop (when (stringp param)
498 (car (read-from-string param))
501 (message "Warning: invalid <x-display> parameter %s" param))
502 (list start end 'display prop)))
504 ;;; enriched.el ends here