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