Mercurial > emacs
annotate lisp/international/utf-7.el @ 91697:056e52861ec3
*** empty log message ***
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Sat, 09 Feb 2008 10:53:10 +0000 |
parents | 606f2d163a64 |
children | 4054054dd212 |
rev | line source |
---|---|
50897 | 1 ;;; utf-7.el --- utf-7 coding system |
2 | |
79709 | 3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
50897 | 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 | |
78274
451a2d341d55
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
10 ;; the Free Software Foundation; either version 3, or (at your option) |
50897 | 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 | |
64085 | 20 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 ;; Boston, MA 02110-1301, USA. | |
50897 | 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 (defun utf-7-decode (len imap) | |
40 "Decode LEN bytes of UTF-7 at point. | |
41 IMAP non-nil means use the IMAP version." | |
42 (save-excursion | |
43 (save-restriction | |
44 (narrow-to-region (point) (+ (point) len)) | |
45 (let ((not-esc (if imap "^&" "^+")) | |
46 (skip-chars (if imap "A-Za-z0-9+," "A-Za-z0-9+/"))) | |
47 (while (not (eobp)) | |
48 (skip-chars-forward not-esc) | |
49 (unless (eobp) | |
50 (forward-char) | |
51 (let ((p (point)) | |
52 (run-length (skip-chars-forward skip-chars))) | |
53 (if (eq ?- (char-after)) | |
54 (delete-char 1)) | |
55 (unless (= run-length 0) ; encoded lone esc-char | |
56 (let ((pl (mod (- run-length) 4))) | |
57 (insert-char ?= pl) | |
58 (if imap | |
59 (subst-char-in-region p (point) ?, ?/)) | |
60 (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
|
61 (decode-coding-region p (point) 'utf-16be) |
50897 | 62 (save-excursion |
63 (goto-char p) | |
64 (delete-backward-char 1))))))) | |
65 (- (point-max) (point-min))))) | |
66 | |
90250
1c8f24770e68
Delete the definition of utf-7.
Kenichi Handa <handa@m17n.org>
parents:
90200
diff
changeset
|
67 ;;;###autoload |
50897 | 68 (defun utf-7-post-read-conversion (len) |
69 (utf-7-decode len nil)) | |
70 | |
91117
57ad20761f87
(utf-7-imap-post-read-conversion)
Kenichi Handa <handa@m17n.org>
parents:
90996
diff
changeset
|
71 ;;;###autoload |
85926
025d69230a9b
(utf-7-imap): New coding system.
Kenichi Handa <handa@m17n.org>
parents:
78274
diff
changeset
|
72 (defun utf-7-imap-post-read-conversion (len) |
025d69230a9b
(utf-7-imap): New coding system.
Kenichi Handa <handa@m17n.org>
parents:
78274
diff
changeset
|
73 (utf-7-decode len t)) |
50897 | 74 |
75 (defun utf-7-encode (from to imap) | |
76 "Encode bytes between FROM and TO to UTF-7. | |
77 ESC and SKIP-CHARS are adjusted for the normal and IMAP versions." | |
78 (let* ((old-buf (current-buffer)) | |
79 (esc (if imap ?& ?+)) | |
80 ;; These are characters which can be encoded asis. | |
81 (skip-chars (if imap | |
82 "\t\n\r\x20-\x25\x27-\x7e" ; rfc2060 | |
83 ;; This includes the rfc2152 optional set. | |
84 ;; Perhaps it shouldn't (like iconv). | |
85 "\t\n\r -*,-[]-}")) | |
86 (not-skip-chars (format "^%s%c" skip-chars esc))) | |
87 (set-buffer (generate-new-buffer " *temp*")) | |
88 (if (stringp from) | |
89 (insert from) | |
90 (insert-buffer-substring old-buf from to)) | |
91 (goto-char (point-min)) | |
92 (while (not (eobp)) | |
93 (skip-chars-forward skip-chars) | |
94 (if (eq ?+ (char-after)) | |
95 (progn (forward-char) | |
96 (insert ?-)) | |
97 (unless (eobp) | |
98 (insert esc) | |
99 (let ((p (point))) | |
100 (skip-chars-forward not-skip-chars) | |
101 (save-restriction | |
102 ;; encode-coding-region doesn't preserve point | |
103 (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
|
104 (encode-coding-region p (point-max) 'utf-16be) |
50897 | 105 (base64-encode-region p (point-max)) |
106 (if imap | |
107 (subst-char-in-region p (point-max) ?/ ?,)) | |
108 (goto-char p) | |
109 ;; As I read the RFC, this isn't correct, but it's | |
110 ;; consistent with iconv, at least regarding `='. | |
111 (skip-chars-forward "^= \t\n") | |
112 (delete-region (point) (point-max)))) | |
113 (unless (eobp) | |
114 (insert ?-))))) | |
115 nil)) | |
116 | |
90250
1c8f24770e68
Delete the definition of utf-7.
Kenichi Handa <handa@m17n.org>
parents:
90200
diff
changeset
|
117 ;;;###autoload |
50897 | 118 (defun utf-7-pre-write-conversion (from to) |
119 (utf-7-encode from to nil)) | |
120 | |
91117
57ad20761f87
(utf-7-imap-post-read-conversion)
Kenichi Handa <handa@m17n.org>
parents:
90996
diff
changeset
|
121 ;;;###autoload |
85926
025d69230a9b
(utf-7-imap): New coding system.
Kenichi Handa <handa@m17n.org>
parents:
78274
diff
changeset
|
122 (defun utf-7-imap-pre-write-conversion (from to) |
025d69230a9b
(utf-7-imap): New coding system.
Kenichi Handa <handa@m17n.org>
parents:
78274
diff
changeset
|
123 (utf-7-encode from to t)) |
50897 | 124 |
125 (provide 'utf-7) | |
52401 | 126 |
127 ;;; arch-tag: 975ee403-90a4-4286-97d2-4ed1323f4ef9 | |
50897 | 128 ;;; utf-7.el ends here |