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