* wl-summary.el (wl-summary-target-mark-reply-with-citation): Fixed.
[elisp/wanderlust.git] / wl / wl-summary.el
index 736a3be..ad0dcbf 100644 (file)
@@ -1,10 +1,15 @@
 ;;; wl-summary.el -- Summary mode for Wanderlust.
 
-;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
+;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
+;; Copyright (C) 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
+;; Copyright (C) 1999,2000      TSUMURA Tomoaki <tsumura@kuis.kyoto-u.ac.jp>
+;; Copyright (C) 1999,2000      Kenichi OKADA <okada@opaopa.org>
 
 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
+;;     Masahiro MURATA <muse@ba2.so-net.ne.jp>
+;;     TSUMURA Tomoaki <tsumura@kuis.kyoto-u.ac.jp>
+;;     Kenichi OKADA <okada@opaopa.org>
 ;; Keywords: mail, net news
-;; Time-stamp: <2000-04-13 16:03:31 teranisi>
 
 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
 
 ;;
 
 ;;; Commentary:
-;; 
+;;
 
 ;;; Code:
-;; 
+;;
 
-(require 'elmo2)
+(require 'elmo)
 (require 'elmo-multi)
 (require 'wl-message)
 (require 'wl-vars)
 (require 'wl-highlight)
 (require 'wl-refile)
 (require 'wl-util)
-(condition-case ()
-    (progn
-      (require 'timezone)
-      (require 'easymenu))
-  (error))
+(condition-case nil (require 'timezone) (error nil))
+(condition-case nil (require 'easymenu) (error nil))
 (require 'elmo-date)
-(condition-case nil
-    (require 'ps-print)
-  (error))
+(condition-case nil (require 'ps-print) (error nil))
 
 (eval-when-compile
   (require 'cl)
   (condition-case () (require 'timer) (error nil))
-  (mapcar
-   (function
-    (lambda (symbol)
-      (unless (boundp symbol)
-       (set (make-local-variable symbol) nil))))
-   '(dragdrop-drop-functions scrollbar-height mail-reply-buffer))
-  (defun-maybe ps-print-buffer-with-faces (a))
-  (defun-maybe elmo-database-msgid-put (a b c))
-  (defun-maybe elmo-database-close ())
-  (defun-maybe elmo-database-msgid-get (a))
-  (defun-maybe run-with-idle-timer (secs repeat function &rest args)))
+  (defalias-maybe 'ps-print-buffer-with-faces 'ignore)
+  (defalias-maybe 'elmo-database-msgid-put 'ignore)
+  (defalias-maybe 'elmo-database-close 'ignore)
+  (defalias-maybe 'elmo-database-msgid-get 'ignore)
+  (defalias-maybe 'run-with-idle-timer 'ignore)
+  (defalias-maybe 'ps-print-preprint 'ignore))
+
+(defvar dragdrop-drop-functions)
+(defvar scrollbar-height)
+(defvar mail-reply-buffer)
 
 (defvar wl-summary-buffer-name "Summary")
 (defvar wl-summary-mode-map nil)
 (defvar wl-current-summary-buffer nil)
 
-(defvar wl-summary-buffer-msgdb       nil)
-(defvar wl-summary-buffer-folder-name nil)
+(defvar wl-summary-buffer-elmo-folder nil)
+
+(defmacro wl-summary-buffer-folder-name ()
+  (` (and wl-summary-buffer-elmo-folder
+         (elmo-folder-name-internal wl-summary-buffer-elmo-folder))))
+
+(defmacro wl-summary-buffer-msgdb ()
+  (` (and wl-summary-buffer-elmo-folder
+         (elmo-folder-msgdb wl-summary-buffer-elmo-folder))))
+
+(defvar wl-summary-buffer-folder-indicator nil)
 (defvar wl-summary-buffer-disp-msg    nil)
 (defvar wl-summary-buffer-disp-folder nil)
-(defvar wl-summary-buffer-refile-list nil) 
-(defvar wl-summary-buffer-delete-list nil) 
+(defvar wl-summary-buffer-refile-list nil)
+(defvar wl-summary-buffer-delete-list nil)
 (defvar wl-summary-buffer-last-displayed-msg nil)
 (defvar wl-summary-buffer-current-msg nil)
 (defvar wl-summary-buffer-unread-status " (0 new/0 unread)")
 (defvar wl-summary-buffer-mime-charset  nil)
 (defvar wl-summary-buffer-weekday-name-lang  nil)
 (defvar wl-summary-buffer-thread-indent-set-alist  nil)
-(defvar wl-summary-buffer-message-redisplay-func nil)
 (defvar wl-summary-buffer-view 'thread)
 (defvar wl-summary-buffer-message-modified nil)
 (defvar wl-summary-buffer-mark-modified nil)
+(defvar wl-summary-buffer-thread-modified nil)
 (defvar wl-summary-buffer-number-column nil)
 (defvar wl-summary-buffer-number-regexp nil)
 (defvar wl-summary-buffer-persistent nil)
 (defvar wl-summary-buffer-thread-nodes nil)
 (defvar wl-summary-buffer-target-mark-list nil)
-(defvar wl-summary-buffer-copy-list nil) 
+(defvar wl-summary-buffer-copy-list nil)
 (defvar wl-summary-buffer-prev-refile-destination nil)
 (defvar wl-summary-buffer-prev-copy-destination nil)
+(defvar wl-summary-buffer-saved-message nil)
+(defvar wl-summary-buffer-prev-folder-function nil)
+(defvar wl-summary-buffer-next-folder-function nil)
+(defvar wl-summary-buffer-exit-function nil)
+(defvar wl-summary-buffer-number-list nil)
+(defvar wl-summary-buffer-msgdb nil)
+(defvar wl-summary-buffer-folder-name nil)
+
 (defvar wl-thread-indent-level-internal nil)
 (defvar wl-thread-have-younger-brother-str-internal nil)
 (defvar wl-thread-youngest-child-str-internal nil)
 (defvar wl-read-folder-hist nil)
 (defvar wl-summary-scored nil)
 (defvar wl-crosspost-alist-modified nil)
+(defvar wl-summary-alike-hashtb nil)
+(defvar wl-summary-search-buf-name " *wl-search-subject*")
+(defvar wl-summary-delayed-update nil)
+(defvar wl-summary-search-buf-folder-name nil)
+
+(defvar wl-summary-get-petname-function 'wl-address-get-petname-1)
 
-(defvar wl-summary-message-regexp "^ *\\([0-9]+\\)")
+(defvar wl-summary-message-regexp "^ *\\([0-9-]+\\)")
 
 (defvar wl-summary-shell-command-last "")
 
 (defvar wl-ps-preprint-hook nil)
 (defvar wl-ps-print-hook nil)
 
-(mapcar 
- (function make-variable-buffer-local)
- (list 'wl-summary-buffer-msgdb
-       'wl-summary-buffer-disp-msg
-       'wl-summary-buffer-disp-folder
-       'wl-summary-buffer-refile-list
-       'wl-summary-buffer-copy-list
-       'wl-summary-buffer-target-mark-list
-       'wl-summary-buffer-delete-list
-       'wl-summary-buffer-folder-name
-       'wl-summary-buffer-last-displayed-msg
-       'wl-summary-buffer-unread-status
-       'wl-summary-buffer-unread-count
-       'wl-summary-buffer-new-count
-       'wl-summary-buffer-mime-charset
-       'wl-summary-buffer-weekday-name-lang
-       'wl-summary-buffer-thread-indent-set
-       'wl-summary-buffer-message-redisplay-func
-       'wl-summary-buffer-view
-       'wl-summary-buffer-message-modified
-       'wl-summary-buffer-mark-modified
-       'wl-summary-buffer-number-column
-       'wl-summary-buffer-number-regexp
-       'wl-summary-buffer-persistent
-       'wl-summary-buffer-thread-nodes
-       'wl-summary-buffer-prev-refile-destination
-       'wl-summary-scored
-       'wl-summary-default-score
-       'wl-summary-move-direction-downward
-       'wl-summary-important-above
-       'wl-summary-temp-above
-       'wl-summary-mark-below
-       'wl-summary-expunge-below
-       'wl-thread-indent-level-internal
-       'wl-thread-have-younger-brother-str-internal
-       'wl-thread-youngest-child-str-internal
-       'wl-thread-vertical-str-internal
-       'wl-thread-horizontal-str-internal
-       'wl-thread-space-str-internal))
+(make-variable-buffer-local 'wl-summary-buffer-elmo-folder)
+(make-variable-buffer-local 'wl-summary-search-buf-folder-name)
+(make-variable-buffer-local 'wl-summary-buffer-disp-msg)
+(make-variable-buffer-local 'wl-summary-buffer-disp-folder)
+(make-variable-buffer-local 'wl-summary-buffer-refile-list)
+(make-variable-buffer-local 'wl-summary-buffer-copy-list)
+(make-variable-buffer-local 'wl-summary-buffer-target-mark-list)
+(make-variable-buffer-local 'wl-summary-buffer-delete-list)
+(make-variable-buffer-local 'wl-summary-buffer-folder-indicator)
+(make-variable-buffer-local 'wl-summary-buffer-last-displayed-msg)
+(make-variable-buffer-local 'wl-summary-buffer-unread-status)
+(make-variable-buffer-local 'wl-summary-buffer-unread-count)
+(make-variable-buffer-local 'wl-summary-buffer-new-count)
+(make-variable-buffer-local 'wl-summary-buffer-mime-charset)
+(make-variable-buffer-local 'wl-summary-buffer-weekday-name-lang)
+(make-variable-buffer-local 'wl-summary-buffer-thread-indent-set)
+(make-variable-buffer-local 'wl-summary-buffer-view)
+(make-variable-buffer-local 'wl-summary-buffer-message-modified)
+(make-variable-buffer-local 'wl-summary-buffer-mark-modified)
+(make-variable-buffer-local 'wl-summary-buffer-thread-modified)
+(make-variable-buffer-local 'wl-summary-buffer-number-column)
+(make-variable-buffer-local 'wl-summary-buffer-number-regexp)
+(make-variable-buffer-local 'wl-summary-buffer-persistent)
+(make-variable-buffer-local 'wl-summary-buffer-thread-nodes)
+(make-variable-buffer-local 'wl-summary-buffer-prev-refile-destination)
+(make-variable-buffer-local 'wl-summary-buffer-saved-message)
+(make-variable-buffer-local 'wl-summary-scored)
+(make-variable-buffer-local 'wl-summary-default-score)
+(make-variable-buffer-local 'wl-summary-move-direction-downward)
+(make-variable-buffer-local 'wl-summary-important-above)
+(make-variable-buffer-local 'wl-summary-target-above)
+(make-variable-buffer-local 'wl-summary-mark-below)
+(make-variable-buffer-local 'wl-summary-expunge-below)
+(make-variable-buffer-local 'wl-thread-indent-level-internal)
+(make-variable-buffer-local 'wl-thread-have-younger-brother-str-internal)
+(make-variable-buffer-local 'wl-thread-youngest-child-str-internal)
+(make-variable-buffer-local 'wl-thread-vertical-str-internal)
+(make-variable-buffer-local 'wl-thread-horizontal-str-internal)
+(make-variable-buffer-local 'wl-thread-space-str-internal)
+(make-variable-buffer-local 'wl-summary-buffer-prev-folder-function)
+(make-variable-buffer-local 'wl-summary-buffer-next-folder-function)
+(make-variable-buffer-local 'wl-summary-buffer-exit-function)
+(make-variable-buffer-local 'wl-summary-buffer-number-list)
+(make-variable-buffer-local 'wl-summary-buffer-msgdb)
+(make-variable-buffer-local 'wl-summary-buffer-folder-name)
 
 ;; internal functions (dummy)
 (unless (fboundp 'wl-summary-append-message-func-internal)
-  (defun wl-summary-append-message-func-internal (entity overview 
-                                                        mark-alist update)))
+  (defun wl-summary-append-message-func-internal (entity overview
+                                                        mark-alist update
+                                                        &optional force-insert)))
 (unless (fboundp 'wl-summary-from-func-internal)
   (defun wl-summary-from-func-internal (from)
     from))
   (defun wl-summary-subject-filter-func-internal (subject)
     subject))
 
-(defmacro wl-summary-sticky-buffer-name (folder)
-  (` (concat wl-summary-buffer-name ":" (, folder))))
+(defmacro wl-summary-sticky-buffer-name (name)
+  (` (concat wl-summary-buffer-name ":" (, name))))
 
 (defun wl-summary-default-subject (subject-string)
   (if (string-match "^[ \t]*\\[[^:]+[,: ][0-9]+\\][ \t]*" subject-string)
        (and (eq major-mode 'wl-summary-mode)
             (stringp wl-summary-showto-folder-regexp)
             (string-match wl-summary-showto-folder-regexp
-                          wl-summary-buffer-folder-name)
+                          (wl-summary-buffer-folder-name))
             (wl-address-user-mail-address-p from)
             (cond
              ((and (setq tos (elmo-msgdb-overview-entity-get-to entity))
                               (lambda (to)
                                 (eword-decode-string
                                  (if wl-use-petname
-                                     (wl-address-get-petname to)
-                                   (car 
-                                    (std11-extract-address-components to))))))
+                                     (or
+                                      (funcall wl-summary-get-petname-function to)
+                                      (car
+                                       (std11-extract-address-components to))
+                                      to)
+                                   to))))
                              (wl-parse-addresses tos)
                              ","))))
              ((setq ng (elmo-msgdb-overview-entity-get-extra-field
                         entity "newsgroups"))
               (setq retval (concat "Ng:" ng)))))
       (if wl-use-petname
-         (setq retval (wl-address-get-petname from))
+         (setq retval (or (funcall wl-summary-get-petname-function from)
+                          (car (std11-extract-address-components from))
+                          from))
        (setq retval from)))
     retval))
 
 (defun wl-summary-simple-from (string)
   (if wl-use-petname
-      (wl-address-get-petname string)
+      (or (funcall wl-summary-get-petname-function string)
+         (car (std11-extract-address-components string))
+         string)
     string))
 
 (defvar wl-summary-mode-menu-spec
      ["By Number" wl-summary-sort-by-number t]
      ["By Date" wl-summary-sort-by-date t]
      ["By From" wl-summary-sort-by-from t]
-     ["By Subject" wl-summary-sort-by-subject t])    
+     ["By Subject" wl-summary-sort-by-subject t])
     "----"
     ("Message Operation"
      ["Mark as read"    wl-summary-mark-as-read t]
     (defun wl-summary-setup-mouse ()
       (define-key wl-summary-mode-map 'button4 'wl-summary-prev)
       (define-key wl-summary-mode-map 'button5 'wl-summary-next)
-      (define-key wl-summary-mode-map [(shift button4)] 
+      (define-key wl-summary-mode-map [(shift button4)]
        'wl-summary-up)
-      (define-key wl-summary-mode-map [(shift button5)] 
+      (define-key wl-summary-mode-map [(shift button5)]
        'wl-summary-down)
       (define-key wl-summary-mode-map 'button2 'wl-summary-click))
   (if wl-on-nemacs
   (define-key wl-summary-mode-map "\e\r" 'wl-summary-prev-line-content)
   (define-key wl-summary-mode-map "g"    'wl-summary-goto-folder)
   (define-key wl-summary-mode-map "c"    'wl-summary-mark-as-read-all)
-  (define-key wl-summary-mode-map "D"    'wl-summary-drop-unsync)
-  
+;  (define-key wl-summary-mode-map "D"    'wl-summary-drop-unsync)
+
   (define-key wl-summary-mode-map "a"    'wl-summary-reply)
   (define-key wl-summary-mode-map "A"    'wl-summary-reply-with-citation)
   (define-key wl-summary-mode-map "C"    'wl-summary-cancel-message)
   (define-key wl-summary-mode-map "p"    'wl-summary-prev)
   (define-key wl-summary-mode-map "N"    'wl-summary-down)
   (define-key wl-summary-mode-map "P"    'wl-summary-up)
-;  (define-key wl-summary-mode-map "w"    'wl-draft)
+;;;(define-key wl-summary-mode-map "w"    'wl-draft)
   (define-key wl-summary-mode-map "w"    'wl-summary-write)
-  (define-key wl-summary-mode-map "W"    'wl-summary-write-current-newsgroup)
-;  (define-key wl-summary-mode-map "e"     'wl-draft-open-file)
+  (define-key wl-summary-mode-map "W"    'wl-summary-write-current-folder)
+;;;(define-key wl-summary-mode-map "e"     'wl-draft-open-file)
   (define-key wl-summary-mode-map "e"     'wl-summary-save)
   (define-key wl-summary-mode-map "\C-c\C-o" 'wl-jump-to-draft-buffer)
   (define-key wl-summary-mode-map "H"    'wl-summary-redisplay-all-header)
   (define-key wl-summary-mode-map "|"    'wl-summary-pipe-message)
   (define-key wl-summary-mode-map "q"    'wl-summary-exit)
   (define-key wl-summary-mode-map "Q"    'wl-summary-force-exit)
-  
+
   (define-key wl-summary-mode-map "j"    'wl-summary-jump-to-current-message)
   (define-key wl-summary-mode-map "J"    'wl-thread-jump-to-msg)
   (define-key wl-summary-mode-map "I"    'wl-summary-incorporate)
   (define-key wl-summary-mode-map "\M-j" 'wl-summary-jump-to-msg-by-message-id)
   (define-key wl-summary-mode-map "^"    'wl-summary-jump-to-parent-message)
   (define-key wl-summary-mode-map "!"    'wl-summary-mark-as-unread)
-  
+
   (define-key wl-summary-mode-map "s"    'wl-summary-sync)
   (define-key wl-summary-mode-map "S"    'wl-summary-sort)
   (define-key wl-summary-mode-map "\M-s"    'wl-summary-stick)
   (define-key wl-summary-mode-map "?"    'wl-summary-pick)
   (define-key wl-summary-mode-map "\ee"  'wl-summary-expire)
 
+  ;; copy & paste.
+  (define-key wl-summary-mode-map "\ew"  'wl-summary-save-current-message)
+  (define-key wl-summary-mode-map "\C-y"  'wl-summary-yank-saved-message)
+
   ;; line commands
   (define-key wl-summary-mode-map "R"    'wl-summary-mark-as-read)
   (define-key wl-summary-mode-map "i"    'wl-summary-prefetch)
   (define-key wl-summary-mode-map "O"    'wl-summary-copy)
   (define-key wl-summary-mode-map "\M-o" 'wl-summary-refile-prev-destination)
 ;  (define-key wl-summary-mode-map "\M-O" 'wl-summary-copy-prev-destination)
-  (define-key wl-summary-mode-map "\C-o" 'wl-summary-auto-refile)  
+  (define-key wl-summary-mode-map "\C-o" 'wl-summary-auto-refile)
   (define-key wl-summary-mode-map "d"    'wl-summary-delete)
   (define-key wl-summary-mode-map "u"    'wl-summary-unmark)
   (define-key wl-summary-mode-map "U"    'wl-summary-unmark-all)
   (define-key wl-summary-mode-map "t!" 'wl-thread-mark-as-unread)
   (define-key wl-summary-mode-map "t$" 'wl-thread-mark-as-important)
   (define-key wl-summary-mode-map "ty" 'wl-thread-save)
+  (define-key wl-summary-mode-map "ts" 'wl-thread-set-parent)
 
   ;; target-mark commands
   (define-key wl-summary-mode-map "m"    (make-sparse-keymap))
   (define-key wl-summary-mode-map "mA"   'wl-summary-target-mark-reply-with-citation)
   (define-key wl-summary-mode-map "mf"   'wl-summary-target-mark-forward)
   (define-key wl-summary-mode-map "m?"   'wl-summary-target-mark-pick)
-  
+
   ;; region commands
   (define-key wl-summary-mode-map "r"    (make-sparse-keymap))
   (define-key wl-summary-mode-map "rR"   'wl-summary-mark-as-read-region)
       (setq mark-alist (cdr mark-alist)))
     ret-val))
 
-(defun wl-summary-count-unread (mark-alist &optional folder)
+(defun wl-summary-count-unread (mark-alist)
   (let ((new 0)
        (unread 0)
        mark)
     (while mark-alist
       (setq mark (cadr (car mark-alist)))
       (and mark
-          (cond 
+          (cond
            ((string= mark wl-summary-new-mark)
             (setq new (+ 1 new)))
            ((or (string= mark wl-summary-unread-uncached-mark)
             (setq unread (+ 1 unread)))))
       (setq mark-alist (cdr mark-alist)))
     (if (eq major-mode 'wl-summary-mode)
-       (setq wl-summary-buffer-new-count new 
+       (setq wl-summary-buffer-new-count new
              wl-summary-buffer-unread-count unread))
     (+ new unread)))
 
-(defun wl-summary-make-modeline ()
-  "Create new modeline format for Wanderlust"
-  (let* ((duplicated (copy-sequence mode-line-format))
-        (cur-entry duplicated)
-        return-modeline)
-    (if (memq 'wl-plug-state-indicator mode-line-format)
-       duplicated
-      (catch 'done
-       (while cur-entry
-         (if (or (and (symbolp (car cur-entry))
-                      (eq 'mode-line-buffer-identification 
-                             (car cur-entry)))
-                 (and (consp (car cur-entry))
-                      (or 
-                       (eq 'modeline-buffer-identification 
-                              (car (car cur-entry)))
-                       (eq 'modeline-buffer-identification 
-                              (cdr (car cur-entry))))))
-             (progn
-               (setq return-modeline (append return-modeline
-                                             (list 
-                                              'wl-plug-state-indicator
-                                              (car cur-entry)
-                                              'wl-summary-buffer-unread-status)
-                                             (cdr cur-entry)))
-               (throw 'done return-modeline))
-           (setq return-modeline (append return-modeline
-                                         (list (car cur-entry)))))
-         (setq cur-entry (cdr cur-entry)))))))
-
 (defun wl-summary-reedit (&optional arg)
   "Re-edit current message.
-If optional argument is non-nil, Supersedes message"
+If ARG is non-nil, Supersedes message"
   (interactive "P")
   (if arg
       (wl-summary-supersedes-message)
-    (if (string= wl-summary-buffer-folder-name wl-draft-folder)
+    (if (string= (wl-summary-buffer-folder-name) wl-draft-folder)
        (if (wl-summary-message-number)
            (unwind-protect
                (wl-draft-reedit (wl-summary-message-number))
@@ -582,10 +592,11 @@ If optional argument is non-nil, Supersedes message"
              (delete-other-windows)))
       (save-excursion
        (let ((mmelmo-force-fetch-entire-message t))
-         (wl-summary-set-message-buffer-or-redisplay)
-         (set-buffer (wl-message-get-original-buffer))
-         (wl-draft-edit-string (buffer-substring (point-min)
-                                                 (point-max))))))))
+         (if (null (wl-summary-message-number))
+             (message "No message.")
+           (set-buffer (wl-summary-get-original-buffer))
+           (wl-draft-edit-string (buffer-substring (point-min)
+                                                   (point-max)))))))))
 
 (defun wl-summary-resend-bounced-mail ()
   "Re-mail the current message.
@@ -675,55 +686,32 @@ you."
        (kill-buffer (current-buffer)))
       (message "Resending message to %s...done" address))))
 
-(defun wl-summary-msgdb-load-async (folder)
-  "Loading msgdb and selecting folder is executed asynchronously in IMAP4.
-Returns nil if selecting folder was in failure."
-  (if (and (elmo-folder-plugged-p folder)
-          (eq (elmo-folder-get-type folder) 'imap4))
-      (let* ((spec (elmo-folder-get-spec folder))
-            (connection (elmo-imap4-get-connection spec))
-            (process (elmo-imap4-connection-get-process connection))
-            msgdb response)
-       (save-excursion
-         (unwind-protect
-             (progn
-               (elmo-imap4-send-command (process-buffer process)
-                                        process
-                                        (format "select \"%s\"" 
-                                                (elmo-imap4-spec-folder
-                                                 spec)))
-               (setq msgdb (elmo-msgdb-load (elmo-string folder)))
-               (setq response (elmo-imap4-read-response 
-                               (process-buffer process)
-                               process)))
-           (if (null response)
-               (progn
-                 (setcar (cddr connection) nil)
-                 (error "Select folder failed"))
-             (setcar (cddr connection) (elmo-imap4-spec-folder spec))))
-         (if response msgdb)))
-    (elmo-msgdb-load (elmo-string folder))))
-
 (defun wl-summary-buffer-set-folder (folder)
-  (setq wl-summary-buffer-folder-name folder)
-  (when (wl-summary-sticky-p)
-    (make-local-variable 'wl-message-buf-name)
-    (setq wl-message-buf-name (format "%s:%s" wl-message-buf-name folder)))
-  (setq wl-summary-buffer-mime-charset (or (wl-get-assoc-list-value 
+  (if (stringp folder)
+      (setq folder (wl-folder-get-elmo-folder folder)))
+  (setq wl-summary-buffer-elmo-folder folder)
+  (setq wl-summary-buffer-folder-indicator
+       (if (memq 'modeline wl-use-folder-petname)
+           (wl-folder-get-petname (elmo-folder-name-internal folder))
+         (elmo-folder-name-internal folder)))
+  (make-local-variable 'wl-message-buffer)
+  (setq wl-summary-buffer-mime-charset (or (wl-get-assoc-list-value
                                            wl-folder-mime-charset-alist
-                                           folder)
+                                           (elmo-folder-name-internal folder))
                                           wl-mime-charset))
-  (setq wl-summary-buffer-weekday-name-lang 
-       (or (wl-get-assoc-list-value 
+  (setq wl-summary-buffer-weekday-name-lang
+       (or (wl-get-assoc-list-value
             wl-folder-weekday-name-lang-alist
-            folder)
+            (elmo-folder-name-internal folder))
            wl-summary-weekday-name-lang))
   (setq wl-summary-buffer-thread-indent-set
-       (wl-get-assoc-list-value 
+       (wl-get-assoc-list-value
         wl-folder-thread-indent-set-alist
-        folder))
-  (setq wl-summary-buffer-persistent (wl-folder-persistent-p folder))
-  (setq 
+        (elmo-folder-name-internal folder)))
+  (setq wl-summary-buffer-persistent
+       (wl-folder-persistent-p (elmo-folder-name-internal folder)))
+  (elmo-folder-set-persistent-internal folder wl-summary-buffer-persistent)
+  (setq
    wl-thread-indent-level-internal
    (or (nth 0 wl-summary-buffer-thread-indent-set)
        wl-thread-indent-level)
@@ -742,8 +730,8 @@ Returns nil if selecting folder was in failure."
    wl-thread-space-str-internal
    (or (nth 5 wl-summary-buffer-thread-indent-set)
        wl-thread-space-str))
-  (setq wl-thread-indent-regexp 
-       (concat 
+  (setq wl-thread-indent-regexp
+       (concat
         (regexp-quote wl-thread-have-younger-brother-str-internal) "\\|"
         (regexp-quote wl-thread-youngest-child-str-internal) "\\|"
         (regexp-quote wl-thread-vertical-str-internal) "\\|"
@@ -753,95 +741,52 @@ Returns nil if selecting folder was in failure."
 
 (defun wl-summary-mode ()
   "Major mode for reading threaded messages.
-The keys that are defined for this mode are:\\<wl-summary-mode-map>
-
-SPC    Read messages. 
-DEL    Back-scroll this message. 
-.      Force to display this message. 
-RET    Make this message scroll up with one line.
-M-RET -        Make this message scroll down with one line.
-
-C-n    Go to the next line.
-C-p    Go to the previous line.
-n      Move to below then display. 
-N       Move to next unread.
-p      Move to above then display. 
-P       Move to previous unread.
-s      Sync current folder.
-t       Same as 's' but force update. 
-g      Go to the folder which you input.
-w      Write a message. A new draft is prepared.
-a      Answer to this message. A new draft is prepared in Draft mode. 
-f      Forward this message to a third person. A new draft is prepared in 
-       Draft mode and this message is automatically attached.
-v      Toggle \"Summary and Folder view\".
-        You can quickly put the delete marks since the next message is not 
-        displayed.
-i       Prefetch message if uncached.
-o      Put the refile mark('o') on this message. 
-!      Mark current message as unread.
-$      Toggle mark current message as important.
-d      Put the delete mark('D') on this message.
-c       Check all messages as read.
-*      Put the temporal mark('*') on this message. 
-u      Cancel the mark on this message.
-x      Process marked messages. 
-
-mo     Put the refile mark onto all messages marked with '*'.
-       This is very convenient to refile all messages picked by '?'.
-md     Put the delete mark onto all messages marked with '*'.
-mi      Prefetch all messages marked with '*'.
-mu     Unmark all target-marked messages.
-mt      Put the '*' mark onto all messages which belong to th current thread.
-ma      Put the '*' mark onto all messages.
-?      Pick messages according to a pick pattern which you input, 
-       then put the '*' mark onto them.
-q      Goto folder mode.
-"
+See Info under Wanderlust for full documentation.
+
+Special commands:
+\\{wl-summary-mode-map}
+
+Entering Folder mode calls the value of `wl-summary-mode-hook'."
   (interactive)
+  (unless (interactive-p) (kill-all-local-variables))
   (setq major-mode 'wl-summary-mode)
   (setq mode-name "Summary")
   (use-local-map wl-summary-mode-map)
-  (setq wl-summary-buffer-refile-list nil)
-  (setq wl-summary-buffer-target-mark-list nil)
-  (setq wl-summary-buffer-delete-list nil)
-  (setq wl-summary-scored nil)
-  (setq wl-summary-buffer-disp-msg nil)  
-;; (setq default-directory (or wl-tmp-dir (expand-file-name "~/")))  
+;;;(setq default-directory (or wl-tmp-dir (expand-file-name "~/")))
   (setq buffer-read-only t)
   (setq truncate-lines t)
-;  (make-local-variable 'tab-width)
-;  (setq tab-width 1)
+;;;(make-local-variable 'tab-width)
+;;;(setq tab-width 1)
   (buffer-disable-undo (current-buffer))
-  (if wl-use-semi
-      (setq wl-summary-buffer-message-redisplay-func 
-           'wl-mmelmo-message-redisplay)
-    (setq wl-summary-buffer-message-redisplay-func
-         'wl-normal-message-redisplay))
-  (wl-xmas-setup-summary) ; setup toolbar, dnd, etc.
-  (when wl-show-plug-status-on-modeline 
-    (setq mode-line-format (wl-summary-make-modeline)))
+  (wl-mode-line-buffer-identification '("Wanderlust: "
+                                       wl-summary-buffer-folder-indicator
+                                       wl-summary-buffer-unread-status))
   (easy-menu-add wl-summary-mode-menu)
+  (when wl-summary-lazy-highlight
+    (make-local-variable 'window-scroll-functions)
+    (add-hook 'window-scroll-functions 'wl-highlight-summary-window))  
+  ;; This hook may contain the function `wl-setup-summary' for reasons
+  ;; of system internal to accord facilities for the Emacs variants.
   (run-hooks 'wl-summary-mode-hook))
 
 (defun wl-summary-overview-entity-compare-by-date (x y)
-  "Compare entity by date"
+  "Compare entity X and Y by date."
   (condition-case nil
       (string<
-       (timezone-make-date-sortable 
+       (timezone-make-date-sortable
        (elmo-msgdb-overview-entity-get-date x))
-       (timezone-make-date-sortable 
+       (timezone-make-date-sortable
        (elmo-msgdb-overview-entity-get-date y)))
     (error))) ;; ignore error.
 
 (defun wl-summary-overview-entity-compare-by-number (x y)
-  "Compare entity by number"
+   "Compare entity X and Y by number."
   (<
    (elmo-msgdb-overview-entity-get-number x)
    (elmo-msgdb-overview-entity-get-number y)))
 
 (defun wl-summary-overview-entity-compare-by-from (x y)
-  "Compare entity by from"
+  "Compare entity X and Y by from."
   (string<
    (wl-address-header-extract-address
     (or (elmo-msgdb-overview-entity-get-from-no-decode x)
@@ -851,7 +796,7 @@ q   Goto folder mode.
        wl-summary-no-from-message))))
 
 (defun wl-summary-overview-entity-compare-by-subject (x y)
-  "Compare entity by subject"
+  "Compare entity X and Y by subject."
   (string< (elmo-msgdb-overview-entity-get-subject-no-decode x)
           (elmo-msgdb-overview-entity-get-subject-no-decode y)))
 
@@ -872,7 +817,7 @@ q   Goto folder mode.
   "Rescan current folder without updating."
   (interactive)
   (let* ((cur-buf (current-buffer))
-        (msgdb wl-summary-buffer-msgdb) 
+        (msgdb (wl-summary-buffer-msgdb))
         (overview (elmo-msgdb-get-overview msgdb))
         (number-alist (elmo-msgdb-get-number-alist msgdb))
         (mark-alist (elmo-msgdb-get-mark-alist msgdb))
@@ -883,8 +828,9 @@ q   Goto folder mode.
         (inhibit-read-only t)
         (buffer-read-only nil)
         expunged)
-    (fset 'wl-summary-append-message-func-internal 
+    (fset 'wl-summary-append-message-func-internal
          (wl-summary-get-append-message-func))
+    (wl-summary-buffer-number-column-detect nil)
     (erase-buffer)
     (message "Re-scanning...")
     (setq i 0)
@@ -896,47 +842,66 @@ q Goto folder mode.
                  (intern (format "wl-summary-overview-entity-compare-by-%s"
                                  sort-by))))
       (message "Sorting by %s...done" sort-by)
-      (elmo-msgdb-set-overview wl-summary-buffer-msgdb
+      (elmo-msgdb-set-overview (wl-summary-buffer-msgdb)
                               overview))
     (setq curp overview)
     (set-buffer cur-buf)
     (setq wl-thread-entity-hashtb (elmo-make-hash (* (length overview) 2)))
     (setq wl-thread-entity-list nil)
     (setq wl-thread-entities nil)
+    (setq wl-summary-buffer-number-list nil)
     (setq wl-summary-buffer-target-mark-list nil)
     (setq wl-summary-buffer-refile-list nil)
     (setq wl-summary-buffer-delete-list nil)
-    (message "Constructing summary structure..." percent)
+    (setq wl-summary-delayed-update nil)
+    (elmo-kill-buffer wl-summary-search-buf-name)
+    (message "Constructing summary structure...")
     (while curp
       (setq entity (car curp))
       (wl-summary-append-message-func-internal entity overview mark-alist
                                               nil)
       (setq curp (cdr curp))
-      (setq i (+ i 1))
-      (elmo-display-progress
-       'wl-summary-rescan "Constructing summary structure..."
-       (/ (* i 100) num)))
-    (message "Constructing summary structure...done." percent)
+      (when (> num elmo-display-progress-threshold)
+       (setq i (+ i 1))
+       (if (or (zerop (% i 5)) (= i num))
+           (elmo-display-progress
+            'wl-summary-rescan "Constructing summary structure..."
+            (/ (* i 100) num)))))
+    (when wl-summary-delayed-update
+      (while wl-summary-delayed-update
+       (message "Parent (%d) of message %d is no entity"
+                (caar wl-summary-delayed-update)
+                (elmo-msgdb-overview-entity-get-number
+                 (cdar wl-summary-delayed-update)))
+       (wl-summary-append-message-func-internal
+        (cdar wl-summary-delayed-update)
+        overview mark-alist nil t)
+       (setq wl-summary-delayed-update (cdr wl-summary-delayed-update))))
+    (message "Constructing summary structure...done")
     (set-buffer cur-buf)
-    (when (eq wl-summary-buffer-view 'thread)
-      (message "Inserting thread...")
-      (wl-thread-insert-top)
-      (message "Inserting thread...done."))
+    (if (eq wl-summary-buffer-view 'thread)
+       (progn
+         (message "Inserting thread...")
+         (wl-thread-insert-top)
+         (message "Inserting thread...done"))
+      (wl-summary-make-number-list))
     (when wl-use-scoring
       (setq wl-summary-scored nil)
       (wl-summary-score-headers nil msgdb
                                (wl-summary-rescore-msgs number-alist)
                                t)
-      (setq expunged (wl-summary-score-update-all-lines))
-      (if expunged
-         (message "%d message(s) are expunged by scoring." (length expunged))))
+      (when (and wl-summary-scored
+                (setq expunged (wl-summary-score-update-all-lines)))
+       (message "%d message(s) are expunged by scoring." (length expunged))))
     (wl-summary-set-message-modified)
-    (wl-summary-count-unread mark-alist)
-    (wl-summary-update-modeline)    
+    (wl-summary-count-unread
+     (elmo-msgdb-get-mark-alist
+      (elmo-folder-msgdb wl-summary-buffer-elmo-folder)))
+    (wl-summary-update-modeline)
     (goto-char (point-max))
     (forward-line -1)
     (set-buffer-modified-p nil)))
-    
+
 (defun wl-summary-next-folder-or-exit (&optional next-entity upward)
   (if (and next-entity
           wl-auto-select-next)
@@ -944,7 +909,7 @@ q   Goto folder mode.
        (wl-summary-toggle-disp-msg 'off)
        (unwind-protect
            (setq retval
-                 (wl-summary-goto-folder-subr next-entity 
+                 (wl-summary-goto-folder-subr next-entity
                                               'force-update
                                               nil
                                               nil ; not sticky
@@ -973,60 +938,35 @@ q Goto folder mode.
             "%" "%%")
            (if (null (car finfo))
                " (? new/? unread)"
-             (format 
-              " (%d new/%d unread)" 
+             (format
+              " (%d new/%d unread)"
               (nth 0 finfo)
               (+ (nth 0 finfo)
                  (nth 1 finfo))))))
       "folder mode"))
 
 (defun wl-summary-set-message-modified ()
+  (elmo-folder-set-message-modified-internal
+   wl-summary-buffer-elmo-folder t)
   (setq wl-summary-buffer-message-modified t))
 (defun wl-summary-message-modified-p ()
   wl-summary-buffer-message-modified)
 (defun wl-summary-set-mark-modified ()
+  (elmo-folder-set-mark-modified-internal
+   wl-summary-buffer-elmo-folder t)
   (setq wl-summary-buffer-mark-modified t))
 (defun wl-summary-mark-modified-p ()
   wl-summary-buffer-mark-modified)
-
-(defun wl-summary-msgdb-save ()
-  "Save msgdb if modified."
-  (when wl-summary-buffer-msgdb
-    (save-excursion
-      (let (path)
-       (when (wl-summary-message-modified-p)
-         (setq path (elmo-msgdb-expand-path wl-summary-buffer-folder-name))
-         (elmo-msgdb-overview-save 
-          path 
-          (elmo-msgdb-get-overview wl-summary-buffer-msgdb))
-         (elmo-msgdb-number-save 
-          path 
-          (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
-         (elmo-folder-set-info-max-by-numdb
-          (elmo-string wl-summary-buffer-folder-name)
-          (elmo-msgdb-get-number-alist
-           wl-summary-buffer-msgdb))
-         (setq wl-summary-buffer-message-modified nil))
-       (when (wl-summary-mark-modified-p)
-         (or path 
-             (setq path (elmo-msgdb-expand-path
-                         wl-summary-buffer-folder-name)))
-         (elmo-msgdb-mark-save 
-          path
-          (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
-;;       (elmo-folder-set-info-hashtb
-;;        (elmo-string wl-summary-buffer-folder-name)
-;;        nil nil
-;;        0
-;;        (+ wl-summary-buffer-new-count wl-summary-buffer-unread-count))
-;;       (setq wl-folder-info-alist-modified t)
-         (setq wl-summary-buffer-mark-modified nil))))))
+(defun wl-summary-set-thread-modified ()
+  (setq wl-summary-buffer-thread-modified t))
+(defun wl-summary-thread-modified-p ()
+  wl-summary-buffer-thread-modified)
 
 (defsubst wl-summary-cleanup-temp-marks (&optional sticky)
   (if (or wl-summary-buffer-refile-list
          wl-summary-buffer-copy-list
          wl-summary-buffer-delete-list)
-      (if (y-or-n-p "Marks remain to be executed. Execute them?")
+      (if (y-or-n-p "Marks remain to be executed.  Execute them? ")
          (progn
            (wl-summary-exec)
            (if (or wl-summary-buffer-refile-list
@@ -1046,131 +986,106 @@ q      Goto folder mode.
   (setq wl-summary-scored nil))
 
 ;; a subroutine for wl-summary-exit/wl-save-status
-(defun wl-summary-save-status (&optional sticky)
+;; Note that folder is not commited here.
+(defun wl-summary-save-view (&optional sticky)
   ;; already in summary buffer.
   (when wl-summary-buffer-persistent
     ;; save the current summary buffer view.
-    (if (and wl-summary-cache-use 
+    (if (and wl-summary-cache-use
             (or (wl-summary-message-modified-p)
-                (wl-summary-mark-modified-p)))
-       (wl-summary-save-view-cache sticky))
-    ;; save msgdb ... 
-    (wl-summary-msgdb-save)))
+                (wl-summary-mark-modified-p)
+                (wl-summary-thread-modified-p)))
+       (wl-summary-save-view-cache))))
 
 (defun wl-summary-force-exit ()
-  "Exit current summary. Buffer is deleted even the buffer is sticky"
+  "Exit current summary.  Buffer is deleted even the buffer is sticky."
   (interactive)
   (wl-summary-exit 'force-exit))
 
 (defun wl-summary-exit (&optional force-exit)
-  "Exit current summary. if FORCE-EXIT, exits even the summary is sticky."
+  "Exit current summary.  if FORCE-EXIT, exits even the summary is sticky."
   (interactive "P")
   (let ((summary-buf (current-buffer))
        (sticky (wl-summary-sticky-p))
-       (message-buf (get-buffer wl-message-buf-name))
        summary-win
        message-buf message-win
        folder-buf folder-win)
-    (wl-summary-cleanup-temp-marks sticky)
-    (unwind-protect
-       ;; save summary status
-       (progn
-         (wl-summary-save-status sticky)
-         ;(wl-summary-msgdb-save)
-         (if wl-use-scoring
-             (wl-score-save)))
-      ;; for sticky summary
-      (wl-delete-all-overlays)
-      (setq wl-summary-buffer-disp-msg nil)
-      ;; delete message window if displayed.
-      (if (setq message-buf (get-buffer wl-message-buf-name))
-         (if (setq message-win (get-buffer-window message-buf))
-             (delete-window message-win)))
-      (if (setq folder-buf (get-buffer wl-folder-buffer-name))
-         (if (setq folder-win (get-buffer-window folder-buf))
-             ;; folder win is already displayed.
-             (select-window folder-win)
-           ;; folder win is not displayed.
-           (switch-to-buffer folder-buf))
-       ;; currently no folder buffer
-       (wl-folder))
-      (and wl-folder-move-cur-folder
-          wl-folder-buffer-cur-point
-          (goto-char wl-folder-buffer-cur-point))
-      (setq wl-folder-buffer-cur-path nil)
-      (setq wl-folder-buffer-cur-entity-id nil)
-      (wl-delete-all-overlays)
-      (if wl-summary-exit-next-move
-         (wl-folder-next-unsync t)
-       (beginning-of-line))
-      (if (setq summary-win (get-buffer-window summary-buf))
-         (delete-window summary-win))
-      (if (or force-exit 
-             (not sticky))
+    (if wl-summary-buffer-exit-function
+       (funcall wl-summary-buffer-exit-function)
+      (wl-summary-cleanup-temp-marks sticky)
+      (unwind-protect
+         ;; save summary status
          (progn
-           (set-buffer summary-buf)
-           (and (get-buffer wl-message-buf-name)
-                (kill-buffer wl-message-buf-name))
-           ;; kill buffers of mime-view-caesar
-           (wl-kill-buffers
-            (format "^%s-([0-9 ]+)$" (regexp-quote wl-message-buf-name)))
-           (kill-buffer summary-buf)))
-      (run-hooks 'wl-summary-exit-hook))))
-
-(defun wl-summary-sync-force-update (&optional unset-cursor)
+           (if (or force-exit
+                   (not sticky))
+               (elmo-folder-close wl-summary-buffer-elmo-folder)
+             (elmo-folder-commit wl-summary-buffer-elmo-folder)
+             (elmo-folder-check wl-summary-buffer-elmo-folder))
+           (wl-summary-save-view sticky)
+           (if wl-use-scoring (wl-score-save)))
+       ;; for sticky summary
+       (wl-delete-all-overlays)
+       (setq wl-summary-buffer-disp-msg nil)
+       (elmo-kill-buffer wl-summary-search-buf-name)
+       ;; delete message window if displayed.
+       (if (and wl-message-buffer (get-buffer-window wl-message-buffer))
+           (delete-window (get-buffer-window wl-message-buffer)))
+       (if (setq folder-buf (get-buffer wl-folder-buffer-name))
+           (if (setq folder-win (get-buffer-window folder-buf))
+               ;; folder win is already displayed.
+               (select-window folder-win)
+             ;; folder win is not displayed.
+             (switch-to-buffer folder-buf))
+         ;; currently no folder buffer
+         (wl-folder))
+       (and wl-folder-move-cur-folder
+            wl-folder-buffer-cur-point
+            (goto-char wl-folder-buffer-cur-point))
+       (setq wl-folder-buffer-cur-path nil)
+       (setq wl-folder-buffer-cur-entity-id nil)
+       (wl-delete-all-overlays)
+       (if wl-summary-exit-next-move
+           (wl-folder-next-unsync t)
+         (beginning-of-line))
+       (if (setq summary-win (get-buffer-window summary-buf))
+           (delete-window summary-win))
+       (if (or force-exit
+               (not sticky))
+           (progn
+             (set-buffer summary-buf)
+             (kill-buffer summary-buf)))
+       (run-hooks 'wl-summary-exit-hook)))))
+
+(defun wl-summary-sync-force-update (&optional unset-cursor no-check)
   (interactive)
-  (let ((msgdb-dir (elmo-msgdb-expand-path wl-summary-buffer-folder-name))
-       ret-val seen-list)
-    (unwind-protect
-       (progn
-         (setq seen-list (elmo-msgdb-seen-load msgdb-dir))
-         (setq ret-val (wl-summary-sync-update3 seen-list unset-cursor))
-         (elmo-msgdb-seen-save msgdb-dir nil))
-      (set-buffer (current-buffer)))
-    (if (interactive-p)
-       (message "%s" ret-val))
-    ret-val))
+  (wl-summary-sync-update unset-cursor nil no-check))
+
+(defsubst wl-summary-sync-all-init ()
+  (wl-summary-cleanup-temp-marks)
+  (erase-buffer)
+  (wl-summary-set-message-modified)
+  (wl-summary-set-mark-modified)
+  (setq wl-thread-entity-hashtb (elmo-make-hash
+                                (* (length (elmo-msgdb-get-number-alist
+                                            (wl-summary-buffer-msgdb))) 2)))
+  (setq wl-thread-entity-list nil)
+  (setq wl-thread-entities nil)
+  (setq wl-summary-buffer-number-list nil)
+  (setq wl-summary-buffer-target-mark-list nil)
+  (setq wl-summary-buffer-refile-list nil)
+  (setq wl-summary-buffer-copy-list nil)
+  (setq wl-summary-buffer-delete-list nil)
+  (wl-summary-buffer-number-column-detect nil))
 
 (defun wl-summary-sync (&optional unset-cursor force-range)
   (interactive)
-  (let* ((folder wl-summary-buffer-folder-name)
+  (let* ((folder wl-summary-buffer-elmo-folder)
         (inhibit-read-only t)
         (buffer-read-only nil)
-        (msgdb-dir (elmo-msgdb-expand-path
-                    folder))
-        (range (or force-range (wl-summary-input-range folder)))
-        mes seen-list)
-    (cond ((string= range "all")
-          ;; initialize buffer local databases.
-          (unless (elmo-folder-plugged-p folder) ; forbidden
-            (error "Unplugged"))
-          (wl-summary-cleanup-temp-marks)
-          (setq seen-list
-                (nconc
-                 (elmo-msgdb-mark-alist-to-seen-list
-                  (elmo-msgdb-get-number-alist
-                   wl-summary-buffer-msgdb)
-                  (elmo-msgdb-get-mark-alist 
-                   wl-summary-buffer-msgdb)
-                  (concat wl-summary-important-mark
-                          wl-summary-read-uncached-mark))
-                 (elmo-msgdb-seen-load msgdb-dir)))
-          (setq wl-thread-entity-hashtb (elmo-make-hash
-                                         (* (length (elmo-msgdb-get-number-alist
-                                                     wl-summary-buffer-msgdb)) 2)))
-          (setq wl-summary-buffer-msgdb '(nil nil nil nil))
-          (setq wl-thread-entity-list nil)
-          (setq wl-thread-entities nil)
-          (setq wl-summary-buffer-target-mark-list nil)
-          (setq wl-summary-buffer-refile-list nil)
-          (setq wl-summary-buffer-copy-list nil)
-          (setq wl-summary-buffer-delete-list nil)
-          (wl-summary-buffer-number-column-detect nil)
-          (setq mes (wl-summary-sync-update3 seen-list unset-cursor))
-          (elmo-msgdb-seen-save msgdb-dir nil) ; delete all seen.
-          (if mes (message "%s" mes)))
-;         (wl-summary-sync-all folder t))
-         ((string= range "rescan")
+        (msgdb-dir (elmo-folder-msgdb-path folder))
+        (range (or force-range (wl-summary-input-range
+                                (elmo-folder-name-internal folder)))))
+    (cond ((string= range "rescan")
           (let ((msg (wl-summary-message-number)))
             (wl-summary-rescan)
             (and msg (wl-summary-jump-to-msg msg))))
@@ -1181,16 +1096,16 @@ q       Goto folder mode.
             (and msg (wl-summary-jump-to-msg msg))))
          ((or (string-match "last:" range)
               (string-match "first:" range))
-          (wl-summary-goto-folder-subr (concat "/" range "/" folder)
-                                       'force-update nil nil t))
-         ((string= range "no-sync")
-          ;; do nothing.
-          )
-         (t 
-          (setq seen-list (elmo-msgdb-seen-load msgdb-dir))
-          (setq mes (wl-summary-sync-update3 seen-list unset-cursor))
-          (elmo-msgdb-seen-save msgdb-dir nil) ; delete all seen.
-          (if mes (message "%s" mes))))))
+          (wl-summary-goto-folder-subr
+           (wl-folder-get-elmo-folder (concat "/" range "/"
+                                              (elmo-folder-name-internal
+                                               folder)))
+           'force-update nil nil t))
+         (t
+          (wl-summary-sync-update unset-cursor
+                                  (cond ((string= range "all") 'all)
+                                        ((string= range "all-visible")
+                                         'visible-only)))))))
 
 (defvar wl-summary-edit-addresses-candidate-fields
   ;; First element becomes default.
@@ -1200,7 +1115,7 @@ q Goto folder mode.
   (let ((fields wl-summary-edit-addresses-candidate-fields)
        body candidates components)
     (while fields
-      (setq body 
+      (setq body
            (mapconcat 'identity (elmo-multiple-field-body (car fields))
                       ","))
       (setq body (wl-parse-addresses body))
@@ -1240,12 +1155,16 @@ q       Goto folder mode.
              (eq char ?\r)
              (eq char ? ))
          ;; Change Addresses
-         (wl-address-petname-add-or-change 
+         (wl-address-petname-add-or-change
           the-email
           (elmo-get-hash-val the-email wl-address-petname-hash)
           (wl-address-header-extract-realname
-           (cdr (assoc (downcase the-email)
-                       wl-address-completion-list))) t)
+           (cdr (assoc
+                 (let ((completion-ignore-case t) comp)
+                   (setq comp
+                         (try-completion the-email wl-address-completion-list))
+                   (if (equal comp t) the-email comp))
+                 wl-address-completion-list))) t)
          "edited")
         ((eq char ?d)
          ;; Delete Addresses
@@ -1259,7 +1178,7 @@ q Goto folder mode.
         (t (message "")
            nil)))
     ;; Add Petname
-    (wl-address-petname-add-or-change 
+    (wl-address-petname-add-or-change
      the-email name-in-addr name-in-addr)
     "added"))
 
@@ -1268,103 +1187,104 @@ q     Goto folder mode.
 Optional argument ADDR-STR is used as a target address if specified."
   (interactive (if current-prefix-arg
                   (list (read-from-minibuffer "Target address: "))))
-  (save-excursion
-    (wl-summary-set-message-buffer-or-redisplay))
-  (let* ((charset wl-summary-buffer-mime-charset)
-        (candidates
-         (with-current-buffer (wl-message-get-original-buffer)
-           (wl-summary-edit-addresses-collect-candidate-fields
-            charset)))
-        address pair result)
-    (if addr-str
-       (setq address addr-str)
-      (when candidates
-       (setq address (car (car candidates)))
-       (setq address
-             (completing-read 
-              (format "Target address (%s): " address)
-              (mapcar
-               (function (lambda (x) (cons (car x) (car x))))
-               candidates)
-              nil nil nil nil address))))
-    (when address
-      (setq pair (assoc address candidates))
-      (unless pair
-       (setq pair (cons address nil)))
-      (when (setq result (wl-summary-edit-addresses-subr (car pair) (cdr pair)))
-       ;; update alias
-       (wl-status-update)
-       (setq address (assoc (car pair) wl-address-list))
-       (if address
-           (message "%s, %s, <%s> is %s."
-                    (nth 2 address)
-                    (nth 1 address)
-                    (nth 0 address)
-                    result)))
-      ;; i'd like to update summary-buffer, but...
-      ;;(wl-summary-rescan)
-      (run-hooks 'wl-summary-edit-addresses-hook))))
+  (if (null (wl-summary-message-number))
+      (message "No message.")
+    (save-excursion
+      (let* ((charset wl-summary-buffer-mime-charset)
+            (candidates
+             (with-current-buffer (wl-summary-get-original-buffer)
+               (wl-summary-edit-addresses-collect-candidate-fields
+                charset)))
+            address pair result)
+       (if addr-str
+           (setq address addr-str)
+         (when candidates
+           (setq address (car (car candidates)))
+           (setq address
+                 (completing-read
+                  (format "Target address (%s): " address)
+                  (mapcar
+                   (function (lambda (x) (cons (car x) (car x))))
+                   candidates)
+                  nil nil nil nil address))))
+       (when address
+         (setq pair (assoc address candidates))
+         (unless pair
+           (setq pair (cons address nil)))
+         (when (setq result (wl-summary-edit-addresses-subr (car pair) (cdr pair)))
+           ;; update alias
+           (wl-status-update)
+           (setq address (assoc (car pair) wl-address-list))
+           (if address
+               (message "%s, %s, <%s> is %s."
+                        (nth 2 address)
+                        (nth 1 address)
+                        (nth 0 address)
+                        result)))
+;;; i'd like to update summary-buffer, but...
+;;;    (wl-summary-rescan)
+         (run-hooks 'wl-summary-edit-addresses-hook))))))
 
 (defun wl-summary-incorporate (&optional arg)
   "Check and prefetch all uncached messages.
-If optional argument is non-nil, checking is omitted."
+If ARG is non-nil, checking is omitted."
   (interactive "P")
   (unless arg
-    (wl-summary-sync-force-update))
+    (save-excursion
+      (wl-summary-sync-force-update)))
   (wl-summary-prefetch-region (point-min) (point-max)
                              wl-summary-incorporate-marks))
 
-(defun wl-summary-prefetch-msg (number)
+(defun wl-summary-prefetch-msg (number &optional arg)
   "Returns status-mark. if skipped, returns nil."
   ;; prefetching procedure.
   (save-excursion
-    (let* ((msgdb wl-summary-buffer-msgdb)
-          (mark-alist (elmo-msgdb-get-mark-alist msgdb))         
+    (let* ((msgdb (wl-summary-buffer-msgdb))
+          (mark-alist (elmo-msgdb-get-mark-alist msgdb))
           (number-alist (elmo-msgdb-get-number-alist msgdb))
           (message-id (cdr (assq number number-alist)))
-          (ov (assoc message-id 
-                     (elmo-msgdb-get-overview msgdb)))
+          (ov (elmo-msgdb-overview-get-entity message-id msgdb))
           (entity ov)
           (size (elmo-msgdb-overview-entity-get-size ov))
           (inhibit-read-only t)
-          (buffer-read-only nil)          
+          (buffer-read-only nil)
           (force-read (and size
                            (or (null wl-prefetch-threshold)
                                (< size wl-prefetch-threshold))))
           mark new-mark)
-      (unwind-protect
-         (progn
-           (when (and size (not force-read) wl-prefetch-confirm)
-             (setq force-read
-                   (save-restriction
-                     (widen)
-                     (y-or-n-p
-                      (format
-                       "Message from %s has %d bytes. Prefetch it?" 
-                       (concat 
-                        "[ "
-                        (save-match-data 
-                          (wl-set-string-width 
-                           wl-from-width
-                           (wl-summary-from-func-internal
-                            (eword-decode-string
-                             (elmo-delete-char 
-                              ?\"
-                              (or 
-                               (elmo-msgdb-overview-entity-get-from ov)
-                               "??")))))) " ]")
-                       size))))
-             (message "")); flush.
-           (setq mark (cadr (assq number mark-alist)))
-            (if force-read
-             (save-excursion
-               (save-match-data
-                 (if (and (null (elmo-folder-plugged-p
-                                 wl-summary-buffer-folder-name))
-                          elmo-enable-disconnected-operation)
-                     (progn ;; append-queue for offline
-                       (elmo-dop-prefetch-msgs
-                        wl-summary-buffer-folder-name (list number))
+      (if (or arg
+             (null (elmo-file-cache-exists-p message-id)))
+         (unwind-protect
+             (progn
+               (when (and size (not force-read) wl-prefetch-confirm)
+                 (setq force-read
+                       (save-restriction
+                         (widen)
+                         (y-or-n-p
+                          (format
+                           "Message from %s has %d bytes.  Prefetch it? "
+                           (concat
+                            "[ "
+                            (save-match-data
+                              (wl-set-string-width
+                               wl-from-width
+                               (wl-summary-from-func-internal
+                                (eword-decode-string
+                                 (elmo-delete-char
+                                  ?\"
+                                  (or
+                                   (elmo-msgdb-overview-entity-get-from ov)
+                                   "??")))))) " ]")
+                           size))))
+                 (message ""))         ; flush.
+               (setq mark (cadr (assq number mark-alist)))
+               (if force-read
+                   (save-excursion
+                     (save-match-data
+                       ;; online
+                       (elmo-message-encache
+                        wl-summary-buffer-elmo-folder
+                        number)
                        (setq new-mark
                              (cond
                               ((string= mark
@@ -1376,47 +1296,25 @@ If optional argument is non-nil, checking is omitted."
                                (setq wl-summary-buffer-unread-count
                                      (+ wl-summary-buffer-unread-count 1))
                                wl-summary-unread-cached-mark)
-                              ((or (null mark)
-                                   (string= mark wl-summary-read-uncached-mark))
-                               (setq wl-summary-buffer-unread-count
-                                     (+ wl-summary-buffer-unread-count 1))
-                               wl-summary-unread-cached-mark)
-                              (t mark))))
-                   ;; online
-                   (elmo-prefetch-msg wl-summary-buffer-folder-name
-                                      number
-                                      (wl-message-get-original-buffer)
-                                      msgdb)
-                   (setq new-mark
-                         (cond
-                          ((string= mark 
-                                    wl-summary-unread-uncached-mark)
-                           wl-summary-unread-cached-mark)
-                          ((string= mark wl-summary-new-mark)
-                           (setq wl-summary-buffer-new-count 
-                                 (- wl-summary-buffer-new-count 1))
-                           (setq wl-summary-buffer-unread-count
-                                 (+ wl-summary-buffer-unread-count 1))
-                           wl-summary-unread-cached-mark)
-                          ((string= mark wl-summary-read-uncached-mark)
-                           nil)
-                          (t mark))))
-                 (setq mark-alist (elmo-msgdb-mark-set
-                                   mark-alist number new-mark))
-                 (or new-mark (setq new-mark " "))
-                 (elmo-msgdb-set-mark-alist msgdb mark-alist)
-                 (wl-summary-set-mark-modified)
-                 (wl-summary-update-modeline)
-                 (wl-folder-update-unread
-                  wl-summary-buffer-folder-name
-                  (+ wl-summary-buffer-unread-count
-                     wl-summary-buffer-new-count)))
-               new-mark)))))))
-
-;(defvar wl-summary-message-uncached-marks
-;  (list wl-summary-new-mark 
-;      wl-summary-unread-uncached-mark
-;      wl-summary-read-uncached-mark))
+                              ((string= mark wl-summary-read-uncached-mark)
+                               nil)
+                              (t mark)))
+                       (setq mark-alist (elmo-msgdb-mark-set
+                                         mark-alist number new-mark))
+                       (or new-mark (setq new-mark " "))
+                       (elmo-msgdb-set-mark-alist msgdb mark-alist)
+                       (wl-summary-set-mark-modified)
+                       (wl-summary-update-modeline)
+                       (wl-folder-update-unread
+                        (wl-summary-buffer-folder-name)
+                        (+ wl-summary-buffer-unread-count
+                           wl-summary-buffer-new-count)))
+                     new-mark))))))))
+
+;;(defvar wl-summary-message-uncached-marks
+;;  (list wl-summary-new-mark
+;;     wl-summary-unread-uncached-mark
+;;     wl-summary-read-uncached-mark))
 
 (defun wl-summary-prefetch-region (beg end &optional prefetch-marks)
   (interactive "r")
@@ -1424,7 +1322,7 @@ If optional argument is non-nil, checking is omitted."
        targets
        mark length
        entity msg
-       start-pos)
+       start-pos pos)
     (save-excursion
       (setq start-pos (point))
       (save-restriction
@@ -1434,15 +1332,15 @@ If optional argument is non-nil, checking is omitted."
        (goto-char (point-min))
        (while (not (eobp))
          (beginning-of-line)
-         (when (looking-at "^ *\\([0-9]+\\)[^0-9]\\([^0-9]\\)")
+         (when (looking-at "^ *\\([0-9-]+\\)[^0-9]\\([^0-9]\\)")
            (setq mark (wl-match-buffer 2))
            (setq msg (string-to-int (wl-match-buffer 1)))
            (if (or (and (null prefetch-marks)
                         msg
-                        (null (elmo-cache-exists-p
+                        (null (elmo-file-cache-exists-p
                                (cdr (assq msg
                                           (elmo-msgdb-get-number-alist
-                                           wl-summary-buffer-msgdb))))))
+                                           (wl-summary-buffer-msgdb)))))))
                    (member mark prefetch-marks))
                (setq targets (nconc targets (list msg))))
            (setq entity (wl-thread-get-entity msg))
@@ -1472,30 +1370,33 @@ If optional argument is non-nil, checking is omitted."
                       (setq count (+ 1 count)) length))
          ;; redisplay!
          (save-excursion
-           (save-restriction
-             (widen)
-             (goto-char start-pos)
-             (sit-for 0)))
+           (setq pos (point))
+           (goto-char start-pos)
+           (if (pos-visible-in-window-p pos)
+               (save-restriction
+                 (widen)
+                 (sit-for 0))))
          (setq targets (cdr targets)))
        (message "Prefetched %d/%d message(s)" count length)
        (cons count length)))))
 
-(defun wl-summary-prefetch ()
+(defun wl-summary-prefetch (&optional arg)
   "Prefetch current message."
-  (interactive)
+  (interactive "P")
   (save-excursion
     (save-match-data
       (beginning-of-line)
-      (when (looking-at "^ *\\([0-9]+\\)[^0-9]\\([^0-9]\\)")
+      (when (looking-at "^ *\\([0-9-]+\\)[^0-9]\\([^0-9]\\)")
        (goto-char (match-beginning 2))
        (let ((inhibit-read-only t)
              (buffer-read-only nil)
+             (beg (match-beginning 2))
+             (end (match-end 2))
              mark)
          (setq mark (wl-summary-prefetch-msg
-                     (string-to-int (wl-match-buffer 1))))
+                     (string-to-int (wl-match-buffer 1)) arg))
          (when mark
-           (delete-region (match-beginning 2)
-                          (match-end 2))
+           (delete-region beg end)
            (insert mark)
            (if wl-summary-highlight
                (wl-highlight-summary-current-line)))
@@ -1509,32 +1410,33 @@ If optional argument is non-nil, checking is omitted."
     (let ((inhibit-read-only t)
          (buffer-read-only nil)
          (case-fold-search nil))
-      (while (re-search-forward 
+      (while (re-search-forward
              (concat "^" wl-summary-buffer-number-regexp ".\\(.\\)") nil t)
        (delete-region (match-beginning 1) (match-end 1))
        (insert " ")))))
 
-(defun wl-summary-delete-copy-marks-on-buffer (cpys)
-  (mapcar (function
-          (lambda (x)
-            (wl-summary-unmark x)))
-         cpys))
+(defun wl-summary-delete-marks-on-buffer (marks)
+  (while marks
+    (wl-summary-unmark (pop marks))))
+
+(defun wl-summary-delete-copy-marks-on-buffer (copies)
+  (wl-summary-delete-marks-on-buffer copies))
 
 (defun wl-summary-delete-all-refile-marks ()
-  (mapcar (function
-          (lambda (x)
-            (wl-summary-unmark (car x)))) wl-summary-buffer-refile-list))
+  (let ((marks wl-summary-buffer-refile-list))
+    (while marks
+      (wl-summary-unmark (car (pop marks))))))
 
 (defun wl-summary-delete-all-copy-marks ()
-  (mapcar (function
-          (lambda (x)
-            (wl-summary-unmark (car x)))) wl-summary-buffer-copy-list))
+  (let ((marks wl-summary-buffer-copy-list))
+    (while marks
+      (wl-summary-unmark (car (pop marks))))))
+
 (defun wl-summary-delete-all-delete-marks ()
-  (mapcar 'wl-summary-unmark wl-summary-buffer-delete-list))
+  (wl-summary-delete-marks-on-buffer wl-summary-buffer-delete-list))
 
 (defun wl-summary-delete-all-target-marks ()
-  (mapcar 'wl-summary-unmark wl-summary-buffer-target-mark-list))
+  (wl-summary-delete-marks-on-buffer wl-summary-buffer-target-mark-list))
 
 (defun wl-summary-delete-all-temp-marks-on-buffer (&optional sticky)
   ;; for summary view cache saving.
@@ -1552,7 +1454,7 @@ If optional argument is non-nil, checking is omitted."
            (wl-highlight-summary-current-line))))))
 
 (defun wl-summary-delete-all-marks (mark-alist mark)
-  "Delete all MARKs in MARK-ALIST"
+  "Delete all MARKs in MARK-ALIST."
   (let ((malist mark-alist)
        (ret-val mark-alist)
        entity)
@@ -1569,8 +1471,10 @@ If optional argument is non-nil, checking is omitted."
   (interactive "r")
   (save-excursion
     (save-restriction
-      (narrow-to-region beg end);(save-excursion (goto-char end)
-                                       ;    (end-of-line) (point)))
+      (narrow-to-region beg end)
+;;; use narrowing.
+;;;   (save-excursion (goto-char end)
+;;;                  (end-of-line) (point)))
       (goto-char (point-min))
       (if (eq wl-summary-buffer-view 'thread)
          (progn
@@ -1585,22 +1489,24 @@ If optional argument is non-nil, checking is omitted."
                  ;; closed
                  (wl-summary-mark-as-read t) ; mark itself.
                  (setq children (wl-thread-get-children-msgs number))
-                 (while children 
-                   (wl-thread-msg-mark-as-read (car children))
+                 (while children
+                   (wl-summary-mark-as-read t nil nil (car children))
                    (setq children (cdr children))))
                (forward-line 1))))
        (while (not (eobp))
          (wl-summary-mark-as-read t)
          (forward-line 1)))))
-  (wl-summary-count-unread (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
+  (wl-summary-count-unread (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
   (wl-summary-update-modeline))
 
 (defun wl-summary-mark-as-unread-region (beg end)
   (interactive "r")
   (save-excursion
     (save-restriction
-      (narrow-to-region beg end);(save-excursion (goto-char end)
-                                       ;    (end-of-line) (point)))
+      (narrow-to-region beg end)
+;;; use narrowing.
+;;;      (save-excursion (goto-char end)
+;;;                  (end-of-line) (point)))
       (goto-char (point-min))
       (if (eq wl-summary-buffer-view 'thread)
          (progn
@@ -1614,16 +1520,16 @@ If optional argument is non-nil, checking is omitted."
                    (wl-summary-mark-as-unread)
                  ;; closed
                  (wl-summary-mark-as-unread) ; mark itself.
-                 (setq children 
+                 (setq children
                        (delq number (wl-thread-get-children-msgs number)))
-                 (while children 
-                   (wl-thread-msg-mark-as-unread (car children))
+                 (while children
+                   (wl-summary-mark-as-unread (car children))
                    (setq children (cdr children))))
                (forward-line 1))))
        (while (not (eobp))
          (wl-summary-mark-as-unread)
          (forward-line 1)))))
-  (wl-summary-count-unread (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
+  (wl-summary-count-unread (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
   (wl-summary-update-modeline))
 
 (defun wl-summary-mark-as-important-region (beg end)
@@ -1645,26 +1551,26 @@ If optional argument is non-nil, checking is omitted."
                    (wl-summary-mark-as-important)
                  ;; closed
                  (wl-summary-mark-as-important) ; mark itself.
-                 (setq children 
+                 (setq children
                        (delq number (wl-thread-get-children-msgs number)))
-                 (while children 
+                 (while children
                    (wl-thread-msg-mark-as-important (car children))
                    (setq children (cdr children))))
                (forward-line 1))))
        (while (not (eobp))
          (wl-summary-mark-as-important)
          (forward-line 1)))))
-  (wl-summary-count-unread (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
+  (wl-summary-count-unread (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
   (wl-summary-update-modeline))
 
 (defun wl-summary-mark-as-read-all ()
   (interactive)
   (if (or (not (interactive-p))
-         (y-or-n-p "Mark all messages as read?"))
-      (let* ((folder wl-summary-buffer-folder-name)
+         (y-or-n-p "Mark all messages as read? "))
+      (let* ((folder wl-summary-buffer-elmo-folder)
             (cur-buf (current-buffer))
-            (msgdb wl-summary-buffer-msgdb)
-            ;;(number-alist (elmo-msgdb-get-number-alist msgdb))
+            (msgdb (wl-summary-buffer-msgdb))
+;;;         (number-alist (elmo-msgdb-get-number-alist msgdb))
             (mark-alist (elmo-msgdb-get-mark-alist msgdb))
             (malist mark-alist)
             (inhibit-read-only t)
@@ -1672,31 +1578,32 @@ If optional argument is non-nil, checking is omitted."
             (case-fold-search nil)
             msg mark)
        (message "Setting all msgs as read...")
-       (elmo-mark-as-read folder (wl-summary-collect-unread mark-alist)
-                          msgdb)
+       (elmo-folder-mark-as-read folder (wl-summary-collect-unread mark-alist))
        (save-excursion
          (goto-char (point-min))
-         (while (re-search-forward "^ *\\([0-9]+\\)[^0-9]\\([^0-9 ]\\)" nil t)
+         (while (re-search-forward "^ *\\([0-9-]+\\)[^0-9]\\([^0-9 ]\\)" nil t)
            (setq msg (string-to-int (wl-match-buffer 1)))
            (setq mark (wl-match-buffer 2))
            (when (and (not (string= mark wl-summary-important-mark))
                       (not (string= mark wl-summary-read-uncached-mark)))
              (delete-region (match-beginning 2) (match-end 2))
-             (if (or (not (elmo-use-cache-p folder msg))
+             (if (or (not (elmo-message-use-cache-p folder msg))
                      (string= mark wl-summary-unread-cached-mark))
                  (progn
                    (insert " ")
                    (setq mark-alist
-                         (elmo-msgdb-mark-set 
+                         (elmo-msgdb-mark-set
                           mark-alist
-                          msg ;(cdr (assq msg number-alist)) 
+                          msg
+;;; Use msg instead of (cdr (assq msg number-alist)).
+;;;                       (cdr (assq msg number-alist))
                           nil)))
                ;; New mark and unread-uncached mark
                (insert wl-summary-read-uncached-mark)
                (setq mark-alist
                      (elmo-msgdb-mark-set mark-alist
                                           msg
-                                       ; (cdr (assq msg number-alist)) 
+;;;                                       (cdr (assq msg number-alist))
                                           wl-summary-read-uncached-mark)))
              (if wl-summary-highlight
                  (wl-highlight-summary-current-line nil nil t)))))
@@ -1704,11 +1611,11 @@ If optional argument is non-nil, checking is omitted."
        (wl-summary-set-mark-modified)
        (set-buffer cur-buf); why is this needed???
        (elmo-msgdb-set-mark-alist msgdb mark-alist)
-       (wl-folder-update-unread wl-summary-buffer-folder-name 0)
+       (wl-folder-update-unread (wl-summary-buffer-folder-name) 0)
        (setq wl-summary-buffer-unread-count 0)
-       (setq wl-summary-buffer-new-count    0) 
+       (setq wl-summary-buffer-new-count    0)
        (wl-summary-update-modeline)
-       (message "Setting all msgs as read...done.")
+       (message "Setting all msgs as read...done")
        (set-buffer-modified-p nil))))
 
 (defun wl-summary-delete-cache ()
@@ -1717,18 +1624,18 @@ If optional argument is non-nil, checking is omitted."
   (save-excursion
     (let* ((inhibit-read-only t)
           (buffer-read-only nil)
-          (folder wl-summary-buffer-folder-name)
-          (msgdb wl-summary-buffer-msgdb)
+          (folder wl-summary-buffer-elmo-folder)
+          (msgdb (wl-summary-buffer-msgdb))
           (mark-alist (elmo-msgdb-get-mark-alist msgdb))
           (number-alist (elmo-msgdb-get-number-alist msgdb))
           (case-fold-search nil)
           mark number unread new-mark)
-;      (re-search-backward "^ *[0-9]+..[0-9]+/[0-9]+" nil t) ; set cursor line
+;;;   (re-search-backward "^ *[0-9]+..[0-9]+/[0-9]+" nil t) ; set cursor line
       (beginning-of-line)
-      (when (looking-at "^ *\\([0-9]+\\)[^0-9]\\([^0-9]\\)")
+      (when (looking-at "^ *\\([0-9-]+\\)[^0-9]\\([^0-9]\\)")
        (progn
          (setq mark (wl-match-buffer 2))
-         (cond 
+         (cond
           ((or (string= mark wl-summary-new-mark)
                (string= mark wl-summary-unread-uncached-mark)
                (string= mark wl-summary-important-mark))
@@ -1736,16 +1643,17 @@ If optional argument is non-nil, checking is omitted."
            )
           ((string= mark wl-summary-unread-cached-mark)
            (setq new-mark wl-summary-unread-uncached-mark))
-          (t 
+          (t
            (setq new-mark wl-summary-read-uncached-mark)))
          (when new-mark
            (setq number (string-to-int (wl-match-buffer 1)))
            (delete-region (match-beginning 2) (match-end 2))
            (goto-char (match-beginning 2))
            (insert new-mark)
-           (elmo-cache-delete (cdr (assq number number-alist))
-                              wl-summary-buffer-folder-name
-                              number)
+           (elmo-file-cache-delete 
+            (elmo-message-field wl-summary-buffer-elmo-folder
+                                    number
+                                    'message-id))
            (setq mark-alist
                  (elmo-msgdb-mark-set mark-alist number new-mark))
            (elmo-msgdb-set-mark-alist msgdb mark-alist)
@@ -1753,13 +1661,13 @@ If optional argument is non-nil, checking is omitted."
            (if wl-summary-highlight
                (wl-highlight-summary-current-line nil nil t))
            (set-buffer-modified-p nil)))))))
-  
+
 (defun wl-summary-resume-cache-status ()
   "Resume the cache status of all messages in the current folder."
   (interactive)
-  (let* ((folder wl-summary-buffer-folder-name)
+  (let* ((folder wl-summary-buffer-elmo-folder)
         (cur-buf (current-buffer))
-        (msgdb wl-summary-buffer-msgdb)
+        (msgdb (wl-summary-buffer-msgdb))
         (number-alist (elmo-msgdb-get-number-alist msgdb))
         (mark-alist (elmo-msgdb-get-mark-alist msgdb))
         (inhibit-read-only t)
@@ -1769,13 +1677,13 @@ If optional argument is non-nil, checking is omitted."
     (message "Resuming cache status...")
     (save-excursion
       (goto-char (point-min))
-      (while (re-search-forward "^ *\\([0-9]+\\)[^0-9]\\([^0-9]\\)" nil t)
+      (while (re-search-forward "^ *\\([0-9-]+\\)[^0-9]\\([^0-9]\\)" nil t)
        (setq msg (string-to-int
                   (wl-match-buffer 1)))
        (setq mark (wl-match-buffer 2))
        (setq msgid (cdr (assq msg number-alist)))
        (setq set-mark nil)
-       (if (elmo-cache-exists-p msgid folder msg)
+       (if (elmo-file-cache-exists-p msgid)
            (if (or
                 (string= mark wl-summary-unread-uncached-mark) ; U -> !
                 (string= mark wl-summary-new-mark)             ; N -> !
@@ -1783,17 +1691,17 @@ If optional argument is non-nil, checking is omitted."
                (setq set-mark wl-summary-unread-cached-mark)
              (if (string= mark wl-summary-read-uncached-mark)  ; u -> ' '
                  (setq set-mark " ")))
-         (if (string= mark " ")                            
+         (if (string= mark " ")
              (setq set-mark wl-summary-read-uncached-mark)     ;' ' -> u
-           (if (string= mark wl-summary-unread-cached-mark) 
+           (if (string= mark wl-summary-unread-cached-mark)
                (setq set-mark wl-summary-unread-uncached-mark) ; !  -> U
              )))
        (when set-mark
          (delete-region (match-beginning 2) (match-end 2))
          (insert set-mark)
          (setq mark-alist
-               (elmo-msgdb-mark-set 
-                mark-alist msg ; msgid 
+               (elmo-msgdb-mark-set
+                mark-alist msg ; msgid
                 (if (string= set-mark " ") nil set-mark)))
          (if wl-summary-highlight
              (wl-highlight-summary-current-line))))
@@ -1802,13 +1710,13 @@ If optional argument is non-nil, checking is omitted."
       (elmo-msgdb-set-mark-alist msgdb mark-alist)
       (wl-summary-count-unread mark-alist)
       (wl-summary-update-modeline)
-      (message "Resuming cache status...done.")
+      (message "Resuming cache status...done")
       (set-buffer-modified-p nil))))
 
 (defun wl-summary-resume-marks-and-highlight ()
-  (let* ((msgdb wl-summary-buffer-msgdb)
+  (let* ((msgdb (wl-summary-buffer-msgdb))
         (mark-alist (elmo-msgdb-get-mark-alist msgdb))
-        ;;(number-alist (elmo-msgdb-get-number-alist msgdb))
+;;;     (number-alist (elmo-msgdb-get-number-alist msgdb))
         (count (count-lines (point-min)(point-max)))
         (i 0)
         msg-num percent smark)
@@ -1824,16 +1732,17 @@ If optional argument is non-nil, checking is omitted."
              (delete-region (match-beginning 1) (match-end 1))
              (insert (or smark " "))))
        (wl-highlight-summary-current-line smark)
-       (setq i (+ i 1))
-       (setq percent (/ (* i 100) count))
-       (elmo-display-progress
-        'wl-summary-resume-marks-and-highlight "Resuming all marks..."
-        percent)
+       (when (> count elmo-display-progress-threshold)
+         (setq i (+ i 1))
+         (setq percent (/ (* i 100) count))
+         (elmo-display-progress
+          'wl-summary-resume-marks-and-highlight "Resuming all marks..."
+          percent))
        (forward-line 1)))
-    (message "Resuming all marks...done.")))
+    (message "Resuming all marks...done")))
 
 (defun wl-summary-resume-marks ()
-  (let* ((msgdb wl-summary-buffer-msgdb)
+  (let* ((msgdb (wl-summary-buffer-msgdb))
         (mark-alist (elmo-msgdb-get-mark-alist msgdb))
         (number-alist (elmo-msgdb-get-number-alist msgdb))
         (count (length mark-alist))
@@ -1844,7 +1753,8 @@ If optional argument is non-nil, checking is omitted."
       (while mark-alist
        (setq entity (car mark-alist))
        (if (setq msg-num (car (rassoc (car entity) number-alist)))
-           (progn ;(goto-char (point-min))
+           (progn 
+;;;          (goto-char (point-min))
              (if (re-search-forward (format "^ *%s \\( \\)" msg-num) nil t)
                  (progn
                    (delete-region (match-beginning 1) (match-end 1))
@@ -1856,13 +1766,14 @@ If optional argument is non-nil, checking is omitted."
                      (delete-region (match-beginning 1) (match-end 1))
                      (insert (or (cadr entity)
                                  " ")))))))
-       (setq i (+ i 1))
-       (setq percent (/ (* i 100) count))
-       (elmo-display-progress
-        'wl-summary-resume-marks "Resuming all marks..."
-        percent)
+       (when (> count elmo-display-progress-threshold)
+         (setq i (+ i 1))
+         (setq percent (/ (* i 100) count))
+         (elmo-display-progress
+          'wl-summary-resume-marks "Resuming all marks..."
+          percent))
        (setq mark-alist (cdr mark-alist)))
-      (message "Resuming all marks...done."))))
+      (message "Resuming all marks...done"))))
 
 (defun wl-summary-delete-messages-on-buffer (msgs &optional deleting-info)
   (interactive)
@@ -1873,42 +1784,46 @@ If optional argument is non-nil, checking is omitted."
          (len (length msgs))
          (i 0)
          update-list)
+      (elmo-kill-buffer wl-summary-search-buf-name)
       (while msgs
        (if (eq wl-summary-buffer-view 'thread)
            (progn
+             ;; don't use wl-append(nconc), because list is broken. ...why?
              (setq update-list
-                   (wl-append update-list
-                              (wl-thread-delete-message (car msgs))))
-             (setq update-list (and update-list
-                                    (delete (car msgs) update-list))))
+                   (append update-list
+                           (wl-thread-delete-message (car msgs))))
+             (setq update-list (delq (car msgs) update-list)))
          (goto-char (point-min))
-         (if (re-search-forward (format "^ *%d[^0-9]\\([^0-9]\\).*$" 
+         (if (re-search-forward (format "^ *%d[^0-9]\\([^0-9]\\).*$"
                                         (car msgs)) nil t)
              (progn
                (delete-region (match-beginning 0) (match-end 0))
                (delete-char 1) ; delete '\n'
-               )))
-       (when deleting-info
+               (setq wl-summary-buffer-number-list
+                     (delq (car msgs) wl-summary-buffer-number-list)))))
+       (when (and deleting-info
+                  (> len elmo-display-progress-threshold))
          (setq i (1+ i))
-         (and (zerop (% i 10))
-              (elmo-display-progress
-               'wl-summary-delete-messages-on-buffer "Deleting..."
-               (/ (* i 100) len))))
+         (if (or (zerop (% i 5)) (= i len))
+             (elmo-display-progress
+              'wl-summary-delete-messages-on-buffer deleting-info
+              (/ (* i 100) len))))
        (setq msgs (cdr msgs)))
-      (if (eq wl-summary-buffer-view 'thread)
-         (wl-thread-update-line-msgs (elmo-uniq-list update-list)))
-      (wl-thread-cleanup-symbols msgs2)
-      (wl-summary-count-unread 
-       (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
-      (wl-summary-update-modeline)         
+      (when (eq wl-summary-buffer-view 'thread)
+       (wl-thread-update-line-msgs (elmo-uniq-list update-list)
+                                   (unless deleting-info 'no-msg))
+       (wl-thread-cleanup-symbols msgs2))
+      (wl-summary-count-unread
+       (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
+      (wl-summary-update-modeline)
       (wl-folder-update-unread
-       wl-summary-buffer-folder-name
+       (wl-summary-buffer-folder-name)
        (+ wl-summary-buffer-unread-count wl-summary-buffer-new-count)))))
 
 (defun wl-summary-set-as-read-mark-alist (mark-alist)
-  (let ((marks (list (cons wl-summary-unread-cached-mark 
+  (let ((marks (list (cons wl-summary-unread-cached-mark
                           nil)
-                    (cons wl-summary-unread-uncached-mark 
+                    (cons wl-summary-unread-uncached-mark
                           wl-summary-read-uncached-mark)
                     (cons wl-summary-new-mark
                           wl-summary-read-uncached-mark)))
@@ -1917,17 +1832,17 @@ If optional argument is non-nil, checking is omitted."
     (while mark-alist
       (setq entity (car mark-alist))
       (when (setq pair (assoc (cadr entity) marks))
-       (if (elmo-use-cache-p wl-summary-buffer-folder-name
-                             (caar mark-alist))
+       (if (elmo-message-use-cache-p wl-summary-buffer-elmo-folder
+                                     (caar mark-alist))
            (if (cdr pair)
                (setcar (cdr entity) (cdr pair))
-               (setq ret-val (delete entity ret-val)))
+             (setq ret-val (delete entity ret-val)))
          (setq ret-val (delete entity ret-val))))
       (setq mark-alist (cdr mark-alist)))
     ret-val))
 
 (defun wl-summary-set-status-marks (mark-alist before after)
-  "Set the BEFORE marks to AFTER"
+  "Set the BEFORE marks to AFTER."
   (let ((ret-val mark-alist)
        entity)
     (while mark-alist
@@ -1940,14 +1855,14 @@ If optional argument is non-nil, checking is omitted."
     ret-val))
 
 (defun wl-summary-set-status-marks-on-buffer (before after)
-  "Set the MARKS marks on buffer"
+  "Set the MARKS marks on buffer."
   (interactive)
   (save-excursion
     (goto-char (point-min))
     (let ((inhibit-read-only t)
          (buffer-read-only nil)
          (regexp (concat "^" wl-summary-buffer-number-regexp ".\\(\\%s\\)")))
-      (while (re-search-forward 
+      (while (re-search-forward
              (format regexp (regexp-quote before)) nil t)
        (delete-region (match-beginning 1) (match-end 1))
        (insert after)
@@ -1966,11 +1881,11 @@ If optional argument is non-nil, checking is omitted."
            (t;; (equal type 'trash)
             wl-trash-folder)))))
 
-(defun wl-summary-delete-important-msgs-from-list (delete-list 
+(defun wl-summary-delete-important-msgs-from-list (delete-list
                                                   mark-alist)
   (let ((dlist delete-list))
     (while dlist
-      (if (string= wl-summary-important-mark 
+      (if (string= wl-summary-important-mark
                   (car (cdr (assq (car dlist) mark-alist))))
          (setq delete-list (delete (car dlist) delete-list)))
       (setq dlist (cdr dlist)))
@@ -1983,11 +1898,11 @@ If optional argument is non-nil, checking is omitted."
          (setq delete-list (delete (car dlist) delete-list)))
       (setq dlist (cdr dlist)))
     delete-list))
-  
+
 (defun wl-summary-get-append-message-func ()
   (if (eq wl-summary-buffer-view 'thread)
       'wl-summary-insert-thread-entity
-;      'wl-summary-insert-thread
+;;;   'wl-summary-insert-thread
     'wl-summary-insert-summary))
 
 (defun wl-summary-sort ()
@@ -1996,9 +1911,9 @@ If optional argument is non-nil, checking is omitted."
                       (default "date")
                       in)
                   (setq in
-                        (completing-read 
+                        (completing-read
                          (format "Sort by (%s): " default)
-                         (mapcar 
+                         (mapcar
                           (function (lambda (x) (cons x x)))
                           input-range-list)))
                   (if (string= in "")
@@ -2011,37 +1926,28 @@ If optional argument is non-nil, checking is omitted."
 (defun wl-summary-sync-marks ()
   "Update marks in summary."
   (interactive)
-  (let ((plugged (elmo-folder-plugged-p wl-summary-buffer-folder-name))
-       (last-progress 0)
-       mark-alist unread-marks msgs mark importants unreads 
-       importants-in-db unreads-in-db has-imap4 diff diffs
+  (let ((last-progress 0)
+       (i 0)
+       mark-alist unread-marks importants unreads
+       importants-in-db unreads-in-db diff diffs
        mes num-ma progress)
     ;; synchronize marks.
-    (when (not (eq (elmo-folder-get-type 
-                   wl-summary-buffer-folder-name)
+    (when (not (eq (elmo-folder-type-internal
+                   wl-summary-buffer-elmo-folder)
                   'internal))
       (message "Updating marks...")
       (setq unread-marks (list wl-summary-unread-cached-mark
                               wl-summary-unread-uncached-mark
                               wl-summary-new-mark)
-           mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb)
+           mark-alist (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb))
             num-ma (length mark-alist)
-           importants (elmo-list-folder-important 
-                       wl-summary-buffer-folder-name
-                       (elmo-msgdb-get-overview wl-summary-buffer-msgdb))
-           has-imap4 (elmo-folder-contains-type 
-                      wl-summary-buffer-folder-name 'imap4)
-           unreads (if (and has-imap4 plugged)
-                       (elmo-list-folder-unread 
-                        wl-summary-buffer-folder-name
-                        mark-alist unread-marks)))
+           importants (elmo-folder-list-importants
+                       wl-summary-buffer-elmo-folder
+                       wl-summary-important-mark)
+           unreads (elmo-folder-list-unreads
+                    wl-summary-buffer-elmo-folder
+                    unread-marks))
       (while mark-alist
-       (setq progress (/ (* (- num-ma (length mark-alist)) 100) num-ma))
-       (if (not (eq progress last-progress))
-           (elmo-display-progress 'wl-summary-sync-marks
-                                  "Updating marks..."
-                                  progress))
-       (setq last-progress progress)
        (if (string= (cadr (car mark-alist))
                     wl-summary-important-mark)
            (setq importants-in-db (cons (car (car mark-alist))
@@ -2049,7 +1955,15 @@ If optional argument is non-nil, checking is omitted."
          (if (member (cadr (car mark-alist)) unread-marks)
              (setq unreads-in-db (cons (car (car mark-alist))
                                        unreads-in-db))))
-       (setq mark-alist (cdr mark-alist)))
+       (setq mark-alist (cdr mark-alist))
+       (when (> num-ma elmo-display-progress-threshold)
+         (setq i (1+ i)
+               progress (/ (* i 100) num-ma))
+         (if (not (eq progress last-progress))
+             (elmo-display-progress 'wl-summary-sync-marks
+                                    "Updating marks..."
+                                    progress))
+         (setq last-progress progress)))
       (setq diff (elmo-list-diff importants importants-in-db))
       (setq diffs (cadr diff)) ; important-deletes
       (setq mes (format "Updated (-%d" (length diffs)))
@@ -2063,226 +1977,191 @@ If optional argument is non-nil, checking is omitted."
       (while diffs
        (wl-summary-mark-as-important (car diffs) " " 'no-server)
        (setq diffs (cdr diffs)))
-      (when (and has-imap4 plugged)
-       (setq diff (elmo-list-diff unreads unreads-in-db))
-       (setq diffs (cadr diff))
-       (setq mes (concat mes (format "(-%d" (length diffs))))
-       (while diffs
-         (wl-summary-mark-as-read t 'no-server nil (car diffs) 'no-cache)
-         (setq diffs (cdr diffs)))
-       (setq diffs (car diff)) ; unread-appends
-       (setq mes (concat mes (format "/+%d) unread mark(s)." (length diffs))))
-       (while diffs
-         (wl-summary-mark-as-unread (car diffs) 'no-server 'no-modeline)
-         (setq diffs (cdr diffs))))
+      (setq diff (elmo-list-diff unreads unreads-in-db))
+      (setq diffs (cadr diff))
+      (setq mes (concat mes (format "(-%d" (length diffs))))
+      (while diffs
+       (wl-summary-mark-as-read t 'no-server nil (car diffs))
+       (setq diffs (cdr diffs)))
+      (setq diffs (car diff)) ; unread-appends
+      (setq mes (concat mes (format "/+%d) unread mark(s)." (length diffs))))
+      (while diffs
+       (wl-summary-mark-as-unread (car diffs) 'no-server 'no-modeline)
+       (setq diffs (cdr diffs)))
       (if (interactive-p) (message mes)))))
 
 (defun wl-summary-confirm-appends (appends)
-  (condition-case nil
-      (let ((len (length appends))
-           in)
-       (if (> len wl-summary-update-confirm-threshold)
-           (if (y-or-n-p (format "Too many messages(%d). Continue?" len))
-               appends
-             (setq in wl-summary-update-confirm-threshold)
-             (catch 'end
-               (while t
-                 (setq in (read-from-minibuffer "Update number: " 
-                                                (int-to-string in))
-                       in (string-to-int in))
-                 (if (y-or-n-p (format "%d messages are disappeared. OK?" 
-                                       (- len in)))
-                     (throw 'end in))))
-             (nthcdr (max (- len in) 0) appends))
-         appends))
-    (quit nil)
-    (error nil))) ;
-
-(defun wl-summary-sync-update3 (&optional seen-list unset-cursor)
-  "Update the summary view."
+  (let ((len (length appends))
+       in)
+    (if (> len wl-summary-update-confirm-threshold)
+       (if (y-or-n-p (format "Too many messages(%d).  Continue? " len))
+           appends
+         (setq in wl-summary-update-confirm-threshold)
+         (catch 'end
+           (while t
+             (setq in (read-from-minibuffer "Update number: "
+                                            (int-to-string in))
+                   in (string-to-int in))
+             (if (< len in)
+                 (throw 'end len))
+             (if (y-or-n-p (format "%d messages are disappeared.  OK? "
+                                   (max (- len in) 0)))
+                 (throw 'end in))))
+         (nthcdr (max (- len in) 0) appends))
+      appends)))
+
+(defun wl-summary-sync-update (&optional unset-cursor sync-all no-check)
+  "Update the summary view to the newest folder status."
   (interactive)
-  (let* ((folder wl-summary-buffer-folder-name)
-        (cur-buf (current-buffer))
-        (msgdb wl-summary-buffer-msgdb)
-        (number-alist (elmo-msgdb-get-number-alist msgdb))
-        (mark-alist (elmo-msgdb-get-mark-alist msgdb))
-        (overview (elmo-msgdb-get-overview msgdb))
-        ;;(location (elmo-msgdb-get-location msgdb))
+  (let* ((folder wl-summary-buffer-elmo-folder)
         (case-fold-search nil)
         (elmo-mime-charset wl-summary-buffer-mime-charset)
         (inhibit-read-only t)
         (buffer-read-only nil)
-        diff append-list delete-list
-        i percent num result
         gc-message
-        in-folder
-        in-db curp
-        overview-append
-        entity ret-val crossed crossed2 sync-all
-        top-num update-top-list mark
-        expunged msgs unreads importants)
-    ;(setq seen-list nil) ;for debug.
-    (fset 'wl-summary-append-message-func-internal 
+        overview number-alist mark-alist 
+        curp num i new-msgdb
+        append-list delete-list crossed
+        update-thread update-top-list
+        expunged mes sync-result)
+    (unless wl-summary-buffer-elmo-folder
+      (error "(Internal error) Folder is not set:%s" (buffer-name
+                                                     (current-buffer))))
+    (fset 'wl-summary-append-message-func-internal
          (wl-summary-get-append-message-func))
     ;; Flush pending append operations (disconnected operation).
-    (setq seen-list
-         (wl-summary-flush-pending-append-operations seen-list))
+    ;;(setq seen-list
+    ;;(wl-summary-flush-pending-append-operations seen-list))
     (goto-char (point-max))
-    (message "Checking folder diff...")
-    (setq in-folder (elmo-list-folder folder))
-    (setq in-db (sort (mapcar 'car number-alist) '<))
-    (when (or (eq msgdb nil) ; trick for unplugged...
-             (equal msgdb '(nil nil nil nil)))
-      (setq sync-all t)
-      (wl-summary-set-message-modified)
-      (wl-summary-set-mark-modified)
-      (erase-buffer))
-    (setq diff (if (eq (elmo-folder-get-type folder) 'multi)
-                  (elmo-multi-list-bigger-diff in-folder in-db)
-                (elmo-list-bigger-diff in-folder in-db)))
-    (setq append-list (car diff))
-    (setq delete-list (cadr diff))
-    (message "Checking folder diff...done.")
-    ;; Don't delete important-marked msgs other than 'internal.
-    (unless (eq (elmo-folder-get-type folder) 'internal)
-      (setq delete-list
-           (wl-summary-delete-important-msgs-from-list delete-list 
-                                                       mark-alist)))
-    (if (and (elmo-folder-contains-type folder 'nntp)
-            (elmo-nntp-max-number-precedes-list-active-p))
-       ;; XXX this does not work correctly in rare case.
-       (setq delete-list
-             (wl-summary-delete-canceled-msgs-from-list delete-list
-                                                        msgdb)))    
-    (if (or (equal diff '(nil nil))
-           (equal diff '(nil))
-           (and (eq (length delete-list) 0)
-                (eq (length append-list) 0)))
+    (wl-folder-confirm-existence folder 'force)
+    (setq sync-result (elmo-folder-synchronize
+                      folder
+                      wl-summary-new-mark
+                      wl-summary-unread-uncached-mark
+                      wl-summary-unread-cached-mark
+                      wl-summary-read-uncached-mark
+                      wl-summary-important-mark
+                      sync-all no-check))
+    (setq new-msgdb (nth 0 sync-result))
+    (setq delete-list (nth 1 sync-result))
+    (setq crossed (nth 2 sync-result))
+    (if (or (and sync-all sync-result)
+           sync-result)
        (progn
-         ;; For max-number update...
-         (if (and (elmo-folder-contains-type folder 'nntp)
-                    (elmo-nntp-max-number-precedes-list-active-p)
-                    (elmo-update-number folder msgdb))
-             (wl-summary-set-message-modified)
-           (setq ret-val (format "No update is needed for \"%s\"" folder))))
-      (when delete-list
-       (message "Deleting...")
-       (elmo-msgdb-delete-msgs folder delete-list msgdb t) ; reserve cache.
-       ;;(set-buffer cur-buf)
-       (wl-summary-delete-messages-on-buffer delete-list t)
-       (message "Deleting...done."))
-      ;;(set-buffer cur-buf)
-      ;; Change "New" marks to "Uncached Unread" marks.
-      (wl-summary-set-status-marks mark-alist 
-                                  wl-summary-new-mark 
-                                  wl-summary-unread-uncached-mark)
-      (wl-summary-set-status-marks-on-buffer 
-       wl-summary-new-mark 
-       wl-summary-unread-uncached-mark)
-      ;; Confirm appended message number.
-      (setq append-list (wl-summary-confirm-appends append-list))
-      (setq num (length append-list))
-      (if append-list
-         (progn
+         ;; Setup sync-all
+         (if sync-all (wl-summary-sync-all-init))
+;    (if (and has-nntp
+;           (elmo-nntp-max-number-precedes-list-active-p))
+       ;; XXX this does not work correctly in rare case.
+;      (setq delete-list
+;            (wl-summary-delete-canceled-msgs-from-list
+;             delete-list
+;             (wl-summary-buffer-msgdb))))
+         (when delete-list
+           (wl-summary-delete-messages-on-buffer delete-list "Deleting...")
+           (message "Deleting...done"))
+         (wl-summary-set-status-marks-on-buffer
+          wl-summary-new-mark
+          wl-summary-unread-uncached-mark)
+         (setq append-list (elmo-msgdb-get-overview new-msgdb))
+         (setq curp append-list)
+         (setq num (length curp))
+         (when append-list
            (setq i 0)
-           (setq result (elmo-msgdb-create 
-                         folder 
-                         append-list
-                         wl-summary-new-mark
-                         wl-summary-unread-cached-mark ; !
-                         wl-summary-read-uncached-mark ; u ;; XXXX
-                         wl-summary-important-mark
-                         seen-list))
-           ;; delete duplicated messages.
-           (when (elmo-folder-contains-multi folder)
-             (setq crossed (elmo-multi-delete-crossposts
-                            msgdb result))
-             (setq result (cdr crossed))
-             (setq crossed (car crossed)))
-           (setq overview-append (car result))
-           (setq msgdb (elmo-msgdb-append msgdb result))
            ;; set these value for append-message-func
-           (setq overview (elmo-msgdb-get-overview msgdb))
-           (setq number-alist (elmo-msgdb-get-number-alist msgdb))
-           (setq mark-alist (elmo-msgdb-get-mark-alist msgdb))
-           ;; (setq location (elmo-msgdb-get-location msgdb))
-           (setq curp overview-append)
-           (setq num (length curp))
+           (setq overview (elmo-msgdb-get-overview
+                           (elmo-folder-msgdb folder)))
+           (setq number-alist (elmo-msgdb-get-number-alist
+                               (elmo-folder-msgdb folder)))
+           (setq mark-alist (elmo-msgdb-get-mark-alist
+                             (elmo-folder-msgdb folder)))
+           (setq wl-summary-delayed-update nil)
+           (elmo-kill-buffer wl-summary-search-buf-name)
            (while curp
              (setq entity (car curp))
-             (setq top-num
-                   (wl-summary-append-message-func-internal 
-                    entity overview mark-alist 
-                    (not sync-all)))
-             (when top-num
-               (wl-append update-top-list (list top-num)))
+             (when (setq update-thread
+                         (wl-summary-append-message-func-internal
+                          entity overview mark-alist
+                          (not sync-all)))
+               (wl-append update-top-list update-thread))
              (if elmo-use-database
-                 (elmo-database-msgid-put 
-                  (car entity) folder
+                 (elmo-database-msgid-put
+                  (car entity) (elmo-folder-name-internal folder)
                   (elmo-msgdb-overview-entity-get-number entity)))
              (setq curp (cdr curp))
-             (setq i (+ i 1))
-             (setq percent (/ (* i 100) num))
-             (elmo-display-progress
-              'wl-summary-sync-update3 "Updating thread..."
-              percent))
-           (setq update-top-list
-                 (elmo-uniq-list update-top-list))
+             (when (> num elmo-display-progress-threshold)
+               (setq i (+ i 1))
+               (if (or (zerop (% i 5)) (= i num))
+                   (elmo-display-progress
+                    'wl-summary-sync-update "Updating thread..."
+                    (/ (* i 100) num)))))
+           (when wl-summary-delayed-update
+             (while wl-summary-delayed-update
+               (message "Parent (%d) of message %d is no entity"
+                        (caar wl-summary-delayed-update)
+                        (elmo-msgdb-overview-entity-get-number
+                         (cdar wl-summary-delayed-update)))
+               (when (setq update-thread
+                           (wl-summary-append-message-func-internal
+                            (cdar wl-summary-delayed-update)
+                            overview mark-alist (not sync-all) t))
+                 (wl-append update-top-list update-thread))
+               (setq wl-summary-delayed-update
+                     (cdr wl-summary-delayed-update))))
            (when (and (eq wl-summary-buffer-view 'thread)
-                      update-top-list )
-             (message "Updating indent...")
-             (wl-thread-update-indent-string-thread update-top-list)
-             (message "Updating indent...done."))
-           (message "Updating thread...done.")
-           ;;(set-buffer cur-buf)
-           ))
-      (wl-summary-set-message-modified)
-      (wl-summary-set-mark-modified)
-      (setq wl-summary-buffer-msgdb msgdb)
-      (when (and sync-all (eq wl-summary-buffer-view 'thread))
-       (message "Inserting thread...")
-       (setq wl-thread-entity-cur 0)
-       (wl-thread-insert-top)
-       (message "Inserting thread...done."))
-      (if elmo-use-database
-         (elmo-database-close))
-      (run-hooks 'wl-summary-sync-updated-hook)
-      (setq ret-val (format "Updated (-%d/+%d) message(s)" 
+                      update-top-list)
+             (wl-thread-update-indent-string-thread
+              (elmo-uniq-list update-top-list)))
+           (message "Updating thread...done"))
+         (unless (eq wl-summary-buffer-view 'thread)
+           (wl-summary-make-number-list))
+         (wl-summary-set-message-modified)
+         (wl-summary-set-mark-modified)
+         (when (and sync-all (eq wl-summary-buffer-view 'thread))
+           (elmo-kill-buffer wl-summary-search-buf-name)
+           (message "Inserting thread...")
+           (setq wl-thread-entity-cur 0)
+           (wl-thread-insert-top)
+           (message "Inserting thread...done"))
+         (if elmo-use-database
+             (elmo-database-close))
+         (run-hooks 'wl-summary-sync-updated-hook)
+         (setq mes (format "Updated (-%d/+%d) message(s)"
                            (length delete-list) num)))
+      (setq mes (format
+                "No updates for \"%s\"" (elmo-folder-name-internal folder))))
     ;; synchronize marks.
     (if wl-summary-auto-sync-marks
        (wl-summary-sync-marks))
     ;; scoring
     (when wl-use-scoring
       (setq wl-summary-scored nil)
-      (wl-summary-score-headers nil msgdb 
+      (wl-summary-score-headers nil (wl-summary-buffer-msgdb)
                                (and sync-all
                                     (wl-summary-rescore-msgs number-alist))
                                sync-all)
-      (setq expunged (wl-summary-score-update-all-lines))
-      (if expunged
-         (setq ret-val (concat ret-val 
-                               (format " (%d expunged)" 
-                                       (length expunged))))))
-    ;; crosspost
-    (setq crossed2 (wl-summary-update-crosspost))
-    (if (or crossed crossed2)
-       (let ((crosses (+ (or crossed 0)
-                         (or crossed2 0))))
-         (setq ret-val
-               (if ret-val
-                   (concat ret-val
-                           (format " (%d crosspost)" crosses))
-                 (format "%d crosspost message(s)" crosses))))
-      (and ret-val
-          (setq ret-val (concat ret-val "."))))
+      (when (and wl-summary-scored
+                (setq expunged (wl-summary-score-update-all-lines)))
+       (setq mes (concat mes
+                         (format " (%d expunged)"
+                                 (length expunged))))))
+    (if (and crossed (> crossed 0))
+       (setq mes
+             (if mes
+                 (concat mes
+                         (format " (%d crosspost)" crossed))
+               (format "%d crosspost message(s)" crossed)))
+      (and mes (setq mes (concat mes "."))))
     ;; Update Folder mode
-    (wl-folder-set-folder-updated folder (list 0 
-                                              (wl-summary-count-unread 
-                                               (elmo-msgdb-get-mark-alist
-                                                msgdb))
-                                              (length in-folder)))
+    (wl-folder-set-folder-updated
+     (elmo-folder-name-internal folder)
+     (list 0
+          (wl-summary-count-unread
+           (elmo-msgdb-get-mark-alist
+            (elmo-folder-msgdb folder)))
+          (elmo-folder-messages folder)))
     (wl-summary-update-modeline)
+    (wl-summary-buffer-number-column-detect t)
     ;;
     (unless unset-cursor
       (goto-char (point-min))
@@ -2293,15 +2172,16 @@ If optional argument is non-nil, checking is omitted."
        (if (and wl-summary-highlight
                 (not (get-text-property (point) 'face)))
            (save-excursion
-             (forward-line (- 0 
+             (forward-line (- 0
                               (or
                                wl-summary-partial-highlight-above-lines
                                wl-summary-highlight-partial-threshold)))
              (wl-highlight-summary (point) (point-max))))))
+    (setq wl-summary-buffer-msgdb (elmo-folder-msgdb folder))
     (wl-delete-all-overlays)
     (set-buffer-modified-p nil)
-    ret-val))
-
+    (if mes (message "%s" mes))))
+  
 (defun wl-summary-set-score-mark (mark)
   (save-excursion
     (beginning-of-line)
@@ -2309,7 +2189,7 @@ If optional argument is non-nil, checking is omitted."
          (buffer-read-only nil)
          msg-num
          cur-mark)
-      (when (looking-at "^ *\\([0-9]+\\)\\([^0-9]\\)")
+      (when (looking-at "^ *\\([0-9-]+\\)\\([^0-9]\\)")
        (setq msg-num  (string-to-int (wl-match-buffer 1)))
        (setq cur-mark (wl-match-buffer 2))
        (when (member cur-mark (list " "
@@ -2331,7 +2211,7 @@ If optional argument is non-nil, checking is omitted."
               "+")))))
 
 (defun wl-summary-update-modeline ()
-  (setq wl-summary-buffer-unread-status 
+  (setq wl-summary-buffer-unread-status
        (format " {%s}(%d new/%d unread)"
                (if (eq wl-summary-buffer-view 'thread)
                    "T" "S")
@@ -2341,8 +2221,8 @@ If optional argument is non-nil, checking is omitted."
 
 (defsubst wl-summary-jump-to-msg (&optional number)
   (interactive)
-  (let ((num (or number 
-                (string-to-int 
+  (let ((num (or number
+                (string-to-int
                  (read-from-minibuffer "Jump to Message(No.): ")))))
     (setq num (int-to-string num))
     (if (re-search-forward (concat "^[ \t]*" num "[^0-9]") nil t)
@@ -2362,90 +2242,56 @@ If optional argument is non-nil, checking is omitted."
       (message "Hilighting...")
       (setq i 0)
       (while msgs
-       (setq i (+ i 1))
-       (elmo-display-progress
-        'wl-summary-highlight-msgs "Highlighting..."
-        (/ (* i 100) len))
        (if (wl-summary-jump-to-msg (car msgs))
            (wl-highlight-summary-current-line))
-       (setq msgs (cdr msgs)))
-      (message "Highlighting...done."))))
+       (setq msgs (cdr msgs))
+       (when (> len elmo-display-progress-threshold)
+         (setq i (+ i 1))
+         (if (or (zerop (% i 5)) (= i len))
+             (elmo-display-progress
+              'wl-summary-highlight-msgs "Highlighting..."
+              (/ (* i 100) len)))))
+      (message "Highlighting...done"))))
 
 (defun wl-summary-message-number ()
   (save-excursion
     (beginning-of-line)
-    (if (looking-at "^ *\\([0-9]+\\)")
+    (if (looking-at "^ *\\([0-9-]+\\)")
        (string-to-int (wl-match-buffer 1))
       nil)))
 
 (defun wl-summary-move (src dsts-msgs)
   (let* ((dsts (car dsts-msgs))                ; (+foo +bar)
-;;      (msgs (cdr dsts-msgs))         ; (1 2 3)
-;;      (msgdb wl-summary-buffer-msgdb)
-;;      result)
+;;;     (msgs (cdr dsts-msgs))         ; (1 2 3)
+;;;     (msgdb (wl-summary-buffer-msgdb))
+;;;     result)
         )
     (while dsts
       (setq dsts (cdr dsts)))))
 
-(defun wl-summary-flush-pending-append-operations (&optional seen-list)
-  "Execute append operations that are done while offline status."
-  (when (and (elmo-folder-plugged-p wl-summary-buffer-folder-name)
-            elmo-enable-disconnected-operation)
-    (let* ((resumed-list (elmo-dop-append-list-load 
-                         wl-summary-buffer-folder-name t))
-          (append-list (elmo-dop-append-list-load 
-                        wl-summary-buffer-folder-name))
-          (appends (append resumed-list append-list))
-          (number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
-          dels pair)
-      (when appends
-       (while appends
-         (if (setq pair (rassoc (car appends) number-alist))
-             (setq dels (append dels (list (car pair)))))
-         (setq appends (cdr appends)))
-       (when dels
-         (setq seen-list
-               (elmo-msgdb-add-msgs-to-seen-list-subr
-                dels
-                wl-summary-buffer-msgdb
-                (concat wl-summary-important-mark
-                        wl-summary-read-uncached-mark)
-                seen-list))
-         (message "Resuming summary status...")
-         (elmo-msgdb-delete-msgs wl-summary-buffer-folder-name
-                                 dels wl-summary-buffer-msgdb t)
-         (wl-summary-delete-messages-on-buffer dels)
-         (message "Resuming summary status...done."))
-       ;; delete resume-file
-       (elmo-dop-append-list-save wl-summary-buffer-folder-name nil t)
-       (when append-list
-         (elmo-dop-flush-pending-append-operations 
-          wl-summary-buffer-folder-name append-list)))))
-  seen-list)
-
 (defun wl-summary-delete-all-msgs ()
   (interactive)
   (let ((cur-buf (current-buffer))
-       (dels (elmo-list-folder wl-summary-buffer-folder-name)))
+       (dels (elmo-folder-list-messages wl-summary-buffer-elmo-folder)))
     (set-buffer cur-buf)
     (if (null dels)
        (message "No message to delete.")
-      (if (y-or-n-p (format "%s has %d message(s). Delete all?"
-                           wl-summary-buffer-folder-name
+      (if (y-or-n-p (format "%s has %d message(s).  Delete all? "
+                           (wl-summary-buffer-folder-name)
                            (length dels)))
          (progn
            (message "Deleting...")
-           (elmo-delete-msgs wl-summary-buffer-folder-name dels
-                             wl-summary-buffer-msgdb)
-           (elmo-msgdb-delete-msgs wl-summary-buffer-folder-name
-                                   dels wl-summary-buffer-msgdb)
-           ;;(elmo-msgdb-save wl-summary-buffer-folder-name nil)
+           (elmo-folder-delete-messages
+            wl-summary-buffer-elmo-folder dels)
+           (elmo-msgdb-delete-msgs (wl-summary-buffer-msgdb)
+                                   dels)
+;;;        (elmo-msgdb-save (wl-summary-buffer-folder-name) nil)
            (wl-summary-set-message-modified)
            (wl-summary-set-mark-modified)
-           (wl-folder-set-folder-updated wl-summary-buffer-folder-name
+           (wl-folder-set-folder-updated (wl-summary-buffer-folder-name)
                                          (list 0 0 0))
-           ;; for thread.
-           ;; (setq wl-thread-top-entity '(nil t nil nil))
+;;; for thread.
+;;;        (setq wl-thread-top-entity '(nil t nil nil))
            (setq wl-summary-buffer-unread-count 0)
            (setq wl-summary-buffer-new-count    0)
            (wl-summary-update-modeline)
@@ -2453,16 +2299,17 @@ If optional argument is non-nil, checking is omitted."
            (let ((inhibit-read-only t)
                  (buffer-read-only nil))
              (erase-buffer))
-           ;;    (if wl-summary-cache-use (wl-summary-save-view-cache))      
-           (message "Deleting...done.")
+;;;        (if wl-summary-cache-use (wl-summary-save-view-cache))
+           (message "Deleting...done")
            t)
        nil))))
 
 (defun wl-summary-toggle-thread (&optional arg)
-  "Toggle thread status (T)hread and (S)equencial."
+  "Toggle thread status (T)hread and (S)equential.
+If ARG, without confirm."
   (interactive "P")
   (when (or arg
-           (y-or-n-p (format "Toggle threading? (y=%s): " 
+           (y-or-n-p (format "Toggle threading? (y=%s): "
                              (if (eq wl-summary-buffer-view 'thread)
                                  "\"off\"" "\"on\""))))
     (if (eq wl-summary-buffer-view 'thread)
@@ -2477,7 +2324,7 @@ If optional argument is non-nil, checking is omitted."
   (save-excursion
     (let ((tmp-buffer (get-buffer-create " *wl-summary-load-file-object*"))
          insert-file-contents-pre-hook   ; To avoid autoconv-xmas...
-         insert-file-contents-post-hook 
+         insert-file-contents-post-hook
          ret-val)
       (if (not (file-readable-p filename))
          ()
@@ -2485,8 +2332,8 @@ If optional argument is non-nil, checking is omitted."
        (as-binary-input-file (insert-file-contents filename))
        (setq ret-val
              (condition-case nil
-                 (read (current-buffer)) 
-               (error (error "reading failed")))))
+                 (read (current-buffer))
+               (error (error "Reading failed")))))
       (kill-buffer tmp-buffer)
       ret-val)))
 
@@ -2504,45 +2351,53 @@ If optional argument is non-nil, checking is omitted."
                (wl-folder-get-entity-id entity))))
   (wl-summary-goto-folder-subr wl-summary-last-visited-folder nil nil nil t))
 
-(defun wl-summary-sticky-p (&optional fld)
-  (if fld
-      (get-buffer (wl-summary-sticky-buffer-name fld))
+(defun wl-summary-sticky-p (&optional folder)
+  (if folder
+      (get-buffer (wl-summary-sticky-buffer-name
+                  (elmo-folder-name-internal folder)))
     (not (string= wl-summary-buffer-name (buffer-name)))))
 
-(defmacro wl-summary-always-sticky-folder-p (fld)
-  (` (wl-string-match-member (, fld) wl-summary-always-sticky-folder-list)))
+(defun wl-summary-always-sticky-folder-p (folder)
+  (or (eq t wl-summary-always-sticky-folder-list)
+      (wl-string-match-member
+       (elmo-folder-name-internal folder)
+       wl-summary-always-sticky-folder-list)))
 
 (defun wl-summary-stick (&optional force)
   "Make current summary buffer sticky."
   (interactive "P")
   (if (wl-summary-sticky-p)
       (message "Current summary buffer is already sticky.")
-    (when (or force (y-or-n-p "Stick current summary buffer?"))
+    (when (or force (y-or-n-p "Stick current summary buffer? "))
       (wl-summary-toggle-disp-msg 'off)
-      (wl-summary-switch-to-clone-buffer 
-       (wl-summary-sticky-buffer-name 
-       wl-summary-buffer-folder-name))
+      (wl-summary-switch-to-clone-buffer
+       (wl-summary-sticky-buffer-name
+       (wl-summary-buffer-folder-name)))
 ;;; ???hang up
-;      (rename-buffer (wl-summary-sticky-buffer-name 
-;                    wl-summary-buffer-folder-name)))
-      (message "Folder `%s' is now sticky." wl-summary-buffer-folder-name))))
+;;;   (rename-buffer (wl-summary-sticky-buffer-name
+;;;                  (wl-summary-buffer-folder-name))))
+      (message "Folder `%s' is now sticky." (wl-summary-buffer-folder-name)))))
 
 (defun wl-summary-switch-to-clone-buffer (buffer-name)
   (let ((cur-buf (current-buffer))
        (msg (wl-summary-message-number))
        (buf (get-buffer-create buffer-name))
-       (folder wl-summary-buffer-folder-name)
+       (folder wl-summary-buffer-elmo-folder)
        (copy-variables
         (append '(wl-summary-buffer-view
                   wl-summary-buffer-refile-list
                   wl-summary-buffer-delete-list
                   wl-summary-buffer-copy-list
                   wl-summary-buffer-target-mark-list
-                  wl-summary-buffer-msgdb
+                  wl-summary-buffer-elmo-folder
                   wl-summary-buffer-number-column
                   wl-summary-buffer-number-regexp
                   wl-summary-buffer-message-modified
-                  wl-summary-buffer-mark-modified)
+                  wl-summary-buffer-mark-modified
+                  wl-summary-buffer-thread-modified
+                  wl-summary-buffer-number-list
+                  wl-summary-buffer-msgdb
+                  wl-summary-buffer-folder-name)
                 (and (eq wl-summary-buffer-view 'thread)
                      '(wl-thread-entity-hashtb
                        wl-thread-entities
@@ -2558,27 +2413,21 @@ If optional argument is non-nil, checking is omitted."
                      '(wl-current-score-file
                        wl-score-alist)))))
     (set-buffer buf)
-    (wl-summary-buffer-set-folder folder)
     (wl-summary-mode)
+    (wl-summary-buffer-set-folder folder)
     (let ((buffer-read-only nil))
       (insert-buffer cur-buf))
     (set-buffer-modified-p nil)
-    (mapcar
-     (function
-      (lambda (var)
-       (set var (save-excursion
-                  (set-buffer cur-buf)
-                  (symbol-value var)))))
-     copy-variables)
+    (while copy-variables
+      (set (car copy-variables)
+          (save-excursion
+            (set-buffer cur-buf)
+            (symbol-value (car copy-variables))))
+      (setq copy-variables (cdr copy-variables)))
     (switch-to-buffer buf)
     (kill-buffer cur-buf)
-    (setq mode-line-buffer-identification
-         (format "Wanderlust: %s" 
-                 (if (memq 'modeline wl-use-folder-petname)
-                     (wl-folder-get-petname folder)
-                   folder)))
-    (wl-summary-count-unread 
-     (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
+    (wl-summary-count-unread
+     (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
     (wl-summary-update-modeline)
     (if msg
        (if (eq wl-summary-buffer-view 'thread)
@@ -2592,60 +2441,48 @@ If optional argument is non-nil, checking is omitted."
           (get-buffer (wl-summary-sticky-buffer-name folder)))
       (get-buffer wl-summary-buffer-name)))
 
-(defun wl-summary-get-buffer-create (folder &optional force-sticky)
+(defun wl-summary-get-buffer-create (name &optional force-sticky)
   (if force-sticky
-      (get-buffer-create 
-       (wl-summary-sticky-buffer-name folder))
-    (or (get-buffer (wl-summary-sticky-buffer-name folder))
+      (get-buffer-create
+       (wl-summary-sticky-buffer-name name))
+    (or (get-buffer (wl-summary-sticky-buffer-name name))
        (get-buffer-create wl-summary-buffer-name))))
 
-(defun wl-summary-disp-msg (folder disp-msg)
-  (let (disp mes-win)
-    (if (and disp-msg
-            wl-summary-buffer-disp-msg)
-       (let ((view-message-buffer (get-buffer wl-message-buf-name))
-             (number (wl-summary-message-number))
-             cur-folder cur-number sel-win)
-         (when view-message-buffer
-           (save-excursion
-             (set-buffer view-message-buffer)
-             (setq cur-folder wl-message-buffer-cur-folder
-                   cur-number wl-message-buffer-cur-number))
-           (when (and (string= folder cur-folder)
-                      (eq number cur-number))
-             (setq sel-win (selected-window))
-             (wl-select-buffer view-message-buffer)
-             (select-window sel-win)
-             (setq disp t)))))
-    (if (not disp)
-       (setq wl-summary-buffer-disp-msg nil))
-    (when (and (not disp)
-              (setq mes-win (wl-message-buffer-window)))
-      (delete-window mes-win)
-      (run-hooks 'wl-summary-toggle-disp-off-hook))))
-
-(defun wl-summary-goto-folder-subr (&optional folder scan-type other-window 
+(defun wl-summary-make-number-list ()
+  (setq wl-summary-buffer-number-list
+       (mapcar
+        (lambda (x) (elmo-msgdb-overview-entity-get-number x))
+        (elmo-msgdb-get-overview (wl-summary-buffer-msgdb)))))
+
+(defun wl-summary-goto-folder-subr (&optional name scan-type other-window
                                              sticky interactive scoring)
-  "Display target folder on summary"
+  "Display target folder on summary."
   (interactive)
   (let* ((keep-cursor (memq this-command
                            wl-summary-keep-cursor-command))
-        (fld (or folder (wl-summary-read-folder wl-default-folder)))
-        (cur-fld wl-summary-buffer-folder-name)
-        buf mes hilit reuse-buf
+        (name (or name (wl-summary-read-folder wl-default-folder)))
+        (cur-fld wl-summary-buffer-elmo-folder)
+        folder buf mes hilit reuse-buf
         retval entity)
-    (if (string= fld "")
-       (setq fld wl-default-folder))
-    (when (and (not (string= cur-fld fld)) ; folder is moved.
+    (if (string= name "")
+       (setq name wl-default-folder))
+    (setq folder (wl-folder-get-elmo-folder name))
+    (when (and (not (string=
+                    (and cur-fld
+                         (elmo-folder-name-internal cur-fld))
+                    (elmo-folder-name-internal folder))) ; folder is moved.
               (eq major-mode 'wl-summary-mode)) ; called in summary.
-      (setq wl-summary-last-visited-folder wl-summary-buffer-folder-name)
+      (setq wl-summary-last-visited-folder (wl-summary-buffer-folder-name))
       (wl-summary-cleanup-temp-marks (wl-summary-sticky-p))
-      (wl-summary-save-status 'keep)) ;; keep current buffer, anyway.
-    (setq buf (wl-summary-get-buffer-create fld sticky))
+      (wl-summary-save-view 'keep) ; keep current buffer, anyway.
+      (elmo-folder-commit wl-summary-buffer-elmo-folder))
+    (setq buf (wl-summary-get-buffer-create (elmo-folder-name-internal folder)
+                                           sticky))
     (setq reuse-buf
          (save-excursion
            (set-buffer buf)
-           (string= fld wl-summary-buffer-folder-name)))
+           (string= (elmo-folder-name-internal folder)
+                    (wl-summary-buffer-folder-name))))
     (unwind-protect
        (if reuse-buf
            (if interactive
@@ -2654,53 +2491,52 @@ If optional argument is non-nil, checking is omitted."
          (if other-window
              (delete-other-windows))
          (set-buffer buf)
-         (wl-summary-buffer-set-folder fld)
          (unless (eq major-mode 'wl-summary-mode)
            (wl-summary-mode))
+         (wl-summary-buffer-set-folder folder)
          (setq wl-summary-buffer-disp-msg nil)
          (setq wl-summary-buffer-last-displayed-msg nil)
          (setq wl-summary-buffer-current-msg nil)
          (let ((case-fold-search nil)
                (inhibit-read-only t)
                (buffer-read-only nil))
+           ;; Select folder
+           (elmo-folder-open folder 'load-msgdb)
+           ;; For compatibility
+           (setq wl-summary-buffer-msgdb (elmo-folder-msgdb folder))
+           (setq wl-summary-buffer-folder-name (elmo-folder-name-internal
+                                                folder))
            (erase-buffer)
-           (setq mode-line-buffer-identification
-                 (format "Wanderlust: %s" 
-                         (if (memq 'modeline wl-use-folder-petname)
-                             (wl-folder-get-petname fld)
-                           fld)))
-             ;; resume summary cache
+           ;; Resume summary view
            (if wl-summary-cache-use
-               (let* ((dir (elmo-msgdb-expand-path fld))
+               (let* ((dir (elmo-folder-msgdb-path folder))
                       (cache (expand-file-name wl-summary-cache-file dir))
                       (view (expand-file-name wl-summary-view-file dir)))
                  (when (file-exists-p cache)
-                   (as-binary-input-file
-                    (insert-file-contents cache))
+                   (insert-file-contents-as-binary cache)
                    (elmo-set-buffer-multibyte
                     default-enable-multibyte-characters)
                    (decode-mime-charset-region
                     (point-min)(point-max)
                     wl-summary-buffer-mime-charset))
                  (when (file-exists-p view)
-                   (setq wl-summary-buffer-view 
+                   (setq wl-summary-buffer-view
                          (wl-summary-load-file-object view)))
                  (if (eq wl-summary-buffer-view 'thread)
-                     (wl-thread-resume-entity fld))))
-           ;; Load msgdb
-           (setq wl-summary-buffer-msgdb nil) ; new msgdb
-           (setq wl-summary-buffer-msgdb 
-                 (wl-summary-msgdb-load-async fld))
-           (if (null wl-summary-buffer-msgdb)
-               (setq wl-summary-buffer-msgdb 
-                     (elmo-msgdb-load (elmo-string fld))))
-           (wl-summary-count-unread 
-            (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
+                     (wl-thread-resume-entity folder)
+                   (wl-summary-make-number-list)))
+             (setq wl-summary-buffer-view
+                   (wl-summary-load-file-object
+                    (expand-file-name wl-summary-view-file
+                                      (elmo-folder-msgdb-path folder))))
+             (wl-summary-rescan))
+           (wl-summary-count-unread
+            (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
            (wl-summary-update-modeline)))
       (wl-summary-buffer-number-column-detect t)
-      (wl-summary-disp-msg fld (and reuse-buf keep-cursor))
+      (wl-summary-toggle-disp-msg (if wl-summary-buffer-disp-msg 'on 'off))
       (unless (and reuse-buf keep-cursor)
-       (setq hilit wl-summary-highlight)
+       ;(setq hilit wl-summary-highlight)
        (unwind-protect
            (let ((wl-summary-highlight (if reuse-buf wl-summary-highlight))
                  (wl-use-scoring
@@ -2708,8 +2544,8 @@ If optional argument is non-nil, checking is omitted."
              (if (and (not scan-type)
                       interactive
                       (not wl-ask-range))
-                 (setq scan-type (wl-summary-get-sync-range fld)))
-             (cond 
+                 (setq scan-type (wl-summary-get-sync-range folder)))
+             (cond
               ((eq scan-type nil)
                (wl-summary-sync 'unset-cursor))
               ((eq scan-type 'all)
@@ -2717,45 +2553,45 @@ If optional argument is non-nil, checking is omitted."
               ((eq scan-type 'no-sync))
               ((or (eq scan-type 'force-update)
                    (eq scan-type 'update))
-               (setq mes (wl-summary-sync-force-update 'unset-cursor)))))
+               (setq mes (wl-summary-sync-force-update
+                          'unset-cursor 'no-check)))))
          (if interactive
              (switch-to-buffer buf)
            (set-buffer buf))
          ;; stick always-sticky-folder
-         (when (wl-summary-always-sticky-folder-p fld)
+         (when (wl-summary-always-sticky-folder-p folder)
            (or (wl-summary-sticky-p) (wl-summary-stick t)))
          (run-hooks 'wl-summary-prepared-pre-hook)
          (set-buffer-modified-p nil)
          (goto-char (point-min))
          (if (wl-summary-cursor-down t)
-             (let ((unreadp (wl-thread-next-mark-p
-                             (wl-thread-entity-get-mark 
-                              (wl-summary-message-number))
-                             wl-summary-move-order)))
+             (let ((unreadp (wl-summary-next-message 
+                             (wl-summary-message-number)
+                             'down t)))
                (cond ((and wl-auto-select-first unreadp)
                       (setq retval 'disp-msg))
                      ((not unreadp)
                       (setq retval 'more-next))))
            (goto-char (point-max))
-           (if (elmo-folder-plugged-p folder) 
+           (if (elmo-folder-plugged-p folder)
                (forward-line -1)
              (wl-summary-prev))
            (setq retval 'more-next))
-         (setq wl-summary-highlight hilit)
+         ;(setq wl-summary-highlight hilit)
          (if (and wl-summary-highlight
+                  (not wl-summary-lazy-highlight)
                   (not reuse-buf))
              (if (and wl-summary-highlight-partial-threshold
                       (> (count-lines (point-min) (point-max))
                          wl-summary-highlight-partial-threshold))
                  (save-excursion
-                   (forward-line (- 
-                                  0 
-                                  wl-summary-partial-highlight-above-lines))
+                   (forward-line (-
+                                  0
+                                  (or
+                                   wl-summary-partial-highlight-above-lines
+                                   wl-summary-highlight-partial-threshold)))
                    (wl-highlight-summary (point) (point-max)))
                (wl-highlight-summary (point-min) (point-max))))
-         (if (null wl-summary-buffer-msgdb) ;; one more try.
-             (setq wl-summary-buffer-msgdb 
-                   (elmo-msgdb-load (elmo-string fld))))
          (if (eq retval 'disp-msg)
              (wl-summary-redisplay))
          (if mes (message "%s" mes))
@@ -2764,7 +2600,8 @@ If optional argument is non-nil, checking is omitted."
     ;; set current entity-id
     (if (and (not folder)
             (setq entity
-                  (wl-folder-search-entity-by-name fld
+                  (wl-folder-search-entity-by-name (elmo-folder-name-internal
+                                                    folder)
                                                    wl-folder-entity
                                                    'folder)))
        ;; entity-id is unknown.
@@ -2776,53 +2613,53 @@ If optional argument is non-nil, checking is omitted."
     retval))
 
 (defun wl-summary-summary-line-already-exists-p (parent-number buffer)
-  "returns the depth."
+  "Return the depth."
   (set-buffer buffer)
   (goto-char (point-max))
   (let ((depth 0))
     (when (re-search-backward (format "^ *%s..../..\(.*\)..:.. "
                                      parent-number) nil t)
       (goto-char (match-end 0))
-      (while (string-match wl-thread-indent-regexp 
+      (while (string-match wl-thread-indent-regexp
                           (char-to-string
-                           (char-after (point))))        
+                           (char-after (point))))
        (setq depth (+ 1 depth))
        (forward-char))
       (/ depth wl-thread-indent-level-internal))))
 
 (defun wl-summary-goto-bottom-of-current-thread ()
-  (if (re-search-forward (concat "^" wl-summary-buffer-number-regexp 
-                                "..../..\(.*\)..:.. \\[") nil t)
+  (if (re-search-forward (concat "^" wl-summary-buffer-number-regexp
+                                "..../..\(.*\)..:.. [[<]") nil t)
       ()
     (goto-char (point-max))))
 
 (defun wl-summary-goto-top-of-current-thread ()
   (wl-summary-jump-to-msg
    (wl-thread-entity-get-number
-    (wl-thread-entity-get-top-entity (wl-thread-get-entity 
+    (wl-thread-entity-get-top-entity (wl-thread-get-entity
                                      (wl-summary-message-number))))))
 
 (defun wl-summary-goto-bottom-of-sub-thread (&optional depth)
   (interactive)
-  (let ((depth (or depth 
+  (let ((depth (or depth
                   (wl-thread-get-depth-of-current-line))))
     (forward-line 1)
     (while (and (not (eobp))
-               (>= (wl-thread-get-depth-of-current-line) 
+               (>= (wl-thread-get-depth-of-current-line)
                    depth))
       (forward-line 1))
     (beginning-of-line)))
 
 (defun wl-summary-insert-line (line)
   "Insert LINE in the Summary."
-  (if wl-use-highlight-mouse-line 
+  (if wl-use-highlight-mouse-line
       ;; remove 'mouse-face of current line.
       (put-text-property
        (save-excursion (beginning-of-line)(point))
        (save-excursion (end-of-line)(point))
        'mouse-face nil))
   (insert line "\n")
-  (if wl-use-highlight-mouse-line 
+  (if wl-use-highlight-mouse-line
       ;; remove 'mouse-face of current line.
       (put-text-property
        (save-excursion (beginning-of-line)(point))
@@ -2833,12 +2670,12 @@ If optional argument is non-nil, checking is omitted."
     (error (ding)
           (message "Error in wl-summary-line-inserted-hook"))))
 
-(defun wl-summary-insert-summary (entity database mark-alist dummy)
+(defun wl-summary-insert-summary (entity database mark-alist dummy &optional dummy)
   (let ((overview-entity entity)
        summary-line msg)
     (setq msg (elmo-msgdb-overview-entity-get-number entity))
     (when (setq summary-line
-               (wl-summary-overview-create-summary-line 
+               (wl-summary-overview-create-summary-line
                 msg entity nil 0 mark-alist))
       (let ((inhibit-read-only t)
            buffer-read-only)
@@ -2847,7 +2684,7 @@ If optional argument is non-nil, checking is omitted."
 
 (defun wl-summary-default-subject-filter (subject)
   (let ((case-fold-search t))
-    (setq subject (elmo-replace-in-string subject "[ \t]*\\(re\\|was\\):" ""))
+    (setq subject (elmo-replace-in-string subject "[ \t]*\\(re\\|was\\)[:>]" ""))
     (setq subject (elmo-replace-in-string subject "[ \t]" ""))
     (elmo-replace-in-string subject "^\\[.*\\]" "")))
 
@@ -2855,28 +2692,146 @@ If optional argument is non-nil, checking is omitted."
   (string= (wl-summary-subject-filter-func-internal subject1)
           (wl-summary-subject-filter-func-internal subject2)))
 
-(defun wl-summary-insert-thread-entity (entity overview mark-alist update)
-  (let* ((this-id (elmo-msgdb-overview-entity-get-id entity))
-        (parent-entity 
-         (elmo-msgdb-overview-get-parent-entity entity overview));; temp
-        ;;(parent-id (elmo-msgdb-overview-entity-get-id parent-entity))
-        (parent-number (elmo-msgdb-overview-entity-get-number parent-entity))
-        msg)
-    (if (and parent-number
-            wl-summary-divide-thread-when-subject-changed
-            (not (wl-summary-subject-equal 
-                  (or (elmo-msgdb-overview-entity-get-subject 
-                       entity) "")
-                  (or (elmo-msgdb-overview-entity-get-subject 
-                       parent-entity) ""))))
-       (setq parent-number nil))
-    (setq msg (elmo-msgdb-overview-entity-get-number entity))
-    (wl-thread-insert-message entity overview mark-alist
-                             msg parent-number update)))
-
-(defun wl-summary-update-thread (entity 
-                                overview 
-                                mark-alist 
+(defmacro wl-summary-put-alike (alike)
+  (` (elmo-set-hash-val (format "#%d" (wl-count-lines))
+                       (, alike)
+                       wl-summary-alike-hashtb)))
+
+(defmacro wl-summary-get-alike ()
+  (` (elmo-get-hash-val (format "#%d" (wl-count-lines))
+                       wl-summary-alike-hashtb)))
+
+(defun wl-summary-insert-headers (overview func mime-decode)
+  (let (ov this last alike)
+    (buffer-disable-undo (current-buffer))
+    (make-local-variable 'wl-summary-alike-hashtb)
+    (setq wl-summary-alike-hashtb (elmo-make-hash (* (length overview) 2)))
+    (when mime-decode
+      (elmo-set-buffer-multibyte default-enable-multibyte-characters))
+    (while (setq ov (pop overview))
+      (setq this (funcall func ov))
+      (and this (setq this (std11-unfold-string this)))
+      (if (equal last this)
+         (wl-append alike (list ov))
+       (when last
+         (wl-summary-put-alike alike)
+         (insert last ?\n))
+       (setq alike (list ov)
+             last this)))
+    (when last
+      (wl-summary-put-alike alike)
+      (insert last ?\n))
+    (when mime-decode
+      (decode-mime-charset-region (point-min) (point-max)
+                                 elmo-mime-charset)
+      (when (eq mime-decode 'mime)
+       (eword-decode-region (point-min) (point-max))))
+    (run-hooks 'wl-summary-insert-headers-hook)))
+
+(defun wl-summary-search-by-subject (entity overview)
+  (let ((buf (get-buffer-create wl-summary-search-buf-name))
+       (folder-name (wl-summary-buffer-folder-name))
+       match founds found-entity)
+    (save-excursion
+      (set-buffer buf)
+      (let ((case-fold-search t))
+       (when (or (not (string= wl-summary-search-buf-folder-name folder-name))
+                 (zerop (buffer-size)))
+         (setq wl-summary-search-buf-folder-name folder-name)
+         (wl-summary-insert-headers
+          overview
+          (function
+           (lambda (x)
+             (wl-summary-subject-filter-func-internal
+              (elmo-msgdb-overview-entity-get-subject-no-decode x))))
+          t))
+       (setq match (wl-summary-subject-filter-func-internal
+                    (elmo-msgdb-overview-entity-get-subject entity)))
+       (if (string= match "")
+           (setq match "\n"))
+       (goto-char (point-max))
+       (while (and (not founds)
+                   (not (= (point) (point-min)))
+                   (search-backward match nil t))
+         ;; check exactly match
+         (when (and (bolp)
+                    (= (point-at-eol)
+                       (match-end 0)))
+           (setq found-entity (wl-summary-get-alike))
+           (if (and found-entity
+                    ;; Is founded entity myself or children?
+                    (not (string=
+                          (elmo-msgdb-overview-entity-get-id entity)
+                          (elmo-msgdb-overview-entity-get-id (car found-entity))))
+                    (not (wl-thread-descendant-p
+                          (elmo-msgdb-overview-entity-get-number entity)
+                          (elmo-msgdb-overview-entity-get-number (car found-entity)))))
+               ;; return matching entity
+               (setq founds found-entity))))
+       (if founds
+           (car founds))))))
+
+(defun wl-summary-insert-thread-entity (entity overview mark-alist update
+                                              &optional force-insert)
+  (let (update-list entity-stack)
+    (while entity
+      (let* ((this-id (elmo-msgdb-overview-entity-get-id entity))
+            (parent-entity
+             (elmo-msgdb-overview-get-parent-entity entity overview));; temp
+;;;         (parent-id (elmo-msgdb-overview-entity-get-id parent-entity))
+            (parent-number (elmo-msgdb-overview-entity-get-number parent-entity))
+            (case-fold-search t)
+            msg overview2 cur-entity linked retval delayed-entity)
+       (setq msg (elmo-msgdb-overview-entity-get-number entity))
+       (if (and parent-number
+                (not (wl-thread-get-entity parent-number))
+                (not force-insert))
+           ;; parent is exists in overview, but not exists in wl-thread-entities
+           (progn
+             (wl-append wl-summary-delayed-update
+                        (list (cons parent-number entity)))
+             (setq entity nil)) ;; exit loop
+         ;; Search parent by subject.
+         (when (and (null parent-number)
+                    wl-summary-search-parent-by-subject-regexp
+                    (string-match wl-summary-search-parent-by-subject-regexp
+                                  (elmo-msgdb-overview-entity-get-subject entity)))
+           (let ((found (wl-summary-search-by-subject entity overview)))
+             (when (and found
+                        (not (member found wl-summary-delayed-update)))
+               (setq parent-entity found)
+               (setq parent-number
+                     (elmo-msgdb-overview-entity-get-number parent-entity))
+               (setq linked t))))
+         ;; If subject is change, divide thread.
+         (if (and parent-number
+                  wl-summary-divide-thread-when-subject-changed
+                  (not (wl-summary-subject-equal
+                        (or (elmo-msgdb-overview-entity-get-subject
+                             entity) "")
+                        (or (elmo-msgdb-overview-entity-get-subject
+                             parent-entity) ""))))
+             (setq parent-number nil))
+         ;;
+         (setq retval
+               (wl-thread-insert-message entity overview mark-alist
+                                         msg parent-number update linked))
+         (and retval
+              (wl-append update-list (list retval)))
+         (setq entity nil) ; exit loop
+         (while (setq delayed-entity (assq msg wl-summary-delayed-update))
+           (setq wl-summary-delayed-update
+                 (delete delayed-entity wl-summary-delayed-update))
+           ;; update delayed message
+           (wl-append entity-stack (list (cdr delayed-entity)))))
+       (if (and (not entity)
+                entity-stack)
+           (setq entity (pop entity-stack)))))
+    update-list))
+
+(defun wl-summary-update-thread (entity
+                                overview
+                                mark-alist
                                 thr-entity
                                 parent-entity)
   (let* ((depth 0)
@@ -2885,27 +2840,27 @@ If optional argument is non-nil, checking is omitted."
         (parent-id (elmo-msgdb-overview-entity-get-id parent-entity))
         (parent-number (elmo-msgdb-overview-entity-get-number parent-entity))
         summary-line msg subject-differ)
-    (cond 
+    (cond
      ((or (not parent-id)
          (string= this-id parent-id))
       (goto-char (point-max))
       (beginning-of-line))
      ;; parent already exists in buffer.
-     ((setq depth (or (wl-summary-summary-line-already-exists-p 
+     ((setq depth (or (wl-summary-summary-line-already-exists-p
                       parent-number (current-buffer)) -1))
       (setq depth (+ 1 depth))
       (wl-thread-goto-bottom-of-sub-thread)))
-    (if (and (elmo-msgdb-overview-entity-get-number entity))
+    (if (and (setq msg (elmo-msgdb-overview-entity-get-number entity)))
        (if (setq summary-line
-                 (wl-summary-overview-create-summary-line 
-                  (elmo-msgdb-overview-entity-get-number entity)
-                  entity parent-entity depth mark-alist nil nil
-                  thr-entity))
+                 (wl-summary-overview-create-summary-line
+                  msg entity parent-entity depth mark-alist
+                  (wl-thread-maybe-get-children-num msg)
+                  nil thr-entity))
            (let ((inhibit-read-only t)
                  (buffer-read-only nil))
              (wl-summary-insert-line summary-line))))))
 
-(defun wl-summary-mark-as-unread (&optional number 
+(defun wl-summary-mark-as-unread (&optional number
                                            no-server-update
                                            no-modeline-update)
   (interactive)
@@ -2913,28 +2868,31 @@ If optional argument is non-nil, checking is omitted."
     (let* (eol
          (inhibit-read-only t)
          (buffer-read-only nil)
-         (folder wl-summary-buffer-folder-name)
-         (msgdb wl-summary-buffer-msgdb)
+         (folder wl-summary-buffer-elmo-folder)
+         (msgdb (wl-summary-buffer-msgdb))
          (mark-alist (elmo-msgdb-get-mark-alist msgdb))
-         ;;(number-alist (elmo-msgdb-get-number-alist msgdb))
+;;;      (number-alist (elmo-msgdb-get-number-alist msgdb))
          new-mark visible mark)
-      (if number 
+      (if number
          (progn
            (setq visible (wl-summary-jump-to-msg number))
            (unless (setq mark (cadr (assq number mark-alist)))
              (setq mark " ")))
        ;; interactive
        (setq visible t))
-      (end-of-line)
-      (setq eol (point))
-      (re-search-backward (concat "^" wl-summary-buffer-number-regexp
-                                 "..../..")) ; set cursor line
-      (beginning-of-line)
+      (when visible
+       (if (null (wl-summary-message-number))
+           (message "No message.")
+         (end-of-line)
+         (setq eol (point))
+         (re-search-backward (concat "^" wl-summary-buffer-number-regexp
+                                     "..../..")) ; set cursor line
+         (beginning-of-line)))
       (if (or (and (not visible)
                   ;; already exists in msgdb.
                   (assq number (elmo-msgdb-get-number-alist msgdb)))
-             (re-search-forward 
-              (format (concat "^ *\\(" 
+             (re-search-forward
+              (format (concat "^ *\\("
                               (if number (int-to-string number)
                                 "[0-9]+")
                               "\\)[^0-9]\\(%s\\|%s\\)")
@@ -2947,13 +2905,14 @@ If optional argument is non-nil, checking is omitted."
              (setq new-mark (if (string= mark
                                          wl-summary-read-uncached-mark)
                                 wl-summary-unread-uncached-mark
-                              (if (elmo-use-cache-p folder number)
+                              (if (elmo-message-use-cache-p folder number)
                                   wl-summary-unread-mark
                                 wl-summary-unread-uncached-mark))))
            ;; server side mark
            (unless no-server-update
-             (elmo-mark-as-unread folder (list number)
-                                  msgdb))
+             (save-match-data
+               (unless (elmo-folder-unmark-read folder (list number))
+                 (error "Setting mark failed"))))
            (when visible
              (delete-region (match-beginning 2) (match-end 2))
              (insert new-mark))
@@ -2963,12 +2922,12 @@ If optional argument is non-nil, checking is omitted."
                                       new-mark))
            (elmo-msgdb-set-mark-alist msgdb mark-alist)
            (unless no-modeline-update
-             (setq wl-summary-buffer-unread-count 
+             (setq wl-summary-buffer-unread-count
                    (+ 1 wl-summary-buffer-unread-count))
              (wl-summary-update-modeline)
-             (wl-folder-update-unread 
-              folder
-              (+ wl-summary-buffer-unread-count 
+             (wl-folder-update-unread
+              (wl-summary-buffer-folder-name)
+              (+ wl-summary-buffer-unread-count
                  wl-summary-buffer-new-count)))
            (wl-summary-set-mark-modified)
            (if (and visible wl-summary-highlight)
@@ -3052,7 +3011,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                  ;; opened...only myself is checked.
                  (if (wl-summary-check-mark number mark)
                      (wl-append msglist (list number)))
-                 (unless (wl-thread-entity-get-opened entity) 
+                 (unless (wl-thread-entity-get-opened entity)
                    ;; closed...children is also checked.
                    (if (setq result (wl-thread-get-children-msgs-with-mark
                                      number
@@ -3061,7 +3020,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                  (forward-line 1)))
              (elmo-uniq-list msglist))
          (let* ((case-fold-search nil)
-                (re (format (concat wl-summary-message-regexp "%s") 
+                (re (format (concat wl-summary-message-regexp "%s")
                             (regexp-quote mark))))
            (while (re-search-forward re nil t)
              (setq msglist (cons (wl-summary-message-number) msglist)))
@@ -3086,22 +3045,27 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
     (wl-summary-exec-subr (wl-summary-mark-collect "o" beg end)
                          (wl-summary-mark-collect "D" beg end)
                          (wl-summary-mark-collect "O" beg end))))
-  
-(defun wl-summary-exec-subr (msgs dels cpys)
-  (save-excursion
-    (let* ((del-fld (wl-summary-get-delete-folder 
-                    wl-summary-buffer-folder-name))
-          (start (point))
-          dst tmp msg msgs2 cpys2
-          msg-dst dst-msgs len 
-          refile-failures 
-          copy-failures
-          succeeds result executed)
-      (if (not (or msgs dels cpys))
-         (message "No marks")
+
+(defun wl-summary-exec-subr (moves dels copies)
+  (if (not (or moves dels copies))
+      (message "No marks")
+    (save-excursion
+      (let ((del-fld (wl-summary-get-delete-folder
+                     (wl-summary-buffer-folder-name)))
+           (start (point))
+           (unread-marks (list wl-summary-unread-cached-mark
+                               wl-summary-unread-uncached-mark
+                               wl-summary-new-mark))
+           (refiles (append moves dels))
+           (refile-executed 0)
+           (refile-failures 0)
+           (copy-executed 0)
+           (copy-failures 0)
+           (copy-len (length copies))
+           refile-len
+           dst-msgs                    ; loop counter
+           result)
        (message "Executing ...")
-       (setq msgs (append msgs dels))
-       (setq msgs2 msgs)
        (while dels
          (when (not (assq (car dels) wl-summary-buffer-refile-list))
            (wl-append wl-summary-buffer-refile-list
@@ -3109,113 +3073,74 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
            (setq wl-summary-buffer-delete-list
                  (delete (car dels) wl-summary-buffer-delete-list)))
          (setq dels (cdr dels)))
-       (setq len (length msgs2))
        ;; begin refile...
-       (while msgs
-         (setq msg (car msgs))
-         (setq msgs (cdr msgs))
-         (setq msg-dst (assq msg wl-summary-buffer-refile-list))
-         (setq dst (cdr msg-dst))
-         (if dst
-             (if (setq tmp (assoc dst dst-msgs))
-                 (setq dst-msgs (cons (append tmp (list msg))
-                                        (delete tmp dst-msgs)))
-               (setq dst-msgs (cons (list dst msg) dst-msgs)))))
-       (setq refile-failures 0)
-       (goto-char start) ; avoid moving cursor to the bottom line.
-       (setq executed 0)
+       (setq refile-len (length refiles))
+       (setq dst-msgs
+             (wl-inverse-alist refiles wl-summary-buffer-refile-list))
+       (goto-char start)               ; avoid moving cursor to
+                                       ; the bottom line.
        (while dst-msgs
-         ;;(elmo-msgdb-add-msgs-to-seen-list 
-         ;; (car (car dst-msgs)) ;dst-folder      
-         ;; (cdr (car dst-msgs)) ;msgs 
-         ;; wl-summary-buffer-msgdb 
-         ;; (concat wl-summary-important-mark
-         ;;  wl-summary-read-uncached-mark))
          (setq result nil)
          (condition-case nil
-             (setq result (elmo-move-msgs wl-summary-buffer-folder-name 
-                                          (cdr (car dst-msgs))
-                                          (car (car dst-msgs))
-                                          wl-summary-buffer-msgdb 
-                                          len executed (cdr dst-msgs)
-                                          nil ; no-delete
-                                          nil ; same-number
-                                          (list wl-summary-unread-cached-mark
-                                                wl-summary-unread-uncached-mark
-                                                wl-summary-new-mark)))
+             (setq result (elmo-folder-move-messages
+                           wl-summary-buffer-elmo-folder
+                           (cdr (car dst-msgs))
+                           (wl-folder-get-elmo-folder
+                            (car (car dst-msgs)))
+                           (wl-summary-buffer-msgdb)
+                           refile-len
+                           refile-executed
+                           (not (null (cdr dst-msgs)))
+                           nil ; no-delete
+                           nil ; same-number
+                           unread-marks
+                           t))
            (error nil))
-         (if result ; succeeded.
+         (if result                    ; succeeded.
              (progn
                ;; update buffer.
-               (wl-summary-delete-messages-on-buffer 
-                (cdr (car dst-msgs)))
+               (wl-summary-delete-messages-on-buffer (cdr (car dst-msgs)))
                ;; update refile-alist.
-               (mapcar 
-                (function 
-                 (lambda (x)
-                   (setq wl-summary-buffer-refile-list
-                         (delq (assq x wl-summary-buffer-refile-list)
-                               wl-summary-buffer-refile-list))))
-                (cdr (car dst-msgs))))
-           (setq refile-failures 
+               (setq wl-summary-buffer-refile-list
+                     (wl-delete-associations (cdr (car dst-msgs))
+                                             wl-summary-buffer-refile-list)))
+           (setq refile-failures
                  (+ refile-failures (length (cdr (car dst-msgs))))))
-         (setq executed (+ executed (length (cdr (car dst-msgs)))))
+         (setq refile-executed (+ refile-executed (length (cdr (car dst-msgs)))))
          (setq dst-msgs (cdr dst-msgs)))
        ;; end refile
        ;; begin cOpy...
-       (setq cpys2 cpys)
-       (setq len (length cpys2))
-       (while cpys
-         (setq msg (car cpys))
-         (setq cpys (cdr cpys))
-         (setq msg-dst (assq msg wl-summary-buffer-copy-list))
-         (setq dst (cdr msg-dst))
-         (if dst
-             (if (setq tmp (assoc dst dst-msgs))
-                 (setq dst-msgs (cons (append tmp (list msg))
-                                      (delete tmp dst-msgs)))
-               (setq dst-msgs (cons (list dst msg) dst-msgs)))))
-       (setq copy-failures 0)
-       (setq executed 0)
+       (setq dst-msgs (wl-inverse-alist copies wl-summary-buffer-copy-list))
        (while dst-msgs
-         ;;(elmo-msgdb-add-msgs-to-seen-list 
-         ;;(car (car dst-msgs)) ;dst-folder       
-         ;;(cdr (car dst-msgs)) ;msgs 
-         ;;wl-summary-buffer-msgdb 
-         ;;(concat wl-summary-important-mark
-         ;;wl-summary-read-uncached-mark))
          (setq result nil)
          (condition-case nil
-             (setq result (elmo-move-msgs wl-summary-buffer-folder-name 
-                                          (cdr (car dst-msgs))
-                                          (car (car dst-msgs))
-                                          wl-summary-buffer-msgdb 
-                                          len executed 
-                                          (cdr dst-msgs) 
-                                          t ; t is no-delete (copy)
-                                          nil ; same number
-                                          (list 
-                                           wl-summary-unread-cached-mark
-                                           wl-summary-unread-uncached-mark
-                                           wl-summary-new-mark)))
+             (setq result (elmo-folder-move-messages
+                           wl-summary-buffer-elmo-folder
+                           (cdr (car dst-msgs))
+                           (wl-folder-get-elmo-folder
+                            (car (car dst-msgs)))
+                           (wl-summary-buffer-msgdb)
+                           copy-len
+                           copy-executed
+                           (not (null (cdr dst-msgs)))
+                           t ; t is no-delete (copy)
+                           nil ; same number
+                           unread-marks
+                           t))
            (error nil))
-         (if result ; succeeded.
+         (if result                    ; succeeded.
              (progn
                ;; update buffer.
                (wl-summary-delete-copy-marks-on-buffer (cdr (car dst-msgs)))
                ;; update copy-alist
-               (mapcar
-                (function
-                 (lambda (x)
-                   (setq wl-summary-buffer-copy-list
-                         (delq (assq x wl-summary-buffer-copy-list)
-                               wl-summary-buffer-copy-list))))
-                (cdr (car dst-msgs))))
+               (setq wl-summary-buffer-copy-list
+                     (wl-delete-associations (cdr (car dst-msgs))
+                                             wl-summary-buffer-copy-list)))
            (setq copy-failures
                  (+ copy-failures (length (cdr (car dst-msgs))))))
-         (setq executed (+ executed (length (cdr (car dst-msgs)))))
+         (setq copy-executed (+ copy-executed (length (cdr (car dst-msgs)))))
          (setq dst-msgs (cdr dst-msgs)))
-       ;; end cOpy 
+       ;; end cOpy
        (wl-summary-folder-info-update)
        (wl-summary-set-message-modified)
        (wl-summary-set-mark-modified)
@@ -3235,20 +3160,27 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
   (let ((fld (completing-read
              (format "Folder name %s(%s): " (or purpose "")
                      default)
-             (or wl-folder-completion-func
+             (or wl-folder-completion-function
                  (if (memq 'read-folder wl-use-folder-petname)
                      (wl-folder-get-entity-with-petname)
                    wl-folder-entity-hashtb))
              nil nil (or init wl-default-spec)
              'wl-read-folder-hist)))
-    (setq fld (elmo-string (wl-folder-get-realname fld)))
-    (if (string-match "\n" fld)
-       (error "Not supported folder name: %s" fld))
     (if (or (string= fld wl-default-spec)
            (string= fld ""))
        (setq fld default))
+    (setq fld (elmo-string (wl-folder-get-realname fld)))
+    (if (string-match "\n" fld)
+       (error "Not supported folder name: %s" fld))    
     (unless no-create
-      (wl-folder-confirm-existence fld ignore-error))
+      (if ignore-error
+         (condition-case nil
+             (wl-folder-confirm-existence
+              (wl-folder-get-elmo-folder
+               fld))
+           (error))
+       (wl-folder-confirm-existence (wl-folder-get-elmo-folder
+                                     fld))))
     fld))
 
 (defun wl-summary-print-destination (msg-num folder)
@@ -3266,6 +3198,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       (while (< c len)
        (forward-char -1)
        (setq c (+ c (char-width (following-char)))))
+      (and (> c len) (setq folder (concat " " folder)))
       (setq rs (point))
       (put-text-property rs re 'invisible t)
       (put-text-property rs re 'wl-summary-destination t)
@@ -3280,14 +3213,14 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
   (defun wl-summary-remove-destination ()))
 
 (defsubst wl-summary-get-mark (number)
-  "Returns a temporal mark of message specified by NUMBER."
+  "Return a temporal mark of message specified by NUMBER."
   (or (and (memq number wl-summary-buffer-delete-list) "D")
       (and (assq number wl-summary-buffer-copy-list) "O")
       (and (assq number wl-summary-buffer-refile-list) "o")
-      (and (assq number wl-summary-buffer-target-mark-list) "*")))
+      (and (memq number wl-summary-buffer-target-mark-list) "*")))
 
 (defsubst wl-summary-reserve-temp-mark-p (mark)
-  "Returns t if temporal MARK should be reserved."
+  "Return t if temporal MARK should be reserved."
   (member mark wl-summary-reserve-mark-list))
 
 (defun wl-summary-refile (&optional dst number)
@@ -3296,39 +3229,37 @@ If optional argument DST is specified, put mark without asking
 destination folder.
 If optional argument NUMBER is specified, mark message specified by NUMBER.
 
-If folder is read-only, message should be copied. 
-See `wl-refile-policy-alist' for more details."  
+If folder is read-only, message should be copied.
+See `wl-refile-policy-alist' for more details."
   (interactive)
   (let ((policy (wl-get-assoc-list-value wl-refile-policy-alist
-                                        wl-summary-buffer-folder-name)))
+                                        (wl-summary-buffer-folder-name))))
     (cond ((eq policy 'copy)
           (if (interactive-p)
               (call-interactively 'wl-summary-copy)
             (wl-summary-copy dst number)))
          (t
-          (wl-summary-refile-subr "refile" (interactive-p) dst number)))))
+          (wl-summary-refile-subr 'refile (interactive-p) dst number)))))
 
 (defun wl-summary-copy (&optional dst number)
-  "Put refile mark on current line message.
+  "Put copy mark on current line message.
 If optional argument DST is specified, put mark without asking
 destination folder.
 If optional argument NUMBER is specified, mark message specified by NUMBER."
   (interactive)
-  (wl-summary-refile-subr "copy" (interactive-p) dst number))
+  (wl-summary-refile-subr 'copy (interactive-p) dst number))
 
 (defun wl-summary-refile-subr (copy-or-refile interactive &optional dst number)
   (interactive)
   (let* ((buffer-num (wl-summary-message-number))
         (msg-num (or number buffer-num))
-        (msgid (and msg-num 
-                    (cdr (assq msg-num
-                               (elmo-msgdb-get-number-alist
-                                wl-summary-buffer-msgdb)))))
+        (msgid (and msg-num
+                    (elmo-message-field wl-summary-buffer-elmo-folder
+                                        msg-num 'message-id)))
         (entity (and msg-num
-                     (elmo-msgdb-overview-get-entity-by-number
-                      (elmo-msgdb-get-overview wl-summary-buffer-msgdb)
-                      msg-num)))
-        (variable 
+                     (elmo-msgdb-overview-get-entity
+                      msg-num (wl-summary-buffer-msgdb))))
+        (variable
          (intern (format "wl-summary-buffer-%s-list" copy-or-refile)))
         folder mark already tmp-folder)
     (catch 'done
@@ -3347,39 +3278,36 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
              (error "Already marked as `%s'" mark))
          (throw 'done nil)))
       (setq folder (and msg-num
-                       (or dst (wl-summary-read-folder 
+                       (or dst (wl-summary-read-folder
                                 (or (wl-refile-guess entity) wl-trash-folder)
                                 (format "for %s" copy-or-refile)))))
       ;; Cache folder hack by okada@opaopa.org
-      (if (and (eq (car (elmo-folder-get-spec folder)) 'cache)
-              (not (string= folder
+      (if (and (eq (elmo-folder-type-internal
+                   (wl-folder-get-elmo-folder
+                    (wl-folder-get-realname folder))) 'cache)
+              (not (string= folder
                             (setq tmp-folder
-                                  (concat "'cache/" 
+                                  (concat "'cache/"
                                           (elmo-cache-get-path-subr
                                            (elmo-msgid-to-cache msgid)))))))
-         (progn 
-           (setq folder tmp-folder)
-           (message "Force refile to %s." folder)))
-      (if (string= folder wl-summary-buffer-folder-name)
+         (progn
+           (setq folder tmp-folder)
+           (message "Force refile to %s." folder)))
+      (if (string= folder (wl-summary-buffer-folder-name))
          (error "Same folder"))
-      (if (and
-          (not (elmo-folder-plugged-p folder))
-          (or (null msgid)
-              (not (elmo-cache-exists-p msgid))))
-         (error "Unplugged (no cache or msgid)"))
       (if (or (string= folder wl-queue-folder)
              (string= folder wl-draft-folder))
          (error "Don't %s messages to %s" copy-or-refile folder))
       ;; learn for refile.
-      (if (string= "refile" copy-or-refile)
+      (if (eq copy-or-refile 'refile)
          (wl-refile-learn entity folder))
       (wl-summary-unmark msg-num)
-      (set variable (append 
+      (set variable (append
                     (symbol-value variable)
                     (list (cons msg-num folder))))
       (when (or interactive
                (eq number buffer-num))
-       (wl-summary-mark-line (if (string= "refile" copy-or-refile)
+       (wl-summary-mark-line (if (eq copy-or-refile 'refile)
                                  "o" "O"))
        ;; print refile destination
        (wl-summary-print-destination msg-num folder))
@@ -3392,7 +3320,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       msg-num)))
 
 (defun wl-summary-refile-prev-destination ()
-  "Refile message to previously refiled destination"
+  "Refile message to previously refiled destination."
   (interactive)
   (wl-summary-refile wl-summary-buffer-prev-refile-destination
                     (wl-summary-message-number))
@@ -3401,7 +3329,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
     (wl-summary-next)))
 
 (defun wl-summary-copy-prev-destination ()
-  "Refile message to previously refiled destination"
+  "Refile message to previously refiled destination."
   (interactive)
   (wl-summary-copy wl-summary-buffer-prev-copy-destination
                   (wl-summary-message-number))
@@ -3409,69 +3337,70 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       (wl-summary-prev)
     (wl-summary-next)))
 
-(defsubst wl-summary-no-auto-refile-message-p (msg mark-alist) 
+(defsubst wl-summary-no-auto-refile-message-p (msg mark-alist)
   (member (cadr (assq msg mark-alist)) wl-summary-auto-refile-skip-marks))
 
 (defun wl-summary-auto-refile (&optional open-all)
-  "Set refile mark automatically according to wl-refile-guess-by-rule."
+  "Set refile mark automatically according to 'wl-refile-guess-by-rule'."
   (interactive "P")
   (message "Marking...")
   (save-excursion
     (if (and (eq wl-summary-buffer-view 'thread)
-            open-all) 
+            open-all)
        (wl-thread-open-all))
-    (let* ((spec wl-summary-buffer-folder-name)
+    (let* ((spec (wl-summary-buffer-folder-name))
           (overview (elmo-msgdb-get-overview
-                     wl-summary-buffer-msgdb))
+                     (wl-summary-buffer-msgdb)))
           (mark-alist (elmo-msgdb-get-mark-alist
-                       wl-summary-buffer-msgdb))
+                       (wl-summary-buffer-msgdb)))
           checked-dsts
           (count 0)
           number dst thr-entity)
       (goto-line 1)
       (while (not (eobp))
        (setq number (wl-summary-message-number))
-       (when (and (not (wl-summary-no-auto-refile-message-p number 
+       (when (and (not (wl-summary-no-auto-refile-message-p number
                                                             mark-alist))
                   (setq dst
-                        (wl-refile-guess-by-rule
-                         (elmo-msgdb-overview-get-entity-by-number
-                          overview number)))
+                        (wl-folder-get-realname
+                         (wl-refile-guess-by-rule
+                          (elmo-msgdb-overview-get-entity
+                           number (wl-summary-buffer-msgdb)))))
                   (not (equal dst spec)))
          (when (not (member dst checked-dsts))
-           (wl-folder-confirm-existence dst)
+           (wl-folder-confirm-existence (wl-folder-get-elmo-folder dst))
            (setq checked-dsts (cons dst checked-dsts)))
          (if (wl-summary-refile dst number)
              (incf count))
          (message "Marking...%d message(s)." count))
        (if (eq wl-summary-buffer-view 'thread)
            ;; process invisible children.
-           (if (not (wl-thread-entity-get-opened
-                     (setq thr-entity (wl-thread-get-entity number))))
-               (mapcar 
-                (function
-                 (lambda (x)
-                   (when (and (setq dst 
-                                    (wl-refile-guess-by-rule
-                                     (elmo-msgdb-overview-get-entity-by-number 
-                                      overview x)))
-                              (not (equal dst spec)))
-                     (if (wl-summary-refile dst x)
-                         (incf count))
-                     (message "Marking...%d message(s)." count))))
-                (elmo-delete-if
-                 (function (lambda (x)
-                             (wl-summary-no-auto-refile-message-p 
-                              x
-                              mark-alist)))
-                 (wl-thread-entity-get-descendant thr-entity)))))
+           (unless (wl-thread-entity-get-opened
+                    (setq thr-entity (wl-thread-get-entity number)))
+             (let ((messages
+                    (elmo-delete-if
+                     (function
+                      (lambda (x)
+                        (wl-summary-no-auto-refile-message-p
+                         x mark-alist)))
+                     (wl-thread-entity-get-descendant thr-entity))))
+               (while messages
+                 (when (and (setq dst
+                                  (wl-refile-guess-by-rule
+                                   (elmo-msgdb-overview-get-entity
+                                    (car messages) (wl-summary-buffer-msgdb))))
+                            (not (equal dst spec)))
+                   (if (wl-summary-refile dst (car messages))
+                       (incf count))
+                   (message "Marking...%d message(s)." count))
+                 (setq messages (cdr messages))))))
        (forward-line))
       (if (eq count 0)
          (message "No message was marked.")
        (message "Marked %d message(s)." count)))))
 
 (defun wl-summary-unmark (&optional number)
-  "Unmark marks (temporary, refile, copy, delete)of current line. 
+  "Unmark marks (temporary, refile, copy, delete)of current line.
 If optional argument NUMBER is specified, unmark message specified by NUMBER."
   (interactive)
   (save-excursion
@@ -3486,7 +3415,7 @@ If optional argument NUMBER is specified, unmark message specified by NUMBER."
          (setq visible (wl-summary-jump-to-msg number))
        (setq visible t))
       ;; Delete mark on buffer.
-      (when (and visible 
+      (when (and visible
                 (looking-at "^ *\\([0-9]+\\)\\([^0-9]\\)"))
        (goto-char (match-end 2))
        (or number
@@ -3501,7 +3430,7 @@ If optional argument NUMBER is specified, unmark message specified by NUMBER."
        (if (or (string= cur-mark "o")
                (string= cur-mark "O"))
            (wl-summary-remove-destination))
-       (if wl-summary-highlight          
+       (if wl-summary-highlight
            (wl-highlight-summary-current-line nil nil score-mark))
        (set-buffer-modified-p nil))
       ;; Remove from temporary mark structure.
@@ -3556,7 +3485,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
   "Put copy mark on messages in the region specified by BEG and END."
   (interactive "r")
   (wl-summary-refile-region-subr "refile" beg end))
-  
+
 (defun wl-summary-copy-region (beg end)
   "Put copy mark on messages in the region specified by BEG and END."
   (interactive "r")
@@ -3568,36 +3497,34 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       (goto-char beg)
       ;; guess by first msg
       (let* ((msgid (cdr (assq (wl-summary-message-number)
-                               (elmo-msgdb-get-number-alist
-                                wl-summary-buffer-msgdb))))
+                              (elmo-msgdb-get-number-alist
+                               (wl-summary-buffer-msgdb)))))
             (function (intern (format "wl-summary-%s" copy-or-refile)))
             (entity (assoc msgid (elmo-msgdb-get-overview
-                                  wl-summary-buffer-msgdb)))
+                                  (wl-summary-buffer-msgdb))))
             folder)
-       (if entity
-           (setq folder (wl-summary-read-folder (wl-refile-guess entity)
-                                                (format "for %s"
+       (if entity
+           (setq folder (wl-summary-read-folder (wl-refile-guess entity)
+                                                (format "for %s"
                                                         copy-or-refile))))
-       (narrow-to-region beg end)
-       (if (eq wl-summary-buffer-view 'thread)
-           (progn
-             (while (not (eobp))
-               (let* ((number (wl-summary-message-number))
-                      (entity (wl-thread-get-entity number))
-                      children)
-                 (if (wl-thread-entity-get-opened entity)
-                     ;; opened...refile line.
-                     (funcall function folder number)
-                   ;; closed
-                   (mapcar
-                    (function
-                     (lambda (x)
-                       (funcall function folder x)))
-                    (wl-thread-get-children-msgs number)))
-                 (forward-line 1))))
-         (while (not (eobp))
-           (funcall function folder (wl-summary-message-number))
-           (forward-line 1)))))))
+       (narrow-to-region beg end)
+       (if (eq wl-summary-buffer-view 'thread)
+           (progn
+             (while (not (eobp))
+               (let* ((number (wl-summary-message-number))
+                      (entity (wl-thread-get-entity number))
+                      children)
+                 (if (wl-thread-entity-get-opened entity)
+                     ;; opened...refile line.
+                     (funcall function folder number)
+                   ;; closed
+                   (setq children (wl-thread-get-children-msgs number))
+                   (while children
+                     (funcall function folder (pop children))))
+                 (forward-line 1))))
+         (while (not (eobp))
+           (funcall function folder (wl-summary-message-number))
+           (forward-line 1)))))))
 
 (defun wl-summary-unmark-region (beg end)
   (interactive "r")
@@ -3614,8 +3541,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                    ;; opened...unmark line.
                    (wl-summary-unmark)
                  ;; closed
-                 (mapcar
-                  'wl-summary-unmark
+                 (wl-summary-delete-marks-on-buffer
                   (wl-thread-get-children-msgs number))))
              (forward-line 1)))
        (while (not (eobp))
@@ -3632,14 +3558,15 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
            (while (not (eobp))
              (let* ((number (wl-summary-message-number))
                     (entity (wl-thread-get-entity number))
-                    (wl-summary-move-direction-downward t))
+                    (wl-summary-move-direction-downward t)
+                    children)
                (if (wl-thread-entity-get-opened entity)
                    ;; opened...delete line.
                    (funcall function number)
                  ;; closed
-                 (mapcar
-                  function
-                  (wl-thread-get-children-msgs number)))
+                 (setq children (wl-thread-get-children-msgs number))
+                 (while children
+                   (funcall function (pop children))))
                (forward-line 1))))
        (while (not (eobp))
          (funcall function (wl-summary-message-number))
@@ -3656,14 +3583,14 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
 (defun wl-summary-target-mark-all ()
   (interactive)
   (wl-summary-target-mark-region (point-min) (point-max))
-  (setq wl-summary-buffer-target-mark-list 
-       (mapcar 'car 
-               (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))))
+  (setq wl-summary-buffer-target-mark-list
+       (mapcar 'car
+               (elmo-msgdb-get-number-alist (wl-summary-buffer-msgdb)))))
 
 (defun wl-summary-delete-all-mark (mark)
   (goto-char (point-min))
   (let ((case-fold-search nil))
-    (while (re-search-forward (format "^ *[0-9]+%s" 
+    (while (re-search-forward (format "^ *[0-9]+%s"
                                      (regexp-quote mark)) nil t)
       (wl-summary-unmark))
     (cond ((string= mark "*")
@@ -3695,7 +3622,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
     (setq beg (point))
     (end-of-line)
     (wl-summary-goto-bottom-of-current-thread)
-;    (forward-line -1)
+;;; (forward-line -1)
     (beginning-of-line)
     (setq end (point))
     (wl-summary-target-mark-region beg end)))
@@ -3710,124 +3637,48 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
 
 (defun wl-summary-pick (&optional from-list delete-marks)
   (interactive)
-  (save-excursion
-    (let* ((completion-ignore-case t)
-          (field (completing-read
-                  (format "Field name (%s): " wl-summary-pick-field-default)
-                  (mapcar 'list
-                          (append '("From" "Subject" "Date"
-                                    "To" "Cc" "Body" "Since" "Before")
-                                  elmo-msgdb-extra-fields))))
-          (field (if (string= field "")
-                     (setq field wl-summary-pick-field-default)
-                   field))
-          (value (if (string-match field "Since\\|Before")
-                     (completing-read "Value: "
-                                      (mapcar (function
-                                               (lambda (x)
-                                                 (list (format "%s" (car x)))))
-                                              elmo-date-descriptions))
-                   (read-from-minibuffer "Value: ")))
-          (overview (elmo-msgdb-get-overview wl-summary-buffer-msgdb))
-          (number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
-          (elmo-search-mime-charset wl-search-mime-charset)
-          server-side-search
-          result get-func sum)
-      (if delete-marks
-         (let ((mlist wl-summary-buffer-target-mark-list))
-           (while mlist
-             (when (wl-summary-jump-to-msg (car mlist))
-               (wl-summary-unmark))
-             (setq mlist (cdr mlist)))
-           (setq wl-summary-buffer-target-mark-list nil)))
-      (setq field (downcase field))
-      (cond 
-       ((string-match field "from")
-       (setq get-func 'elmo-msgdb-overview-entity-get-from))
-       ((string-match field "subject")
-       (setq get-func 'elmo-msgdb-overview-entity-get-subject))
-       ((string-match field "date")
-       (setq get-func 'elmo-msgdb-overview-entity-get-date))
-       ((string-match field "to")
-       (setq get-func 'elmo-msgdb-overview-entity-get-to))
-       ((string-match field "cc")
-       (setq get-func 'elmo-msgdb-overview-entity-get-cc))
-       ((string-match field "since")
-       (setq server-side-search (vector 'date "since" value)))
-       ((string-match field "before")
-       (setq server-side-search (vector 'date "before" value)))
-       ((string-match field "body")
-       (setq server-side-search (vector 'match "body" value)))
-       ((member field elmo-msgdb-extra-fields)
-       (setq get-func
-             (lambda (entity)
-               (elmo-msgdb-overview-entity-get-extra-field entity field))))
-       (t
-       (error "Pick by %s is not supported" field)))
-      (unwind-protect
-         (if server-side-search
-             (progn
-               (message "Searching...")
-               (let ((elmo-mime-charset wl-summary-buffer-mime-charset))
-                 (setq result (elmo-search wl-summary-buffer-folder-name 
-                                           (list server-side-search))))
-               (if from-list
-                   (setq result (elmo-list-filter from-list result)))
-               (message "%d message(s) are picked." (length result)))
-           (setq sum 0)
-           (message "Searching...")
-           (while overview
-             (when (and (string-match value
-                                      (or 
-                                       (funcall get-func (car overview))
-                                       ""))
-                        (or (not from-list)
-                            (memq 
-                             (elmo-msgdb-overview-entity-get-number
-                              (car overview)) from-list)))
-               (setq result
-                     (append result
-                             (list
-                              (elmo-msgdb-overview-entity-get-number
-                               (car overview)))))
-               (message "Picked %d message(s)." (setq sum (+ sum 1))))
-             (setq overview (cdr overview)))
-           (message "%d message(s) are picked." sum))
-       (if (null result)
-           (message "No message was picked.")
-         (wl-summary-target-mark-msgs result))))))
-  
+  (let ((result (elmo-msgdb-search
+                wl-summary-buffer-elmo-folder
+                (elmo-read-search-condition wl-summary-pick-field-default)
+                (wl-summary-buffer-msgdb))))
+    (if delete-marks
+      (let ((mlist wl-summary-buffer-target-mark-list))
+       (while mlist
+         (when (wl-summary-jump-to-msg (car mlist))
+           (wl-summary-unmark))
+         (setq mlist (cdr mlist)))
+       (setq wl-summary-buffer-target-mark-list nil)))
+    (if from-list
+       (setq result (elmo-list-filter from-list result)))
+    (message "%d message(s) are picked." (length result))
+    (if (null result)
+       (message "No message was picked.")
+      (wl-summary-target-mark-msgs result))))
+
 (defun wl-summary-unvirtual ()
   "Exit from current virtual folder."
   (interactive)
   (if (eq 'filter
-         (elmo-folder-get-type wl-summary-buffer-folder-name))
-      (wl-summary-goto-folder-subr (nth 2 (elmo-folder-get-spec
-                                          wl-summary-buffer-folder-name))
-                                  'update nil nil t)
+         (elmo-folder-type-internal wl-summary-buffer-elmo-folder))
+      (wl-summary-goto-folder-subr
+       (elmo-folder-name-internal
+       (elmo-filter-folder-target-internal
+        wl-summary-buffer-elmo-folder))
+       'update nil nil t)
     (error "This folder is not filtered")))
 
 (defun wl-summary-virtual (&optional arg)
-  "Goto virtual folder."
+  "Goto virtual folder.
+If ARG, exit virtual folder."
   (interactive "P")
   (if arg
       (wl-summary-unvirtual)
-    (let* ((completion-ignore-case t)
-          (field (completing-read (format "Field name (%s): " 
-                                          wl-summary-pick-field-default)
-                                  '(("From" . "From") 
-                                    ("Subject" . "Subject")
-                                    ("To" . "To")
-                                    ("Cc" . "Cc")
-                                    ("Body" . "Body")
-                                    ("Since" . "Since")
-                                    ("Before" . "Before"))))
-          (value (read-from-minibuffer "Value: ")))
-      (if (string= field "")
-         (setq field wl-summary-pick-field-default))
-      (wl-summary-goto-folder-subr (concat "/" (downcase field) "=" value "/" 
-                                          wl-summary-buffer-folder-name)
-                                  'update nil nil t))))
+    (wl-summary-goto-folder-subr (concat "/"
+                                        (elmo-read-search-condition
+                                         wl-summary-pick-field-default)
+                                        "/"
+                                        (wl-summary-buffer-folder-name))
+                                'update nil nil t)))
 
 (defun wl-summary-delete-all-temp-marks ()
   (interactive)
@@ -3837,12 +3688,12 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
     (while (not (eobp))
       (wl-summary-unmark)
       (forward-line))
-    (message "Unmarking...done.")
+    (message "Unmarking...done")
     (setq wl-summary-buffer-target-mark-list nil)
     (setq wl-summary-buffer-delete-list nil)
     (setq wl-summary-buffer-refile-list nil)
     (setq wl-summary-buffer-copy-list nil)))
-    
+
 (defun wl-summary-delete-mark (number)
   "Delete temporary mark of the message specified by NUMBER."
   (cond
@@ -3854,7 +3705,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
          (delq number wl-summary-buffer-delete-list)))
    (t
     (let (pair)
-      (cond 
+      (cond
        ((setq pair (assq number wl-summary-buffer-copy-list))
        (setq wl-summary-buffer-copy-list
              (delq pair wl-summary-buffer-copy-list)))
@@ -3863,7 +3714,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
              (delq pair wl-summary-buffer-refile-list))))))))
 
 (defun wl-summary-mark-line (mark)
-  "Put MARK on current line. Returns message number."
+  "Put MARK on current line.  Return message number."
   (save-excursion
     (beginning-of-line)
     (let ((inhibit-read-only t)
@@ -3875,7 +3726,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
        (setq cur-mark (wl-match-buffer 2))
        (goto-char (match-end 1))
        (delete-region (match-beginning 2) (match-end 2))
-       ;(wl-summary-delete-mark msg-num)
+;;;    (wl-summary-delete-mark msg-num)
        (insert mark)
        (if wl-summary-highlight
            (wl-highlight-summary-current-line nil nil t))
@@ -3925,7 +3776,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                    (delete-region (match-beginning 1) (match-end 1)))
                  (goto-char (match-beginning 1))
                  (insert new-mark)
-                 (if wl-summary-highlight      
+                 (if wl-summary-highlight
                      (wl-highlight-summary-current-line))
                  (save-excursion
                    (goto-char pos)
@@ -3949,10 +3800,10 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       (when (re-search-forward regexp nil t)
        (setq msgid (cdr (assq (setq number (wl-summary-message-number))
                               (elmo-msgdb-get-number-alist
-                               wl-summary-buffer-msgdb)))
+                               (wl-summary-buffer-msgdb))))
              entity (assoc msgid
                            (elmo-msgdb-get-overview
-                            wl-summary-buffer-msgdb)))
+                            (wl-summary-buffer-msgdb))))
        (if (null entity)
            (error "Cannot %s" copy-or-refile))
        (funcall function
@@ -3972,7 +3823,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
        ;; process invisible messages.
        (setq mlist wl-summary-buffer-target-mark-list)
        (while mlist
-         (set variable 
+         (set variable
               (append (symbol-value variable)
                       (list (cons (car mlist) folder))))
          (setq wl-summary-buffer-target-mark-list
@@ -4001,19 +3852,19 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
          (delete-region (match-beginning 1) (match-end 1))
          (insert " ")
          (setq number (wl-summary-mark-as-read t))
-         (if wl-summary-highlight      
+         (if wl-summary-highlight
              (wl-highlight-summary-current-line))
          (if number
              (setq wl-summary-buffer-target-mark-list
                    (delq number wl-summary-buffer-target-mark-list)))))
       (setq mlist wl-summary-buffer-target-mark-list)
       (while mlist
-       (wl-thread-msg-mark-as-read (car mlist))
+       (wl-summary-mark-as-read t nil nil (car mlist))
        (setq wl-summary-buffer-target-mark-list
              (delq (car mlist) wl-summary-buffer-target-mark-list))
        (setq mlist (cdr mlist)))
-      (wl-summary-count-unread 
-       (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
+      (wl-summary-count-unread
+       (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
       (wl-summary-update-modeline))))
 
 (defun wl-summary-target-mark-mark-as-unread ()
@@ -4030,7 +3881,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
          (delete-region (match-beginning 1) (match-end 1))
          (insert " ")
          (setq number (wl-summary-mark-as-unread))
-         (if wl-summary-highlight      
+         (if wl-summary-highlight
              (wl-highlight-summary-current-line))
          (if number
              (setq wl-summary-buffer-target-mark-list
@@ -4038,12 +3889,12 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       (setq mlist wl-summary-buffer-target-mark-list)
       (while mlist
        (wl-summary-mark-as-unread (car mlist))
-       (wl-thread-msg-mark-as-unread (car mlist))
+;;;    (wl-thread-msg-mark-as-unread (car mlist))
        (setq wl-summary-buffer-target-mark-list
              (delq (car mlist) wl-summary-buffer-target-mark-list))
        (setq mlist (cdr mlist)))
-      (wl-summary-count-unread 
-       (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
+      (wl-summary-count-unread
+       (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
       (wl-summary-update-modeline))))
 
 (defun wl-summary-target-mark-mark-as-important ()
@@ -4060,7 +3911,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
          (delete-region (match-beginning 1) (match-end 1))
          (insert " ")
          (setq number (wl-summary-mark-as-important))
-         (if wl-summary-highlight      
+         (if wl-summary-highlight
              (wl-highlight-summary-current-line))
          (if number
              (setq wl-summary-buffer-target-mark-list
@@ -4072,8 +3923,8 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
        (setq wl-summary-buffer-target-mark-list
              (delq (car mlist) wl-summary-buffer-target-mark-list))
        (setq mlist (cdr mlist)))
-      (wl-summary-count-unread 
-       (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
+      (wl-summary-count-unread
+       (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
       (wl-summary-update-modeline))))
 
 (defun wl-summary-target-mark-save ()
@@ -4098,23 +3949,23 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
   (interactive)
   (wl-summary-pick wl-summary-buffer-target-mark-list 'delete))
 
-(defun wl-summary-mark-as-read (&optional notcrosses 
+(defun wl-summary-mark-as-read (&optional notcrosses
                                          leave-server-side-mark-untouched
-                                         displayed 
+                                         displayed
                                          number
-                                         no-cache)
+                                         cached)
   (interactive)
   (save-excursion
     (let* (eol
           (inhibit-read-only t)
           (buffer-read-only nil)
-          (folder wl-summary-buffer-folder-name)
-          (msgdb wl-summary-buffer-msgdb)
+          (folder wl-summary-buffer-elmo-folder)
+          (msgdb (wl-summary-buffer-msgdb))
           (mark-alist (elmo-msgdb-get-mark-alist msgdb))
-          ;;(number-alist (elmo-msgdb-get-number-alist msgdb))
+;;;       (number-alist (elmo-msgdb-get-number-alist msgdb))
           (case-fold-search nil)
-          mark unread visible uncached new-mark)
-      (if number 
+          mark stat visible uncached new-mark marked)
+      (if number
          (progn
            (setq visible (wl-summary-jump-to-msg number))
            (setq mark (cadr (assq number mark-alist))))
@@ -4122,7 +3973,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       (beginning-of-line)
       (if (or (not visible)
              (looking-at
-              (format "^ *\\([0-9]+\\)[^0-9]\\(%s\\|%s\\|%s\\|%s\\).*$" 
+              (format "^ *\\([0-9]+\\)[^0-9]\\(%s\\|%s\\|%s\\|%s\\).*$"
                       (regexp-quote wl-summary-read-uncached-mark)
                       (regexp-quote wl-summary-unread-uncached-mark)
                       (regexp-quote wl-summary-unread-cached-mark)
@@ -4130,52 +3981,59 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
          (progn
            (setq mark (or mark (wl-match-buffer 2)))
            (when mark
-             (cond 
+             (cond
               ((string= mark wl-summary-new-mark) ; N
-               (setq wl-summary-buffer-new-count 
-                     (- wl-summary-buffer-new-count 1))
-               (setq uncached t)
-               (setq unread t))
+               (setq stat 'new)
+               (setq uncached t))
               ((string= mark wl-summary-unread-uncached-mark) ; U
-               (setq wl-summary-buffer-unread-count 
-                     (- wl-summary-buffer-unread-count 1))
-               (setq uncached t)
-               (setq unread t))
+               (setq stat 'unread)
+               (setq uncached t))
               ((string= mark wl-summary-unread-cached-mark)  ; !
-               (setq wl-summary-buffer-unread-count 
-                     (- wl-summary-buffer-unread-count 1))
-               (setq unread t))
+               (setq stat 'unread))
               (t
                ;; no need to mark server.
-               (setq leave-server-side-mark-untouched t)))
-             (wl-summary-update-modeline)
-             (wl-folder-update-unread 
-              folder
-              (+ wl-summary-buffer-unread-count 
-                 wl-summary-buffer-new-count)))
+               (setq leave-server-side-mark-untouched t))))
            (setq number (or number (string-to-int (wl-match-buffer 1))))
            ;; set server side mark...
-           (setq new-mark (if (and uncached no-cache) 
+           (setq new-mark (if (and uncached
+                                   (if (elmo-message-use-cache-p folder number)
+                                       (not (elmo-folder-local-p folder)))
+                                   (not cached))
                               wl-summary-read-uncached-mark
                             nil))
            (if (not leave-server-side-mark-untouched)
-               (elmo-mark-as-read folder
-                                  (list number) msgdb))
-           (when visible
-             (goto-char (match-end 2))
-             (delete-region (match-beginning 2) (match-end 2))
-             (insert (or new-mark " ")))
-           (setq mark-alist
-                 (elmo-msgdb-mark-set mark-alist number new-mark))
-           (elmo-msgdb-set-mark-alist msgdb mark-alist)
-           (wl-summary-set-mark-modified)
-           (if (and visible wl-summary-highlight)
-               (wl-highlight-summary-current-line nil nil t))
-           (if (not notcrosses)
-               (wl-summary-set-crosspost nil (and wl-summary-buffer-disp-msg
-                                                   (interactive-p))))))
+               (save-match-data
+                 (setq marked (elmo-folder-mark-as-read
+                               folder
+                               (list number)))))
+           (if (or leave-server-side-mark-untouched
+                   marked)
+               (progn
+                 (cond ((eq stat 'unread)
+                        (setq wl-summary-buffer-unread-count
+                              (1- wl-summary-buffer-unread-count)))
+                       ((eq stat 'new)
+                        (setq wl-summary-buffer-new-count
+                              (1- wl-summary-buffer-new-count))))
+                 (wl-summary-update-modeline)
+                 (wl-folder-update-unread
+                  (wl-summary-buffer-folder-name)
+                  (+ wl-summary-buffer-unread-count
+                     wl-summary-buffer-new-count))
+                 (when (or stat cached)
+                   (when visible
+                     (goto-char (match-end 2))
+                     (delete-region (match-beginning 2) (match-end 2))
+                     (insert (or new-mark " ")))
+                   (setq mark-alist
+                         (elmo-msgdb-mark-set mark-alist number new-mark))
+                   (elmo-msgdb-set-mark-alist msgdb mark-alist)
+                   (wl-summary-set-mark-modified))
+                 (if (and visible wl-summary-highlight)
+                     (wl-highlight-summary-current-line nil nil t)))
+             (if mark (message "Warning: Changing mark failed.")))))
       (set-buffer-modified-p nil)
-      (if unread
+      (if stat
          (run-hooks 'wl-summary-unread-message-hook))
       number ;return value
       )))
@@ -4184,40 +4042,55 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                                               mark
                                               no-server-update)
   (interactive)
-  (if (eq (elmo-folder-get-type wl-summary-buffer-folder-name)
+  (if (eq (elmo-folder-type-internal wl-summary-buffer-elmo-folder)
          'internal)
       (error "Cannot process mark in this folder"))
   (save-excursion
     (let* (eol
          (inhibit-read-only t)
          (buffer-read-only nil)
-         (folder wl-summary-buffer-folder-name)
-         (msgdb wl-summary-buffer-msgdb)
+         (folder wl-summary-buffer-elmo-folder)
+         (msgdb (wl-summary-buffer-msgdb))
          (mark-alist (elmo-msgdb-get-mark-alist msgdb))
          (number-alist (elmo-msgdb-get-number-alist msgdb))
          message-id visible)
-      (if number 
+      (if number
          (progn
            (setq visible (wl-summary-jump-to-msg number))
            (setq mark (or mark (cadr (assq number mark-alist)))))
        (setq visible t))
-      (end-of-line)
-      (setq eol (point))
-      (if visible
+      (when visible
+       (if (null (wl-summary-message-number))
+           (progn
+             (message "No message.")
+             (setq visible nil))
+         (end-of-line)
+         (setq eol (point))
          (re-search-backward (concat "^" wl-summary-buffer-number-regexp
                                      "..../..") nil t)) ; set cursor line
+       )
       (beginning-of-line)
       (if (re-search-forward "^ *\\([0-9]+\\)[^0-9]\\([^0-9]\\)" eol t)
          (progn
            (setq number (or number (string-to-int (wl-match-buffer 1))))
            (setq mark (or mark (wl-match-buffer 2)))
-           (setq message-id (cdr (assq number number-alist)))
+           (setq message-id (elmo-message-field
+                             wl-summary-buffer-elmo-folder
+                             number
+                             'message-id))
            (if (string= mark wl-summary-important-mark)
                (progn
                  ;; server side mark
                  (unless no-server-update
-                   (elmo-unmark-important folder (list number) msgdb)
+                   (elmo-folder-unmark-important folder (list number))
                    (elmo-msgdb-global-mark-delete message-id))
+                 ;; Remove cache if local folder.
+                 (save-match-data
+                   (if (and (elmo-folder-local-p folder)
+                            (not (eq 'mark
+                                     (elmo-folder-type-internal folder))))
+                       (elmo-file-cache-delete
+                        (elmo-file-cache-get-path message-id))))
                  (when visible
                    (delete-region (match-beginning 2) (match-end 2))
                    (insert " "))
@@ -4227,7 +4100,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                                             nil)))
              ;; server side mark
              (unless no-server-update
-               (elmo-mark-as-important folder (list number) msgdb))
+               (elmo-folder-mark-as-important folder (list number)))
              (when visible
                (delete-region (match-beginning 2) (match-end 2))
                (insert wl-summary-important-mark))
@@ -4236,12 +4109,9 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                                         (string-to-int (wl-match-buffer 1))
                                         wl-summary-important-mark))
              ;; Force cache message!!
-             (save-match-data
-               (unless (elmo-cache-exists-p message-id)
-                 (elmo-force-cache-msg folder number message-id
-                                       (elmo-msgdb-get-location msgdb))))
+             (elmo-message-encache folder number)
              (unless no-server-update
-               (elmo-msgdb-global-mark-set message-id 
+               (elmo-msgdb-global-mark-set message-id
                                            wl-summary-important-mark)))
            (elmo-msgdb-set-mark-alist msgdb mark-alist)
            (wl-summary-set-mark-modified)))
@@ -4252,7 +4122,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
 
 (defsubst wl-summary-format-date (date-string)
   (condition-case nil
-      (let ((datevec (timezone-fix-time date-string nil 
+      (let ((datevec (timezone-fix-time date-string nil
                                        wl-summary-fix-timezone)))
        (format "%02d/%02d(%s)%02d:%02d"
                (aref datevec 1)
@@ -4264,12 +4134,12 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                (aref datevec 4)))
     (error "??/??(??)??:??")))
 
-(defun wl-summary-overview-create-summary-line (msg 
-                                               entity 
-                                               parent-entity 
+(defun wl-summary-overview-create-summary-line (msg
+                                               entity
+                                               parent-entity
                                                depth
                                                mark-alist
-                                               &optional 
+                                               &optional
                                                children-num
                                                temp-mark thr-entity
                                                subject-differ)
@@ -4280,20 +4150,22 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
        mark line
        (elmo-lang wl-summary-buffer-weekday-name-lang)
        (children-num (if children-num (int-to-string children-num)))
-       (thr-str ""))
-    (if thr-entity
-       (setq thr-str (wl-thread-make-indent-string thr-entity)))
+       (thr-str "")
+       linked)
+    (when thr-entity
+      (setq thr-str (wl-thread-make-indent-string thr-entity))
+      (setq linked (wl-thread-entity-get-linked thr-entity)))
     (if (string= thr-str "")
        (setq no-parent t)) ; no parent
-    (if (and wl-summary-width 
+    (if (and wl-summary-width
             wl-summary-indent-length-limit
             (< wl-summary-indent-length-limit
                (string-width thr-str)))
-       (setq thr-str (wl-set-string-width 
+       (setq thr-str (wl-set-string-width
                       wl-summary-indent-length-limit
                       thr-str)))
-    (setq from 
-         (wl-set-string-width 
+    (setq from
+         (wl-set-string-width
           (if children-num
               (- wl-from-width (length children-num) 2)
             wl-from-width)
@@ -4302,20 +4174,20 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                              (elmo-msgdb-overview-entity-get-from entity)))))
     (setq subject
          (elmo-delete-char ?\n
-                           (or (elmo-msgdb-overview-entity-get-subject 
+                           (or (elmo-msgdb-overview-entity-get-subject
                                 entity)
                                wl-summary-no-subject-message)))
-    (setq parent-raw-subject 
+    (setq parent-raw-subject
          (elmo-msgdb-overview-entity-get-subject parent-entity))
-    (setq parent-subject 
-         (if parent-raw-subject 
+    (setq parent-subject
+         (if parent-raw-subject
              (elmo-delete-char ?\n parent-raw-subject)))
     (setq mark (or (cadr (assq msg mark-alist)) " "))
-    (setq line 
-         (concat 
+    (setq line
+         (concat
           (setq before-indent
-                (format (concat "%" 
-                                (int-to-string 
+                (format (concat "%"
+                                (int-to-string
                                  wl-summary-buffer-number-column)
                                 "s%s%s%s %s")
                         msg
@@ -4324,20 +4196,28 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                         (wl-summary-format-date
                          (elmo-msgdb-overview-entity-get-date entity))
                         (if thr-str thr-str "")))
-          (format "[%s ] %s"
+          (format (if linked
+                      "<%s > %s"
+                    "[%s ] %s")
                   (if children-num
                       (concat "+" children-num ": " from)
                     (concat " " from))
-                  (if (or no-parent
-                          (null parent-subject)
-                          (not (wl-summary-subject-equal 
-                                subject parent-subject)))
-                      (wl-summary-subject-func-internal subject) ""))))
-    (if wl-summary-width (setq line 
-                              (wl-set-string-width 
+                  (progn
+                    (setq subject
+                          (if (or no-parent
+                                  (null parent-subject)
+                                  (not (wl-summary-subject-equal
+                                        subject parent-subject)))
+                              (wl-summary-subject-func-internal subject) ""))
+                    (if (and (not wl-summary-width)
+                             wl-subject-length-limit)
+                        (truncate-string subject wl-subject-length-limit)
+                      subject)))))
+    (if wl-summary-width (setq line
+                              (wl-set-string-width
                                (- wl-summary-width 1) line)))
     (if wl-summary-highlight
-       (wl-highlight-summary-line-string line 
+       (wl-highlight-summary-line-string line
                                          mark
                                          temp-mark
                                          thr-str))
@@ -4346,136 +4226,110 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
 (defsubst wl-summary-buffer-number-column-detect (update)
   (let (end)
     (save-excursion
+      (goto-char (point-min))
       (setq wl-summary-buffer-number-column
            (or
-            (if (and update 
-                     (setq end (if (re-search-forward "^ *[0-9]+[^0-9]" nil t)
+            (if (and update
+                     (setq end (if (re-search-forward "^ *[0-9-]+[^0-9]" nil t)
                                    (point))))
                 (- end (progn (beginning-of-line) (point)) 1))
             (wl-get-assoc-list-value wl-summary-number-column-alist
-                                     wl-summary-buffer-folder-name)
+                                     (wl-summary-buffer-folder-name))
             wl-summary-default-number-column))
       (setq wl-summary-buffer-number-regexp
            (wl-repeat-string "." wl-summary-buffer-number-column)))))
-       
+
 (defsubst wl-summary-proc-wday (wday-str year month mday)
   (save-match-data
     (if (string-match "\\([A-Z][a-z][a-z]\\).*" wday-str)
        (wl-match-string 1 wday-str)
       (elmo-date-get-week year month mday))))
 
-(defmacro wl-summary-cursor-move-regex ()
-  (` (let ((mark-alist
-           (if (elmo-folder-plugged-p wl-summary-buffer-folder-name)
-               (cond ((eq wl-summary-move-order 'new)
-                      (list
-                       (list
-                        wl-summary-new-mark)
-                       (list
-                        wl-summary-unread-uncached-mark
-                        wl-summary-unread-cached-mark
-                        wl-summary-important-mark)))
-                     ((eq wl-summary-move-order 'unread)
-                      (list
-                      (list
-                       wl-summary-unread-uncached-mark
-                       wl-summary-unread-cached-mark
-                       wl-summary-new-mark)
-                      (list
-                       wl-summary-important-mark)))
-                     (t
-                      (list
-                      (list
-                       wl-summary-unread-uncached-mark
-                       wl-summary-unread-cached-mark
-                       wl-summary-new-mark
-                       wl-summary-important-mark))))
-             (cond ((eq wl-summary-move-order 'unread)
-                    (list
-                    (list
-                     wl-summary-unread-cached-mark)
-                    (list
-                     wl-summary-important-mark)))
-                   (t
-                    (list
-                    (list
-                     wl-summary-unread-cached-mark
-                     wl-summary-important-mark)))))))
-       (mapcar
-       (function
-        (lambda (mark-list)
-          (concat wl-summary-message-regexp
-                  ".\\("
-                  (mapconcat 'regexp-quote
-                             mark-list
-                             "\\|")
-                  "\\)\\|"
-                  wl-summary-message-regexp "\\*")))
-       mark-alist))))
-
-;;
-;; Goto unread or important
-;; 
-(defun wl-summary-cursor-up (&optional hereto)
-  (interactive "P")
-  (if (and (not wl-summary-buffer-target-mark-list)
-          (eq wl-summary-buffer-view 'thread))
-      (progn
-       (if (eobp)
-           (forward-line -1))
-       (wl-thread-jump-to-prev-unread hereto))
-    (if hereto
-       (end-of-line)
-      (beginning-of-line))
-    (let ((case-fold-search nil)
-         regex-list)
-      (setq regex-list (wl-summary-cursor-move-regex))
-      (catch 'done
-       (while regex-list
-         (when (re-search-backward
-                (car regex-list)
-                nil t nil)
-           (beginning-of-line)
-           (throw 'done t))
-         (setq regex-list (cdr regex-list)))
-       (beginning-of-line)
-       (throw 'done nil)))))
-
+(defvar wl-summary-move-spec-plugged-alist
+  (` ((new . ((t . nil)
+             (p . (, wl-summary-new-mark))
+             (p . (, (wl-regexp-opt
+                      (list wl-summary-unread-uncached-mark
+                            wl-summary-unread-cached-mark))))
+             (p . (, (regexp-quote wl-summary-important-mark)))))
+      (unread . ((t . nil)
+                (p . (, (wl-regexp-opt
+                         (list wl-summary-new-mark
+                               wl-summary-unread-uncached-mark
+                               wl-summary-unread-cached-mark))))
+                (p . (, (regexp-quote wl-summary-important-mark))))))))
+
+(defvar wl-summary-move-spec-unplugged-alist
+  (` ((new . ((t . nil)
+             (p . (, wl-summary-unread-cached-mark))
+             (p . (, (regexp-quote wl-summary-important-mark)))))
+      (unread . ((t . nil)
+                (p . (, wl-summary-unread-cached-mark))
+                (p . (, (regexp-quote wl-summary-important-mark))))))))
+
+(defsubst wl-summary-next-message (num direction hereto)
+  (let ((cur-spec (cdr (assq wl-summary-move-order 
+                            (if (elmo-folder-plugged-p
+                                 wl-summary-buffer-elmo-folder)
+                                wl-summary-move-spec-plugged-alist
+                              wl-summary-move-spec-unplugged-alist))))
+       (nums (memq num (if (eq direction 'up)
+                           (reverse wl-summary-buffer-number-list)
+                         wl-summary-buffer-number-list)))
+       marked-list nums2)
+    (unless hereto (setq nums (cdr nums)))
+    (setq nums2 nums)
+    (if cur-spec
+       (catch 'done
+         (while cur-spec
+           (setq nums nums2)
+           (cond ((eq (car (car cur-spec)) 'p)
+                  (if (setq marked-list (elmo-folder-list-messages-mark-match
+                                         wl-summary-buffer-elmo-folder
+                                         (cdr (car cur-spec))))
+                      (while nums
+                        (if (memq (car nums) marked-list)
+                            (throw 'done (car nums)))
+                        (setq nums (cdr nums)))))
+                 ((eq (car (car cur-spec)) 't)
+                  (if wl-summary-buffer-target-mark-list
+                      (while nums
+                        (if (memq (car nums)
+                                  wl-summary-buffer-target-mark-list)
+                            (throw 'done (car nums)))
+                        (setq nums (cdr nums))))))
+           (setq cur-spec (cdr cur-spec))))
+      (car nums))))
+
+(defsubst wl-summary-cursor-move (direction hereto)
+  (when (and (eq direction 'up)
+            (eobp))
+    (forward-line -1)
+    (setq hereto t))
+  (let (num)
+    (when (setq num (wl-summary-next-message (wl-summary-message-number)
+                                            direction hereto))
+      (wl-thread-jump-to-msg num)
+      t)))
 ;;
 ;; Goto unread or important
 ;; returns t if next message exists in this folder.
 (defun wl-summary-cursor-down (&optional hereto)
   (interactive "P")
-  (if (and (null wl-summary-buffer-target-mark-list)
-          (eq wl-summary-buffer-view 'thread))
-      (wl-thread-jump-to-next-unread hereto)
-    (if hereto
-       (beginning-of-line)
-      (end-of-line))
-    (let ((case-fold-search nil)
-         regex-list)
-      (setq regex-list (wl-summary-cursor-move-regex))
-      (catch 'done
-       (while regex-list
-         (when (re-search-forward
-                (car regex-list)
-                nil t nil)
-           (beginning-of-line)
-           (throw 'done t))
-         (setq regex-list (cdr regex-list)))
-       (beginning-of-line)
-       (throw 'done nil)))))
+  (wl-summary-cursor-move 'down hereto))
+
+(defun wl-summary-cursor-up (&optional hereto)
+  (interactive "P")
+  (wl-summary-cursor-move 'up hereto))
 
-(defun wl-summary-save-view-cache (&optional keep-current-buffer)
+(defun wl-summary-save-view-cache ()
   (save-excursion
-    (let* ((dir (elmo-msgdb-expand-path wl-summary-buffer-folder-name))
+    (let* ((dir (elmo-folder-msgdb-path wl-summary-buffer-elmo-folder))
           (cache (expand-file-name wl-summary-cache-file dir))
           (view (expand-file-name wl-summary-view-file dir))
-          ;;(coding-system-for-write wl-cs-cache)
-          ;;(output-coding-system wl-cs-cache)
           (save-view wl-summary-buffer-view)
-          (tmp-buffer(get-buffer-create " *wl-summary-save-view-cache*"))
-          charset)
+          (tmp-buffer (get-buffer-create " *wl-summary-save-view-cache*"))
+          (charset wl-summary-buffer-mime-charset))
       (if (file-directory-p dir)
          (); ok.
        (if (file-exists-p dir)
@@ -4486,27 +4340,13 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       (unwind-protect
          (progn
            (when (file-writable-p cache)
-             (if keep-current-buffer
-                 (progn
-                   (save-excursion
-                     (set-buffer tmp-buffer)
-                     (erase-buffer))
-                   (setq charset wl-summary-buffer-mime-charset)
-                   (copy-to-buffer tmp-buffer (point-min) (point-max))
-                   (save-excursion
-                     (set-buffer tmp-buffer)
-                     (widen)
-                     (encode-mime-charset-region 
-                      (point-min) (point-max) charset)
-                     (as-binary-output-file
-                      (write-region (point-min)
-                                    (point-max) cache nil 'no-msg))))
-               (let (buffer-read-only)
-                 (widen)
-                 (encode-mime-charset-region (point-min) (point-max)
-                                             wl-summary-buffer-mime-charset)
-                 (as-binary-output-file
-                  (write-region (point-min) (point-max) cache nil 'no-msg)))))
+             (copy-to-buffer tmp-buffer (point-min) (point-max))
+             (with-current-buffer tmp-buffer
+               (widen)
+               (encode-mime-charset-region
+                (point-min) (point-max) charset)
+               (write-region-as-binary (point-min)(point-max)
+                                       cache nil 'no-msg)))
            (when (file-writable-p view) ; 'thread or 'sequence
              (save-excursion
                (set-buffer tmp-buffer)
@@ -4520,20 +4360,20 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
 (defsubst wl-summary-get-sync-range (folder)
   (intern (or (and
               (elmo-folder-plugged-p folder)
-              (wl-get-assoc-list-value 
+              (wl-get-assoc-list-value
                wl-folder-sync-range-alist
-               folder))
+               (elmo-folder-name-internal folder)))
              wl-default-sync-range)))
 
 ;; redefined for wl-summary-sync-update
 (defun wl-summary-input-range (folder)
   "returns update or all or rescan."
   ;; for the case when parts are expanded in the bottom of the folder
-  (let ((input-range-list '("update" "all" "rescan" "first:" "last:" 
-                           "no-sync" "rescan-noscore"))
-       (default (or (wl-get-assoc-list-value 
+  (let ((input-range-list '("update" "all" "rescan" "first:" "last:"
+                           "no-sync" "rescan-noscore" "all-visible"))
+       (default (or (wl-get-assoc-list-value
                      wl-folder-sync-range-alist
-                     folder) 
+                     folder)
                     wl-default-sync-range))
        range)
     (setq range
@@ -4547,11 +4387,10 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
 
 (defun wl-summary-toggle-disp-folder (&optional arg)
   (interactive)
-  (let (fld-buf fld-win
-       (view-message-buffer (wl-message-get-buffer-create))
-       (cur-buf (current-buffer))
-       (summary-win (get-buffer-window (current-buffer))))
-    (cond 
+  (let ((cur-buf (current-buffer))
+       (summary-win (get-buffer-window (current-buffer)))
+       fld-buf fld-win)
+    (cond
      ((eq arg 'on)
       (setq wl-summary-buffer-disp-folder t)
       ;; hide your folder window
@@ -4561,8 +4400,9 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
      ((eq arg 'off)
       (setq wl-summary-buffer-disp-folder nil)
       ;; hide your wl-message window!
-      (wl-select-buffer view-message-buffer)
-      (delete-window)
+      (when (buffer-live-p wl-message-buffer)
+       (wl-message-select-buffer wl-message-buffer)
+       (delete-window))
       (select-window (get-buffer-window cur-buf))
       ;; display wl-folder window!!
       (if (setq fld-buf (get-buffer wl-folder-buffer-name))
@@ -4581,12 +4421,13 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       (switch-to-buffer cur-buf))
      (t
       (if (setq fld-buf (get-buffer wl-folder-buffer-name))
-         (if (setq fld-win (get-buffer-window fld-buf))      
+         (if (setq fld-win (get-buffer-window fld-buf))
              (setq wl-summary-buffer-disp-folder nil)
            (setq wl-summary-buffer-disp-folder t)))
       (if (not wl-summary-buffer-disp-folder)
          ;; hide message window
-         (let ((mes-win (get-buffer-window view-message-buffer))
+         (let ((mes-win (and wl-message-buffer
+                             (get-buffer-window wl-message-buffer)))
                (wl-stay-folder-window t))
            (if mes-win (delete-window mes-win))
            ;; hide your folder window
@@ -4599,82 +4440,86 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
            (run-hooks 'wl-summary-toggle-disp-folder-off-hook)
            ;; resume message window.
            (when mes-win
-             (wl-select-buffer view-message-buffer)
+             (wl-message-select-buffer wl-message-buffer)
              (run-hooks 'wl-summary-toggle-disp-folder-message-resumed-hook)
              (select-window (get-buffer-window cur-buf)))
            )
-       (save-excursion
-         ;; hide message window
-         (let ((mes-win (get-buffer-window view-message-buffer))
-               (wl-stay-folder-window t))
-           (if mes-win (delete-window mes-win))
-           (select-window (get-buffer-window cur-buf))
-           ;; display wl-folder window!!
-           (if (setq fld-buf (get-buffer wl-folder-buffer-name))
-               (if (setq fld-win (get-buffer-window fld-buf))
-                   ;; folder win is already displayed.
-                   (select-window fld-win)
-                 ;; folder win is not displayed...occupy all.
-                 (switch-to-buffer fld-buf))
-             ;; no folder buf
-             (wl-folder))
-           (split-window-horizontally wl-folder-window-width)
-           (other-window 1)
-           (switch-to-buffer cur-buf)
-           ;; resume message window.
-           (run-hooks 'wl-summary-toggle-disp-folder-on-hook)
-           (when mes-win
-             (wl-select-buffer view-message-buffer)
-             (run-hooks 'wl-summary-toggle-disp-folder-message-resumed-hook)
-             (select-window (get-buffer-window cur-buf))))
-         )))))
+       ;; hide message window
+       (let ((wl-stay-folder-window t)
+             (mes-win (and wl-message-buffer
+                           (get-buffer-window wl-message-buffer))))
+         (if mes-win (delete-window mes-win))
+         (select-window (get-buffer-window cur-buf))
+         ;; display wl-folder window!!
+         (if (setq fld-buf (get-buffer wl-folder-buffer-name))
+             (if (setq fld-win (get-buffer-window fld-buf))
+                 ;; folder win is already displayed.
+                 (select-window fld-win)
+               ;; folder win is not displayed...occupy all.
+               (switch-to-buffer fld-buf))
+           ;; no folder buf
+           (wl-folder))
+         (split-window-horizontally wl-folder-window-width)
+         (other-window 1)
+         (switch-to-buffer cur-buf)
+         ;; resume message window.
+         (run-hooks 'wl-summary-toggle-disp-folder-on-hook)
+         (when mes-win
+           (wl-message-select-buffer wl-message-buffer)
+           (run-hooks 'wl-summary-toggle-disp-folder-message-resumed-hook)
+           (select-window (get-buffer-window cur-buf))))
+       ))))
   (run-hooks 'wl-summary-toggle-disp-folder-hook))
-  
+
 (defun wl-summary-toggle-disp-msg (&optional arg)
   (interactive)
-  (let (fld-buf fld-win
-       (view-message-buffer (wl-message-get-buffer-create))
-       (cur-buf (current-buffer))
+  (let ((cur-buf (current-buffer))
+       fld-buf fld-win
        summary-win)
-    (cond 
+    (cond
      ((eq arg 'on)
       (setq wl-summary-buffer-disp-msg t)
-      ;; hide your folder window
-      (if (and (not wl-stay-folder-window)
-              (setq fld-buf (get-buffer wl-folder-buffer-name)))
-         (if (setq fld-win (get-buffer-window fld-buf))
-             (delete-window fld-win))))
+      (save-excursion
+       ;; hide your folder window
+       (if (and (not wl-stay-folder-window)
+                (setq fld-buf (get-buffer wl-folder-buffer-name)))
+           (if (setq fld-win (get-buffer-window fld-buf))
+               (unless (one-window-p fld-win)
+                 (delete-window fld-win))))))
      ((eq arg 'off)
       (wl-delete-all-overlays)
       (setq wl-summary-buffer-disp-msg nil)
       (save-excursion
-        (wl-select-buffer view-message-buffer)
-        (delete-window)
-       (and (get-buffer-window cur-buf)
-            (select-window (get-buffer-window cur-buf)))
+       (when (buffer-live-p wl-message-buffer)
+         (wl-message-select-buffer wl-message-buffer)
+         (delete-window)
+         (and (get-buffer-window cur-buf)
+              (select-window (get-buffer-window cur-buf))))
         (run-hooks 'wl-summary-toggle-disp-off-hook)))
      (t
-      (if (get-buffer-window view-message-buffer) ; already displayed
+      (if (and wl-message-buffer
+              (get-buffer-window wl-message-buffer)) ; already displayed
          (setq wl-summary-buffer-disp-msg nil)
        (setq wl-summary-buffer-disp-msg t))
       (if wl-summary-buffer-disp-msg
          (progn
            (wl-summary-redisplay)
-           ;; hide your folder window
-;;             (setq fld-buf (get-buffer wl-folder-buffer-name))
-;;             (if (setq fld-win (get-buffer-window fld-buf))
-;;                 (delete-window fld-win)))       
+;;; hide your folder window
+;;;        (setq fld-buf (get-buffer wl-folder-buffer-name))
+;;;        (if (setq fld-win (get-buffer-window fld-buf))
+;;;            (delete-window fld-win)))
            (run-hooks 'wl-summary-toggle-disp-on-hook))
        (wl-delete-all-overlays)
        (save-excursion
-         (wl-select-buffer view-message-buffer)
+         (wl-message-select-buffer wl-message-buffer)
          (delete-window)
          (select-window (get-buffer-window cur-buf))
          (run-hooks 'wl-summary-toggle-disp-off-hook))
-       ;;(switch-to-buffer cur-buf)
+;;;    (switch-to-buffer cur-buf)
        )))))
 
 (defun wl-summary-next-line-content ()
+  "Show next line of the message."
   (interactive)
   (let ((cur-buf (current-buffer)))
     (wl-summary-toggle-disp-msg 'on)
@@ -4697,39 +4542,33 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
 (defun wl-summary-prev-page ()
   (interactive)
   (wl-message-prev-page))
-  
-(defsubst wl-summary-no-mime-p (folder)
-  (wl-string-match-member folder wl-summary-no-mime-folder-list))
 
-(defun wl-summary-set-message-buffer-or-redisplay (&optional ignore-original)
-  ;; if current message is not displayed, display it.
-  ;; return t if exists.
-  (let ((folder wl-summary-buffer-folder-name)
+(defsubst wl-summary-no-mime-p (folder)
+  (wl-string-match-member (elmo-folder-name-internal folder)
+                         wl-summary-no-mime-folder-list))
+
+(defun wl-summary-set-message-buffer-or-redisplay (&rest args)
+  "Set message buffer.
+If message is not displayed yet, display it.
+Return t if message exists."
+  (let ((folder wl-summary-buffer-elmo-folder)
        (number (wl-summary-message-number))
-       cur-folder cur-number message-last-pos
-       (view-message-buffer (wl-message-get-buffer-create)))
-    (save-excursion
-      (set-buffer view-message-buffer)
-      (setq cur-folder wl-message-buffer-cur-folder)
-      (setq cur-number wl-message-buffer-cur-number))
-    (if (and (not ignore-original)
-            (not 
-             (and (eq number (wl-message-original-buffer-number))
-                  (string= folder (wl-message-original-buffer-folder)))))
+       cur-folder cur-number message-last-pos)
+    (when (buffer-live-p wl-message-buffer)
+      (save-window-excursion
+       (wl-message-select-buffer wl-message-buffer)
+       (setq cur-folder wl-message-buffer-cur-folder)
+       (setq cur-number wl-message-buffer-cur-number)))
+    (if (and (string= (elmo-folder-name-internal folder) (or cur-folder ""))
+            (eq number (or cur-number 0)))
        (progn
-         (if (wl-summary-no-mime-p folder)
-             (wl-summary-redisplay-no-mime folder number)
-           (wl-summary-redisplay-internal folder number))
-         nil)
-      (if (and (string= folder (or cur-folder ""))
-              (eq number (or cur-number 0)))
-         (progn
-           (set-buffer view-message-buffer)
-           t)
-       (if (wl-summary-no-mime-p folder)
-           (wl-summary-redisplay-no-mime folder number)
-         (wl-summary-redisplay-internal folder number))
-       nil))))
+         (set-buffer wl-message-buffer)
+         t)
+      (if (wl-summary-no-mime-p folder)
+         (wl-summary-redisplay-no-mime folder number)
+       (wl-summary-redisplay-internal folder number))
+      (set-buffer wl-message-buffer)
+      nil)))
 
 (defun wl-summary-target-mark-forward (&optional arg)
   (interactive "P")
@@ -4771,11 +4610,12 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
     (wl-summary-jump-to-msg (car mlist))
     (wl-summary-reply arg t)
     (goto-char (point-max))
-    (setq start-point (point))
+    (setq start-point (point-marker))
     (setq draft-buf (current-buffer))
     (save-window-excursion
       (while mlist
        (set-buffer summary-buf)
+       (delete-other-windows)
        (wl-summary-jump-to-msg (car mlist))
        (wl-summary-redisplay)
        (set-buffer draft-buf)
@@ -4790,8 +4630,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
 
 (defun wl-summary-reply-with-citation (&optional arg)
   (interactive "P")
-  (unwind-protect
-      (wl-summary-reply arg t)
+  (when (wl-summary-reply arg t)
     (goto-char (point-max))
     (wl-draft-yank-original)
     (run-hooks 'wl-mail-setup-hook)))
@@ -4800,104 +4639,106 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
   (interactive)
   (let* ((original (wl-summary-message-number))
         (msgid (elmo-string (or id (read-from-minibuffer "Message-ID: "))))
-        (number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
+        (number-alist (elmo-msgdb-get-number-alist (wl-summary-buffer-msgdb)))
         msg otherfld schar
         (errmsg
          (format "No message with id \"%s\" in the folder." msgid)))
     (if (setq msg (car (rassoc msgid number-alist)))
-       ;;(wl-summary-jump-to-msg-internal
-       ;;wl-summary-buffer-folder-name msg 'no-sync)
+;;;    (wl-summary-jump-to-msg-internal
+;;;     (wl-summary-buffer-folder-name) msg 'no-sync)
        (progn
          (wl-thread-jump-to-msg msg)
          t)
       ;; for XEmacs!
       (if (and elmo-use-database
-              (setq errmsg
-                    (format 
+              (setq errmsg
+                    (format
                      "No message with id \"%s\" in the database." msgid))
-              (setq otherfld (elmo-database-msgid-get msgid)))
+              (setq otherfld (elmo-database-msgid-get msgid)))
          (if (cdr (wl-summary-jump-to-msg-internal
                    (car otherfld) (nth 1 otherfld) 'no-sync))
              t ; succeed.
            ;; Back to original.
            (wl-summary-jump-to-msg-internal
-            wl-summary-buffer-folder-name original 'no-sync))
-       (cond ((eq wl-summary-search-via-nntp 'confirm)
+            (wl-summary-buffer-folder-name) original 'no-sync))
+       (cond ((eq wl-summary-search-via-nntp 'confirm)
+              (require 'elmo-nntp)
               (message "Search message in nntp server \"%s\" <y/n/s(elect)>?"
-                       elmo-default-nntp-server)
+                       elmo-nntp-default-server)
               (setq schar (read-char))
               (cond ((eq schar ?y)
                      (wl-summary-jump-to-msg-by-message-id-via-nntp msgid))
                     ((eq schar ?s)
                      (wl-summary-jump-to-msg-by-message-id-via-nntp
-                      msgid 
+                      msgid
                       (read-from-minibuffer "NNTP Server: ")))
                     (t
                      (message errmsg)
                      nil)))
              (wl-summary-search-via-nntp
-              (wl-summary-jump-to-msg-by-message-id-via-nntp msgid))
-             (t
-              (message errmsg)
-              nil))))))
+              (wl-summary-jump-to-msg-by-message-id-via-nntp msgid))
+             (t
+              (message errmsg)
+              nil))))))
 
 (defun wl-summary-jump-to-msg-by-message-id-via-nntp (&optional id server-spec)
   (interactive)
   (let* ((msgid (elmo-string (or id (read-from-minibuffer "Message-ID: "))))
-        newsgroups folder ret
-        user server port ssl spec)
+        newsgroups folder ret
+        user server port type spec)
     (if server-spec
        (if (string-match "^-" server-spec)
-           (setq spec (elmo-nntp-get-spec server-spec)
-                 user (nth 2 spec)
-                 server (nth 3 spec)
-                 port (nth 4 spec)
-                 ssl (nth 5 spec))
+           (setq spec (wl-folder-get-elmo-folder server-spec)
+                 user (elmo-net-folder-user-internal spec)
+                 server (elmo-net-folder-server-internal spec)
+                 port (elmo-net-folder-port-internal spec)
+                 type (elmo-net-folder-stream-type-internal spec))
          (setq server server-spec)))
     (when (setq ret (elmo-nntp-get-newsgroup-by-msgid
                     msgid
-                    (or server elmo-default-nntp-server)
-                    (or user elmo-default-nntp-user)
-                    (or port elmo-default-nntp-port)
-                    (or ssl elmo-default-nntp-ssl)))
-      (setq newsgroups (wl-parse-newsgroups ret))
+                    (or server elmo-nntp-default-server)
+                    (or user elmo-nntp-default-user)
+                    (or port elmo-nntp-default-port)
+                    (or type elmo-nntp-default-stream-type)))
+      (setq newsgroups (elmo-nntp-parse-newsgroups ret))
       (setq folder (concat "-" (car newsgroups)
-                          (elmo-nntp-folder-postfix user server port ssl)))
+                          (elmo-nntp-folder-postfix user server port type)))
       (catch 'found
        (while newsgroups
          (if (wl-folder-entity-exists-p (car newsgroups)
                                         wl-folder-newsgroups-hashtb)
              (throw 'found
                     (setq folder (concat "-" (car newsgroups)
-                                         (elmo-nntp-folder-postfix user server port ssl)))))
+                                         (elmo-nntp-folder-postfix
+                                          user server port type)))))
          (setq newsgroups (cdr newsgroups)))))
     (if ret
        (wl-summary-jump-to-msg-internal folder nil 'update msgid)
       (message "No message id \"%s\" in nntp server \"%s\"."
-              msgid (or server elmo-default-nntp-server))
+              msgid (or server elmo-nntp-default-server))
       nil)))
 
 (defun wl-summary-jump-to-msg-internal (folder msg scan-type &optional msgid)
   (let (wl-auto-select-first entity)
-    (if (or (string= folder wl-summary-buffer-folder-name)
-           (y-or-n-p 
-            (format
-             "Message was found in the folder \"%s\". Jump to it? "
-             folder)))
-       (progn
-         (unwind-protect
-             (wl-summary-goto-folder-subr
-              folder scan-type nil nil t)
-           (if msgid
-               (setq msg
-                     (car (rassoc msgid
-                                  (elmo-msgdb-get-number-alist
-                                   wl-summary-buffer-msgdb)))))
+    (if (or (string= folder (wl-summary-buffer-folder-name))
+           (y-or-n-p
+            (format
+             "Message was found in the folder \"%s\". Jump to it? "
+             folder)))
+       (progn
+         (unwind-protect
+             (wl-summary-goto-folder-subr
+              folder scan-type nil nil t)
+           (if msgid
+               (setq msg
+                     (car (rassoc msgid
+                                  (elmo-msgdb-get-number-alist
+                                   (wl-summary-buffer-msgdb))))))
            (setq entity (wl-folder-search-entity-by-name folder
                                                          wl-folder-entity
                                                          'folder))
            (if entity
-               (wl-folder-set-current-entity-id 
+               (wl-folder-set-current-entity-id
                 (wl-folder-get-entity-id entity))))
          (if (null msg)
              (message "Message was not found currently in this folder.")
@@ -4907,128 +4748,166 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
 (defun wl-summary-jump-to-parent-message (arg)
   (interactive "P")
   (let ((cur-buf (current-buffer))
+       (number (wl-summary-message-number))
        (regexp "\\(<[^<>]*>\\)[ \t]*$")
        (i -1) ;; xxx
-       msg-id ref-list ref irt)
-    (wl-summary-set-message-buffer-or-redisplay)
-    (set-buffer (wl-message-get-original-buffer))
-    (message "Searching parent message...")
-    (setq ref (std11-field-body "References")
-         irt (std11-field-body "In-Reply-To"))
-    (cond
-     ((and arg (not (numberp arg)) ref (not (string= ref ""))
-          (string-match regexp ref))
-      ;; The first message of the thread.
-      (setq msg-id (wl-match-string 1 ref)))
-     ;; "In-Reply-To:" has only one msg-id.
-     ((and irt (not (string= irt ""))
-          (string-match regexp irt))
-      (setq msg-id (wl-match-string 1 irt)))
-     ((and (or (null arg) (numberp arg)) ref (not (string= ref ""))
-          (string-match regexp ref))
-      ;; "^" searching parent, "C-u 2 ^" looking for grandparent.
-      (while (string-match regexp ref)
-       (setq ref-list
-             (append (list
-                      (wl-match-string 1 ref))
-                     ref-list))
-       (setq ref (substring ref (match-end 0)))
-       (setq i (1+ i)))
-      (setq msg-id
-            (if (null arg) (nth 0 ref-list) ;; previous
-              (if (<= arg i) (nth (1- arg) ref-list)
-                (nth i ref-list))))))
-    (set-buffer cur-buf)
-    (cond ((null msg-id)
-          (message "No parent message!")
-          nil)
-         ((wl-summary-jump-to-msg-by-message-id msg-id)
-          (wl-summary-redisplay)
-          (message "Searching parent message...done.")
-          t)
-         (t ; failed.
-          (message "Parent message was not found.")
-          nil))))
+       msg-id msg-num ref-list ref irt)
+    (if (null number)
+       (message "No message.")
+      (when (eq wl-summary-buffer-view 'thread)
+       (cond ((and arg (not (numberp arg)))
+              (setq msg-num
+                    (wl-thread-entity-get-number
+                     (wl-thread-entity-get-top-entity
+                      (wl-thread-get-entity number)))))
+             ((and arg (numberp arg))
+              (setq i 0)
+              (setq msg-num number)
+              (while (< i arg)
+                (setq msg-num
+                      (wl-thread-entity-get-number
+                       (wl-thread-entity-get-parent-entity
+                        (wl-thread-get-entity msg-num))))
+                (setq i (1+ i))))
+             (t (setq msg-num
+                      (wl-thread-entity-get-number
+                       (wl-thread-entity-get-parent-entity
+                        (wl-thread-get-entity number)))))))
+      (when (null msg-num)
+       (wl-summary-set-message-buffer-or-redisplay)
+       (set-buffer (wl-message-get-original-buffer))
+       (message "Searching parent message...")
+       (setq ref (std11-field-body "References")
+             irt (std11-field-body "In-Reply-To"))
+       (cond
+        ((and arg (not (numberp arg)) ref (not (string= ref ""))
+              (string-match regexp ref))
+         ;; The first message of the thread.
+         (setq msg-id (wl-match-string 1 ref)))
+        ;; "In-Reply-To:" has only one msg-id.
+        ((and (null arg) irt (not (string= irt ""))
+              (string-match regexp irt))
+         (setq msg-id (wl-match-string 1 irt)))
+        ((and (or (null arg) (numberp arg)) ref (not (string= ref ""))
+              (string-match regexp ref))
+         ;; "^" searching parent, "C-u 2 ^" looking for grandparent.
+         (while (string-match regexp ref)
+           (setq ref-list
+                 (append (list
+                          (wl-match-string 1 ref))
+                         ref-list))
+           (setq ref (substring ref (match-end 0)))
+           (setq i (1+ i)))
+         (setq msg-id
+               (if (null arg) (nth 0 ref-list) ;; previous
+                 (if (<= arg i) (nth (1- arg) ref-list)
+                   (nth i ref-list)))))))
+      (set-buffer cur-buf)
+      (cond ((and (null msg-id) (null msg-num))
+            (message "No parent message!")
+            nil)
+           ((and msg-id (wl-summary-jump-to-msg-by-message-id msg-id))
+            (wl-summary-redisplay)
+            (message "Searching parent message...done")
+            t)
+           ((and msg-num (wl-summary-jump-to-msg msg-num))
+            (wl-summary-redisplay)
+            (message "Searching parent message...done")
+            t)
+           (t ; failed.
+            (message "Parent message was not found.")
+            nil)))))
 
 (defun wl-summary-reply (&optional arg without-setup-hook)
   "Reply to current message. Default is \"wide\" reply.
-Reply to author if invoked with argument."
+Reply to author if invoked with ARG."
   (interactive "P")
-  (let ((folder wl-summary-buffer-folder-name)
+  (let ((folder wl-summary-buffer-elmo-folder)
        (number (wl-summary-message-number))
        (summary-buf (current-buffer))
        mes-buf)
-    (wl-summary-redisplay-internal folder number)
-    (wl-select-buffer (get-buffer (setq mes-buf (wl-current-message-buffer))))
-    (set-buffer mes-buf)
-    (goto-char (point-min))
-    (or wl-draft-use-frame
-       (split-window-vertically))
-    (other-window 1)
-    (when (setq mes-buf (wl-message-get-original-buffer))
-      (wl-draft-reply mes-buf (not arg) summary-buf)
-      (unless without-setup-hook
-       (run-hooks 'wl-mail-setup-hook)))))
+    (when number
+      (save-excursion
+       (wl-summary-redisplay-internal folder number))
+      (setq mes-buf wl-message-buffer)
+      (wl-message-select-buffer wl-message-buffer)
+      (set-buffer mes-buf)
+      (goto-char (point-min))
+      (when (setq mes-buf (wl-message-get-original-buffer))
+       (wl-draft-reply mes-buf arg summary-buf)
+       (unless without-setup-hook
+         (run-hooks 'wl-mail-setup-hook)))
+      t)))
 
 (defun wl-summary-write ()
   "Write a new draft from Summary."
   (interactive)
   (wl-draft nil nil nil nil nil
-           nil nil nil nil nil (current-buffer))
+           nil nil nil nil nil nil (current-buffer))
   (run-hooks 'wl-mail-setup-hook)
   (mail-position-on-field "To"))
 
-(defun wl-summary-write-current-newsgroup (&optional folder)
+(defvar wl-summary-write-current-folder-functions
+  '(wl-folder-get-newsgroups
+    wl-folder-guess-mailing-list-by-refile-rule
+    wl-folder-guess-mailing-list-by-folder-name)
+  "Newsgroups or Mailing List address guess functions list.
+Call from `wl-summary-write-current-folder'")
+
+(defun wl-summary-write-current-folder (&optional folder)
+  "Write message to current FOLDER's newsgroup or mailing-list.
+Use function list is `wl-summary-write-current-folder-functions'."
   (interactive)
-  (let* ((folder (or folder wl-summary-buffer-folder-name))
-        (flist (elmo-folder-get-primitive-folder-list folder))
-        newsgroups fld ret)
-    (while (setq fld (car flist))
-      (if (setq ret
-               (cond ((eq 'nntp (elmo-folder-get-type fld))
-                      (nth 1 (elmo-folder-get-spec fld)))
-                     ((eq 'localnews (elmo-folder-get-type fld))
-                      (elmo-replace-in-string
-                       (nth 1 (elmo-folder-get-spec fld)) "/" "\\."))))
-         (setq newsgroups (cond (newsgroups
-                                 (concat newsgroups "," ret))
-                                (t ret))))
-      (setq flist (cdr flist)))
-    (if newsgroups
-       (progn
-         (wl-draft nil nil nil nil nil newsgroups)
-         (run-hooks 'wl-mail-setup-hook))
-      (error "%s is not newsgroup" folder))))
+  (let (newsgroups to cc)
+    ;; default FOLDER is current buffer folder
+    (setq folder (or folder (wl-summary-buffer-folder-name)))
+    (let ((flist wl-summary-write-current-folder-functions)
+         guess-list)
+      (while flist
+       (setq guess-list (funcall (car flist) folder))
+       (if (or (nth 0 guess-list)      ; To:
+;;;            (nth 1 guess-list)      ; Cc:
+               (nth 2 guess-list))     ; Newsgroups:
+           (setq flist nil)
+         (setq flist (cdr flist))))
+      (if guess-list
+         (progn
+           (wl-draft (nth 0 guess-list) ; To:
+                     nil nil
+                     (nth 1 guess-list) ; Cc:
+                     nil               
+                     (nth 2 guess-list)) ; Newsgroups:
+           (run-hooks 'wl-mail-setup-hook))
+;;;    (error "%s is not newsgroup" folder)
+       (error "Can't guess by folder %s" folder)))))
 
 (defun wl-summary-forward (&optional without-setup-hook)
+  ""
   (interactive)
-  (let ((folder wl-summary-buffer-folder-name)
+  (let ((folder wl-summary-buffer-elmo-folder)
        (number (wl-summary-message-number))
        (summary-buf (current-buffer))
        (wl-draft-forward t)
+       mes-buf
        entity subject num)
-    (wl-summary-redisplay-internal folder number)
-    (wl-select-buffer (get-buffer wl-message-buf-name))
-    (or wl-draft-use-frame
-       (split-window-vertically))
-    (other-window 1)
-    ;; get original subject.
-    (if summary-buf
-       (save-excursion 
-         (set-buffer summary-buf)
-         (setq num (wl-summary-message-number))
-         (setq entity (assoc (cdr (assq num 
-                                        (elmo-msgdb-get-number-alist 
-                                         wl-summary-buffer-msgdb)))
-                             (elmo-msgdb-get-overview 
-                              wl-summary-buffer-msgdb)))
-         (and entity
-              (setq subject 
-                    (or (elmo-msgdb-overview-entity-get-subject entity) 
-                        "")))))
-    (wl-draft-forward subject summary-buf)
-    (unless without-setup-hook
-      (run-hooks 'wl-mail-setup-hook))))
+    (if (null number)
+       (message "No message.")
+      (wl-summary-redisplay-internal nil nil 'force-reload)
+      (setq mes-buf wl-message-buffer)
+      (wl-message-select-buffer mes-buf)
+      (or wl-draft-use-frame
+         (split-window-vertically))
+      (other-window 1)
+      ;; get original subject.
+      (if summary-buf
+         (save-excursion
+           (set-buffer summary-buf)
+           (setq subject 
+                 (or (elmo-message-field folder number 'subject) ""))))
+      (set-buffer mes-buf)
+      (wl-draft-forward subject summary-buf)
+      (unless without-setup-hook
+       (run-hooks 'wl-mail-setup-hook)))))
 
 (defun wl-summary-click (e)
   (interactive "e")
@@ -5036,46 +4915,26 @@ Reply to author if invoked with argument."
   (wl-summary-read))
 
 (defun wl-summary-read ()
+  "Proceed reading message in the summary buffer."
   (interactive)
-  (let ((folder wl-summary-buffer-folder-name)
-       (number (wl-summary-message-number))
-       cur-folder cur-number message-last-pos
-       (view-message-buffer (get-buffer-create wl-message-buf-name))
-        (sticky-buf-name (and (wl-summary-sticky-p) wl-message-buf-name))
-        (summary-buf-name (buffer-name)))
-    (save-excursion
-      (set-buffer view-message-buffer)
-      (when (and sticky-buf-name
-                 (not (wl-local-variable-p 'wl-message-buf-name
-                                          (current-buffer))))
-        (make-local-variable 'wl-message-buf-name)
-        (setq wl-message-buf-name sticky-buf-name)
-        (make-local-variable 'wl-message-buffer-cur-summary-buffer)
-        (setq wl-message-buffer-cur-summary-buffer summary-buf-name))
-      (setq cur-folder wl-message-buffer-cur-folder)
-      (setq cur-number wl-message-buffer-cur-number))
+  (let ((cur-buf (current-buffer)))
     (wl-summary-toggle-disp-msg 'on)
-    (if (and (string= folder cur-folder)
-            (eq number cur-number))
-       (progn
-         (if (wl-summary-next-page)
-             (wl-summary-down t)))
-;          (wl-summary-scroll-up-content)))
-      (if (wl-summary-no-mime-p folder)
-         (wl-summary-redisplay-no-mime folder number)
-       (wl-summary-redisplay-internal folder number)))))
+    (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
+      (set-buffer cur-buf)
+      (if (wl-message-next-page)
+         (wl-summary-down t)))))
 
 (defun wl-summary-prev (&optional interactive)
+  ""
   (interactive)
   (if wl-summary-move-direction-toggle
       (setq wl-summary-move-direction-downward nil))
-  (let ((type (elmo-folder-get-type wl-summary-buffer-folder-name))
-       (skip-mark-regexp (mapconcat
+  (let ((skip-mark-regexp (mapconcat
                           'regexp-quote
                           wl-summary-skip-mark-list ""))
        goto-next regex-list regex next-entity finfo)
     (beginning-of-line)
-    (if (elmo-folder-plugged-p wl-summary-buffer-folder-name)
+    (if (elmo-folder-plugged-p wl-summary-buffer-elmo-folder)
        (setq regex (format "^%s[^%s]"
                            wl-summary-buffer-number-regexp
                            skip-mark-regexp))
@@ -5092,28 +4951,29 @@ Reply to author if invoked with argument."
          (if wl-summary-buffer-disp-msg
              (wl-summary-redisplay)))
       (if (or interactive (interactive-p))
-         (progn
+         (if wl-summary-buffer-prev-folder-function
+             (funcall wl-summary-buffer-prev-folder-function)
            (when wl-auto-select-next
              (setq next-entity (wl-summary-get-prev-folder))
              (if next-entity
                  (setq finfo (wl-folder-get-entity-info next-entity))))
-           (wl-ask-folder 
+           (wl-ask-folder
             '(lambda () (wl-summary-next-folder-or-exit next-entity))
             (format
              "No more messages. Type SPC to go to %s."
              (wl-summary-entity-info-msg next-entity finfo))))))))
 
 (defun wl-summary-next (&optional interactive)
+  ""
   (interactive)
   (if wl-summary-move-direction-toggle
       (setq wl-summary-move-direction-downward t))
-  (let ((type (elmo-folder-get-type wl-summary-buffer-folder-name))
-       (skip-mark-regexp (mapconcat
+  (let ((skip-mark-regexp (mapconcat
                           'regexp-quote
                           wl-summary-skip-mark-list ""))
        goto-next regex regex-list next-entity finfo)
     (end-of-line)
-    (if (elmo-folder-plugged-p wl-summary-buffer-folder-name)
+    (if (elmo-folder-plugged-p wl-summary-buffer-elmo-folder)
        (setq regex (format "^%s[^%s]"
                            wl-summary-buffer-number-regexp
                            skip-mark-regexp))
@@ -5121,7 +4981,7 @@ Reply to author if invoked with argument."
                          wl-summary-buffer-number-regexp
                          skip-mark-regexp
                          (regexp-quote wl-summary-unread-cached-mark)
-                         (regexp-quote wl-summary-important-mark))))    
+                         (regexp-quote wl-summary-important-mark))))
     (unless (re-search-forward regex nil t)
       (forward-line 1)
       (setq goto-next t))
@@ -5130,18 +4990,20 @@ Reply to author if invoked with argument."
        (if wl-summary-buffer-disp-msg
            (wl-summary-redisplay))
       (if (or interactive (interactive-p))
-         (progn
+         (if wl-summary-buffer-next-folder-function
+             (funcall wl-summary-buffer-next-folder-function)
            (when wl-auto-select-next
              (setq next-entity (wl-summary-get-next-folder))
              (if next-entity
                  (setq finfo (wl-folder-get-entity-info next-entity))))
-           (wl-ask-folder 
+           (wl-ask-folder
             '(lambda () (wl-summary-next-folder-or-exit next-entity))
             (format
              "No more messages. Type SPC to go to %s."
              (wl-summary-entity-info-msg next-entity finfo))))))))
 
 (defun wl-summary-up (&optional interactive skip-no-unread)
+  ""
   (interactive)
   (if wl-summary-move-direction-toggle
       (setq wl-summary-move-direction-downward nil))
@@ -5150,20 +5012,22 @@ Reply to author if invoked with argument."
          (wl-summary-redisplay))
     (if (or interactive
            (interactive-p))
-       (let (next-entity finfo)
-         (when wl-auto-select-next
-           (progn
-             (setq next-entity (wl-summary-get-prev-unread-folder))
-             (if next-entity
-                 (setq finfo (wl-folder-get-entity-info next-entity)))))
-         (if (and skip-no-unread
-                  (eq wl-auto-select-next 'skip-no-unread))
-             (wl-summary-next-folder-or-exit next-entity t)
-           (wl-ask-folder 
-            '(lambda () (wl-summary-next-folder-or-exit next-entity t))
-            (format
-             "No more unread messages. Type SPC to go to %s."
-             (wl-summary-entity-info-msg next-entity finfo))))))))
+       (if wl-summary-buffer-prev-folder-function
+           (funcall wl-summary-buffer-prev-folder-function)
+         (let (next-entity finfo)
+           (when wl-auto-select-next
+             (progn
+               (setq next-entity (wl-summary-get-prev-unread-folder))
+               (if next-entity
+                   (setq finfo (wl-folder-get-entity-info next-entity)))))
+           (if (and skip-no-unread
+                    (eq wl-auto-select-next 'skip-no-unread))
+               (wl-summary-next-folder-or-exit next-entity t)
+             (wl-ask-folder
+              '(lambda () (wl-summary-next-folder-or-exit next-entity t))
+              (format
+               "No more unread messages. Type SPC to go to %s."
+               (wl-summary-entity-info-msg next-entity finfo)))))))))
 
 (defun wl-summary-get-prev-folder ()
   (let ((folder-buf (get-buffer wl-folder-buffer-name))
@@ -5206,24 +5070,26 @@ Reply to author if invoked with argument."
          (wl-summary-redisplay))
     (if (or interactive
            (interactive-p))
-       (let (next-entity finfo)
-         (when wl-auto-select-next
-           (setq next-entity (wl-summary-get-next-unread-folder))
+       (if wl-summary-buffer-next-folder-function
+           (funcall wl-summary-buffer-next-folder-function)
+         (let (next-entity finfo)
+           (when wl-auto-select-next
+             (setq next-entity (wl-summary-get-next-unread-folder)))
            (if next-entity
-               (setq finfo (wl-folder-get-entity-info next-entity))))
-         (if (and skip-no-unread
-                  (eq wl-auto-select-next 'skip-no-unread))
-             (wl-summary-next-folder-or-exit next-entity)
-           (wl-ask-folder 
-            '(lambda () (wl-summary-next-folder-or-exit next-entity))
-            (format
-             "No more unread messages. Type SPC to go to %s."
-             (wl-summary-entity-info-msg next-entity finfo))))))))
+               (setq finfo (wl-folder-get-entity-info next-entity)))
+           (if (and skip-no-unread
+                    (eq wl-auto-select-next 'skip-no-unread))
+               (wl-summary-next-folder-or-exit next-entity)
+             (wl-ask-folder
+              '(lambda () (wl-summary-next-folder-or-exit next-entity))
+              (format
+               "No more unread messages. Type SPC to go to %s."
+               (wl-summary-entity-info-msg next-entity finfo)))))))))
 
 (defun wl-summary-goto-last-displayed-msg ()
   (interactive)
   (unless wl-summary-buffer-last-displayed-msg
-    (setq wl-summary-buffer-last-displayed-msg 
+    (setq wl-summary-buffer-last-displayed-msg
          wl-summary-buffer-current-msg))
   (if wl-summary-buffer-last-displayed-msg
       (progn
@@ -5235,74 +5101,82 @@ Reply to author if invoked with argument."
 (defun wl-summary-redisplay (&optional arg)
   (interactive "P")
   (if (and (not arg)
-          (wl-summary-no-mime-p wl-summary-buffer-folder-name))
+          (wl-summary-no-mime-p wl-summary-buffer-elmo-folder))
       (wl-summary-redisplay-no-mime)
     (wl-summary-redisplay-internal nil nil arg)))
 
 (defsubst wl-summary-redisplay-internal (&optional folder number force-reload)
   (interactive)
-  (let* ((msgdb wl-summary-buffer-msgdb)
-        (fld (or folder wl-summary-buffer-folder-name))
+  (let* ((msgdb (wl-summary-buffer-msgdb))
+        (folder (or folder wl-summary-buffer-elmo-folder))
         (num (or number (wl-summary-message-number)))
         (wl-mime-charset      wl-summary-buffer-mime-charset)
         (default-mime-charset wl-summary-buffer-mime-charset)
-        (wl-message-redisplay-func 
-         wl-summary-buffer-message-redisplay-func)
         fld-buf fld-win thr-entity)
     (if (and wl-thread-open-reading-thread
             (eq wl-summary-buffer-view 'thread)
-            (not (wl-thread-entity-get-opened 
-                  (setq thr-entity (wl-thread-get-entity 
+            (not (wl-thread-entity-get-opened
+                  (setq thr-entity (wl-thread-get-entity
                                     num))))
             (wl-thread-entity-get-children thr-entity))
        (wl-thread-force-open))
     (if num
        (progn
          (setq wl-summary-buffer-disp-msg t)
-         (setq wl-summary-buffer-last-displayed-msg 
+         (setq wl-summary-buffer-last-displayed-msg
                wl-summary-buffer-current-msg)
          ;; hide folder window
          (if (and (not wl-stay-folder-window)
                   (setq fld-buf (get-buffer wl-folder-buffer-name)))
              (if (setq fld-win (get-buffer-window fld-buf))
-                 (delete-window fld-win)))       
+                 (delete-window fld-win)))
           (setq wl-current-summary-buffer (current-buffer))
-         (if (wl-message-redisplay fld num 'mime msgdb force-reload) 
-             (wl-summary-mark-as-read nil
-                                      ;; cached, then change server-mark.
-                                      (if wl-message-cache-used
-                                          nil
-                                        ;; plugged, then leave server-mark.
-                                        (if (elmo-folder-plugged-p
-                                             wl-summary-buffer-folder-name)
-                                            'leave))
-                                      t) ;; displayed
-           )
+         (wl-summary-mark-as-read
+          nil
+          ;; not fetched, then change server-mark.
+          (if (wl-message-redisplay folder num 'mime force-reload)
+              nil
+            ;; plugged, then leave server-mark.
+            (if (and
+                 (not
+                  (elmo-folder-local-p
+                   wl-summary-buffer-elmo-folder))
+                 (elmo-folder-plugged-p
+                  wl-summary-buffer-elmo-folder))
+                'leave))
+          t ; displayed
+          nil
+          'cached ; cached by reading.
+          )
          (setq wl-summary-buffer-current-msg num)
-         (if wl-summary-recenter
-             (recenter (/ (- (window-height) 2) 2)))
+         (when wl-summary-recenter
+           (recenter (/ (- (window-height) 2) 2))
+           (if (not wl-summary-width)
+               (wl-horizontal-recenter)))
          (wl-highlight-summary-displaying)
-         (wl-cache-prefetch-next fld num (current-buffer))
+         (wl-message-buffer-prefetch-next folder num (current-buffer)
+                                          wl-summary-buffer-mime-charset)
          (run-hooks 'wl-summary-redisplay-hook))
       (message "No message to display."))))
 
 (defun wl-summary-redisplay-no-mime (&optional folder number)
   (interactive)
-  (let* ((msgdb wl-summary-buffer-msgdb)
-        (fld (or folder wl-summary-buffer-folder-name))
+  (let* ((fld (or folder wl-summary-buffer-elmo-folder))
         (num (or number (wl-summary-message-number)))
-        (wl-mime-charset      wl-summary-buffer-mime-charset)
-        (default-mime-charset wl-summary-buffer-mime-charset)   
         wl-break-pages)
     (if num
        (progn
          (setq wl-summary-buffer-disp-msg t)
+         (setq wl-summary-buffer-last-displayed-msg
+               wl-summary-buffer-current-msg)
          (setq wl-current-summary-buffer (current-buffer))
-         (wl-normal-message-redisplay fld num 'no-mime msgdb)
+         (wl-message-redisplay fld num 'as-is)
          (wl-summary-mark-as-read nil nil t)
          (setq wl-summary-buffer-current-msg num)
-         (if wl-summary-recenter
-             (recenter (/ (- (window-height) 2) 2)))
+         (when wl-summary-recenter
+           (recenter (/ (- (window-height) 2) 2))
+           (if (not wl-summary-width)
+               (wl-horizontal-recenter)))
          (wl-highlight-summary-displaying)
          (run-hooks 'wl-summary-redisplay-hook))
       (message "No message to display.")
@@ -5311,98 +5185,122 @@ Reply to author if invoked with argument."
 
 (defun wl-summary-redisplay-all-header (&optional folder number)
   (interactive)
-  (let* ((msgdb wl-summary-buffer-msgdb)
-        (fld (or folder wl-summary-buffer-folder-name))
+  (let* ((fld (or folder wl-summary-buffer-elmo-folder))
         (num (or number (wl-summary-message-number)))
         (wl-mime-charset      wl-summary-buffer-mime-charset)
-        (default-mime-charset wl-summary-buffer-mime-charset)   
-        (wl-message-redisplay-func wl-summary-buffer-message-redisplay-func))
+        (default-mime-charset wl-summary-buffer-mime-charset))
     (if num
        (progn
-         (if (wl-message-redisplay fld num 'all-header msgdb); t if displayed.
+         (setq wl-summary-buffer-disp-msg t)
+         (setq wl-summary-buffer-last-displayed-msg
+               wl-summary-buffer-current-msg)
+         (setq wl-current-summary-buffer (current-buffer))
+         (if (wl-message-redisplay fld num 'all-header); t if displayed.
              (wl-summary-mark-as-read nil nil t))
+         (setq wl-summary-buffer-current-msg num)
+         (when wl-summary-recenter
+           (recenter (/ (- (window-height) 2) 2))
+           (if (not wl-summary-width)
+               (wl-horizontal-recenter)))
+         (wl-highlight-summary-displaying)
          (run-hooks 'wl-summary-redisplay-hook))
       (message "No message to display."))))
-        
+
 (defun wl-summary-jump-to-current-message ()
   (interactive)
   (let (message-buf message-win)
-    (if (setq message-buf (get-buffer wl-message-buf-name))
+    (if (setq message-buf wl-message-buffer)
        (if (setq message-win (get-buffer-window message-buf))
            (select-window message-win)
-         (wl-select-buffer (get-buffer wl-message-buf-name)))
+         (wl-message-select-buffer wl-message-buffer))
       (wl-summary-redisplay)
-      (wl-select-buffer (get-buffer wl-message-buf-name)))
+      (wl-message-select-buffer wl-message-buffer))
     (goto-char (point-min))))
 
 (defun wl-summary-cancel-message ()
   "Cancel an article on news."
   (interactive)
-  (let ((summary-buf (current-buffer))
-       message-buf)
-    (wl-summary-set-message-buffer-or-redisplay)
-    (if (setq message-buf (wl-message-get-original-buffer))
-       (set-buffer message-buf))
-    (unless (wl-message-news-p)
-      (error "This is not a news article; canceling is impossible"))
-    (when (yes-or-no-p "Do you really want to cancel this article? ")
-      (let (from newsgroups message-id distribution buf)
-       (save-excursion
-         (setq from (std11-field-body "from")
-               newsgroups (std11-field-body "newsgroups")
-               message-id (std11-field-body "message-id")
-               distribution (std11-field-body "distribution"))
-         ;; Make sure that this article was written by the user.
-         (unless (wl-address-user-mail-address-p 
-                  (wl-address-header-extract-address
-                   (car (wl-parse-addresses from))))
-           (error "This article is not yours"))
-         ;; Make control message.
-         (setq buf (set-buffer (get-buffer-create " *message cancel*")))
-         (setq wl-draft-buffer-cur-summary-buffer summary-buf)
-         (buffer-disable-undo (current-buffer))
-         (erase-buffer)
-         (insert "Newsgroups: " newsgroups "\n"
-                 "From: " (wl-address-header-extract-address
-                           wl-from) "\n"
-                           "Subject: cmsg cancel " message-id "\n"
-                           "Control: cancel " message-id "\n"
-                           (if distribution
-                               (concat "Distribution: " distribution "\n")
-                             "")
-                           mail-header-separator "\n"
-                           wl-summary-cancel-message)
-         (message "Canceling your message...")
-         (wl-draft-raw-send t t) ; kill when done, force-pre-hooks.
-         (message "Canceling your message...done"))))))
+  (if (null (wl-summary-message-number))
+      (message "No message.")
+    (let ((summary-buf (current-buffer))
+         message-buf)
+      (wl-summary-set-message-buffer-or-redisplay)
+      (if (setq message-buf (wl-message-get-original-buffer))
+         (set-buffer message-buf))
+      (unless (wl-message-news-p)
+       (set-buffer summary-buf)
+       (if (and (eq (elmo-folder-type-internal wl-summary-buffer-elmo-folder)
+                    'nntp)
+                (y-or-n-p "Cannot get Newsgroups. Fetch again? "))
+           (progn
+             (wl-summary-redisplay t)
+             (wl-summary-supersedes-message))
+         (error "This is not a news article; supersedes is impossible")))
+      (when (yes-or-no-p "Do you really want to cancel this article? ")
+       (let (from newsgroups message-id distribution buf)
+         (save-excursion
+           (setq from (std11-field-body "from")
+                 newsgroups (std11-field-body "newsgroups")
+                 message-id (std11-field-body "message-id")
+                 distribution (std11-field-body "distribution"))
+           ;; Make sure that this article was written by the user.
+           (unless (wl-address-user-mail-address-p
+                    (wl-address-header-extract-address
+                     (car (wl-parse-addresses from))))
+             (error "This article is not yours"))
+           ;; Make control message.
+           (setq buf (set-buffer (get-buffer-create " *message cancel*")))
+           (setq wl-draft-buffer-cur-summary-buffer summary-buf)
+           (buffer-disable-undo (current-buffer))
+           (erase-buffer)
+           (insert "Newsgroups: " newsgroups "\n"
+                   "From: " (wl-address-header-extract-address
+                             wl-from) "\n"
+                             "Subject: cmsg cancel " message-id "\n"
+                             "Control: cancel " message-id "\n"
+                             (if distribution
+                                 (concat "Distribution: " distribution "\n")
+                               "")
+                             mail-header-separator "\n"
+                             wl-summary-cancel-message)
+           (message "Canceling your message...")
+           (wl-draft-raw-send t t) ; kill when done, force-pre-hooks.
+           (message "Canceling your message...done")))))))
 
 (defun wl-summary-supersedes-message ()
   "Supersede current message."
   (interactive)
   (let ((summary-buf (current-buffer))
-       (mmelmo-force-fetch-entire-message t)
-       message-buf from)
+       (mmelmo-force-fetch-entire-message t)
+       message-buf from)
     (wl-summary-set-message-buffer-or-redisplay)
     (if (setq message-buf (wl-message-get-original-buffer))
-       (set-buffer message-buf))
+       (set-buffer message-buf))
     (unless (wl-message-news-p)
-      (error "This is not a news article; supersedes is impossible"))
+      (set-buffer summary-buf)
+      (if (and (eq (elmo-folder-type-internal wl-summary-buffer-elmo-folder)
+                  'nntp)
+              (y-or-n-p "Cannot get Newsgroups. Fetch again? "))
+         (progn
+           (wl-summary-redisplay t)
+           (wl-summary-supersedes-message))
+       (error "This is not a news article; supersedes is impossible")))
     (save-excursion
       (setq from (std11-field-body "from"))
       ;; Make sure that this article was written by the user.
-      (unless (wl-address-user-mail-address-p 
+      (unless (wl-address-user-mail-address-p
               (wl-address-header-extract-address
                (car (wl-parse-addresses from))))
-       (error "This article is not yours"))
+       (error "This article is not yours"))
       (let* ((message-id (std11-field-body "message-id"))
-            (followup-to (std11-field-body "followup-to"))
-            (mail-default-headers
-             (concat mail-default-headers
-                     "Supersedes: " message-id "\n"
-                     (and followup-to
-                          (concat "Followup-To: " followup-to "\n")))))
-       (set-buffer (wl-message-get-original-buffer))
-       (wl-draft-edit-string (buffer-substring (point-min) (point-max)))))))
+            (followup-to (std11-field-body "followup-to"))
+            (mail-default-headers
+             (concat mail-default-headers
+                     "Supersedes: " message-id "\n"
+                     (and followup-to
+                          (concat "Followup-To: " followup-to "\n")))))
+       (set-buffer (wl-message-get-original-buffer))
+       (wl-draft-edit-string (buffer-substring (point-min) (point-max)))))))
 
 (defun wl-summary-save (&optional arg wl-save-dir)
   (interactive)
@@ -5420,11 +5318,11 @@ Reply to author if invoked with argument."
                         (null (file-exists-p filename))))
              (setq filename
                    (read-file-name "Save to file: " filename)))
-                                
+
          (wl-summary-set-message-buffer-or-redisplay)
          (set-buffer (wl-message-get-original-buffer))
          (if (and (null arg) (file-exists-p filename))
-             (if (y-or-n-p "file already exists. override it?")
+             (if (y-or-n-p "File already exists.  override it? ")
                  (write-region (point-min) (point-max) filename))
            (write-region (point-min) (point-max) filename)))
       (message "No message to save."))
@@ -5457,201 +5355,110 @@ Reply to author if invoked with argument."
 ;; mew-summary-pipe-message()
 (defun wl-summary-pipe-message (prefix command)
   "Send this message via pipe."
-  (interactive
-   (list current-prefix-arg
-        (read-string "Shell command on message: " wl-summary-shell-command-last)))
-  (if (y-or-n-p "Send this message to pipe? ")
-      (save-excursion
-       (wl-summary-set-message-buffer-or-redisplay)
-       (set-buffer (wl-message-get-original-buffer))
-       (if (string= command "")
-           (setq command wl-summary-shell-command-last))
-       (goto-char (point-min)) ; perhaps this line won't be necessary
-       (if prefix
-           (search-forward "\n\n"))
-       (shell-command-on-region (point) (point-max) command nil)
-       (setq wl-summary-shell-command-last command))))
+  (interactive (list current-prefix-arg nil))
+  (if (null (wl-summary-message-number))
+      (message "No message.")
+    (setq command (read-string "Shell command on message: "
+                              wl-summary-shell-command-last))
+    (if (y-or-n-p "Send this message to pipe? ")
+       (save-excursion
+         (wl-summary-set-message-buffer-or-redisplay)
+         (set-buffer (wl-message-get-original-buffer))
+         (if (string= command "")
+             (setq command wl-summary-shell-command-last))
+         (goto-char (point-min)) ; perhaps this line won't be necessary
+         (if prefix
+             (search-forward "\n\n"))
+         (shell-command-on-region (point) (point-max) command nil)
+         (setq wl-summary-shell-command-last command)))))
 
 (defun wl-summary-print-message (&optional arg)
   (interactive "P")
-  (save-excursion
-    (wl-summary-set-message-buffer-or-redisplay)
+  (if (null (wl-summary-message-number))
+      (message "No message.")
+    (save-excursion
+      (wl-summary-set-message-buffer-or-redisplay)
+      (if (or (not (interactive-p))
+             (y-or-n-p "Print ok? "))
+         (progn
+           (let ((buffer (generate-new-buffer " *print*")))
+             (copy-to-buffer buffer (point-min) (point-max))
+             (set-buffer buffer)
+             (funcall wl-print-buffer-function)
+             (kill-buffer buffer)))
+       (message "")))))
+
+(defun wl-summary-print-message-with-ps-print (&optional filename)
+  (interactive)
+  (if (null (wl-summary-message-number))
+      (message "No message.")
+    (setq filename (ps-print-preprint current-prefix-arg))
     (if (or (not (interactive-p))
-           (y-or-n-p "Print ok?"))
-       (progn
-         (let* ((message-buffer (get-buffer wl-message-buf-name))
-                ;; (summary-buffer (get-buffer wl-summary-buffer-name))
-                (buffer (generate-new-buffer " *print*")))
-           (set-buffer message-buffer)
-           (copy-to-buffer buffer (point-min) (point-max))
-           (set-buffer buffer)
-           (funcall wl-print-buffer-func)
-           (kill-buffer buffer)))
+           (y-or-n-p "Print ok? "))
+       (let ((summary-buffer (current-buffer))
+             wl-break-pages)
+         (save-excursion
+           (wl-summary-set-message-buffer-or-redisplay)
+           ;; (wl-summary-redisplay-internal)
+           (let* ((buffer (generate-new-buffer " *print*"))
+                  (entity (progn
+                            (set-buffer summary-buffer)
+                            (assoc (cdr (assq
+                                         (wl-summary-message-number)
+                                         (elmo-msgdb-get-number-alist
+                                          (wl-summary-buffer-msgdb))))
+                                   (elmo-msgdb-get-overview
+                                    (wl-summary-buffer-msgdb)))))
+                  (wl-ps-subject
+                   (and entity
+                        (or (elmo-msgdb-overview-entity-get-subject entity)
+                            "")))
+                  (wl-ps-from
+                   (and entity
+                        (or (elmo-msgdb-overview-entity-get-from entity) "")))
+                  (wl-ps-date
+                   (and entity
+                        (or (elmo-msgdb-overview-entity-get-date entity) ""))))
+             (run-hooks 'wl-ps-preprint-hook)
+             (set-buffer wl-message-buffer)
+             (copy-to-buffer buffer (point-min) (point-max))
+             (set-buffer buffer)
+             (unwind-protect
+                 (let ((ps-left-header
+                        (list (concat "(" wl-ps-subject ")")
+                              (concat "(" wl-ps-from ")")))
+                       (ps-right-header
+                        (list "/pagenumberstring load"
+                              (concat "(" wl-ps-date ")"))))
+                   (run-hooks 'wl-ps-print-hook)
+                   (funcall wl-ps-print-buffer-function filename))
+               (kill-buffer buffer)))))
       (message ""))))
 
-(defun wl-summary-print-message-with-ps-print (&optional filename)
-  (interactive (list (ps-print-preprint current-prefix-arg)))
-  (if (or (not (interactive-p))
-         (y-or-n-p "Print ok?"))
-      (let ((summary-buffer (current-buffer))
-           wl-break-pages)
-       (save-excursion
-         ;;(wl-summary-set-message-buffer-or-redisplay)
-         (wl-summary-redisplay-internal)
-         (let* ((message-buffer (get-buffer wl-message-buf-name))
-                (buffer (generate-new-buffer " *print*"))
-                (entity (progn
-                          (set-buffer summary-buffer)
-                          (assoc (cdr (assq 
-                                       (wl-summary-message-number)
-                                       (elmo-msgdb-get-number-alist 
-                                        wl-summary-buffer-msgdb)))
-                                 (elmo-msgdb-get-overview 
-                                  wl-summary-buffer-msgdb))))
-                (wl-ps-subject
-                 (and entity
-                      (or (elmo-msgdb-overview-entity-get-subject entity) 
-                          "")))
-                (wl-ps-from
-                 (and entity
-                      (or (elmo-msgdb-overview-entity-get-from entity) "")))
-                (wl-ps-date
-                 (and entity
-                      (or (elmo-msgdb-overview-entity-get-date entity) ""))))
-           (run-hooks 'wl-ps-preprint-hook)
-           (set-buffer message-buffer)
-           (copy-to-buffer buffer (point-min) (point-max))
-           (set-buffer buffer)
-           (unwind-protect
-               (let ((ps-left-header
-                      (list (concat "(" wl-ps-subject ")")
-                            (concat "(" wl-ps-from ")")))
-                     (ps-right-header 
-                      (list "/pagenumberstring load" 
-                            (concat "(" wl-ps-date ")"))))
-                 (run-hooks 'wl-ps-print-hook)
-                 (funcall wl-ps-print-buffer-func filename))
-             (kill-buffer buffer)))))
-    (message "")))
-  
 (if (featurep 'ps-print) ; ps-print is available.
     (fset 'wl-summary-print-message 'wl-summary-print-message-with-ps-print))
 
 (defun wl-summary-folder-info-update ()
-  (let ((folder (elmo-string wl-summary-buffer-folder-name))
+  (let ((folder (elmo-string (wl-summary-buffer-folder-name)))
        (num-db (elmo-msgdb-get-number-alist
-                wl-summary-buffer-msgdb)))
+                (wl-summary-buffer-msgdb))))
     (wl-folder-set-folder-updated folder
                                  (list 0
-                                       (+ wl-summary-buffer-unread-count 
+                                       (+ wl-summary-buffer-unread-count
                                           wl-summary-buffer-new-count)
                                        (length num-db)))))
 
-(defun wl-summary-get-newsgroups ()
-  (let ((spec-list (elmo-folder-get-primitive-spec-list
-                   (elmo-string wl-summary-buffer-folder-name)))
-       ng-list)
-    (while spec-list
-      (when (eq (caar spec-list) 'nntp)
-       (wl-append ng-list (list (nth 1 (car spec-list)))))
-      (setq spec-list (cdr spec-list)))
-    ng-list))
-
-(defun wl-summary-set-crosspost (&optional type redisplay)
-  (let* ((number (wl-summary-message-number))
-        (spec (elmo-folder-number-get-spec wl-summary-buffer-folder-name
-                                           number))
-        (folder (nth 1 spec))
-        message-buf newsgroups)
-    (when (eq (car spec) 'nntp)
-      (if redisplay
-         (wl-summary-redisplay))
-      (save-excursion
-       (if (setq message-buf (wl-message-get-original-buffer))
-           (set-buffer message-buf))
-       (setq newsgroups (std11-field-body "newsgroups")))
-      (when newsgroups
-       (let* ((msgdb wl-summary-buffer-msgdb)
-              (num-db (elmo-msgdb-get-number-alist msgdb))
-              (ng-list (wl-summary-get-newsgroups)) ;; for multi folder
-              crosspost-folders)
-         (when (setq crosspost-folders
-                     (elmo-delete-lists ng-list
-                                        (wl-parse-newsgroups newsgroups t)))
-           (elmo-crosspost-message-set (cdr (assq number num-db)) ;;message-id
-                                       crosspost-folders
-                                       type) ;;not used
-           (setq wl-crosspost-alist-modified t)))))))
-
-(defun wl-summary-is-crosspost-folder (spec-list fld-list)
-  (let (fld flds)
-    (while spec-list
-      (if (and (eq (caar spec-list) 'nntp)
-              (member (setq fld (nth 1 (car spec-list))) fld-list))
-         (wl-append flds (list fld)))
-      (setq spec-list (cdr spec-list)))
-    flds))
-
-(defun wl-summary-update-crosspost ()
-  (let* ((msgdb wl-summary-buffer-msgdb) 
-        (number-alist (elmo-msgdb-get-number-alist msgdb))
-        (mark-alist (elmo-msgdb-get-mark-alist msgdb))
-        (spec-list (elmo-folder-get-primitive-spec-list
-                    (elmo-string wl-summary-buffer-folder-name)))
-        (alist elmo-crosspost-message-alist)
-        (crossed 0)
-        mark ngs num)
-    (when (assq 'nntp spec-list)
-      (while alist
-       (when (setq ngs
-                   (wl-summary-is-crosspost-folder
-                    spec-list
-                    (nth 1 (car alist))))
-         (when (setq num (car (rassoc (caar alist) number-alist)))
-           (if (and (setq mark (cadr (assq num mark-alist)))
-                    (member mark (list wl-summary-new-mark
-                                       wl-summary-unread-uncached-mark
-                                       wl-summary-unread-cached-mark)))
-               (setq crossed (1+ crossed)))
-           (if (wl-summary-jump-to-msg num)
-               (wl-summary-mark-as-read t);; opened
-             (wl-thread-msg-mark-as-read num)));; closed
-         ;; delete if message does't exists.
-         (elmo-crosspost-message-delete (caar alist) ngs)
-         (setq wl-crosspost-alist-modified t))
-       (setq alist (cdr alist))))
-    (if (> crossed 0)
-       crossed)))
-
-(defun wl-crosspost-alist-load ()
-  (setq elmo-crosspost-message-alist (elmo-crosspost-alist-load))
-  (setq wl-crosspost-alist-modified nil))
-
-(defun wl-crosspost-alist-save ()
-  (when wl-crosspost-alist-modified
-    ;; delete non-exists newsgroups
-    (let ((alist elmo-crosspost-message-alist)
-         newsgroups)
-      (while alist
-       (setq newsgroups
-             (elmo-delete-if
-              '(lambda (x)
-                 (not (intern-soft x wl-folder-newsgroups-hashtb)))
-              (nth 1 (car alist))))
-       (if newsgroups
-           (setcar (cdar alist) newsgroups)
-         (setq elmo-crosspost-message-alist
-               (delete (car alist) elmo-crosspost-message-alist)))
-       (setq alist (cdr alist)))
-      (elmo-crosspost-alist-save elmo-crosspost-message-alist)
-      (setq wl-crosspost-alist-modified nil))))
+(defun wl-summary-get-original-buffer ()
+  "Get original buffer for the current summary."
+  (save-excursion
+    (wl-summary-set-message-buffer-or-redisplay)
+    (wl-message-get-original-buffer)))
 
 (defun wl-summary-pack-number (&optional arg)
   (interactive "P")
-  (setq wl-summary-buffer-msgdb
-       (elmo-pack-number
-        wl-summary-buffer-folder-name wl-summary-buffer-msgdb arg))
-  (wl-summary-rescan))
+  (elmo-folder-pack-numbers wl-summary-buffer-elmo-folder)
+  (let (wl-use-scoring)
+    (wl-summary-rescan)))
 
 (defun wl-summary-target-mark-uudecode ()
   (interactive)
@@ -5668,12 +5475,14 @@ Reply to author if invoked with argument."
        (set-buffer summary-buf)
        (wl-summary-jump-to-msg (car mlist))
        (wl-summary-redisplay)
-       (set-buffer (setq orig-buf (wl-message-get-original-buffer)))
+       (set-buffer (setq orig-buf (wl-summary-get-original-buffer)))
        (goto-char (point-min))
        (cond ((= i 1) ; first
-              (setq filename (wl-message-uu-substring 
-                              orig-buf tmp-buf t 
-                              (= i k))))
+              (if (setq filename (wl-message-uu-substring
+                                  orig-buf tmp-buf t
+                                  (= i k)))
+                  nil
+                (error "Can't find begin line")))
              ((< i k)
               (wl-message-uu-substring orig-buf tmp-buf))
              (t ; last
@@ -5694,7 +5503,7 @@ Reply to author if invoked with argument."
                                            wl-tmp-dir))
              (setq filename (expand-file-name filename decode-dir)))
            (if (file-exists-p filename)
-               (or (yes-or-no-p (format "File %s exists. Save anyway? " 
+               (or (yes-or-no-p (format "File %s exists. Save anyway? "
                                         filename))
                    (error "")))
            (elmo-bind-directory
@@ -5706,7 +5515,7 @@ Reply to author if invoked with argument."
                           wl-prog-uudecode-arg))))
            (when (not (= 0 rc))
              (setq errmsg (buffer-substring (point-min)(point-max)))
-             (error "uudecode error: %s" errmsg))
+             (error "Uudecode error: %s" errmsg))
            (if (not wl-prog-uudecode-no-stdout-option)
                (let (file-name-handler-alist) ;; void jka-compr
                  (as-binary-output-file
@@ -5719,124 +5528,73 @@ Reply to author if invoked with argument."
                (message "Saved as %s" filename)))
        (kill-buffer tmp-buf)))))
 
-(defun wl-summary-drop-unsync ()
-  "Drop all unsync messages."
-  (interactive)
-  (if (elmo-folder-pipe-p wl-summary-buffer-folder-name)
-      (error "You cannot drop unsync messages in this folder"))
-  (if (or (not (interactive-p))
-         (y-or-n-p "Drop all unsync messages?"))
-      (let* ((folder-list (elmo-folder-get-primitive-folder-list
-                          wl-summary-buffer-folder-name))
-            (is-multi (elmo-multi-p wl-summary-buffer-folder-name))
-            (sum 0)
-            (multi-num 0)
-            pair)
-       (message "Dropping...")
-       (while folder-list
-         (setq pair (elmo-max-of-folder (car folder-list)))
-         (when is-multi ;; dirty hack...
-           (incf multi-num)
-           (setcar pair (+ (* multi-num elmo-multi-divide-number)
-                           (car pair))))
-         (elmo-msgdb-set-number-alist 
-          wl-summary-buffer-msgdb 
-          (nconc 
-           (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb)
-           (list (cons (car pair) nil))))
-         (setq sum (+ sum (cdr pair)))
-         (setq folder-list (cdr folder-list)))
-       (wl-summary-set-message-modified)
-       (wl-folder-set-folder-updated wl-summary-buffer-folder-name
-                                     (list 0
-                                           (+ wl-summary-buffer-unread-count 
-                                              wl-summary-buffer-new-count)
-                                           sum))
-       (message "Dropping...done."))))
+;; Someday
+;; (defun wl-summary-drop-unsync ()
+;;   "Drop all unsync messages."
+;;   (interactive)
+;;   (if (elmo-folder-pipe-p (wl-summary-buffer-folder-name))
+;;       (error "You cannot drop unsync messages in this folder"))
+;;   (if (or (not (interactive-p))
+;;       (y-or-n-p "Drop all unsync messages? "))
+;;       (let* ((folder-list (elmo-folder-get-primitive-folder-list
+;;                        (wl-summary-buffer-folder-name)))
+;;          (is-multi (elmo-multi-p (wl-summary-buffer-folder-name)))
+;;          (sum 0)
+;;          (multi-num 0)
+;;          pair)
+;;     (message "Dropping...")
+;;     (while folder-list
+;;       (setq pair (elmo-folder-message-numbers (car folder-list)))
+;;       (when is-multi ;; dirty hack...
+;;         (incf multi-num)
+;;         (setcar pair (+ (* multi-num elmo-multi-divide-number)
+;;                         (car pair))))
+;;       (elmo-msgdb-set-number-alist
+;;        (wl-summary-buffer-msgdb)
+;;        (nconc
+;;         (elmo-msgdb-get-number-alist (wl-summary-buffer-msgdb))
+;;         (list (cons (car pair) nil))))
+;;       (setq sum (+ sum (cdr pair)))
+;;       (setq folder-list (cdr folder-list)))
+;;     (wl-summary-set-message-modified)
+;;     (wl-folder-set-folder-updated (wl-summary-buffer-folder-name)
+;;                                   (list 0
+;;                                         (+ wl-summary-buffer-unread-count
+;;                                            wl-summary-buffer-new-count)
+;;                                         sum))
+;;     (message "Dropping...done"))))
 
 (defun wl-summary-default-get-next-msg (msg)
-  (let (next)
-    (if (and (not wl-summary-buffer-target-mark-list)
-            (eq wl-summary-buffer-view 'thread)
-            (if (eq wl-summary-move-direction-downward nil)
-                (setq next (wl-thread-get-prev-unread msg))
-              (setq next (wl-thread-get-next-unread msg))))
-       next
-      (save-excursion
-       (wl-summary-jump-to-msg msg)
-       (let (wl-summary-buffer-disp-msg)
-         (if (eq wl-summary-move-direction-downward nil)
-             (unless (wl-summary-cursor-up)
-               (wl-summary-prev))
-           (unless (wl-summary-cursor-down)
-             (wl-summary-next)))
-         (wl-summary-message-number))))))
-
-(defsubst wl-cache-prefetch-p (fld &optional num)
-  (cond ((and num wl-cache-prefetch-folder-type-list)
-        (memq
-         (elmo-folder-number-get-type fld num)
-         wl-cache-prefetch-folder-type-list))
-       (wl-cache-prefetch-folder-type-list
-        (let ((list wl-cache-prefetch-folder-type-list)
-              type)
-          (catch 'done
-            (while (setq type (pop list))
-              (if (elmo-folder-contains-type fld type)
-                  (throw 'done t))))))
-       ((consp wl-cache-prefetch-folder-list)
-        (wl-string-match-member fld wl-cache-prefetch-folder-list))
-       (t
-        wl-cache-prefetch-folder-list)))
-
-(defconst wl-cache-prefetch-idle-time 
-  (if (featurep 'lisp-float-type) (/ (float 1) (float 10)) 1))
-
-(defun wl-cache-prefetch-next (fld msg &optional summary)
-  (if (wl-cache-prefetch-p fld)
-      (if (not elmo-use-buffer-cache)
-        ;; (message "`elmo-use-buffer-cache' is nil, cache prefetch is disable.")
-       (save-excursion
-         (set-buffer (or summary (get-buffer wl-summary-buffer-name)))
-         (let ((next (funcall wl-cache-prefetch-get-next-func msg)))
-           (when (and next
-                      (wl-cache-prefetch-p fld next))
-             (if (not (fboundp 'run-with-idle-timer))
-                 (when (sit-for wl-cache-prefetch-idle-time)
-                   (wl-cache-prefetch-message fld next summary))
-               (run-with-idle-timer
-                wl-cache-prefetch-idle-time
-                nil
-                'wl-cache-prefetch-message fld next summary)
-               (sit-for 0))))))))
-
-(defvar wl-cache-prefetch-debug nil)
-(defun wl-cache-prefetch-message (folder msg summary &optional next)
-  (when (buffer-live-p summary)
-    (save-excursion
-      (set-buffer summary)
-      (when (string= folder wl-summary-buffer-folder-name)
-       (unless next
-         (setq next msg))
-       (let* ((msgdb wl-summary-buffer-msgdb)
-              (message-id (cdr (assq next
-                                     (elmo-msgdb-get-number-alist msgdb)))))
-         (if (not (elmo-buffer-cache-hit (list folder next message-id)))
-             (let* ((size (elmo-msgdb-overview-entity-get-size
-                           (assoc message-id 
-                                  (elmo-msgdb-get-overview msgdb)))))
-               (when (or (elmo-local-file-p folder next)
-                         (not (and (integerp size)
-                                   wl-cache-prefetch-threshold
-                                   (>= size wl-cache-prefetch-threshold)
-                                   (not (elmo-cache-exists-p message-id
-                                                             folder next)))))
-                 (if wl-cache-prefetch-debug
-                     (message "Reading %d..." msg))
-                 (elmo-buffer-cache-message folder next msgdb)
-                 (if wl-cache-prefetch-debug
-                     (message "Reading %d... done" msg))))))))))
-
-(provide 'wl-summary)
+  (or (wl-summary-next-message msg
+                              (if wl-summary-move-direction-downward 'down
+                                'up)
+                              nil)
+      (cadr (memq msg (if wl-summary-move-direction-downward
+                         wl-summary-buffer-number-list
+                       (reverse wl-summary-buffer-number-list))))))
+
+(defun wl-summary-save-current-message ()
+  "Save current message for `wl-summary-yank-saved-message'."
+  (interactive)
+  (let ((number (wl-summary-message-number)))
+    (setq wl-summary-buffer-saved-message number)
+    (and number (message "No: %s is saved." number))))
+
+(defun wl-summary-yank-saved-message ()
+  "Set current message as a parent of the saved message."
+  (interactive)
+  (if wl-summary-buffer-saved-message
+      (let ((number (wl-summary-message-number)))
+       (if (eq wl-summary-buffer-saved-message number)
+           (message "Cannot set itself as a parent.")
+         (save-excursion
+           (wl-thread-jump-to-msg wl-summary-buffer-saved-message)
+           (wl-thread-set-parent number)
+           (wl-summary-set-thread-modified))
+         (setq  wl-summary-buffer-saved-message nil)))
+    (message "There's no saved message.")))
+
+(require 'product)
+(product-provide (provide 'wl-summary) (require 'wl-version))
 
 ;;; wl-summary.el ends here