annotate lisp/international/utf-7.el @ 53973:f5cb73154f03

*** empty log message ***
author Eli Zaretskii <eliz@is.elta.co.il>
date Mon, 16 Feb 2004 12:07:09 +0000
parents 695cf19ef79e
children 18a818a2ee7c 375f2633d815
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50897
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
1 ;;; utf-7.el --- utf-7 coding system
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
2
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 2003 Free Software Foundation, Inc.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
4
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
5 ;; Author: Dave Love <fx@gnu.org>
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
6 ;; Keywords: i18n, mail
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
7
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
8 ;; This file is free software; you can redistribute it and/or modify
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
9 ;; it under the terms of the GNU General Public License as published by
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
10 ;; the Free Software Foundation; either version 2, or (at your option)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
11 ;; any later version.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
12
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
13 ;; This file is distributed in the hope that it will be useful,
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
16 ;; GNU General Public License for more details.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
17
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
18 ;; You should have received a copy of the GNU General Public License
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
20 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
21 ;; Boston, MA 02111-1307, USA.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
22
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
23 ;;; Commentary:
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
24
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
25 ;; Defines a coding system for UTF-7, defined in RFC 2152. Non-ASCII
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
26 ;; segments are encoded as base64-encoded big endian UTF-16. Also
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
27 ;; defines a variation required for IMAP (RFC 2060).
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
28
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
29 ;; The encoding and decoding was originally taken from Jon K Hellan's
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
30 ;; implementation in Gnus, but has been substantially re-done.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
31
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
32 ;; This probably needs more attention. In particular, it's not
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
33 ;; completely consistent with iconv's behaviour. It's arguable
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
34 ;; whether the IMAP version should be a coding system since it's
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
35 ;; apparently only used for IMAP mailbox names, so it's commented out.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
36
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
37 ;;; Code:
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
38
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
39 (make-coding-system
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
40 'utf-7 0 ?U
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
41 "UTF-7 encoding of Unicode (RFC 2152)"
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
42 nil
51630
90215b7be64d Adjusted for the name change: xxx-utf-16-{le,be} -> xxx-utf-16{le,be}.
Kenichi Handa <handa@m17n.org>
parents: 50897
diff changeset
43 `((safe-chars . ,(coding-system-get 'utf-16be 'safe-chars))
50897
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
44 (mime-charset . utf-7)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
45 (pre-write-conversion . utf-7-pre-write-conversion)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
46 (post-read-conversion . utf-7-post-read-conversion)))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
47
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
48 ;; (make-coding-system
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
49 ;; 'utf-7-imap 0 ?u
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
50 ;; "UTF-7 encoding of Unicode, IMAP version (RFC 2060)"
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
51 ;; nil
51630
90215b7be64d Adjusted for the name change: xxx-utf-16-{le,be} -> xxx-utf-16{le,be}.
Kenichi Handa <handa@m17n.org>
parents: 50897
diff changeset
52 ;; `((safe-chars . ,(coding-system-get 'utf-16be 'safe-chars))
50897
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
53 ;; (pre-write-conversion . utf-7-imap-pre-write-conversion)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
54 ;; (post-read-conversion . utf-7-imap-post-read-conversion)))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
55
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
56 (defun utf-7-decode (len imap)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
57 "Decode LEN bytes of UTF-7 at point.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
58 IMAP non-nil means use the IMAP version."
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
59 (save-excursion
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
60 (save-restriction
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
61 (narrow-to-region (point) (+ (point) len))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
62 (let ((not-esc (if imap "^&" "^+"))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
63 (skip-chars (if imap "A-Za-z0-9+," "A-Za-z0-9+/")))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
64 (while (not (eobp))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
65 (skip-chars-forward not-esc)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
66 (unless (eobp)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
67 (forward-char)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
68 (let ((p (point))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
69 (run-length (skip-chars-forward skip-chars)))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
70 (if (eq ?- (char-after))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
71 (delete-char 1))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
72 (unless (= run-length 0) ; encoded lone esc-char
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
73 (let ((pl (mod (- run-length) 4)))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
74 (insert-char ?= pl)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
75 (if imap
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
76 (subst-char-in-region p (point) ?, ?/))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
77 (base64-decode-region p (point)))
51630
90215b7be64d Adjusted for the name change: xxx-utf-16-{le,be} -> xxx-utf-16{le,be}.
Kenichi Handa <handa@m17n.org>
parents: 50897
diff changeset
78 (decode-coding-region p (point) 'utf-16be)
50897
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
79 (save-excursion
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
80 (goto-char p)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
81 (delete-backward-char 1)))))))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
82 (- (point-max) (point-min)))))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
83
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
84 (defun utf-7-post-read-conversion (len)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
85 (utf-7-decode len nil))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
86
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
87 ;; (defun utf-7-imap-post-read-conversion (len)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
88 ;; (utf-7-decode len t))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
89
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
90 (defun utf-7-encode (from to imap)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
91 "Encode bytes between FROM and TO to UTF-7.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
92 ESC and SKIP-CHARS are adjusted for the normal and IMAP versions."
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
93 (let* ((old-buf (current-buffer))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
94 (esc (if imap ?& ?+))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
95 ;; These are characters which can be encoded asis.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
96 (skip-chars (if imap
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
97 "\t\n\r\x20-\x25\x27-\x7e" ; rfc2060
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
98 ;; This includes the rfc2152 optional set.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
99 ;; Perhaps it shouldn't (like iconv).
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
100 "\t\n\r -*,-[]-}"))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
101 (not-skip-chars (format "^%s%c" skip-chars esc)))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
102 (set-buffer (generate-new-buffer " *temp*"))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
103 (if (stringp from)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
104 (insert from)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
105 (insert-buffer-substring old-buf from to))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
106 (goto-char (point-min))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
107 (while (not (eobp))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
108 (skip-chars-forward skip-chars)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
109 (if (eq ?+ (char-after))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
110 (progn (forward-char)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
111 (insert ?-))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
112 (unless (eobp)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
113 (insert esc)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
114 (let ((p (point)))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
115 (skip-chars-forward not-skip-chars)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
116 (save-restriction
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
117 ;; encode-coding-region doesn't preserve point
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
118 (narrow-to-region p (point))
51630
90215b7be64d Adjusted for the name change: xxx-utf-16-{le,be} -> xxx-utf-16{le,be}.
Kenichi Handa <handa@m17n.org>
parents: 50897
diff changeset
119 (encode-coding-region p (point-max) 'utf-16be)
50897
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
120 (base64-encode-region p (point-max))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
121 (if imap
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
122 (subst-char-in-region p (point-max) ?/ ?,))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
123 (goto-char p)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
124 ;; As I read the RFC, this isn't correct, but it's
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
125 ;; consistent with iconv, at least regarding `='.
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
126 (skip-chars-forward "^= \t\n")
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
127 (delete-region (point) (point-max))))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
128 (unless (eobp)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
129 (insert ?-)))))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
130 nil))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
131
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
132 (defun utf-7-pre-write-conversion (from to)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
133 (utf-7-encode from to nil))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
134
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
135 ;; (defun utf-7-imap-pre-write-conversion (from to)
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
136 ;; (utf-7-encode from to t))
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
137
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
138 (provide 'utf-7)
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 51630
diff changeset
139
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 51630
diff changeset
140 ;;; arch-tag: 975ee403-90a4-4286-97d2-4ed1323f4ef9
50897
289523dd4fa7 *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
141 ;;; utf-7.el ends here