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