* gnus-srvr.el (gnus-server-kill-server): Offer to kill all the
[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          entry)
599     ;; Try to jump to a visible group.
600     (while (and g (not (gnus-group-goto-group (car g) t)))
601       (pop g))
602     ;; It wasn't visible, so we try to see where to insert it.
603     (when (not g)
604       (setq g (cdr (member group (reverse groups))))
605       (while (and g unfound)
606         (when (gnus-group-goto-group (pop g) t)
607           (forward-line 1)
608           (setq unfound nil)))
609       (when (and unfound
610                  topic
611                  (not (gnus-topic-goto-missing-topic topic)))
612         (let* ((top (gnus-topic-find-topology topic))
613                (children (cddr top))
614                (type (cadr top))
615                (unread 0)
616                (entries (gnus-topic-find-groups
617                          (car type) (car gnus-group-list-mode)
618                          (cdr gnus-group-list-mode))))
619           (while children
620             (incf unread (gnus-topic-unread (caar (pop children)))))
621           (while (setq entry (pop entries))
622             (when (numberp (car entry))
623               (incf unread (car entry))))
624           (gnus-topic-insert-topic-line
625            topic t t (car (gnus-topic-find-topology topic)) nil unread))))))
626
627 (defun gnus-topic-goto-missing-topic (topic)
628   (if (gnus-topic-goto-topic topic)
629       (forward-line 1)
630     ;; Topic not displayed.
631     (let* ((top (gnus-topic-find-topology
632                  (gnus-topic-parent-topic topic)))
633            (tp (reverse (cddr top))))
634       (if (not top)
635           (gnus-topic-insert-topic-line
636            topic t t (car (gnus-topic-find-topology topic)) nil 0)
637         (while (not (equal (caaar tp) topic))
638           (setq tp (cdr tp)))
639         (pop tp)
640         (while (and tp
641                     (not (gnus-topic-goto-topic (caaar tp))))
642           (pop tp))
643         (if tp
644             (gnus-topic-forward-topic 1)
645           (gnus-topic-goto-missing-topic (caadr top)))))
646     nil))
647
648 (defun gnus-topic-update-topic-line (topic-name &optional reads)
649   (let* ((top (gnus-topic-find-topology topic-name))
650          (type (cadr top))
651          (children (cddr top))
652          (entries (gnus-topic-find-groups
653                    (car type) (car gnus-group-list-mode)
654                    (cdr gnus-group-list-mode)))
655          (parent (gnus-topic-parent-topic topic-name))
656          (all-entries entries)
657          (unread 0)
658          old-unread entry new-unread)
659     (when (gnus-topic-goto-topic (car type))
660       ;; Tally all the groups that belong in this topic.
661       (if reads
662           (setq unread (- (gnus-group-topic-unread) reads))
663         (while children
664           (incf unread (gnus-topic-unread (caar (pop children)))))
665         (while (setq entry (pop entries))
666           (when (numberp (car entry))
667             (incf unread (car entry)))))
668       (setq old-unread (gnus-group-topic-unread))
669       ;; Insert the topic line.
670       (gnus-topic-insert-topic-line
671        (car type) (gnus-topic-visible-p)
672        (not (eq (nth 2 type) 'hidden))
673        (gnus-group-topic-level) all-entries unread)
674       (gnus-delete-line)
675       (forward-line -1)
676       (setq new-unread (gnus-group-topic-unread)))
677     (when parent
678       (forward-line -1)
679       (gnus-topic-update-topic-line
680        parent
681        (- (or old-unread 0) (or new-unread 0))))
682     unread))
683
684 (defun gnus-topic-group-indentation ()
685   (make-string
686    (* gnus-topic-indent-level
687       (or (save-excursion
688             (forward-line -1)
689             (gnus-topic-goto-topic (gnus-current-topic))
690             (gnus-group-topic-level))
691           0))
692    ? ))
693
694 ;;; Initialization
695
696 (gnus-add-shutdown 'gnus-topic-close 'gnus)
697
698 (defun gnus-topic-close ()
699   (setq gnus-topic-active-topology nil
700         gnus-topic-active-alist nil
701         gnus-topic-killed-topics nil
702         gnus-topology-checked-p nil))
703
704 (defun gnus-topic-check-topology ()
705   ;; The first time we set the topology to whatever we have
706   ;; gotten here, which can be rather random.
707   (unless gnus-topic-alist
708     (gnus-topic-init-alist))
709
710   (setq gnus-topology-checked-p t)
711   ;; Go through the topic alist and make sure that all topics
712   ;; are in the topic topology.
713   (let ((topics (gnus-topic-list))
714         (alist gnus-topic-alist)
715         changed)
716     (while alist
717       (unless (member (caar alist) topics)
718         (nconc gnus-topic-topology
719                (list (list (list (caar alist) 'visible))))
720         (setq changed t))
721       (setq alist (cdr alist)))
722     (when changed
723       (gnus-topic-enter-dribble))
724     ;; Conversely, go through the topology and make sure that all
725     ;; topologies have alists.
726     (while topics
727       (unless (assoc (car topics) gnus-topic-alist)
728         (push (list (car topics)) gnus-topic-alist))
729       (pop topics)))
730   ;; Go through all living groups and make sure that
731   ;; they belong to some topic.
732   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
733                                          gnus-topic-alist)))
734          (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
735          (newsrc (cdr gnus-newsrc-alist))
736          group)
737     (while newsrc
738       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
739         (setcdr entry (list group))
740         (setq entry (cdr entry)))))
741   ;; Go through all topics and make sure they contain only living groups.
742   (let ((alist gnus-topic-alist)
743         topic)
744     (while (setq topic (pop alist))
745       (while (cdr topic)
746         (if (and (cadr topic)
747                  (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
748             (setq topic (cdr topic))
749           (setcdr topic (cddr topic)))))))
750
751 (defun gnus-topic-init-alist ()
752   "Initialize the topic structures."
753   (setq gnus-topic-topology
754         (cons (list "Gnus" 'visible)
755               (mapcar (lambda (topic)
756                         (list (list (car topic) 'visible)))
757                       '(("misc")))))
758   (setq gnus-topic-alist
759         (list (cons "misc"
760                     (mapcar (lambda (info) (gnus-info-group info))
761                             (cdr gnus-newsrc-alist)))
762               (list "Gnus")))
763   (gnus-topic-enter-dribble))
764
765 ;;; Maintenance
766
767 (defun gnus-topic-clean-alist ()
768   "Remove bogus groups from the topic alist."
769   (let ((topic-alist gnus-topic-alist)
770         result topic)
771     (unless gnus-killed-hashtb
772       (gnus-make-hashtable-from-killed))
773     (while (setq topic (pop topic-alist))
774       (let ((topic-name (pop topic))
775             group filtered-topic)
776         (while (setq group (pop topic))
777           (when (and (or (gnus-gethash group gnus-active-hashtb)
778                          (gnus-info-method (gnus-get-info group)))
779                      (not (gnus-gethash group gnus-killed-hashtb)))
780             (push group filtered-topic)))
781         (push (cons topic-name (nreverse filtered-topic)) result)))
782     (setq gnus-topic-alist (nreverse result))))
783
784 (defun gnus-topic-change-level (group level oldlevel &optional previous)
785   "Run when changing levels to enter/remove groups from topics."
786   (save-excursion
787     (set-buffer gnus-group-buffer)
788     (let ((buffer-read-only nil))
789       (unless gnus-topic-inhibit-change-level
790         (gnus-group-goto-group (or (car (nth 2 previous)) group))
791         (when (and gnus-topic-mode
792                    gnus-topic-alist
793                    (not gnus-topic-inhibit-change-level))
794           ;; Remove the group from the topics.
795           (if (and (< oldlevel gnus-level-zombie)
796                    (>= level gnus-level-zombie))
797               (let ((alist gnus-topic-alist))
798                 (while (gnus-group-goto-group group)
799                   (gnus-delete-line))
800                 (while alist
801                   (when (member group (car alist))
802                     (setcdr (car alist) (delete group (cdar alist))))
803                   (pop alist)))
804             ;; If the group is subscribed we enter it into the topics.
805             (when (and (< level gnus-level-zombie)
806                        (>= oldlevel gnus-level-zombie))
807               (let* ((prev (gnus-group-group-name))
808                      (gnus-topic-inhibit-change-level t)
809                      (gnus-group-indentation
810                       (make-string
811                        (* gnus-topic-indent-level
812                           (or (save-excursion
813                                 (gnus-topic-goto-topic (gnus-current-topic))
814                                 (gnus-group-topic-level))
815                               0))
816                        ? ))
817                      (yanked (list group))
818                      alist talist end)
819                 ;; Then we enter the yanked groups into the topics they belong
820                 ;; to.
821                 (when (setq alist (assoc (save-excursion
822                                            (forward-line -1)
823                                            (or
824                                             (gnus-current-topic)
825                                             (caar gnus-topic-topology)))
826                                          gnus-topic-alist))
827                   (setq talist alist)
828                   (when (stringp yanked)
829                     (setq yanked (list yanked)))
830                   (if (not prev)
831                       (nconc alist yanked)
832                     (if (not (cdr alist))
833                         (setcdr alist (nconc yanked (cdr alist)))
834                       (while (and (not end) (cdr alist))
835                         (when (equal (cadr alist) prev)
836                           (setcdr alist (nconc yanked (cdr alist)))
837                           (setq end t))
838                         (setq alist (cdr alist)))
839                       (unless end
840                         (nconc talist yanked))))))
841               (gnus-topic-update-topic))))))))
842
843 (defun gnus-topic-goto-next-group (group props)
844   "Go to group or the next group after group."
845   (if (not group)
846       (if (not (memq 'gnus-topic props))
847           (goto-char (point-max))
848         (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
849     (if (gnus-group-goto-group group)
850         t
851       ;; The group is no longer visible.
852       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
853              (after (cdr (member group (cdr list)))))
854         ;; First try to put point on a group after the current one.
855         (while (and after
856                     (not (gnus-group-goto-group (car after))))
857           (setq after (cdr after)))
858         ;; Then try to put point on a group before point.
859         (unless after
860           (setq after (cdr (member group (reverse (cdr list)))))
861           (while (and after
862                       (not (gnus-group-goto-group (car after))))
863             (setq after (cdr after))))
864         ;; Finally, just put point on the topic.
865         (if (not (car list))
866             (goto-char (point-min))
867           (unless after
868             (gnus-topic-goto-topic (car list))
869             (setq after nil)))
870         t))))
871
872 ;;; Topic-active functions
873
874 (defun gnus-topic-grok-active (&optional force)
875   "Parse all active groups and create topic structures for them."
876   ;; First we make sure that we have really read the active file.
877   (when (or force
878             (not gnus-topic-active-alist))
879     (let (groups)
880       ;; Get a list of all groups available.
881       (mapatoms (lambda (g) (when (symbol-value g)
882                               (push (symbol-name g) groups)))
883                 gnus-active-hashtb)
884       (setq groups (sort groups 'string<))
885       ;; Init the variables.
886       (setq gnus-topic-active-topology (list (list "" 'visible)))
887       (setq gnus-topic-active-alist nil)
888       ;; Descend the top-level hierarchy.
889       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
890       ;; Set the top-level topic names to something nice.
891       (setcar (car gnus-topic-active-topology) "Gnus active")
892       (setcar (car gnus-topic-active-alist) "Gnus active"))))
893
894 (defun gnus-topic-grok-active-1 (topology groups)
895   (let* ((name (caar topology))
896          (prefix (concat "^" (regexp-quote name)))
897          tgroups ntopology group)
898     (while (and groups
899                 (string-match prefix (setq group (car groups))))
900       (if (not (string-match "\\." group (match-end 0)))
901           ;; There are no further hierarchies here, so we just
902           ;; enter this group into the list belonging to this
903           ;; topic.
904           (push (pop groups) tgroups)
905         ;; New sub-hierarchy, so we add it to the topology.
906         (nconc topology (list (setq ntopology
907                                     (list (list (substring
908                                                  group 0 (match-end 0))
909                                                 'invisible)))))
910         ;; Descend the hierarchy.
911         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
912     ;; We remove the trailing "." from the topic name.
913     (setq name
914           (if (string-match "\\.$" name)
915               (substring name 0 (match-beginning 0))
916             name))
917     ;; Add this topic and its groups to the topic alist.
918     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
919     (setcar (car topology) name)
920     ;; We return the rest of the groups that didn't belong
921     ;; to this topic.
922     groups))
923
924 ;;; Topic mode, commands and keymap.
925
926 (defvar gnus-topic-mode-map nil)
927 (defvar gnus-group-topic-map nil)
928
929 (unless gnus-topic-mode-map
930   (setq gnus-topic-mode-map (make-sparse-keymap))
931
932   ;; Override certain group mode keys.
933   (gnus-define-keys gnus-topic-mode-map
934     "=" gnus-topic-select-group
935     "\r" gnus-topic-select-group
936     " " gnus-topic-read-group
937     "\C-c\C-x" gnus-topic-expire-articles
938     "\C-k" gnus-topic-kill-group
939     "\C-y" gnus-topic-yank-group
940     "\M-g" gnus-topic-get-new-news-this-topic
941     "AT" gnus-topic-list-active
942     "Gp" gnus-topic-edit-parameters
943     "#" gnus-topic-mark-topic
944     "\M-#" gnus-topic-unmark-topic
945     [tab] gnus-topic-indent
946     [(meta tab)] gnus-topic-unindent
947     "\C-i" gnus-topic-indent
948     "\M-\C-i" gnus-topic-unindent
949     gnus-mouse-2 gnus-mouse-pick-topic)
950
951   ;; Define a new submap.
952   (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
953     "#" gnus-topic-mark-topic
954     "\M-#" gnus-topic-unmark-topic
955     "n" gnus-topic-create-topic
956     "m" gnus-topic-move-group
957     "D" gnus-topic-remove-group
958     "c" gnus-topic-copy-group
959     "h" gnus-topic-hide-topic
960     "s" gnus-topic-show-topic
961     "j" gnus-topic-jump-to-topic
962     "M" gnus-topic-move-matching
963     "C" gnus-topic-copy-matching
964     "\C-i" gnus-topic-indent
965     [tab] gnus-topic-indent
966     "r" gnus-topic-rename
967     "\177" gnus-topic-delete
968     [delete] gnus-topic-delete
969     "H" gnus-topic-toggle-display-empty-topics)
970
971   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
972     "s" gnus-topic-sort-groups
973     "a" gnus-topic-sort-groups-by-alphabet
974     "u" gnus-topic-sort-groups-by-unread
975     "l" gnus-topic-sort-groups-by-level
976     "v" gnus-topic-sort-groups-by-score
977     "r" gnus-topic-sort-groups-by-rank
978     "m" gnus-topic-sort-groups-by-method))
979
980 (defun gnus-topic-make-menu-bar ()
981   (unless (boundp 'gnus-topic-menu)
982     (easy-menu-define
983      gnus-topic-menu gnus-topic-mode-map ""
984      '("Topics"
985        ["Toggle topics" gnus-topic-mode t]
986        ("Groups"
987         ["Copy" gnus-topic-copy-group t]
988         ["Move" gnus-topic-move-group t]
989         ["Remove" gnus-topic-remove-group t]
990         ["Copy matching" gnus-topic-copy-matching t]
991         ["Move matching" gnus-topic-move-matching t])
992        ("Topics"
993         ["Goto" gnus-topic-jump-to-topic t]
994         ["Show" gnus-topic-show-topic t]
995         ["Hide" gnus-topic-hide-topic t]
996         ["Delete" gnus-topic-delete t]
997         ["Rename" gnus-topic-rename t]
998         ["Create" gnus-topic-create-topic t]
999         ["Mark" gnus-topic-mark-topic t]
1000         ["Indent" gnus-topic-indent t]
1001         ["Sort" gnus-topic-sort-topics t]
1002         ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1003         ["Edit parameters" gnus-topic-edit-parameters t])
1004        ["List active" gnus-topic-list-active t]))))
1005
1006 (defun gnus-topic-mode (&optional arg redisplay)
1007   "Minor mode for topicsifying Gnus group buffers."
1008   (interactive (list current-prefix-arg t))
1009   (when (eq major-mode 'gnus-group-mode)
1010     (make-local-variable 'gnus-topic-mode)
1011     (setq gnus-topic-mode
1012           (if (null arg) (not gnus-topic-mode)
1013             (> (prefix-numeric-value arg) 0)))
1014     ;; Infest Gnus with topics.
1015     (if (not gnus-topic-mode)
1016         (setq gnus-goto-missing-group-function nil)
1017       (when (gnus-visual-p 'topic-menu 'menu)
1018         (gnus-topic-make-menu-bar))
1019       (gnus-set-format 'topic t)
1020       (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
1021       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1022       (set (make-local-variable 'gnus-group-prepare-function)
1023            'gnus-group-prepare-topics)
1024       (set (make-local-variable 'gnus-group-get-parameter-function)
1025            'gnus-group-topic-parameters)
1026       (set (make-local-variable 'gnus-group-goto-next-group-function)
1027            'gnus-topic-goto-next-group)
1028       (set (make-local-variable 'gnus-group-indentation-function)
1029            'gnus-topic-group-indentation)
1030       (set (make-local-variable 'gnus-group-update-group-function)
1031            'gnus-topic-update-topics-containing-group)
1032       (set (make-local-variable 'gnus-group-sort-alist-function)
1033            'gnus-group-sort-topic)
1034       (setq gnus-group-change-level-function 'gnus-topic-change-level)
1035       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1036       (make-local-hook 'gnus-check-bogus-groups-hook)
1037       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1038       (setq gnus-topology-checked-p nil)
1039       ;; We check the topology.
1040       (when gnus-newsrc-alist
1041         (gnus-topic-check-topology))
1042       (gnus-run-hooks 'gnus-topic-mode-hook))
1043     ;; Remove topic infestation.
1044     (unless gnus-topic-mode
1045       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1046       (remove-hook 'gnus-group-change-level-function
1047                    'gnus-topic-change-level)
1048       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1049       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1050       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1051     (when redisplay
1052       (gnus-group-list-groups))))
1053
1054 (defun gnus-topic-select-group (&optional all)
1055   "Select this newsgroup.
1056 No article is selected automatically.
1057 If ALL is non-nil, already read articles become readable.
1058 If ALL is a number, fetch this number of articles.
1059
1060 If performed over a topic line, toggle folding the topic."
1061   (interactive "P")
1062   (if (gnus-group-topic-p)
1063       (let ((gnus-group-list-mode
1064              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1065         (gnus-topic-fold all))
1066     (gnus-group-select-group all)))
1067
1068 (defun gnus-mouse-pick-topic (e)
1069   "Select the group or topic under the mouse pointer."
1070   (interactive "e")
1071   (mouse-set-point e)
1072   (gnus-topic-read-group nil))
1073
1074 (defun gnus-topic-expire-articles (topic)
1075   "Expire articles in this topic or group."
1076   (interactive (list (gnus-group-topic-name)))
1077   (if (not topic)
1078       (call-interactively 'gnus-group-expire-articles)
1079     (save-excursion
1080       (gnus-message 5 "Expiring groups in %s..." topic)
1081       (let ((gnus-group-marked
1082              (mapcar (lambda (entry) (car (nth 2 entry)))
1083                      (gnus-topic-find-groups topic gnus-level-killed t))))
1084         (gnus-group-expire-articles nil))
1085       (gnus-message 5 "Expiring groups in %s...done" topic))))
1086
1087 (defun gnus-topic-read-group (&optional all no-article group)
1088   "Read news in this newsgroup.
1089 If the prefix argument ALL is non-nil, already read articles become
1090 readable.  IF ALL is a number, fetch this number of articles.  If the
1091 optional argument NO-ARTICLE is non-nil, no article will be
1092 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1093 group.
1094
1095 If performed over a topic line, toggle folding the topic."
1096   (interactive "P")
1097   (if (gnus-group-topic-p)
1098       (let ((gnus-group-list-mode
1099              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1100         (gnus-topic-fold all))
1101     (gnus-group-read-group all no-article group)))
1102
1103 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1104   "Create a new TOPIC under PARENT.
1105 When used interactively, PARENT will be the topic under point."
1106   (interactive
1107    (list
1108     (read-string "New topic: ")
1109     (gnus-current-topic)))
1110   ;; Check whether this topic already exists.
1111   (when (gnus-topic-find-topology topic)
1112     (error "Topic already exists"))
1113   (unless parent
1114     (setq parent (caar gnus-topic-topology)))
1115   (let ((top (cdr (gnus-topic-find-topology parent)))
1116         (full-topic (or full-topic `((,topic visible)))))
1117     (unless top
1118       (error "No such parent topic: %s" parent))
1119     (if previous
1120         (progn
1121           (while (and (cdr top)
1122                       (not (equal (caaadr top) previous)))
1123             (setq top (cdr top)))
1124           (setcdr top (cons full-topic (cdr top))))
1125       (nconc top (list full-topic)))
1126     (unless (assoc topic gnus-topic-alist)
1127       (push (list topic) gnus-topic-alist)))
1128   (gnus-topic-enter-dribble)
1129   (gnus-group-list-groups)
1130   (gnus-topic-goto-topic topic))
1131
1132 (defun gnus-topic-move-group (n topic &optional copyp)
1133   "Move the next N groups to TOPIC.
1134 If COPYP, copy the groups instead."
1135   (interactive
1136    (list current-prefix-arg
1137          (completing-read "Move to topic: " gnus-topic-alist nil t)))
1138   (let ((groups (gnus-group-process-prefix n))
1139         (topicl (assoc topic gnus-topic-alist))
1140         (start-topic (gnus-group-topic-name))
1141         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1142         entry)
1143     (if (and (not groups) (not copyp) start-topic)
1144         (gnus-topic-move start-topic topic)
1145       (mapcar
1146        (lambda (g)
1147          (gnus-group-remove-mark g)
1148          (when (and
1149                 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1150                 (not copyp))
1151            (setcdr entry (gnus-delete-first g (cdr entry))))
1152          (nconc topicl (list g)))
1153        groups)
1154       (gnus-topic-enter-dribble)
1155       (if start-group
1156           (gnus-group-goto-group start-group)
1157         (gnus-topic-goto-topic start-topic))
1158       (gnus-group-list-groups))))
1159
1160 (defun gnus-topic-remove-group (&optional arg)
1161   "Remove the current group from the topic."
1162   (interactive "P")
1163   (gnus-group-iterate arg
1164     (lambda (group)
1165       (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1166             (buffer-read-only nil))
1167         (when (and topicl group)
1168           (gnus-delete-line)
1169           (gnus-delete-first group topicl))
1170         (gnus-topic-update-topic)
1171         (gnus-group-position-point)))))
1172
1173 (defun gnus-topic-copy-group (n topic)
1174   "Copy the current group to a topic."
1175   (interactive
1176    (list current-prefix-arg
1177          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1178   (gnus-topic-move-group n topic t))
1179
1180 (defun gnus-topic-kill-group (&optional n discard)
1181   "Kill the next N groups."
1182   (interactive "P")
1183   (if (gnus-group-topic-p)
1184       (let ((topic (gnus-group-topic-name)))
1185         (push (cons
1186                (gnus-topic-find-topology topic)
1187                (assoc topic gnus-topic-alist))
1188               gnus-topic-killed-topics)
1189         (gnus-topic-remove-topic nil t)
1190         (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1191         (gnus-topic-enter-dribble))
1192     (gnus-group-kill-group n discard)
1193     (gnus-topic-update-topic)))
1194
1195 (defun gnus-topic-yank-group (&optional arg)
1196   "Yank the last topic."
1197   (interactive "p")
1198   (if gnus-topic-killed-topics
1199       (let* ((previous
1200               (or (gnus-group-topic-name)
1201                   (gnus-topic-next-topic (gnus-current-topic))))
1202              (data (pop gnus-topic-killed-topics))
1203              (alist (cdr data))
1204              (item (cdar data)))
1205         (push alist gnus-topic-alist)
1206         (gnus-topic-create-topic
1207          (caar item) (gnus-topic-parent-topic previous) previous
1208          item)
1209         (gnus-topic-enter-dribble)
1210         (gnus-topic-goto-topic (caar item)))
1211     (let* ((prev (gnus-group-group-name))
1212            (gnus-topic-inhibit-change-level t)
1213            (gnus-group-indentation
1214             (make-string
1215              (* gnus-topic-indent-level
1216                 (or (save-excursion
1217                       (gnus-topic-goto-topic (gnus-current-topic))
1218                       (gnus-group-topic-level))
1219                     0))
1220              ? ))
1221            yanked alist)
1222       ;; We first yank the groups the normal way...
1223       (setq yanked (gnus-group-yank-group arg))
1224       ;; Then we enter the yanked groups into the topics they belong
1225       ;; to.
1226       (setq alist (assoc (save-excursion
1227                            (forward-line -1)
1228                            (gnus-current-topic))
1229                          gnus-topic-alist))
1230       (when (stringp yanked)
1231         (setq yanked (list yanked)))
1232       (if (not prev)
1233           (nconc alist yanked)
1234         (if (not (cdr alist))
1235             (setcdr alist (nconc yanked (cdr alist)))
1236           (while (cdr alist)
1237             (when (equal (cadr alist) prev)
1238               (setcdr alist (nconc yanked (cdr alist)))
1239               (setq alist nil))
1240             (setq alist (cdr alist))))))
1241     (gnus-topic-update-topic)))
1242
1243 (defun gnus-topic-hide-topic ()
1244   "Hide the current topic."
1245   (interactive)
1246   (when (gnus-current-topic)
1247     (gnus-topic-goto-topic (gnus-current-topic))
1248     (gnus-topic-remove-topic nil nil 'hidden)))
1249
1250 (defun gnus-topic-show-topic ()
1251   "Show the hidden topic."
1252   (interactive)
1253   (when (gnus-group-topic-p)
1254     (gnus-topic-remove-topic t nil 'shown)))
1255
1256 (defun gnus-topic-mark-topic (topic &optional unmark)
1257   "Mark all groups in the topic with the process mark."
1258   (interactive (list (gnus-group-topic-name)))
1259   (if (not topic)
1260       (call-interactively 'gnus-group-mark-group)
1261     (save-excursion
1262       (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
1263         (while groups
1264           (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1265                    (gnus-info-group (nth 2 (pop groups)))))))))
1266
1267 (defun gnus-topic-unmark-topic (topic &optional unmark)
1268   "Remove the process mark from all groups in the topic."
1269   (interactive (list (gnus-group-topic-name)))
1270   (if (not topic)
1271       (call-interactively 'gnus-group-unmark-group)
1272     (gnus-topic-mark-topic topic t)))
1273
1274 (defun gnus-topic-get-new-news-this-topic (&optional n)
1275   "Check for new news in the current topic."
1276   (interactive "P")
1277   (if (not (gnus-group-topic-p))
1278       (gnus-group-get-new-news-this-group n)
1279     (gnus-topic-mark-topic (gnus-group-topic-name))
1280     (gnus-group-get-new-news-this-group)))
1281
1282 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1283   "Move all groups that match REGEXP to some topic."
1284   (interactive
1285    (let (topic)
1286      (nreverse
1287       (list
1288        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1289        (read-string (format "Move to %s (regexp): " topic))))))
1290   (gnus-group-mark-regexp regexp)
1291   (gnus-topic-move-group nil topic copyp))
1292
1293 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1294   "Copy all groups that match REGEXP to some topic."
1295   (interactive
1296    (let (topic)
1297      (nreverse
1298       (list
1299        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1300        (read-string (format "Copy to %s (regexp): " topic))))))
1301   (gnus-topic-move-matching regexp topic t))
1302
1303 (defun gnus-topic-delete (topic)
1304   "Delete a topic."
1305   (interactive (list (gnus-group-topic-name)))
1306   (unless topic
1307     (error "No topic to be deleted"))
1308   (let ((entry (assoc topic gnus-topic-alist))
1309         (buffer-read-only nil))
1310     (when (cdr entry)
1311       (error "Topic not empty"))
1312     ;; Delete if visible.
1313     (when (gnus-topic-goto-topic topic)
1314       (gnus-delete-line))
1315     ;; Remove from alist.
1316     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1317     ;; Remove from topology.
1318     (gnus-topic-find-topology topic nil nil 'delete)
1319     (gnus-dribble-touch)))
1320
1321 (defun gnus-topic-rename (old-name new-name)
1322   "Rename a topic."
1323   (interactive
1324    (let ((topic (gnus-current-topic)))
1325      (list topic
1326            (read-string (format "Rename %s to: " topic)))))
1327   ;; Check whether the new name exists.
1328   (when (gnus-topic-find-topology new-name)
1329     (error "Topic '%s' already exists" new-name))
1330   ;; "nil" is an invalid name, for reasons I'd rather not go
1331   ;; into here.  Trust me.
1332   (when (equal new-name "nil")
1333     (error "Invalid name: %s" nil))
1334   ;; Do the renaming.
1335   (let ((top (gnus-topic-find-topology old-name))
1336         (entry (assoc old-name gnus-topic-alist)))
1337     (when top
1338       (setcar (cadr top) new-name))
1339     (when entry
1340       (setcar entry new-name))
1341     (forward-line -1)
1342     (gnus-dribble-touch)
1343     (gnus-group-list-groups)
1344     (forward-line 1)))
1345
1346 (defun gnus-topic-indent (&optional unindent)
1347   "Indent a topic -- make it a sub-topic of the previous topic.
1348 If UNINDENT, remove an indentation."
1349   (interactive "P")
1350   (if unindent
1351       (gnus-topic-unindent)
1352     (let* ((topic (gnus-current-topic))
1353            (parent (gnus-topic-previous-topic topic))
1354            (buffer-read-only nil))
1355       (unless parent
1356         (error "Nothing to indent %s into" topic))
1357       (when topic
1358         (gnus-topic-goto-topic topic)
1359         (gnus-topic-kill-group)
1360         (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1361         (gnus-topic-create-topic
1362          topic parent nil (cdaar gnus-topic-killed-topics))
1363         (pop gnus-topic-killed-topics)
1364         (or (gnus-topic-goto-topic topic)
1365             (gnus-topic-goto-topic parent))))))
1366
1367 (defun gnus-topic-unindent ()
1368   "Unindent a topic."
1369   (interactive)
1370   (let* ((topic (gnus-current-topic))
1371          (parent (gnus-topic-parent-topic topic))
1372          (grandparent (gnus-topic-parent-topic parent)))
1373     (unless grandparent
1374       (error "Nothing to indent %s into" topic))
1375     (when topic
1376       (gnus-topic-goto-topic topic)
1377       (gnus-topic-kill-group)
1378       (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1379       (gnus-topic-create-topic
1380        topic grandparent (gnus-topic-next-topic parent)
1381        (cdaar gnus-topic-killed-topics))
1382       (pop gnus-topic-killed-topics)
1383       (gnus-topic-goto-topic topic))))
1384
1385 (defun gnus-topic-list-active (&optional force)
1386   "List all groups that Gnus knows about in a topicsified fashion.
1387 If FORCE, always re-read the active file."
1388   (interactive "P")
1389   (when force
1390     (gnus-get-killed-groups))
1391   (gnus-topic-grok-active force)
1392   (let ((gnus-topic-topology gnus-topic-active-topology)
1393         (gnus-topic-alist gnus-topic-active-alist)
1394         gnus-killed-list gnus-zombie-list)
1395     (gnus-group-list-groups gnus-level-killed nil 1)))
1396
1397 (defun gnus-topic-toggle-display-empty-topics ()
1398   "Show/hide topics that have no unread articles."
1399   (interactive)
1400   (setq gnus-topic-display-empty-topics
1401         (not gnus-topic-display-empty-topics))
1402   (gnus-group-list-groups)
1403   (message "%s empty topics"
1404            (if gnus-topic-display-empty-topics
1405                "Showing" "Hiding")))
1406
1407 ;;; Topic sorting functions
1408
1409 (defun gnus-topic-edit-parameters (group)
1410   "Edit the group parameters of GROUP.
1411 If performed on a topic, edit the topic parameters instead."
1412   (interactive (list (gnus-group-group-name)))
1413   (if group
1414       (gnus-group-edit-group-parameters group)
1415     (if (not (gnus-group-topic-p))
1416         (error "Nothing to edit on the current line")
1417       (let ((topic (gnus-group-topic-name)))
1418         (gnus-edit-form
1419          (gnus-topic-parameters topic)
1420          (format "Editing the topic parameters for `%s'."
1421                  (or group topic))
1422          `(lambda (form)
1423             (gnus-topic-set-parameters ,topic form)))))))
1424
1425 (defun gnus-group-sort-topic (func reverse)
1426   "Sort groups in the topics according to FUNC and REVERSE."
1427   (let ((alist gnus-topic-alist))
1428     (while alist
1429       ;; !!!Sometimes nil elements sneak into the alist,
1430       ;; for some reason or other.
1431       (setcar alist (delq nil (car alist)))
1432       (setcar alist (delete "dummy.group" (car alist)))
1433       (gnus-topic-sort-topic (pop alist) func reverse))))
1434
1435 (defun gnus-topic-sort-topic (topic func reverse)
1436   ;; Each topic only lists the name of the group, while
1437   ;; the sort predicates expect group infos as inputs.
1438   ;; So we first transform the group names into infos,
1439   ;; then sort, and then transform back into group names.
1440   (setcdr
1441    topic
1442    (mapcar
1443     (lambda (info) (gnus-info-group info))
1444     (sort
1445      (mapcar
1446       (lambda (group) (gnus-get-info group))
1447       (cdr topic))
1448      func)))
1449   ;; Do the reversal, if necessary.
1450   (when reverse
1451     (setcdr topic (nreverse (cdr topic)))))
1452
1453 (defun gnus-topic-sort-groups (func &optional reverse)
1454   "Sort the current topic according to FUNC.
1455 If REVERSE, reverse the sorting order."
1456   (interactive (list gnus-group-sort-function current-prefix-arg))
1457   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1458     (gnus-topic-sort-topic
1459      topic (gnus-make-sort-function func) reverse)
1460     (gnus-group-list-groups)))
1461
1462 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1463   "Sort the current topic alphabetically by group name.
1464 If REVERSE, sort in reverse order."
1465   (interactive "P")
1466   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1467
1468 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1469   "Sort the current topic by number of unread articles.
1470 If REVERSE, sort in reverse order."
1471   (interactive "P")
1472   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1473
1474 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1475   "Sort the current topic by group level.
1476 If REVERSE, sort in reverse order."
1477   (interactive "P")
1478   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1479
1480 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1481   "Sort the current topic by group score.
1482 If REVERSE, sort in reverse order."
1483   (interactive "P")
1484   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1485
1486 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1487   "Sort the current topic by group rank.
1488 If REVERSE, sort in reverse order."
1489   (interactive "P")
1490   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1491
1492 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1493   "Sort the current topic alphabetically by backend name.
1494 If REVERSE, sort in reverse order."
1495   (interactive "P")
1496   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1497
1498 (defun gnus-topic-sort-topics-1 (top reverse)
1499   (if (cdr top)
1500       (let ((subtop
1501              (mapcar `(lambda (top)
1502                         (gnus-topic-sort-topics-1 top ,reverse))
1503                      (sort (cdr top)
1504                            '(lambda (t1 t2) 
1505                               (string-lessp (caar t1) (caar t2)))))))
1506         (setcdr top (if reverse (reverse subtop) subtop))))
1507   top)
1508
1509 (defun gnus-topic-sort-topics (&optional topic reverse)
1510   "Sort topics in TOPIC alphabeticaly by topic name.
1511 If REVERSE, reverse the sorting order."
1512   (interactive 
1513    (list (completing-read "Sort topics in : " gnus-topic-alist nil t 
1514                           (gnus-current-topic))
1515          current-prefix-arg))
1516   (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1517                             gnus-topic-topology)))
1518     (gnus-topic-sort-topics-1 topic-topology reverse)
1519     (gnus-topic-enter-dribble)
1520     (gnus-group-list-groups)
1521     (gnus-topic-goto-topic topic)))
1522
1523 (defun gnus-topic-move (current to)
1524   "Move the CURRENT topic to TO."
1525   (interactive 
1526    (list 
1527     (gnus-group-topic-name)
1528     (completing-read "Move to topic: " gnus-topic-alist nil t)))
1529   (unless (and current to)
1530     (error "Can't find topic"))
1531   (let ((current-top (cdr (gnus-topic-find-topology current)))
1532         (to-top (cdr (gnus-topic-find-topology to))))
1533     (unless current-top
1534       (error "Can't find topic `%s'" current))
1535     (unless to-top
1536       (error "Can't find topic `%s'" to))
1537     (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1538         (error "Can't move `%s' to its sub-level" current))
1539     (gnus-topic-find-topology current nil nil 'delete)
1540     (while (cdr to-top)
1541       (setq to-top (cdr to-top)))
1542     (setcdr to-top (list current-top))
1543     (gnus-topic-enter-dribble)
1544     (gnus-group-list-groups)
1545     (gnus-topic-goto-topic current)))
1546
1547 (defun gnus-subscribe-topics (newsgroup)
1548   (catch 'end
1549     (let (match gnus-group-change-level-function)
1550       (dolist (topic (gnus-topic-list))
1551         (when (and (setq match (cdr (assq 'subscribe
1552                                           (gnus-topic-parameters topic))))
1553                    (string-match match newsgroup))
1554           ;; Just subscribe the group.
1555           (gnus-subscribe-alphabetically newsgroup)
1556           ;; Add the group to the topic.
1557           (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1558           (throw 'end t))))))
1559           
1560 (provide 'gnus-topic)
1561
1562 ;;; gnus-topic.el ends here