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