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