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