New feature: toggle visibility of mime buttons.
[more-wl.git] / elmo / utf7.el
blobc6081e8d6ec5dd8d930483869bffba2850e748aa
1 ;;; utf7.el --- UTF-7 encoding/decoding for Emacs -*-coding: iso-8859-1;-*-
2 ;; Copyright (C) 1999, 2000, 2003 Free Software Foundation, Inc.
4 ;; Author: Jon K Hellan <hellan@acm.org>
5 ;; Maintainer: bugs@gnus.org
6 ;; Keywords: mail
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
23 ;;; Commentary:
25 ;; UTF-7 - A Mail-Safe Transformation Format of Unicode - RFC 2152
26 ;; This is a transformation format of Unicode that contains only 7-bit
27 ;; ASCII octets and is intended to be readable by humans in the limiting
28 ;; case that the document consists of characters from the US-ASCII
29 ;; repertoire.
30 ;; In short, runs of characters outside US-ASCII are encoded as base64
31 ;; inside delimiters.
32 ;; A variation of UTF-7 is specified in IMAP 4rev1 (RFC 2060) as the way
33 ;; to represent characters outside US-ASCII in mailbox names in IMAP.
34 ;; This library supports both variants, but the IMAP variation was the
35 ;; reason I wrote it.
36 ;; The routines convert UTF-7 -> UTF-16 (16 bit encoding of Unicode)
37 ;; -> current character set, and vice versa.
38 ;; However, until Emacs supports Unicode, the only Emacs character set
39 ;; supported here is ISO-8859.1, which can trivially be converted to/from
40 ;; Unicode.
41 ;; When decoding results in a character outside the Emacs character set,
42 ;; an error is thrown. It is up to the application to recover.
44 ;; UTF-7 should be done by providing a coding system. Mule-UCS does
45 ;; already, but I don't know if it does the IMAP version and it's not
46 ;; clear whether that should really be a coding system. The UTF-16
47 ;; part of the conversion can be done with coding systems available
48 ;; with Mule-UCS or some versions of Emacs. Unfortunately these were
49 ;; done wrongly (regarding handling of byte-order marks and how the
50 ;; variants were named), so we don't have a consistent name for the
51 ;; necessary coding system. The code below doesn't seem to DTRT
52 ;; generally. E.g.:
54 ;; (utf7-encode "a+£")
55 ;; => "a+ACsAow-"
57 ;; $ echo "a+£"|iconv -f iso-8859-1 -t utf-7
58 ;; a+-+AKM
60 ;; -- fx
62 ;; Modified 5 May 2004 by Yuuichi Teranishi so that it can run with APEL/FLIM
63 ;; instead of mm-util in Gnus.
65 ;; * Use find-coding-system instead of mm-coding-system-p.
66 ;; * Use mel.el instead of base64.el.
67 ;; * Don't use mm-with-unibyte-current-buffer etc.
68 ;; * Do nothing if utf-16 coding system is not found.
70 ;; Modified 31 Aug 2004 by Yuuichi Teranishi so that it can avoid the bug of
71 ;; Emacs 21.3 release version.
73 ;;; Code:
75 (eval-when-compile (require 'cl))
76 (require 'pces)
77 (require 'mel)
79 ;; base64 encoding/decoding
80 (defun utf7-base64-encode-region (beg end &optional no-line-break))
81 (defun utf7-base64-decode-region (beg end))
82 (fset 'utf7-base64-encode-region
83 (mel-find-function 'mime-encode-region "base64"))
84 (fset 'utf7-base64-decode-region
85 (mel-find-function 'mime-decode-region "base64"))
87 (defconst utf7-direct-encoding-chars " -%'-*,-[]-}"
88 "Character ranges which do not need escaping in UTF-7.")
90 (defconst utf7-imap-direct-encoding-chars
91 (concat utf7-direct-encoding-chars "+\\~")
92 "Character ranges which do not need escaping in the IMAP variant of UTF-7.")
94 (defconst utf7-utf-16-coding-system (and (fboundp 'find-coding-system)
95 (find-coding-system 'utf-16-be))
96 "Coding system which encodes big endian UTF-16.")
98 (defsubst utf7-imap-get-pad-length (len modulus)
99 "Return required length of padding for IMAP modified base64 fragment."
100 (mod (- len) modulus))
102 (defun utf7-encode-internal (&optional for-imap)
103 "Encode text in (temporary) buffer as UTF-7.
104 Use IMAP modification if FOR-IMAP is non-nil."
105 (let ((start (point-min))
106 (end (point-max)))
107 (narrow-to-region start end)
108 (goto-char start)
109 (let* ((esc-char (if for-imap ?& ?+))
110 (direct-encoding-chars
111 (if for-imap utf7-imap-direct-encoding-chars
112 utf7-direct-encoding-chars))
113 (not-direct-encoding-chars (concat "^" direct-encoding-chars)))
114 (while (not (eobp))
115 (skip-chars-forward direct-encoding-chars)
116 (unless (eobp)
117 (insert esc-char)
118 (let ((p (point))
119 (fc (following-char))
120 (run-length
121 (skip-chars-forward not-direct-encoding-chars)))
122 (if (and (= fc esc-char)
123 (= run-length 1)) ; Lone esc-char?
124 (delete-backward-char 1) ; Now there's one too many
125 (utf7-fragment-encode p (point) for-imap))
126 (insert "-")))))))
128 (defun utf7-fragment-encode (start end &optional for-imap)
129 "Encode text from START to END in buffer as UTF-7 escape fragment.
130 Use IMAP modification if FOR-IMAP is non-nil."
131 (let ((converter (utf7-get-u16char-converter 'to-utf-16))
132 (str (buffer-substring start end))
134 (when converter
135 (delete-region start end)
136 (goto-char start)
137 (insert
138 (with-temp-buffer
139 (insert str)
140 (funcall converter)
141 (set-buffer-multibyte nil)
142 (utf7-base64-encode-region (point-min) (point-max))
143 (goto-char (point-min))
144 (setq pm (point-max))
145 (when for-imap
146 (while (search-forward "/" nil t)
147 (replace-match ",")))
148 (skip-chars-forward "^= \t\n" pm)
149 (delete-region (point) pm)
150 (buffer-string))))))
152 (defun utf7-decode-internal (&optional for-imap)
153 "Decode UTF-7 text in (temporary) buffer.
154 Use IMAP modification if FOR-IMAP is non-nil."
155 (let ((start (point-min))
156 (end (point-max)))
157 (goto-char start)
158 (let* ((esc-pattern (concat "^" (char-to-string (if for-imap ?& ?+))))
159 (base64-chars (concat "A-Za-z0-9+"
160 (char-to-string (if for-imap ?, ?/)))))
161 (while (not (eobp))
162 (skip-chars-forward esc-pattern)
163 (unless (eobp)
164 (forward-char)
165 (let ((p (point))
166 (run-length (skip-chars-forward base64-chars)))
167 (when (and (not (eobp)) (= (following-char) ?-))
168 (delete-char 1))
169 (unless (= run-length 0) ; Encoded lone esc-char?
170 (save-excursion
171 (utf7-fragment-decode p (point) for-imap)
172 (goto-char p)
173 (delete-backward-char 1)))))))))
175 (defun utf7-fragment-decode (start end &optional for-imap)
176 "Decode base64 encoded fragment from START to END of UTF-7 text in buffer.
177 Use IMAP modification if FOR-IMAP is non-nil."
178 (save-restriction
179 (narrow-to-region start end)
180 (let ((converter (utf7-get-u16char-converter 'from-utf-16)))
181 (when converter
182 (when for-imap
183 (goto-char start)
184 (while (search-forward "," nil 'move-to-end) (replace-match "/")))
185 (let ((pl (utf7-imap-get-pad-length (- end start) 4)))
186 (insert (make-string pl ?=))
187 (utf7-base64-decode-region start (+ end pl)))
188 (funcall converter)))))
190 (defun utf7-get-u16char-converter (which-way)
191 "Return a function to convert between UTF-16 and current character set."
192 (if utf7-utf-16-coding-system
193 (if (eq which-way 'to-utf-16)
194 (lambda ()
195 (encode-coding-region (point-min)(point-max)
196 utf7-utf-16-coding-system)
197 (set-buffer-multibyte nil)
198 (goto-char (point-min))
199 ;; Remove BOM (Big-endian UTF-16 FE FF)
200 (while (re-search-forward "\376\377" nil t)
201 (delete-region (match-beginning 0)(match-end 0))))
202 (lambda ()
203 (goto-char (point-min))
204 ;; Add BOM (Big-endian UTF-16 FE FF)
205 (insert "\376\377")
206 (decode-coding-region (point-min) (point-max)
207 utf7-utf-16-coding-system)))))
209 (defun utf7-encode (string &optional for-imap)
210 "Encode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
211 (let ((default-enable-multibyte-characters t))
212 (with-temp-buffer
213 (insert string)
214 (utf7-encode-internal for-imap)
215 (buffer-string))))
217 (defun utf7-decode (string &optional for-imap)
218 "Decode UTF-7 STRING. Use IMAP modification if FOR-IMAP is non-nil."
219 (let ((default-enable-multibyte-characters nil))
220 (with-temp-buffer
221 (insert string)
222 (utf7-decode-internal for-imap)
223 (set-buffer-multibyte t)
224 (buffer-string))))
226 (defalias 'utf7-encode-string 'utf7-encode)
227 (defalias 'utf7-decode-string 'utf7-decode)
229 (provide 'utf7)
231 ;;; utf7.el ends here