1 ;;; wl-message.el -- Message displaying modules for Wanderlust.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
10 ;; This program 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 ;; This program 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
33 (require 'wl-highlight)
40 (defalias-maybe 'event-window 'ignore)
41 (defalias-maybe 'posn-window 'ignore)
42 (defalias-maybe 'event-start 'ignore)
43 (defalias-maybe 'mime-open-entity 'ignore))
45 (defconst wl-message-buffer-prefetch-idle-time
46 (if (featurep 'lisp-float-type) (/ (float 1) (float 5)) 1))
47 (defvar wl-message-buffer-prefetch-get-next-function
48 'wl-summary-default-get-next-msg)
50 (defvar wl-message-buffer-prefetch-folder-type-list t)
52 (defvar wl-message-buffer-prefetch-debug nil)
54 (defvar wl-message-buffer nil) ; message buffer.
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)
59 (defvar wl-message-buffer-cur-flag nil)
60 (defvar wl-message-buffer-cur-summary-buffer nil)
61 (defvar wl-message-buffer-original-buffer nil) ; original buffer.
62 (defvar wl-message-buffer-all-header-flag nil)
64 (make-variable-buffer-local 'wl-message-buffer-cur-folder)
65 (make-variable-buffer-local 'wl-message-buffer-cur-number)
66 (make-variable-buffer-local 'wl-message-buffer-cur-flag)
67 (make-variable-buffer-local 'wl-message-buffer-cur-summary-buffer)
68 (make-variable-buffer-local 'wl-message-buffer-original-buffer)
69 (make-variable-buffer-local 'wl-message-buffer-all-header-flag)
71 (defvar wl-fixed-window-configuration nil)
73 (defvar wl-message-buffer-cache-size 10) ; At least 1.
75 ;;; Message buffer cache.
77 (defvar wl-message-buffer-cache nil
78 "Message cache. (old ... new) order alist.
79 With association ((\"folder\" message \"message-id\") . cache-buffer).")
81 (defmacro wl-message-buffer-cache-buffer-get (entry)
84 (defmacro wl-message-buffer-cache-folder-get (entry)
85 (` (car (car (, entry)))))
87 (defmacro wl-message-buffer-cache-message-get (entry)
88 (` (cdr (car (, entry)))))
90 (defmacro wl-message-buffer-cache-entry-make (key buf)
91 (` (cons (, key) (, buf))))
93 (defmacro wl-message-buffer-cache-hit (key)
94 "Return value assosiated with key."
95 (` (wl-message-buffer-cache-buffer-get
96 (assoc (, key) wl-message-buffer-cache))))
98 (defun wl-message-buffer-cache-sort (entry)
99 "Move ENTRY to the top of `wl-message-buffer-cache'."
100 (setq wl-message-buffer-cache
101 (cons entry (delete entry wl-message-buffer-cache))))
102 ; (let* ((pointer (cons nil wl-message-buffer-cache))
104 ; (while (cdr pointer)
105 ; (if (equal (car (cdr pointer)) entry)
106 ; (setcdr pointer (cdr (cdr pointer)))
107 ; (setq pointer (cdr pointer))))
108 ; (setcdr pointer (list entry))
109 ; (setq wl-message-buffer-cache (cdr top))))
111 (defconst wl-message-buffer-cache-name " *WL:Message*")
112 (defconst wl-original-message-buffer-name " *Original*")
114 (defun wl-original-message-mode ()
115 "A major mode for original message buffer."
116 (setq major-mode 'wl-original-message-mode)
117 (setq buffer-read-only t)
118 (elmo-set-buffer-multibyte nil)
119 (setq mode-name "Wanderlust original message"))
121 (defun wl-original-message-buffer-get (name)
122 "Get original message buffer for NAME.
123 If original message buffer already exists, it is re-used."
124 (let* ((name (concat wl-original-message-buffer-name name))
125 (buffer (get-buffer name)))
126 (unless (and buffer (buffer-live-p buffer))
127 (with-current-buffer (setq buffer (get-buffer-create name))
128 (wl-original-message-mode)))
131 (defun wl-message-buffer-create ()
132 "Create a new message buffer."
133 (let* ((buffer (generate-new-buffer wl-message-buffer-cache-name))
134 (name (buffer-name buffer)))
135 (with-current-buffer buffer
136 (setq wl-message-buffer-original-buffer
137 (wl-original-message-buffer-get name)))
140 (defun wl-message-buffer-cache-add (key)
141 "Add (KEY . buf) to the top of `wl-message-buffer-cache'.
142 Return its cache buffer."
143 (let ((len (length wl-message-buffer-cache))
145 (if (< len wl-message-buffer-cache-size)
146 (setq buf (wl-message-buffer-create))
147 (setq buf (wl-message-buffer-cache-buffer-get
148 (nth (1- len) wl-message-buffer-cache)))
149 (setcdr (nthcdr (- len 2) wl-message-buffer-cache) nil))
150 (setq wl-message-buffer-cache
151 (cons (wl-message-buffer-cache-entry-make key buf)
152 wl-message-buffer-cache))
155 (defun wl-message-buffer-cache-delete (&optional key)
156 "Delete the most recent cache entry"
158 (setq wl-message-buffer-cache
159 (delq (assoc key wl-message-buffer-cache)
160 wl-message-buffer-cache))
161 (let ((buf (wl-message-buffer-cache-buffer-get
162 (car wl-message-buffer-cache))))
163 (setq wl-message-buffer-cache
164 (nconc (cdr wl-message-buffer-cache)
165 (list (wl-message-buffer-cache-entry-make nil buf)))))))
167 (defun wl-message-buffer-cache-clean-up ()
168 "A function to flush all decoded messages in cache list."
170 (if (and (eq major-mode 'wl-summary-mode)
172 (get-buffer-window wl-message-buffer))
173 (delete-window (get-buffer-window wl-message-buffer)))
174 (wl-kill-buffers (regexp-quote wl-message-buffer-cache-name))
175 (setq wl-message-buffer-cache nil))
177 ;;; Message buffer handling from summary buffer.
179 (defun wl-message-buffer-window ()
180 "Get message buffer window if any."
181 (let* ((start-win (selected-window))
185 (setq cur-win (next-window cur-win))
186 (with-current-buffer (window-buffer cur-win)
187 (if (or (eq major-mode 'wl-message-mode)
188 (eq major-mode 'mime-view-mode))
189 (throw 'found cur-win)))
190 (not (eq cur-win start-win)))))))
192 (defun wl-message-select-buffer (buffer)
193 "Select BUFFER as a message buffer."
194 (let ((window (get-buffer-window buffer))
195 (sum (car wl-message-window-size))
196 (mes (cdr wl-message-window-size))
199 (not (eq (save-excursion (set-buffer (window-buffer window))
200 wl-message-buffer-cur-summary-buffer)
202 (delete-window window)
203 (run-hooks 'wl-message-window-deleted-hook)
206 (select-window window)
207 (when wl-fixed-window-configuration
208 (delete-other-windows)
209 (and wl-stay-folder-window
210 (wl-summary-toggle-disp-folder)))
211 ;; There's no buffer window. Search for message window and snatch it.
212 (if (setq window (wl-message-buffer-window))
213 (select-window window)
214 (setq whi (1- (window-height)))
217 (let ((total (+ sum mes)))
218 (setq sum (max window-min-height (/ (* whi sum) total)))
219 (setq mes (max window-min-height (/ (* whi mes) total))))
220 (if (< whi (+ sum mes))
221 (enlarge-window (- (+ sum mes) whi)))))
222 (split-window (get-buffer-window (current-buffer)) sum)
224 (switch-to-buffer buffer)))
226 (defun wl-message-narrow-to-page (&optional arg)
228 If ARG is specified, narrow to ARGth page."
230 (setq arg (if arg (prefix-numeric-value arg) 0))
233 (forward-page -1) ; Beginning of current page.
235 (goto-char (point-min))))
236 (forward-char 1) ; for compatibility with emacs-19.28 and emacs-19.29
239 ((> arg 0) (forward-page arg))
240 ((< arg 0) (forward-page (1- arg))))
243 (narrow-to-region (point)
246 (if (and (eolp) (not (bobp)))
250 (defun wl-message-prev-page (&optional lines)
251 "Scroll down current message by LINES.
252 Returns non-nil if top of message."
254 (let (cur-buf top real-top)
255 (unless (eq major-mode 'mime-view-mode)
256 (when (buffer-live-p wl-message-buffer)
257 (setq cur-buf (current-buffer))
258 (wl-message-select-buffer wl-message-buffer)))
259 (move-to-window-line 0)
260 (if (and wl-break-pages
262 (not (setq real-top (save-restriction (widen) (bobp)))))
264 (wl-message-narrow-to-page -1)
265 (goto-char (point-max))
273 (let ((f (assq (mime-preview-original-major-mode)
274 mime-preview-over-to-previous-method-alist)))
275 (if f (funcall (cdr f))))
277 (select-window (get-buffer-window cur-buf))))
280 (defun wl-message-next-page (&optional lines)
281 "Scroll up current message by LINES.
282 Returns non-nil if bottom of message."
284 (let (cur-buf bottom)
285 (unless (eq major-mode 'mime-view-mode)
286 (when (buffer-live-p wl-message-buffer)
287 (setq cur-buf (current-buffer))
288 (wl-message-select-buffer wl-message-buffer)))
289 (move-to-window-line -1)
292 (and (pos-visible-in-window-p)
294 (if (or (null wl-break-pages)
297 (widen) (forward-line) (eobp))))
299 (wl-message-narrow-to-page 1)
302 (static-if (boundp 'window-pixel-scroll-increment)
303 ;; XEmacs 21.2.20 and later.
304 (let (window-pixel-scroll-increment)
308 (goto-char (point-max))))
311 (let ((f (assq (mime-preview-original-major-mode)
312 mime-preview-over-to-next-method-alist)))
313 (if f (funcall (cdr f))))
315 (select-window (get-buffer-window cur-buf))))
319 (defun wl-message-follow-current-entity (buffer)
320 "Follow to current message."
321 (wl-draft-reply (wl-message-get-original-buffer)
322 nil wl-message-buffer-cur-summary-buffer) ; reply to all
323 (let ((mail-reply-buffer buffer))
324 (wl-draft-yank-from-mail-reply-buffer nil)))
328 (defun wl-message-mode ()
329 "A major mode for message displaying."
331 (setq major-mode 'wl-message-mode)
332 (setq buffer-read-only t)
333 (setq mode-name "Message"))
335 (defun wl-message-exit ()
336 "Move to summary buffer."
338 (let (summary-buf summary-win)
339 (if (setq summary-buf wl-message-buffer-cur-summary-buffer)
340 (if (setq summary-win (get-buffer-window summary-buf))
341 (select-window summary-win)
342 (switch-to-buffer summary-buf)
343 (wl-message-select-buffer wl-message-buffer)
344 (select-window (get-buffer-window summary-buf))))
345 (run-hooks 'wl-message-exit-hook)))
347 (defun wl-message-toggle-disp-summary ()
349 (let ((summary-buf (get-buffer wl-message-buffer-cur-summary-buffer))
352 (buffer-live-p summary-buf))
353 (if (setq summary-win (get-buffer-window summary-buf))
354 (delete-window summary-win)
355 (switch-to-buffer summary-buf)
356 (wl-message-select-buffer wl-message-buffer))
357 (wl-summary-goto-folder-subr wl-message-buffer-cur-folder 'no-sync
360 (let ((sum-buf (current-buffer)))
361 (wl-message-select-buffer wl-message-buffer)
362 (setq wl-message-buffer-cur-summary-buffer sum-buf)))))
364 (defun wl-message-get-original-buffer ()
365 "Get original buffer for current message buffer."
366 wl-message-buffer-original-buffer)
368 (defun wl-message-add-buttons-to-body (start end)
371 (narrow-to-region start end)
372 (let ((case-fold-search t)
373 (alist wl-message-body-button-alist)
376 (setq entry (car alist)
378 (goto-char (point-min))
379 (while (re-search-forward (car entry) nil t)
380 (unless (get-text-property (point) 'keymap)
381 (wl-message-add-button
382 (match-beginning (nth 1 entry))
383 (match-end (nth 1 entry))
385 (match-string (nth 3 entry))))))))))
387 (defun wl-message-add-buttons-to-header (start end)
390 (narrow-to-region start end)
391 (let ((case-fold-search t)
392 (alist wl-message-header-button-alist)
395 (setq entry (car alist)
397 (goto-char (point-min))
398 (while (re-search-forward (car entry) nil t)
399 (setq start (match-beginning 0)
400 end (if (re-search-forward "^[^ \t]" nil t)
404 (while (re-search-forward (nth 1 entry) end t)
405 (goto-char (match-end 0))
406 (wl-message-add-button
407 (match-beginning (nth 2 entry))
408 (match-end (nth 2 entry))
409 (nth 3 entry) (match-string (nth 4 entry))))
410 (goto-char end)))))))
412 (defun wl-message-redisplay (folder number flag &optional force-reload)
413 (let* ((default-mime-charset wl-mime-charset)
414 (buffer-read-only nil)
415 (summary-buf (current-buffer))
419 header-end real-fld-num summary-win delim)
420 (setq buffer-read-only nil)
421 (setq cache-used (wl-message-buffer-display
422 folder number flag force-reload))
423 (setq wl-message-buffer (car cache-used))
424 (setq message-buf wl-message-buffer)
425 (wl-message-select-buffer wl-message-buffer)
427 (set-buffer message-buf)
428 (make-local-variable 'truncate-partial-width-windows)
429 (setq truncate-partial-width-windows nil)
430 (setq truncate-lines wl-message-truncate-lines)
431 (setq buffer-read-only nil)
432 (setq wl-message-buffer-cur-summary-buffer summary-buf)
433 (setq wl-message-buffer-cur-folder (elmo-folder-name-internal folder))
434 (setq wl-message-buffer-cur-number number)
435 (setq mode-line-buffer-identification
436 (format "Wanderlust: << %s / %s >>"
437 (if (memq 'modeline wl-use-folder-petname)
438 (wl-folder-get-petname (elmo-folder-name-internal
440 (elmo-folder-name-internal folder)) number))
442 ; (when wl-highlight-body-too
443 ; (wl-highlight-body))
445 (wl-message-narrow-to-page)
446 (error nil)); ignore errors.
447 (setq cache-used (cdr cache-used))
448 (goto-char (point-min))
449 (when (re-search-forward "^$" nil t)
450 (wl-message-add-buttons-to-header (point-min) (point))
451 (wl-message-add-buttons-to-body (point) (point-max)))
452 (goto-char (point-min))
455 (run-hooks 'wl-message-redisplay-hook))
456 ;; go back to summary mode
457 (set-buffer-modified-p nil)
458 (setq buffer-read-only t)
459 (set-buffer summary-buf)
460 (setq summary-win (get-buffer-window summary-buf))
461 (if (window-live-p summary-win)
462 (select-window summary-win)))
465 ;; Use message buffer cache.
466 (defun wl-message-buffer-display (folder number flag
467 &optional force-reload unread)
468 (let* ((msg-id (elmo-message-field folder number 'message-id))
469 (fname (elmo-folder-name-internal folder))
470 (hit (wl-message-buffer-cache-hit (list fname number msg-id)))
473 (when (and hit (not (buffer-live-p hit)))
474 (wl-message-buffer-cache-delete (list fname number msg-id))
478 ;; move hit to the top.
479 (wl-message-buffer-cache-sort
480 (wl-message-buffer-cache-entry-make (list fname number msg-id) hit))
481 ;; buffer cache is used.
483 (with-current-buffer hit
484 (unless (eq wl-message-buffer-cur-flag flag)
486 ;; delete tail and add new to the top.
487 (setq hit (wl-message-buffer-cache-add (list fname number msg-id)))
489 (if (or force-reload read)
495 (wl-message-display-internal folder number flag
496 force-reload unread))
497 (setq wl-message-buffer-cur-flag flag))
499 (wl-message-buffer-cache-delete)
500 (error "Display message %s/%s is quitted" fname number))
502 (wl-message-buffer-cache-delete)
503 (signal (car err) (cdr err))
504 nil))) ;; will not be used
505 (cons hit cache-used)))
507 (defun wl-message-display-internal (folder number flag
508 &optional force-reload unread)
509 (let ((elmo-message-fetch-threshold wl-fetch-confirm-threshold))
510 (setq wl-message-buffer-all-header-flag (eq flag 'all-header))
513 (let (wl-highlight-x-face-function)
514 (prog1 (elmo-mime-display-as-is folder number
516 (wl-message-get-original-buffer)
517 'wl-original-message-mode
520 (wl-message-define-keymap))
521 (let (buffer-read-only)
522 (wl-highlight-message (point-min) (point-max) t))))
523 (elmo-mime-message-display folder number
525 (wl-message-get-original-buffer)
526 'wl-original-message-mode
529 (wl-message-define-keymap)))
530 (run-hooks 'wl-message-display-internal-hook)
531 (setq buffer-read-only t))))
533 (defsubst wl-message-buffer-prefetch-p (folder &optional number)
535 ((eq wl-message-buffer-prefetch-folder-type-list t)
537 ((and number wl-message-buffer-prefetch-folder-type-list)
538 (memq (elmo-folder-type-internal
539 (elmo-message-folder folder number))
540 wl-message-buffer-prefetch-folder-type-list))
541 (wl-message-buffer-prefetch-folder-type-list
542 (let ((list wl-message-buffer-prefetch-folder-type-list)
545 (while (setq type (pop list))
546 (if (elmo-folder-contains-type folder type)
548 ((consp wl-message-buffer-prefetch-folder-type-list)
549 (wl-string-match-member (elmo-folder-name-internal folder)
550 wl-message-buffer-prefetch-folder-type-list))
551 (t wl-message-buffer-prefetch-folder-type-list)))
553 (defvar wl-message-buffer-prefetch-timer nil)
555 (defun wl-message-buffer-prefetch-next (folder number &optional
557 (if (wl-message-buffer-prefetch-p folder)
558 (with-current-buffer (or summary (get-buffer wl-summary-buffer-name))
559 (let* ((next (funcall wl-message-buffer-prefetch-get-next-function
561 (when (and next (wl-message-buffer-prefetch-p folder next))
562 (if (not (fboundp 'run-with-idle-timer))
563 (when (sit-for wl-message-buffer-prefetch-idle-time)
564 (wl-message-buffer-prefetch folder next summary charset))
565 (unless wl-message-buffer-prefetch-timer
566 (setq wl-message-buffer-prefetch-timer
568 wl-message-buffer-prefetch-idle-time
570 'wl-message-buffer-prefetch
571 folder next summary charset)))))))))
573 (defun wl-message-buffer-prefetch (folder number summary charset)
574 (when (buffer-live-p summary)
577 (when (string= (elmo-folder-name-internal folder)
578 (wl-summary-buffer-folder-name))
579 (let ((message-id (elmo-message-field folder number 'message-id))
580 (wl-mime-charset charset)
581 (default-mime-charset charset)
582 result time1 time2 sec micro)
583 (if (not (wl-message-buffer-cache-hit (list folder
585 (let* ((size (elmo-message-field folder number 'size)))
586 (when (or (elmo-message-file-p folder number)
589 elmo-message-fetch-threshold
591 elmo-message-fetch-threshold))))
592 ;;(not (elmo-file-cache-exists-p message-id)))))
593 (when wl-message-buffer-prefetch-debug
594 (setq time1 (current-time))
595 (message "Prefetching %d..." number))
596 (setq result (wl-message-buffer-display folder number
598 (when wl-message-buffer-prefetch-debug
599 (setq time2 (current-time))
600 (setq sec (- (nth 1 time2)(nth 1 time1)))
601 (setq micro (- (nth 2 time2)(nth 2 time1)))
602 (setq micro (+ micro (* 1000000 sec)))
603 (message "Prefetching %d...done(%f msec)."
605 (/ micro 1000.0))))))))))
606 (setq wl-message-buffer-prefetch-timer nil))
608 (defvar wl-message-button-map (make-sparse-keymap))
610 (defun wl-message-add-button (from to function &optional data)
611 "Create a button between FROM and TO with callback FUNCTION and DATA."
614 (nconc (list 'wl-message-button-callback function)
616 (list 'wl-message-button-data data))))
617 (let ((ov (make-overlay from to)))
618 (overlay-put ov 'mouse-face 'highlight)
619 (overlay-put ov 'local-map wl-message-button-map)
620 (overlay-put ov 'evaporate t)))
622 (defun wl-message-button-dispatcher (event)
623 "Select the button under point."
625 (mouse-set-point event)
626 (let ((callback (get-text-property (point) 'wl-message-button-callback))
627 (data (get-text-property (point) 'wl-message-button-data)))
629 (funcall callback data)
630 (wl-message-button-dispatcher-internal event))))
632 (defun wl-message-button-refer-article (data)
633 "Read article specified by Message-ID DATA at point."
634 (switch-to-buffer-other-window
635 wl-message-buffer-cur-summary-buffer)
636 (if (wl-summary-jump-to-msg-by-message-id data)
637 (wl-summary-redisplay)))
639 (defun wl-message-uu-substring (buf outbuf &optional first last)
642 (search-forward "\n\n")
644 ep filename case-fold-search)
648 (if (re-search-forward "^begin[ \t]+[0-9]+[ \t]+\\([^ ].*\\)" nil t)
649 (setq filename (buffer-substring (match-beginning 1)(match-end 1)))
651 (re-search-forward "^M.*$" nil t)) ; uuencoded string
654 (goto-char (point-max))
656 (re-search-backward "^end" sp t)
657 (re-search-backward "^M.*$" sp t)) ; uuencoded string
661 (goto-char (point-max))
662 (insert-buffer-substring buf sp ep)
667 (product-provide (provide 'wl-message) (require 'wl-version))
669 ;;; wl-message.el ends here