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