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