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