* wl-highlight.el (wl-highlight-folder-opened-regexp)
[elisp/wanderlust.git] / wl / wl-folder.el
index a2f90f6..64c2b09 100644 (file)
@@ -77,7 +77,7 @@
 (make-variable-buffer-local 'wl-folder-buffer-cur-point)
 
 (defconst wl-folder-entity-regexp "^\\([ ]*\\)\\(\\[[\\+-]\\]\\)?\\([^\\[].+\\):[-*0-9]+/[-*0-9]+/[-*0-9]+")
-(defconst wl-folder-group-regexp  "^\\([ ]*\\)\\[\\([\\+-]\\)\\]\\(.+\\):[-0-9-]+/[0-9-]+/[0-9-]+\n")
+(defconst wl-folder-group-regexp  "^\\([ ]*\\)\\[\\([\\+-]\\)\\]\\(.+\\):[0-9-]+/[0-9-]+/[0-9-]+\n")
 ;;                             1:indent 2:opened 3:group-name
 (defconst wl-folder-unsync-regexp ":[^0\\*][0-9]*/[0-9\\*-]+/[0-9\\*-]+$")
 
@@ -865,19 +865,16 @@ Optional argument ARG is repeart count."
                     (if (wl-string-match-member entity wl-strict-diff-folders)
                         (elmo-strict-folder-diff folder)
                       (elmo-folder-diff folder)))
