New feature: toggle visibility of mime buttons.
[more-wl.git] / wl / wl-news.el.in
blobd3803cb00dfa8dce611fac5fe859a2259540268b
1 ;;; wl-news.el --- Create notification from NEWS(.ja) for Wanderlust.
3 ;; Copyright (C) 2002 Yoichi NAKAYAMA <yoichi@eken.phys.nagoya-u.ac.jp>
4 ;; Copyright (C) 2002 Kenichi OKADA <okada@opaopa.org>
6 ;; Author: Yoichi NAKAYAMA <yoichi@eken.phys.nagoya-u.ac.jp>
7 ;; Kenichi OKADA <okada@opaopa.org>
8 ;; Keywords: mail, net news
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
31 ;;; Code:
34 (require 'elmo)
35 (require 'wl-vars)
36 (require 'wl-util)
37 (require 'wl-address)
38 (require 'wl-folder)
40 (defvar wl-news-version-file-name "previous-version")
41 (defvar wl-news-default-previous-version '(2 0 0))
43 (defvar wl-news-lang
44 (if (and (boundp 'current-language-environment)
45 (string-equal "Japanese"
46 (symbol-value 'current-language-environment)))
47 '("ja" "en") '("en" "ja"))
48 "The list of languages to show NEWS. (order sensitive)")
50 (defun wl-news-check ()
51 (let* ((updated (not (wl-news-already-current-p))))
52 (if updated
53 (if (and wl-news-lang
54 (wl-news-check-news
55 (cdr (wl-news-previous-version-load))
56 wl-news-lang)
57 (not (memq 'wl-news wl-hook)))
58 (add-hook 'wl-hook 'wl-news))
59 ;; update wl-news-version-file
60 (wl-news-previous-version-save
61 (product-version (product-find 'wl-version))
62 (cdr (wl-news-previous-version-load))))
63 updated))
65 ;;; -*- news-list -*-
68 ;;; -*- news-list-end -*-
70 (defun wl-news-previous-version-load ()
71 (with-temp-buffer
72 (let ((filename (expand-file-name
73 wl-news-version-file-name
74 elmo-msgdb-directory))
75 insert-file-contents-pre-hook
76 insert-file-contents-post-hook
77 ret-val)
78 (if (not (file-readable-p filename))
79 (cons wl-news-default-previous-version
80 wl-news-default-previous-version)
81 (insert-file-contents filename)
82 (condition-case nil
83 (read (current-buffer))
84 (error nil nil))))))
86 (defun wl-news-previous-version-save (current-version previous-version)
87 (with-temp-buffer
88 (let ((filename (expand-file-name
89 wl-news-version-file-name
90 elmo-msgdb-directory))
91 print-length print-level)
92 (prin1 (cons current-version previous-version) (current-buffer))
93 (princ "\n" (current-buffer))
94 (if (file-writable-p filename)
95 (write-region (point-min) (point-max)
96 filename nil 'no-msg)
97 (message "%s is not writable." filename)))))
99 (defun wl-news-append-news (lang previous-version &optional no-mime-tag)
100 (require 'wl-mime)
101 (let ((news-list (cdr (assoc lang wl-news-news-alist)))
102 ret)
103 (when news-list
104 (if no-mime-tag
105 (insert "--------------\n")
106 (mime-edit-insert-tag "text" "plain" "" ""))
107 (while (< 0
108 (product-version-compare
109 (car (car news-list))
110 previous-version))
111 (setq ret t)
112 (insert (cdr (car news-list)) "\n\n")
113 (setq news-list (cdr news-list))))
114 ret))
116 (defun wl-news-check-news (version news-lang)
117 (let ((lang news-lang)
118 news-list ret)
119 (while (car lang)
120 (setq news-list (cdr (assoc (car lang) wl-news-news-alist)))
121 (while (< 0
122 (product-version-compare
123 (car (car news-list)) version))
124 (setq ret t)
125 (setq news-list (cdr news-list)))
126 (setq lang (cdr lang)))
127 ret))
129 (defun wl-news-already-current-p ()
130 (>= 0 (product-version-compare
131 (product-version (product-find 'wl-version))
132 (car (wl-news-previous-version-load)))))
134 (defun wl-news-send-news (version news-lang folder)
135 (require 'wl-draft)
136 (let ((lang (if (listp wl-news-lang)
137 wl-news-lang
138 (list wl-news-lang)))
139 send-buffer
140 wl-fcc wl-bcc ret)
141 (save-window-excursion
142 (set-buffer
143 (setq send-buffer (wl-draft-create-buffer)))
144 (wl-draft-create-contents
145 (list (cons 'From "WL Release 'Bot <wl@lists.airs.net>")
146 (cons 'To (wl-draft-eword-encode-address-list wl-from))
147 (cons 'Subject "Wanderlust NEWS")
148 (cons 'Date (wl-make-date-string))
149 (cons 'User-Agent wl-generate-mailer-string-function)))
150 (wl-draft-insert-mail-header-separator)
151 (wl-draft-prepare-edit)
152 (goto-char (point-max))
153 (insert "\nThis message is automatically generated by Wanderlust.\n\n")
154 ;; insert news
155 (while (car lang)
156 (wl-news-append-news (car lang) version)
157 (setq lang (cdr lang)))
158 ;; encode
159 (let ((mime-header-encode-method-alist
160 '((eword-encode-unstructured-field-body))))
161 (mime-edit-translate-buffer))
162 (wl-draft-get-header-delimiter t)
163 (setq ret
164 (and (elmo-folder-writable-p
165 (wl-folder-get-elmo-folder folder))
166 (elmo-folder-append-buffer
167 (wl-folder-get-elmo-folder folder))))
168 (wl-draft-hide send-buffer)
169 (wl-draft-delete send-buffer))
170 ret))
172 ;;; wl-news-mode
174 (defvar wl-news-buf-name "NEWS")
175 (defvar wl-news-mode-map nil)
176 (defvar wl-news-winconf nil)
177 (defvar wl-news-buffer-oldest-version nil)
178 (make-variable-buffer-local 'wl-news-buffer-oldest-version)
180 (unless wl-news-mode-map
181 (setq wl-news-mode-map (make-sparse-keymap))
182 (define-key wl-news-mode-map "q" 'wl-news-exit)
183 (define-key wl-news-mode-map "Q" 'wl-news-force-exit)
184 (define-key wl-news-mode-map "\C-xk" 'wl-news-exit)
185 (define-key wl-news-mode-map "a" 'wl-news-show-all)
186 (define-key wl-news-mode-map "m" 'wl-news-append-to-folder)
187 (define-key wl-news-mode-map "\C-m" 'wl-news-next-line)
188 (define-key wl-news-mode-map " " 'wl-news-next-page)
189 (define-key wl-news-mode-map "\177" 'wl-news-previous-page)
190 ;; re-bind commands of outline-mode
191 (define-key wl-news-mode-map "n" 'outline-next-visible-heading)
192 (define-key wl-news-mode-map "p" 'outline-previous-visible-heading)
193 (define-key wl-news-mode-map "u" 'outline-up-heading)
194 (define-key wl-news-mode-map "N" 'outline-forward-same-level)
195 (define-key wl-news-mode-map "P" 'outline-backward-same-level))
197 (require 'derived)
198 (define-derived-mode wl-news-mode outline-mode "NEWS"
199 "Mode for Wanderlust NEWS(.ja)."
200 (setq buffer-read-only t))
202 (defun wl-news (&optional arg)
203 (interactive "P")
204 (remove-hook 'wl-hook 'wl-news)
205 (let* ((previous-version (if arg wl-news-default-previous-version
206 (cdr (wl-news-previous-version-load))))
207 (lang wl-news-lang)
208 window-lines lines)
209 (if (or (get-buffer wl-news-buf-name)
210 (if (wl-news-check-news previous-version wl-news-lang)
211 (progn
212 (setq wl-news-winconf (current-window-configuration))
213 (set-buffer (get-buffer-create wl-news-buf-name))
214 (wl-news-mode)
215 (setq wl-news-buffer-oldest-version previous-version)
216 (buffer-disable-undo (current-buffer))
217 ;; insert news
218 (let ((buffer-read-only nil))
219 (insert "--- Wanderlust NEWS --- press 'a' to show all NEWS\n")
220 (insert " press 'm' to mail this NEWS to your folder\n")
221 (insert " press 'q' to quit\n")
222 (insert " press 'Q' to force quit\n\n")
223 (while (car lang)
224 (wl-news-append-news
225 (car lang) previous-version t)
226 (setq lang (cdr lang))))
228 (message "No NEWS.")
229 nil))
230 (progn
231 (switch-to-buffer wl-news-buf-name)
232 (delete-other-windows)
233 (goto-char (point-min))))))
235 (defun wl-news-next-line ()
236 (interactive)
237 (scroll-up 1))
239 (defun wl-news-next-page ()
240 (interactive)
241 (scroll-up))
243 (defun wl-news-previous-page ()
244 (interactive)
245 (scroll-down))
247 (defun wl-news-show-all ()
248 (interactive)
249 (when (eq major-mode 'wl-news-mode)
250 (kill-buffer (current-buffer))
251 (wl-news t)))
253 (defun wl-news-exit ()
254 (interactive)
255 (let* ((oldest-version (cdr (wl-news-previous-version-load)))
256 (current-version (product-version (product-find 'wl-version)))
257 (new-old-version current-version)
258 (buf (get-buffer wl-news-buf-name)))
259 (when buf
260 (if (wl-news-check-news oldest-version wl-news-lang)
261 (if (y-or-n-p "Do you want to see this message again? ")
262 (progn
263 (message "Please M-x wl-news if you want to see it.")
264 (setq new-old-version oldest-version))))
265 (wl-news-previous-version-save
266 current-version new-old-version)
267 (kill-buffer (current-buffer))
268 (if wl-news-winconf
269 (set-window-configuration wl-news-winconf))
270 (kill-buffer buf)
271 (if wl-news-winconf
272 (set-window-configuration wl-news-winconf)))))
274 (defun wl-news-append-to-folder ()
275 (interactive)
276 (let* ((current-version (product-version (product-find 'wl-version)))
277 (new-old-version current-version)
278 (folder wl-default-folder))
279 (if (or (and (elmo-folder-writable-p
280 (wl-folder-get-elmo-folder folder))
281 (y-or-n-p (format
282 "Do you want to append this message to %s ? "
283 wl-default-folder)))
284 (setq folder
285 (wl-summary-read-folder wl-default-folder "to append ")))
286 (or (wl-news-send-news wl-news-buffer-oldest-version wl-news-lang folder)
287 (error "Cannot append NEWS mail to %s" folder)))))
289 (defun wl-news-force-exit ()
290 (interactive)
291 (let ((buf))
292 (when (setq buf (get-buffer wl-news-buf-name))
293 (wl-news-previous-version-save
294 (product-version (product-find 'wl-version))
295 (cdr (wl-news-previous-version-load)))
296 (kill-buffer buf)
297 (if wl-news-winconf
298 (set-window-configuration wl-news-winconf)))))
301 (require 'product)
302 (product-provide (provide 'wl-news) (require 'wl-version))
305 ;; Local Variables:
306 ;; no-byte-compile: t
307 ;; End:
308 ;;; wl-news.el ends here