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