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