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