5c94034d0d41c5b4b4520cea777b5c04dcbe3b08
[gnus] / lisp / gnus-topic.el
1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Ilja Weis <kult@uni-paderborn.de>
5 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-start)
34 (require 'gnus-util)
35
36 (defgroup gnus-topic nil
37   "Group topics."
38   :group 'gnus-group)
39
40 (defvar gnus-topic-mode nil
41   "Minor mode for Gnus group buffers.")
42
43 (defcustom gnus-topic-mode-hook nil
44   "Hook run in topic mode buffers."
45   :type 'hook
46   :group 'gnus-topic)
47
48 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
49   "Format of topic lines.
50 It works along the same lines as a normal formatting string,
51 with some simple extensions.
52
53 %i  Indentation based on topic level.
54 %n  Topic name.
55 %v  Nothing if the topic is visible, \"...\" otherwise.
56 %g  Number of groups in the topic.
57 %a  Number of unread articles in the groups in the topic.
58 %A  Number of unread articles in the groups in the topic and its subtopics.
59 "
60   :type 'string
61   :group 'gnus-topic)
62
63 (defcustom gnus-topic-indent-level 2
64   "*How much each subtopic should be indented."
65   :type 'integer
66   :group 'gnus-topic)
67
68 (defcustom gnus-topic-display-empty-topics t
69   "*If non-nil, display the topic lines even of topics that have no unread articles."
70   :type 'boolean
71   :group 'gnus-topic)
72
73 ;; Internal variables.
74
75 (defvar gnus-topic-active-topology nil)
76 (defvar gnus-topic-active-alist nil)
77 (defvar gnus-topic-unreads nil)
78
79 (defvar gnus-topology-checked-p nil
80   "Whether the topology has been checked in this session.")
81
82 (defvar gnus-topic-killed-topics nil)
83 (defvar gnus-topic-inhibit-change-level nil)
84
85 (defconst gnus-topic-line-format-alist
86   `((?n name ?s)
87     (?v visible ?s)
88     (?i indentation ?s)
89     (?g number-of-groups ?d)
90     (?a (gnus-topic-articles-in-topic entries) ?d)
91     (?A total-number-of-articles ?d)
92     (?l level ?d)))
93
94 (defvar gnus-topic-line-format-spec nil)
95
96 ;;; Utility functions
97
98 (defun gnus-group-topic-name ()
99   "The name of the topic on the current line."
100   (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
101     (and topic (symbol-name topic))))
102
103 (defun gnus-group-topic-level ()
104   "The level of the topic on the current line."
105   (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
106
107 (defun gnus-group-topic-unread ()
108   "The number of unread articles in topic on the current line."
109   (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
110
111 (defun gnus-topic-unread (topic)
112   "Return the number of unread articles in TOPIC."
113   (or (cdr (assoc topic gnus-topic-unreads))
114       0))
115
116 (defun gnus-group-topic-p ()
117   "Return non-nil if the current line is a topic."
118   (gnus-group-topic-name))
119
120 (defun gnus-topic-visible-p ()
121   "Return non-nil if the current topic is visible."
122   (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
123
124 (defun gnus-topic-articles-in-topic (entries)
125   (let ((total 0)
126         number)
127     (while entries
128       (when (numberp (setq number (car (pop entries))))
129         (incf total number)))
130     total))
131
132 (defun gnus-group-topic (group)
133   "Return the topic GROUP is a member of."
134   (let ((alist gnus-topic-alist)
135         out)
136     (while alist
137       (when (member group (cdar alist))
138         (setq out (caar alist)
139               alist nil))
140       (setq alist (cdr alist)))
141     out))
142
143 (defun gnus-group-parent-topic (group)
144   "Return the topic GROUP is member of by looking at the group buffer."
145   (save-excursion
146     (set-buffer gnus-group-buffer)
147     (if (gnus-group-goto-group group)
148         (gnus-current-topic)
149       (gnus-group-topic group))))
150
151 (defun gnus-topic-goto-topic (topic)
152   (when topic
153     (gnus-goto-char (text-property-any (point-min) (point-max)
154                                        'gnus-topic (intern topic)))))
155
156 (defun gnus-topic-jump-to-topic (topic)
157   "Go to TOPIC."
158   (interactive
159    (list (completing-read "Go to topic: "
160                           (mapcar 'list (gnus-topic-list))
161                           nil t)))
162   (dolist (topic (gnus-current-topics topic))
163     (gnus-topic-fold t))
164   (gnus-topic-goto-topic topic))
165   
166 (defun gnus-current-topic ()
167   "Return the name of the current topic."
168   (let ((result
169          (or (get-text-property (point) 'gnus-topic)
170              (save-excursion
171                (and (gnus-goto-char (previous-single-property-change
172                                      (point) 'gnus-topic))
173                     (get-text-property (max (1- (point)) (point-min))
174                                        'gnus-topic))))))
175     (when result
176       (symbol-name result))))
177
178 (defun gnus-current-topics (&optional topic)
179   "Return a list of all current topics, lowest in hierarchy first.
180 If TOPIC, start with that topic."
181   (let ((topic (or topic (gnus-current-topic)))
182         topics)
183     (while topic
184       (push topic topics)
185       (setq topic (gnus-topic-parent-topic topic)))
186     (nreverse topics)))
187
188 (defun gnus-group-active-topic-p ()
189   "Say whether the current topic comes from the active topics."
190   (save-excursion
191     (beginning-of-line)
192     (get-text-property (point) 'gnus-active)))
193
194 (defun gnus-topic-find-groups (topic &optional level all lowest)
195   "Return entries for all visible groups in TOPIC."
196   (let ((groups (cdr (assoc topic gnus-topic-alist)))
197         info clevel unread group params visible-groups entry active)
198     (setq lowest (or lowest 1))
199     (setq level (or level gnus-level-unsubscribed))
200     ;; We go through the newsrc to look for matches.
201     (while groups
202       (when (setq group (pop groups))
203         (setq entry (gnus-gethash group gnus-newsrc-hashtb)
204               info (nth 2 entry)
205               params (gnus-info-params info)
206               active (gnus-active group)
207               unread (or (car entry)
208                          (and (not (equal group "dummy.group"))
209                               active
210                               (- (1+ (cdr active)) (car active))))
211               clevel (or (gnus-info-level info)
212                          (if (member group gnus-zombie-list)
213                              gnus-level-zombie gnus-level-killed))))
214       (and
215        info                             ; nil means that the group is dead.
216        (<= clevel level)
217        (>= clevel lowest)               ; Is inside the level we want.
218        (or all
219            (if (or (eq unread t)
220                    (eq unread nil))
221                gnus-group-list-inactive-groups
222              (> unread 0))
223            (and gnus-list-groups-with-ticked-articles
224                 (cdr (assq 'tick (gnus-info-marks info))))
225            ;; Has right readedness.
226            ;; Check for permanent visibility.
227            (and gnus-permanently-visible-groups
228                 (string-match gnus-permanently-visible-groups group))
229            (memq 'visible params)
230            (cdr (assq 'visible params)))
231        ;; Add this group to the list of visible groups.
232        (push (or entry group) visible-groups)))
233     (nreverse visible-groups)))
234
235 (defun gnus-topic-previous-topic (topic)
236   "Return the previous topic on the same level as TOPIC."
237   (let ((top (cddr (gnus-topic-find-topology
238                     (gnus-topic-parent-topic topic)))))
239     (unless (equal topic (caaar top))
240       (while (and top (not (equal (caaadr top) topic)))
241         (setq top (cdr top)))
242       (caaar top))))
243
244 (defun gnus-topic-parent-topic (topic &optional topology)
245   "Return the parent of TOPIC."
246   (unless topology
247     (setq topology gnus-topic-topology))
248   (let ((parent (car (pop topology)))
249         result found)
250     (while (and topology
251                 (not (setq found (equal (caaar topology) topic)))
252                 (not (setq result (gnus-topic-parent-topic
253                                    topic (car topology)))))
254       (setq topology (cdr topology)))
255     (or result (and found parent))))
256
257 (defun gnus-topic-next-topic (topic &optional previous)
258   "Return the next sibling of TOPIC."
259   (let ((parentt (cddr (gnus-topic-find-topology
260                         (gnus-topic-parent-topic topic))))
261         prev)
262     (while (and parentt
263                 (not (equal (caaar parentt) topic)))
264       (setq prev (caaar parentt)
265             parentt (cdr parentt)))
266     (if previous
267         prev
268       (caaadr parentt))))
269
270 (defun gnus-topic-forward-topic (num)
271   "Go to the next topic on the same level as the current one."
272   (let* ((topic (gnus-current-topic))
273          (way (if (< num 0) 'gnus-topic-previous-topic
274                 'gnus-topic-next-topic))
275          (num (abs num)))
276     (while (and (not (zerop num))
277                 (setq topic (funcall way topic)))
278       (when (gnus-topic-goto-topic topic)
279         (decf num)))
280     (unless (zerop num)
281       (goto-char (point-max)))
282     num))
283
284 (defun gnus-topic-find-topology (topic &optional topology level remove)
285   "Return the topology of TOPIC."
286   (unless topology
287     (setq topology gnus-topic-topology)
288     (setq level 0))
289   (let ((top topology)
290         result)
291     (if (equal (caar topology) topic)
292         (progn
293           (when remove
294             (delq topology remove))
295           (cons level topology))
296       (setq topology (cdr topology))
297       (while (and topology
298                   (not (setq result (gnus-topic-find-topology
299                                      topic (car topology) (1+ level)
300                                      (and remove top)))))
301         (setq topology (cdr topology)))
302       result)))
303
304 (defvar gnus-tmp-topics nil)
305 (defun gnus-topic-list (&optional topology)
306   "Return a list of all topics in the topology."
307   (unless topology
308     (setq topology gnus-topic-topology
309           gnus-tmp-topics nil))
310   (push (caar topology) gnus-tmp-topics)
311   (mapcar 'gnus-topic-list (cdr topology))
312   gnus-tmp-topics)
313
314 ;;; Topic parameter jazz
315
316 (defun gnus-topic-parameters (topic)
317   "Return the parameters for TOPIC."
318   (let ((top (gnus-topic-find-topology topic)))
319     (when top
320       (nth 3 (cadr top)))))
321
322 (defun gnus-topic-set-parameters (topic parameters)
323   "Set the topic parameters of TOPIC to PARAMETERS."
324   (let ((top (gnus-topic-find-topology topic)))
325     (unless top
326       (error "No such topic: %s" topic))
327     ;; We may have to extend if there is no parameters here
328     ;; to begin with.
329     (unless (nthcdr 2 (cadr top))
330       (nconc (cadr top) (list nil)))
331     (unless (nthcdr 3 (cadr top))
332       (nconc (cadr top) (list nil)))
333     (setcar (nthcdr 3 (cadr top)) parameters)
334     (gnus-dribble-enter
335      (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
336
337 (defun gnus-group-topic-parameters (group)
338   "Compute the group parameters for GROUP taking into account inheritance from topics."
339   (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
340     (save-excursion
341       (gnus-group-goto-group group)
342       (nconc params-list
343              (gnus-topic-hierarchical-parameters (gnus-current-topic))))))
344
345 (defun gnus-topic-hierarchical-parameters (topic)
346   "Return a topic list computed for TOPIC."
347   (let ((topics (gnus-current-topics topic))
348         params-list param out params)
349     (while topics
350       (push (gnus-topic-parameters (pop topics)) params-list))
351     ;; We probably have lots of nil elements here, so
352     ;; we remove them.  Probably faster than doing this "properly".
353     (setq params-list (delq nil params-list))
354     ;; Now we have all the parameters, so we go through them
355     ;; and do inheritance in the obvious way.
356     (while (setq params (pop params-list))
357       (while (setq param (pop params))
358         (when (atom param)
359           (setq param (cons param t)))
360         ;; Override any old versions of this param.
361         (gnus-pull (car param) out)
362         (push param out)))
363     ;; Return the resulting parameter list.
364     out))
365
366 ;;; General utility functions
367
368 (defun gnus-topic-enter-dribble ()
369   (gnus-dribble-enter
370    (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
371
372 ;;; Generating group buffers
373
374 (defun gnus-group-prepare-topics (level &optional all lowest
375                                         regexp list-topic topic-level)
376   "List all newsgroups with unread articles of level LEVEL or lower.
377 Use the `gnus-group-topics' to sort the groups.
378 If ALL is non-nil, list groups that have no unread articles.
379 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
380   (set-buffer gnus-group-buffer)
381   (let ((buffer-read-only nil)
382         (lowest (or lowest 1)))
383
384     (when (or (not gnus-topic-alist)
385               (not gnus-topology-checked-p))
386       (gnus-topic-check-topology))
387
388     (unless list-topic
389       (erase-buffer))
390
391     ;; List dead groups?
392     (when (and (>= level gnus-level-zombie)
393                (<= lowest gnus-level-zombie))
394       (gnus-group-prepare-flat-list-dead
395        (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
396        gnus-level-zombie ?Z
397        regexp))
398
399     (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
400       (gnus-group-prepare-flat-list-dead
401        (setq gnus-killed-list (sort gnus-killed-list 'string<))
402        gnus-level-killed ?K
403        regexp))
404
405     ;; Use topics.
406     (prog1
407         (when (< lowest gnus-level-zombie)
408           (if list-topic
409               (let ((top (gnus-topic-find-topology list-topic)))
410                 (gnus-topic-prepare-topic (cdr top) (car top)
411                                           (or topic-level level) all
412                                           nil lowest))
413             (gnus-topic-prepare-topic gnus-topic-topology 0
414                                       (or topic-level level) all
415                                       nil lowest)))
416
417       (gnus-group-set-mode-line)
418       (setq gnus-group-list-mode (cons level all))
419       (gnus-run-hooks 'gnus-group-prepare-hook))))
420
421 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent
422                                         lowest)
423   "Insert TOPIC into the group buffer.
424 If SILENT, don't insert anything.  Return the number of unread
425 articles in the topic and its subtopics."
426   (let* ((type (pop topicl))
427          (entries (gnus-topic-find-groups
428                    (car type) list-level
429                    (or all
430                        (cdr (assq 'visible
431                                   (gnus-topic-hierarchical-parameters
432                                    (car type)))))
433                    lowest))
434          (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
435          (gnus-group-indentation
436           (make-string (* gnus-topic-indent-level level) ? ))
437          (beg (progn (beginning-of-line) (point)))
438          (topicl (reverse topicl))
439          (all-entries entries)
440          (point-max (point-max))
441          (unread 0)
442          (topic (car type))
443          info entry end active tick)
444     ;; Insert any sub-topics.
445     (while topicl
446       (incf unread
447             (gnus-topic-prepare-topic
448              (pop topicl) (1+ level) list-level all
449              (not visiblep) lowest)))
450     (setq end (point))
451     (goto-char beg)
452     ;; Insert all the groups that belong in this topic.
453     (while (setq entry (pop entries))
454       (when visiblep
455         (if (stringp entry)
456             ;; Dead groups.
457             (gnus-group-insert-group-line
458              entry (if (member entry gnus-zombie-list)
459                        gnus-level-zombie gnus-level-killed)
460              nil (- (1+ (cdr (setq active (gnus-active entry))))
461                     (car active))
462              nil)
463           ;; Living groups.
464           (when (setq info (nth 2 entry))
465             (gnus-group-insert-group-line
466              (gnus-info-group info)
467              (gnus-info-level info) (gnus-info-marks info)
468              (car entry) (gnus-info-method info)))))
469       (when (and (listp entry)
470                  (numberp (car entry)))
471         (incf unread (car entry)))
472       (when (listp entry)
473         (setq tick t)))
474     (goto-char beg)
475     ;; Insert the topic line.
476     (when (and (not silent)
477                (or gnus-topic-display-empty-topics ;We want empty topics
478                    (not (zerop unread)) ;Non-empty
479                    tick                 ;Ticked articles
480                    (/= point-max (point-max)))) ;Unactivated groups
481       (gnus-extent-start-open (point))
482       (gnus-topic-insert-topic-line
483        (car type) visiblep
484        (not (eq (nth 2 type) 'hidden))
485        level all-entries unread))
486     (gnus-topic-update-unreads (car type) unread)
487     (goto-char end)
488     unread))
489
490 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
491   "Remove the current topic."
492   (let ((topic (gnus-group-topic-name))
493         (level (gnus-group-topic-level))
494         (beg (progn (beginning-of-line) (point)))
495         buffer-read-only)
496     (when topic
497       (while (and (zerop (forward-line 1))
498                   (> (or (gnus-group-topic-level) (1+ level)) level)))
499       (delete-region beg (point))
500       ;; Do the change in this rather odd manner because it has been
501       ;; reported that some topics share parts of some lists, for some
502       ;; reason.  I have been unable to determine why this is the
503       ;; case, but this hack seems to take care of things.
504       (let ((data (cadr (gnus-topic-find-topology topic))))
505         (setcdr data
506                 (list (if insert 'visible 'invisible)
507                       (if hide 'hide nil)
508                       (cadddr data))))
509       (if total-remove
510           (setq gnus-topic-alist
511                 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
512         (gnus-topic-insert-topic topic in-level)))))
513
514 (defun gnus-topic-insert-topic (topic &optional level)
515   "Insert TOPIC."
516   (gnus-group-prepare-topics
517    (car gnus-group-list-mode) (cdr gnus-group-list-mode)
518    nil nil topic level))
519
520 (defun gnus-topic-fold (&optional insert topic)
521   "Remove/insert the current topic."
522   (let ((topic (or topic (gnus-group-topic-name))))
523     (when topic
524       (save-excursion
525         (if (not (gnus-group-active-topic-p))
526             (gnus-topic-remove-topic
527              (or insert (not (gnus-topic-visible-p))))
528           (let ((gnus-topic-topology gnus-topic-active-topology)
529                 (gnus-topic-alist gnus-topic-active-alist)
530                 (gnus-group-list-mode (cons 5 t)))
531             (gnus-topic-remove-topic
532              (or insert (not (gnus-topic-visible-p))) nil nil 9)
533             (gnus-topic-enter-dribble)))))))
534
535 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
536                                           &optional unread)
537   (let* ((visible (if visiblep "" "..."))
538          (indentation (make-string (* gnus-topic-indent-level level) ? ))
539          (total-number-of-articles unread)
540          (number-of-groups (length entries))
541          (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
542          gnus-tmp-header)
543     (gnus-topic-update-unreads name unread)
544     (beginning-of-line)
545     ;; Insert the text.
546     (gnus-add-text-properties
547      (point)
548      (prog1 (1+ (point))
549        (eval gnus-topic-line-format-spec))
550      (list 'gnus-topic (intern name)
551            'gnus-topic-level level
552            'gnus-topic-unread unread
553            'gnus-active active-topic
554            'gnus-topic-visible visiblep))))
555
556 (defun gnus-topic-update-unreads (topic unreads)
557   (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
558                                  gnus-topic-unreads))
559   (push (cons topic unreads) gnus-topic-unreads))
560
561 (defun gnus-topic-update-topics-containing-group (group)
562   "Update all topics that have GROUP as a member."
563   (when (and (eq major-mode 'gnus-group-mode)
564              gnus-topic-mode)
565     (save-excursion
566       (let ((alist gnus-topic-alist))
567         ;; This is probably not entirely correct.  If a topic
568         ;; isn't shown, then it's not updated.  But the updating
569         ;; should be performed in any case, since the topic's
570         ;; parent should be updated.  Pfft.
571         (while alist
572           (when (and (member group (cdar alist))
573                      (gnus-topic-goto-topic (caar alist)))
574             (gnus-topic-update-topic-line (caar alist)))
575           (pop alist))))))
576
577 (defun gnus-topic-update-topic ()
578   "Update all parent topics to the current group."
579   (when (and (eq major-mode 'gnus-group-mode)
580              gnus-topic-mode)
581     (let ((group (gnus-group-group-name))
582           (m (point-marker))
583           (buffer-read-only nil))
584       (when (and group
585                  (gnus-get-info group)
586                  (gnus-topic-goto-topic (gnus-current-topic)))
587         (gnus-topic-update-topic-line (gnus-group-topic-name))
588         (goto-char m)
589         (set-marker m nil)
590         (gnus-group-position-point)))))
591
592 (defun gnus-topic-goto-missing-group (group)
593   "Place point where GROUP is supposed to be inserted."
594   (let* ((topic (gnus-group-topic group))
595          (groups (cdr (assoc topic gnus-topic-alist)))
596          (g (cdr (member group groups)))
597          (unfound t))
598     ;; Try to jump to a visible group.
599     (while (and g (not (gnus-group-goto-group (car g) t)))
600       (pop g))
601     ;; It wasn't visible, so we try to see where to insert it.
602     (when (not g)
603       (setq g (cdr (member group (reverse groups))))
604       (while (and g unfound)
605         (when (gnus-group-goto-group (pop g) t)
606           (forward-line 1)
607           (setq unfound nil)))
608       (when (and unfound
609                  topic
610                  (not (gnus-topic-goto-missing-topic topic)))
611         (let* ((top (gnus-topic-find-topology topic))
612                (children (cddr top))
613                (type (cadr top))
614                (unread 0)
615                (entries (gnus-topic-find-groups
616                          (car type) (car gnus-group-list-mode)
617                          (cdr gnus-group-list-mode))))
618           (while children
619             (incf unread (gnus-topic-unread (caar (pop children)))))
620           (while (setq entry (pop entries))
621             (when (numberp (car entry))
622               (incf unread (car entry))))
623           (gnus-topic-insert-topic-line
624            topic t t (car (gnus-topic-find-topology topic)) nil unread))))))
625
626 (defun gnus-topic-goto-missing-topic (topic)
627   (if (gnus-topic-goto-topic topic)
628       (forward-line 1)
629     ;; Topic not displayed.
630     (let* ((top (gnus-topic-find-topology
631                  (gnus-topic-parent-topic topic)))
632            (tp (reverse (cddr top))))
633       (if (not top)
634           (gnus-topic-insert-topic-line
635            topic t t (car (gnus-topic-find-topology topic)) nil unread)
636         (while (not (equal (caaar tp) topic))
637           (setq tp (cdr tp)))
638         (pop tp)
639         (while (and tp
640                     (not (gnus-topic-goto-topic (caaar tp))))
641           (pop tp))
642         (if tp
643             (gnus-topic-forward-topic 1)
644           (gnus-topic-goto-missing-topic (caadr top)))))
645     nil))
646
647 (defun gnus-topic-update-topic-line (topic-name &optional reads)
648   (let* ((top (gnus-topic-find-topology topic-name))
649          (type (cadr top))
650          (children (cddr top))
651          (entries (gnus-topic-find-groups
652                    (car type) (car gnus-group-list-mode)
653                    (cdr gnus-group-list-mode)))
654          (parent (gnus-topic-parent-topic topic-name))
655          (all-entries entries)
656          (unread 0)
657          old-unread entry new-unread)
658     (when (gnus-topic-goto-topic (car type))
659       ;; Tally all the groups that belong in this topic.
660       (if reads
661           (setq unread (- (gnus-group-topic-unread) reads))
662         (while children
663           (incf unread (gnus-topic-unread (caar (pop children)))))
664         (while (setq entry (pop entries))
665           (when (numberp (car entry))
666             (incf unread (car entry)))))
667       (setq old-unread (gnus-group-topic-unread))
668       ;; Insert the topic line.
669       (gnus-topic-insert-topic-line
670        (car type) (gnus-topic-visible-p)
671        (not (eq (nth 2 type) 'hidden))
672        (gnus-group-topic-level) all-entries unread)
673       (gnus-delete-line)
674       (forward-line -1)
675       (setq new-unread (gnus-group-topic-unread)))
676     (when parent
677       (forward-line -1)
678       (gnus-topic-update-topic-line
679        parent
680        (- (or old-unread 0) (or new-unread 0))))
681     unread))
682
683 (defun gnus-topic-group-indentation ()
684   (make-string
685    (* gnus-topic-indent-level
686       (or (save-excursion
687             (forward-line -1)
688             (gnus-topic-goto-topic (gnus-current-topic))
689             (gnus-group-topic-level))
690           0))
691    ? ))
692
693 ;;; Initialization
694
695 (gnus-add-shutdown 'gnus-topic-close 'gnus)
696
697 (defun gnus-topic-close ()
698   (setq gnus-topic-active-topology nil
699         gnus-topic-active-alist nil
700         gnus-topic-killed-topics nil
701         gnus-topology-checked-p nil))
702
703 (defun gnus-topic-check-topology ()
704   ;; The first time we set the topology to whatever we have
705   ;; gotten here, which can be rather random.
706   (unless gnus-topic-alist
707     (gnus-topic-init-alist))
708
709   (setq gnus-topology-checked-p t)
710   ;; Go through the topic alist and make sure that all topics
711   ;; are in the topic topology.
712   (let ((topics (gnus-topic-list))
713         (alist gnus-topic-alist)
714         changed)
715     (while alist
716       (unless (member (caar alist) topics)
717         (nconc gnus-topic-topology
718                (list (list (list (caar alist) 'visible))))
719         (setq changed t))
720       (setq alist (cdr alist)))
721     (when changed
722       (gnus-topic-enter-dribble))
723     ;; Conversely, go through the topology and make sure that all
724     ;; topologies have alists.
725     (while topics
726       (unless (assoc (car topics) gnus-topic-alist)
727         (push (list (car topics)) gnus-topic-alist))
728       (pop topics)))
729   ;; Go through all living groups and make sure that
730   ;; they belong to some topic.
731   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
732                                          gnus-topic-alist)))
733          (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
734          (newsrc (cdr gnus-newsrc-alist))
735          group)
736     (while newsrc
737       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
738         (setcdr entry (list group))
739         (setq entry (cdr entry)))))
740   ;; Go through all topics and make sure they contain only living groups.
741   (let ((alist gnus-topic-alist)
742         topic)
743     (while (setq topic (pop alist))
744       (while (cdr topic)
745         (if (and (cadr topic)
746                  (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
747             (setq topic (cdr topic))
748           (setcdr topic (cddr topic)))))))
749
750 (defun gnus-topic-init-alist ()
751   "Initialize the topic structures."
752   (setq gnus-topic-topology
753         (cons (list "Gnus" 'visible)
754               (mapcar (lambda (topic)
755                         (list (list (car topic) 'visible)))
756                       '(("misc")))))
757   (setq gnus-topic-alist
758         (list (cons "misc"
759                     (mapcar (lambda (info) (gnus-info-group info))
760                             (cdr gnus-newsrc-alist)))
761               (list "Gnus")))
762   (gnus-topic-enter-dribble))
763
764 ;;; Maintenance
765
766 (defun gnus-topic-clean-alist ()
767   "Remove bogus groups from the topic alist."
768   (let ((topic-alist gnus-topic-alist)
769         result topic)
770     (unless gnus-killed-hashtb
771       (gnus-make-hashtable-from-killed))
772     (while (setq topic (pop topic-alist))
773       (let ((topic-name (pop topic))
774             group filtered-topic)
775         (while (setq group (pop topic))
776           (when (and (or (gnus-gethash group gnus-active-hashtb)
777                          (gnus-info-method (gnus-get-info group)))
778                      (not (gnus-gethash group gnus-killed-hashtb)))
779             (push group filtered-topic)))
780         (push (cons topic-name (nreverse filtered-topic)) result)))
781     (setq gnus-topic-alist (nreverse result))))
782
783 (defun gnus-topic-change-level (group level oldlevel &optional previous)
784   "Run when changing levels to enter/remove groups from topics."
785   (save-excursion
786     (set-buffer gnus-group-buffer)
787     (let ((buffer-read-only nil))
788       (unless gnus-topic-inhibit-change-level
789         (gnus-group-goto-group (or (car (nth 2 previous)) group))
790         (when (and gnus-topic-mode
791                    gnus-topic-alist
792                    (not gnus-topic-inhibit-change-level))
793           ;; Remove the group from the topics.
794           (if (and (< oldlevel gnus-level-zombie)
795                    (>= level gnus-level-zombie))
796               (let ((alist gnus-topic-alist))
797                 (while (gnus-group-goto-group group)
798                   (gnus-delete-line))
799                 (while alist
800                   (when (member group (car alist))
801                     (setcdr (car alist) (delete group (cdar alist))))
802                   (pop alist)))
803             ;; If the group is subscribed we enter it into the topics.
804             (when (and (< level gnus-level-zombie)
805                        (>= oldlevel gnus-level-zombie))
806               (let* ((prev (gnus-group-group-name))
807                      (gnus-topic-inhibit-change-level t)
808                      (gnus-group-indentation
809                       (make-string
810                        (* gnus-topic-indent-level
811                           (or (save-excursion
812                                 (gnus-topic-goto-topic (gnus-current-topic))
813                                 (gnus-group-topic-level))
814                               0))
815                        ? ))
816                      (yanked (list group))
817                      alist talist end)
818                 ;; Then we enter the yanked groups into the topics they belong
819                 ;; to.
820                 (when (setq alist (assoc (save-excursion
821                                            (forward-line -1)
822                                            (or
823                                             (gnus-current-topic)
824                                             (caar gnus-topic-topology)))
825                                          gnus-topic-alist))
826                   (setq talist alist)
827                   (when (stringp yanked)
828                     (setq yanked (list yanked)))
829                   (if (not prev)
830                       (nconc alist yanked)
831                     (if (not (cdr alist))
832                         (setcdr alist (nconc yanked (cdr alist)))
833                       (while (and (not end) (cdr alist))
834                         (when (equal (cadr alist) prev)
835                           (setcdr alist (nconc yanked (cdr alist)))
836                           (setq end t))
837                         (setq alist (cdr alist)))
838                       (unless end
839                         (nconc talist yanked))))))
840               (gnus-topic-update-topic))))))))
841
842 (defun gnus-topic-goto-next-group (group props)
843   "Go to group or the next group after group."
844   (if (not group)
845       (if (not (memq 'gnus-topic props))
846           (goto-char (point-max))
847         (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
848     (if (gnus-group-goto-group group)
849         t
850       ;; The group is no longer visible.
851       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
852              (after (cdr (member group (cdr list)))))
853         ;; First try to put point on a group after the current one.
854         (while (and after
855                     (not (gnus-group-goto-group (car after))))
856           (setq after (cdr after)))
857         ;; Then try to put point on a group before point.
858         (unless after
859           (setq after (cdr (member group (reverse (cdr list)))))
860           (while (and after
861                       (not (gnus-group-goto-group (car after))))
862             (setq after (cdr after))))
863         ;; Finally, just put point on the topic.
864         (if (not (car list))
865             (goto-char (point-min))
866           (unless after
867             (gnus-topic-goto-topic (car list))
868             (setq after nil)))
869         t))))
870
871 ;;; Topic-active functions
872
873 (defun gnus-topic-grok-active (&optional force)
874   "Parse all active groups and create topic structures for them."
875   ;; First we make sure that we have really read the active file.
876   (when (or force
877             (not gnus-topic-active-alist))
878     (let (groups)
879       ;; Get a list of all groups available.
880       (mapatoms (lambda (g) (when (symbol-value g)
881                               (push (symbol-name g) groups)))
882                 gnus-active-hashtb)
883       (setq groups (sort groups 'string<))
884       ;; Init the variables.
885       (setq gnus-topic-active-topology (list (list "" 'visible)))
886       (setq gnus-topic-active-alist nil)
887       ;; Descend the top-level hierarchy.
888       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
889       ;; Set the top-level topic names to something nice.
890       (setcar (car gnus-topic-active-topology) "Gnus active")
891       (setcar (car gnus-topic-active-alist) "Gnus active"))))
892
893 (defun gnus-topic-grok-active-1 (topology groups)
894   (let* ((name (caar topology))
895          (prefix (concat "^" (regexp-quote name)))
896          tgroups ntopology group)
897     (while (and groups
898                 (string-match prefix (setq group (car groups))))
899       (if (not (string-match "\\." group (match-end 0)))
900           ;; There are no further hierarchies here, so we just
901           ;; enter this group into the list belonging to this
902           ;; topic.
903           (push (pop groups) tgroups)
904         ;; New sub-hierarchy, so we add it to the topology.
905         (nconc topology (list (setq ntopology
906                                     (list (list (substring
907                                                  group 0 (match-end 0))
908                                                 'invisible)))))
909         ;; Descend the hierarchy.
910         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
911     ;; We remove the trailing "." from the topic name.
912     (setq name
913           (if (string-match "\\.$" name)
914               (substring name 0 (match-beginning 0))
915             name))
916     ;; Add this topic and its groups to the topic alist.
917     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
918     (setcar (car topology) name)
919     ;; We return the rest of the groups that didn't belong
920     ;; to this topic.
921     groups))
922
923 ;;; Topic mode, commands and keymap.
924
925 (defvar gnus-topic-mode-map nil)
926 (defvar gnus-group-topic-map nil)
927
928 (unless gnus-topic-mode-map
929   (setq gnus-topic-mode-map (make-sparse-keymap))
930
931   ;; Override certain group mode keys.
932   (gnus-define-keys gnus-topic-mode-map
933     "=" gnus-topic-select-group
934     "\r" gnus-topic-select-group
935     " " gnus-topic-read-group
936     "\C-c\C-x" gnus-topic-expire-articles
937     "\C-k" gnus-topic-kill-group
938     "\C-y" gnus-topic-yank-group
939     "\M-g" gnus-topic-get-new-news-this-topic
940     "AT" gnus-topic-list-active
941     "Gp" gnus-topic-edit-parameters
942     "#" gnus-topic-mark-topic
943     "\M-#" gnus-topic-unmark-topic
944     [tab] gnus-topic-indent
945     [(meta tab)] gnus-topic-unindent
946     "\C-i" gnus-topic-indent
947     "\M-\C-i" gnus-topic-unindent
948     gnus-mouse-2 gnus-mouse-pick-topic)
949
950   ;; Define a new submap.
951   (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
952     "#" gnus-topic-mark-topic
953     "\M-#" gnus-topic-unmark-topic
954     "n" gnus-topic-create-topic
955     "m" gnus-topic-move-group
956     "D" gnus-topic-remove-group
957     "c" gnus-topic-copy-group
958     "h" gnus-topic-hide-topic
959     "s" gnus-topic-show-topic
960     "j" gnus-topic-jump-to-topic
961     "M" gnus-topic-move-matching
962     "C" gnus-topic-copy-matching
963     "\C-i" gnus-topic-indent
964     [tab] gnus-topic-indent
965     "r" gnus-topic-rename
966     "\177" gnus-topic-delete
967     [delete] gnus-topic-delete
968     "H" gnus-topic-toggle-display-empty-topics)
969
970   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
971     "s" gnus-topic-sort-groups
972     "a" gnus-topic-sort-groups-by-alphabet
973     "u" gnus-topic-sort-groups-by-unread
974     "l" gnus-topic-sort-groups-by-level
975     "v" gnus-topic-sort-groups-by-score
976     "r" gnus-topic-sort-groups-by-rank
977     "m" gnus-topic-sort-groups-by-method))
978
979 (defun gnus-topic-make-menu-bar ()
980   (unless (boundp 'gnus-topic-menu)
981     (easy-menu-define
982      gnus-topic-menu gnus-topic-mode-map ""
983      '("Topics"
984        ["Toggle topics" gnus-topic-mode t]
985        ("Groups"
986         ["Copy" gnus-topic-copy-group t]
987         ["Move" gnus-topic-move-group t]
988         ["Remove" gnus-topic-remove-group t]
989         ["Copy matching" gnus-topic-copy-matching t]
990         ["Move matching" gnus-topic-move-matching t])
991        ("Topics"
992         ["Goto" gnus-topic-jump-to-topic t]
993         ["Show" gnus-topic-show-topic t]
994         ["Hide" gnus-topic-hide-topic t]
995         ["Delete" gnus-topic-delete t]
996         ["Rename" gnus-topic-rename t]
997         ["Create" gnus-topic-create-topic t]
998         ["Mark" gnus-topic-mark-topic t]
999         ["Indent" gnus-topic-indent t]
1000         ["Sort" gnus-topic-sort-topics t]
1001         ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1002         ["Edit parameters" gnus-topic-edit-parameters t])
1003        ["List active" gnus-topic-list-active t]))))
1004
1005 (defun gnus-topic-mode (&optional arg redisplay)
1006   "Minor mode for topicsifying Gnus group buffers."
1007   (interactive (list current-prefix-arg t))
1008   (when (eq major-mode 'gnus-group-mode)
1009     (make-local-variable 'gnus-topic-mode)
1010     (setq gnus-topic-mode
1011           (if (null arg) (not gnus-topic-mode)
1012             (> (prefix-numeric-value arg) 0)))
1013     ;; Infest Gnus with topics.
1014     (if (not gnus-topic-mode)
1015         (setq gnus-goto-missing-group-function nil)
1016       (when (gnus-visual-p 'topic-menu 'menu)
1017         (gnus-topic-make-menu-bar))
1018       (gnus-set-format 'topic t)
1019       (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
1020       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1021       (set (make-local-variable 'gnus-group-prepare-function)
1022            'gnus-group-prepare-topics)
1023       (set (make-local-variable 'gnus-group-get-parameter-function)
1024            'gnus-group-topic-parameters)
1025       (set (make-local-variable 'gnus-group-goto-next-group-function)
1026            'gnus-topic-goto-next-group)
1027       (set (make-local-variable 'gnus-group-indentation-function)
1028            'gnus-topic-group-indentation)
1029       (set (make-local-variable 'gnus-group-update-group-function)
1030            'gnus-topic-update-topics-containing-group)
1031       (set (make-local-variable 'gnus-group-sort-alist-function)
1032            'gnus-group-sort-topic)
1033       (setq gnus-group-change-level-function 'gnus-topic-change-level)
1034       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1035       (make-local-hook 'gnus-check-bogus-groups-hook)
1036       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1037       (setq gnus-topology-checked-p nil)
1038       ;; We check the topology.
1039       (when gnus-newsrc-alist
1040         (gnus-topic-check-topology))
1041       (gnus-run-hooks 'gnus-topic-mode-hook))
1042     ;; Remove topic infestation.
1043     (unless gnus-topic-mode
1044       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1045       (remove-hook 'gnus-group-change-level-function
1046                    'gnus-topic-change-level)
1047       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1048       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1049       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1050     (when redisplay
1051       (gnus-group-list-groups))))
1052
1053 (defun gnus-topic-select-group (&optional all)
1054   "Select this newsgroup.
1055 No article is selected automatically.
1056 If ALL is non-nil, already read articles become readable.
1057 If ALL is a number, fetch this number of articles.
1058
1059 If performed over a topic line, toggle folding the topic."
1060   (interactive "P")
1061   (if (gnus-group-topic-p)
1062       (let ((gnus-group-list-mode
1063              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1064         (gnus-topic-fold all))
1065     (gnus-group-select-group all)))
1066
1067 (defun gnus-mouse-pick-topic (e)
1068   "Select the group or topic under the mouse pointer."
1069   (interactive "e")
1070   (mouse-set-point e)
1071   (gnus-topic-read-group nil))
1072
1073 (defun gnus-topic-expire-articles (topic)
1074   "Expire articles in this topic or group."
1075   (interactive (list (gnus-group-topic-name)))
1076   (if (not topic)
1077       (call-interactively 'gnus-group-expire-articles)
1078     (save-excursion
1079       (gnus-message 5 "Expiring groups in %s..." topic)
1080       (let ((gnus-group-marked
1081              (mapcar (lambda (entry) (car (nth 2 entry)))
1082                      (gnus-topic-find-groups topic gnus-level-killed t))))
1083         (gnus-group-expire-articles nil))
1084       (gnus-message 5 "Expiring groups in %s...done" topic))))
1085
1086 (defun gnus-topic-read-group (&optional all no-article group)
1087   "Read news in this newsgroup.
1088 If the prefix argument ALL is non-nil, already read articles become
1089 readable.  IF ALL is a number, fetch this number of articles.  If the
1090 optional argument NO-ARTICLE is non-nil, no article will be
1091 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1092 group.
1093
1094 If performed over a topic line, toggle folding the topic."
1095   (interactive "P")
1096   (if (gnus-group-topic-p)
1097       (let ((gnus-group-list-mode
1098              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1099         (gnus-topic-fold all))
1100     (gnus-group-read-group all no-article group)))
1101
1102 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1103   "Create a new TOPIC under PARENT.
1104 When used interactively, PARENT will be the topic under point."
1105   (interactive
1106    (list
1107     (read-string "New topic: ")
1108     (gnus-current-topic)))
1109   ;; Check whether this topic already exists.
1110   (when (gnus-topic-find-topology topic)
1111     (error "Topic already exists"))
1112   (unless parent
1113     (setq parent (caar gnus-topic-topology)))
1114   (let ((top (cdr (gnus-topic-find-topology parent)))
1115         (full-topic (or full-topic `((,topic visible)))))
1116     (unless top
1117       (error "No such parent topic: %s" parent))
1118     (if previous
1119         (progn
1120           (while (and (cdr top)
1121                       (not (equal (caaadr top) previous)))
1122             (setq top (cdr top)))
1123           (setcdr top (cons full-topic (cdr top))))
1124       (nconc top (list full-topic)))
1125     (unless (assoc topic gnus-topic-alist)
1126       (push (list topic) gnus-topic-alist)))
1127   (gnus-topic-enter-dribble)
1128   (gnus-group-list-groups)
1129   (gnus-topic-goto-topic topic))
1130
1131 (defun gnus-topic-move-group (n topic &optional copyp)
1132   "Move the next N groups to TOPIC.
1133 If COPYP, copy the groups instead."
1134   (interactive
1135    (list current-prefix-arg
1136          (completing-read "Move to topic: " gnus-topic-alist nil t)))
1137   (let ((groups (gnus-group-process-prefix n))
1138         (topicl (assoc topic gnus-topic-alist))
1139         (start-topic (gnus-group-topic-name))
1140         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1141         entry)
1142     (if (and (not groups) (not copyp) start-topic)
1143         (gnus-topic-move start-topic topic)
1144       (mapcar
1145        (lambda (g)
1146          (gnus-group-remove-mark g)
1147          (when (and
1148                 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1149                 (not copyp))
1150            (setcdr entry (gnus-delete-first g (cdr entry))))
1151          (nconc topicl (list g)))
1152        groups)
1153       (gnus-topic-enter-dribble)
1154       (if start-group
1155           (gnus-group-goto-group start-group)
1156         (gnus-topic-goto-topic start-topic))
1157       (gnus-group-list-groups))))
1158
1159 (defun gnus-topic-remove-group (&optional arg)
1160   "Remove the current group from the topic."
1161   (interactive "P")
1162   (gnus-group-iterate arg
1163     (lambda (group)
1164       (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1165             (buffer-read-only nil))
1166         (when (and topicl group)
1167           (gnus-delete-line)
1168           (gnus-delete-first group topicl))
1169         (gnus-topic-update-topic)
1170         (gnus-group-position-point)))))
1171
1172 (defun gnus-topic-copy-group (n topic)
1173   "Copy the current group to a topic."
1174   (interactive
1175    (list current-prefix-arg
1176          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1177   (gnus-topic-move-group n topic t))
1178
1179 (defun gnus-topic-kill-group (&optional n discard)
1180   "Kill the next N groups."
1181   (interactive "P")
1182   (if (gnus-group-topic-p)
1183       (let ((topic (gnus-group-topic-name)))
1184         (push (cons
1185                (gnus-topic-find-topology topic)
1186                (assoc topic gnus-topic-alist))
1187               gnus-topic-killed-topics)
1188         (gnus-topic-remove-topic nil t)
1189         (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1190         (gnus-topic-enter-dribble))
1191     (gnus-group-kill-group n discard)
1192     (gnus-topic-update-topic)))
1193
1194 (defun gnus-topic-yank-group (&optional arg)
1195   "Yank the last topic."
1196   (interactive "p")
1197   (if gnus-topic-killed-topics
1198       (let* ((previous
1199               (or (gnus-group-topic-name)
1200                   (gnus-topic-next-topic (gnus-current-topic))))
1201              (data (pop gnus-topic-killed-topics))
1202              (alist (cdr data))
1203              (item (cdar data)))
1204         (push alist gnus-topic-alist)
1205         (gnus-topic-create-topic
1206          (caar item) (gnus-topic-parent-topic previous) previous
1207          item)
1208         (gnus-topic-enter-dribble)
1209         (gnus-topic-goto-topic (caar item)))
1210     (let* ((prev (gnus-group-group-name))
1211            (gnus-topic-inhibit-change-level t)
1212            (gnus-group-indentation
1213             (make-string
1214              (* gnus-topic-indent-level
1215                 (or (save-excursion
1216                       (gnus-topic-goto-topic (gnus-current-topic))
1217                       (gnus-group-topic-level))
1218                     0))
1219              ? ))
1220            yanked alist)
1221       ;; We first yank the groups the normal way...
1222       (setq yanked (gnus-group-yank-group arg))
1223       ;; Then we enter the yanked groups into the topics they belong
1224       ;; to.
1225       (setq alist (assoc (save-excursion
1226                            (forward-line -1)
1227                            (gnus-current-topic))
1228                          gnus-topic-alist))
1229       (when (stringp yanked)
1230         (setq yanked (list yanked)))
1231       (if (not prev)
1232           (nconc alist yanked)
1233         (if (not (cdr alist))
1234             (setcdr alist (nconc yanked (cdr alist)))
1235           (while (cdr alist)
1236             (when (equal (cadr alist) prev)
1237               (setcdr alist (nconc yanked (cdr alist)))
1238               (setq alist nil))
1239             (setq alist (cdr alist))))))
1240     (gnus-topic-update-topic)))
1241
1242 (defun gnus-topic-hide-topic ()
1243   "Hide the current topic."
1244   (interactive)
1245   (when (gnus-current-topic)
1246     (gnus-topic-goto-topic (gnus-current-topic))
1247     (gnus-topic-remove-topic nil nil 'hidden)))
1248
1249 (defun gnus-topic-show-topic ()
1250   "Show the hidden topic."
1251   (interactive)
1252   (when (gnus-group-topic-p)
1253     (gnus-topic-remove-topic t nil 'shown)))
1254
1255 (defun gnus-topic-mark-topic (topic &optional unmark)
1256   "Mark all groups in the topic with the process mark."
1257   (interactive (list (gnus-group-topic-name)))
1258   (if (not topic)
1259       (call-interactively 'gnus-group-mark-group)
1260     (save-excursion
1261       (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
1262         (while groups
1263           (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1264                    (gnus-info-group (nth 2 (pop groups)))))))))
1265
1266 (defun gnus-topic-unmark-topic (topic &optional unmark)
1267   "Remove the process mark from all groups in the topic."
1268   (interactive (list (gnus-group-topic-name)))
1269   (if (not topic)
1270       (call-interactively 'gnus-group-unmark-group)
1271     (gnus-topic-mark-topic topic t)))
1272
1273 (defun gnus-topic-get-new-news-this-topic (&optional n)
1274   "Check for new news in the current topic."
1275   (interactive "P")
1276   (if (not (gnus-group-topic-p))
1277       (gnus-group-get-new-news-this-group n)
1278     (gnus-topic-mark-topic (gnus-group-topic-name))
1279     (gnus-group-get-new-news-this-group)))
1280
1281 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1282   "Move all groups that match REGEXP to some topic."
1283   (interactive
1284    (let (topic)
1285      (nreverse
1286       (list
1287        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1288        (read-string (format "Move to %s (regexp): " topic))))))
1289   (gnus-group-mark-regexp regexp)
1290   (gnus-topic-move-group nil topic copyp))
1291
1292 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1293   "Copy all groups that match REGEXP to some topic."
1294   (interactive
1295    (let (topic)
1296      (nreverse
1297       (list
1298        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1299        (read-string (format "Copy to %s (regexp): " topic))))))
1300   (gnus-topic-move-matching regexp topic t))
1301
1302 (defun gnus-topic-delete (topic)
1303   "Delete a topic."
1304   (interactive (list (gnus-group-topic-name)))
1305   (unless topic
1306     (error "No topic to be deleted"))
1307   (let ((entry (assoc topic gnus-topic-alist))
1308         (buffer-read-only nil))
1309     (when (cdr entry)
1310       (error "Topic not empty"))
1311     ;; Delete if visible.
1312     (when (gnus-topic-goto-topic topic)
1313       (gnus-delete-line))
1314     ;; Remove from alist.
1315     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1316     ;; Remove from topology.
1317     (gnus-topic-find-topology topic nil nil 'delete)
1318     (gnus-dribble-touch)))
1319
1320 (defun gnus-topic-rename (old-name new-name)
1321   "Rename a topic."
1322   (interactive
1323    (let ((topic (gnus-current-topic)))
1324      (list topic
1325            (read-string (format "Rename %s to: " topic)))))
1326   ;; Check whether the new name exists.
1327   (when (gnus-topic-find-topology new-name)
1328     (error "Topic '%s' already exists" new-name))
1329   ;; "nil" is an invalid name, for reasons I'd rather not go
1330   ;; into here.  Trust me.
1331   (when (equal new-name "nil")
1332     (error "Invalid name: %s" nil))
1333   ;; Do the renaming.
1334   (let ((top (gnus-topic-find-topology old-name))
1335         (entry (assoc old-name gnus-topic-alist)))
1336     (when top
1337       (setcar (cadr top) new-name))
1338     (when entry
1339       (setcar entry new-name))
1340     (forward-line -1)
1341     (gnus-dribble-touch)
1342     (gnus-group-list-groups)
1343     (forward-line 1)))
1344
1345 (defun gnus-topic-indent (&optional unindent)
1346   "Indent a topic -- make it a sub-topic of the previous topic.
1347 If UNINDENT, remove an indentation."
1348   (interactive "P")
1349   (if unindent
1350       (gnus-topic-unindent)
1351     (let* ((topic (gnus-current-topic))
1352            (parent (gnus-topic-previous-topic topic))
1353            (buffer-read-only nil))
1354       (unless parent
1355         (error "Nothing to indent %s into" topic))
1356       (when topic
1357         (gnus-topic-goto-topic topic)
1358         (gnus-topic-kill-group)
1359         (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1360         (gnus-topic-create-topic
1361          topic parent nil (cdaar gnus-topic-killed-topics))
1362         (pop gnus-topic-killed-topics)
1363         (or (gnus-topic-goto-topic topic)
1364             (gnus-topic-goto-topic parent))))))
1365
1366 (defun gnus-topic-unindent ()
1367   "Unindent a topic."
1368   (interactive)
1369   (let* ((topic (gnus-current-topic))
1370          (parent (gnus-topic-parent-topic topic))
1371          (grandparent (gnus-topic-parent-topic parent)))
1372     (unless grandparent
1373       (error "Nothing to indent %s into" topic))
1374     (when topic
1375       (gnus-topic-goto-topic topic)
1376       (gnus-topic-kill-group)
1377       (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1378       (gnus-topic-create-topic
1379        topic grandparent (gnus-topic-next-topic parent)
1380        (cdaar gnus-topic-killed-topics))
1381       (pop gnus-topic-killed-topics)
1382       (gnus-topic-goto-topic topic))))
1383
1384 (defun gnus-topic-list-active (&optional force)
1385   "List all groups that Gnus knows about in a topicsified fashion.
1386 If FORCE, always re-read the active file."
1387   (interactive "P")
1388   (when force
1389     (gnus-get-killed-groups))
1390   (gnus-topic-grok-active force)
1391   (let ((gnus-topic-topology gnus-topic-active-topology)
1392         (gnus-topic-alist gnus-topic-active-alist)
1393         gnus-killed-list gnus-zombie-list)
1394     (gnus-group-list-groups gnus-level-killed nil 1)))
1395
1396 (defun gnus-topic-toggle-display-empty-topics ()
1397   "Show/hide topics that have no unread articles."
1398   (interactive)
1399   (setq gnus-topic-display-empty-topics
1400         (not gnus-topic-display-empty-topics))
1401   (gnus-group-list-groups)
1402   (message "%s empty topics"
1403            (if gnus-topic-display-empty-topics
1404                "Showing" "Hiding")))
1405
1406 ;;; Topic sorting functions
1407
1408 (defun gnus-topic-edit-parameters (group)
1409   "Edit the group parameters of GROUP.
1410 If performed on a topic, edit the topic parameters instead."
1411   (interactive (list (gnus-group-group-name)))
1412   (if group
1413       (gnus-group-edit-group-parameters group)
1414     (if (not (gnus-group-topic-p))
1415         (error "Nothing to edit on the current line")
1416       (let ((topic (gnus-group-topic-name)))
1417         (gnus-edit-form
1418          (gnus-topic-parameters topic)
1419          (format "Editing the topic parameters for `%s'."
1420                  (or group topic))
1421          `(lambda (form)
1422             (gnus-topic-set-parameters ,topic form)))))))
1423
1424 (defun gnus-group-sort-topic (func reverse)
1425   "Sort groups in the topics according to FUNC and REVERSE."
1426   (let ((alist gnus-topic-alist))
1427     (while alist
1428       ;; !!!Sometimes nil elements sneak into the alist,
1429       ;; for some reason or other.
1430       (setcar alist (delq nil (car alist)))
1431       (setcar alist (delete "dummy.group" (car alist)))
1432       (gnus-topic-sort-topic (pop alist) func reverse))))
1433
1434 (defun gnus-topic-sort-topic (topic func reverse)
1435   ;; Each topic only lists the name of the group, while
1436   ;; the sort predicates expect group infos as inputs.
1437   ;; So we first transform the group names into infos,
1438   ;; then sort, and then transform back into group names.
1439   (setcdr
1440    topic
1441    (mapcar
1442     (lambda (info) (gnus-info-group info))
1443     (sort
1444      (mapcar
1445       (lambda (group) (gnus-get-info group))
1446       (cdr topic))
1447      func)))
1448   ;; Do the reversal, if necessary.
1449   (when reverse
1450     (setcdr topic (nreverse (cdr topic)))))
1451
1452 (defun gnus-topic-sort-groups (func &optional reverse)
1453   "Sort the current topic according to FUNC.
1454 If REVERSE, reverse the sorting order."
1455   (interactive (list gnus-group-sort-function current-prefix-arg))
1456   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1457     (gnus-topic-sort-topic
1458      topic (gnus-make-sort-function func) reverse)
1459     (gnus-group-list-groups)))
1460
1461 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1462   "Sort the current topic alphabetically by group name.
1463 If REVERSE, sort in reverse order."
1464   (interactive "P")
1465   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1466
1467 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1468   "Sort the current topic by number of unread articles.
1469 If REVERSE, sort in reverse order."
1470   (interactive "P")
1471   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1472
1473 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1474   "Sort the current topic by group level.
1475 If REVERSE, sort in reverse order."
1476   (interactive "P")
1477   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1478
1479 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1480   "Sort the current topic by group score.
1481 If REVERSE, sort in reverse order."
1482   (interactive "P")
1483   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1484
1485 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1486   "Sort the current topic by group rank.
1487 If REVERSE, sort in reverse order."
1488   (interactive "P")
1489   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1490
1491 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1492   "Sort the current topic alphabetically by backend name.
1493 If REVERSE, sort in reverse order."
1494   (interactive "P")
1495   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1496
1497 (defun gnus-topic-sort-topics-1 (top reverse)
1498   (if (cdr top)
1499       (let ((subtop
1500              (mapcar `(lambda (top)
1501                         (gnus-topic-sort-topics-1 top ,reverse))
1502                      (sort (cdr top)
1503                            '(lambda (t1 t2) 
1504                               (string-lessp (caar t1) (caar t2)))))))
1505         (setcdr top (if reverse (reverse subtop) subtop))))
1506   top)
1507
1508 (defun gnus-topic-sort-topics (&optional topic reverse)
1509   "Sort topics in TOPIC alphabeticaly by topic name.
1510 If REVERSE, reverse the sorting order."
1511   (interactive 
1512    (list (completing-read "Sort topics in : " gnus-topic-alist nil t 
1513                           (gnus-current-topic))
1514          current-prefix-arg))
1515   (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1516                             gnus-topic-topology)))
1517     (gnus-topic-sort-topics-1 topic-topology reverse)
1518     (gnus-topic-enter-dribble)
1519     (gnus-group-list-groups)
1520     (gnus-topic-goto-topic topic)))
1521
1522 (defun gnus-topic-move (current to)
1523   "Move the CURRENT topic to TO."
1524   (interactive 
1525    (list 
1526     (gnus-group-topic-name)
1527     (completing-read "Move to topic: " gnus-topic-alist nil t)))
1528   (unless (and current to)
1529     (error "Can't find topic"))
1530   (let ((current-top (cdr (gnus-topic-find-topology current)))
1531         (to-top (cdr (gnus-topic-find-topology to))))
1532     (unless current-top
1533       (error "Can't find topic `%s'" current))
1534     (unless to-top
1535       (error "Can't find topic `%s'" to))
1536     (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1537         (error "Can't move `%s' to its sub-level" current))
1538     (gnus-topic-find-topology current nil nil 'delete)
1539     (while (cdr to-top)
1540       (setq to-top (cdr to-top)))
1541     (setcdr to-top (list current-top))
1542     (gnus-topic-enter-dribble)
1543     (gnus-group-list-groups)
1544     (gnus-topic-goto-topic current)))
1545
1546 (defun gnus-subscribe-topics (newsgroup)
1547   (catch 'end
1548     (let (match gnus-group-change-level-function)
1549       (dolist (topic (gnus-topic-list))
1550         (when (and (setq match (cdr (assq 'subscribe
1551                                           (gnus-topic-parameters topic))))
1552                    (string-match match newsgroup))
1553           ;; Just subscribe the group.
1554           (gnus-subscribe-alphabetically newsgroup)
1555           ;; Add the group to the topic.
1556           (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1557           (throw 'end t))))))
1558           
1559 (provide 'gnus-topic)
1560
1561 ;;; gnus-topic.el ends here