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