Mercurial > emacs
comparison lisp/gnus/rfc1843.el @ 31717:6b20b7e85e3c
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Tue, 19 Sep 2000 13:40:08 +0000 |
parents | |
children | 95da1a83f93b |
comparison
equal
deleted
inserted
replaced
31716:9968f55ad26e | 31717:6b20b7e85e3c |
---|---|
1 ;;; rfc1843.el --- HZ (rfc1843) decoding | |
2 ;; Copyright (c) 1998, 1999, 2000 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu> | |
5 ;; Keywords: news HZ HZ+ mail i18n | |
6 | |
7 ;; This file is a part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published | |
11 ;; by the Free Software Foundation; either version 2, or (at your | |
12 ;; option) any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, but | |
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 ;; General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;; Usage: | |
27 ;; (require 'rfc1843) | |
28 ;; (rfc1843-gnus-setup) | |
29 ;; | |
30 ;; Test: | |
31 ;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}") | |
32 | |
33 ;;; Code: | |
34 | |
35 (require 'mm-util) | |
36 | |
37 (defvar rfc1843-word-regexp | |
38 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)") | |
39 | |
40 (defvar rfc1843-word-regexp-strictly | |
41 "~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)") | |
42 | |
43 (defvar rfc1843-hzp-word-regexp | |
44 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\ | |
45 [<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)") | |
46 | |
47 (defvar rfc1843-hzp-word-regexp-strictly | |
48 "~\\({\\([\041-\167][\041-\176]\\)+\\|\ | |
49 [<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)") | |
50 | |
51 (defcustom rfc1843-decode-loosely nil | |
52 "Loosely check HZ encoding if non-nil. | |
53 When it is set non-nil, only buffers or strings with strictly | |
54 HZ-encoded are decoded." | |
55 :type 'boolean | |
56 :group 'gnus) | |
57 | |
58 (defcustom rfc1843-decode-hzp t | |
59 "HZ+ decoding support if non-nil. | |
60 HZ+ specification (also known as HZP) is to provide a standardized | |
61 7-bit representation of mixed Big5, GB, and ASCII text for convenient | |
62 e-mail transmission, news posting, etc. | |
63 The document of HZ+ 0.78 specification can be found at | |
64 ftp://ftp.math.psu.edu/pub/simpson/chinese/hzp/hzp.doc" | |
65 :type 'boolean | |
66 :group 'gnus) | |
67 | |
68 (defcustom rfc1843-newsgroups-regexp "chinese\\|hz" | |
69 "Regexp of newsgroups in which might be HZ encoded." | |
70 :type 'string | |
71 :group 'gnus) | |
72 | |
73 (defun rfc1843-decode-region (from to) | |
74 "Decode HZ in the region between FROM and TO." | |
75 (interactive "r") | |
76 (let (str firstc) | |
77 (save-excursion | |
78 (goto-char from) | |
79 (if (or rfc1843-decode-loosely | |
80 (re-search-forward (if rfc1843-decode-hzp | |
81 rfc1843-hzp-word-regexp-strictly | |
82 rfc1843-word-regexp-strictly) to t)) | |
83 (save-restriction | |
84 (narrow-to-region from to) | |
85 (goto-char (point-min)) | |
86 (while (re-search-forward (if rfc1843-decode-hzp | |
87 rfc1843-hzp-word-regexp | |
88 rfc1843-word-regexp) (point-max) t) | |
89 ;;; Text with extents may cause XEmacs crash | |
90 (setq str (buffer-substring-no-properties | |
91 (match-beginning 1) | |
92 (match-end 1))) | |
93 (setq firstc (aref str 0)) | |
94 (insert (mm-decode-coding-string | |
95 (rfc1843-decode | |
96 (prog1 | |
97 (substring str 1) | |
98 (delete-region (match-beginning 0) (match-end 0))) | |
99 firstc) | |
100 (if (eq firstc ?{) 'cn-gb-2312 'cn-big5)))) | |
101 (goto-char (point-min)) | |
102 (while (search-forward "~" (point-max) t) | |
103 (cond ((eq (char-after) ?\n) | |
104 (delete-char -1) | |
105 (delete-char 1)) | |
106 ((eq (char-after) ?~) | |
107 (delete-char 1))))))))) | |
108 | |
109 (defun rfc1843-decode-string (string) | |
110 "Decode HZ STRING and return the results." | |
111 (let ((m (mm-multibyte-p))) | |
112 (with-temp-buffer | |
113 (when m | |
114 (mm-enable-multibyte)) | |
115 (insert string) | |
116 (inline | |
117 (rfc1843-decode-region (point-min) (point-max))) | |
118 (buffer-string)))) | |
119 | |
120 (defun rfc1843-decode (word &optional firstc) | |
121 "Decode HZ WORD and return it." | |
122 (let ((i -1) (s (substring word 0)) v) | |
123 (if (or (not firstc) (eq firstc ?{)) | |
124 (while (< (incf i) (length s)) | |
125 (if (eq (setq v (aref s i)) ? ) nil | |
126 (aset s i (+ 128 v)))) | |
127 (while (< (incf i) (length s)) | |
128 (if (eq (setq v (aref s i)) ? ) nil | |
129 (setq v (+ (* 94 v) (aref s (1+ i)) -3135)) | |
130 (aset s i (+ (/ v 157) (if (eq firstc ?<) 201 161))) | |
131 (setq v (% v 157)) | |
132 (aset s (incf i) (+ v (if (< v 63) 64 98)))))) | |
133 s)) | |
134 | |
135 (defun rfc1843-decode-article-body () | |
136 "Decode HZ encoded text in the article body." | |
137 (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
138 (or gnus-newsgroup-name "")) | |
139 (save-excursion | |
140 (save-restriction | |
141 (message-narrow-to-head) | |
142 (let* ((inhibit-point-motion-hooks t) | |
143 (case-fold-search t) | |
144 (ct (message-fetch-field "Content-Type" t)) | |
145 (ctl (and ct (ignore-errors | |
146 (mail-header-parse-content-type ct))))) | |
147 (if (and ctl (not (string-match "/" (car ctl)))) | |
148 (setq ctl nil)) | |
149 (goto-char (point-max)) | |
150 (widen) | |
151 (forward-line 1) | |
152 (narrow-to-region (point) (point-max)) | |
153 (when (or (not ctl) | |
154 (equal (car ctl) "text/plain")) | |
155 (rfc1843-decode-region (point) (point-max)))))))) | |
156 | |
157 (defvar rfc1843-old-gnus-decode-header-function nil) | |
158 (defvar gnus-decode-header-methods) | |
159 (defvar gnus-decode-encoded-word-methods) | |
160 | |
161 (defun rfc1843-gnus-setup () | |
162 "Setup HZ decoding for Gnus." | |
163 (require 'gnus-art) | |
164 (require 'gnus-sum) | |
165 (add-hook 'gnus-article-decode-hook 'rfc1843-decode-article-body t) | |
166 (setq gnus-decode-encoded-word-function | |
167 'gnus-multi-decode-encoded-word-string | |
168 gnus-decode-header-function | |
169 'gnus-multi-decode-header | |
170 gnus-decode-encoded-word-methods | |
171 (nconc gnus-decode-encoded-word-methods | |
172 (list | |
173 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
174 'rfc1843-decode-string))) | |
175 gnus-decode-header-methods | |
176 (nconc gnus-decode-header-methods | |
177 (list | |
178 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
179 'rfc1843-decode-region))))) | |
180 | |
181 (provide 'rfc1843) | |
182 | |
183 ;;; rfc1843.el ends here |