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