*** empty log message ***
[gnus] / lisp / gnus-topic.el
1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Ilja Weis <kult@uni-paderborn.de>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'gnus)
30 (eval-when-compile (require 'cl))
31
32 (defvar gnus-topic-mode nil
33   "Minor mode for Gnus group buffers.")
34
35 (defvar gnus-topic-mode-hook nil
36   "Hook run in topic mode buffers.")
37
38 (defvar gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
39   "Format of topic lines.
40 It works along the same lines as a normal formatting string,
41 with some simple extensions.
42
43 %i  Indentation based on topic level.
44 %n  Topic name.
45 %v  Nothing if the topic is visible, \"...\" otherwise.
46 %g  Number of groups in the topic.
47 %a  Number of unread articles in the groups in the topic.
48 %A  Number of unread articles in the groups in the topic and its subtopics.
49 ")
50
51 (defvar gnus-topic-unique t
52   "*If non-nil, each group will only belong to one topic.")
53
54 (defvar gnus-topic-indent-level 2
55   "*How much each subtopic should be indented.")
56
57 ;; Internal variables.
58
59 (defvar gnus-topic-killed-topics nil)
60 (defvar gnus-topic-inhibit-change-level nil)
61
62
63 (defconst gnus-topic-line-format-alist
64   `((?n name ?s)
65     (?v visible ?s)
66     (?i indentation ?s)
67     (?g number-of-groups ?d)
68     (?a (gnus-topic-articles-in-topic entries) ?d)
69     (?A total-number-of-articles ?d)
70     (?l level ?d)))
71
72 (defvar gnus-topic-line-format-spec nil)
73 (defvar gnus-topic-active-topology nil)
74 (defvar gnus-topic-active-alist nil)
75
76 ;; Functions.
77
78 (defun gnus-group-topic-name ()
79   "The name of the topic on the current line."
80   (get-text-property (gnus-point-at-bol) 'gnus-topic))
81
82 (defun gnus-group-topic-level ()
83   "The level of the topic on the current line."
84   (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
85
86 (defun gnus-topic-init-alist ()
87   "Initialize the topic structures."
88   (setq gnus-topic-topology
89         (cons (list "Gnus" 'visible)
90               (mapcar (lambda (topic)
91                         (list (list (car topic) 'visible)))
92                       '(("misc")))))
93   (setq gnus-topic-alist
94         (list (cons "misc"
95                     (mapcar (lambda (info) (gnus-info-group info))
96                             (cdr gnus-newsrc-alist)))
97               (list "Gnus")))
98   (gnus-topic-enter-dribble))
99
100 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
101   "List all newsgroups with unread articles of level LEVEL or lower, and
102 use the `gnus-group-topics' to sort the groups.
103 If ALL is non-nil, list groups that have no unread articles.
104 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
105   (set-buffer gnus-group-buffer)
106   (let ((buffer-read-only nil)
107         (lowest (or lowest 1))
108         tlist info)
109
110     (unless list-topic 
111       (erase-buffer))
112     
113     ;; List dead groups?
114     (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
115       (gnus-group-prepare-flat-list-dead 
116        (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
117        gnus-level-zombie ?Z
118        regexp))
119     
120     (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
121       (gnus-group-prepare-flat-list-dead 
122        (setq gnus-killed-list (sort gnus-killed-list 'string<))
123        gnus-level-killed ?K
124        regexp))
125     
126     ;; Use topics.
127     (when (< lowest gnus-level-zombie)
128       (if list-topic
129           (let ((top (gnus-topic-find-topology list-topic)))
130             (gnus-topic-prepare-topic (cdr top) (car top)
131                                       (or topic-level level) all))
132         (gnus-topic-prepare-topic gnus-topic-topology 0
133                                   (or topic-level level) all))))
134
135   (gnus-group-set-mode-line)
136   (setq gnus-group-list-mode (cons level all))
137   (run-hooks 'gnus-group-prepare-hook))
138
139 (defun gnus-topic-prepare-topic (topic level &optional list-level all silent)
140   "Insert TOPIC into the group buffer.
141 If SILENT, don't insert anything.  Return the number of unread
142 articles in the topic and its subtopics."
143   (let* ((type (pop topic))
144          (entries (gnus-topic-find-groups (car type) list-level all))
145          (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
146          (gnus-group-indentation 
147           (make-string (* gnus-topic-indent-level level) ? ))
148          (beg (progn (beginning-of-line) (point)))
149          (topic (reverse topic))
150          (all-entries entries)
151          (unread 0)
152          info entry end active)
153     ;; Insert any sub-topics.
154     (while topic
155       (incf unread
156             (gnus-topic-prepare-topic 
157              (pop topic) (1+ level) list-level all
158              (not visiblep))))
159     (setq end (point))
160     (goto-char beg)
161     ;; Insert all the groups that belong in this topic.
162     (while (setq entry (pop entries))
163       (when visiblep 
164         (if (stringp entry)
165             ;; Dead groups.
166             (gnus-group-insert-group-line
167              entry (if (member entry gnus-zombie-list) 8 9)
168              nil (- (1+ (cdr (setq active (gnus-active entry))))
169                     (car active)) nil)
170           ;; Living groups.
171           (when (setq info (nth 2 entry))
172             (gnus-group-insert-group-line 
173              (gnus-info-group info)
174              (gnus-info-level info) (gnus-info-marks info) 
175              (car entry) (gnus-info-method info)))))
176       (when (and (listp entry)
177                  (numberp (car entry)))
178         (incf unread (car entry))))
179     (goto-char beg)
180     ;; Insert the topic line.
181     (unless silent
182       (gnus-extent-start-open (point))
183       (gnus-topic-insert-topic-line 
184        (car type) visiblep
185        (not (eq (nth 2 type) 'hidden))
186        level all-entries unread))
187     (goto-char end)
188     unread))
189
190 (defun gnus-topic-find-groups (topic &optional level all)
191   "Return entries for all visible groups in TOPIC."
192   (let ((groups (cdr (assoc topic gnus-topic-alist)))
193         info clevel unread group lowest params visible-groups entry active)
194     (setq lowest (or lowest 1))
195     (setq level (or level 7))
196     ;; We go through the newsrc to look for matches.
197     (while groups
198       (setq entry (gnus-gethash (setq group (pop groups)) gnus-newsrc-hashtb)
199             info (nth 2 entry)
200             params (gnus-info-params info)
201             active (gnus-active group)
202             unread (or (car entry)
203                        (and (not (equal group "dummy.group"))
204                             active
205                             (- (1+ (cdr active)) (car active))))
206             clevel (or (gnus-info-level info)
207                        (if (member group gnus-zombie-list) 8 9)))
208       (and 
209        unread                           ; nil means that the group is dead.
210        (<= clevel level) 
211        (>= clevel lowest)               ; Is inside the level we want.
212        (or all
213            (and gnus-group-list-inactive-groups
214                 (eq unread t))
215            (> unread 0)
216            (and gnus-list-groups-with-ticked-articles
217                 (cdr (assq 'tick (gnus-info-marks info))))
218                                         ; Has right readedness.
219            ;; Check for permanent visibility.
220            (and gnus-permanently-visible-groups
221                 (string-match gnus-permanently-visible-groups group))
222            (memq 'visible params)
223            (cdr (assq 'visible params)))
224        ;; Add this group to the list of visible groups.
225        (push (or entry group) visible-groups)))
226     (nreverse visible-groups)))
227
228 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
229   "Remove the current topic."
230   (let ((topic (gnus-group-topic-name))
231         (level (gnus-group-topic-level))
232         (beg (progn (beginning-of-line) (point)))
233         buffer-read-only)
234     (when topic
235       (while (and (zerop (forward-line 1))
236                   (> (or (gnus-group-topic-level) (1+ level)) level)))
237       (delete-region beg (point))
238       (setcar (cdadr (gnus-topic-find-topology topic))
239               (if insert 'visible 'invisible))
240       (when hide
241         (setcdr (cdadr (gnus-topic-find-topology topic))
242                 (list hide)))
243       (unless total-remove
244         (gnus-topic-insert-topic topic in-level)))))
245
246 (defun gnus-topic-insert-topic (topic &optional level)
247   "Insert TOPIC."
248   (gnus-group-prepare-topics 
249    (car gnus-group-list-mode) (cdr gnus-group-list-mode)
250    nil nil topic level))
251   
252 (defun gnus-topic-fold (&optional insert)
253   "Remove/insert the current topic."
254   (let ((topic (gnus-group-topic-name))) 
255     (when topic
256       (save-excursion
257         (if (not (gnus-group-active-topic-p))
258             (gnus-topic-remove-topic
259              (or insert (not (gnus-topic-visible-p))))
260           (let ((gnus-topic-topology gnus-topic-active-topology)
261                 (gnus-topic-alist gnus-topic-active-alist)
262                 (gnus-group-list-mode (cons 5 t)))
263             (gnus-topic-remove-topic
264              (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
265
266 (defun gnus-group-topic-p ()
267   "Return non-nil if the current line is a topic."
268   (get-text-property (gnus-point-at-bol) 'gnus-topic))
269
270 (defun gnus-topic-visible-p ()
271   "Return non-nil if the current topic is visible."
272   (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
273
274 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries 
275                                           &optional unread)
276   (let* ((visible (if visiblep "" "..."))
277          (indentation (make-string (* gnus-topic-indent-level level) ? ))
278          (total-number-of-articles unread)
279          (number-of-groups (length entries))
280          (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
281     (beginning-of-line)
282     ;; Insert the text.
283     (add-text-properties 
284      (point)
285      (prog1 (1+ (point)) 
286        (eval gnus-topic-line-format-spec)
287        (gnus-topic-remove-excess-properties))
288      (list 'gnus-topic name
289            'gnus-topic-level level
290            'gnus-active active-topic
291            'gnus-topic-visible visiblep))))
292
293 (defun gnus-topic-previous-topic (topic)
294   "Return the previous topic on the same level as TOPIC."
295   (let ((top (cddr (gnus-topic-find-topology
296                         (gnus-topic-parent-topic topic)))))
297     (unless (equal topic (caaar top))
298       (while (and top (not (equal (caaadr top) topic)))
299         (setq top (cdr top)))
300       (caaar top))))
301
302 (defun gnus-topic-parent-topic (topic &optional topology)
303   "Return the parent of TOPIC."
304   (unless topology
305     (setq topology gnus-topic-topology))
306   (let ((parent (car (pop topology)))
307         result found)
308     (while (and topology
309                 (not (setq found (equal (caaar topology) topic)))
310                 (not (setq result (gnus-topic-parent-topic topic 
311                                                            (car topology)))))
312       (setq topology (cdr topology)))
313     (or result (and found parent))))
314
315 (defun gnus-topic-find-topology (topic &optional topology level remove)
316   "Return the topology of TOPIC."
317   (unless topology
318     (setq topology gnus-topic-topology)
319     (setq level 0))
320   (let ((top topology)
321         result)
322     (if (equal (caar topology) topic)
323         (progn
324           (when remove
325             (delq topology remove))
326           (cons level topology))
327       (setq topology (cdr topology))
328       (while (and topology
329                   (not (setq result (gnus-topic-find-topology
330                                      topic (car topology) (1+ level)
331                                      (and remove top)))))
332         (setq topology (cdr topology)))
333       result)))
334
335 (defun gnus-topic-check-topology ()  
336   ;; The first time we set the topology to whatever we have
337   ;; gotten here, which can be rather random.
338   (unless gnus-topic-alist
339     (gnus-topic-init-alist))
340
341   (let ((topics (gnus-topic-list))
342         (alist gnus-topic-alist)
343         changed)
344     (while alist
345       (unless (member (caar alist) topics)
346         (nconc gnus-topic-topology
347                (list (list (list (caar alist) 'visible))))
348         (setq changed t))
349       (setq alist (cdr alist)))
350     (when changed
351       (gnus-topic-enter-dribble)))
352   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
353                                          gnus-topic-alist)))
354          (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
355          (newsrc gnus-newsrc-alist)
356          group)
357     (while newsrc
358       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
359         (setcdr entry (cons group (cdr entry)))))))
360
361 (defvar gnus-tmp-topics nil)
362 (defun gnus-topic-list (&optional topology)
363   (unless topology
364     (setq topology gnus-topic-topology 
365           gnus-tmp-topics nil))
366   (push (caar topology) gnus-tmp-topics)
367   (mapcar 'gnus-topic-list (cdr topology))
368   gnus-tmp-topics)
369
370 (defun gnus-topic-enter-dribble ()
371   (gnus-dribble-enter
372    (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
373
374 (defun gnus-topic-articles-in-topic (entries)
375   (let ((total 0)
376         number)
377     (while entries
378       (when (numberp (setq number (car (pop entries))))
379         (incf total number)))
380     total))
381
382 (defun gnus-group-parent-topic ()
383   "Return the topic the current group belongs in."
384   (let ((group (gnus-group-group-name)))
385     (if group
386         (gnus-group-topic group)
387       (gnus-group-topic-name))))
388
389 (defun gnus-group-topic (group)
390   "Return the topic GROUP is a member of."
391   (let ((alist gnus-topic-alist)
392         out)
393     (while alist
394       (when (member group (cdar alist))
395         (setq out (caar alist)
396               alist nil))
397       (setq alist (cdr alist)))
398     out))
399
400 (defun gnus-topic-goto-topic (topic)
401   (let ((orig (point)))
402     (goto-char (point-min))
403     (while (and (not (equal topic (gnus-group-topic-name)))
404                 (zerop (forward-line 1))))
405     (or (gnus-group-topic-name)
406         (progn
407           (goto-char orig)
408           nil))))
409   
410 (defun gnus-topic-update-topic ()
411   "Update all parent topics to the current group."
412   (when (and (eq major-mode 'gnus-group-mode)
413              gnus-topic-mode)
414     (let ((group (gnus-group-group-name)))
415       (gnus-topic-goto-topic (gnus-group-parent-topic))
416       (gnus-topic-update-topic-line)
417       (gnus-group-goto-group group)
418       (gnus-group-position-point))))
419
420 (defun gnus-topic-goto-missing-group (group) 
421   "Place point where GROUP is supposed to be inserted."
422   (let* ((topic (gnus-group-topic group))
423          (groups (cdr (assoc topic gnus-topic-alist)))
424          (g (cdr (member group groups)))
425          (unfound t))
426     (while (and g unfound)
427       (when (gnus-group-goto-group (pop g))
428         (beginning-of-line)
429         (setq unfound nil)))
430     (when unfound
431       (setq g (cdr (member group (reverse groups))))
432       (while (and g unfound)
433         (when (gnus-group-goto-group (pop g))
434           (forward-line 1)
435           (setq unfound nil)))
436       (when unfound
437         (gnus-topic-goto-topic topic)
438         (forward-line 1)))))
439
440 (defun gnus-topic-update-topic-line (&optional topic level)
441   (unless topic
442     (setq topic gnus-topic-topology)
443     (setq level 0))
444   (let* ((type (pop topic))
445          (buffer-read-only nil)
446          (entries (gnus-topic-find-groups 
447                    (car type) (car gnus-group-list-mode)
448                    (cdr gnus-group-list-mode)))
449          (visiblep (eq (nth 1 type) 'visible))
450          (all-entries entries)
451          (unread 0)
452          info entry end)
453     ;; Tally any sub-topics.
454     (while topic
455       (incf unread (gnus-topic-update-topic-line (pop topic) (1+ level))))
456     ;; Tally all the groups that belong in this topic.
457     (while (setq info (nth 2 (setq entry (pop entries))))
458       (when (numberp (car entry))
459         (incf unread (car entry))))
460     ;; Insert the topic line.
461     (when (gnus-topic-goto-topic (car type))
462       (gnus-topic-insert-topic-line 
463        (car type) visiblep
464        (not (eq (nth 2 type) 'hidden))
465        level all-entries unread)
466       (gnus-delete-line))
467     unread))
468
469 (defun gnus-topic-grok-active (&optional force read-active)
470   "Parse all active groups and create topic structures for them."
471   ;; First we make sure that we have really read the active file. 
472   (when (or force
473             (not gnus-topic-active-alist))
474     (when (and read-active
475                (or force
476                    (not (member gnus-select-method 
477                                 gnus-have-read-active-file))))
478       (let ((gnus-read-active-file t))
479         (gnus-read-active-file)))
480     (let (topology groups alist)
481       ;; Get a list of all groups available.
482       (mapatoms (lambda (g) (when (symbol-value g)
483                               (push (symbol-name g) groups)))
484                 gnus-active-hashtb)
485       (setq groups (sort groups 'string<))
486       ;; Init the variables.
487       (setq gnus-topic-active-topology '(("" visible)))
488       (setq gnus-topic-active-alist nil)
489       ;; Descend the top-level hierarchy.
490       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
491       ;; Set the top-level topic names to something nice.
492       (setcar (car gnus-topic-active-topology) "Gnus active")
493       (setcar (car gnus-topic-active-alist) "Gnus active"))))
494
495 (defun gnus-topic-grok-active-1 (topology groups)
496   (let* ((name (caar topology))
497          (prefix (concat "^" (regexp-quote name)))
498          tgroups nprefix ntopology group)
499     (while (and groups
500                 (string-match prefix (setq group (car groups))))
501       (if (not (string-match "\\." group (match-end 0)))
502           ;; There are no further hierarchies here, so we just
503           ;; enter this group into the list belonging to this
504           ;; topic.
505           (push (pop groups) tgroups)
506         ;; New sub-hierarchy, so we add it to the topology.
507         (nconc topology (list (setq ntopology 
508                                     (list (list (substring 
509                                                  group 0 (match-end 0))
510                                                 'invisible)))))
511         ;; Descend the hierarchy.
512         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
513     ;; We remove the trailing "." from the topic name.
514     (setq name
515           (if (string-match "\\.$" name)
516               (substring name 0 (match-beginning 0))
517             name))
518     ;; Add this topic and its groups to the topic alist.
519     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
520     (setcar (car topology) name)
521     ;; We return the rest of the groups that didn't belong
522     ;; to this topic.
523     groups))
524
525 (defun gnus-group-active-topic-p ()
526   "Return whether the current active comes from the active topics."
527   (save-excursion
528     (beginning-of-line)
529     (get-text-property (point) 'gnus-active)))
530
531 ;;; Topic mode, commands and keymap.
532
533 (defvar gnus-topic-mode-map nil)
534 (defvar gnus-group-topic-map nil)
535
536 (unless gnus-topic-mode-map
537   (setq gnus-topic-mode-map (make-sparse-keymap))
538
539   ;; Override certain group mode keys.
540   (gnus-define-keys
541    gnus-topic-mode-map
542    "=" gnus-topic-select-group
543    "\r" gnus-topic-select-group
544    " " gnus-topic-read-group
545    "\C-k" gnus-topic-kill-group
546    "\C-y" gnus-topic-yank-group
547    "\M-g" gnus-topic-get-new-news-this-topic
548    "\C-i" gnus-topic-indent
549    "AT" gnus-topic-list-active
550    gnus-mouse-2 gnus-mouse-pick-topic)
551
552   ;; Define a new submap.
553   (gnus-define-keys
554    (gnus-group-topic-map "T" gnus-group-mode-map)
555    "#" gnus-topic-mark-topic
556    "n" gnus-topic-create-topic
557    "m" gnus-topic-move-group
558    "D" gnus-topic-remove-group
559    "c" gnus-topic-copy-group
560    "h" gnus-topic-hide-topic
561    "s" gnus-topic-show-topic
562    "M" gnus-topic-move-matching
563    "C" gnus-topic-copy-matching
564    "r" gnus-topic-rename
565    "\177" gnus-topic-delete))
566
567 (defun gnus-topic-make-menu-bar ()
568   (unless (boundp 'gnus-topic-menu)
569     (easy-menu-define
570      gnus-topic-menu gnus-topic-mode-map ""
571      '("Topics"
572        ["Toggle topics" gnus-topic-mode t]
573        ("Groups"
574         ["Copy" gnus-topic-copy-group t]
575         ["Move" gnus-topic-move-group t]
576         ["Remove" gnus-topic-remove-group t]
577         ["Copy matching" gnus-topic-copy-matching t]
578         ["Move matching" gnus-topic-move-matching t])
579        ("Topics"
580         ["Show" gnus-topic-show-topic t]
581         ["Hide" gnus-topic-hide-topic t]
582         ["Delete" gnus-topic-delete t]
583         ["Rename" gnus-topic-rename t]
584         ["Create" gnus-topic-create-topic t]
585         ["Mark" gnus-topic-mark-topic t]
586         ["Indent" gnus-topic-indent t])
587        ["List active" gnus-topic-list-active t]))))
588
589
590 (defun gnus-topic-mode (&optional arg redisplay)
591   "Minor mode for topicsifying Gnus group buffers."
592   (interactive (list current-prefix-arg t))
593   (when (eq major-mode 'gnus-group-mode)
594     (make-local-variable 'gnus-topic-mode)
595     (setq gnus-topic-mode 
596           (if (null arg) (not gnus-topic-mode)
597             (> (prefix-numeric-value arg) 0)))
598     ;; Infest Gnus with topics.
599     (when gnus-topic-mode
600       (when (and menu-bar-mode
601                  (gnus-visual-p 'topic-menu 'menu))
602         (gnus-topic-make-menu-bar))
603       (setq gnus-topic-line-format-spec 
604             (gnus-parse-format gnus-topic-line-format 
605                                gnus-topic-line-format-alist t))
606       (unless (assq 'gnus-topic-mode minor-mode-alist)
607         (push '(gnus-topic-mode " Topic") minor-mode-alist))
608       (unless (assq 'gnus-topic-mode minor-mode-map-alist)
609         (push (cons 'gnus-topic-mode gnus-topic-mode-map)
610               minor-mode-map-alist))
611       (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
612       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
613       (make-local-variable 'gnus-group-prepare-function)
614       (setq gnus-group-prepare-function 'gnus-group-prepare-topics)
615       (make-local-variable 'gnus-group-goto-next-group-function)
616       (setq gnus-group-goto-next-group-function 
617             'gnus-topic-goto-next-group)
618       (setq gnus-group-change-level-function 'gnus-topic-change-level)
619       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
620       (run-hooks 'gnus-topic-mode-hook)
621       ;; We check the topology.
622       (gnus-topic-check-topology))
623     ;; Remove topic infestation.
624     (unless gnus-topic-mode
625       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
626       (remove-hook 'gnus-group-change-level-function 
627                    'gnus-topic-change-level)
628       (setq gnus-group-prepare-function 'gnus-group-prepare-flat))
629     (when redisplay
630       (gnus-group-list-groups))))
631     
632 (defun gnus-topic-select-group (&optional all)
633   "Select this newsgroup.
634 No article is selected automatically.
635 If ALL is non-nil, already read articles become readable.
636 If ALL is a number, fetch this number of articles."
637   (interactive "P")
638   (if (gnus-group-topic-p)
639       (let ((gnus-group-list-mode 
640              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
641         (gnus-topic-fold all))
642     (gnus-group-select-group all)))
643
644 (defun gnus-mouse-pick-topic (e)
645   "Select the group or topic under the mouse pointer."
646   (interactive "e")
647   (mouse-set-point e)
648   (gnus-topic-read-group nil))
649
650 (defun gnus-topic-read-group (&optional all no-article group)
651   "Read news in this newsgroup.
652 If the prefix argument ALL is non-nil, already read articles become
653 readable.  IF ALL is a number, fetch this number of articles.  If the
654 optional argument NO-ARTICLE is non-nil, no article will be
655 auto-selected upon group entry.  If GROUP is non-nil, fetch that
656 group."
657   (interactive "P")
658   (if (gnus-group-topic-p)
659       (let ((gnus-group-list-mode 
660              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
661         (gnus-topic-fold all))
662     (gnus-group-read-group all no-article group)))
663
664 (defun gnus-topic-create-topic (topic parent &optional previous)
665   (interactive 
666    (list
667     (read-string "Create topic: ")
668     (completing-read "Parent topic: " gnus-topic-alist nil t
669                      (cons (gnus-group-parent-topic) 0))))
670   ;; Check whether this topic already exists.
671   (when (gnus-topic-find-topology topic)
672     (error "Topic aleady exists"))
673   (unless parent
674     (setq parent (caar gnus-topic-topology)))
675   (let ((top (cdr (gnus-topic-find-topology parent))))
676     (unless top
677       (error "No such parent topic: %s" parent))
678     (if previous
679         (progn
680           (while (and (cdr top)
681                       (not (equal (caaadr top) previous)))
682             (setq top (cdr top)))
683           (setcdr top (cons (list (list topic 'visible)) (cdr top))))
684       (nconc top (list (list (list topic 'visible)))))
685     (unless (assoc topic gnus-topic-alist)
686       (push (list topic) gnus-topic-alist)))
687   (gnus-topic-enter-dribble)
688   (gnus-group-list-groups))
689
690 (defun gnus-topic-move-group (n topic &optional copyp)
691   "Move the current group to a topic."
692   (interactive
693    (list current-prefix-arg
694          (completing-read "Move to topic: " gnus-topic-alist nil t)))
695   (let ((groups (gnus-group-process-prefix n))
696         (topicl (assoc topic gnus-topic-alist))
697         entry)
698     (mapcar (lambda (g) 
699               (gnus-group-remove-mark g)
700               (when (and
701                      (setq entry (assoc (gnus-group-topic g) gnus-topic-alist))
702                      (not copyp))
703                 (setcdr entry (delete g (cdr entry))))
704               (when topicl
705                 (nconc topicl (list g))))
706             groups)
707     (gnus-group-position-point))
708   (gnus-topic-enter-dribble)
709   (gnus-group-list-groups))
710
711 (defun gnus-topic-remove-group (n)
712   "Remove the current group the topic."
713   (interactive "P")
714   (gnus-topic-move-group n nil))
715
716 (defun gnus-topic-copy-group (n topic)
717   "Copy the current group to a topic."
718   (interactive
719    (list current-prefix-arg
720          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
721   (gnus-topic-move-group n topic t))
722
723 (defun gnus-topic-change-level (group level oldlevel)
724   "Run when changing levels to enter/remove groups from topics."
725   (when (and gnus-topic-mode 
726              (not gnus-topic-inhibit-change-level))
727     ;; Remove the group from the topics.
728     (when (and (< oldlevel gnus-level-zombie)
729                (>= level gnus-level-zombie))
730       (let (alist)
731         (when (setq alist (assoc (gnus-group-topic group) gnus-topic-alist))
732           (setcdr alist (delete group (cdr alist))))))
733     ;; If the group is subscribed. then we enter it into the topics.
734     (when (and (< level gnus-level-zombie)
735                (>= oldlevel gnus-level-zombie))
736       (let ((entry (assoc (caar gnus-topic-topology) gnus-topic-alist)))
737         (setcdr entry (cons group (cdr entry)))))))
738
739 (defun gnus-topic-goto-next-group (group props)
740   "Go to group or the next group after group."
741   (if (null group)
742       (gnus-topic-goto-topic (cadr (memq 'gnus-topic props)))
743     (if (gnus-group-goto-group group)
744         t
745       ;; The group is no longer visible.
746       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
747              (after (cdr (member group (cdr list)))))
748         ;; First try to put point on a group after the current one.
749         (while (and after
750                     (not (gnus-group-goto-group (car after))))
751           (setq after (cdr after)))
752         ;; Then try to put point on a group before point.
753         (unless after
754           (setq after (cdr (member group (reverse (cdr list)))))
755           (while (and after 
756                       (not (gnus-group-goto-group (car after))))
757             (setq after (cdr after))))
758         ;; Finally, just put point on the topic.
759         (unless after
760           (gnus-topic-goto-topic (car list))
761           (setq after nil))
762         t))))
763
764 (defun gnus-topic-kill-group (&optional n discard)
765   "Kill the next N groups."
766   (interactive "P")
767   (if (gnus-group-topic-p)
768       (let ((topic (gnus-group-topic-name)))
769         (gnus-topic-remove-topic nil t)
770         (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
771               gnus-topic-killed-topics))
772     (gnus-group-kill-group n discard)
773     (gnus-topic-update-topic)))
774   
775 (defun gnus-topic-yank-group (&optional arg)
776   "Yank the last topic."
777   (interactive "p")
778   (if gnus-topic-killed-topics
779       (let ((previous (gnus-group-parent-topic))
780             (item (nth 1 (pop gnus-topic-killed-topics))))
781         (gnus-topic-create-topic
782          (car item) (gnus-topic-parent-topic previous) previous))
783     (let* ((prev (gnus-group-group-name))
784            (gnus-topic-inhibit-change-level t)
785            (gnus-group-indentation
786             (make-string 
787              (* gnus-topic-indent-level
788                 (or (save-excursion
789                       (gnus-topic-goto-topic (gnus-group-parent-topic))
790                       (gnus-group-topic-level)) 0)) ? ))
791            yanked group alist)
792       ;; We first yank the groups the normal way...
793       (setq yanked (gnus-group-yank-group arg))
794       ;; Then we enter the yanked groups into the topics they belong
795       ;; to. 
796       (setq alist (assoc (save-excursion
797                            (forward-line -1)
798                            (gnus-group-parent-topic))
799                          gnus-topic-alist))
800       (when (stringp yanked)
801         (setq yanked (list yanked)))
802       (if (not prev)
803           (nconc alist yanked)
804         (if (not (cdr alist))
805             (setcdr alist (nconc yanked (cdr alist)))
806           (while (cdr alist)
807             (when (equal (cadr alist) prev)
808               (setcdr alist (nconc yanked (cdr alist)))
809               (setq alist nil))
810             (setq alist (cdr alist))))))
811     (gnus-topic-update-topic)))
812
813 (defun gnus-topic-hide-topic ()
814   "Hide all subtopics under the current topic."
815   (interactive)
816   (when (gnus-group-topic-p)
817     (gnus-topic-remove-topic nil nil 'hidden)))
818
819 (defun gnus-topic-show-topic ()
820   "Show the hidden topic."
821   (interactive)
822   (when (gnus-group-topic-p)
823     (gnus-topic-remove-topic t nil 'shown)))
824
825 (defun gnus-topic-mark-topic (topic)
826   "Mark all groups in the topic with the process mark."
827   (interactive (list (gnus-group-parent-topic)))
828   (let ((groups (cdr (gnus-topic-find-groups topic))))
829     (while groups
830       (gnus-group-set-mark (gnus-info-group (nth 2 (pop groups)))))))
831
832 (defun gnus-topic-get-new-news-this-topic (&optional n)
833   "Check for new news in the current topic."
834   (interactive "P")
835   (if (not (gnus-group-topic-p))
836       (gnus-group-get-new-news-this-group n)
837     (gnus-topic-mark-topic (gnus-group-topic-name))
838     (gnus-group-get-new-news-this-group)))
839
840 (defun gnus-topic-move-matching (regexp topic &optional copyp)
841   "Move all groups that match REGEXP to some topic."
842   (interactive
843    (let (topic)
844      (nreverse
845       (list
846        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
847        (read-string (format "Move to %s (regexp): " topic))))))
848   (gnus-group-mark-regexp regexp)
849   (gnus-topic-move-group nil topic copyp))
850
851 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
852   "Copy all groups that match REGEXP to some topic."
853   (interactive
854    (let (topic)
855      (nreverse
856       (list
857        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
858        (read-string (format "Copy to %s (regexp): " topic))))))
859   (gnus-topic-move-matching regexp topic t))
860
861 (defun gnus-topic-delete (topic)
862   "Delete a topic."
863   (interactive (list (gnus-group-topic-name)))
864   (unless topic
865     (error "No topic to be deleted"))
866   (let ((entry (assoc topic gnus-topic-alist))
867         (buffer-read-only nil))
868     (when (cdr entry)
869       (error "Topic not empty"))
870     ;; Delete if visible.
871     (when (gnus-topic-goto-topic topic)
872       (gnus-delete-line))
873     ;; Remove from alist.
874     (setq gnus-topic-alist (delq entry gnus-topic-alist))
875     ;; Remove from topology.
876     (gnus-topic-find-topology topic nil nil 'delete)))
877
878 (defun gnus-topic-rename (old-name new-name)
879   "Rename a topic."
880   (interactive
881    (let (topic)
882      (list
883       (setq topic (completing-read "Rename topic: " gnus-topic-alist nil t
884                                    (cons (gnus-group-parent-topic) 0)))
885       (read-string (format "Rename %s to: " topic)))))
886   (let ((top (gnus-topic-find-topology old-name))
887         (entry (assoc old-name gnus-topic-alist)))
888     (when top
889       (setcar (cadr top) new-name))
890     (when entry 
891       (setcar entry new-name))
892     (gnus-group-list-groups)))
893
894 (defun gnus-topic-indent (&optional unindent)
895   "Indent a topic -- make it a sub-topic of the previous topic.
896 If UNINDENT, remove an indentation."
897   (interactive "P")
898   (if unindent
899       (gnus-topic-unindent)
900     (let* ((topic (gnus-group-parent-topic))
901            (parent (gnus-topic-previous-topic topic)))
902       (unless parent
903         (error "Nothing to indent %s into" topic))
904       (when topic
905         (gnus-topic-goto-topic topic)
906         (gnus-topic-kill-group)
907         (gnus-topic-create-topic topic parent)
908         (gnus-topic-goto-topic topic)))))
909
910 (defun gnus-topic-unindent ()
911   "Unindent a topic."
912   (interactive)
913   (let* ((topic (gnus-group-parent-topic))
914          (parent (gnus-topic-parent-topic topic))
915          (grandparent (gnus-topic-parent-topic parent)))
916     (unless grandparent
917       (error "Nothing to indent %s into" topic))
918     (when topic
919       (gnus-topic-goto-topic topic)
920       (gnus-topic-kill-group)
921       (gnus-topic-create-topic topic grandparent)
922       (gnus-topic-goto-topic topic))))
923
924 (defun gnus-topic-list-active (&optional force)
925   "List all groups that Gnus knows about in a topicsified fashion.
926 If FORCE, always re-read the active file."
927   (interactive "P")
928   (gnus-topic-grok-active force)
929   (let ((gnus-topic-topology gnus-topic-active-topology)
930         (gnus-topic-alist gnus-topic-active-alist)
931         gnus-killed-list gnus-zombie-list)
932     (gnus-group-list-groups 9 nil 1)))
933
934 (provide 'gnus-topic)
935
936 ;;; gnus-topic.el ends here