Initial revision
[bpt/emacs.git] / lisp / gnus / gnus-mule.el
1 ;; gnus-mule.el -- Provide multilingual environment to GNUS
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
5
6 ;; Keywords: gnus, mule
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
23
24 ;;; Commentary:
25
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)
33 ;;
34 ;; Decoding of summary buffer is not yet implemented.
35
36 (require 'gnus)
37
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.")
42
43 ;;;###autoload
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)))
53 (if group
54 (setcdr group coding-system)
55 (setq gnus-newsgroup-coding-systems
56 (cons (cons name coding-system) gnus-newsgroup-coding-systems)))))
57
58 (defun gnus-mule-get-coding-system (group)
59 "Return the coding system for news group GROUP."
60 (let ((groups gnus-newsgroup-coding-systems)
61 (len -1)
62 coding-system)
63 ;; Find an entry which matches GROUP the best (i.e. longest).
64 (while groups
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)))
70 coding-system))
71
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)
79
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)
83 (if (< start end)
84 (save-excursion
85 (if encoding
86 (encode-coding-region start end coding-system)
87 (decode-coding-region start end coding-system)))))
88
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)
93 (if coding-system
94 (let ((win (get-buffer-window (current-buffer))))
95 (if win
96 ;; We should keep (point) and (window-start).
97 (save-window-excursion
98 (select-window win)
99 (if encoding
100 ;; Simple way to assure point is on valid character boundary.
101 (beginning-of-line))
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))
115 )))
116
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)))))
125
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))
131
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.
138 nil)
139
140 (defun gnus-mule-toggle-article-format ()
141 "Toggle decoding/encoding of the current article buffer."
142 (interactive)
143 (let ((buf (get-buffer gnus-article-buffer)))
144 (if (and gnus-mule-coding-system buf)
145 (save-excursion
146 (set-buffer buf)
147 (let ((modif (buffer-modified-p))
148 buffer-read-only)
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))))))
153
154 ;;;###autoload
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)
160 ;; Hook definition
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))
164
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))
176
177 (add-hook 'gnus-startup-hook 'gnus-mule-initialize)
178
179 ;; gnus-mule.el ends here