* wl-message.el (wl-message-buffer-cache-buffer-get)
[elisp/wanderlust.git] / wl / wl-thread.el
index ce3ad81..5ac6e9d 100644 (file)
 (defun wl-thread-save-top-list (dir)
   (let ((top-file (expand-file-name wl-thread-entity-list-file dir))
        (entity wl-thread-entity-list)
-       (tmp-buffer (get-buffer-create " *wl-thread-save-top-list*"))
        print-length)
-    (save-excursion
-      (set-buffer tmp-buffer)
-      (erase-buffer)
+    (with-temp-buffer
       (when (file-writable-p top-file)
-       (prin1 entity tmp-buffer)
-       (princ "\n" tmp-buffer)
-       (write-region (point-min) (point-max) top-file nil 'no-msg)
-       (kill-buffer tmp-buffer)))))
+       (prin1 entity (current-buffer))
+       (princ "\n" (current-buffer))
+       (write-region (point-min) (point-max) top-file nil 'no-msg)))))
 
 (defun wl-thread-save-entities (dir)
   (let ((top-file (expand-file-name wl-thread-entity-file dir))
        (entities wl-thread-entities)
-       (tmp-buffer (get-buffer-create " *wl-thread-save-entities*"))
        print-length print-level)
-    (save-excursion
-      (set-buffer tmp-buffer)
-      (erase-buffer)
+    (with-temp-buffer
       (when (file-writable-p top-file)
-       (prin1 entities tmp-buffer)
-       (princ "\n" tmp-buffer)
-       (write-region (point-min) (point-max) top-file nil 'no-msg)
-       (kill-buffer tmp-buffer)))))
+       (prin1 entities (current-buffer))
+       (princ "\n" (current-buffer))
+       (write-region (point-min) (point-max) top-file nil 'no-msg)))))
 
 (defsubst wl-thread-entity-get-number (entity)
   (nth 0 entity))
                                      (nth (- (length curc) 1)
                                           curc))))
                        (wl-thread-entity-get-number curp)))
-    (setcar (cddr to) (wl-append children
-                                (list (car entity))))
+    (wl-thread-entity-set-children to (wl-append children (list (car entity))))
     (setq wl-thread-entities (cons entity wl-thread-entities))
     (elmo-set-hash-val (format "#%d" (car entity)) entity
                       wl-thread-entity-hashtb)))
      (setq entity (wl-thread-get-entity (car msgs))))
    ret-val))
 
