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