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