* wl-summary.el (wl-summary-insert-thread-entity): Refined loop detection.
* wl-message.el (wl-message-buffer-display): Back to the first page when cache
 is hit.
+2002-12-13  Yuuichi Teranishi  <teranisi@gohome.org>
+
+       * wl-thread.el (wl-thread-insert-message): Use
+       wl-summary-max-thread-depth.
+
+       * wl-summary.el (wl-summary-insert-thread-entity): Refined
+       loop detection.
+
+       * wl-message.el (wl-message-buffer-display): Back to the first page
+       when cache is hit.
+
 2002-12-12  Yuuichi Teranishi  <teranisi@gohome.org>
 
        * wl-vars.el (wl-summary-max-thread-depth): New user option.
 
          ;; buffer cache is used.
          (setq cache-used t)
          (with-current-buffer hit
+           ;; Rewind to the top page
+           (widen)
+           (goto-char (point-min))
+           (ignore-errors (wl-message-narrow-to-page))
            (unless (eq wl-message-buffer-cur-flag flag)
              (setq read t))))
       ;; delete tail and add new to the top.
 
         parent-entity
         parent-number
         (case-fold-search t)
-        (depth 0)
+        (depth 0) relatives anumber
         cur number overview2 cur-entity linked retval delayed-entity
         update-list entity-stack)
     (while entity
            parent-number (elmo-msgdb-overview-entity-get-number
                           parent-entity))
       (setq number (elmo-msgdb-overview-entity-get-number entity))
-      ;; If thread loop detected or reached to max depth, set parent as nil.
       (setq cur entity)
-      (while (and cur (< depth wl-summary-max-thread-depth))
-       (if (eq number (elmo-msgdb-overview-entity-get-number
-                       (setq cur
-                             (elmo-msgdb-get-parent-entity cur msgdb))))
+      ;; If thread loop detected, set parent as nil.
+      (while cur
+       (setq anumber
+             (elmo-msgdb-overview-entity-get-number
+              (setq cur (elmo-msgdb-get-parent-entity cur msgdb))))
+       (if (memq anumber relatives)
            (setq parent-number nil
                  cur nil))
-       (incf depth))
+       (setq relatives (cons
+                        (elmo-msgdb-overview-entity-get-number cur)
+                        relatives)))
       (if (and parent-number
               (not (wl-thread-get-entity parent-number))
               (not force-insert))
 
 When optional argument UPDATE is non-nil,
 Message is inserted to the summary buffer."
   (let ((parent (wl-thread-get-entity parent-msg))
+       (depth 0) cur
        child-entity invisible-top)
 ;;; Update the thread view...not implemented yet.
 ;;;  (when force-insert
 ;;;    (if parent
 ;;;      (wl-thread-entity-force-open parent))
+    (when (and wl-summary-max-thread-depth parent)
+      (setq cur parent)
+      (while cur
+       (incf depth)
+       (setq cur (wl-thread-entity-get-parent-entity cur)))
+      (when (> depth wl-summary-max-thread-depth)
+       (setq parent nil)))
     (if parent
        ;; insert as children.
        (wl-thread-entity-insert-as-children