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