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