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