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