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