+                (elmo-open-error
+                 (signal (car err) (cdr err)))
                 (error
                  ;; maybe not exist folder.
-                 (if (and (not (or (memq 'elmo-open-error
-                                         (get (car err) 'error-conditions))
-                                   (memq 'elmo-imap4-bye-error
-                                         (get (car err) 'error-conditions))))
-                          (not (elmo-folder-exists-p folder)))
+                 (if (not (elmo-folder-exists-p folder))
                      (wl-folder-create-subr folder)
                    (signal (car err) (cdr err))))))
         (new    (elmo-diff-new nums))
         (unread (elmo-diff-unread nums))
-        (all    (elmo-diff-all nums))
-        unsync nomif)
+        (all    (elmo-diff-all nums)))
     (if (and (eq wl-folder-notify-deleted 'sync)
             (or (and new    (> 0 new))
                 (and unread (> 0 unread))
@@ -892,10 +889,11 @@ Optional argument ARG is repeart count."
        (setq new    (and new    (max 0 new))
              unread (and unread (max 0 unread))
              all    (and all    (max 0 all))))
-      (setq unread (or (and unread (- unread (or new 0)))
-                      (elmo-folder-get-info-unread folder)
-                      (or (cdr (assq 'unread
-                                     (elmo-folder-count-flags folder))) 0)))
+      (setq unread (if unread
+                      (- unread (or new 0))
+                    (or (elmo-folder-get-info-unread folder)
+                        (cdr (assq 'unread (elmo-folder-count-flags folder)))
+                        0)))
       (wl-folder-entity-hashtb-set wl-folder-entity-hashtb entity
                                   (list new unread all)
                                   (get-buffer wl-folder-buffer-name)))
@@ -1043,36 +1041,32 @@ If current line is group folder, check all sub entries."
    ((stringp entity)
     (let* ((folder (wl-folder-get-elmo-folder entity))
           (nums (wl-folder-get-entity-info entity))
-          (wl-summary-highlight (if (or (wl-summary-sticky-p folder)
-                                        (wl-summary-always-sticky-folder-p
-                                         folder))
-                                    wl-summary-highlight))
-          wl-auto-select-first new unread sticky)
-      (setq new (or (car nums) 0))
-      (setq unread (or (cadr nums) 0))
-      (if (or (not unread-only)
-             (or (< 0 new) (< 0 unread)))
-         (let ((wl-summary-buffer-name
-                (if (setq sticky (get-buffer (wl-summary-sticky-buffer-name
-                                              (elmo-folder-name-internal
-                                               folder))))
-                    ;; Sticky folder exists.
-                    (wl-summary-sticky-buffer-name
-                     (elmo-folder-name-internal folder))
-                  (concat
-                   wl-summary-buffer-name
-                   (symbol-name this-command))))
-               (wl-summary-use-frame nil)
-               (wl-summary-always-sticky-folder-list nil))
-           (save-window-excursion
-             (save-excursion
-               (wl-summary-goto-folder-subr entity
-                                            (wl-summary-get-sync-range
-                                             folder)
-                                            nil nil nil t)
-               (if sticky
-                   (wl-summary-save-status)
-                 (wl-summary-exit))))))))))
+          (new (or (car nums) 0))
+          (unread (or (cadr nums) 0)))
+      (when (or (not unread-only)
+               (or (> new 0) (> unread 0)))
+       (let ((summary (wl-summary-get-buffer entity))
+             (range (wl-summary-get-sync-range folder)))
+         (if summary
+             (save-selected-window
+               (with-current-buffer summary
+                 (let ((win (get-buffer-window summary t)))
+                   (when win
+                     (select-window win)))
+                 (wl-summary-sync 'unset-cursor range)
+                 (wl-summary-save-status)))
+           (elmo-folder-open folder 'load-msgdb)
+           (unwind-protect
+               (progn
+                 (elmo-folder-synchronize folder nil (eq range 'all))
+                 (wl-folder-set-folder-updated
+                  entity
+                  (list
+                   0
+                   (or (cdr (assq 'unread (elmo-folder-count-flags folder)))
+                       0)
+                   (elmo-folder-length folder))))
+             (elmo-folder-close folder)))))))))
 
 (defun wl-folder-sync-current-entity (&optional unread-only)
   "Synchronize the folder at position.
@@ -1100,39 +1094,34 @@ If current line is group folder, check all subfolders."
        (wl-folder-mark-as-read-all-entity (car flist))
        (setq flist (cdr flist)))))
    ((stringp entity)
-    (let* ((nums (wl-folder-get-entity-info entity))
-          (folder (wl-folder-get-elmo-folder entity))
-          (wl-summary-highlight (if (or (wl-summary-sticky-p folder)
-                                        (wl-summary-always-sticky-folder-p
-                                         folder))
-                                    wl-summary-highlight))
-          wl-auto-select-first new unread sticky)
-      (setq new (or (car nums) 0))
-      (setq unread (or (cadr nums) 0))
-      (if (or (< 0 new) (< 0 unread))
-         (save-window-excursion
-           (save-excursion
-             (let ((wl-summary-buffer-name
-                    (if (setq sticky (get-buffer
-                                      (wl-summary-sticky-buffer-name
-                                       (elmo-folder-name-internal
-                                        folder))))
-                        ;; Sticky folder exists.
-                        (wl-summary-sticky-buffer-name
-                         (elmo-folder-name-internal folder))
-                      (concat
-                       wl-summary-buffer-name
-                       (symbol-name this-command))))
-                   (wl-summary-use-frame nil)
-                   (wl-summary-always-sticky-folder-list nil))
-               (wl-summary-goto-folder-subr entity
-                                            (wl-summary-get-sync-range folder)
-                                            nil)
-               (wl-summary-mark-as-read-all)
-               (if sticky
-                   (wl-summary-save-status)
-                 (wl-summary-exit)))))
-       (sit-for 0))))))
+    (let* ((folder (wl-folder-get-elmo-folder entity))
+          (nums (wl-folder-get-entity-info entity))
+          (new (or (car nums) 0))
+          (unread (or (cadr nums) 0)))
+      (when (or (> new 0) (> unread 0))
+       (let ((summary (wl-summary-get-buffer entity))
+             (range (wl-summary-get-sync-range folder)))
+         (if summary
+             (save-selected-window
+               (with-current-buffer summary
+                 (let ((win (get-buffer-window summary t)))
+                   (when win
+                     (select-window win)))
+                 (wl-summary-sync 'unset-cursor range)
+                 (wl-summary-mark-as-read-all)
+                 (wl-summary-save-status)))
+           (elmo-folder-open folder 'load-msgdb)
+           (unwind-protect
+               (progn
+                 (elmo-folder-synchronize folder nil (eq range 'all))
+                 (elmo-folder-unset-flag
+                  folder
+                  (elmo-folder-list-flagged folder 'unread 'in-msgdb)
+                  'unread)
+                 (wl-folder-set-folder-updated
+                  entity
+                  (list 0 0 (elmo-folder-length folder))))
+             (elmo-folder-close folder)))))))))
 
 (defun wl-folder-mark-as-read-all-current-entity ()
   "Mark as read all messages in the folder at position.
@@ -1774,9 +1763,9 @@ Entering Folder mode calls the value of `wl-folder-mode-hook'."
                                  (wl-highlight-folder-current-line))
                  (setq removed (cdr removed)))
                (remove-text-properties beg (point) '(wl-folder-entity-id)))
-             (let* ((len (length flist))
-                    (mes (> len 100))
-                    (i 0))
+             (elmo-with-progress-display
+                 (wl-folder-insert-entity (length flist))
+                 (format "Inserting group %s" (car entity))
                (while flist
                  (setq ret-val
                        (wl-folder-insert-entity
@@ -1784,15 +1773,8 @@ Entering Folder mode calls the value of `wl-folder-mode-hook'."
                  (setq new    (+ (or new 0) (or (nth 0 ret-val) 0)))
                  (setq unread (+ (or unread 0) (or (nth 1 ret-val) 0)))
                  (setq all    (+ (or all 0) (or (nth 2 ret-val) 0)))
-                 (when (and mes
-                            (> len elmo-display-progress-threshold))
-                   (setq i (1+ i))
-                   (elmo-display-progress
-                    'wl-folder-insert-entity "Inserting group %s..."
-                    (/ (* i 100) len) (car entity)))
-                 (setq flist (cdr flist)))
-               (if (> len 0)
-                   (message "Inserting group %s...done" (car entity))))
+                 (elmo-progress-notify 'wl-folder-insert-entity)
+                 (setq flist (cdr flist))))
              (save-excursion
                (goto-char group-name-end)
                (delete-region (point) (save-excursion (end-of-line)
@@ -2562,37 +2544,30 @@ Use `wl-subscribed-mailing-list'."
          (erase-buffer)
          (wl-folder-insert-entity " " wl-folder-entity)
          (wl-folder-move-path id))
-      (message "Opening all folders...")
-      (wl-folder-open-all-pre)
-      (save-excursion
-       (goto-char (point-min))
-       (while (re-search-forward
-               "^\\([ ]*\\)\\[\\([+]\\)\\]\\(.+\\):[-0-9-]+/[0-9-]+/[0-9-]+$"
-               nil t)
-         (setq indent (wl-match-buffer 1))
-         (setq name (wl-folder-get-entity-from-buffer))
-         (setq entity (wl-folder-search-group-entity-by-name
-                       name
-                       wl-folder-entity))
-         ;; insert as opened
-         (setcdr (assoc (car entity) wl-folder-group-alist) t)
-         (beginning-of-line)
-         (wl-folder-insert-entity indent entity)
-         (delete-region (save-excursion (beginning-of-line)
-                                        (point))
-                        (save-excursion (end-of-line)
-                                        (+ 1 (point))))
-         (when (> len elmo-display-progress-threshold)
-           (setq i (1+ i))
-           (if (or (zerop (% i 5)) (= i len))
-               (elmo-display-progress
-                'wl-folder-open-all "Opening all folders..."
-                (/ (* i 100) len)))))
-       (when (> len elmo-display-progress-threshold)
-         (elmo-display-progress
-          'wl-folder-open-all "Opening all folders..." 100))))
+      (elmo-with-progress-display
+         (wl-folder-open-all (length wl-folder-group-alist))
+         "Opening all folders"
+       (wl-folder-open-all-pre)
+       (save-excursion
+         (goto-char (point-min))
+         (while (re-search-forward
+                 "^\\([ ]*\\)\\[\\([+]\\)\\]\\(.+\\):[-0-9-]+/[0-9-]+/[0-9-]+$"
+                 nil t)
+           (setq indent (wl-match-buffer 1))
+           (setq name (wl-folder-get-entity-from-buffer))
+           (setq entity (wl-folder-search-group-entity-by-name
+                         name
+                         wl-folder-entity))
+           ;; insert as opened
+           (setcdr (assoc (car entity) wl-folder-group-alist) t)
+           (beginning-of-line)
+           (wl-folder-insert-entity indent entity)
+           (delete-region (save-excursion (beginning-of-line)
+                                          (point))
+                          (save-excursion (end-of-line)
+                                          (+ 1 (point))))
+           (elmo-progress-notify 'wl-folder-open-all)))))
     (wl-highlight-folder-path wl-folder-buffer-cur-path)
-    (message "Opening all folders...done")
     (set-buffer-modified-p nil)))
 
 (defun wl-folder-close-all ()
@@ -2646,101 +2621,91 @@ Use `wl-subscribed-mailing-list'."
       t)))
 
 (defun wl-folder-update-access-group (entity new-flist)
-  (let* ((flist (nth 2 entity))
-        (unsubscribes (nth 3 entity))
-        (len (+ (length flist) (length unsubscribes)))
-        (i 0)
-        diff new-unsubscribes removes
-        subscribed-list folder group entry)
-    ;; check subscribed groups
-    (while flist
-      (cond
-       ((listp (car flist))    ;; group
-       (setq group (elmo-string (caar flist)))
+  (let ((flist (nth 2 entity))
+       (unsubscribes (nth 3 entity))
+       diff new-unsubscribes removes
+       subscribed-list folder group entry)
+    (elmo-with-progress-display
+       (wl-folder-update-access-group (+ (length flist) (length unsubscribes)))
+       "Updating access group"
+      ;; check subscribed groups
+      (while flist
        (cond
-        ((assoc group new-flist)       ;; found in new-flist
-         (setq new-flist (delete (assoc group new-flist)
-                                 new-flist))
-         (if (wl-folder-access-subscribe-p (car entity) group)
-             (wl-append subscribed-list (list (car flist)))
-           (wl-append new-unsubscribes (list (car flist)))
-           (setq diff t)))
-        (t
-         (setq wl-folder-group-alist
-               (delete (wl-string-assoc group wl-folder-group-alist)
-                       wl-folder-group-alist))
-         (wl-append removes (list (list group))))))
-       (t                      ;; folder
-       (setq folder (elmo-string (car flist)))
+        ((listp (car flist))   ;; group
+         (setq group (elmo-string (caar flist)))
+         (cond
+          ((assoc group new-flist)     ;; found in new-flist
+           (setq new-flist (delete (assoc group new-flist)
+                                   new-flist))
+           (if (wl-folder-access-subscribe-p (car entity) group)
+               (wl-append subscribed-list (list (car flist)))
+             (wl-append new-unsubscribes (list (car flist)))
+             (setq diff t)))
+          (t
+           (setq wl-folder-group-alist
+                 (delete (wl-string-assoc group wl-folder-group-alist)
+                         wl-folder-group-alist))
+           (wl-append removes (list (list group))))))
+        (t                     ;; folder
+         (setq folder (elmo-string (car flist)))
+         (cond
+          ((member folder new-flist)   ;; found in new-flist
+           (setq new-flist (delete folder new-flist))
+           (if (wl-folder-access-subscribe-p (car entity) folder)
+               (wl-append subscribed-list (list (car flist)))
+             (wl-append new-unsubscribes (list folder))
+             (setq diff t)))
+          (t
+           (wl-append removes (list folder))))))
+       (elmo-progress-notify 'wl-folder-update-access-group)
+       (setq flist (cdr flist)))
+      ;; check unsubscribed groups
+      (while unsubscribes
        (cond
-        ((member folder new-flist)     ;; found in new-flist
-         (setq new-flist (delete folder new-flist))
-         (if (wl-folder-access-subscribe-p (car entity) folder)
-             (wl-append subscribed-list (list (car flist)))
-           (wl-append new-unsubscribes (list folder))
-           (setq diff t)))
+        ((listp (car unsubscribes))
+         (when (setq entry (assoc (caar unsubscribes) new-flist))
+           (setq new-flist (delete entry new-flist))
+           (wl-append new-unsubscribes (list (car unsubscribes)))))
         (t
-         (wl-append removes (list folder))))))
-      (when (> len elmo-display-progress-threshold)
-       (setq i (1+ i))
-       (if (or (zerop (% i 10)) (= i len))
-           (elmo-display-progress
-            'wl-folder-update-access-group "Updating access group..."
-            (/ (* i 100) len))))
-      (setq flist (cdr flist)))
-    ;; check unsubscribed groups
-    (while unsubscribes
-      (cond
-       ((listp (car unsubscribes))
-       (when (setq entry (assoc (caar unsubscribes) new-flist))
-         (setq new-flist (delete entry new-flist))
-         (wl-append new-unsubscribes (list (car unsubscribes)))))
-       (t
-       (when (member (car unsubscribes) new-flist)
-         (setq new-flist (delete (car unsubscribes) new-flist))
-         (wl-append new-unsubscribes (list (car unsubscribes))))))
-      (when (> len elmo-display-progress-threshold)
-       (setq i (1+ i))
-       (if (or (zerop (% i 10)) (= i len))
-           (elmo-display-progress
-            'wl-folder-update-access-group "Updating access group..."
-            (/ (* i 100) len))))
-      (setq unsubscribes (cdr unsubscribes)))
-    ;;
-    (if (or new-flist removes)
-       (setq diff t))
-    (setq new-flist
-         (mapcar '(lambda (x)
-                    (cond ((consp x) (list (car x) 'access))
-                          (t x)))
-                 new-flist))
-    ;; check new groups
-    (let ((new-list new-flist))
-      (while new-list
-       (if (not (wl-folder-access-subscribe-p
-                 (car entity)
-                 (if (listp (car new-list))
-                     (caar new-list)
-                   (car new-list))))
-           ;; auto unsubscribe
-           (progn
-             (wl-append new-unsubscribes (list (car new-list)))
-             (setq new-flist (delete (car new-list) new-flist)))
-         (cond
-          ((listp (car new-list))
-           ;; check group exists
-           (if (wl-string-assoc (caar new-list) wl-folder-group-alist)
-               (progn
-                 (message "%s: group already exists." (caar new-list))
-                 (sit-for 1)
-                 (wl-append new-unsubscribes (list (car new-list)))
-                 (setq new-flist (delete (car new-list) new-flist)))
-             (wl-append wl-folder-group-alist
-                        (list (cons (caar new-list) nil)))))))
-       (setq new-list (cdr new-list))))
-    (if new-flist
-       (message "%d new folder(s)." (length new-flist))
-      (message "Updating access group...done"))
+         (when (member (car unsubscribes) new-flist)
+           (setq new-flist (delete (car unsubscribes) new-flist))
+           (wl-append new-unsubscribes (list (car unsubscribes))))))
+       (elmo-progress-notify 'wl-folder-update-access-group)
+       (setq unsubscribes (cdr unsubscribes)))
+      ;;
+      (if (or new-flist removes)
+         (setq diff t))
+      (setq new-flist
+           (mapcar '(lambda (x)
+                      (cond ((consp x) (list (car x) 'access))
+                            (t x)))
+                   new-flist))
+      ;; check new groups
+      (let ((new-list new-flist))
+       (while new-list
+         (if (not (wl-folder-access-subscribe-p
+                   (car entity)
+                   (if (listp (car new-list))
+                       (caar new-list)
+                     (car new-list))))
+             ;; auto unsubscribe
+             (progn
+               (wl-append new-unsubscribes (list (car new-list)))
+               (setq new-flist (delete (car new-list) new-flist)))
+           (cond
+            ((listp (car new-list))
+             ;; check group exists
+             (if (wl-string-assoc (caar new-list) wl-folder-group-alist)
+                 (progn
+                   (message "%s: group already exists." (caar new-list))
+                   (sit-for 1)
+                   (wl-append new-unsubscribes (list (car new-list)))
+                   (setq new-flist (delete (car new-list) new-flist)))
+               (wl-append wl-folder-group-alist
+                          (list (cons (caar new-list) nil)))))))
+         (setq new-list (cdr new-list)))))
+    (when new-flist
+      (message "%d new folder(s)." (length new-flist)))
     (wl-append new-flist subscribed-list)      ;; new is first
     (run-hooks 'wl-folder-update-access-group-hook)
     (setcdr (cdr entity) (list new-flist new-unsubscribes))