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