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