-(defsubst wl-thread-entity-get-descendant (entity)
+(defun wl-thread-entity-get-descendant (entity)
   (let (children
        ret-val msgs-stack
        (msgs (list (car entity))))
      (if (null children)
         (while (and (null msgs) msgs-stack)
           (setq msgs (wl-pop msgs-stack)))
-       (setq ret-val (append ret-val (copy-sequence children)))
+       (setq ret-val (nconc ret-val (copy-sequence children)))
        (wl-push msgs msgs-stack)
        (setq msgs children))
      (setq entity (wl-thread-get-entity (car msgs))))
@@ -301,6 +292,7 @@ ENTITY is returned."
       (cdr (memq (car entity) wl-thread-entity-list)))))
 
 (defun wl-thread-jump-to-msg (&optional number)
+  "Jump to the message with specified number in the current summary."
   (interactive)
   (let ((num (or number
                 (string-to-int
@@ -311,53 +303,34 @@ ENTITY is returned."
 (defun wl-thread-close-all ()
   "Close all top threads."
   (interactive)
-  (message "Closing all threads...")
-  (save-excursion
-    (let ((entities wl-thread-entity-list)
-         (cur 0)
-         (len (length wl-thread-entity-list)))
-      (while entities
+  (elmo-with-progress-display
+      (wl-thread-close-all (length wl-thread-entity-list))
+      "Closing all threads"
+    (save-excursion
+      (dolist (entity wl-thread-entity-list)
        (when (and (wl-thread-entity-get-opened (wl-thread-get-entity
-                                                (car entities)))
+                                                entity))
                   (wl-thread-entity-get-children (wl-thread-get-entity
-                                                  (car entities))))
-         (wl-summary-jump-to-msg (car entities))
+                                                  entity)))
+         (wl-summary-jump-to-msg entity)
          (wl-thread-open-close))
-       (when (> len elmo-display-progress-threshold)
-         (setq cur (1+ cur))
-         (if (or (zerop (% cur 5)) (= cur len))
-             (elmo-display-progress
-              'wl-thread-close-all "Closing all threads..."
-              (/ (* cur 100) len))))
-       (setq entities (cdr entities)))))
-  (message "Closing all threads...done"))
+       (elmo-progress-notify 'wl-thread-close-all)))))
 
 (defun wl-thread-open-all ()
   "Open all threads."
   (interactive)
-  (message "Opening all threads...")
-  (save-excursion
-    (goto-char (point-min))
-    (let ((len (count-lines (point-min) (point-max)))
-         (cur 0)
-         entity)
+  (elmo-with-progress-display
+      (wl-thread-open-all (count-lines (point-min) (point-max)))
+      "Opening all threads"
+    (save-excursion
+      (goto-char (point-min))
       (while (not (eobp))
        (if (wl-thread-entity-get-opened
-            (setq entity (wl-thread-get-entity
-                          (wl-summary-message-number))))
+            (wl-thread-get-entity (wl-summary-message-number)))
            (forward-line 1)
          (wl-thread-force-open)
          (wl-thread-goto-bottom-of-sub-thread))
-       (when (> len elmo-display-progress-threshold)
-         (setq cur (1+ cur))
-         (elmo-display-progress
-          'wl-thread-open-all "Opening all threads..."
-          (/ (* cur 100) len)))))
-    ;; Make sure to be 100%.
-    (elmo-display-progress
-     'wl-thread-open-all "Opening all threads..."
-     100))
-  (message "Opening all threads...done"))
+       (elmo-progress-notify 'wl-thread-open-all)))))
 
 (defun wl-thread-open-all-unread ()
   (interactive)
@@ -375,15 +348,14 @@ ENTITY is returned."
         (parent-msg (or parent-msg (wl-thread-entity-get-parent entity)))
         (buffer-read-only nil)
         (inhibit-read-only t)
-        message-entity temp-mark summary-line invisible-top dest-pair)
+        message-entity temp-mark summary-line invisible-top)
     (if (wl-thread-delete-line-from-buffer msg)
        (progn
          (cond
           ((memq msg wl-summary-buffer-target-mark-list)
            (setq temp-mark "*"))
           ((setq temp-mark (wl-summary-registered-temp-mark msg))
-           (setq dest-pair (cons (nth 0 temp-mark)(nth 2 temp-mark))
-                 temp-mark (nth 1 temp-mark)))
+           (setq temp-mark (nth 1 temp-mark)))
           (t (setq temp-mark (wl-summary-get-score-mark msg))))
          (when (setq message-entity
                      (elmo-message-entity wl-summary-buffer-elmo-folder
@@ -394,18 +366,12 @@ ENTITY is returned."
              (elmo-message-entity wl-summary-buffer-elmo-folder
                                   parent-msg)
              temp-mark
-             (elmo-message-flags wl-summary-buffer-elmo-folder
-                                 msg)
-             (elmo-message-cached-p wl-summary-buffer-elmo-folder
-                                    msg)
+             (elmo-message-status wl-summary-buffer-elmo-folder msg)
              (if wl-thread-insert-force-opened
                  nil
                (wl-thread-maybe-get-children-num msg))
              (wl-thread-make-indent-string entity)
-             (wl-thread-entity-get-linked entity)))
-           (if dest-pair
-               (wl-summary-print-argument (car dest-pair)
-                                          (cdr dest-pair)))))
+             (wl-thread-entity-get-linked entity)))))
       ;; insert thread (moving thread)
       (if (not (setq invisible-top
                     (wl-thread-entity-parent-invisible-p entity)))
@@ -445,28 +411,11 @@ ENTITY is returned."
                  (wl-thread-get-entity (car msgs)))))))))
    updates))
 
