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