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