]> code.delx.au - gnu-emacs/blob - lisp/enriched.el
(viet-encode-viscii-char): New function.
[gnu-emacs] / lisp / enriched.el
1 ;;; enriched.el --- read and save files in text/enriched format
2
3 ;; Copyright (c) 1994, 1995, 1996 Free Software Foundation, Inc.
4
5 ;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu>
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 (:bold t)))
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 (:italic t)))
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 (read-only (t "x-read-only"))
126 (unknown (nil format-annotate-value))
127 ; (font-size (2 "bigger") ; unimplemented
128 ; (-2 "smaller"))
129 )
130 "List of definitions of text/enriched annotations.
131 See `format-annotate-region' and `format-deannotate-region' for the definition
132 of this structure.")
133
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.")
139
140 ;;; Internal variables
141
142 (defvar enriched-mode nil
143 "True if Enriched mode is in use.")
144 (make-variable-buffer-local 'enriched-mode)
145
146 (if (not (assq 'enriched-mode minor-mode-alist))
147 (setq minor-mode-alist
148 (cons '(enriched-mode " Enriched")
149 minor-mode-alist)))
150
151 (defcustom enriched-mode-hook nil
152 "Functions to run when entering Enriched mode.
153 If you set variables in this hook, you should arrange for them to be restored
154 to their old values if you leave Enriched mode. One way to do this is to add
155 them and their old values to `enriched-old-bindings'."
156 :type 'hook
157 :group 'enriched)
158
159 (defvar enriched-old-bindings nil
160 "Store old variable values that we change when entering mode.
161 The value is a list of \(VAR VALUE VAR VALUE...).")
162 (make-variable-buffer-local 'enriched-old-bindings)
163
164 ;;;
165 ;;; Define the mode
166 ;;;
167
168 ;;;###autoload
169 (defun enriched-mode (&optional arg)
170 "Minor mode for editing text/enriched files.
171 These are files with embedded formatting information in the MIME standard
172 text/enriched format.
173 Turning the mode on runs `enriched-mode-hook'.
174
175 More information about Enriched mode is available in the file
176 etc/enriched.doc in the Emacs distribution directory.
177
178 Commands:
179
180 \\<enriched-mode-map>\\{enriched-mode-map}"
181 (interactive "P")
182 (let ((mod (buffer-modified-p)))
183 (cond ((or (<= (prefix-numeric-value arg) 0)
184 (and enriched-mode (null arg)))
185 ;; Turn mode off
186 (setq enriched-mode nil)
187 (setq buffer-file-format (delq 'text/enriched buffer-file-format))
188 ;; restore old variable values
189 (while enriched-old-bindings
190 (funcall 'set (car enriched-old-bindings)
191 (car (cdr enriched-old-bindings)))
192 (setq enriched-old-bindings (cdr (cdr enriched-old-bindings)))))
193
194 (enriched-mode nil) ; Mode already on; do nothing.
195
196 (t (setq enriched-mode t) ; Turn mode on
197 (add-to-list 'buffer-file-format 'text/enriched)
198 ;; Save old variable values before we change them.
199 ;; These will be restored if we exit Enriched mode.
200 (setq enriched-old-bindings
201 (list 'buffer-display-table buffer-display-table
202 'indent-line-function indent-line-function
203 'default-text-properties default-text-properties))
204 (make-local-variable 'indent-line-function)
205 (make-local-variable 'default-text-properties)
206 (setq indent-line-function 'indent-to-left-margin
207 buffer-display-table enriched-display-table)
208 (use-hard-newlines 1 nil)
209 (let ((sticky (plist-get default-text-properties 'front-sticky))
210 (p enriched-par-props))
211 (while p
212 (add-to-list 'sticky (car p))
213 (setq p (cdr p)))
214 (if sticky
215 (setq default-text-properties
216 (plist-put default-text-properties
217 'front-sticky sticky))))
218 (run-hooks 'enriched-mode-hook)))
219 (set-buffer-modified-p mod)
220 (force-mode-line-update)))
221
222 ;;;
223 ;;; Keybindings
224 ;;;
225
226 (defvar enriched-mode-map nil
227 "Keymap for Enriched mode.")
228
229 (if (null enriched-mode-map)
230 (fset 'enriched-mode-map (setq enriched-mode-map (make-sparse-keymap))))
231
232 (if (not (assq 'enriched-mode minor-mode-map-alist))
233 (setq minor-mode-map-alist
234 (cons (cons 'enriched-mode enriched-mode-map)
235 minor-mode-map-alist)))
236
237 (define-key enriched-mode-map "\C-a" 'beginning-of-line-text)
238 (define-key enriched-mode-map "\C-m" 'reindent-then-newline-and-indent)
239 (define-key enriched-mode-map "\C-j" 'reindent-then-newline-and-indent)
240 (define-key enriched-mode-map "\M-j" 'facemenu-justification-menu)
241 (define-key enriched-mode-map "\M-S" 'set-justification-center)
242 (define-key enriched-mode-map "\C-x\t" 'increase-left-margin)
243 (define-key enriched-mode-map "\C-c\C-l" 'set-left-margin)
244 (define-key enriched-mode-map "\C-c\C-r" 'set-right-margin)
245
246 ;;;
247 ;;; Some functions dealing with text-properties, especially indentation
248 ;;;
249
250 (defun enriched-map-property-regions (prop func &optional from to)
251 "Apply a function to regions of the buffer based on a text property.
252 For each contiguous region of the buffer for which the value of PROPERTY is
253 eq, the FUNCTION will be called. Optional arguments FROM and TO specify the
254 region over which to scan.
255
256 The specified function receives three arguments: the VALUE of the property in
257 the region, and the START and END of each region."
258 (save-excursion
259 (save-restriction
260 (if to (narrow-to-region (point-min) to))
261 (goto-char (or from (point-min)))
262 (let ((begin (point))
263 end
264 (marker (make-marker))
265 (val (get-text-property (point) prop)))
266 (while (setq end (text-property-not-all begin (point-max) prop val))
267 (move-marker marker end)
268 (funcall func val begin (marker-position marker))
269 (setq begin (marker-position marker)
270 val (get-text-property marker prop)))
271 (if (< begin (point-max))
272 (funcall func val begin (point-max)))))))
273
274 (put 'enriched-map-property-regions 'lisp-indent-hook 1)
275
276 (defun enriched-insert-indentation (&optional from to)
277 "Indent and justify each line in the region."
278 (save-excursion
279 (save-restriction
280 (if to (narrow-to-region (point-min) to))
281 (goto-char (or from (point-min)))
282 (if (not (bolp)) (forward-line 1))
283 (while (not (eobp))
284 (if (eolp)
285 nil ; skip blank lines
286 (indent-to (current-left-margin))
287 (justify-current-line t nil t))
288 (forward-line 1)))))
289
290 ;;;
291 ;;; Encoding Files
292 ;;;
293
294 ;;;###autoload
295 (defun enriched-encode (from to orig-buf)
296 (if enriched-verbose (message "Enriched: encoding document..."))
297 (save-restriction
298 (narrow-to-region from to)
299 (delete-to-left-margin)
300 (unjustify-region)
301 (goto-char from)
302 (format-replace-strings '(("<" . "<<")))
303 (format-insert-annotations
304 (format-annotate-region from (point-max) enriched-translations
305 'enriched-make-annotation enriched-ignore))
306 (goto-char from)
307 (insert (if (stringp enriched-initial-annotation)
308 enriched-initial-annotation
309 (save-excursion
310 ;; Eval this in the buffer we are annotating. This
311 ;; fixes a bug which was saving incorrect File-Width
312 ;; information, since we were looking at local
313 ;; variables in the wrong buffer.
314 (if orig-buf (set-buffer orig-buf))
315 (funcall enriched-initial-annotation))))
316 (enriched-map-property-regions 'hard
317 (lambda (v b e)
318 (if (and v (= ?\n (char-after b)))
319 (progn (goto-char b) (insert "\n"))))
320 (point) nil)
321 (if enriched-verbose (message nil))
322 ;; Return new end.
323 (point-max)))
324
325 (defun enriched-make-annotation (name positive)
326 "Format an annotation called NAME.
327 If POSITIVE is non-nil, this is the opening annotation, if nil, this is the
328 matching close."
329 (cond ((stringp name)
330 (format enriched-annotation-format (if positive "" "/") name))
331 ;; Otherwise it is an annotation with parameters, represented as a list
332 (positive
333 (let ((item (car name))
334 (params (cdr name)))
335 (concat (format enriched-annotation-format "" item)
336 (mapconcat (lambda (i) (concat "<param>" i "</param>"))
337 params ""))))
338 (t (format enriched-annotation-format "/" (car name)))))
339
340 (defun enriched-encode-other-face (old new)
341 "Generate annotations for random face change.
342 One annotation each for foreground color, background color, italic, etc."
343 (cons (and old (enriched-face-ans old))
344 (and new (enriched-face-ans new))))
345
346 (defun enriched-face-ans (face)
347 "Return annotations specifying FACE."
348 (cond ((string-match "^fg:" (symbol-name face))
349 (list (list "x-color" (substring (symbol-name face) 3))))
350 ((string-match "^bg:" (symbol-name face))
351 (list (list "x-bg-color" (substring (symbol-name face) 3))))
352 ((let* ((fg (face-foreground face))
353 (bg (face-background face))
354 (props (face-font face t))
355 (ans (cdr (format-annotate-single-property-change
356 'face nil props enriched-translations))))
357 (if fg (setq ans (cons (list "x-color" fg) ans)))
358 (if bg (setq ans (cons (list "x-bg-color" bg) ans)))
359 ans))))
360
361 ;;;
362 ;;; Decoding files
363 ;;;
364
365 ;;;###autoload
366 (defun enriched-decode (from to)
367 (if enriched-verbose (message "Enriched: decoding document..."))
368 (use-hard-newlines 1 'never)
369 (save-excursion
370 (save-restriction
371 (narrow-to-region from to)
372 (goto-char from)
373
374 ;; Deal with header
375 (let ((file-width (enriched-get-file-width)))
376 (enriched-remove-header)
377
378 ;; Deal with newlines
379 (while (search-forward-regexp "\n\n+" nil t)
380 (if (current-justification)
381 (delete-char -1))
382 (set-hard-newline-properties (match-beginning 0) (point)))
383
384 ;; Translate annotations
385 (format-deannotate-region from (point-max) enriched-translations
386 'enriched-next-annotation)
387
388 ;; Indent or fill the buffer
389 (cond (file-width ; File was filled to this width
390 (setq fill-column file-width)
391 (if enriched-verbose (message "Indenting..."))
392 (enriched-insert-indentation))
393 (t ; File was not filled.
394 (if enriched-verbose (message "Filling paragraphs..."))
395 (fill-region (point-min) (point-max))))
396 (if enriched-verbose (message nil)))
397 (point-max))))
398
399 (defun enriched-next-annotation ()
400 "Find and return next text/enriched annotation.
401 Any \"<<\" strings encountered are converted to \"<\".
402 Return value is \(begin end name positive-p), or nil if none was found."
403 (while (and (search-forward "<" nil 1)
404 (progn (goto-char (match-beginning 0))
405 (not (looking-at enriched-annotation-regexp))))
406 (forward-char 1)
407 (if (= ?< (char-after (point)))
408 (delete-char 1)
409 ;; A single < that does not start an annotation is an error,
410 ;; which we note and then ignore.
411 (message "Warning: malformed annotation in file at %s"
412 (1- (point)))))
413 (if (not (eobp))
414 (let* ((beg (match-beginning 0))
415 (end (match-end 0))
416 (name (downcase (buffer-substring
417 (match-beginning 2) (match-end 2))))
418 (pos (not (match-beginning 1))))
419 (list beg end name pos))))
420
421 (defun enriched-get-file-width ()
422 "Look for file width information on this line."
423 (save-excursion
424 (if (search-forward "Text-Width: " (+ (point) 1000) t)
425 (read (current-buffer)))))
426
427 (defun enriched-remove-header ()
428 "Remove file-format header at point."
429 (while (looking-at "^[-A-Za-z]+: .*\n")
430 (delete-region (point) (match-end 0)))
431 (if (looking-at "^\n")
432 (delete-char 1)))
433
434 (defun enriched-decode-foreground (from to &optional color)
435 (let ((face (intern (concat "fg:" color))))
436 (cond ((null color)
437 (message "Warning: no color specified for <x-color>"))
438 ((internal-find-face face))
439 ((and window-system (facemenu-get-face face)))
440 ((make-face face)
441 (message "Warning: color `%s' can't be displayed" color)))
442 (list from to 'face face)))
443
444 (defun enriched-decode-background (from to &optional color)
445 (let ((face (intern (concat "bg:" color))))
446 (cond ((null color)
447 (message "Warning: no color specified for <x-bg-color>"))
448 ((internal-find-face face))
449 ((and window-system (facemenu-get-face face)))
450 ((make-face face)
451 (message "Warning: color `%s' can't be displayed" color)))
452 (list from to 'face face)))
453
454 ;;; enriched.el ends here