*** 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
651        (max 0 (- (or old-unread 0) (or (gnus-group-topic-unread) 0)))))
652     unread))
653
654 (defun gnus-topic-group-indentation ()
655   (make-string
656    (* gnus-topic-indent-level
657       (or (save-excursion
658             (forward-line -1)
659             (gnus-topic-goto-topic (gnus-current-topic))
660             (gnus-group-topic-level))
661           0))
662    ? ))
663
664 ;;; Initialization
665
666 (gnus-add-shutdown 'gnus-topic-close 'gnus)
667
668 (defun gnus-topic-close ()
669   (setq gnus-topic-active-topology nil
670         gnus-topic-active-alist nil
671         gnus-topic-killed-topics nil
672         gnus-topology-checked-p nil))
673
674 (defun gnus-topic-check-topology ()
675   ;; The first time we set the topology to whatever we have
676   ;; gotten here, which can be rather random.
677   (unless gnus-topic-alist
678     (gnus-topic-init-alist))
679
680   (setq gnus-topology-checked-p t)
681   ;; Go through the topic alist and make sure that all topics
682   ;; are in the topic topology.
683   (let ((topics (gnus-topic-list))
684         (alist gnus-topic-alist)
685         changed)
686     (while alist
687       (unless (member (caar alist) topics)
688         (nconc gnus-topic-topology
689                (list (list (list (caar alist) 'visible))))
690         (setq changed t))
691       (setq alist (cdr alist)))
692     (when changed
693       (gnus-topic-enter-dribble))
694     ;; Conversely, go through the topology and make sure that all
695     ;; topologies have alists.
696     (while topics
697       (unless (assoc (car topics) gnus-topic-alist)
698         (push (list (car topics)) gnus-topic-alist))
699       (pop topics)))
700   ;; Go through all living groups and make sure that
701   ;; they belong to some topic.
702   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
703                                          gnus-topic-alist)))
704          (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
705          (newsrc (cdr gnus-newsrc-alist))
706          group)
707     (while newsrc
708       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
709         (setcdr entry (list group))
710         (setq entry (cdr entry)))))
711   ;; Go through all topics and make sure they contain only living groups.
712   (let ((alist gnus-topic-alist)
713         topic)
714     (while (setq topic (pop alist))
715       (while (cdr topic)
716         (if (and (cadr topic)
717                  (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
718             (setq topic (cdr topic))
719           (setcdr topic (cddr topic)))))))
720
721 (defun gnus-topic-init-alist ()
722   "Initialize the topic structures."
723   (setq gnus-topic-topology
724         (cons (list "Gnus" 'visible)
725               (mapcar (lambda (topic)
726                         (list (list (car topic) 'visible)))
727                       '(("misc")))))
728   (setq gnus-topic-alist
729         (list (cons "misc"
730                     (mapcar (lambda (info) (gnus-info-group info))
731                             (cdr gnus-newsrc-alist)))
732               (list "Gnus")))
733   (gnus-topic-enter-dribble))
734
735 ;;; Maintenance
736
737 (defun gnus-topic-clean-alist ()
738   "Remove bogus groups from the topic alist."
739   (let ((topic-alist gnus-topic-alist)
740         result topic)
741     (unless gnus-killed-hashtb
742       (gnus-make-hashtable-from-killed))
743     (while (setq topic (pop topic-alist))
744       (let ((topic-name (pop topic))
745             group filtered-topic)
746         (while (setq group (pop topic))
747           (when (and (or (gnus-gethash group gnus-active-hashtb)
748                          (gnus-info-method (gnus-get-info group)))
749                      (not (gnus-gethash group gnus-killed-hashtb)))
750             (push group filtered-topic)))
751         (push (cons topic-name (nreverse filtered-topic)) result)))
752     (setq gnus-topic-alist (nreverse result))))
753
754 (defun gnus-topic-change-level (group level oldlevel &optional previous)
755   "Run when changing levels to enter/remove groups from topics."
756   (save-excursion
757     (set-buffer gnus-group-buffer)
758     (let ((buffer-read-only nil))
759       (unless gnus-topic-inhibit-change-level
760         (gnus-group-goto-group (or (car (nth 2 previous)) group))
761         (when (and gnus-topic-mode
762                    gnus-topic-alist
763                    (not gnus-topic-inhibit-change-level))
764           ;; Remove the group from the topics.
765           (if (and (< oldlevel gnus-level-zombie)
766                    (>= level gnus-level-zombie))
767               (let ((alist gnus-topic-alist))
768                 (while (gnus-group-goto-group group)
769                   (gnus-delete-line))
770                 (while alist
771                   (when (member group (car alist))
772                     (setcdr (car alist) (delete group (cdar alist))))
773                   (pop alist)))
774             ;; If the group is subscribed we enter it into the topics.
775             (when (and (< level gnus-level-zombie)
776                        (>= oldlevel gnus-level-zombie))
777               (let* ((prev (gnus-group-group-name))
778                      (gnus-topic-inhibit-change-level t)
779                      (gnus-group-indentation
780                       (make-string
781                        (* gnus-topic-indent-level
782                           (or (save-excursion
783                                 (gnus-topic-goto-topic (gnus-current-topic))
784                                 (gnus-group-topic-level))
785                               0))
786                        ? ))
787                      (yanked (list group))
788                      alist talist end)
789                 ;; Then we enter the yanked groups into the topics they belong
790                 ;; to.
791                 (when (setq alist (assoc (save-excursion
792                                            (forward-line -1)
793                                            (or
794                                             (gnus-current-topic)
795                                             (caar gnus-topic-topology)))
796                                          gnus-topic-alist))
797                   (setq talist alist)
798                   (when (stringp yanked)
799                     (setq yanked (list yanked)))
800                   (if (not prev)
801                       (nconc alist yanked)
802                     (if (not (cdr alist))
803                         (setcdr alist (nconc yanked (cdr alist)))
804                       (while (and (not end) (cdr alist))
805                         (when (equal (cadr alist) prev)
806                           (setcdr alist (nconc yanked (cdr alist)))
807                           (setq end t))
808                         (setq alist (cdr alist)))
809                       (unless end
810                         (nconc talist yanked))))))
811               (gnus-topic-update-topic))))))))
812
813 (defun gnus-topic-goto-next-group (group props)
814   "Go to group or the next group after group."
815   (if (not group)
816       (if (not (memq 'gnus-topic props))
817           (goto-char (point-max))
818         (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
819     (if (gnus-group-goto-group group)
820         t
821       ;; The group is no longer visible.
822       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
823              (after (cdr (member group (cdr list)))))
824         ;; First try to put point on a group after the current one.
825         (while (and after
826                     (not (gnus-group-goto-group (car after))))
827           (setq after (cdr after)))
828         ;; Then try to put point on a group before point.
829         (unless after
830           (setq after (cdr (member group (reverse (cdr list)))))
831           (while (and after
832                       (not (gnus-group-goto-group (car after))))
833             (setq after (cdr after))))
834         ;; Finally, just put point on the topic.
835         (if (not (car list))
836             (goto-char (point-min))
837           (unless after
838             (gnus-topic-goto-topic (car list))
839             (setq after nil)))
840         t))))
841
842 ;;; Topic-active functions
843
844 (defun gnus-topic-grok-active (&optional force)
845   "Parse all active groups and create topic structures for them."
846   ;; First we make sure that we have really read the active file.
847   (when (or force
848             (not gnus-topic-active-alist))
849     (let (groups)
850       ;; Get a list of all groups available.
851       (mapatoms (lambda (g) (when (symbol-value g)
852                               (push (symbol-name g) groups)))
853                 gnus-active-hashtb)
854       (setq groups (sort groups 'string<))
855       ;; Init the variables.
856       (setq gnus-topic-active-topology (list (list "" 'visible)))
857       (setq gnus-topic-active-alist nil)
858       ;; Descend the top-level hierarchy.
859       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
860       ;; Set the top-level topic names to something nice.
861       (setcar (car gnus-topic-active-topology) "Gnus active")
862       (setcar (car gnus-topic-active-alist) "Gnus active"))))
863
864 (defun gnus-topic-grok-active-1 (topology groups)
865   (let* ((name (caar topology))
866          (prefix (concat "^" (regexp-quote name)))
867          tgroups ntopology group)
868     (while (and groups
869                 (string-match prefix (setq group (car groups))))
870       (if (not (string-match "\\." group (match-end 0)))
871           ;; There are no further hierarchies here, so we just
872           ;; enter this group into the list belonging to this
873           ;; topic.
874           (push (pop groups) tgroups)
875         ;; New sub-hierarchy, so we add it to the topology.
876         (nconc topology (list (setq ntopology
877                                     (list (list (substring
878                                                  group 0 (match-end 0))
879                                                 'invisible)))))
880         ;; Descend the hierarchy.
881         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
882     ;; We remove the trailing "." from the topic name.
883     (setq name
884           (if (string-match "\\.$" name)
885               (substring name 0 (match-beginning 0))
886             name))
887     ;; Add this topic and its groups to the topic alist.
888     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
889     (setcar (car topology) name)
890     ;; We return the rest of the groups that didn't belong
891     ;; to this topic.
892     groups))
893
894 ;;; Topic mode, commands and keymap.
895
896 (defvar gnus-topic-mode-map nil)
897 (defvar gnus-group-topic-map nil)
898
899 (unless gnus-topic-mode-map
900   (setq gnus-topic-mode-map (make-sparse-keymap))
901
902   ;; Override certain group mode keys.
903   (gnus-define-keys gnus-topic-mode-map
904     "=" gnus-topic-select-group
905     "\r" gnus-topic-select-group
906     " " gnus-topic-read-group
907     "\C-k" gnus-topic-kill-group
908     "\C-y" gnus-topic-yank-group
909     "\M-g" gnus-topic-get-new-news-this-topic
910     "AT" gnus-topic-list-active
911     "Gp" gnus-topic-edit-parameters
912     "#" gnus-topic-mark-topic
913     "\M-#" gnus-topic-unmark-topic
914     [tab] gnus-topic-indent
915     [(meta tab)] gnus-topic-unindent
916     "\C-i" gnus-topic-indent
917     "\M-\C-i" gnus-topic-unindent
918     gnus-mouse-2 gnus-mouse-pick-topic)
919
920   ;; Define a new submap.
921   (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
922     "#" gnus-topic-mark-topic
923     "\M-#" gnus-topic-unmark-topic
924     "n" gnus-topic-create-topic
925     "m" gnus-topic-move-group
926     "D" gnus-topic-remove-group
927     "c" gnus-topic-copy-group
928     "h" gnus-topic-hide-topic
929     "s" gnus-topic-show-topic
930     "M" gnus-topic-move-matching
931     "C" gnus-topic-copy-matching
932     "\C-i" gnus-topic-indent
933     [tab] gnus-topic-indent
934     "r" gnus-topic-rename
935     "\177" gnus-topic-delete
936     [delete] gnus-topic-delete
937     "H" gnus-topic-toggle-display-empty-topics)
938
939   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
940     "s" gnus-topic-sort-groups
941     "a" gnus-topic-sort-groups-by-alphabet
942     "u" gnus-topic-sort-groups-by-unread
943     "l" gnus-topic-sort-groups-by-level
944     "v" gnus-topic-sort-groups-by-score
945     "r" gnus-topic-sort-groups-by-rank
946     "m" gnus-topic-sort-groups-by-method))
947
948 (defun gnus-topic-make-menu-bar ()
949   (unless (boundp 'gnus-topic-menu)
950     (easy-menu-define
951      gnus-topic-menu gnus-topic-mode-map ""
952      '("Topics"
953        ["Toggle topics" gnus-topic-mode t]
954        ("Groups"
955         ["Copy" gnus-topic-copy-group t]
956         ["Move" gnus-topic-move-group t]
957         ["Remove" gnus-topic-remove-group t]
958         ["Copy matching" gnus-topic-copy-matching t]
959         ["Move matching" gnus-topic-move-matching t])
960        ("Topics"
961         ["Show" gnus-topic-show-topic t]
962         ["Hide" gnus-topic-hide-topic t]
963         ["Delete" gnus-topic-delete t]
964         ["Rename" gnus-topic-rename t]
965         ["Create" gnus-topic-create-topic t]
966         ["Mark" gnus-topic-mark-topic t]
967         ["Indent" gnus-topic-indent t]
968         ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
969         ["Edit parameters" gnus-topic-edit-parameters t])
970        ["List active" gnus-topic-list-active t]))))
971
972 (defun gnus-topic-mode (&optional arg redisplay)
973   "Minor mode for topicsifying Gnus group buffers."
974   (interactive (list current-prefix-arg t))
975   (when (eq major-mode 'gnus-group-mode)
976     (make-local-variable 'gnus-topic-mode)
977     (setq gnus-topic-mode
978           (if (null arg) (not gnus-topic-mode)
979             (> (prefix-numeric-value arg) 0)))
980     ;; Infest Gnus with topics.
981      (if (not gnus-topic-mode)
982         (setq gnus-goto-missing-group-function nil)
983       (when (gnus-visual-p 'topic-menu 'menu)
984         (gnus-topic-make-menu-bar))
985       (gnus-set-format 'topic t)
986       (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
987       (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
988       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
989       (set (make-local-variable 'gnus-group-prepare-function)
990            'gnus-group-prepare-topics)
991       (set (make-local-variable 'gnus-group-get-parameter-function)
992            'gnus-group-topic-parameters)
993       (set (make-local-variable 'gnus-group-goto-next-group-function)
994            'gnus-topic-goto-next-group)
995       (set (make-local-variable 'gnus-group-indentation-function)
996            'gnus-topic-group-indentation)
997       (set (make-local-variable 'gnus-group-update-group-function)
998            'gnus-topic-update-topics-containing-group)
999       (set (make-local-variable 'gnus-group-sort-alist-function)
1000            'gnus-group-sort-topic)
1001       (setq gnus-group-change-level-function 'gnus-topic-change-level)
1002       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1003       (make-local-hook 'gnus-check-bogus-groups-hook)
1004       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1005       (setq gnus-topology-checked-p nil)
1006       ;; We check the topology.
1007       (when gnus-newsrc-alist
1008         (gnus-topic-check-topology))
1009       (gnus-run-hooks 'gnus-topic-mode-hook))
1010     ;; Remove topic infestation.
1011     (unless gnus-topic-mode
1012       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1013       (remove-hook 'gnus-group-change-level-function
1014                    'gnus-topic-change-level)
1015       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1016       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1017       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1018     (when redisplay
1019       (gnus-group-list-groups))))
1020
1021 (defun gnus-topic-select-group (&optional all)
1022   "Select this newsgroup.
1023 No article is selected automatically.
1024 If ALL is non-nil, already read articles become readable.
1025 If ALL is a number, fetch this number of articles.
1026
1027 If performed over a topic line, toggle folding the topic."
1028   (interactive "P")
1029   (if (gnus-group-topic-p)
1030       (let ((gnus-group-list-mode
1031              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1032         (gnus-topic-fold all))
1033     (gnus-group-select-group all)))
1034
1035 (defun gnus-mouse-pick-topic (e)
1036   "Select the group or topic under the mouse pointer."
1037   (interactive "e")
1038   (mouse-set-point e)
1039   (gnus-topic-read-group nil))
1040
1041 (defun gnus-topic-read-group (&optional all no-article group)
1042   "Read news in this newsgroup.
1043 If the prefix argument ALL is non-nil, already read articles become
1044 readable.  IF ALL is a number, fetch this number of articles.  If the
1045 optional argument NO-ARTICLE is non-nil, no article will be
1046 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1047 group.
1048
1049 If performed over a topic line, toggle folding the topic."
1050   (interactive "P")
1051   (if (gnus-group-topic-p)
1052       (let ((gnus-group-list-mode
1053              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1054         (gnus-topic-fold all))
1055     (gnus-group-read-group all no-article group)))
1056
1057 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1058   "Create a new TOPIC under PARENT.
1059 When used interactively, PARENT will be the topic under point."
1060   (interactive
1061    (list
1062     (read-string "New topic: ")
1063     (gnus-current-topic)))
1064   ;; Check whether this topic already exists.
1065   (when (gnus-topic-find-topology topic)
1066     (error "Topic already exists"))
1067   (unless parent
1068     (setq parent (caar gnus-topic-topology)))
1069   (let ((top (cdr (gnus-topic-find-topology parent)))
1070         (full-topic (or full-topic `((,topic visible)))))
1071     (unless top
1072       (error "No such parent topic: %s" parent))
1073     (if previous
1074         (progn
1075           (while (and (cdr top)
1076                       (not (equal (caaadr top) previous)))
1077             (setq top (cdr top)))
1078           (setcdr top (cons full-topic (cdr top))))
1079       (nconc top (list full-topic)))
1080     (unless (assoc topic gnus-topic-alist)
1081       (push (list topic) gnus-topic-alist)))
1082   (gnus-topic-enter-dribble)
1083   (gnus-group-list-groups)
1084   (gnus-topic-goto-topic topic))
1085
1086 (defun gnus-topic-move-group (n topic &optional copyp)
1087   "Move the next N groups to TOPIC.
1088 If COPYP, copy the groups instead."
1089   (interactive
1090    (list current-prefix-arg
1091          (completing-read "Move to topic: " gnus-topic-alist nil t)))
1092   (let ((groups (gnus-group-process-prefix n))
1093         (topicl (assoc topic gnus-topic-alist))
1094         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1095         (start-topic (gnus-group-topic-name))
1096         entry)
1097     (mapcar
1098      (lambda (g)
1099        (gnus-group-remove-mark g)
1100        (when (and
1101               (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1102               (not copyp))
1103          (setcdr entry (gnus-delete-first g (cdr entry))))
1104        (nconc topicl (list g)))
1105      groups)
1106     (gnus-topic-enter-dribble)
1107     (if start-group
1108         (gnus-group-goto-group start-group)
1109       (gnus-topic-goto-topic start-topic))
1110     (gnus-group-list-groups)))
1111
1112 (defun gnus-topic-remove-group (&optional arg)
1113   "Remove the current group from the topic."
1114   (interactive "P")
1115   (gnus-group-iterate arg
1116     (lambda (group)
1117       (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1118             (buffer-read-only nil))
1119         (when (and topicl group)
1120           (gnus-delete-line)
1121           (gnus-delete-first group topicl))
1122         (gnus-topic-update-topic)
1123         (gnus-group-position-point)))))
1124
1125 (defun gnus-topic-copy-group (n topic)
1126   "Copy the current group to a topic."
1127   (interactive
1128    (list current-prefix-arg
1129          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1130   (gnus-topic-move-group n topic t))
1131
1132 (defun gnus-topic-kill-group (&optional n discard)
1133   "Kill the next N groups."
1134   (interactive "P")
1135   (if (gnus-group-topic-p)
1136       (let ((topic (gnus-group-topic-name)))
1137         (push (cons
1138                (gnus-topic-find-topology topic)
1139                (assoc topic gnus-topic-alist))
1140               gnus-topic-killed-topics)
1141         (gnus-topic-remove-topic nil t)
1142         (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1143         (gnus-topic-enter-dribble))
1144     (gnus-group-kill-group n discard)
1145     (gnus-topic-update-topic)))
1146
1147 (defun gnus-topic-yank-group (&optional arg)
1148   "Yank the last topic."
1149   (interactive "p")
1150   (if gnus-topic-killed-topics
1151       (let* ((previous
1152               (or (gnus-group-topic-name)
1153                   (gnus-topic-next-topic (gnus-current-topic))))
1154              (data (pop gnus-topic-killed-topics))
1155              (alist (cdr data))
1156              (item (cdar data)))
1157         (push alist gnus-topic-alist)
1158         (gnus-topic-create-topic
1159          (caar item) (gnus-topic-parent-topic previous) previous
1160          item)
1161         (gnus-topic-enter-dribble)
1162         (gnus-topic-goto-topic (caar item)))
1163     (let* ((prev (gnus-group-group-name))
1164            (gnus-topic-inhibit-change-level t)
1165            (gnus-group-indentation
1166             (make-string
1167              (* gnus-topic-indent-level
1168                 (or (save-excursion
1169                       (gnus-topic-goto-topic (gnus-current-topic))
1170                       (gnus-group-topic-level))
1171                     0))
1172              ? ))
1173            yanked alist)
1174       ;; We first yank the groups the normal way...
1175       (setq yanked (gnus-group-yank-group arg))
1176       ;; Then we enter the yanked groups into the topics they belong
1177       ;; to.
1178       (setq alist (assoc (save-excursion
1179                            (forward-line -1)
1180                            (gnus-current-topic))
1181                          gnus-topic-alist))
1182       (when (stringp yanked)
1183         (setq yanked (list yanked)))
1184       (if (not prev)
1185           (nconc alist yanked)
1186         (if (not (cdr alist))
1187             (setcdr alist (nconc yanked (cdr alist)))
1188           (while (cdr alist)
1189             (when (equal (cadr alist) prev)
1190               (setcdr alist (nconc yanked (cdr alist)))
1191               (setq alist nil))
1192             (setq alist (cdr alist))))))
1193     (gnus-topic-update-topic)))
1194
1195 (defun gnus-topic-hide-topic ()
1196   "Hide the current topic."
1197   (interactive)
1198   (when (gnus-current-topic)
1199     (gnus-topic-goto-topic (gnus-current-topic))
1200     (gnus-topic-remove-topic nil nil 'hidden)))
1201
1202 (defun gnus-topic-show-topic ()
1203   "Show the hidden topic."
1204   (interactive)
1205   (when (gnus-group-topic-p)
1206     (gnus-topic-remove-topic t nil 'shown)))
1207
1208 (defun gnus-topic-mark-topic (topic &optional unmark)
1209   "Mark all groups in the topic with the process mark."
1210   (interactive (list (gnus-group-topic-name)))
1211   (if (not topic)
1212       (call-interactively 'gnus-group-mark-group)
1213     (save-excursion
1214       (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
1215         (while groups
1216           (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1217                    (gnus-info-group (nth 2 (pop groups)))))))))
1218
1219 (defun gnus-topic-unmark-topic (topic &optional unmark)
1220   "Remove the process mark from all groups in the topic."
1221   (interactive (list (gnus-group-topic-name)))
1222   (if (not topic)
1223       (call-interactively 'gnus-group-unmark-group)
1224     (gnus-topic-mark-topic topic t)))
1225
1226 (defun gnus-topic-get-new-news-this-topic (&optional n)
1227   "Check for new news in the current topic."
1228   (interactive "P")
1229   (if (not (gnus-group-topic-p))
1230       (gnus-group-get-new-news-this-group n)
1231     (gnus-topic-mark-topic (gnus-group-topic-name))
1232     (gnus-group-get-new-news-this-group)))
1233
1234 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1235   "Move all groups that match REGEXP to some topic."
1236   (interactive
1237    (let (topic)
1238      (nreverse
1239       (list
1240        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1241        (read-string (format "Move to %s (regexp): " topic))))))
1242   (gnus-group-mark-regexp regexp)
1243   (gnus-topic-move-group nil topic copyp))
1244
1245 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1246   "Copy all groups that match REGEXP to some topic."
1247   (interactive
1248    (let (topic)
1249      (nreverse
1250       (list
1251        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1252        (read-string (format "Copy to %s (regexp): " topic))))))
1253   (gnus-topic-move-matching regexp topic t))
1254
1255 (defun gnus-topic-delete (topic)
1256   "Delete a topic."
1257   (interactive (list (gnus-group-topic-name)))
1258   (unless topic
1259     (error "No topic to be deleted"))
1260   (let ((entry (assoc topic gnus-topic-alist))
1261         (buffer-read-only nil))
1262     (when (cdr entry)
1263       (error "Topic not empty"))
1264     ;; Delete if visible.
1265     (when (gnus-topic-goto-topic topic)
1266       (gnus-delete-line))
1267     ;; Remove from alist.
1268     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1269     ;; Remove from topology.
1270     (gnus-topic-find-topology topic nil nil 'delete)
1271     (gnus-dribble-touch)))
1272
1273 (defun gnus-topic-rename (old-name new-name)
1274   "Rename a topic."
1275   (interactive
1276    (let ((topic (gnus-current-topic)))
1277      (list topic
1278            (read-string (format "Rename %s to: " topic)))))
1279   ;; Check whether the new name exists.
1280   (when (gnus-topic-find-topology new-name)
1281     (error "Topic '%s' already exists" new-name))
1282   ;; "nil" is an invalid name, for reasons I'd rather not go
1283   ;; into here.  Trust me.
1284   (when (equal new-name "nil")
1285     (error "Invalid name: %s" nil))
1286   ;; Do the renaming.
1287   (let ((top (gnus-topic-find-topology old-name))
1288         (entry (assoc old-name gnus-topic-alist)))
1289     (when top
1290       (setcar (cadr top) new-name))
1291     (when entry
1292       (setcar entry new-name))
1293     (forward-line -1)
1294     (gnus-dribble-touch)
1295     (gnus-group-list-groups)
1296     (forward-line 1)))
1297
1298 (defun gnus-topic-indent (&optional unindent)
1299   "Indent a topic -- make it a sub-topic of the previous topic.
1300 If UNINDENT, remove an indentation."
1301   (interactive "P")
1302   (if unindent
1303       (gnus-topic-unindent)
1304     (let* ((topic (gnus-current-topic))
1305            (parent (gnus-topic-previous-topic topic))
1306            (buffer-read-only nil))
1307       (unless parent
1308         (error "Nothing to indent %s into" topic))
1309       (when topic
1310         (gnus-topic-goto-topic topic)
1311         (gnus-topic-kill-group)
1312         (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1313         (gnus-topic-create-topic
1314          topic parent nil (cdaar gnus-topic-killed-topics))
1315         (pop gnus-topic-killed-topics)
1316         (or (gnus-topic-goto-topic topic)
1317             (gnus-topic-goto-topic parent))))))
1318
1319 (defun gnus-topic-unindent ()
1320   "Unindent a topic."
1321   (interactive)
1322   (let* ((topic (gnus-current-topic))
1323          (parent (gnus-topic-parent-topic topic))
1324          (grandparent (gnus-topic-parent-topic parent)))
1325     (unless grandparent
1326       (error "Nothing to indent %s into" topic))
1327     (when topic
1328       (gnus-topic-goto-topic topic)
1329       (gnus-topic-kill-group)
1330       (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1331       (gnus-topic-create-topic
1332        topic grandparent (gnus-topic-next-topic parent)
1333        (cdaar gnus-topic-killed-topics))
1334       (pop gnus-topic-killed-topics)
1335       (gnus-topic-goto-topic topic))))
1336
1337 (defun gnus-topic-list-active (&optional force)
1338   "List all groups that Gnus knows about in a topicsified fashion.
1339 If FORCE, always re-read the active file."
1340   (interactive "P")
1341   (when force
1342     (gnus-get-killed-groups))
1343   (gnus-topic-grok-active force)
1344   (let ((gnus-topic-topology gnus-topic-active-topology)
1345         (gnus-topic-alist gnus-topic-active-alist)
1346         gnus-killed-list gnus-zombie-list)
1347     (gnus-group-list-groups gnus-level-killed nil 1)))
1348
1349 (defun gnus-topic-toggle-display-empty-topics ()
1350   "Show/hide topics that have no unread articles."
1351   (interactive)
1352   (setq gnus-topic-display-empty-topics
1353         (not gnus-topic-display-empty-topics))
1354   (gnus-group-list-groups)
1355   (message "%s empty topics"
1356            (if gnus-topic-display-empty-topics
1357                "Showing" "Hiding")))
1358
1359 ;;; Topic sorting functions
1360
1361 (defun gnus-topic-edit-parameters (group)
1362   "Edit the group parameters of GROUP.
1363 If performed on a topic, edit the topic parameters instead."
1364   (interactive (list (gnus-group-group-name)))
1365   (if group
1366       (gnus-group-edit-group-parameters group)
1367     (if (not (gnus-group-topic-p))
1368         (error "Nothing to edit on the current line")
1369       (let ((topic (gnus-group-topic-name)))
1370         (gnus-edit-form
1371          (gnus-topic-parameters topic)
1372          (format "Editing the topic parameters for `%s'."
1373                  (or group topic))
1374          `(lambda (form)
1375             (gnus-topic-set-parameters ,topic form)))))))
1376
1377 (defun gnus-group-sort-topic (func reverse)
1378   "Sort groups in the topics according to FUNC and REVERSE."
1379   (let ((alist gnus-topic-alist))
1380     (while alist
1381       ;; !!!Sometimes nil elements sneak into the alist,
1382       ;; for some reason or other.
1383       (setcar alist (delq nil (car alist)))
1384       (setcar alist (delete "dummy.group" (car alist)))
1385       (gnus-topic-sort-topic (pop alist) func reverse))))
1386
1387 (defun gnus-topic-sort-topic (topic func reverse)
1388   ;; Each topic only lists the name of the group, while
1389   ;; the sort predicates expect group infos as inputs.
1390   ;; So we first transform the group names into infos,
1391   ;; then sort, and then transform back into group names.
1392   (setcdr
1393    topic
1394    (mapcar
1395     (lambda (info) (gnus-info-group info))
1396     (sort
1397      (mapcar
1398       (lambda (group) (gnus-get-info group))
1399       (cdr topic))
1400      func)))
1401   ;; Do the reversal, if necessary.
1402   (when reverse
1403     (setcdr topic (nreverse (cdr topic)))))
1404
1405 (defun gnus-topic-sort-groups (func &optional reverse)
1406   "Sort the current topic according to FUNC.
1407 If REVERSE, reverse the sorting order."
1408   (interactive (list gnus-group-sort-function current-prefix-arg))
1409   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1410     (gnus-topic-sort-topic
1411      topic (gnus-make-sort-function func) reverse)
1412     (gnus-group-list-groups)))
1413
1414 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1415   "Sort the current topic alphabetically by group name.
1416 If REVERSE, sort in reverse order."
1417   (interactive "P")
1418   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1419
1420 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1421   "Sort the current topic by number of unread articles.
1422 If REVERSE, sort in reverse order."
1423   (interactive "P")
1424   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1425
1426 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1427   "Sort the current topic by group level.
1428 If REVERSE, sort in reverse order."
1429   (interactive "P")
1430   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1431
1432 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1433   "Sort the current topic by group score.
1434 If REVERSE, sort in reverse order."
1435   (interactive "P")
1436   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1437
1438 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1439   "Sort the current topic by group rank.
1440 If REVERSE, sort in reverse order."
1441   (interactive "P")
1442   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1443
1444 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1445   "Sort the current topic alphabetically by backend name.
1446 If REVERSE, sort in reverse order."
1447   (interactive "P")
1448   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1449
1450 (provide 'gnus-topic)
1451
1452 ;;; gnus-topic.el ends here