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