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