annotate lisp/apel/richtext.el @ 203:850242ba4a81 r20-3b28

Import from CVS: tag r20-3b28
author cvs
date Mon, 13 Aug 2007 10:02:21 +0200
parents 85ec50267440
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
1 ;;; richtext.el -- read and save files in text/richtext format
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
2
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
3 ;; Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
4
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
6 ;; Created: 1995/7/15
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 155
diff changeset
7 ;; Version: $Id: richtext.el,v 1.2 1997/06/29 23:12:06 steve Exp $
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
8 ;; Keywords: wp, faces, MIME, multimedia
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
9
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
10 ;; This file is not part of GNU Emacs yet.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
11
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
12 ;; This program is free software; you can redistribute it and/or
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
13 ;; modify it under the terms of the GNU General Public License as
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
14 ;; published by the Free Software Foundation; either version 2, or (at
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
15 ;; your option) any later version.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
16
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
17 ;; This program is distributed in the hope that it will be useful, but
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
20 ;; General Public License for more details.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
21
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
25 ;; Boston, MA 02111-1307, USA.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
26
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
27 ;;; Code:
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
28
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
29 (require 'enriched)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
30
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
31
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
32 ;;; @ variables
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
33 ;;;
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
34
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
35 (defconst richtext-initial-annotation
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
36 (lambda ()
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
37 (format "Content-Type: text/richtext\nText-Width: %d\n\n"
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
38 (enriched-text-width)))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
39 "What to insert at the start of a text/richtext file.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
40 If this is a string, it is inserted. If it is a list, it should be a lambda
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
41 expression, which is evaluated to get the string to insert.")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
42
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
43 (defconst richtext-annotation-regexp
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
44 "[ \t\n]*\\(<\\(/\\)?\\([-A-za-z0-9]+\\)>\\)[ \t\n]*"
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
45 "Regular expression matching richtext annotations.")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
46
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
47 (defconst richtext-translations
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
48 '((face (bold-italic "bold" "italic")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
49 (bold "bold")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
50 (italic "italic")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
51 (underline "underline")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
52 (fixed "fixed")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
53 (excerpt "excerpt")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
54 (default )
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
55 (nil enriched-encode-other-face))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
56 (invisible (t "comment"))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
57 (left-margin (4 "indent"))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
58 (right-margin (4 "indentright"))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
59 (justification (right "flushright")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
60 (left "flushleft")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
61 (full "flushboth")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
62 (center "center"))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
63 ;; The following are not part of the standard:
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
64 (FUNCTION (enriched-decode-foreground "x-color")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
65 (enriched-decode-background "x-bg-color"))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
66 (read-only (t "x-read-only"))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
67 (unknown (nil format-annotate-value))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
68 ; (font-size (2 "bigger") ; unimplemented
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
69 ; (-2 "smaller"))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
70 )
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
71 "List of definitions of text/richtext annotations.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
72 See `format-annotate-region' and `format-deannotate-region' for the definition
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
73 of this structure.")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
74
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
75
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
76 ;;; @ encoder
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
77 ;;;
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
78
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 155
diff changeset
79 ;;;###autoload
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
80 (defun richtext-encode (from to)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
81 (if enriched-verbose (message "Richtext: encoding document..."))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
82 (save-restriction
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
83 (narrow-to-region from to)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
84 (delete-to-left-margin)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
85 (unjustify-region)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
86 (goto-char from)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
87 (format-replace-strings '(("<" . "<lt>")))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
88 (format-insert-annotations
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
89 (format-annotate-region from (point-max) richtext-translations
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
90 'enriched-make-annotation enriched-ignore))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
91 (goto-char from)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
92 (insert (if (stringp enriched-initial-annotation)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
93 richtext-initial-annotation
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
94 (funcall richtext-initial-annotation)))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
95 (enriched-map-property-regions 'hard
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
96 (lambda (v b e)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
97 (goto-char b)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
98 (if (eolp)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
99 (while (search-forward "\n" nil t)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
100 (replace-match "<nl>\n")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
101 )))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
102 (point) nil)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
103 (if enriched-verbose (message nil))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
104 ;; Return new end.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
105 (point-max)))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
106
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
107
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
108 ;;; @ decoder
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
109 ;;;
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
110
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
111 (defun richtext-next-annotation ()
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
112 "Find and return next text/richtext annotation.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
113 Return value is \(begin end name positive-p), or nil if none was found."
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
114 (catch 'tag
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
115 (while (re-search-forward richtext-annotation-regexp nil t)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
116 (let* ((beg0 (match-beginning 0))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
117 (end0 (match-end 0))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
118 (beg (match-beginning 1))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
119 (end (match-end 1))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
120 (name (downcase (buffer-substring
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
121 (match-beginning 3) (match-end 3))))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
122 (pos (not (match-beginning 2)))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
123 )
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
124 (cond ((equal name "lt")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
125 (delete-region beg end)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
126 (goto-char beg)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
127 (insert "<")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
128 )
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
129 ((equal name "comment")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
130 (if pos
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
131 (throw 'tag (list beg0 end name pos))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
132 (throw 'tag (list beg end0 name pos))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
133 )
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
134 )
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
135 (t
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
136 (throw 'tag (list beg end name pos))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
137 ))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
138 ))))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
139
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 155
diff changeset
140 ;;;###autoload
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
141 (defun richtext-decode (from to)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
142 (if enriched-verbose (message "Richtext: decoding document..."))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
143 (save-excursion
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
144 (save-restriction
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
145 (narrow-to-region from to)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
146 (goto-char from)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
147 (let ((file-width (enriched-get-file-width))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
148 (use-hard-newlines t))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
149 (enriched-remove-header)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
150
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
151 (goto-char from)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
152 (while (re-search-forward "\n\n+" nil t)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
153 (replace-match "\n")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
154 )
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
156 ;; Deal with newlines
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
157 (goto-char from)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
158 (while (re-search-forward "[ \t\n]*<nl>[ \t\n]*" nil t)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
159 (replace-match "\n")
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
160 (put-text-property (match-beginning 0) (point) 'hard t)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
161 (put-text-property (match-beginning 0) (point) 'front-sticky nil)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
162 )
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
163
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
164 ;; Translate annotations
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
165 (format-deannotate-region from (point-max) richtext-translations
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
166 'richtext-next-annotation)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
167
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
168 ;; Fill paragraphs
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
169 (if (and file-width ; possible reasons not to fill:
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
170 (= file-width (enriched-text-width))) ; correct wd.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
171 ;; Minimally, we have to insert indentation and justification.
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
172 (enriched-insert-indentation)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
173 (if enriched-verbose (message "Filling paragraphs..."))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
174 (fill-region (point-min) (point-max))))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
175 (if enriched-verbose (message nil))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
176 (point-max))))
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
177
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
178
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
179 ;;; @ end
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
180 ;;;
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
181
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
182 (provide 'richtext)
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
183
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents:
diff changeset
184 ;;; richtext.el ends here