* wl-summary.el (wl-summary-redisplay-internal): If `elmo-message-use-cache-p'
[elisp/wanderlust.git] / wl / wl-summary.el
index 271c53f..ae7a442 100644 (file)
 (defvar wl-summary-buffer-current-msg nil)
 (defvar wl-summary-buffer-unread-count 0)
 (defvar wl-summary-buffer-new-count    0)
+(defvar wl-summary-buffer-answered-count 0)
 (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-view nil)
 (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)
 (make-variable-buffer-local 'wl-summary-buffer-last-displayed-msg)
 (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-answered-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-temp-mark-column)
 (defvar wl-temp-mark)
 (defvar wl-persistent-mark)
 
-;; internal functions (dummy)
-(unless (fboundp 'wl-summary-append-message-func-internal)
-  (defun wl-summary-append-message-func-internal (entity msgdb update
-                                                        &optional force-insert)))
-
 (defmacro wl-summary-sticky-buffer-name (name)
   (` (concat wl-summary-buffer-name ":" (, name))))
 
     subject-string))
 
 (defun wl-summary-default-from (from)
+  "Instance of `wl-summary-from-function'.
+Ordinarily returns the sender name. Returns recipient names if (1)
+summary's folder name matches with `wl-summary-showto-folder-regexp'
+and (2) sender address is yours.
+
+See also variable `wl-use-petname'."
   (let (retval tos ng)
     (unless
        (and (eq major-mode 'wl-summary-mode)
       (wl-summary-redisplay)))
 
 (defun wl-summary-count-unread ()
-  (let ((pair
-        (elmo-msgdb-count-marks (wl-summary-buffer-msgdb)
-                                wl-summary-new-mark
-                                (list wl-summary-unread-uncached-mark
-                                      wl-summary-unread-cached-mark))))
+  (let ((lst (elmo-msgdb-count-marks (wl-summary-buffer-msgdb))))
     (if (eq major-mode 'wl-summary-mode)
-       (setq wl-summary-buffer-new-count (car pair)
-             wl-summary-buffer-unread-count (cdr pair)))
-    pair))
+       (setq wl-summary-buffer-new-count (car lst)
+             wl-summary-buffer-unread-count (nth 1 lst)
+             wl-summary-buffer-answered-count (nth 2 lst)))
+    lst))
 
 (defun wl-summary-message-string (&optional use-cache)
   "Return full body string of current message.
@@ -819,7 +817,7 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
   (wl-mode-line-buffer-identification '(wl-summary-buffer-mode-line))
   (easy-menu-add wl-summary-mode-menu)
   (when wl-summary-lazy-highlight
-    (if wl-on-xemacs 
+    (if wl-on-xemacs
        (progn
          (make-local-variable 'pre-idle-hook)
          (add-hook 'pre-idle-hook 'wl-highlight-summary-window))
@@ -944,8 +942,6 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
         (inhibit-read-only t)
         (buffer-read-only nil)
         expunged)
-    (fset 'wl-summary-append-message-func-internal
-         (wl-summary-get-append-message-func))
     (erase-buffer)
     (message "Re-scanning...")
     (setq i 0)
@@ -972,7 +968,7 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
     (elmo-kill-buffer wl-summary-search-buf-name)
     (while curp
       (setq entity (car curp))
-      (wl-summary-append-message-func-internal entity msgdb nil)
+      (wl-summary-insert-message entity msgdb nil)
       (setq curp (cdr curp))
       (when (> num elmo-display-progress-threshold)
        (setq i (+ i 1))
@@ -986,7 +982,7 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
                 (caar wl-summary-delayed-update)
                 (elmo-msgdb-overview-entity-get-number
                  (cdar wl-summary-delayed-update)))
-       (wl-summary-append-message-func-internal
+       (wl-summary-insert-message
         (cdar wl-summary-delayed-update) msgdb nil t)
        (setq wl-summary-delayed-update (cdr wl-summary-delayed-update))))
     (message "Constructing summary structure...done")
@@ -995,8 +991,7 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
        (progn
          (message "Inserting thread...")
          (wl-thread-insert-top)
-         (message "Inserting thread...done"))
-      (wl-summary-make-number-list))
+         (message "Inserting thread...done")))
     (when wl-use-scoring
       (setq wl-summary-scored nil)
       (wl-summary-score-headers nil msgdb
@@ -1063,10 +1058,10 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
   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))
+   wl-summary-buffer-elmo-folder t))
 (defun wl-summary-mark-modified-p ()
-  wl-summary-buffer-mark-modified)
+  (elmo-folder-mark-modified-internal
+   wl-summary-buffer-elmo-folder))
 (defun wl-summary-set-thread-modified ()
   (setq wl-summary-buffer-thread-modified t))
 (defun wl-summary-thread-modified-p ()
@@ -1076,7 +1071,8 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
   (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 (format "Execute remaining marks in %s? "
+                           (wl-summary-buffer-folder-name)))
          (progn
            (wl-summary-exec)
            (if (or wl-summary-buffer-refile-list
@@ -1203,7 +1199,6 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
   (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)))
@@ -1426,23 +1421,12 @@ If ARG is non-nil, checking is omitted."
                        (elmo-message-encache
                         wl-summary-buffer-elmo-folder
                         number))
+                   (elmo-message-set-cached wl-summary-buffer-elmo-folder
+                                            number t)
                    (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)))
-                   (elmo-msgdb-set-mark msgdb number new-mark)
-                   (or new-mark (setq new-mark " "))
-                   (wl-summary-set-mark-modified)
+                         (or (elmo-message-mark wl-summary-buffer-elmo-folder
+                                                number)
+                             " "))
                    (wl-summary-update-modeline)
                    (wl-folder-update-unread
                     (wl-summary-buffer-folder-name)
@@ -1450,11 +1434,6 @@ If ARG is non-nil, checking is omitted."
                        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")
   (let ((count 0)
@@ -1500,8 +1479,8 @@ If ARG is non-nil, checking is omitted."
                           (wl-summary-prefetch))
                       (wl-summary-prefetch-msg (car targets))))
          (if (if prefetch-marks
-                 (string= mark wl-summary-unread-cached-mark)
-               (or (string= mark wl-summary-unread-cached-mark)
+                 (string= mark elmo-msgdb-unread-cached-mark)
+               (or (string= mark elmo-msgdb-unread-cached-mark)
                    (string= mark " ")))
              (message "Prefetching... %d/%d message(s)"
                       (setq count (+ 1 count)) length))
@@ -1670,44 +1649,24 @@ If ARG is non-nil, checking is omitted."
             (inhibit-read-only t)
             (buffer-read-only nil)
             (case-fold-search nil)
-            msg mark)
+            msg mark new-mark)
        (message "Setting all msgs as read...")
        (elmo-folder-mark-as-read folder
                                  (elmo-folder-list-unreads
-                                  folder
-                                  (list wl-summary-unread-cached-mark
-                                        wl-summary-unread-uncached-mark
-                                        wl-summary-new-mark)))
+                                  folder))
        (save-excursion
          (goto-char (point-min))
          (while (not (eobp))
-           (setq msg (wl-summary-message-number))
-           (setq mark (wl-summary-persistent-mark))
-           (when (and (not (string= mark " "))
-                      (not (string= mark wl-summary-important-mark))
-                      (not (string= mark wl-summary-read-uncached-mark)))
+           (setq msg (wl-summary-message-number)
+                 mark (wl-summary-persistent-mark)
+                 new-mark (or (elmo-message-mark folder msg) " "))
+           (unless (string= mark new-mark)
              (delete-backward-char 1)
-             (if (or (not (elmo-message-use-cache-p folder msg))
-                     (string= mark wl-summary-unread-cached-mark))
-                 (progn
-                   (insert " ")
-                   (elmo-msgdb-set-mark msgdb msg nil))
-               ;; New mark and unread-uncached mark
-               (insert wl-summary-read-uncached-mark)
-               (elmo-msgdb-set-mark
-                msgdb msg wl-summary-read-uncached-mark))
+             ;; New mark and unread-uncached mark
+             (insert new-mark)
              (if wl-summary-highlight
                  (wl-highlight-summary-current-line nil nil t)))
            (forward-line 1)))
-       (elmo-folder-replace-marks
-        folder
-        (list (cons wl-summary-unread-cached-mark
-                    nil)
-              (cons wl-summary-unread-uncached-mark
-                    wl-summary-read-uncached-mark)
-              (cons wl-summary-new-mark
-                    wl-summary-read-uncached-mark)))
-       (wl-summary-set-mark-modified)
        (wl-folder-update-unread (wl-summary-buffer-folder-name) 0)
        (setq wl-summary-buffer-unread-count 0)
        (setq wl-summary-buffer-new-count    0)
@@ -1725,20 +1684,12 @@ If ARG is non-nil, checking is omitted."
           (msgdb (wl-summary-buffer-msgdb))
           (number-alist (elmo-msgdb-get-number-alist msgdb))
           (case-fold-search nil)
-          mark number unread new-mark)
-      (setq mark (wl-summary-persistent-mark))
-      (cond
-       ((or (string= mark wl-summary-new-mark)
-           (string= mark wl-summary-unread-uncached-mark)
-           (string= mark wl-summary-important-mark))
-       ;; noop
-       )
-       ((string= mark wl-summary-unread-cached-mark)
-       (setq new-mark wl-summary-unread-uncached-mark))
-       (t
-       (setq new-mark wl-summary-read-uncached-mark)))
-      (when new-mark
-       (setq number (wl-summary-message-number))
+          new-mark mark number unread)
+      (setq number (wl-summary-message-number))
+      (elmo-message-set-cached folder number nil)
+      (setq new-mark (elmo-message-mark folder number)
+           mark (wl-summary-persistent-mark))
+      (unless (string= new-mark mark)
        (delete-backward-char 1)
        (insert new-mark)
        (elmo-file-cache-delete
@@ -1746,8 +1697,6 @@ If ARG is non-nil, checking is omitted."
          (elmo-message-field wl-summary-buffer-elmo-folder
                              number
                              'message-id)))
-       (elmo-msgdb-set-mark msgdb number new-mark)
-       (wl-summary-set-mark-modified)
        (if wl-summary-highlight
            (wl-highlight-summary-current-line nil nil t))
        (set-buffer-modified-p nil)))))
@@ -1755,44 +1704,31 @@ If ARG is non-nil, checking is omitted."
 (defun wl-summary-resume-cache-status ()
   "Resume the cache status of all messages in the current folder."
   (interactive)
-  (let* ((folder wl-summary-buffer-elmo-folder)
-        (cur-buf (current-buffer))
-        (msgdb (wl-summary-buffer-msgdb))
-        (number-alist (elmo-msgdb-get-number-alist msgdb))
-        (inhibit-read-only t)
-        (buffer-read-only nil)
-        (case-fold-search nil)
-        msg mark msgid set-mark)
+  (let ((folder wl-summary-buffer-elmo-folder)
+       (buffer-read-only nil)
+       (case-fold-search nil)
+       number msgid set-mark mark)
     (message "Resuming cache status...")
     (save-excursion
       (goto-char (point-min))
       (while (not (eobp))
-       (setq msg (wl-summary-message-number))
+       (setq number (wl-summary-message-number))
        (setq mark (wl-summary-persistent-mark))
-       (setq msgid (elmo-msgdb-get-field msgdb msg 'message-id))
+       (setq msgid (elmo-message-field folder number 'message-id))
        (setq set-mark nil)
        (if (elmo-file-cache-exists-p msgid)
-           (if (or
-                (string= mark wl-summary-unread-uncached-mark) ; U -> !
-                (string= mark wl-summary-new-mark)             ; N -> !
-                )
-               (setq set-mark wl-summary-unread-cached-mark)
-             (if (string= mark wl-summary-read-uncached-mark)  ; u -> ' '
-                 (setq set-mark " ")))
-         (if (string= mark " ")
-             (setq set-mark wl-summary-read-uncached-mark)     ;' ' -> u
-           (if (string= mark wl-summary-unread-cached-mark)
-               (setq set-mark wl-summary-unread-uncached-mark) ; !  -> U
-             )))
+           (when (member mark (elmo-msgdb-uncached-marks))
+             (elmo-message-set-cached folder number t)
+             (setq set-mark (elmo-message-mark folder number)))
+         (unless (member mark (elmo-msgdb-uncached-marks))
+           (elmo-message-set-cached folder number nil)
+           (setq set-mark (or (elmo-message-mark folder number) " "))))
        (when set-mark
          (delete-backward-char 1)
          (insert set-mark)
-         (elmo-msgdb-set-mark msgdb msg
-                              (if (string= set-mark " ") nil set-mark))
          (if wl-summary-highlight
              (wl-highlight-summary-current-line)))
        (forward-line 1))
-      (wl-summary-set-mark-modified)
       (wl-summary-count-unread)
       (wl-summary-update-modeline)
       (message "Resuming cache status...done")
@@ -1856,6 +1792,25 @@ If ARG is non-nil, checking is omitted."
              (wl-highlight-summary-current-line)))
        (forward-line 1)))))
 
+(defun wl-summary-update-status-marks ()
+  "Synchronize status marks on current buffer to the msgdb."
+  (interactive)
+  (save-excursion
+    (goto-char (point-min))
+    (let ((inhibit-read-only t)
+         (buffer-read-only nil)
+         mark)
+      (while (not (eobp))
+       (unless (string= (wl-summary-persistent-mark)
+                        (setq mark (or (elmo-message-mark
+                                        wl-summary-buffer-elmo-folder
+                                        (wl-summary-message-number))
+                                       " ")))
+         (delete-backward-char 1)
+         (insert mark)
+         (if wl-summary-highlight (wl-highlight-summary-current-line)))
+       (forward-line 1)))))
+
 (defun wl-summary-get-delete-folder (folder)
   (if (string= folder wl-trash-folder)
       'null
@@ -1875,10 +1830,10 @@ If ARG is non-nil, checking is omitted."
                   (error "Trash Folder is not created"))))
               wl-trash-folder)))))
 
-(defun wl-summary-get-append-message-func ()
+(defun wl-summary-insert-message (&rest args)
   (if (eq wl-summary-buffer-view 'thread)
-      'wl-summary-insert-thread-entity
-    'wl-summary-insert-sequential))
+      (apply 'wl-summary-insert-thread-entity args)
+    (apply 'wl-summary-insert-sequential args)))
 
 (defun wl-summary-sort ()
   (interactive)
@@ -1894,8 +1849,9 @@ If ARG is non-nil, checking is omitted."
   "Update marks in summary."
   (interactive)
   (let ((last-progress 0)
+       (folder wl-summary-buffer-elmo-folder)
        (i 0)
-       unread-marks importants unreads
+       importants unreads
        importants-in-db unreads-in-db diff diffs
        mes progress)
     ;; synchronize marks.
@@ -1903,27 +1859,27 @@ If ARG is non-nil, checking is omitted."
                    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)
-           importants-in-db (elmo-folder-list-messages-mark-match
+      (setq importants-in-db (elmo-folder-list-messages-mark-match
                              wl-summary-buffer-elmo-folder
-                             (regexp-quote wl-summary-important-mark))
+                             (regexp-quote elmo-msgdb-important-mark))
            unreads-in-db (elmo-folder-list-messages-mark-match
                           wl-summary-buffer-elmo-folder
-                          (wl-regexp-opt unread-marks))
-           importants (elmo-folder-list-importants
-                       wl-summary-buffer-elmo-folder
-                       wl-summary-important-mark)
+                          (wl-regexp-opt (elmo-msgdb-unread-marks)))
+           importants (elmo-uniq-list
+                       (nconc
+                        (elmo-folder-list-importants
+                         wl-summary-buffer-elmo-folder)
+                        (elmo-folder-list-messages-with-global-mark
+                         wl-summary-buffer-elmo-folder
+                         elmo-msgdb-important-mark)))
            unreads (elmo-folder-list-unreads
-                    wl-summary-buffer-elmo-folder
-                    unread-marks))
+                    wl-summary-buffer-elmo-folder))
       (setq diff (elmo-list-diff importants importants-in-db))
       (setq diffs (cadr diff)) ; important-deletes
       (setq mes (format "Updated (-%d" (length diffs)))
       (while diffs
        (wl-summary-mark-as-important (car diffs)
-                                     wl-summary-important-mark
+                                     elmo-msgdb-important-mark
                                      'no-server)
        (setq diffs (cdr diffs)))
       (setq diffs (car diff)) ; important-appends
@@ -1935,12 +1891,12 @@ If ARG is non-nil, checking is omitted."
       (setq diffs (cadr diff))
       (setq mes (concat mes (format "(-%d" (length diffs))))
       (while diffs
-       (wl-summary-mark-as-read (car diffs) 'no-folder)
+       (wl-summary-mark-as-read (car diffs) 'no-folder 'no-modeline)
        (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)
+       (wl-summary-mark-as-unread (car diffs) 'no-folder 'no-modeline)
        (setq diffs (cdr diffs)))
       (if (interactive-p) (message mes)))))
 
@@ -1954,34 +1910,22 @@ If ARG is non-nil, checking is omitted."
         (buffer-read-only nil)
         gc-message
         overview number-alist
-        curp num i new-msgdb
+        curp num i diff
         append-list delete-list crossed
         update-thread update-top-list
-        expunged mes sync-result entity)
+        expunged mes entity)
     (unwind-protect
        (progn
          (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))
          (goto-char (point-max))
          (wl-folder-confirm-existence folder (elmo-folder-plugged-p folder))
-         (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 sync-result
+         (setq crossed (elmo-folder-synchronize folder sync-all no-check))
+         (if crossed
              (progn
                ;; Setup sync-all
                (if sync-all (wl-summary-sync-all-init))
@@ -1992,19 +1936,24 @@ If ARG is non-nil, checking is omitted."
 ;            (wl-summary-delete-canceled-msgs-from-list
 ;             delete-list
 ;             (wl-summary-buffer-msgdb))))
+               (setq diff (elmo-list-diff (elmo-folder-list-messages
+                                           folder
+                                           'visible-only
+                                           'in-msgdb)
+                                          wl-summary-buffer-number-list))
+               (setq append-list (car diff))
+               (setq delete-list (cadr diff))
+               
                (when delete-list
-                 (wl-summary-delete-messages-on-buffer delete-list "Deleting...")
+                 (wl-summary-delete-messages-on-buffer
+                  delete-list "Deleting...")
                  (message "Deleting...done"))
-               (when new-msgdb
-                 (wl-summary-replace-status-marks
-                  wl-summary-new-mark
-                  wl-summary-unread-uncached-mark))
-               (setq append-list (elmo-msgdb-get-overview new-msgdb))
+               (wl-summary-update-status-marks)
                (setq curp append-list)
                (setq num (length curp))
                (when append-list
                  (setq i 0)
-
+                 
                  ;; set these value for append-message-func
                  (setq overview (elmo-msgdb-get-overview
                                  (elmo-folder-msgdb folder)))
@@ -2014,9 +1963,10 @@ If ARG is non-nil, checking is omitted."
                  (setq wl-summary-delayed-update nil)
                  (elmo-kill-buffer wl-summary-search-buf-name)
                  (while curp
-                   (setq entity (car curp))
+                   (setq entity (elmo-msgdb-overview-get-entity
+                                 (car curp) (elmo-folder-msgdb folder)))
                    (when (setq update-thread
-                               (wl-summary-append-message-func-internal
+                               (wl-summary-insert-message
                                 entity (elmo-folder-msgdb folder)
                                 (not sync-all)))
                      (wl-append update-top-list update-thread))
@@ -2029,7 +1979,10 @@ If ARG is non-nil, checking is omitted."
                      (setq i (+ i 1))
                      (if (or (zerop (% i 5)) (= i num))
                          (elmo-display-progress
-                          'wl-summary-sync-update "Updating thread..."
+                          'wl-summary-sync-update
+                          (if (eq wl-summary-buffer-view 'thread)
+                              "Making thread..."
+                            "Inserting message...")
                           (/ (* i 100) num)))))
                  (when wl-summary-delayed-update
                    (while wl-summary-delayed-update
@@ -2038,7 +1991,7 @@ If ARG is non-nil, checking is omitted."
                               (elmo-msgdb-overview-entity-get-number
                                (cdar wl-summary-delayed-update)))
                      (when (setq update-thread
-                                 (wl-summary-append-message-func-internal
+                                 (wl-summary-insert-message
                                   (cdar wl-summary-delayed-update)
                                   (elmo-folder-msgdb folder)
                                   (not sync-all) t))
@@ -2049,16 +2002,15 @@ If ARG is non-nil, checking is omitted."
                             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))
+                 (message (if (eq wl-summary-buffer-view 'thread)
+                              "Making thread...done"
+                            "Inserting message...done")))
                (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...")
+                 (message "Inserting message...")
                  (wl-thread-insert-top)
-                 (message "Inserting thread...done"))
+                 (message "Inserting message...done"))
                (if elmo-use-database
                    (elmo-database-close))
                (run-hooks 'wl-summary-sync-updated-hook)
@@ -2072,7 +2024,7 @@ If ARG is non-nil, checking is omitted."
                                (length delete-list) num))))
            (setq mes "Quit updating.")))
       ;; synchronize marks.
-      (if (and wl-summary-auto-sync-marks sync-result)
+      (if (and crossed wl-summary-auto-sync-marks)
          (wl-summary-sync-marks))
       ;; scoring
       (when wl-use-scoring
@@ -2097,8 +2049,8 @@ If ARG is non-nil, checking is omitted."
       (wl-folder-set-folder-updated
        (elmo-folder-name-internal folder)
        (list 0
-            (let ((pair (wl-summary-count-unread)))
-              (+ (car pair) (cdr pair)))
+            (let ((lst (wl-summary-count-unread)))
+              (+ (car lst) (nth 1 lst)))
             (elmo-folder-messages folder)))
       (wl-summary-update-modeline)
       ;;
@@ -2208,7 +2160,6 @@ If ARG is non-nil, checking is omitted."
                                    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)
                                          (list 0 0 0))
 ;;; for thread.
@@ -2319,7 +2270,6 @@ If ARG, without confirm."
                   wl-summary-buffer-temp-mark-column
                   wl-summary-buffer-persistent-mark-column
                   wl-summary-buffer-message-modified
-                  wl-summary-buffer-mark-modified
                   wl-summary-buffer-thread-modified
                   wl-summary-buffer-number-list
                   wl-summary-buffer-msgdb
@@ -2375,10 +2325,15 @@ If ARG, without confirm."
        (get-buffer-create wl-summary-buffer-name))))
 
 (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)))))
+  (save-excursion
+    (goto-char (point-min))
+    (while (not (eobp))
+      (setq wl-summary-buffer-number-list
+           (cons (wl-summary-message-number)
+                 wl-summary-buffer-number-list))
+      (forward-line 1))
+    (setq wl-summary-buffer-number-list
+         (nreverse wl-summary-buffer-number-list))))
 
 (defun wl-summary-auto-select-msg-p (unread-msg)
   (and unread-msg
@@ -2386,7 +2341,7 @@ If ARG, without confirm."
             (elmo-msgdb-get-mark
              (wl-summary-buffer-msgdb)
              unread-msg)
-            wl-summary-important-mark))))
+            elmo-msgdb-important-mark))))
 
 (defsubst wl-summary-open-folder (folder)
   ;; Select folder
@@ -2464,7 +2419,11 @@ If ARG, without confirm."
                  (if (file-exists-p view)
                      (setq wl-summary-buffer-view
                            (wl-summary-load-file-object view))
-                   (setq wl-summary-buffer-view wl-summary-default-view))
+                   (setq wl-summary-buffer-view
+                         (or (wl-get-assoc-list-value
+                              wl-summary-default-view-alist
+                              (elmo-folder-name-internal folder))
+                             wl-summary-default-view)))
                  (wl-thread-resume-entity folder)
                  (wl-summary-open-folder folder))
              (setq wl-summary-buffer-view
@@ -2628,7 +2587,11 @@ If ARG, without confirm."
                             (elmo-msgdb-get-mark
                              msgdb
                              (elmo-msgdb-overview-entity-get-number
-                              entity))))))
+                              entity))))
+    (setq wl-summary-buffer-number-list
+         (wl-append wl-summary-buffer-number-list
+                    (list (elmo-msgdb-overview-entity-get-number entity))))
+    nil))
 
 (defun wl-summary-default-subject-filter (subject)
   (let ((case-fold-search t))
@@ -2823,69 +2786,38 @@ If ARG, without confirm."
        (wl-thread-entity-get-linked thr-entity))))))
 
 (defun wl-summary-mark-as-unread (&optional number
-                                           no-server-update
+                                           no-folder-mark
                                            no-modeline-update)
   (interactive)
   (save-excursion
-    (let* (eol
-         (inhibit-read-only t)
-         (buffer-read-only nil)
+    (let ((buffer-read-only nil)
          (folder wl-summary-buffer-elmo-folder)
-         (msgdb (wl-summary-buffer-msgdb))
-;;;      (number-alist (elmo-msgdb-get-number-alist msgdb))
-         new-mark visible mark cur-mark)
-      (if number
-         (progn
-           (setq visible (wl-summary-jump-to-msg number))
-           (unless (setq mark (elmo-msgdb-get-mark msgdb number))
-             (setq mark " ")))
-       ;; interactive
-       (setq visible t))
+         mark new-mark visible)
+      (setq visible (if number
+                       (wl-summary-jump-to-msg number)
+                     t)
+           number (or number (wl-summary-message-number))
+           mark (elmo-message-mark folder number))
+      (unless (member mark (elmo-msgdb-unread-marks))
+       (elmo-folder-unmark-read folder (list number) no-folder-mark))
+      (setq new-mark (elmo-message-mark folder number))
+      (unless no-modeline-update
+       ;; Update unread numbers.
+       ;; should elmo-folder-mark-as-read return unread numbers?
+       (wl-summary-count-unread)
+       (wl-summary-update-modeline)
+       (wl-folder-update-unread
+        (wl-summary-buffer-folder-name)
+        (+ wl-summary-buffer-unread-count
+           wl-summary-buffer-new-count)))
       (when visible
-       (if (null (wl-summary-message-number))
-           (message "No message.")
-         (end-of-line)
-         (setq eol (point))
-         (wl-summary-goto-previous-message-beginning)))
-      (if (or (and (not visible)
-                  ;; already exists in msgdb.
-                  (elmo-msgdb-overview-get-entity number msgdb))
-             (progn
-               ;; visible.
-               (setq cur-mark (wl-summary-persistent-mark))
-               (or (string= cur-mark " ")
-                   (string= cur-mark wl-summary-read-uncached-mark))))
-         (progn
-           (setq number (or number (wl-summary-message-number)))
-           (setq mark (or mark cur-mark))
-           (save-match-data
-             (setq new-mark (if (string= mark
-                                         wl-summary-read-uncached-mark)
-                                wl-summary-unread-uncached-mark
-                              (if (elmo-message-use-cache-p folder number)
-                                  wl-summary-unread-mark
-                                wl-summary-unread-uncached-mark))))
-           ;; server side mark
-           (unless no-server-update
-             (save-match-data
-               (unless (elmo-folder-unmark-read folder (list number))
-                 (error "Setting mark failed"))))
-           (when visible
-             (delete-backward-char 1)
-             (insert new-mark))
-           (elmo-msgdb-set-mark msgdb number new-mark)
-           (unless no-modeline-update
-             (setq wl-summary-buffer-unread-count
-                   (+ 1 wl-summary-buffer-unread-count))
-             (wl-summary-update-modeline)
-             (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)
-               (wl-highlight-summary-current-line))))))
-  (set-buffer-modified-p nil))
+       (unless (string= (wl-summary-persistent-mark) new-mark)
+         (delete-backward-char 1)
+         (insert (or new-mark " "))
+         (if (and visible wl-summary-highlight)
+             (wl-highlight-summary-current-line))
+         (set-buffer-modified-p nil)))
+      number)))
 
 (defun wl-summary-delete (&optional number)
   "Mark a delete mark 'D'.
@@ -3005,9 +2937,6 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
       (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-failures 0)
            (copy-failures 0)
@@ -3046,7 +2975,6 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                            (not (null (cdr dst-msgs)))
                            nil ; no-delete
                            nil ; same-number
-                           unread-marks
                            t))
            (error nil))
          (if result                    ; succeeded.
@@ -3079,7 +3007,6 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
                            (not (null (cdr dst-msgs)))
                            t ; t is no-delete (copy)
                            nil ; same number
-                           unread-marks
                            t))
            (error nil))
          (if result                    ; succeeded.
@@ -3098,7 +3025,6 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
        ;; end cOpy
        (wl-summary-folder-info-update)
        (wl-summary-set-message-modified)
-       (wl-summary-set-mark-modified)
        (run-hooks 'wl-summary-exec-hook)
        (unless (and wl-message-buffer
                     (eq (wl-summary-message-number)
@@ -3123,14 +3049,7 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
              (or wl-folder-completion-function
                  (if (memq 'read-folder wl-use-folder-petname)
                      (wl-folder-get-entity-with-petname)
-                   (let (alist)
-                     (mapatoms 
-                      (lambda (atom)
-                        (setq alist
-                              (cons (list (elmo-string
-                                           (symbol-name atom))) alist)))
-                      wl-folder-entity-hashtb)
-                     alist)))
+                   wl-folder-entity-hashtb))
              nil nil (or init wl-default-spec)
              'wl-read-folder-hist)))
     (if (or (string= fld wl-default-spec)
@@ -3935,69 +3854,44 @@ If ARG, exit virtual folder."
   (interactive)
   (wl-summary-pick wl-summary-buffer-target-mark-list 'delete))
 
-(defun wl-summary-mark-as-read (&optional number no-folder-mark)
+(defun wl-summary-mark-as-read (&optional number
+                                         no-folder-mark
+                                         no-modeline-update)
   (interactive)
   (save-excursion
     (let ((buffer-read-only nil)
          (folder wl-summary-buffer-elmo-folder)
          (msgdb (wl-summary-buffer-msgdb))
          (case-fold-search nil)
-         cur-mark mark stat visible uncached new-mark marked)
-      (setq number (or number (wl-summary-message-number))
-           visible (if number
+         mark visible new-mark)
+      (setq visible (if number
                        (wl-summary-jump-to-msg number)
                      ;; interactive
                      t)
-           mark (elmo-msgdb-get-mark msgdb number))
-      (cond
-       ((string= mark wl-summary-new-mark) ; N
-       (setq stat 'new))
-       ((string= mark wl-summary-unread-uncached-mark) ; U
-       (setq stat 'unread))
-       ((string= mark wl-summary-unread-cached-mark)  ; !
-       (setq stat 'unread))
-       ((string= mark wl-summary-read-uncached-mark)  ; u
-       (setq stat 'read))
-       (t
-       ;; no need to mark server.
-       (setq no-folder-mark t)))
-      (setq new-mark
-           (if (and (if (elmo-message-use-cache-p folder number)
-                        (not (elmo-folder-local-p folder)))
-                    (not (elmo-file-cache-exists-p
-                          (elmo-message-field wl-summary-buffer-elmo-folder
-                                              number 'message-id))))
-               wl-summary-read-uncached-mark
-             nil))
-      ;; folder mark.
-      (unless no-folder-mark
-       (setq marked (elmo-folder-mark-as-read folder (list number))))
-      (when (or no-folder-mark marked)
-       (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))))
+           number (or number (wl-summary-message-number))
+           mark (elmo-message-mark folder number))
+      (when (member mark (elmo-msgdb-unread-marks))
+       ;; folder mark.
+       (elmo-folder-mark-as-read folder (list number) no-folder-mark))
+      (setq new-mark (elmo-message-mark folder number))
+      (unless no-modeline-update
+       ;; Update unread numbers.
+       ;; should elmo-folder-mark-as-read return unread numbers?
+       (wl-summary-count-unread)
        (wl-summary-update-modeline)
        (wl-folder-update-unread
         (wl-summary-buffer-folder-name)
         (+ wl-summary-buffer-unread-count
-           wl-summary-buffer-new-count))
-       (when stat
-         ;; set mark on buffer
-         (when visible
-           (unless (string= (wl-summary-persistent-mark) new-mark)
-             (delete-backward-char 1)
-             (insert (or new-mark " "))))
-         ;; set msgdb mark.
-         (unless (string= mark new-mark)
-           (elmo-msgdb-set-mark msgdb number new-mark))
-         (wl-summary-set-mark-modified))
+           wl-summary-buffer-new-count)))
+      ;; set mark on buffer
+      (when visible
+       (unless (string= (wl-summary-persistent-mark) new-mark)
+         (delete-backward-char 1)
+         (insert (or new-mark " ")))
        (if (and visible wl-summary-highlight)
-           (wl-highlight-summary-current-line nil nil t)))
-      (set-buffer-modified-p nil)
-      (if stat
+           (wl-highlight-summary-current-line nil nil t))
+       (set-buffer-modified-p nil))
+      (if (member mark (elmo-msgdb-unread-marks))
          (run-hooks 'wl-summary-unread-message-hook))
       number ;return value
       )))
@@ -4041,7 +3935,7 @@ If ARG, exit virtual folder."
                              wl-summary-buffer-elmo-folder
                              number
                              'message-id))
-           (if (string= mark wl-summary-important-mark)
+           (if (string= mark elmo-msgdb-important-mark)
                (progn
                  ;; server side mark
                  (save-match-data
@@ -4064,17 +3958,16 @@ If ARG, exit virtual folder."
                  (elmo-folder-mark-as-important folder (list number))))
              (when visible
                (delete-backward-char 1)
-               (insert wl-summary-important-mark))
+               (insert elmo-msgdb-important-mark))
              (elmo-msgdb-set-mark msgdb number
-                                  wl-summary-important-mark)
+                                  elmo-msgdb-important-mark)
              (if (eq (elmo-file-cache-exists-p message-id) 'entire)
                  (elmo-folder-mark-as-read folder (list number))
                ;; Force cache message.
                (elmo-message-encache folder number 'read))
              (unless no-server-update
                (elmo-msgdb-global-mark-set message-id
-                                           wl-summary-important-mark)))
-           (wl-summary-set-mark-modified)))
+                                           elmo-msgdb-important-mark)))))
       (if (and visible wl-summary-highlight)
          (wl-highlight-summary-current-line nil nil t))))
   (set-buffer-modified-p nil)
@@ -4094,7 +3987,7 @@ If ARG, exit virtual folder."
   "Return non-nil when summary line format is changed."
   (not (string=
        wl-summary-buffer-line-format
-       (or (elmo-object-load (expand-file-name 
+       (or (elmo-object-load (expand-file-name
                               wl-summary-line-format-file
                               (elmo-folder-msgdb-path
                                wl-summary-buffer-elmo-folder))
@@ -4199,7 +4092,8 @@ If ARG, exit virtual folder."
 
 (defun wl-summary-line-attached ()
   (let ((content-type (elmo-msgdb-overview-entity-get-extra-field
-                      wl-message-entity "content-type")))
+                      wl-message-entity "content-type"))
+       (case-fold-search t))
     (if (and content-type
             (string-match "multipart/mixed" content-type))
        "@"
@@ -4257,25 +4151,25 @@ If ARG, exit virtual folder."
 
 (defvar wl-summary-move-spec-plugged-alist
   (` ((new . ((t . nil)
-             (p . (, wl-summary-new-mark))
+             (p . (, elmo-msgdb-new-mark))
              (p . (, (wl-regexp-opt
-                      (list wl-summary-unread-uncached-mark
-                            wl-summary-unread-cached-mark))))
-             (p . (, (regexp-quote wl-summary-important-mark)))))
+                      (list elmo-msgdb-unread-uncached-mark
+                            elmo-msgdb-unread-cached-mark))))
+             (p . (, (regexp-quote elmo-msgdb-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))))))))
+                         (list elmo-msgdb-new-mark
+                               elmo-msgdb-unread-uncached-mark
+                               elmo-msgdb-unread-cached-mark))))
+                (p . (, (regexp-quote elmo-msgdb-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)))))
+             (p . (, elmo-msgdb-unread-cached-mark))
+             (p . (, (regexp-quote elmo-msgdb-important-mark)))))
       (unread . ((t . nil)
-                (p . (, wl-summary-unread-cached-mark))
-                (p . (, (regexp-quote wl-summary-important-mark))))))))
+                (p . (, elmo-msgdb-unread-cached-mark))
+                (p . (, (regexp-quote elmo-msgdb-important-mark))))))))
 
 (defsubst wl-summary-next-message (num direction hereto)
   (if wl-summary-buffer-next-message-function
@@ -4374,7 +4268,8 @@ If ARG, exit virtual folder."
                (wl-summary-delete-all-temp-marks)
                (encode-coding-region
                 (point-min) (point-max)
-                (or (mime-charset-to-coding-system charset 'LF)
+                (or (and wl-on-mule ; one in mcs-ltn1 cannot take 2 arg.
+                         (mime-charset-to-coding-system charset 'LF))
                     ;; Mule 2 doesn't have `*ctext*unix'.
                     (mime-charset-to-coding-system charset)))
                (write-region-as-binary (point-min)(point-max)
@@ -4989,14 +4884,14 @@ Use function list is `wl-summary-write-current-folder-functions'."
        ()
       (setq skip-pmark-regexp
            (wl-regexp-opt (list " "
-                                wl-summary-unread-cached-mark
-                                wl-summary-important-mark))))
+                                elmo-msgdb-unread-cached-mark
+                                elmo-msgdb-important-mark))))
     (while (and skip
                (not (if downward (eobp) (bobp))))
       (if downward
          (forward-line 1)
        (forward-line -1))
-      (setq skip (or (string-match skip-tmark-regexp 
+      (setq skip (or (string-match skip-tmark-regexp
                                   (save-excursion
                                     (wl-summary-temp-mark)))
                     (and skip-pmark-regexp
@@ -5148,7 +5043,7 @@ Use function list is `wl-summary-write-current-folder-functions'."
         (num (or number (wl-summary-message-number)))
         (wl-mime-charset      wl-summary-buffer-mime-charset)
         (default-mime-charset wl-summary-buffer-mime-charset)
-        fld-buf fld-win thr-entity)
+        no-folder-mark 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
@@ -5167,23 +5062,25 @@ Use function list is `wl-summary-write-current-folder-functions'."
              (if (setq fld-win (get-buffer-window fld-buf))
                  (delete-window fld-win)))
          (setq wl-current-summary-buffer (current-buffer))
-         (wl-summary-mark-as-read
-          num
-          ;; not fetched, then change server-mark.
-          (if (wl-message-redisplay folder num 'mime
-                                    (or force-reload
-                                        (string= (elmo-folder-name-internal
-                                                  folder)
-                                                 wl-draft-folder)))
-              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)))
+         (setq no-folder-mark
+               ;; If cache is used, change folder-mark.
+               (if (wl-message-redisplay folder num
+                                         'mime
+                                         (or
+                                          force-reload
+                                          (string= (elmo-folder-name-internal
+                                                    folder)
+                                                   wl-draft-folder)))
+                   nil
+                 ;; plugged, then leave folder-mark.
+                 (if (and (not (elmo-folder-local-p
+                                wl-summary-buffer-elmo-folder))
+                          (elmo-folder-plugged-p
+                           wl-summary-buffer-elmo-folder))
+                     'leave)))
+         (if (elmo-message-use-cache-p folder num)
+             (elmo-message-set-cached folder num t))
+         (wl-summary-mark-as-read num no-folder-mark)
          (setq wl-summary-buffer-current-msg num)
          (when wl-summary-recenter
            (recenter (/ (- (window-height) 2) 2))