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