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