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