0d715e1e819f0360aab48087dce870b9f10a75ac
[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         (save-excursion
591           (goto-char beg)
592           (while (and (< (point) end) (not (eobp)))
593             (when (null (get-text-property (point) 'face))
594               (setq number (wl-summary-message-number)
595                     flags (elmo-message-flags wl-summary-buffer-elmo-folder
596                                               number))
597               (let (wl-summary-highlight)
598                 (wl-summary-update-persistent-mark number flags))
599               (wl-highlight-summary-current-line number flags))
600             (forward-line 1)))))
601     (set-buffer-modified-p nil)))
602
603 (defun wl-summary-window-scroll-functions ()
604   (cond ((and wl-summary-highlight
605               wl-summary-lazy-highlight
606               wl-summary-lazy-update-mark)
607          (list 'wl-summary-update-mark-and-highlight-window))
608         ((and wl-summary-highlight
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 (defun wl-summary-exec-with-confirmation (&optional message)
1093   (when wl-summary-buffer-temp-mark-list
1094     (if (y-or-n-p (or message
1095                       (format "Execute marks in %s? "
1096                               (wl-summary-buffer-folder-name))))
1097         (progn
1098           (wl-summary-exec)
1099           (if wl-summary-buffer-temp-mark-list
1100               (error "Some execution was failed")))
1101       ;; temp-mark-list is remained.
1102       (message ""))))
1103
1104 (defun wl-summary-cleanup-temp-marks ()
1105   (when wl-summary-buffer-temp-mark-list
1106     (wl-summary-exec-with-confirmation
1107      (format "Execute marks in %s? (answer \"n\" to discard them) "
1108              (wl-summary-buffer-folder-name)))
1109     (wl-summary-delete-all-temp-marks 'no-msg)
1110     (setq wl-summary-scored nil)))
1111
1112 ;; a subroutine for wl-summary-exit/wl-save-status
1113 ;; Note that folder is not commited here.
1114 (defun wl-summary-save-view ()
1115   ;; already in summary buffer.
1116   (when wl-summary-buffer-persistent
1117     ;; save the current summary buffer view.
1118     (if (and wl-summary-cache-use
1119              (or (wl-summary-message-modified-p)
1120                  (wl-summary-thread-modified-p)))
1121         (wl-summary-save-view-cache))))
1122
1123 (defun wl-summary-save-status ()
1124   "Save summary view and msgdb."
1125   (interactive)
1126   (if (interactive-p) (message "Saving summary status..."))
1127   (wl-summary-save-view)
1128   (elmo-folder-commit wl-summary-buffer-elmo-folder)
1129   (elmo-folder-check wl-summary-buffer-elmo-folder)
1130   (if wl-use-scoring (wl-score-save))
1131   (if (interactive-p) (message "Saving summary status...done")))
1132
1133 (defun wl-summary-force-exit ()
1134   "Exit current summary.  Buffer is deleted even the buffer is sticky."
1135   (interactive)
1136   (wl-summary-exit 'force-exit))
1137
1138 (defun wl-summary-exit (&optional force-exit)
1139   "Exit current summary.  if FORCE-EXIT, exits even the summary is sticky."
1140   (interactive "P")
1141   (let ((summary-buf (current-buffer))
1142         (sticky (wl-summary-sticky-p))
1143         summary-win
1144         message-buf message-win
1145         folder-buf folder-win)
1146     (run-hooks 'wl-summary-exit-pre-hook)
1147     (if wl-summary-buffer-exit-function
1148         (funcall wl-summary-buffer-exit-function)
1149       (if (or force-exit (not sticky))
1150           (wl-summary-cleanup-temp-marks))
1151       (unwind-protect
1152           ;; save summary status
1153           (progn
1154             (wl-summary-save-view)
1155             (if (or force-exit (not sticky))
1156                 (elmo-folder-close wl-summary-buffer-elmo-folder)
1157               (elmo-folder-commit wl-summary-buffer-elmo-folder)
1158               (elmo-folder-check wl-summary-buffer-elmo-folder))
1159             (if wl-use-scoring (wl-score-save)))
1160         ;; for sticky summary
1161         (wl-delete-all-overlays)
1162         (setq wl-summary-buffer-disp-msg nil)
1163         (elmo-kill-buffer wl-summary-search-buf-name)
1164         ;; delete message window if displayed.
1165         (if (and wl-message-buffer (get-buffer-window wl-message-buffer))
1166             (delete-window (get-buffer-window wl-message-buffer)))
1167         (if (and wl-summary-use-frame
1168                  (> (length (visible-frame-list)) 1))
1169             (delete-frame))
1170         (if (setq folder-buf (get-buffer wl-folder-buffer-name))
1171             (if wl-summary-use-frame
1172                 (let (select-frame)
1173                   (save-selected-window
1174                     (dolist (frame (visible-frame-list))
1175                       (select-frame frame)
1176                       (if (get-buffer-window folder-buf)
1177                           (setq select-frame frame))))
1178                   (if select-frame
1179                       (select-frame select-frame)
1180                     (switch-to-buffer folder-buf)))
1181               (if (setq folder-win (get-buffer-window folder-buf))
1182                   ;; folder win is already displayed.
1183                   (select-window folder-win)
1184                 ;; folder win is not displayed.
1185                 (switch-to-buffer folder-buf)))
1186           ;; currently no folder buffer
1187           (wl-folder))
1188         (and wl-folder-move-cur-folder
1189              wl-folder-buffer-cur-point
1190              (goto-char wl-folder-buffer-cur-point))
1191         (setq wl-folder-buffer-cur-path nil)
1192         (setq wl-folder-buffer-last-visited-entity-id wl-folder-buffer-cur-entity-id)
1193         (setq wl-folder-buffer-cur-entity-id nil)
1194         (wl-delete-all-overlays)
1195         (if wl-summary-exit-next-move
1196             (wl-folder-next-unsync t)
1197           (beginning-of-line))
1198         (if (setq summary-win (get-buffer-window summary-buf))
1199             (delete-window summary-win))
1200         (if (or force-exit
1201                 (not sticky))
1202             (progn
1203               (set-buffer summary-buf)
1204               (kill-buffer summary-buf)))
1205         (run-hooks 'wl-summary-exit-hook)))))
1206
1207 (defun wl-summary-suspend ()
1208   (interactive)
1209   (wl-summary-exit)
1210   (wl-folder-suspend))
1211
1212 (defun wl-summary-sync-force-update (&optional unset-cursor no-check)
1213   (interactive)
1214   (wl-summary-sync-update unset-cursor nil nil no-check))
1215
1216 (defsubst wl-summary-sync-all-init ()
1217   (wl-summary-cleanup-temp-marks)
1218   (erase-buffer)
1219   (wl-summary-set-message-modified)
1220   (setq wl-thread-entity-hashtb (elmo-make-hash
1221                                  (* (elmo-folder-length
1222                                      wl-summary-buffer-elmo-folder)
1223                                     2)))
1224   (setq wl-thread-entity-list nil)
1225   (setq wl-thread-entities nil)
1226   (setq wl-summary-buffer-number-list nil)
1227   (setq wl-summary-buffer-target-mark-list nil)
1228   (setq wl-summary-buffer-temp-mark-list nil))
1229
1230 (defun wl-summary-sync (&optional unset-cursor force-range)
1231   (interactive)
1232   (let* ((folder wl-summary-buffer-elmo-folder)
1233          (inhibit-read-only t)
1234          (buffer-read-only nil)
1235          (msgdb-dir (elmo-folder-msgdb-path folder))
1236          (range (or force-range (wl-summary-input-range
1237                                  (elmo-folder-name-internal folder)))))
1238     (cond ((string-match "rescan" range)
1239            (let ((msg (wl-summary-message-number))
1240                  (wl-use-scoring (if (string-match "noscore" range)
1241                                      nil
1242                                    wl-use-scoring)))
1243              (wl-summary-rescan nil
1244                                 (string-match "noscore" range)
1245                                 (string-match "thread" range))
1246              (and msg (wl-summary-jump-to-msg msg))))
1247           ((string= range "mark")
1248            (let ((msg (wl-summary-message-number)))
1249              (call-interactively 'wl-summary-sync-marks)
1250              (and msg (wl-summary-jump-to-msg msg))))
1251           ((string= range "cache-status")
1252            (let ((msg (wl-summary-message-number)))
1253              (wl-summary-resume-cache-status)
1254              (and msg (wl-summary-jump-to-msg msg))))
1255           ((string= range "no-sync"))
1256           ((or (string-match "^last:" range)
1257                (string-match "^first:" range))
1258            (wl-summary-goto-folder-subr (concat "/" range "/"
1259                                                 (elmo-folder-name-internal
1260                                                  folder))
1261                                         'force-update nil nil t))
1262           (t
1263            (wl-summary-sync-update unset-cursor
1264                                    (string-match "entirely" range)
1265                                    (string-match "all" range))))))
1266
1267 (defvar wl-summary-edit-addresses-candidate-fields
1268   ;; First element becomes default.
1269   '("from" "to" "cc"))
1270
1271 (defun wl-summary-edit-addresses-collect-candidate-fields (mime-charset)
1272   (let ((fields wl-summary-edit-addresses-candidate-fields)
1273         body candidates components)
1274     (while fields
1275       (setq body
1276             (mapconcat 'identity (elmo-multiple-field-body (car fields))
1277                        ","))
1278       (setq body (wl-parse-addresses body))
1279       (if body (setq candidates (append candidates body)))
1280       (setq fields (cdr fields)))
1281     (setq candidates (elmo-uniq-list candidates))
1282     (elmo-set-work-buf
1283      (set-buffer-multibyte default-enable-multibyte-characters)
1284      (mapcar (function
1285               (lambda (x)
1286                 (setq components (std11-extract-address-components x))
1287                 (cons (nth 1 components)
1288                       (and (car components)
1289                            (eword-decode-string
1290                             (decode-mime-charset-string
1291                              (car components)
1292                              mime-charset))))))
1293              candidates))))
1294
1295 (defun wl-summary-edit-addresses-subr (the-email name-in-addr)
1296   ;; returns nil if there's no change.
1297   (if (elmo-get-hash-val (downcase the-email) wl-address-petname-hash)
1298       (let (char)
1299         (message "'%s' already exists. (e)dit/(d)elete/(c)ancel?"
1300                  the-email)
1301         (while (not (or (eq (setq char (read-char)) ?\r)
1302                         (eq char ?\n)
1303                         (eq char ? )
1304                         (eq char ?e)
1305                         (eq char ?c)
1306                         (eq char ?d)))
1307           (message
1308            "Please answer `e' or `d' or `c'. (e)dit/(d)elete/(c)ancel?"))
1309         (cond
1310          ((or (eq char ?e)
1311               (eq char ?\n)
1312               (eq char ?\r)
1313               (eq char ? ))
1314           ;; Change Addresses
1315           (wl-address-add-or-change
1316            the-email
1317            (wl-address-header-extract-realname
1318             (cdr (assoc
1319                   (let ((completion-ignore-case t) comp)
1320                     (setq comp
1321                           (try-completion the-email wl-address-completion-list))
1322                     (if (equal comp t) the-email comp))
1323                   wl-address-completion-list))))
1324           "edited")
1325          ((eq char ?d)
1326           ;; Delete Addresses
1327           (if (y-or-n-p (format "Delete '%s'? "
1328                                 the-email))
1329               (progn
1330                 (wl-address-delete the-email)
1331                 "deleted")
1332             (message "")
1333             nil))
1334          (t (message "")
1335             nil)))
1336     ;; Add Petname
1337     (wl-address-add-or-change the-email name-in-addr)
1338     "added"))
1339
1340 (defun wl-summary-edit-addresses (&optional addr-str)
1341   "Edit address book interactively.
1342 Optional argument ADDR-STR is used as a target address if specified."
1343   (interactive (if current-prefix-arg
1344                    (list (read-from-minibuffer "Target address: "))))
1345   (if (null (wl-summary-message-number))
1346       (message "No message.")
1347     (save-excursion
1348       (let* ((charset wl-summary-buffer-mime-charset)
1349              (candidates
1350               (with-current-buffer (wl-summary-get-original-buffer)
1351                 (wl-summary-edit-addresses-collect-candidate-fields
1352                  charset)))
1353              address pair result)
1354         (if addr-str
1355             (setq address addr-str)
1356           (when candidates
1357             (setq address (car (car candidates)))
1358             (setq address
1359                   (completing-read
1360                    (format "Target address (%s): " address)
1361                    (mapcar
1362                     (function (lambda (x) (cons (car x) (car x))))
1363                     candidates)
1364                    nil nil nil nil address))))
1365         (when address
1366           (setq pair (assoc address candidates))
1367           (unless pair
1368             (setq pair (cons address nil)))
1369           (when (setq result (wl-summary-edit-addresses-subr (car pair) (cdr pair)))
1370             ;; update alias
1371             (wl-status-update)
1372             (setq address (assoc (car pair) wl-address-list))
1373             (if address
1374                 (message "%s, %s, <%s> is %s."
1375                          (nth 2 address)
1376                          (nth 1 address)
1377                          (nth 0 address)
1378                          result)))
1379 ;;; i'd like to update summary-buffer, but...
1380 ;;;     (wl-summary-rescan)
1381           (run-hooks 'wl-summary-edit-addresses-hook))))))
1382
1383 (defun wl-summary-incorporate (&optional arg)
1384   "Check and prefetch all uncached messages.
1385 If ARG is non-nil, checking is omitted."
1386   (interactive "P")
1387   (unless arg
1388     (save-excursion
1389       (wl-summary-sync-force-update)))
1390   (wl-summary-prefetch-region-no-mark (point-min) (point-max)
1391                                       wl-summary-incorporate-marks))
1392
1393 (defun wl-summary-prefetch-msg (number &optional arg)
1394   "Prefetch message and return non-nil value. If skipped, return nil."
1395   ;; prefetching procedure.
1396   (save-excursion
1397     (let* ((size (elmo-message-field wl-summary-buffer-elmo-folder
1398                                      number 'size))
1399            (file-cached (elmo-file-cache-exists-p
1400                          (elmo-message-field wl-summary-buffer-elmo-folder
1401                                              number 'message-id)))
1402            (force-read (and size
1403                             (or file-cached
1404                                 (and (null wl-prefetch-confirm) arg)
1405                                 (null wl-prefetch-threshold)
1406                                 (< size wl-prefetch-threshold))))
1407            mark new-mark)
1408       (ignore-errors
1409         (when (and (or arg (not file-cached))
1410                    size (not force-read) wl-prefetch-confirm)
1411           (let ((wl-message-entity (elmo-message-entity
1412                                     wl-summary-buffer-elmo-folder
1413                                     number)))
1414             (setq force-read
1415                   (save-restriction
1416                     (widen)
1417                     (y-or-n-p
1418                      (format
1419                       "Message from %s has %d bytes.  Prefetch it? "
1420                       (concat
1421                        "[ "
1422                        (save-match-data
1423                          (wl-set-string-width
1424                           17
1425                           (funcall
1426                            wl-summary-from-function
1427                            (elmo-delete-char
1428                             ?\"
1429                             (or
1430                              (elmo-message-entity-field
1431                               wl-message-entity
1432                               'from t)
1433                              "??")))))
1434                        " ]")
1435                       size))))
1436             (message "")))              ; flush.
1437         (if force-read
1438             (save-excursion
1439               (save-match-data
1440                 ;; online
1441                 (if (or arg (not file-cached))
1442                     (elmo-message-encache
1443                      wl-summary-buffer-elmo-folder
1444                      number))
1445                 (elmo-message-set-cached wl-summary-buffer-elmo-folder
1446                                          number t)
1447                 (when (and (wl-summary-jump-to-msg number)
1448                            (wl-summary-update-persistent-mark))
1449                   (sit-for 0)
1450                   (wl-summary-count-unread)
1451                   (wl-summary-update-modeline)
1452                   (wl-folder-update-unread
1453                    (wl-summary-buffer-folder-name)
1454                    (+ wl-summary-buffer-unread-count
1455                       wl-summary-buffer-new-count))))
1456               t)
1457           nil)))))
1458
1459 (defsubst wl-summary-narrow-to-region (beg end)
1460   (narrow-to-region
1461    (save-excursion
1462      (goto-char beg)
1463      (beginning-of-line)
1464      (point))
1465    (save-excursion
1466      (goto-char end)
1467      (if (eq (current-column) 0) (beginning-of-line) (end-of-line))
1468      (point))))
1469
1470 (defun wl-summary-prefetch-region-no-mark (beg end &optional prefetch-marks)
1471   (interactive "r")
1472   (let ((count 0)
1473         targets
1474         mark length
1475         entity msg
1476         start-pos pos)
1477     (save-excursion
1478       (setq start-pos (point))
1479       (save-restriction
1480         (wl-summary-narrow-to-region beg end)
1481         ;; collect prefetch targets.
1482         (message "Collecting marks...")
1483         (goto-char (point-min))
1484         (while (not (eobp))
1485           (setq mark (wl-summary-persistent-mark)
1486                 msg (wl-summary-message-number))
1487           (if (or (and (null prefetch-marks)
1488                        msg
1489                        (null (elmo-file-cache-exists-p
1490                               (elmo-message-field
1491                                wl-summary-buffer-elmo-folder
1492                                msg
1493                                'message-id))))
1494                   (member mark prefetch-marks))
1495               (setq targets (nconc targets (list msg))))
1496           (setq entity (wl-thread-get-entity msg))
1497           (if (or (not (eq wl-summary-buffer-view 'thread))
1498                   (wl-thread-entity-get-opened entity))
1499               (); opened. no hidden children.
1500             (setq targets (nconc
1501                            targets
1502                            (wl-thread-get-children-msgs-uncached
1503                             msg prefetch-marks))))
1504           (forward-line 1))
1505         (setq length (length targets))
1506         (message "Prefetching...")
1507         (while targets
1508           (when (if (not (wl-thread-entity-parent-invisible-p
1509                           (wl-thread-get-entity (car targets))))
1510                     (progn
1511                       (wl-summary-jump-to-msg (car targets))
1512                       (wl-summary-prefetch-msg
1513                        (wl-summary-message-number)))
1514                   (wl-summary-prefetch-msg (car targets)))
1515             (message "Prefetching... %d/%d message(s)"
1516                      (setq count (+ 1 count)) length))
1517           (setq targets (cdr targets)))
1518         (message "Prefetched %d/%d message(s)" count length)
1519         (cons count length)))))
1520
1521 (defun wl-summary-delete-marks-on-buffer (marks)
1522   (while marks
1523     (wl-summary-unmark (pop marks))))
1524
1525 (defun wl-summary-delete-copy-marks-on-buffer (copies)
1526   (wl-summary-delete-marks-on-buffer copies))
1527
1528 ;;;
1529 (defun wl-summary-delete-all-target-marks ()
1530   (wl-summary-delete-marks-on-buffer wl-summary-buffer-target-mark-list))
1531
1532 (defun wl-summary-mark-as-read-region (beg end)
1533   (interactive "r")
1534   (save-excursion
1535     (save-restriction
1536       (wl-summary-narrow-to-region beg end)
1537       (goto-char (point-min))
1538       (if (eq wl-summary-buffer-view 'thread)
1539           (let (number-list)
1540             (while (not (eobp))
1541               (let* ((number (wl-summary-message-number))
1542                      (entity (wl-thread-get-entity number)))
1543                 (if (wl-thread-entity-get-opened entity)
1544                     (setq number-list (append number-list (list number)))
1545                   (setq number-list
1546                         (append number-list
1547                                 (wl-thread-get-children-msgs number))))
1548                 (forward-line 1)))
1549             (wl-summary-mark-as-read number-list))
1550         (let (number-list)
1551           (while (not (eobp))
1552             (setq number-list
1553                   (append number-list (list (wl-summary-message-number))))
1554             (forward-line 1))
1555           (wl-summary-mark-as-read number-list))))))
1556
1557 (defun wl-summary-mark-as-unread-region (beg end)
1558   (interactive "r")
1559   (save-excursion
1560     (save-restriction
1561       (wl-summary-narrow-to-region beg end)
1562       (goto-char (point-min))
1563       (if (eq wl-summary-buffer-view 'thread)
1564           (let (number-list)
1565             (while (not (eobp))
1566               (let* ((number (wl-summary-message-number))
1567                      (entity (wl-thread-get-entity number)))
1568                 (if (wl-thread-entity-get-opened entity)
1569                     (setq number-list (append number-list (list number)))
1570                   (setq number-list
1571                         (append number-list
1572                                 (wl-thread-get-children-msgs number))))
1573                 (forward-line 1)))
1574             (wl-summary-mark-as-unread number-list))
1575         (let (number-list)
1576           (while (not (eobp))
1577             (setq number-list
1578                   (append number-list (list (wl-summary-message-number))))
1579             (forward-line 1))
1580           (wl-summary-mark-as-unread number-list))))))
1581
1582 (defun wl-summary-set-flags-region (beg end)
1583   (interactive "r")
1584   (save-excursion
1585     (save-restriction
1586       (wl-summary-narrow-to-region beg end)
1587       (goto-char (point-min))
1588       (let (flags)
1589         (if (eq wl-summary-buffer-view 'thread)
1590             (while (not (eobp))
1591               (let* ((number (wl-summary-message-number))
1592                      (entity (wl-thread-get-entity number))
1593                      children)
1594                 (if (wl-thread-entity-get-opened entity)
1595                     ;; opened...mark line.
1596                     ;; Crossposts are not processed
1597                     (setq flags (wl-summary-set-flags-internal
1598                                 number
1599                                 flags))
1600                   ;; closed
1601                   (setq flags (wl-summary-set-flags-internal
1602                               (wl-thread-get-children-msgs number)
1603                               flags)))
1604                 (forward-line 1)))
1605           (while (not (eobp))
1606             (setq flags (wl-summary-set-flags-internal
1607                         (wl-summary-message-number) flags))
1608             (forward-line 1))))))
1609   (wl-summary-count-unread)
1610   (wl-summary-update-modeline))
1611
1612 (defun wl-summary-mark-as-answered-region (beg end)
1613   (interactive "r")
1614   (save-excursion
1615     (save-restriction
1616       (wl-summary-narrow-to-region beg end)
1617       (goto-char (point-min))
1618       (let ((inverse (elmo-message-flagged-p wl-summary-buffer-elmo-folder
1619                                              (wl-summary-message-number)
1620                                              'answered)))
1621         (if (eq wl-summary-buffer-view 'thread)
1622             (while (not (eobp))
1623               (let* ((number (wl-summary-message-number))
1624                      (entity (wl-thread-get-entity number))
1625                      children)
1626                 (if (wl-thread-entity-get-opened entity)
1627                     ;; opened...mark line.
1628                     ;; Crossposts are not processed
1629                     (wl-summary-mark-as-answered-internal inverse)
1630                   ;; closed
1631                   (wl-summary-mark-as-answered-internal
1632                    inverse
1633                    (wl-thread-get-children-msgs number)))
1634                 (forward-line 1)))
1635           (while (not (eobp))
1636             (wl-summary-mark-as-answered-internal inverse)
1637             (forward-line 1))))))
1638   (wl-summary-count-unread)
1639   (wl-summary-update-modeline))
1640
1641 (defun wl-summary-mark-as-important-region (beg end)
1642   (interactive "r")
1643   (save-excursion
1644     (save-restriction
1645       (wl-summary-narrow-to-region beg end)
1646       (goto-char (point-min))
1647       (let ((inverse (elmo-message-flagged-p wl-summary-buffer-elmo-folder
1648                                              (wl-summary-message-number)
1649                                              'important)))
1650         (if (eq wl-summary-buffer-view 'thread)
1651             (while (not (eobp))
1652               (let* ((number (wl-summary-message-number))
1653                      (entity (wl-thread-get-entity number))
1654                      children)
1655                 (if (wl-thread-entity-get-opened entity)
1656                     ;; opened...mark line.
1657                     ;; Crossposts are not processed
1658                     (wl-summary-mark-as-important-internal inverse)
1659                   ;; closed
1660                   (wl-summary-mark-as-important-internal
1661                    inverse
1662                    (wl-thread-get-children-msgs number)))
1663                 (forward-line 1)))
1664           (while (not (eobp))
1665             (wl-summary-mark-as-important-internal inverse)
1666             (forward-line 1))))))
1667   (wl-summary-count-unread)
1668   (wl-summary-update-modeline))
1669
1670 (defun wl-summary-mark-as-read-all ()
1671   (interactive)
1672   (if (or (not (interactive-p))
1673           (y-or-n-p "Mark all messages as read? "))
1674       (let ((folder wl-summary-buffer-elmo-folder)
1675             (cur-buf (current-buffer)))
1676         (message "Setting all msgs as read...")
1677         (elmo-folder-set-flag
1678          folder
1679          (elmo-folder-list-flagged folder 'unread 'in-msgdb)
1680          'read)
1681         (save-excursion
1682           (goto-char (point-min))
1683           (while (not (eobp))
1684             (wl-summary-update-persistent-mark)
1685             (forward-line 1)))
1686         (wl-folder-update-unread (wl-summary-buffer-folder-name) 0)
1687         (setq wl-summary-buffer-unread-count 0)
1688         (setq wl-summary-buffer-new-count    0)
1689         (wl-summary-update-modeline)
1690         (message "Setting all msgs as read...done"))))
1691
1692 (defun wl-summary-delete-cache ()
1693   "Delete cache of current message."
1694   (interactive)
1695   (save-excursion
1696     (let* ((folder wl-summary-buffer-elmo-folder)
1697            number)
1698       (setq number (wl-summary-message-number))
1699       (elmo-message-set-cached folder number nil)
1700       (when (wl-summary-update-persistent-mark)
1701         (elmo-file-cache-delete
1702          (elmo-file-cache-get-path
1703           (elmo-message-field wl-summary-buffer-elmo-folder
1704                               number
1705                               'message-id)))))))
1706
1707 (defun wl-summary-resume-cache-status ()
1708   "Resume the cache status of all messages in the current folder."
1709   (interactive)
1710   (let ((folder wl-summary-buffer-elmo-folder)
1711         number msgid)
1712     (message "Resuming cache status...")
1713     (save-excursion
1714       (goto-char (point-min))
1715       (while (not (eobp))
1716         (setq number (wl-summary-message-number))
1717         (setq msgid (elmo-message-field folder number 'message-id))
1718         (elmo-message-set-cached folder number
1719                                  (elmo-file-cache-exists-p msgid))
1720         (wl-summary-update-persistent-mark)
1721         (forward-line 1))
1722       (wl-summary-count-unread)
1723       (wl-summary-update-modeline)
1724       (message "Resuming cache status...done"))))
1725
1726 (defun wl-summary-delete-messages-on-buffer (msgs &optional deleting-info)
1727   (interactive)
1728   (save-excursion
1729     (let ((inhibit-read-only t)
1730           (buffer-read-only nil)
1731           (msgs2 msgs)
1732           (len (length msgs))
1733           (i 0)
1734           ;(deleting-info (or deleting-info "Deleting..."))
1735           update-list)
1736       (elmo-kill-buffer wl-summary-search-buf-name)
1737       (while msgs
1738         (if (eq wl-summary-buffer-view 'thread)
1739             (progn
1740               ;; don't use wl-append(nconc), because list is broken. ...why?
1741               (setq update-list
1742                     (append update-list
1743                             (wl-thread-delete-message (car msgs))))
1744               (setq update-list (delq (car msgs) update-list)))
1745           (goto-char (point-min))
1746           (if (wl-summary-jump-to-msg (car msgs))
1747               (progn
1748                 (delete-region (point-at-bol) (point-at-eol))
1749                 (delete-char 1) ; delete '\n'
1750                 (setq wl-summary-buffer-number-list
1751                       (delq (car msgs) wl-summary-buffer-number-list)))))
1752 ;       (when (> len elmo-display-progress-threshold)
1753 ;         (setq i (1+ i))
1754 ;         (if (or (zerop (% i 5)) (= i len))
1755 ;             (elmo-display-progress
1756 ;              'wl-summary-delete-messages-on-buffer deleting-info
1757 ;              (/ (* i 100) len))))
1758         (setq msgs (cdr msgs)))
1759       (when (eq wl-summary-buffer-view 'thread)
1760         (wl-thread-update-line-msgs (elmo-uniq-list update-list))
1761         (wl-thread-cleanup-symbols msgs2))
1762       ;;(message (concat deleting-info "done"))
1763       (wl-summary-count-unread)
1764       (wl-summary-update-modeline)
1765       (wl-summary-folder-info-update))))
1766
1767 (defun wl-summary-update-status-marks (beg end &optional check)
1768   "Synchronize status marks on current buffer to the msgdb."
1769   (interactive "r")
1770   (save-excursion
1771     (goto-char beg)
1772     (while (and (< (point) end) (not (eobp)))
1773       (when (or (not check)
1774                 (let ((number (wl-summary-message-number)))
1775                   (when (memq number wl-summary-buffer-unsync-mark-number-list)
1776                     (setq wl-summary-buffer-unsync-mark-number-list
1777                           (delq number
1778                                 wl-summary-buffer-unsync-mark-number-list))
1779                     t)))
1780         (wl-summary-update-persistent-mark))
1781       (forward-line 1))))
1782
1783 (defun wl-summary-update-mark-window (&optional win beg)
1784   "Update persistent mark in visible summary window.
1785 This function is defined for `window-scroll-functions'"
1786   (with-current-buffer (window-buffer win)
1787     (when (eq major-mode 'wl-summary-mode)
1788       (let ((start (window-start win))
1789             (end (condition-case nil
1790                      (window-end win t) ; old emacsen doesn't support 2nd arg.
1791                    (error (window-end win)))))
1792         (wl-summary-update-status-marks start end 'check)))))
1793
1794 (defun wl-summary-insert-message (&rest args)
1795   (if (eq wl-summary-buffer-view 'thread)
1796       (apply 'wl-summary-insert-thread args)
1797     (apply 'wl-summary-insert-sequential args)))
1798
1799 (defun wl-summary-sort ()
1800   (interactive)
1801   (wl-summary-rescan
1802    (completing-read
1803     (format "Sort by (%s): " (symbol-name wl-summary-default-sort-spec))
1804     (mapcar (lambda (spec)
1805               (list (symbol-name spec)))
1806             wl-summary-sort-specs)
1807     nil t nil nil (symbol-name wl-summary-default-sort-spec))))
1808
1809 (defun wl-summary-sync-marks ()
1810   "Update persistent marks in summary."
1811   (interactive)
1812   (let ((mes "Updated ")
1813         diff diffs)
1814     ;; synchronize marks.
1815     (when (not (eq (elmo-folder-type-internal
1816                     wl-summary-buffer-elmo-folder)
1817                    'internal))
1818
1819       (message "Updating marks...")
1820       (dolist (flag elmo-global-flag-list)
1821         (unless (memq flag '(answered cached new unread))
1822           (setq diff (elmo-list-diff (elmo-folder-list-flagged
1823                                       wl-summary-buffer-elmo-folder
1824                                       flag)
1825                                      (elmo-folder-list-flagged
1826                                       wl-summary-buffer-elmo-folder
1827                                       flag 'in-msgdb)))
1828           (setq diffs (cadr diff)) ; deletes
1829           (setq mes (concat mes (format "-%d" (length diffs))))
1830           (while diffs
1831             (wl-summary-remove-flags-internal (car diffs)
1832                                              (list flag) 'no-server)
1833             (setq diffs (cdr diffs)))
1834           (setq diffs (car diff)) ; appends
1835           (setq mes (concat mes (format "/+%d %s," (length diffs) flag)))
1836           (while diffs
1837             (wl-summary-add-flags-internal (car diffs)
1838                                           (list flag) 'no-server)
1839             (setq diffs (cdr diffs)))))
1840
1841       (setq diff (elmo-list-diff (elmo-folder-list-flagged
1842                                   wl-summary-buffer-elmo-folder
1843                                   'answered)
1844                                  (elmo-folder-list-flagged
1845                                   wl-summary-buffer-elmo-folder
1846                                   'answered 'in-msgdb)))
1847       (setq diffs (cadr diff))
1848       (setq mes (concat mes (format "-%d" (length diffs))))
1849       (while diffs
1850         (wl-summary-mark-as-unanswered (car diffs) 'no-modeline)
1851         (setq diffs (cdr diffs)))
1852       (setq diffs (car diff)) ; unread-appends
1853       (setq mes (concat mes (format "/+%d answered," (length diffs))))
1854       (while diffs
1855         (wl-summary-mark-as-answered (car diffs) 'no-modeline)
1856         (setq diffs (cdr diffs)))
1857
1858       (setq diff (elmo-list-diff (elmo-folder-list-flagged
1859                                   wl-summary-buffer-elmo-folder
1860                                   'unread)
1861                                  (elmo-folder-list-flagged
1862                                   wl-summary-buffer-elmo-folder
1863                                   'unread 'in-msgdb)))
1864       (setq diffs (cadr diff))
1865       (setq mes (concat mes (format "-%d" (length diffs))))
1866       (while diffs
1867         (wl-summary-mark-as-read (car diffs) 'no-folder 'no-modeline)
1868         (setq diffs (cdr diffs)))
1869       (setq diffs (car diff)) ; unread-appends
1870       (setq mes (concat mes (format "/+%d unread." (length diffs))))
1871       (while diffs
1872         (wl-summary-mark-as-unread (car diffs) 'no-folder 'no-modeline)
1873         (setq diffs (cdr diffs)))
1874       (if (interactive-p) (message "%s" mes)))))
1875
1876 (defun wl-summary-sync-update (&optional unset-cursor
1877                                          disable-killed
1878                                          sync-all
1879                                          no-check)
1880   "Update the summary view to the newest folder status."
1881   (interactive)
1882   (let* ((folder wl-summary-buffer-elmo-folder)
1883          (elmo-mime-charset wl-summary-buffer-mime-charset)
1884          (inhibit-read-only t)
1885          (buffer-read-only nil)
1886          gc-message
1887          overview
1888          curp num i diff
1889          append-list delete-list crossed
1890          update-thread update-top-list
1891          expunged mes entity)
1892     (unwind-protect
1893         (progn
1894           (unless wl-summary-buffer-elmo-folder
1895             (error "(Internal error) Folder is not set:%s" (buffer-name
1896                                                             (current-buffer))))
1897           ;; Flush pending append operations (disconnected operation).
1898           ;;(setq seen-list
1899           ;;(wl-summary-flush-pending-append-operations seen-list))
1900           (goto-char (point-max))
1901           (wl-folder-confirm-existence folder (elmo-folder-plugged-p folder))
1902           (setq crossed (elmo-folder-synchronize folder
1903                                                  disable-killed
1904                                                  sync-all
1905                                                  no-check))
1906           (if crossed
1907               (let ((wl-summary-highlight
1908                      (and wl-summary-highlight
1909                           (not wl-summary-lazy-highlight))))
1910                 ;; Setup sync-all
1911                 (if sync-all (wl-summary-sync-all-init))
1912                 (setq diff (elmo-list-diff (elmo-folder-list-messages
1913                                             folder
1914                                             (not disable-killed)
1915                                             'in-msgdb)
1916                                            wl-summary-buffer-number-list))
1917                 (setq append-list (car diff))
1918                 (setq delete-list (cadr diff))
1919
1920                 (when delete-list
1921                   (wl-summary-delete-messages-on-buffer delete-list))
1922                 (unless wl-summary-lazy-update-mark
1923                   (wl-summary-update-status-marks (point-min) (point-max)))
1924                 (when (and wl-summary-lazy-highlight
1925                            wl-summary-lazy-update-mark)
1926                   (let (buffer-read-only)
1927                     (put-text-property (point-min) (point-max) 'face nil)))
1928                 (setq num (length append-list))
1929                 (setq i 0)
1930                 (setq wl-summary-delayed-update nil)
1931                 (elmo-kill-buffer wl-summary-search-buf-name)
1932                 (dolist (number append-list)
1933                   (setq entity (elmo-message-entity folder number))
1934                   (when (setq update-thread
1935                               (wl-summary-insert-message
1936                                entity folder
1937                                (not sync-all)))
1938                     (wl-append update-top-list update-thread))
1939                   (if elmo-use-database
1940                       (elmo-database-msgid-put
1941                        (car entity) (elmo-folder-name-internal folder)
1942                        (elmo-message-entity-number entity)))
1943                   (when (> num elmo-display-progress-threshold)
1944                     (setq i (+ i 1))
1945                     (if (or (zerop (% i 5)) (= i num))
1946                         (elmo-display-progress
1947                          'wl-summary-sync-update
1948                          (if (eq wl-summary-buffer-view 'thread)
1949                              "Making thread..."
1950                            "Inserting message...")
1951                          (/ (* i 100) num)))))
1952                 (when wl-summary-delayed-update
1953                   (while wl-summary-delayed-update
1954                     (message "Parent (%d) of message %d is no entity"
1955                              (caar wl-summary-delayed-update)
1956                              (elmo-message-entity-number
1957                               (cdar wl-summary-delayed-update)))
1958                     (when (setq update-thread
1959                                 (wl-summary-insert-message
1960                                  (cdar wl-summary-delayed-update)
1961                                  wl-summary-buffer-elmo-folder
1962                                  (not sync-all) t))
1963                       (wl-append update-top-list update-thread))
1964                     (setq wl-summary-delayed-update
1965                           (cdr wl-summary-delayed-update))))
1966                 (when (and (eq wl-summary-buffer-view 'thread)
1967                            update-top-list)
1968                   (wl-thread-update-indent-string-thread
1969                    (elmo-uniq-list update-top-list)))
1970                 (message (if (eq wl-summary-buffer-view 'thread)
1971                              "Making thread...done"
1972                            "Inserting message...done"))
1973                 (when (or delete-list append-list)
1974                   (wl-summary-set-message-modified))
1975                 (when (and sync-all (eq wl-summary-buffer-view 'thread))
1976                   (elmo-kill-buffer wl-summary-search-buf-name)
1977                   (message "Inserting message...")
1978                   (wl-thread-insert-top)
1979                   (message "Inserting message...done"))
1980                 (if elmo-use-database
1981                     (elmo-database-close))
1982                 (run-hooks 'wl-summary-sync-updated-hook)
1983                 (setq mes
1984                       (if (and (eq (length delete-list) 0)
1985                                (eq num 0))
1986                           (format
1987                            "No updates for \"%s\"" (elmo-folder-name-internal
1988                                                     folder))
1989                         (format "Updated (-%d/+%d) message(s)"
1990                                 (length delete-list) num))))
1991             (setq mes "Quit updating")))
1992       ;; synchronize marks.
1993       (if (and crossed wl-summary-auto-sync-marks)
1994           (wl-summary-sync-marks))
1995       ;; scoring
1996       (when wl-use-scoring
1997         (setq wl-summary-scored nil)
1998         (wl-summary-score-headers (and sync-all
1999                                        (wl-summary-rescore-msgs
2000                                         wl-summary-buffer-number-list))
2001                                   sync-all)
2002         (when (and wl-summary-scored
2003                    (setq expunged (wl-summary-score-update-all-lines)))
2004           (setq mes (concat mes
2005                             (format " (%d expunged)"
2006                                     (length expunged))))))
2007       (if (and crossed (> crossed 0))
2008           (setq mes
2009                 (if mes
2010                     (concat mes
2011                             (format " (%d crosspost)" crossed))
2012                   (format "%d crosspost message(s)" crossed)))
2013         (and mes (setq mes (concat mes "."))))
2014       ;; Update Folder mode
2015       (wl-folder-set-folder-updated
2016        (elmo-folder-name-internal folder)
2017        (list 0
2018              (let ((lst (wl-summary-count-unread)))
2019                (+ (car lst) (nth 1 lst)))
2020              (elmo-folder-length folder)))
2021       (wl-summary-update-modeline)
2022       ;;
2023       (unless unset-cursor
2024         (goto-char (point-min))
2025         (if (not (wl-summary-cursor-down t))
2026             (progn
2027               (goto-char (point-max))
2028               (forward-line -1))
2029           (when (and wl-summary-highlight
2030                      (not wl-summary-lazy-highlight)
2031                      (not (get-text-property (point) 'face)))
2032             (save-excursion
2033               (forward-line (- 0
2034                                (or
2035                                 wl-summary-partial-highlight-above-lines
2036                                 wl-summary-highlight-partial-threshold)))
2037               (wl-highlight-summary (point) (point-max))))))
2038       (wl-delete-all-overlays)
2039       (run-hooks 'wl-summary-buffer-window-scroll-functions)
2040       (set-buffer-modified-p nil)
2041       (if mes (message "%s" mes)))))
2042
2043 (defun wl-summary-set-score-mark (mark)
2044   (save-excursion
2045     (beginning-of-line)
2046     (let ((cur-mark (wl-summary-temp-mark)))
2047       (when (member cur-mark (list " "
2048                                    wl-summary-score-below-mark
2049                                    wl-summary-score-over-mark))
2050         (wl-summary-put-temp-mark mark)
2051         (if wl-summary-highlight
2052             (wl-highlight-summary-current-line))
2053         (set-buffer-modified-p nil)))))
2054
2055 (defun wl-summary-get-score-mark (msg-num)
2056   (let ((score (cdr (assq msg-num wl-summary-scored))))
2057     (if score
2058         (cond ((< score wl-summary-default-score)
2059                "-")
2060               ((> score wl-summary-default-score)
2061                "+")))))
2062
2063 (defun wl-summary-update-modeline ()
2064   (setq wl-summary-buffer-mode-line
2065         (funcall wl-summary-buffer-mode-line-formatter)))
2066
2067 (defun wl-summary-jump-to-msg (&optional number)
2068   (interactive)
2069   (let ((num (or number
2070                  (string-to-int
2071                   (read-from-minibuffer "Jump to Message(No.): ")))))
2072     (setq num (int-to-string num))
2073     (beginning-of-line)
2074     (if (or (and (re-search-forward (concat "\r" num "[^0-9]") nil t)
2075                  (progn (backward-char 1) t))
2076             (re-search-backward (concat "\r" num "[^0-9]") nil t))
2077         (progn (beginning-of-line) t)
2078       nil)))
2079
2080 (defun wl-summary-highlight-msgs (msgs)
2081   (save-excursion
2082     (let ((len (length msgs))
2083           i)
2084       (message "Hilighting...")
2085       (setq i 0)
2086       (while msgs
2087         (if (wl-summary-jump-to-msg (car msgs))
2088             (wl-highlight-summary-current-line))
2089         (setq msgs (cdr msgs))
2090         (when (> len elmo-display-progress-threshold)
2091           (setq i (+ i 1))
2092           (if (or (zerop (% i 5)) (= i len))
2093               (elmo-display-progress
2094                'wl-summary-highlight-msgs "Highlighting..."
2095                (/ (* i 100) len)))))
2096       (message "Highlighting...done"))))
2097
2098 (defun wl-summary-message-number ()
2099   (save-excursion
2100     (beginning-of-line)
2101     (if (or (re-search-forward "\r\\(-?[0-9]+\\)" (point-at-eol) t)
2102             (re-search-forward "^ *\\(-?[0-9]+\\)" (point-at-eol) t))
2103         (string-to-int (wl-match-buffer 1))
2104       nil)))
2105
2106 (defun wl-summary-delete-all-msgs ()
2107   (interactive)
2108   (let ((cur-buf (current-buffer))
2109         (dels (elmo-folder-list-messages wl-summary-buffer-elmo-folder)))
2110     (set-buffer cur-buf)
2111     (if (null dels)
2112         (message "No message to delete.")
2113       (if (y-or-n-p (format "%s has %d message(s).  Delete all? "
2114                             (wl-summary-buffer-folder-name)
2115                             (length dels)))
2116           (progn
2117             (message "Deleting...")
2118             (elmo-folder-move-messages wl-summary-buffer-elmo-folder dels
2119                                        'null)
2120             (wl-summary-set-message-modified)
2121             (wl-folder-set-folder-updated (wl-summary-buffer-folder-name)
2122                                           (list 0 0 0))
2123 ;;; for thread.
2124 ;;;         (setq wl-thread-top-entity '(nil t nil nil))
2125             (setq wl-summary-buffer-unread-count 0)
2126             (setq wl-summary-buffer-new-count    0)
2127             (wl-summary-update-modeline)
2128             (set-buffer cur-buf)
2129             (let ((inhibit-read-only t)
2130                   (buffer-read-only nil))
2131               (erase-buffer))
2132 ;;;         (if wl-summary-cache-use (wl-summary-save-view-cache))
2133             (message "Deleting...done")
2134             t)
2135         nil))))
2136
2137 (defun wl-summary-toggle-thread (&optional arg)
2138   "Toggle thread status (T)hread and (S)equential.
2139 If ARG, without confirm."
2140   (interactive "P")
2141   (when (or arg
2142             (y-or-n-p (format "Toggle threading? (y=%s): "
2143                               (if (eq wl-summary-buffer-view 'thread)
2144                                   "\"off\"" "\"on\""))))
2145     (if (eq wl-summary-buffer-view 'thread)
2146         (setq wl-summary-buffer-view 'sequence)
2147       (setq wl-summary-buffer-view 'thread))
2148     (wl-summary-update-modeline)
2149     (force-mode-line-update)
2150     (wl-summary-rescan nil nil t)))
2151
2152 (defun wl-summary-load-file-object (filename)
2153   "Load lisp object from dir."
2154   (save-excursion
2155     (let ((tmp-buffer (get-buffer-create " *wl-summary-load-file-object*"))
2156           insert-file-contents-pre-hook   ; To avoid autoconv-xmas...
2157           insert-file-contents-post-hook
2158           ret-val)
2159       (if (not (file-readable-p filename))
2160           ()
2161         (set-buffer tmp-buffer)
2162         (as-binary-input-file (insert-file-contents filename))
2163         (setq ret-val
2164               (condition-case nil
2165                   (read (current-buffer))
2166                 (error (error "Reading failed")))))
2167       (kill-buffer tmp-buffer)
2168       ret-val)))
2169
2170 (defun wl-summary-goto-folder (&optional arg)
2171   (interactive "P")
2172   (wl-summary-goto-folder-subr nil nil nil nil t nil arg))
2173
2174 (defun wl-summary-goto-folder-sticky ()
2175   (interactive)
2176   (wl-summary-goto-folder-subr nil nil nil t t))
2177
2178 (defun wl-summary-goto-last-visited-folder ()
2179   (interactive)
2180   (let ((entity
2181          (wl-folder-search-entity-by-name wl-summary-last-visited-folder
2182                                           wl-folder-entity
2183                                           'folder)))
2184     (if entity (wl-folder-set-current-entity-id
2185                 (wl-folder-get-entity-id entity))))
2186   (wl-summary-goto-folder-subr wl-summary-last-visited-folder nil nil nil t))
2187
2188 (defun wl-summary-sticky-p (&optional folder)
2189   (if folder
2190       (get-buffer (wl-summary-sticky-buffer-name
2191                    (elmo-folder-name-internal folder)))
2192     (not (string= wl-summary-buffer-name (buffer-name)))))
2193
2194 (defun wl-summary-always-sticky-folder-p (folder)
2195   (or (eq t wl-summary-always-sticky-folder-list)
2196       (wl-string-match-member
2197        (elmo-folder-name-internal folder)
2198        wl-summary-always-sticky-folder-list)))
2199
2200 (defun wl-summary-stick (&optional force)
2201   "Make current summary buffer sticky."
2202   (interactive "P")
2203   (if (wl-summary-sticky-p)
2204       (message "Current summary buffer is already sticky.")
2205     (when (or force (y-or-n-p "Stick current summary buffer? "))
2206       (wl-summary-toggle-disp-msg 'off)
2207       (wl-summary-switch-to-clone-buffer
2208        (wl-summary-sticky-buffer-name
2209         (wl-summary-buffer-folder-name)))
2210 ;;; ???hang up
2211 ;;;   (rename-buffer (wl-summary-sticky-buffer-name
2212 ;;;                   (wl-summary-buffer-folder-name))))
2213       (message "Folder `%s' is now sticky." (wl-summary-buffer-folder-name)))))
2214
2215 (defun wl-summary-switch-to-clone-buffer (buffer-name)
2216   (let ((cur-buf (current-buffer))
2217         (msg (wl-summary-message-number))
2218         (buf (get-buffer-create buffer-name))
2219         (folder wl-summary-buffer-elmo-folder)
2220         (copy-variables
2221          (append '(wl-summary-buffer-view
2222                    wl-summary-buffer-temp-mark-list
2223                    wl-summary-buffer-target-mark-list
2224                    wl-summary-buffer-elmo-folder
2225                    wl-summary-buffer-number-column
2226                    wl-summary-buffer-temp-mark-column
2227                    wl-summary-buffer-persistent-mark-column
2228                    wl-summary-buffer-message-modified
2229                    wl-summary-buffer-thread-modified
2230                    wl-summary-buffer-number-list
2231                    wl-summary-buffer-unsync-mark-number-list
2232                    wl-summary-buffer-folder-name
2233                    wl-summary-buffer-line-formatter)
2234                  (and (eq wl-summary-buffer-view 'thread)
2235                       '(wl-thread-entity-hashtb
2236                         wl-thread-entities
2237                         wl-thread-entity-list))
2238                  (and wl-use-scoring
2239                       '(wl-summary-scored
2240                         wl-summary-default-score
2241                         wl-summary-important-above
2242                         wl-summary-target-above
2243                         wl-summary-mark-below
2244                         wl-summary-expunge-below))
2245                  (and (featurep 'wl-score)
2246                       '(wl-current-score-file
2247                         wl-score-alist)))))
2248     (set-buffer buf)
2249     (wl-summary-mode)
2250     (wl-summary-buffer-set-folder folder)
2251     (let ((buffer-read-only nil))
2252       (insert-buffer cur-buf))
2253     (set-buffer-modified-p nil)
2254     (while copy-variables
2255       (set (car copy-variables)
2256            (save-excursion
2257              (set-buffer cur-buf)
2258              (symbol-value (car copy-variables))))
2259       (setq copy-variables (cdr copy-variables)))
2260     (switch-to-buffer buf)
2261     (kill-buffer cur-buf)
2262     (wl-summary-count-unread)
2263     (wl-summary-update-modeline)
2264     (if msg
2265         (if (eq wl-summary-buffer-view 'thread)
2266             (wl-thread-jump-to-msg msg)
2267           (wl-summary-jump-to-msg msg))
2268       (goto-char (point-max))
2269       (beginning-of-line))))
2270
2271 (defun wl-summary-get-buffer (folder)
2272   (or (and folder
2273            (get-buffer (wl-summary-sticky-buffer-name folder)))
2274       (get-buffer wl-summary-buffer-name)))
2275
2276 (defun wl-summary-get-buffer-create (name &optional force-sticky)
2277   (if force-sticky
2278       (get-buffer-create
2279        (wl-summary-sticky-buffer-name name))
2280     (or (get-buffer (wl-summary-sticky-buffer-name name))
2281         (get-buffer-create wl-summary-buffer-name))))
2282
2283 (defun wl-summary-make-number-list ()
2284   (save-excursion
2285     (goto-char (point-min))
2286     (setq wl-summary-buffer-number-list nil)
2287     (while (not (eobp))
2288       (setq wl-summary-buffer-number-list
2289             (cons (wl-summary-message-number)
2290                   wl-summary-buffer-number-list))
2291       (forward-line 1))
2292     (setq wl-summary-buffer-number-list
2293           (nreverse wl-summary-buffer-number-list))))
2294
2295 (defun wl-summary-auto-select-msg-p (unread-msg)
2296   (and unread-msg
2297        (not (elmo-message-has-global-flag-p
2298              wl-summary-buffer-elmo-folder unread-msg))))
2299
2300 (defsubst wl-summary-open-folder (folder)
2301   ;; Select folder
2302   (let ((elmo-mime-charset wl-summary-buffer-mime-charset))
2303     (unwind-protect
2304         (elmo-folder-open folder 'load-msgdb)
2305       ;; For compatibility
2306       (setq wl-summary-buffer-folder-name (elmo-folder-name-internal
2307                                            folder)))))
2308
2309 (defun wl-summary-goto-folder-subr (&optional name scan-type other-window
2310                                               sticky interactive scoring
2311                                               force-exit)
2312   "Display target folder on summary."
2313   (interactive)
2314   (let* ((keep-cursor (memq this-command
2315                             wl-summary-keep-cursor-command))
2316          (name (or name (wl-summary-read-folder wl-default-folder)))
2317          (cur-fld wl-summary-buffer-elmo-folder)
2318          folder buf mes hilit reuse-buf
2319          retval entity)
2320     (if (string= name "")
2321         (setq name wl-default-folder))
2322     (setq folder (wl-folder-get-elmo-folder name))
2323     (when (and (not (string=
2324                      (and cur-fld (elmo-folder-name-internal cur-fld))
2325                      (elmo-folder-name-internal folder))) ; folder is moved.
2326                (eq major-mode 'wl-summary-mode)) ; called in summary.
2327       (setq wl-summary-last-visited-folder (wl-summary-buffer-folder-name))
2328       (run-hooks 'wl-summary-exit-pre-hook)
2329       (if (or force-exit (not (wl-summary-sticky-p)))
2330           (wl-summary-cleanup-temp-marks))
2331       (wl-summary-save-view)
2332       (elmo-folder-commit wl-summary-buffer-elmo-folder)
2333       (if (and (wl-summary-sticky-p) force-exit)
2334           (kill-buffer (current-buffer))))
2335     (setq buf (wl-summary-get-buffer-create (elmo-folder-name-internal folder)
2336                                             sticky))
2337     (setq reuse-buf
2338           (save-excursion
2339             (set-buffer buf)
2340             (string= (elmo-folder-name-internal folder)
2341                      (wl-summary-buffer-folder-name))))
2342     (unwind-protect
2343         (if reuse-buf
2344             (if interactive
2345                 (switch-to-buffer buf)
2346               (set-buffer buf))
2347           (if other-window
2348               (delete-other-windows))
2349           (set-buffer buf)
2350           (unless (eq major-mode 'wl-summary-mode)
2351             (wl-summary-mode))
2352           (wl-summary-buffer-set-folder folder)
2353           (setq wl-summary-buffer-disp-msg nil)
2354           (setq wl-summary-buffer-last-displayed-msg nil)
2355           (setq wl-summary-buffer-current-msg nil)
2356           (let ((inhibit-read-only t)
2357                 (buffer-read-only nil))
2358             (erase-buffer)
2359             ;; Resume summary view
2360             (if wl-summary-cache-use
2361                 (let* ((dir (elmo-folder-msgdb-path folder))
2362                        (cache (expand-file-name wl-summary-cache-file dir))
2363                        (view (expand-file-name wl-summary-view-file dir)))
2364                   (when (file-exists-p cache)
2365                     (insert-file-contents-as-binary cache)
2366                     (set-buffer-multibyte
2367                      default-enable-multibyte-characters)
2368                     (decode-mime-charset-region
2369                      (point-min)(point-max)
2370                      wl-summary-buffer-mime-charset 'LF))
2371                   (if (file-exists-p view)
2372                       (setq wl-summary-buffer-view
2373                             (wl-summary-load-file-object view))
2374                     (setq wl-summary-buffer-view
2375                           (or (wl-get-assoc-list-value
2376                                wl-summary-default-view-alist
2377                                (elmo-folder-name-internal folder))
2378                               wl-summary-default-view)))
2379                   (wl-thread-resume-entity folder)
2380                   (wl-summary-open-folder folder)
2381                   (wl-summary-detect-mark-position))
2382               (setq wl-summary-buffer-view
2383                     (wl-summary-load-file-object
2384                      (expand-file-name wl-summary-view-file
2385                                        (elmo-folder-msgdb-path folder))))
2386               (wl-summary-open-folder folder)
2387               (wl-summary-detect-mark-position)
2388               (wl-summary-rescan))
2389             (wl-summary-count-unread)
2390             (wl-summary-update-modeline)))
2391       (unless (eq wl-summary-buffer-view 'thread)
2392         (wl-summary-make-number-list))
2393       (setq wl-summary-buffer-unsync-mark-number-list
2394             (copy-sequence wl-summary-buffer-number-list))
2395       (when (and wl-summary-cache-use
2396                  (or (and wl-summary-check-line-format
2397                           (wl-summary-line-format-changed-p))
2398                      (wl-summary-view-old-p)))
2399         (wl-summary-rescan))
2400       (wl-summary-toggle-disp-msg (if wl-summary-buffer-disp-msg 'on 'off))
2401       (unless (and reuse-buf keep-cursor)
2402         (unwind-protect
2403             (let ((wl-use-scoring
2404                    (if (or scoring interactive) wl-use-scoring)))
2405               (if (and (not scan-type)
2406                        interactive
2407                        (not wl-ask-range))
2408                   (setq scan-type (wl-summary-get-sync-range folder)))
2409               (cond
2410                ((eq scan-type nil)
2411                 (wl-summary-sync 'unset-cursor))
2412                ((eq scan-type 'all)
2413                 (wl-summary-sync 'unset-cursor "all"))
2414                ((eq scan-type 'no-sync))
2415                ((eq scan-type 'rescan)
2416                 (wl-summary-rescan))
2417                ((or (eq scan-type 'force-update)
2418                     (eq scan-type 'update))
2419                 (setq mes (wl-summary-sync-force-update
2420                            'unset-cursor)))))
2421           (if interactive
2422               (switch-to-buffer buf)
2423             (set-buffer buf))
2424           ;; stick always-sticky-folder
2425           (when (wl-summary-always-sticky-folder-p folder)
2426             (or (wl-summary-sticky-p) (wl-summary-stick t)))
2427           (run-hooks 'wl-summary-prepared-pre-hook)
2428           (set-buffer-modified-p nil)
2429           (goto-char (point-min))
2430           (if (wl-summary-cursor-down t)
2431               (let ((unreadp (wl-summary-next-message
2432                               (wl-summary-message-number)
2433                               'down t)))
2434                 (cond ((and wl-auto-select-first
2435                             (wl-summary-auto-select-msg-p unreadp))
2436                        ;; wl-auto-select-first is non-nil and
2437                        ;; unreadp is non-nil but not flagged
2438                        (setq retval 'disp-msg))
2439                       ((and wl-auto-prefetch-first
2440                             (wl-summary-auto-select-msg-p unreadp))
2441                        ;; wl-auto-select-first is non-nil and
2442                        ;; unreadp is non-nil but not flagged
2443                        (setq retval 'prefetch-msg))
2444                       ((not (wl-summary-auto-select-msg-p unreadp))
2445                        ;; unreadp is nil or flagged
2446                        (setq retval 'more-next))))
2447             (goto-char (point-max))
2448             (if (elmo-folder-plugged-p folder)
2449                 (forward-line -1)
2450               (wl-summary-prev))
2451             (setq retval 'more-next))
2452           (if (and wl-summary-highlight
2453                    (not wl-summary-lazy-highlight)
2454                    (not reuse-buf))
2455               (if (and wl-summary-highlight-partial-threshold
2456                        (> (count-lines (point-min) (point-max))
2457                           wl-summary-highlight-partial-threshold))
2458                   (save-excursion
2459                     (forward-line (-
2460                                    0
2461                                    (or
2462                                     wl-summary-partial-highlight-above-lines
2463                                     wl-summary-highlight-partial-threshold)))
2464                     (wl-highlight-summary (point) (point-max)))
2465                 (wl-highlight-summary (point-min) (point-max))))
2466           (if (eq retval 'disp-msg)
2467               (wl-summary-redisplay))
2468           (if (eq retval 'prefetch-msg)
2469               (wl-message-buffer-prefetch
2470                folder
2471                (wl-summary-message-number)
2472                wl-message-buffer-prefetch-depth
2473                (current-buffer)
2474                wl-summary-buffer-mime-charset))
2475           (if mes (message "%s" mes))
2476           (if (and interactive wl-summary-recenter)
2477               (recenter (/ (- (window-height) 2) 2))))))
2478     ;; set current entity-id
2479     (when (and folder
2480                (setq entity
2481                      (wl-folder-search-entity-by-name
2482                       (elmo-folder-name-internal folder)
2483                       wl-folder-entity
2484                       'folder)))
2485       ;; entity-id is unknown.
2486       (wl-folder-set-current-entity-id
2487        (wl-folder-get-entity-id entity)))
2488     (when (and wl-summary-buffer-window-scroll-functions
2489                wl-on-xemacs)
2490       (sit-for 0))
2491     (unwind-protect
2492         (run-hooks 'wl-summary-prepared-hook)
2493       (set-buffer-modified-p nil))
2494     retval))
2495
2496 (defun wl-summary-goto-previous-message-beginning ()
2497   (end-of-line)
2498   (re-search-backward "\r\\(-?[0-9]+\\)" nil t)
2499   (beginning-of-line))
2500
2501 (defun wl-summary-goto-top-of-current-thread ()
2502   (wl-summary-jump-to-msg
2503    (wl-thread-entity-get-number
2504     (wl-thread-entity-get-top-entity (wl-thread-get-entity
2505                                       (wl-summary-message-number))))))
2506
2507 (defun wl-summary-goto-bottom-of-sub-thread (&optional depth)
2508   (interactive)
2509   (let ((depth (or depth
2510                    (wl-thread-get-depth-of-current-line))))
2511     (forward-line 1)
2512     (while (and (not (eobp))
2513                 (>= (wl-thread-get-depth-of-current-line)
2514                     depth))
2515       (forward-line 1))
2516     (beginning-of-line)))
2517
2518 (defun wl-summary-insert-line (line)
2519   "Insert LINE in the Summary."
2520   (if wl-use-highlight-mouse-line
2521       ;; remove 'mouse-face of current line.
2522       (put-text-property
2523        (save-excursion (beginning-of-line)(point))
2524        (save-excursion (end-of-line)(point))
2525        'mouse-face nil))
2526   (insert line "\n")
2527   (if wl-use-highlight-mouse-line
2528       ;; remove 'mouse-face of current line.
2529       (put-text-property
2530        (save-excursion (beginning-of-line)(point))
2531        (save-excursion (end-of-line)(point))
2532        'mouse-face nil))
2533   (ignore-errors
2534     (run-hooks 'wl-summary-line-inserted-hook)))
2535
2536 (defun wl-summary-insert-sequential (entity folder &rest args)
2537   (when entity
2538     (let ((inhibit-read-only t)
2539           (number (elmo-message-entity-number entity))
2540           buffer-read-only)
2541       (goto-char (point-max))
2542       (wl-summary-insert-line
2543        (wl-summary-create-line entity nil nil
2544                                (elmo-message-flags
2545                                 wl-summary-buffer-elmo-folder
2546                                 number)
2547                                (elmo-message-cached-p
2548                                 wl-summary-buffer-elmo-folder
2549                                 number)))
2550       (setq wl-summary-buffer-number-list
2551             (wl-append wl-summary-buffer-number-list
2552                        (list (elmo-message-entity-number entity))))
2553       nil)))
2554
2555 (defun wl-summary-default-subject-filter (subject)
2556   (setq subject (elmo-replace-in-string subject "[ \t]*\\(re\\|was\\)[:>]" ""))
2557   (setq subject (elmo-replace-in-string subject "[ \t]" ""))
2558   (elmo-replace-in-string subject "^\\[.*\\]" ""))
2559
2560 (defun wl-summary-subject-equal (subject1 subject2)
2561   (string= (funcall wl-summary-subject-filter-function subject1)
2562            (funcall wl-summary-subject-filter-function subject2)))
2563
2564 (defmacro wl-summary-put-alike (alike)
2565   (` (elmo-set-hash-val (format "#%d" (wl-count-lines))
2566                         (, alike)
2567                         wl-summary-alike-hashtb)))
2568
2569 (defmacro wl-summary-get-alike ()
2570   (` (elmo-get-hash-val (format "#%d" (wl-count-lines))
2571                         wl-summary-alike-hashtb)))
2572
2573 (defun wl-summary-insert-headers (folder func mime-decode)
2574   (let ((numbers (elmo-folder-list-messages folder 'visible t))
2575         ov this last alike)
2576     (buffer-disable-undo (current-buffer))
2577     (make-local-variable 'wl-summary-alike-hashtb)
2578     (setq wl-summary-alike-hashtb (elmo-make-hash (* (length numbers) 2)))
2579     (when mime-decode
2580       (set-buffer-multibyte default-enable-multibyte-characters))
2581     (while (setq ov (elmo-message-entity folder (pop numbers)))
2582       (setq this (funcall func ov))
2583       (and this (setq this (std11-unfold-string this)))
2584       (if (equal last this)
2585           (setq alike (cons ov alike))
2586         (when last
2587           (wl-summary-put-alike alike)
2588           (insert last ?\n))
2589         (setq alike (list ov)
2590               last this)))
2591     (when last
2592       (wl-summary-put-alike alike)
2593       (insert last ?\n))
2594     (when mime-decode
2595       (decode-mime-charset-region (point-min) (point-max)
2596                                   elmo-mime-charset)
2597       (when (eq mime-decode 'mime)
2598         (eword-decode-region (point-min) (point-max))))
2599     (run-hooks 'wl-summary-insert-headers-hook)))
2600
2601 (defun wl-summary-search-by-subject (entity folder)
2602   (let ((summary-buf (current-buffer))
2603         (buf (get-buffer-create wl-summary-search-buf-name))
2604         (folder-name (wl-summary-buffer-folder-name))
2605         match founds result)
2606     (with-current-buffer buf
2607       (let ((case-fold-search t))
2608         (when (or (not (string= wl-summary-search-buf-folder-name folder-name))
2609                   (zerop (buffer-size)))
2610           (setq wl-summary-search-buf-folder-name folder-name)
2611           (message "Creating subject cache...")
2612           (wl-summary-insert-headers
2613            folder
2614            (function
2615             (lambda (x)
2616               (funcall wl-summary-subject-filter-function
2617                        (elmo-message-entity-field x 'subject))))
2618            t)
2619           (message "Creating subject cache...done"))
2620         (setq match (funcall wl-summary-subject-filter-function
2621                              (elmo-message-entity-field entity 'subject
2622                                                         'decode)))
2623         (if (string= match "")
2624             (setq match "\n"))
2625         (goto-char (point-max))
2626         (while (and (null result)
2627                     (not (= (point) (point-min)))
2628                     (search-backward match nil t))
2629           ;; check exactly match
2630           (when (and (bolp) (= (point-at-eol)(match-end 0)))
2631             (setq founds (wl-summary-get-alike))
2632             (with-current-buffer summary-buf
2633               (while founds
2634                 (when (and
2635                        ;; the first element of found-entity list exists on
2636                        ;; thread tree.
2637                        (wl-thread-get-entity
2638                         (elmo-message-entity-number (car founds)))
2639                        ;; message id is not same as myself.
2640                        (not (string=
2641                              (elmo-message-entity-field entity 'message-id)
2642                              (elmo-message-entity-field (car founds)
2643                                                         'message-id)))
2644                        ;; not a descendant.
2645                        (not (wl-thread-descendant-p
2646                              (elmo-message-entity-number entity)
2647                              (elmo-message-entity-number (car founds)))))
2648                   (setq result (car founds)
2649                         founds nil))
2650                 (setq founds (cdr founds))))))
2651         result))))
2652
2653 (defun wl-summary-insert-thread (entity folder update
2654                                         &optional force-insert)
2655   (let ((depth 0)
2656         this-id parent-entity parent-number
2657         number cur-entity linked retval delayed-entity
2658         update-list entity-stack thread-entity)
2659     (while entity
2660       (setq this-id (elmo-message-entity-field entity 'message-id)
2661             number (elmo-message-entity-number entity))
2662       (if (and wl-thread-saved-entity-hashtb-internal
2663                (setq thread-entity
2664                      (elmo-get-hash-val
2665                       (format "#%d" (elmo-message-entity-number entity))
2666                       wl-thread-saved-entity-hashtb-internal)))
2667           (setq parent-entity
2668                 (elmo-message-entity
2669                  folder
2670                  (wl-thread-entity-get-parent thread-entity))
2671                 linked (wl-thread-entity-get-linked thread-entity))
2672         (setq parent-entity (elmo-message-entity-parent folder entity)
2673               linked nil))
2674       (setq parent-number (and parent-entity
2675                                (elmo-message-entity-number parent-entity)))
2676       ;; If thread loop detected, set parent as nil.
2677       (let ((cur entity)
2678             anumber relatives)
2679         (while cur
2680           (when (setq anumber
2681                       (elmo-message-entity-number
2682                        (setq cur (elmo-message-entity-parent folder cur))))
2683             (if (memq anumber relatives)
2684                 (setq parent-number nil
2685                       cur nil))
2686             (setq relatives (cons anumber relatives)))))
2687       (if (and parent-number
2688                (not (wl-thread-get-entity parent-number))
2689                (not force-insert))
2690           ;; parent exists in overview, but not in wl-thread-entities
2691           (progn
2692             (wl-append wl-summary-delayed-update
2693                        (list (cons parent-number entity)))
2694             (setq entity nil)) ;; exit loop
2695         ;; Search parent by subject.
2696         (when (and (null parent-number)
2697                    wl-summary-search-parent-by-subject-regexp
2698                    (string-match
2699                     wl-summary-search-parent-by-subject-regexp
2700                     (elmo-message-entity-field entity 'subject)))
2701           (let ((found (wl-summary-search-by-subject entity folder)))
2702             (when (and found
2703                        (not (member found wl-summary-delayed-update)))
2704               (setq parent-entity found)
2705               (setq parent-number
2706                     (elmo-message-entity-number parent-entity))
2707               (setq linked t))))
2708         ;; If subject is change, divide thread.
2709         (if (and parent-number
2710                  wl-summary-divide-thread-when-subject-changed
2711                  (not (wl-summary-subject-equal
2712                        (or (elmo-message-entity-field entity
2713                                                       'subject t) "")
2714                        (or (elmo-message-entity-field parent-entity
2715                                                       'subject t) ""))))
2716             (setq parent-number nil))
2717         (setq retval
2718               (wl-thread-insert-message entity
2719                                         number parent-number update linked))
2720         (and retval
2721              (wl-append update-list (list retval)))
2722         (setq entity nil) ; exit loop
2723         (while (setq delayed-entity (assq number wl-summary-delayed-update))
2724           (setq wl-summary-delayed-update
2725                 (delq delayed-entity wl-summary-delayed-update))
2726           ;; update delayed message
2727           (wl-append entity-stack (list (cdr delayed-entity)))))
2728       (if (and (not entity)
2729                entity-stack)
2730           (setq entity (pop entity-stack))))
2731     update-list))
2732
2733 (defun wl-summary-update-thread (entity
2734                                  thr-entity
2735                                  parent-entity)
2736   (let* ((this-id (elmo-message-entity-field entity 'message-id))
2737          (overview-entity entity)
2738          (parent-id (elmo-message-entity-field parent-entity 'message-id))
2739          (number (elmo-message-entity-number entity))
2740          (parent-number (elmo-message-entity-number parent-entity))
2741          insert-line)
2742     (cond
2743      ((or (not parent-id)
2744           (string= this-id parent-id))
2745       (goto-char (point-max))
2746       (beginning-of-line)
2747       (setq insert-line t))
2748      ;; parent already exists in buffer.
2749      ((wl-summary-jump-to-msg parent-number)
2750       (wl-thread-goto-bottom-of-sub-thread)
2751       (setq insert-line t)))
2752     (when insert-line
2753       (let (buffer-read-only)
2754         (wl-summary-insert-line
2755          (wl-summary-create-line
2756           entity
2757           parent-entity
2758           nil
2759           (elmo-message-flags wl-summary-buffer-elmo-folder number)
2760           (elmo-message-cached-p wl-summary-buffer-elmo-folder number)
2761           (wl-thread-maybe-get-children-num number)
2762           (wl-thread-make-indent-string thr-entity)
2763           (wl-thread-entity-get-linked thr-entity)))))))
2764
2765 (defun wl-summary-target-mark-msgs (msgs)
2766   "Return the number of marked messages."
2767   (let ((i 0))
2768     (dolist (number msgs)
2769       (when (wl-summary-target-mark number)
2770         (setq i (1+ i))))
2771     i))
2772
2773 (defun wl-summary-pick (&optional from-list delete-marks)
2774   (interactive)
2775   (save-excursion
2776     (let* ((condition (car (elmo-parse-search-condition
2777                             (elmo-read-search-condition
2778                              wl-summary-pick-field-default))))
2779            (result (elmo-folder-search wl-summary-buffer-elmo-folder
2780                                        condition
2781                                        from-list))
2782            num)
2783       (if delete-marks
2784           (let ((mlist wl-summary-buffer-target-mark-list))
2785             (while mlist
2786               (when (wl-summary-jump-to-msg (car mlist))
2787                 (wl-summary-unmark))
2788               (setq mlist (cdr mlist)))
2789             (setq wl-summary-buffer-target-mark-list nil)))
2790       (if (and result
2791                (setq num (wl-summary-target-mark-msgs result))
2792                (> num 0))
2793           (if (= num (length result))
2794               (message "%d message(s) are picked." num)
2795             (message "%d(%d) message(s) are picked." num
2796                      (- (length result) num)))
2797         (message "No message was picked.")))))
2798
2799 (defun wl-summary-unvirtual ()
2800   "Exit from current virtual folder."
2801   (interactive)
2802   (if (eq 'filter
2803           (elmo-folder-type-internal wl-summary-buffer-elmo-folder))
2804       (wl-summary-goto-folder-subr
2805        (elmo-folder-name-internal
2806         (elmo-filter-folder-target-internal
2807          wl-summary-buffer-elmo-folder))
2808        'update nil nil t)
2809     (error "This folder is not filtered")))
2810
2811 (defun wl-summary-virtual (&optional arg)
2812   "Goto virtual folder.
2813 If ARG, exit virtual folder."
2814   (interactive "P")
2815   (if arg
2816       (wl-summary-unvirtual)
2817     (wl-summary-goto-folder-subr (concat "/"
2818                                          (elmo-read-search-condition
2819                                           wl-summary-pick-field-default)
2820                                          "/"
2821                                          (wl-summary-buffer-folder-name))
2822                                  'update nil nil t)
2823     (run-hooks 'wl-summary-virtual-hook)))
2824
2825 (defun wl-summary-delete-all-target-marks ()
2826   (let (wl-summary-buffer-temp-mark-list)
2827     (wl-summary-delete-all-temp-marks)))
2828
2829 (defun wl-summary-delete-all-temp-marks (&optional no-msg force)
2830   "Erase all temp marks from buffer."
2831   (interactive)
2832   (when (or wl-summary-buffer-target-mark-list
2833             wl-summary-buffer-temp-mark-list
2834             wl-summary-scored)
2835     (save-excursion
2836       (goto-char (point-min))
2837       (unless no-msg
2838         (message "Unmarking..."))
2839       (while (not (eobp))
2840         (wl-summary-unset-mark nil nil force)
2841         (forward-line 1))
2842       (unless no-msg
2843         (message "Unmarking...done"))
2844       (setq wl-summary-buffer-target-mark-list nil)
2845       (setq wl-summary-buffer-temp-mark-list nil))))
2846
2847 (defsubst wl-summary-temp-mark (&optional number)
2848   "Return temp-mark string of current line."
2849   (let ((number (or number (wl-summary-message-number)))
2850         info)
2851     (or (and (wl-summary-have-target-mark-p number)
2852              "*")
2853         (and (setq info (wl-summary-registered-temp-mark number))
2854              (nth 1 info))
2855         (wl-summary-get-score-mark number)
2856         " ")))
2857
2858 (defsubst wl-summary-persistent-mark-string (folder flags cached)
2859   "Return the persistent mark string.
2860 The mark is decided according to the FOLDER, FLAGS and CACHED."
2861   (let ((priorities wl-summary-persistent-mark-priority-list)
2862         mark)
2863     (while (and (null mark) priorities)
2864       (if (and (eq (car priorities) 'flag)
2865                (elmo-get-global-flags flags 'ignore-preserved))
2866           (let ((specs wl-summary-flag-alist)
2867                 spec)
2868             (while (setq spec (car specs))
2869               (if (memq (car spec) flags)
2870                   (setq mark (or (nth 2 spec) wl-summary-flag-mark)
2871                         specs nil)
2872                 (setq specs (cdr specs))))
2873             (unless mark
2874               (setq mark wl-summary-flag-mark)))
2875         (when (memq (car priorities) flags)
2876           (setq mark
2877                 (or (case (car priorities)
2878                       (new
2879                        (if cached
2880                            wl-summary-new-cached-mark
2881                          wl-summary-new-uncached-mark))
2882                       (answered
2883                        (if cached
2884                            wl-summary-answered-cached-mark
2885                          wl-summary-answered-uncached-mark))
2886                       (unread
2887                        (if cached
2888                            wl-summary-unread-cached-mark
2889                          wl-summary-unread-uncached-mark)))))))
2890       (setq priorities (cdr priorities)))
2891     (or mark
2892         (if (or cached (elmo-folder-local-p folder))
2893             nil
2894           wl-summary-read-uncached-mark))))
2895
2896 (defsubst wl-summary-message-mark (folder number &optional flags)
2897   "Return mark of the message."
2898   (ignore-errors
2899     (wl-summary-persistent-mark-string
2900      folder
2901      (or flags (setq flags (elmo-message-flags folder number)))
2902      (memq 'cached flags) ; XXX for speed-up.
2903      )))
2904
2905 (defsubst wl-summary-persistent-mark (&optional number flags)
2906   "Return persistent-mark string of current line."
2907   (or (wl-summary-message-mark wl-summary-buffer-elmo-folder
2908                                (or number (wl-summary-message-number))
2909                                flags)
2910       " "))
2911
2912 (defun wl-summary-put-temp-mark (mark)
2913   "Put temp MARK on current line."
2914   (when wl-summary-buffer-temp-mark-column
2915     (save-excursion
2916       (beginning-of-line)
2917       (let ((inhibit-read-only t)
2918             (buffer-read-only nil))
2919         (move-to-column wl-summary-buffer-temp-mark-column)
2920         (delete-backward-char 1)
2921         (insert mark)))))
2922
2923 (defun wl-summary-next-buffer ()
2924   "Switch to next summary buffer."
2925   (interactive)
2926   (let ((buffers (sort (wl-collect-summary)
2927                        (lambda (buffer1 buffer2)
2928                          (string-lessp (buffer-name buffer1)
2929                                        (buffer-name buffer2))))))
2930     (switch-to-buffer
2931      (or (cadr (memq (current-buffer) buffers))
2932          (car buffers)))))
2933
2934 (defun wl-summary-previous-buffer ()
2935   "Switch to previous summary buffer."
2936   (interactive)
2937   (let ((buffers (sort (wl-collect-summary)
2938                        (lambda (buffer1 buffer2)
2939                          (not (string-lessp (buffer-name buffer1)
2940                                             (buffer-name buffer2)))))))
2941     (switch-to-buffer
2942      (or (cadr (memq (current-buffer) buffers))
2943          (car buffers)))))
2944
2945 (defun wl-summary-target-mark-mark-as-read ()
2946   (interactive)
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       (wl-summary-mark-as-read wl-summary-buffer-target-mark-list)
2953       (dolist (number wl-summary-buffer-target-mark-list)
2954         (wl-summary-unset-mark number)))))
2955
2956 (defun wl-summary-target-mark-mark-as-unread ()
2957   (interactive)
2958   (save-excursion
2959     (goto-char (point-min))
2960     (let ((inhibit-read-only t)
2961           (buffer-read-only nil)
2962           wl-summary-buffer-disp-msg)
2963       (wl-summary-mark-as-unread wl-summary-buffer-target-mark-list)
2964       (dolist (number wl-summary-buffer-target-mark-list)
2965         (wl-summary-unset-mark number)))))
2966
2967 (defun wl-summary-target-mark-operation (flag)
2968   (save-excursion
2969     (goto-char (point-min))
2970     (let ((inhibit-read-only t)
2971           (buffer-read-only nil)
2972           wl-summary-buffer-disp-msg)
2973       (funcall
2974        (intern (format "wl-summary-mark-as-%s-internal" flag))
2975        nil
2976        wl-summary-buffer-target-mark-list)
2977       (dolist (number wl-summary-buffer-target-mark-list)
2978         (wl-summary-unset-mark number)))))
2979
2980 (defun wl-summary-target-mark-mark-as-important ()
2981   (interactive)
2982   (wl-summary-target-mark-operation 'important))
2983
2984 (defun wl-summary-target-mark-mark-as-answered ()
2985   (interactive)
2986   (wl-summary-target-mark-operation 'answered))
2987
2988 (defun wl-summary-target-mark-set-flags ()
2989   (interactive)
2990   (save-excursion
2991     (let ((inhibit-read-only t)
2992           (buffer-read-only nil)
2993           wl-summary-buffer-disp-msg
2994           flags)
2995       (dolist (number wl-summary-buffer-target-mark-list)
2996         (setq flags (wl-summary-set-flags-internal number flags)))
2997       (wl-summary-delete-all-target-marks)
2998       (wl-summary-count-unread)
2999       (wl-summary-update-modeline))))
3000
3001 (defun wl-summary-target-mark-save ()
3002   (interactive)
3003   (let ((wl-save-dir
3004          (wl-read-directory-name "Save to directory: "
3005                                  wl-temporary-file-directory))
3006         number)
3007     (if (null (file-exists-p wl-save-dir))
3008         (make-directory wl-save-dir))
3009     (while (setq number (car wl-summary-buffer-target-mark-list))
3010       (wl-thread-jump-to-msg number)
3011       (wl-summary-save t wl-save-dir)
3012       (wl-summary-unmark))))
3013
3014 (defun wl-summary-target-mark-pick ()
3015   (interactive)
3016   (wl-summary-pick wl-summary-buffer-target-mark-list 'delete))
3017
3018 (defun wl-summary-update-persistent-mark (&optional number flags)
3019   "Synch up persistent mark of current line with msgdb's.
3020 Return non-nil if the mark is updated"
3021   (prog1
3022       (when wl-summary-buffer-persistent-mark-column
3023         (save-excursion
3024           (move-to-column wl-summary-buffer-persistent-mark-column)
3025           (let ((inhibit-read-only t)
3026                 (buffer-read-only nil)
3027                 (mark (buffer-substring (- (point) 1) (point)))
3028                 (new-mark (wl-summary-persistent-mark number flags)))
3029             (unless (string= new-mark mark)
3030               (delete-backward-char 1)
3031               (insert new-mark)
3032               (wl-summary-set-message-modified)
3033               t))))
3034     (when wl-summary-highlight
3035       (wl-highlight-summary-current-line))
3036     (set-buffer-modified-p nil)))
3037
3038 (defsubst wl-summary-mark-as-read-internal (inverse
3039                                             number-or-numbers
3040                                             no-folder-mark
3041                                             no-modeline-update)
3042   (save-excursion
3043     (let ((folder wl-summary-buffer-elmo-folder)
3044           unread-message number
3045           number-list visible)
3046       (setq number-list (cond ((numberp number-or-numbers)
3047                                (setq unread-message
3048                                      (elmo-message-flagged-p
3049                                       folder
3050                                       number-or-numbers
3051                                       'unread))
3052                                (list number-or-numbers))
3053                               ((and (not (null number-or-numbers))
3054                                     (listp number-or-numbers))
3055                                number-or-numbers)
3056                               ((setq number (wl-summary-message-number))
3057                                ;; interactive
3058                                (setq unread-message
3059                                      (elmo-message-flagged-p
3060                                       folder
3061                                       number
3062                                       'unread))
3063                                (list number))))
3064       (if (null number-list)
3065           (message "No message.")
3066         (if inverse
3067             (elmo-folder-unset-flag folder number-list 'read no-folder-mark)
3068           (elmo-folder-set-flag folder number-list 'read no-folder-mark))
3069         (dolist (number number-list)
3070           (setq visible (wl-summary-jump-to-msg number))
3071           (unless inverse
3072             (when unread-message
3073               (run-hooks 'wl-summary-unread-message-hook)))
3074           ;; set mark on buffer
3075           (when visible
3076             (wl-summary-update-persistent-mark)))
3077         (unless no-modeline-update
3078           ;; Update unread numbers.
3079           (wl-summary-count-unread)
3080           (wl-summary-update-modeline)
3081           (wl-folder-update-unread
3082            (wl-summary-buffer-folder-name)
3083            (+ wl-summary-buffer-unread-count
3084               wl-summary-buffer-new-count)))))))
3085
3086 (defun wl-summary-mark-as-read (&optional number-or-numbers
3087                                           no-folder-mark
3088                                           no-modeline-update)
3089   (interactive)
3090   (wl-summary-mark-as-read-internal nil
3091                                     number-or-numbers
3092                                     no-folder-mark
3093                                     no-modeline-update))
3094
3095 (defun wl-summary-mark-as-unread (&optional number-or-numbers
3096                                             no-folder-mark
3097                                             no-modeline-update)
3098   (interactive)
3099   (wl-summary-mark-as-read-internal 'inverse
3100                                     number-or-numbers
3101                                     no-folder-mark
3102                                     no-modeline-update))
3103
3104 (defsubst wl-summary-mark-as-answered-internal (inverse
3105                                                 &optional
3106                                                 number-or-numbers
3107                                                 no-modeline-update)
3108   (save-excursion
3109     (let ((folder wl-summary-buffer-elmo-folder)
3110           number number-list visible)
3111       (setq number-list (cond ((numberp number-or-numbers)
3112                                (list number-or-numbers))
3113                               ((and (not (null number-or-numbers))
3114                                     (listp number-or-numbers))
3115                                number-or-numbers)
3116                               ((setq number (wl-summary-message-number))
3117                                ;; interactive
3118                                (list number))))
3119       (if (null number-list)
3120           (message "No message.")
3121         (if inverse
3122             (elmo-folder-unset-flag folder number-list 'answered)
3123           (elmo-folder-set-flag folder number-list 'answered))
3124         (dolist (number number-list)
3125           (setq visible (wl-summary-jump-to-msg number))
3126           ;; set mark on buffer
3127           (when visible
3128             (wl-summary-update-persistent-mark)))
3129         (unless no-modeline-update
3130           ;; Update unread numbers.
3131           ;; should elmo-flag-mark-as-read return unread numbers?
3132           (wl-summary-count-unread)
3133           (wl-summary-update-modeline)
3134           (wl-folder-update-unread
3135            (wl-summary-buffer-folder-name)
3136            (+ wl-summary-buffer-unread-count
3137               wl-summary-buffer-new-count)))))))
3138
3139 (defun wl-summary-mark-as-answered (&optional number-or-numbers
3140                                               no-modeline-update)
3141   (interactive)
3142   (wl-summary-mark-as-answered-internal
3143    (and (interactive-p)
3144         (elmo-message-flagged-p wl-summary-buffer-elmo-folder
3145                                 (wl-summary-message-number)
3146                                 'answered))
3147    number-or-numbers
3148    no-modeline-update))
3149
3150 (defun wl-summary-mark-as-unanswered (&optional number-or-numbers
3151                                                 no-modeline-update)
3152   (wl-summary-mark-as-answered-internal 'inverse
3153                                         number-or-numbers
3154                                         no-modeline-update))
3155
3156 (defun wl-summary-decide-flag (folder number)
3157   (let ((flags (elmo-get-global-flags (elmo-message-flags
3158                                        folder number)))
3159         (completion-ignore-case t)
3160         new-flags)
3161     (setq new-flags
3162           (delq nil
3163                 (mapcar
3164                  (lambda (flag)
3165                    (and (> (length flag) 0)
3166                         (intern (downcase flag))))
3167                  (wl-completing-read-multiple
3168                   "Flags: "
3169                   (mapcar (lambda (flag)
3170                             (list (capitalize (symbol-name flag))))
3171                           elmo-global-flag-list)
3172                   nil nil (mapconcat (lambda (flag)
3173                                        (capitalize (symbol-name flag)))
3174                                      flags
3175                                      ",")))))
3176     (dolist (flag new-flags)
3177       (unless (memq flag elmo-global-flag-list)
3178         (if (y-or-n-p (format "Flag `%s' does not exist yet. Create?"
3179                               (capitalize (symbol-name flag))))
3180             (setq elmo-global-flag-list (append
3181                                          elmo-global-flag-list
3182                                          (list flag)))
3183           (error "Stopped"))))
3184     new-flags))
3185
3186 (defsubst wl-summary-set-flags-internal (&optional
3187                                         number-or-numbers
3188                                         flags
3189                                         local
3190                                         remove-all)
3191   (save-excursion
3192     (let ((folder wl-summary-buffer-elmo-folder)
3193           number number-list visible)
3194       (setq number-list (cond ((numberp number-or-numbers)
3195                                (list number-or-numbers))
3196                               ((and (not (null number-or-numbers))
3197                                     (listp number-or-numbers))
3198                                number-or-numbers)
3199                               ((setq number (wl-summary-message-number))
3200                                ;; interactive
3201                                (list number))))
3202       (if remove-all
3203           (setq flags nil)
3204         (unless flags
3205           (setq flags (wl-summary-decide-flag folder (car number-list)))))
3206       (if (null number-list)
3207           (message "No message.")
3208         (dolist (number number-list)
3209           (elmo-message-set-global-flags folder number flags local)
3210           (setq visible (wl-summary-jump-to-msg number))
3211           ;; set mark on buffer
3212           (when visible
3213             (wl-summary-update-persistent-mark))))
3214       flags)))
3215
3216 (defsubst wl-summary-add-flags-internal (&optional
3217                                          number-or-numbers
3218                                          flags
3219                                          local)
3220   (save-excursion
3221     (let ((folder wl-summary-buffer-elmo-folder)
3222           set-flags msg number-list visible)
3223       (setq number-list (cond ((numberp number-or-numbers)
3224                                (list number-or-numbers))
3225                               ((and (not (null number-or-numbers))
3226                                     (listp number-or-numbers))
3227                                number-or-numbers)
3228                               ((setq msg (wl-summary-message-number))
3229                                ;; interactive
3230                                (list msg))))
3231       (if (null number-list)
3232           (message "No message.")
3233         (dolist (number number-list)
3234           (setq set-flags
3235                 (elmo-get-global-flags
3236                  (elmo-message-flags folder number)))
3237           (setq set-flags (nconc flags set-flags))
3238           (elmo-message-set-global-flags folder number set-flags local)
3239           (setq visible (wl-summary-jump-to-msg number))
3240           ;; set mark on buffer
3241           (when visible
3242             (wl-summary-update-persistent-mark)))))))
3243
3244 (defsubst wl-summary-remove-flags-internal (&optional
3245                                             number-or-numbers
3246                                             flags
3247                                             local)
3248   (save-excursion
3249     (let ((folder wl-summary-buffer-elmo-folder)
3250           set-flags msg number-list visible)
3251       (setq number-list (cond ((numberp number-or-numbers)
3252                                (list number-or-numbers))
3253                               ((and (not (null number-or-numbers))
3254                                     (listp number-or-numbers))
3255                                number-or-numbers)
3256                               ((setq msg (wl-summary-message-number))
3257                                ;; interactive
3258                                (list msg))))
3259       (if (null number-list)
3260           (message "No message.")
3261         (dolist (number number-list)
3262           (setq set-flags (elmo-get-global-flags
3263                            (elmo-message-flags folder number)))
3264           (dolist (flag flags)
3265             (setq set-flags (delq flag set-flags)))
3266           (elmo-message-set-global-flags folder number set-flags local)
3267           (setq visible (wl-summary-jump-to-msg number))
3268           ;; set mark on buffer
3269           (when visible
3270             (wl-summary-update-persistent-mark)))))))
3271
3272 (defun wl-summary-set-flags (&optional remove)
3273   (interactive "P")
3274   (if (eq 'flag (elmo-folder-type-internal wl-summary-buffer-elmo-folder))
3275       (error "Cannot process flags in this folder"))
3276   (wl-summary-set-flags-internal nil nil nil remove))
3277
3278 (defun wl-summary-mark-as-important-internal (inverse
3279                                               &optional number-or-numbers)
3280   (if inverse
3281       (wl-summary-remove-flags-internal number-or-numbers '(important))
3282     (wl-summary-add-flags-internal number-or-numbers '(important))))
3283
3284 (defun wl-summary-mark-as-important (&optional prompt)
3285   (interactive "P")
3286   (if (eq 'flag (elmo-folder-type-internal wl-summary-buffer-elmo-folder))
3287       (error "Cannot process flags in this folder"))
3288   (if prompt
3289       (wl-summary-set-flags-internal)
3290     (wl-summary-mark-as-important-internal
3291      (and (interactive-p)
3292           (elmo-message-flagged-p wl-summary-buffer-elmo-folder
3293                                   (wl-summary-message-number)
3294                                   'important)))))
3295
3296 ;;; Summary line.
3297 (defvar wl-summary-line-formatter nil)
3298
3299 (defun wl-summary-view-old-p ()
3300   "Return non-nil when summary view cache has old format."
3301   (save-excursion
3302     (goto-char (point-min))
3303     (and wl-summary-buffer-number-list
3304          (not (re-search-forward "\r-?[0-9]+" (point-at-eol) t)))))
3305
3306 (defun wl-summary-line-format-changed-p ()
3307   "Return non-nil when summary line format is changed."
3308   (not (string=
3309         wl-summary-buffer-line-format
3310         (or (elmo-object-load (expand-file-name
3311                                wl-summary-line-format-file
3312                                (elmo-folder-msgdb-path
3313                                 wl-summary-buffer-elmo-folder))
3314                               wl-summary-buffer-mime-charset)
3315             wl-summary-buffer-line-format))))
3316
3317 (defun wl-summary-line-format-save ()
3318   "Save current summary line format."
3319   (elmo-object-save
3320    (expand-file-name wl-summary-line-format-file
3321                      (elmo-folder-msgdb-path
3322                       wl-summary-buffer-elmo-folder))
3323    wl-summary-buffer-line-format
3324    wl-summary-buffer-mime-charset))
3325
3326 (defun wl-summary-line-number ()
3327   (wl-set-string-width
3328    (- wl-summary-buffer-number-column)
3329    (number-to-string
3330     (elmo-message-entity-number wl-message-entity))))
3331
3332 (defun wl-summary-line-year ()
3333   (aref wl-datevec 0))
3334 (defun wl-summary-line-month ()
3335   (format "%02d" (aref wl-datevec 1)))
3336 (defun wl-summary-line-day ()
3337   (format "%02d" (aref wl-datevec 2)))
3338 (defun wl-summary-line-day-of-week ()
3339   (condition-case nil
3340       (elmo-date-get-week (aref wl-datevec 0)
3341                           (aref wl-datevec 1)
3342                           (aref wl-datevec 2))
3343     (error "??")))
3344 (defun wl-summary-line-hour ()
3345   (format "%02d" (aref wl-datevec 3)))
3346 (defun wl-summary-line-minute ()
3347   (format "%02d" (aref wl-datevec 4)))
3348
3349 (defun wl-summary-line-size ()
3350   (let ((size (elmo-message-entity-field wl-message-entity 'size)))
3351     (if size
3352         (cond
3353          ((<= 1 (/ size 1048576))
3354           (format "%.0fM" (/ size 1048576.0)))
3355          ((<= 1 (/ size 1024))
3356           (format "%.0fK" (/ size 1024.0)))
3357          (t (format "%dB" size)))
3358       "")))
3359
3360 (defun wl-summary-line-subject ()
3361   (let (no-parent subject parent-raw-subject parent-subject)
3362     (if (string= wl-thr-indent-string "")
3363         (setq no-parent t)) ; no parent
3364     (setq subject
3365           (elmo-delete-char ?\n
3366                             (or (elmo-message-entity-field
3367                                  wl-message-entity
3368                                  'subject t)
3369                                 wl-summary-no-subject-message)))
3370     (setq parent-raw-subject
3371           (elmo-message-entity-field wl-parent-message-entity
3372                                      'subject t))
3373     (setq parent-subject
3374           (if parent-raw-subject
3375               (elmo-delete-char ?\n parent-raw-subject)))
3376     (if (or no-parent
3377             (null parent-subject)
3378             (not (wl-summary-subject-equal
3379                   subject parent-subject)))
3380         (funcall wl-summary-subject-function subject)
3381       "")))
3382
3383 (defun wl-summary-line-from ()
3384   (elmo-delete-char ?\n
3385                     (funcall wl-summary-from-function
3386                              (elmo-message-entity-field
3387                               wl-message-entity
3388                               'from t))))
3389
3390 (defun wl-summary-line-list-info ()
3391   (let ((list-info (wl-summary-get-list-info wl-message-entity)))
3392     (if (car list-info)
3393         (format (if (cdr list-info) "(%s %05.0f)" "(%s)")
3394                 (car list-info) (cdr list-info))
3395       "")))
3396
3397 (defun wl-summary-line-list-count ()
3398   (let ((ml-count (cdr (wl-summary-get-list-info wl-message-entity))))
3399     (if ml-count
3400         (format "%.0f" ml-count)
3401       "")))
3402
3403 (defun wl-summary-line-attached ()
3404   (let ((content-type (elmo-message-entity-field
3405                        wl-message-entity 'content-type))
3406         (case-fold-search t))
3407     (if (and content-type
3408              (string-match "multipart/mixed" content-type))
3409         "@"
3410       "")))
3411
3412 ;;; For future use.
3413 ;;(defun wl-summary-line-cached ()
3414 ;;  (if (elmo-message-cached-p wl-summary-buffer-elmo-folder
3415 ;;                           (elmo-message-entity-number wl-message-entity))
3416 ;;      " "
3417 ;;    "u"))
3418
3419 (defun wl-summary-create-line (wl-message-entity
3420                                wl-parent-message-entity
3421                                wl-temp-mark
3422                                wl-flags
3423                                wl-cached
3424                                &optional
3425                                wl-thr-children-number
3426                                wl-thr-indent-string
3427                                wl-thr-linked)
3428   "Create a summary line."
3429   (let ((wl-mime-charset wl-summary-buffer-mime-charset)
3430         (wl-persistent-mark (wl-summary-persistent-mark-string
3431                              wl-summary-buffer-elmo-folder
3432                              wl-flags
3433                              wl-cached))
3434         (elmo-mime-charset wl-summary-buffer-mime-charset)
3435         (elmo-lang wl-summary-buffer-weekday-name-lang)
3436         (wl-datevec (or (ignore-errors (timezone-fix-time
3437                                         (elmo-message-entity-field
3438                                          wl-message-entity
3439                                          'date)
3440                                         nil
3441                                         wl-summary-fix-timezone))
3442                         (make-vector 5 0)))
3443         (entity wl-message-entity) ; backward compatibility.
3444         line mark)
3445     (if (and wl-thr-indent-string
3446              wl-summary-indent-length-limit
3447              (< wl-summary-indent-length-limit
3448                 (string-width wl-thr-indent-string)))
3449         (setq wl-thr-indent-string (wl-set-string-width
3450                                     wl-summary-indent-length-limit
3451                                     wl-thr-indent-string)))
3452     (setq line (funcall wl-summary-buffer-line-formatter))
3453     (if wl-summary-width (setq line
3454                                (wl-set-string-width
3455                                 (- wl-summary-width 1) line nil
3456                                 'ignore-invalid)))
3457     (setq line (concat line
3458                        "\r"
3459                        (number-to-string
3460                         (elmo-message-entity-number
3461                          wl-message-entity))))
3462     (if wl-summary-highlight
3463         (wl-highlight-summary-line-string
3464          (elmo-message-entity-number wl-message-entity)
3465          line
3466          wl-flags
3467          wl-temp-mark
3468          wl-thr-indent-string))
3469     line))
3470
3471 (defsubst wl-summary-proc-wday (wday-str year month mday)
3472   (save-match-data
3473     (if (string-match "\\([A-Z][a-z][a-z]\\).*" wday-str)
3474         (wl-match-string 1 wday-str)
3475       (elmo-date-get-week year month mday))))
3476
3477 (defvar wl-summary-move-spec-alist
3478   '((new . ((t . nil)
3479             (p . new)
3480             (p . unread)
3481             (p . digest)))
3482     (unread . ((t . nil)
3483                (p . unread)
3484                (p . digest)))))
3485
3486 (defsubst wl-summary-next-message (num direction hereto)
3487   (if wl-summary-buffer-next-message-function
3488       (funcall wl-summary-buffer-next-message-function num direction hereto)
3489     (let ((cur-spec (cdr (assq wl-summary-move-order
3490                                wl-summary-move-spec-alist)))
3491           (nums (memq num (if (eq direction 'up)
3492                               (reverse wl-summary-buffer-number-list)
3493                             wl-summary-buffer-number-list)))
3494           flagged-list nums2)
3495       (unless hereto (setq nums (cdr nums)))
3496       (setq nums2 nums)
3497       (if cur-spec
3498           (catch 'done
3499             (while cur-spec
3500               (setq nums nums2)
3501               (cond ((eq (car (car cur-spec)) 'p)
3502                      (if (setq flagged-list
3503                                (elmo-folder-list-flagged
3504                                 wl-summary-buffer-elmo-folder
3505                                 (cdr (car cur-spec)) t))
3506                          (while nums
3507                            (if (and (memq (car nums) flagged-list)
3508                                     (elmo-message-accessible-p
3509                                      wl-summary-buffer-elmo-folder
3510                                      (car nums)))
3511                                (throw 'done (car nums)))
3512                            (setq nums (cdr nums)))))
3513                     ((eq (car (car cur-spec)) 't)
3514                      (if wl-summary-buffer-target-mark-list
3515                          (while nums
3516                            (if (memq (car nums)
3517                                      wl-summary-buffer-target-mark-list)
3518                                (throw 'done (car nums)))
3519                            (setq nums (cdr nums))))))
3520               (setq cur-spec (cdr cur-spec))))
3521         (car nums)))))
3522
3523 (defsubst wl-summary-cursor-move (direction hereto)
3524   (when (and (eq direction 'up)
3525              (eobp))
3526     (forward-line -1)
3527     (setq hereto t))
3528   (let (num)
3529     (when (setq num (wl-summary-next-message (wl-summary-message-number)
3530                                              direction hereto))
3531       (if (numberp num)
3532           (wl-thread-jump-to-msg num))
3533       t)))
3534 ;;
3535 ;; Goto unread or global flag message
3536 ;; returns t if next message exists in this folder.
3537 (defun wl-summary-cursor-down (&optional hereto)
3538   (interactive "P")
3539   (wl-summary-cursor-move 'down hereto))
3540
3541 (defun wl-summary-cursor-up (&optional hereto)
3542   (interactive "P")
3543   (wl-summary-cursor-move 'up hereto))
3544
3545 (defun wl-summary-save-view-cache ()
3546   (save-excursion
3547     (let* ((dir (elmo-folder-msgdb-path wl-summary-buffer-elmo-folder))
3548            (cache (expand-file-name wl-summary-cache-file dir))
3549            (view (expand-file-name wl-summary-view-file dir))
3550            (save-view wl-summary-buffer-view)
3551            (mark-list (copy-sequence wl-summary-buffer-target-mark-list))
3552            (temp-list (copy-sequence wl-summary-buffer-temp-mark-list))
3553            (tmp-buffer (get-buffer-create " *wl-summary-save-view-cache*"))
3554            (temp-column wl-summary-buffer-temp-mark-column)
3555            (charset wl-summary-buffer-mime-charset))
3556       (when dir
3557         (if (file-directory-p dir)
3558             (); ok.
3559           (if (file-exists-p dir)
3560               (error "File %s already exists" dir)
3561             (elmo-make-directory dir)))
3562         (if (eq save-view 'thread)
3563             (wl-thread-save-entity dir))
3564         (when wl-summary-check-line-format
3565           (wl-summary-line-format-save))
3566         (unwind-protect
3567             (progn
3568               (when (file-writable-p cache)
3569                 (copy-to-buffer tmp-buffer (point-min) (point-max))
3570                 (with-current-buffer tmp-buffer
3571                   (widen)
3572                   (make-local-variable 'wl-summary-highlight)
3573                   (setq wl-summary-highlight nil
3574                         wl-summary-buffer-target-mark-list mark-list
3575                         wl-summary-buffer-temp-mark-list temp-list
3576                         wl-summary-buffer-temp-mark-column temp-column)
3577                   (wl-summary-delete-all-temp-marks 'no-msg 'force)
3578                   (encode-coding-region
3579                    (point-min) (point-max)
3580                    (or (and wl-on-mule
3581                             ;; one in mcs-ltn1(apel<10.4) cannot take 2 arg.
3582                             (mime-charset-to-coding-system charset 'LF))
3583                        ;; Mule 2 doesn't have `*ctext*unix'.
3584                        (mime-charset-to-coding-system charset)))
3585                   (write-region-as-binary (point-min)(point-max)
3586                                           cache nil 'no-msg)))
3587               (when (file-writable-p view) ; 'thread or 'sequence
3588                 (save-excursion
3589                   (set-buffer tmp-buffer)
3590                   (erase-buffer)
3591                   (prin1 save-view tmp-buffer)
3592                   (princ "\n" tmp-buffer)
3593                   (write-region (point-min) (point-max) view nil 'no-msg))))
3594           ;; kill tmp buffer.
3595           (kill-buffer tmp-buffer))))))
3596
3597 (defsubst wl-summary-get-sync-range (folder)
3598   (intern (or (and
3599                (elmo-folder-plugged-p folder)
3600                (wl-get-assoc-list-value
3601                 wl-folder-sync-range-alist
3602                 (elmo-folder-name-internal folder)
3603                 'function))
3604               wl-default-sync-range)))
3605
3606 ;; redefined for wl-summary-sync-update
3607 (defun wl-summary-input-range (folder)
3608   "returns update or all or rescan."
3609   ;; for the case when parts are expanded in the bottom of the folder
3610   (let ((input-range-list '("no-sync"
3611                             "first:"
3612                             "last:"
3613                             "cache-status"
3614                             "mark"
3615                             "rescan"
3616                             "rescan-noscore"
3617                             "rescan-thread"
3618                             "update"
3619                             "update-entirely"
3620                             "all"
3621                             "all-entirely"))
3622         (default (or (wl-get-assoc-list-value
3623                       wl-folder-sync-range-alist
3624                       folder
3625                       'function)
3626                      wl-default-sync-range))
3627         range)
3628     (setq range
3629           (completing-read (format "Range (%s): " default)
3630                            (mapcar
3631                             (function (lambda (x) (cons x x)))
3632                             input-range-list)))
3633     (if (string= range "")
3634         default
3635       range)))
3636
3637 (defun wl-summary-toggle-disp-folder (&optional arg)
3638   (interactive)
3639   (let ((cur-buf (current-buffer))
3640         (summary-win (get-buffer-window (current-buffer)))
3641         fld-buf fld-win)
3642     (cond
3643      ((eq arg 'on)
3644       (setq wl-summary-buffer-disp-folder t)
3645       ;; hide your folder window
3646       (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3647           (if (setq fld-win (get-buffer-window fld-buf))
3648               (delete-window fld-win))))
3649      ((eq arg 'off)
3650       (setq wl-summary-buffer-disp-folder nil)
3651       ;; hide your wl-message window!
3652       (when (buffer-live-p wl-message-buffer)
3653         (wl-message-select-buffer wl-message-buffer)
3654         (delete-window))
3655       (select-window (get-buffer-window cur-buf))
3656       ;; display wl-folder window!!
3657       (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3658           (if (setq fld-win (get-buffer-window fld-buf))
3659               ;; folder win is already displayed.
3660               (select-window fld-win)
3661             ;; folder win is not displayed.
3662             (switch-to-buffer fld-buf))
3663         ;; no folder buf
3664         (wl-folder))
3665       ;; temporarily delete summary-win.
3666       (if summary-win
3667           (delete-window summary-win))
3668       (split-window-horizontally wl-folder-window-width)
3669       (other-window 1)
3670       (switch-to-buffer cur-buf))
3671      (t
3672       (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3673           (if (setq fld-win (get-buffer-window fld-buf))
3674               (setq wl-summary-buffer-disp-folder nil)
3675             (setq wl-summary-buffer-disp-folder t)))
3676       (if (not wl-summary-buffer-disp-folder)
3677           ;; hide message window
3678           (let ((mes-win (and wl-message-buffer
3679                               (get-buffer-window wl-message-buffer)))
3680                 (wl-stay-folder-window t))
3681             (if mes-win (delete-window mes-win))
3682             ;; hide your folder window
3683             (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3684                 (if (setq fld-win (get-buffer-window fld-buf))
3685                     (progn
3686                       (delete-window (get-buffer-window cur-buf))
3687                       (select-window fld-win)
3688                       (switch-to-buffer cur-buf))))
3689             (run-hooks 'wl-summary-toggle-disp-folder-off-hook)
3690             ;; resume message window.
3691             (when mes-win
3692               (wl-message-select-buffer wl-message-buffer)
3693               (run-hooks 'wl-summary-toggle-disp-folder-message-resumed-hook)
3694               (select-window (get-buffer-window cur-buf))))
3695         ;; hide message window
3696         (let ((wl-stay-folder-window t)
3697               (mes-win (and wl-message-buffer
3698                             (get-buffer-window wl-message-buffer))))
3699           (if mes-win (delete-window mes-win))
3700           (select-window (get-buffer-window cur-buf))
3701           ;; display wl-folder window!!
3702           (if (setq fld-buf (get-buffer wl-folder-buffer-name))
3703               (if (setq fld-win (get-buffer-window fld-buf))
3704                   ;; folder win is already displayed.
3705                   (select-window fld-win)
3706                 ;; folder win is not displayed...occupy all.
3707                 (switch-to-buffer fld-buf))
3708             ;; no folder buf
3709             (wl-folder))
3710           (split-window-horizontally wl-folder-window-width)
3711           (other-window 1)
3712           (switch-to-buffer cur-buf)
3713           ;; resume message window.
3714           (run-hooks 'wl-summary-toggle-disp-folder-on-hook)
3715           (when mes-win
3716             (wl-message-select-buffer wl-message-buffer)
3717             (run-hooks 'wl-summary-toggle-disp-folder-message-resumed-hook)
3718             (select-window (get-buffer-window cur-buf))))))))
3719   (run-hooks 'wl-summary-toggle-disp-folder-hook))
3720
3721 (defun wl-summary-toggle-disp-msg (&optional arg)
3722   (interactive)
3723   (let ((cur-buf (current-buffer))
3724         fld-buf fld-win
3725         summary-win)
3726     (cond
3727      ((eq arg 'on)
3728       (setq wl-summary-buffer-disp-msg t)
3729       (save-excursion
3730         ;; hide your folder window
3731         (if (and (not wl-stay-folder-window)
3732                  (setq fld-buf (get-buffer wl-folder-buffer-name)))
3733             (if (setq fld-win (get-buffer-window fld-buf))
3734                 (unless (one-window-p fld-win)
3735                   (delete-window fld-win))))))
3736      ((eq arg 'off)
3737       (wl-delete-all-overlays)
3738       (setq wl-summary-buffer-disp-msg nil)
3739       (save-excursion
3740         (when (buffer-live-p wl-message-buffer)
3741           (wl-message-select-buffer wl-message-buffer)
3742           (delete-window)
3743           (and (get-buffer-window cur-buf)
3744                (select-window (get-buffer-window cur-buf))))
3745         (run-hooks 'wl-summary-toggle-disp-off-hook)))
3746      (t
3747       (if (and wl-message-buffer
3748                (get-buffer-window wl-message-buffer)) ; already displayed
3749           (setq wl-summary-buffer-disp-msg nil)
3750         (setq wl-summary-buffer-disp-msg t))
3751       (if wl-summary-buffer-disp-msg
3752           (progn
3753             (wl-summary-redisplay)
3754 ;;; hide your folder window
3755 ;;;         (setq fld-buf (get-buffer wl-folder-buffer-name))
3756 ;;;         (if (setq fld-win (get-buffer-window fld-buf))
3757 ;;;             (delete-window fld-win)))
3758             (run-hooks 'wl-summary-toggle-disp-on-hook))
3759         (wl-delete-all-overlays)
3760         (save-excursion
3761           (wl-message-select-buffer wl-message-buffer)
3762           (delete-window)
3763           (select-window (get-buffer-window cur-buf))
3764           (setq wl-message-buffer nil)
3765           (run-hooks 'wl-summary-toggle-disp-off-hook))
3766 ;;;     (switch-to-buffer cur-buf)
3767         )))
3768     (run-hooks 'wl-summary-buffer-window-scroll-functions)))
3769
3770 (defun wl-summary-next-line-content ()
3771   "Show next line of the message."
3772   (interactive)
3773   (let ((cur-buf (current-buffer)))
3774     (wl-summary-toggle-disp-msg 'on)
3775     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
3776       (set-buffer cur-buf)
3777       (wl-message-next-page 1))))
3778
3779 (defun wl-summary-prev-line-content ()
3780   (interactive)
3781   (let ((cur-buf (current-buffer)))
3782     (wl-summary-toggle-disp-msg 'on)
3783     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
3784       (set-buffer cur-buf)
3785       (wl-message-prev-page 1))))
3786
3787 (defun wl-summary-next-page ()
3788   (interactive)
3789   (let ((cur-buf (current-buffer)))
3790     (wl-summary-toggle-disp-msg 'on)
3791     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
3792       (set-buffer cur-buf)
3793       (wl-message-next-page))))
3794
3795 (defun wl-summary-prev-page ()
3796   (interactive)
3797   (let ((cur-buf (current-buffer)))
3798     (wl-summary-toggle-disp-msg 'on)
3799     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
3800       (set-buffer cur-buf)
3801       (wl-message-prev-page))))
3802
3803 (defsubst wl-summary-no-mime-p (folder)
3804   (wl-string-match-member (elmo-folder-name-internal folder)
3805                           wl-summary-no-mime-folder-list))
3806
3807 (defun wl-summary-set-message-buffer-or-redisplay (&rest args)
3808   "Set message buffer.
3809 If message is not displayed yet, display it.
3810 Return t if message exists."
3811   (let ((folder wl-summary-buffer-elmo-folder)
3812         (number (wl-summary-message-number))
3813         cur-folder cur-number message-last-pos)
3814     (when (buffer-live-p wl-message-buffer)
3815       (save-window-excursion
3816         (wl-message-select-buffer wl-message-buffer)
3817         (setq cur-folder wl-message-buffer-cur-folder)
3818         (setq cur-number wl-message-buffer-cur-number)))
3819     (if (and (string= (elmo-folder-name-internal folder) (or cur-folder ""))
3820              (eq number (or cur-number 0)))
3821         (progn
3822           (set-buffer wl-message-buffer)
3823           t)
3824       (if (wl-summary-no-mime-p folder)
3825           (wl-summary-redisplay-no-mime-internal folder number)
3826         (wl-summary-redisplay-internal folder number))
3827       (when (buffer-live-p wl-message-buffer)
3828         (set-buffer wl-message-buffer))
3829       nil)))
3830
3831 (defun wl-summary-target-mark-forward (&optional arg)
3832   (interactive "P")
3833   (let ((mlist (nreverse (copy-sequence wl-summary-buffer-target-mark-list)))
3834         (summary-buf (current-buffer))
3835         (wl-draft-forward t)
3836         start-point
3837         draft-buf)
3838     (wl-summary-jump-to-msg (car mlist))
3839     (wl-summary-forward t)
3840     (setq start-point (point))
3841     (setq draft-buf (current-buffer))
3842     (setq mlist (cdr mlist))
3843     (save-window-excursion
3844       (when mlist
3845         (while mlist
3846           (set-buffer summary-buf)
3847           (wl-summary-jump-to-msg (car mlist))
3848           (wl-summary-redisplay)
3849           (set-buffer draft-buf)
3850           (goto-char (point-max))
3851           (wl-draft-insert-message)
3852           (setq mlist (cdr mlist)))
3853         (wl-draft-body-goto-top)
3854         (wl-draft-enclose-digest-region (point) (point-max)))
3855       (goto-char start-point)
3856       (save-excursion
3857         (set-buffer summary-buf)
3858         (wl-summary-delete-all-target-marks)))
3859     (run-hooks 'wl-mail-setup-hook)))
3860
3861 (defun wl-summary-target-mark-reply-with-citation (&optional arg)
3862   (interactive "P")
3863   (let ((mlist (nreverse (copy-sequence wl-summary-buffer-target-mark-list)))
3864         (summary-buf (current-buffer))
3865         change-major-mode-hook
3866         start-point
3867         draft-buf)
3868     (wl-summary-jump-to-msg (car mlist))
3869     (when (wl-summary-reply arg t)
3870       (goto-char (point-max))
3871       (setq start-point (point-marker))
3872       (setq draft-buf (current-buffer))
3873       (save-window-excursion
3874         (while mlist
3875           (set-buffer summary-buf)
3876           (delete-other-windows)
3877           (wl-summary-jump-to-msg (car mlist))
3878           (wl-summary-redisplay)
3879           (set-buffer draft-buf)
3880           (goto-char (point-max))
3881           (wl-draft-yank-original)
3882           (setq mlist (cdr mlist)))
3883         (goto-char start-point)
3884         (save-excursion
3885           (set-buffer summary-buf)
3886           (wl-summary-delete-all-target-marks)))
3887       (wl-draft-reply-position wl-draft-reply-default-position)
3888       (run-hooks 'wl-mail-setup-hook))))
3889
3890 (defun wl-summary-reply-with-citation (&optional arg)
3891   (interactive "P")
3892   (when (wl-summary-reply arg t)
3893     (goto-char (point-max))
3894     (wl-draft-yank-original)
3895     (wl-draft-reply-position wl-draft-reply-default-position)
3896     (run-hooks 'wl-mail-setup-hook)))
3897
3898 (defun wl-summary-jump-to-msg-by-message-id (&optional id)
3899   (interactive)
3900   (let* ((original (wl-summary-message-number))
3901          (msgid (elmo-string (or id (read-from-minibuffer "Message-ID: "))))
3902          (entity (elmo-message-entity wl-summary-buffer-elmo-folder msgid))
3903          msg otherfld schar
3904          (errmsg (format "No message with id \"%s\" in the folder." msgid)))
3905     (if (setq msg (elmo-message-entity-number entity))
3906         (progn
3907           (wl-thread-jump-to-msg msg)
3908           t)
3909       ;; for XEmacs!
3910       (if (and elmo-use-database
3911                (setq errmsg
3912                      (format
3913                       "No message with id \"%s\" in the database." msgid))
3914                (setq otherfld (elmo-database-msgid-get msgid)))
3915           (if (cdr (wl-summary-jump-to-msg-internal
3916                     (car otherfld) (nth 1 otherfld) 'no-sync))
3917               t ; succeed.
3918             ;; Back to original.
3919             (wl-summary-jump-to-msg-internal
3920              (wl-summary-buffer-folder-name) original 'no-sync))
3921         (cond ((eq wl-summary-search-via-nntp 'confirm)
3922                (require 'elmo-nntp)
3923                (message "Search message in nntp server \"%s\" <y/n/s(elect)>? "
3924                         elmo-nntp-default-server)
3925                (setq schar (let ((cursor-in-echo-area t)) (read-char)))
3926                (cond ((eq schar ?y)
3927                       (wl-summary-jump-to-msg-by-message-id-via-nntp msgid))
3928                      ((eq schar ?s)
3929                       (wl-summary-jump-to-msg-by-message-id-via-nntp
3930                        msgid
3931                        (read-from-minibuffer "NNTP Server: ")))
3932                      (t
3933                       (message "%s" errmsg)
3934                       nil)))
3935               ((or (eq wl-summary-search-via-nntp 'force)
3936                    (and
3937                     (eq (elmo-folder-type-internal wl-summary-buffer-elmo-folder)
3938                         'nntp)
3939                     wl-summary-search-via-nntp))
3940                (wl-summary-jump-to-msg-by-message-id-via-nntp msgid))
3941               (t
3942                (message "%s" errmsg)
3943                nil))))))
3944
3945 (defun wl-summary-jump-to-msg-by-message-id-via-nntp (&optional id server-spec)
3946   (interactive)
3947   (let* ((msgid (elmo-string (or id (read-from-minibuffer "Message-ID: "))))
3948          newsgroups folder ret
3949          user server port type spec)
3950     (if server-spec
3951         (if (string-match "^-" server-spec)
3952             (setq spec (wl-folder-get-elmo-folder server-spec)
3953                   user (elmo-net-folder-user-internal spec)
3954                   server (elmo-net-folder-server-internal spec)
3955                   port (elmo-net-folder-port-internal spec)
3956                   type (elmo-net-folder-stream-type-internal spec))
3957           (setq server server-spec)))
3958     (when (setq ret (elmo-nntp-get-newsgroup-by-msgid
3959                      msgid
3960                      (or server elmo-nntp-default-server)
3961                      (or user elmo-nntp-default-user)
3962                      (or port elmo-nntp-default-port)
3963                      (or type elmo-nntp-default-stream-type)))
3964       (setq newsgroups (elmo-nntp-parse-newsgroups ret))
3965       (setq folder (concat "-" (car newsgroups)
3966                            (elmo-nntp-folder-postfix user server port type)))
3967       (catch 'found
3968         (while newsgroups
3969           (if (wl-folder-entity-exists-p (car newsgroups)
3970                                          wl-folder-newsgroups-hashtb)
3971               (throw 'found
3972                      (setq folder (concat "-" (car newsgroups)
3973                                           (elmo-nntp-folder-postfix
3974                                            user server port type)))))
3975           (setq newsgroups (cdr newsgroups)))))
3976     (if ret
3977         (wl-summary-jump-to-msg-internal folder nil 'update msgid)
3978       (message "No message id \"%s\" in nntp server \"%s\"."
3979                msgid (or server elmo-nntp-default-server))
3980       nil)))
3981
3982 (defun wl-summary-jump-to-msg-internal (folder msg scan-type &optional msgid)
3983   (let (wl-auto-select-first entity)
3984     (if (or (string= folder (wl-summary-buffer-folder-name))
3985             (y-or-n-p
3986              (format
3987               "Message was found in the folder \"%s\". Jump to it? "
3988               folder)))
3989         (progn
3990           (unwind-protect
3991               (wl-summary-goto-folder-subr
3992                folder scan-type nil nil t)
3993             (if msgid
3994                 (setq msg
3995                       (elmo-message-entity-number
3996                        (elmo-message-entity
3997                         wl-summary-buffer-elmo-folder
3998                         msgid))))
3999             (setq entity (wl-folder-search-entity-by-name folder
4000                                                           wl-folder-entity
4001                                                           'folder))
4002             (if entity
4003                 (wl-folder-set-current-entity-id
4004                  (wl-folder-get-entity-id entity))))
4005           (if (null msg)
4006               (message "Message was not found currently in this folder.")
4007             (setq msg (and (wl-thread-jump-to-msg msg) msg)))
4008           (cons folder msg)))))
4009
4010 (defun wl-summary-jump-to-parent-message (arg)
4011   (interactive "P")
4012   (let ((cur-buf (current-buffer))
4013         (disp-msg wl-summary-buffer-disp-msg)
4014         (number (wl-summary-message-number))
4015         (regexp "\\(<[^<>]*>\\)[ \t]*$")
4016         (i -1) ;; xxx
4017         msg-id msg-num ref-list ref irt)
4018     (if (null number)
4019         (message "No message.")
4020       (when (eq wl-summary-buffer-view 'thread)
4021         (cond ((and arg (not (numberp arg)))
4022                (setq msg-num
4023                      (wl-thread-entity-get-number
4024                       (wl-thread-entity-get-top-entity
4025                        (wl-thread-get-entity number)))))
4026               ((and arg (numberp arg))
4027                (setq i 0)
4028                (setq msg-num number)
4029                (while (< i arg)
4030                  (setq msg-num
4031                        (wl-thread-entity-get-number
4032                         (wl-thread-entity-get-parent-entity
4033                          (wl-thread-get-entity msg-num))))
4034                  (setq i (1+ i))))
4035               (t (setq msg-num
4036                        (wl-thread-entity-get-number
4037                         (wl-thread-entity-get-parent-entity
4038                          (wl-thread-get-entity number)))))))
4039       (when (null msg-num)
4040         (wl-summary-set-message-buffer-or-redisplay)
4041         (set-buffer (wl-message-get-original-buffer))
4042         (message "Searching parent message...")
4043         (setq ref (std11-field-body "References")
4044               irt (std11-field-body "In-Reply-To"))
4045         (cond
4046          ((and arg (not (numberp arg)) ref (not (string= ref ""))
4047                (string-match regexp ref))
4048           ;; The first message of the thread.
4049           (setq msg-id (wl-match-string 1 ref)))
4050          ;; "In-Reply-To:" has only one msg-id.
4051          ((and (null arg) irt (not (string= irt ""))
4052                (string-match regexp irt))
4053           (setq msg-id (wl-match-string 1 irt)))
4054          ((and (or (null arg) (numberp arg)) ref (not (string= ref ""))
4055                (string-match regexp ref))
4056           ;; "^" searching parent, "C-u 2 ^" looking for grandparent.
4057           (while (string-match regexp ref)
4058             (setq ref-list
4059                   (append (list
4060                            (wl-match-string 1 ref))
4061                           ref-list))
4062             (setq ref (substring ref (match-end 0)))
4063             (setq i (1+ i)))
4064           (setq msg-id
4065                 (if (null arg) (nth 0 ref-list) ;; previous
4066                   (if (<= arg i) (nth (1- arg) ref-list)
4067                     (nth i ref-list))))))
4068         (set-buffer cur-buf)
4069         (or disp-msg (wl-summary-toggle-disp-msg 'off)))
4070       (cond ((and (null msg-id) (null msg-num))
4071              (message "No parent message!")
4072              nil)
4073             ((and msg-id (wl-summary-jump-to-msg-by-message-id msg-id))
4074              (if wl-summary-buffer-disp-msg (wl-summary-redisplay))
4075              (message "Searching parent message...done")
4076              t)
4077             ((and msg-num (wl-summary-jump-to-msg msg-num))
4078              (if wl-summary-buffer-disp-msg (wl-summary-redisplay))
4079              (message "Searching parent message...done")
4080              t)
4081             (t ; failed.
4082              (message "Parent message was not found.")
4083              nil)))))
4084
4085 (defun wl-summary-reply (&optional arg without-setup-hook)
4086   "Reply to current message. Default is \"wide\" reply.
4087 Reply to author if invoked with ARG."
4088   (interactive "P")
4089   (let ((folder wl-summary-buffer-elmo-folder)
4090         (number (wl-summary-message-number))
4091         (summary-buf (current-buffer))
4092         (winconf (current-window-configuration))
4093         mes-buf)
4094     (when number
4095       (save-excursion
4096         (wl-summary-set-message-buffer-or-redisplay))
4097       (setq mes-buf wl-message-buffer)
4098       (wl-message-select-buffer wl-message-buffer)
4099       (set-buffer mes-buf)
4100       (condition-case err
4101           (when (setq mes-buf (wl-message-get-original-buffer))
4102             (wl-draft-reply mes-buf arg summary-buf number)
4103             (wl-draft-reply-position wl-draft-reply-default-position)
4104             (unless without-setup-hook
4105               (run-hooks 'wl-mail-setup-hook)))
4106         (error (set-window-configuration winconf)
4107                (signal (car err)(cdr err))))
4108       (with-current-buffer summary-buf
4109         (elmo-folder-set-flag folder (list number) 'answered)
4110         (wl-summary-update-persistent-mark))
4111       t)))
4112
4113 (defun wl-summary-write ()
4114   "Write a new draft from Summary."
4115   (interactive)
4116   (wl-draft (list (cons 'To ""))
4117             nil nil nil nil (wl-summary-buffer-folder-name))
4118   (run-hooks 'wl-mail-setup-hook)
4119   (mail-position-on-field "To"))
4120
4121 (defvar wl-summary-write-current-folder-functions
4122   '(wl-folder-get-newsgroups
4123     wl-folder-guess-mailing-list-by-refile-rule
4124     wl-folder-guess-mailing-list-by-folder-name)
4125   "Newsgroups or Mailing List address guess functions list.
4126 Call from `wl-summary-write-current-folder'.
4127 When guess function return nil, challenge next guess-function.")
4128
4129 (defun wl-summary-write-current-folder (&optional folder)
4130   "Write message to current FOLDER's newsgroup or mailing-list.
4131 Use function list is `wl-summary-write-current-folder-functions'."
4132   (interactive)
4133   ;; default FOLDER is current buffer folder
4134   (setq folder (or folder (wl-summary-buffer-folder-name)))
4135   (let ((func-list wl-summary-write-current-folder-functions)
4136         guess-list guess-func)
4137     (while func-list
4138       (setq guess-list (funcall (car func-list) folder))
4139       (if (null guess-list)
4140           (setq func-list (cdr func-list))
4141         (setq guess-func (car func-list))
4142         (setq func-list nil)))
4143     (if (null guess-func)
4144         (wl-summary-write)
4145       (unless (or (stringp (nth 0 guess-list))
4146                   (stringp (nth 1 guess-list))
4147                   (stringp (nth 2 guess-list)))
4148         (error "Invalid value return guess function `%s'"
4149                (symbol-name guess-func)))
4150       (wl-draft (list (cons 'To (nth 0 guess-list))
4151                       (cons 'Cc (nth 1 guess-list))
4152                       (cons 'Newsgroups (nth 2 guess-list)))
4153                 nil nil nil nil folder)
4154       (run-hooks 'wl-mail-setup-hook)
4155       (mail-position-on-field "Subject"))))
4156
4157 (defun wl-summary-forward (&optional without-setup-hook)
4158   ""
4159   (interactive)
4160   (let ((folder wl-summary-buffer-elmo-folder)
4161         (number (wl-summary-message-number))
4162         (summary-buf (current-buffer))
4163         (wl-draft-forward t)
4164         mes-buf
4165         entity subject num)
4166     (if (null number)
4167         (message "No message.")
4168       (if (and (elmo-message-use-cache-p folder number)
4169                (eq (elmo-file-cache-status
4170                     (elmo-file-cache-get
4171                      (elmo-message-field folder number 'message-id)))
4172                    'section))
4173           ;; Reload.
4174           (wl-summary-redisplay-internal nil nil 'force-reload)
4175         (wl-summary-redisplay-internal folder number))
4176       (setq mes-buf wl-message-buffer)
4177       (wl-message-select-buffer mes-buf)
4178       ;; get original subject.
4179       (if summary-buf
4180           (save-excursion
4181             (set-buffer summary-buf)
4182             (setq subject
4183                   (or (elmo-message-entity-field
4184                        (elmo-message-entity folder number) 'subject 'decode)
4185                       ""))))
4186       (set-buffer mes-buf)
4187       (wl-draft-forward subject summary-buf)
4188       (unless without-setup-hook
4189         (run-hooks 'wl-mail-setup-hook)))))
4190
4191 (defun wl-summary-click (e)
4192   (interactive "e")
4193   (mouse-set-point e)
4194   (wl-summary-read))
4195
4196 (defun wl-summary-read ()
4197   "Proceed reading message in the summary buffer."
4198   (interactive)
4199   (let ((cur-buf (current-buffer)))
4200     (wl-summary-toggle-disp-msg 'on)
4201     (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
4202       (set-buffer cur-buf)
4203       (if (wl-message-next-page)
4204           (wl-summary-down t)))))
4205
4206 (defsubst wl-summary-cursor-move-surface (downward interactive)
4207   (if wl-summary-move-direction-toggle
4208       (setq wl-summary-move-direction-downward downward))
4209   (let ((start (point))
4210         (skip-tmark-regexp (wl-regexp-opt wl-summary-skip-mark-list))
4211         (skip t)
4212         (column (current-column))
4213         goto-next next-entity finfo)
4214     (beginning-of-line)
4215     (while (and skip
4216                 (not (if downward (eobp) (bobp))))
4217       (if downward
4218           (forward-line 1)
4219         (forward-line -1))
4220       (setq skip (or (string-match skip-tmark-regexp
4221                                    (wl-summary-temp-mark))
4222                      (not (and (wl-summary-message-number)
4223                                (elmo-message-accessible-p
4224                                 wl-summary-buffer-elmo-folder
4225                                 (wl-summary-message-number)))))))
4226     (if (if downward (eobp) (and (bobp) skip)) (setq goto-next t))
4227     (if (or (eobp) (and (bobp) skip))
4228         (goto-char start))
4229     (move-to-column column)
4230
4231     (if (not goto-next)
4232         (if wl-summary-buffer-disp-msg
4233             (wl-summary-redisplay))
4234       (if interactive
4235           (cond
4236            ((and (not downward) wl-summary-buffer-prev-folder-function)
4237             (funcall wl-summary-buffer-prev-folder-function))
4238            ((and downward wl-summary-buffer-next-folder-function)
4239             (funcall wl-summary-buffer-next-folder-function))
4240            (t
4241             (when wl-auto-select-next
4242               (setq next-entity
4243                     (if downward
4244                         (wl-summary-get-next-folder)
4245                       (wl-summary-get-prev-folder)))
4246               (if next-entity
4247                   (setq finfo (wl-folder-get-entity-info next-entity))))
4248             (wl-ask-folder
4249              '(lambda () (wl-summary-next-folder-or-exit next-entity))
4250              (format
4251               "No more messages. Type SPC to go to %s."
4252               (wl-summary-entity-info-msg next-entity finfo)))))))))
4253
4254 (defun wl-summary-prev (&optional interactive)
4255   (interactive)
4256   (wl-summary-cursor-move-surface nil (or interactive (interactive-p))))
4257
4258 (defun wl-summary-next (&optional interactive)
4259   (interactive)
4260   (wl-summary-cursor-move-surface t (or interactive (interactive-p))))
4261
4262 (defun wl-summary-up (&optional interactive skip-no-unread)
4263   ""
4264   (interactive)
4265   (if wl-summary-move-direction-toggle
4266       (setq wl-summary-move-direction-downward nil))
4267   (if (wl-summary-cursor-up)
4268       (if wl-summary-buffer-disp-msg
4269           (wl-summary-redisplay))
4270     (if (or interactive
4271             (interactive-p))
4272         (if wl-summary-buffer-prev-folder-function
4273             (funcall wl-summary-buffer-prev-folder-function)
4274           (let (next-entity finfo)
4275             (when wl-auto-select-next
4276               (progn
4277                 (setq next-entity (wl-summary-get-prev-unread-folder))
4278                 (if next-entity
4279                     (setq finfo (wl-folder-get-entity-info next-entity)))))
4280             (if (and skip-no-unread
4281                      (eq wl-auto-select-next 'skip-no-unread))
4282                 (wl-summary-next-folder-or-exit next-entity t)
4283               (wl-ask-folder
4284                '(lambda () (wl-summary-next-folder-or-exit next-entity t))
4285                (format
4286                 "No more unread messages. Type SPC to go to %s."
4287                 (wl-summary-entity-info-msg next-entity finfo)))))))))
4288
4289 (defun wl-summary-get-prev-folder ()
4290   (let ((folder-buf (get-buffer wl-folder-buffer-name))
4291         last-entity cur-id)
4292     (when folder-buf
4293       (setq cur-id (save-excursion (set-buffer folder-buf)
4294                                    wl-folder-buffer-cur-entity-id))
4295       (wl-folder-get-prev-folder cur-id))))
4296
4297 (defun wl-summary-get-next-folder ()
4298   (let ((folder-buf (get-buffer wl-folder-buffer-name))
4299         cur-id)
4300     (when folder-buf
4301       (setq cur-id (save-excursion (set-buffer folder-buf)
4302                                    wl-folder-buffer-cur-entity-id))
4303       (wl-folder-get-next-folder cur-id))))
4304
4305 (defun wl-summary-get-next-unread-folder ()
4306   (let ((folder-buf (get-buffer wl-folder-buffer-name))
4307         cur-id)
4308     (when folder-buf
4309       (setq cur-id (save-excursion (set-buffer folder-buf)
4310                                    wl-folder-buffer-cur-entity-id))
4311       (wl-folder-get-next-folder cur-id 'unread))))
4312
4313 (defun wl-summary-get-prev-unread-folder ()
4314   (let ((folder-buf (get-buffer wl-folder-buffer-name))
4315         cur-id)
4316     (when folder-buf
4317       (setq cur-id (save-excursion (set-buffer folder-buf)
4318                                    wl-folder-buffer-cur-entity-id))
4319       (wl-folder-get-prev-folder cur-id 'unread))))
4320
4321 (defun wl-summary-down (&optional interactive skip-no-unread)
4322   (interactive)
4323   (if wl-summary-move-direction-toggle
4324       (setq wl-summary-move-direction-downward t))
4325   (if (wl-summary-cursor-down)
4326       (if wl-summary-buffer-disp-msg
4327           (wl-summary-redisplay))
4328     (if (or interactive
4329             (interactive-p))
4330         (if wl-summary-buffer-next-folder-function
4331             (funcall wl-summary-buffer-next-folder-function)
4332           (let (next-entity finfo)
4333             (when wl-auto-select-next
4334               (setq next-entity (wl-summary-get-next-unread-folder)))
4335             (if next-entity
4336                 (setq finfo (wl-folder-get-entity-info next-entity)))
4337             (if (and skip-no-unread
4338                      (eq wl-auto-select-next 'skip-no-unread))
4339                 (wl-summary-next-folder-or-exit next-entity)
4340               (wl-ask-folder
4341                '(lambda () (wl-summary-next-folder-or-exit next-entity))
4342                (format
4343                 "No more unread messages. Type SPC to go to %s."
4344                 (wl-summary-entity-info-msg next-entity finfo)))))))))
4345
4346 (defun wl-summary-goto-last-displayed-msg ()
4347   (interactive)
4348   (unless wl-summary-buffer-last-displayed-msg
4349     (setq wl-summary-buffer-last-displayed-msg
4350           wl-summary-buffer-current-msg))
4351   (if wl-summary-buffer-last-displayed-msg
4352       (progn
4353         (wl-summary-jump-to-msg wl-summary-buffer-last-displayed-msg)
4354         (if wl-summary-buffer-disp-msg
4355             (wl-summary-redisplay)))
4356     (message "No last message.")))
4357
4358 (defun wl-summary-redisplay (&optional arg)
4359   (interactive "P")
4360   (if (and (not arg)
4361            (wl-summary-no-mime-p wl-summary-buffer-elmo-folder))
4362       (wl-summary-redisplay-no-mime)
4363     (wl-summary-redisplay-internal nil nil arg)))
4364
4365 (defsubst wl-summary-redisplay-internal (&optional folder number force-reload)
4366   (interactive)
4367   (let* ((folder (or folder wl-summary-buffer-elmo-folder))
4368          (num (or number (wl-summary-message-number)))
4369          (wl-mime-charset      wl-summary-buffer-mime-charset)
4370          (default-mime-charset wl-summary-buffer-mime-charset)
4371          no-folder-mark fld-buf fld-win thr-entity)
4372     (if (and wl-thread-open-reading-thread
4373              (eq wl-summary-buffer-view 'thread)
4374              (not (wl-thread-entity-get-opened
4375                    (setq thr-entity (wl-thread-get-entity
4376                                      num))))
4377              (wl-thread-entity-get-children thr-entity))
4378         (wl-thread-force-open))
4379     (if num
4380         (progn
4381           (setq wl-summary-buffer-disp-msg t)
4382           (setq wl-summary-buffer-last-displayed-msg
4383                 wl-summary-buffer-current-msg)
4384           ;; hide folder window
4385           (if (and (not wl-stay-folder-window)
4386                    (setq fld-buf (get-buffer wl-folder-buffer-name)))
4387               (if (setq fld-win (get-buffer-window fld-buf))
4388                   (delete-window fld-win)))
4389           (setq wl-current-summary-buffer (current-buffer))
4390           (setq no-folder-mark
4391                 ;; If cache is used, change folder-mark.
4392                 (if (wl-message-redisplay folder num
4393                                           'mime
4394                                           (or
4395                                            force-reload
4396                                            (string= (elmo-folder-name-internal
4397                                                      folder)
4398                                                     wl-draft-folder)))
4399                     nil
4400                   ;; plugged, then leave folder-mark.
4401                   (if (and (not (elmo-folder-local-p
4402                                  wl-summary-buffer-elmo-folder))
4403                            (elmo-folder-plugged-p
4404                             wl-summary-buffer-elmo-folder))
4405                       'leave)))
4406           (when (elmo-message-use-cache-p folder num)
4407             (elmo-message-set-cached folder num t))
4408           (ignore-errors
4409             (if (elmo-message-flagged-p wl-summary-buffer-elmo-folder
4410                                         num
4411                                         'unread)
4412                 (wl-summary-mark-as-read num no-folder-mark)
4413               (wl-summary-update-persistent-mark)))
4414           (setq wl-summary-buffer-current-msg num)
4415           (when wl-summary-recenter
4416             (recenter (/ (- (window-height) 2) 2))
4417             (if (not wl-summary-indent-length-limit)
4418                 (wl-horizontal-recenter)))
4419           (wl-highlight-summary-displaying)
4420           (wl-message-buffer-prefetch-next folder num
4421                                            wl-message-buffer-prefetch-depth
4422                                            (current-buffer)
4423                                            wl-summary-buffer-mime-charset)
4424           (run-hooks 'wl-summary-redisplay-hook))
4425       (message "No message to display."))))
4426
4427 (defun wl-summary-redisplay-no-mime (&optional ask-coding)
4428   "Display message without MIME decoding.
4429 If ASK-CODING is non-nil, coding-system for the message is asked."
4430   (interactive "P")
4431   (let ((elmo-mime-display-as-is-coding-system
4432          (if ask-coding
4433              (or (read-coding-system "Coding system: ")
4434                  elmo-mime-display-as-is-coding-system)
4435            elmo-mime-display-as-is-coding-system)))
4436     (wl-summary-redisplay-no-mime-internal)))
4437
4438 (defun wl-summary-redisplay-no-mime-internal (&optional folder number)
4439   (let* ((fld (or folder wl-summary-buffer-elmo-folder))
4440          (num (or number (wl-summary-message-number)))
4441          wl-break-pages)
4442     (if num
4443         (progn
4444           (setq wl-summary-buffer-disp-msg t)
4445           (setq wl-summary-buffer-last-displayed-msg
4446                 wl-summary-buffer-current-msg)
4447           (setq wl-current-summary-buffer (current-buffer))
4448           (wl-message-redisplay fld num 'as-is
4449                                 (string= (elmo-folder-name-internal fld)
4450                                          wl-draft-folder))
4451           (when (elmo-message-use-cache-p fld num)
4452             (elmo-message-set-cached fld num t))
4453           (ignore-errors
4454             (if (elmo-message-flagged-p fld num 'unread)
4455                 (wl-summary-mark-as-read num); no-folder-mark)
4456               (wl-summary-update-persistent-mark)))
4457           (setq wl-summary-buffer-current-msg num)
4458           (when wl-summary-recenter
4459             (recenter (/ (- (window-height) 2) 2))
4460             (if (not wl-summary-indent-length-limit)
4461                 (wl-horizontal-recenter)))
4462           (wl-highlight-summary-displaying)
4463           (run-hooks 'wl-summary-redisplay-hook))
4464       (message "No message to display.")
4465       (wl-ask-folder 'wl-summary-exit
4466                      "No more messages. Type SPC to go to folder mode."))))
4467
4468 (defun wl-summary-redisplay-all-header (&optional folder number)
4469   (interactive)
4470   (let* ((fld (or folder wl-summary-buffer-elmo-folder))
4471          (num (or number (wl-summary-message-number)))
4472          (wl-mime-charset      wl-summary-buffer-mime-charset)
4473          (default-mime-charset wl-summary-buffer-mime-charset))
4474     (if num
4475         (progn
4476           (setq wl-summary-buffer-disp-msg t)
4477           (setq wl-summary-buffer-last-displayed-msg
4478                 wl-summary-buffer-current-msg)
4479           (setq wl-current-summary-buffer (current-buffer))
4480           (when (elmo-message-use-cache-p fld num)
4481             (elmo-message-set-cached fld num t))
4482           (if (wl-message-redisplay fld num 'all-header
4483                                     (string= (elmo-folder-name-internal fld)
4484                                              wl-draft-folder))
4485               (wl-summary-mark-as-read num))
4486           (setq wl-summary-buffer-current-msg num)
4487           (when wl-summary-recenter
4488             (recenter (/ (- (window-height) 2) 2))
4489             (if (not wl-summary-indent-length-limit)
4490                 (wl-horizontal-recenter)))
4491           (wl-highlight-summary-displaying)
4492           (run-hooks 'wl-summary-redisplay-hook))
4493       (message "No message to display."))))
4494
4495 (defun wl-summary-jump-to-current-message ()
4496   "Jump into Message buffer."
4497   (interactive)
4498   (let (message-buf message-win)
4499     (if (setq message-buf wl-message-buffer)
4500         (if (setq message-win (get-buffer-window message-buf))
4501             (select-window message-win)
4502           (wl-message-select-buffer wl-message-buffer))
4503       (wl-summary-redisplay)
4504       (wl-message-select-buffer wl-message-buffer))))
4505
4506 (defun wl-summary-cancel-message ()
4507   "Cancel an article on news."
4508   (interactive)
4509   (if (null (wl-summary-message-number))
4510       (message "No message.")
4511     (let ((summary-buf (current-buffer))
4512           message-buf)
4513       (wl-summary-set-message-buffer-or-redisplay)
4514       (if (setq message-buf (wl-message-get-original-buffer))
4515           (set-buffer message-buf))
4516       (unless (wl-message-news-p)
4517         (set-buffer summary-buf)
4518         (if (and (eq (elmo-folder-type-internal wl-summary-buffer-elmo-folder)
4519                      'nntp)
4520                  (y-or-n-p "Cannot get Newsgroups. Fetch again? "))
4521             (progn
4522               (wl-summary-redisplay t)
4523               (wl-summary-supersedes-message))
4524           (error "This is not a news article; supersedes is impossible")))
4525       (when (yes-or-no-p "Do you really want to cancel this article? ")
4526         (let (from newsgroups message-id distribution buf)
4527           (save-excursion
4528             (setq from (std11-field-body "from")
4529                   newsgroups (std11-field-body "newsgroups")
4530                   message-id (std11-field-body "message-id")
4531                   distribution (std11-field-body "distribution"))
4532             ;; Make sure that this article was written by the user.
4533             (unless (wl-address-user-mail-address-p
4534                      (wl-address-header-extract-address
4535                       (car (wl-parse-addresses from))))
4536               (error "This article is not yours"))
4537             ;; Make control message.
4538             (setq buf (set-buffer (get-buffer-create " *message cancel*")))
4539             (setq wl-draft-buffer-cur-summary-buffer summary-buf)
4540             (buffer-disable-undo (current-buffer))
4541             (erase-buffer)
4542             (insert "Newsgroups: " newsgroups "\n"
4543                     "From: " (wl-address-header-extract-address
4544                               wl-from) "\n"
4545                               "Subject: cmsg cancel " message-id "\n"
4546                               "Control: cancel " message-id "\n"
4547                               (if distribution
4548                                   (concat "Distribution: " distribution "\n")
4549                                 "")
4550                               mail-header-separator "\n"
4551                               wl-summary-cancel-message)
4552             (message "Canceling your message...")
4553             (wl-draft-raw-send t t) ; kill when done, force-pre-hooks.
4554             (message "Canceling your message...done")))))))
4555
4556 (defun wl-summary-supersedes-message ()
4557   "Supersede current message."
4558   (interactive)
4559   (wl-summary-toggle-disp-msg 'off)
4560   (let ((summary-buf (current-buffer))
4561         message-buf from)
4562     (wl-summary-set-message-buffer-or-redisplay)
4563     (if (setq message-buf (wl-message-get-original-buffer))
4564         (set-buffer message-buf))
4565     (unless (wl-message-news-p)
4566       (set-buffer summary-buf)
4567       (if (and (eq (elmo-folder-type-internal wl-summary-buffer-elmo-folder)
4568                    'nntp)
4569                (y-or-n-p "Cannot get Newsgroups. Fetch again? "))
4570           (progn
4571             (wl-summary-redisplay t)
4572             (wl-summary-supersedes-message))
4573         (error "This is not a news article; supersedes is impossible")))
4574     (save-excursion
4575       (setq from (std11-field-body "from"))
4576       ;; Make sure that this article was written by the user.
4577       (unless (wl-address-user-mail-address-p
4578                (wl-address-header-extract-address
4579                 (car (wl-parse-addresses from))))
4580         (error "This article is not yours"))
4581       (let* ((message-id (std11-field-body "message-id"))
4582              (followup-to (std11-field-body "followup-to"))
4583              (mail-default-headers
4584               (concat mail-default-headers
4585                       "Supersedes: " message-id "\n"
4586                       (and followup-to
4587                            (concat "Followup-To: " followup-to "\n")))))
4588         (if message-buf (set-buffer message-buf))
4589         (wl-draft-edit-string (buffer-substring (point-min) (point-max)))))))
4590
4591 (defun wl-summary-save (&optional arg wl-save-dir)
4592   "Save current message to disk."
4593   (interactive)
4594   (let ((filename)
4595         (num (wl-summary-message-number)))
4596     (if (null wl-save-dir)
4597         (setq wl-save-dir wl-temporary-file-directory))
4598     (if num
4599         (save-excursion
4600           (setq filename (expand-file-name
4601                           (concat (int-to-string num)
4602                                   wl-summary-save-file-suffix)
4603                           wl-save-dir))
4604           (if (null (and arg
4605                          (null (file-exists-p filename))))
4606               (setq filename
4607                     (read-file-name "Save to file: " filename)))
4608
4609           (wl-summary-set-message-buffer-or-redisplay)
4610           (set-buffer (wl-message-get-original-buffer))
4611           (if (and (null arg) (file-exists-p filename))
4612               (if (y-or-n-p "File already exists.  override it? ")
4613                   (write-region (point-min) (point-max) filename))
4614             (write-region (point-min) (point-max) filename)))
4615       (message "No message to save."))
4616     num))
4617
4618 (defun wl-summary-save-region (beg end)
4619   (interactive "r")
4620   (save-excursion
4621     (save-restriction
4622       (wl-summary-narrow-to-region beg end)
4623       (goto-char (point-min))
4624       (let ((wl-save-dir
4625              (wl-read-directory-name "Save to directory: "
4626                                      wl-temporary-file-directory)))
4627         (if (null (file-exists-p wl-save-dir))
4628             (make-directory wl-save-dir))
4629         (if (eq wl-summary-buffer-view 'thread)
4630             (progn
4631               (while (not (eobp))
4632                 (let* ((number (wl-summary-message-number))
4633                        (entity (wl-thread-get-entity number)))
4634                   (if (wl-thread-entity-get-opened entity)
4635                       (wl-summary-save t wl-save-dir)
4636                     ;; closed
4637                     (wl-summary-save t wl-save-dir))
4638                   (forward-line 1))))
4639           (while (not (eobp))
4640             (wl-summary-save t wl-save-dir)
4641             (forward-line 1)))))))
4642
4643 ;; mew-summary-pipe-message()
4644 (defun wl-summary-pipe-message (prefix command)
4645   "Send this message via pipe."
4646   (interactive (list current-prefix-arg nil))
4647   (if (null (wl-summary-message-number))
4648       (message "No message.")
4649     (setq command (read-string "Shell command on message: "
4650                                wl-summary-shell-command-last))
4651     (if (y-or-n-p "Send this message to pipe? ")
4652         (wl-summary-pipe-message-subr prefix command))))
4653
4654 (defun wl-summary-target-mark-pipe (prefix command)
4655   "Send each marked messages via pipe."
4656   (interactive (list current-prefix-arg nil))
4657   (if (null wl-summary-buffer-target-mark-list)
4658       (message "No marked message.")
4659     (setq command (read-string "Shell command on each marked message: "
4660                                wl-summary-shell-command-last))
4661     (when (y-or-n-p "Send each marked message to pipe? ")
4662       (while (car wl-summary-buffer-target-mark-list)
4663         (let ((num (car wl-summary-buffer-target-mark-list)))
4664           (wl-thread-jump-to-msg num)
4665           (wl-summary-pipe-message-subr prefix command)
4666           (wl-summary-unmark))))))
4667
4668 (defun wl-summary-pipe-message-subr (prefix command)
4669   (save-excursion
4670     (wl-summary-set-message-buffer-or-redisplay)
4671     (set-buffer (wl-message-get-original-buffer))
4672     (if (string= command "")
4673         (setq command wl-summary-shell-command-last))
4674     (goto-char (point-min)) ; perhaps this line won't be necessary
4675     (if prefix
4676         (search-forward "\n\n"))
4677     (shell-command-on-region (point) (point-max) command nil)
4678     (setq wl-summary-shell-command-last command)))
4679
4680 (defun wl-summary-print-message (&optional arg)
4681   (interactive "P")
4682   (if (null (wl-summary-message-number))
4683       (message "No message.")
4684     (save-excursion
4685       (wl-summary-set-message-buffer-or-redisplay)
4686       (if (or (not (interactive-p))
4687               (y-or-n-p "Print ok? "))
4688           (progn
4689             (let ((buffer (generate-new-buffer " *print*")))
4690               (copy-to-buffer buffer (point-min) (point-max))
4691               (set-buffer buffer)
4692               (funcall wl-print-buffer-function)
4693               (kill-buffer buffer)))
4694         (message "")))))
4695
4696 (defun wl-summary-print-message-with-ps-print (&optional filename)
4697   "Print message via ps-print."
4698   (interactive)
4699   (if (null (wl-summary-message-number))
4700       (message "No message.")
4701     (setq filename (ps-print-preprint current-prefix-arg))
4702     (if (or (not (interactive-p))
4703             (y-or-n-p "Print ok? "))
4704         (let ((summary-buffer (current-buffer))
4705               wl-break-pages)
4706           (save-excursion
4707             (wl-summary-set-message-buffer-or-redisplay)
4708             ;; (wl-summary-redisplay-internal)
4709             (let* ((buffer (generate-new-buffer " *print*"))
4710                    (entity (progn
4711                              (set-buffer summary-buffer)
4712                              (elmo-message-entity
4713                               wl-summary-buffer-elmo-folder
4714                               (wl-summary-message-number))))
4715                    (wl-ps-subject
4716                     (and entity
4717                          (or (elmo-message-entity-field entity 'subject t)
4718                              "")))
4719                    (wl-ps-from
4720                     (and entity
4721                          (or (elmo-message-entity-field entity 'from t) "")))
4722                    (wl-ps-date
4723                     (and entity
4724                          (or (elmo-message-entity-field entity 'date) ""))))
4725               (run-hooks 'wl-ps-preprint-hook)
4726               (set-buffer wl-message-buffer)
4727               (copy-to-buffer buffer (point-min) (point-max))
4728               (set-buffer buffer)
4729               (unwind-protect
4730                   (let ((ps-left-header
4731                          (list (concat "(" wl-ps-subject ")")
4732                                (concat "(" wl-ps-from ")")))
4733                         (ps-right-header
4734                          (list "/pagenumberstring load"
4735                                (concat "(" wl-ps-date ")"))))
4736                     (run-hooks 'wl-ps-print-hook)
4737                     (funcall wl-ps-print-buffer-function filename))
4738                 (kill-buffer buffer)))))
4739       (message ""))))
4740
4741 (if (featurep 'ps-print) ; ps-print is available.
4742     (fset 'wl-summary-print-message 'wl-summary-print-message-with-ps-print))
4743
4744 (defun wl-summary-target-mark-print ()
4745   (interactive)
4746   (if (null wl-summary-buffer-target-mark-list)
4747       (message "No marked message.")
4748     (when (y-or-n-p "Print all marked messages. OK? ")
4749       (while (car wl-summary-buffer-target-mark-list)
4750         (let ((num (car wl-summary-buffer-target-mark-list)))
4751           (wl-thread-jump-to-msg num)
4752           (wl-summary-print-message)
4753           (wl-summary-unmark))))))
4754
4755 (defun wl-summary-folder-info-update ()
4756   (wl-folder-set-folder-updated
4757    (elmo-string (wl-summary-buffer-folder-name))
4758    (list 0
4759          (+ wl-summary-buffer-unread-count
4760             wl-summary-buffer-new-count)
4761          (elmo-folder-length
4762           wl-summary-buffer-elmo-folder))))
4763
4764 (defun wl-summary-get-original-buffer ()
4765   "Get original buffer for the current summary."
4766   (save-excursion
4767     (wl-summary-set-message-buffer-or-redisplay)
4768     (wl-message-get-original-buffer)))
4769
4770 (defun wl-summary-pack-number (&optional arg)
4771   (interactive "P")
4772   (elmo-folder-pack-numbers wl-summary-buffer-elmo-folder)
4773   (let (wl-use-scoring)
4774     (wl-summary-rescan nil nil t)))
4775
4776 (defun wl-summary-target-mark-uudecode ()
4777   (interactive)
4778   (let ((mlist (reverse wl-summary-buffer-target-mark-list))
4779         (summary-buf (current-buffer))
4780         (tmp-buf (get-buffer-create "*WL UUENCODE*"))
4781         orig-buf i k filename rc errmsg)
4782     (setq i 1)
4783     (setq k (length mlist))
4784     (set-buffer tmp-buf)
4785     (erase-buffer)
4786     (save-window-excursion
4787       (while mlist
4788         (set-buffer summary-buf)
4789         (wl-summary-jump-to-msg (car mlist))
4790         (wl-summary-redisplay)
4791         (set-buffer (setq orig-buf (wl-summary-get-original-buffer)))
4792         (goto-char (point-min))
4793         (cond ((= i 1) ; first
4794                (if (setq filename (wl-message-uu-substring
4795                                    orig-buf tmp-buf t
4796                                    (= i k)))
4797                    nil
4798                  (error "Can't find begin line")))
4799               ((< i k)
4800                (wl-message-uu-substring orig-buf tmp-buf))
4801               (t ; last
4802                (wl-message-uu-substring orig-buf tmp-buf nil t)))
4803         (setq i (1+ i))
4804         (setq mlist (cdr mlist)))
4805       (set-buffer tmp-buf)
4806       (message "Exec %s..." wl-prog-uudecode)
4807       (unwind-protect
4808           (let ((decode-dir wl-temporary-file-directory))
4809             (if (not wl-prog-uudecode-no-stdout-option)
4810                 (setq filename (read-file-name "Save to file: "
4811                                                (expand-file-name
4812                                                 (elmo-safe-filename filename)
4813                                                 wl-temporary-file-directory)))
4814               (setq decode-dir
4815                     (wl-read-directory-name "Save to directory: "
4816                                             wl-temporary-file-directory))
4817               (setq filename (expand-file-name filename decode-dir)))
4818             (if (file-exists-p filename)
4819                 (or (yes-or-no-p (format "File %s exists. Save anyway? "
4820                                          filename))
4821                     (error "")))
4822             (elmo-bind-directory
4823              decode-dir
4824              (setq rc
4825                    (as-binary-process
4826                     (apply 'call-process-region (point-min) (point-max)
4827                            wl-prog-uudecode t (current-buffer) nil
4828                            wl-prog-uudecode-arg))))
4829             (when (not (= 0 rc))
4830               (setq errmsg (buffer-substring (point-min)(point-max)))
4831               (error "Uudecode error: %s" errmsg))
4832             (if (not wl-prog-uudecode-no-stdout-option)
4833                 (let (file-name-handler-alist) ;; void jka-compr
4834                   (as-binary-output-file
4835                    (write-region (point-min) (point-max)
4836                                  filename nil 'no-msg))))
4837             (save-excursion
4838               (set-buffer summary-buf)
4839               (wl-summary-delete-all-target-marks))
4840             (if (file-exists-p filename)
4841                 (message "Saved as %s" filename)))
4842         (kill-buffer tmp-buf)))))
4843
4844 ;; Someday
4845 ;; (defun wl-summary-drop-unsync ()
4846 ;;   "Drop all unsync messages."
4847 ;;   (interactive)
4848 ;;   (if (elmo-folder-pipe-p (wl-summary-buffer-folder-name))
4849 ;;       (error "You cannot drop unsync messages in this folder"))
4850 ;;   (if (or (not (interactive-p))
4851 ;;        (y-or-n-p "Drop all unsync messages? "))
4852 ;;       (let* ((folder-list (elmo-folder-get-primitive-folder-list
4853 ;;                         (wl-summary-buffer-folder-name)))
4854 ;;           (is-multi (elmo-multi-p (wl-summary-buffer-folder-name)))
4855 ;;           (sum 0)
4856 ;;           (multi-num 0)
4857 ;;           pair)
4858 ;;      (message "Dropping...")
4859 ;;      (while folder-list
4860 ;;        (setq pair (elmo-folder-message-numbers (car folder-list)))
4861 ;;        (when is-multi ;; dirty hack...
4862 ;;          (incf multi-num)
4863 ;;          (setcar pair (+ (* multi-num elmo-multi-divide-number)
4864 ;;                          (car pair))))
4865 ;;        (elmo-msgdb-set-number-alist
4866 ;;         (wl-summary-buffer-msgdb)
4867 ;;         (nconc
4868 ;;          (elmo-msgdb-get-number-alist (wl-summary-buffer-msgdb))
4869 ;;          (list (cons (car pair) nil))))
4870 ;;        (setq sum (+ sum (cdr pair)))
4871 ;;        (setq folder-list (cdr folder-list)))
4872 ;;      (wl-summary-set-message-modified)
4873 ;;      (wl-folder-set-folder-updated (wl-summary-buffer-folder-name)
4874 ;;                                    (list 0
4875 ;;                                          (+ wl-summary-buffer-unread-count
4876 ;;                                             wl-summary-buffer-new-count)
4877 ;;                                          sum))
4878 ;;      (message "Dropping...done"))))
4879
4880 (defun wl-summary-default-get-next-msg (msg)
4881   (or (wl-summary-next-message msg
4882                                (if wl-summary-move-direction-downward 'down
4883                                  'up)
4884                                nil)
4885       (cadr (memq msg (if wl-summary-move-direction-downward
4886                           wl-summary-buffer-number-list
4887                         (reverse wl-summary-buffer-number-list))))))
4888
4889 (defun wl-summary-save-current-message ()
4890   "Save current message for `wl-summary-yank-saved-message'."
4891   (interactive)
4892   (let ((number (wl-summary-message-number)))
4893     (setq wl-summary-buffer-saved-message number)
4894     (and number (message "No: %s is saved." number))))
4895
4896 (defun wl-summary-yank-saved-message ()
4897   "Set current message as a parent of the saved message."
4898   (interactive)
4899   (if wl-summary-buffer-saved-message
4900       (let ((number (wl-summary-message-number)))
4901         (if (eq wl-summary-buffer-saved-message number)
4902             (message "Cannot set itself as a parent.")
4903           (save-excursion
4904             (wl-thread-jump-to-msg wl-summary-buffer-saved-message)
4905             (wl-thread-set-parent number)
4906             (wl-summary-set-thread-modified))
4907           (setq  wl-summary-buffer-saved-message nil)))
4908     (message "There's no saved message.")))
4909
4910 (defun wl-summary-toggle-header-narrowing ()
4911   "Toggle message header narrowing."
4912   (interactive)
4913   (when wl-message-use-header-narrowing
4914     (save-selected-window
4915       (let* ((mbuf wl-message-buffer)
4916              (mwin (when mbuf (get-buffer-window mbuf)))
4917              (wpos (when mwin (window-start mwin))))
4918         (when mbuf
4919           (set-buffer mbuf)
4920           (wl-message-header-narrowing-toggle)
4921           (and wpos (set-window-start mwin wpos)))))))
4922
4923 (autoload 'elmo-folder-list-global-flag-messages "elmo-flag")
4924
4925 (require 'product)
4926 (product-provide (provide 'wl-summary) (require 'wl-version))
4927
4928 ;;; wl-summary.el ends here