* wl-addrmgr.el (wl-addrmgr-replace-field): Use `point-at-bol'.
[elisp/wanderlust.git] / wl / wl-action.el
index f1dedd9..223abe5 100644 (file)
@@ -53,7 +53,6 @@
 
 ;; Set mark
 (defun wl-summary-set-mark (&optional set-mark number interactive data)
-  (interactive)
   "Set temporary mark SET-MARK on the message with NUMBER.
 NUMBER is the message number to set the mark on.
 INTERACTIVE is set as t if it have to run interactively.
@@ -69,13 +68,14 @@ Return number if put mark succeed"
     (prog1
        (save-excursion
          ;; Put mark
-         (setq visible (or
-                        ;; not-interactive and visible
-                        (and number (wl-summary-jump-to-msg number))
-                        ;; interactive
-                        (and (null number) current))
-               number (or number current)
-               cur-mark (nth 1 (wl-summary-registered-temp-mark number)))
+         (if number
+             ;; Jump to message if cursor is not on the message.
+             (when (and (setq visible (wl-summary-message-visible-p number))
+                        (not (eq number current)))
+               (wl-summary-jump-to-msg number))
+           (setq visible t
+                 number current))
+         (setq cur-mark (nth 1 (wl-summary-registered-temp-mark number)))
          (unless number
            (error "No message"))
          (if (wl-summary-reserve-temp-mark-p cur-mark)
@@ -87,23 +87,27 @@ Return number if put mark succeed"
              (setq data (funcall (wl-summary-action-argument-function action)
                                  (wl-summary-action-symbol action)
                                  number)))
+           ;; Unset the current mark.
            (wl-summary-unset-mark number)
-           (when visible
-             (wl-summary-mark-line set-mark)
-             (when wl-summary-highlight
-               (wl-highlight-summary-current-line))
-             (when data
-               (wl-summary-print-argument number data)))
            ;; Set action.
            (funcall (wl-summary-action-set-function action)
                     number
                     (wl-summary-action-mark action)
                     data)
+           (when visible
+             (wl-summary-put-temp-mark set-mark)
+             (when wl-summary-highlight
+               (wl-highlight-summary-current-line))
+             (when data
+               (wl-summary-print-argument number data)))
+           (when (and (eq wl-summary-buffer-view 'thread)
+                      interactive)
+             (wl-thread-open-children number))
            (set-buffer-modified-p nil)
            ;; Return value.
            number))
       ;; Move the cursor.
-      (if (or interactive (interactive-p))
+      (if interactive
          (if (eq wl-summary-move-direction-downward nil)
              (wl-summary-prev)
            (wl-summary-next))))))
@@ -122,6 +126,7 @@ Return number if put mark succeed"
 
 (defun wl-summary-target-mark-set-action (action)
   (unless (eq (wl-summary-action-symbol action) 'target-mark)
+    (unless wl-summary-buffer-target-mark-list (error "no target"))
     (save-excursion
       (goto-char (point-min))
       (let ((numlist wl-summary-buffer-number-list)
@@ -141,7 +146,7 @@ Return number if put mark succeed"
            (let (wl-summary-buffer-disp-msg)
              (when (setq number (wl-summary-message-number))
                (wl-summary-set-mark (wl-summary-action-mark action)
-                                    number nil data)
+                                    nil nil data)
                (setq wl-summary-buffer-target-mark-list
                      (delq number wl-summary-buffer-target-mark-list)))))
          (forward-line 1))
@@ -168,7 +173,8 @@ Return number if put mark succeed"
          (delq elem wl-summary-buffer-temp-mark-list))))
 
 (defun wl-summary-registered-temp-mark (number)
-  (assq number wl-summary-buffer-temp-mark-list))
+  (and wl-summary-buffer-temp-mark-list
+       (assq number wl-summary-buffer-temp-mark-list)))
 
 (defun wl-summary-collect-temp-mark (mark &optional begin end)
   (if (or begin end)
@@ -210,11 +216,12 @@ Return number if put mark succeed"
       mark-list)))
 
 ;; Unset mark
-(defun wl-summary-unset-mark (&optional number interactive)
+(defun wl-summary-unset-mark (&optional number interactive force)
   "Unset temporary mark of the message with NUMBER.
 NUMBER is the message number to unset the mark.
 If not specified, the message on the cursor position is treated.
 Optional INTERACTIVE is non-nil when it should be called interactively.
+If optional FORCE is non-nil, remove scored mark too.
 Return number if put mark succeed"
   (interactive)
   (save-excursion
@@ -222,25 +229,28 @@ Return number if put mark succeed"
     (let ((buffer-read-only nil)
          visible mark action)
       (if number
-         (setq visible (wl-summary-jump-to-msg number)) ; can be nil
-       (setq visible t))
-      (setq number (or number (wl-summary-message-number)))
+         ;; Jump to message
+         (when (and (setq visible (wl-summary-message-visible-p number))
+                    (not (eq number (wl-summary-message-number))))
+           (wl-summary-jump-to-msg number))
+       (setq visible t
+             number (wl-summary-message-number)))
+      (setq mark (wl-summary-temp-mark))
+      ;; Remove from temporal mark structure.
+      (wl-summary-unregister-target-mark number)
+      (wl-summary-unregister-temp-mark number)
       ;; Delete mark on buffer.
       (when visible
-       (setq mark (wl-summary-temp-mark))
        (unless (string= mark " ")
-         (delete-backward-char 1)
-         (insert (or (wl-summary-get-score-mark number)
-                     " "))
+         (wl-summary-put-temp-mark
+          (or (unless force (wl-summary-get-score-mark number))
+              " "))
          (setq action (assoc mark wl-summary-mark-action-list))
          (when wl-summary-highlight
            (wl-highlight-summary-current-line))
          (when (wl-summary-action-argument-function action)
            (wl-summary-remove-argument)))
-       (set-buffer-modified-p nil))
-      ;; Remove from temporal mark structure.
-      (wl-summary-unregister-target-mark number)
-      (wl-summary-unregister-temp-mark number)))
+       (set-buffer-modified-p nil))))
   ;; Move the cursor.
   ;;  (if (or interactive (interactive-p))
   ;;      (if (eq wl-summary-move-direction-downward nil)
@@ -268,40 +278,30 @@ Return number if put mark succeed"
       (let ((start (point))
            (refiles (mapcar 'car mark-list))
            (refile-failures 0)
-           refile-len
            dst-msgs                    ; loop counter
            result)
        ;; begin refile...
-       (setq refile-len (length refiles))
        (goto-char start)               ; avoid moving cursor to
                                        ; the bottom line.
-       (message message)
-       (when (> refile-len elmo-display-progress-threshold)
-         (elmo-progress-set 'elmo-folder-move-messages
-                            refile-len message))
-       (setq result nil)
-       (condition-case nil
-           (setq result (elmo-folder-move-messages
-                         wl-summary-buffer-elmo-folder
-                         refiles
-                         (if (eq folder-name 'null)
-                             'null
-                           (wl-folder-get-elmo-folder folder-name))
-                         (wl-summary-buffer-msgdb)
-                         (not (null (cdr dst-msgs)))
-                         nil ; no-delete
-                         nil ; same-number
-                         t))
-         (error nil))
-       (when result            ; succeeded.
-         ;; update buffer.
-         (wl-summary-delete-messages-on-buffer refiles)
-         ;; update wl-summary-buffer-temp-mark-list.
-         (dolist (mark-info mark-list)
-           (setq wl-summary-buffer-temp-mark-list
-                 (delq mark-info wl-summary-buffer-temp-mark-list))))
-       (elmo-progress-clear 'elmo-folder-move-messages)
-       (message (concat message "done"))
+       (elmo-with-progress-display
+           (elmo-folder-move-messages (length refiles))
+           message
+         (setq result nil)
+         (condition-case nil
+             (setq result (elmo-folder-move-messages
+                           wl-summary-buffer-elmo-folder
+                           refiles
+                           (if (eq folder-name 'null)
+                               'null
+                             (wl-folder-get-elmo-folder folder-name))))
+           (error nil))
+         (when result          ; succeeded.
+           ;; update buffer.
+           (wl-summary-delete-messages-on-buffer refiles)
+           ;; update wl-summary-buffer-temp-mark-list.
+           (dolist (mark-info mark-list)
+             (setq wl-summary-buffer-temp-mark-list
+                   (delq mark-info wl-summary-buffer-temp-mark-list)))))
        (wl-summary-set-message-modified)
        ;; Return the operation failed message numbers.
        (if result
@@ -422,29 +422,20 @@ Return number if put mark succeed"
   (wl-summary-move-mark-list-messages mark-list
                                      (wl-summary-get-dispose-folder
                                       (wl-summary-buffer-folder-name))
-                                     "Disposing messages..."))
+                                     "Disposing messages"))
 
 ;; Delete action.
 (defun wl-summary-exec-action-delete (mark-list)
   (wl-summary-move-mark-list-messages mark-list
                                      'null
-                                     "Deleting messages..."))
+                                     "Deleting messages"))
 
 ;; Refile action
 (defun wl-summary-set-action-refile (number mark data)
-  (let ((policy (wl-get-assoc-list-value wl-refile-policy-alist
-                                        (wl-summary-buffer-folder-name)))
-       (elem wl-summary-mark-action-list))
-    (if (eq policy 'copy)
-       (while elem
-         (when (eq (wl-summary-action-symbol (car elem)) 'copy)
-           (wl-summary-register-temp-mark number
-                                          (wl-summary-action-mark (car elem))
-                                          data)
-           (setq elem nil))
-         (setq elem (cdr elem)))
-      (wl-summary-register-temp-mark number mark data)
-      (setq wl-summary-buffer-prev-refile-destination data))))
+  (when (null data)
+    (error "Destination folder is empty"))
+  (wl-summary-register-temp-mark number mark data)
+  (setq wl-summary-buffer-prev-refile-destination data))
 
 (defun wl-summary-get-refile-destination (action number)
   "Decide refile destination."
@@ -454,42 +445,28 @@ Return number if put mark succeed"
   (save-excursion
     (let ((start (point))
          (failures 0)
-         (refile-len (length mark-list))
-         dst-msgs ; loop counter
-         result)
+         dst-msgs)
       ;; begin refile...
-      (setq dst-msgs
-           (wl-summary-make-destination-numbers-list mark-list))
+      (setq dst-msgs (wl-summary-make-destination-numbers-list mark-list))
       (goto-char start)        ; avoid moving cursor to the bottom line.
-      (when (> refile-len elmo-display-progress-threshold)
-       (elmo-progress-set 'elmo-folder-move-messages
-                          refile-len "Refiling messages..."))
-      (while dst-msgs
-       (setq result nil)
-       (condition-case nil
-           (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)
-                         (not (null (cdr dst-msgs)))
-                         nil ; no-delete
-                         nil ; same-number
-                         t))
-         (error nil))
-       (if result              ; succeeded.
-           (progn
-             ;; update buffer.
-             (wl-summary-delete-messages-on-buffer (cdr (car dst-msgs)))
-             (setq wl-summary-buffer-temp-mark-list
-                   (wl-delete-associations 
-                    (cdr (car dst-msgs))
-                    wl-summary-buffer-temp-mark-list)))
-         (setq failures
-               (+ failures (length (cdr (car dst-msgs))))))
-       (setq dst-msgs (cdr dst-msgs)))
-      (elmo-progress-clear 'elmo-folder-move-messages)
+      (elmo-with-progress-display
+         (elmo-folder-move-messages (length mark-list))
+         "Refiling messages"
+       (dolist (pair dst-msgs)
+         (if (condition-case nil
+                 (elmo-folder-move-messages
+                  wl-summary-buffer-elmo-folder
+                  (cdr pair)
+                  (wl-folder-get-elmo-folder (car pair)))
+               (error nil))
+             (progn
+               ;; update buffer.
+               (wl-summary-delete-messages-on-buffer (cdr pair))
+               (setq wl-summary-buffer-temp-mark-list
+                     (wl-delete-associations
+                      (cdr pair)
+                      wl-summary-buffer-temp-mark-list)))
+           (setq failures (+ failures (length (cdr pair)))))))
       failures)))
 
 ;; Copy action
@@ -500,42 +477,30 @@ Return number if put mark succeed"
   (save-excursion
     (let ((start (point))
          (failures 0)
-         (refile-len (length mark-list))
-         dst-msgs ; loop counter
-         result)
+         dst-msgs)
       ;; begin refile...
       (setq dst-msgs
            (wl-summary-make-destination-numbers-list mark-list))
       (goto-char start)        ; avoid moving cursor to the bottom line.
-      (when (> refile-len elmo-display-progress-threshold)
-       (elmo-progress-set 'elmo-folder-move-messages
-                          refile-len "Copying messages..."))
-      (while dst-msgs
-       (setq result nil)
-       (condition-case nil
-           (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)
-                           (not (null (cdr dst-msgs)))
-                           t ; t is no-delete (copy)
-                           nil ; same-number
-                           t))
-         (error nil))
-       (if result              ; succeeded.
-           (progn
-             ;; update buffer.
-             (wl-summary-delete-copy-marks-on-buffer (cdr (car dst-msgs)))
-             (setq wl-summary-buffer-temp-mark-list
-                   (wl-delete-associations 
-                    (cdr (car dst-msgs))
-                    wl-summary-buffer-temp-mark-list)))
-         (setq failures
-               (+ failures (length (cdr (car dst-msgs))))))
-       (setq dst-msgs (cdr dst-msgs)))
-      (elmo-progress-clear 'elmo-folder-move-messages)
+      (elmo-with-progress-display
+         (elmo-folder-move-messages (length mark-list))
+         "Copying messages"
+       (dolist (pair dst-msgs)
+         (if (condition-case nil
+                 (elmo-folder-move-messages
+                  wl-summary-buffer-elmo-folder
+                  (cdr pair)
+                  (wl-folder-get-elmo-folder (car pair))
+                  'no-delete)
+               (error nil))
+             (progn
+               ;; update buffer.
+               (wl-summary-delete-copy-marks-on-buffer (cdr pair))
+               (setq wl-summary-buffer-temp-mark-list
+                     (wl-delete-associations
+                      (cdr pair)
+                      wl-summary-buffer-temp-mark-list)))
+           (setq failures (+ failures (length (cdr pair)))))))
       failures)))
 
 ;; Prefetch.
@@ -560,7 +525,7 @@ Return number if put mark succeed"
 ;; Resend.
 (defun wl-summary-get-resend-address (action number)
   "Decide resend address."
-  (completing-read "Resend message to: " 'wl-complete-address))
+  (wl-address-read-from-minibuffer "Resend message to: "))
 
 (defun wl-summary-exec-action-resend (mark-list)
   (let ((failure 0))
@@ -579,10 +544,9 @@ Return number if put mark succeed"
   "Resend the message with NUMBER to ADDRESS."
   (message "Resending message to %s..." address)
   (let ((folder wl-summary-buffer-elmo-folder))
-    (save-excursion
+    (with-current-buffer (get-buffer-create " *wl-draft-resend*")
       ;; We first set up a normal mail buffer.
-      (set-buffer (get-buffer-create " *wl-draft-resend*"))
-      (buffer-disable-undo (current-buffer))
+      (set-buffer-multibyte nil)
       (erase-buffer)
       (setq wl-sent-message-via nil)
       ;; Insert our usual headers.
@@ -600,11 +564,16 @@ Return number if put mark succeed"
       (let ((beg (point)))
        ;; Insert the message to be resent.
        (insert
-        (with-temp-buffer
-          (elmo-message-fetch folder number
-                              (elmo-make-fetch-strategy 'entire)
-                              nil (current-buffer) 'unread)
-          (buffer-string)))
+        ;; elmo-message-fetch is erase current buffer before fetch message
+        (elmo-message-fetch-string folder number
+                                   (if wl-summary-resend-use-cache
+                                       (elmo-make-fetch-strategy
+                                        'entire 'maybe nil
+                                        (elmo-file-cache-get-path
+                                         (elmo-message-field
+                                          folder number 'message-id)))
+                                     (elmo-make-fetch-strategy 'entire))
+                                   'unread))
        (goto-char (point-min))
        (search-forward "\n\n")
        (forward-char -1)
@@ -621,6 +590,7 @@ Return number if put mark succeed"
        (goto-char beg)
        (when (looking-at "From ")
          (replace-match "X-From-Line: ")))
+      (run-hooks 'wl-summary-resend-hook)
       ;; Send it.
       (wl-draft-dispatch-message)
       (kill-buffer (current-buffer)))
@@ -633,8 +603,8 @@ Return number if put mark succeed"
          (buffer-read-only nil)
          (buf (current-buffer))
          sol eol rs re)
+      (setq sol (point-at-bol))
       (beginning-of-line)
-      (setq sol (point))
       (search-forward "\r")
       (forward-char -1)
       (setq eol (point))
@@ -652,11 +622,11 @@ Return number if put mark succeed"
   (save-excursion
     (save-restriction
       (let (numbers)
-       (narrow-to-region (or begin (point-min))(or end (point-max)))
+       (wl-summary-narrow-to-region (or begin (point-min))(or end (point-max)))
        (goto-char (point-min))
        ;; for thread...
        (if (eq wl-summary-buffer-view 'thread)
-           (let (number entity mark-info)
+           (let (number entity)
              (while (not (eobp))
                (setq numbers (cons (wl-summary-message-number) numbers)
                      entity (wl-thread-get-entity number))
@@ -665,36 +635,37 @@ Return number if put mark succeed"
                  (dolist (msg (wl-thread-get-children-msgs number))
                    (setq numbers (cons msg numbers))))
                (forward-line 1)))
-         (let (number mark-info)
+         (let (number)
            (while (not (eobp))
              (setq numbers (cons (wl-summary-message-number) numbers))
              (forward-line 1))))
-       numbers))))
+       (nreverse (delq nil numbers))))))
 
 (defun wl-summary-exec (&optional numbers)
   (interactive)
   (let ((failures 0)
        collected pair action modified)
     (dolist (action wl-summary-mark-action-list)
-      (setq collected (cons (cons 
+      (setq collected (cons (cons
                             (wl-summary-action-mark action)
                             nil) collected)))
     (dolist (mark-info wl-summary-buffer-temp-mark-list)
-      (if numbers
-         (when (memq (nth 0 mark-info) numbers)
-           (setq pair (assoc (nth 1 mark-info) collected)))
-       (setq pair (assoc (nth 1 mark-info) collected)))
-      (setq pair (assoc (nth 1 mark-info) collected))
-      (setcdr pair (cons mark-info (cdr pair))))
+      (setq pair
+           (when (or (null numbers)
+                     (memq (nth 0 mark-info) numbers))
+             (assoc (nth 1 mark-info) collected)))
+      (if pair
+         (setcdr pair (cons mark-info (cdr pair)))))
     ;; collected is a pair of
     ;; mark-string and a list of mark-info
     (dolist (pair collected)
-      (setq action (assoc (car pair) wl-summary-mark-action-list))
-      (when (and (cdr pair) (wl-summary-action-exec-function action))
-       (setq modified t)
-       (setq failures (+ failures (funcall
-                                   (wl-summary-action-exec-function action)
-                                   (cdr pair))))))
+      (when (cdr pair)
+       (setq action (assoc (car pair) wl-summary-mark-action-list))
+       (when (wl-summary-action-exec-function action)
+         (setq modified t)
+         (setq failures (+ failures (funcall
+                                     (wl-summary-action-exec-function action)
+                                     (cdr pair)))))))
     (when modified
       (wl-summary-set-message-modified))
     (run-hooks 'wl-summary-exec-hook)
@@ -707,7 +678,7 @@ Return number if put mark succeed"
       (setq wl-message-buffer nil))
     (set-buffer-modified-p nil)
     (when (> failures 0)
-      (format "%d execution(s) were failed" failures))))
+      (message "%d execution(s) were failed" failures))))
 
 (defun wl-summary-exec-region (beg end)
   (interactive "r")
@@ -721,7 +692,7 @@ Return number if put mark succeed"
                      default)
              'wl-folder-complete-folder
              nil nil (or init wl-default-spec)
-             'wl-read-folder-hist)))
+             'wl-read-folder-history)))
     (if (or (string= fld wl-default-spec)
            (string= fld ""))
        (setq fld default))
@@ -739,54 +710,109 @@ Return number if put mark succeed"
                                      fld))))
     fld))
 
-(defun wl-summary-print-argument (msg-num folder)
+(defun wl-summary-print-argument (msg-num data)
   "Print action argument on line."
-  (when folder
+  (when data
     (wl-summary-remove-argument)
     (save-excursion
       (let ((inhibit-read-only t)
-           (folder (copy-sequence folder))
+           (data (copy-sequence data))
            (buffer-read-only nil)
            len rs re c)
-       (setq len (string-width folder))
+       (setq len (string-width data))
        (if (< len 1) ()
          ;;(end-of-line)
          (beginning-of-line)
          (search-forward "\r")
          (forward-char -1)
          (setq re (point))
-         (setq c 0)
-         (while (< c len)
-           (forward-char -1)
-           (setq c (+ c (char-width (following-char)))))
-         (and (> c len) (setq folder (concat " " folder)))
-         (setq rs (point))
-         (when wl-summary-width
-           (put-text-property rs re 'invisible t))
+         (let ((width (cond (wl-summary-width
+                             (1- wl-summary-width))
+                            (wl-summary-print-argument-within-window
+                             (1- (window-width)))))
+               (c (current-column))
+               (padding 0))
+           (if (and width
+                    (> (+ c len) width))
+               (progn
+                 (move-to-column width)
+                 (setq c (current-column))
+                 (while (> (+ c len) width)
+                   (forward-char -1)
+                   (setq c (current-column)))
+                 (when (< (+ c len) width)
+                   (setq data (concat " " data)))
+                 (setq rs (point))
+                 (put-text-property rs re 'invisible t))
+             (when (and width
+                        (> (setq padding (- width len c)) 0))
+               (setq data (concat (make-string padding (string-to-char " "))
+                                  data)))
+             (setq rs (1- re))))
          (put-text-property rs re 'wl-summary-action-argument t)
          (goto-char re)
-         (wl-highlight-action-argument-string folder)
-         (insert folder)
+         (wl-highlight-action-argument-string data)
+         (insert data)
          (set-buffer-modified-p nil))))))
 
 (defsubst wl-summary-reserve-temp-mark-p (mark)
   "Return t if temporal MARK should be reserved."
   (member mark wl-summary-reserve-mark-list))
 
+;; Refile prev destination
 (defun wl-summary-refile-prev-destination ()
   "Refile message to previously refiled destination."
   (interactive)
   (funcall (symbol-function 'wl-summary-refile)
-          wl-summary-buffer-prev-refile-destination
-          (wl-summary-message-number))
-  (if (eq wl-summary-move-direction-downward nil)
+          (wl-summary-message-number)
+          wl-summary-buffer-prev-refile-destination)
+  (if (and (interactive-p)
+          (eq wl-summary-move-direction-downward nil))
       (wl-summary-prev)
     (wl-summary-next)))
 
-(defsubst wl-summary-no-auto-refile-message-p (msg)
-  (member (elmo-msgdb-get-mark (wl-summary-buffer-msgdb) msg)
+(defun wl-summary-refile-prev-destination-region (beg end)
+  "Refile messages in the region to previously refiled destination."
+  (interactive "r")
+  (wl-summary-mark-region-subr 'wl-summary-refile
+                              beg end
+                              wl-summary-buffer-prev-refile-destination))
+
+(defun wl-thread-refile-prev-destination (arg)
+  "Refile messages in the thread to previously refiled destination."
+  (interactive "P")
+  (wl-thread-call-region-func
+   'wl-summary-refile-prev-destination-region
+   arg))
+
+(defun wl-summary-target-mark-refile-prev-destination ()
+  "Refile messages with target mark to previously refiled destination."
+  (interactive)
+  (let ((elem wl-summary-mark-action-list)
+       action)
+    (while elem
+      (when (eq (wl-summary-action-symbol (car elem)) 'refile)
+       (setq action (car elem))
+       (setq elem nil))
+      (setq elem (cdr elem)))
+    (wl-summary-target-mark-set-action
+     (list
+      (car action)
+      'refile-prev-destination
+      (lambda (&rest args) wl-summary-buffer-prev-refile-destination)
+      (nth 2 action)
+      (nth 3 action)
+      (nth 4 action)
+      (nth 6 action)))))
+
+(defsubst wl-summary-no-auto-refile-message-p (number)
+  (member (wl-summary-message-mark wl-summary-buffer-elmo-folder number)
          wl-summary-auto-refile-skip-marks))
 
+(defvar wl-auto-refile-guess-functions
+  '(wl-refile-guess-by-rule)
+  "*List of functions which is used for guessing refile destination folder.")
+
 (defun wl-summary-auto-refile (&optional open-all)
   "Set refile mark automatically according to 'wl-refile-guess-by-rule'."
   (interactive "P")
@@ -799,7 +825,7 @@ Return number if put mark succeed"
           checked-dsts
           (count 0)
           number dst thr-entity)
-      (goto-line 1)
+      (goto-char (point-min))
       (while (not (eobp))
        (setq number (wl-summary-message-number))
        (dolist (number (cons number
@@ -810,13 +836,15 @@ Return number if put mark succeed"
                                               (wl-thread-get-entity number))))
                                   (wl-thread-entity-get-descendant
                                    thr-entity))))
-         (when (and (not (wl-summary-no-auto-refile-message-p
-                          number))
+         (when (and (not (wl-summary-no-auto-refile-message-p number))
+                    (not (wl-summary-reserve-temp-mark-p
+                          (nth 1 (wl-summary-registered-temp-mark number))))
                     (setq dst
                           (wl-folder-get-realname
-                           (wl-refile-guess-by-rule
-                            (elmo-msgdb-overview-get-entity
-                             number (wl-summary-buffer-msgdb)))))
+                           (wl-refile-guess
+                            (elmo-message-entity wl-summary-buffer-elmo-folder
+                                                 number)
+                            wl-auto-refile-guess-functions)))
                     (not (equal dst spec))
                     (let ((pair (assoc dst checked-dsts))
                           ret)
@@ -849,7 +877,7 @@ If optional argument NUMBER is specified, unmark message specified by NUMBER."
   (interactive "r")
   (save-excursion
     (save-restriction
-      (narrow-to-region beg end)
+      (wl-summary-narrow-to-region beg end)
       (goto-char (point-min))
       (if (eq wl-summary-buffer-view 'thread)
          (progn
@@ -870,7 +898,7 @@ If optional argument NUMBER is specified, unmark message specified by NUMBER."
 (defun wl-summary-mark-region-subr (function beg end data)
   (save-excursion
     (save-restriction
-      (narrow-to-region beg end)
+      (wl-summary-narrow-to-region beg end)
       (goto-char (point-min))
       (if (eq wl-summary-buffer-view 'thread)
          (progn
@@ -881,22 +909,19 @@ If optional argument NUMBER is specified, unmark message specified by NUMBER."
                     children)
                (if (wl-thread-entity-get-opened entity)
                    ;; opened...delete line.
-                   (funcall function number data)
+                   (funcall function nil data)
                  ;; closed
                  (setq children (wl-thread-get-children-msgs number))
                  (while children
                    (funcall function (pop children) data)))
                (forward-line 1))))
        (while (not (eobp))
-         (funcall function (wl-summary-message-number) data)
+         (funcall function nil data)
          (forward-line 1))))))
 
 (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)))))
+  (wl-summary-target-mark-region (point-min) (point-max)))
 
 (defun wl-summary-delete-all-mark (mark)
   (goto-char (point-min))
@@ -904,13 +929,15 @@ If optional argument NUMBER is specified, unmark message specified by NUMBER."
     (when (string= (wl-summary-temp-mark) mark)
       (wl-summary-unmark))
     (forward-line 1))
-  (let (deleted)
-    (dolist (mark-info wl-summary-buffer-temp-mark-list)
-      (when (string= (nth 1 mark-info) mark)
-       (setq deleted (cons mark-info deleted))))
-    (dolist (delete deleted)
-      (setq wl-summary-buffer-temp-mark-list
-           (delq delete wl-summary-buffer-temp-mark-list)))))
+  (if (string= mark "*")
+      (setq wl-summary-buffer-target-mark-list nil)
+    (let (deleted)
+      (dolist (mark-info wl-summary-buffer-temp-mark-list)
+       (when (string= (nth 1 mark-info) mark)
+         (setq deleted (cons mark-info deleted))))
+      (dolist (delete deleted)
+       (setq wl-summary-buffer-temp-mark-list
+             (delq delete wl-summary-buffer-temp-mark-list))))))
 
 (defun wl-summary-unmark-all ()
   "Unmark all according to what you input."