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