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