* wl-summary.el (wl-summary-next-message):
[elisp/wanderlust.git] / wl / wl-summary.el
index ca55875..ddea1ae 100644 (file)
@@ -97,6 +97,8 @@
 (defvar wl-summary-buffer-prev-folder-func nil)
 (defvar wl-summary-buffer-next-folder-func nil)
 (defvar wl-summary-buffer-exit-func nil)
+(defvar wl-summary-buffer-number-list 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)
 (make-variable-buffer-local 'wl-summary-buffer-prev-folder-func)
 (make-variable-buffer-local 'wl-summary-buffer-next-folder-func)
 (make-variable-buffer-local 'wl-summary-buffer-exit-func)
+(make-variable-buffer-local 'wl-summary-buffer-number-list)
 
 ;; internal functions (dummy)
 (unless (fboundp 'wl-summary-append-message-func-internal)
@@ -777,6 +780,9 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
                                        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))
@@ -861,6 +867,7 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
     (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)
@@ -890,10 +897,12 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
        (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
@@ -1138,6 +1147,7 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
   (setq wl-summary-buffer-msgdb (elmo-msgdb-clear)) ;;'(nil nil nil nil))
   (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)
@@ -1153,7 +1163,8 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
                     folder))
         (range (or force-range (wl-summary-input-range folder)))
         mes seen-list killed-list)
-    (cond ((string= range "all")
+    (cond ((or (string= range "all")
+              (string= range "all-visible"))
           ;; initialize buffer local databases.
           (unless (elmo-folder-plugged-p folder) ; forbidden
             (error "Unplugged"))
@@ -1171,7 +1182,7 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
           (elmo-clear-killed wl-summary-buffer-folder-name)
           (condition-case nil
               (setq mes (wl-summary-sync-update3 seen-list unset-cursor
-                                                 'sync-all))
+                                                 (string= range "all")))
             (quit
              ;; Resume killed-list if quit.
              (message "") ; clear minibuffer.
@@ -1918,7 +1929,8 @@ If ARG is non-nil, checking is omitted."
              (progn
                (delete-region (match-beginning 0) (match-end 0))
                (delete-char 1) ; delete '\n'
-               )))
+               (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))
@@ -2163,7 +2175,7 @@ If ARG is non-nil, checking is omitted."
     (wl-folder-confirm-existence folder 'force)
     (message "Checking folder diff...")
     (elmo-commit folder)
-    (setq in-folder (elmo-list-folder folder))
+    (setq in-folder (elmo-list-folder folder sync-all))
     (setq in-db (unless sync-all (sort (mapcar 'car number-alist) '<)))
     (if (not elmo-use-killed-list)
        (setq diff (if (eq (elmo-folder-get-type folder) 'multi)
@@ -2290,6 +2302,8 @@ If ARG is non-nil, checking is omitted."
            (message "Updating thread...done")
 ;;;        (set-buffer cur-buf)
            ))
+      (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))
@@ -2602,7 +2616,8 @@ If ARG, without confirm."
                   wl-summary-buffer-number-regexp
                   wl-summary-buffer-message-modified
                   wl-summary-buffer-mark-modified
-                  wl-summary-buffer-thread-modified)
+                  wl-summary-buffer-thread-modified
+                  wl-summary-buffer-number-list)
                 (and (eq wl-summary-buffer-view 'thread)
                      '(wl-thread-entity-hashtb
                        wl-thread-entities
@@ -2678,6 +2693,12 @@ If ARG, without confirm."
       (delete-window mes-win)
       (run-hooks 'wl-summary-toggle-disp-off-hook))))
 
+(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 folder scan-type other-window
                                              sticky interactive scoring)
   "Display target folder on summary."
@@ -2717,15 +2738,21 @@ If ARG, without confirm."
          (let ((case-fold-search nil)
                (inhibit-read-only t)
                (buffer-read-only nil))
+           ;; 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))))
            (erase-buffer)
-           ;; resume summary cache
+           ;; Resume summary view
            (if wl-summary-cache-use
                (let* ((dir (elmo-msgdb-expand-path fld))
                       (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
@@ -2735,21 +2762,20 @@ If ARG, without confirm."
                    (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-thread-resume-entity fld)
+                   (wl-summary-make-number-list)))
+             (setq wl-summary-buffer-view
+                   (wl-summary-load-file-object
+                    (expand-file-name wl-summary-view-file
+                                      (elmo-msgdb-expand-path fld))))
+             (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))
       (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
@@ -2777,10 +2803,9 @@ If ARG, without confirm."
          (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 nil)))
                (cond ((and wl-auto-select-first unreadp)
                       (setq retval 'disp-msg))
                      ((not unreadp)
@@ -2790,8 +2815,9 @@ If ARG, without confirm."
                (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))
@@ -2898,7 +2924,7 @@ If ARG, without confirm."
 
 (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 "^\\[.*\\]" "")))
 
@@ -4302,7 +4328,7 @@ If ARG, exit virtual folder."
                  (unless no-server-update
                    (elmo-unmark-important folder (list number) msgdb)
                    (elmo-msgdb-global-mark-delete message-id))
-                 ;; Remove cache if local it is folder.
+                 ;; Remove cache if local folder.
                  (if (elmo-folder-local-p folder)
                      (elmo-cache-delete message-id folder number))
                  (when visible
@@ -4462,107 +4488,81 @@ If ARG, exit virtual folder."
        (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-folder-name)
+                                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-msgdb-list-messages-mark-match
+                                         wl-summary-buffer-msgdb
+                                         (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)
+                  (while nums
+                    (if (and wl-summary-buffer-target-mark-list
+                             (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 ()
   (save-excursion
@@ -4612,7 +4612,7 @@ If ARG, exit virtual 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"))
+                           "no-sync" "rescan-noscore" "all-visible"))
        (default (or (wl-get-assoc-list-value
                      wl-folder-sync-range-alist
                      folder)
@@ -5078,7 +5078,7 @@ Reply to author if invoked with ARG."
                  (split-window-vertically))
              (other-window 1)
              (when (setq mes-buf (wl-message-get-original-buffer))
-               (wl-draft-reply mes-buf (not arg) summary-buf)
+               (wl-draft-reply mes-buf arg summary-buf)
                (unless without-setup-hook
                  (run-hooks 'wl-mail-setup-hook)))
              t)))))
@@ -5942,22 +5942,13 @@ Use function list is `wl-summary-write-current-folder-functions'."
        (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))))))
+  (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))))))
 
 (defsubst wl-cache-prefetch-p (fld &optional num)
   (cond ((and num wl-cache-prefetch-folder-type-list)
@@ -5981,7 +5972,7 @@ Use function list is `wl-summary-write-current-folder-functions'."
 
 (defun wl-cache-prefetch-next (fld msg &optional summary)
   (if (wl-cache-prefetch-p fld)
-      (if (not elmo-use-buffer-cache)
+      (if 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)))
@@ -6020,7 +6011,7 @@ Use function list is `wl-summary-write-current-folder-functions'."
                                                              folder next)))))
                  (if wl-cache-prefetch-debug
                      (message "Reading %d..." msg))
-                 (elmo-buffer-cache-message folder next msgdb)
+                 (elmo-buffer-cache-message folder next msgdb nil 'unread)
                  (if wl-cache-prefetch-debug
                      (message "Reading %d... done" msg))))))))))