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