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