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