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