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