Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.777
diff -u -r1.777 common.inc
--- includes/common.inc	16 Jul 2008 21:59:25 -0000	1.777
+++ includes/common.inc	17 Jul 2008 16:42:51 -0000
@@ -1443,6 +1443,8 @@
  *   an HTML string containing a link to the given path.
  */
 function l($text, $path, $options = array()) {
+  $items = array();
+  
   // Merge in defaults.
   $options += array(
       'attributes' => array(),
@@ -1465,6 +1467,13 @@
     $options['attributes']['title'] = strip_tags($options['attributes']['title']);
   }
 
+  if (module_exists('comment') && isset($options['fragment'])) {
+    $items = explode('-', $options['fragment']);
+    if ($items[0] == 'comment' && is_numeric($items[1]) && !isset($options['query']['page'])) {
+      $options['query']['page'] = comment_get_page($items[1]);
+    }
+  }
+
   return '<a href="' . check_url(url($path, $options)) . '"' . drupal_attributes($options['attributes']) . '>' . ($options['html'] ? $text : check_plain($text)) . '</a>';
 }
 
Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.643
diff -u -r1.643 comment.module
--- modules/comment/comment.module	16 Jul 2008 21:59:26 -0000	1.643
+++ modules/comment/comment.module	17 Jul 2008 16:42:53 -0000
@@ -917,23 +917,9 @@
     else {
       // Multiple comment view.
       $query_count = 'SELECT COUNT(*) FROM {comments} c WHERE c.nid = %d';
-      $query = 'SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.thread, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d';
+      $query = comment_build_query($node);
 
       $query_args = array($nid);
-      if (!user_access('administer comments')) {
-        $query .= ' AND c.status = %d';
-        $query_count .= ' AND c.status = %d';
-        $query_args[] = COMMENT_PUBLISHED;
-      }
-      if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
-        $query .= ' ORDER BY c.cid';
-      }
-      else {
-        // See comment above. Analysis reveals that this doesn't cost too
-        // much. It scales much much better than having the whole comment
-        // structure.
-        $query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
-      }
 
       $query = db_rewrite_sql($query, 'c', 'cid');
       $query_count = db_rewrite_sql($query_count, 'c', 'cid');
@@ -1997,3 +1983,80 @@
     ),
   );
 }
+
+/**
+ * Get the page number for a comment
+ *
+ * @param $cid
+ *   A comment cid.
+ * @return $page An integer representing page id of a paged comment query
+ */
+function comment_get_page($cid, $nid = NULL) {  
+  $count = 0;
+  
+  if (!$nid) {
+    $nid = db_result(db_query("SELECT nid FROM {comments} WHERE cid=%d", $cid));
+  }
+  $node = node_load(array('nid' => $nid));
+  $comments_per_page = _comment_get_display_setting('comments_per_page', $node);
+  
+  $tree = comment_get_tree($node);
+  foreach ($tree as $comment) {
+    $count++;
+    $c[$comment->cid] = $count;
+  }
+
+  $division = ($c[$cid] / $comments_per_page);
+  $page     = (($division <= (int)$division) ? ($division - 1) : floor($division));
+
+  return $page;
+}
+
+/**
+ * Get the whole comment tree for a node
+ *
+ * @param $nid
+ *   A node nid.
+ * @return $comments An array of comment objects each containing a nid,
+ *   subject, cid, and timstamp, or an empty array if there are no recent
+ *   comments visible to the current user.
+ */
+function comment_get_tree($node) {
+  $query_args = array();
+  $query = comment_build_query($node);
+
+  $result = db_query($query, $query_args);
+  while($comment = db_fetch_object($result)) {
+    $comments[] = $comment;
+  }
+  
+  return $comments;
+}
+
+/**
+ * Build the query for the comment tree
+ */
+function comment_build_query($node) {
+  $mode = _comment_get_display_setting('mode', $node);
+  $query_args = array($node->nid);
+
+  // Multiple comment view
+  $query = 'SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.thread, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d';
+
+  if (!user_access('administer comments')) {
+    $query .= ' AND c.status = %d';
+    $query_count .= ' AND c.status = %d';
+    $query_args[] = COMMENT_PUBLISHED;
+  }
+  if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
+    $query .= ' ORDER BY c.cid';
+  }
+  else {
+    // See comment above. Analysis reveals that this doesn't cost too
+    // much. It scales much much better than having the whole comment
+    // structure.
+    $query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
+  }
+
+  return sprintf($query, $query_args);
+}
\ No newline at end of file