-(defun wl-thread-update-line-msgs (msgs &optional no-msg)
+(defun wl-thread-update-line-msgs (msgs)
   (wl-delete-all-overlays)
-  (let ((i 0)
-       (updates msgs)
-       len)
-;;; (while msgs
-;;;   (setq updates
-;;;        (append updates
-;;;                (wl-thread-get-children-msgs (car msgs))))
-;;;   (setq msgs (cdr msgs)))
-;;; (setq updates (elmo-uniq-list updates))
-    (setq len (length updates))
-    (while updates
-      (wl-thread-update-line-on-buffer-sub nil (car updates))
-      (setq updates (cdr updates))
-      (when (and (not no-msg)
-                (> len elmo-display-progress-threshold))
-       (setq i (1+ i))
-       (if (or (zerop (% i 5)) (= i len))
-           (elmo-display-progress
-            'wl-thread-update-line-msgs "Updating deleted thread..."
-            (/ (* i 100) len)))))))
+  (dolist (message msgs)
+    (wl-thread-update-line-on-buffer-sub nil message)
+    (elmo-progress-notify 'wl-thread-update-line)))
 
 (defun wl-thread-delete-line-from-buffer (msg)
   "Simply delete msg line."
@@ -515,8 +464,14 @@ ENTITY is returned."
     (let ((entity (wl-thread-get-entity msg))
          top-child top-entity update-msgs invisible-top)
       (setq wl-summary-buffer-number-list
-           (delq msg wl-summary-buffer-number-list))         
+           (delq msg wl-summary-buffer-number-list))
       (when entity
+       (when deep
+         (setq wl-summary-buffer-number-list
+               (elmo-list-delete
+                (wl-thread-entity-get-descendant entity)
+                wl-summary-buffer-number-list
+                #'delq)))
        (let ((parent (wl-thread-entity-get-parent-entity entity)))
          (if parent
              ;; has parent.
@@ -657,8 +612,9 @@ Message is inserted to the summary buffer."
        ;; insert as children.
        (wl-thread-entity-insert-as-children
         parent
-        (setq child-entity (wl-thread-create-entity
-                            msg (nth 0 parent) nil linked)))
+        (setq child-entity
+              (wl-thread-create-entity
+               msg (wl-thread-entity-get-number parent) nil linked)))
       ;; insert as top message.
       (wl-thread-entity-insert-as-top
        (wl-thread-create-entity msg nil)))
@@ -711,24 +667,19 @@ Message is inserted to the summary buffer."
     ret))
 
 (defun wl-thread-update-indent-string-thread (top-list)
-  (let* ((top-list (wl-thread-get-parent-list top-list))
-        (num (length top-list))
-        (i 0)
-        beg)
-    (while top-list
-      (when (or (zerop (% i 5)) (= i num))
-       (elmo-display-progress
-        'wl-thread-update-indent-string-thread
-        "Updating thread indent..."
-        (/ (* i 100) num)))
-      (when (car top-list)
-       (wl-summary-jump-to-msg (car top-list))
-       (setq beg (point))
-       (wl-thread-goto-bottom-of-sub-thread)
-       (wl-thread-update-indent-string-region beg (point)))
-      (setq top-list (cdr top-list)
-           i (1+ i)))
-    (message "Updating thread indent...done")))
+  (let ((top-list (wl-thread-get-parent-list top-list))
+       beg)
+    (elmo-with-progress-display
+       (wl-thread-update-indent-string-thread (length top-list))
+       "Updating thread indent"
+      (while top-list
+       (when (car top-list)
+         (wl-summary-jump-to-msg (car top-list))
+         (setq beg (point))
+         (wl-thread-goto-bottom-of-sub-thread)
+         (wl-thread-update-indent-string-region beg (point)))
+       (elmo-progress-notify 'wl-thread-update-indent-string-thread)
+       (setq top-list (cdr top-list))))))
 
 (defun wl-thread-update-children-number (entity)
   "Update the children number."
@@ -770,6 +721,11 @@ Message is inserted to the summary buffer."
   (interactive "P")
   (wl-thread-call-region-func 'wl-summary-mark-as-answered-region arg))
 
+(defun wl-thread-recover-messages (&optional arg)
+  "Recover killed messages which are contained current thread."
+  (interactive "P")
+  (wl-thread-call-region-func 'wl-summary-recover-messages-region arg))
+
 (defun wl-thread-unmark (&optional arg)
   (interactive "P")
   (wl-thread-call-region-func 'wl-summary-unmark-region arg))
@@ -784,10 +740,9 @@ Message is inserted to the summary buffer."
 
 (defun wl-thread-force-open (&optional msg-num)
   "force open current folder"
-  (if msg-num
-      (wl-summary-jump-to-msg msg-num))
-  (let ((wl-thread-insert-force-opened t))
-    (wl-thread-open-close)))
+  (when msg-num
+    (wl-summary-jump-to-msg msg-num))
+  (wl-thread-open-close 'force-open))
 
 (defun wl-thread-entity-force-open (entity)
   (let ((wl-thread-insert-force-opened t)
@@ -802,22 +757,19 @@ Message is inserted to the summary buffer."
 
 (defun wl-thread-insert-top ()
   (let ((elist wl-thread-entity-list)
-       (len (length wl-thread-entity-list))
-       (cur 0))
-    (wl-delete-all-overlays)
-    (while elist
-      (wl-thread-insert-entity
-       0
-       (wl-thread-get-entity (car elist))
-       nil
-       len)
-      (setq elist (cdr elist))
-      (when (> len elmo-display-progress-threshold)
-       (setq cur (1+ cur))
-       (if (or (zerop (% cur 2)) (= cur len))
-           (elmo-display-progress
-            'wl-thread-insert-top "Inserting message..."
-            (/ (* cur 100) len)))))))
+       (len (length wl-thread-entity-list)))
+    (elmo-with-progress-display
+       (wl-thread-insert-entity (length wl-thread-entity-list))
+       "Inserting message"
+      (wl-delete-all-overlays)
+      (while elist
+       (wl-thread-insert-entity
+        0
+        (wl-thread-get-entity (car elist))
+        nil
+        len)
+       (elmo-progress-notify 'wl-thread-insert-entity)
+       (setq elist (cdr elist))))))
 
 (defsubst wl-thread-insert-entity-sub (indent entity parent-entity all)
   (let (msg-num
@@ -834,19 +786,16 @@ Message is inserted to the summary buffer."
        (setq temp-mark (wl-summary-get-score-mark msg-num)))
       (setq message-entity
            (elmo-message-entity wl-summary-buffer-elmo-folder
-                                (nth 0 entity)))
+                                msg-num))
 ;;;   (wl-delete-all-overlays)
       (when message-entity
        (wl-summary-insert-line
         (wl-summary-create-line
          message-entity
          (elmo-message-entity wl-summary-buffer-elmo-folder
-                              (nth 0 parent-entity))
+                              (wl-thread-entity-get-number parent-entity))
          temp-mark
-         (elmo-message-flags wl-summary-buffer-elmo-folder
-                             msg-num)
-         (elmo-message-cached-p wl-summary-buffer-elmo-folder
-                                msg-num)
+         (elmo-message-status wl-summary-buffer-elmo-folder msg-num)
          (if wl-thread-insert-force-opened
              nil
            (wl-thread-maybe-get-children-num msg-num))
@@ -860,7 +809,7 @@ Message is inserted to the summary buffer."
     (while msgs
       (wl-thread-insert-entity-sub indent entity parent-entity all)
       (setq msgs (cdr msgs))
-      (setq children (nth 2 entity))
+      (setq children (wl-thread-entity-get-children entity))
       (if children
          ;; insert children
          (when (or wl-thread-insert-force-opened
@@ -1005,11 +954,21 @@ Message is inserted to the summary buffer."
     (wl-thread-insert-entity (- depth 1)
                             entity
                             (wl-thread-get-entity
-                             (nth 3 entity))
+                             (wl-thread-entity-get-parent entity))
                             nil)
     (delete-char 1) ; delete '\n'
     (wl-thread-print-argument-region beg (point))))
 
+(defun wl-thread-close-children (&optional number)
+  (interactive)
+  (when (eq wl-summary-buffer-view 'thread)
+    (setq number (or number (wl-summary-message-number)))
+    (save-excursion
+      (let ((inhibit-read-only t)
+           (entity (wl-thread-get-entity number)))
+       (when (wl-thread-entity-get-opened entity)
+         (wl-thread-close entity))))))
+
 (defun wl-thread-open (entity)
   (let (depth beg)
     (beginning-of-line)
@@ -1021,10 +980,21 @@ Message is inserted to the summary buffer."
     (wl-thread-insert-entity depth ;(- depth 1)
                             entity
                             (wl-thread-get-entity
-                             (nth 3 entity)) nil)
+                             (wl-thread-entity-get-parent entity))
+                            nil)
     (delete-char 1) ; delete '\n'
     (wl-thread-print-argument-region beg (point))))
 
+(defun wl-thread-open-children (&optional number)
+  (interactive)
+  (when (eq wl-summary-buffer-view 'thread)
+    (setq number (or number (wl-summary-message-number)))
+    (save-excursion
+      (let ((inhibit-read-only t)
+           (entity (wl-thread-get-entity number)))
+       (unless (wl-thread-entity-get-opened entity)
+         (wl-thread-open entity))))))
+
 (defun wl-thread-open-close (&optional force-open)
   (interactive "P")
   (when (eq wl-summary-buffer-view 'thread)
@@ -1072,7 +1042,7 @@ Message is inserted to the summary buffer."
       (incf depth)
       (setq entity (wl-thread-get-entity number)))
     depth))
-  
+
 (defun wl-thread-update-indent-string-region (beg end)
   (interactive "r")
   (save-excursion
@@ -1143,7 +1113,15 @@ Message is inserted to the summary buffer."
            (wl-thread-entity-set-children
             dst-parent-entity
             (append children (list number)))
-           (wl-thread-entity-set-linked entity t))
+           (wl-thread-entity-set-linked
+            entity
+            (let ((parent (elmo-message-entity-parent
+                           wl-summary-buffer-elmo-folder
+                           (elmo-message-entity
+                            wl-summary-buffer-elmo-folder
+                            number))))
+              (or (null parent)
+                  (/= parent-number (elmo-message-entity-number parent))))))
        ;; insert as top
        (wl-append wl-thread-entity-list (list number))
        (wl-thread-entity-set-linked entity nil))
@@ -1154,7 +1132,7 @@ Message is inserted to the summary buffer."
       (wl-thread-entity-set-parent entity dst-parent)
       ;; update thread on buffer
       (wl-thread-make-number-list)
-      (wl-thread-update-line-msgs update-msgs t))))
+      (wl-thread-update-line-msgs update-msgs))))
 
 (require 'product)
 (product-provide (provide 'wl-thread) (require 'wl-version))