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