1 ;; gnus-mule.el -- Provide multilingual environment to GNUS
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
6 ;; Keywords: gnus, mule
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This package enables GNUS to code convert automatically
27 ;; accoding to a coding system specified for each news group.
28 ;; Please put the following line in your .emacs:
29 ;; (add-hook 'gnus-startup-hook 'gnus-mule-initialize)
30 ;; If you want to specify some coding system for a specific news
31 ;; group, add the fllowing line in your .emacs:
32 ;; (gnus-mule-add-group "xxx.yyy.zzz" 'some-coding-system)
34 ;; Decoding of summary buffer is not yet implemented.
38 (defvar gnus-newsgroup-coding-systems nil
39 "Assoc list of news groups vs corresponding coding systems.
40 Each element is a list of news group name and cons of coding systems
41 for reading and posting.")
44 (defun gnus-mule-add-group (name coding-system
)
45 "Specify that articles of news group NAME are encoded in CODING-SYSTEM.
46 All news groups deeper than NAME are also the target.
47 If CODING-SYSTEM is a cons, the car and cdr part are regarded as
48 coding-system for reading and writing respectively."
49 (if (not (consp coding-system
))
50 (setq coding-system
(cons coding-system coding-system
)))
51 (setq name
(concat "^" (regexp-quote name
)))
52 (let ((group (assoc name gnus-newsgroup-coding-systems
)))
54 (setcdr group coding-system
)
55 (setq gnus-newsgroup-coding-systems
56 (cons (cons name coding-system
) gnus-newsgroup-coding-systems
)))))
58 (defun gnus-mule-get-coding-system (group)
59 "Return the coding system for news group GROUP."
60 (let ((groups gnus-newsgroup-coding-systems
)
63 ;; Find an entry which matches GROUP the best (i.e. longest).
65 (if (and (string-match (car (car groups
)) group
)
66 (> (match-end 0) len
))
67 (setq len
(match-end 0)
68 coding-system
(cdr (car groups
))))
69 (setq groups
(cdr groups
)))
72 ;; Flag to indicate if article buffer is already decoded or not.")
73 (defvar gnus-mule-article-decoded nil
)
74 ;; Codingsystem for reading articles of the current news group.
75 (defvar gnus-mule-coding-system nil
)
76 (defvar gnus-mule-subject nil
)
77 (defvar gnus-mule-decoded-subject nil
)
78 (defvar gnus-mule-original-subject nil
)
80 ;; Encode (if ENCODING is t) or decode (if ENCODING is nil) the
81 ;; region from START to END by CODING-SYSTEM.
82 (defun gnus-mule-code-convert1 (start end coding-system encoding
)
86 (encode-coding-region start end coding-system
)
87 (decode-coding-region start end coding-system
)))))
89 ;; Encode (if ENCODING is t) or decode (if ENCODING is nil) the
90 ;; current buffer by CODING-SYSTEM. Try not to move positions of
91 ;; (window-start) and (point).
92 (defun gnus-mule-code-convert (coding-system encoding
)
94 (let ((win (get-buffer-window (current-buffer))))
96 ;; We should keep (point) and (window-start).
97 (save-window-excursion
100 ;; Simple way to assure point is on valid character boundary.
102 (gnus-mule-code-convert1 (point-min) (window-start)
103 coding-system encoding
)
104 (gnus-mule-code-convert1 (window-start) (point)
105 coding-system encoding
)
106 (gnus-mule-code-convert1 (point) (point-max)
107 coding-system encoding
)
108 (if (not (pos-visible-in-window-p))
109 ;; point went out of window, move to the bottom of window.
110 (move-to-window-line -
1)))
111 ;; No window for the buffer, no need to worry about (point)
112 ;; and (windos-start).
113 (gnus-mule-code-convert1 (point-min) (point-max)
114 coding-system encoding
))
117 ;; Set `gnus-mule-coding-system' to the coding system articles of the
118 ;; current news group is encoded. This function is set in
119 ;; `gnus-select-group-hook'.
120 (defun gnus-mule-select-coding-system ()
121 (let ((coding-system (gnus-mule-get-coding-system gnus-newsgroup-name
)))
122 (setq gnus-mule-coding-system
123 (if (and coding-system
(coding-system-p (car coding-system
)))
124 (car coding-system
)))))
126 ;; Decode the current article. This function is set in
127 ;; `gnus-article-prepare-hook'.
128 (defun gnus-mule-decode-article ()
129 (gnus-mule-code-convert gnus-mule-coding-system nil
)
130 (setq gnus-mule-article-decoded t
))
132 ;; Decode the current summary buffer. This function is set in
133 ;; `gnus-summary-prepare-hook'.
134 (defun gnus-mule-decode-summary ()
135 ;; I have not yet implemented this function because I'm not yet
136 ;; familiar with the new Gnus codes, especialy how to extract only
137 ;; subjects from a summary buffer.
140 (defun gnus-mule-toggle-article-format ()
141 "Toggle decoding/encoding of the current article buffer."
143 (let ((buf (get-buffer gnus-article-buffer
)))
144 (if (and gnus-mule-coding-system buf
)
147 (let ((modif (buffer-modified-p))
149 (gnus-mule-code-convert gnus-mule-coding-system
150 gnus-mule-article-decoded
)
151 (setq gnus-mule-article-decoded
(not gnus-mule-article-decoded
))
152 (set-buffer-modified-p modif
))))))
155 (defun gnus-mule-initialize ()
156 "Do several settings for GNUS to enable automatic code conversion."
157 ;; Convenient key definitions
158 (define-key gnus-article-mode-map
"z" 'gnus-mule-toggle-article-format
)
159 (define-key gnus-summary-mode-map
"z" 'gnus-mule-toggle-article-format
)
161 (add-hook 'gnus-select-group-hook
'gnus-mule-select-coding-system
)
162 (add-hook 'gnus-summary-prepare-hook
'gnus-mule-decode-summary
)
163 (add-hook 'gnus-article-prepare-hook
'gnus-mule-decode-article
))
165 (gnus-mule-add-group "" 'coding-system-iso-2022-7
) ;; default coding system
166 (gnus-mule-add-group "alt" 'no-conversion
)
167 (gnus-mule-add-group "comp" 'no-conversion
)
168 (gnus-mule-add-group "gnu" 'no-conversion
)
169 (gnus-mule-add-group "rec" 'no-conversion
)
170 (gnus-mule-add-group "sci" 'no-conversion
)
171 (gnus-mule-add-group "soc" 'no-conversion
)
172 (gnus-mule-add-group "alt.chinese.text" 'coding-system-hz
)
173 (gnus-mule-add-group "alt.hk" 'coding-system-hz
)
174 (gnus-mule-add-group "alt.chinese.text.big5" 'coding-system-big5
)
175 (gnus-mule-add-group "soc.culture.vietnamese" '(nil . coding-system-viqr
))
177 (add-hook 'gnus-startup-hook
'gnus-mule-initialize
)
179 ;; gnus-mule.el ends here