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