(gnus-point-at-bol, gnus-point-at-eol): Remove.
[gnus] / lisp / gnus-topic.el
index 4f22227..da404a5 100644 (file)
@@ -1,8 +1,9 @@
 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
-;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
+;;        Free Software Foundation, Inc.
 
 ;; Author: Ilja Weis <kult@uni-paderborn.de>
-;;     Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
+;;     Lars Magne Ingebrigtsen <larsi@gnus.org>
 ;; Keywords: news
 
 ;; This file is part of GNU Emacs.
 
 ;;; Code:
 
+(eval-when-compile (require 'cl))
+
 (require 'gnus)
 (require 'gnus-group)
 (require 'gnus-start)
+(require 'gnus-util)
 
 (defgroup gnus-topic nil
   "Group topics."
@@ -42,6 +46,9 @@
   :type 'hook
   :group 'gnus-topic)
 
+(when (featurep 'xemacs)
+  (add-hook 'gnus-topic-mode-hook 'gnus-xmas-topic-menu-add))
+
 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
   "Format of topic lines.
 It works along the same lines as a normal formatting string,
@@ -53,7 +60,10 @@ with some simple extensions.
 %g  Number of groups in the topic.
 %a  Number of unread articles in the groups in the topic.
 %A  Number of unread articles in the groups in the topic and its subtopics.
-"
+
+General format specifiers can also be used.
+See Info node `(gnus)Formatting Variables'."
+  :link '(custom-manual "(gnus)Formatting Variables")
   :type 'string
   :group 'gnus-topic)
 
@@ -71,13 +81,13 @@ with some simple extensions.
 
 (defvar gnus-topic-active-topology nil)
 (defvar gnus-topic-active-alist nil)
+(defvar gnus-topic-unreads nil)
 
 (defvar gnus-topology-checked-p nil
   "Whether the topology has been checked in this session.")
 
 (defvar gnus-topic-killed-topics nil)
 (defvar gnus-topic-inhibit-change-level nil)
-(defvar gnus-topic-tallied-groups nil)
 
 (defconst gnus-topic-line-format-alist
   `((?n name ?s)
@@ -94,22 +104,20 @@ with some simple extensions.
 
 (defun gnus-group-topic-name ()
   "The name of the topic on the current line."
-  (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
+  (let ((topic (get-text-property (point-at-bol) 'gnus-topic)))
     (and topic (symbol-name topic))))
 
 (defun gnus-group-topic-level ()
   "The level of the topic on the current line."
-  (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
+  (get-text-property (point-at-bol) 'gnus-topic-level))
 
 (defun gnus-group-topic-unread ()
   "The number of unread articles in topic on the current line."
-  (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
+  (get-text-property (point-at-bol) 'gnus-topic-unread))
 
 (defun gnus-topic-unread (topic)
   "Return the number of unread articles in TOPIC."
-  (or (save-excursion
-       (and (gnus-topic-goto-topic topic)
-            (gnus-group-topic-unread)))
+  (or (cdr (assoc topic gnus-topic-unreads))
       0))
 
 (defun gnus-group-topic-p ()
@@ -118,7 +126,7 @@ with some simple extensions.
 
 (defun gnus-topic-visible-p ()
   "Return non-nil if the current topic is visible."
-  (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
+  (get-text-property (point-at-bol) 'gnus-topic-visible))
 
 (defun gnus-topic-articles-in-topic (entries)
   (let ((total 0)
@@ -148,11 +156,21 @@ with some simple extensions.
       (gnus-group-topic group))))
 
 (defun gnus-topic-goto-topic (topic)
-  "Go to TOPIC."
   (when topic
     (gnus-goto-char (text-property-any (point-min) (point-max)
                                       'gnus-topic (intern topic)))))
 
+(defun gnus-topic-jump-to-topic (topic)
+  "Go to TOPIC."
+  (interactive
+   (list (completing-read "Go to topic: "
+                         (mapcar 'list (gnus-topic-list))
+                         nil t)))
+  (dolist (topic (gnus-current-topics topic))
+    (gnus-topic-goto-topic topic)
+    (gnus-topic-fold t))
+  (gnus-topic-goto-topic topic))
+
 (defun gnus-current-topic ()
   "Return the name of the current topic."
   (let ((result
@@ -165,9 +183,10 @@ with some simple extensions.
     (when result
       (symbol-name result))))
 
-(defun gnus-current-topics ()
-  "Return a list of all current topics, lowest in hierarchy first."
-  (let ((topic (gnus-current-topic))
+(defun gnus-current-topics (&optional topic)
+  "Return a list of all current topics, lowest in hierarchy first.
+If TOPIC, start with that topic."
+  (let ((topic (or topic (gnus-current-topic)))
        topics)
     (while topic
       (push topic topics)
@@ -180,12 +199,13 @@ with some simple extensions.
     (beginning-of-line)
     (get-text-property (point) 'gnus-active)))
 
-(defun gnus-topic-find-groups (topic &optional level all)
-  "Return entries for all visible groups in TOPIC."
+(defun gnus-topic-find-groups (topic &optional level all lowest recursive)
+  "Return entries for all visible groups in TOPIC.
+If RECURSIVE is t, return groups in its subtopics too."
   (let ((groups (cdr (assoc topic gnus-topic-alist)))
-        info clevel unread group lowest params visible-groups entry active)
+       info clevel unread group params visible-groups entry active)
     (setq lowest (or lowest 1))
-    (setq level (or level 7))
+    (setq level (or level gnus-level-unsubscribed))
     ;; We go through the newsrc to look for matches.
     (while groups
       (when (setq group (pop groups))
@@ -198,18 +218,20 @@ with some simple extensions.
                              active
                              (- (1+ (cdr active)) (car active))))
              clevel (or (gnus-info-level info)
-                        (if (member group gnus-zombie-list) 8 9))))
-      (and 
-       unread                          ; nil means that the group is dead.
+                        (if (member group gnus-zombie-list)
+                            gnus-level-zombie gnus-level-killed))))
+      (and
+       info                            ; nil means that the group is dead.
        (<= clevel level)
        (>= clevel lowest)              ; Is inside the level we want.
        (or all
-          (if (eq unread t)
+          (if (or (eq unread t)
+                  (eq unread nil))
               gnus-group-list-inactive-groups
             (> unread 0))
           (and gnus-list-groups-with-ticked-articles
                (cdr (assq 'tick (gnus-info-marks info))))
-                                       ; Has right readedness.
+          ;; Has right readedness.
           ;; Check for permanent visibility.
           (and gnus-permanently-visible-groups
                (string-match gnus-permanently-visible-groups group))
@@ -217,7 +239,40 @@ with some simple extensions.
           (cdr (assq 'visible params)))
        ;; Add this group to the list of visible groups.
        (push (or entry group) visible-groups)))
-    (nreverse visible-groups)))
+    (setq visible-groups (nreverse visible-groups))
+    (when recursive
+      (if (eq recursive t)
+         (setq recursive (cdr (gnus-topic-find-topology topic))))
+      (mapcar (lambda (topic-topology)
+               (setq visible-groups
+                     (nconc visible-groups
+                            (gnus-topic-find-groups
+                             (caar topic-topology)
+                             level all lowest topic-topology))))
+             (cdr recursive)))
+    visible-groups))
+
+(defun gnus-topic-goto-previous-topic (n)
+  "Go to the N'th previous topic."
+  (interactive "p")
+  (gnus-topic-goto-next-topic (- n)))
+
+(defun gnus-topic-goto-next-topic (n)
+  "Go to the N'th next topic."
+  (interactive "p")
+  (let ((backward (< n 0))
+       (n (abs n))
+       (topic (gnus-current-topic)))
+    (while (and (> n 0)
+               (setq topic
+                     (if backward
+                         (gnus-topic-previous-topic topic)
+                       (gnus-topic-next-topic topic))))
+      (gnus-topic-goto-topic topic)
+      (setq n (1- n)))
+    (when (/= 0 n)
+      (gnus-message 7 "No more topics"))
+    n))
 
 (defun gnus-topic-previous-topic (topic)
   "Return the previous topic on the same level as TOPIC."
@@ -236,14 +291,14 @@ with some simple extensions.
        result found)
     (while (and topology
                (not (setq found (equal (caaar topology) topic)))
-               (not (setq result (gnus-topic-parent-topic topic 
-                                                          (car topology)))))
+               (not (setq result (gnus-topic-parent-topic
+                                  topic (car topology)))))
       (setq topology (cdr topology)))
     (or result (and found parent))))
 
 (defun gnus-topic-next-topic (topic &optional previous)
   "Return the next sibling of TOPIC."
-  (let ((parentt (cddr (gnus-topic-find-topology 
+  (let ((parentt (cddr (gnus-topic-find-topology
                        (gnus-topic-parent-topic topic))))
        prev)
     (while (and parentt
@@ -254,6 +309,20 @@ with some simple extensions.
        prev
       (caaadr parentt))))
 
+(defun gnus-topic-forward-topic (num)
+  "Go to the next topic on the same level as the current one."
+  (let* ((topic (gnus-current-topic))
+        (way (if (< num 0) 'gnus-topic-previous-topic
+               'gnus-topic-next-topic))
+        (num (abs num)))
+    (while (and (not (zerop num))
+               (setq topic (funcall way topic)))
+      (when (gnus-topic-goto-topic topic)
+       (decf num)))
+    (unless (zerop num)
+      (goto-char (point-max)))
+    num))
+
 (defun gnus-topic-find-topology (topic &optional topology level remove)
   "Return the topology of TOPIC."
   (unless topology
@@ -278,7 +347,7 @@ with some simple extensions.
 (defun gnus-topic-list (&optional topology)
   "Return a list of all topics in the topology."
   (unless topology
-    (setq topology gnus-topic-topology 
+    (setq topology gnus-topic-topology
          gnus-tmp-topics nil))
   (push (caar topology) gnus-tmp-topics)
   (mapcar 'gnus-topic-list (cdr topology))
@@ -309,27 +378,40 @@ with some simple extensions.
 
 (defun gnus-group-topic-parameters (group)
   "Compute the group parameters for GROUP taking into account inheritance from topics."
-  (let ((params-list (list (gnus-group-get-parameter group)))
-       topics params param out)
+  (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
     (save-excursion
-      (gnus-group-goto-group group)
-      (setq topics (gnus-current-topics))
-      (while topics
-       (push (gnus-topic-parameters (pop topics)) params-list))
-      ;; We probably have lots of nil elements here, so
-      ;; we remove them.  Probably faster than doing this "properly".
-      (setq params-list (delq nil params-list))
-      ;; Now we have all the parameters, so we go through them
-      ;; and do inheritance in the obvious way.
-      (while (setq params (pop params-list))
-       (while (setq param (pop params))
-         (when (atom param)
-           (setq param (cons param t)))
-         ;; Override any old versions of this param.
-         (setq out (delq (assq (car param) out) out))
-         (push param out)))
-      ;; Return the resulting parameter list.
-      out)))
+      (nconc params-list
+            (gnus-topic-hierarchical-parameters
+             ;; First we try to go to the group within the group
+             ;; buffer and find the topic for the group that way.
+             ;; This hopefully copes well with groups that are in
+             ;; more than one topic.  Failing that (i.e. when the
+             ;; group isn't visible in the group buffer) we find a
+             ;; topic for the group via gnus-group-topic.
+             (or (and (gnus-group-goto-group group)
+                      (gnus-current-topic))
+                 (gnus-group-topic group)))))))
+
+(defun gnus-topic-hierarchical-parameters (topic)
+  "Return a topic list computed for TOPIC."
+  (let ((topics (gnus-current-topics topic))
+       params-list param out params)
+    (while topics
+      (push (gnus-topic-parameters (pop topics)) params-list))
+    ;; We probably have lots of nil elements here, so
+    ;; we remove them.  Probably faster than doing this "properly".
+    (setq params-list (delq nil params-list))
+    ;; Now we have all the parameters, so we go through them
+    ;; and do inheritance in the obvious way.
+    (while (setq params (pop params-list))
+      (while (setq param (pop params))
+       (when (atom param)
+         (setq param (cons param t)))
+       ;; Override any old versions of this param.
+       (gnus-pull (car param) out)
+       (push param out)))
+    ;; Return the resulting parameter list.
+    out))
 
 ;;; General utility functions
 
@@ -339,59 +421,89 @@ with some simple extensions.
 
 ;;; Generating group buffers
 
-(defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
-  "List all newsgroups with unread articles of level LEVEL or lower, and
-use the `gnus-group-topics' to sort the groups.
-If ALL is non-nil, list groups that have no unread articles.
+(defun gnus-group-prepare-topics (level &optional predicate lowest
+                                       regexp list-topic topic-level)
+  "List all newsgroups with unread articles of level LEVEL or lower.
+Use the `gnus-group-topics' to sort the groups.
+If PREDICTE is a function, list groups that the function returns non-nil;
+if it is t, list groups that have no unread articles.
 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
   (set-buffer gnus-group-buffer)
   (let ((buffer-read-only nil)
-        (lowest (or lowest 1)))
-
-    (setq gnus-topic-tallied-groups nil)
+       (lowest (or lowest 1))
+       (not-in-list
+        (and gnus-group-listed-groups
+             (copy-sequence gnus-group-listed-groups))))
 
+    (gnus-update-format-specifications nil 'topic)
+    
     (when (or (not gnus-topic-alist)
              (not gnus-topology-checked-p))
       (gnus-topic-check-topology))
 
-    (unless list-topic 
+    (unless list-topic
       (erase-buffer))
-    
+
     ;; List dead groups?
-    (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
-      (gnus-group-prepare-flat-list-dead 
+    (when (or gnus-group-listed-groups
+             (and (>= level gnus-level-zombie)
+                  (<= lowest gnus-level-zombie)))
+      (gnus-group-prepare-flat-list-dead
        (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
        gnus-level-zombie ?Z
        regexp))
-    
-    (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
-      (gnus-group-prepare-flat-list-dead 
+
+    (when (or gnus-group-listed-groups
+              (and (>= level gnus-level-killed)
+                   (<= lowest gnus-level-killed)))
+      (gnus-group-prepare-flat-list-dead
        (setq gnus-killed-list (sort gnus-killed-list 'string<))
-       gnus-level-killed ?K
-       regexp))
+       gnus-level-killed ?K regexp)
+      (when not-in-list
+       (unless gnus-killed-hashtb
+         (gnus-make-hashtable-from-killed))
+       (gnus-group-prepare-flat-list-dead
+        (gnus-remove-if (lambda (group)
+                          (or (gnus-gethash group gnus-newsrc-hashtb)
+                              (gnus-gethash group gnus-killed-hashtb)))
+                        not-in-list)
+        gnus-level-killed ?K regexp)))
 
     ;; Use topics.
     (prog1
-       (when (< lowest gnus-level-zombie)
+       (when (or (< lowest gnus-level-zombie)
+                 gnus-group-listed-groups)
          (if list-topic
              (let ((top (gnus-topic-find-topology list-topic)))
                (gnus-topic-prepare-topic (cdr top) (car top)
-                                         (or topic-level level) all))
+                                         (or topic-level level) predicate
+                                         nil lowest regexp))
            (gnus-topic-prepare-topic gnus-topic-topology 0
-                                     (or topic-level level) all)))
-      
+                                     (or topic-level level) predicate
+                                     nil lowest regexp)))
       (gnus-group-set-mode-line)
-      (setq gnus-group-list-mode (cons level all))
-      (run-hooks 'gnus-group-prepare-hook))))
+      (setq gnus-group-list-mode (cons level predicate))
+      (gnus-run-hooks 'gnus-group-prepare-hook))))
 
-(defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
+(defun gnus-topic-prepare-topic (topicl level &optional list-level
+                                       predicate silent
+                                       lowest regexp)
   "Insert TOPIC into the group buffer.
 If SILENT, don't insert anything.  Return the number of unread
 articles in the topic and its subtopics."
   (let* ((type (pop topicl))
-        (entries (gnus-topic-find-groups (car type) list-level all))
+        (entries (gnus-topic-find-groups
+                  (car type)
+                  (if gnus-group-listed-groups
+                      gnus-level-killed
+                    list-level)
+                  (or predicate gnus-group-listed-groups
+                      (cdr (assq 'visible
+                                 (gnus-topic-hierarchical-parameters
+                                  (car type)))))
+                  (if gnus-group-listed-groups 0 lowest)))
         (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
-        (gnus-group-indentation 
+        (gnus-group-indentation
          (make-string (* gnus-topic-indent-level level) ? ))
         (beg (progn (beginning-of-line) (point)))
         (topicl (reverse topicl))
@@ -399,48 +511,79 @@ articles in the topic and its subtopics."
         (point-max (point-max))
         (unread 0)
         (topic (car type))
-        info entry end active)
+        info entry end active tick)
     ;; Insert any sub-topics.
     (while topicl
       (incf unread
-           (gnus-topic-prepare-topic 
-            (pop topicl) (1+ level) list-level all
-            (not visiblep))))
+           (gnus-topic-prepare-topic
+            (pop topicl) (1+ level) list-level predicate
+            (not visiblep) lowest regexp)))
     (setq end (point))
     (goto-char beg)
     ;; Insert all the groups that belong in this topic.
     (while (setq entry (pop entries))
-      (when visiblep 
-       (if (stringp entry)
-           ;; Dead groups.
-           (gnus-group-insert-group-line
-            entry (if (member entry gnus-zombie-list) 8 9)
-            nil (- (1+ (cdr (setq active (gnus-active entry))))
-                   (car active))
-            nil)
-         ;; Living groups.
-         (when (setq info (nth 2 entry))
-           (gnus-group-insert-group-line 
-            (gnus-info-group info)
-            (gnus-info-level info) (gnus-info-marks info)
-            (car entry) (gnus-info-method info)))))
-      (when (and (listp entry)
-                (numberp (car entry))
-                (not (member (gnus-info-group (setq info (nth 2 entry)))
-                             gnus-topic-tallied-groups)))
-       (push (gnus-info-group info) gnus-topic-tallied-groups)
-       (incf unread (car entry))))
+      (when (if (stringp entry)
+               (gnus-group-prepare-logic
+                entry
+                (and
+                 (or (not gnus-group-listed-groups)
+                     (if (< list-level gnus-level-zombie) nil
+                       (let ((entry-level
+                              (if (member entry gnus-zombie-list)
+                                  gnus-level-zombie gnus-level-killed)))
+                         (and (<= entry-level list-level)
+                              (>= entry-level lowest)))))
+                 (cond
+                  ((stringp regexp)
+                   (string-match regexp entry))
+                  ((functionp regexp)
+                   (funcall regexp entry))
+                  ((null regexp) t)
+                  (t nil))))
+             (setq info (nth 2 entry))
+             (gnus-group-prepare-logic
+              (gnus-info-group info)
+              (and (or (not gnus-group-listed-groups)
+                       (let ((entry-level (gnus-info-level info)))
+                         (and (<= entry-level list-level)
+                              (>= entry-level lowest))))
+                   (or (not (functionp predicate))
+                       (funcall predicate info))
+                   (or (not (stringp regexp))
+                       (string-match regexp (gnus-info-group info))))))
+       (when visiblep
+         (if (stringp entry)
+             ;; Dead groups.
+             (gnus-group-insert-group-line
+              entry (if (member entry gnus-zombie-list)
+                        gnus-level-zombie gnus-level-killed)
+              nil (- (1+ (cdr (setq active (gnus-active entry))))
+                     (car active))
+              nil)
+           ;; Living groups.
+           (when (setq info (nth 2 entry))
+             (gnus-group-insert-group-line
+              (gnus-info-group info)
+              (gnus-info-level info) (gnus-info-marks info)
+              (car entry) (gnus-info-method info)))))
+       (when (and (listp entry)
+                  (numberp (car entry)))
+         (incf unread (car entry)))
+       (when (listp entry)
+         (setq tick t))))
     (goto-char beg)
     ;; Insert the topic line.
     (when (and (not silent)
-              (or gnus-topic-display-empty-topics
-                  (not (zerop unread))
-                  (/= point-max (point-max))))
+              (or gnus-topic-display-empty-topics ;We want empty topics
+                  (not (zerop unread)) ;Non-empty
+                  tick                 ;Ticked articles
+                  (/= point-max (point-max)))) ;Unactivated groups
       (gnus-extent-start-open (point))
-      (gnus-topic-insert-topic-line 
+      (gnus-topic-insert-topic-line
        (car type) visiblep
        (not (eq (nth 2 type) 'hidden))
        level all-entries unread))
+    (gnus-topic-update-unreads (car type) unread)
     (goto-char end)
     unread))
 
@@ -461,7 +604,7 @@ articles in the topic and its subtopics."
       (let ((data (cadr (gnus-topic-find-topology topic))))
        (setcdr data
                (list (if insert 'visible 'invisible)
-                     (if hide 'hide nil)
+                     (caddr data)
                      (cadddr data))))
       (if total-remove
          (setq gnus-topic-alist
@@ -470,13 +613,13 @@ articles in the topic and its subtopics."
 
 (defun gnus-topic-insert-topic (topic &optional level)
   "Insert TOPIC."
-  (gnus-group-prepare-topics 
+  (gnus-group-prepare-topics
    (car gnus-group-list-mode) (cdr gnus-group-list-mode)
    nil nil topic level))
-  
-(defun gnus-topic-fold (&optional insert)
+
+(defun gnus-topic-fold (&optional insert topic)
   "Remove/insert the current topic."
-  (let ((topic (gnus-group-topic-name)))
+  (let ((topic (or topic (gnus-group-topic-name))))
     (when topic
       (save-excursion
        (if (not (gnus-group-active-topic-p))
@@ -486,27 +629,35 @@ articles in the topic and its subtopics."
                (gnus-topic-alist gnus-topic-active-alist)
                (gnus-group-list-mode (cons 5 t)))
            (gnus-topic-remove-topic
-            (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
+            (or insert (not (gnus-topic-visible-p))) nil nil 9)
+           (gnus-topic-enter-dribble)))))))
 
-(defun gnus-topic-insert-topic-line (name visiblep shownp level entries 
+(defun gnus-topic-insert-topic-line (name visiblep shownp level entries
                                          &optional unread)
   (let* ((visible (if visiblep "" "..."))
         (indentation (make-string (* gnus-topic-indent-level level) ? ))
         (total-number-of-articles unread)
         (number-of-groups (length entries))
-        (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
+        (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
+        gnus-tmp-header)
+    (gnus-topic-update-unreads name unread)
     (beginning-of-line)
     ;; Insert the text.
-    (gnus-add-text-properties 
-     (point)
-     (prog1 (1+ (point))
-       (eval gnus-topic-line-format-spec)
-       (gnus-topic-remove-excess-properties)1)
-     (list 'gnus-topic (intern name)
-          'gnus-topic-level level
-          'gnus-topic-unread unread
-          'gnus-active active-topic
-          'gnus-topic-visible visiblep))))
+    (if shownp
+       (gnus-add-text-properties
+        (point)
+        (prog1 (1+ (point))
+          (eval gnus-topic-line-format-spec))
+        (list 'gnus-topic (intern name)
+              'gnus-topic-level level
+              'gnus-topic-unread unread
+              'gnus-active active-topic
+              'gnus-topic-visible visiblep)))))
+
+(defun gnus-topic-update-unreads (topic unreads)
+  (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
+                                gnus-topic-unreads))
+  (push (cons topic unreads) gnus-topic-unreads))
 
 (defun gnus-topic-update-topics-containing-group (group)
   "Update all topics that have GROUP as a member."
@@ -529,12 +680,14 @@ articles in the topic and its subtopics."
   (when (and (eq major-mode 'gnus-group-mode)
             gnus-topic-mode)
     (let ((group (gnus-group-group-name))
+         (m (point-marker))
          (buffer-read-only nil))
-      (when (and group 
+      (when (and group
                 (gnus-get-info group)
                 (gnus-topic-goto-topic (gnus-current-topic)))
        (gnus-topic-update-topic-line (gnus-group-topic-name))
-       (gnus-group-goto-group group)
+       (goto-char m)
+       (set-marker m nil)
        (gnus-group-position-point)))))
 
 (defun gnus-topic-goto-missing-group (group)
@@ -542,9 +695,11 @@ articles in the topic and its subtopics."
   (let* ((topic (gnus-group-topic group))
         (groups (cdr (assoc topic gnus-topic-alist)))
         (g (cdr (member group groups)))
-        (unfound t))
+        (unfound t)
+        entry)
     ;; Try to jump to a visible group.
-    (while (and g (not (gnus-group-goto-group (car g) t)))
+    (while (and g
+               (not (gnus-group-goto-group (car g) t)))
       (pop g))
     ;; It wasn't visible, so we try to see where to insert it.
     (when (not g)
@@ -553,21 +708,67 @@ articles in the topic and its subtopics."
        (when (gnus-group-goto-group (pop g) t)
          (forward-line 1)
          (setq unfound nil)))
-      (when unfound
-       (gnus-topic-goto-topic topic)
-       (forward-line 1)))))
+      (when (and unfound
+                topic
+                (not (gnus-topic-goto-missing-topic topic)))
+       (gnus-topic-display-missing-topic topic)))))
+
+(defun gnus-topic-display-missing-topic (topic)
+  "Insert topic lines recursively for missing topics."
+  (let ((parent (gnus-topic-find-topology
+                (gnus-topic-parent-topic topic))))
+    (when (and parent
+              (not (gnus-topic-goto-missing-topic (caadr parent))))
+      (gnus-topic-display-missing-topic (caadr parent))))
+  (gnus-topic-goto-missing-topic topic)
+  (let* ((top (gnus-topic-find-topology topic))
+        (children (cddr top))
+        (type (cadr top))
+        (unread 0)
+        (entries (gnus-topic-find-groups
+                  (car type) (car gnus-group-list-mode)
+                  (cdr gnus-group-list-mode)))
+       entry)
+    (while children
+      (incf unread (gnus-topic-unread (caar (pop children)))))
+    (while (setq entry (pop entries))
+      (when (numberp (car entry))
+       (incf unread (car entry))))
+    (gnus-topic-insert-topic-line
+     topic t t (car (gnus-topic-find-topology topic)) nil unread)))
+
+(defun gnus-topic-goto-missing-topic (topic)