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