* gnus-vers.el (gnus-revision-number): Increment to 01.
[elisp/gnus.git-] / lisp / gnus-topic.el
1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Ilja Weis <kult@uni-paderborn.de>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31 (eval-when-compile (require 'gnus-clfns))
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 (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 (gnus-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 (gnus-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 (gnus-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 (gnus-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   (dolist (topic (gnus-current-topics topic))
171     (gnus-topic-goto-topic topic)
172     (gnus-topic-fold t))
173   (gnus-topic-goto-topic topic))
174
175 (defun gnus-current-topic ()
176   "Return the name of the current topic."
177   (let ((result
178          (or (get-text-property (point) 'gnus-topic)
179              (save-excursion
180                (and (gnus-goto-char (previous-single-property-change
181                                      (point) 'gnus-topic))
182                     (get-text-property (max (1- (point)) (point-min))
183                                        'gnus-topic))))))
184     (when result
185       (symbol-name result))))
186
187 (defun gnus-current-topics (&optional topic)
188   "Return a list of all current topics, lowest in hierarchy first.
189 If TOPIC, start with that topic."
190   (let ((topic (or topic (gnus-current-topic)))
191         topics)
192     (while topic
193       (push topic topics)
194       (setq topic (gnus-topic-parent-topic topic)))
195     (nreverse topics)))
196
197 (defun gnus-group-active-topic-p ()
198   "Say whether the current topic comes from the active topics."
199   (save-excursion
200     (beginning-of-line)
201     (get-text-property (point) '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-gethash group gnus-newsrc-hashtb)
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 ((topics (gnus-current-topics topic))
399         params-list param out params)
400     (while topics
401       (push (gnus-topic-parameters (pop topics)) params-list))
402     ;; We probably have lots of nil elements here, so
403     ;; we remove them.  Probably faster than doing this "properly".
404     (setq params-list (delq nil params-list))
405     ;; Now we have all the parameters, so we go through them
406     ;; and do inheritance in the obvious way.
407     (while (setq params (pop params-list))
408       (while (setq param (pop params))
409         (when (atom param)
410           (setq param (cons param t)))
411         ;; Override any old versions of this param.
412         (gnus-pull (car param) out)
413         (push param out)))
414     ;; Return the resulting parameter list.
415     out))
416
417 ;;; General utility functions
418
419 (defun gnus-topic-enter-dribble ()
420   (gnus-dribble-enter
421    (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
422
423 ;;; Generating group buffers
424
425 (defun gnus-group-prepare-topics (level &optional predicate lowest
426                                         regexp list-topic topic-level)
427   "List all newsgroups with unread articles of level LEVEL or lower.
428 Use the `gnus-group-topics' to sort the groups.
429 If PREDICTE is a function, list groups that the function returns non-nil;
430 if it is t, list groups that have no unread articles.
431 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
432   (set-buffer gnus-group-buffer)
433   (let ((buffer-read-only nil)
434         (lowest (or lowest 1))
435         (not-in-list
436          (and gnus-group-listed-groups
437               (copy-sequence gnus-group-listed-groups))))
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-delete-if (lambda (group)
466                            (or (gnus-gethash group gnus-newsrc-hashtb)
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 (not (gnus-group-goto-group (car g) t)))
701       (pop g))
702     ;; It wasn't visible, so we try to see where to insert it.
703     (when (not g)
704       (setq g (cdr (member group (reverse groups))))
705       (while (and g unfound)
706         (when (gnus-group-goto-group (pop g) t)
707           (forward-line 1)
708           (setq unfound nil)))
709       (when (and unfound
710                  topic
711                  (not (gnus-topic-goto-missing-topic topic)))
712         (let* ((top (gnus-topic-find-topology topic))
713                (children (cddr top))
714                (type (cadr top))
715                (unread 0)
716                (entries (gnus-topic-find-groups
717                          (car type) (car gnus-group-list-mode)
718                          (cdr gnus-group-list-mode))))
719           (while children
720             (incf unread (gnus-topic-unread (caar (pop children)))))
721           (while (setq entry (pop entries))
722             (when (numberp (car entry))
723               (incf unread (car entry))))
724           (gnus-topic-insert-topic-line
725            topic t t (car (gnus-topic-find-topology topic)) nil unread))))))
726
727 (defun gnus-topic-goto-missing-topic (topic)
728   (if (gnus-topic-goto-topic topic)
729       (forward-line 1)
730     ;; Topic not displayed.
731     (let* ((top (gnus-topic-find-topology
732                  (gnus-topic-parent-topic topic)))
733            (tp (reverse (cddr top))))
734       (if (not top)
735           (gnus-topic-insert-topic-line
736            topic t t (car (gnus-topic-find-topology topic)) nil 0)
737         (while (not (equal (caaar tp) topic))
738           (setq tp (cdr tp)))
739         (pop tp)
740         (while (and tp
741                     (not (gnus-topic-goto-topic (caaar tp))))
742           (pop tp))
743         (if tp
744             (gnus-topic-forward-topic 1)
745           (gnus-topic-goto-missing-topic (caadr top)))))
746     nil))
747
748 (defun gnus-topic-update-topic-line (topic-name &optional reads)
749   (let* ((top (gnus-topic-find-topology topic-name))
750          (type (cadr top))
751          (children (cddr top))
752          (entries (gnus-topic-find-groups
753                    (car type) (car gnus-group-list-mode)
754                    (cdr gnus-group-list-mode)))
755          (parent (gnus-topic-parent-topic topic-name))
756          (all-entries entries)
757          (unread 0)
758          old-unread entry new-unread)
759     (when (gnus-topic-goto-topic (car type))
760       ;; Tally all the groups that belong in this topic.
761       (if reads
762           (setq unread (- (gnus-group-topic-unread) reads))
763         (while children
764           (incf unread (gnus-topic-unread (caar (pop children)))))
765         (while (setq entry (pop entries))
766           (when (numberp (car entry))
767             (incf unread (car entry)))))
768       (setq old-unread (gnus-group-topic-unread))
769       ;; Insert the topic line.
770       (gnus-topic-insert-topic-line
771        (car type) (gnus-topic-visible-p)
772        (not (eq (nth 2 type) 'hidden))
773        (gnus-group-topic-level) all-entries unread)
774       (gnus-delete-line)
775       (forward-line -1)
776       (setq new-unread (gnus-group-topic-unread)))
777     (when parent
778       (forward-line -1)
779       (gnus-topic-update-topic-line
780        parent
781        (- (or old-unread 0) (or new-unread 0))))
782     unread))
783
784 (defun gnus-topic-group-indentation ()
785   (make-string
786    (* gnus-topic-indent-level
787       (or (save-excursion
788             (forward-line -1)
789             (gnus-topic-goto-topic (gnus-current-topic))
790             (gnus-group-topic-level))
791           0))
792    ? ))
793
794 ;;; Initialization
795
796 (gnus-add-shutdown 'gnus-topic-close 'gnus)
797
798 (defun gnus-topic-close ()
799   (setq gnus-topic-active-topology nil
800         gnus-topic-active-alist nil
801         gnus-topic-killed-topics nil
802         gnus-topology-checked-p nil))
803
804 (defun gnus-topic-check-topology ()
805   ;; The first time we set the topology to whatever we have
806   ;; gotten here, which can be rather random.
807   (unless gnus-topic-alist
808     (gnus-topic-init-alist))
809
810   (setq gnus-topology-checked-p t)
811   ;; Go through the topic alist and make sure that all topics
812   ;; are in the topic topology.
813   (let ((topics (gnus-topic-list))
814         (alist gnus-topic-alist)
815         changed)
816     (while alist
817       (unless (member (caar alist) topics)
818         (nconc gnus-topic-topology
819                (list (list (list (caar alist) 'visible))))
820         (setq changed t))
821       (setq alist (cdr alist)))
822     (when changed
823       (gnus-topic-enter-dribble))
824     ;; Conversely, go through the topology and make sure that all
825     ;; topologies have alists.
826     (while topics
827       (unless (assoc (car topics) gnus-topic-alist)
828         (push (list (car topics)) gnus-topic-alist))
829       (pop topics)))
830   ;; Go through all living groups and make sure that
831   ;; they belong to some topic.
832   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
833                                          gnus-topic-alist)))
834          (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
835          (newsrc (cdr gnus-newsrc-alist))
836          group)
837     (while newsrc
838       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
839         (setcdr entry (list group))
840         (setq entry (cdr entry)))))
841   ;; Go through all topics and make sure they contain only living groups.
842   (let ((alist gnus-topic-alist)
843         topic)
844     (while (setq topic (pop alist))
845       (while (cdr topic)
846         (if (and (cadr topic)
847                  (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
848             (setq topic (cdr topic))
849           (setcdr topic (cddr topic)))))))
850
851 (defun gnus-topic-init-alist ()
852   "Initialize the topic structures."
853   (setq gnus-topic-topology
854         (cons (list "Gnus" 'visible)
855               (mapcar (lambda (topic)
856                         (list (list (car topic) 'visible)))
857                       '(("misc")))))
858   (setq gnus-topic-alist
859         (list (cons "misc"
860                     (mapcar (lambda (info) (gnus-info-group info))
861                             (cdr gnus-newsrc-alist)))
862               (list "Gnus")))
863   (gnus-topic-enter-dribble))
864
865 ;;; Maintenance
866
867 (defun gnus-topic-clean-alist ()
868   "Remove bogus groups from the topic alist."
869   (let ((topic-alist gnus-topic-alist)
870         result topic)
871     (unless gnus-killed-hashtb
872       (gnus-make-hashtable-from-killed))
873     (while (setq topic (pop topic-alist))
874       (let ((topic-name (pop topic))
875             group filtered-topic)
876         (while (setq group (pop topic))
877           (when (and (or (gnus-gethash group gnus-active-hashtb)
878                          (gnus-info-method (gnus-get-info group)))
879                      (not (gnus-gethash group gnus-killed-hashtb)))
880             (push group filtered-topic)))
881         (push (cons topic-name (nreverse filtered-topic)) result)))
882     (setq gnus-topic-alist (nreverse result))))
883
884 (defun gnus-topic-change-level (group level oldlevel &optional previous)
885   "Run when changing levels to enter/remove groups from topics."
886   (save-excursion
887     (set-buffer gnus-group-buffer)
888     (let ((buffer-read-only nil))
889       (unless gnus-topic-inhibit-change-level
890         (gnus-group-goto-group (or (car (nth 2 previous)) group))
891         (when (and gnus-topic-mode
892                    gnus-topic-alist
893                    (not gnus-topic-inhibit-change-level))
894           ;; Remove the group from the topics.
895           (if (and (< oldlevel gnus-level-zombie)
896                    (>= level gnus-level-zombie))
897               (let ((alist gnus-topic-alist))
898                 (while (gnus-group-goto-group group)
899                   (gnus-delete-line))
900                 (while alist
901                   (when (member group (car alist))
902                     (setcdr (car alist) (delete group (cdar alist))))
903                   (pop alist)))
904             ;; If the group is subscribed we enter it into the topics.
905             (when (and (< level gnus-level-zombie)
906                        (>= oldlevel gnus-level-zombie))
907               (let* ((prev (gnus-group-group-name))
908                      (gnus-topic-inhibit-change-level t)
909                      (gnus-group-indentation
910                       (make-string
911                        (* gnus-topic-indent-level
912                           (or (save-excursion
913                                 (gnus-topic-goto-topic (gnus-current-topic))
914                                 (gnus-group-topic-level))
915                               0))
916                        ? ))
917                      (yanked (list group))
918                      alist talist end)
919                 ;; Then we enter the yanked groups into the topics they belong
920                 ;; to.
921                 (when (setq alist (assoc (save-excursion
922                                            (forward-line -1)
923                                            (or
924                                             (gnus-current-topic)
925                                             (caar gnus-topic-topology)))
926                                          gnus-topic-alist))
927                   (setq talist alist)
928                   (when (stringp yanked)
929                     (setq yanked (list yanked)))
930                   (if (not prev)
931                       (nconc alist yanked)
932                     (if (not (cdr alist))
933                         (setcdr alist (nconc yanked (cdr alist)))
934                       (while (and (not end) (cdr alist))
935                         (when (equal (cadr alist) prev)
936                           (setcdr alist (nconc yanked (cdr alist)))
937                           (setq end t))
938                         (setq alist (cdr alist)))
939                       (unless end
940                         (nconc talist yanked))))))
941               (gnus-topic-update-topic))))))))
942
943 (defun gnus-topic-goto-next-group (group props)
944   "Go to group or the next group after group."
945   (if (not group)
946       (if (not (memq 'gnus-topic props))
947           (goto-char (point-max))
948         (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
949     (if (gnus-group-goto-group group)
950         t
951       ;; The group is no longer visible.
952       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
953              (after (cdr (member group (cdr list)))))
954         ;; First try to put point on a group after the current one.
955         (while (and after
956                     (not (gnus-group-goto-group (car after))))
957           (setq after (cdr after)))
958         ;; Then try to put point on a group before point.
959         (unless after
960           (setq after (cdr (member group (reverse (cdr list)))))
961           (while (and after
962                       (not (gnus-group-goto-group (car after))))
963             (setq after (cdr after))))
964         ;; Finally, just put point on the topic.
965         (if (not (car list))
966             (goto-char (point-min))
967           (unless after
968             (gnus-topic-goto-topic (car list))
969             (setq after nil)))
970         t))))
971
972 ;;; Topic-active functions
973
974 (defun gnus-topic-grok-active (&optional force)
975   "Parse all active groups and create topic structures for them."
976   ;; First we make sure that we have really read the active file.
977   (when (or force
978             (not gnus-topic-active-alist))
979     (let (groups)
980       ;; Get a list of all groups available.
981       (mapatoms (lambda (g) (when (symbol-value g)
982                               (push (symbol-name g) groups)))
983                 gnus-active-hashtb)
984       (setq groups (sort groups 'string<))
985       ;; Init the variables.
986       (setq gnus-topic-active-topology (list (list "" 'visible)))
987       (setq gnus-topic-active-alist nil)
988       ;; Descend the top-level hierarchy.
989       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
990       ;; Set the top-level topic names to something nice.
991       (setcar (car gnus-topic-active-topology) "Gnus active")
992       (setcar (car gnus-topic-active-alist) "Gnus active"))))
993
994 (defun gnus-topic-grok-active-1 (topology groups)
995   (let* ((name (caar topology))
996          (prefix (concat "^" (regexp-quote name)))
997          tgroups ntopology group)
998     (while (and groups
999                 (string-match prefix (setq group (car groups))))
1000       (if (not (string-match "\\." group (match-end 0)))
1001           ;; There are no further hierarchies here, so we just
1002           ;; enter this group into the list belonging to this
1003           ;; topic.
1004           (push (pop groups) tgroups)
1005         ;; New sub-hierarchy, so we add it to the topology.
1006         (nconc topology (list (setq ntopology
1007                                     (list (list (substring
1008                                                  group 0 (match-end 0))
1009                                                 'invisible)))))
1010         ;; Descend the hierarchy.
1011         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
1012     ;; We remove the trailing "." from the topic name.
1013     (setq name
1014           (if (string-match "\\.$" name)
1015               (substring name 0 (match-beginning 0))
1016             name))
1017     ;; Add this topic and its groups to the topic alist.
1018     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
1019     (setcar (car topology) name)
1020     ;; We return the rest of the groups that didn't belong
1021     ;; to this topic.
1022     groups))
1023
1024 ;;; Topic mode, commands and keymap.
1025
1026 (defvar gnus-topic-mode-map nil)
1027 (defvar gnus-group-topic-map nil)
1028
1029 (unless gnus-topic-mode-map
1030   (setq gnus-topic-mode-map (make-sparse-keymap))
1031
1032   ;; Override certain group mode keys.
1033   (gnus-define-keys gnus-topic-mode-map
1034     "=" gnus-topic-select-group
1035     "\r" gnus-topic-select-group
1036     " " gnus-topic-read-group
1037     "\C-c\C-x" gnus-topic-expire-articles
1038     "c" gnus-topic-catchup-articles
1039     "\C-k" gnus-topic-kill-group
1040     "\C-y" gnus-topic-yank-group
1041     "\M-g" gnus-topic-get-new-news-this-topic
1042     "AT" gnus-topic-list-active
1043     "Gp" gnus-topic-edit-parameters
1044     "#" gnus-topic-mark-topic
1045     "\M-#" gnus-topic-unmark-topic
1046     [tab] gnus-topic-indent
1047     [(meta tab)] gnus-topic-unindent
1048     "\C-i" gnus-topic-indent
1049     "\M-\C-i" gnus-topic-unindent
1050     gnus-mouse-2 gnus-mouse-pick-topic)
1051
1052   ;; Define a new submap.
1053   (gnus-define-keys (gnus-group-topic-map "T" gnus-topic-mode-map)
1054     "#" gnus-topic-mark-topic
1055     "\M-#" gnus-topic-unmark-topic
1056     "n" gnus-topic-create-topic
1057     "m" gnus-topic-move-group
1058     "D" gnus-topic-remove-group
1059     "c" gnus-topic-copy-group
1060     "h" gnus-topic-hide-topic
1061     "s" gnus-topic-show-topic
1062     "j" gnus-topic-jump-to-topic
1063     "M" gnus-topic-move-matching
1064     "C" gnus-topic-copy-matching
1065     "\M-p" gnus-topic-goto-previous-topic
1066     "\M-n" gnus-topic-goto-next-topic
1067     "\C-i" gnus-topic-indent
1068     [tab] gnus-topic-indent
1069     "r" gnus-topic-rename
1070     "\177" gnus-topic-delete
1071     [delete] gnus-topic-delete
1072     "H" gnus-topic-toggle-display-empty-topics)
1073
1074   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1075     "s" gnus-topic-sort-groups
1076     "a" gnus-topic-sort-groups-by-alphabet
1077     "u" gnus-topic-sort-groups-by-unread
1078     "l" gnus-topic-sort-groups-by-level
1079     "e" gnus-topic-sort-groups-by-server
1080     "v" gnus-topic-sort-groups-by-score
1081     "r" gnus-topic-sort-groups-by-rank
1082     "m" gnus-topic-sort-groups-by-method))
1083
1084 (defun gnus-topic-make-menu-bar ()
1085   (unless (boundp 'gnus-topic-menu)
1086     (easy-menu-define
1087      gnus-topic-menu gnus-topic-mode-map ""
1088      '("Topics"
1089        ["Toggle topics" gnus-topic-mode t]
1090        ("Groups"
1091         ["Copy" gnus-topic-copy-group t]
1092         ["Move" gnus-topic-move-group t]
1093         ["Remove" gnus-topic-remove-group t]
1094         ["Copy matching" gnus-topic-copy-matching t]
1095         ["Move matching" gnus-topic-move-matching t])
1096        ("Topics"
1097         ["Goto" gnus-topic-jump-to-topic t]
1098         ["Show" gnus-topic-show-topic t]
1099         ["Hide" gnus-topic-hide-topic t]
1100         ["Delete" gnus-topic-delete t]
1101         ["Rename" gnus-topic-rename t]
1102         ["Create" gnus-topic-create-topic t]
1103         ["Mark" gnus-topic-mark-topic t]
1104         ["Indent" gnus-topic-indent t]
1105         ["Sort" gnus-topic-sort-topics t]
1106         ["Previous topic" gnus-topic-goto-previous-topic t]
1107         ["Next topic" gnus-topic-goto-next-topic t]
1108         ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1109         ["Edit parameters" gnus-topic-edit-parameters t])
1110        ["List active" gnus-topic-list-active t]))))
1111
1112 (defun gnus-topic-mode (&optional arg redisplay)
1113   "Minor mode for topicsifying Gnus group buffers."
1114   (interactive (list current-prefix-arg t))
1115   (when (eq major-mode 'gnus-group-mode)
1116     (make-local-variable 'gnus-topic-mode)
1117     (setq gnus-topic-mode
1118           (if (null arg) (not gnus-topic-mode)
1119             (> (prefix-numeric-value arg) 0)))
1120     ;; Infest Gnus with topics.
1121     (if (not gnus-topic-mode)
1122         (setq gnus-goto-missing-group-function nil)
1123       (when (gnus-visual-p 'topic-menu 'menu)
1124         (gnus-topic-make-menu-bar))
1125       (gnus-set-format 'topic t)
1126       (gnus-add-minor-mode 'gnus-topic-mode " Topic"
1127                            gnus-topic-mode-map nil (lambda (&rest junk)
1128                                                      (interactive)
1129                                                      (gnus-topic-mode nil t)))
1130       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1131       (set (make-local-variable 'gnus-group-prepare-function)
1132            'gnus-group-prepare-topics)
1133       (set (make-local-variable 'gnus-group-get-parameter-function)
1134            'gnus-group-topic-parameters)
1135       (set (make-local-variable 'gnus-group-goto-next-group-function)
1136            'gnus-topic-goto-next-group)
1137       (set (make-local-variable 'gnus-group-indentation-function)
1138            'gnus-topic-group-indentation)
1139       (set (make-local-variable 'gnus-group-update-group-function)
1140            'gnus-topic-update-topics-containing-group)
1141       (set (make-local-variable 'gnus-group-sort-alist-function)
1142            'gnus-group-sort-topic)
1143       (setq gnus-group-change-level-function 'gnus-topic-change-level)
1144       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1145       (make-local-hook 'gnus-check-bogus-groups-hook)
1146       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1147                 nil 'local)
1148       (setq gnus-topology-checked-p nil)
1149       ;; We check the topology.
1150       (when gnus-newsrc-alist
1151         (gnus-topic-check-topology))
1152       (gnus-run-hooks 'gnus-topic-mode-hook))
1153     ;; Remove topic infestation.
1154     (unless gnus-topic-mode
1155       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1156       (setq gnus-group-change-level-function nil)
1157       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1158       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1159       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1160     (when redisplay
1161       (gnus-group-list-groups))))
1162
1163 (defun gnus-topic-select-group (&optional all)
1164   "Select this newsgroup.
1165 No article is selected automatically.
1166 If the group is opened, just switch the summary buffer.
1167 If ALL is non-nil, already read articles become readable.
1168 If ALL is a number, fetch this number of articles.
1169
1170 If performed over a topic line, toggle folding the topic."
1171   (interactive "P")
1172   (if (gnus-group-topic-p)
1173       (let ((gnus-group-list-mode
1174              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1175         (gnus-topic-fold all)
1176         (gnus-dribble-touch))
1177     (gnus-group-select-group all)))
1178
1179 (defun gnus-mouse-pick-topic (e)
1180   "Select the group or topic under the mouse pointer."
1181   (interactive "e")
1182   (mouse-set-point e)
1183   (gnus-topic-read-group nil))
1184
1185 (defun gnus-topic-expire-articles (topic)
1186   "Expire articles in this topic or group."
1187   (interactive (list (gnus-group-topic-name)))
1188   (if (not topic)
1189       (call-interactively 'gnus-group-expire-articles)
1190     (save-excursion
1191       (gnus-message 5 "Expiring groups in %s..." topic)
1192       (let ((gnus-group-marked
1193              (mapcar (lambda (entry) (car (nth 2 entry)))
1194                      (gnus-topic-find-groups topic gnus-level-killed t))))
1195         (gnus-group-expire-articles nil))
1196       (gnus-message 5 "Expiring groups in %s...done" topic))))
1197
1198 (defun gnus-topic-catchup-articles (topic)
1199   "Catchup this topic or group.
1200 Also see `gnus-group-catchup'."
1201   (interactive (list (gnus-group-topic-name)))
1202   (if (not topic)
1203       (call-interactively 'gnus-group-catchup-current)
1204     (save-excursion
1205       (let* ((groups
1206              (mapcar (lambda (entry) (car (nth 2 entry)))
1207                      (gnus-topic-find-groups topic gnus-level-killed t)))
1208              (buffer-read-only nil)
1209              (gnus-group-marked groups))
1210         (gnus-group-catchup-current)
1211         (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1212
1213 (defun gnus-topic-read-group (&optional all no-article group)
1214   "Read news in this newsgroup.
1215 If the prefix argument ALL is non-nil, already read articles become
1216 readable.  IF ALL is a number, fetch this number of articles.  If the
1217 optional argument NO-ARTICLE is non-nil, no article will be
1218 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1219 group.
1220
1221 If performed over a topic line, toggle folding the topic."
1222   (interactive "P")
1223   (if (gnus-group-topic-p)
1224       (let ((gnus-group-list-mode
1225              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1226         (gnus-topic-fold all))
1227     (gnus-group-read-group all no-article group)))
1228
1229 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1230   "Create a new TOPIC under PARENT.
1231 When used interactively, PARENT will be the topic under point."
1232   (interactive
1233    (list
1234     (read-string "New topic: ")
1235     (gnus-current-topic)))
1236   ;; Check whether this topic already exists.
1237   (when (gnus-topic-find-topology topic)
1238     (error "Topic already exists"))
1239   (unless parent
1240     (setq parent (caar gnus-topic-topology)))
1241   (let ((top (cdr (gnus-topic-find-topology parent)))
1242         (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1243     (unless top
1244       (error "No such parent topic: %s" parent))
1245     (if previous
1246         (progn
1247           (while (and (cdr top)
1248                       (not (equal (caaadr top) previous)))
1249             (setq top (cdr top)))
1250           (setcdr top (cons full-topic (cdr top))))
1251       (nconc top (list full-topic)))
1252     (unless (assoc topic gnus-topic-alist)
1253       (push (list topic) gnus-topic-alist)))
1254   (gnus-topic-enter-dribble)
1255   (gnus-group-list-groups)
1256   (gnus-topic-goto-topic topic))
1257
1258 ;; FIXME:
1259 ;;  1. When the marked groups are overlapped with the process
1260 ;;     region, the behavior of move or remove is not right.
1261 ;;  2. Can't process on several marked groups with a same name,
1262 ;;     because gnus-group-marked only keeps one copy.
1263
1264 (defun gnus-topic-move-group (n topic &optional copyp)
1265   "Move the next N groups to TOPIC.
1266 If COPYP, copy the groups instead."
1267   (interactive
1268    (list current-prefix-arg
1269          (gnus-completing-read "Move to topic" gnus-topic-alist nil t
1270                                'gnus-topic-history)))
1271   (let ((use-marked (and (not n) (not (gnus-region-active-p))
1272                          gnus-group-marked t))
1273         (groups (gnus-group-process-prefix n))
1274         (topicl (assoc topic gnus-topic-alist))
1275         (start-topic (gnus-group-topic-name))
1276         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1277         entry)
1278     (if (and (not groups) (not copyp) start-topic)
1279         (gnus-topic-move start-topic topic)
1280       (mapcar
1281        (lambda (g)
1282          (gnus-group-remove-mark g use-marked)
1283          (when (and
1284                 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1285                 (not copyp))
1286            (setcdr entry (gnus-delete-first g (cdr entry))))
1287          (nconc topicl (list g)))
1288        groups)
1289       (gnus-topic-enter-dribble)
1290       (if start-group
1291           (gnus-group-goto-group start-group)
1292         (gnus-topic-goto-topic start-topic))
1293       (gnus-group-list-groups))))
1294
1295 (defun gnus-topic-remove-group (&optional n)
1296   "Remove the current group from the topic."
1297   (interactive "P")
1298   (let ((use-marked (and (not n) (not (gnus-region-active-p))
1299                          gnus-group-marked t))
1300         (groups (gnus-group-process-prefix n)))
1301     (mapcar
1302      (lambda (group)
1303        (gnus-group-remove-mark group use-marked)
1304        (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1305              (buffer-read-only nil))
1306          (when (and topicl group)
1307            (gnus-delete-line)
1308            (gnus-delete-first group topicl))
1309          (gnus-topic-update-topic)))
1310      groups)
1311     (gnus-topic-enter-dribble)
1312     (gnus-group-position-point)))
1313
1314 (defun gnus-topic-copy-group (n topic)
1315   "Copy the current group to a topic."
1316   (interactive
1317    (list current-prefix-arg
1318          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1319   (gnus-topic-move-group n topic t))
1320
1321 (defun gnus-topic-kill-group (&optional n discard)
1322   "Kill the next N groups."
1323   (interactive "P")
1324   (if (gnus-group-topic-p)
1325       (let ((topic (gnus-group-topic-name)))
1326         (push (cons
1327                (gnus-topic-find-topology topic)
1328                (assoc topic gnus-topic-alist))
1329               gnus-topic-killed-topics)
1330         (gnus-topic-remove-topic nil t)
1331         (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1332         (gnus-topic-enter-dribble))
1333     (gnus-group-kill-group n discard)
1334     (if (not (gnus-group-topic-p))
1335         (gnus-topic-update-topic)
1336       ;; Move up one line so that we update the right topic.
1337       (forward-line -1)
1338       (gnus-topic-update-topic)
1339       (forward-line 1))))
1340
1341 (defun gnus-topic-yank-group (&optional arg)
1342   "Yank the last topic."
1343   (interactive "p")
1344   (if gnus-topic-killed-topics
1345       (let* ((previous
1346               (or (gnus-group-topic-name)
1347                   (gnus-topic-next-topic (gnus-current-topic))))
1348              (data (pop gnus-topic-killed-topics))
1349              (alist (cdr data))
1350              (item (cdar data)))
1351         (push alist gnus-topic-alist)
1352         (gnus-topic-create-topic
1353          (caar item) (gnus-topic-parent-topic previous) previous
1354          item)
1355         (gnus-topic-enter-dribble)
1356         (gnus-topic-goto-topic (caar item)))
1357     (let* ((prev (gnus-group-group-name))
1358            (gnus-topic-inhibit-change-level t)
1359            (gnus-group-indentation
1360             (make-string
1361              (* gnus-topic-indent-level
1362                 (or (save-excursion
1363                       (gnus-topic-goto-topic (gnus-current-topic))
1364                       (gnus-group-topic-level))
1365                     0))
1366              ? ))
1367            yanked alist)
1368       ;; We first yank the groups the normal way...
1369       (setq yanked (gnus-group-yank-group arg))
1370       ;; Then we enter the yanked groups into the topics they belong
1371       ;; to.
1372       (setq alist (assoc (save-excursion
1373                            (forward-line -1)
1374                            (gnus-current-topic))
1375                          gnus-topic-alist))
1376       (when (stringp yanked)
1377         (setq yanked (list yanked)))
1378       (if (not prev)
1379           (nconc alist yanked)
1380         (if (not (cdr alist))
1381             (setcdr alist (nconc yanked (cdr alist)))
1382           (while (cdr alist)
1383             (when (equal (cadr alist) prev)
1384               (setcdr alist (nconc yanked (cdr alist)))
1385               (setq alist nil))
1386             (setq alist (cdr alist))))))
1387     (gnus-topic-update-topic)))
1388
1389 (defun gnus-topic-hide-topic (&optional permanent)
1390   "Hide the current topic.
1391 If PERMANENT, make it stay hidden in subsequent sessions as well."
1392   (interactive "P")
1393   (when (gnus-current-topic)
1394     (gnus-topic-goto-topic (gnus-current-topic))
1395     (if permanent
1396         (setcar (cddr
1397                  (cadr
1398                   (gnus-topic-find-topology (gnus-current-topic))))
1399                 'hidden))
1400     (gnus-topic-remove-topic nil nil)))
1401
1402 (defun gnus-topic-show-topic (&optional permanent)
1403   "Show the hidden topic.
1404 If PERMANENT, make it stay shown in subsequent sessions as well."
1405   (interactive "P")
1406   (when (gnus-group-topic-p)
1407     (if (not permanent)
1408         (gnus-topic-remove-topic t nil)
1409       (let ((topic
1410              (gnus-topic-find-topology
1411               (completing-read "Show topic: " gnus-topic-alist nil t))))
1412         (setcar (cddr (cadr topic)) nil)
1413         (setcar (cdr (cadr topic)) 'visible)
1414         (gnus-group-list-groups)))))
1415
1416 (defun gnus-topic-mark-topic (topic &optional unmark recursive)
1417   "Mark all groups in the TOPIC with the process mark.
1418 If RECURSIVE is t, mark its subtopics too."
1419   (interactive (list (gnus-group-topic-name)
1420                      nil
1421                      (and current-prefix-arg t)))
1422   (if (not topic)
1423       (call-interactively 'gnus-group-mark-group)
1424     (save-excursion
1425       (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1426                                             recursive)))
1427         (while groups
1428           (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1429                    (gnus-info-group (nth 2 (pop groups)))))))))
1430
1431 (defun gnus-topic-unmark-topic (topic &optional dummy recursive)
1432   "Remove the process mark from all groups in the TOPIC.
1433 If RECURSIVE is t, unmark its subtopics too."
1434   (interactive (list (gnus-group-topic-name)
1435                      nil
1436                      (and current-prefix-arg t)))
1437   (if (not topic)
1438       (call-interactively 'gnus-group-unmark-group)
1439     (gnus-topic-mark-topic topic t recursive)))
1440
1441 (defun gnus-topic-get-new-news-this-topic (&optional n)
1442   "Check for new news in the current topic."
1443   (interactive "P")
1444   (if (not (gnus-group-topic-p))
1445       (gnus-group-get-new-news-this-group n)
1446     (let* ((topic (gnus-group-topic-name))
1447            (data (cadr (gnus-topic-find-topology topic))))
1448       (save-excursion
1449         (gnus-topic-mark-topic topic nil (and n t))
1450         (gnus-group-get-new-news-this-group))
1451       (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1452
1453 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1454   "Move all groups that match REGEXP to some topic."
1455   (interactive
1456    (let (topic)
1457      (nreverse
1458       (list
1459        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1460        (read-string (format "Move to %s (regexp): " topic))))))
1461   (gnus-group-mark-regexp regexp)
1462   (gnus-topic-move-group nil topic copyp))
1463
1464 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1465   "Copy all groups that match REGEXP to some topic."
1466   (interactive
1467    (let (topic)
1468      (nreverse
1469       (list
1470        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1471        (read-string (format "Copy to %s (regexp): " topic))))))
1472   (gnus-topic-move-matching regexp topic t))
1473
1474 (defun gnus-topic-delete (topic)
1475   "Delete a topic."
1476   (interactive (list (gnus-group-topic-name)))
1477   (unless topic
1478     (error "No topic to be deleted"))
1479   (let ((entry (assoc topic gnus-topic-alist))
1480         (buffer-read-only nil))
1481     (when (cdr entry)
1482       (error "Topic not empty"))
1483     ;; Delete if visible.
1484     (when (gnus-topic-goto-topic topic)
1485       (gnus-delete-line))
1486     ;; Remove from alist.
1487     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1488     ;; Remove from topology.
1489     (gnus-topic-find-topology topic nil nil 'delete)
1490     (gnus-dribble-touch)))
1491
1492 (defun gnus-topic-rename (old-name new-name)
1493   "Rename a topic."
1494   (interactive
1495    (let ((topic (gnus-current-topic)))
1496      (list topic
1497            (read-string (format "Rename %s to: " topic) topic))))
1498   ;; Check whether the new name exists.
1499   (when (gnus-topic-find-topology new-name)
1500     (error "Topic '%s' already exists" new-name))
1501   ;; "nil" is an invalid name, for reasons I'd rather not go
1502   ;; into here.  Trust me.
1503   (when (equal new-name "nil")
1504     (error "Invalid name: %s" nil))
1505   ;; Do the renaming.
1506   (let ((top (gnus-topic-find-topology old-name))
1507         (entry (assoc old-name gnus-topic-alist)))
1508     (when top
1509       (setcar (cadr top) new-name))
1510     (when entry
1511       (setcar entry new-name))
1512     (forward-line -1)
1513     (gnus-dribble-touch)
1514     (gnus-group-list-groups)
1515     (forward-line 1)))
1516
1517 (defun gnus-topic-indent (&optional unindent)
1518   "Indent a topic -- make it a sub-topic of the previous topic.
1519 If UNINDENT, remove an indentation."
1520   (interactive "P")
1521   (if unindent
1522       (gnus-topic-unindent)
1523     (let* ((topic (gnus-current-topic))
1524            (parent (gnus-topic-previous-topic topic))
1525            (buffer-read-only nil))
1526       (unless parent
1527         (error "Nothing to indent %s into" topic))
1528       (when topic
1529         (gnus-topic-goto-topic topic)
1530         (gnus-topic-kill-group)
1531         (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1532         (gnus-topic-create-topic
1533          topic parent nil (cdar (car gnus-topic-killed-topics)))
1534         (pop gnus-topic-killed-topics)
1535         (or (gnus-topic-goto-topic topic)
1536             (gnus-topic-goto-topic parent))))))
1537
1538 (defun gnus-topic-unindent ()
1539   "Unindent a topic."
1540   (interactive)
1541   (let* ((topic (gnus-current-topic))
1542          (parent (gnus-topic-parent-topic topic))
1543          (grandparent (gnus-topic-parent-topic parent)))
1544     (unless grandparent
1545       (error "Nothing to indent %s into" topic))
1546     (when topic
1547       (gnus-topic-goto-topic topic)
1548       (gnus-topic-kill-group)
1549       (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1550       (gnus-topic-create-topic
1551        topic grandparent (gnus-topic-next-topic parent)
1552        (cdar (car gnus-topic-killed-topics)))
1553       (pop gnus-topic-killed-topics)
1554       (gnus-topic-goto-topic topic))))
1555
1556 (defun gnus-topic-list-active (&optional force)
1557   "List all groups that Gnus knows about in a topicsified fashion.
1558 If FORCE, always re-read the active file."
1559   (interactive "P")
1560   (when force
1561     (gnus-get-killed-groups))
1562   (gnus-topic-grok-active force)
1563   (let ((gnus-topic-topology gnus-topic-active-topology)
1564         (gnus-topic-alist gnus-topic-active-alist)
1565         gnus-killed-list gnus-zombie-list)
1566     (gnus-group-list-groups gnus-level-killed nil 1)))
1567
1568 (defun gnus-topic-toggle-display-empty-topics ()
1569   "Show/hide topics that have no unread articles."
1570   (interactive)
1571   (setq gnus-topic-display-empty-topics
1572         (not gnus-topic-display-empty-topics))
1573   (gnus-group-list-groups)
1574   (message "%s empty topics"
1575            (if gnus-topic-display-empty-topics
1576                "Showing" "Hiding")))
1577
1578 ;;; Topic sorting functions
1579
1580 (defun gnus-topic-edit-parameters (group)
1581   "Edit the group parameters of GROUP.
1582 If performed on a topic, edit the topic parameters instead."
1583   (interactive (list (gnus-group-group-name)))
1584   (if group
1585       (gnus-group-edit-group-parameters group)
1586     (if (not (gnus-group-topic-p))
1587         (error "Nothing to edit on the current line")
1588       (let ((topic (gnus-group-topic-name)))
1589         (gnus-edit-form
1590          (gnus-topic-parameters topic)
1591          (format "Editing the topic parameters for `%s'."
1592                  (or group topic))
1593          `(lambda (form)
1594             (gnus-topic-set-parameters ,topic form)))))))
1595
1596 (defun gnus-group-sort-topic (func reverse)
1597   "Sort groups in the topics according to FUNC and REVERSE."
1598   (let ((alist gnus-topic-alist))
1599     (while alist
1600       ;; !!!Sometimes nil elements sneak into the alist,
1601       ;; for some reason or other.
1602       (setcar alist (delq nil (car alist)))
1603       (setcar alist (delete "dummy.group" (car alist)))
1604       (gnus-topic-sort-topic (pop alist) func reverse))))
1605
1606 (defun gnus-topic-sort-topic (topic func reverse)
1607   ;; Each topic only lists the name of the group, while
1608   ;; the sort predicates expect group infos as inputs.
1609   ;; So we first transform the group names into infos,
1610   ;; then sort, and then transform back into group names.
1611   (setcdr
1612    topic
1613    (mapcar
1614     (lambda (info) (gnus-info-group info))
1615     (sort
1616      (mapcar
1617       (lambda (group) (gnus-get-info group))
1618       (cdr topic))
1619      func)))
1620   ;; Do the reversal, if necessary.
1621   (when reverse
1622     (setcdr topic (nreverse (cdr topic)))))
1623
1624 (defun gnus-topic-sort-groups (func &optional reverse)
1625   "Sort the current topic according to FUNC.
1626 If REVERSE, reverse the sorting order."
1627   (interactive (list gnus-group-sort-function current-prefix-arg))
1628   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1629     (gnus-topic-sort-topic
1630      topic (gnus-make-sort-function func) reverse)
1631     (gnus-group-list-groups)))
1632
1633 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1634   "Sort the current topic alphabetically by group name.
1635 If REVERSE, sort in reverse order."
1636   (interactive "P")
1637   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1638
1639 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1640   "Sort the current topic by number of unread articles.
1641 If REVERSE, sort in reverse order."
1642   (interactive "P")
1643   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1644
1645 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1646   "Sort the current topic by group level.
1647 If REVERSE, sort in reverse order."
1648   (interactive "P")
1649   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1650
1651 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1652   "Sort the current topic by group score.
1653 If REVERSE, sort in reverse order."
1654   (interactive "P")
1655   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1656
1657 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1658   "Sort the current topic by group rank.
1659 If REVERSE, sort in reverse order."
1660   (interactive "P")
1661   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1662
1663 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1664   "Sort the current topic alphabetically by backend name.
1665 If REVERSE, sort in reverse order."
1666   (interactive "P")
1667   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1668
1669 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1670   "Sort the current topic alphabetically by server name.
1671 If REVERSE, sort in reverse order."
1672   (interactive "P")
1673   (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1674
1675 (defun gnus-topic-sort-topics-1 (top reverse)
1676   (if (cdr top)
1677       (let ((subtop
1678              (mapcar (gnus-byte-compile
1679                       `(lambda (top)
1680                          (gnus-topic-sort-topics-1 top ,reverse)))
1681                      (sort (cdr top)
1682                            (lambda (t1 t2)
1683                              (string-lessp (caar t1) (caar t2)))))))
1684         (setcdr top (if reverse (reverse subtop) subtop))))
1685   top)
1686
1687 (defun gnus-topic-sort-topics (&optional topic reverse)
1688   "Sort topics in TOPIC alphabeticaly by topic name.
1689 If REVERSE, reverse the sorting order."
1690   (interactive
1691    (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1692                           (gnus-current-topic))
1693          current-prefix-arg))
1694   (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1695                             gnus-topic-topology)))
1696     (gnus-topic-sort-topics-1 topic-topology reverse)
1697     (gnus-topic-enter-dribble)
1698     (gnus-group-list-groups)
1699     (gnus-topic-goto-topic topic)))
1700
1701 (defun gnus-topic-move (current to)
1702   "Move the CURRENT topic to TO."
1703   (interactive
1704    (list
1705     (gnus-group-topic-name)
1706     (completing-read "Move to topic: " gnus-topic-alist nil t)))
1707   (unless (and current to)
1708     (error "Can't find topic"))
1709   (let ((current-top (cdr (gnus-topic-find-topology current)))
1710         (to-top (cdr (gnus-topic-find-topology to))))
1711     (unless current-top
1712       (error "Can't find topic `%s'" current))
1713     (unless to-top
1714       (error "Can't find topic `%s'" to))
1715     (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1716         (error "Can't move `%s' to its sub-level" current))
1717     (gnus-topic-find-topology current nil nil 'delete)
1718     (while (cdr to-top)
1719       (setq to-top (cdr to-top)))
1720     (setcdr to-top (list current-top))
1721     (gnus-topic-enter-dribble)
1722     (gnus-group-list-groups)
1723     (gnus-topic-goto-topic current)))
1724
1725 (defun gnus-subscribe-topics (newsgroup)
1726   (catch 'end
1727     (let (match gnus-group-change-level-function)
1728       (dolist (topic (gnus-topic-list))
1729         (when (and (setq match (cdr (assq 'subscribe
1730                                           (gnus-topic-parameters topic))))
1731                    (string-match match newsgroup))
1732           ;; Just subscribe the group.
1733           (gnus-subscribe-alphabetically newsgroup)
1734           ;; Add the group to the topic.
1735           (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1736           ;; if this topic specifies a default level, use it
1737           (let ((subscribe-level (cdr (assq 'subscribe-level
1738                                             (gnus-topic-parameters topic)))))
1739             (when subscribe-level
1740                 (gnus-group-change-level newsgroup subscribe-level
1741                                          gnus-level-default-subscribed)))
1742           (throw 'end t)))
1743       nil)))
1744
1745 (provide 'gnus-topic)
1746
1747 ;;; gnus-topic.el ends here