*** 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       (if total-remove
450           (setq gnus-topic-alist
451                 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
452         (gnus-topic-insert-topic topic in-level)))))
453
454 (defun gnus-topic-insert-topic (topic &optional level)
455   "Insert TOPIC."
456   (gnus-group-prepare-topics 
457    (car gnus-group-list-mode) (cdr gnus-group-list-mode)
458    nil nil topic level))
459   
460 (defun gnus-topic-fold (&optional insert)
461   "Remove/insert the current topic."
462   (let ((topic (gnus-group-topic-name)))
463     (when topic
464       (save-excursion
465         (if (not (gnus-group-active-topic-p))
466             (gnus-topic-remove-topic
467              (or insert (not (gnus-topic-visible-p))))
468           (let ((gnus-topic-topology gnus-topic-active-topology)
469                 (gnus-topic-alist gnus-topic-active-alist)
470                 (gnus-group-list-mode (cons 5 t)))
471             (gnus-topic-remove-topic
472              (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
473
474 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries 
475                                           &optional unread)
476   (let* ((visible (if visiblep "" "..."))
477          (indentation (make-string (* gnus-topic-indent-level level) ? ))
478          (total-number-of-articles unread)
479          (number-of-groups (length entries))
480          (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
481     (beginning-of-line)
482     ;; Insert the text.
483     (gnus-add-text-properties 
484      (point)
485      (prog1 (1+ (point))
486        (eval gnus-topic-line-format-spec)
487        (gnus-topic-remove-excess-properties)1)
488      (list 'gnus-topic (intern name)
489            'gnus-topic-level level
490            'gnus-topic-unread unread
491            'gnus-active active-topic
492            'gnus-topic-visible visiblep))))
493
494 (defun gnus-topic-update-topics-containing-group (group)
495   "Update all topics that have GROUP as a member."
496   (when (and (eq major-mode 'gnus-group-mode)
497              gnus-topic-mode)
498     (save-excursion
499       (let ((alist gnus-topic-alist))
500         ;; This is probably not entirely correct.  If a topic
501         ;; isn't shown, then it's not updated.  But the updating
502         ;; should be performed in any case, since the topic's
503         ;; parent should be updated.  Pfft.
504         (while alist
505           (when (and (member group (cdar alist))
506                      (gnus-topic-goto-topic (caar alist)))
507             (gnus-topic-update-topic-line (caar alist)))
508           (pop alist))))))
509
510 (defun gnus-topic-update-topic ()
511   "Update all parent topics to the current group."
512   (when (and (eq major-mode 'gnus-group-mode)
513              gnus-topic-mode)
514     (let ((group (gnus-group-group-name))
515           (buffer-read-only nil))
516       (when (and group 
517                  (gnus-get-info group)
518                  (gnus-topic-goto-topic (gnus-current-topic)))
519         (gnus-topic-update-topic-line (gnus-group-topic-name))
520         (gnus-group-goto-group group)
521         (gnus-group-position-point)))))
522
523 (defun gnus-topic-goto-missing-group (group)
524   "Place point where GROUP is supposed to be inserted."
525   (let* ((topic (gnus-group-topic group))
526          (groups (cdr (assoc topic gnus-topic-alist)))
527          (g (cdr (member group groups)))
528          (unfound t))
529     ;; Try to jump to a visible group.
530     (while (and g (not (gnus-group-goto-group (car g) t)))
531       (pop g))
532     ;; It wasn't visible, so we try to see where to insert it.
533     (when (not g)
534       (setq g (cdr (member group (reverse groups))))
535       (while (and g unfound)
536         (when (gnus-group-goto-group (pop g) t)
537           (forward-line 1)
538           (setq unfound nil)))
539       (when unfound
540         (gnus-topic-goto-topic topic)
541         (forward-line 1)))))
542
543 (defun gnus-topic-update-topic-line (topic-name &optional reads)
544   (let* ((top (gnus-topic-find-topology topic-name))
545          (type (cadr top))
546          (children (cddr top))
547          (entries (gnus-topic-find-groups 
548                    (car type) (car gnus-group-list-mode)
549                    (cdr gnus-group-list-mode)))
550          (parent (gnus-topic-parent-topic topic-name))
551          (all-entries entries)
552          (unread 0)
553          old-unread entry)
554     (when (gnus-topic-goto-topic (car type))
555       ;; Tally all the groups that belong in this topic.
556       (if reads
557           (setq unread (- (gnus-group-topic-unread) reads))
558         (while children
559           (incf unread (gnus-topic-unread (caar (pop children)))))
560         (while (setq entry (pop entries))
561           (when (numberp (car entry))
562             (incf unread (car entry)))))
563       (setq old-unread (gnus-group-topic-unread))
564       ;; Insert the topic line.
565       (gnus-topic-insert-topic-line 
566        (car type) (gnus-topic-visible-p)
567        (not (eq (nth 2 type) 'hidden))
568        (gnus-group-topic-level) all-entries unread)
569       (gnus-delete-line))
570     (when parent
571       (forward-line -1)
572       (gnus-topic-update-topic-line
573        parent (- old-unread (gnus-group-topic-unread))))
574     unread))
575
576 (defun gnus-topic-group-indentation ()
577   (make-string 
578    (* gnus-topic-indent-level
579       (or (save-excursion
580             (forward-line -1)
581             (gnus-topic-goto-topic (gnus-current-topic))
582             (gnus-group-topic-level))
583           0))
584    ? ))
585
586 ;;; Initialization
587
588 (gnus-add-shutdown 'gnus-topic-close 'gnus)
589
590 (defun gnus-topic-close ()
591   (setq gnus-topic-active-topology nil
592         gnus-topic-active-alist nil
593         gnus-topic-killed-topics nil
594         gnus-topic-tallied-groups nil
595         gnus-topology-checked-p nil))
596
597 (defun gnus-topic-check-topology ()
598   ;; The first time we set the topology to whatever we have
599   ;; gotten here, which can be rather random.
600   (unless gnus-topic-alist
601     (gnus-topic-init-alist))
602
603   (setq gnus-topology-checked-p t)
604   ;; Go through the topic alist and make sure that all topics
605   ;; are in the topic topology.
606   (let ((topics (gnus-topic-list))
607         (alist gnus-topic-alist)
608         changed)
609     (while alist
610       (unless (member (caar alist) topics)
611         (nconc gnus-topic-topology
612                (list (list (list (caar alist) 'visible))))
613         (setq changed t))
614       (setq alist (cdr alist)))
615     (when changed
616       (gnus-topic-enter-dribble))
617     ;; Conversely, go through the topology and make sure that all
618     ;; topologies have alists.
619     (while topics
620       (unless (assoc (car topics) gnus-topic-alist)
621         (push (list (car topics)) gnus-topic-alist))
622       (pop topics)))
623   ;; Go through all living groups and make sure that
624   ;; they belong to some topic.
625   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
626                                          gnus-topic-alist)))
627          (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
628          (newsrc gnus-newsrc-alist)
629          group)
630     (while newsrc
631       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
632         (setcdr entry (cons group (cdr entry))))))
633   ;; Go through all topics and make sure they contain only living groups.
634   (let ((alist gnus-topic-alist)
635         topic)
636     (while (setq topic (pop alist))
637       (while (cdr topic)
638         (if (gnus-gethash (cadr topic) gnus-newsrc-hashtb)
639             (setq topic (cdr topic))
640           (setcdr topic (cddr topic)))))))
641
642 (defun gnus-topic-init-alist ()
643   "Initialize the topic structures."
644   (setq gnus-topic-topology
645         (cons (list "Gnus" 'visible)
646               (mapcar (lambda (topic)
647                         (list (list (car topic) 'visible)))
648                       '(("misc")))))
649   (setq gnus-topic-alist
650         (list (cons "misc"
651                     (mapcar (lambda (info) (gnus-info-group info))
652                             (cdr gnus-newsrc-alist)))
653               (list "Gnus")))
654   (gnus-topic-enter-dribble))
655
656 ;;; Maintenance
657
658 (defun gnus-topic-clean-alist ()
659   "Remove bogus groups from the topic alist."
660   (let ((topic-alist gnus-topic-alist)
661         result topic)
662     (unless gnus-killed-hashtb
663       (gnus-make-hashtable-from-killed))
664     (while (setq topic (pop topic-alist))
665       (let ((topic-name (pop topic))
666             group filtered-topic)
667         (while (setq group (pop topic))
668           (when (and (or (gnus-gethash group gnus-active-hashtb)
669                          (gnus-info-method (gnus-get-info group)))
670                      (not (gnus-gethash group gnus-killed-hashtb)))
671             (push group filtered-topic)))
672         (push (cons topic-name (nreverse filtered-topic)) result)))
673     (setq gnus-topic-alist (nreverse result))))
674
675 (defun gnus-topic-change-level (group level oldlevel)
676   "Run when changing levels to enter/remove groups from topics."
677   (save-excursion
678     (set-buffer gnus-group-buffer)
679     (when (and gnus-topic-mode 
680                gnus-topic-alist
681                (not gnus-topic-inhibit-change-level))
682       ;; Remove the group from the topics.
683       (when (and (< oldlevel gnus-level-zombie)
684                  (>= level gnus-level-zombie))
685         (let (alist)
686           (forward-line -1)
687           (when (setq alist (assoc (gnus-current-topic) gnus-topic-alist))
688             (setcdr alist (gnus-delete-first group (cdr alist))))))
689       ;; If the group is subscribed.  then we enter it into the topics.
690       (when (and (< level gnus-level-zombie)
691                  (>= oldlevel gnus-level-zombie))
692         (let* ((prev (gnus-group-group-name))
693                (gnus-topic-inhibit-change-level t)
694                (gnus-group-indentation
695                 (make-string 
696                  (* gnus-topic-indent-level
697                     (or (save-excursion
698                           (gnus-topic-goto-topic (gnus-current-topic))
699                           (gnus-group-topic-level))
700                         0))
701                  ? ))
702                (yanked (list group))
703                alist talist end)
704           ;; Then we enter the yanked groups into the topics they belong
705           ;; to. 
706           (when (setq alist (assoc (save-excursion
707                                      (forward-line -1)
708                                      (or
709                                       (gnus-current-topic)
710                                       (caar gnus-topic-topology)))
711                                    gnus-topic-alist))
712             (setq talist alist)
713             (when (stringp yanked)
714               (setq yanked (list yanked)))
715             (if (not prev)
716                 (nconc alist yanked)
717               (if (not (cdr alist))
718                   (setcdr alist (nconc yanked (cdr alist)))
719                 (while (and (not end) (cdr alist))
720                   (when (equal (cadr alist) prev)
721                     (setcdr alist (nconc yanked (cdr alist)))
722                     (setq end t))
723                   (setq alist (cdr alist)))
724                 (unless end
725                   (nconc talist yanked))))))
726         (gnus-topic-update-topic)))))
727
728 (defun gnus-topic-goto-next-group (group props)
729   "Go to group or the next group after group."
730   (if (null group)
731       (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props))))
732     (if (gnus-group-goto-group group)
733         t
734       ;; The group is no longer visible.
735       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
736              (after (cdr (member group (cdr list)))))
737         ;; First try to put point on a group after the current one.
738         (while (and after
739                     (not (gnus-group-goto-group (car after))))
740           (setq after (cdr after)))
741         ;; Then try to put point on a group before point.
742         (unless after
743           (setq after (cdr (member group (reverse (cdr list)))))
744           (while (and after 
745                       (not (gnus-group-goto-group (car after))))
746             (setq after (cdr after))))
747         ;; Finally, just put point on the topic.
748         (unless after
749           (gnus-topic-goto-topic (car list))
750           (setq after nil))
751         t))))
752
753 ;;; Topic-active functions
754
755 (defun gnus-topic-grok-active (&optional force)
756   "Parse all active groups and create topic structures for them."
757   ;; First we make sure that we have really read the active file. 
758   (when (or force
759             (not gnus-topic-active-alist))
760     (let (groups)
761       ;; Get a list of all groups available.
762       (mapatoms (lambda (g) (when (symbol-value g)
763                               (push (symbol-name g) groups)))
764                 gnus-active-hashtb)
765       (setq groups (sort groups 'string<))
766       ;; Init the variables.
767       (setq gnus-topic-active-topology (list (list "" 'visible)))
768       (setq gnus-topic-active-alist nil)
769       ;; Descend the top-level hierarchy.
770       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
771       ;; Set the top-level topic names to something nice.
772       (setcar (car gnus-topic-active-topology) "Gnus active")
773       (setcar (car gnus-topic-active-alist) "Gnus active"))))
774
775 (defun gnus-topic-grok-active-1 (topology groups)
776   (let* ((name (caar topology))
777          (prefix (concat "^" (regexp-quote name)))
778          tgroups ntopology group)
779     (while (and groups
780                 (string-match prefix (setq group (car groups))))
781       (if (not (string-match "\\." group (match-end 0)))
782           ;; There are no further hierarchies here, so we just
783           ;; enter this group into the list belonging to this
784           ;; topic.
785           (push (pop groups) tgroups)
786         ;; New sub-hierarchy, so we add it to the topology.
787         (nconc topology (list (setq ntopology 
788                                     (list (list (substring 
789                                                  group 0 (match-end 0))
790                                                 'invisible)))))
791         ;; Descend the hierarchy.
792         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
793     ;; We remove the trailing "." from the topic name.
794     (setq name
795           (if (string-match "\\.$" name)
796               (substring name 0 (match-beginning 0))
797             name))
798     ;; Add this topic and its groups to the topic alist.
799     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
800     (setcar (car topology) name)
801     ;; We return the rest of the groups that didn't belong
802     ;; to this topic.
803     groups))
804
805 ;;; Topic mode, commands and keymap.
806
807 (defvar gnus-topic-mode-map nil)
808 (defvar gnus-group-topic-map nil)
809
810 (unless gnus-topic-mode-map
811   (setq gnus-topic-mode-map (make-sparse-keymap))
812
813   ;; Override certain group mode keys.
814   (gnus-define-keys gnus-topic-mode-map
815     "=" gnus-topic-select-group
816     "\r" gnus-topic-select-group
817     " " gnus-topic-read-group
818     "\C-k" gnus-topic-kill-group
819     "\C-y" gnus-topic-yank-group
820     "\M-g" gnus-topic-get-new-news-this-topic
821     "AT" gnus-topic-list-active
822     "Gp" gnus-topic-edit-parameters
823     gnus-mouse-2 gnus-mouse-pick-topic)
824
825   ;; Define a new submap.
826   (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
827     "#" gnus-topic-mark-topic
828     "\M-#" gnus-topic-unmark-topic
829     "n" gnus-topic-create-topic
830     "m" gnus-topic-move-group
831     "D" gnus-topic-remove-group
832     "c" gnus-topic-copy-group
833     "h" gnus-topic-hide-topic
834     "s" gnus-topic-show-topic
835     "M" gnus-topic-move-matching
836     "C" gnus-topic-copy-matching
837     "\C-i" gnus-topic-indent
838     [tab] gnus-topic-indent
839     "r" gnus-topic-rename
840     "\177" gnus-topic-delete)
841
842   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
843     "s" gnus-topic-sort-groups
844     "a" gnus-topic-sort-groups-by-alphabet
845     "u" gnus-topic-sort-groups-by-unread
846     "l" gnus-topic-sort-groups-by-level
847     "v" gnus-topic-sort-groups-by-score
848     "r" gnus-topic-sort-groups-by-rank
849     "m" gnus-topic-sort-groups-by-method))
850
851 (defun gnus-topic-make-menu-bar ()
852   (unless (boundp 'gnus-topic-menu)
853     (easy-menu-define
854      gnus-topic-menu gnus-topic-mode-map ""
855      '("Topics"
856        ["Toggle topics" gnus-topic-mode t]
857        ("Groups"
858         ["Copy" gnus-topic-copy-group t]
859         ["Move" gnus-topic-move-group t]
860         ["Remove" gnus-topic-remove-group t]
861         ["Copy matching" gnus-topic-copy-matching t]
862         ["Move matching" gnus-topic-move-matching t])
863        ("Topics"
864         ["Show" gnus-topic-show-topic t]
865         ["Hide" gnus-topic-hide-topic t]
866         ["Delete" gnus-topic-delete t]
867         ["Rename" gnus-topic-rename t]
868         ["Create" gnus-topic-create-topic t]
869         ["Mark" gnus-topic-mark-topic t]
870         ["Indent" gnus-topic-indent t])
871        ["List active" gnus-topic-list-active t]))))
872
873 (defun gnus-topic-mode (&optional arg redisplay)
874   "Minor mode for topicsifying Gnus group buffers."
875   (interactive (list current-prefix-arg t))
876   (when (eq major-mode 'gnus-group-mode)
877     (make-local-variable 'gnus-topic-mode)
878     (setq gnus-topic-mode 
879           (if (null arg) (not gnus-topic-mode)
880             (> (prefix-numeric-value arg) 0)))
881     ;; Infest Gnus with topics.
882     (when gnus-topic-mode
883       (when (and menu-bar-mode
884                  (gnus-visual-p 'topic-menu 'menu))
885         (gnus-topic-make-menu-bar))
886       (setq gnus-topic-line-format-spec 
887             (gnus-parse-format gnus-topic-line-format 
888                                gnus-topic-line-format-alist t))
889       (unless (assq 'gnus-topic-mode minor-mode-alist)
890         (push '(gnus-topic-mode " Topic") minor-mode-alist))
891       (unless (assq 'gnus-topic-mode minor-mode-map-alist)
892         (push (cons 'gnus-topic-mode gnus-topic-mode-map)
893               minor-mode-map-alist))
894       (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
895       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
896       (set (make-local-variable 'gnus-group-prepare-function)
897            'gnus-group-prepare-topics)
898       (set (make-local-variable 'gnus-group-get-parameter-function)
899            'gnus-group-topic-parameters)
900       (set (make-local-variable 'gnus-group-goto-next-group-function)
901            'gnus-topic-goto-next-group)
902       (set (make-local-variable 'gnus-group-indentation-function)
903            'gnus-topic-group-indentation)
904       (set (make-local-variable 'gnus-group-update-group-function)
905            'gnus-topic-update-topics-containing-group)
906       (set (make-local-variable 'gnus-group-sort-alist-function)
907            'gnus-group-sort-topic)
908       (setq gnus-group-change-level-function 'gnus-topic-change-level)
909       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
910       (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
911       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
912       (setq gnus-topology-checked-p nil)
913       ;; We check the topology.
914       (when gnus-newsrc-alist
915         (gnus-topic-check-topology))
916       (run-hooks 'gnus-topic-mode-hook))
917     ;; Remove topic infestation.
918     (unless gnus-topic-mode
919       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
920       (remove-hook 'gnus-group-change-level-function 
921                    'gnus-topic-change-level)
922       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
923       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
924       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
925     (when redisplay
926       (gnus-group-list-groups))))
927     
928 (defun gnus-topic-select-group (&optional all)
929   "Select this newsgroup.
930 No article is selected automatically.
931 If ALL is non-nil, already read articles become readable.
932 If ALL is a number, fetch this number of articles.
933
934 If performed over a topic line, toggle folding the topic."
935   (interactive "P")
936   (if (gnus-group-topic-p)
937       (let ((gnus-group-list-mode 
938              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
939         (gnus-topic-fold all))
940     (gnus-group-select-group all)))
941
942 (defun gnus-mouse-pick-topic (e)
943   "Select the group or topic under the mouse pointer."
944   (interactive "e")
945   (mouse-set-point e)
946   (gnus-topic-read-group nil))
947
948 (defun gnus-topic-read-group (&optional all no-article group)
949   "Read news in this newsgroup.
950 If the prefix argument ALL is non-nil, already read articles become
951 readable.  IF ALL is a number, fetch this number of articles.  If the
952 optional argument NO-ARTICLE is non-nil, no article will be
953 auto-selected upon group entry.  If GROUP is non-nil, fetch that
954 group.
955
956 If performed over a topic line, toggle folding the topic."
957   (interactive "P")
958   (if (gnus-group-topic-p)
959       (let ((gnus-group-list-mode 
960              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
961         (gnus-topic-fold all))
962     (gnus-group-read-group all no-article group)))
963
964 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
965   (interactive 
966    (list
967     (read-string "New topic: ")
968     (gnus-current-topic)))
969   ;; Check whether this topic already exists.
970   (when (gnus-topic-find-topology topic)
971     (error "Topic already exists"))
972   (unless parent
973     (setq parent (caar gnus-topic-topology)))
974   (let ((top (cdr (gnus-topic-find-topology parent)))
975         (full-topic (or full-topic `((,topic visible)))))
976     (unless top
977       (error "No such parent topic: %s" parent))
978     (if previous
979         (progn
980           (while (and (cdr top)
981                       (not (equal (caaadr top) previous)))
982             (setq top (cdr top)))
983           (setcdr top (cons full-topic (cdr top))))
984       (nconc top (list full-topic)))
985     (unless (assoc topic gnus-topic-alist)
986       (push (list topic) gnus-topic-alist)))
987   (gnus-topic-enter-dribble)
988   (gnus-group-list-groups)
989   (gnus-topic-goto-topic topic))
990
991 (defun gnus-topic-move-group (n topic &optional copyp)
992   "Move the next N groups to TOPIC.
993 If COPYP, copy the groups instead."
994   (interactive
995    (list current-prefix-arg
996          (completing-read "Move to topic: " gnus-topic-alist nil t)))
997   (let ((groups (gnus-group-process-prefix n))
998         (topicl (assoc topic gnus-topic-alist))
999         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1000         (start-topic (gnus-group-topic-name))
1001         entry)
1002     (mapcar 
1003      (lambda (g)
1004        (gnus-group-remove-mark g)
1005        (when (and
1006               (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1007               (not copyp))
1008          (setcdr entry (gnus-delete-first g (cdr entry))))
1009        (nconc topicl (list g)))
1010      groups)
1011     (gnus-topic-enter-dribble)
1012     (if start-group
1013         (gnus-group-goto-group start-group)
1014       (gnus-topic-goto-topic start-topic))
1015     (gnus-group-list-groups)))
1016
1017 (defun gnus-topic-remove-group (&optional arg)
1018   "Remove the current group from the topic."
1019   (interactive "P")
1020   (gnus-group-iterate arg 
1021     (lambda (group)
1022       (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1023             (buffer-read-only nil))
1024         (when (and topicl group)
1025           (gnus-delete-line)
1026           (gnus-delete-first group topicl))
1027         (gnus-topic-update-topic)
1028         (gnus-group-position-point)))))
1029
1030 (defun gnus-topic-copy-group (n topic)
1031   "Copy the current group to a topic."
1032   (interactive
1033    (list current-prefix-arg
1034          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1035   (gnus-topic-move-group n topic t))
1036
1037 (defun gnus-topic-kill-group (&optional n discard)
1038   "Kill the next N groups."
1039   (interactive "P")
1040   (if (gnus-group-topic-p)
1041       (let ((topic (gnus-group-topic-name)))
1042         (gnus-topic-remove-topic nil t)
1043         (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1044               gnus-topic-killed-topics)
1045         (gnus-topic-enter-dribble))
1046     (gnus-group-kill-group n discard)
1047     (gnus-topic-update-topic)))
1048   
1049 (defun gnus-topic-yank-group (&optional arg)
1050   "Yank the last topic."
1051   (interactive "p")
1052   (if gnus-topic-killed-topics
1053       (let ((previous 
1054              (or (gnus-group-topic-name)
1055                  (gnus-topic-next-topic (gnus-current-topic))))
1056             (item (cdr (pop gnus-topic-killed-topics))))
1057         (gnus-topic-create-topic
1058          (caar item) (gnus-topic-parent-topic previous) previous
1059          item)
1060         (gnus-topic-enter-dribble)
1061         (gnus-topic-goto-topic (caar item)))
1062     (let* ((prev (gnus-group-group-name))
1063            (gnus-topic-inhibit-change-level t)
1064            (gnus-group-indentation
1065             (make-string 
1066              (* gnus-topic-indent-level
1067                 (or (save-excursion
1068                       (gnus-topic-goto-topic (gnus-current-topic))
1069                       (gnus-group-topic-level))
1070                     0))
1071              ? ))
1072            yanked alist)
1073       ;; We first yank the groups the normal way...
1074       (setq yanked (gnus-group-yank-group arg))
1075       ;; Then we enter the yanked groups into the topics they belong
1076       ;; to. 
1077       (setq alist (assoc (save-excursion
1078                            (forward-line -1)
1079                            (gnus-current-topic))
1080                          gnus-topic-alist))
1081       (when (stringp yanked)
1082         (setq yanked (list yanked)))
1083       (if (not prev)
1084           (nconc alist yanked)
1085         (if (not (cdr alist))
1086             (setcdr alist (nconc yanked (cdr alist)))
1087           (while (cdr alist)
1088             (when (equal (cadr alist) prev)
1089               (setcdr alist (nconc yanked (cdr alist)))
1090               (setq alist nil))
1091             (setq alist (cdr alist))))))
1092     (gnus-topic-update-topic)))
1093
1094 (defun gnus-topic-hide-topic ()
1095   "Hide the current topic."
1096   (interactive)
1097   (when (gnus-current-topic)
1098     (gnus-topic-goto-topic (gnus-current-topic))
1099     (gnus-topic-remove-topic nil nil 'hidden)))
1100
1101 (defun gnus-topic-show-topic ()
1102   "Show the hidden topic."
1103   (interactive)
1104   (when (gnus-group-topic-p)
1105     (gnus-topic-remove-topic t nil 'shown)))
1106
1107 (defun gnus-topic-mark-topic (topic &optional unmark)
1108   "Mark all groups in the topic with the process mark."
1109   (interactive (list (gnus-current-topic)))
1110   (save-excursion
1111     (let ((groups (gnus-topic-find-groups topic 9 t)))
1112       (while groups
1113         (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1114                  (gnus-info-group (nth 2 (pop groups))))))))
1115
1116 (defun gnus-topic-unmark-topic (topic &optional unmark)
1117   "Remove the process mark from all groups in the topic."
1118   (interactive (list (gnus-current-topic)))
1119   (gnus-topic-mark-topic topic t))
1120
1121 (defun gnus-topic-get-new-news-this-topic (&optional n)
1122   "Check for new news in the current topic."
1123   (interactive "P")
1124   (if (not (gnus-group-topic-p))
1125       (gnus-group-get-new-news-this-group n)
1126     (gnus-topic-mark-topic (gnus-group-topic-name))
1127     (gnus-group-get-new-news-this-group)))
1128
1129 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1130   "Move all groups that match REGEXP to some topic."
1131   (interactive
1132    (let (topic)
1133      (nreverse
1134       (list
1135        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1136        (read-string (format "Move to %s (regexp): " topic))))))
1137   (gnus-group-mark-regexp regexp)
1138   (gnus-topic-move-group nil topic copyp))
1139
1140 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1141   "Copy all groups that match REGEXP to some topic."
1142   (interactive
1143    (let (topic)
1144      (nreverse
1145       (list
1146        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1147        (read-string (format "Copy to %s (regexp): " topic))))))
1148   (gnus-topic-move-matching regexp topic t))
1149
1150 (defun gnus-topic-delete (topic)
1151   "Delete a topic."
1152   (interactive (list (gnus-group-topic-name)))
1153   (unless topic
1154     (error "No topic to be deleted"))
1155   (let ((entry (assoc topic gnus-topic-alist))
1156         (buffer-read-only nil))
1157     (when (cdr entry)
1158       (error "Topic not empty"))
1159     ;; Delete if visible.
1160     (when (gnus-topic-goto-topic topic)
1161       (gnus-delete-line))
1162     ;; Remove from alist.
1163     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1164     ;; Remove from topology.
1165     (gnus-topic-find-topology topic nil nil 'delete)))
1166
1167 (defun gnus-topic-rename (old-name new-name)
1168   "Rename a topic."
1169   (interactive
1170    (let ((topic (gnus-current-topic)))
1171      (list topic
1172            (read-string (format "Rename %s to: " topic)))))
1173   (let ((top (gnus-topic-find-topology old-name))
1174         (entry (assoc old-name gnus-topic-alist)))
1175     (when top
1176       (setcar (cadr top) new-name))
1177     (when entry 
1178       (setcar entry new-name))
1179     (forward-line -1)
1180     (gnus-group-list-groups)))
1181
1182 (defun gnus-topic-indent (&optional unindent)
1183   "Indent a topic -- make it a sub-topic of the previous topic.
1184 If UNINDENT, remove an indentation."
1185   (interactive "P")
1186   (if unindent
1187       (gnus-topic-unindent)
1188     (let* ((topic (gnus-current-topic))
1189            (parent (gnus-topic-previous-topic topic)))
1190       (unless parent
1191         (error "Nothing to indent %s into" topic))
1192       (when topic
1193         (gnus-topic-goto-topic topic)
1194         (gnus-topic-kill-group)
1195         (gnus-topic-create-topic
1196          topic parent nil (cdr (pop gnus-topic-killed-topics)))
1197         (or (gnus-topic-goto-topic topic)
1198             (gnus-topic-goto-topic parent))))))
1199
1200 (defun gnus-topic-unindent ()
1201   "Unindent a topic."
1202   (interactive)
1203   (let* ((topic (gnus-current-topic))
1204          (parent (gnus-topic-parent-topic topic))
1205          (grandparent (gnus-topic-parent-topic parent)))
1206     (unless grandparent
1207       (error "Nothing to indent %s into" topic))
1208     (when topic
1209       (gnus-topic-goto-topic topic)
1210       (gnus-topic-kill-group)
1211       (gnus-topic-create-topic
1212        topic grandparent (gnus-topic-next-topic parent)
1213        (cdr (pop gnus-topic-killed-topics)))
1214       (gnus-topic-goto-topic topic))))
1215
1216 (defun gnus-topic-list-active (&optional force)
1217   "List all groups that Gnus knows about in a topicsified fashion.
1218 If FORCE, always re-read the active file."
1219   (interactive "P")
1220   (when force
1221     (gnus-get-killed-groups))
1222   (gnus-topic-grok-active force)
1223   (let ((gnus-topic-topology gnus-topic-active-topology)
1224         (gnus-topic-alist gnus-topic-active-alist)
1225         gnus-killed-list gnus-zombie-list)
1226     (gnus-group-list-groups 9 nil 1)))
1227
1228 ;;; Topic sorting functions
1229
1230 (defun gnus-topic-edit-parameters (group)
1231   "Edit the group parameters of GROUP.
1232 If performed on a topic, edit the topic parameters instead."
1233   (interactive (list (gnus-group-group-name)))
1234   (if group
1235       (gnus-group-edit-group-parameters group)
1236     (if (not (gnus-group-topic-p))
1237         (error "Nothing to edit on the current line.")
1238       (let ((topic (gnus-group-topic-name)))
1239         (gnus-edit-form
1240          (gnus-topic-parameters topic)
1241          "Editing the topic parameters."
1242          `(lambda (form)
1243             (gnus-topic-set-parameters ,topic form)))))))
1244
1245 (defun gnus-group-sort-topic (func reverse)
1246   "Sort groups in the topics according to FUNC and REVERSE."
1247   (let ((alist gnus-topic-alist))
1248     (while alist
1249       (gnus-topic-sort-topic (pop alist) func reverse))))
1250
1251 (defun gnus-topic-sort-topic (topic func reverse)
1252   ;; Each topic only lists the name of the group, while
1253   ;; the sort predicates expect group infos as inputs.
1254   ;; So we first transform the group names into infos,
1255   ;; then sort, and then transform back into group names.
1256   (setcdr
1257    topic
1258    (mapcar
1259     (lambda (info) (gnus-info-group info))
1260     (sort
1261      (mapcar
1262       (lambda (group) (gnus-get-info group))
1263       (cdr topic))
1264      func)))
1265   ;; Do the reversal, if necessary.
1266   (when reverse
1267     (setcdr topic (nreverse (cdr topic)))))
1268
1269 (defun gnus-topic-sort-groups (func &optional reverse)
1270   "Sort the current topic according to FUNC.
1271 If REVERSE, reverse the sorting order."
1272   (interactive (list gnus-group-sort-function current-prefix-arg))
1273   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1274     (gnus-topic-sort-topic
1275      topic (gnus-make-sort-function func) reverse)
1276     (gnus-group-list-groups)))
1277
1278 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1279   "Sort the current topic alphabetically by group name.
1280 If REVERSE, sort in reverse order."
1281   (interactive "P")
1282   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1283
1284 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1285   "Sort the current topic by number of unread articles.
1286 If REVERSE, sort in reverse order."
1287   (interactive "P")
1288   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1289
1290 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1291   "Sort the current topic by group level.
1292 If REVERSE, sort in reverse order."
1293   (interactive "P")
1294   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1295
1296 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1297   "Sort the current topic by group score.
1298 If REVERSE, sort in reverse order."
1299   (interactive "P")
1300   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1301
1302 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1303   "Sort the current topic by group rank.
1304 If REVERSE, sort in reverse order."
1305   (interactive "P")
1306   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1307
1308 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1309   "Sort the current topic alphabetically by backend name.
1310 If REVERSE, sort in reverse order."
1311   (interactive "P")
1312   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1313
1314 (provide 'gnus-topic)
1315
1316 ;;; gnus-topic.el ends here