*** empty log message ***
[gnus] / lisp / gnus-topic.el
1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Ilja Weis <kult@uni-paderborn.de>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'gnus)
30 (require 'gnus-group)
31 (require 'gnus-start)
32
33 (defgroup gnus-topic nil
34   "Group topics."
35   :group 'gnus-group)
36
37 (defvar gnus-topic-mode nil
38   "Minor mode for Gnus group buffers.")
39
40 (defcustom gnus-topic-mode-hook nil
41   "Hook run in topic mode buffers."
42   :type 'hook
43   :group 'gnus-topic)
44
45 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
46   "Format of topic lines.
47 It works along the same lines as a normal formatting string,
48 with some simple extensions.
49
50 %i  Indentation based on topic level.
51 %n  Topic name.
52 %v  Nothing if the topic is visible, \"...\" otherwise.
53 %g  Number of groups in the topic.
54 %a  Number of unread articles in the groups in the topic.
55 %A  Number of unread articles in the groups in the topic and its subtopics.
56 "
57   :type 'string
58   :group 'gnus-topic)
59
60 (defcustom gnus-topic-indent-level 2
61   "*How much each subtopic should be indented."
62   :type 'integer
63   :group 'gnus-topic)
64
65 (defcustom gnus-topic-display-empty-topics t
66   "*If non-nil, display the topic lines even of topics that have no unread articles."
67   :type 'boolean
68   :group 'gnus-topic)
69
70 ;; Internal variables.
71
72 (defvar gnus-topic-active-topology nil)
73 (defvar gnus-topic-active-alist nil)
74
75 (defvar gnus-topology-checked-p nil
76   "Whether the topology has been checked in this session.")
77
78 (defvar gnus-topic-killed-topics nil)
79 (defvar gnus-topic-inhibit-change-level nil)
80 (defvar gnus-topic-tallied-groups nil)
81
82 (defconst gnus-topic-line-format-alist
83   `((?n name ?s)
84     (?v visible ?s)
85     (?i indentation ?s)
86     (?g number-of-groups ?d)
87     (?a (gnus-topic-articles-in-topic entries) ?d)
88     (?A total-number-of-articles ?d)
89     (?l level ?d)))
90
91 (defvar gnus-topic-line-format-spec nil)
92
93 ;;; Utility functions
94
95 (defun gnus-group-topic-name ()
96   "The name of the topic on the current line."
97   (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
98     (and topic (symbol-name topic))))
99
100 (defun gnus-group-topic-level ()
101   "The level of the topic on the current line."
102   (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
103
104 (defun gnus-group-topic-unread ()
105   "The number of unread articles in topic on the current line."
106   (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
107
108 (defun gnus-topic-unread (topic)
109   "Return the number of unread articles in TOPIC."
110   (or (save-excursion
111         (and (gnus-topic-goto-topic topic)
112              (gnus-group-topic-unread)))
113       0))
114
115 (defun gnus-group-topic-p ()
116   "Return non-nil if the current line is a topic."
117   (gnus-group-topic-name))
118
119 (defun gnus-topic-visible-p ()
120   "Return non-nil if the current topic is visible."
121   (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
122
123 (defun gnus-topic-articles-in-topic (entries)
124   (let ((total 0)
125         number)
126     (while entries
127       (when (numberp (setq number (car (pop entries))))
128         (incf total number)))
129     total))
130
131 (defun gnus-group-topic (group)
132   "Return the topic GROUP is a member of."
133   (let ((alist gnus-topic-alist)
134         out)
135     (while alist
136       (when (member group (cdar alist))
137         (setq out (caar alist)
138               alist nil))
139       (setq alist (cdr alist)))
140     out))
141
142 (defun gnus-group-parent-topic (group)
143   "Return the topic GROUP is member of by looking at the group buffer."
144   (save-excursion
145     (set-buffer gnus-group-buffer)
146     (if (gnus-group-goto-group group)
147         (gnus-current-topic)
148       (gnus-group-topic group))))
149
150 (defun gnus-topic-goto-topic (topic)
151   "Go to TOPIC."
152   (when topic
153     (gnus-goto-char (text-property-any (point-min) (point-max)
154                                        'gnus-topic (intern topic)))))
155
156 (defun gnus-current-topic ()
157   "Return the name of the current topic."
158   (let ((result
159          (or (get-text-property (point) 'gnus-topic)
160              (save-excursion
161                (and (gnus-goto-char (previous-single-property-change
162                                      (point) 'gnus-topic))
163                     (get-text-property (max (1- (point)) (point-min))
164                                        'gnus-topic))))))
165     (when result
166       (symbol-name result))))
167
168 (defun gnus-current-topics ()
169   "Return a list of all current topics, lowest in hierarchy first."
170   (let ((topic (gnus-current-topic))
171         topics)
172     (while topic
173       (push topic topics)
174       (setq topic (gnus-topic-parent-topic topic)))
175     (nreverse topics)))
176
177 (defun gnus-group-active-topic-p ()
178   "Say whether the current topic comes from the active topics."
179   (save-excursion
180     (beginning-of-line)
181     (get-text-property (point) 'gnus-active)))
182
183 (defun gnus-topic-find-groups (topic &optional level all)
184   "Return entries for all visible groups in TOPIC."
185   (let ((groups (cdr (assoc topic gnus-topic-alist)))
186         info clevel unread group lowest params visible-groups entry active)
187     (setq lowest (or lowest 1))
188     (setq level (or level 7))
189     ;; We go through the newsrc to look for matches.
190     (while groups
191       (setq entry (gnus-gethash (setq group (pop groups)) gnus-newsrc-hashtb)
192             info (nth 2 entry)
193             params (gnus-info-params info)
194             active (gnus-active group)
195             unread (or (car entry)
196                        (and (not (equal group "dummy.group"))
197                             active
198                             (- (1+ (cdr active)) (car active))))
199             clevel (or (gnus-info-level info)
200                        (if (member group gnus-zombie-list) 8 9)))
201       (and 
202        unread                           ; nil means that the group is dead.
203        (<= clevel level)
204        (>= clevel lowest)               ; Is inside the level we want.
205        (or all
206            (if (eq unread t)
207                gnus-group-list-inactive-groups
208              (> unread 0))
209            (and gnus-list-groups-with-ticked-articles
210                 (cdr (assq 'tick (gnus-info-marks info))))
211                                         ; Has right readedness.
212            ;; Check for permanent visibility.
213            (and gnus-permanently-visible-groups
214                 (string-match gnus-permanently-visible-groups group))
215            (memq 'visible params)
216            (cdr (assq 'visible params)))
217        ;; Add this group to the list of visible groups.
218        (push (or entry group) visible-groups)))
219     (nreverse visible-groups)))
220
221 (defun gnus-topic-previous-topic (topic)
222   "Return the previous topic on the same level as TOPIC."
223   (let ((top (cddr (gnus-topic-find-topology
224                     (gnus-topic-parent-topic topic)))))
225     (unless (equal topic (caaar top))
226       (while (and top (not (equal (caaadr top) topic)))
227         (setq top (cdr top)))
228       (caaar top))))
229
230 (defun gnus-topic-parent-topic (topic &optional topology)
231   "Return the parent of TOPIC."
232   (unless topology
233     (setq topology gnus-topic-topology))
234   (let ((parent (car (pop topology)))
235         result found)
236     (while (and topology
237                 (not (setq found (equal (caaar topology) topic)))
238                 (not (setq result (gnus-topic-parent-topic topic 
239                                                            (car topology)))))
240       (setq topology (cdr topology)))
241     (or result (and found parent))))
242
243 (defun gnus-topic-next-topic (topic &optional previous)
244   "Return the next sibling of TOPIC."
245   (let ((parentt (cddr (gnus-topic-find-topology 
246                         (gnus-topic-parent-topic topic))))
247         prev)
248     (while (and parentt
249                 (not (equal (caaar parentt) topic)))
250       (setq prev (caaar parentt)
251             parentt (cdr parentt)))
252     (if previous
253         prev
254       (caaadr parentt))))
255
256 (defun gnus-topic-find-topology (topic &optional topology level remove)
257   "Return the topology of TOPIC."
258   (unless topology
259     (setq topology gnus-topic-topology)
260     (setq level 0))
261   (let ((top topology)
262         result)
263     (if (equal (caar topology) topic)
264         (progn
265           (when remove
266             (delq topology remove))
267           (cons level topology))
268       (setq topology (cdr topology))
269       (while (and topology
270                   (not (setq result (gnus-topic-find-topology
271                                      topic (car topology) (1+ level)
272                                      (and remove top)))))
273         (setq topology (cdr topology)))
274       result)))
275
276 (defvar gnus-tmp-topics nil)
277 (defun gnus-topic-list (&optional topology)
278   "Return a list of all topics in the topology."
279   (unless topology
280     (setq topology gnus-topic-topology 
281           gnus-tmp-topics nil))
282   (push (caar topology) gnus-tmp-topics)
283   (mapcar 'gnus-topic-list (cdr topology))
284   gnus-tmp-topics)
285
286 ;;; Topic parameter jazz
287
288 (defun gnus-topic-parameters (topic)
289   "Return the parameters for TOPIC."
290   (let ((top (gnus-topic-find-topology topic)))
291     (when top
292       (nth 3 (cadr top)))))
293
294 (defun gnus-topic-set-parameters (topic parameters)
295   "Set the topic parameters of TOPIC to PARAMETERS."
296   (let ((top (gnus-topic-find-topology topic)))
297     (unless top
298       (error "No such topic: %s" topic))
299     ;; We may have to extend if there is no parameters here
300     ;; to begin with.
301     (unless (nthcdr 2 (cadr top))
302       (nconc (cadr top) (list nil)))
303     (unless (nthcdr 3 (cadr top))
304       (nconc (cadr top) (list nil)))
305     (setcar (nthcdr 3 (cadr top)) parameters)))
306
307 (defun gnus-group-topic-parameters (group)
308   "Compute the group parameters for GROUP taking into account inheritance from topics."
309   (let ((params-list (list (gnus-group-get-parameter group)))
310         topics params param out)
311     (save-excursion
312       (gnus-group-goto-group group)
313       (setq topics (gnus-current-topics))
314       (while topics
315         (push (gnus-topic-parameters (pop topics)) params-list))
316       ;; We probably have lots of nil elements here, so
317       ;; we remove them.  Probably faster than doing this "properly".
318       (setq params-list (delq nil params-list))
319       ;; Now we have all the parameters, so we go through them
320       ;; and do inheritance in the obvious way.
321       (while (setq params (pop params-list))
322         (while (setq param (pop params))
323           (when (atom param)
324             (setq param (cons param t)))
325           ;; Override any old versions of this param.
326           (setq out (delq (assq (car param) out) out))
327           (push param out)))
328       ;; Return the resulting parameter list.
329       out)))
330
331 ;;; General utility functions
332
333 (defun gnus-topic-enter-dribble ()
334   (gnus-dribble-enter
335    (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
336
337 ;;; Generating group buffers
338
339 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
340   "List all newsgroups with unread articles of level LEVEL or lower, and
341 use the `gnus-group-topics' to sort the groups.
342 If ALL is non-nil, list groups that have no unread articles.
343 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
344   (set-buffer gnus-group-buffer)
345   (let ((buffer-read-only nil)
346         (lowest (or lowest 1)))
347
348     (setq gnus-topic-tallied-groups nil)
349
350     (when (or (not gnus-topic-alist)
351               (not gnus-topology-checked-p))
352       (gnus-topic-check-topology))
353
354     (unless list-topic 
355       (erase-buffer))
356     
357     ;; List dead groups?
358     (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
359       (gnus-group-prepare-flat-list-dead 
360        (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
361        gnus-level-zombie ?Z
362        regexp))
363     
364     (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
365       (gnus-group-prepare-flat-list-dead 
366        (setq gnus-killed-list (sort gnus-killed-list 'string<))
367        gnus-level-killed ?K
368        regexp))
369
370     ;; Use topics.
371     (when (< lowest gnus-level-zombie)
372       (if list-topic
373           (let ((top (gnus-topic-find-topology list-topic)))
374             (gnus-topic-prepare-topic (cdr top) (car top)
375                                       (or topic-level level) all))
376         (gnus-topic-prepare-topic gnus-topic-topology 0
377                                   (or topic-level level) all))))
378
379   (gnus-group-set-mode-line)
380   (setq gnus-group-list-mode (cons level all))
381   (run-hooks 'gnus-group-prepare-hook))
382
383 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
384   "Insert TOPIC into the group buffer.
385 If SILENT, don't insert anything.  Return the number of unread
386 articles in the topic and its subtopics."
387   (let* ((type (pop topicl))
388          (entries (gnus-topic-find-groups (car type) list-level all))
389          (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
390          (gnus-group-indentation 
391           (make-string (* gnus-topic-indent-level level) ? ))
392          (beg (progn (beginning-of-line) (point)))
393          (topicl (reverse topicl))
394          (all-entries entries)
395          (point-max (point-max))
396          (unread 0)
397          (topic (car type))
398          info entry end active)
399     ;; Insert any sub-topics.
400     (while topicl
401       (incf unread
402             (gnus-topic-prepare-topic 
403              (pop topicl) (1+ level) list-level all
404              (not visiblep))))
405     (setq end (point))
406     (goto-char beg)
407     ;; Insert all the groups that belong in this topic.
408     (while (setq entry (pop entries))
409       (when visiblep 
410         (if (stringp entry)
411             ;; Dead groups.
412             (gnus-group-insert-group-line
413              entry (if (member entry gnus-zombie-list) 8 9)
414              nil (- (1+ (cdr (setq active (gnus-active entry))))
415                     (car active))
416              nil)
417           ;; Living groups.
418           (when (setq info (nth 2 entry))
419             (gnus-group-insert-group-line 
420              (gnus-info-group info)
421              (gnus-info-level info) (gnus-info-marks info)
422              (car entry) (gnus-info-method info)))))
423       (when (and (listp entry)
424                  (numberp (car entry))
425                  (not (member (gnus-info-group (setq info (nth 2 entry)))
426                               gnus-topic-tallied-groups)))
427         (push (gnus-info-group info) gnus-topic-tallied-groups)
428         (incf unread (car entry))))
429     (goto-char beg)
430     ;; Insert the topic line.
431     (when (and (not silent)
432                (or gnus-topic-display-empty-topics
433                    (not (zerop unread))
434                    (/= point-max (point-max))))
435       (gnus-extent-start-open (point))
436       (gnus-topic-insert-topic-line 
437        (car type) visiblep
438        (not (eq (nth 2 type) 'hidden))
439        level all-entries unread))
440     (goto-char end)
441     unread))
442
443 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
444   "Remove the current topic."
445   (let ((topic (gnus-group-topic-name))
446         (level (gnus-group-topic-level))
447         (beg (progn (beginning-of-line) (point)))
448         buffer-read-only)
449     (when topic
450       (while (and (zerop (forward-line 1))
451                   (> (or (gnus-group-topic-level) (1+ level)) level)))
452       (delete-region beg (point))
453       ;; Do the change in this rather odd manner because it has been
454       ;; reported that some topics share parts of some lists, for some
455       ;; reason.  I have been unable to determine why this is the
456       ;; case, but this hack seems to take care of things.
457       (let ((data (cadr (gnus-topic-find-topology topic))))
458         (setcdr data
459                 (list (if insert 'visible 'invisible)
460                       (if hide 'hide nil)
461                       (cadddr data))))
462       (if total-remove
463           (setq gnus-topic-alist
464                 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
465         (gnus-topic-insert-topic topic in-level)))))
466
467 (defun gnus-topic-insert-topic (topic &optional level)
468   "Insert TOPIC."
469   (gnus-group-prepare-topics 
470    (car gnus-group-list-mode) (cdr gnus-group-list-mode)
471    nil nil topic level))
472   
473 (defun gnus-topic-fold (&optional insert)
474   "Remove/insert the current topic."
475   (let ((topic (gnus-group-topic-name)))
476     (when topic
477       (save-excursion
478         (if (not (gnus-group-active-topic-p))
479             (gnus-topic-remove-topic
480              (or insert (not (gnus-topic-visible-p))))
481           (let ((gnus-topic-topology gnus-topic-active-topology)
482                 (gnus-topic-alist gnus-topic-active-alist)
483                 (gnus-group-list-mode (cons 5 t)))
484             (gnus-topic-remove-topic
485              (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
486
487 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries 
488                                           &optional unread)
489   (let* ((visible (if visiblep "" "..."))
490          (indentation (make-string (* gnus-topic-indent-level level) ? ))
491          (total-number-of-articles unread)
492          (number-of-groups (length entries))
493          (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
494     (beginning-of-line)
495     ;; Insert the text.
496     (gnus-add-text-properties 
497      (point)
498      (prog1 (1+ (point))
499        (eval gnus-topic-line-format-spec)
500        (gnus-topic-remove-excess-properties)1)
501      (list 'gnus-topic (intern name)
502            'gnus-topic-level level
503            'gnus-topic-unread unread
504            'gnus-active active-topic
505            'gnus-topic-visible visiblep))))
506
507 (defun gnus-topic-update-topics-containing-group (group)
508   "Update all topics that have GROUP as a member."
509   (when (and (eq major-mode 'gnus-group-mode)
510              gnus-topic-mode)
511     (save-excursion
512       (let ((alist gnus-topic-alist))
513         ;; This is probably not entirely correct.  If a topic
514         ;; isn't shown, then it's not updated.  But the updating
515         ;; should be performed in any case, since the topic's
516         ;; parent should be updated.  Pfft.
517         (while alist
518           (when (and (member group (cdar alist))
519                      (gnus-topic-goto-topic (caar alist)))
520             (gnus-topic-update-topic-line (caar alist)))
521           (pop alist))))))
522
523 (defun gnus-topic-update-topic ()
524   "Update all parent topics to the current group."
525   (when (and (eq major-mode 'gnus-group-mode)
526              gnus-topic-mode)
527     (let ((group (gnus-group-group-name))
528           (buffer-read-only nil))
529       (when (and group 
530                  (gnus-get-info group)
531                  (gnus-topic-goto-topic (gnus-current-topic)))
532         (gnus-topic-update-topic-line (gnus-group-topic-name))
533         (gnus-group-goto-group group)
534         (gnus-group-position-point)))))
535
536 (defun gnus-topic-goto-missing-group (group)
537   "Place point where GROUP is supposed to be inserted."
538   (let* ((topic (gnus-group-topic group))
539          (groups (cdr (assoc topic gnus-topic-alist)))
540          (g (cdr (member group groups)))
541          (unfound t))
542     ;; Try to jump to a visible group.
543     (while (and g (not (gnus-group-goto-group (car g) t)))
544       (pop g))
545     ;; It wasn't visible, so we try to see where to insert it.
546     (when (not g)
547       (setq g (cdr (member group (reverse groups))))
548       (while (and g unfound)
549         (when (gnus-group-goto-group (pop g) t)
550           (forward-line 1)
551           (setq unfound nil)))
552       (when unfound
553         (gnus-topic-goto-topic topic)
554         (forward-line 1)))))
555
556 (defun gnus-topic-update-topic-line (topic-name &optional reads)
557   (let* ((top (gnus-topic-find-topology topic-name))
558          (type (cadr top))
559          (children (cddr top))
560          (entries (gnus-topic-find-groups 
561                    (car type) (car gnus-group-list-mode)
562                    (cdr gnus-group-list-mode)))
563          (parent (gnus-topic-parent-topic topic-name))
564          (all-entries entries)
565          (unread 0)
566          old-unread entry)
567     (when (gnus-topic-goto-topic (car type))
568       ;; Tally all the groups that belong in this topic.
569       (if reads
570           (setq unread (- (gnus-group-topic-unread) reads))
571         (while children
572           (incf unread (gnus-topic-unread (caar (pop children)))))
573         (while (setq entry (pop entries))
574           (when (numberp (car entry))
575             (incf unread (car entry)))))
576       (setq old-unread (gnus-group-topic-unread))
577       ;; Insert the topic line.
578       (gnus-topic-insert-topic-line 
579        (car type) (gnus-topic-visible-p)
580        (not (eq (nth 2 type) 'hidden))
581        (gnus-group-topic-level) all-entries unread)
582       (gnus-delete-line))
583     (when parent
584       (forward-line -1)
585       (gnus-topic-update-topic-line
586        parent (- old-unread (gnus-group-topic-unread))))
587     unread))
588
589 (defun gnus-topic-group-indentation ()
590   (make-string 
591    (* gnus-topic-indent-level
592       (or (save-excursion
593             (forward-line -1)
594             (gnus-topic-goto-topic (gnus-current-topic))
595             (gnus-group-topic-level))
596           0))
597    ? ))
598
599 ;;; Initialization
600
601 (gnus-add-shutdown 'gnus-topic-close 'gnus)
602
603 (defun gnus-topic-close ()
604   (setq gnus-topic-active-topology nil
605         gnus-topic-active-alist nil
606         gnus-topic-killed-topics nil
607         gnus-topic-tallied-groups nil
608         gnus-topology-checked-p nil))
609
610 (defun gnus-topic-check-topology ()
611   ;; The first time we set the topology to whatever we have
612   ;; gotten here, which can be rather random.
613   (unless gnus-topic-alist
614     (gnus-topic-init-alist))
615
616   (setq gnus-topology-checked-p t)
617   ;; Go through the topic alist and make sure that all topics
618   ;; are in the topic topology.
619   (let ((topics (gnus-topic-list))
620         (alist gnus-topic-alist)
621         changed)
622     (while alist
623       (unless (member (caar alist) topics)
624         (nconc gnus-topic-topology
625                (list (list (list (caar alist) 'visible))))
626         (setq changed t))
627       (setq alist (cdr alist)))
628     (when changed
629       (gnus-topic-enter-dribble))
630     ;; Conversely, go through the topology and make sure that all
631     ;; topologies have alists.
632     (while topics
633       (unless (assoc (car topics) gnus-topic-alist)
634         (push (list (car topics)) gnus-topic-alist))
635       (pop topics)))
636   ;; Go through all living groups and make sure that
637   ;; they belong to some topic.
638   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
639                                          gnus-topic-alist)))
640          (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
641          (newsrc gnus-newsrc-alist)
642          group)
643     (while newsrc
644       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
645         (setcdr entry (cons group (cdr entry))))))
646   ;; Go through all topics and make sure they contain only living groups.
647   (let ((alist gnus-topic-alist)
648         topic)
649     (while (setq topic (pop alist))
650       (while (cdr topic)
651         (if (gnus-gethash (cadr topic) gnus-newsrc-hashtb)
652             (setq topic (cdr topic))
653           (setcdr topic (cddr topic)))))))
654
655 (defun gnus-topic-init-alist ()
656   "Initialize the topic structures."
657   (setq gnus-topic-topology
658         (cons (list "Gnus" 'visible)
659               (mapcar (lambda (topic)
660                         (list (list (car topic) 'visible)))
661                       '(("misc")))))
662   (setq gnus-topic-alist
663         (list (cons "misc"
664                     (mapcar (lambda (info) (gnus-info-group info))
665                             (cdr gnus-newsrc-alist)))
666               (list "Gnus")))
667   (gnus-topic-enter-dribble))
668
669 ;;; Maintenance
670
671 (defun gnus-topic-clean-alist ()
672   "Remove bogus groups from the topic alist."
673   (let ((topic-alist gnus-topic-alist)
674         result topic)
675     (unless gnus-killed-hashtb
676       (gnus-make-hashtable-from-killed))
677     (while (setq topic (pop topic-alist))
678       (let ((topic-name (pop topic))
679             group filtered-topic)
680         (while (setq group (pop topic))
681           (when (and (or (gnus-gethash group gnus-active-hashtb)
682                          (gnus-info-method (gnus-get-info group)))
683                      (not (gnus-gethash group gnus-killed-hashtb)))
684             (push group filtered-topic)))
685         (push (cons topic-name (nreverse filtered-topic)) result)))
686     (setq gnus-topic-alist (nreverse result))))
687
688 (defun gnus-topic-change-level (group level oldlevel)
689   "Run when changing levels to enter/remove groups from topics."
690   (save-excursion
691     (set-buffer gnus-group-buffer)
692     (when (and gnus-topic-mode 
693                gnus-topic-alist
694                (not gnus-topic-inhibit-change-level))
695       ;; Remove the group from the topics.
696       (when (and (< oldlevel gnus-level-zombie)
697                  (>= level gnus-level-zombie))
698         (let (alist)
699           (forward-line -1)
700           (when (setq alist (assoc (gnus-current-topic) gnus-topic-alist))
701             (setcdr alist (gnus-delete-first group (cdr alist))))))
702       ;; If the group is subscribed.  then we enter it into the topics.
703       (when (and (< level gnus-level-zombie)
704                  (>= oldlevel gnus-level-zombie))
705         (let* ((prev (gnus-group-group-name))
706                (gnus-topic-inhibit-change-level t)
707                (gnus-group-indentation
708                 (make-string 
709                  (* gnus-topic-indent-level
710                     (or (save-excursion
711                           (gnus-topic-goto-topic (gnus-current-topic))
712                           (gnus-group-topic-level))
713                         0))
714                  ? ))
715                (yanked (list group))
716                alist talist end)
717           ;; Then we enter the yanked groups into the topics they belong
718           ;; to. 
719           (when (setq alist (assoc (save-excursion
720                                      (forward-line -1)
721                                      (or
722                                       (gnus-current-topic)
723                                       (caar gnus-topic-topology)))
724                                    gnus-topic-alist))
725             (setq talist alist)
726             (when (stringp yanked)
727               (setq yanked (list yanked)))
728             (if (not prev)
729                 (nconc alist yanked)
730               (if (not (cdr alist))
731                   (setcdr alist (nconc yanked (cdr alist)))
732                 (while (and (not end) (cdr alist))
733                   (when (equal (cadr alist) prev)
734                     (setcdr alist (nconc yanked (cdr alist)))
735                     (setq end t))
736                   (setq alist (cdr alist)))
737                 (unless end
738                   (nconc talist yanked))))))
739         (gnus-topic-update-topic)))))
740
741 (defun gnus-topic-goto-next-group (group props)
742   "Go to group or the next group after group."
743   (if (null group)
744       (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props))))
745     (if (gnus-group-goto-group group)
746         t
747       ;; The group is no longer visible.
748       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
749              (after (cdr (member group (cdr list)))))
750         ;; First try to put point on a group after the current one.
751         (while (and after
752                     (not (gnus-group-goto-group (car after))))
753           (setq after (cdr after)))
754         ;; Then try to put point on a group before point.
755         (unless after
756           (setq after (cdr (member group (reverse (cdr list)))))
757           (while (and after 
758                       (not (gnus-group-goto-group (car after))))
759             (setq after (cdr after))))
760         ;; Finally, just put point on the topic.
761         (unless after
762           (gnus-topic-goto-topic (car list))
763           (setq after nil))
764         t))))
765
766 ;;; Topic-active functions
767
768 (defun gnus-topic-grok-active (&optional force)
769   "Parse all active groups and create topic structures for them."
770   ;; First we make sure that we have really read the active file. 
771   (when (or force
772             (not gnus-topic-active-alist))
773     (let (groups)
774       ;; Get a list of all groups available.
775       (mapatoms (lambda (g) (when (symbol-value g)
776                               (push (symbol-name g) groups)))
777                 gnus-active-hashtb)
778       (setq groups (sort groups 'string<))
779       ;; Init the variables.
780       (setq gnus-topic-active-topology (list (list "" 'visible)))
781       (setq gnus-topic-active-alist nil)
782       ;; Descend the top-level hierarchy.
783       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
784       ;; Set the top-level topic names to something nice.
785       (setcar (car gnus-topic-active-topology) "Gnus active")
786       (setcar (car gnus-topic-active-alist) "Gnus active"))))
787
788 (defun gnus-topic-grok-active-1 (topology groups)
789   (let* ((name (caar topology))
790          (prefix (concat "^" (regexp-quote name)))
791          tgroups ntopology group)
792     (while (and groups
793                 (string-match prefix (setq group (car groups))))
794       (if (not (string-match "\\." group (match-end 0)))
795           ;; There are no further hierarchies here, so we just
796           ;; enter this group into the list belonging to this
797           ;; topic.
798           (push (pop groups) tgroups)
799         ;; New sub-hierarchy, so we add it to the topology.
800         (nconc topology (list (setq ntopology 
801                                     (list (list (substring 
802                                                  group 0 (match-end 0))
803                                                 'invisible)))))
804         ;; Descend the hierarchy.
805         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
806     ;; We remove the trailing "." from the topic name.
807     (setq name
808           (if (string-match "\\.$" name)
809               (substring name 0 (match-beginning 0))
810             name))
811     ;; Add this topic and its groups to the topic alist.
812     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
813     (setcar (car topology) name)
814     ;; We return the rest of the groups that didn't belong
815     ;; to this topic.
816     groups))
817
818 ;;; Topic mode, commands and keymap.
819
820 (defvar gnus-topic-mode-map nil)
821 (defvar gnus-group-topic-map nil)
822
823 (unless gnus-topic-mode-map
824   (setq gnus-topic-mode-map (make-sparse-keymap))
825
826   ;; Override certain group mode keys.
827   (gnus-define-keys gnus-topic-mode-map
828     "=" gnus-topic-select-group
829     "\r" gnus-topic-select-group
830     " " gnus-topic-read-group
831     "\C-k" gnus-topic-kill-group
832     "\C-y" gnus-topic-yank-group
833     "\M-g" gnus-topic-get-new-news-this-topic
834     "AT" gnus-topic-list-active
835     "Gp" gnus-topic-edit-parameters
836     gnus-mouse-2 gnus-mouse-pick-topic)
837
838   ;; Define a new submap.
839   (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
840     "#" gnus-topic-mark-topic
841     "\M-#" gnus-topic-unmark-topic
842     "n" gnus-topic-create-topic
843     "m" gnus-topic-move-group
844     "D" gnus-topic-remove-group
845     "c" gnus-topic-copy-group
846     "h" gnus-topic-hide-topic
847     "s" gnus-topic-show-topic
848     "M" gnus-topic-move-matching
849     "C" gnus-topic-copy-matching
850     "\C-i" gnus-topic-indent
851     [tab] gnus-topic-indent
852     "r" gnus-topic-rename
853     "\177" gnus-topic-delete)
854
855   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
856     "s" gnus-topic-sort-groups
857     "a" gnus-topic-sort-groups-by-alphabet
858     "u" gnus-topic-sort-groups-by-unread
859     "l" gnus-topic-sort-groups-by-level
860     "v" gnus-topic-sort-groups-by-score
861     "r" gnus-topic-sort-groups-by-rank
862     "m" gnus-topic-sort-groups-by-method))
863
864 (defun gnus-topic-make-menu-bar ()
865   (unless (boundp 'gnus-topic-menu)
866     (easy-menu-define
867      gnus-topic-menu gnus-topic-mode-map ""
868      '("Topics"
869        ["Toggle topics" gnus-topic-mode t]
870        ("Groups"
871         ["Copy" gnus-topic-copy-group t]
872         ["Move" gnus-topic-move-group t]
873         ["Remove" gnus-topic-remove-group t]
874         ["Copy matching" gnus-topic-copy-matching t]
875         ["Move matching" gnus-topic-move-matching t])
876        ("Topics"
877         ["Show" gnus-topic-show-topic t]
878         ["Hide" gnus-topic-hide-topic t]
879         ["Delete" gnus-topic-delete t]
880         ["Rename" gnus-topic-rename t]
881         ["Create" gnus-topic-create-topic t]
882         ["Mark" gnus-topic-mark-topic t]
883         ["Indent" gnus-topic-indent t])
884        ["List active" gnus-topic-list-active t]))))
885
886 (defun gnus-topic-mode (&optional arg redisplay)
887   "Minor mode for topicsifying Gnus group buffers."
888   (interactive (list current-prefix-arg t))
889   (when (eq major-mode 'gnus-group-mode)
890     (make-local-variable 'gnus-topic-mode)
891     (setq gnus-topic-mode 
892           (if (null arg) (not gnus-topic-mode)
893             (> (prefix-numeric-value arg) 0)))
894     ;; Infest Gnus with topics.
895     (when gnus-topic-mode
896       (when (gnus-visual-p 'topic-menu 'menu)
897         (gnus-topic-make-menu-bar))
898       (setq gnus-topic-line-format-spec 
899             (gnus-parse-format gnus-topic-line-format 
900                                gnus-topic-line-format-alist t))
901       (unless (assq 'gnus-topic-mode minor-mode-alist)
902         (push '(gnus-topic-mode " Topic") minor-mode-alist))
903       (unless (assq 'gnus-topic-mode minor-mode-map-alist)
904         (push (cons 'gnus-topic-mode gnus-topic-mode-map)
905               minor-mode-map-alist))
906       (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
907       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
908       (set (make-local-variable 'gnus-group-prepare-function)
909            'gnus-group-prepare-topics)
910       (set (make-local-variable 'gnus-group-get-parameter-function)
911            'gnus-group-topic-parameters)
912       (set (make-local-variable 'gnus-group-goto-next-group-function)
913            'gnus-topic-goto-next-group)
914       (set (make-local-variable 'gnus-group-indentation-function)
915            'gnus-topic-group-indentation)
916       (set (make-local-variable 'gnus-group-update-group-function)
917            'gnus-topic-update-topics-containing-group)
918       (set (make-local-variable 'gnus-group-sort-alist-function)
919            'gnus-group-sort-topic)
920       (setq gnus-group-change-level-function 'gnus-topic-change-level)
921       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
922       (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
923       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
924       (setq gnus-topology-checked-p nil)
925       ;; We check the topology.
926       (when gnus-newsrc-alist
927         (gnus-topic-check-topology))
928       (run-hooks 'gnus-topic-mode-hook))
929     ;; Remove topic infestation.
930     (unless gnus-topic-mode
931       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
932       (remove-hook 'gnus-group-change-level-function 
933                    'gnus-topic-change-level)
934       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
935       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
936       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
937     (when redisplay
938       (gnus-group-list-groups))))
939     
940 (defun gnus-topic-select-group (&optional all)
941   "Select this newsgroup.
942 No article is selected automatically.
943 If ALL is non-nil, already read articles become readable.
944 If ALL is a number, fetch this number of articles.
945
946 If performed over a topic line, toggle folding the topic."
947   (interactive "P")
948   (if (gnus-group-topic-p)
949       (let ((gnus-group-list-mode 
950              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
951         (gnus-topic-fold all))
952     (gnus-group-select-group all)))
953
954 (defun gnus-mouse-pick-topic (e)
955   "Select the group or topic under the mouse pointer."
956   (interactive "e")
957   (mouse-set-point e)
958   (gnus-topic-read-group nil))
959
960 (defun gnus-topic-read-group (&optional all no-article group)
961   "Read news in this newsgroup.
962 If the prefix argument ALL is non-nil, already read articles become
963 readable.  IF ALL is a number, fetch this number of articles.  If the
964 optional argument NO-ARTICLE is non-nil, no article will be
965 auto-selected upon group entry.  If GROUP is non-nil, fetch that
966 group.
967
968 If performed over a topic line, toggle folding the topic."
969   (interactive "P")
970   (if (gnus-group-topic-p)
971       (let ((gnus-group-list-mode 
972              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
973         (gnus-topic-fold all))
974     (gnus-group-read-group all no-article group)))
975
976 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
977   (interactive 
978    (list
979     (read-string "New topic: ")
980     (gnus-current-topic)))
981   ;; Check whether this topic already exists.
982   (when (gnus-topic-find-topology topic)
983     (error "Topic already exists"))
984   (unless parent
985     (setq parent (caar gnus-topic-topology)))
986   (let ((top (cdr (gnus-topic-find-topology parent)))
987         (full-topic (or full-topic `((,topic visible)))))
988     (unless top
989       (error "No such parent topic: %s" parent))
990     (if previous
991         (progn
992           (while (and (cdr top)
993                       (not (equal (caaadr top) previous)))
994             (setq top (cdr top)))
995           (setcdr top (cons full-topic (cdr top))))
996       (nconc top (list full-topic)))
997     (unless (assoc topic gnus-topic-alist)
998       (push (list topic) gnus-topic-alist)))
999   (gnus-topic-enter-dribble)
1000   (gnus-group-list-groups)
1001   (gnus-topic-goto-topic topic))
1002
1003 (defun gnus-topic-move-group (n topic &optional copyp)
1004   "Move the next N groups to TOPIC.
1005 If COPYP, copy the groups instead."
1006   (interactive
1007    (list current-prefix-arg
1008          (completing-read "Move to topic: " gnus-topic-alist nil t)))
1009   (let ((groups (gnus-group-process-prefix n))
1010         (topicl (assoc topic gnus-topic-alist))
1011         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1012         (start-topic (gnus-group-topic-name))
1013         entry)
1014     (mapcar 
1015      (lambda (g)
1016        (gnus-group-remove-mark g)
1017        (when (and
1018               (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1019               (not copyp))
1020          (setcdr entry (gnus-delete-first g (cdr entry))))
1021        (nconc topicl (list g)))
1022      groups)
1023     (gnus-topic-enter-dribble)
1024     (if start-group
1025         (gnus-group-goto-group start-group)
1026       (gnus-topic-goto-topic start-topic))
1027     (gnus-group-list-groups)))
1028
1029 (defun gnus-topic-remove-group (&optional arg)
1030   "Remove the current group from the topic."
1031   (interactive "P")
1032   (gnus-group-iterate arg 
1033     (lambda (group)
1034       (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1035             (buffer-read-only nil))
1036         (when (and topicl group)
1037           (gnus-delete-line)
1038           (gnus-delete-first group topicl))
1039         (gnus-topic-update-topic)
1040         (gnus-group-position-point)))))
1041
1042 (defun gnus-topic-copy-group (n topic)
1043   "Copy the current group to a topic."
1044   (interactive
1045    (list current-prefix-arg
1046          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1047   (gnus-topic-move-group n topic t))
1048
1049 (defun gnus-topic-kill-group (&optional n discard)
1050   "Kill the next N groups."
1051   (interactive "P")
1052   (if (gnus-group-topic-p)
1053       (let ((topic (gnus-group-topic-name)))
1054         (gnus-topic-remove-topic nil t)
1055         (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1056               gnus-topic-killed-topics)
1057         (gnus-topic-enter-dribble))
1058     (gnus-group-kill-group n discard)
1059     (gnus-topic-update-topic)))
1060   
1061 (defun gnus-topic-yank-group (&optional arg)
1062   "Yank the last topic."
1063   (interactive "p")
1064   (if gnus-topic-killed-topics
1065       (let ((previous 
1066              (or (gnus-group-topic-name)
1067                  (gnus-topic-next-topic (gnus-current-topic))))
1068             (item (cdr (pop gnus-topic-killed-topics))))
1069         (gnus-topic-create-topic
1070          (caar item) (gnus-topic-parent-topic previous) previous
1071          item)
1072         (gnus-topic-enter-dribble)
1073         (gnus-topic-goto-topic (caar item)))
1074     (let* ((prev (gnus-group-group-name))
1075            (gnus-topic-inhibit-change-level t)
1076            (gnus-group-indentation
1077             (make-string 
1078              (* gnus-topic-indent-level
1079                 (or (save-excursion
1080                       (gnus-topic-goto-topic (gnus-current-topic))
1081                       (gnus-group-topic-level))
1082                     0))
1083              ? ))
1084            yanked alist)
1085       ;; We first yank the groups the normal way...
1086       (setq yanked (gnus-group-yank-group arg))
1087       ;; Then we enter the yanked groups into the topics they belong
1088       ;; to. 
1089       (setq alist (assoc (save-excursion
1090                            (forward-line -1)
1091                            (gnus-current-topic))
1092                          gnus-topic-alist))
1093       (when (stringp yanked)
1094         (setq yanked (list yanked)))
1095       (if (not prev)
1096           (nconc alist yanked)
1097         (if (not (cdr alist))
1098             (setcdr alist (nconc yanked (cdr alist)))
1099           (while (cdr alist)
1100             (when (equal (cadr alist) prev)
1101               (setcdr alist (nconc yanked (cdr alist)))
1102               (setq alist nil))
1103             (setq alist (cdr alist))))))
1104     (gnus-topic-update-topic)))
1105
1106 (defun gnus-topic-hide-topic ()
1107   "Hide the current topic."
1108   (interactive)
1109   (when (gnus-current-topic)
1110     (gnus-topic-goto-topic (gnus-current-topic))
1111     (gnus-topic-remove-topic nil nil 'hidden)))
1112
1113 (defun gnus-topic-show-topic ()
1114   "Show the hidden topic."
1115   (interactive)
1116   (when (gnus-group-topic-p)
1117     (gnus-topic-remove-topic t nil 'shown)))
1118
1119 (defun gnus-topic-mark-topic (topic &optional unmark)
1120   "Mark all groups in the topic with the process mark."
1121   (interactive (list (gnus-current-topic)))
1122   (save-excursion
1123     (let ((groups (gnus-topic-find-groups topic 9 t)))
1124       (while groups
1125         (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1126                  (gnus-info-group (nth 2 (pop groups))))))))
1127
1128 (defun gnus-topic-unmark-topic (topic &optional unmark)
1129   "Remove the process mark from all groups in the topic."
1130   (interactive (list (gnus-current-topic)))
1131   (gnus-topic-mark-topic topic t))
1132
1133 (defun gnus-topic-get-new-news-this-topic (&optional n)
1134   "Check for new news in the current topic."
1135   (interactive "P")
1136   (if (not (gnus-group-topic-p))
1137       (gnus-group-get-new-news-this-group n)
1138     (gnus-topic-mark-topic (gnus-group-topic-name))
1139     (gnus-group-get-new-news-this-group)))
1140
1141 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1142   "Move all groups that match REGEXP to some topic."
1143   (interactive
1144    (let (topic)
1145      (nreverse
1146       (list
1147        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1148        (read-string (format "Move to %s (regexp): " topic))))))
1149   (gnus-group-mark-regexp regexp)
1150   (gnus-topic-move-group nil topic copyp))
1151
1152 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1153   "Copy all groups that match REGEXP to some topic."
1154   (interactive
1155    (let (topic)
1156      (nreverse
1157       (list
1158        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1159        (read-string (format "Copy to %s (regexp): " topic))))))
1160   (gnus-topic-move-matching regexp topic t))
1161
1162 (defun gnus-topic-delete (topic)
1163   "Delete a topic."
1164   (interactive (list (gnus-group-topic-name)))
1165   (unless topic
1166     (error "No topic to be deleted"))
1167   (let ((entry (assoc topic gnus-topic-alist))
1168         (buffer-read-only nil))
1169     (when (cdr entry)
1170       (error "Topic not empty"))
1171     ;; Delete if visible.
1172     (when (gnus-topic-goto-topic topic)
1173       (gnus-delete-line))
1174     ;; Remove from alist.
1175     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1176     ;; Remove from topology.
1177     (gnus-topic-find-topology topic nil nil 'delete)))
1178
1179 (defun gnus-topic-rename (old-name new-name)
1180   "Rename a topic."
1181   (interactive
1182    (let ((topic (gnus-current-topic)))
1183      (list topic
1184            (read-string (format "Rename %s to: " topic)))))
1185   (let ((top (gnus-topic-find-topology old-name))
1186         (entry (assoc old-name gnus-topic-alist)))
1187     (when top
1188       (setcar (cadr top) new-name))
1189     (when entry 
1190       (setcar entry new-name))
1191     (forward-line -1)
1192     (gnus-group-list-groups)))
1193
1194 (defun gnus-topic-indent (&optional unindent)
1195   "Indent a topic -- make it a sub-topic of the previous topic.
1196 If UNINDENT, remove an indentation."
1197   (interactive "P")
1198   (if unindent
1199       (gnus-topic-unindent)
1200     (let* ((topic (gnus-current-topic))
1201            (parent (gnus-topic-previous-topic topic)))
1202       (unless parent
1203         (error "Nothing to indent %s into" topic))
1204       (when topic
1205         (gnus-topic-goto-topic topic)
1206         (gnus-topic-kill-group)
1207         (gnus-topic-create-topic
1208          topic parent nil (cdr (pop gnus-topic-killed-topics)))
1209         (or (gnus-topic-goto-topic topic)
1210             (gnus-topic-goto-topic parent))))))
1211
1212 (defun gnus-topic-unindent ()
1213   "Unindent a topic."
1214   (interactive)
1215   (let* ((topic (gnus-current-topic))
1216          (parent (gnus-topic-parent-topic topic))
1217          (grandparent (gnus-topic-parent-topic parent)))
1218     (unless grandparent
1219       (error "Nothing to indent %s into" topic))
1220     (when topic
1221       (gnus-topic-goto-topic topic)
1222       (gnus-topic-kill-group)
1223       (gnus-topic-create-topic
1224        topic grandparent (gnus-topic-next-topic parent)
1225        (cdr (pop gnus-topic-killed-topics)))
1226       (gnus-topic-goto-topic topic))))
1227
1228 (defun gnus-topic-list-active (&optional force)
1229   "List all groups that Gnus knows about in a topicsified fashion.
1230 If FORCE, always re-read the active file."
1231   (interactive "P")
1232   (when force
1233     (gnus-get-killed-groups))
1234   (gnus-topic-grok-active force)
1235   (let ((gnus-topic-topology gnus-topic-active-topology)
1236         (gnus-topic-alist gnus-topic-active-alist)
1237         gnus-killed-list gnus-zombie-list)
1238     (gnus-group-list-groups 9 nil 1)))
1239
1240 ;;; Topic sorting functions
1241
1242 (defun gnus-topic-edit-parameters (group)
1243   "Edit the group parameters of GROUP.
1244 If performed on a topic, edit the topic parameters instead."
1245   (interactive (list (gnus-group-group-name)))
1246   (if group
1247       (gnus-group-edit-group-parameters group)
1248     (if (not (gnus-group-topic-p))
1249         (error "Nothing to edit on the current line.")
1250       (let ((topic (gnus-group-topic-name)))
1251         (gnus-edit-form
1252          (gnus-topic-parameters topic)
1253          "Editing the topic parameters."
1254          `(lambda (form)
1255             (gnus-topic-set-parameters ,topic form)))))))
1256
1257 (defun gnus-group-sort-topic (func reverse)
1258   "Sort groups in the topics according to FUNC and REVERSE."
1259   (let ((alist gnus-topic-alist))
1260     (while alist
1261       (gnus-topic-sort-topic (pop alist) func reverse))))
1262
1263 (defun gnus-topic-sort-topic (topic func reverse)
1264   ;; Each topic only lists the name of the group, while
1265   ;; the sort predicates expect group infos as inputs.
1266   ;; So we first transform the group names into infos,
1267   ;; then sort, and then transform back into group names.
1268   (setcdr
1269    topic
1270    (mapcar
1271     (lambda (info) (gnus-info-group info))
1272     (sort
1273      (mapcar
1274       (lambda (group) (gnus-get-info group))
1275       (cdr topic))
1276      func)))
1277   ;; Do the reversal, if necessary.
1278   (when reverse
1279     (setcdr topic (nreverse (cdr topic)))))
1280
1281 (defun gnus-topic-sort-groups (func &optional reverse)
1282   "Sort the current topic according to FUNC.
1283 If REVERSE, reverse the sorting order."
1284   (interactive (list gnus-group-sort-function current-prefix-arg))
1285   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1286     (gnus-topic-sort-topic
1287      topic (gnus-make-sort-function func) reverse)
1288     (gnus-group-list-groups)))
1289
1290 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1291   "Sort the current topic alphabetically by group name.
1292 If REVERSE, sort in reverse order."
1293   (interactive "P")
1294   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1295
1296 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1297   "Sort the current topic by number of unread articles.
1298 If REVERSE, sort in reverse order."
1299   (interactive "P")
1300   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1301
1302 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1303   "Sort the current topic by group level.
1304 If REVERSE, sort in reverse order."
1305   (interactive "P")
1306   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1307
1308 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1309   "Sort the current topic by group score.
1310 If REVERSE, sort in reverse order."
1311   (interactive "P")
1312   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1313
1314 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1315   "Sort the current topic by group rank.
1316 If REVERSE, sort in reverse order."
1317   (interactive "P")
1318   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1319
1320 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1321   "Sort the current topic alphabetically by backend name.
1322 If REVERSE, sort in reverse order."
1323   (interactive "P")
1324   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1325
1326 (provide 'gnus-topic)
1327
1328 ;;; gnus-topic.el ends here