3aed79467c4141247cb47b47dd727565fa80c7db
[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 (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, and
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       (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     (unless gnus-topic-inhibit-change-level
738       (gnus-group-goto-group (or (car (nth 2 previous)) group))
739       (when (and gnus-topic-mode
740                  gnus-topic-alist
741                  (not gnus-topic-inhibit-change-level))
742         ;; Remove the group from the topics.
743         (if (and (< oldlevel gnus-level-zombie)
744                  (>= level gnus-level-zombie))
745             (let ((alist gnus-topic-alist))
746               (while (gnus-group-goto-group group)
747                 (gnus-delete-line))
748               (while alist
749                 (when (member group (car alist))
750                   (setcdr (car alist) (delete group (cdar alist))))
751                 (pop alist)))
752           ;; If the group is subscribed we enter it into the topics.
753           (when (and (< level gnus-level-zombie)
754                      (>= oldlevel gnus-level-zombie))
755             (let* ((prev (gnus-group-group-name))
756                    (gnus-topic-inhibit-change-level t)
757                    (gnus-group-indentation
758                     (make-string
759                      (* gnus-topic-indent-level
760                         (or (save-excursion
761                               (gnus-topic-goto-topic (gnus-current-topic))
762                               (gnus-group-topic-level))
763                             0))
764                      ? ))
765                    (yanked (list group))
766                    alist talist end)
767               ;; Then we enter the yanked groups into the topics they belong
768               ;; to.
769               (when (setq alist (assoc (save-excursion
770                                          (forward-line -1)
771                                          (or
772                                           (gnus-current-topic)
773                                           (caar gnus-topic-topology)))
774                                        gnus-topic-alist))
775                 (setq talist alist)
776                 (when (stringp yanked)
777                   (setq yanked (list yanked)))
778                 (if (not prev)
779                     (nconc alist yanked)
780                   (if (not (cdr alist))
781                       (setcdr alist (nconc yanked (cdr alist)))
782                     (while (and (not end) (cdr alist))
783                       (when (equal (cadr alist) prev)
784                         (setcdr alist (nconc yanked (cdr alist)))
785                         (setq end t))
786                       (setq alist (cdr alist)))
787                     (unless end
788                       (nconc talist yanked))))))
789             (gnus-topic-update-topic)))))))
790
791 (defun gnus-topic-goto-next-group (group props)
792   "Go to group or the next group after group."
793   (if (not group)
794       (if (not (memq 'gnus-topic props))
795           (goto-char (point-max))
796         (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
797     (if (gnus-group-goto-group group)
798         t
799       ;; The group is no longer visible.
800       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
801              (after (cdr (member group (cdr list)))))
802         ;; First try to put point on a group after the current one.
803         (while (and after
804                     (not (gnus-group-goto-group (car after))))
805           (setq after (cdr after)))
806         ;; Then try to put point on a group before point.
807         (unless after
808           (setq after (cdr (member group (reverse (cdr list)))))
809           (while (and after
810                       (not (gnus-group-goto-group (car after))))
811             (setq after (cdr after))))
812         ;; Finally, just put point on the topic.
813         (if (not (car list))
814             (goto-char (point-min))
815           (unless after
816             (gnus-topic-goto-topic (car list))
817             (setq after nil)))
818         t))))
819
820 ;;; Topic-active functions
821
822 (defun gnus-topic-grok-active (&optional force)
823   "Parse all active groups and create topic structures for them."
824   ;; First we make sure that we have really read the active file.
825   (when (or force
826             (not gnus-topic-active-alist))
827     (let (groups)
828       ;; Get a list of all groups available.
829       (mapatoms (lambda (g) (when (symbol-value g)
830                               (push (symbol-name g) groups)))
831                 gnus-active-hashtb)
832       (setq groups (sort groups 'string<))
833       ;; Init the variables.
834       (setq gnus-topic-active-topology (list (list "" 'visible)))
835       (setq gnus-topic-active-alist nil)
836       ;; Descend the top-level hierarchy.
837       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
838       ;; Set the top-level topic names to something nice.
839       (setcar (car gnus-topic-active-topology) "Gnus active")
840       (setcar (car gnus-topic-active-alist) "Gnus active"))))
841
842 (defun gnus-topic-grok-active-1 (topology groups)
843   (let* ((name (caar topology))
844          (prefix (concat "^" (regexp-quote name)))
845          tgroups ntopology group)
846     (while (and groups
847                 (string-match prefix (setq group (car groups))))
848       (if (not (string-match "\\." group (match-end 0)))
849           ;; There are no further hierarchies here, so we just
850           ;; enter this group into the list belonging to this
851           ;; topic.
852           (push (pop groups) tgroups)
853         ;; New sub-hierarchy, so we add it to the topology.
854         (nconc topology (list (setq ntopology
855                                     (list (list (substring
856                                                  group 0 (match-end 0))
857                                                 'invisible)))))
858         ;; Descend the hierarchy.
859         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
860     ;; We remove the trailing "." from the topic name.
861     (setq name
862           (if (string-match "\\.$" name)
863               (substring name 0 (match-beginning 0))
864             name))
865     ;; Add this topic and its groups to the topic alist.
866     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
867     (setcar (car topology) name)
868     ;; We return the rest of the groups that didn't belong
869     ;; to this topic.
870     groups))
871
872 ;;; Topic mode, commands and keymap.
873
874 (defvar gnus-topic-mode-map nil)
875 (defvar gnus-group-topic-map nil)
876
877 (unless gnus-topic-mode-map
878   (setq gnus-topic-mode-map (make-sparse-keymap))
879
880   ;; Override certain group mode keys.
881   (gnus-define-keys gnus-topic-mode-map
882     "=" gnus-topic-select-group
883     "\r" gnus-topic-select-group
884     " " gnus-topic-read-group
885     "\C-k" gnus-topic-kill-group
886     "\C-y" gnus-topic-yank-group
887     "\M-g" gnus-topic-get-new-news-this-topic
888     "AT" gnus-topic-list-active
889     "Gp" gnus-topic-edit-parameters
890     "#" gnus-topic-mark-topic
891     "\M-#" gnus-topic-unmark-topic
892     gnus-mouse-2 gnus-mouse-pick-topic)
893
894   ;; Define a new submap.
895   (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
896     "#" gnus-topic-mark-topic
897     "\M-#" gnus-topic-unmark-topic
898     "n" gnus-topic-create-topic
899     "m" gnus-topic-move-group
900     "D" gnus-topic-remove-group
901     "c" gnus-topic-copy-group
902     "h" gnus-topic-hide-topic
903     "s" gnus-topic-show-topic
904     "M" gnus-topic-move-matching
905     "C" gnus-topic-copy-matching
906     "\C-i" gnus-topic-indent
907     [tab] gnus-topic-indent
908     "r" gnus-topic-rename
909     "\177" gnus-topic-delete
910     [delete] gnus-topic-delete
911     "h" gnus-topic-toggle-display-empty-topics)
912
913   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
914     "s" gnus-topic-sort-groups
915     "a" gnus-topic-sort-groups-by-alphabet
916     "u" gnus-topic-sort-groups-by-unread
917     "l" gnus-topic-sort-groups-by-level
918     "v" gnus-topic-sort-groups-by-score
919     "r" gnus-topic-sort-groups-by-rank
920     "m" gnus-topic-sort-groups-by-method))
921
922 (defun gnus-topic-make-menu-bar ()
923   (unless (boundp 'gnus-topic-menu)
924     (easy-menu-define
925      gnus-topic-menu gnus-topic-mode-map ""
926      '("Topics"
927        ["Toggle topics" gnus-topic-mode t]
928        ("Groups"
929         ["Copy" gnus-topic-copy-group t]
930         ["Move" gnus-topic-move-group t]
931         ["Remove" gnus-topic-remove-group t]
932         ["Copy matching" gnus-topic-copy-matching t]
933         ["Move matching" gnus-topic-move-matching t])
934        ("Topics"
935         ["Show" gnus-topic-show-topic t]
936         ["Hide" gnus-topic-hide-topic t]
937         ["Delete" gnus-topic-delete t]
938         ["Rename" gnus-topic-rename t]
939         ["Create" gnus-topic-create-topic t]
940         ["Mark" gnus-topic-mark-topic t]
941         ["Indent" gnus-topic-indent t]
942         ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
943         ["Edit parameters" gnus-topic-edit-parameters t])
944        ["List active" gnus-topic-list-active t]))))
945
946 (defun gnus-topic-mode (&optional arg redisplay)
947   "Minor mode for topicsifying Gnus group buffers."
948   (interactive (list current-prefix-arg t))
949   (when (eq major-mode 'gnus-group-mode)
950     (make-local-variable 'gnus-topic-mode)
951     (setq gnus-topic-mode
952           (if (null arg) (not gnus-topic-mode)
953             (> (prefix-numeric-value arg) 0)))
954     ;; Infest Gnus with topics.
955      (if (not gnus-topic-mode)
956         (setq gnus-goto-missing-group-function nil)
957       (when (gnus-visual-p 'topic-menu 'menu)
958         (gnus-topic-make-menu-bar))
959       (gnus-set-format 'topic t)
960       (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
961       (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
962       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
963       (set (make-local-variable 'gnus-group-prepare-function)
964            'gnus-group-prepare-topics)
965       (set (make-local-variable 'gnus-group-get-parameter-function)
966            'gnus-group-topic-parameters)
967       (set (make-local-variable 'gnus-group-goto-next-group-function)
968            'gnus-topic-goto-next-group)
969       (set (make-local-variable 'gnus-group-indentation-function)
970            'gnus-topic-group-indentation)
971       (set (make-local-variable 'gnus-group-update-group-function)
972            'gnus-topic-update-topics-containing-group)
973       (set (make-local-variable 'gnus-group-sort-alist-function)
974            'gnus-group-sort-topic)
975       (setq gnus-group-change-level-function 'gnus-topic-change-level)
976       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
977       (make-local-hook 'gnus-check-bogus-groups-hook)
978       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
979       (setq gnus-topology-checked-p nil)
980       ;; We check the topology.
981       (when gnus-newsrc-alist
982         (gnus-topic-check-topology))
983       (run-hooks 'gnus-topic-mode-hook))
984     ;; Remove topic infestation.
985     (unless gnus-topic-mode
986       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
987       (remove-hook 'gnus-group-change-level-function
988                    'gnus-topic-change-level)
989       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
990       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
991       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
992     (when redisplay
993       (gnus-group-list-groups))))
994
995 (defun gnus-topic-select-group (&optional all)
996   "Select this newsgroup.
997 No article is selected automatically.
998 If ALL is non-nil, already read articles become readable.
999 If ALL is a number, fetch this number of articles.
1000
1001 If performed over a topic line, toggle folding the topic."
1002   (interactive "P")
1003   (if (gnus-group-topic-p)
1004       (let ((gnus-group-list-mode
1005              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1006         (gnus-topic-fold all))
1007     (gnus-group-select-group all)))
1008
1009 (defun gnus-mouse-pick-topic (e)
1010   "Select the group or topic under the mouse pointer."
1011   (interactive "e")
1012   (mouse-set-point e)
1013   (gnus-topic-read-group nil))
1014
1015 (defun gnus-topic-read-group (&optional all no-article group)
1016   "Read news in this newsgroup.
1017 If the prefix argument ALL is non-nil, already read articles become
1018 readable.  IF ALL is a number, fetch this number of articles.  If the
1019 optional argument NO-ARTICLE is non-nil, no article will be
1020 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1021 group.
1022
1023 If performed over a topic line, toggle folding the topic."
1024   (interactive "P")
1025   (if (gnus-group-topic-p)
1026       (let ((gnus-group-list-mode
1027              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1028         (gnus-topic-fold all))
1029     (gnus-group-read-group all no-article group)))
1030
1031 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1032   "Create a new TOPIC under PARENT.
1033 When used interactively, PARENT will be the topic under point."
1034   (interactive
1035    (list
1036     (read-string "New topic: ")
1037     (gnus-current-topic)))
1038   ;; Check whether this topic already exists.
1039   (when (gnus-topic-find-topology topic)
1040     (error "Topic already exists"))
1041   (unless parent
1042     (setq parent (caar gnus-topic-topology)))
1043   (let ((top (cdr (gnus-topic-find-topology parent)))
1044         (full-topic (or full-topic `((,topic visible)))))
1045     (unless top
1046       (error "No such parent topic: %s" parent))
1047     (if previous
1048         (progn
1049           (while (and (cdr top)
1050                       (not (equal (caaadr top) previous)))
1051             (setq top (cdr top)))
1052           (setcdr top (cons full-topic (cdr top))))
1053       (nconc top (list full-topic)))
1054     (unless (assoc topic gnus-topic-alist)
1055       (push (list topic) gnus-topic-alist)))
1056   (gnus-topic-enter-dribble)
1057   (gnus-group-list-groups)
1058   (gnus-topic-goto-topic topic))
1059
1060 (defun gnus-topic-move-group (n topic &optional copyp)
1061   "Move the next N groups to TOPIC.
1062 If COPYP, copy the groups instead."
1063   (interactive
1064    (list current-prefix-arg
1065          (completing-read "Move to topic: " gnus-topic-alist nil t)))
1066   (let ((groups (gnus-group-process-prefix n))
1067         (topicl (assoc topic gnus-topic-alist))
1068         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1069         (start-topic (gnus-group-topic-name))
1070         entry)
1071     (mapcar
1072      (lambda (g)
1073        (gnus-group-remove-mark g)
1074        (when (and
1075               (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1076               (not copyp))
1077          (setcdr entry (gnus-delete-first g (cdr entry))))
1078        (nconc topicl (list g)))
1079      groups)
1080     (gnus-topic-enter-dribble)
1081     (if start-group
1082         (gnus-group-goto-group start-group)
1083       (gnus-topic-goto-topic start-topic))
1084     (gnus-group-list-groups)))
1085
1086 (defun gnus-topic-remove-group (&optional arg)
1087   "Remove the current group from the topic."
1088   (interactive "P")
1089   (gnus-group-iterate arg
1090     (lambda (group)
1091       (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1092             (buffer-read-only nil))
1093         (when (and topicl group)
1094           (gnus-delete-line)
1095           (gnus-delete-first group topicl))
1096         (gnus-topic-update-topic)
1097         (gnus-group-position-point)))))
1098
1099 (defun gnus-topic-copy-group (n topic)
1100   "Copy the current group to a topic."
1101   (interactive
1102    (list current-prefix-arg
1103          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1104   (gnus-topic-move-group n topic t))
1105
1106 (defun gnus-topic-kill-group (&optional n discard)
1107   "Kill the next N groups."
1108   (interactive "P")
1109   (if (gnus-group-topic-p)
1110       (let ((topic (gnus-group-topic-name)))
1111         (push (cons
1112                (gnus-topic-find-topology topic)
1113                (assoc topic gnus-topic-alist))
1114               gnus-topic-killed-topics)
1115         (gnus-topic-remove-topic nil t)
1116         (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1117         (gnus-topic-enter-dribble))
1118     (gnus-group-kill-group n discard)
1119     (gnus-topic-update-topic)))
1120
1121 (defun gnus-topic-yank-group (&optional arg)
1122   "Yank the last topic."
1123   (interactive "p")
1124   (if gnus-topic-killed-topics
1125       (let* ((previous
1126               (or (gnus-group-topic-name)
1127                   (gnus-topic-next-topic (gnus-current-topic))))
1128              (data (pop gnus-topic-killed-topics))
1129              (alist (cdr data))
1130              (item (cdar data)))
1131         (push alist gnus-topic-alist)
1132         (gnus-topic-create-topic
1133          (caar item) (gnus-topic-parent-topic previous) previous
1134          item)
1135         (gnus-topic-enter-dribble)
1136         (gnus-topic-goto-topic (caar item)))
1137     (let* ((prev (gnus-group-group-name))
1138            (gnus-topic-inhibit-change-level t)
1139            (gnus-group-indentation
1140             (make-string
1141              (* gnus-topic-indent-level
1142                 (or (save-excursion
1143                       (gnus-topic-goto-topic (gnus-current-topic))
1144                       (gnus-group-topic-level))
1145                     0))
1146              ? ))
1147            yanked alist)
1148       ;; We first yank the groups the normal way...
1149       (setq yanked (gnus-group-yank-group arg))
1150       ;; Then we enter the yanked groups into the topics they belong
1151       ;; to.
1152       (setq alist (assoc (save-excursion
1153                            (forward-line -1)
1154                            (gnus-current-topic))
1155                          gnus-topic-alist))
1156       (when (stringp yanked)
1157         (setq yanked (list yanked)))
1158       (if (not prev)
1159           (nconc alist yanked)
1160         (if (not (cdr alist))
1161             (setcdr alist (nconc yanked (cdr alist)))
1162           (while (cdr alist)
1163             (when (equal (cadr alist) prev)
1164               (setcdr alist (nconc yanked (cdr alist)))
1165               (setq alist nil))
1166             (setq alist (cdr alist))))))
1167     (gnus-topic-update-topic)))
1168
1169 (defun gnus-topic-hide-topic ()
1170   "Hide the current topic."
1171   (interactive)
1172   (when (gnus-current-topic)
1173     (gnus-topic-goto-topic (gnus-current-topic))
1174     (gnus-topic-remove-topic nil nil 'hidden)))
1175
1176 (defun gnus-topic-show-topic ()
1177   "Show the hidden topic."
1178   (interactive)
1179   (when (gnus-group-topic-p)
1180     (gnus-topic-remove-topic t nil 'shown)))
1181
1182 (defun gnus-topic-mark-topic (topic &optional unmark)
1183   "Mark all groups in the topic with the process mark."
1184   (interactive (list (gnus-group-topic-name)))
1185   (if (not topic)
1186       (call-interactively 'gnus-group-mark-group)
1187     (save-excursion
1188       (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
1189         (while groups
1190           (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1191                    (gnus-info-group (nth 2 (pop groups)))))))))
1192
1193 (defun gnus-topic-unmark-topic (topic &optional unmark)
1194   "Remove the process mark from all groups in the topic."
1195   (interactive (list (gnus-group-topic-name)))
1196   (if (not topic)
1197       (call-interactively 'gnus-group-unmark-group)
1198     (gnus-topic-mark-topic topic t)))
1199
1200 (defun gnus-topic-get-new-news-this-topic (&optional n)
1201   "Check for new news in the current topic."
1202   (interactive "P")
1203   (if (not (gnus-group-topic-p))
1204       (gnus-group-get-new-news-this-group n)
1205     (gnus-topic-mark-topic (gnus-group-topic-name))
1206     (gnus-group-get-new-news-this-group)))
1207
1208 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1209   "Move all groups that match REGEXP to some topic."
1210   (interactive
1211    (let (topic)
1212      (nreverse
1213       (list
1214        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1215        (read-string (format "Move to %s (regexp): " topic))))))
1216   (gnus-group-mark-regexp regexp)
1217   (gnus-topic-move-group nil topic copyp))
1218
1219 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1220   "Copy all groups that match REGEXP to some topic."
1221   (interactive
1222    (let (topic)
1223      (nreverse
1224       (list
1225        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1226        (read-string (format "Copy to %s (regexp): " topic))))))
1227   (gnus-topic-move-matching regexp topic t))
1228
1229 (defun gnus-topic-delete (topic)
1230   "Delete a topic."
1231   (interactive (list (gnus-group-topic-name)))
1232   (unless topic
1233     (error "No topic to be deleted"))
1234   (let ((entry (assoc topic gnus-topic-alist))
1235         (buffer-read-only nil))
1236     (when (cdr entry)
1237       (error "Topic not empty"))
1238     ;; Delete if visible.
1239     (when (gnus-topic-goto-topic topic)
1240       (gnus-delete-line))
1241     ;; Remove from alist.
1242     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1243     ;; Remove from topology.
1244     (gnus-topic-find-topology topic nil nil 'delete)
1245     (gnus-dribble-touch)))
1246
1247 (defun gnus-topic-rename (old-name new-name)
1248   "Rename a topic."
1249   (interactive
1250    (let ((topic (gnus-current-topic)))
1251      (list topic
1252            (read-string (format "Rename %s to: " topic)))))
1253   ;; Check whether the new name exists.
1254   (when (gnus-topic-find-topology new-name)
1255     (error "Topic '%s' already exists"))
1256   ;; Do the renaming.
1257   (let ((top (gnus-topic-find-topology old-name))
1258         (entry (assoc old-name gnus-topic-alist)))
1259     (when top
1260       (setcar (cadr top) new-name))
1261     (when entry
1262       (setcar entry new-name))
1263     (forward-line -1)
1264     (gnus-dribble-touch)
1265     (gnus-group-list-groups)))
1266
1267 (defun gnus-topic-indent (&optional unindent)
1268   "Indent a topic -- make it a sub-topic of the previous topic.
1269 If UNINDENT, remove an indentation."
1270   (interactive "P")
1271   (if unindent
1272       (gnus-topic-unindent)
1273     (let* ((topic (gnus-current-topic))
1274            (parent (gnus-topic-previous-topic topic))
1275            (buffer-read-only nil))
1276       (unless parent
1277         (error "Nothing to indent %s into" topic))
1278       (when topic
1279         (gnus-topic-goto-topic topic)
1280         (gnus-topic-kill-group)
1281         (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1282         (gnus-topic-create-topic
1283          topic parent nil (cdaar gnus-topic-killed-topics))
1284         (pop gnus-topic-killed-topics)
1285         (or (gnus-topic-goto-topic topic)
1286             (gnus-topic-goto-topic parent))))))
1287
1288 (defun gnus-topic-unindent ()
1289   "Unindent a topic."
1290   (interactive)
1291   (let* ((topic (gnus-current-topic))
1292          (parent (gnus-topic-parent-topic topic))
1293          (grandparent (gnus-topic-parent-topic parent)))
1294     (unless grandparent
1295       (error "Nothing to indent %s into" topic))
1296     (when topic
1297       (gnus-topic-goto-topic topic)
1298       (gnus-topic-kill-group)
1299       (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1300       (gnus-topic-create-topic
1301        topic grandparent (gnus-topic-next-topic parent)
1302        (cdaar gnus-topic-killed-topics))
1303       (pop gnus-topic-killed-topics)
1304       (gnus-topic-goto-topic topic))))
1305
1306 (defun gnus-topic-list-active (&optional force)
1307   "List all groups that Gnus knows about in a topicsified fashion.
1308 If FORCE, always re-read the active file."
1309   (interactive "P")
1310   (when force
1311     (gnus-get-killed-groups))
1312   (gnus-topic-grok-active force)
1313   (let ((gnus-topic-topology gnus-topic-active-topology)
1314         (gnus-topic-alist gnus-topic-active-alist)
1315         gnus-killed-list gnus-zombie-list)
1316     (gnus-group-list-groups gnus-level-killed nil 1)))
1317
1318 (defun gnus-topic-toggle-display-empty-topics ()
1319   "Show/hide topics that have no unread articles."
1320   (interactive)
1321   (setq gnus-topic-display-empty-topics
1322         (not gnus-topic-display-empty-topics))
1323   (gnus-group-list-groups)
1324   (message "%s empty topics"
1325            (if gnus-topic-display-empty-topics
1326                "Showing" "Hiding")))
1327
1328 ;;; Topic sorting functions
1329
1330 (defun gnus-topic-edit-parameters (group)
1331   "Edit the group parameters of GROUP.
1332 If performed on a topic, edit the topic parameters instead."
1333   (interactive (list (gnus-group-group-name)))
1334   (if group
1335       (gnus-group-edit-group-parameters group)
1336     (if (not (gnus-group-topic-p))
1337         (error "Nothing to edit on the current line")
1338       (let ((topic (gnus-group-topic-name)))
1339         (gnus-edit-form
1340          (gnus-topic-parameters topic)
1341          (format "Editing the topic parameters for `%s'."
1342                  (or group topic))
1343          `(lambda (form)
1344             (gnus-topic-set-parameters ,topic form)))))))
1345
1346 (defun gnus-group-sort-topic (func reverse)
1347   "Sort groups in the topics according to FUNC and REVERSE."
1348   (let ((alist gnus-topic-alist))
1349     (while alist
1350       ;; !!!Sometimes nil elements sneak into the alist,
1351       ;; for some reason or other.
1352       (setcar alist (delq nil (car alist)))
1353       (setcar alist (delete "dummy.group" (car alist)))
1354       (gnus-topic-sort-topic (pop alist) func reverse))))
1355
1356 (defun gnus-topic-sort-topic (topic func reverse)
1357   ;; Each topic only lists the name of the group, while
1358   ;; the sort predicates expect group infos as inputs.
1359   ;; So we first transform the group names into infos,
1360   ;; then sort, and then transform back into group names.
1361   (setcdr
1362    topic
1363    (mapcar
1364     (lambda (info) (gnus-info-group info))
1365     (sort
1366      (mapcar
1367       (lambda (group) (gnus-get-info group))
1368       (cdr topic))
1369      func)))
1370   ;; Do the reversal, if necessary.
1371   (when reverse
1372     (setcdr topic (nreverse (cdr topic)))))
1373
1374 (defun gnus-topic-sort-groups (func &optional reverse)
1375   "Sort the current topic according to FUNC.
1376 If REVERSE, reverse the sorting order."
1377   (interactive (list gnus-group-sort-function current-prefix-arg))
1378   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1379     (gnus-topic-sort-topic
1380      topic (gnus-make-sort-function func) reverse)
1381     (gnus-group-list-groups)))
1382
1383 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1384   "Sort the current topic alphabetically by group name.
1385 If REVERSE, sort in reverse order."
1386   (interactive "P")
1387   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1388
1389 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1390   "Sort the current topic by number of unread articles.
1391 If REVERSE, sort in reverse order."
1392   (interactive "P")
1393   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1394
1395 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1396   "Sort the current topic by group level.
1397 If REVERSE, sort in reverse order."
1398   (interactive "P")
1399   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1400
1401 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1402   "Sort the current topic by group score.
1403 If REVERSE, sort in reverse order."
1404   (interactive "P")
1405   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1406
1407 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1408   "Sort the current topic by group rank.
1409 If REVERSE, sort in reverse order."
1410   (interactive "P")
1411   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1412
1413 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1414   "Sort the current topic alphabetically by backend name.
1415 If REVERSE, sort in reverse order."
1416   (interactive "P")
1417   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1418
1419 (provide 'gnus-topic)
1420
1421 ;;; gnus-topic.el ends here