1 ;;; wl-message.el -- Message displaying modules for Wanderlust.
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7 ;; Time-stamp: <2000-03-17 10:19:41 teranisi>
9 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
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
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
34 (require 'wl-highlight)
41 (require 'mmelmo-imap4))
46 (unless (boundp symbol)
47 (set (make-local-variable symbol) nil))))
48 '(mime-view-ignored-field-list mmelmo-imap4-skipped-parts))
49 (defun-maybe event-window (a))
50 (defun-maybe posn-window (a))
51 (defun-maybe event-start (a))
52 (defun-maybe mime-open-entity (a b)))
54 (defvar wl-original-buf-name "*Message*")
55 (defvar wl-message-buf-name "Message")
56 (defvar wl-message-buffer-cur-summary-buffer nil)
57 (defvar wl-message-buffer-cur-folder nil)
58 (defvar wl-message-buffer-cur-number nil)
60 (defvar wl-original-buffer-cur-folder nil)
61 (defvar wl-original-buffer-cur-number nil)
62 (defvar wl-original-buffer-cur-msgdb nil)
65 (function make-variable-buffer-local)
66 (list 'wl-message-buffer-cur-folder
67 'wl-message-buffer-cur-number))
71 (defvar wl-fixed-window-configuration nil)
73 (defun wl-message-buffer-window ()
74 (let* ((mes-buf (concat "^" (default-value 'wl-message-buf-name)))
75 (start-win (selected-window))
79 (setq cur-win (next-window cur-win))
80 (if (string-match mes-buf (buffer-name (window-buffer cur-win)))
81 (throw 'found cur-win))
82 (not (eq cur-win start-win)))))))
84 (defun wl-select-buffer (buffer)
85 (let ((gbw (or (get-buffer-window buffer)
86 (wl-message-buffer-window)))
87 (sum (car wl-message-window-size))
88 (mes (cdr wl-message-window-size))
91 (not (eq (save-excursion (set-buffer (window-buffer gbw))
92 wl-message-buffer-cur-summary-buffer)
95 (run-hooks 'wl-message-window-deleted-hook)
100 ; wl-stay-folder-window)
101 ; (delete-other-windows))
102 (when wl-fixed-window-configuration
103 (delete-other-windows)
104 (and wl-stay-folder-window
105 (wl-summary-toggle-disp-folder)))
106 (setq whi (1- (window-height)))
109 (let ((total (+ sum mes)))
110 (setq sum (max window-min-height (/ (* whi sum) total)))
111 (setq mes (max window-min-height (/ (* whi mes) total))))
112 (if (< whi (+ sum mes))
113 (enlarge-window (- (+ sum mes) whi)))))
114 (split-window (get-buffer-window (current-buffer)) sum)
116 (switch-to-buffer buffer)))
119 ;; called by wl-summary-mode buffer
121 (defvar wl-message-func-called-hook nil)
123 (defun wl-message-scroll-down (amount)
124 (let ((view-message-buffer (get-buffer-create wl-message-buf-name))
125 (cur-buf (current-buffer)))
126 (wl-select-buffer view-message-buffer)
130 (select-window (get-buffer-window cur-buf))))
132 (defun wl-message-scroll-up (amount)
133 (let ((view-message-buffer (get-buffer-create wl-message-buf-name))
134 (cur-buf (current-buffer)))
135 (wl-select-buffer view-message-buffer)
140 (if (pos-visible-in-window-p (point))
141 (wl-message-narrow-to-page 1)))) ;Go to next page.
145 (select-window (get-buffer-window cur-buf))))
147 (defun wl-message-follow-current-entity (buffer)
148 "Follow to current message"
149 (wl-draft-reply (wl-message-get-original-buffer)
150 'to-all wl-message-buffer-cur-summary-buffer)
151 (let ((mail-reply-buffer buffer))
152 (wl-draft-yank-from-mail-reply-buffer nil)))
154 (defun wl-message-original-mode ()
155 (setq major-mode 'wl-message-original-mode)
156 (setq mode-name "Original")
157 (setq buffer-read-only t)
158 (if (fboundp 'set-buffer-file-coding-system)
159 (set-buffer-file-coding-system wl-cs-noconv)))
161 (defun wl-message-mode ()
163 (setq major-mode 'wl-message-mode)
164 (setq buffer-read-only t)
165 (setq mode-name "Message"))
167 (defun wl-message-get-buffer-create ()
168 (let ((buf-name wl-message-buf-name))
169 (or (get-buffer buf-name)
171 (set-buffer (get-buffer-create buf-name))
173 (run-hooks 'wl-message-buffer-created-hook)
174 (get-buffer buf-name)))))
176 (defun wl-message-original-get-buffer-create ()
177 (or (get-buffer wl-original-buf-name)
179 (set-buffer (get-buffer-create wl-original-buf-name))
180 (wl-message-original-mode)
181 (get-buffer wl-original-buf-name))))
183 (defun wl-message-exit ()
185 (let (summary-buf summary-win)
186 (if (setq summary-buf wl-message-buffer-cur-summary-buffer)
187 (if (setq summary-win (get-buffer-window summary-buf))
188 (select-window summary-win)
189 (switch-to-buffer summary-buf)
190 (wl-select-buffer wl-message-buf-name)
191 (select-window (get-buffer-window summary-buf))))
192 (run-hooks 'wl-message-exit-hook)))
194 (defun wl-message-decode (outbuf inbuf flag)
196 ((eq flag 'all-header)
199 (let ((buffer-read-only nil))
200 (decode-mime-charset-region (point-min)
202 (goto-char (point-min))
203 (re-search-forward "^$" nil t)
206 (wl-message-decode-with-all-header outbuf inbuf))
210 (let ((buffer-read-only nil))
213 (elmo-set-buffer-multibyte nil))
214 (copy-to-buffer outbuf (point-min) (point-max))
216 (local-set-key "q" 'wl-message-exit)
217 (local-set-key "p" 'wl-message-exit)
218 (local-set-key "n" 'wl-message-exit)
219 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
220 ;;(decode-mime-charset-region (point-min) (point-max) wl-mime-charset)
221 ;; we can call decode-coding-region() directly, because multibyte flag is t.
222 (decode-coding-region (point-min) (point-max) wl-cs-autoconv)
223 (wl-highlight-message (point-min)
225 (goto-char (point-min))
226 (re-search-forward "^$" nil t)) nil))))
230 (let ((buffer-read-only nil))
231 (decode-mime-charset-region (point-min)
233 (goto-char (point-min))
234 (re-search-forward "^$" nil t)
237 (wl-message-decode-mode outbuf inbuf))))
239 (defun wl-message-prev-page (&optional lines)
240 "Scroll down this message. Returns non-nil if top of message"
242 (let ((cur-buf (current-buffer))
243 (view-message-buffer (get-buffer-create wl-message-buf-name))
245 (wl-select-buffer view-message-buffer)
246 (move-to-window-line 0)
247 (if (and wl-break-pages
249 (not (save-restriction (widen) (bobp))))
251 (wl-message-narrow-to-page -1)
252 (goto-char (point-max))
257 (select-window (get-buffer-window cur-buf))
260 (static-if (fboundp 'luna-make-entity)
261 (defsubst wl-message-make-mime-entity (backend number backend folder msgdb)
262 (luna-make-entity (mm-expand-class-name 'elmo)
263 :location (get-buffer-create
264 (concat mmelmo-entity-buffer-name "0"))
265 :imap (eq backend 'elmo-imap4)
268 :msgdb msgdb :size 0))
269 (defsubst wl-message-make-mime-entity (backend number backend folder msgdb)
270 (mime-open-entity backend (list folder number msgdb nil))))
272 (defun wl-message-next-page (&optional lines)
273 "Scroll up this message. Returns non-nil if bottom of message"
275 (let ((cur-buf (current-buffer))
276 (view-message-buffer (get-buffer-create wl-message-buf-name))
278 (wl-select-buffer view-message-buffer)
279 (move-to-window-line -1)
282 (and (pos-visible-in-window-p)
284 (if (or (null wl-break-pages)
287 (widen) (forward-line) (eobp))))
289 (wl-message-narrow-to-page 1)
294 (goto-char (point-max))))
296 (select-window (get-buffer-window cur-buf))
300 (defun wl-message-narrow-to-page (&optional arg)
302 (setq arg (if arg (prefix-numeric-value arg) 0))
305 (forward-page -1) ; Beginning of current page.
307 (goto-char (point-min))))
308 (forward-char 1) ; for compatibility with emacs-19.28 and emacs-19.29
311 ((> arg 0) (forward-page arg))
312 ((< arg 0) (forward-page (1- arg))))
315 (narrow-to-region (point)
318 (if (and (eolp) (not (bobp)))
322 (defun wl-message-toggle-disp-summary ()
324 (let ((summary-buf (get-buffer wl-message-buffer-cur-summary-buffer))
327 (buffer-live-p summary-buf))
328 (if (setq summary-win (get-buffer-window summary-buf))
329 (delete-window summary-win)
330 (switch-to-buffer summary-buf)
331 (wl-select-buffer wl-message-buf-name))
332 (wl-summary-goto-folder-subr wl-message-buffer-cur-folder 'no-sync
335 (let ((sum-buf (current-buffer)))
336 (wl-select-buffer wl-message-buf-name)
337 (setq wl-message-buffer-cur-summary-buffer sum-buf)))))
339 (defun wl-message-normal-get-original-buffer ()
341 (if (setq ret-val (get-buffer wl-original-buf-name))
343 (set-buffer (setq ret-val
344 (get-buffer-create wl-original-buf-name)))
345 (wl-message-original-mode)
350 (defalias 'wl-message-get-original-buffer
351 'mmelmo-get-original-buffer)
352 (defalias 'wl-message-get-original-buffer
353 'wl-message-normal-get-original-buffer))
355 (defvar wl-message-redisplay-func 'wl-normal-message-redisplay)
356 (defvar wl-message-cache-used nil) ;whether cache is used or not.
358 (defun wl-message-redisplay (folder number flag msgdb &optional force-reload)
359 (let ((default-mime-charset wl-mime-charset)
360 (buffer-read-only nil))
361 (setq wl-message-cache-used nil)
362 (if wl-message-redisplay-func
363 (funcall wl-message-redisplay-func
364 folder number flag msgdb force-reload))))
366 ;; nil means don't fetch all.
367 (defun wl-message-decide-backend (folder number message-id size)
368 (let ((dont-do-that (and
369 (not (setq wl-message-cache-used
371 (elmo-buffer-cache-hit
372 (list folder number message-id))
373 (elmo-cache-exists-p message-id
376 (not (elmo-local-file-p folder number))
377 wl-fetch-confirm-threshold
378 (>= size wl-fetch-confirm-threshold)
380 (format "Fetch entire message? (%dbytes)"
383 (cond ((and dont-do-that
384 (eq (elmo-folder-number-get-type folder number) 'imap4)
385 (not (and (elmo-use-cache-p folder number)
386 (elmo-cache-exists-p message-id folder number))))
388 (t (if (not dont-do-that) 'elmo)))))
390 (defmacro wl-message-original-buffer-folder ()
391 wl-original-buffer-cur-folder)
393 (defmacro wl-message-original-buffer-number ()
394 wl-original-buffer-cur-number)
396 (defun wl-message-set-original-buffer-information (folder number)
397 (when (or (not (string= folder (or wl-original-buffer-cur-folder "")))
398 (not (eq number (or wl-original-buffer-cur-number 0))))
399 (setq wl-original-buffer-cur-folder folder)
400 (setq wl-original-buffer-cur-number number)))
402 ;; Works on FLIM-1.9.0/SEMI-1.8.2 or later (maybe).
403 (defun wl-mmelmo-message-redisplay (folder number flag msgdb
404 &optional force-reload)
405 (let* ((cur-buf (current-buffer))
406 (view-message-buffer (wl-message-get-buffer-create))
407 (message-id (cdr (assq number
408 (elmo-msgdb-get-number-alist msgdb))))
409 (size (elmo-msgdb-overview-entity-get-size
411 (elmo-msgdb-get-overview msgdb))))
412 (backend (wl-message-decide-backend folder number message-id size))
413 cur-entity ret-val header-end real-fld-num summary-win)
415 (wl-select-buffer view-message-buffer)
416 (set-buffer view-message-buffer)
419 (setq wl-message-buffer-cur-summary-buffer cur-buf)
420 (setq wl-message-buffer-cur-folder folder)
421 (setq wl-message-buffer-cur-number number)
422 (setq buffer-read-only nil)
425 (let (mime-display-header-hook ;; bind to nil...
426 (mime-view-ignored-field-list
427 (if (eq flag 'all-header)
429 mime-view-ignored-field-list))
430 (mmelmo-force-reload force-reload)
431 (mmelmo-imap4-threshold wl-fetch-confirm-threshold))
432 (setq real-fld-num (elmo-get-real-folder-number
435 (wl-message-make-mime-entity
437 (if (eq backend 'elmo-imap4)
441 (if (eq backend 'elmo-imap4)
445 (setq mmelmo-imap4-skipped-parts nil)
446 ;;; mime-display-message sets buffer-read-only variable as t.
447 ;;; which makes buffer read-only status confused...
448 (wl-mime-display-message cur-entity view-message-buffer
449 nil nil 'mmelmo-original-mode)
450 (if mmelmo-imap4-skipped-parts
452 (message "Skipped fetching of %s."
456 mmelmo-imap4-skipped-parts ","))))
457 (if (and (eq backend 'elmo-imap4)
458 (null mmelmo-imap4-skipped-parts))
459 (message "No required part was skipped."))
460 (setq ret-val (not (eq backend 'elmo-imap4))))
461 (message "Skipped fetching.")
463 (setq buffer-read-only nil)
464 (wl-message-set-original-buffer-information folder number)
465 (wl-message-overload-functions)
467 (when wl-highlight-body-too
470 (wl-message-narrow-to-page)
471 (error nil));; ignore errors.
472 (setq mode-line-buffer-identification
473 (format "Wanderlust: << %s / %s >>"
474 (if (memq 'modeline wl-use-folder-petname)
475 (wl-folder-get-petname folder)
477 (goto-char (point-min))
480 (run-hooks 'wl-message-redisplay-hook))
481 ;; go back to summary mode
482 (set-buffer-modified-p nil)
483 (setq buffer-read-only t)
485 (setq summary-win (get-buffer-window cur-buf))
486 (if (window-live-p summary-win)
487 (select-window summary-win))))
491 (defun wl-normal-message-redisplay (folder number flag msgdb
492 &optional force-reload)
494 (let* ((cur-buf (current-buffer))
495 (original-message-buffer (wl-message-get-original-buffer))
496 (view-message-buffer (wl-message-get-buffer-create))
497 (message-id (cdr (assq number
498 (elmo-msgdb-get-number-alist msgdb))))
499 (size (elmo-msgdb-overview-entity-get-size
501 (elmo-msgdb-get-overview msgdb))))
502 header-end ret-val summary-win
504 (wl-select-buffer view-message-buffer)
507 (setq wl-message-buffer-cur-summary-buffer cur-buf)
508 (setq wl-message-buffer-cur-folder folder)
509 (setq wl-message-buffer-cur-number number)
510 (setq buffer-read-only nil)
512 (if (or (eq (elmo-folder-number-get-type folder number) 'localdir)
513 (not (and (integerp size)
514 wl-fetch-confirm-threshold
515 (>= size wl-fetch-confirm-threshold)
516 (not (elmo-cache-exists-p message-id
519 (format "Fetch entire message? (%dbytes)"
523 (set-buffer original-message-buffer)
524 (let ((buffer-read-only nil))
525 (elmo-read-msg-with-buffer-cache
526 folder number original-message-buffer msgdb force-reload)))
527 ;; decode MIME message.
530 original-message-buffer flag)
533 (set-buffer view-message-buffer)
535 (setq buffer-read-only nil)
536 (wl-message-set-original-buffer-information folder number)
537 (wl-message-overload-functions)
539 (and wl-highlight-body-too (wl-highlight-body))
541 (wl-message-narrow-to-page)
542 (error nil)) ; ignore errors.
543 (setq mode-line-buffer-identification
544 (format "Wanderlust: << %s / %s >>"
545 (if (memq 'modeline wl-use-folder-petname)
546 (wl-folder-get-petname folder)
549 (goto-char (point-min))
551 (run-hooks 'wl-message-redisplay-hook)
552 ;; go back to summary mode
553 (set-buffer-modified-p nil)
554 (setq buffer-read-only t)
556 (setq summary-win (get-buffer-window cur-buf))
557 (if (window-live-p summary-win)
558 (select-window summary-win)))
562 (defun wl-message-refer-article-or-url (e)
563 "Read article specified by message-id around point. If failed,
564 attempt to execute button-dispatcher."
566 (let ((window (get-buffer-window (current-buffer)))
567 mouse-window point beg end msg-id)
571 (setq mouse-window (get-buffer-window (current-buffer)))
573 (setq beg (save-excursion (beginning-of-line) (point)))
574 (setq end (save-excursion (end-of-line) (point)))
575 (search-forward ">" end t) ;Move point to end of "<....>".
576 (if (and (re-search-backward "\\(<[^<> \t\n]+@[^<> \t\n]+>\\)"
578 (not (string-match "mailto:"
579 (setq msg-id (wl-match-buffer 1)))))
582 (switch-to-buffer-other-window
583 wl-message-buffer-cur-summary-buffer)
584 (if (wl-summary-jump-to-msg-by-message-id msg-id)
585 (wl-summary-redisplay)))
586 (wl-message-button-dispatcher e)))
587 (if (eq mouse-window (get-buffer-window (current-buffer)))
588 (select-window window)))))
590 (defun wl-message-uu-substring (buf outbuf &optional first last)
593 (search-forward "\n\n")
595 ep filename case-fold-search)
598 (re-search-forward "^begin[ \t]+[0-9]+[ \t]+\\([^ ].*\\)" nil t)
599 (setq filename (buffer-substring (match-beginning 1)(match-end 1))))
600 (re-search-forward "^M.*$" nil t)) ; uuencoded string
603 (goto-char (point-max))
605 (re-search-backward "^end" sp t)
606 (re-search-backward "^M.*$" sp t)) ; uuencoded string
610 (goto-char (point-max))
611 (insert-buffer-substring buf sp ep)
615 ;;; wl-message.el ends here