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