*** 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       (if (not top)
622           (gnus-topic-insert-topic-line
623            topic t t (car (gnus-topic-find-topology topic)) nil 0)
624         (while (not (equal (caaar tp) topic))
625           (setq tp (cdr tp)))
626         (pop tp)
627         (while (and tp
628                     (not (gnus-topic-goto-topic (caaar tp))))
629           (pop tp))
630         (if tp
631             (gnus-topic-forward-topic 1)
632           (gnus-topic-goto-missing-topic (caadr top)))))
633     nil))
634
635 (defun gnus-topic-update-topic-line (topic-name &optional reads)
636   (let* ((top (gnus-topic-find-topology topic-name))
637          (type (cadr top))
638          (children (cddr top))
639          (entries (gnus-topic-find-groups
640                    (car type) (car gnus-group-list-mode)
641                    (cdr gnus-group-list-mode)))
642          (parent (gnus-topic-parent-topic topic-name))
643          (all-entries entries)
644          (unread 0)
645          old-unread entry new-unread)
646     (when (gnus-topic-goto-topic (car type))
647       ;; Tally all the groups that belong in this topic.
648       (if reads
649           (setq unread (- (gnus-group-topic-unread) reads))
650         (while children
651           (incf unread (gnus-topic-unread (caar (pop children)))))
652         (while (setq entry (pop entries))
653           (when (numberp (car entry))
654             (incf unread (car entry)))))
655       (setq old-unread (gnus-group-topic-unread))
656       ;; Insert the topic line.
657       (gnus-topic-insert-topic-line
658        (car type) (gnus-topic-visible-p)
659        (not (eq (nth 2 type) 'hidden))
660        (gnus-group-topic-level) all-entries unread)
661       (gnus-delete-line)
662       (forward-line -1)
663       (setq new-unread (gnus-group-topic-unread)))
664     (when parent
665       (forward-line -1)
666       (gnus-topic-update-topic-line
667        parent
668        (- (or old-unread 0) (or new-unread 0))))
669     unread))
670
671 (defun gnus-topic-group-indentation ()
672   (make-string
673    (* gnus-topic-indent-level
674       (or (save-excursion
675             (forward-line -1)
676             (gnus-topic-goto-topic (gnus-current-topic))
677             (gnus-group-topic-level))
678           0))
679    ? ))
680
681 ;;; Initialization
682
683 (gnus-add-shutdown 'gnus-topic-close 'gnus)
684
685 (defun gnus-topic-close ()
686   (setq gnus-topic-active-topology nil
687         gnus-topic-active-alist nil
688         gnus-topic-killed-topics nil
689         gnus-topology-checked-p nil))
690
691 (defun gnus-topic-check-topology ()
692   ;; The first time we set the topology to whatever we have
693   ;; gotten here, which can be rather random.
694   (unless gnus-topic-alist
695     (gnus-topic-init-alist))
696
697   (setq gnus-topology-checked-p t)
698   ;; Go through the topic alist and make sure that all topics
699   ;; are in the topic topology.
700   (let ((topics (gnus-topic-list))
701         (alist gnus-topic-alist)
702         changed)
703     (while alist
704       (unless (member (caar alist) topics)
705         (nconc gnus-topic-topology
706                (list (list (list (caar alist) 'visible))))
707         (setq changed t))
708       (setq alist (cdr alist)))
709     (when changed
710       (gnus-topic-enter-dribble))
711     ;; Conversely, go through the topology and make sure that all
712     ;; topologies have alists.
713     (while topics
714       (unless (assoc (car topics) gnus-topic-alist)
715         (push (list (car topics)) gnus-topic-alist))
716       (pop topics)))
717   ;; Go through all living groups and make sure that
718   ;; they belong to some topic.
719   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
720                                          gnus-topic-alist)))
721          (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
722          (newsrc (cdr gnus-newsrc-alist))
723          group)
724     (while newsrc
725       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
726         (setcdr entry (list group))
727         (setq entry (cdr entry)))))
728   ;; Go through all topics and make sure they contain only living groups.
729   (let ((alist gnus-topic-alist)
730         topic)
731     (while (setq topic (pop alist))
732       (while (cdr topic)
733         (if (and (cadr topic)
734                  (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
735             (setq topic (cdr topic))
736           (setcdr topic (cddr topic)))))))
737
738 (defun gnus-topic-init-alist ()
739   "Initialize the topic structures."
740   (setq gnus-topic-topology
741         (cons (list "Gnus" 'visible)
742               (mapcar (lambda (topic)
743                         (list (list (car topic) 'visible)))
744                       '(("misc")))))
745   (setq gnus-topic-alist
746         (list (cons "misc"
747                     (mapcar (lambda (info) (gnus-info-group info))
748                             (cdr gnus-newsrc-alist)))
749               (list "Gnus")))
750   (gnus-topic-enter-dribble))
751
752 ;;; Maintenance
753
754 (defun gnus-topic-clean-alist ()
755   "Remove bogus groups from the topic alist."
756   (let ((topic-alist gnus-topic-alist)
757         result topic)
758     (unless gnus-killed-hashtb
759       (gnus-make-hashtable-from-killed))
760     (while (setq topic (pop topic-alist))
761       (let ((topic-name (pop topic))
762             group filtered-topic)
763         (while (setq group (pop topic))
764           (when (and (or (gnus-gethash group gnus-active-hashtb)
765                          (gnus-info-method (gnus-get-info group)))
766                      (not (gnus-gethash group gnus-killed-hashtb)))
767             (push group filtered-topic)))
768         (push (cons topic-name (nreverse filtered-topic)) result)))
769     (setq gnus-topic-alist (nreverse result))))
770
771 (defun gnus-topic-change-level (group level oldlevel &optional previous)
772   "Run when changing levels to enter/remove groups from topics."
773   (save-excursion
774     (set-buffer gnus-group-buffer)
775     (let ((buffer-read-only nil))
776       (unless gnus-topic-inhibit-change-level
777         (gnus-group-goto-group (or (car (nth 2 previous)) group))
778         (when (and gnus-topic-mode
779                    gnus-topic-alist
780                    (not gnus-topic-inhibit-change-level))
781           ;; Remove the group from the topics.
782           (if (and (< oldlevel gnus-level-zombie)
783                    (>= level gnus-level-zombie))
784               (let ((alist gnus-topic-alist))
785                 (while (gnus-group-goto-group group)
786                   (gnus-delete-line))
787                 (while alist
788                   (when (member group (car alist))
789                     (setcdr (car alist) (delete group (cdar alist))))
790                   (pop alist)))
791             ;; If the group is subscribed we enter it into the topics.
792             (when (and (< level gnus-level-zombie)
793                        (>= oldlevel gnus-level-zombie))
794               (let* ((prev (gnus-group-group-name))
795                      (gnus-topic-inhibit-change-level t)
796                      (gnus-group-indentation
797                       (make-string
798                        (* gnus-topic-indent-level
799                           (or (save-excursion
800                                 (gnus-topic-goto-topic (gnus-current-topic))
801                                 (gnus-group-topic-level))
802                               0))
803                        ? ))
804                      (yanked (list group))
805                      alist talist end)
806                 ;; Then we enter the yanked groups into the topics they belong
807                 ;; to.
808                 (when (setq alist (assoc (save-excursion
809                                            (forward-line -1)
810                                            (or
811                                             (gnus-current-topic)
812                                             (caar gnus-topic-topology)))
813                                          gnus-topic-alist))
814                   (setq talist alist)
815                   (when (stringp yanked)
816                     (setq yanked (list yanked)))
817                   (if (not prev)
818                       (nconc alist yanked)
819                     (if (not (cdr alist))
820                         (setcdr alist (nconc yanked (cdr alist)))
821                       (while (and (not end) (cdr alist))
822                         (when (equal (cadr alist) prev)
823                           (setcdr alist (nconc yanked (cdr alist)))
824                           (setq end t))
825                         (setq alist (cdr alist)))
826                       (unless end
827                         (nconc talist yanked))))))
828               (gnus-topic-update-topic))))))))
829
830 (defun gnus-topic-goto-next-group (group props)
831   "Go to group or the next group after group."
832   (if (not group)
833       (if (not (memq 'gnus-topic props))
834           (goto-char (point-max))
835         (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
836     (if (gnus-group-goto-group group)
837         t
838       ;; The group is no longer visible.
839       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
840              (after (cdr (member group (cdr list)))))
841         ;; First try to put point on a group after the current one.
842         (while (and after
843                     (not (gnus-group-goto-group (car after))))
844           (setq after (cdr after)))
845         ;; Then try to put point on a group before point.
846         (unless after
847           (setq after (cdr (member group (reverse (cdr list)))))
848           (while (and after
849                       (not (gnus-group-goto-group (car after))))
850             (setq after (cdr after))))
851         ;; Finally, just put point on the topic.
852         (if (not (car list))
853             (goto-char (point-min))
854           (unless after
855             (gnus-topic-goto-topic (car list))
856             (setq after nil)))
857         t))))
858
859 ;;; Topic-active functions
860
861 (defun gnus-topic-grok-active (&optional force)
862   "Parse all active groups and create topic structures for them."
863   ;; First we make sure that we have really read the active file.
864   (when (or force
865             (not gnus-topic-active-alist))
866     (let (groups)
867       ;; Get a list of all groups available.
868       (mapatoms (lambda (g) (when (symbol-value g)
869                               (push (symbol-name g) groups)))
870                 gnus-active-hashtb)
871       (setq groups (sort groups 'string<))
872       ;; Init the variables.
873       (setq gnus-topic-active-topology (list (list "" 'visible)))
874       (setq gnus-topic-active-alist nil)
875       ;; Descend the top-level hierarchy.
876       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
877       ;; Set the top-level topic names to something nice.
878       (setcar (car gnus-topic-active-topology) "Gnus active")
879       (setcar (car gnus-topic-active-alist) "Gnus active"))))
880
881 (defun gnus-topic-grok-active-1 (topology groups)
882   (let* ((name (caar topology))
883          (prefix (concat "^" (regexp-quote name)))
884          tgroups ntopology group)
885     (while (and groups
886                 (string-match prefix (setq group (car groups))))
887       (if (not (string-match "\\." group (match-end 0)))
888           ;; There are no further hierarchies here, so we just
889           ;; enter this group into the list belonging to this
890           ;; topic.
891           (push (pop groups) tgroups)
892         ;; New sub-hierarchy, so we add it to the topology.
893         (nconc topology (list (setq ntopology
894                                     (list (list (substring
895                                                  group 0 (match-end 0))
896                                                 'invisible)))))
897         ;; Descend the hierarchy.
898         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
899     ;; We remove the trailing "." from the topic name.
900     (setq name
901           (if (string-match "\\.$" name)
902               (substring name 0 (match-beginning 0))
903             name))
904     ;; Add this topic and its groups to the topic alist.
905     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
906     (setcar (car topology) name)
907     ;; We return the rest of the groups that didn't belong
908     ;; to this topic.
909     groups))
910
911 ;;; Topic mode, commands and keymap.
912
913 (defvar gnus-topic-mode-map nil)
914 (defvar gnus-group-topic-map nil)
915
916 (unless gnus-topic-mode-map
917   (setq gnus-topic-mode-map (make-sparse-keymap))
918
919   ;; Override certain group mode keys.
920   (gnus-define-keys gnus-topic-mode-map
921     "=" gnus-topic-select-group
922     "\r" gnus-topic-select-group
923     " " gnus-topic-read-group
924     "\C-c\C-x" gnus-topic-expire-articles
925     "\C-k" gnus-topic-kill-group
926     "\C-y" gnus-topic-yank-group
927     "\M-g" gnus-topic-get-new-news-this-topic
928     "AT" gnus-topic-list-active
929     "Gp" gnus-topic-edit-parameters
930     "#" gnus-topic-mark-topic
931     "\M-#" gnus-topic-unmark-topic
932     [tab] gnus-topic-indent
933     [(meta tab)] gnus-topic-unindent
934     "\C-i" gnus-topic-indent
935     "\M-\C-i" gnus-topic-unindent
936     gnus-mouse-2 gnus-mouse-pick-topic)
937
938   ;; Define a new submap.
939   (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
940     "#" gnus-topic-mark-topic
941     "\M-#" gnus-topic-unmark-topic
942     "n" gnus-topic-create-topic
943     "m" gnus-topic-move-group
944     "D" gnus-topic-remove-group
945     "c" gnus-topic-copy-group
946     "h" gnus-topic-hide-topic
947     "s" gnus-topic-show-topic
948     "j" gnus-topic-jump-to-topic
949     "M" gnus-topic-move-matching
950     "C" gnus-topic-copy-matching
951     "\C-i" gnus-topic-indent
952     [tab] gnus-topic-indent
953     "r" gnus-topic-rename
954     "\177" gnus-topic-delete
955     [delete] gnus-topic-delete
956     "H" gnus-topic-toggle-display-empty-topics)
957
958   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
959     "s" gnus-topic-sort-groups
960     "a" gnus-topic-sort-groups-by-alphabet
961     "u" gnus-topic-sort-groups-by-unread
962     "l" gnus-topic-sort-groups-by-level
963     "v" gnus-topic-sort-groups-by-score
964     "r" gnus-topic-sort-groups-by-rank
965     "m" gnus-topic-sort-groups-by-method))
966
967 (defun gnus-topic-make-menu-bar ()
968   (unless (boundp 'gnus-topic-menu)
969     (easy-menu-define
970      gnus-topic-menu gnus-topic-mode-map ""
971      '("Topics"
972        ["Toggle topics" gnus-topic-mode t]
973        ("Groups"
974         ["Copy" gnus-topic-copy-group t]
975         ["Move" gnus-topic-move-group t]
976         ["Remove" gnus-topic-remove-group t]
977         ["Copy matching" gnus-topic-copy-matching t]
978         ["Move matching" gnus-topic-move-matching t])
979        ("Topics"
980         ["Goto" gnus-topic-jump-to-topic t]
981         ["Show" gnus-topic-show-topic t]
982         ["Hide" gnus-topic-hide-topic t]
983         ["Delete" gnus-topic-delete t]
984         ["Rename" gnus-topic-rename t]
985         ["Create" gnus-topic-create-topic t]
986         ["Mark" gnus-topic-mark-topic t]
987         ["Indent" gnus-topic-indent t]
988         ["Sort" gnus-topic-sort-topics t]
989         ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
990         ["Edit parameters" gnus-topic-edit-parameters t])
991        ["List active" gnus-topic-list-active t]))))
992
993 (defun gnus-topic-mode (&optional arg redisplay)
994   "Minor mode for topicsifying Gnus group buffers."
995   (interactive (list current-prefix-arg t))
996   (when (eq major-mode 'gnus-group-mode)
997     (make-local-variable 'gnus-topic-mode)
998     (setq gnus-topic-mode
999           (if (null arg) (not gnus-topic-mode)
1000             (> (prefix-numeric-value arg) 0)))
1001     ;; Infest Gnus with topics.
1002      (if (not gnus-topic-mode)
1003         (setq gnus-goto-missing-group-function nil)
1004       (when (gnus-visual-p 'topic-menu 'menu)
1005         (gnus-topic-make-menu-bar))
1006       (gnus-set-format 'topic t)
1007       (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
1008       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1009       (set (make-local-variable 'gnus-group-prepare-function)
1010            'gnus-group-prepare-topics)
1011       (set (make-local-variable 'gnus-group-get-parameter-function)
1012            'gnus-group-topic-parameters)
1013       (set (make-local-variable 'gnus-group-goto-next-group-function)
1014            'gnus-topic-goto-next-group)
1015       (set (make-local-variable 'gnus-group-indentation-function)
1016            'gnus-topic-group-indentation)
1017       (set (make-local-variable 'gnus-group-update-group-function)
1018            'gnus-topic-update-topics-containing-group)
1019       (set (make-local-variable 'gnus-group-sort-alist-function)
1020            'gnus-group-sort-topic)
1021       (setq gnus-group-change-level-function 'gnus-topic-change-level)
1022       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1023       (make-local-hook 'gnus-check-bogus-groups-hook)
1024       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1025       (setq gnus-topology-checked-p nil)
1026       ;; We check the topology.
1027       (when gnus-newsrc-alist
1028         (gnus-topic-check-topology))
1029       (gnus-run-hooks 'gnus-topic-mode-hook))
1030     ;; Remove topic infestation.
1031     (unless gnus-topic-mode
1032       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1033       (remove-hook 'gnus-group-change-level-function
1034                    'gnus-topic-change-level)
1035       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1036       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1037       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1038     (when redisplay
1039       (gnus-group-list-groups))))
1040
1041 (defun gnus-topic-select-group (&optional all)
1042   "Select this newsgroup.
1043 No article is selected automatically.
1044 If ALL is non-nil, already read articles become readable.
1045 If ALL is a number, fetch this number of articles.
1046
1047 If performed over a topic line, toggle folding the topic."
1048   (interactive "P")
1049   (if (gnus-group-topic-p)
1050       (let ((gnus-group-list-mode
1051              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1052         (gnus-topic-fold all))
1053     (gnus-group-select-group all)))
1054
1055 (defun gnus-mouse-pick-topic (e)
1056   "Select the group or topic under the mouse pointer."
1057   (interactive "e")
1058   (mouse-set-point e)
1059   (gnus-topic-read-group nil))
1060
1061 (defun gnus-topic-expire-articles (topic)
1062   "Expire articles in this topic or group."
1063   (interactive (list (gnus-group-topic-name)))
1064   (if (not topic)
1065       (call-interactively 'gnus-group-expire-articles)
1066     (save-excursion
1067       (gnus-message 5 "Expiring groups in %s..." topic)
1068       (let ((gnus-group-marked
1069             (mapcar (lambda (entry) (car (nth 2 entry)))
1070                     (gnus-topic-find-groups topic gnus-level-killed t))))
1071        (gnus-group-expire-articles nil))
1072       (gnus-message 5 "Expiring groups in %s...done" topic))))
1073
1074 (defun gnus-topic-read-group (&optional all no-article group)
1075   "Read news in this newsgroup.
1076 If the prefix argument ALL is non-nil, already read articles become
1077 readable.  IF ALL is a number, fetch this number of articles.  If the
1078 optional argument NO-ARTICLE is non-nil, no article will be
1079 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1080 group.
1081
1082 If performed over a topic line, toggle folding the topic."
1083   (interactive "P")
1084   (if (gnus-group-topic-p)
1085       (let ((gnus-group-list-mode
1086              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1087         (gnus-topic-fold all))
1088     (gnus-group-read-group all no-article group)))
1089
1090 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1091   "Create a new TOPIC under PARENT.
1092 When used interactively, PARENT will be the topic under point."
1093   (interactive
1094    (list
1095     (read-string "New topic: ")
1096     (gnus-current-topic)))
1097   ;; Check whether this topic already exists.
1098   (when (gnus-topic-find-topology topic)
1099     (error "Topic already exists"))
1100   (unless parent
1101     (setq parent (caar gnus-topic-topology)))
1102   (let ((top (cdr (gnus-topic-find-topology parent)))
1103         (full-topic (or full-topic `((,topic visible)))))
1104     (unless top
1105       (error "No such parent topic: %s" parent))
1106     (if previous
1107         (progn
1108           (while (and (cdr top)
1109                       (not (equal (caaadr top) previous)))
1110             (setq top (cdr top)))
1111           (setcdr top (cons full-topic (cdr top))))
1112       (nconc top (list full-topic)))
1113     (unless (assoc topic gnus-topic-alist)
1114       (push (list topic) gnus-topic-alist)))
1115   (gnus-topic-enter-dribble)
1116   (gnus-group-list-groups)
1117   (gnus-topic-goto-topic topic))
1118
1119 (defun gnus-topic-move-group (n topic &optional copyp)
1120   "Move the next N groups to TOPIC.
1121 If COPYP, copy the groups instead."
1122   (interactive
1123    (list current-prefix-arg
1124          (completing-read "Move to topic: " gnus-topic-alist nil t)))
1125   (let ((groups (gnus-group-process-prefix n))
1126         (topicl (assoc topic gnus-topic-alist))
1127         (start-topic (gnus-group-topic-name))
1128         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1129         entry)
1130     (if (and (not groups) (not copyp) start-topic)
1131         (gnus-topic-move start-topic topic)
1132       (mapcar
1133        (lambda (g)
1134          (gnus-group-remove-mark g)
1135          (when (and
1136                 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1137                 (not copyp))
1138            (setcdr entry (gnus-delete-first g (cdr entry))))
1139          (nconc topicl (list g)))
1140        groups)
1141       (gnus-topic-enter-dribble)
1142       (if start-group
1143           (gnus-group-goto-group start-group)
1144         (gnus-topic-goto-topic start-topic))
1145       (gnus-group-list-groups))))
1146
1147 (defun gnus-topic-remove-group (&optional arg)
1148   "Remove the current group from the topic."
1149   (interactive "P")
1150   (gnus-group-iterate arg
1151     (lambda (group)
1152       (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1153             (buffer-read-only nil))
1154         (when (and topicl group)
1155           (gnus-delete-line)
1156           (gnus-delete-first group topicl))
1157         (gnus-topic-update-topic)
1158         (gnus-group-position-point)))))
1159
1160 (defun gnus-topic-copy-group (n topic)
1161   "Copy the current group to a topic."
1162   (interactive
1163    (list current-prefix-arg
1164          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1165   (gnus-topic-move-group n topic t))
1166
1167 (defun gnus-topic-kill-group (&optional n discard)
1168   "Kill the next N groups."
1169   (interactive "P")
1170   (if (gnus-group-topic-p)
1171       (let ((topic (gnus-group-topic-name)))
1172         (push (cons
1173                (gnus-topic-find-topology topic)
1174                (assoc topic gnus-topic-alist))
1175               gnus-topic-killed-topics)
1176         (gnus-topic-remove-topic nil t)
1177         (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1178         (gnus-topic-enter-dribble))
1179     (gnus-group-kill-group n discard)
1180     (gnus-topic-update-topic)))
1181
1182 (defun gnus-topic-yank-group (&optional arg)
1183   "Yank the last topic."
1184   (interactive "p")
1185   (if gnus-topic-killed-topics
1186       (let* ((previous
1187               (or (gnus-group-topic-name)
1188                   (gnus-topic-next-topic (gnus-current-topic))))
1189              (data (pop gnus-topic-killed-topics))
1190              (alist (cdr data))
1191              (item (cdar data)))
1192         (push alist gnus-topic-alist)
1193         (gnus-topic-create-topic
1194          (caar item) (gnus-topic-parent-topic previous) previous
1195          item)
1196         (gnus-topic-enter-dribble)
1197         (gnus-topic-goto-topic (caar item)))
1198     (let* ((prev (gnus-group-group-name))
1199            (gnus-topic-inhibit-change-level t)
1200            (gnus-group-indentation
1201             (make-string
1202              (* gnus-topic-indent-level
1203                 (or (save-excursion
1204                       (gnus-topic-goto-topic (gnus-current-topic))
1205                       (gnus-group-topic-level))
1206                     0))
1207              ? ))
1208            yanked alist)
1209       ;; We first yank the groups the normal way...
1210       (setq yanked (gnus-group-yank-group arg))
1211       ;; Then we enter the yanked groups into the topics they belong
1212       ;; to.
1213       (setq alist (assoc (save-excursion
1214                            (forward-line -1)
1215                            (gnus-current-topic))
1216                          gnus-topic-alist))
1217       (when (stringp yanked)
1218         (setq yanked (list yanked)))
1219       (if (not prev)
1220           (nconc alist yanked)
1221         (if (not (cdr alist))
1222             (setcdr alist (nconc yanked (cdr alist)))
1223           (while (cdr alist)
1224             (when (equal (cadr alist) prev)
1225               (setcdr alist (nconc yanked (cdr alist)))
1226               (setq alist nil))
1227             (setq alist (cdr alist))))))
1228     (gnus-topic-update-topic)))
1229
1230 (defun gnus-topic-hide-topic ()
1231   "Hide the current topic."
1232   (interactive)
1233   (when (gnus-current-topic)
1234     (gnus-topic-goto-topic (gnus-current-topic))
1235     (gnus-topic-remove-topic nil nil 'hidden)))
1236
1237 (defun gnus-topic-show-topic ()
1238   "Show the hidden topic."
1239   (interactive)
1240   (when (gnus-group-topic-p)
1241     (gnus-topic-remove-topic t nil 'shown)))
1242
1243 (defun gnus-topic-mark-topic (topic &optional unmark)
1244   "Mark all groups in the topic with the process mark."
1245   (interactive (list (gnus-group-topic-name)))
1246   (if (not topic)
1247       (call-interactively 'gnus-group-mark-group)
1248     (save-excursion
1249       (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
1250         (while groups
1251           (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1252                    (gnus-info-group (nth 2 (pop groups)))))))))
1253
1254 (defun gnus-topic-unmark-topic (topic &optional unmark)
1255   "Remove the process mark from all groups in the topic."
1256   (interactive (list (gnus-group-topic-name)))
1257   (if (not topic)
1258       (call-interactively 'gnus-group-unmark-group)
1259     (gnus-topic-mark-topic topic t)))
1260
1261 (defun gnus-topic-get-new-news-this-topic (&optional n)
1262   "Check for new news in the current topic."
1263   (interactive "P")
1264   (if (not (gnus-group-topic-p))
1265       (gnus-group-get-new-news-this-group n)
1266     (gnus-topic-mark-topic (gnus-group-topic-name))
1267     (gnus-group-get-new-news-this-group)))
1268
1269 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1270   "Move all groups that match REGEXP to some topic."
1271   (interactive
1272    (let (topic)
1273      (nreverse
1274       (list
1275        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1276        (read-string (format "Move to %s (regexp): " topic))))))
1277   (gnus-group-mark-regexp regexp)
1278   (gnus-topic-move-group nil topic copyp))
1279
1280 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1281   "Copy all groups that match REGEXP to some topic."
1282   (interactive
1283    (let (topic)
1284      (nreverse
1285       (list
1286        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1287        (read-string (format "Copy to %s (regexp): " topic))))))
1288   (gnus-topic-move-matching regexp topic t))
1289
1290 (defun gnus-topic-delete (topic)
1291   "Delete a topic."
1292   (interactive (list (gnus-group-topic-name)))
1293   (unless topic
1294     (error "No topic to be deleted"))
1295   (let ((entry (assoc topic gnus-topic-alist))
1296         (buffer-read-only nil))
1297     (when (cdr entry)
1298       (error "Topic not empty"))
1299     ;; Delete if visible.
1300     (when (gnus-topic-goto-topic topic)
1301       (gnus-delete-line))
1302     ;; Remove from alist.
1303     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1304     ;; Remove from topology.
1305     (gnus-topic-find-topology topic nil nil 'delete)
1306     (gnus-dribble-touch)))
1307
1308 (defun gnus-topic-rename (old-name new-name)
1309   "Rename a topic."
1310   (interactive
1311    (let ((topic (gnus-current-topic)))
1312      (list topic
1313            (read-string (format "Rename %s to: " topic)))))
1314   ;; Check whether the new name exists.
1315   (when (gnus-topic-find-topology new-name)
1316     (error "Topic '%s' already exists" new-name))
1317   ;; "nil" is an invalid name, for reasons I'd rather not go
1318   ;; into here.  Trust me.
1319   (when (equal new-name "nil")
1320     (error "Invalid name: %s" nil))
1321   ;; Do the renaming.
1322   (let ((top (gnus-topic-find-topology old-name))
1323         (entry (assoc old-name gnus-topic-alist)))
1324     (when top
1325       (setcar (cadr top) new-name))
1326     (when entry
1327       (setcar entry new-name))
1328     (forward-line -1)
1329     (gnus-dribble-touch)
1330     (gnus-group-list-groups)
1331     (forward-line 1)))
1332
1333 (defun gnus-topic-indent (&optional unindent)
1334   "Indent a topic -- make it a sub-topic of the previous topic.
1335 If UNINDENT, remove an indentation."
1336   (interactive "P")
1337   (if unindent
1338       (gnus-topic-unindent)
1339     (let* ((topic (gnus-current-topic))
1340            (parent (gnus-topic-previous-topic topic))
1341            (buffer-read-only nil))
1342       (unless parent
1343         (error "Nothing to indent %s into" topic))
1344       (when topic
1345         (gnus-topic-goto-topic topic)
1346         (gnus-topic-kill-group)
1347         (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1348         (gnus-topic-create-topic
1349          topic parent nil (cdaar gnus-topic-killed-topics))
1350         (pop gnus-topic-killed-topics)
1351         (or (gnus-topic-goto-topic topic)
1352             (gnus-topic-goto-topic parent))))))
1353
1354 (defun gnus-topic-unindent ()
1355   "Unindent a topic."
1356   (interactive)
1357   (let* ((topic (gnus-current-topic))
1358          (parent (gnus-topic-parent-topic topic))
1359          (grandparent (gnus-topic-parent-topic parent)))
1360     (unless grandparent
1361       (error "Nothing to indent %s into" topic))
1362     (when topic
1363       (gnus-topic-goto-topic topic)
1364       (gnus-topic-kill-group)
1365       (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1366       (gnus-topic-create-topic
1367        topic grandparent (gnus-topic-next-topic parent)
1368        (cdaar gnus-topic-killed-topics))
1369       (pop gnus-topic-killed-topics)
1370       (gnus-topic-goto-topic topic))))
1371
1372 (defun gnus-topic-list-active (&optional force)
1373   "List all groups that Gnus knows about in a topicsified fashion.
1374 If FORCE, always re-read the active file."
1375   (interactive "P")
1376   (when force
1377     (gnus-get-killed-groups))
1378   (gnus-topic-grok-active force)
1379   (let ((gnus-topic-topology gnus-topic-active-topology)
1380         (gnus-topic-alist gnus-topic-active-alist)
1381         gnus-killed-list gnus-zombie-list)
1382     (gnus-group-list-groups gnus-level-killed nil 1)))
1383
1384 (defun gnus-topic-toggle-display-empty-topics ()
1385   "Show/hide topics that have no unread articles."
1386   (interactive)
1387   (setq gnus-topic-display-empty-topics
1388         (not gnus-topic-display-empty-topics))
1389   (gnus-group-list-groups)
1390   (message "%s empty topics"
1391            (if gnus-topic-display-empty-topics
1392                "Showing" "Hiding")))
1393
1394 ;;; Topic sorting functions
1395
1396 (defun gnus-topic-edit-parameters (group)
1397   "Edit the group parameters of GROUP.
1398 If performed on a topic, edit the topic parameters instead."
1399   (interactive (list (gnus-group-group-name)))
1400   (if group
1401       (gnus-group-edit-group-parameters group)
1402     (if (not (gnus-group-topic-p))
1403         (error "Nothing to edit on the current line")
1404       (let ((topic (gnus-group-topic-name)))
1405         (gnus-edit-form
1406          (gnus-topic-parameters topic)
1407          (format "Editing the topic parameters for `%s'."
1408                  (or group topic))
1409          `(lambda (form)
1410             (gnus-topic-set-parameters ,topic form)))))))
1411
1412 (defun gnus-group-sort-topic (func reverse)
1413   "Sort groups in the topics according to FUNC and REVERSE."
1414   (let ((alist gnus-topic-alist))
1415     (while alist
1416       ;; !!!Sometimes nil elements sneak into the alist,
1417       ;; for some reason or other.
1418       (setcar alist (delq nil (car alist)))
1419       (setcar alist (delete "dummy.group" (car alist)))
1420       (gnus-topic-sort-topic (pop alist) func reverse))))
1421
1422 (defun gnus-topic-sort-topic (topic func reverse)
1423   ;; Each topic only lists the name of the group, while
1424   ;; the sort predicates expect group infos as inputs.
1425   ;; So we first transform the group names into infos,
1426   ;; then sort, and then transform back into group names.
1427   (setcdr
1428    topic
1429    (mapcar
1430     (lambda (info) (gnus-info-group info))
1431     (sort
1432      (mapcar
1433       (lambda (group) (gnus-get-info group))
1434       (cdr topic))
1435      func)))
1436   ;; Do the reversal, if necessary.
1437   (when reverse
1438     (setcdr topic (nreverse (cdr topic)))))
1439
1440 (defun gnus-topic-sort-groups (func &optional reverse)
1441   "Sort the current topic according to FUNC.
1442 If REVERSE, reverse the sorting order."
1443   (interactive (list gnus-group-sort-function current-prefix-arg))
1444   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1445     (gnus-topic-sort-topic
1446      topic (gnus-make-sort-function func) reverse)
1447     (gnus-group-list-groups)))
1448
1449 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1450   "Sort the current topic alphabetically by group name.
1451 If REVERSE, sort in reverse order."
1452   (interactive "P")
1453   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1454
1455 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1456   "Sort the current topic by number of unread articles.
1457 If REVERSE, sort in reverse order."
1458   (interactive "P")
1459   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1460
1461 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1462   "Sort the current topic by group level.
1463 If REVERSE, sort in reverse order."
1464   (interactive "P")
1465   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1466
1467 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1468   "Sort the current topic by group score.
1469 If REVERSE, sort in reverse order."
1470   (interactive "P")
1471   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1472
1473 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1474   "Sort the current topic by group rank.
1475 If REVERSE, sort in reverse order."
1476   (interactive "P")
1477   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1478
1479 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1480   "Sort the current topic alphabetically by backend name.
1481 If REVERSE, sort in reverse order."
1482   (interactive "P")
1483   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1484
1485 (defun gnus-topic-sort-topics-1 (top reverse)
1486   (if (cdr top)
1487       (let ((subtop
1488              (mapcar `(lambda (top)
1489                         (gnus-topic-sort-topics-1 top ,reverse))
1490                      (sort (cdr top)
1491                            '(lambda (t1 t2) 
1492                               (string-lessp (caar t1) (caar t2)))))))
1493         (setcdr top (if reverse (reverse subtop) subtop))))
1494   top)
1495
1496 (defun gnus-topic-sort-topics (&optional topic reverse)
1497   "Sort topics in TOPIC alphabeticaly by topic name.
1498 If REVERSE, reverse the sorting order."
1499   (interactive 
1500    (list (completing-read "Sort topics in : " gnus-topic-alist nil t 
1501                           (gnus-current-topic))
1502          current-prefix-arg))
1503   (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1504                             gnus-topic-topology)))
1505     (gnus-topic-sort-topics-1 topic-topology reverse)
1506     (gnus-topic-enter-dribble)
1507     (gnus-group-list-groups)
1508     (gnus-topic-goto-topic topic)))
1509
1510 (defun gnus-topic-move (current to)
1511   "Move the CURRENT topic to TO."
1512   (interactive 
1513    (list 
1514     (gnus-group-topic-name)
1515     (completing-read "Move to topic: " gnus-topic-alist nil t)))
1516   (unless (and current to)
1517     (error "Can't find topic"))
1518   (let ((current-top (cdr (gnus-topic-find-topology current)))
1519         (to-top (cdr (gnus-topic-find-topology to))))
1520     (unless current-top
1521       (error "Can't find topic `%s'" current))
1522     (unless to-top
1523       (error "Can't find topic `%s'" to))
1524     (if (gnus-topic-find-topology to current-top 0) ;; Don't care the level
1525         (error "Can't move `%s' to its sub-level" current))
1526     (gnus-topic-find-topology current nil nil 'delete)
1527     (while (cdr to-top)
1528       (setq to-top (cdr to-top)))
1529     (setcdr to-top (list current-top))
1530     (gnus-topic-enter-dribble)
1531     (gnus-group-list-groups)
1532     (gnus-topic-goto-topic current)))
1533
1534 (provide 'gnus-topic)
1535
1536 ;;; gnus-topic.el ends here