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 the group is opened, just switch the summary buffer.
1126 If ALL is non-nil, already read articles become readable.
1127 If ALL is a number, fetch this number of articles.
1128
1129 If performed over a topic line, toggle folding the topic."
1130   (interactive "P")
1131   (if (gnus-group-topic-p)
1132       (let ((gnus-group-list-mode
1133              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1134         (gnus-topic-fold all)
1135         (gnus-dribble-touch))
1136     (gnus-group-select-group all)))
1137
1138 (defun gnus-mouse-pick-topic (e)
1139   "Select the group or topic under the mouse pointer."
1140   (interactive "e")
1141   (mouse-set-point e)
1142   (gnus-topic-read-group nil))
1143
1144 (defun gnus-topic-expire-articles (topic)
1145   "Expire articles in this topic or group."
1146   (interactive (list (gnus-group-topic-name)))
1147   (if (not topic)
1148       (call-interactively 'gnus-group-expire-articles)
1149     (save-excursion
1150       (gnus-message 5 "Expiring groups in %s..." topic)
1151       (let ((gnus-group-marked
1152              (mapcar (lambda (entry) (car (nth 2 entry)))
1153                      (gnus-topic-find-groups topic gnus-level-killed t))))
1154         (gnus-group-expire-articles nil))
1155       (gnus-message 5 "Expiring groups in %s...done" topic))))
1156
1157 (defun gnus-topic-read-group (&optional all no-article group)
1158   "Read news in this newsgroup.
1159 If the prefix argument ALL is non-nil, already read articles become
1160 readable.  IF ALL is a number, fetch this number of articles.  If the
1161 optional argument NO-ARTICLE is non-nil, no article will be
1162 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1163 group.
1164
1165 If performed over a topic line, toggle folding the topic."
1166   (interactive "P")
1167   (if (gnus-group-topic-p)
1168       (let ((gnus-group-list-mode
1169              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1170         (gnus-topic-fold all))
1171     (gnus-group-read-group all no-article group)))
1172
1173 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1174   "Create a new TOPIC under PARENT.
1175 When used interactively, PARENT will be the topic under point."
1176   (interactive
1177    (list
1178     (read-string "New topic: ")
1179     (gnus-current-topic)))
1180   ;; Check whether this topic already exists.
1181   (when (gnus-topic-find-topology topic)
1182     (error "Topic already exists"))
1183   (unless parent
1184     (setq parent (caar gnus-topic-topology)))
1185   (let ((top (cdr (gnus-topic-find-topology parent)))
1186         (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1187     (unless top
1188       (error "No such parent topic: %s" parent))
1189     (if previous
1190         (progn
1191           (while (and (cdr top)
1192                       (not (equal (caaadr top) previous)))
1193             (setq top (cdr top)))
1194           (setcdr top (cons full-topic (cdr top))))
1195       (nconc top (list full-topic)))
1196     (unless (assoc topic gnus-topic-alist)
1197       (push (list topic) gnus-topic-alist)))
1198   (gnus-topic-enter-dribble)
1199   (gnus-group-list-groups)
1200   (gnus-topic-goto-topic topic))
1201
1202 ;; FIXME:
1203 ;;  1. When the marked groups are overlapped with the process
1204 ;;     region, the behavior of move or remove is not right.
1205 ;;  2. Can't process on several marked groups with a same name,
1206 ;;     because gnus-group-marked only keeps one copy.
1207
1208 (defun gnus-topic-move-group (n topic &optional copyp)
1209   "Move the next N groups to TOPIC.
1210 If COPYP, copy the groups instead."
1211   (interactive
1212    (list current-prefix-arg
1213          (completing-read "Move to topic: " gnus-topic-alist nil t)))
1214   (let ((use-marked (and (not n) (not (gnus-region-active-p))
1215                          gnus-group-marked t))
1216         (groups (gnus-group-process-prefix n))
1217         (topicl (assoc topic gnus-topic-alist))
1218         (start-topic (gnus-group-topic-name))
1219         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1220         entry)
1221     (if (and (not groups) (not copyp) start-topic)
1222         (gnus-topic-move start-topic topic)
1223       (mapcar
1224        (lambda (g)
1225          (gnus-group-remove-mark g use-marked)
1226          (when (and
1227                 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1228                 (not copyp))
1229            (setcdr entry (gnus-delete-first g (cdr entry))))
1230          (nconc topicl (list g)))
1231        groups)
1232       (gnus-topic-enter-dribble)
1233       (if start-group
1234           (gnus-group-goto-group start-group)
1235         (gnus-topic-goto-topic start-topic))
1236       (gnus-group-list-groups))))
1237
1238 (defun gnus-topic-remove-group (&optional n)
1239   "Remove the current group from the topic."
1240   (interactive "P")
1241   (let ((use-marked (and (not n) (not (gnus-region-active-p))
1242                          gnus-group-marked t))
1243         (groups (gnus-group-process-prefix n)))
1244     (mapcar
1245      (lambda (group)
1246        (gnus-group-remove-mark group use-marked)
1247        (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1248              (buffer-read-only nil))
1249          (when (and topicl group)
1250            (gnus-delete-line)
1251            (gnus-delete-first group topicl))
1252          (gnus-topic-update-topic)))
1253      groups)
1254     (gnus-topic-enter-dribble)
1255     (gnus-group-position-point)))
1256
1257 (defun gnus-topic-copy-group (n topic)
1258   "Copy the current group to a topic."
1259   (interactive
1260    (list current-prefix-arg
1261          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1262   (gnus-topic-move-group n topic t))
1263
1264 (defun gnus-topic-kill-group (&optional n discard)
1265   "Kill the next N groups."
1266   (interactive "P")
1267   (if (gnus-group-topic-p)
1268       (let ((topic (gnus-group-topic-name)))
1269         (push (cons
1270                (gnus-topic-find-topology topic)
1271                (assoc topic gnus-topic-alist))
1272               gnus-topic-killed-topics)
1273         (gnus-topic-remove-topic nil t)
1274         (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1275         (gnus-topic-enter-dribble))
1276     (gnus-group-kill-group n discard)
1277     (if (not (gnus-group-topic-p))
1278         (gnus-topic-update-topic)
1279       ;; Move up one line so that we update the right topic.
1280       (forward-line -1)
1281       (gnus-topic-update-topic)
1282       (forward-line 1))))
1283
1284 (defun gnus-topic-yank-group (&optional arg)
1285   "Yank the last topic."
1286   (interactive "p")
1287   (if gnus-topic-killed-topics
1288       (let* ((previous
1289               (or (gnus-group-topic-name)
1290                   (gnus-topic-next-topic (gnus-current-topic))))
1291              (data (pop gnus-topic-killed-topics))
1292              (alist (cdr data))
1293              (item (cdar data)))
1294         (push alist gnus-topic-alist)
1295         (gnus-topic-create-topic
1296          (caar item) (gnus-topic-parent-topic previous) previous
1297          item)
1298         (gnus-topic-enter-dribble)
1299         (gnus-topic-goto-topic (caar item)))
1300     (let* ((prev (gnus-group-group-name))
1301            (gnus-topic-inhibit-change-level t)
1302            (gnus-group-indentation
1303             (make-string
1304              (* gnus-topic-indent-level
1305                 (or (save-excursion
1306                       (gnus-topic-goto-topic (gnus-current-topic))
1307                       (gnus-group-topic-level))
1308                     0))
1309              ? ))
1310            yanked alist)
1311       ;; We first yank the groups the normal way...
1312       (setq yanked (gnus-group-yank-group arg))
1313       ;; Then we enter the yanked groups into the topics they belong
1314       ;; to.
1315       (setq alist (assoc (save-excursion
1316                            (forward-line -1)
1317                            (gnus-current-topic))
1318                          gnus-topic-alist))
1319       (when (stringp yanked)
1320         (setq yanked (list yanked)))
1321       (if (not prev)
1322           (nconc alist yanked)
1323         (if (not (cdr alist))
1324             (setcdr alist (nconc yanked (cdr alist)))
1325           (while (cdr alist)
1326             (when (equal (cadr alist) prev)
1327               (setcdr alist (nconc yanked (cdr alist)))
1328               (setq alist nil))
1329             (setq alist (cdr alist))))))
1330     (gnus-topic-update-topic)))
1331
1332 (defun gnus-topic-hide-topic (&optional permanent)
1333   "Hide the current topic.
1334 If PERMANENT, make it stay hidden in subsequent sessions as well."
1335   (interactive "P")
1336   (when (gnus-current-topic)
1337     (gnus-topic-goto-topic (gnus-current-topic))
1338     (if permanent
1339         (setcar (cddr
1340                  (cadr
1341                   (gnus-topic-find-topology (gnus-current-topic))))
1342                 'hidden))
1343     (gnus-topic-remove-topic nil nil)))
1344
1345 (defun gnus-topic-show-topic (&optional permanent)
1346   "Show the hidden topic.
1347 If PERMANENT, make it stay shown in subsequent sessions as well."
1348   (interactive "P")
1349   (when (gnus-group-topic-p)
1350     (if (not permanent)
1351         (gnus-topic-remove-topic t nil)
1352       (let ((topic
1353              (gnus-topic-find-topology
1354               (completing-read "Show topic: " gnus-topic-alist nil t))))
1355         (setcar (cddr (cadr topic)) nil)
1356         (setcar (cdr (cadr topic)) 'visible)
1357         (gnus-group-list-groups)))))
1358
1359 (defun gnus-topic-mark-topic (topic &optional unmark recursive)
1360   "Mark all groups in the TOPIC with the process mark.
1361 If RECURSIVE is t, mark its subtopics too."
1362   (interactive (list (gnus-group-topic-name)
1363                      nil
1364                      (and current-prefix-arg t)))
1365   (if (not topic)
1366       (call-interactively 'gnus-group-mark-group)
1367     (save-excursion
1368       (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1369                                             recursive)))
1370         (while groups
1371           (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1372                    (gnus-info-group (nth 2 (pop groups)))))))))
1373
1374 (defun gnus-topic-unmark-topic (topic &optional dummy recursive)
1375   "Remove the process mark from all groups in the TOPIC.
1376 If RECURSIVE is t, unmark its subtopics too."
1377   (interactive (list (gnus-group-topic-name)
1378                      nil
1379                      (and current-prefix-arg t)))
1380   (if (not topic)
1381       (call-interactively 'gnus-group-unmark-group)
1382     (gnus-topic-mark-topic topic t recursive)))
1383
1384 (defun gnus-topic-get-new-news-this-topic (&optional n)
1385   "Check for new news in the current topic."
1386   (interactive "P")
1387   (if (not (gnus-group-topic-p))
1388       (gnus-group-get-new-news-this-group n)
1389     (let* ((topic (gnus-group-topic-name))
1390            (data (cadr (gnus-topic-find-topology topic))))
1391       (save-excursion
1392         (gnus-topic-mark-topic topic nil (and n t))
1393         (gnus-group-get-new-news-this-group))
1394       (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1395
1396 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1397   "Move all groups that match REGEXP to some topic."
1398   (interactive
1399    (let (topic)
1400      (nreverse
1401       (list
1402        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1403        (read-string (format "Move to %s (regexp): " topic))))))
1404   (gnus-group-mark-regexp regexp)
1405   (gnus-topic-move-group nil topic copyp))
1406
1407 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1408   "Copy all groups that match REGEXP to some topic."
1409   (interactive
1410    (let (topic)
1411      (nreverse
1412       (list
1413        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1414        (read-string (format "Copy to %s (regexp): " topic))))))
1415   (gnus-topic-move-matching regexp topic t))
1416
1417 (defun gnus-topic-delete (topic)
1418   "Delete a topic."
1419   (interactive (list (gnus-group-topic-name)))
1420   (unless topic
1421     (error "No topic to be deleted"))
1422   (let ((entry (assoc topic gnus-topic-alist))
1423         (buffer-read-only nil))
1424     (when (cdr entry)
1425       (error "Topic not empty"))
1426     ;; Delete if visible.
1427     (when (gnus-topic-goto-topic topic)
1428       (gnus-delete-line))
1429     ;; Remove from alist.
1430     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1431     ;; Remove from topology.
1432     (gnus-topic-find-topology topic nil nil 'delete)
1433     (gnus-dribble-touch)))
1434
1435 (defun gnus-topic-rename (old-name new-name)
1436   "Rename a topic."
1437   (interactive
1438    (let ((topic (gnus-current-topic)))
1439      (list topic
1440            (read-string "Rename topic to: " topic))))
1441   ;; Check whether the new name exists.
1442   (when (gnus-topic-find-topology new-name)
1443     (error "Topic '%s' already exists" new-name))
1444   ;; "nil" is an invalid name, for reasons I'd rather not go
1445   ;; into here.  Trust me.
1446   (when (equal new-name "nil")
1447     (error "Invalid name: %s" nil))
1448   ;; Do the renaming.
1449   (let ((top (gnus-topic-find-topology old-name))
1450         (entry (assoc old-name gnus-topic-alist)))
1451     (when top
1452       (setcar (cadr top) new-name))
1453     (when entry
1454       (setcar entry new-name))
1455     (forward-line -1)
1456     (gnus-dribble-touch)
1457     (gnus-group-list-groups)
1458     (forward-line 1)))
1459
1460 (defun gnus-topic-indent (&optional unindent)
1461   "Indent a topic -- make it a sub-topic of the previous topic.
1462 If UNINDENT, remove an indentation."
1463   (interactive "P")
1464   (if unindent
1465       (gnus-topic-unindent)
1466     (let* ((topic (gnus-current-topic))
1467            (parent (gnus-topic-previous-topic topic))
1468            (buffer-read-only nil))
1469       (unless parent
1470         (error "Nothing to indent %s into" topic))
1471       (when topic
1472         (gnus-topic-goto-topic topic)
1473         (gnus-topic-kill-group)
1474         (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1475         (gnus-topic-create-topic
1476          topic parent nil (cdaar gnus-topic-killed-topics))
1477         (pop gnus-topic-killed-topics)
1478         (or (gnus-topic-goto-topic topic)
1479             (gnus-topic-goto-topic parent))))))
1480
1481 (defun gnus-topic-unindent ()
1482   "Unindent a topic."
1483   (interactive)
1484   (let* ((topic (gnus-current-topic))
1485          (parent (gnus-topic-parent-topic topic))
1486          (grandparent (gnus-topic-parent-topic parent)))
1487     (unless grandparent
1488       (error "Nothing to indent %s into" topic))
1489     (when topic
1490       (gnus-topic-goto-topic topic)
1491       (gnus-topic-kill-group)
1492       (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1493       (gnus-topic-create-topic
1494        topic grandparent (gnus-topic-next-topic parent)
1495        (cdaar gnus-topic-killed-topics))
1496       (pop gnus-topic-killed-topics)
1497       (gnus-topic-goto-topic topic))))
1498
1499 (defun gnus-topic-list-active (&optional force)
1500   "List all groups that Gnus knows about in a topicsified fashion.
1501 If FORCE, always re-read the active file."
1502   (interactive "P")
1503   (when force
1504     (gnus-get-killed-groups))
1505   (gnus-topic-grok-active force)
1506   (let ((gnus-topic-topology gnus-topic-active-topology)
1507         (gnus-topic-alist gnus-topic-active-alist)
1508         gnus-killed-list gnus-zombie-list)
1509     (gnus-group-list-groups gnus-level-killed nil 1)))
1510
1511 (defun gnus-topic-toggle-display-empty-topics ()
1512   "Show/hide topics that have no unread articles."
1513   (interactive)
1514   (setq gnus-topic-display-empty-topics
1515         (not gnus-topic-display-empty-topics))
1516   (gnus-group-list-groups)
1517   (message "%s empty topics"
1518            (if gnus-topic-display-empty-topics
1519                "Showing" "Hiding")))
1520
1521 ;;; Topic sorting functions
1522
1523 (defun gnus-topic-edit-parameters (group)
1524   "Edit the group parameters of GROUP.
1525 If performed on a topic, edit the topic parameters instead."
1526   (interactive (list (gnus-group-group-name)))
1527   (if group
1528       (gnus-group-edit-group-parameters group)
1529     (if (not (gnus-group-topic-p))
1530         (error "Nothing to edit on the current line")
1531       (let ((topic (gnus-group-topic-name)))
1532         (gnus-edit-form
1533          (gnus-topic-parameters topic)
1534          (format "Editing the topic parameters for `%s'."
1535                  (or group topic))
1536          `(lambda (form)
1537             (gnus-topic-set-parameters ,topic form)))))))
1538
1539 (defun gnus-group-sort-topic (func reverse)
1540   "Sort groups in the topics according to FUNC and REVERSE."
1541   (let ((alist gnus-topic-alist))
1542     (while alist
1543       ;; !!!Sometimes nil elements sneak into the alist,
1544       ;; for some reason or other.
1545       (setcar alist (delq nil (car alist)))
1546       (setcar alist (delete "dummy.group" (car alist)))
1547       (gnus-topic-sort-topic (pop alist) func reverse))))
1548
1549 (defun gnus-topic-sort-topic (topic func reverse)
1550   ;; Each topic only lists the name of the group, while
1551   ;; the sort predicates expect group infos as inputs.
1552   ;; So we first transform the group names into infos,
1553   ;; then sort, and then transform back into group names.
1554   (setcdr
1555    topic
1556    (mapcar
1557     (lambda (info) (gnus-info-group info))
1558     (sort
1559      (mapcar
1560       (lambda (group) (gnus-get-info group))
1561       (cdr topic))
1562      func)))
1563   ;; Do the reversal, if necessary.
1564   (when reverse
1565     (setcdr topic (nreverse (cdr topic)))))
1566
1567 (defun gnus-topic-sort-groups (func &optional reverse)
1568   "Sort the current topic according to FUNC.
1569 If REVERSE, reverse the sorting order."
1570   (interactive (list gnus-group-sort-function current-prefix-arg))
1571   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1572     (gnus-topic-sort-topic
1573      topic (gnus-make-sort-function func) reverse)
1574     (gnus-group-list-groups)))
1575
1576 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1577   "Sort the current topic alphabetically by group name.
1578 If REVERSE, sort in reverse order."
1579   (interactive "P")
1580   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1581
1582 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1583   "Sort the current topic by number of unread articles.
1584 If REVERSE, sort in reverse order."
1585   (interactive "P")
1586   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1587
1588 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1589   "Sort the current topic by group level.
1590 If REVERSE, sort in reverse order."
1591   (interactive "P")
1592   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1593
1594 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1595   "Sort the current topic by group score.
1596 If REVERSE, sort in reverse order."
1597   (interactive "P")
1598   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1599
1600 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1601   "Sort the current topic by group rank.
1602 If REVERSE, sort in reverse order."
1603   (interactive "P")
1604   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1605
1606 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1607   "Sort the current topic alphabetically by backend name.
1608 If REVERSE, sort in reverse order."
1609   (interactive "P")
1610   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1611
1612 (defun gnus-topic-sort-topics-1 (top reverse)
1613   (if (cdr top)
1614       (let ((subtop
1615              (mapcar `(lambda (top)
1616                         (gnus-topic-sort-topics-1 top ,reverse))
1617                      (sort (cdr top)
1618                            '(lambda (t1 t2)
1619                               (string-lessp (caar t1) (caar t2)))))))
1620         (setcdr top (if reverse (reverse subtop) subtop))))
1621   top)
1622
1623 (defun gnus-topic-sort-topics (&optional topic reverse)
1624   "Sort topics in TOPIC alphabeticaly by topic name.
1625 If REVERSE, reverse the sorting order."
1626   (interactive
1627    (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1628                           (gnus-current-topic))
1629          current-prefix-arg))
1630   (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1631                             gnus-topic-topology)))
1632     (gnus-topic-sort-topics-1 topic-topology reverse)
1633     (gnus-topic-enter-dribble)
1634     (gnus-group-list-groups)
1635     (gnus-topic-goto-topic topic)))
1636
1637 (defun gnus-topic-move (current to)
1638   "Move the CURRENT topic to TO."
1639   (interactive
1640    (list
1641     (gnus-group-topic-name)
1642     (completing-read "Move to topic: " gnus-topic-alist nil t)))
1643   (unless (and current to)
1644     (error "Can't find topic"))
1645   (let ((current-top (cdr (gnus-topic-find-topology current)))
1646         (to-top (cdr (gnus-topic-find-topology to))))
1647     (unless current-top
1648       (error "Can't find topic `%s'" current))
1649     (unless to-top
1650       (error "Can't find topic `%s'" to))
1651     (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1652         (error "Can't move `%s' to its sub-level" current))
1653     (gnus-topic-find-topology current nil nil 'delete)
1654     (while (cdr to-top)
1655       (setq to-top (cdr to-top)))
1656     (setcdr to-top (list current-top))
1657     (gnus-topic-enter-dribble)
1658     (gnus-group-list-groups)
1659     (gnus-topic-goto-topic current)))
1660
1661 (defun gnus-subscribe-topics (newsgroup)
1662   (catch 'end
1663     (let (match gnus-group-change-level-function)
1664       (dolist (topic (gnus-topic-list))
1665         (when (and (setq match (cdr (assq 'subscribe
1666                                           (gnus-topic-parameters topic))))
1667                    (string-match match newsgroup))
1668           ;; Just subscribe the group.
1669           (gnus-subscribe-alphabetically newsgroup)
1670           ;; Add the group to the topic.
1671           (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1672           (throw 'end t)))
1673       nil)))
1674
1675 (provide 'gnus-topic)
1676
1677 ;;; gnus-topic.el ends here