53de45760d849a47079fcb75485ed1b8b50cc666
[elisp/wanderlust.git] / wl / wl-summary.el
1 ;;; wl-summary.el --- Summary mode for Wanderlust.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
5 ;; Copyright (C) 1999,2000      TSUMURA Tomoaki <tsumura@kuis.kyoto-u.ac.jp>
6 ;; Copyright (C) 1999,2000      Kenichi OKADA <okada@opaopa.org>
7
8 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
9 ;;      Masahiro MURATA <muse@ba2.so-net.ne.jp>
10 ;;      TSUMURA Tomoaki <tsumura@kuis.kyoto-u.ac.jp>
11 ;;      Kenichi OKADA <okada@opaopa.org>
12 ;; Keywords: mail, net news
13
14 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
15
16 ;; This program is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
20 ;;
21 ;; This program is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 ;; GNU General Public License for more details.
25 ;;
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
30 ;;
31
32 ;;; Commentary:
33 ;;
34
35 ;;; Code:
36 ;;
37
38 (require 'elmo)
39 (require 'elmo-multi)
40 (require 'wl-message)
41 (require 'wl-vars)
42 (require 'wl-highlight)
43 (require 'wl-refile)
44 (require 'wl-util)
45 (condition-case nil (require 'timezone) (error nil))
46 (condition-case nil (require 'easymenu) (error nil))
47 (require 'elmo-date)
48 (condition-case nil (require 'ps-print) (error nil))
49
50 (eval-when-compile
51   (require 'cl)
52   (condition-case () (require 'timer) (error nil))
53   (defalias-maybe 'ps-print-buffer-with-faces 'ignore)
54   (defalias-maybe 'elmo-database-msgid-put 'ignore)
55   (defalias-maybe 'elmo-database-close 'ignore)
56   (defalias-maybe 'elmo-database-msgid-get 'ignore)
57   (defalias-maybe 'run-with-idle-timer 'ignore)
58   (defalias-maybe 'ps-print-preprint 'ignore))
59
60 (defvar dragdrop-drop-functions)
61 (defvar scrollbar-height)
62 (defvar mail-reply-buffer)
63 (defvar elmo-global-flag-list)
64
65 (defvar wl-summary-buffer-name "Summary")
66 (defvar wl-summary-mode-map nil)
67 (defvar wl-current-summary-buffer nil)
68
69 (defvar wl-summary-buffer-elmo-folder nil)
70
71 (defmacro wl-summary-buffer-folder-name ()
72   (` (and wl-summary-buffer-elmo-folder
73           (elmo-folder-name-internal wl-summary-buffer-elmo-folder))))
74
75 (defvar wl-summary-buffer-disp-msg    nil)
76 (defvar wl-summary-buffer-disp-folder nil)
77 (defvar wl-summary-buffer-temp-mark-list nil)
78 (defvar wl-summary-buffer-last-displayed-msg nil)
79 (defvar wl-summary-buffer-current-msg nil)
80 (defvar wl-summary-buffer-unread-count 0)
81 (defvar wl-summary-buffer-new-count    0)
82 (defvar wl-summary-buffer-answered-count 0)
83 (defvar wl-summary-buffer-mime-charset  nil)
84 (defvar wl-summary-buffer-weekday-name-lang  nil)
85 (defvar wl-summary-buffer-thread-indent-set-alist  nil)
86 (defvar wl-summary-buffer-view nil)
87 (defvar wl-summary-buffer-message-modified nil)
88 (defvar wl-summary-buffer-thread-modified nil)
89
90 (defvar wl-summary-buffer-number-column nil)
91 (defvar wl-summary-buffer-temp-mark-column nil)
92 (defvar wl-summary-buffer-persistent-mark-column nil)
93
94 (defvar wl-summary-buffer-unsync-mark-number-list nil)
95
96 (defvar wl-summary-buffer-persistent nil)
97 (defvar wl-summary-buffer-thread-nodes nil)
98 (defvar wl-summary-buffer-target-mark-list nil)
99 (defvar wl-summary-buffer-prev-refile-destination nil)
100 (defvar wl-summary-buffer-saved-message nil)
101 (defvar wl-summary-buffer-prev-folder-function nil)
102 (defvar wl-summary-buffer-next-folder-function nil)
103 (defvar wl-summary-buffer-exit-function nil)
104 (defvar wl-summary-buffer-next-message-function nil)
105 (defvar wl-summary-buffer-window-scroll-functions nil)
106 (defvar wl-summary-buffer-number-list nil)
107 (defvar wl-summary-buffer-folder-name nil)
108 (defvar wl-summary-buffer-line-formatter nil)
109 (defvar wl-summary-buffer-line-format nil)
110 (defvar wl-summary-buffer-mode-line-formatter nil)
111 (defvar wl-summary-buffer-mode-line nil)
112
113 (defvar wl-thread-indent-level-internal nil)
114 (defvar wl-thread-have-younger-brother-str-internal nil)
115 (defvar wl-thread-youngest-child-str-internal nil)
116 (defvar wl-thread-vertical-str-internal nil)
117 (defvar wl-thread-horizontal-str-internal nil)
118 (defvar wl-thread-space-str-internal nil)
119 (defvar wl-summary-last-visited-folder nil)
120 (defvar wl-read-folder-history nil)
121 (defvar wl-summary-scored nil)
122 (defvar wl-crosspost-alist-modified nil)
123 (defvar wl-summary-alike-hashtb nil)
124 (defvar wl-summary-search-buf-name " *wl-search-subject*")
125 (defvar wl-summary-delayed-update nil)
126 (defvar wl-summary-search-buf-folder-name nil)
127
128 (defvar wl-summary-get-petname-function 'wl-address-get-petname-1)
129
130 (defvar wl-summary-shell-command-last "")
131
132 (defvar wl-ps-preprint-hook nil)
133 (defvar wl-ps-print-hook nil)
134
135 (defvar wl-thread-saved-entity-hashtb-internal nil)
136
137 (make-variable-buffer-local 'wl-summary-buffer-elmo-folder)
138 (make-variable-buffer-local 'wl-summary-search-buf-folder-name)
139 (make-variable-buffer-local 'wl-summary-buffer-disp-msg)
140 (make-variable-buffer-local 'wl-summary-buffer-disp-folder)
141 (make-variable-buffer-local 'wl-summary-buffer-target-mark-list)
142 (make-variable-buffer-local 'wl-summary-buffer-temp-mark-list)
143 (make-variable-buffer-local 'wl-summary-buffer-last-displayed-msg)
144 (make-variable-buffer-local 'wl-summary-buffer-unread-count)
145 (make-variable-buffer-local 'wl-summary-buffer-new-count)
146 (make-variable-buffer-local 'wl-summary-buffer-answered-count)
147 (make-variable-buffer-local 'wl-summary-buffer-mime-charset)
148 (make-variable-buffer-local 'wl-summary-buffer-weekday-name-lang)
149 (make-variable-buffer-local 'wl-summary-buffer-thread-indent-set)
150 (make-variable-buffer-local 'wl-summary-buffer-view)
151 (make-variable-buffer-local 'wl-summary-buffer-message-modified)
152 (make-variable-buffer-local 'wl-summary-buffer-thread-modified)
153 (make-variable-buffer-local 'wl-summary-buffer-number-column)
154 (make-variable-buffer-local 'wl-summary-buffer-temp-mark-column)
155 (make-variable-buffer-local 'wl-summary-buffer-persistent-mark-column)
156 (make-variable-buffer-local 'wl-summary-buffer-unsync-mark-number-list)
157 (make-variable-buffer-local 'wl-summary-buffer-persistent)
158 (make-variable-buffer-local 'wl-summary-buffer-thread-nodes)
159 (make-variable-buffer-local 'wl-summary-buffer-prev-refile-destination)
160 (make-variable-buffer-local 'wl-summary-buffer-saved-message)
161 (make-variable-buffer-local 'wl-summary-scored)
162 (make-variable-buffer-local 'wl-summary-default-score)
163 (make-variable-buffer-local 'wl-summary-move-direction-downward)
164 (make-variable-buffer-local 'wl-summary-important-above)
165 (make-variable-buffer-local 'wl-summary-target-above)
166 (make-variable-buffer-local 'wl-summary-mark-below)
167 (make-variable-buffer-local 'wl-summary-expunge-below)
168 (make-variable-buffer-local 'wl-thread-indent-level-internal)
169 (make-variable-buffer-local 'wl-thread-have-younger-brother-str-internal)
170 (make-variable-buffer-local 'wl-thread-youngest-child-str-internal)
171 (make-variable-buffer-local 'wl-thread-vertical-str-internal)
172 (make-variable-buffer-local 'wl-thread-horizontal-str-internal)
173 (make-variable-buffer-local 'wl-thread-space-str-internal)
174 (make-variable-buffer-local 'wl-summary-buffer-prev-folder-function)
175 (make-variable-buffer-local 'wl-summary-buffer-next-folder-function)
176 (make-variable-buffer-local 'wl-summary-buffer-exit-function)
177 (make-variable-buffer-local 'wl-summary-buffer-next-message-function)
178 (make-variable-buffer-local 'wl-summary-buffer-window-scroll-functions)
179 (make-variable-buffer-local 'wl-summary-buffer-number-list)
180 (make-variable-buffer-local 'wl-summary-buffer-folder-name)
181 (make-variable-buffer-local 'wl-summary-buffer-line-formatter)
182 (make-variable-buffer-local 'wl-summary-buffer-line-format)
183 (make-variable-buffer-local 'wl-summary-buffer-mode-line-formatter)
184 (make-variable-buffer-local 'wl-summary-buffer-mode-line)
185
186 (defvar wl-datevec)
187 (defvar wl-thr-indent-string)
188 (defvar wl-thr-children-number)
189 (defvar wl-thr-linked)
190 (defvar wl-message-entity)
191 (defvar wl-parent-message-entity)
192 (defvar wl-temp-mark)
193 (defvar wl-persistent-mark)
194
195 (defmacro wl-summary-sticky-buffer-name (name)
196   (` (concat wl-summary-buffer-name ":" (, name))))
197
198 (defun wl-summary-default-subject (subject-string)
199   (if (string-match "^[ \t]*\\[[^:]+[,: ][0-9]+\\][ \t]*" subject-string)
200       (substring subject-string (match-end 0))
201     subject-string))
202
203 (defun wl-summary-default-from (from)
204   "Instance of `wl-summary-from-function'.
205 Ordinarily returns the sender name. Returns recipient names if (1)
206 summary's folder name matches with `wl-summary-showto-folder-regexp'
207 and (2) sender address is yours.
208
209 See also variable `wl-use-petname'."
210   (let (retval tos ng)
211     (unless
212         (and (eq major-mode 'wl-summary-mode)
213              (stringp wl-summary-showto-folder-regexp)
214              (string-match wl-summary-showto-folder-regexp
215                            (wl-summary-buffer-folder-name))
216              (wl-address-user-mail-address-p from)
217              (cond
218               ((and (setq tos (elmo-message-entity-field
219                                wl-message-entity 'to t))
220                     (not (string= "" tos)))
221                (setq retval
222                      (concat "To:"
223                              (mapconcat
224                               (function
225                                (lambda (to)
226                                  (eword-decode-string
227                                   (if wl-use-petname
228                                       (or
229                                        (funcall
230                                         wl-summary-get-petname-function to)
231                                        (car
232                                         (std11-extract-address-components to))
233                                        to)
234                                     to))))
235                               (wl-parse-addresses tos)
236                               ","))))
237               ((setq ng (elmo-message-entity-field
238                          wl-message-entity 'newsgroups))
239                (setq retval (concat "Ng:" ng)))))
240       (if wl-use-petname
241           (setq retval (or (funcall wl-summary-get-petname-function from)
242                            (car (std11-extract-address-components from))
243                            from))
244         (setq retval from)))
245     retval))
246
247 (defun wl-summary-simple-from (string)
248   (if wl-use-petname
249       (or (funcall wl-summary-get-petname-function string)
250           (car (std11-extract-address-components string))
251           string)
252     string))
253
254 (defvar wl-summary-sort-specs '(number date subject from list-info size))
255 (defvar wl-summary-default-sort-spec 'date)
256
257 (defvar wl-summary-mode-menu-spec
258   '("Summary"
259     ["Read" wl-summary-read t]
260     ["Prev page" wl-summary-prev-page t]
261     ["Next page" wl-summary-next-page t]
262     ["Top"       wl-summary-display-top t]
263     ["Bottom"    wl-summary-display-bottom t]
264     ["Prev"      wl-summary-prev t]
265     ["Next"      wl-summary-next t]
266     ["Up"        wl-summary-up t]
267     ["Down"      wl-summary-down t]
268     ["Parent message" wl-summary-jump-to-parent-message t]
269     "----"
270     ["Sync"            wl-summary-sync t]
271     ["Execute"         wl-summary-exec t]
272     ["Go to other folder" wl-summary-goto-folder t]
273     ["Pick" wl-summary-pick t]
274     ["Mark as read all" wl-summary-mark-as-read-all t]
275     ["Unmark all"      wl-summary-unmark-all t]
276     ["Toggle display message" wl-summary-toggle-disp-msg t]
277     ["Display folder" wl-summary-toggle-disp-folder t]
278     ["Toggle threading" wl-summary-toggle-thread t]
279     ["Stick" wl-summary-stick t]
280     ("Sort"
281      ["By Number" wl-summary-sort-by-number t]
282      ["By Size" wl-summary-sort-by-size t]
283      ["By Date" wl-summary-sort-by-date t]
284      ["By From" wl-summary-sort-by-from t]
285      ["By Subject" wl-summary-sort-by-subject t]
286      ["By List Info" wl-summary-sort-by-list-info t])
287     "----"
288     ("Message Operation"
289      ["Mark as read"    wl-summary-mark-as-read t]
290      ["Set flags"       wl-summary-set-flags t]
291      ["Mark as unread"   wl-summary-mark-as-unread t]
292      ["Mark as answered" wl-summary-mark-as-answered t]
293      ["Set dispose mark" wl-summary-dispose t]
294      ["Set refile mark" wl-summary-refile t]
295      ["Set copy mark"   wl-summary-copy t]
296      ["Set resend mark" wl-summary-resend t]
297      ["Prefetch"        wl-summary-prefetch t]
298      ["Set target mark" wl-summary-target-mark t]
299      ["Unmark"          wl-summary-unmark t]
300      ["Save"            wl-summary-save t]
301      ["Cancel posted news" wl-summary-cancel-message t]
302      ["Supersedes message" wl-summary-supersedes-message t]
303      ["Resend bounced mail" wl-summary-resend-bounced-mail t]
304      ["Enter the message" wl-summary-jump-to-current-message t]
305      ["Pipe message" wl-summary-pipe-message t]
306      ["Print message" wl-summary-print-message t])
307     ("Thread Operation"
308      ["Open or Close" wl-thread-open-close (eq wl-summary-buffer-view 'thread)]
309      ["Open all"     wl-thread-open-all (eq wl-summary-buffer-view 'thread)]
310      ["Close all"    wl-thread-close-all (eq wl-summary-buffer-view 'thread)]
311      ["Mark as read" wl-thread-mark-as-read (eq wl-summary-buffer-view 'thread)]
312      ["Set flags"       wl-thread-set-flags (eq wl-summary-buffer-view 'thread)]
313      ["Mark as unread"          wl-thread-mark-as-unread (eq wl-summary-buffer-view 'thread)]
314      ["Mark as answered"        wl-thread-mark-as-answered (eq wl-summary-buffer-view 'thread)]
315      ["Set delete mark"  wl-thread-delete (eq wl-summary-buffer-view 'thread)]
316      ["Set refile mark"  wl-thread-refile (eq wl-summary-buffer-view 'thread)]
317      ["Set copy mark"    wl-thread-copy (eq wl-summary-buffer-view 'thread)]
318      ["Prefetch"     wl-thread-prefetch (eq wl-summary-buffer-view 'thread)]
319      ["Set target mark"        wl-thread-target-mark (eq wl-summary-buffer-view 'thread)]
320      ["Unmark"      wl-thread-unmark (eq wl-summary-buffer-view 'thread)]
321      ["Save"            wl-thread-save (eq wl-summary-buffer-view 'thread)]
322      ["Execute"      wl-thread-exec (eq wl-summary-buffer-view 'thread)])
323     ("Region Operation"
324      ["Mark as read" wl-summary-mark-as-read-region t]
325      ["Set flags" wl-summary-set-flags-region t]
326      ["Mark as unread" wl-summary-mark-as-unread-region t]
327      ["Mark as answered" wl-summary-mark-as-answered-region t]
328      ["Set dispose mark" wl-summary-dispose-region t]
329      ["Set refile mark" wl-summary-refile-region t]
330      ["Set copy mark" wl-summary-copy-region t]
331      ["Prefetch" wl-summary-prefetch-region t]
332      ["Set target mark" wl-summary-target-mark-region t]
333      ["Unmark" wl-summary-unmark-region t]
334      ["Save" wl-summary-save-region t]
335      ["Execute" wl-summary-exec-region t])
336     ("Mark Operation"
337      ["Mark as read" wl-summary-target-mark-mark-as-read t]
338      ["Set flags" wl-summary-target-mark-set-flags t]
339      ["Mark as unread" wl-summary-target-mark-mark-as-unread t]
340      ["Set delete mark" wl-summary-target-mark-delete t]
341      ["Set refile mark" wl-summary-target-mark-refile t]
342      ["Set copy mark" wl-summary-target-mark-copy t]
343      ["Prefetch" wl-summary-target-mark-prefetch t]
344      ["Save" wl-summary-target-mark-save t]
345      ["Reply with citation" wl-summary-target-mark-reply-with-citation t]
346      ["Forward" wl-summary-target-mark-forward t]
347      ["uudecode" wl-summary-target-mark-uudecode t])
348     ("Score Operation"
349      ["Switch current score file" wl-score-change-score-file t]
350      ["Edit current score file" wl-score-edit-current-scores t]
351      ["Edit score file" wl-score-edit-file t]
352      ["Set mark below" wl-score-set-mark-below t]
353      ["Set expunge below" wl-score-set-expunge-below t]
354      ["Rescore buffer" wl-summary-rescore t]
355      ["Increase score" wl-summary-increase-score t]
356      ["Lower score" wl-summary-lower-score t])
357     "----"
358     ("Writing Messages"
359      ["Write a message" wl-summary-write t]
360      ["Write for current folder" wl-summary-write-current-folder t]
361      ["Reply" wl-summary-reply t]
362      ["Reply with citation" wl-summary-reply-with-citation t]
363      ["Forward" wl-summary-forward t])
364     "----"
365     ["Toggle Plug Status" wl-toggle-plugged t]
366     ["Change Plug Status" wl-plugged-change t]
367     "----"
368     ["Exit Current Folder" wl-summary-exit t]))
369
370 (if wl-on-xemacs
371     (defun wl-summary-setup-mouse ()
372       (define-key wl-summary-mode-map 'button4 'wl-summary-prev)
373       (define-key wl-summary-mode-map 'button5 'wl-summary-next)
374       (define-key wl-summary-mode-map [(shift button4)]
375         'wl-summary-up)
376       (define-key wl-summary-mode-map [(shift button5)]
377         'wl-summary-down)
378       (define-key wl-summary-mode-map 'button2 'wl-summary-click))
379   (defun wl-summary-setup-mouse ()
380     (define-key wl-summary-mode-map [mouse-4] 'wl-summary-prev)
381     (define-key wl-summary-mode-map [mouse-5] 'wl-summary-next)
382     (define-key wl-summary-mode-map [S-mouse-4] 'wl-summary-up)
383     (define-key wl-summary-mode-map [S-mouse-5] 'wl-summary-down)
384     ;; For Meadow2
385     (define-key wl-summary-mode-map [mouse-wheel1]
386       'wl-summary-wheel-dispatcher)
387     (define-key wl-summary-mode-map [S-mouse-wheel1]
388       'wl-summary-wheel-dispatcher)
389     (define-key wl-summary-mode-map [mouse-2] 'wl-summary-click)))
390
391 (if wl-summary-mode-map
392     ()
393   (setq wl-summary-mode-map (make-sparse-keymap))
394   (define-key wl-summary-mode-map " "    'wl-summary-read)
395   (define-key wl-summary-mode-map "."    'wl-summary-redisplay)
396   (define-key wl-summary-mode-map "<"    'wl-summary-display-top)
397   (define-key wl-summary-mode-map ">"    'wl-summary-display-bottom)
398   (define-key wl-summary-mode-map "\177" 'wl-summary-prev-page)
399   (define-key wl-summary-mode-map [backspace] 'wl-summary-prev-page)
400   (define-key wl-summary-mode-map "\r"   'wl-summary-next-line-content)
401   (define-key wl-summary-mode-map "\C-m" 'wl-summary-next-line-content)
402   (define-key wl-summary-mode-map "/"    'wl-thread-open-close)
403   (define-key wl-summary-mode-map "["    'wl-thread-open-all)
404   (define-key wl-summary-mode-map "]"    'wl-thread-close-all)
405   (define-key wl-summary-mode-map "-"    'wl-summary-prev-line-content)
406   (define-key wl-summary-mode-map "\e\r" 'wl-summary-prev-line-content)
407   (define-key wl-summary-mode-map "g"    'wl-summary-goto-folder)
408   (define-key wl-summary-mode-map "G"    'wl-summary-goto-folder-sticky)
409   (define-key wl-summary-mode-map "c"    'wl-summary-mark-as-read-all)
410
411   (define-key wl-summary-mode-map "a"    'wl-summary-reply)
412   (define-key wl-summary-mode-map "A"    'wl-summary-reply-with-citation)
413   (define-key wl-summary-mode-map "C"    'wl-summary-cancel-message)
414   (define-key wl-summary-mode-map "E"    'wl-summary-reedit)
415   (define-key wl-summary-mode-map "\eE"  'wl-summary-resend-bounced-mail)
416   (define-key wl-summary-mode-map "f"    'wl-summary-forward)
417   (define-key wl-summary-mode-map "$"    'wl-summary-mark-as-important)
418   (define-key wl-summary-mode-map "F"    'wl-summary-set-flags)
419   (define-key wl-summary-mode-map "&"    'wl-summary-mark-as-answered)
420   (define-key wl-summary-mode-map "@"    'wl-summary-edit-addresses)
421
422   (define-key wl-summary-mode-map "y"    'wl-summary-save)
423   (define-key wl-summary-mode-map "n"    'wl-summary-next)
424   (define-key wl-summary-mode-map "p"    'wl-summary-prev)
425   (define-key wl-summary-mode-map "N"    'wl-summary-down)
426   (define-key wl-summary-mode-map "P"    'wl-summary-up)
427   (define-key wl-summary-mode-map "w"    'wl-summary-write)
428   (define-key wl-summary-mode-map "W"    'wl-summary-write-current-folder)
429   (define-key wl-summary-mode-map "e"     'wl-summary-save)
430   (define-key wl-summary-mode-map "\C-c\C-o" 'wl-jump-to-draft-buffer)
431   (define-key wl-summary-mode-map "\C-c\C-a" 'wl-addrmgr)
432   (define-key wl-summary-mode-map "\C-c\C-p" 'wl-summary-previous-buffer)
433   (define-key wl-summary-mode-map "\C-c\C-n" 'wl-summary-next-buffer)
434   (define-key wl-summary-mode-map "H"    'wl-summary-redisplay-all-header)
435   (define-key wl-summary-mode-map "M"    'wl-summary-redisplay-no-mime)
436   (define-key wl-summary-mode-map "B"    'wl-summary-burst)
437   (define-key wl-summary-mode-map "Z"    'wl-status-update)
438   (define-key wl-summary-mode-map "#"    'wl-summary-print-message)
439   (define-key wl-summary-mode-map "|"    'wl-summary-pipe-message)
440   (define-key wl-summary-mode-map "z"    'wl-summary-suspend)
441   (define-key wl-summary-mode-map "q"    'wl-summary-exit)
442   (define-key wl-summary-mode-map "Q"    'wl-summary-force-exit)
443
444   (define-key wl-summary-mode-map "j"    'wl-summary-jump-to-current-message)
445   (define-key wl-summary-mode-map "J"    'wl-thread-jump-to-msg)
446   (define-key wl-summary-mode-map "I"    'wl-summary-incorporate)
447   (define-key wl-summary-mode-map "\M-j" 'wl-summary-jump-to-msg-by-message-id)
448   (define-key wl-summary-mode-map "^"    'wl-summary-jump-to-parent-message)
449   (define-key wl-summary-mode-map "!"    'wl-summary-mark-as-unread)
450
451   (define-key wl-summary-mode-map "s"    'wl-summary-sync)
452   (define-key wl-summary-mode-map "S"    'wl-summary-sort)
453   (define-key wl-summary-mode-map "\M-s"    'wl-summary-stick)
454   (define-key wl-summary-mode-map "T"    'wl-summary-toggle-thread)
455
456   (define-key wl-summary-mode-map "l"    'wl-summary-toggle-disp-folder)
457   (define-key wl-summary-mode-map "v"    'wl-summary-toggle-disp-msg)
458   (define-key wl-summary-mode-map "V"    'wl-summary-virtual)
459
460   (define-key wl-summary-mode-map "\C-i"  'wl-summary-goto-last-displayed-msg)
461   (define-key wl-summary-mode-map "?"    'wl-summary-pick)
462   (define-key wl-summary-mode-map "\ee"  'wl-summary-expire)
463
464   ;; copy & paste.
465   (define-key wl-summary-mode-map "\ew"  'wl-summary-save-current-message)
466   (define-key wl-summary-mode-map "\C-y"  'wl-summary-yank-saved-message)
467
468   ;; line commands
469   (define-key wl-summary-mode-map "R"    'wl-summary-mark-as-read)
470   (define-key wl-summary-mode-map "i"    'wl-summary-prefetch)
471   (define-key wl-summary-mode-map "x"    'wl-summary-exec)
472   (define-key wl-summary-mode-map "*"    'wl-summary-target-mark)
473   (define-key wl-summary-mode-map "o"    'wl-summary-refile)
474   (define-key wl-summary-mode-map "O"    'wl-summary-copy)
475   (define-key wl-summary-mode-map "\M-o" 'wl-summary-refile-prev-destination)
476   (define-key wl-summary-mode-map "\C-o" 'wl-summary-auto-refile)
477   (define-key wl-summary-mode-map "d"    'wl-summary-dispose)
478   (define-key wl-summary-mode-map "u"    'wl-summary-unmark)
479   (define-key wl-summary-mode-map "U"    'wl-summary-unmark-all)
480   (define-key wl-summary-mode-map "D"    'wl-summary-delete)
481   (define-key wl-summary-mode-map "~"    'wl-summary-resend)
482
483   ;; thread commands
484   (define-key wl-summary-mode-map "t"   (make-sparse-keymap))
485   (define-key wl-summary-mode-map "tR" 'wl-thread-mark-as-read)
486   (define-key wl-summary-mode-map "ti" 'wl-thread-prefetch)
487   (define-key wl-summary-mode-map "tx" 'wl-thread-exec)
488   (define-key wl-summary-mode-map "t*" 'wl-thread-target-mark)
489   (define-key wl-summary-mode-map "to" 'wl-thread-refile)
490   (define-key wl-summary-mode-map "tO" 'wl-thread-copy)
491   (define-key wl-summary-mode-map "t\M-o" 'wl-thread-refile-prev-destination)
492   (define-key wl-summary-mode-map "td" 'wl-thread-dispose)
493   (define-key wl-summary-mode-map "tD" 'wl-thread-delete)
494   (define-key wl-summary-mode-map "t~" 'wl-thread-resend)
495   (define-key wl-summary-mode-map "tu" 'wl-thread-unmark)
496   (define-key wl-summary-mode-map "t!" 'wl-thread-mark-as-unread)
497   (define-key wl-summary-mode-map "t$" 'wl-thread-mark-as-important)
498   (define-key wl-summary-mode-map "tF" 'wl-thread-set-flags)
499   (define-key wl-summary-mode-map "t&" 'wl-thread-mark-as-answered)
500   (define-key wl-summary-mode-map "ty" 'wl-thread-save)
501   (define-key wl-summary-mode-map "ts" 'wl-thread-set-parent)
502
503   ;; target-mark commands
504   (define-key wl-summary-mode-map "m"     (make-sparse-keymap))
505   (define-key wl-summary-mode-map "mi"   'wl-summary-target-mark-prefetch)
506   (define-key wl-summary-mode-map "mo"   'wl-summary-target-mark-refile)
507   (define-key wl-summary-mode-map "mO"   'wl-summary-target-mark-copy)
508   (define-key wl-summary-mode-map "m\M-o" 'wl-summary-target-mark-refile-prev-destination)
509   (define-key wl-summary-mode-map "md"   'wl-summary-target-mark-dispose)
510   (define-key wl-summary-mode-map "mD"   'wl-summary-target-mark-delete)
511   (define-key wl-summary-mode-map "m~"   'wl-summary-target-mark-resend)
512
513   (define-key wl-summary-mode-map "mu"   'wl-summary-delete-all-temp-marks)
514
515   (define-key wl-summary-mode-map "my"   'wl-summary-target-mark-save)
516   (define-key wl-summary-mode-map "mR"   'wl-summary-target-mark-mark-as-read)
517   (define-key wl-summary-mode-map "m!"   'wl-summary-target-mark-mark-as-unread)
518   (define-key wl-summary-mode-map "m&"   'wl-summary-target-mark-mark-as-answered)
519   (define-key wl-summary-mode-map "m$"   'wl-summary-target-mark-mark-as-important)
520   (define-key wl-summary-mode-map "mF"   'wl-summary-target-mark-set-flags)
521   (define-key wl-summary-mode-map "mU"   'wl-summary-target-mark-uudecode)
522   (define-key wl-summary-mode-map "ma"   'wl-summary-target-mark-all)
523   (define-key wl-summary-mode-map "mt"   'wl-summary-target-mark-thread)
524   (define-key wl-summary-mode-map "mA"   'wl-summary-target-mark-reply-with-citation)
525   (define-key wl-summary-mode-map "mf"   'wl-summary-target-mark-forward)
526   (define-key wl-summary-mode-map "m?"   'wl-summary-target-mark-pick)
527   (define-key wl-summary-mode-map "m#"   'wl-summary-target-mark-print)
528   (define-key wl-summary-mode-map "m|"   'wl-summary-target-mark-pipe)
529
530   ;; region commands
531   (define-key wl-summary-mode-map "r"    (make-sparse-keymap))
532   (define-key wl-summary-mode-map "rR"   'wl-summary-mark-as-read-region)
533   (define-key wl-summary-mode-map "ri"   'wl-summary-prefetch-region)
534   (define-key wl-summary-mode-map "rx"   'wl-summary-exec-region)
535   (define-key wl-summary-mode-map "mr"   'wl-summary-target-mark-region)
536   (define-key wl-summary-mode-map "r*"   'wl-summary-target-mark-region)
537   (define-key wl-summary-mode-map "ro"   'wl-summary-refile-region)
538   (define-key wl-summary-mode-map "rO"   'wl-summary-copy-region)
539   (define-key wl-summary-mode-map "r\M-o" 'wl-summary-refile-prev-destination-region)
540   (define-key wl-summary-mode-map "rd"   'wl-summary-dispose-region)
541   (define-key wl-summary-mode-map "rD"   'wl-summary-delete-region)
542   (define-key wl-summary-mode-map "r~"   'wl-summary-resend-region)
543   (define-key wl-summary-mode-map "ru"   'wl-summary-unmark-region)
544   (define-key wl-summary-mode-map "r!"   'wl-summary-mark-as-unread-region)
545   (define-key wl-summary-mode-map "r$"   'wl-summary-mark-as-important-region)
546   (define-key wl-summary-mode-map "rF"   'wl-summary-set-flags-region)
547   (define-key wl-summary-mode-map "r&"   'wl-summary-mark-as-answered-region)
548   (define-key wl-summary-mode-map "ry"   'wl-summary-save-region)
549
550   ;; score commands
551   (define-key wl-summary-mode-map "K"    'wl-summary-increase-score)
552   (define-key wl-summary-mode-map "L"    'wl-summary-lower-score)
553   (define-key wl-summary-mode-map "h"    (make-sparse-keymap))
554   (define-key wl-summary-mode-map "hR"   'wl-summary-rescore)
555   (define-key wl-summary-mode-map "hc"   'wl-score-change-score-file)
556   (define-key wl-summary-mode-map "he"   'wl-score-edit-current-scores)
557   (define-key wl-summary-mode-map "hf"   'wl-score-edit-file)
558   (define-key wl-summary-mode-map "hF"   'wl-score-flush-cache)
559   (define-key wl-summary-mode-map "hm"   'wl-score-set-mark-below)
560   (define-key wl-summary-mode-map "hx"   'wl-score-set-expunge-below)
561
562   ;; misc
563   (define-key wl-summary-mode-map "\C-c\C-f" 'wl-summary-toggle-header-narrowing)
564   (define-key wl-summary-mode-map "\M-t" 'wl-toggle-plugged)
565   (define-key wl-summary-mode-map "\C-t" 'wl-plugged-change)
566   ;;
567   (define-key wl-summary-mode-map "\C-x\C-s" 'wl-summary-save-status)
568   (wl-summary-setup-mouse)
569   (easy-menu-define
570    wl-summary-mode-menu
571    wl-summary-mode-map
572    "Menu used in Summary mode."
573    wl-summary-mode-menu-spec))
574
575 (defsubst wl-summary-message-visible-p (number)
576   "Return non-nil if the message with NUMBER is visible."
577   (or (eq wl-summary-buffer-view 'sequence)
578       (not (wl-thread-entity-parent-invisible-p
579             (wl-thread-get-entity number)))))
580
581 (defun wl-summary-update-mark-and-highlight-window (&optional win beg)
582   "A function to be called as window-scroll-functions."
583   (with-current-buffer (window-buffer win)
584     (when (eq major-mode 'wl-summary-mode)
585       (let ((beg (or beg (window-start win)))
586             (end (condition-case nil
587                      (window-end win t) ; old emacsen doesn't support 2nd arg.
588                    (error (window-end win))))
589             number flags
590             wl-summary-highlight)
591         (save-excursion
592           (goto-char beg)
593           (while (and (< (point) end) (not (eobp)))
594             (when (null (get-text-property (point) 'face))
595               (setq number (wl-summary-message-number)
596                     flags (elmo-message-flags wl-summary-buffer-elmo-folder
597                                               number))
598               (setq wl-summary-highlight nil)
599               (wl-summary-update-persistent-mark number flags)
600               (setq wl-summary-highlight t)
601               (wl-highlight-summary-current-line number flags))
602             (forward-line 1)))))
603     (set-buffer-modified-p nil)))
604
605 (defun wl-summary-window-scroll-functions ()
606   (cond ((and wl-summary-lazy-highlight
607               wl-summary-lazy-update-mark)
608          (list 'wl-summary-update-mark-and-highlight-window))
609         (wl-summary-lazy-highlight
610          (list 'wl-highlight-summary-window))
611         (wl-summary-lazy-update-mark
612          (list 'wl-summary-update-mark-window))))
613
614 (defun wl-status-update ()
615   (interactive)
616   (wl-address-init))
617
618 (defun wl-summary-display-top ()
619   (interactive)
620   (goto-char (point-min))
621   (run-hooks 'wl-summary-buffer-window-scroll-functions)
622   (if wl-summary-buffer-disp-msg
623       (wl-summary-redisplay)))
624
625 (defun wl-summary-display-bottom ()
626   (interactive)
627   (goto-char (point-max))
628   (forward-line -1)
629   (run-hooks 'wl-summary-buffer-window-scroll-functions)
630   (if wl-summary-buffer-disp-msg
631       (wl-summary-redisplay)))
632
633 (defun wl-summary-count-unread ()
634   (let ((lst (elmo-folder-count-flags wl-summary-buffer-elmo-folder)))
635     (if (eq major-mode 'wl-summary-mode)
636         (setq wl-summary-buffer-new-count (car lst)
637               wl-summary-buffer-unread-count (nth 1 lst)
638               wl-summary-buffer-answered-count (nth 2 lst)))
639     lst))
640
641 (defun wl-summary-message-string (&optional use-cache)
642   "Return full body string of current message.
643 If optional USE-CACHE is non-nil, use cache if exists."
644   (let ((number (wl-summary-message-number))
645         (folder wl-summary-buffer-elmo-folder))
646     (if (null number)
647         (message "No message.")
648       (elmo-set-work-buf
649        (elmo-message-fetch folder
650                            number
651                            (elmo-make-fetch-strategy
652                             'entire
653                             use-cache ; use cache
654                             nil ; save cache (should `t'?)
655                             (and
656                              use-cache
657                              (elmo-file-cache-get-path
658                               (elmo-message-field folder number 'message-id))))
659                            nil
660                            (current-buffer)
661                            'unread)
662        (buffer-string)))))
663
664 (defun wl-summary-reedit (&optional arg)
665   "Re-edit current message.
666 If ARG is non-nil, Supersedes message"
667   (interactive "P")
668   (wl-summary-toggle-disp-msg 'off)
669   (cond
670    ((not (wl-summary-message-number))
671     (message "No message."))
672    (arg
673     (wl-summary-supersedes-message))
674    ((string= (wl-summary-buffer-folder-name) wl-draft-folder)
675     (wl-draft-reedit (wl-summary-message-number))
676     (if (wl-message-news-p)
677         (mail-position-on-field "Newsgroups")
678       (mail-position-on-field "To")))
679    (t
680     (wl-draft-edit-string (wl-summary-message-string)))))
681
682 (defun wl-summary-resend-bounced-mail ()
683   "Re-mail the current message.
684 This only makes sense if the current message is a bounce message which
685 contains some mail you have written but has been bounced back to
686 you."
687   (interactive)
688   (wl-summary-toggle-disp-msg 'off)
689   (save-excursion
690     (wl-summary-set-message-buffer-or-redisplay)
691     (set-buffer (wl-message-get-original-buffer))
692     (goto-char (point-min))
693     (let ((case-fold-search nil))
694       (cond
695        ((and
696          (re-search-forward
697           (concat "^\\($\\|[Cc]ontent-[Tt]ype:[ \t]+multipart/\\(report\\|mixed\\)\\)") nil t)
698          (not (bolp))
699          (re-search-forward "boundary=\"\\([^\"]+\\)\"" nil t))
700         (let ((boundary (buffer-substring (match-beginning 1) (match-end 1)))
701               start)
702           (cond
703            ((and (setq start (re-search-forward
704                               (concat "^--" boundary "\n"
705                                       "\\([Cc]ontent-[Dd]escription:.*\n\\)?"
706                                       "[Cc]ontent-[Tt]ype:[ \t]+"
707                                       "\\(message/rfc822\\|text/rfc822-headers\\).*\n"
708                                       "\\(.+\n\\)*\n") nil t))
709                  (re-search-forward
710                   (concat "\n\\(--" boundary "\\)--\n") nil t))
711             (wl-draft-edit-string (buffer-substring start (match-beginning 1))))
712            (t
713             (message "Seems no message/rfc822 part.")))))
714        ((let ((case-fold-search t))
715           (re-search-forward wl-rejected-letter-start nil t))
716         (skip-chars-forward " \t\n")
717         (wl-draft-edit-string (buffer-substring (point) (point-max))))
718        (t
719         (message "Does not appear to be a rejected letter."))))))
720
721 (defun wl-summary-detect-mark-position ()
722   (let ((column wl-summary-buffer-number-column)
723         (formatter wl-summary-buffer-line-formatter)
724         (dummy-temp (char-to-string 200))
725         ;; bind only for the check.
726         (wl-summary-new-uncached-mark (char-to-string 201))
727         (wl-summary-persistent-mark-priority-list '(new))     ; ditto.
728         (lang wl-summary-buffer-weekday-name-lang)
729         wl-summary-highlight
730         temp persistent)
731     (with-temp-buffer
732       (setq wl-summary-buffer-number-column column
733             wl-summary-buffer-line-formatter formatter
734             wl-summary-buffer-weekday-name-lang lang)
735       (insert
736        (wl-summary-create-line
737         (elmo-msgdb-make-message-entity
738          (luna-make-entity 'modb-entity-handler)
739          :number 10000
740          :from "foo"
741          :subject "bar"
742          :size 100)
743         nil
744         dummy-temp
745         '(new)
746         nil))
747       (goto-char (point-min))
748       (setq temp (save-excursion
749                    (when (search-forward dummy-temp nil t)
750                      (current-column)))
751             persistent
752             (save-excursion
753               (when (search-forward wl-summary-new-uncached-mark nil t)
754                 (current-column)))))
755     (setq wl-summary-buffer-temp-mark-column temp
756           wl-summary-buffer-persistent-mark-column persistent)))
757
758 (defun wl-summary-buffer-set-folder (folder)
759   (if (stringp folder)
760       (setq folder (wl-folder-get-elmo-folder folder)))
761   (setq wl-summary-buffer-elmo-folder folder)
762   (make-local-variable 'wl-message-buffer)
763   (setq wl-summary-buffer-mime-charset (or (wl-get-assoc-list-value
764                                             wl-folder-mime-charset-alist
765                                             (elmo-folder-name-internal folder))
766                                            wl-mime-charset))
767   (setq wl-summary-buffer-weekday-name-lang
768         (or (wl-get-assoc-list-value
769              wl-folder-weekday-name-lang-alist
770              (elmo-folder-name-internal folder))
771             wl-summary-weekday-name-lang))
772   (setq wl-summary-buffer-thread-indent-set
773         (wl-get-assoc-list-value
774          wl-folder-thread-indent-set-alist
775          (elmo-folder-name-internal folder)))
776   (setq wl-summary-buffer-number-column
777         (or (wl-get-assoc-list-value wl-summary-number-column-alist
778                                      (wl-summary-buffer-folder-name))
779             wl-summary-default-number-column))
780   (wl-line-formatter-setup
781    wl-summary-buffer-line-formatter
782    (setq wl-summary-buffer-line-format
783          (or (wl-get-assoc-list-value
784               wl-folder-summary-line-format-alist
785               (elmo-folder-name-internal folder))
786              wl-summary-line-format))
787    wl-summary-line-format-spec-alist)
788   (wl-line-formatter-setup
789    wl-summary-buffer-mode-line-formatter
790    wl-summary-mode-line-format
791    wl-summary-mode-line-format-spec-alist)
792   (setq wl-summary-buffer-persistent
793         (wl-folder-persistent-p (elmo-folder-name-internal folder)))
794   (elmo-folder-set-persistent-internal folder wl-summary-buffer-persistent)
795   ;; process duplicates.
796   (elmo-folder-set-process-duplicates-internal
797    folder (cdr (elmo-string-matched-assoc
798                 (elmo-folder-name-internal folder)
799                 wl-folder-process-duplicates-alist)))
800   (setq
801    wl-thread-indent-level-internal
802    (or (nth 0 wl-summary-buffer-thread-indent-set)
803        wl-thread-indent-level)
804    wl-thread-have-younger-brother-str-internal
805    (or (nth 1 wl-summary-buffer-thread-indent-set)
806        wl-thread-have-younger-brother-str)
807    wl-thread-youngest-child-str-internal
808    (or (nth 2 wl-summary-buffer-thread-indent-set)
809        wl-thread-youngest-child-str)
810    wl-thread-vertical-str-internal
811    (or (nth 3 wl-summary-buffer-thread-indent-set)
812        wl-thread-vertical-str)
813    wl-thread-horizontal-str-internal
814    (or (nth 4 wl-summary-buffer-thread-indent-set)
815        wl-thread-horizontal-str)
816    wl-thread-space-str-internal
817    (or (nth 5 wl-summary-buffer-thread-indent-set)
818        wl-thread-space-str))
819   (run-hooks 'wl-summary-buffer-set-folder-hook))
820
821 (defun wl-summary-mode ()
822   "Major mode for reading threaded messages.
823 See Info under Wanderlust for full documentation.
824
825 Special commands:
826 \\{wl-summary-mode-map}
827
828 Entering Folder mode calls the value of `wl-summary-mode-hook'."
829   (interactive)
830   (unless (interactive-p) (kill-all-local-variables))
831   (setq major-mode 'wl-summary-mode)
832   (setq mode-name "Summary")
833   (use-local-map wl-summary-mode-map)
834 ;;;(setq default-directory (or wl-tmp-dir (expand-file-name "~/")))
835   (setq buffer-read-only t)
836   (setq truncate-lines t)
837 ;;;(make-local-variable 'tab-width)
838 ;;;(setq tab-width 1)
839   (buffer-disable-undo (current-buffer))
840   (setq selective-display t
841         selective-display-ellipses nil)
842   (wl-mode-line-buffer-identification '(wl-summary-buffer-mode-line))
843   (easy-menu-add wl-summary-mode-menu)
844   (setq wl-summary-buffer-window-scroll-functions
845         (wl-summary-window-scroll-functions))
846   (when wl-summary-buffer-window-scroll-functions
847     (let ((hook (if wl-on-xemacs 'pre-idle-hook 'window-scroll-functions)))
848       (make-local-hook hook)
849       (dolist (function wl-summary-buffer-window-scroll-functions)
850         (add-hook hook function nil t))))
851   ;; This hook may contain the function `wl-setup-summary' for reasons
852   ;; of system internal to accord facilities for the Emacs variants.
853   (run-hooks 'wl-summary-mode-hook))
854
855 ;;;
856 (defun wl-summary-overview-entity-compare-by-size (x y)
857    "Compare entity X and Y by size."
858    (< (elmo-message-entity-field x 'size)
859       (elmo-message-entity-field y 'size)))
860
861
862 (defun wl-summary-overview-entity-compare-by-date (x y)
863   "Compare entity X and Y by date."
864   (condition-case nil
865       (string<
866        (timezone-make-date-sortable
867         (elmo-message-entity-field x 'date))
868        (timezone-make-date-sortable
869         (elmo-message-entity-field y 'date)))
870     (error))) ;; ignore error.
871
872 (defun wl-summary-overview-entity-compare-by-number (x y)
873    "Compare entity X and Y by number."
874   (<
875    (elmo-message-entity-number x)
876    (elmo-message-entity-number y)))
877
878 (defun wl-summary-overview-entity-compare-by-from (x y)
879   "Compare entity X and Y by from."
880   (string<
881    (wl-address-header-extract-address
882     (or (elmo-message-entity-field x 'from t)
883         wl-summary-no-from-message))
884    (wl-address-header-extract-address
885     (or (elmo-message-entity-field y 'from t)
886         wl-summary-no-from-message))))
887
888 (defun wl-summary-overview-entity-compare-by-subject (x y)
889   "Compare entity X and Y by subject."
890   (string< (elmo-message-entity-field x 'subject)
891            (elmo-message-entity-field y 'subject)))
892
893 (defun wl-summary-get-list-info (entity)
894   "Returns (\"ML-name\" . ML-count) of ENTITY."
895   (let (sequence ml-name ml-count subject return-path delivered-to mailing-list)
896     (setq sequence (elmo-message-entity-field entity 'x-sequence)
897           ml-name (or (elmo-message-entity-field entity 'x-ml-name)
898                       (and sequence
899                            (car (split-string sequence " "))))
900           ml-count (or (elmo-message-entity-field entity 'x-mail-count)
901                        (elmo-message-entity-field entity 'x-ml-count)
902                        (and sequence
903                             (cadr (split-string sequence " ")))))
904     (and (setq subject (elmo-message-entity-field entity 'subject t))
905          (setq subject (elmo-delete-char ?\n subject))
906          (string-match "^\\s(\\(\\S)+\\)[ :]\\([0-9]+\\)\\s)[ \t]*" subject)
907          (progn
908            (or ml-name (setq ml-name (match-string 1 subject)))
909            (or ml-count (setq ml-count (match-string 2 subject)))))
910     (and (setq return-path
911                (elmo-message-entity-field entity 'return-path))
912          (string-match "^<\\([^@>]+\\)-return-\\([0-9]+\\)-" return-path)
913          (progn
914            (or ml-name (setq ml-name (match-string 1 return-path)))
915            (or ml-count (setq ml-count (match-string 2 return-path)))))
916     (and (setq delivered-to
917                (elmo-message-entity-field entity 'delivered-to))
918          (string-match "^mailing list \\([^@]+\\)@" delivered-to)
919          (or ml-name (setq ml-name (match-string 1 delivered-to))))
920     (and (setq mailing-list
921                (elmo-message-entity-field entity 'mailing-list))
922          ;; *-help@, *-owner@, etc.
923          (string-match "\\(^\\|; \\)contact \\([^@]+\\)-[^-@]+@" mailing-list)
924          (or ml-name (setq ml-name (match-string 2 mailing-list))))
925     (cons (and ml-name (car (split-string ml-name " ")))
926           (and ml-count (string-to-int ml-count)))))
927
928 (defun wl-summary-overview-entity-compare-by-list-info (x y)
929   "Compare entity X and Y by mailing-list info."
930   (let* ((list-info-x (wl-summary-get-list-info x))
931          (list-info-y (wl-summary-get-list-info y)))
932     (if (equal (car list-info-x) (car list-info-y))
933         (if (equal (cdr list-info-x) (cdr list-info-y))
934             (wl-summary-overview-entity-compare-by-date x y)
935           (< (or (cdr list-info-x) 0)
936              (or (cdr list-info-y) 0)))
937       (string< (or (car list-info-x) "")
938                (or (car list-info-y) "")))))
939
940 (defun wl-summary-sort-by-date ()
941   (interactive)
942   (wl-summary-rescan "date"))
943 (defun wl-summary-sort-by-number ()
944   (interactive)
945   (wl-summary-rescan "number"))
946 (defun wl-summary-sort-by-subject ()
947   (interactive)
948   (wl-summary-rescan "subject"))
949 (defun wl-summary-sort-by-from ()
950   (interactive)
951   (wl-summary-rescan "from"))
952 (defun wl-summary-sort-by-list-info ()
953   (interactive)
954   (wl-summary-rescan "list-info"))
955
956 (defun wl-summary-rescan (&optional sort-by disable-killed disable-thread)
957   "Rescan current folder without updating."
958   (interactive)
959   (let ((elmo-mime-charset wl-summary-buffer-mime-charset)
960         i percent num
961         gc-message entity
962         curp
963         (inhibit-read-only t)
964         (buffer-read-only nil)
965         (numbers (elmo-folder-list-messages wl-summary-buffer-elmo-folder
966                                             (not disable-killed) t)) ; in-msgdb
967         (wl-thread-saved-entity-hashtb-internal (and (not disable-thread)
968                                                      wl-thread-entity-hashtb))
969         (wl-summary-search-parent-by-subject-regexp
970          (and disable-thread wl-summary-search-parent-by-subject-regexp))
971         (wl-summary-divide-thread-when-subject-changed
972          (and disable-thread wl-summary-divide-thread-when-subject-changed))
973         expunged)
974     (erase-buffer)
975     (message "Re-scanning...")
976     (setq i 0)
977     (when sort-by
978       (message "Sorting by %s..." sort-by)
979       (setq numbers
980             (sort numbers
981                   (lambda (x y)
982                     (funcall
983                      (intern (format "wl-summary-overview-entity-compare-by-%s"
984                                      sort-by))
985                      (elmo-message-entity wl-summary-buffer-elmo-folder x)
986                      (elmo-message-entity wl-summary-buffer-elmo-folder y)))))
987       (message "Sorting by %s...done" sort-by))
988     (setq num (length numbers))
989     (setq wl-thread-entity-hashtb (elmo-make-hash (* num 2))
990           wl-thread-entity-list nil
991           wl-thread-entities nil
992           wl-summary-scored nil
993           wl-summary-buffer-number-list nil
994           wl-summary-buffer-unsync-mark-number-list nil
995           wl-summary-buffer-target-mark-list nil
996           wl-summary-buffer-temp-mark-list nil
997           wl-summary-delayed-update nil)
998     (elmo-kill-buffer wl-summary-search-buf-name)
999     (while numbers
1000       (setq entity (elmo-message-entity wl-summary-buffer-elmo-folder
1001                                         (car numbers)))
1002       (wl-summary-insert-message entity
1003                                  wl-summary-buffer-elmo-folder
1004                                  nil)
1005       (setq numbers (cdr numbers))
1006       (when (> num elmo-display-progress-threshold)
1007         (setq i (+ i 1))
1008         (if (or (zerop (% i 5)) (= i num))
1009             (elmo-display-progress
1010              'wl-summary-rescan "Constructing summary structure..."
1011              (/ (* i 100) num)))))
1012     (when wl-summary-delayed-update
1013       (while wl-summary-delayed-update
1014         (message "Parent (%d) of message %d is no entity"
1015                  (caar wl-summary-delayed-update)
1016                  (elmo-message-entity-number
1017                   (cdar wl-summary-delayed-update)))
1018         (wl-summary-insert-message
1019          (cdar wl-summary-delayed-update)
1020          wl-summary-buffer-elmo-folder nil t)
1021         (setq wl-summary-delayed-update (cdr wl-summary-delayed-update))))
1022     (message "Constructing summary structure...done")
1023     (if (eq wl-summary-buffer-view 'thread)
1024         (progn
1025           (message "Inserting thread...")
1026           (wl-thread-insert-top)
1027           (message "Inserting thread...done")))
1028     (when wl-use-scoring
1029       (wl-summary-score-headers (wl-summary-rescore-msgs
1030                                  wl-summary-buffer-number-list)
1031                                 t)
1032       (when (and wl-summary-scored
1033                  (setq expunged (wl-summary-score-update-all-lines)))
1034         (message "%d message(s) are expunged by scoring." (length expunged))))
1035     (wl-summary-set-message-modified)
1036     (wl-summary-count-unread)
1037     (wl-summary-update-modeline)
1038     (goto-char (point-max))
1039     (forward-line -1)
1040     (set-buffer-modified-p nil)))
1041
1042 (defun wl-summary-next-folder-or-exit (&optional next-entity upward)
1043   (if (and next-entity
1044            wl-auto-select-next)
1045       (let (retval)
1046         (wl-summary-toggle-disp-msg 'off)
1047         (unwind-protect
1048             (setq retval
1049                   (wl-summary-goto-folder-subr next-entity
1050                                                'force-update
1051                                                nil
1052                                                nil ; not sticky
1053                                                t   ; interactive!
1054                                                ))
1055           (wl-folder-set-current-entity-id (wl-folder-get-entity-id next-entity))
1056           (if (and (eq retval 'more-next)
1057                    (memq wl-auto-select-next '(unread skip-no-unread))
1058                    (memq this-command wl-summary-next-no-unread-command))
1059               (if upward
1060                   (wl-summary-up
1061                    t (eq wl-auto-select-next 'skip-no-unread))
1062                 (goto-char (point-max))
1063                 (forward-line -1)
1064                 (wl-summary-down
1065                  t (eq wl-auto-select-next 'skip-no-unread))))))
1066     (wl-summary-exit)))
1067
1068 (defun wl-summary-entity-info-msg (entity finfo)
1069   (or (and entity
1070            (concat
1071             (if (memq 'ask-folder wl-use-folder-petname)
1072                 (wl-folder-get-petname entity)
1073               entity)
1074             (if (null (car finfo))
1075                 " (? new/? unread)"
1076               (format
1077                " (%d new/%d unread)"
1078                (nth 0 finfo)
1079                (+ (nth 0 finfo)
1080                   (nth 1 finfo))))))
1081       "folder mode"))
1082
1083 (defun wl-summary-set-message-modified ()
1084   (setq wl-summary-buffer-message-modified t))
1085 (defun wl-summary-message-modified-p ()
1086   wl-summary-buffer-message-modified)
1087 (defun wl-summary-set-thread-modified ()
1088   (setq wl-summary-buffer-thread-modified t))
1089 (defun wl-summary-thread-modified-p ()
1090   wl-summary-buffer-thread-modified)
1091
1092 (defsubst wl-summary-cleanup-temp-marks (&optional sticky)
1093   (when wl-summary-buffer-temp-mark-list
1094     (if (y-or-n-p (format "Execute remaining marks in %s? "
1095                           (wl-summary-buffer-folder-name)))
1096         (progn
1097           (wl-summary-exec)
1098           (if wl-summary-buffer-temp-mark-list
1099               (error "Some execution was failed")))
1100       ;; temp-mark-list is remained.
1101       (message "")))
1102   (wl-summary-delete-all-temp-marks 'no-msg)
1103   (setq wl-summary-scored nil))
1104
1105 ;; a subroutine for wl-summary-exit/wl-save-status
1106 ;; Note that folder is not commited here.
1107 (defun wl-summary-save-view ()
1108   ;; already in summary buffer.
1109   (when wl-summary-buffer-persistent
1110     ;; save the current summary buffer view.
1111     (if (and wl-summary-cache-use
1112              (or (wl-summary-message-modified-p)
1113                  (wl-summary-thread-modified-p)))
1114         (wl-summary-save-view-cache))))
1115
1116 (defun wl-summary-save-status ()
1117   "Save summary view and msgdb."
1118   (interactive)
1119   (if (interactive-p) (message "Saving summary status..."))
1120   (wl-summary-save-view)
1121   (elmo-folder-commit wl-summary-buffer-elmo-folder)
1122   (elmo-folder-check wl-summary-buffer-elmo-folder)
1123   (if wl-use-scoring (wl-score-save))
1124   (if (interactive-p) (message "Saving summary status...done")))
1125
1126 (defun wl-summary-force-exit ()
1127   "Exit current summary.  Buffer is deleted even the buffer is sticky."
1128   (interactive)
1129   (wl-summary-exit 'force-exit))
1130
1131 (defun wl-summary-exit (&optional force-exit)
1132   "Exit current summary.  if FORCE-EXIT, exits even the summary is sticky."
1133   (interactive "P")
1134   (let ((summary-buf (current-buffer))
1135         (sticky (wl-summary-sticky-p))
1136         summary-win
1137         message-buf message-win
1138         folder-buf folder-win)
1139     (run-hooks 'wl-summary-exit-pre-hook)
1140     (if wl-summary-buffer-exit-function
1141         (funcall wl-summary-buffer-exit-function)
1142       (if (or force-exit (not sticky))
1143           (wl-summary-cleanup-temp-marks sticky))
1144       (unwind-protect
1145           ;; save summary status
1146           (progn
1147             (wl-summary-save-view)
1148             (if (or force-exit (not sticky))
1149                 (elmo-folder-close wl-summary-buffer-elmo-folder)
1150               (elmo-folder-commit wl-summary-buffer-elmo-folder)
1151               (elmo-folder-check wl-summary-buffer-elmo-folder))
1152             (if wl-use-scoring (wl-score-save)))
1153         ;; for sticky summary
1154         (wl-delete-all-overlays)
1155         (setq wl-summary-buffer-disp-msg nil)
1156         (elmo-kill-buffer wl-summary-search-buf-name)
1157         ;; delete message window if displayed.
1158         (if (and wl-message-buffer (get-buffer-window wl-message-buffer))
1159             (delete-window (get-buffer-window wl-message-buffer)))
1160         (if (and wl-summary-use-frame
1161                  (> (length (visible-frame-list)) 1))
1162             (delete-frame))
1163         (if (setq folder-buf (get-buffer wl-folder-buffer-name))
1164             (if wl-summary-use-frame
1165                 (let (select-frame)
1166                   (save-selected-window
1167                     (dolist (frame (visible-frame-list))
1168                       (select-frame frame)
1169                       (if (get-buffer-window folder-buf)
1170                           (setq select-frame frame))))
1171                   (if select-frame
1172                       (select-frame select-frame)
1173                     (switch-to-buffer folder-buf)))
1174               (if (setq folder-win (get-buffer-window folder-buf))
1175                   ;; folder win is already displayed.
1176                   (select-window folder-win)
1177                 ;; folder win is not displayed.
1178                 (switch-to-buffer folder-buf)))
1179           ;; currently no folder buffer
1180           (wl-folder))
1181         (and wl-folder-move-cur-folder
1182              wl-folder-buffer-cur-point
1183              (goto-char wl-folder-buffer-cur-point))
1184         (setq wl-folder-buffer-cur-path nil)
1185         (setq wl-folder-buffer-last-visited-entity-id wl-folder-buffer-cur-entity-id)
1186         (setq wl-folder-buffer-cur-entity-id nil)
1187         (wl-delete-all-overlays)
1188         (if wl-summary-exit-next-move
1189             (wl-folder-next-unsync t)
1190           (beginning-of-line))
1191         (if (setq summary-win (get-buffer-window summary-buf))
1192             (delete-window summary-win))
1193         (if (or force-exit
1194                 (not sticky))
1195             (progn
1196               (set-buffer summary-buf)
1197               (kill-buffer summary-buf)))
1198         (run-hooks 'wl-summary-exit-hook)))))
1199
1200 (defun wl-summary-suspend ()
1201   (interactive)
1202   (wl-summary-exit)
1203   (wl-folder-suspend))
1204
1205 (defun wl-summary-sync-force-update (&optional unset-cursor no-check)
1206   (interactive)
1207   (wl-summary-sync-update unset-cursor nil nil no-check))
1208
1209 (defsubst wl-summary-sync-all-init ()
1210   (wl-summary-cleanup-temp-marks)
1211   (erase-buffer)
1212   (wl-summary-set-message-modified)
1213   (setq wl-thread-entity-hashtb (elmo-make-hash
1214                                  (* (elmo-folder-length
1215                                      wl-summary-buffer-elmo-folder)
1216                                     2)))
1217   (setq wl-thread-entity-list nil)
1218   (setq wl-thread-entities nil)
1219   (setq wl-summary-buffer-number-list nil)
1220   (setq wl-summary-buffer-target-mark-list nil)
1221   (setq wl-summary-buffer-temp-mark-list nil))
1222
1223 (defun wl-summary-sync (&optional unset-cursor force-range)
1224   (interactive)
1225   (let* ((folder wl-summary-buffer-elmo-folder)
1226          (inhibit-read-only t)
1227          (buffer-read-only nil)
1228          (msgdb-dir (elmo-folder-msgdb-path folder))
1229          (range (or force-range (wl-summary-input-range
1230                                  (elmo-folder-name-internal folder)))))
1231     (cond ((string-match "rescan" range)
1232            (let ((msg (wl-summary-message-number))
1233                  (wl-use-scoring (if (string-match "noscore" range)
1234                                      nil
1235                                    wl-use-scoring)))
1236              (wl-summary-rescan nil
1237                                 (string-match "noscore" range)
1238                                 (string-match "thread" range))
1239              (and msg (wl-summary-jump-to-msg msg))))
1240           ((string= range "mark")
1241            (let ((msg (wl-summary-message-number)))
1242              (call-interactively 'wl-summary-sync-marks)
1243              (and msg (wl-summary-jump-to-msg msg))))
1244           ((string= range "cache-status")
1245            (let ((msg (wl-summary-message-number)))
1246              (wl-summary-resume-cache-status)
1247              (and msg (wl-summary-jump-to-msg msg))))
1248           ((string= range "no-sync"))
1249           ((or (string-match "^last:" range)
1250                (string-match "^first:" range))
1251            (wl-summary-goto-folder-subr (concat "/" range "/"
1252                                                 (elmo-folder-name-internal
1253                                                  folder))
1254                                         'force-update nil nil t))
1255           (t
1256            (wl-summary-sync-update unset-cursor
1257                                    (string-match "entirely" range)
1258                                    (string-match "all" range))))))
1259
1260 (defvar wl-summary-edit-addresses-candidate-fields
1261   ;; First element becomes default.
1262   '("from" "to" "cc"))
1263
1264 (defun wl-summary-edit-addresses-collect-candidate-fields (mime-charset)
1265   (let ((fields wl-summary-edit-addresses-candidate-fields)
1266         body candidates components)
1267     (while fields
1268       (setq body
1269             (mapconcat 'identity (elmo-multiple-field-body (car fields))
1270                        ","))
1271       (setq body (wl-parse-addresses body))
1272       (if body (setq candidates (append candidates body)))
1273       (setq fields (cdr fields)))
1274     (setq candidates (elmo-uniq-list candidates))
1275     (elmo-set-work-buf
1276      (set-buffer-multibyte default-enable-multibyte-characters)
1277      (mapcar (function
1278               (lambda (x)
1279                 (setq components (std11-extract-address-components x))
1280                 (cons (nth 1 components)
1281                       (and (car components)
1282                            (eword-decode-string
1283                             (decode-mime-charset-string
1284                              (car components)
1285                              mime-charset))))))
1286              candidates))))
1287
1288 (defun wl-summary-edit-addresses-subr (the-email name-in-addr)
1289   ;; returns nil if there's no change.
1290   (if (elmo-get-hash-val (downcase the-email) wl-address-petname-hash)
1291       (let (char)
1292         (message "'%s' already exists. (e)dit/(d)elete/(c)ancel?"
1293                  the-email)
1294         (while (not (or (eq (setq char (read-char)) ?\r)
1295                         (eq char ?\n)
1296                         (eq char ? )
1297                         (eq char ?e)
1298                         (eq char ?c)
1299                         (eq char ?d)))
1300           (message
1301            "Please answer `e' or `d' or `c'. (e)dit/(d)elete/(c)ancel?"))
1302         (cond
1303          ((or (eq char ?e)
1304               (eq char ?\n)
1305               (eq char ?\r)
1306               (eq char ? ))
1307           ;; Change Addresses
1308           (wl-address-add-or-change
1309            the-email
1310            (wl-address-header-extract-realname
1311             (cdr (assoc
1312                   (let ((completion-ignore-case t) comp)
1313                     (setq comp
1314                           (try-completion the-email wl-address-completion-list))
1315                     (if (equal comp t) the-email comp))
1316                   wl-address-completion-list))))
1317           "edited")
1318          ((eq char ?d)
1319           ;; Delete Addresses
1320           (if (y-or-n-p (format "Delete '%s'? "
1321                                 the-email))
1322               (progn
1323                 (wl-address-delete the-email)
1324                 "deleted")
1325             (message "")
1326             nil))
1327          (t (message "")
1328             nil)))
1329     ;; Add Petname
1330     (wl-address-add-or-change the-email name-in-addr)
1331     "added"))
1332
1333 (defun wl-summary-edit-addresses (&optional addr-str)
1334   "Edit address book interactively.
1335 Optional argument ADDR-STR is used as a target address if specified."
1336   (interactive (if current-prefix-arg
1337                    (list (read-from-minibuffer "Target address: "))))
1338   (if (null (wl-summary-message-number))
1339       (message "No message.")
1340     (save-excursion
1341       (let* ((charset wl-summary-buffer-mime-charset)
1342              (candidates
1343               (with-current-buffer (wl-summary-get-original-buffer)
1344                 (wl-summary-edit-addresses-collect-candidate-fields
1345                  charset)))
1346              address pair result)
1347         (if addr-str
1348             (setq address addr-str)
1349           (when candidates
1350             (setq address (car (car candidates)))
1351             (setq address
1352                   (completing-read
1353                    (format "Target address (%s): " address)
1354                    (mapcar
1355                     (function (lambda (x) (cons (car x) (car x))))
1356                     candidates)
1357                    nil nil nil nil address))))
1358         (when address
1359           (setq pair (assoc address candidates))
1360           (unless pair
1361             (setq pair (cons address nil)))
1362           (when (setq result (wl-summary-edit-addresses-subr (car pair) (cdr pair)))
1363             ;; update alias
1364             (wl-status-update)
1365             (setq address (assoc (car pair) wl-address-list))
1366             (if address
1367                 (message "%s, %s, <%s> is %s."
1368                          (nth 2 address)
1369                          (nth 1 address)
1370                          (nth 0 address)
1371                          result)))
1372 ;;; i'd like to update summary-buffer, but...
1373 ;;;     (wl-summary-rescan)
1374           (run-hooks 'wl-summary-edit-addresses-hook))))))
1375
1376 (defun wl-summary-incorporate (&optional arg)
1377   "Check and prefetch all uncached messages.
1378 If ARG is non-nil, checking is omitted."
1379   (interactive "P")
1380   (unless arg
1381     (save-excursion
1382       (wl-summary-sync-force-update)))
1383   (wl-summary-prefetch-region-no-mark (point-min) (point-max)
1384                                       wl-summary-incorporate-marks))
1385
1386 (defun wl-summary-prefetch-msg (number &optional arg)
1387   "Prefetch message and return non-nil value. If skipped, return nil."
1388   ;; prefetching procedure.
1389   (save-excursion
1390     (let* ((size (elmo-message-field wl-summary-buffer-elmo-folder
1391                                      number 'size))
1392            (file-cached (elmo-file-cache-exists-p
1393                          (elmo-message-field wl-summary-buffer-elmo-folder
1394                                              number 'message-id)))
1395            (force-read (and size
1396                             (or file-cached
1397                                 (and (null wl-prefetch-confirm) arg)
1398                                 (null wl-prefetch-threshold)
1399                                 (< size wl-prefetch-threshold))))
1400            mark new-mark)
1401       (ignore-errors
1402         (when (and (or arg (not file-cached))
1403                    size (not force-read) wl-prefetch-confirm)
1404           (let ((wl-message-entity (elmo-message-entity
1405                                     wl-summary-buffer-elmo-folder
1406                                     number)))
1407             (setq force-read
1408                   (save-restriction
1409                     (widen)
1410                     (y-or-n-p
1411                      (format
1412                       "Message from %s has %d bytes.  Prefetch it? "
1413                       (concat
1414                        "[ "
1415                        (save-match-data
1416                          (wl-set-string-width
1417                           17
1418                           (funcall
1419                            wl-summary-from-function
1420                            (elmo-delete-char
1421                             ?\"
1422                             (or
1423                              (elmo-message-entity-field
1424                               wl-message-entity
1425                               'from t)
1426                              "??")))))
1427                        " ]")
1428                       size))))
1429             (message "")))              ; flush.
1430         (if force-read
1431             (save-excursion
1432               (save-match-data
1433                 ;; online
1434                 (if (or arg (not file-cached))
1435                     (elmo-message-encache
1436                      wl-summary-buffer-elmo-folder
1437                      number))
1438                 (elmo-message-set-cached wl-summary-buffer-elmo-folder
1439                                          number t)
1440                 (when (and (wl-summary-jump-to-msg number)
1441                            (wl-summary-update-persistent-mark))
1442                   (sit-for 0)
1443                   (wl-summary-count-unread)
1444                   (wl-summary-update-modeline)
1445                   (wl-folder-update-unread
1446                    (wl-summary-buffer-folder-name)
1447                    (+ wl-summary-buffer-unread-count
1448                       wl-summary-buffer-new-count))))
1449               t)
1450           nil)))))
1451
1452 (defsubst wl-summary-narrow-to-region (beg end)
1453   (narrow-to-region
1454    (save-excursion
1455      (goto-char beg)
1456      (beginning-of-line)
1457      (point))
1458    (save-excursion
1459      (goto-char end)
1460      (if (eq (current-column) 0) (beginning-of-line) (end-of-line))
1461      (point))))
1462
1463 (defun wl-summary-prefetch-region-no-mark (beg end &optional prefetch-marks)
1464   (interactive "r")
1465   (let ((count 0)
1466         targets
1467         mark length
1468         entity msg
1469         start-pos pos)
1470     (save-excursion
1471       (setq start-pos (point))
1472       (save-restriction
1473         (wl-summary-narrow-to-region beg end)
1474         ;; collect prefetch targets.
1475         (message "Collecting marks...")
1476         (goto-char (point-min))
1477         (while (not (eobp))
1478           (setq mark (wl-summary-persistent-mark)
1479                 msg (wl-summary-message-number))
1480           (if (or (and (null prefetch-marks)
1481                        msg
1482                        (null (elmo-file-cache-exists-p
1483                               (elmo-message-field
1484                                wl-summary-buffer-elmo-folder
1485                                msg
1486                                'message-id))))
1487                   (member mark prefetch-marks))
1488               (setq targets (nconc targets (list msg))))
1489           (setq entity (wl-thread-get-entity msg))
1490           (if (or (not (eq wl-summary-buffer-view 'thread))
1491                   (wl-thread-entity-get-opened entity))
1492               (); opened. no hidden children.
1493             (setq targets (nconc
1494                            targets
1495                            (wl-thread-get-children-msgs-uncached
1496                             msg prefetch-marks))))
1497           (forward-line 1))
1498         (setq length (length targets))
1499         (message "Prefetching...")
1500         (while targets
1501           (when (if (not (wl-thread-entity-parent-invisible-p
1502                           (wl-thread-get-entity (car targets))))
1503                     (progn
1504                       (wl-summary-jump-to-msg (car targets))
1505                       (wl-summary-prefetch-msg
1506                        (wl-summary-message-number)))
1507                   (wl-summary-prefetch-msg (car targets)))
1508             (message "Prefetching... %d/%d message(s)"
1509                      (setq count (+ 1 count)) length))
1510           (setq targets (cdr targets)))
1511         (message "Prefetched %d/%d message(s)" count length)
1512         (cons count length)))))
1513
1514 (defun wl-summary-delete-marks-on-buffer (marks)
1515   (while marks
1516     (wl-summary-unmark (pop marks))))
1517
1518 (defun wl-summary-delete-copy-marks-on-buffer (copies)
1519   (wl-summary-delete-marks-on-buffer copies))
1520
1521 ;;;
1522 (defun wl-summary-delete-all-target-marks ()
1523   (wl-summary-delete-marks-on-buffer wl-summary-buffer-target-mark-list))
1524
1525 (defun wl-summary-mark-as-read-region (beg end)
1526   (interactive "r")
1527   (save-excursion
1528     (save-restriction
1529       (wl-summary-narrow-to-region beg end)
1530       (goto-char (point-min))
1531       (if (eq wl-summary-buffer-view 'thread)
1532           (let (number-list)
1533             (while (not (eobp))
1534               (let* ((number (wl-summary-message-number))
1535                      (entity (wl-thread-get-entity number)))
1536                 (if (wl-thread-entity-get-opened entity)
1537                     (setq number-list (append number-list (list number)))
1538                   (setq number-list
1539                         (append number-list
1540                                 (wl-thread-get-children-msgs number))))
1541                 (forward-line 1)))
1542             (wl-summary-mark-as-read number-list))
1543         (let (number-list)
1544           (while (not (eobp))
1545             (setq number-list
1546                   (append number-list (list (wl-summary-message-number))))
1547             (forward-line 1))
1548           (wl-summary-mark-as-read number-list))))))
1549
1550 (defun wl-summary-mark-as-unread-region (beg end)
1551   (interactive "r")
1552   (save-excursion
1553     (save-restriction
1554       (wl-summary-narrow-to-region beg end)
1555       (goto-char (point-min))
1556       (if (eq wl-summary-buffer-view 'thread)
1557           (let (number-list)
1558             (while (not (eobp))
1559               (let* ((number (wl-summary-message-number))
1560                      (entity (wl-thread-get-entity number)))
1561                 (if (wl-thread-entity-get-opened entity)
1562                     (setq number-list (append number-list (list number)))
1563                   (setq number-list
1564                         (append number-list
1565                                 (wl-thread-get-children-msgs number))))
1566                 (forward-line 1)))
1567             (wl-summary-mark-as-unread number-list))
1568         (let (number-list)
1569           (while (not (eobp))
1570             (setq number-list
1571                   (append number-list (list (wl-summary-message-number))))
1572             (forward-line 1))
1573           (wl-summary-mark-as-unread number-list))))))
1574
1575 (defun wl-summary-set-flags-region (beg end)
1576   (interactive "r")
1577   (save-excursion
1578     (save-restriction
1579       (wl-summary-narrow-to-region beg end)
1580       (goto-char (point-min))
1581       (let (flags)
1582         (if (eq wl-summary-buffer-view 'thread)
1583             (while (not (eobp))
1584               (let* ((number (wl-summary-message-number))
1585                      (entity (wl-thread-get-entity number))
1586                      children)
1587                 (if (wl-thread-entity-get-opened entity)
1588                     ;; opened...mark line.
1589                     ;; Crossposts are not processed
1590                     (setq flags (wl-summary-set-flags-internal
1591                                 number
1592                                 flags))
1593                   ;; closed
1594                   (setq flags (wl-summary-set-flags-internal
1595                               (wl-thread-get-children-msgs number)
1596                               flags)))
1597                 (forward-line 1)))
1598           (while (not (eobp))
1599             (setq flags (wl-summary-set-flags-internal
1600                         (wl-summary-message-number) flags))
1601             (forward-line 1))))))
1602   (wl-summary-count-unread)
1603   (wl-summary-update-modeline))
1604
1605 (defun wl-summary-mark-as-answered-region (beg end)
1606   (interactive "r")
1607   (save-excursion
1608     (save-restriction
1609       (wl-summary-narrow-to-region beg end)
1610       (goto-char (point-min))
1611       (let ((inverse (elmo-message-flagged-p wl-summary-buffer-elmo-folder
1612                                              (wl-summary-message-number)
1613                                              'answered)))
1614         (if (eq wl-summary-buffer-view 'thread)
1615             (while (not (eobp))
1616               (let* ((number (wl-summary-message-number))
1617                      (entity (wl-thread-get-entity number))
1618                      children)
1619                 (if (wl-thread-entity-get-opened entity)
1620                     ;; opened...mark line.
1621                     ;; Crossposts are not processed
1622                     (wl-summary-mark-as-answered-internal inverse)
1623                   ;; closed
1624                   (wl-summary-mark-as-answered-internal
1625                    inverse
1626                    (wl-thread-get-children-msgs number)))
1627                 (forward-line 1)))
1628           (while (not (eobp))
1629             (wl-summary-mark-as-answered-internal inverse)
1630             (forward-line 1))))))
1631   (wl-summary-count-unread)
1632   (wl-summary-update-modeline))
1633
1634 (defun wl-summary-mark-as-important-region (beg end)
1635   (interactive "r")
1636   (save-excursion
1637     (save-restriction
1638       (wl-summary-narrow-to-region beg end)
1639       (goto-char (point-min))
1640       (let ((inverse (elmo-message-flagged-p wl-summary-buffer-elmo-folder
1641                                              (wl-summary-message-number)
1642                                              'important)))
1643         (if (eq wl-summary-buffer-view 'thread)
1644             (while (not (eobp))
1645               (let* ((number (wl-summary-message-number))
1646                      (entity (wl-thread-get-entity number))
1647                      children)
1648                 (if (wl-thread-entity-get-opened entity)
1649                     ;; opened...mark line.
1650                     ;; Crossposts are not processed
1651                     (wl-summary-mark-as-important-internal inverse)
1652                   ;; closed
1653                   (wl-summary-mark-as-important-internal
1654                    inverse
1655                    (wl-thread-get-children-msgs number)))
1656                 (forward-line 1)))
1657           (while (not (eobp))
1658             (wl-summary-mark-as-important-internal inverse)
1659             (forward-line 1))))))
1660   (wl-summary-count-unread)
1661   (wl-summary-update-modeline))
1662
1663 (defun wl-summary-mark-as-read-all ()
1664   (interactive)
1665   (if (or (not (interactive-p))
1666           (y-or-n-p "Mark all messages as read? "))
1667       (let ((folder wl-summary-buffer-elmo-folder)
1668             (cur-buf (current-buffer)))
1669         (message "Setting all msgs as read...")
1670         (elmo-folder-set-flag
1671          folder
1672          (elmo-folder-list-flagged folder 'unread 'in-msgdb)
1673          'read)
1674         (save-excursion
1675           (goto-char (point-min))
1676           (while (not (eobp))
1677             (wl-summary-update-persistent-mark)
1678             (forward-line 1)))
1679         (wl-folder-update-unread (wl-summary-buffer-folder-name) 0)
1680         (setq wl-summary-buffer-unread-count 0)
1681         (setq wl-summary-buffer-new-count    0)
1682         (wl-summary-update-modeline)
1683         (message "Setting all msgs as read...done"))))
1684
1685 (defun wl-summary-delete-cache ()
1686   "Delete cache of current message."
1687   (interactive)
1688   (save-excursion
1689     (let* ((folder wl-summary-buffer-elmo-folder)
1690            number)
1691       (setq number (wl-summary-message-number))
1692       (elmo-message-set-cached folder number nil)
1693       (when (wl-summary-update-persistent-mark)
1694         (elmo-file-cache-delete
1695          (elmo-file-cache-get-path
1696           (elmo-message-field wl-summary-buffer-elmo-folder
1697                               number
1698                               'message-id)))))))
1699
1700 (defun wl-summary-resume-cache-status ()
1701   "Resume the cache status of all messages in the current folder."
1702   (interactive)
1703   (let ((folder wl-summary-buffer-elmo-folder)
1704         number msgid)
1705     (message "Resuming cache status...")
1706     (save-excursion
1707       (goto-char (point-min))
1708       (while (not (eobp))
1709         (setq number (wl-summary-message-number))
1710         (setq msgid (elmo-message-field folder number 'message-id))
1711         (elmo-message-set-cached folder number
1712                                  (elmo-file-cache-exists-p msgid))
1713         (wl-summary-update-persistent-mark)
1714         (forward-line 1))
1715       (wl-summary-count-unread)
1716       (wl-summary-update-modeline)
1717       (message "Resuming cache status...done"))))
1718
1719 (defun wl-summary-delete-messages-on-buffer (msgs &optional deleting-info)
1720   (interactive)
1721   (save-excursion
1722     (let ((inhibit-read-only t)
1723           (buffer-read-only nil)
1724           (msgs2 msgs)
1725           (len (length msgs))
1726           (i 0)
1727           ;(deleting-info (or deleting-info "Deleting..."))
1728           update-list)
1729       (elmo-kill-buffer wl-summary-search-buf-name)
1730       (while msgs
1731         (if (eq wl-summary-buffer-view 'thread)
1732             (progn
1733               ;; don't use wl-append(nconc), because list is broken. ...why?
1734               (setq update-list
1735                     (append update-list
1736                             (wl-thread-delete-message (car msgs))))
1737               (setq update-list (delq (car msgs) update-list)))
1738           (goto-char (point-min))
1739           (if (wl-summary-jump-to-msg (car msgs))
1740               (progn
1741                 (delete-region (point-at-bol) (point-at-eol))
1742                 (delete-char 1) ; delete '\n'
1743                 (setq wl-summary-buffer-number-list
1744                       (delq (car msgs) wl-summary-buffer-number-list)))))
1745 ;       (when (> len elmo-display-progress-threshold)
1746 ;         (setq i (1+ i))
1747 ;         (if (or (zerop (% i 5)) (= i len))
1748 ;             (elmo-display-progress
1749 ;              'wl-summary-delete-messages-on-buffer deleting-info
1750 ;              (/ (* i 100) len))))
1751         (setq msgs (cdr msgs)))
1752       (when (eq wl-summary-buffer-view 'thread)
1753         (wl-thread-update-line-msgs (elmo-uniq-list update-list))
1754         (wl-thread-cleanup-symbols msgs2))
1755       ;;(message (concat deleting-info "done"))
1756       (wl-summary-count-unread)
1757       (wl-summary-update-modeline)
1758       (wl-summary-folder-info-update))))
1759
1760 (defun wl-summary-update-status-marks (beg end &optional check)
1761   "Synchronize status marks on current buffer to the msgdb."
1762   (interactive "r")
1763   (save-excursion
1764     (goto-char beg)
1765     (while (and (< (point) end) (not (eobp)))
1766       (when (or (not check)
1767                 (let ((number (wl-summary-message-number)))
1768                   (when (memq number wl-summary-buffer-unsync-mark-number-list)
1769                     (setq wl-summary-buffer-unsync-mark-number-list
1770                           (delq number
1771                                 wl-summary-buffer-unsync-mark-number-list))
1772                     t)))
1773         (wl-summary-update-persistent-mark))
1774       (forward-line 1))))
1775
1776 (defun wl-summary-update-mark-window (&optional win beg)
1777   "Update persistent mark in visible summary window.
1778 This function is defined for `window-scroll-functions'"
1779   (with-current-buffer (window-buffer win)
1780     (when (eq major-mode 'wl-summary-mode)
1781       (let ((start (window-start win))
1782             (end (condition-case nil
1783                      (window-end win t) ; old emacsen doesn't support 2nd arg.
1784                    (error (window-end win)))))
1785         (wl-summary-update-status-marks start end 'check)))))
1786
1787 (defun wl-summary-insert-message (&rest args)
1788   (if (eq wl-summary-buffer-view 'thread)
1789       (apply 'wl-summary-insert-thread args)
1790     (apply 'wl-summary-insert-sequential args)))
1791
1792 (defun wl-summary-sort ()
1793   (interactive)
1794   (wl-summary-rescan
1795    (completing-read
1796     (format "Sort by (%s): " (symbol-name wl-summary-default-sort-spec))
1797     (mapcar (lambda (spec)
1798               (list (symbol-name spec)))
1799             wl-summary-sort-specs)
1800     nil t nil nil (symbol-name wl-summary-default-sort-spec))))
1801
1802 (defun wl-summary-sync-marks ()
1803   "Update persistent marks in summary."
1804   (interactive)
1805   (let ((mes "Updated ")
1806         diff diffs)
1807     ;; synchronize marks.
1808     (when (not (eq (elmo-folder-type-internal
1809                     wl-summary-buffer-elmo-folder)
1810                    'internal))
1811
1812       (message "Updating marks...")
1813       (dolist (flag elmo-global-flag-list)
1814         (unless (memq flag '(answered cached new unread))
1815           (setq diff (elmo-list-diff (elmo-folder-list-flagged
1816                                       wl-summary-buffer-elmo-folder
1817                                       flag)
1818                                      (elmo-folder-list-flagged
1819                                       wl-summary-buffer-elmo-folder
1820                                       flag 'in-msgdb)))
1821           (setq diffs (cadr diff)) ; deletes
1822           (setq mes (concat mes (format "-%d" (length diffs))))
1823           (while diffs
1824             (wl-summary-remove-flags-internal (car diffs)
1825                                              (list flag) 'no-server)
1826             (setq diffs (cdr diffs)))
1827           (setq diffs (car diff)) ; appends
1828           (setq mes (concat mes (format "/+%d %s," (length diffs) flag)))
1829           (while diffs
1830             (wl-summary-add-flags-internal (car diffs)
1831                                           (list flag) 'no-server)
1832             (setq diffs (cdr diffs)))))
1833
1834       (setq diff (elmo-list-diff (elmo-folder-list-flagged
1835                                   wl-summary-buffer-elmo-folder
1836                                   'answered)
1837                                  (elmo-folder-list-flagged
1838                                   wl-summary-buffer-elmo-folder
1839                                   'answered 'in-msgdb)))
1840       (setq diffs (cadr diff))
1841       (setq mes (concat mes (format "-%d" (length diffs))))
1842       (while diffs
1843         (wl-summary-mark-as-unanswered (car diffs) 'no-modeline)
1844         (setq diffs (cdr diffs)))
1845       (setq diffs (car diff)) ; unread-appends
1846       (setq mes (concat mes (format "/+%d answered," (length diffs))))
1847       (while diffs
1848         (wl-summary-mark-as-answered (car diffs) 'no-modeline)
1849         (setq diffs (cdr diffs)))
1850
1851       (setq diff (elmo-list-diff (elmo-folder-list-flagged
1852                                   wl-summary-buffer-elmo-folder
1853                                   'unread)
1854                                  (elmo-folder-list-flagged
1855                                   wl-summary-buffer-elmo-folder
1856                                   'unread 'in-msgdb)))
1857       (setq diffs (cadr diff))
1858       (setq mes (concat mes (format "-%d" (length diffs))))
1859       (while diffs
1860         (wl-summary-mark-as-read (car diffs) 'no-folder 'no-modeline)
1861         (setq diffs (cdr diffs)))
1862       (setq diffs (car diff)) ; unread-appends
1863       (setq mes (concat mes (format "/+%d unread." (length diffs))))
1864       (while diffs
1865         (wl-summary-mark-as-unread (car diffs) 'no-folder 'no-modeline)
1866         (setq diffs (cdr diffs)))
1867       (if (interactive-p) (message "%s" mes)))))
1868
1869 (defun wl-summary-sync-update (&optional unset-cursor
1870                                          disable-killed
1871                                          sync-all
1872                                          no-check)
1873   "Update the summary view to the newest folder status."
1874   (interactive)
1875   (let* ((folder wl-summary-buffer-elmo-folder)
1876          (elmo-mime-charset wl-summary-buffer-mime-charset)
1877          (inhibit-read-only t)
1878          (buffer-read-only nil)
1879          gc-message
1880          overview
1881          curp num i diff
1882          append-list delete-list crossed
1883          update-thread update-top-list
1884          expunged mes entity)
1885     (unwind-protect
1886         (progn
1887           (unless wl-summary-buffer-elmo-folder
1888             (error "(Internal error) Folder is not set:%s" (buffer-name
1889                                                             (current-buffer))))
1890           ;; Flush pending append operations (disconnected operation).
1891           ;;(setq seen-list
1892           ;;(wl-summary-flush-pending-append-operations seen-list))
1893           (goto-char (point-max))
1894           (wl-folder-confirm-existence folder (elmo-folder-plugged-p folder))
1895           (setq crossed (elmo-folder-synchronize folder
1896                                                  disable-killed
1897                                                  sync-all
1898                                                  no-check))
1899           (if crossed
1900               (progn
1901                 ;; Setup sync-all
1902                 (if sync-all (wl-summary-sync-all-init))
1903                 (setq diff (elmo-list-diff (elmo-folder-list-messages
1904                                             folder
1905                                             (not disable-killed)
1906                                             'in-msgdb)
1907                                            wl-summary-buffer-number-list))
1908                 (setq append-list (car diff))
1909                 (setq delete-list (cadr diff))
1910
1911                 (when delete-list
1912                   (wl-summary-delete-messages-on-buffer delete-list))
1913                 (unless wl-summary-lazy-update-mark
1914                   (wl-summary-update-status-marks (point-min) (point-max)))
1915                 (when (and wl-summary-lazy-highlight
1916                            wl-summary-lazy-update-mark)
1917                   (let (buffer-read-only)
1918                     (put-text-property (point-min) (point-max) 'face nil))
1919                   (run-hooks 'wl-summary-buffer-window-scroll-functions))
1920                 (setq num (length append-list))
1921                 (setq i 0)
1922                 (setq wl-summary-delayed-update nil)
1923                 (elmo-kill-buffer wl-summary-search-buf-name)
1924                 (dolist (number append-list)
1925                   (setq entity (elmo-message-entity folder number))
1926                   (when (setq update-thread
1927                               (wl-summary-insert-message
1928                                entity folder
1929                                (not sync-all)))
1930                     (wl-append update-top-list update-thread))
1931                   (if elmo-use-database
1932                       (elmo-database-msgid-put
1933                        (car entity) (elmo-folder-name-internal folder)
1934                        (elmo-message-entity-number entity)))
1935                   (when (> num elmo-display-progress-threshold)
1936                     (setq i (+ i 1))
1937                     (if (or (zerop (% i 5)) (= i num))
1938                         (elmo-display-progress
1939                          'wl-summary-sync-update
1940                          (if (eq wl-summary-buffer-view 'thread)
1941                              "Making thread..."
1942                            "Inserting message...")
1943                          (/ (* i 100) num)))))
1944                 (when wl-summary-delayed-update
1945                   (while wl-summary-delayed-update
1946                     (message "Parent (%d) of message %d is no entity"
1947                              (caar wl-summary-delayed-update)
1948                              (elmo-message-entity-number
1949                               (cdar wl-summary-delayed-update)))
1950                     (when (setq update-thread
1951                                 (wl-summary-insert-message
1952                                  (cdar wl-summary-delayed-update)
1953                                  wl-summary-buffer-elmo-folder
1954                                  (not sync-all) t))
1955                       (wl-append update-top-list update-thread))
1956                     (setq wl-summary-delayed-update
1957                           (cdr wl-summary-delayed-update))))
1958                 (when (and (eq wl-summary-buffer-view 'thread)
1959                            update-top-list)
1960                   (wl-thread-update-indent-string-thread
1961                    (elmo-uniq-list update-top-list)))
1962                 (message (if (eq wl-summary-buffer-view 'thread)
1963                              "Making thread...done"
1964                            "Inserting message...done"))
1965                 (when (or delete-list append-list)
1966                   (wl-summary-set-message-modified))
1967                 (when (and sync-all (eq wl-summary-buffer-view 'thread))
1968                   (elmo-kill-buffer wl-summary-search-buf-name)
1969                   (message "Inserting message...")
1970                   (wl-thread-insert-top)
1971                   (message "Inserting message...done"))
1972                 (if elmo-use-database
1973                     (elmo-database-close))
1974                 (run-hooks 'wl-summary-sync-updated-hook)
1975                 (setq mes
1976                       (if (and (eq (length delete-list) 0)
1977                                (eq num 0))
1978                           (format
1979                            "No updates for \"%s\"" (elmo-folder-name-internal
1980                                                     folder))
1981                         (format "Updated (-%d/+%d) message(s)"
1982                                 (length delete-list) num))))
1983             (setq mes "Quit updating")))
1984       ;; synchronize marks.
1985       (if (and crossed wl-summary-auto-sync-marks)
1986           (wl-summary-sync-marks))
1987       ;; scoring
1988       (when wl-use-scoring
1989         (setq wl-summary-scored nil)
1990         (wl-summary-score-headers (and sync-all
1991                                        (wl-summary-rescore-msgs
1992                                         wl-summary-buffer-number-list))
1993                                   sync-all)
1994         (when (and wl-summary-scored
1995                    (setq expunged (wl-summary-score-update-all-lines)))
1996           (setq mes (concat mes
1997                             (format " (%d expunged)"
1998                                     (length expunged))))))
1999       (if (and crossed (> crossed 0))
2000           (setq mes
2001                 (if mes
2002                     (concat mes
2003                             (format " (%d crosspost)" crossed))
2004                   (format "%d crosspost message(s)" crossed)))
2005         (and mes (setq mes (concat mes "."))))
2006       ;; Update Folder mode
2007       (wl-folder-set-folder-updated
2008        (elmo-folder-name-internal folder)
2009        (list 0
2010              (let ((lst (wl-summary-count-unread)))
2011                (+ (car lst) (nth 1 lst)))
2012              (elmo-folder-length folder)))
2013       (wl-summary-update-modeline)
2014       ;;
2015       (unless unset-cursor
2016         (goto-char (point-min))
2017         (if (not (wl-summary-cursor-down t))
2018             (progn
2019               (goto-char (point-max))
2020               (forward-line -1))
2021           (if (and wl-summary-highlight
2022                    (not wl-summary-lazy-highlight)
2023                    (not (get-text-property (point) 'face)))
2024               (save-excursion
2025                 (forward-line (- 0
2026                                  (or
2027                                   wl-summary-partial-highlight-above-lines
2028                                   wl-summary-highlight-partial-threshold)))
2029                 (wl-highlight-summary (point) (point-max))))))
2030       (wl-delete-all-overlays)
2031       (set-buffer-modified-p nil)
2032       (if mes (message "%s" mes)))))
2033
2034 (defun wl-summary-set-score-mark (mark)
2035   (save-excursion
2036     (beginning-of-line)
2037     (let ((cur-mark (wl-summary-temp-mark)))
2038       (when (member cur-mark (list " "
2039                                    wl-summary-score-below-mark
2040                                    wl-summary-score-over-mark))
2041         (wl-summary-put-temp-mark mark)
2042         (if wl-summary-highlight
2043             (wl-highlight-summary-current-line))
2044         (set-buffer-modified-p nil)))))
2045
2046 (defun wl-summary-get-score-mark (msg-num)
2047   (let ((score (cdr (assq msg-num wl-summary-scored))))
2048     (if score
2049         (cond ((< score wl-summary-default-score)
2050                "-")
2051               ((> score wl-summary-default-score)
2052                "+")))))
2053
2054 (defun wl-summary-update-modeline ()
2055   (setq wl-summary-buffer-mode-line
2056         (funcall wl-summary-buffer-mode-line-formatter)))
2057
2058 (defun wl-summary-jump-to-msg (&optional number)
2059   (interactive)
2060   (let ((num (or number
2061                  (string-to-int
2062                   (read-from-minibuffer "Jump to Message(No.): ")))))
2063     (setq num (int-to-string num))
2064     (beginning-of-line)
2065     (if (or (and (re-search-forward (concat "\r" num "[^0-9]") nil t)
2066                  (progn (backward-char 1) t))
2067             (re-search-backward (concat "\r" num "[^0-9]") nil t))
2068         (progn (beginning-of-line) t)
2069       nil)))
2070
2071 (defun wl-summary-highlight-msgs (msgs)
2072   (save-excursion
2073     (let ((len (length msgs))
2074           i)
2075       (message "Hilighting...")
2076       (setq i 0)
2077       (while msgs
2078         (if (wl-summary-jump-to-msg (car msgs))
2079             (wl-highlight-summary-current-line))
2080         (setq msgs (cdr msgs))
2081         (when (> len elmo-display-progress-threshold)
2082           (setq i (+ i 1))
2083           (if (or (zerop (% i 5)) (= i len))
2084               (elmo-display-progress
2085                'wl-summary-highlight-msgs "Highlighting..."
2086                (/ (* i 100) len)))))
2087       (message "Highlighting...done"))))
2088
2089 (defun wl-summary-message-number ()
2090   (save-excursion
2091     (beginning-of-line)
2092     (if (or (re-search-forward "\r\\(-?[0-9]+\\)" (point-at-eol) t)
2093             (re-search-forward "^ *\\(-?[0-9]+\\)" (point-at-eol) t))
2094         (string-to-int (wl-match-buffer 1))
2095       nil)))
2096
2097 (defun wl-summary-delete-all-msgs ()
2098   (interactive)
2099   (let ((cur-buf (current-buffer))
2100         (dels (elmo-folder-list-messages wl-summary-buffer-elmo-folder)))
2101     (set-buffer cur-buf)
2102     (if (null dels)
2103         (message "No message to delete.")
2104       (if (y-or-n-p (format "%s has %d message(s).  Delete all? "
2105                             (wl-summary-buffer-folder-name)
2106                             (length dels)))
2107           (progn
2108             (message "Deleting...")
2109             (elmo-folder-move-messages wl-summary-buffer-elmo-folder dels
2110                                        'null)
2111             (wl-summary-set-message-modified)
2112             (wl-folder-set-folder-updated (wl-summary-buffer-folder-name)
2113                                           (list 0 0 0))
2114 ;;; for thread.
2115 ;;;         (setq wl-thread-top-entity '(nil t nil nil))
2116             (setq wl-summary-buffer-unread-count 0)
2117             (setq wl-summary-buffer-new-count    0)
2118             (wl-summary-update-modeline)
2119             (set-buffer cur-buf)
2120             (let ((inhibit-read-only t)
2121                   (buffer-read-only nil))
2122               (erase-buffer))
2123 ;;;         (if wl-summary-cache-use (wl-summary-save-view-cache))
2124             (message "Deleting...done")
2125             t)
2126         nil))))
2127
2128 (defun wl-summary-toggle-thread (&optional arg)
2129   "Toggle thread status (T)hread and (S)equential.
2130 If ARG, without confirm."
2131   (interactive "P")
2132   (when (or arg
2133             (y-or-n-p (format "Toggle threading? (y=%s): "
2134                               (if (eq wl-summary-buffer-view 'thread)
2135                                   "\"off\"" "\"on\""))))
2136     (if (eq wl-summary-buffer-view 'thread)
2137         (setq wl-summary-buffer-view 'sequence)
2138       (setq wl-summary-buffer-view 'thread))
2139     (wl-summary-update-modeline)
2140     (force-mode-line-update)
2141     (wl-summary-rescan nil nil t)))
2142
2143 (defun wl-summary-load-file-object (filename)
2144   "Load lisp object from dir."
2145   (save-excursion
2146     (let ((tmp-buffer (get-buffer-create " *wl-summary-load-file-object*"))
2147           insert-file-contents-pre-hook   ; To avoid autoconv-xmas...
2148           insert-file-contents-post-hook
2149           ret-val)
2150       (if (not (file-readable-p filename))
2151           ()
2152         (set-buffer tmp-buffer)
2153         (as-binary-input-file (insert-file-contents filename))
2154         (setq ret-val
2155               (condition-case nil
2156                   (read (current-buffer))
2157                 (error (error "Reading failed")))))
2158       (kill-buffer tmp-buffer)
2159       ret-val)))
2160
2161 (defun wl-summary-goto-folder (&optional arg)
2162   (interactive "P")
2163   (wl-summary-goto-folder-subr nil nil nil nil t nil arg))
2164
2165 (defun wl-summary-goto-folder-sticky ()
2166   (interactive)
2167   (wl-summary-goto-folder-subr nil nil nil t t))
2168
2169 (defun wl-summary-goto-last-visited-folder ()
2170   (interactive)
2171   (let ((entity
2172          (wl-folder-search-entity-by-name wl-summary-last-visited-folder
2173                                           wl-folder-entity
2174                                           'folder)))
2175     (if entity (wl-folder-set-current-entity-id
2176                 (wl-folder-get-entity-id entity))))
2177   (wl-summary-goto-folder-subr wl-summary-last-visited-folder nil nil nil t))
2178
2179 (defun wl-summary-sticky-p (&optional folder)
2180   (if folder
2181       (get-buffer (wl-summary-sticky-buffer-name
2182                    (elmo-folder-name-internal folder)))
2183     (not (string= wl-summary-buffer-name (buffer-name)))))
2184
2185 (defun wl-summary-always-sticky-folder-p (folder)
2186   (or (eq t wl-summary-always-sticky-folder-list)
2187       (wl-string-match-member
2188        (elmo-folder-name-internal folder)
2189        wl-summary-always-sticky-folder-list)))
2190
2191 (defun wl-summary-stick (&optional force)
2192   "Make current summary buffer sticky."
2193   (interactive "P")
2194   (if (wl-summary-sticky-p)
2195       (message "Current summary buffer is already sticky.")
2196     (when (or force (y-or-n-p "Stick current summary buffer? "))
2197       (wl-summary-toggle-disp-msg 'off)
2198       (wl-summary-switch-to-clone-buffer
2199        (wl-summary-sticky-buffer-name
2200         (wl-summary-buffer-folder-name)))
2201 ;;; ???hang up
2202 ;;;   (rename-buffer (wl-summary-sticky-buffer-name
2203 ;;;                   (wl-summary-buffer-folder-name))))
2204       (message "Folder `%s' is now sticky." (wl-summary-buffer-folder-name)))))
2205
2206 (defun wl-summary-switch-to-clone-buffer (buffer-name)
2207   (let ((cur-buf (current-buffer))
2208         (msg (wl-summary-message-number))
2209         (buf (get-buffer-create buffer-name))
2210         (folder wl-summary-buffer-elmo-folder)
2211         (copy-variables
2212          (append '(wl-summary-buffer-view
2213                    wl-summary-buffer-temp-mark-list
2214                    wl-summary-buffer-target-mark-list
2215                    wl-summary-buffer-elmo-folder
2216                    wl-summary-buffer-number-column
2217                    wl-summary-buffer-temp-mark-column
2218                    wl-summary-buffer-persistent-mark-column
2219                    wl-summary-buffer-message-modified
2220                    wl-summary-buffer-thread-modified
2221                    wl-summary-buffer-number-list
2222                    wl-summary-buffer-unsync-mark-number-list
2223                    wl-summary-buffer-folder-name
2224                    wl-summary-buffer-line-formatter)
2225                  (and (eq wl-summary-buffer-view 'thread)
2226                       '(wl-thread-entity-hashtb
2227                         wl-thread-entities
2228                         wl-thread-entity-list))
2229                  (and wl-use-scoring
2230                       '(wl-summary-scored
2231                         wl-summary-default-score
2232                         wl-summary-important-above
2233                         wl-summary-target-above
2234                         wl-summary-mark-below
2235                         wl-summary-expunge-below))
2236                  (and (featurep 'wl-score)
2237                       '(wl-current-score-file
2238                         wl-score-alist)))))
2239     (set-buffer buf)
2240     (wl-summary-mode)
2241     (wl-summary-buffer-set-folder folder)
2242     (let ((buffer-read-only nil))
2243       (insert-buffer cur-buf))
2244     (set-buffer-modified-p nil)
2245     (while copy-variables
2246       (set (car copy-variables)
2247            (save-excursion
2248              (set-buffer cur-buf)
2249              (symbol-value (car copy-variables))))
2250       (setq copy-variables (cdr copy-variables)))
2251     (switch-to-buffer buf)
2252     (kill-buffer cur-buf)
2253     (wl-summary-count-unread)
2254     (wl-summary-update-modeline)
2255     (if msg
2256         (if (eq wl-summary-buffer-view 'thread)
2257             (wl-thread-jump-to-msg msg)
2258           (wl-summary-jump-to-msg msg))
2259       (goto-char (point-max))
2260       (beginning-of-line))))
2261
2262 (defun wl-summary-get-buffer (folder)
2263   (or (and folder
2264            (get-buffer (wl-summary-sticky-buffer-name folder)))
2265       (get-buffer wl-summary-buffer-name)))
2266
2267 (defun wl-summary-get-buffer-create (name &optional force-sticky)
2268   (if force-sticky
2269       (get-buffer-create
2270        (wl-summary-sticky-buffer-name name))
2271     (or (get-buffer (wl-summary-sticky-buffer-name name))
2272         (get-buffer-create wl-summary-buffer-name))))
2273
2274 (defun wl-summary-make-number-list ()
2275   (save-excursion
2276     (goto-char (point-min))
2277     (setq wl-summary-buffer-number-list nil)
2278     (while (not (eobp))
2279       (setq wl-summary-buffer-number-list
2280             (cons (wl-summary-message-number)
2281                   wl-summary-buffer-number-list))
2282       (forward-line 1))
2283     (setq wl-summary-buffer-number-list
2284           (nreverse wl-summary-buffer-number-list))))
2285
2286 (defun wl-summary-auto-select-msg-p (unread-msg)
2287   (and unread-msg
2288        (not (elmo-message-has-global-flag-p
2289              wl-summary-buffer-elmo-folder unread-msg))))
2290
2291 (defsubst wl-summary-open-folder (folder)
2292   ;; Select folder
2293   (let ((elmo-mime-charset wl-summary-buffer-mime-charset))
2294     (unwind-protect
2295         (elmo-folder-open folder 'load-msgdb)
2296       ;; For compatibility
2297       (setq wl-summary-buffer-folder-name (elmo-folder-name-internal
2298                                            folder)))))
2299
2300 (defun wl-summary-goto-folder-subr (&optional name scan-type other-window
2301                                               sticky interactive scoring
2302                                               force-exit)
2303   "Display target folder on summary."
2304   (interactive)
2305   (let* ((keep-cursor (memq this-command
2306                             wl-summary-keep-cursor-command))
2307          (name (or name (wl-summary-read-folder wl-default-folder)))
2308          (cur-fld wl-summary-buffer-elmo-folder)
2309          folder buf mes hilit reuse-buf
2310          retval entity)
2311     (if (string= name "")
2312         (setq name wl-default-folder))
2313     (setq folder (wl-folder-get-elmo-folder name))
2314     (when (and (not (string=
2315                      (and cur-fld (elmo-folder-name-internal cur-fld))
2316                      (elmo-folder-name-internal folder))) ; folder is moved.
2317                (eq major-mode 'wl-summary-mode)) ; called in summary.
2318       (setq wl-summary-last-visited-folder (wl-summary-buffer-folder-name))
2319       (run-hooks 'wl-summary-exit-pre-hook)
2320       (if (or force-exit (not (wl-summary-sticky-p)))
2321           (wl-summary-cleanup-temp-marks (wl-summary-sticky-p)))
2322       (wl-summary-save-view)
2323       (elmo-folder-commit wl-summary-buffer-elmo-folder)
2324       (if (and (wl-summary-sticky-p) force-exit)
2325           (kill-buffer (current-buffer))))
2326     (setq buf (wl-summary-get-buffer-create (elmo-folder-name-internal folder)
2327                                             sticky))
2328     (setq reuse-buf
2329           (save-excursion
2330             (set-buffer buf)
2331             (string= (elmo-folder-name-internal folder)
2332                      (wl-summary-buffer-folder-name))))
2333     (unwind-protect
2334         (if reuse-buf
2335             (if interactive
2336                 (switch-to-buffer buf)
2337               (set-buffer buf))
2338           (if other-window
2339               (delete-other-windows))
2340           (set-buffer buf)
2341           (unless (eq major-mode 'wl-summary-mode)
2342             (wl-summary-mode))
2343           (wl-summary-buffer-set-folder folder)
2344           (setq wl-summary-buffer-disp-msg nil)
2345           (setq wl-summary-buffer-last-displayed-msg nil)
2346           (setq wl-summary-buffer-current-msg nil)
2347           (let ((inhibit-read-only t)
2348                 (buffer-read-only nil))
2349             (erase-buffer)
2350             ;; Resume summary view
2351             (if wl-summary-cache-use
2352                 (let* ((dir (elmo-folder-msgdb-path folder))
2353                        (cache (expand-file-name wl-summary-cache-file dir))
2354                        (view (expand-file-name wl-summary-view-file dir)))
2355                   (when (file-exists-p cache)
2356                     (insert-file-contents-as-binary cache)
2357                     (set-buffer-multibyte
2358                      default-enable-multibyte-characters)
2359                     (decode-mime-charset-region
2360                      (point-min)(point-max)
2361                      wl-summary-buffer-mime-charset 'LF))
2362                   (if (file-exists-p view)
2363                       (setq wl-summary-buffer-view
2364                             (wl-summary-load-file-object view))
2365                     (setq wl-summary-buffer-view
2366                           (or (wl-get-assoc-list-value
2367                                wl-summary-default-view-alist
2368                                (elmo-folder-name-internal folder))
2369                               wl-summary-default-view)))
2370                   (wl-thread-resume-entity folder)
2371                   (wl-summary-open-folder folder)
2372                   (wl-summary-detect-mark-position))
2373               (setq wl-summary-buffer-view
2374                     (wl-summary-load-file-object
2375                      (expand-file-name wl-summary-view-file
2376                                        (elmo-folder-msgdb-path folder))))
2377               (wl-summary-open-folder folder)
2378               (wl-summary-detect-mark-position)
2379               (wl-summary-rescan))
2380             (wl-summary-count-unread)
2381             (wl-summary-update-modeline)))
2382       (unless (eq wl-summary-buffer-view 'thread)
2383         (wl-summary-make-number-list))
2384       (setq wl-summary-buffer-unsync-mark-number-list
2385             (copy-sequence wl-summary-buffer-number-list))
2386       (when (and wl-summary-cache-use
2387                  (or (and wl-summary-check-line-format
2388                           (wl-summary-line-format-changed-p))
2389                      (wl-summary-view-old-p)))
2390         (wl-summary-rescan))
2391       (wl-summary-toggle-disp-msg (if wl-summary-buffer-disp-msg 'on 'off))
2392       (unless (and reuse-buf keep-cursor)
2393         (unwind-protect
2394             (let ((wl-use-scoring
2395                    (if (or scoring interactive) wl-use-scoring)))
2396               (if (and (not scan-type)
2397                        interactive
2398                        (not wl-ask-range))
2399                   (setq scan-type (wl-summary-get-sync-range folder)))
2400               (cond
2401                ((eq scan-type nil)
2402                 (wl-summary-sync 'unset-cursor))
2403                ((eq scan-type 'all)
2404                 (wl-summary-sync 'unset-cursor "all"))
2405                ((eq scan-type 'no-sync))
2406                ((eq scan-type 'rescan)
2407                 (wl-summary-rescan))
2408                ((or (eq scan-type 'force-update)
2409                     (eq scan-type 'update))
2410                 (setq mes (wl-summary-sync-force-update
2411                            'unset-cursor)))))
2412           (if interactive
2413               (switch-to-buffer buf)
2414             (set-buffer buf))
2415           ;; stick always-sticky-folder
2416           (when (wl-summary-always-sticky-folder-p folder)
2417             (or (wl-summary-sticky-p) (wl-summary-stick t)))
2418           (run-hooks 'wl-summary-prepared-pre-hook)
2419           (set-buffer-modified-p nil)
2420           (goto-char (point-min))
2421           (if (wl-summary-cursor-down t)
2422               (let ((unreadp (wl-summary-next-message
2423                               (wl-summary-message-number)
2424                               'down t)))
2425                 (cond ((and wl-auto-select-first
2426                             (wl-summary-auto-select-msg-p unreadp))
2427                        ;; wl-auto-select-first is non-nil and
2428                        ;; unreadp is non-nil but not flagged
2429                        (setq retval 'disp-msg))
2430                       ((and wl-auto-prefetch-first
2431                             (wl-summary-auto-select-msg-p unreadp))
2432                        ;; wl-auto-select-first is non-nil and
2433                        ;; unreadp is non-nil but not flagged
2434                        (setq retval 'prefetch-msg))
2435                       ((not (wl-summary-auto-select-msg-p unreadp))
2436                        ;; unreadp is nil or flagged
2437                        (setq retval 'more-next))))
2438             (goto-char (point-max))
2439             (if (elmo-folder-plugged-p folder)
2440                 (forward-line -1)
2441               (wl-summary-prev))
2442             (setq retval 'more-next))
2443           (if (and wl-summary-highlight
2444                    (not wl-summary-lazy-highlight)
2445                    (not reuse-buf))
2446               (if (and wl-summary-highlight-partial-threshold
2447                        (> (count-lines (point-min) (point-max))
2448                           wl-summary-highlight-partial-threshold))
2449                   (save-excursion
2450                     (forward-line (-
2451                                    0
2452                                    (or
2453                                     wl-summary-partial-highlight-above-lines
2454                                     wl-summary-highlight-partial-threshold)))
2455                     (wl-highlight-summary (point) (point-max)))
2456                 (wl-highlight-summary (point-min) (point-max))))
2457           (if (eq retval 'disp-msg)
2458               (wl-summary-redisplay))
2459           (if (eq retval 'prefetch-msg)
2460               (wl-message-buffer-prefetch
2461                folder
2462                (wl-summary-message-number)
2463                wl-message-buffer-prefetch-depth
2464                (current-buffer)
2465                wl-summary-buffer-mime-charset))
2466           (if mes (message "%s" mes))
2467           (if (and interactive wl-summary-recenter)
2468               (recenter (/ (- (window-height) 2) 2))))))
2469     ;; set current entity-id
2470     (when (and folder
2471                (setq entity
2472                      (wl-folder-search-entity-by-name
2473                       (elmo-folder-name-internal folder)
2474                       wl-folder-entity
2475                       'folder)))
2476       ;; entity-id is unknown.
2477       (wl-folder-set-current-entity-id
2478        (wl-folder-get-entity-id entity)))
2479     (when (and wl-summary-buffer-window-scroll-functions
2480                wl-on-xemacs)
2481       (sit-for 0))
2482     (unwind-protect
2483         (run-hooks 'wl-summary-prepared-hook)
2484       (set-buffer-modified-p nil))
2485     retval))
2486
2487 (defun wl-summary-goto-previous-message-beginning ()
2488   (end-of-line)
2489   (re-search-backward "\r\\(-?[0-9]+\\)" nil t)
2490   (beginning-of-line))
2491
2492 (defun wl-summary-goto-top-of-current-thread ()
2493   (wl-summary-jump-to-msg
2494    (wl-thread-entity-get-number
2495     (wl-thread-entity-get-top-entity (wl-thread-get-entity
2496                                       (wl-summary-message-number))))))
2497
2498 (defun wl-summary-goto-bottom-of-sub-thread (&optional depth)
2499   (interactive)
2500   (let ((depth (or depth
2501                    (wl-thread-get-depth-of-current-line))))
2502     (forward-line 1)
2503     (while (and (not (eobp))
2504                 (>= (wl-thread-get-depth-of-current-line)
2505                     depth))
2506       (forward-line 1))
2507     (beginning-of-line)))
2508
2509 (defun wl-summary-insert-line (line)
2510   "Insert LINE in the Summary."
2511   (if wl-use-highlight-mouse-line
2512       ;; remove 'mouse-face of current line.
2513       (put-text-property
2514        (save-excursion (beginning-of-line)(point))
2515        (save-excursion (end-of-line)(point))
2516        'mouse-face nil))
2517   (insert line "\n")
2518   (if wl-use-highlight-mouse-line
2519       ;; remove 'mouse-face of current line.
2520       (put-text-property
2521        (save-excursion (beginning-of-line)(point))
2522        (save-excursion (end-of-line)(point))
2523        'mouse-face nil))
2524   (ignore-errors
2525     (run-hooks 'wl-summary-line-inserted-hook)))
2526
2527 (defun wl-summary-insert-sequential (entity folder &rest args)
2528   (when entity
2529     (let ((inhibit-read-only t)
2530           (number (elmo-message-entity-number entity))
2531           buffer-read-only)
2532       (goto-char (point-max))
2533       (wl-summary-insert-line
2534        (wl-summary-create-line entity nil nil
2535                                (elmo-message-flags
2536                                 wl-summary-buffer-elmo-folder
2537                                 number)
2538                                (elmo-message-cached-p
2539                                 wl-summary-buffer-elmo-folder
2540                                 number)))
2541       (setq wl-summary-buffer-number-list
2542             (wl-append wl-summary-buffer-number-list
2543                        (list (elmo-message-entity-number entity))))
2544       nil)))
2545
2546 (defun wl-summary-default-subject-filter (subject)
2547   (setq subject (elmo-replace-in-string subject "[ \t]*\\(re\\|was\\)[:>]" ""))
2548   (setq subject (elmo-replace-in-string subject "[ \t]" ""))
2549   (elmo-replace-in-string subject "^\\[.*\\]" ""))
2550
2551 (defun wl-summary-subject-equal (subject1 subject2)
2552   (string= (funcall wl-summary-subject-filter-function subject1)
2553            (funcall wl-summary-subject-filter-function subject2)))
2554
2555 (defmacro wl-summary-put-alike (alike)
2556   (` (elmo-set-hash-val (format "#%d" (wl-count-lines))
2557                         (, alike)
2558                         wl-summary-alike-hashtb)))
2559
2560 (defmacro wl-summary-get-alike ()
2561   (` (elmo-get-hash-val (format "#%d" (wl-count-lines))
2562                         wl-summary-alike-hashtb)))
2563
2564 (defun wl-summary-insert-headers (folder func mime-decode)
2565   (let ((numbers (elmo-folder-list-messages folder 'visible t))
2566         ov this last alike)
2567     (buffer-disable-undo (current-buffer))
2568     (make-local-variable 'wl-summary-alike-hashtb)
2569     (setq wl-summary-alike-hashtb (elmo-make-hash (* (length numbers) 2)))
2570     (when mime-decode
2571       (set-buffer-multibyte default-enable-multibyte-characters))
2572     (while (setq ov (elmo-message-entity folder (pop numbers)))
2573       (setq this (funcall func ov))
2574       (and this (setq this (std11-unfold-string this)))
2575       (if (equal last this)
2576           (setq alike (cons ov alike))
2577         (when last
2578           (wl-summary-put-alike alike)
2579           (insert last ?\n))
2580         (setq alike (list ov)
2581               last this)))
2582     (when last
2583       (wl-summary-put-alike alike)
2584       (insert last ?\n))
2585     (when mime-decode
2586       (decode-mime-charset-region (point-min) (point-max)
2587                                   elmo-mime-charset)
2588       (when (eq mime-decode 'mime)
2589         (eword-decode-region (point-min) (point-max))))
2590     (run-hooks 'wl-summary-insert-headers-hook)))
2591
2592 (defun wl-summary-search-by-subject (entity folder)
2593   (let ((summary-buf (current-buffer))
2594         (buf (get-buffer-create wl-summary-search-buf-name))
2595         (folder-name (wl-summary-buffer-folder-name))
2596         match founds result)
2597     (with-current-buffer buf
2598       (let ((case-fold-search t))
2599         (when (or (not (string= wl-summary-search-buf-folder-name folder-name))
2600                   (zerop (buffer-size)))
2601           (setq wl-summary-search-buf-folder-name folder-name)
2602           (message "Creating subject cache...")
2603           (wl-summary-insert-headers
2604            folder
2605            (function
2606             (lambda (x)
2607               (funcall wl-summary-subject-filter-function
2608                        (elmo-message-entity-field x 'subject))))
2609            t)
2610           (message "Creating subject cache...done"))
2611         (setq match (funcall wl-summary-subject-filter-function
2612                              (elmo-message-entity-field entity 'subject
2613                                                         'decode)))
2614         (if (string= match "")
2615             (setq match "\n"))
2616         (goto-char (point-max))
2617         (while (and (null result)
2618                     (not (= (point) (point-min)))
2619                     (search-backward match nil t))
2620           ;; check exactly match
2621           (when (and (bolp) (= (point-at-eol)(match-end 0)))
2622             (setq founds (wl-summary-get-alike))
2623             (with-current-buffer summary-buf
2624               (while founds
2625                 (when (and
2626                        ;; the first element of found-entity list exists on
2627                        ;; thread tree.
2628                        (wl-thread-get-entity
2629                         (elmo-message-entity-number (car founds)))
2630                        ;; message id is not same as myself.
2631                        (not (string=
2632                              (elmo-message-entity-field entity 'message-id)
2633                              (elmo-message-entity-field (car founds)
2634                                                         'message-id)))
2635                        ;; not a descendant.
2636                        (not (wl-thread-descendant-p
2637                              (elmo-message-entity-number entity)
2638                              (elmo-message-entity-number (car founds)))))
2639                   (setq result (car founds)
2640                         founds nil))
2641                 (setq founds (cdr founds))))))
2642         result))))
2643
2644 (defun wl-summary-insert-thread (entity folder update
2645                                         &optional force-insert)
2646   (let ((depth 0)
2647         this-id parent-entity parent-number
2648         number cur-entity linked retval delayed-entity
2649         update-list entity-stack thread-entity)
2650     (while entity
2651       (setq this-id (elmo-message-entity-field entity 'message-id)
2652             number (elmo-message-entity-number entity))
2653       (if (and wl-thread-saved-entity-hashtb-internal
2654                (setq thread-entity
2655                      (elmo-get-hash-val
2656                       (format "#%d" (elmo-message-entity-number entity))
2657                       wl-thread-saved-entity-hashtb-internal)))
2658           (setq parent-entity
2659                 (elmo-message-entity
2660                  folder
2661                  (wl-thread-entity-get-parent thread-entity))
2662                 linked (wl-thread-entity-get-linked thread-entity))
2663         (setq parent-entity (elmo-message-entity-parent folder entity)
2664               linked nil))
2665       (setq parent-number (and parent-entity
2666                                (elmo-message-entity-number parent-entity)))
2667       ;; If thread loop detected, set parent as nil.
2668       (let ((cur entity)
2669             anumber relatives)
2670         (while cur
2671           (when (setq anumber
2672                       (elmo-message-entity-number
2673                        (setq cur (elmo-message-entity-parent folder cur))))
2674             (if (memq anumber relatives)
2675                 (setq parent-number nil
2676                       cur nil))
2677             (setq relatives (cons anumber relatives)))))
2678       (if (and parent-number
2679                (not (wl-thread-get-entity parent-number))
2680                (not force-insert))
2681           ;; parent exists in overview, but not in wl-thread-entities
2682           (progn
2683             (wl-append wl-summary-delayed-update
2684                        (list (cons parent-number entity)))
2685             (setq entity nil)) ;; exit loop
2686         ;; Search parent by subject.
2687         (when (and (null parent-number)
2688                    wl-summary-search-parent-by-subject-regexp
2689                    (string-match
2690                     wl-summary-search-parent-by-subject-regexp
2691                     (elmo-message-entity-field entity 'subject)))
2692           (let ((found (wl-summary-search-by-subject entity folder)))
2693             (when (and found
2694                        (not (member found wl-summary-delayed-update)))
2695               (setq parent-entity found)
2696               (setq parent-number
2697                     (elmo-message-entity-number parent-entity))
2698               (setq linked t))))
2699         ;; If subject is change, divide thread.
2700         (if (and parent-number
2701                  wl-summary-divide-thread-when-subject-changed
2702                  (not (wl-summary-subject-equal
2703                        (or (elmo-message-entity-field entity
2704                                                       'subject t) "")
2705                        (or (elmo-message-entity-field parent-entity
2706                                                       'subject t) ""))))
2707             (setq parent-number nil))
2708         (setq retval
2709               (wl-thread-insert-message entity
2710                                         number parent-number update linked))
2711         (and retval
2712              (wl-append update-list (list retval)))
2713         (setq entity nil) ; exit loop
2714         (while (setq delayed-entity (assq number wl-summary-delayed-update))
2715           (setq wl-summary-delayed-update
2716                 (delq delayed-entity wl-summary-delayed-update))
2717           ;; update delayed message
2718           (wl-append entity-stack (list (cdr delayed-entity)))))
2719       (if (and (not entity)
2720                entity-stack)
2721           (setq entity (pop entity-stack))))
2722     update-list))
2723
2724 (defun wl-summary-update-thread (entity
2725                                  thr-entity
2726                                  parent-entity)
2727   (let* ((this-id (elmo-message-entity-field entity 'message-id))
2728          (overview-entity entity)
2729          (parent-id (elmo-message-entity-field parent-entity 'message-id))
2730          (number (elmo-message-entity-number entity))
2731          (parent-number (elmo-message-entity-number parent-entity))
2732          insert-line)
2733     (cond
2734      ((or (not parent-id)
2735           (string= this-id parent-id))
2736       (goto-char (point-max))
2737       (beginning-of-line)
2738       (setq insert-line t))
2739      ;; parent already exists in buffer.
2740      ((wl-summary-jump-to-msg parent-number)
2741       (wl-thread-goto-bottom-of-sub-thread)
2742       (setq insert-line t)))
2743     (when insert-line
2744       (let (buffer-read-only)
2745         (wl-summary-insert-line
2746          (wl-summary-create-line
2747           entity
2748           parent-entity
2749           nil
2750           (elmo-message-flags wl-summary-buffer-elmo-folder number)
2751           (elmo-message-cached-p wl-summary-buffer-elmo-folder number)
2752           (wl-thread-maybe-get-children-num number)
2753           (wl-thread-make-indent-string thr-entity)
2754           (wl-thread-entity-get-linked thr-entity)))))))
2755
2756 (defun wl-summary-target-mark-msgs (msgs)
2757   "Return the number of marked messages."
2758   (let ((i 0))
2759     (dolist (number msgs)
2760       (when (wl-summary-target-mark number)
2761         (setq i (1+ i))))
2762     i))
2763
2764 (defun wl-summary-pick (&optional from-list delete-marks)
2765   (interactive)
2766   (save-excursion
2767     (let* ((condition (car (elmo-parse-search-condition
2768                             (elmo-read-search-condition
2769                              wl-summary-pick-field-default))))
2770            (result (elmo-folder-search wl-summary-buffer-elmo-folder
2771                                        condition
2772                                        from-list))
2773            num)
2774       (if delete-marks
2775           (let ((mlist wl-summary-buffer-target-mark-list))
2776             (while mlist
2777               (when (wl-summary-jump-to-msg (car mlist))
2778                 (wl-summary-unmark))
2779               (setq mlist (cdr mlist)))
2780             (setq wl-summary-buffer-target-mark-list nil)))
2781       (if (and result
2782                (setq num (wl-summary-target-mark-msgs result))
2783                (> num 0))
2784           (if (= num (length result))
2785               (message "%d message(s) are picked." num)
2786             (message "%d(%d) message(s) are picked." num
2787                      (- (length result) num)))
2788         (message "No message was picked.")))))
2789
2790 (defun wl-summary-unvirtual ()
2791   "Exit from current virtual folder."
2792   (interactive)
2793   (if (eq 'filter
2794           (elmo-folder-type-internal wl-summary-buffer-elmo-folder))
2795       (wl-summary-goto-folder-subr
2796        (elmo-folder-name-internal
2797         (elmo-filter-folder-target-internal
2798          wl-summary-buffer-elmo-folder))
2799        'update nil nil t)
2800     (error "This folder is not filtered")))
2801
2802 (defun wl-summary-virtual (&optional arg)
2803   "Goto virtual folder.
2804 If ARG, exit virtual folder."
2805   (interactive "P")
2806   (if arg
2807       (wl-summary-unvirtual)
2808     (wl-summary-goto-folder-subr (concat "/"
2809                                          (elmo-read-search-condition
2810                                           wl-summary-pick-field-default)
2811                                          "/"
2812                                          (wl-summary-buffer-folder-name))
2813                                  'update nil nil t)
2814     (run-hooks 'wl-summary-virtual-hook)))
2815
2816 (defun wl-summary-delete-all-temp-marks (&optional no-msg force)
2817   "Erase all temp marks from buffer."
2818   (interactive)
2819   (when (or wl-summary-buffer-target-mark-list
2820             wl-summary-buffer-temp-mark-list
2821             wl-summary-scored)
2822     (save-excursion
2823       (goto-char (point-min))
2824       (unless no-msg
2825         (message "Unmarking..."))
2826       (while (not (eobp))
2827         (wl-summary-unset-mark nil nil force)
2828         (forward-line 1))
2829       (unless no-msg
2830         (message "Unmarking...done"))
2831       (setq wl-summary-buffer-target-mark-list nil)
2832       (setq wl-summary-buffer-temp-mark-list nil))))
2833
2834 (defsubst wl-summary-temp-mark (&optional number)
2835   "Return temp-mark string of current line."
2836   (let ((number (or number (wl-summary-message-number)))
2837         info)
2838     (or (and (wl-summary-have-target-mark-p number)
2839              "*")
2840         (and (setq info (wl-summary-registered-temp-mark number))
2841              (nth 1 info))
2842         (wl-summary-get-score-mark number)
2843         " ")))
2844
2845 (defsubst wl-summary-persistent-mark-string (folder flags cached)
2846   "Return the persistent mark string.
2847 The mark is decided according to the FOLDER, FLAGS and CACHED."
2848   (let ((priorities wl-summary-persistent-mark-priority-list)
2849         mark)
2850     (while (and (null mark) priorities)
2851       (if (and (eq (car priorities) 'flag)
2852                (elmo-get-global-flags flags 'ignore-preserved))
2853           (setq mark wl-summary-flag-mark)
2854         (when (memq (car priorities) flags)
2855           (setq mark
2856                 (or (case (car priorities)
2857                       (new
2858                        (if cached
2859                            wl-summary-new-cached-mark
2860                          wl-summary-new-uncached-mark))
2861                       (answered
2862                        (if cached
2863                            wl-summary-answered-cached-mark
2864                          wl-summary-answered-uncached-mark))
2865                       (unread
2866                        (if cached
2867                            wl-summary-unread-cached-mark
2868                          wl-summary-unread-uncached-mark)))))))
2869       (setq priorities (cdr priorities)))
2870     (or mark
2871         (if (or cached (elmo-folder-local-p folder))
2872             nil
2873           wl-summary-read-uncached-mark))))
2874
2875 (defsubst wl-summary-message-mark (folder number &optional flags)
2876   "Return mark of the message."
2877   (ignore-errors
2878     (wl-summary-persistent-mark-string
2879      folder
2880      (or flags (setq flags (elmo-message-flags folder number)))
2881      (memq 'cached flags) ; XXX for speed-up.
2882      )))
2883
2884 (defsubst wl-summary-persistent-mark (&optional number flags)
2885   "Return persistent-mark string of current line."
2886   (or (wl-summary-message-mark wl-summary-buffer-elmo-folder
2887                                (or number (wl-summary-message-number))
2888                                flags)
2889       " "))
2890
2891 (defun wl-summary-put-temp-mark (mark)
2892   "Put temp MARK on current line."
2893   (when wl-summary-buffer-temp-mark-column
2894     (save-excursion
2895       (beginning-of-line)
2896       (let ((inhibit-read-only t)
2897             (buffer-read-only nil))
2898         (move-to-column wl-summary-buffer-temp-mark-column)
2899         (delete-backward-char 1)
2900         (insert mark)))))
2901
2902 (defun wl-summary-next-buffer ()
2903   "Switch to next summary buffer."
2904   (interactive)
2905   (let ((buffers (sort (wl-collect-summary)
2906                        (lambda (buffer1 buffer2)
2907                          (string-lessp (buffer-name buffer1)
2908                                        (buffer-name buffer2))))))
2909     (switch-to-buffer
2910      (or (cadr (memq (current-buffer) buffers))
2911          (car buffers)))))
2912
2913 (defun wl-summary-previous-buffer ()
2914   "Switch to previous summary buffer."
2915   (interactive)
2916   (let ((buffers (sort (wl-collect-summary)
2917                        (lambda (buffer1 buffer2)
2918                          (not (string-lessp (buffer-name buffer1)
2919                                             (buffer-name buffer2)))))))
2920     (switch-to-buffer
2921      (or (cadr (memq (current-buffer) buffers))
2922          (car buffers)))))
2923
2924 (defun wl-summary-target-mark-mark-as-read ()
2925   (interactive)
2926   (save-excursion
2927     (goto-char (point-min))
2928     (let ((inhibit-read-only t)
2929           (buffer-read-only nil)
2930           wl-summary-buffer-disp-msg)
2931       (wl-summary-mark-as-read wl-summary-buffer-target-mark-list)
2932       (dolist (number wl-summary-buffer-target-mark-list)
2933         (wl-summary-unset-mark number)))))
2934
2935 (defun wl-summary-target-mark-mark-as-unread ()
2936   (interactive)
2937   (save-excursion
2938     (goto-char (point-min))
2939     (let ((inhibit-read-only t)
2940           (buffer-read-only nil)
2941           wl-summary-buffer-disp-msg)
2942       (wl-summary-mark-as-unread wl-summary-buffer-target-mark-list)
2943       (dolist (number wl-summary-buffer-target-mark-list)
2944         (wl-summary-unset-mark number)))))
2945
2946 (defun wl-summary-target-mark-operation (flag)
2947   (save-excursion
2948     (goto-char (point-min))
2949     (let ((inhibit-read-only t)
2950           (buffer-read-only nil)
2951           wl-summary-buffer-disp-msg)
2952       (funcall
2953        (intern (format "wl-summary-mark-as-%s-internal" flag))
2954        nil
2955        wl-summary-buffer-target-mark-list)
2956       (dolist (number wl-summary-buffer-target-mark-list)
2957         (wl-summary-unset-mark number)))))
2958
2959 (defun wl-summary-target-mark-mark-as-important ()
2960   (interactive)
2961   (wl-summary-target-mark-operation 'important))
2962
2963 (defun wl-summary-target-mark-mark-as-answered ()
2964   (interactive)
2965   (wl-summary-target-mark-operation 'answered))
2966
2967 (defun wl-summary-target-mark-set-flags ()
2968   (interactive)
2969   (save-excursion
2970     (goto-char (point-min))
2971     (let ((inhibit-read-only t)
2972           (buffer-read-only nil)
2973           wl-summary-buffer-disp-msg
2974           flags)
2975       (dolist (number wl-summary-buffer-target-mark-list)
2976         (wl-summary-unset-mark number)
2977         (setq flags (wl-summary-set-flags-internal number flags)))
2978       (wl-summary-count-unread)
2979       (wl-summary-update-modeline))))
2980
2981 (defun wl-summary-target-mark-save ()
2982   (interactive)
2983   (let ((wl-save-dir
2984          (wl-read-directory-name "Save to directory: "
2985                                  wl-temporary-file-directory))
2986         number)
2987     (if (null (file-exists-p wl-save-dir))
2988         (make-directory wl-save-dir))
2989     (while (setq number (car wl-summary-buffer-target-mark-list))
2990       (wl-thread-jump-to-msg number)
2991       (wl-summary-save t wl-save-dir)
2992       (wl-summary-unmark))))
2993
2994 (defun wl-summary-target-mark-pick ()
2995   (interactive)
2996   (wl-summary-pick wl-summary-buffer-target-mark-list 'delete))
2997
2998 (defun wl-summary-update-persistent-mark (&optional number flags)
2999   "Synch up persistent mark of current line with msgdb's.
3000 Return non-nil if the mark is updated"
3001   (prog1
3002       (when wl-summary-buffer-persistent-mark-column
3003         (save-excursion
3004           (move-to-column wl-summary-buffer-persistent-mark-column)
3005           (let ((inhibit-read-only t)
3006                 (buffer-read-only nil)
3007                 (mark (buffer-substring (- (point) 1) (point)))
3008                 (new-mark (wl-summary-persistent-mark number flags)))
3009             (unless (string= new-mark mark)
3010               (delete-backward-char 1)
3011               (insert new-mark)
3012               (wl-summary-set-message-modified)
3013               t))))
3014     (when wl-summary-highlight
3015       (wl-highlight-summary-current-line))
3016     (set-buffer-modified-p nil)))
3017
3018 (defsubst wl-summary-mark-as-read-internal (inverse
3019                                             number-or-numbers
3020                                             no-folder-mark
3021                                             no-modeline-update)
3022   (save-excursion
3023     (let ((folder wl-summary-buffer-elmo-folder)
3024           unread-message number
3025           number-list visible)
3026       (setq number-list (cond ((numberp number-or-numbers)
3027                                (setq unread-message
3028                                      (elmo-message-flagged-p
3029                                       folder
3030                                       number-or-numbers
3031                                       'unread))
3032                                (list number-or-numbers))
3033                               ((and (not (null number-or-numbers))
3034                                     (listp number-or-numbers))
3035                                number-or-numbers)
3036                               ((setq number (wl-summary-message-number))
3037                                ;; interactive
3038                                (setq unread-message
3039                                      (elmo-message-flagged-p
3040                                       folder
3041                                       number
3042                                       'unread))
3043                                (list number))))
3044       (if (null number-list)
3045           (message "No message.")
3046         (if inverse
3047             (elmo-folder-unset-flag folder number-list 'read no-folder-mark)
3048           (elmo-folder-set-flag folder number-list 'read no-folder-mark))
3049         (dolist (number number-list)
3050           (setq visible (wl-summary-jump-to-msg number))
3051           (unless inverse
3052             (when unread-message
3053               (run-hooks 'wl-summary-unread-message-hook)))
3054           ;; set mark on buffer
3055           (when visible
3056             (wl-summary-update-persistent-mark)))
3057         (unless no-modeline-update
3058           ;; Update unread numbers.
3059           (wl-summary-count-unread)
3060           (wl-summary-update-modeline)
3061           (wl-folder-update-unread
3062            (wl-summary-buffer-folder-name)
3063            (+ wl-summary-buffer-unread-count
3064               wl-summary-buffer-new-count)))))))
3065
3066 (defun wl-summary-mark-as-read (&optional number-or-numbers
3067                                           no-folder-mark
3068                                           no-modeline-update)
3069   (interactive)
3070   (wl-summary-mark-as-read-internal nil
3071                                     number-or-numbers
3072                                     no-folder-mark
3073                                     no-modeline-update))
3074
3075 (defun wl-summary-mark-as-unread (&optional number-or-numbers
3076                                             no-folder-mark
3077                                             no-modeline-update)
3078   (interactive)
3079   (wl-summary-mark-as-read-internal 'inverse
3080                                     number-or-numbers
3081                                     no-folder-mark
3082                                     no-modeline-update))
3083
3084 (defsubst wl-summary-mark-as-answered-internal (inverse
3085                                                 &optional
3086                                                 number-or-numbers
3087                                                 no-modeline-update)
3088   (save-excursion
3089     (let ((folder wl-summary-buffer-elmo-folder)
3090           number number-list visible)
3091       (setq number-list (cond ((numberp number-or-numbers)
3092                                (list number-or-numbers))
3093                               ((and (not (null number-or-numbers))
3094                                     (listp number-or-numbers))
3095                                number-or-numbers)
3096                               ((setq number (wl-summary-message-number))
3097                                ;; interactive
3098                                (list number))))
3099       (if (null number-list)
3100           (message "No message.")
3101         (if inverse
3102             (elmo-folder-unset-flag folder number-list 'answered)
3103           (elmo-folder-set-flag folder number-list 'answered))
3104         (dolist (number number-list)
3105           (setq visible (wl-summary-jump-to-msg number))
3106           ;; set mark on buffer
3107           (when visible
3108             (wl-summary-update-persistent-mark)))
3109         (unless no-modeline-update
3110           ;; Update unread numbers.
3111           ;; should elmo-flag-mark-as-read return unread numbers?
3112           (wl-summary-count-unread)
3113           (wl-summary-update-modeline)
3114           (wl-folder-update-unread
3115            (wl-summary-buffer-folder-name)
3116            (+ wl-summary-buffer-unread-count
3117               wl-summary-buffer-new-count)))))))
3118
3119 (defun wl-summary-mark-as-answered (&optional number-or-numbers
3120                                               no-modeline-update)
3121   (interactive)
3122   (wl-summary-mark-as-answered-internal
3123    (and (interactive-p)
3124         (elmo-message-flagged-p wl-summary-buffer-elmo-folder
3125                                 (wl-summary-message-number)
3126                                 'answered))
3127    number-or-numbers
3128    no-modeline-update))
3129
3130 (defun wl-summary-mark-as-unanswered (&optional number-or-numbers
3131                                                 no-modeline-update)
3132   (wl-summary-mark-as-answered-internal 'inverse
3133                                         number-or-numbers
3134                                         no-modeline-update))
3135
3136 (defun wl-summary-decide-flag (folder number)
3137   (let ((flags (elmo-get-global-flags (elmo-message-flags
3138                                        folder number)))
3139         (completion-ignore-case t)
3140         new-flags)
3141     (setq new-flags
3142           (delq nil
3143                 (mapcar
3144                  (lambda (flag)
3145                    (and (> (length flag) 0)
3146                         (intern (downcase flag))))
3147                  (wl-completing-read-multiple
3148                   "Flags: "
3149                   (mapcar (lambda (flag)
3150                             (list (capitalize (symbol-name flag))))
3151                           elmo-global-flag-list)
3152                   nil nil (mapconcat (lambda (flag)
3153                                        (capitalize (symbol-name flag)))
3154                                      flags
3155                                      ",")))))
3156     (dolist (flag new-flags)
3157       (unless (memq flag elmo-global-flag-list)
3158         (if (y-or-n-p (format "Flag `%s' does not exist yet. Create?"
3159                               (capitalize (symbol-name flag))))
3160             (setq elmo-global-flag-list (append
3161                                          elmo-global-flag-list
3162                                          (list flag)))
3163           (error "Stopped"))))
3164     new-flags))
3165
3166 (defsubst wl-summary-set-flags-internal (&optional
3167                                         number-or-numbers
3168                                         flags
3169                                         local
3170                                         remove-all)
3171   (save-excursion
3172     (let ((folder wl-summary-buffer-elmo-folder)
3173           number number-list visible)
3174       (setq number-list (cond ((numberp number-or-numbers)
3175                                (list number-or-numbers))
3176                               ((and (not (null number-or-numbers))
3177                                     (listp number-or-numbers))
3178                                number-or-numbers)
3179                               ((setq number (wl-summary-message-number))
3180                                ;; interactive
3181                                (list number))))
3182       (if remove-all
3183           (setq flags nil)
3184         (unless flags
3185           (setq flags (wl-summary-decide-flag folder (car number-list)))))
3186       (if (null number-list)
3187           (message "No message.")
3188         (dolist (number number-list)
3189           (elmo-message-set-global-flags folder number flags local)
3190           (setq visible (wl-summary-jump-to-msg number))
3191           ;; set mark on buffer
3192           (when visible
3193             (wl-summary-update-persistent-mark))))
3194       flags)))
3195
3196 (defsubst wl-summary-add-flags-internal (&optional
3197                                          number-or-numbers
3198                                          flags
3199                                          local)
3200   (save-excursion
3201     (let ((folder wl-summary-buffer-elmo-folder)
3202           set-flags msg number-list visible)
3203       (setq number-list (cond ((numberp number-or-numbers)
3204                                (list number-or-numbers))
3205                               ((and (not (null number-or-numbers))
3206                                     (listp number-or-numbers))
3207                                number-or-numbers)
3208                               ((setq msg (wl-summary-message-number))
3209                                ;; interactive
3210                                (list msg))))
3211       (if (null number-list)
3212           (message "No message.")
3213         (dolist (number number-list)
3214           (setq set-flags
3215                 (elmo-get-global-flags
3216                  (elmo-message-flags folder number)))
3217           (setq set-flags (nconc flags set-flags))
3218           (elmo-message-set-global-flags folder number set-flags local)
3219           (setq visible (wl-summary-jump-to-msg number))
3220           ;; set mark on buffer
3221           (when visible
3222             (wl-summary-update-persistent-mark)))))))
3223
3224 (defsubst wl-summary-remove-flags-internal (&optional
3225                                             number-or-numbers
3226                                             flags
3227                                             local)
3228   (save-excursion
3229     (let ((folder wl-summary-buffer-elmo-folder)
3230           set-flags msg number-list visible)
3231       (setq number-list (cond ((numberp number-or-numbers)
3232                                (list number-or-numbers))
3233                               ((and (not (null number-or-numbers))
3234                                     (listp number-or-numbers))
3235                                number-or-numbers)
3236                               ((setq msg (wl-summary-message-number))
3237                                ;; interactive
3238                                (list msg))))
3239       (if (null number-list)
3240           (message "No message.")
3241         (dolist (number number-list)
3242           (setq set-flags (elmo-get-global-flags
3243                            (elmo-message-flags folder number)))
3244           (dolist (flag flags)
3245             (setq set-flags (delq flag set-flags)))
3246           (elmo-message-set-global-flags folder number set-flags local)
3247           (setq visible (wl-summary-jump-to-msg number))
3248           ;; set mark on buffer
3249           (when visible
3250             (wl-summary-update-persistent-mark)))))))
3251
3252 (defun wl-summary-set-flags (&optional remove)
3253   (interactive "P")
3254   (if (eq 'flag (elmo-folder-type-internal wl-summary-buffer-elmo-folder))
3255       (error "Cannot process flags in this folder"))
3256   (wl-summary-set-flags-internal nil nil nil remove))
3257
3258 (defun wl-summary-mark-as-important-internal (inverse
3259                                               &optional number-or-numbers)
3260   (if inverse
3261       (wl-summary-remove-flags-internal number-or-numbers '(important))
3262     (wl-summary-add-flags-internal number-or-numbers '(important))))
3263
3264 (defun wl-summary-mark-as-important (&optional prompt)
3265   (interactive "P")
3266   (if (eq 'flag (elmo-folder-type-internal wl-summary-buffer-elmo-folder))
3267       (error "Cannot process flags in this folder"))
3268   (if prompt
3269       (wl-summary-set-flags-internal)
3270     (wl-summary-mark-as-important-internal
3271      (and (interactive-p)
3272           (elmo-message-flagged-p wl-summary-buffer-elmo-folder
3273                                   (wl-summary-message-number)
3274                                   'important)))))
3275
3276 ;;; Summary line.
3277 (defvar wl-summary-line-formatter nil)
3278
3279 (defun wl-summary-view-old-p ()
3280   "Return non-nil when summary view cache has old format."
3281   (save-excursion
3282     (goto-char (point-min))
3283     (and wl-summary-buffer-number-list
3284          (not (re-search-forward "\r-?[0-9]+" (point-at-eol) t)))))
3285
3286 (defun wl-summary-line-format-changed-p ()
3287   "Return non-nil when summary line format is changed."
3288   (not (string=
3289         wl-summary-buffer-line-format
3290         (or (elmo-object-load (expand-file-name
3291                                wl-summary-line-format-file
3292                                (elmo-folder-msgdb-path
3293                                 wl-summary-buffer-elmo-folder))
3294                               wl-summary-buffer-mime-charset)
3295             wl-summary-buffer-line-format))))
3296
3297 (defun wl-summary-line-format-save ()
3298   "Save current summary line format."
3299   (elmo-object-save
3300    (expand-file-name wl-summary-line-format-file
3301                      (elmo-folder-msgdb-path
3302                       wl-summary-buffer-elmo-folder))
3303    wl-summary-buffer-line-format
3304    wl-summary-buffer-mime-charset))
3305
3306 (defun wl-summary-line-number ()
3307   (wl-set-string-width
3308    (- wl-summary-buffer-number-column)
3309    (number-to-string
3310     (elmo-message-entity-number wl-message-entity))))
3311
3312 (defun wl-summary-line-year ()
3313   (aref wl-datevec 0))
3314 (defun wl-summary-line-month ()
3315   (format "%02d" (aref wl-datevec 1)))
3316 (defun wl-summary-line-day ()
3317   (format "%02d" (aref wl-datevec 2)))
3318 (defun wl-summary-line-day-of-week ()
3319   (condition-case nil
3320       (elmo-date-get-week (aref wl-datevec 0)
3321                           (aref wl-datevec 1)
3322                           (aref wl-datevec 2))
3323     (error "??")))
3324 (defun wl-summary-line-hour ()
3325   (format "%02d" (aref wl-datevec 3)))
3326 (defun wl-summary-line-minute ()
3327   (format "%02d" (aref wl-datevec 4)))
3328
3329 (defun wl-summary-line-size ()
3330   (let ((size (elmo-message-entity-field wl-message-entity 'size)))
3331     (if size
3332         (cond
3333          ((<= 1 (/ size 1048576))
3334           (format "%.0fM" (/ size 1048576.0)))
3335          ((<= 1 (/ size 1024))
3336           (format "%.0fK" (/ size 1024.0)))
3337          (t (format "%dB" size)))
3338       "")))
3339
3340 (defun wl-summary-line-subject ()
3341   (let (no-parent subject parent-raw-subject parent-subject)
3342     (if (string= wl-thr-indent-string "")
3343         (setq no-parent t)) ; no parent
3344     (setq subject
3345           (elmo-delete-char ?\n
3346                             (or (elmo-message-entity-field
3347                                  wl-message-entity
3348                                  'subject t)
3349                                 wl-summary-no-subject-message)))
3350     (setq parent-raw-subject
3351           (elmo-message-entity-field wl-parent-message-entity
3352                                      'subject t))
3353     (setq parent-subject
3354           (if parent-raw-subject
3355               (elmo-delete-char ?\n parent-raw-subject)))
3356     (if (or no-parent
3357             (null parent-subject)
3358             (not (wl-summary-subject-equal
3359                   subject parent-subject)))
3360         (funcall wl-summary-subject-function subject)
3361       "")))
3362
3363 (defun wl-summary-line-from ()
3364   (elmo-delete-char ?\n
3365                     (funcall wl-summary-from-function
3366                              (elmo-message-entity-field
3367                               wl-message-entity
3368                               'from t))))
3369
3370 (defun wl-summary-line-list-info ()
3371   (let ((list-info (wl-summary-get-list-info wl-message-entity)))
3372     (if (car list-info)
3373         (format (if (cdr list-info) "(%s %05.0f)" "(%s)")
3374                 (car list-info) (cdr list-info))
3375       "")))
3376
3377 (defun wl-summary-line-list-count ()
3378   (let ((ml-count (cdr (wl-summary-get-list-info wl-message-entity))))
3379     (if ml-count
3380         (format "%.0f" ml-count)
3381       "")))
3382
3383 (defun wl-summary-line-attached ()
3384   (let ((content-type (elmo-message-entity-field
3385                        wl-message-entity 'content-type))
3386         (case-fold-search t))
3387     (if (and content-type
3388              (string-match "multipart/mixed" content-type))
3389         "@"
3390       "")))
3391
3392 ;;; For future use.
3393 ;;(defun wl-summary-line-cached ()
3394 ;;  (if (elmo-message-cached-p wl-summary-buffer-elmo-folder
3395 ;;                           (elmo-message-entity-number wl-message-entity))
3396 ;;      " "
3397 ;;    "u"))
3398
3399 (defun wl-summary-create-line (wl-message-entity
3400                                wl-parent-message-entity
3401                                wl-temp-mark
3402                                wl-flags
3403                                wl-cached
3404                                &optional
3405                                wl-thr-children-number
3406                                wl-thr-indent-string
3407                                wl-thr-linked)
3408   "Create a summary line."
3409   (let ((wl-mime-charset wl-summary-buffer-mime-charset)
3410         (wl-persistent-mark (wl-summary-persistent-mark-string
3411                              wl-summary-buffer-elmo-folder
3412                              wl-flags
3413                              wl-cached))
3414         (elmo-mime-charset wl-summary-buffer-mime-charset)
3415         (elmo-lang wl-summary-buffer-weekday-name-lang)
3416         (wl-datevec (or (ignore-errors (timezone-fix-time
3417                                         (elmo-message-entity-field
3418                                          wl-message-entity
3419                                          'date)
3420                                         nil
3421                                         wl-summary-fix-timezone))
3422                         (make-vector 5 0)))
3423         (entity wl-message-entity) ; backward compatibility.
3424         line mark)
3425     (if (and wl-thr-indent-string
3426              wl-summary-indent-length-limit
3427              (< wl-summary-indent-length-limit
3428                 (string-width wl-thr-indent-string)))
3429         (setq wl-thr-indent-string (wl-set-string-width
3430                                     wl-summary-indent-length-limit
3431                                     wl-thr-indent-string)))
3432     (setq line (funcall wl-summary-buffer-line-formatter))
3433     (if wl-summary-width (setq line
3434                                (wl-set-string-width
3435                                 (- wl-summary-width 1) line nil
3436                                 'ignore-invalid)))
3437     (setq line (concat line
3438                        "\r"
3439                        (number-to-string
3440                         (elmo-message-entity-number
3441                          wl-message-entity))))
3442     (if wl-summary-highlight
3443         (wl-highlight-summary-line-string
3444          (elmo-message-entity-number wl-message-entity)
3445          line
3446          wl-flags
3447          wl-temp-mark
3448          wl-thr-indent-string))
3449     line))
3450
3451 (defsubst wl-summary-proc-wday (wday-str year month mday)
3452   (save-match-data
3453     (if (string-match "\\([A-Z][a-z][a-z]\\).*" wday-str)
3454         (wl-match-string 1 wday-str)
3455       (elmo-date-get-week year month mday))))
3456
3457 (defvar wl-summary-move-spec-alist
3458   '((new . ((t . nil)
3459             (p . new)
3460             (p . unread)
3461             (p . digest)))
3462     (unread . ((t . nil)
3463                (p . unread)
3464                (p . digest)))))
3465
3466 (defsubst wl-summary-next-message (num direction hereto)
3467   (if wl-summary-buffer-next-message-function
3468       (funcall wl-summary-buffer-next-message-function num direction hereto)
3469     (let ((cur-spec (cdr (assq wl-summary-move-order
3470                                wl-summary-move-spec-alist)))
3471           (nums (memq num (if (eq direction 'up)
3472                               (reverse wl-summary-buffer-number-list)
3473                             wl-summary-buffer-number-list)))
3474           flagged-list nums2)
3475       (unless hereto (setq nums (cdr nums)))
3476       (setq nums2 nums)
3477       (if cur-spec
3478           (catch 'done
3479             (while cur-spec
3480               (setq nums nums2)
3481               (cond ((eq (car (car cur-spec)) 'p)
3482                      (if (setq flagged-list
3483                                (elmo-folder-list-flagged
3484                                 wl-summary-buffer-elmo-folder
3485                                 (cdr (car cur-spec)) t))
3486                          (while nums
3487                            (if (and (memq (car nums) flagged-list)
3488                                     (elmo-message-accessible-p
3489                                      wl-summary-buffer-elmo-folder
3490                                      (car nums)))
3491                                (throw 'done (car nums)))
3492                            (setq nums (cdr nums)))))
3493                     ((eq (car (car cur-spec)) 't)
3494                      (if wl-summary-buffer-target-mark-list
3495                          (while nums
3496                            (if (memq (car nums)
3497                                      wl-summary-buffer-target-mark-list)
3498                                (throw 'done (car nums)))
3499                            (setq nums (cdr nums))))))
3500               (setq cur-spec (cdr cur-spec))))
3501         (car nums)))))
3502
3503 (defsubst wl-summary-cursor-move (direction hereto)
3504   (when (and (eq direction 'up)
3505              (eobp))
3506     (forward-line -1)
3507     (setq hereto t))
3508   (let (num)
3509     (when (setq num (wl-summary-next-message (wl-summary-message-number)
3510                                              direction hereto))
3511       (if (numberp num)
3512           (wl-thread-jump-to-msg num))
3513       t)))
3514 ;;
3515 ;; Goto unread or global flag message
3516 ;; returns t if next message exists in this folder.
3517 (defun wl-summary-cursor-down (&optional hereto)
3518   (interactive "P")
3519   (wl-summary-cursor-move 'down hereto))
3520
3521 (defun wl-summary-cursor-up (&optional hereto)
3522   (interactive "P")
3523   (wl-summary-cursor-move 'up hereto))
3524
3525 (defun wl-summary-save-view-cache ()
3526   (save-excursion
3527     (let* ((dir (elmo-folder-msgdb-path wl-summary-buffer-elmo-folder))
3528            (cache (expand-file-name wl-summary-cache-file dir))
3529            (view (expand-file-name wl-summary-view-file dir))
3530            (save-view wl-summary-buffer-view)
3531            (mark-list (copy-sequence wl-summary-buffer-target-mark-list))
3532            (temp-list (copy-sequence wl-summary-buffer-temp-mark-list))
3533            (tmp-buffer (get-buffer-create " *wl-summary-save-view-cache*"))
3534            (temp-column wl-summary-buffer-temp-mark-column)
3535            (charset wl-summary-buffer-mime-charset))
3536       (when dir
3537         (if (file-directory-p dir)
3538             (); ok.
3539           (if (file-exists-p dir)
3540               (error "File %s already exists" dir)
3541             (elmo-make-directory dir)))
3542         (if (eq save-view 'thread)
3543             (wl-thread-save-entity dir))
3544         (when wl-summary-check-line-format
3545           (wl-summary-line-format-save))
3546         (unwind-protect
3547             (progn
3548               (when (file-writable-p cache)
3549                 (copy-to-buffer tmp-buffer (point-min) (point-max))
3550                 (with-current-buffer tmp-buffer
3551                   (widen)
3552                   (make-local-variable 'wl-summary-highlight)
3553                   (setq wl-summary-highlight nil
3554                         wl-summary-buffer-target-mark-list mark-list
3555                         wl-summary-buffer-temp-mark-list temp-list
3556                         wl-summary-buffer-temp-mark-column temp-column)
3557                   (wl-summary-delete-all-temp-marks 'no-msg 'force)
3558                   (encode-coding-region
3559                    (point-min) (point-max)
3560                    (or (and wl-on-mule
3561                             ;; one in mcs-ltn1(apel<10.4) cannot take 2 arg.
3562                             (mime-charset-to-coding-system charset 'LF))
3563                        ;; Mule 2 doesn't have `*ctext*unix'.
3564                        (mime-charset-to-coding-system charset)))
3565                   (write-region-as-binary (point-min)(point-max)
3566                                           cache nil 'no-msg)))
3567               (when (file-writable-p view) ; 'thread or 'sequence
3568                 (save-excursion
3569                   (set-buffer tmp-buffer)
3570                   (erase-buffer)
3571                   (prin1 save-view tmp-buffer)
3572                   (princ "\n" tmp-buffer)
3573                   (write-region (point-min) (point-max) view nil 'no-msg))))
3574           ;; kill tmp buffer.
3575           (kill-buffer tmp-buffer))))))
3576
3577 (defsubst wl-summary-get-sync-range (folder)
3578   (intern (or (and
3579                (elmo-folder-plugged-p folder)
3580                (wl-get-assoc-list-value
3581                 wl-folder-sync-range-alist
3582                 (elmo-folder-name-internal folder)
3583                 'function))
3584               wl-default-sync-range)))
3585
3586 ;; redefined for wl-summary-sync-update
3587 (defun wl-summary-input-range (folder)
3588   "returns update or all or rescan."
3589   ;; for the case when parts are expanded in the bottom of the folder
3590   (let ((input-range-list '("no-sync"
3591                             "first:"
3592                             "last:"
3593                             "cache-status"
3594                             "mark"
3595                             "rescan"
3596                             "rescan-noscore"
3597                             "rescan-thread"
3598                             "update"
3599                             "update-entirely"
3600                             "all"
3601                             "all-entirely"))
3602         (default (or (wl-get-assoc-list-value
3603                       wl-folder-sync-range-alist
3604                       folder
3605                       'function)
3606                      wl-default-sync-range))
3607         range)
3608     (setq range
3609           (completing-read (format "Range (%s): " default)
3610                            (mapcar
3611                             (function (lambda (x) (cons x x)))
3612                             input-range-list)))
3613     (if (string= range "")
3614         default
3615       range)))
3616
3617 (defun wl-summary-toggle-disp-folder (&optional arg)
3618   (interactive)
3619   (let ((cur-buf (current-buffer))
3620         (summary-win (get-buffer-window (current-buffer)))
3621         fld-buf fld-win)
3622     (cond
3623      ((eq arg 'on)
3624       (setq wl-summary-buffer-disp-folder t)
3625       ;; hide your folder window
3626       (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3627           (if (setq fld-win (get-buffer-window fld-buf))
3628               (delete-window fld-win))))
3629      ((eq arg 'off)
3630       (setq wl-summary-buffer-disp-folder nil)
3631       ;; hide your wl-message window!
3632       (when (buffer-live-p wl-message-buffer)
3633         (wl-message-select-buffer wl-message-buffer)
3634         (delete-window))
3635       (select-window (get-buffer-window cur-buf))
3636       ;; display wl-folder window!!
3637       (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3638           (if (setq fld-win (get-buffer-window fld-buf))
3639               ;; folder win is already displayed.
3640               (select-window fld-win)
3641             ;; folder win is not displayed.
3642             (switch-to-buffer fld-buf))
3643         ;; no folder buf
3644         (wl-folder))
3645       ;; temporarily delete summary-win.
3646       (if summary-win
3647           (delete-window summary-win))
3648       (split-window-horizontally wl-folder-window-width)
3649       (other-window 1)
3650       (switch-to-buffer cur-buf))
3651      (t
3652       (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3653           (if (setq fld-win (get-buffer-window fld-buf))
3654               (setq wl-summary-buffer-disp-folder nil)
3655             (setq wl-summary-buffer-disp-folder t)))
3656       (if (not wl-summary-buffer-disp-folder)
3657           ;; hide message window
3658           (let ((mes-win (and wl-message-buffer
3659                               (get-buffer-window wl-message-buffer)))
3660                 (wl-stay-folder-window t))
3661             (if mes-win (delete-window mes-win))
3662             ;; hide your folder window
3663             (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3664                 (if (setq fld-win (get-buffer-window fld-buf))
3665                     (progn
3666                       (delete-window (get-buffer-window cur-buf))
3667                       (select-window fld-win)
3668                       (switch-to-buffer cur-buf))))
3669             (run-hooks 'wl-summary-toggle-disp-folder-off-hook)
3670             ;; resume message window.
3671             (when mes-win
3672               (wl-message-select-buffer wl-message-buffer)
3673               (run-hooks 'wl-summary-toggle-disp-folder-message-resumed-hook)
3674               (select-window (get-buffer-window cur-buf))))
3675         ;; hide message window
3676         (let ((wl-stay-folder-window t)
3677               (mes-win (and wl-message-buffer
3678                             (get-buffer-window wl-message-buffer))))
3679           (if mes-win (delete-window mes-win))
3680           (select-window (get-buffer-window cur-buf))
3681           ;; display wl-folder window!!
3682           (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3683               (if (setq fld-win (get-buffer-window fld-buf))
3684                   ;; folder win is already displayed.
3685                   (select-window fld-win)
3686                 ;; folder win is not displayed...occupy all.
3687                 (switch-to-buffer fld-buf))
3688             ;; no folder buf
3689             (wl-folder))
3690           (split-window-horizontally wl-folder-window-width)
3691           (other-window 1)
3692           (switch-to-buffer cur-buf)
3693           ;; resume message window.
3694           (run-hooks 'wl-summary-toggle-disp-folder-on-hook)
3695           (when mes-win
3696             (wl-message-select-buffer wl-message-buffer)
3697             (run-hooks 'wl-summary-toggle-disp-folder-message-resumed-hook)
3698             (select-window (get-buffer-window cur-buf))))))))
3699   (run-hooks 'wl-summary-toggle-disp-folder-hook))
3700
3701 (defun wl-summary-toggle-disp-msg (&optional arg)
3702   (interactive)
3703   (let ((cur-buf (current-buffer))
3704         fld-buf fld-win
3705         summary-win)
3706     (cond
3707      ((eq arg 'on)
3708       (setq wl-summary-buffer-disp-msg t)
3709       (save-excursion
3710         ;; hide your folder window
3711         (if (and (not wl-stay-folder-window)
3712                  (setq fld-buf (get-buffer wl-folder-buffer-name)))
3713             (if (setq fld-win (get-buffer-window fld-buf))
3714                 (unless (one-window-p fld-win)
3715                   (delete-window fld-win))))))
3716      ((eq arg 'off)
3717       (wl-delete-all-overlays)
3718       (setq wl-summary-buffer-disp-msg nil)
3719       (save-excursion
3720         (when (buffer-live-p wl-message-buffer)
3721           (wl-message-select-buffer wl-message-buffer)
3722           (delete-window)
3723           (and (get-buffer-window cur-buf)
3724                (select-window (get-buffer-window cur-buf))))
3725         (run-hooks 'wl-summary-toggle-disp-off-hook)))
3726      (t
3727       (if (and wl-message-buffer
3728                (get-buffer-window wl-message-buffer)) ; already displayed
3729           (setq wl-summary-buffer-disp-msg nil)
3730         (setq wl-summary-buffer-disp-msg t))
3731       (if wl-summary-buffer-disp-msg
3732           (progn
3733             (wl-summary-redisplay)
3734 ;;; hide your folder window
3735 ;;;         (setq fld-buf (get-buffer wl-folder-buffer-name))
3736 ;;;         (if (setq fld-win (get-buffer-window fld-buf))
3737 ;;;             (delete-window fld-win)))
3738             (run-hooks 'wl-summary-toggle-disp-on-hook))
3739         (wl-delete-all-overlays)
3740         (save-excursion
3741           (wl-message-select-buffer wl-message-buffer)
3742           (delete-window)
3743           (select-window (get-buffer-window cur-buf))
3744           (setq wl-message-buffer nil)
3745           (run-hooks 'wl-summary-toggle-disp-off-hook))
3746 ;;;     (switch-to-buffer cur-buf)
3747         )))
3748     (run-hooks 'wl-summary-buffer-window-scroll-functions)))
3749
3750 (defun wl-summary-next-line-content ()
3751   "Show next line of the message."
3752   (interactive)
3753   (let ((cur-buf (current-buffer)))
3754     (wl-summary-toggle-disp-msg 'on)
3755     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
3756       (set-buffer cur-buf)
3757       (wl-message-next-page 1))))
3758
3759 (defun wl-summary-prev-line-content ()
3760   (interactive)
3761   (let ((cur-buf (current-buffer)))
3762     (wl-summary-toggle-disp-msg 'on)
3763     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
3764       (set-buffer cur-buf)
3765       (wl-message-prev-page 1))))
3766
3767 (defun wl-summary-next-page ()
3768   (interactive)
3769   (let ((cur-buf (current-buffer)))
3770     (wl-summary-toggle-disp-msg 'on)
3771     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
3772       (set-buffer cur-buf)
3773       (wl-message-next-page))))
3774
3775 (defun wl-summary-prev-page ()
3776   (interactive)
3777   (let ((cur-buf (current-buffer)))
3778     (wl-summary-toggle-disp-msg 'on)
3779     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
3780       (set-buffer cur-buf)
3781       (wl-message-prev-page))))
3782
3783 (defsubst wl-summary-no-mime-p (folder)
3784   (wl-string-match-member (elmo-folder-name-internal folder)
3785                           wl-summary-no-mime-folder-list))
3786
3787 (defun wl-summary-set-message-buffer-or-redisplay (&rest args)
3788   "Set message buffer.
3789 If message is not displayed yet, display it.
3790 Return t if message exists."
3791   (let ((folder wl-summary-buffer-elmo-folder)
3792         (number (wl-summary-message-number))
3793         cur-folder cur-number message-last-pos)
3794     (when (buffer-live-p wl-message-buffer)
3795       (save-window-excursion
3796         (wl-message-select-buffer wl-message-buffer)
3797         (setq cur-folder wl-message-buffer-cur-folder)
3798         (setq cur-number wl-message-buffer-cur-number)))
3799     (if (and (string= (elmo-folder-name-internal folder) (or cur-folder ""))
3800              (eq number (or cur-number 0)))
3801         (progn
3802           (set-buffer wl-message-buffer)
3803           t)
3804       (if (wl-summary-no-mime-p folder)
3805           (wl-summary-redisplay-no-mime-internal folder number)
3806         (wl-summary-redisplay-internal folder number))
3807       (when (buffer-live-p wl-message-buffer)
3808         (set-buffer wl-message-buffer))
3809       nil)))
3810
3811 (defun wl-summary-target-mark-forward (&optional arg)
3812   (interactive "P")
3813   (let ((mlist (nreverse wl-summary-buffer-target-mark-list))
3814         (summary-buf (current-buffer))
3815         (wl-draft-forward t)
3816         start-point
3817         draft-buf)
3818     (wl-summary-jump-to-msg (car mlist))
3819     (wl-summary-forward t)
3820     (setq start-point (point))
3821     (setq draft-buf (current-buffer))
3822     (setq mlist (cdr mlist))
3823     (save-window-excursion
3824       (when mlist
3825         (while mlist
3826           (set-buffer summary-buf)
3827           (wl-summary-jump-to-msg (car mlist))
3828           (wl-summary-redisplay)
3829           (set-buffer draft-buf)
3830           (goto-char (point-max))
3831           (wl-draft-insert-message)
3832           (setq mlist (cdr mlist)))
3833         (wl-draft-body-goto-top)
3834         (wl-draft-enclose-digest-region (point) (point-max)))
3835       (goto-char start-point)
3836       (save-excursion
3837         (set-buffer summary-buf)
3838         (wl-summary-delete-all-temp-marks)))
3839     (run-hooks 'wl-mail-setup-hook)))
3840
3841 (defun wl-summary-target-mark-reply-with-citation (&optional arg)
3842   (interactive "P")
3843   (let ((mlist (nreverse wl-summary-buffer-target-mark-list))
3844         (summary-buf (current-buffer))
3845         change-major-mode-hook
3846         start-point
3847         draft-buf)
3848     (wl-summary-jump-to-msg (car mlist))
3849     (when (wl-summary-reply arg t)
3850       (goto-char (point-max))
3851       (setq start-point (point-marker))
3852       (setq draft-buf (current-buffer))
3853       (save-window-excursion
3854         (while mlist
3855           (set-buffer summary-buf)
3856           (delete-other-windows)
3857           (wl-summary-jump-to-msg (car mlist))
3858           (wl-summary-redisplay)
3859           (set-buffer draft-buf)
3860           (goto-char (point-max))
3861           (wl-draft-yank-original)
3862           (setq mlist (cdr mlist)))
3863         (goto-char start-point)
3864         (save-excursion
3865           (set-buffer summary-buf)
3866           (wl-summary-delete-all-temp-marks)))
3867       (wl-draft-reply-position wl-draft-reply-default-position)
3868       (run-hooks 'wl-mail-setup-hook))))
3869
3870 (defun wl-summary-reply-with-citation (&optional arg)
3871   (interactive "P")
3872   (when (wl-summary-reply arg t)
3873     (goto-char (point-max))
3874     (wl-draft-yank-original)
3875     (wl-draft-reply-position wl-draft-reply-default-position)
3876     (run-hooks 'wl-mail-setup-hook)))
3877
3878 (defun wl-summary-jump-to-msg-by-message-id (&optional id)
3879   (interactive)
3880   (let* ((original (wl-summary-message-number))
3881          (msgid (elmo-string (or id (read-from-minibuffer "Message-ID: "))))
3882          (entity (elmo-message-entity wl-summary-buffer-elmo-folder msgid))
3883          msg otherfld schar
3884          (errmsg (format "No message with id \"%s\" in the folder." msgid)))
3885     (if (setq msg (elmo-message-entity-number entity))
3886         (progn
3887           (wl-thread-jump-to-msg msg)
3888           t)
3889       ;; for XEmacs!
3890       (if (and elmo-use-database
3891                (setq errmsg
3892                      (format
3893                       "No message with id \"%s\" in the database." msgid))
3894                (setq otherfld (elmo-database-msgid-get msgid)))
3895           (if (cdr (wl-summary-jump-to-msg-internal
3896                     (car otherfld) (nth 1 otherfld) 'no-sync))
3897               t ; succeed.
3898             ;; Back to original.
3899             (wl-summary-jump-to-msg-internal
3900              (wl-summary-buffer-folder-name) original 'no-sync))
3901         (cond ((eq wl-summary-search-via-nntp 'confirm)
3902                (require 'elmo-nntp)
3903                (message "Search message in nntp server \"%s\" <y/n/s(elect)>? "
3904                         elmo-nntp-default-server)
3905                (setq schar (let ((cursor-in-echo-area t)) (read-char)))
3906                (cond ((eq schar ?y)
3907                       (wl-summary-jump-to-msg-by-message-id-via-nntp msgid))
3908                      ((eq schar ?s)
3909                       (wl-summary-jump-to-msg-by-message-id-via-nntp
3910                        msgid
3911                        (read-from-minibuffer "NNTP Server: ")))
3912                      (t
3913                       (message "%s" errmsg)
3914                       nil)))
3915               ((or (eq wl-summary-search-via-nntp 'force)
3916                    (and
3917                     (eq (elmo-folder-type-internal wl-summary-buffer-elmo-folder)
3918                         'nntp)
3919                     wl-summary-search-via-nntp))
3920                (wl-summary-jump-to-msg-by-message-id-via-nntp msgid))
3921               (t
3922                (message "%s" errmsg)
3923                nil))))))
3924
3925 (defun wl-summary-jump-to-msg-by-message-id-via-nntp (&optional id server-spec)
3926   (interactive)
3927   (let* ((msgid (elmo-string (or id (read-from-minibuffer "Message-ID: "))))
3928          newsgroups folder ret
3929          user server port type spec)
3930     (if server-spec
3931         (if (string-match "^-" server-spec)
3932             (setq spec (wl-folder-get-elmo-folder server-spec)
3933                   user (elmo-net-folder-user-internal spec)
3934                   server (elmo-net-folder-server-internal spec)
3935                   port (elmo-net-folder-port-internal spec)
3936                   type (elmo-net-folder-stream-type-internal spec))
3937           (setq server server-spec)))
3938     (when (setq ret (elmo-nntp-get-newsgroup-by-msgid
3939                      msgid
3940                      (or server elmo-nntp-default-server)
3941                      (or user elmo-nntp-default-user)
3942                      (or port elmo-nntp-default-port)
3943                      (or type elmo-nntp-default-stream-type)))
3944       (setq newsgroups (elmo-nntp-parse-newsgroups ret))
3945       (setq folder (concat "-" (car newsgroups)
3946                            (elmo-nntp-folder-postfix user server port type)))
3947       (catch 'found
3948         (while newsgroups
3949           (if (wl-folder-entity-exists-p (car newsgroups)
3950                                          wl-folder-newsgroups-hashtb)
3951               (throw 'found
3952                      (setq folder (concat "-" (car newsgroups)
3953                                           (elmo-nntp-folder-postfix
3954                                            user server port type)))))
3955           (setq newsgroups (cdr newsgroups)))))
3956     (if ret
3957         (wl-summary-jump-to-msg-internal folder nil 'update msgid)
3958       (message "No message id \"%s\" in nntp server \"%s\"."
3959                msgid (or server elmo-nntp-default-server))
3960       nil)))
3961
3962 (defun wl-summary-jump-to-msg-internal (folder msg scan-type &optional msgid)
3963   (let (wl-auto-select-first entity)
3964     (if (or (string= folder (wl-summary-buffer-folder-name))
3965             (y-or-n-p
3966              (format
3967               "Message was found in the folder \"%s\". Jump to it? "
3968               folder)))
3969         (progn
3970           (unwind-protect
3971               (wl-summary-goto-folder-subr
3972                folder scan-type nil nil t)
3973             (if msgid
3974                 (setq msg
3975                       (elmo-message-entity-number
3976                        (elmo-message-entity
3977                         wl-summary-buffer-elmo-folder
3978                         msgid))))
3979             (setq entity (wl-folder-search-entity-by-name folder
3980                                                           wl-folder-entity
3981                                                           'folder))
3982             (if entity
3983                 (wl-folder-set-current-entity-id
3984                  (wl-folder-get-entity-id entity))))
3985           (if (null msg)
3986               (message "Message was not found currently in this folder.")
3987             (setq msg (and (wl-thread-jump-to-msg msg) msg)))
3988           (cons folder msg)))))
3989
3990 (defun wl-summary-jump-to-parent-message (arg)
3991   (interactive "P")
3992   (let ((cur-buf (current-buffer))
3993         (disp-msg wl-summary-buffer-disp-msg)
3994         (number (wl-summary-message-number))
3995         (regexp "\\(<[^<>]*>\\)[ \t]*$")
3996         (i -1) ;; xxx
3997         msg-id msg-num ref-list ref irt)
3998     (if (null number)
3999         (message "No message.")
4000       (when (eq wl-summary-buffer-view 'thread)
4001         (cond ((and arg (not (numberp arg)))
4002                (setq msg-num
4003                      (wl-thread-entity-get-number
4004                       (wl-thread-entity-get-top-entity
4005                        (wl-thread-get-entity number)))))
4006               ((and arg (numberp arg))
4007                (setq i 0)
4008                (setq msg-num number)
4009                (while (< i arg)
4010                  (setq msg-num
4011                        (wl-thread-entity-get-number
4012                         (wl-thread-entity-get-parent-entity
4013                          (wl-thread-get-entity msg-num))))
4014                  (setq i (1+ i))))
4015               (t (setq msg-num
4016                        (wl-thread-entity-get-number
4017                         (wl-thread-entity-get-parent-entity
4018                          (wl-thread-get-entity number)))))))
4019       (when (null msg-num)
4020         (wl-summary-set-message-buffer-or-redisplay)
4021         (set-buffer (wl-message-get-original-buffer))
4022         (message "Searching parent message...")
4023         (setq ref (std11-field-body "References")
4024               irt (std11-field-body "In-Reply-To"))
4025         (cond
4026          ((and arg (not (numberp arg)) ref (not (string= ref ""))
4027                (string-match regexp ref))
4028           ;; The first message of the thread.
4029           (setq msg-id (wl-match-string 1 ref)))
4030          ;; "In-Reply-To:" has only one msg-id.
4031          ((and (null arg) irt (not (string= irt ""))
4032                (string-match regexp irt))
4033           (setq msg-id (wl-match-string 1 irt)))
4034          ((and (or (null arg) (numberp arg)) ref (not (string= ref ""))
4035                (string-match regexp ref))
4036           ;; "^" searching parent, "C-u 2 ^" looking for grandparent.
4037           (while (string-match regexp ref)
4038             (setq ref-list
4039                   (append (list
4040                            (wl-match-string 1 ref))
4041                           ref-list))
4042             (setq ref (substring ref (match-end 0)))
4043             (setq i (1+ i)))
4044           (setq msg-id
4045                 (if (null arg) (nth 0 ref-list) ;; previous
4046                   (if (<= arg i) (nth (1- arg) ref-list)
4047                     (nth i ref-list))))))
4048         (set-buffer cur-buf)
4049         (or disp-msg (wl-summary-toggle-disp-msg 'off)))
4050       (cond ((and (null msg-id) (null msg-num))
4051              (message "No parent message!")
4052              nil)
4053             ((and msg-id (wl-summary-jump-to-msg-by-message-id msg-id))
4054              (if wl-summary-buffer-disp-msg (wl-summary-redisplay))
4055              (message "Searching parent message...done")
4056              t)
4057             ((and msg-num (wl-summary-jump-to-msg msg-num))
4058              (if wl-summary-buffer-disp-msg (wl-summary-redisplay))
4059              (message "Searching parent message...done")
4060              t)
4061             (t ; failed.
4062              (message "Parent message was not found.")
4063              nil)))))
4064
4065 (defun wl-summary-reply (&optional arg without-setup-hook)
4066   "Reply to current message. Default is \"wide\" reply.
4067 Reply to author if invoked with ARG."
4068   (interactive "P")
4069   (let ((folder wl-summary-buffer-elmo-folder)
4070         (number (wl-summary-message-number))
4071         (summary-buf (current-buffer))
4072         (winconf (current-window-configuration))
4073         mes-buf)
4074     (when number
4075       (save-excursion
4076         (wl-summary-set-message-buffer-or-redisplay))
4077       (setq mes-buf wl-message-buffer)
4078       (wl-message-select-buffer wl-message-buffer)
4079       (set-buffer mes-buf)
4080       (condition-case err
4081           (when (setq mes-buf (wl-message-get-original-buffer))
4082             (wl-draft-reply mes-buf arg summary-buf number)
4083             (wl-draft-reply-position wl-draft-reply-default-position)
4084             (unless without-setup-hook
4085               (run-hooks 'wl-mail-setup-hook)))
4086         (error (set-window-configuration winconf)
4087                (signal (car err)(cdr err))))
4088       (with-current-buffer summary-buf
4089         (elmo-folder-set-flag folder (list number) 'answered)
4090         (wl-summary-update-persistent-mark))
4091       t)))
4092
4093 (defun wl-summary-write ()
4094   "Write a new draft from Summary."
4095   (interactive)
4096   (wl-draft (list (cons 'To ""))
4097             nil nil nil nil (wl-summary-buffer-folder-name))
4098   (run-hooks 'wl-mail-setup-hook)
4099   (mail-position-on-field "To"))
4100
4101 (defvar wl-summary-write-current-folder-functions
4102   '(wl-folder-get-newsgroups
4103     wl-folder-guess-mailing-list-by-refile-rule
4104     wl-folder-guess-mailing-list-by-folder-name)
4105   "Newsgroups or Mailing List address guess functions list.
4106 Call from `wl-summary-write-current-folder'.
4107 When guess function return nil, challenge next guess-function.")
4108
4109 (defun wl-summary-write-current-folder (&optional folder)
4110   "Write message to current FOLDER's newsgroup or mailing-list.
4111 Use function list is `wl-summary-write-current-folder-functions'."
4112   (interactive)
4113   ;; default FOLDER is current buffer folder
4114   (setq folder (or folder (wl-summary-buffer-folder-name)))
4115   (let ((func-list wl-summary-write-current-folder-functions)
4116         guess-list guess-func)
4117     (while func-list
4118       (setq guess-list (funcall (car func-list) folder))
4119       (if (null guess-list)
4120           (setq func-list (cdr func-list))
4121         (setq guess-func (car func-list))
4122         (setq func-list nil)))
4123     (if (null guess-func)
4124         (wl-summary-write)
4125       (unless (or (stringp (nth 0 guess-list))
4126                   (stringp (nth 1 guess-list))
4127                   (stringp (nth 2 guess-list)))
4128         (error "Invalid value return guess function `%s'"
4129                (symbol-name guess-func)))
4130       (wl-draft (list (cons 'To (nth 0 guess-list))
4131                       (cons 'Cc (nth 1 guess-list))
4132                       (cons 'Newsgroups (nth 2 guess-list)))
4133                 nil nil nil nil folder)
4134       (run-hooks 'wl-mail-setup-hook)
4135       (mail-position-on-field "Subject"))))
4136
4137 (defun wl-summary-forward (&optional without-setup-hook)
4138   ""
4139   (interactive)
4140   (let ((folder wl-summary-buffer-elmo-folder)
4141         (number (wl-summary-message-number))
4142         (summary-buf (current-buffer))
4143         (wl-draft-forward t)
4144         mes-buf
4145         entity subject num)
4146     (if (null number)
4147         (message "No message.")
4148       (if (and (elmo-message-use-cache-p folder number)
4149                (eq (elmo-file-cache-status
4150                     (elmo-file-cache-get
4151                      (elmo-message-field folder number 'message-id)))
4152                    'section))
4153           ;; Reload.
4154           (wl-summary-redisplay-internal nil nil 'force-reload)
4155         (wl-summary-redisplay-internal folder number))
4156       (setq mes-buf wl-message-buffer)
4157       (wl-message-select-buffer mes-buf)
4158       ;; get original subject.
4159       (if summary-buf
4160           (save-excursion
4161             (set-buffer summary-buf)
4162             (setq subject
4163                   (or (elmo-message-entity-field
4164                        (elmo-message-entity folder number) 'subject 'decode)
4165                       ""))))
4166       (set-buffer mes-buf)
4167       (wl-draft-forward subject summary-buf)
4168       (unless without-setup-hook
4169         (run-hooks 'wl-mail-setup-hook)))))
4170
4171 (defun wl-summary-click (e)
4172   (interactive "e")
4173   (mouse-set-point e)
4174   (wl-summary-read))
4175
4176 (defun wl-summary-read ()
4177   "Proceed reading message in the summary buffer."
4178   (interactive)
4179   (let ((cur-buf (current-buffer)))
4180     (wl-summary-toggle-disp-msg 'on)
4181     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
4182       (set-buffer cur-buf)
4183       (if (wl-message-next-page)
4184           (wl-summary-down t)))))
4185
4186 (defsubst wl-summary-cursor-move-surface (downward interactive)
4187   (if wl-summary-move-direction-toggle
4188       (setq wl-summary-move-direction-downward downward))
4189   (let ((start (point))
4190         (skip-tmark-regexp (wl-regexp-opt wl-summary-skip-mark-list))
4191         (skip t)
4192         (column (current-column))
4193         goto-next next-entity finfo)
4194     (beginning-of-line)
4195     (while (and skip
4196                 (not (if downward (eobp) (bobp))))
4197       (if downward
4198           (forward-line 1)
4199         (forward-line -1))
4200       (setq skip (or (string-match skip-tmark-regexp
4201                                    (wl-summary-temp-mark))
4202                      (not (and (wl-summary-message-number)
4203                                (elmo-message-accessible-p
4204                                 wl-summary-buffer-elmo-folder
4205                                 (wl-summary-message-number)))))))
4206     (if (if downward (eobp) (and (bobp) skip)) (setq goto-next t))
4207     (if (or (eobp) (and (bobp) skip))
4208         (goto-char start))
4209     (move-to-column column)
4210
4211     (if (not goto-next)
4212         (if wl-summary-buffer-disp-msg
4213             (wl-summary-redisplay))
4214       (if interactive
4215           (cond
4216            ((and (not downward) wl-summary-buffer-prev-folder-function)
4217             (funcall wl-summary-buffer-prev-folder-function))
4218            ((and downward wl-summary-buffer-next-folder-function)
4219             (funcall wl-summary-buffer-next-folder-function))
4220            (t
4221             (when wl-auto-select-next
4222               (setq next-entity
4223                     (if downward
4224                         (wl-summary-get-next-folder)
4225                       (wl-summary-get-prev-folder)))
4226               (if next-entity
4227                   (setq finfo (wl-folder-get-entity-info next-entity))))
4228             (wl-ask-folder
4229              '(lambda () (wl-summary-next-folder-or-exit next-entity))
4230              (format
4231               "No more messages. Type SPC to go to %s."
4232               (wl-summary-entity-info-msg next-entity finfo)))))))))
4233
4234 (defun wl-summary-prev (&optional interactive)
4235   (interactive)
4236   (wl-summary-cursor-move-surface nil (or interactive (interactive-p))))
4237
4238 (defun wl-summary-next (&optional interactive)
4239   (interactive)
4240   (wl-summary-cursor-move-surface t (or interactive (interactive-p))))
4241
4242 (defun wl-summary-up (&optional interactive skip-no-unread)
4243   ""
4244   (interactive)
4245   (if wl-summary-move-direction-toggle
4246       (setq wl-summary-move-direction-downward nil))
4247   (if (wl-summary-cursor-up)
4248       (if wl-summary-buffer-disp-msg
4249           (wl-summary-redisplay))
4250     (if (or interactive
4251             (interactive-p))
4252         (if wl-summary-buffer-prev-folder-function
4253             (funcall wl-summary-buffer-prev-folder-function)
4254           (let (next-entity finfo)
4255             (when wl-auto-select-next
4256               (progn
4257                 (setq next-entity (wl-summary-get-prev-unread-folder))
4258                 (if next-entity
4259                     (setq finfo (wl-folder-get-entity-info next-entity)))))
4260             (if (and skip-no-unread
4261                      (eq wl-auto-select-next 'skip-no-unread))
4262                 (wl-summary-next-folder-or-exit next-entity t)
4263               (wl-ask-folder
4264                '(lambda () (wl-summary-next-folder-or-exit next-entity t))
4265                (format
4266                 "No more unread messages. Type SPC to go to %s."
4267                 (wl-summary-entity-info-msg next-entity finfo)))))))))
4268
4269 (defun wl-summary-get-prev-folder ()
4270   (let ((folder-buf (get-buffer wl-folder-buffer-name))
4271         last-entity cur-id)
4272     (when folder-buf
4273       (setq cur-id (save-excursion (set-buffer folder-buf)
4274                                    wl-folder-buffer-cur-entity-id))
4275       (wl-folder-get-prev-folder cur-id))))
4276
4277 (defun wl-summary-get-next-folder ()
4278   (let ((folder-buf (get-buffer wl-folder-buffer-name))
4279         cur-id)
4280     (when folder-buf
4281       (setq cur-id (save-excursion (set-buffer folder-buf)
4282                                    wl-folder-buffer-cur-entity-id))
4283       (wl-folder-get-next-folder cur-id))))
4284
4285 (defun wl-summary-get-next-unread-folder ()
4286   (let ((folder-buf (get-buffer wl-folder-buffer-name))
4287         cur-id)
4288     (when folder-buf
4289       (setq cur-id (save-excursion (set-buffer folder-buf)
4290                                    wl-folder-buffer-cur-entity-id))
4291       (wl-folder-get-next-folder cur-id 'unread))))
4292
4293 (defun wl-summary-get-prev-unread-folder ()
4294   (let ((folder-buf (get-buffer wl-folder-buffer-name))
4295         cur-id)
4296     (when folder-buf
4297       (setq cur-id (save-excursion (set-buffer folder-buf)
4298                                    wl-folder-buffer-cur-entity-id))
4299       (wl-folder-get-prev-folder cur-id 'unread))))
4300
4301 (defun wl-summary-down (&optional interactive skip-no-unread)
4302   (interactive)
4303   (if wl-summary-move-direction-toggle
4304       (setq wl-summary-move-direction-downward t))
4305   (if (wl-summary-cursor-down)
4306       (if wl-summary-buffer-disp-msg
4307           (wl-summary-redisplay))
4308     (if (or interactive
4309             (interactive-p))
4310         (if wl-summary-buffer-next-folder-function
4311             (funcall wl-summary-buffer-next-folder-function)
4312           (let (next-entity finfo)
4313             (when wl-auto-select-next
4314               (setq next-entity (wl-summary-get-next-unread-folder)))
4315             (if next-entity
4316                 (setq finfo (wl-folder-get-entity-info next-entity)))
4317             (if (and skip-no-unread
4318                      (eq wl-auto-select-next 'skip-no-unread))
4319                 (wl-summary-next-folder-or-exit next-entity)
4320               (wl-ask-folder
4321                '(lambda () (wl-summary-next-folder-or-exit next-entity))
4322                (format
4323                 "No more unread messages. Type SPC to go to %s."
4324                 (wl-summary-entity-info-msg next-entity finfo)))))))))
4325
4326 (defun wl-summary-goto-last-displayed-msg ()
4327   (interactive)
4328   (unless wl-summary-buffer-last-displayed-msg
4329     (setq wl-summary-buffer-last-displayed-msg
4330           wl-summary-buffer-current-msg))
4331   (if wl-summary-buffer-last-displayed-msg
4332       (progn
4333         (wl-summary-jump-to-msg wl-summary-buffer-last-displayed-msg)
4334         (if wl-summary-buffer-disp-msg
4335             (wl-summary-redisplay)))
4336     (message "No last message.")))
4337
4338 (defun wl-summary-redisplay (&optional arg)
4339   (interactive "P")
4340   (if (and (not arg)
4341            (wl-summary-no-mime-p wl-summary-buffer-elmo-folder))
4342       (wl-summary-redisplay-no-mime)
4343     (wl-summary-redisplay-internal nil nil arg)))
4344
4345 (defsubst wl-summary-redisplay-internal (&optional folder number force-reload)
4346   (interactive)
4347   (let* ((folder (or folder wl-summary-buffer-elmo-folder))
4348          (num (or number (wl-summary-message-number)))
4349          (wl-mime-charset      wl-summary-buffer-mime-charset)
4350          (default-mime-charset wl-summary-buffer-mime-charset)
4351          no-folder-mark fld-buf fld-win thr-entity)
4352     (if (and wl-thread-open-reading-thread
4353              (eq wl-summary-buffer-view 'thread)
4354              (not (wl-thread-entity-get-opened
4355                    (setq thr-entity (wl-thread-get-entity
4356                                      num))))
4357              (wl-thread-entity-get-children thr-entity))
4358         (wl-thread-force-open))
4359     (if num
4360         (progn
4361           (setq wl-summary-buffer-disp-msg t)
4362           (setq wl-summary-buffer-last-displayed-msg
4363                 wl-summary-buffer-current-msg)
4364           ;; hide folder window
4365           (if (and (not wl-stay-folder-window)
4366                    (setq fld-buf (get-buffer wl-folder-buffer-name)))
4367               (if (setq fld-win (get-buffer-window fld-buf))
4368                   (delete-window fld-win)))
4369           (setq wl-current-summary-buffer (current-buffer))
4370           (setq no-folder-mark
4371                 ;; If cache is used, change folder-mark.
4372                 (if (wl-message-redisplay folder num
4373                                           'mime
4374                                           (or
4375                                            force-reload
4376                                            (string= (elmo-folder-name-internal
4377                                                      folder)
4378                                                     wl-draft-folder)))
4379                     nil
4380                   ;; plugged, then leave folder-mark.
4381                   (if (and (not (elmo-folder-local-p
4382                                  wl-summary-buffer-elmo-folder))
4383                            (elmo-folder-plugged-p
4384                             wl-summary-buffer-elmo-folder))
4385                       'leave)))
4386           (when (elmo-message-use-cache-p folder num)
4387             (elmo-message-set-cached folder num t))
4388           (ignore-errors
4389             (if (elmo-message-flagged-p wl-summary-buffer-elmo-folder
4390                                         num
4391                                         'unread)
4392                 (wl-summary-mark-as-read num no-folder-mark)
4393               (wl-summary-update-persistent-mark)))
4394           (setq wl-summary-buffer-current-msg num)
4395           (when wl-summary-recenter
4396             (recenter (/ (- (window-height) 2) 2))
4397             (if (not wl-summary-indent-length-limit)
4398                 (wl-horizontal-recenter)))
4399           (wl-highlight-summary-displaying)
4400           (wl-message-buffer-prefetch-next folder num
4401                                            wl-message-buffer-prefetch-depth
4402                                            (current-buffer)
4403                                            wl-summary-buffer-mime-charset)
4404           (run-hooks 'wl-summary-redisplay-hook))
4405       (message "No message to display."))))
4406
4407 (defun wl-summary-redisplay-no-mime (&optional ask-coding)
4408   "Display message without MIME decoding.
4409 If ASK-CODING is non-nil, coding-system for the message is asked."
4410   (interactive "P")
4411   (let ((elmo-mime-display-as-is-coding-system
4412          (if ask-coding
4413              (or (read-coding-system "Coding system: ")
4414                  elmo-mime-display-as-is-coding-system)
4415            elmo-mime-display-as-is-coding-system)))
4416     (wl-summary-redisplay-no-mime-internal)))
4417
4418 (defun wl-summary-redisplay-no-mime-internal (&optional folder number)
4419   (let* ((fld (or folder wl-summary-buffer-elmo-folder))
4420          (num (or number (wl-summary-message-number)))
4421          wl-break-pages)
4422     (if num
4423         (progn
4424           (setq wl-summary-buffer-disp-msg t)
4425           (setq wl-summary-buffer-last-displayed-msg
4426                 wl-summary-buffer-current-msg)
4427           (setq wl-current-summary-buffer (current-buffer))
4428           (wl-message-redisplay fld num 'as-is
4429                                 (string= (elmo-folder-name-internal fld)
4430                                          wl-draft-folder))
4431           (when (elmo-message-use-cache-p fld num)
4432             (elmo-message-set-cached fld num t))
4433           (ignore-errors
4434             (if (elmo-message-flagged-p fld num 'unread)
4435                 (wl-summary-mark-as-read num); no-folder-mark)
4436               (wl-summary-update-persistent-mark)))
4437           (setq wl-summary-buffer-current-msg num)
4438           (when wl-summary-recenter
4439             (recenter (/ (- (window-height) 2) 2))
4440             (if (not wl-summary-indent-length-limit)
4441                 (wl-horizontal-recenter)))
4442           (wl-highlight-summary-displaying)
4443           (run-hooks 'wl-summary-redisplay-hook))
4444       (message "No message to display.")
4445       (wl-ask-folder 'wl-summary-exit
4446                      "No more messages. Type SPC to go to folder mode."))))
4447
4448 (defun wl-summary-redisplay-all-header (&optional folder number)
4449   (interactive)
4450   (let* ((fld (or folder wl-summary-buffer-elmo-folder))
4451          (num (or number (wl-summary-message-number)))
4452          (wl-mime-charset      wl-summary-buffer-mime-charset)
4453          (default-mime-charset wl-summary-buffer-mime-charset))
4454     (if num
4455         (progn
4456           (setq wl-summary-buffer-disp-msg t)
4457           (setq wl-summary-buffer-last-displayed-msg
4458                 wl-summary-buffer-current-msg)
4459           (setq wl-current-summary-buffer (current-buffer))
4460           (when (elmo-message-use-cache-p fld num)
4461             (elmo-message-set-cached fld num t))
4462           (if (wl-message-redisplay fld num 'all-header
4463                                     (string= (elmo-folder-name-internal fld)
4464                                              wl-draft-folder))
4465               (wl-summary-mark-as-read num))
4466           (setq wl-summary-buffer-current-msg num)
4467           (when wl-summary-recenter
4468             (recenter (/ (- (window-height) 2) 2))
4469             (if (not wl-summary-indent-length-limit)
4470                 (wl-horizontal-recenter)))
4471           (wl-highlight-summary-displaying)
4472           (run-hooks 'wl-summary-redisplay-hook))
4473       (message "No message to display."))))
4474
4475 (defun wl-summary-jump-to-current-message ()
4476   "Jump into Message buffer."
4477   (interactive)
4478   (let (message-buf message-win)
4479     (if (setq message-buf wl-message-buffer)
4480         (if (setq message-win (get-buffer-window message-buf))
4481             (select-window message-win)
4482           (wl-message-select-buffer wl-message-buffer))
4483       (wl-summary-redisplay)
4484       (wl-message-select-buffer wl-message-buffer))))
4485
4486 (defun wl-summary-cancel-message ()
4487   "Cancel an article on news."
4488   (interactive)
4489   (if (null (wl-summary-message-number))
4490       (message "No message.")
4491     (let ((summary-buf (current-buffer))
4492           message-buf)
4493       (wl-summary-set-message-buffer-or-redisplay)
4494       (if (setq message-buf (wl-message-get-original-buffer))
4495           (set-buffer message-buf))
4496       (unless (wl-message-news-p)
4497         (set-buffer summary-buf)
4498         (if (and (eq (elmo-folder-type-internal wl-summary-buffer-elmo-folder)
4499                      'nntp)
4500                  (y-or-n-p "Cannot get Newsgroups. Fetch again? "))
4501             (progn
4502               (wl-summary-redisplay t)
4503               (wl-summary-supersedes-message))
4504           (error "This is not a news article; supersedes is impossible")))
4505       (when (yes-or-no-p "Do you really want to cancel this article? ")
4506         (let (from newsgroups message-id distribution buf)
4507           (save-excursion
4508             (setq from (std11-field-body "from")
4509                   newsgroups (std11-field-body "newsgroups")
4510                   message-id (std11-field-body "message-id")
4511                   distribution (std11-field-body "distribution"))
4512             ;; Make sure that this article was written by the user.
4513             (unless (wl-address-user-mail-address-p
4514                      (wl-address-header-extract-address
4515                       (car (wl-parse-addresses from))))
4516               (error "This article is not yours"))
4517             ;; Make control message.
4518             (setq buf (set-buffer (get-buffer-create " *message cancel*")))
4519             (setq wl-draft-buffer-cur-summary-buffer summary-buf)
4520             (buffer-disable-undo (current-buffer))
4521             (erase-buffer)
4522             (insert "Newsgroups: " newsgroups "\n"
4523                     "From: " (wl-address-header-extract-address
4524                               wl-from) "\n"
4525                               "Subject: cmsg cancel " message-id "\n"
4526                               "Control: cancel " message-id "\n"
4527                               (if distribution
4528                                   (concat "Distribution: " distribution "\n")
4529                                 "")
4530                               mail-header-separator "\n"
4531                               wl-summary-cancel-message)
4532             (message "Canceling your message...")
4533             (wl-draft-raw-send t t) ; kill when done, force-pre-hooks.
4534             (message "Canceling your message...done")))))))
4535
4536 (defun wl-summary-supersedes-message ()
4537   "Supersede current message."
4538   (interactive)
4539   (wl-summary-toggle-disp-msg 'off)
4540   (let ((summary-buf (current-buffer))
4541         message-buf from)
4542     (wl-summary-set-message-buffer-or-redisplay)
4543     (if (setq message-buf (wl-message-get-original-buffer))
4544         (set-buffer message-buf))
4545     (unless (wl-message-news-p)
4546       (set-buffer summary-buf)
4547       (if (and (eq (elmo-folder-type-internal wl-summary-buffer-elmo-folder)
4548                    'nntp)
4549                (y-or-n-p "Cannot get Newsgroups. Fetch again? "))
4550           (progn
4551             (wl-summary-redisplay t)
4552             (wl-summary-supersedes-message))
4553         (error "This is not a news article; supersedes is impossible")))
4554     (save-excursion
4555       (setq from (std11-field-body "from"))
4556       ;; Make sure that this article was written by the user.
4557       (unless (wl-address-user-mail-address-p
4558                (wl-address-header-extract-address
4559                 (car (wl-parse-addresses from))))
4560         (error "This article is not yours"))
4561       (let* ((message-id (std11-field-body "message-id"))
4562              (followup-to (std11-field-body "followup-to"))
4563              (mail-default-headers
4564               (concat mail-default-headers
4565                       "Supersedes: " message-id "\n"
4566                       (and followup-to
4567                            (concat "Followup-To: " followup-to "\n")))))
4568         (if message-buf (set-buffer message-buf))
4569         (wl-draft-edit-string (buffer-substring (point-min) (point-max)))))))
4570
4571 (defun wl-summary-save (&optional arg wl-save-dir)
4572   "Save current message to disk."
4573   (interactive)
4574   (let ((filename)
4575         (num (wl-summary-message-number)))
4576     (if (null wl-save-dir)
4577         (setq wl-save-dir wl-temporary-file-directory))
4578     (if num
4579         (save-excursion
4580           (setq filename (expand-file-name
4581                           (concat (int-to-string num)
4582                                   wl-summary-save-file-suffix)
4583                           wl-save-dir))
4584           (if (null (and arg
4585                          (null (file-exists-p filename))))
4586               (setq filename
4587                     (read-file-name "Save to file: " filename)))
4588
4589           (wl-summary-set-message-buffer-or-redisplay)
4590           (set-buffer (wl-message-get-original-buffer))
4591           (if (and (null arg) (file-exists-p filename))
4592               (if (y-or-n-p "File already exists.  override it? ")
4593                   (write-region (point-min) (point-max) filename))
4594             (write-region (point-min) (point-max) filename)))
4595       (message "No message to save."))
4596     num))
4597
4598 (defun wl-summary-save-region (beg end)
4599   (interactive "r")
4600   (save-excursion
4601     (save-restriction
4602       (wl-summary-narrow-to-region beg end)
4603       (goto-char (point-min))
4604       (let ((wl-save-dir
4605              (wl-read-directory-name "Save to directory: "
4606                                      wl-temporary-file-directory)))
4607         (if (null (file-exists-p wl-save-dir))
4608             (make-directory wl-save-dir))
4609         (if (eq wl-summary-buffer-view 'thread)
4610             (progn
4611               (while (not (eobp))
4612                 (let* ((number (wl-summary-message-number))
4613                        (entity (wl-thread-get-entity number)))
4614                   (if (wl-thread-entity-get-opened entity)
4615                       (wl-summary-save t wl-save-dir)
4616                     ;; closed
4617                     (wl-summary-save t wl-save-dir))
4618                   (forward-line 1))))
4619           (while (not (eobp))
4620             (wl-summary-save t wl-save-dir)
4621             (forward-line 1)))))))
4622
4623 ;; mew-summary-pipe-message()
4624 (defun wl-summary-pipe-message (prefix command)
4625   "Send this message via pipe."
4626   (interactive (list current-prefix-arg nil))
4627   (if (null (wl-summary-message-number))
4628       (message "No message.")
4629     (setq command (read-string "Shell command on message: "
4630                                wl-summary-shell-command-last))
4631     (if (y-or-n-p "Send this message to pipe? ")
4632         (wl-summary-pipe-message-subr prefix command))))
4633
4634 (defun wl-summary-target-mark-pipe (prefix command)
4635   "Send each marked messages via pipe."
4636   (interactive (list current-prefix-arg nil))
4637   (if (null wl-summary-buffer-target-mark-list)
4638       (message "No marked message.")
4639     (setq command (read-string "Shell command on each marked message: "
4640                                wl-summary-shell-command-last))
4641     (when (y-or-n-p "Send each marked message to pipe? ")
4642       (while (car wl-summary-buffer-target-mark-list)
4643         (let ((num (car wl-summary-buffer-target-mark-list)))
4644           (wl-thread-jump-to-msg num)
4645           (wl-summary-pipe-message-subr prefix command)
4646           (wl-summary-unmark))))))
4647
4648 (defun wl-summary-pipe-message-subr (prefix command)
4649   (save-excursion
4650     (wl-summary-set-message-buffer-or-redisplay)
4651     (set-buffer (wl-message-get-original-buffer))
4652     (if (string= command "")
4653         (setq command wl-summary-shell-command-last))
4654     (goto-char (point-min)) ; perhaps this line won't be necessary
4655     (if prefix
4656         (search-forward "\n\n"))
4657     (shell-command-on-region (point) (point-max) command nil)
4658     (setq wl-summary-shell-command-last command)))
4659
4660 (defun wl-summary-print-message (&optional arg)
4661   (interactive "P")
4662   (if (null (wl-summary-message-number))
4663       (message "No message.")
4664     (save-excursion
4665       (wl-summary-set-message-buffer-or-redisplay)
4666       (if (or (not (interactive-p))
4667               (y-or-n-p "Print ok? "))
4668           (progn
4669             (let ((buffer (generate-new-buffer " *print*")))
4670               (copy-to-buffer buffer (point-min) (point-max))
4671               (set-buffer buffer)
4672               (funcall wl-print-buffer-function)
4673               (kill-buffer buffer)))
4674         (message "")))))
4675
4676 (defun wl-summary-print-message-with-ps-print (&optional filename)
4677   "Print message via ps-print."
4678   (interactive)
4679   (if (null (wl-summary-message-number))
4680       (message "No message.")
4681     (setq filename (ps-print-preprint current-prefix-arg))
4682     (if (or (not (interactive-p))
4683             (y-or-n-p "Print ok? "))
4684         (let ((summary-buffer (current-buffer))
4685               wl-break-pages)
4686           (save-excursion
4687             (wl-summary-set-message-buffer-or-redisplay)
4688             ;; (wl-summary-redisplay-internal)
4689             (let* ((buffer (generate-new-buffer " *print*"))
4690                    (entity (progn
4691                              (set-buffer summary-buffer)
4692                              (elmo-message-entity
4693                               wl-summary-buffer-elmo-folder
4694                               (wl-summary-message-number))))
4695                    (wl-ps-subject
4696                     (and entity
4697                          (or (elmo-message-entity-field entity 'subject t)
4698                              "")))
4699                    (wl-ps-from
4700                     (and entity
4701                          (or (elmo-message-entity-field entity 'from t) "")))
4702                    (wl-ps-date
4703                     (and entity
4704                          (or (elmo-message-entity-field entity 'date) ""))))
4705               (run-hooks 'wl-ps-preprint-hook)
4706               (set-buffer wl-message-buffer)
4707               (copy-to-buffer buffer (point-min) (point-max))
4708               (set-buffer buffer)
4709               (unwind-protect
4710                   (let ((ps-left-header
4711                          (list (concat "(" wl-ps-subject ")")
4712                                (concat "(" wl-ps-from ")")))
4713                         (ps-right-header
4714                          (list "/pagenumberstring load"
4715                                (concat "(" wl-ps-date ")"))))
4716                     (run-hooks 'wl-ps-print-hook)
4717                     (funcall wl-ps-print-buffer-function filename))
4718                 (kill-buffer buffer)))))
4719       (message ""))))
4720
4721 (if (featurep 'ps-print) ; ps-print is available.
4722     (fset 'wl-summary-print-message 'wl-summary-print-message-with-ps-print))
4723
4724 (defun wl-summary-target-mark-print ()
4725   (interactive)
4726   (if (null wl-summary-buffer-target-mark-list)
4727       (message "No marked message.")
4728     (when (y-or-n-p "Print all marked messages. OK? ")
4729       (while (car wl-summary-buffer-target-mark-list)
4730         (let ((num (car wl-summary-buffer-target-mark-list)))
4731           (wl-thread-jump-to-msg num)
4732           (wl-summary-print-message)
4733           (wl-summary-unmark))))))
4734
4735 (defun wl-summary-folder-info-update ()
4736   (wl-folder-set-folder-updated
4737    (elmo-string (wl-summary-buffer-folder-name))
4738    (list 0
4739          (+ wl-summary-buffer-unread-count
4740             wl-summary-buffer-new-count)
4741          (elmo-folder-length
4742           wl-summary-buffer-elmo-folder))))
4743
4744 (defun wl-summary-get-original-buffer ()
4745   "Get original buffer for the current summary."
4746   (save-excursion
4747     (wl-summary-set-message-buffer-or-redisplay)
4748     (wl-message-get-original-buffer)))
4749
4750 (defun wl-summary-pack-number (&optional arg)
4751   (interactive "P")
4752   (elmo-folder-pack-numbers wl-summary-buffer-elmo-folder)
4753   (let (wl-use-scoring)
4754     (wl-summary-rescan nil nil t)))
4755
4756 (defun wl-summary-target-mark-uudecode ()
4757   (interactive)
4758   (let ((mlist (reverse wl-summary-buffer-target-mark-list))
4759         (summary-buf (current-buffer))
4760         (tmp-buf (get-buffer-create "*WL UUENCODE*"))
4761         orig-buf i k filename rc errmsg)
4762     (setq i 1)
4763     (setq k (length mlist))
4764     (set-buffer tmp-buf)
4765     (erase-buffer)
4766     (save-window-excursion
4767       (while mlist
4768         (set-buffer summary-buf)
4769         (wl-summary-jump-to-msg (car mlist))
4770         (wl-summary-redisplay)
4771         (set-buffer (setq orig-buf (wl-summary-get-original-buffer)))
4772         (goto-char (point-min))
4773         (cond ((= i 1) ; first
4774                (if (setq filename (wl-message-uu-substring
4775                                    orig-buf tmp-buf t
4776                                    (= i k)))
4777                    nil
4778                  (error "Can't find begin line")))
4779               ((< i k)
4780                (wl-message-uu-substring orig-buf tmp-buf))
4781               (t ; last
4782                (wl-message-uu-substring orig-buf tmp-buf nil t)))
4783         (setq i (1+ i))
4784         (setq mlist (cdr mlist)))
4785       (set-buffer tmp-buf)
4786       (message "Exec %s..." wl-prog-uudecode)
4787       (unwind-protect
4788           (let ((decode-dir wl-temporary-file-directory))
4789             (if (not wl-prog-uudecode-no-stdout-option)
4790                 (setq filename (read-file-name "Save to file: "
4791                                                (expand-file-name
4792                                                 (elmo-safe-filename filename)
4793                                                 wl-temporary-file-directory)))
4794               (setq decode-dir
4795                     (wl-read-directory-name "Save to directory: "
4796                                             wl-temporary-file-directory))
4797               (setq filename (expand-file-name filename decode-dir)))
4798             (if (file-exists-p filename)
4799                 (or (yes-or-no-p (format "File %s exists. Save anyway? "
4800                                          filename))
4801                     (error "")))
4802             (elmo-bind-directory
4803              decode-dir
4804              (setq rc
4805                    (as-binary-process
4806                     (apply 'call-process-region (point-min) (point-max)
4807                            wl-prog-uudecode t (current-buffer) nil
4808                            wl-prog-uudecode-arg))))
4809             (when (not (= 0 rc))
4810               (setq errmsg (buffer-substring (point-min)(point-max)))
4811               (error "Uudecode error: %s" errmsg))
4812             (if (not wl-prog-uudecode-no-stdout-option)
4813                 (let (file-name-handler-alist) ;; void jka-compr
4814                   (as-binary-output-file
4815                    (write-region (point-min) (point-max)
4816                                  filename nil 'no-msg))))
4817             (save-excursion
4818               (set-buffer summary-buf)
4819               (wl-summary-delete-all-temp-marks))
4820             (if (file-exists-p filename)
4821                 (message "Saved as %s" filename)))
4822         (kill-buffer tmp-buf)))))
4823
4824 ;; Someday
4825 ;; (defun wl-summary-drop-unsync ()
4826 ;;   "Drop all unsync messages."
4827 ;;   (interactive)
4828 ;;   (if (elmo-folder-pipe-p (wl-summary-buffer-folder-name))
4829 ;;       (error "You cannot drop unsync messages in this folder"))
4830 ;;   (if (or (not (interactive-p))
4831 ;;        (y-or-n-p "Drop all unsync messages? "))
4832 ;;       (let* ((folder-list (elmo-folder-get-primitive-folder-list
4833 ;;                         (wl-summary-buffer-folder-name)))
4834 ;;           (is-multi (elmo-multi-p (wl-summary-buffer-folder-name)))
4835 ;;           (sum 0)
4836 ;;           (multi-num 0)
4837 ;;           pair)
4838 ;;      (message "Dropping...")
4839 ;;      (while folder-list
4840 ;;        (setq pair (elmo-folder-message-numbers (car folder-list)))
4841 ;;        (when is-multi ;; dirty hack...
4842 ;;          (incf multi-num)
4843 ;;          (setcar pair (+ (* multi-num elmo-multi-divide-number)
4844 ;;                          (car pair))))
4845 ;;        (elmo-msgdb-set-number-alist
4846 ;;         (wl-summary-buffer-msgdb)
4847 ;;         (nconc
4848 ;;          (elmo-msgdb-get-number-alist (wl-summary-buffer-msgdb))
4849 ;;          (list (cons (car pair) nil))))
4850 ;;        (setq sum (+ sum (cdr pair)))
4851 ;;        (setq folder-list (cdr folder-list)))
4852 ;;      (wl-summary-set-message-modified)
4853 ;;      (wl-folder-set-folder-updated (wl-summary-buffer-folder-name)
4854 ;;                                    (list 0
4855 ;;                                          (+ wl-summary-buffer-unread-count
4856 ;;                                             wl-summary-buffer-new-count)
4857 ;;                                          sum))
4858 ;;      (message "Dropping...done"))))
4859
4860 (defun wl-summary-default-get-next-msg (msg)
4861   (or (wl-summary-next-message msg
4862                                (if wl-summary-move-direction-downward 'down
4863                                  'up)
4864                                nil)
4865       (cadr (memq msg (if wl-summary-move-direction-downward
4866                           wl-summary-buffer-number-list
4867                         (reverse wl-summary-buffer-number-list))))))
4868
4869 (defun wl-summary-save-current-message ()
4870   "Save current message for `wl-summary-yank-saved-message'."
4871   (interactive)
4872   (let ((number (wl-summary-message-number)))
4873     (setq wl-summary-buffer-saved-message number)
4874     (and number (message "No: %s is saved." number))))
4875
4876 (defun wl-summary-yank-saved-message ()
4877   "Set current message as a parent of the saved message."
4878   (interactive)
4879   (if wl-summary-buffer-saved-message
4880       (let ((number (wl-summary-message-number)))
4881         (if (eq wl-summary-buffer-saved-message number)
4882             (message "Cannot set itself as a parent.")
4883           (save-excursion
4884             (wl-thread-jump-to-msg wl-summary-buffer-saved-message)
4885             (wl-thread-set-parent number)
4886             (wl-summary-set-thread-modified))
4887           (setq  wl-summary-buffer-saved-message nil)))
4888     (message "There's no saved message.")))
4889
4890 (defun wl-summary-toggle-header-narrowing ()
4891   "Toggle message header narrowing."
4892   (interactive)
4893   (when wl-message-use-header-narrowing
4894     (save-selected-window
4895       (let* ((mbuf wl-message-buffer)
4896              (mwin (when mbuf (get-buffer-window mbuf)))
4897              (wpos (when mwin (window-start mwin))))
4898         (when mbuf
4899           (set-buffer mbuf)
4900           (wl-message-header-narrowing-toggle)
4901           (and wpos (set-window-start mwin wpos)))))))
4902
4903 (autoload 'elmo-folder-list-global-flag-messages "elmo-flag")
4904
4905 (require 'product)
4906 (product-provide (provide 'wl-summary) (require 'wl-version))
4907
4908 ;;; wl-summary.el ends here