(require 'nntp)
(defvar gnus-newsgroup-coding-systems nil
"Assoc list of news groups vs corresponding coding systems.
Each element is has the form (PATTERN CODING-FOR-READ . CODING-FOR-POST),
where,
PATTERN is a regular expression matching news group names,
CODING-FOR-READ is a coding system of articles of the news groups, and
CODING-FOR-POST is a coding system to be encoded for posting articles
to the news groups.")
(defun gnus-mule-add-group (name coding-system)
"Specify that articles of news group NAME are encoded in CODING-SYSTEM.
All news groups deeper than NAME are also the target.
If CODING-SYSTEM is a cons, the car and cdr part are regarded as
coding-system for reading and writing respectively."
(if (not (consp coding-system))
(setq coding-system (cons coding-system coding-system)))
(setq name (concat "^" (regexp-quote name)))
(let ((group (assoc name gnus-newsgroup-coding-systems)))
(if group
(setcdr group coding-system)
(setq gnus-newsgroup-coding-systems
(cons (cons name coding-system) gnus-newsgroup-coding-systems)))))
(defun gnus-mule-get-coding-system (group)
"Return the coding system for news group GROUP."
(let ((groups gnus-newsgroup-coding-systems)
(len -1)
coding-system)
(while groups
(if (and (string-match (car (car groups)) group)
(> (match-end 0) len))
(setq len (match-end 0)
coding-system (cdr (car groups))))
(setq groups (cdr groups)))
coding-system))
(defvar gnus-mule-article-decoded nil)
(defvar gnus-mule-coding-system nil)
(defvar gnus-mule-subject nil)
(defvar gnus-mule-decoded-subject nil)
(defvar gnus-mule-original-subject nil)
(defun gnus-mule-code-convert1 (start end coding-system encoding)
(if (< start end)
(save-excursion
(if encoding
(encode-coding-region start end coding-system)
(decode-coding-region start end coding-system)))))
(defun gnus-mule-code-convert (coding-system encoding)
(if coding-system
(let ((win (get-buffer-window (current-buffer))))
(if win
(save-window-excursion
(select-window win)
(if encoding
(beginning-of-line))
(gnus-mule-code-convert1 (point-min) (window-start)
coding-system encoding)
(gnus-mule-code-convert1 (window-start) (point)
coding-system encoding)
(gnus-mule-code-convert1 (point) (point-max)
coding-system encoding)
(if (not (pos-visible-in-window-p))
(move-to-window-line -1)))
(gnus-mule-code-convert1 (point-min) (point-max)
coding-system encoding))
)))
(defun gnus-mule-select-coding-system ()
(if (gnus-buffer-live-p gnus-summary-buffer)
(save-excursion
(set-buffer gnus-summary-buffer)
(let ((coding-system
(gnus-mule-get-coding-system gnus-newsgroup-name)))
(setq gnus-mule-coding-system
(if (and coding-system (coding-system-p (car coding-system)))
(car coding-system)))))
'binary))
(defun gnus-mule-decode-article ()
(gnus-mule-code-convert gnus-mule-coding-system nil)
(setq gnus-mule-article-decoded t))
(defun gnus-mule-toggle-article-format ()
"Toggle decoding/encoding of the current article buffer."
(interactive)
(let ((buf (get-buffer gnus-article-buffer)))
(if (and gnus-mule-coding-system buf)
(save-excursion
(set-buffer buf)
(let ((modif (buffer-modified-p))
buffer-read-only)
(gnus-mule-code-convert gnus-mule-coding-system
gnus-mule-article-decoded)
(setq gnus-mule-article-decoded (not gnus-mule-article-decoded))
(set-buffer-modified-p modif))))))
(defun gnus-mule-message-send-news-function ()
(let ((groups (message-fetch-field "newsgroups"))
(idx 0)
coding-system coding-system-list group-list)
(if (not groups)
nil
(while (string-match "[^ ,]+" groups idx)
(setq idx (match-end 0))
(setq coding-system
(cdr (gnus-mule-get-coding-system
(substring groups (match-beginning 0) idx))))
(if (not (memq coding-system coding-system-list))
(setq coding-system-list (cons coding-system coding-system-list))))
(if (> (length coding-system-list) 1)
(setq coding-system (read-coding-system "Coding system: ")))
(if coding-system
(encode-coding-region (point-min) (point-max) coding-system)))))
(defun gnus-mule-message-send-mail-function ()
(let ((coding (if enable-multibyte-characters
(select-message-coding-system))))
(if coding
(encode-coding-region (point-min) (point-max) coding))))
(defun gnus-mule-initialize ()
"Do several settings for GNUS to enable automatic code conversion."
(define-key gnus-article-mode-map "z" 'gnus-mule-toggle-article-format)
(define-key gnus-summary-mode-map "z" 'gnus-mule-toggle-article-format)
(add-hook 'gnus-parse-headers-hook 'gnus-mule-select-coding-system)
(add-hook 'message-send-news-hook
'gnus-mule-message-send-news-function)
(add-hook 'message-send-mail-hook
'gnus-mule-message-send-mail-function)
(setq nnheader-file-coding-system 'binary
nnmail-file-coding-system 'binary)
)
(gnus-mule-add-group "" 'iso-latin-1)
(gnus-mule-add-group "fj" 'iso-2022-7bit)
(gnus-mule-add-group "tnn" 'iso-2022-7bit)
(gnus-mule-add-group "japan" 'iso-2022-7bit)
(gnus-mule-add-group "pin" 'iso-2022-7bit)
(gnus-mule-add-group "han" 'euc-kr)
(gnus-mule-add-group "alt.chinese.text" 'chinese-hz)
(gnus-mule-add-group "alt.hk" 'chinese-hz)
(gnus-mule-add-group "alt.chinese.text.big5" 'chinese-big5)
(gnus-mule-add-group "soc.culture.vietnamese" '(nil . vietnamese-viqr))
(gnus-mule-add-group "relcom" 'cyrillic-koi8)
(gnus-mule-add-group "tw." 'chinese-big5)
(provide 'gnus-mule)