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