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