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