Add arch taglines
[bpt/emacs.git] / lisp / international / utf-7.el
CommitLineData
347003be
DL
1;;; utf-7.el --- utf-7 coding system
2
3;; Copyright (C) 2003 Free Software Foundation, Inc.
4
5;; Author: Dave Love <fx@gnu.org>
6;; Keywords: i18n, mail
7
8;; This file 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.
12
13;; This file 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.
17
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.
22
23;;; Commentary:
24
25;; Defines a coding system for UTF-7, defined in RFC 2152. Non-ASCII
26;; segments are encoded as base64-encoded big endian UTF-16. Also
27;; defines a variation required for IMAP (RFC 2060).
28
29;; The encoding and decoding was originally taken from Jon K Hellan's
30;; implementation in Gnus, but has been substantially re-done.
31
32;; This probably needs more attention. In particular, it's not
33;; completely consistent with iconv's behaviour. It's arguable
34;; whether the IMAP version should be a coding system since it's
35;; apparently only used for IMAP mailbox names, so it's commented out.
36
37;;; Code:
38
39(make-coding-system
40 'utf-7 0 ?U
41 "UTF-7 encoding of Unicode (RFC 2152)"
42 nil
9ef9b28e 43 `((safe-chars . ,(coding-system-get 'utf-16be 'safe-chars))
347003be
DL
44 (mime-charset . utf-7)
45 (pre-write-conversion . utf-7-pre-write-conversion)
46 (post-read-conversion . utf-7-post-read-conversion)))
47
48;; (make-coding-system
49;; 'utf-7-imap 0 ?u
50;; "UTF-7 encoding of Unicode, IMAP version (RFC 2060)"
51;; nil
9ef9b28e 52;; `((safe-chars . ,(coding-system-get 'utf-16be 'safe-chars))
347003be
DL
53;; (pre-write-conversion . utf-7-imap-pre-write-conversion)
54;; (post-read-conversion . utf-7-imap-post-read-conversion)))
55
56(defun utf-7-decode (len imap)
57 "Decode LEN bytes of UTF-7 at point.
58IMAP non-nil means use the IMAP version."
59 (save-excursion
60 (save-restriction
61 (narrow-to-region (point) (+ (point) len))
62 (let ((not-esc (if imap "^&" "^+"))
63 (skip-chars (if imap "A-Za-z0-9+," "A-Za-z0-9+/")))
64 (while (not (eobp))
65 (skip-chars-forward not-esc)
66 (unless (eobp)
67 (forward-char)
68 (let ((p (point))
69 (run-length (skip-chars-forward skip-chars)))
70 (if (eq ?- (char-after))
71 (delete-char 1))
72 (unless (= run-length 0) ; encoded lone esc-char
73 (let ((pl (mod (- run-length) 4)))
74 (insert-char ?= pl)
75 (if imap
76 (subst-char-in-region p (point) ?, ?/))
77 (base64-decode-region p (point)))
9ef9b28e 78 (decode-coding-region p (point) 'utf-16be)
347003be
DL
79 (save-excursion
80 (goto-char p)
81 (delete-backward-char 1)))))))
82 (- (point-max) (point-min)))))
83
84(defun utf-7-post-read-conversion (len)
85 (utf-7-decode len nil))
86
87;; (defun utf-7-imap-post-read-conversion (len)
88;; (utf-7-decode len t))
89
90(defun utf-7-encode (from to imap)
91 "Encode bytes between FROM and TO to UTF-7.
92ESC and SKIP-CHARS are adjusted for the normal and IMAP versions."
93 (let* ((old-buf (current-buffer))
94 (esc (if imap ?& ?+))
95 ;; These are characters which can be encoded asis.
96 (skip-chars (if imap
97 "\t\n\r\x20-\x25\x27-\x7e" ; rfc2060
98 ;; This includes the rfc2152 optional set.
99 ;; Perhaps it shouldn't (like iconv).
100 "\t\n\r -*,-[]-}"))
101 (not-skip-chars (format "^%s%c" skip-chars esc)))
102 (set-buffer (generate-new-buffer " *temp*"))
103 (if (stringp from)
104 (insert from)
105 (insert-buffer-substring old-buf from to))
106 (goto-char (point-min))
107 (while (not (eobp))
108 (skip-chars-forward skip-chars)
109 (if (eq ?+ (char-after))
110 (progn (forward-char)
111 (insert ?-))
112 (unless (eobp)
113 (insert esc)
114 (let ((p (point)))
115 (skip-chars-forward not-skip-chars)
116 (save-restriction
117 ;; encode-coding-region doesn't preserve point
118 (narrow-to-region p (point))
9ef9b28e 119 (encode-coding-region p (point-max) 'utf-16be)
347003be
DL
120 (base64-encode-region p (point-max))
121 (if imap
122 (subst-char-in-region p (point-max) ?/ ?,))
123 (goto-char p)
124 ;; As I read the RFC, this isn't correct, but it's
125 ;; consistent with iconv, at least regarding `='.
126 (skip-chars-forward "^= \t\n")
127 (delete-region (point) (point-max))))
128 (unless (eobp)
129 (insert ?-)))))
130 nil))
131
132(defun utf-7-pre-write-conversion (from to)
133 (utf-7-encode from to nil))
134
135;; (defun utf-7-imap-pre-write-conversion (from to)
136;; (utf-7-encode from to t))
137
138(provide 'utf-7)
ab5796a9
MB
139
140;;; arch-tag: 975ee403-90a4-4286-97d2-4ed1323f4ef9
347003be 141;;; utf-7.el ends here