Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.617
diff -u -p -r1.617 comment.module
--- modules/comment/comment.module	25 Jan 2008 16:19:12 -0000	1.617
+++ modules/comment/comment.module	3 Feb 2008 03:56:21 -0000
@@ -988,27 +988,27 @@ function comment_render($node, $cid = 0)
       // Start a form, for use with comment control.
       $result = pager_query($query, $comments_per_page, 0, $query_count, $query_args);
 
-      $divs = 0;
+      // Get the total comment count for the current page.
+      // This is needed to flag the last comment inside theme_comment_view.
+      global $pager_page_array, $pager_total, $pager_total_items;
+      if ($pager_page_array[0] == $pager_total[0] -1) {
+        $comment_count = $pager_total_items[0] - ($comments_per_page * ($pager_total[0] -1));
+      }
+      else {
+        $comment_count = $comments_per_page;
+      }
+
       $num_rows = FALSE;
       $comments = '';
+      $i = 0;
       drupal_add_css(drupal_get_path('module', 'comment') .'/comment.css');
       while ($comment = db_fetch_object($result)) {
+        $i++;
         $comment = drupal_unpack($comment);
         $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
         $comment->depth = count(explode('.', $comment->thread)) - 1;
-
-        if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
-          if ($comment->depth > $divs) {
-            $divs++;
-            $comments .= '<div class="indented">';
-          }
-          else {
-            while ($comment->depth < $divs) {
-              $divs--;
-              $comments .= '</div>';
-            }
-          }
-        }
+        // Each comment will increment by 1 except the last which will equal -1.
+        $comment->position = $i != $comment_count ? $i : -1;
 
         if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
           $comments .= theme('comment_flat_collapsed', $comment, $node);
@@ -1025,9 +1025,6 @@ function comment_render($node, $cid = 0)
 
         $num_rows = TRUE;
       }
-      while ($divs-- > 0) {
-        $comments .= '</div>';
-      }
 
       $comment_controls = variable_get('comment_controls_'. $node->type, COMMENT_CONTROLS_HIDDEN);
       if ($num_rows && ($comment_controls == COMMENT_CONTROLS_ABOVE || $comment_controls == COMMENT_CONTROLS_ABOVE_BELOW)) {
@@ -1556,9 +1553,27 @@ function comment_form_submit($form, &$fo
  * @ingroup themeable
  */
 function theme_comment_view($comment, $node, $links = array(), $visible = TRUE) {
-  static $first_new = TRUE;
+  static $first_new = TRUE, $divs = 0;
 
   $output = '';
+  // Use wrappers to indent threaded comments.
+  $mode = _comment_get_display_setting('mode', $node);
+  if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
+    if ($comment->depth > $divs) {
+      // Opening indent wrapper for new children threads.
+      $divs++;
+      $output .= '<div class="indented">';
+    }
+    else {
+      // If the current comment is not a child of the previous comment or the
+      // previous depth is not the same as the current depth, close the wrapper.
+      while ($comment->depth < $divs) {
+        $divs--;
+        $output .= '</div>';
+      }
+    }
+  }
+
   $comment->new = node_mark($comment->nid, $comment->timestamp);
   if ($first_new && $comment->new != MARK_READ) {
     // Assign the anchor only for the first new comment. This avoids duplicate
@@ -1582,6 +1597,13 @@ function theme_comment_view($comment, $n
     $output .= theme('comment_folded', $comment);
   }
 
+  // Close all remaining indent wrappers when displaying the last comment.
+  if (isset($comment->position) && $comment->position === -1) {
+    while ($divs-- > 0) {
+      $output .= '</div>';
+    }
+  }
+
   return $output;
 }
 
