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