*** 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)
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 (or force
475               (not (member gnus-select-method gnus-have-read-active-file)))
476       (let ((gnus-read-active-file t))
477         (gnus-read-active-file)))
478     (let (topology groups alist)
479       ;; Get a list of all groups available.
480       (mapatoms (lambda (g) (when (symbol-value g)
481                               (push (symbol-name g) groups)))
482                 gnus-active-hashtb)
483       (setq groups (sort groups 'string<))
484       ;; Init the variables.
485       (setq gnus-topic-active-topology '(("" visible)))
486       (setq gnus-topic-active-alist nil)
487       ;; Descend the top-level hierarchy.
488       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
489       ;; Set the top-level topic names to something nice.
490       (setcar (car gnus-topic-active-topology) "Gnus active")
491       (setcar (car gnus-topic-active-alist) "Gnus active"))))
492
493 (defun gnus-topic-grok-active-1 (topology groups)
494   (let* ((name (caar topology))
495          (prefix (concat "^" (regexp-quote name)))
496          tgroups nprefix ntopology group)
497     (while (and groups
498                 (string-match prefix (setq group (car groups))))
499       (if (not (string-match "\\." group (match-end 0)))
500           ;; There are no further hierarchies here, so we just
501           ;; enter this group into the list belonging to this
502           ;; topic.
503           (push (pop groups) tgroups)
504         ;; New sub-hierarchy, so we add it to the topology.
505         (nconc topology (list (setq ntopology 
506                                     (list (list (substring 
507                                                  group 0 (match-end 0))
508                                                 'invisible)))))
509         ;; Descend the hierarchy.
510         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
511     ;; We remove the trailing "." from the topic name.
512     (setq name
513           (if (string-match "\\.$" name)
514               (substring name 0 (match-beginning 0))
515             name))
516     ;; Add this topic and its groups to the topic alist.
517     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
518     (setcar (car topology) name)
519     ;; We return the rest of the groups that didn't belong
520     ;; to this topic.
521     groups))
522
523 (defun gnus-group-active-topic-p ()
524   "Return whether the current active comes from the active topics."
525   (save-excursion
526     (beginning-of-line)
527     (get-text-property (point) 'gnus-active)))
528
529 ;;; Topic mode, commands and keymap.
530
531 (defvar gnus-topic-mode-map nil)
532 (defvar gnus-group-topic-map nil)
533
534 (unless gnus-topic-mode-map
535   (setq gnus-topic-mode-map (make-sparse-keymap))
536
537   ;; Override certain group mode keys.
538   (gnus-define-keys
539    gnus-topic-mode-map
540    "=" gnus-topic-select-group
541    "\r" gnus-topic-select-group
542    " " gnus-topic-read-group
543    "\C-k" gnus-topic-kill-group
544    "\C-y" gnus-topic-yank-group
545    "\M-g" gnus-topic-get-new-news-this-topic
546    "\C-i" gnus-topic-indent
547    "AT" gnus-topic-list-active
548    gnus-mouse-2 gnus-mouse-pick-topic)
549
550   ;; Define a new submap.
551   (gnus-define-keys
552    (gnus-group-topic-map "T" gnus-group-mode-map)
553    "#" gnus-topic-mark-topic
554    "n" gnus-topic-create-topic
555    "m" gnus-topic-move-group
556    "D" gnus-topic-remove-group
557    "c" gnus-topic-copy-group
558    "h" gnus-topic-hide-topic
559    "s" gnus-topic-show-topic
560    "M" gnus-topic-move-matching
561    "C" gnus-topic-copy-matching
562    "r" gnus-topic-rename
563    "\177" gnus-topic-delete))
564
565 (defun gnus-topic-make-menu-bar ()
566   (unless (boundp 'gnus-topic-menu)
567     (easy-menu-define
568      gnus-topic-menu gnus-topic-mode-map ""
569      '("Topics"
570        ["Toggle topics" gnus-topic-mode t]
571        ("Groups"
572         ["Copy" gnus-topic-copy-group t]
573         ["Move" gnus-topic-move-group t]
574         ["Remove" gnus-topic-remove-group t]
575         ["Copy matching" gnus-topic-copy-matching t]
576         ["Move matching" gnus-topic-move-matching t])
577        ("Topics"
578         ["Show" gnus-topic-show-topic t]
579         ["Hide" gnus-topic-hide-topic t]
580         ["Delete" gnus-topic-delete t]
581         ["Rename" gnus-topic-rename t]
582         ["Create" gnus-topic-create-topic t]
583         ["Mark" gnus-topic-mark-topic t]
584         ["Indent" gnus-topic-indent t])
585        ["List active" gnus-topic-list-active t]))))
586
587
588 (defun gnus-topic-mode (&optional arg redisplay)
589   "Minor mode for topicsifying Gnus group buffers."
590   (interactive (list current-prefix-arg t))
591   (when (eq major-mode 'gnus-group-mode)
592     (make-local-variable 'gnus-topic-mode)
593     (setq gnus-topic-mode 
594           (if (null arg) (not gnus-topic-mode)
595             (> (prefix-numeric-value arg) 0)))
596     ;; Infest Gnus with topics.
597     (when gnus-topic-mode
598       (when (and menu-bar-mode
599                  (gnus-visual-p 'topic-menu 'menu))
600         (gnus-topic-make-menu-bar))
601       (setq gnus-topic-line-format-spec 
602             (gnus-parse-format gnus-topic-line-format 
603                                gnus-topic-line-format-alist t))
604       (unless (assq 'gnus-topic-mode minor-mode-alist)
605         (push '(gnus-topic-mode " Topic") minor-mode-alist))
606       (unless (assq 'gnus-topic-mode minor-mode-map-alist)
607         (push (cons 'gnus-topic-mode gnus-topic-mode-map)
608               minor-mode-map-alist))
609       (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
610       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
611       (make-local-variable 'gnus-group-prepare-function)
612       (setq gnus-group-prepare-function 'gnus-group-prepare-topics)
613       (make-local-variable 'gnus-group-goto-next-group-function)
614       (setq gnus-group-goto-next-group-function 
615             'gnus-topic-goto-next-group)
616       (setq gnus-group-change-level-function 'gnus-topic-change-level)
617       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
618       (run-hooks 'gnus-topic-mode-hook)
619       ;; We check the topology.
620       (gnus-topic-check-topology))
621     ;; Remove topic infestation.
622     (unless gnus-topic-mode
623       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
624       (remove-hook 'gnus-group-change-level-function 
625                    'gnus-topic-change-level)
626       (setq gnus-group-prepare-function 'gnus-group-prepare-flat))
627     (when redisplay
628       (gnus-group-list-groups))))
629     
630 (defun gnus-topic-select-group (&optional all)
631   "Select this newsgroup.
632 No article is selected automatically.
633 If ALL is non-nil, already read articles become readable.
634 If ALL is a number, fetch this number of articles."
635   (interactive "P")
636   (if (gnus-group-topic-p)
637       (let ((gnus-group-list-mode 
638              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
639         (gnus-topic-fold all))
640     (gnus-group-select-group all)))
641
642 (defun gnus-mouse-pick-topic (e)
643   "Select the group or topic under the mouse pointer."
644   (interactive "e")
645   (mouse-set-point e)
646   (gnus-topic-read-group nil))
647
648 (defun gnus-topic-read-group (&optional all no-article group)
649   "Read news in this newsgroup.
650 If the prefix argument ALL is non-nil, already read articles become
651 readable.  IF ALL is a number, fetch this number of articles.  If the
652 optional argument NO-ARTICLE is non-nil, no article will be
653 auto-selected upon group entry.  If GROUP is non-nil, fetch that
654 group."
655   (interactive "P")
656   (if (gnus-group-topic-p)
657       (let ((gnus-group-list-mode 
658              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
659         (gnus-topic-fold all))
660     (gnus-group-read-group all no-article group)))
661
662 (defun gnus-topic-create-topic (topic parent &optional previous)
663   (interactive 
664    (list
665     (read-string "Create topic: ")
666     (completing-read "Parent topic: " gnus-topic-alist nil t
667                      (cons (gnus-group-parent-topic) 0))))
668   ;; Check whether this topic already exists.
669   (when (gnus-topic-find-topology topic)
670     (error "Topic aleady exists"))
671   (unless parent
672     (setq parent (caar gnus-topic-topology)))
673   (let ((top (cdr (gnus-topic-find-topology parent))))
674     (unless top
675       (error "No such parent topic: %s" parent))
676     (if previous
677         (progn
678           (while (and (cdr top)
679                       (not (equal (caaadr top) previous)))
680             (setq top (cdr top)))
681           (setcdr top (cons (list (list topic 'visible)) (cdr top))))
682       (nconc top (list (list (list topic 'visible)))))
683     (unless (assoc topic gnus-topic-alist)
684       (push (list topic) gnus-topic-alist)))
685   (gnus-topic-enter-dribble)
686   (gnus-group-list-groups))
687
688 (defun gnus-topic-move-group (n topic &optional copyp)
689   "Move the current group to a topic."
690   (interactive
691    (list current-prefix-arg
692          (completing-read "Move to topic: " gnus-topic-alist nil t)))
693   (let ((groups (gnus-group-process-prefix n))
694         (topicl (assoc topic gnus-topic-alist))
695         entry)
696     (mapcar (lambda (g) 
697               (gnus-group-remove-mark g)
698               (when (and
699                      (setq entry (assoc (gnus-group-topic g) gnus-topic-alist))
700                      (not copyp))
701                 (setcdr entry (delete g (cdr entry))))
702               (when topicl
703                 (nconc topicl (list g))))
704             groups)
705     (gnus-group-position-point))
706   (gnus-topic-enter-dribble)
707   (gnus-group-list-groups))
708
709 (defun gnus-topic-remove-group (n)
710   "Remove the current group the topic."
711   (interactive "P")
712   (gnus-topic-move-group n nil))
713
714 (defun gnus-topic-copy-group (n topic)
715   "Copy the current group to a topic."
716   (interactive
717    (list current-prefix-arg
718          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
719   (gnus-topic-move-group n topic t))
720
721 (defun gnus-topic-change-level (group level oldlevel)
722   "Run when changing levels to enter/remove groups from topics."
723   (when (and gnus-topic-mode 
724              (not gnus-topic-inhibit-change-level))
725     ;; Remove the group from the topics.
726     (when (and (< oldlevel gnus-level-zombie)
727                (>= level gnus-level-zombie))
728       (let (alist)
729         (when (setq alist (assoc (gnus-group-topic group) gnus-topic-alist))
730           (setcdr alist (delete group (cdr alist))))))
731     ;; If the group is subscribed. then we enter it into the topics.
732     (when (and (< level gnus-level-zombie)
733                (>= oldlevel gnus-level-zombie))
734       (let ((entry (assoc (caar gnus-topic-topology) gnus-topic-alist)))
735         (setcdr entry (cons group (cdr entry)))))))
736
737 (defun gnus-topic-goto-next-group (group props)
738   "Go to group or the next group after group."
739   (if (null group)
740       (gnus-topic-goto-topic (cadr (memq 'gnus-topic props)))
741     (if (gnus-group-goto-group group)
742         t
743       ;; The group is no longer visible.
744       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
745              (after (cdr (member group (cdr list)))))
746         ;; First try to put point on a group after the current one.
747         (while (and after
748                     (not (gnus-group-goto-group (car after))))
749           (setq after (cdr after)))
750         ;; Then try to put point on a group before point.
751         (unless after
752           (setq after (cdr (member group (reverse (cdr list)))))
753           (while (and after 
754                       (not (gnus-group-goto-group (car after))))
755             (setq after (cdr after))))
756         ;; Finally, just put point on the topic.
757         (unless after
758           (gnus-topic-goto-topic (car list))
759           (setq after nil))
760         t))))
761
762 (defun gnus-topic-kill-group (&optional n discard)
763   "Kill the next N groups."
764   (interactive "P")
765   (if (gnus-group-topic-p)
766       (let ((topic (gnus-group-topic-name)))
767         (gnus-topic-remove-topic nil t)
768         (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
769               gnus-topic-killed-topics))
770     (gnus-group-kill-group n discard)
771     (gnus-topic-update-topic)))
772   
773 (defun gnus-topic-yank-group (&optional arg)
774   "Yank the last topic."
775   (interactive "p")
776   (if gnus-topic-killed-topics
777       (let ((previous (gnus-group-parent-topic))
778             (item (nth 1 (pop gnus-topic-killed-topics))))
779         (gnus-topic-create-topic
780          (car item) (gnus-topic-parent-topic previous) previous))
781     (let* ((prev (gnus-group-group-name))
782            (gnus-topic-inhibit-change-level t)
783            (gnus-group-indentation
784             (make-string 
785              (* gnus-topic-indent-level
786                 (or (save-excursion
787                       (gnus-topic-goto-topic (gnus-group-parent-topic))
788                       (gnus-group-topic-level)) 0)) ? ))
789            yanked group alist)
790       ;; We first yank the groups the normal way...
791       (setq yanked (gnus-group-yank-group arg))
792       ;; Then we enter the yanked groups into the topics they belong
793       ;; to. 
794       (setq alist (assoc (save-excursion
795                            (forward-line -1)
796                            (gnus-group-parent-topic))
797                          gnus-topic-alist))
798       (when (stringp yanked)
799         (setq yanked (list yanked)))
800       (if (not prev)
801           (nconc alist yanked)
802         (if (not (cdr alist))
803             (setcdr alist (nconc yanked (cdr alist)))
804           (while (cdr alist)
805             (when (equal (cadr alist) prev)
806               (setcdr alist (nconc yanked (cdr alist)))
807               (setq alist nil))
808             (setq alist (cdr alist))))))
809     (gnus-topic-update-topic)))
810
811 (defun gnus-topic-hide-topic ()
812   "Hide all subtopics under the current topic."
813   (interactive)
814   (when (gnus-group-topic-p)
815     (gnus-topic-remove-topic nil nil 'hidden)))
816
817 (defun gnus-topic-show-topic ()
818   "Show the hidden topic."
819   (interactive)
820   (when (gnus-group-topic-p)
821     (gnus-topic-remove-topic t nil 'shown)))
822
823 (defun gnus-topic-mark-topic (topic)
824   "Mark all groups in the topic with the process mark."
825   (interactive (list (gnus-group-parent-topic)))
826   (let ((groups (cdr (gnus-topic-find-groups topic))))
827     (while groups
828       (gnus-group-set-mark (gnus-info-group (nth 2 (pop groups)))))))
829
830 (defun gnus-topic-get-new-news-this-topic (&optional n)
831   "Check for new news in the current topic."
832   (interactive "P")
833   (if (not (gnus-group-topic-p))
834       (gnus-group-get-new-news-this-group n)
835     (gnus-topic-mark-topic (gnus-group-topic-name))
836     (gnus-group-get-new-news-this-group)))
837
838 (defun gnus-topic-move-matching (regexp topic &optional copyp)
839   "Move all groups that match REGEXP to some topic."
840   (interactive
841    (let (topic)
842      (nreverse
843       (list
844        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
845        (read-string (format "Move to %s (regexp): " topic))))))
846   (gnus-group-mark-regexp regexp)
847   (gnus-topic-move-group nil topic copyp))
848
849 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
850   "Copy all groups that match REGEXP to some topic."
851   (interactive
852    (let (topic)
853      (nreverse
854       (list
855        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
856        (read-string (format "Copy to %s (regexp): " topic))))))
857   (gnus-topic-move-matching regexp topic t))
858
859 (defun gnus-topic-delete (topic)
860   "Delete a topic."
861   (interactive (list (gnus-group-topic-name)))
862   (unless topic
863     (error "No topic to be deleted"))
864   (let ((entry (assoc topic gnus-topic-alist))
865         (buffer-read-only nil))
866     (when (cdr entry)
867       (error "Topic not empty"))
868     ;; Delete if visible.
869     (when (gnus-topic-goto-topic topic)
870       (gnus-delete-line))
871     ;; Remove from alist.
872     (setq gnus-topic-alist (delq entry gnus-topic-alist))
873     ;; Remove from topology.
874     (gnus-topic-find-topology topic nil nil 'delete)))
875
876 (defun gnus-topic-rename (old-name new-name)
877   "Rename a topic."
878   (interactive
879    (let (topic)
880      (list
881       (setq topic (completing-read "Rename topic: " gnus-topic-alist nil t
882                                    (cons (gnus-group-parent-topic) 0)))
883       (read-string (format "Rename %s to: " topic)))))
884   (let ((top (gnus-topic-find-topology old-name))
885         (entry (assoc old-name gnus-topic-alist)))
886     (when top
887       (setcar (cadr top) new-name))
888     (when entry 
889       (setcar entry new-name))
890     (gnus-group-list-groups)))
891
892 (defun gnus-topic-indent (&optional unindent)
893   "Indent a topic -- make it a sub-topic of the previous topic.
894 If UNINDENT, remove an indentation."
895   (interactive "P")
896   (if unindent
897       (gnus-topic-unindent)
898     (let* ((topic (gnus-group-parent-topic))
899            (parent (gnus-topic-previous-topic topic)))
900       (unless parent
901         (error "Nothing to indent %s into" topic))
902       (when topic
903         (gnus-topic-goto-topic topic)
904         (gnus-topic-kill-group)
905         (gnus-topic-create-topic topic parent)
906         (gnus-topic-goto-topic topic)))))
907
908 (defun gnus-topic-unindent ()
909   "Unindent a topic."
910   (interactive)
911   (let* ((topic (gnus-group-parent-topic))
912          (parent (gnus-topic-parent-topic topic))
913          (grandparent (gnus-topic-parent-topic parent)))
914     (unless grandparent
915       (error "Nothing to indent %s into" topic))
916     (when topic
917       (gnus-topic-goto-topic topic)
918       (gnus-topic-kill-group)
919       (gnus-topic-create-topic topic grandparent)
920       (gnus-topic-goto-topic topic))))
921
922 (defun gnus-topic-list-active (&optional force)
923   "List all groups that Gnus knows about in a topicsified fashion.
924 If FORCE, always re-read the active file."
925   (interactive "P")
926   (gnus-topic-grok-active)
927   (let ((gnus-topic-topology gnus-topic-active-topology)
928         (gnus-topic-alist gnus-topic-active-alist)
929         gnus-killed-list gnus-zombie-list)
930     (gnus-group-list-groups 9 nil 1)))
931
932 (provide 'gnus-topic)
933
934 ;;; gnus-topic.el ends here