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