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