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