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