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