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