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