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