Mercurial > emacs
annotate lisp/gnus/rfc1843.el @ 88018:4e0d27f39575
*** empty log message ***
author | Thien-Thi Nguyen <ttn@gnuvola.org> |
---|---|
date | Sun, 27 Jan 2008 11:06:45 +0000 |
parents | 107ccd98fa12 |
children | 606f2d163a64 1e3a407766b9 |
rev | line source |
---|---|
31717 | 1 ;;; rfc1843.el --- HZ (rfc1843) decoding |
64754
fafd692d1e40
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
2 |
74547 | 3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
79708 | 4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
31717 | 5 |
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu> | |
7 ;; Keywords: news HZ HZ+ mail i18n | |
8 | |
38413
a26d9b55abb6
Some fixes to follow coding conventions in files from Gnus.
Pavel Janík <Pavel@Janik.cz>
parents:
32178
diff
changeset
|
9 ;; This file is part of GNU Emacs. |
31717 | 10 |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published | |
78224
24202b793a08
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
13 ;; by the Free Software Foundation; either version 3, or (at your |
31717 | 14 ;; option) any later version. |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, but | |
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 ;; General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 ;; Boston, MA 02110-1301, USA. | |
31717 | 25 |
26 ;;; Commentary: | |
27 | |
28 ;; Usage: | |
29 ;; (require 'rfc1843) | |
30 ;; (rfc1843-gnus-setup) | |
31 ;; | |
32 ;; Test: | |
33 ;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}") | |
34 | |
35 ;;; Code: | |
36 | |
87229
e4e336c0677e
Add declare-function compatibility definition.
Glenn Morris <rgm@gnu.org>
parents:
78224
diff
changeset
|
37 ;; For Emacs < 22.2. |
e4e336c0677e
Add declare-function compatibility definition.
Glenn Morris <rgm@gnu.org>
parents:
78224
diff
changeset
|
38 (eval-and-compile |
e4e336c0677e
Add declare-function compatibility definition.
Glenn Morris <rgm@gnu.org>
parents:
78224
diff
changeset
|
39 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))) |
e4e336c0677e
Add declare-function compatibility definition.
Glenn Morris <rgm@gnu.org>
parents:
78224
diff
changeset
|
40 |
32178 | 41 (eval-when-compile (require 'cl)) |
31717 | 42 (require 'mm-util) |
43 | |
65283
48de3c91b9bc
(gnus-decode-encoded-word-function, gnus-decode-header-function,
Juanma Barranquero <lekktu@gmail.com>
parents:
64754
diff
changeset
|
44 (defvar gnus-decode-encoded-word-function) |
48de3c91b9bc
(gnus-decode-encoded-word-function, gnus-decode-header-function,
Juanma Barranquero <lekktu@gmail.com>
parents:
64754
diff
changeset
|
45 (defvar gnus-decode-header-function) |
48de3c91b9bc
(gnus-decode-encoded-word-function, gnus-decode-header-function,
Juanma Barranquero <lekktu@gmail.com>
parents:
64754
diff
changeset
|
46 (defvar gnus-newsgroup-name) |
48de3c91b9bc
(gnus-decode-encoded-word-function, gnus-decode-header-function,
Juanma Barranquero <lekktu@gmail.com>
parents:
64754
diff
changeset
|
47 |
31717 | 48 (defvar rfc1843-word-regexp |
49 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)") | |
50 | |
51 (defvar rfc1843-word-regexp-strictly | |
52 "~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)") | |
53 | |
54 (defvar rfc1843-hzp-word-regexp | |
55 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\ | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
56 \[<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)") |
31717 | 57 |
58 (defvar rfc1843-hzp-word-regexp-strictly | |
59 "~\\({\\([\041-\167][\041-\176]\\)+\\|\ | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
60 \[<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)") |
31717 | 61 |
62 (defcustom rfc1843-decode-loosely nil | |
63 "Loosely check HZ encoding if non-nil. | |
64 When it is set non-nil, only buffers or strings with strictly | |
65 HZ-encoded are decoded." | |
66 :type 'boolean | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
67 :group 'mime) |
31717 | 68 |
69 (defcustom rfc1843-decode-hzp t | |
70 "HZ+ decoding support if non-nil. | |
71 HZ+ specification (also known as HZP) is to provide a standardized | |
72 7-bit representation of mixed Big5, GB, and ASCII text for convenient | |
73 e-mail transmission, news posting, etc. | |
74 The document of HZ+ 0.78 specification can be found at | |
75 ftp://ftp.math.psu.edu/pub/simpson/chinese/hzp/hzp.doc" | |
76 :type 'boolean | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
77 :group 'mime) |
31717 | 78 |
79 (defcustom rfc1843-newsgroups-regexp "chinese\\|hz" | |
80 "Regexp of newsgroups in which might be HZ encoded." | |
81 :type 'string | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
82 :group 'mime) |
31717 | 83 |
84 (defun rfc1843-decode-region (from to) | |
85 "Decode HZ in the region between FROM and TO." | |
86 (interactive "r") | |
87 (let (str firstc) | |
88 (save-excursion | |
89 (goto-char from) | |
90 (if (or rfc1843-decode-loosely | |
91 (re-search-forward (if rfc1843-decode-hzp | |
92 rfc1843-hzp-word-regexp-strictly | |
93 rfc1843-word-regexp-strictly) to t)) | |
94 (save-restriction | |
95 (narrow-to-region from to) | |
96 (goto-char (point-min)) | |
97 (while (re-search-forward (if rfc1843-decode-hzp | |
98 rfc1843-hzp-word-regexp | |
99 rfc1843-word-regexp) (point-max) t) | |
100 ;;; Text with extents may cause XEmacs crash | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38413
diff
changeset
|
101 (setq str (buffer-substring-no-properties |
31717 | 102 (match-beginning 1) |
103 (match-end 1))) | |
104 (setq firstc (aref str 0)) | |
105 (insert (mm-decode-coding-string | |
106 (rfc1843-decode | |
107 (prog1 | |
108 (substring str 1) | |
109 (delete-region (match-beginning 0) (match-end 0))) | |
110 firstc) | |
111 (if (eq firstc ?{) 'cn-gb-2312 'cn-big5)))) | |
112 (goto-char (point-min)) | |
113 (while (search-forward "~" (point-max) t) | |
114 (cond ((eq (char-after) ?\n) | |
115 (delete-char -1) | |
116 (delete-char 1)) | |
117 ((eq (char-after) ?~) | |
118 (delete-char 1))))))))) | |
119 | |
120 (defun rfc1843-decode-string (string) | |
121 "Decode HZ STRING and return the results." | |
122 (let ((m (mm-multibyte-p))) | |
123 (with-temp-buffer | |
124 (when m | |
125 (mm-enable-multibyte)) | |
126 (insert string) | |
127 (inline | |
128 (rfc1843-decode-region (point-min) (point-max))) | |
129 (buffer-string)))) | |
130 | |
131 (defun rfc1843-decode (word &optional firstc) | |
132 "Decode HZ WORD and return it." | |
133 (let ((i -1) (s (substring word 0)) v) | |
134 (if (or (not firstc) (eq firstc ?{)) | |
135 (while (< (incf i) (length s)) | |
136 (if (eq (setq v (aref s i)) ? ) nil | |
137 (aset s i (+ 128 v)))) | |
138 (while (< (incf i) (length s)) | |
139 (if (eq (setq v (aref s i)) ? ) nil | |
140 (setq v (+ (* 94 v) (aref s (1+ i)) -3135)) | |
141 (aset s i (+ (/ v 157) (if (eq firstc ?<) 201 161))) | |
142 (setq v (% v 157)) | |
143 (aset s (incf i) (+ v (if (< v 63) 64 98)))))) | |
144 s)) | |
145 | |
87229
e4e336c0677e
Add declare-function compatibility definition.
Glenn Morris <rgm@gnu.org>
parents:
78224
diff
changeset
|
146 (autoload 'mail-header-parse-content-type "mail-parse") |
e4e336c0677e
Add declare-function compatibility definition.
Glenn Morris <rgm@gnu.org>
parents:
78224
diff
changeset
|
147 (autoload 'message-narrow-to-head "message") |
e4e336c0677e
Add declare-function compatibility definition.
Glenn Morris <rgm@gnu.org>
parents:
78224
diff
changeset
|
148 (declare-function message-fetch-field "message" (header &optional not-all)) |
e4e336c0677e
Add declare-function compatibility definition.
Glenn Morris <rgm@gnu.org>
parents:
78224
diff
changeset
|
149 |
31717 | 150 (defun rfc1843-decode-article-body () |
151 "Decode HZ encoded text in the article body." | |
152 (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
153 (or gnus-newsgroup-name "")) | |
154 (save-excursion | |
155 (save-restriction | |
156 (message-narrow-to-head) | |
157 (let* ((inhibit-point-motion-hooks t) | |
158 (case-fold-search t) | |
159 (ct (message-fetch-field "Content-Type" t)) | |
68720
d9dde5b81e71
Revision: emacs@sv.gnu.org/emacs--devo--0--patch-57
Miles Bader <miles@gnu.org>
parents:
68633
diff
changeset
|
160 (ctl (and ct (mail-header-parse-content-type ct)))) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38413
diff
changeset
|
161 (if (and ctl (not (string-match "/" (car ctl)))) |
31717 | 162 (setq ctl nil)) |
163 (goto-char (point-max)) | |
164 (widen) | |
165 (forward-line 1) | |
166 (narrow-to-region (point) (point-max)) | |
167 (when (or (not ctl) | |
168 (equal (car ctl) "text/plain")) | |
169 (rfc1843-decode-region (point) (point-max)))))))) | |
170 | |
171 (defvar rfc1843-old-gnus-decode-header-function nil) | |
172 (defvar gnus-decode-header-methods) | |
173 (defvar gnus-decode-encoded-word-methods) | |
174 | |
175 (defun rfc1843-gnus-setup () | |
176 "Setup HZ decoding for Gnus." | |
177 (require 'gnus-art) | |
178 (require 'gnus-sum) | |
179 (add-hook 'gnus-article-decode-hook 'rfc1843-decode-article-body t) | |
180 (setq gnus-decode-encoded-word-function | |
181 'gnus-multi-decode-encoded-word-string | |
182 gnus-decode-header-function | |
183 'gnus-multi-decode-header | |
184 gnus-decode-encoded-word-methods | |
185 (nconc gnus-decode-encoded-word-methods | |
186 (list | |
187 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
188 'rfc1843-decode-string))) | |
189 gnus-decode-header-methods | |
190 (nconc gnus-decode-header-methods | |
191 (list | |
192 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
193 'rfc1843-decode-region))))) | |
194 | |
195 (provide 'rfc1843) | |
196 | |
52401 | 197 ;;; arch-tag: 5149c301-a6ca-4731-9c9d-ba616e2cb687 |
31717 | 198 ;;; rfc1843.el ends here |