7aac67573f543aa7f58c9f7596a8e7da4cdefa9b
[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 (gnus-visual-p 'topic-menu 'menu)
895         (gnus-topic-make-menu-bar))
896       (setq gnus-topic-line-format-spec 
897             (gnus-parse-format gnus-topic-line-format 
898                                gnus-topic-line-format-alist t))
899       (unless (assq 'gnus-topic-mode minor-mode-alist)
900         (push '(gnus-topic-mode " Topic") minor-mode-alist))
901       (unless (assq 'gnus-topic-mode minor-mode-map-alist)
902         (push (cons 'gnus-topic-mode gnus-topic-mode-map)
903               minor-mode-map-alist))
904       (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
905       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
906       (set (make-local-variable 'gnus-group-prepare-function)
907            'gnus-group-prepare-topics)
908       (set (make-local-variable 'gnus-group-get-parameter-function)
909            'gnus-group-topic-parameters)
910       (set (make-local-variable 'gnus-group-goto-next-group-function)
911            'gnus-topic-goto-next-group)
912       (set (make-local-variable 'gnus-group-indentation-function)
913            'gnus-topic-group-indentation)
914       (set (make-local-variable 'gnus-group-update-group-function)
915            'gnus-topic-update-topics-containing-group)
916       (set (make-local-variable 'gnus-group-sort-alist-function)
917            'gnus-group-sort-topic)
918       (setq gnus-group-change-level-function 'gnus-topic-change-level)
919       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
920       (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
921       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
922       (setq gnus-topology-checked-p nil)
923       ;; We check the topology.
924       (when gnus-newsrc-alist
925         (gnus-topic-check-topology))
926       (run-hooks 'gnus-topic-mode-hook))
927     ;; Remove topic infestation.
928     (unless gnus-topic-mode
929       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
930       (remove-hook 'gnus-group-change-level-function 
931                    'gnus-topic-change-level)
932       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
933       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
934       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
935     (when redisplay
936       (gnus-group-list-groups))))
937     
938 (defun gnus-topic-select-group (&optional all)
939   "Select this newsgroup.
940 No article is selected automatically.
941 If ALL is non-nil, already read articles become readable.
942 If ALL is a number, fetch this number of articles.
943
944 If performed over a topic line, toggle folding the topic."
945   (interactive "P")
946   (if (gnus-group-topic-p)
947       (let ((gnus-group-list-mode 
948              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
949         (gnus-topic-fold all))
950     (gnus-group-select-group all)))
951
952 (defun gnus-mouse-pick-topic (e)
953   "Select the group or topic under the mouse pointer."
954   (interactive "e")
955   (mouse-set-point e)
956   (gnus-topic-read-group nil))
957
958 (defun gnus-topic-read-group (&optional all no-article group)
959   "Read news in this newsgroup.
960 If the prefix argument ALL is non-nil, already read articles become
961 readable.  IF ALL is a number, fetch this number of articles.  If the
962 optional argument NO-ARTICLE is non-nil, no article will be
963 auto-selected upon group entry.  If GROUP is non-nil, fetch that
964 group.
965
966 If performed over a topic line, toggle folding the topic."
967   (interactive "P")
968   (if (gnus-group-topic-p)
969       (let ((gnus-group-list-mode 
970              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
971         (gnus-topic-fold all))
972     (gnus-group-read-group all no-article group)))
973
974 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
975   (interactive 
976    (list
977     (read-string "New topic: ")
978     (gnus-current-topic)))
979   ;; Check whether this topic already exists.
980   (when (gnus-topic-find-topology topic)
981     (error "Topic already exists"))
982   (unless parent
983     (setq parent (caar gnus-topic-topology)))
984   (let ((top (cdr (gnus-topic-find-topology parent)))
985         (full-topic (or full-topic `((,topic visible)))))
986     (unless top
987       (error "No such parent topic: %s" parent))
988     (if previous
989         (progn
990           (while (and (cdr top)
991                       (not (equal (caaadr top) previous)))
992             (setq top (cdr top)))
993           (setcdr top (cons full-topic (cdr top))))
994       (nconc top (list full-topic)))
995     (unless (assoc topic gnus-topic-alist)
996       (push (list topic) gnus-topic-alist)))
997   (gnus-topic-enter-dribble)
998   (gnus-group-list-groups)
999   (gnus-topic-goto-topic topic))
1000
1001 (defun gnus-topic-move-group (n topic &optional copyp)
1002   "Move the next N groups to TOPIC.
1003 If COPYP, copy the groups instead."
1004   (interactive
1005    (list current-prefix-arg
1006          (completing-read "Move to topic: " gnus-topic-alist nil t)))
1007   (let ((groups (gnus-group-process-prefix n))
1008         (topicl (assoc topic gnus-topic-alist))
1009         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1010         (start-topic (gnus-group-topic-name))
1011         entry)
1012     (mapcar 
1013      (lambda (g)
1014        (gnus-group-remove-mark g)
1015        (when (and
1016               (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1017               (not copyp))
1018          (setcdr entry (gnus-delete-first g (cdr entry))))
1019        (nconc topicl (list g)))
1020      groups)
1021     (gnus-topic-enter-dribble)
1022     (if start-group
1023         (gnus-group-goto-group start-group)
1024       (gnus-topic-goto-topic start-topic))
1025     (gnus-group-list-groups)))
1026
1027 (defun gnus-topic-remove-group (&optional arg)
1028   "Remove the current group from the topic."
1029   (interactive "P")
1030   (gnus-group-iterate arg 
1031     (lambda (group)
1032       (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1033             (buffer-read-only nil))
1034         (when (and topicl group)
1035           (gnus-delete-line)
1036           (gnus-delete-first group topicl))
1037         (gnus-topic-update-topic)
1038         (gnus-group-position-point)))))
1039
1040 (defun gnus-topic-copy-group (n topic)
1041   "Copy the current group to a topic."
1042   (interactive
1043    (list current-prefix-arg
1044          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1045   (gnus-topic-move-group n topic t))
1046
1047 (defun gnus-topic-kill-group (&optional n discard)
1048   "Kill the next N groups."
1049   (interactive "P")
1050   (if (gnus-group-topic-p)
1051       (let ((topic (gnus-group-topic-name)))
1052         (gnus-topic-remove-topic nil t)
1053         (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1054               gnus-topic-killed-topics)
1055         (gnus-topic-enter-dribble))
1056     (gnus-group-kill-group n discard)
1057     (gnus-topic-update-topic)))
1058   
1059 (defun gnus-topic-yank-group (&optional arg)
1060   "Yank the last topic."
1061   (interactive "p")
1062   (if gnus-topic-killed-topics
1063       (let ((previous 
1064              (or (gnus-group-topic-name)
1065                  (gnus-topic-next-topic (gnus-current-topic))))
1066             (item (cdr (pop gnus-topic-killed-topics))))
1067         (gnus-topic-create-topic
1068          (caar item) (gnus-topic-parent-topic previous) previous
1069          item)
1070         (gnus-topic-enter-dribble)
1071         (gnus-topic-goto-topic (caar item)))
1072     (let* ((prev (gnus-group-group-name))
1073            (gnus-topic-inhibit-change-level t)
1074            (gnus-group-indentation
1075             (make-string 
1076              (* gnus-topic-indent-level
1077                 (or (save-excursion
1078                       (gnus-topic-goto-topic (gnus-current-topic))
1079                       (gnus-group-topic-level))
1080                     0))
1081              ? ))
1082            yanked alist)
1083       ;; We first yank the groups the normal way...
1084       (setq yanked (gnus-group-yank-group arg))
1085       ;; Then we enter the yanked groups into the topics they belong
1086       ;; to. 
1087       (setq alist (assoc (save-excursion
1088                            (forward-line -1)
1089                            (gnus-current-topic))
1090                          gnus-topic-alist))
1091       (when (stringp yanked)
1092         (setq yanked (list yanked)))
1093       (if (not prev)
1094           (nconc alist yanked)
1095         (if (not (cdr alist))
1096             (setcdr alist (nconc yanked (cdr alist)))
1097           (while (cdr alist)
1098             (when (equal (cadr alist) prev)
1099               (setcdr alist (nconc yanked (cdr alist)))
1100               (setq alist nil))
1101             (setq alist (cdr alist))))))
1102     (gnus-topic-update-topic)))
1103
1104 (defun gnus-topic-hide-topic ()
1105   "Hide the current topic."
1106   (interactive)
1107   (when (gnus-current-topic)
1108     (gnus-topic-goto-topic (gnus-current-topic))
1109     (gnus-topic-remove-topic nil nil 'hidden)))
1110
1111 (defun gnus-topic-show-topic ()
1112   "Show the hidden topic."
1113   (interactive)
1114   (when (gnus-group-topic-p)
1115     (gnus-topic-remove-topic t nil 'shown)))
1116
1117 (defun gnus-topic-mark-topic (topic &optional unmark)
1118   "Mark all groups in the topic with the process mark."
1119   (interactive (list (gnus-current-topic)))
1120   (save-excursion
1121     (let ((groups (gnus-topic-find-groups topic 9 t)))
1122       (while groups
1123         (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1124                  (gnus-info-group (nth 2 (pop groups))))))))
1125
1126 (defun gnus-topic-unmark-topic (topic &optional unmark)
1127   "Remove the process mark from all groups in the topic."
1128   (interactive (list (gnus-current-topic)))
1129   (gnus-topic-mark-topic topic t))
1130
1131 (defun gnus-topic-get-new-news-this-topic (&optional n)
1132   "Check for new news in the current topic."
1133   (interactive "P")
1134   (if (not (gnus-group-topic-p))
1135       (gnus-group-get-new-news-this-group n)
1136     (gnus-topic-mark-topic (gnus-group-topic-name))
1137     (gnus-group-get-new-news-this-group)))
1138
1139 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1140   "Move all groups that match REGEXP to some topic."
1141   (interactive
1142    (let (topic)
1143      (nreverse
1144       (list
1145        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1146        (read-string (format "Move to %s (regexp): " topic))))))
1147   (gnus-group-mark-regexp regexp)
1148   (gnus-topic-move-group nil topic copyp))
1149
1150 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1151   "Copy all groups that match REGEXP to some topic."
1152   (interactive
1153    (let (topic)
1154      (nreverse
1155       (list
1156        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1157        (read-string (format "Copy to %s (regexp): " topic))))))
1158   (gnus-topic-move-matching regexp topic t))
1159
1160 (defun gnus-topic-delete (topic)
1161   "Delete a topic."
1162   (interactive (list (gnus-group-topic-name)))
1163   (unless topic
1164     (error "No topic to be deleted"))
1165   (let ((entry (assoc topic gnus-topic-alist))
1166         (buffer-read-only nil))
1167     (when (cdr entry)
1168       (error "Topic not empty"))
1169     ;; Delete if visible.
1170     (when (gnus-topic-goto-topic topic)
1171       (gnus-delete-line))
1172     ;; Remove from alist.
1173     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1174     ;; Remove from topology.
1175     (gnus-topic-find-topology topic nil nil 'delete)))
1176
1177 (defun gnus-topic-rename (old-name new-name)
1178   "Rename a topic."
1179   (interactive
1180    (let ((topic (gnus-current-topic)))
1181      (list topic
1182            (read-string (format "Rename %s to: " topic)))))
1183   (let ((top (gnus-topic-find-topology old-name))
1184         (entry (assoc old-name gnus-topic-alist)))
1185     (when top
1186       (setcar (cadr top) new-name))
1187     (when entry 
1188       (setcar entry new-name))
1189     (forward-line -1)
1190     (gnus-group-list-groups)))
1191
1192 (defun gnus-topic-indent (&optional unindent)
1193   "Indent a topic -- make it a sub-topic of the previous topic.
1194 If UNINDENT, remove an indentation."
1195   (interactive "P")
1196   (if unindent
1197       (gnus-topic-unindent)
1198     (let* ((topic (gnus-current-topic))
1199            (parent (gnus-topic-previous-topic topic)))
1200       (unless parent
1201         (error "Nothing to indent %s into" topic))
1202       (when topic
1203         (gnus-topic-goto-topic topic)
1204         (gnus-topic-kill-group)
1205         (gnus-topic-create-topic
1206          topic parent nil (cdr (pop gnus-topic-killed-topics)))
1207         (or (gnus-topic-goto-topic topic)
1208             (gnus-topic-goto-topic parent))))))
1209
1210 (defun gnus-topic-unindent ()
1211   "Unindent a topic."
1212   (interactive)
1213   (let* ((topic (gnus-current-topic))
1214          (parent (gnus-topic-parent-topic topic))
1215          (grandparent (gnus-topic-parent-topic parent)))
1216     (unless grandparent
1217       (error "Nothing to indent %s into" topic))
1218     (when topic
1219       (gnus-topic-goto-topic topic)
1220       (gnus-topic-kill-group)
1221       (gnus-topic-create-topic
1222        topic grandparent (gnus-topic-next-topic parent)
1223        (cdr (pop gnus-topic-killed-topics)))
1224       (gnus-topic-goto-topic topic))))
1225
1226 (defun gnus-topic-list-active (&optional force)
1227   "List all groups that Gnus knows about in a topicsified fashion.
1228 If FORCE, always re-read the active file."
1229   (interactive "P")
1230   (when force
1231     (gnus-get-killed-groups))
1232   (gnus-topic-grok-active force)
1233   (let ((gnus-topic-topology gnus-topic-active-topology)
1234         (gnus-topic-alist gnus-topic-active-alist)
1235         gnus-killed-list gnus-zombie-list)
1236     (gnus-group-list-groups 9 nil 1)))
1237
1238 ;;; Topic sorting functions
1239
1240 (defun gnus-topic-edit-parameters (group)
1241   "Edit the group parameters of GROUP.
1242 If performed on a topic, edit the topic parameters instead."
1243   (interactive (list (gnus-group-group-name)))
1244   (if group
1245       (gnus-group-edit-group-parameters group)
1246     (if (not (gnus-group-topic-p))
1247         (error "Nothing to edit on the current line.")
1248       (let ((topic (gnus-group-topic-name)))
1249         (gnus-edit-form
1250          (gnus-topic-parameters topic)
1251          "Editing the topic parameters."
1252          `(lambda (form)
1253             (gnus-topic-set-parameters ,topic form)))))))
1254
1255 (defun gnus-group-sort-topic (func reverse)
1256   "Sort groups in the topics according to FUNC and REVERSE."
1257   (let ((alist gnus-topic-alist))
1258     (while alist
1259       (gnus-topic-sort-topic (pop alist) func reverse))))
1260
1261 (defun gnus-topic-sort-topic (topic func reverse)
1262   ;; Each topic only lists the name of the group, while
1263   ;; the sort predicates expect group infos as inputs.
1264   ;; So we first transform the group names into infos,
1265   ;; then sort, and then transform back into group names.
1266   (setcdr
1267    topic
1268    (mapcar
1269     (lambda (info) (gnus-info-group info))
1270     (sort
1271      (mapcar
1272       (lambda (group) (gnus-get-info group))
1273       (cdr topic))
1274      func)))
1275   ;; Do the reversal, if necessary.
1276   (when reverse
1277     (setcdr topic (nreverse (cdr topic)))))
1278
1279 (defun gnus-topic-sort-groups (func &optional reverse)
1280   "Sort the current topic according to FUNC.
1281 If REVERSE, reverse the sorting order."
1282   (interactive (list gnus-group-sort-function current-prefix-arg))
1283   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1284     (gnus-topic-sort-topic
1285      topic (gnus-make-sort-function func) reverse)
1286     (gnus-group-list-groups)))
1287
1288 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1289   "Sort the current topic alphabetically by group name.
1290 If REVERSE, sort in reverse order."
1291   (interactive "P")
1292   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1293
1294 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1295   "Sort the current topic by number of unread articles.
1296 If REVERSE, sort in reverse order."
1297   (interactive "P")
1298   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1299
1300 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1301   "Sort the current topic by group level.
1302 If REVERSE, sort in reverse order."
1303   (interactive "P")
1304   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1305
1306 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1307   "Sort the current topic by group score.
1308 If REVERSE, sort in reverse order."
1309   (interactive "P")
1310   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1311
1312 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1313   "Sort the current topic by group rank.
1314 If REVERSE, sort in reverse order."
1315   (interactive "P")
1316   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1317
1318 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1319   "Sort the current topic alphabetically by backend name.
1320 If REVERSE, sort in reverse order."
1321   (interactive "P")
1322   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1323
1324 (provide 'gnus-topic)
1325
1326 ;;; gnus-topic.el ends here