(gnus-topic-goto-missing-topic): Just move to the
[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, 2003
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-remove-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         entry)
730     (while children
731       (incf unread (gnus-topic-unread (caar (pop children)))))
732     (while (setq entry (pop entries))
733       (when (numberp (car entry))
734         (incf unread (car entry))))
735     (gnus-topic-insert-topic-line
736      topic t t (car (gnus-topic-find-topology topic)) nil unread)))
737
738 (defun gnus-topic-goto-missing-topic (topic)
739   (if (gnus-topic-goto-topic topic)
740       (forward-line 1)
741     ;; Topic not displayed.
742     (let* ((top (gnus-topic-find-topology
743                  (gnus-topic-parent-topic topic)))
744            (tp (reverse (cddr top))))
745       (if (not top)
746           (gnus-topic-insert-topic-line
747            topic t t (car (gnus-topic-find-topology topic)) nil 0)
748         (while (not (equal (caaar tp) topic))
749           (setq tp (cdr tp)))
750         (pop tp)
751         (while (and tp
752                     (not (gnus-topic-goto-topic (caaar tp))))
753           (pop tp))
754         (if tp
755             (forward-line 1)
756           (gnus-topic-goto-missing-topic (caadr top)))))
757     nil))
758
759 (defun gnus-topic-update-topic-line (topic-name &optional reads)
760   (let* ((top (gnus-topic-find-topology topic-name))
761          (type (cadr top))
762          (children (cddr top))
763          (entries (gnus-topic-find-groups
764                    (car type) (car gnus-group-list-mode)
765                    (cdr gnus-group-list-mode)))
766          (parent (gnus-topic-parent-topic topic-name))
767          (all-entries entries)
768          (unread 0)
769          old-unread entry new-unread)
770     (when (gnus-topic-goto-topic (car type))
771       ;; Tally all the groups that belong in this topic.
772       (if reads
773           (setq unread (- (gnus-group-topic-unread) reads))
774         (while children
775           (incf unread (gnus-topic-unread (caar (pop children)))))
776         (while (setq entry (pop entries))
777           (when (numberp (car entry))
778             (incf unread (car entry)))))
779       (setq old-unread (gnus-group-topic-unread))
780       ;; Insert the topic line.
781       (gnus-topic-insert-topic-line
782        (car type) (gnus-topic-visible-p)
783        (not (eq (nth 2 type) 'hidden))
784        (gnus-group-topic-level) all-entries unread)
785       (gnus-delete-line)
786       (forward-line -1)
787       (setq new-unread (gnus-group-topic-unread)))
788     (when parent
789       (forward-line -1)
790       (gnus-topic-update-topic-line
791        parent
792        (- (or old-unread 0) (or new-unread 0))))
793     unread))
794
795 (defun gnus-topic-group-indentation ()
796   (make-string
797    (* gnus-topic-indent-level
798       (or (save-excursion
799             (forward-line -1)
800             (gnus-topic-goto-topic (gnus-current-topic))
801             (gnus-group-topic-level))
802           0))
803    ? ))
804
805 ;;; Initialization
806
807 (gnus-add-shutdown 'gnus-topic-close 'gnus)
808
809 (defun gnus-topic-close ()
810   (setq gnus-topic-active-topology nil
811         gnus-topic-active-alist nil
812         gnus-topic-killed-topics nil
813         gnus-topology-checked-p nil))
814
815 (defun gnus-topic-check-topology ()
816   ;; The first time we set the topology to whatever we have
817   ;; gotten here, which can be rather random.
818   (unless gnus-topic-alist
819     (gnus-topic-init-alist))
820
821   (setq gnus-topology-checked-p t)
822   ;; Go through the topic alist and make sure that all topics
823   ;; are in the topic topology.
824   (let ((topics (gnus-topic-list))
825         (alist gnus-topic-alist)
826         changed)
827     (while alist
828       (unless (member (caar alist) topics)
829         (nconc gnus-topic-topology
830                (list (list (list (caar alist) 'visible))))
831         (setq changed t))
832       (setq alist (cdr alist)))
833     (when changed
834       (gnus-topic-enter-dribble))
835     ;; Conversely, go through the topology and make sure that all
836     ;; topologies have alists.
837     (while topics
838       (unless (assoc (car topics) gnus-topic-alist)
839         (push (list (car topics)) gnus-topic-alist))
840       (pop topics)))
841   ;; Go through all living groups and make sure that
842   ;; they belong to some topic.
843   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
844                                          gnus-topic-alist)))
845          (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
846          (newsrc (cdr gnus-newsrc-alist))
847          group)
848     (while newsrc
849       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
850         (setcdr entry (list group))
851         (setq entry (cdr entry)))))
852   ;; Go through all topics and make sure they contain only living groups.
853   (let ((alist gnus-topic-alist)
854         topic)
855     (while (setq topic (pop alist))
856       (while (cdr topic)
857         (if (and (cadr topic)
858                  (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
859             (setq topic (cdr topic))
860           (setcdr topic (cddr topic)))))))
861
862 (defun gnus-topic-init-alist ()
863   "Initialize the topic structures."
864   (setq gnus-topic-topology
865         (cons (list "Gnus" 'visible)
866               (mapcar (lambda (topic)
867                         (list (list (car topic) 'visible)))
868                       '(("misc")))))
869   (setq gnus-topic-alist
870         (list (cons "misc"
871                     (mapcar (lambda (info) (gnus-info-group info))
872                             (cdr gnus-newsrc-alist)))
873               (list "Gnus")))
874   (gnus-topic-enter-dribble))
875
876 ;;; Maintenance
877
878 (defun gnus-topic-clean-alist ()
879   "Remove bogus groups from the topic alist."
880   (let ((topic-alist gnus-topic-alist)
881         result topic)
882     (unless gnus-killed-hashtb
883       (gnus-make-hashtable-from-killed))
884     (while (setq topic (pop topic-alist))
885       (let ((topic-name (pop topic))
886             group filtered-topic)
887         (while (setq group (pop topic))
888           (when (and (or (gnus-gethash group gnus-active-hashtb)
889                          (gnus-info-method (gnus-get-info group)))
890                      (not (gnus-gethash group gnus-killed-hashtb)))
891             (push group filtered-topic)))
892         (push (cons topic-name (nreverse filtered-topic)) result)))
893     (setq gnus-topic-alist (nreverse result))))
894
895 (defun gnus-topic-change-level (group level oldlevel &optional previous)
896   "Run when changing levels to enter/remove groups from topics."
897   (save-excursion
898     (set-buffer gnus-group-buffer)
899     (let ((buffer-read-only nil))
900       (unless gnus-topic-inhibit-change-level
901         (gnus-group-goto-group (or (car (nth 2 previous)) group))
902         (when (and gnus-topic-mode
903                    gnus-topic-alist
904                    (not gnus-topic-inhibit-change-level))
905           ;; Remove the group from the topics.
906           (if (and (< oldlevel gnus-level-zombie)
907                    (>= level gnus-level-zombie))
908               (let ((alist gnus-topic-alist))
909                 (while (gnus-group-goto-group group)
910                   (gnus-delete-line))
911                 (while alist
912                   (when (member group (car alist))
913                     (setcdr (car alist) (delete group (cdar alist))))
914                   (pop alist)))
915             ;; If the group is subscribed we enter it into the topics.
916             (when (and (< level gnus-level-zombie)
917                        (>= oldlevel gnus-level-zombie))
918               (let* ((prev (gnus-group-group-name))
919                      (gnus-topic-inhibit-change-level t)
920                      (gnus-group-indentation
921                       (make-string
922                        (* gnus-topic-indent-level
923                           (or (save-excursion
924                                 (gnus-topic-goto-topic (gnus-current-topic))
925                                 (gnus-group-topic-level))
926                               0))
927                        ? ))
928                      (yanked (list group))
929                      alist talist end)
930                 ;; Then we enter the yanked groups into the topics they belong
931                 ;; to.
932                 (when (setq alist (assoc (save-excursion
933                                            (forward-line -1)
934                                            (or
935                                             (gnus-current-topic)
936                                             (caar gnus-topic-topology)))
937                                          gnus-topic-alist))
938                   (setq talist alist)
939                   (when (stringp yanked)
940                     (setq yanked (list yanked)))
941                   (if (not prev)
942                       (nconc alist yanked)
943                     (if (not (cdr alist))
944                         (setcdr alist (nconc yanked (cdr alist)))
945                       (while (and (not end) (cdr alist))
946                         (when (equal (cadr alist) prev)
947                           (setcdr alist (nconc yanked (cdr alist)))
948                           (setq end t))
949                         (setq alist (cdr alist)))
950                       (unless end
951                         (nconc talist yanked))))))
952               (gnus-topic-update-topic))))))))
953
954 (defun gnus-topic-goto-next-group (group props)
955   "Go to group or the next group after group."
956   (if (not group)
957       (if (not (memq 'gnus-topic props))
958           (goto-char (point-max))
959         (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
960     (if (gnus-group-goto-group group)
961         t
962       ;; The group is no longer visible.
963       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
964              (after (cdr (member group (cdr list)))))
965         ;; First try to put point on a group after the current one.
966         (while (and after
967                     (not (gnus-group-goto-group (car after))))
968           (setq after (cdr after)))
969         ;; Then try to put point on a group before point.
970         (unless after
971           (setq after (cdr (member group (reverse (cdr list)))))
972           (while (and after
973                       (not (gnus-group-goto-group (car after))))
974             (setq after (cdr after))))
975         ;; Finally, just put point on the topic.
976         (if (not (car list))
977             (goto-char (point-min))
978           (unless after
979             (gnus-topic-goto-topic (car list))
980             (setq after nil)))
981         t))))
982
983 ;;; Topic-active functions
984
985 (defun gnus-topic-grok-active (&optional force)
986   "Parse all active groups and create topic structures for them."
987   ;; First we make sure that we have really read the active file.
988   (when (or force
989             (not gnus-topic-active-alist))
990     (let (groups)
991       ;; Get a list of all groups available.
992       (mapatoms (lambda (g) (when (symbol-value g)
993                               (push (symbol-name g) groups)))
994                 gnus-active-hashtb)
995       (setq groups (sort groups 'string<))
996       ;; Init the variables.
997       (setq gnus-topic-active-topology (list (list "" 'visible)))
998       (setq gnus-topic-active-alist nil)
999       ;; Descend the top-level hierarchy.
1000       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
1001       ;; Set the top-level topic names to something nice.
1002       (setcar (car gnus-topic-active-topology) "Gnus active")
1003       (setcar (car gnus-topic-active-alist) "Gnus active"))))
1004
1005 (defun gnus-topic-grok-active-1 (topology groups)
1006   (let* ((name (caar topology))
1007          (prefix (concat "^" (regexp-quote name)))
1008          tgroups ntopology group)
1009     (while (and groups
1010                 (string-match prefix (setq group (car groups))))
1011       (if (not (string-match "\\." group (match-end 0)))
1012           ;; There are no further hierarchies here, so we just
1013           ;; enter this group into the list belonging to this
1014           ;; topic.
1015           (push (pop groups) tgroups)
1016         ;; New sub-hierarchy, so we add it to the topology.
1017         (nconc topology (list (setq ntopology
1018                                     (list (list (substring
1019                                                  group 0 (match-end 0))
1020                                                 'invisible)))))
1021         ;; Descend the hierarchy.
1022         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
1023     ;; We remove the trailing "." from the topic name.
1024     (setq name
1025           (if (string-match "\\.$" name)
1026               (substring name 0 (match-beginning 0))
1027             name))
1028     ;; Add this topic and its groups to the topic alist.
1029     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
1030     (setcar (car topology) name)
1031     ;; We return the rest of the groups that didn't belong
1032     ;; to this topic.
1033     groups))
1034
1035 ;;; Topic mode, commands and keymap.
1036
1037 (defvar gnus-topic-mode-map nil)
1038 (defvar gnus-group-topic-map nil)
1039
1040 (unless gnus-topic-mode-map
1041   (setq gnus-topic-mode-map (make-sparse-keymap))
1042
1043   ;; Override certain group mode keys.
1044   (gnus-define-keys gnus-topic-mode-map
1045     "=" gnus-topic-select-group
1046     "\r" gnus-topic-select-group
1047     " " gnus-topic-read-group
1048     "\C-c\C-x" gnus-topic-expire-articles
1049     "c" gnus-topic-catchup-articles
1050     "\C-k" gnus-topic-kill-group
1051     "\C-y" gnus-topic-yank-group
1052     "\M-g" gnus-topic-get-new-news-this-topic
1053     "AT" gnus-topic-list-active
1054     "Gp" gnus-topic-edit-parameters
1055     "#" gnus-topic-mark-topic
1056     "\M-#" gnus-topic-unmark-topic
1057     [tab] gnus-topic-indent
1058     [(meta tab)] gnus-topic-unindent
1059     "\C-i" gnus-topic-indent
1060     "\M-\C-i" gnus-topic-unindent
1061     gnus-mouse-2 gnus-mouse-pick-topic)
1062
1063   ;; Define a new submap.
1064   (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
1065     "#" gnus-topic-mark-topic
1066     "\M-#" gnus-topic-unmark-topic
1067     "n" gnus-topic-create-topic
1068     "m" gnus-topic-move-group
1069     "D" gnus-topic-remove-group
1070     "c" gnus-topic-copy-group
1071     "h" gnus-topic-hide-topic
1072     "s" gnus-topic-show-topic
1073     "j" gnus-topic-jump-to-topic
1074     "M" gnus-topic-move-matching
1075     "C" gnus-topic-copy-matching
1076     "\M-p" gnus-topic-goto-previous-topic
1077     "\M-n" gnus-topic-goto-next-topic
1078     "\C-i" gnus-topic-indent
1079     [tab] gnus-topic-indent
1080     "r" gnus-topic-rename
1081     "\177" gnus-topic-delete
1082     [delete] gnus-topic-delete
1083     "H" gnus-topic-toggle-display-empty-topics)
1084
1085   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1086     "s" gnus-topic-sort-groups
1087     "a" gnus-topic-sort-groups-by-alphabet
1088     "u" gnus-topic-sort-groups-by-unread
1089     "l" gnus-topic-sort-groups-by-level
1090     "e" gnus-topic-sort-groups-by-server
1091     "v" gnus-topic-sort-groups-by-score
1092     "r" gnus-topic-sort-groups-by-rank
1093     "m" gnus-topic-sort-groups-by-method))
1094
1095 (defun gnus-topic-make-menu-bar ()
1096   (unless (boundp 'gnus-topic-menu)
1097     (easy-menu-define
1098      gnus-topic-menu gnus-topic-mode-map ""
1099      '("Topics"
1100        ["Toggle topics" gnus-topic-mode t]
1101        ("Groups"
1102         ["Copy..." gnus-topic-copy-group t]
1103         ["Move..." gnus-topic-move-group t]
1104         ["Remove" gnus-topic-remove-group t]
1105         ["Copy matching..." gnus-topic-copy-matching t]
1106         ["Move matching" gnus-topic-move-matching t])
1107        ("Topics"
1108         ["Goto..." gnus-topic-jump-to-topic t]
1109         ["Show" gnus-topic-show-topic t]
1110         ["Hide" gnus-topic-hide-topic t]
1111         ["Delete" gnus-topic-delete t]
1112         ["Rename..." gnus-topic-rename t]
1113         ["Create..." gnus-topic-create-topic t]
1114         ["Mark" gnus-topic-mark-topic t]
1115         ["Indent" gnus-topic-indent t]
1116         ["Sort" gnus-topic-sort-topics t]
1117         ["Previous topic" gnus-topic-goto-previous-topic t]
1118         ["Next topic" gnus-topic-goto-next-topic t]
1119         ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1120         ["Edit parameters" gnus-topic-edit-parameters t])
1121        ["List active" gnus-topic-list-active t]))))
1122
1123 (defun gnus-topic-mode (&optional arg redisplay)
1124   "Minor mode for topicsifying Gnus group buffers."
1125   (interactive (list current-prefix-arg t))
1126   (when (eq major-mode 'gnus-group-mode)
1127     (make-local-variable 'gnus-topic-mode)
1128     (setq gnus-topic-mode
1129           (if (null arg) (not gnus-topic-mode)
1130             (> (prefix-numeric-value arg) 0)))
1131     ;; Infest Gnus with topics.
1132     (if (not gnus-topic-mode)
1133         (setq gnus-goto-missing-group-function nil)
1134       (when (gnus-visual-p 'topic-menu 'menu)
1135         (gnus-topic-make-menu-bar))
1136       (gnus-set-format 'topic t)
1137       (gnus-add-minor-mode 'gnus-topic-mode " Topic"
1138                            gnus-topic-mode-map nil (lambda (&rest junk)
1139                                                      (interactive)
1140                                                      (gnus-topic-mode nil t)))
1141       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1142       (set (make-local-variable 'gnus-group-prepare-function)
1143            'gnus-group-prepare-topics)
1144       (set (make-local-variable 'gnus-group-get-parameter-function)
1145            'gnus-group-topic-parameters)
1146       (set (make-local-variable 'gnus-group-goto-next-group-function)
1147            'gnus-topic-goto-next-group)
1148       (set (make-local-variable 'gnus-group-indentation-function)
1149            'gnus-topic-group-indentation)
1150       (set (make-local-variable 'gnus-group-update-group-function)
1151            'gnus-topic-update-topics-containing-group)
1152       (set (make-local-variable 'gnus-group-sort-alist-function)
1153            'gnus-group-sort-topic)
1154       (setq gnus-group-change-level-function 'gnus-topic-change-level)
1155       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1156       (make-local-hook 'gnus-check-bogus-groups-hook)
1157       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1158                 nil 'local)
1159       (setq gnus-topology-checked-p nil)
1160       ;; We check the topology.
1161       (when gnus-newsrc-alist
1162         (gnus-topic-check-topology))
1163       (gnus-run-hooks 'gnus-topic-mode-hook))
1164     ;; Remove topic infestation.
1165     (unless gnus-topic-mode
1166       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1167       (setq gnus-group-change-level-function nil)
1168       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1169       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1170       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1171     (when redisplay
1172       (gnus-group-list-groups))))
1173
1174 (defun gnus-topic-select-group (&optional all)
1175   "Select this newsgroup.
1176 No article is selected automatically.
1177 If the group is opened, just switch the summary buffer.
1178 If ALL is non-nil, already read articles become readable.
1179 If ALL is a number, fetch this number of articles.
1180
1181 If performed over a topic line, toggle folding the topic."
1182   (interactive "P")
1183   (if (gnus-group-topic-p)
1184       (let ((gnus-group-list-mode
1185              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1186         (gnus-topic-fold all)
1187         (gnus-dribble-touch))
1188     (gnus-group-select-group all)))
1189
1190 (defun gnus-mouse-pick-topic (e)
1191   "Select the group or topic under the mouse pointer."
1192   (interactive "e")
1193   (mouse-set-point e)
1194   (gnus-topic-read-group nil))
1195
1196 (defun gnus-topic-expire-articles (topic)
1197   "Expire articles in this topic or group."
1198   (interactive (list (gnus-group-topic-name)))
1199   (if (not topic)
1200       (call-interactively 'gnus-group-expire-articles)
1201     (save-excursion
1202       (gnus-message 5 "Expiring groups in %s..." topic)
1203       (let ((gnus-group-marked
1204              (mapcar (lambda (entry) (car (nth 2 entry)))
1205                      (gnus-topic-find-groups topic gnus-level-killed t))))
1206         (gnus-group-expire-articles nil))
1207       (gnus-message 5 "Expiring groups in %s...done" topic))))
1208
1209 (defun gnus-topic-catchup-articles (topic)
1210   "Catchup this topic or group.
1211 Also see `gnus-group-catchup'."
1212   (interactive (list (gnus-group-topic-name)))
1213   (if (not topic)
1214       (call-interactively 'gnus-group-catchup-current)
1215     (save-excursion
1216       (let* ((groups
1217              (mapcar (lambda (entry) (car (nth 2 entry)))
1218                      (gnus-topic-find-groups topic gnus-level-killed t)))
1219              (buffer-read-only nil)
1220              (gnus-group-marked groups))
1221         (gnus-group-catchup-current)
1222         (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1223
1224 (defun gnus-topic-read-group (&optional all no-article group)
1225   "Read news in this newsgroup.
1226 If the prefix argument ALL is non-nil, already read articles become
1227 readable.  IF ALL is a number, fetch this number of articles.  If the
1228 optional argument NO-ARTICLE is non-nil, no article will be
1229 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1230 group.
1231
1232 If performed over a topic line, toggle folding the topic."
1233   (interactive "P")
1234   (if (gnus-group-topic-p)
1235       (let ((gnus-group-list-mode
1236              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1237         (gnus-topic-fold all))
1238     (gnus-group-read-group all no-article group)))
1239
1240 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1241   "Create a new TOPIC under PARENT.
1242 When used interactively, PARENT will be the topic under point."
1243   (interactive
1244    (list
1245     (read-string "New topic: ")
1246     (gnus-current-topic)))
1247   ;; Check whether this topic already exists.
1248   (when (gnus-topic-find-topology topic)
1249     (error "Topic already exists"))
1250   (unless parent
1251     (setq parent (caar gnus-topic-topology)))
1252   (let ((top (cdr (gnus-topic-find-topology parent)))
1253         (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1254     (unless top
1255       (error "No such parent topic: %s" parent))
1256     (if previous
1257         (progn
1258           (while (and (cdr top)
1259                       (not (equal (caaadr top) previous)))
1260             (setq top (cdr top)))
1261           (setcdr top (cons full-topic (cdr top))))
1262       (nconc top (list full-topic)))
1263     (unless (assoc topic gnus-topic-alist)
1264       (push (list topic) gnus-topic-alist)))
1265   (gnus-topic-enter-dribble)
1266   (gnus-group-list-groups)
1267   (gnus-topic-goto-topic topic))
1268
1269 ;; FIXME:
1270 ;;  1. When the marked groups are overlapped with the process
1271 ;;     region, the behavior of move or remove is not right.
1272 ;;  2. Can't process on several marked groups with a same name,
1273 ;;     because gnus-group-marked only keeps one copy.
1274
1275 (defun gnus-topic-move-group (n topic &optional copyp)
1276   "Move the next N groups to TOPIC.
1277 If COPYP, copy the groups instead."
1278   (interactive
1279    (list current-prefix-arg
1280          (gnus-completing-read "Move to topic" gnus-topic-alist nil t
1281                                'gnus-topic-history)))
1282   (let ((use-marked (and (not n) (not (gnus-region-active-p))
1283                          gnus-group-marked t))
1284         (groups (gnus-group-process-prefix n))
1285         (topicl (assoc topic gnus-topic-alist))
1286         (start-topic (gnus-group-topic-name))
1287         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1288         entry)
1289     (if (and (not groups) (not copyp) start-topic)
1290         (gnus-topic-move start-topic topic)
1291       (mapcar
1292        (lambda (g)
1293          (gnus-group-remove-mark g use-marked)
1294          (when (and
1295                 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1296                 (not copyp))
1297            (setcdr entry (gnus-delete-first g (cdr entry))))
1298          (nconc topicl (list g)))
1299        groups)
1300       (gnus-topic-enter-dribble)
1301       (if start-group
1302           (gnus-group-goto-group start-group)
1303         (gnus-topic-goto-topic start-topic))
1304       (gnus-group-list-groups))))
1305
1306 (defun gnus-topic-remove-group (&optional n)
1307   "Remove the current group from the topic."
1308   (interactive "P")
1309   (let ((use-marked (and (not n) (not (gnus-region-active-p))
1310                          gnus-group-marked t))
1311         (groups (gnus-group-process-prefix n)))
1312     (mapcar
1313      (lambda (group)
1314        (gnus-group-remove-mark group use-marked)
1315        (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1316              (buffer-read-only nil))
1317          (when (and topicl group)
1318            (gnus-delete-line)
1319            (gnus-delete-first group topicl))
1320          (gnus-topic-update-topic)))
1321      groups)
1322     (gnus-topic-enter-dribble)
1323     (gnus-group-position-point)))
1324
1325 (defun gnus-topic-copy-group (n topic)
1326   "Copy the current group to a topic."
1327   (interactive
1328    (list current-prefix-arg
1329          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1330   (gnus-topic-move-group n topic t))
1331
1332 (defun gnus-topic-kill-group (&optional n discard)
1333   "Kill the next N groups."
1334   (interactive "P")
1335   (if (gnus-group-topic-p)
1336       (let ((topic (gnus-group-topic-name)))
1337         (push (cons
1338                (gnus-topic-find-topology topic)
1339                (assoc topic gnus-topic-alist))
1340               gnus-topic-killed-topics)
1341         (gnus-topic-remove-topic nil t)
1342         (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1343         (gnus-topic-enter-dribble))
1344     (gnus-group-kill-group n discard)
1345     (if (not (gnus-group-topic-p))
1346         (gnus-topic-update-topic)
1347       ;; Move up one line so that we update the right topic.
1348       (forward-line -1)
1349       (gnus-topic-update-topic)
1350       (forward-line 1))))
1351
1352 (defun gnus-topic-yank-group (&optional arg)
1353   "Yank the last topic."
1354   (interactive "p")
1355   (if gnus-topic-killed-topics
1356       (let* ((previous
1357               (or (gnus-group-topic-name)
1358                   (gnus-topic-next-topic (gnus-current-topic))))
1359              (data (pop gnus-topic-killed-topics))
1360              (alist (cdr data))
1361              (item (cdar data)))
1362         (push alist gnus-topic-alist)
1363         (gnus-topic-create-topic
1364          (caar item) (gnus-topic-parent-topic previous) previous
1365          item)
1366         (gnus-topic-enter-dribble)
1367         (gnus-topic-goto-topic (caar item)))
1368     (let* ((prev (gnus-group-group-name))
1369            (gnus-topic-inhibit-change-level t)
1370            (gnus-group-indentation
1371             (make-string
1372              (* gnus-topic-indent-level
1373                 (or (save-excursion
1374                       (gnus-topic-goto-topic (gnus-current-topic))
1375                       (gnus-group-topic-level))
1376                     0))
1377              ? ))
1378            yanked alist)
1379       ;; We first yank the groups the normal way...
1380       (setq yanked (gnus-group-yank-group arg))
1381       ;; Then we enter the yanked groups into the topics they belong
1382       ;; to.
1383       (setq alist (assoc (save-excursion
1384                            (forward-line -1)
1385                            (gnus-current-topic))
1386                          gnus-topic-alist))
1387       (when (stringp yanked)
1388         (setq yanked (list yanked)))
1389       (if (not prev)
1390           (nconc alist yanked)
1391         (if (not (cdr alist))
1392             (setcdr alist (nconc yanked (cdr alist)))
1393           (while (cdr alist)
1394             (when (equal (cadr alist) prev)
1395               (setcdr alist (nconc yanked (cdr alist)))
1396               (setq alist nil))
1397             (setq alist (cdr alist))))))
1398     (gnus-topic-update-topic)))
1399
1400 (defun gnus-topic-hide-topic (&optional permanent)
1401   "Hide the current topic.
1402 If PERMANENT, make it stay hidden in subsequent sessions as well."
1403   (interactive "P")
1404   (when (gnus-current-topic)
1405     (gnus-topic-goto-topic (gnus-current-topic))
1406     (if permanent
1407         (setcar (cddr
1408                  (cadr
1409                   (gnus-topic-find-topology (gnus-current-topic))))
1410                 'hidden))
1411     (gnus-topic-remove-topic nil nil)))
1412
1413 (defun gnus-topic-show-topic (&optional permanent)
1414   "Show the hidden topic.
1415 If PERMANENT, make it stay shown in subsequent sessions as well."
1416   (interactive "P")
1417   (when (gnus-group-topic-p)
1418     (if (not permanent)
1419         (gnus-topic-remove-topic t nil)
1420       (let ((topic
1421              (gnus-topic-find-topology
1422               (completing-read "Show topic: " gnus-topic-alist nil t))))
1423         (setcar (cddr (cadr topic)) nil)
1424         (setcar (cdr (cadr topic)) 'visible)
1425         (gnus-group-list-groups)))))
1426
1427 (defun gnus-topic-mark-topic (topic &optional unmark recursive)
1428   "Mark all groups in the TOPIC with the process mark.
1429 If RECURSIVE is t, mark its subtopics too."
1430   (interactive (list (gnus-group-topic-name)
1431                      nil
1432                      (and current-prefix-arg t)))
1433   (if (not topic)
1434       (call-interactively 'gnus-group-mark-group)
1435     (save-excursion
1436       (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1437                                             recursive)))
1438         (while groups
1439           (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1440                    (gnus-info-group (nth 2 (pop groups)))))))))
1441
1442 (defun gnus-topic-unmark-topic (topic &optional dummy recursive)
1443   "Remove the process mark from all groups in the TOPIC.
1444 If RECURSIVE is t, unmark its subtopics too."
1445   (interactive (list (gnus-group-topic-name)
1446                      nil
1447                      (and current-prefix-arg t)))
1448   (if (not topic)
1449       (call-interactively 'gnus-group-unmark-group)
1450     (gnus-topic-mark-topic topic t recursive)))
1451
1452 (defun gnus-topic-get-new-news-this-topic (&optional n)
1453   "Check for new news in the current topic."
1454   (interactive "P")
1455   (if (not (gnus-group-topic-p))
1456       (gnus-group-get-new-news-this-group n)
1457     (let* ((topic (gnus-group-topic-name))
1458            (data (cadr (gnus-topic-find-topology topic))))
1459       (save-excursion
1460         (gnus-topic-mark-topic topic nil (and n t))
1461         (gnus-group-get-new-news-this-group))
1462       (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1463
1464 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1465   "Move all groups that match REGEXP to some topic."
1466   (interactive
1467    (let (topic)
1468      (nreverse
1469       (list
1470        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1471        (read-string (format "Move to %s (regexp): " topic))))))
1472   (gnus-group-mark-regexp regexp)
1473   (gnus-topic-move-group nil topic copyp))
1474
1475 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1476   "Copy all groups that match REGEXP to some topic."
1477   (interactive
1478    (let (topic)
1479      (nreverse
1480       (list
1481        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1482        (read-string (format "Copy to %s (regexp): " topic))))))
1483   (gnus-topic-move-matching regexp topic t))
1484
1485 (defun gnus-topic-delete (topic)
1486   "Delete a topic."
1487   (interactive (list (gnus-group-topic-name)))
1488   (unless topic
1489     (error "No topic to be deleted"))
1490   (let ((entry (assoc topic gnus-topic-alist))
1491         (buffer-read-only nil))
1492     (when (cdr entry)
1493       (error "Topic not empty"))
1494     ;; Delete if visible.
1495     (when (gnus-topic-goto-topic topic)
1496       (gnus-delete-line))
1497     ;; Remove from alist.
1498     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1499     ;; Remove from topology.
1500     (gnus-topic-find-topology topic nil nil 'delete)
1501     (gnus-dribble-touch)))
1502
1503 (defun gnus-topic-rename (old-name new-name)
1504   "Rename a topic."
1505   (interactive
1506    (let ((topic (gnus-current-topic)))
1507      (list topic
1508            (read-string (format "Rename %s to: " topic) topic))))
1509   ;; Check whether the new name exists.
1510   (when (gnus-topic-find-topology new-name)
1511     (error "Topic '%s' already exists" new-name))
1512   ;; "nil" is an invalid name, for reasons I'd rather not go
1513   ;; into here.  Trust me.
1514   (when (equal new-name "nil")
1515     (error "Invalid name: %s" nil))
1516   ;; Do the renaming.
1517   (let ((top (gnus-topic-find-topology old-name))
1518         (entry (assoc old-name gnus-topic-alist)))
1519     (when top
1520       (setcar (cadr top) new-name))
1521     (when entry
1522       (setcar entry new-name))
1523     (forward-line -1)
1524     (gnus-dribble-touch)
1525     (gnus-group-list-groups)
1526     (forward-line 1)))
1527
1528 (defun gnus-topic-indent (&optional unindent)
1529   "Indent a topic -- make it a sub-topic of the previous topic.
1530 If UNINDENT, remove an indentation."
1531   (interactive "P")
1532   (if unindent
1533       (gnus-topic-unindent)
1534     (let* ((topic (gnus-current-topic))
1535            (parent (gnus-topic-previous-topic topic))
1536            (buffer-read-only nil))
1537       (unless parent
1538         (error "Nothing to indent %s into" topic))
1539       (when topic
1540         (gnus-topic-goto-topic topic)
1541         (gnus-topic-kill-group)
1542         (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1543         (gnus-topic-create-topic
1544          topic parent nil (cdar (car gnus-topic-killed-topics)))
1545         (pop gnus-topic-killed-topics)
1546         (or (gnus-topic-goto-topic topic)
1547             (gnus-topic-goto-topic parent))))))
1548
1549 (defun gnus-topic-unindent ()
1550   "Unindent a topic."
1551   (interactive)
1552   (let* ((topic (gnus-current-topic))
1553          (parent (gnus-topic-parent-topic topic))
1554          (grandparent (gnus-topic-parent-topic parent)))
1555     (unless grandparent
1556       (error "Nothing to indent %s into" topic))
1557     (when topic
1558       (gnus-topic-goto-topic topic)
1559       (gnus-topic-kill-group)
1560       (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1561       (gnus-topic-create-topic
1562        topic grandparent (gnus-topic-next-topic parent)
1563        (cdar (car gnus-topic-killed-topics)))
1564       (pop gnus-topic-killed-topics)
1565       (gnus-topic-goto-topic topic))))
1566
1567 (defun gnus-topic-list-active (&optional force)
1568   "List all groups that Gnus knows about in a topicsified fashion.
1569 If FORCE, always re-read the active file."
1570   (interactive "P")
1571   (when force
1572     (gnus-get-killed-groups))
1573   (gnus-topic-grok-active force)
1574   (let ((gnus-topic-topology gnus-topic-active-topology)
1575         (gnus-topic-alist gnus-topic-active-alist)
1576         gnus-killed-list gnus-zombie-list)
1577     (gnus-group-list-groups gnus-level-killed nil 1)))
1578
1579 (defun gnus-topic-toggle-display-empty-topics ()
1580   "Show/hide topics that have no unread articles."
1581   (interactive)
1582   (setq gnus-topic-display-empty-topics
1583         (not gnus-topic-display-empty-topics))
1584   (gnus-group-list-groups)
1585   (message "%s empty topics"
1586            (if gnus-topic-display-empty-topics
1587                "Showing" "Hiding")))
1588
1589 ;;; Topic sorting functions
1590
1591 (defun gnus-topic-edit-parameters (group)
1592   "Edit the group parameters of GROUP.
1593 If performed on a topic, edit the topic parameters instead."
1594   (interactive (list (gnus-group-group-name)))
1595   (if group
1596       (gnus-group-edit-group-parameters group)
1597     (if (not (gnus-group-topic-p))
1598         (error "Nothing to edit on the current line")
1599       (let ((topic (gnus-group-topic-name)))
1600         (gnus-edit-form
1601          (gnus-topic-parameters topic)
1602          (format "Editing the topic parameters for `%s'."
1603                  (or group topic))
1604          `(lambda (form)
1605             (gnus-topic-set-parameters ,topic form)))))))
1606
1607 (defun gnus-group-sort-topic (func reverse)
1608   "Sort groups in the topics according to FUNC and REVERSE."
1609   (let ((alist gnus-topic-alist))
1610     (while alist
1611       ;; !!!Sometimes nil elements sneak into the alist,
1612       ;; for some reason or other.
1613       (setcar alist (delq nil (car alist)))
1614       (setcar alist (delete "dummy.group" (car alist)))
1615       (gnus-topic-sort-topic (pop alist) func reverse))))
1616
1617 (defun gnus-topic-sort-topic (topic func reverse)
1618   ;; Each topic only lists the name of the group, while
1619   ;; the sort predicates expect group infos as inputs.
1620   ;; So we first transform the group names into infos,
1621   ;; then sort, and then transform back into group names.
1622   (setcdr
1623    topic
1624    (mapcar
1625     (lambda (info) (gnus-info-group info))
1626     (sort
1627      (mapcar
1628       (lambda (group) (gnus-get-info group))
1629       (cdr topic))
1630      func)))
1631   ;; Do the reversal, if necessary.
1632   (when reverse
1633     (setcdr topic (nreverse (cdr topic)))))
1634
1635 (defun gnus-topic-sort-groups (func &optional reverse)
1636   "Sort the current topic according to FUNC.
1637 If REVERSE, reverse the sorting order."
1638   (interactive (list gnus-group-sort-function current-prefix-arg))
1639   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1640     (gnus-topic-sort-topic
1641      topic (gnus-make-sort-function func) reverse)
1642     (gnus-group-list-groups)))
1643
1644 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1645   "Sort the current topic alphabetically by group name.
1646 If REVERSE, sort in reverse order."
1647   (interactive "P")
1648   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1649
1650 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1651   "Sort the current topic by number of unread articles.
1652 If REVERSE, sort in reverse order."
1653   (interactive "P")
1654   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1655
1656 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1657   "Sort the current topic by group level.
1658 If REVERSE, sort in reverse order."
1659   (interactive "P")
1660   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1661
1662 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1663   "Sort the current topic by group score.
1664 If REVERSE, sort in reverse order."
1665   (interactive "P")
1666   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1667
1668 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1669   "Sort the current topic by group rank.
1670 If REVERSE, sort in reverse order."
1671   (interactive "P")
1672   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1673
1674 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1675   "Sort the current topic alphabetically by backend name.
1676 If REVERSE, sort in reverse order."
1677   (interactive "P")
1678   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1679
1680 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1681   "Sort the current topic alphabetically by server name.
1682 If REVERSE, sort in reverse order."
1683   (interactive "P")
1684   (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1685
1686 (defun gnus-topic-sort-topics-1 (top reverse)
1687   (if (cdr top)
1688       (let ((subtop
1689              (mapcar (gnus-byte-compile
1690                       `(lambda (top)
1691                          (gnus-topic-sort-topics-1 top ,reverse)))
1692                      (sort (cdr top)
1693                            (lambda (t1 t2)
1694                              (string-lessp (caar t1) (caar t2)))))))
1695         (setcdr top (if reverse (reverse subtop) subtop))))
1696   top)
1697
1698 (defun gnus-topic-sort-topics (&optional topic reverse)
1699   "Sort topics in TOPIC alphabetically by topic name.
1700 If REVERSE, reverse the sorting order."
1701   (interactive
1702    (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1703                           (gnus-current-topic))
1704          current-prefix-arg))
1705   (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1706                             gnus-topic-topology)))
1707     (gnus-topic-sort-topics-1 topic-topology reverse)
1708     (gnus-topic-enter-dribble)
1709     (gnus-group-list-groups)
1710     (gnus-topic-goto-topic topic)))
1711
1712 (defun gnus-topic-move (current to)
1713   "Move the CURRENT topic to TO."
1714   (interactive
1715    (list
1716     (gnus-group-topic-name)
1717     (completing-read "Move to topic: " gnus-topic-alist nil t)))
1718   (unless (and current to)
1719     (error "Can't find topic"))
1720   (let ((current-top (cdr (gnus-topic-find-topology current)))
1721         (to-top (cdr (gnus-topic-find-topology to))))
1722     (unless current-top
1723       (error "Can't find topic `%s'" current))
1724     (unless to-top
1725       (error "Can't find topic `%s'" to))
1726     (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1727         (error "Can't move `%s' to its sub-level" current))
1728     (gnus-topic-find-topology current nil nil 'delete)
1729     (while (cdr to-top)
1730       (setq to-top (cdr to-top)))
1731     (setcdr to-top (list current-top))
1732     (gnus-topic-enter-dribble)
1733     (gnus-group-list-groups)
1734     (gnus-topic-goto-topic current)))
1735
1736 (defun gnus-subscribe-topics (newsgroup)
1737   (catch 'end
1738     (let (match gnus-group-change-level-function)
1739       (dolist (topic (gnus-topic-list))
1740         (when (and (setq match (cdr (assq 'subscribe
1741                                           (gnus-topic-parameters topic))))
1742                    (string-match match newsgroup))
1743           ;; Just subscribe the group.
1744           (gnus-subscribe-alphabetically newsgroup)
1745           ;; Add the group to the topic.
1746           (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1747           ;; if this topic specifies a default level, use it
1748           (let ((subscribe-level (cdr (assq 'subscribe-level
1749                                             (gnus-topic-parameters topic)))))
1750             (when subscribe-level
1751                 (gnus-group-change-level newsgroup subscribe-level
1752                                          gnus-level-default-subscribed)))
1753           (throw 'end t)))
1754       nil)))
1755
1756 (provide 'gnus-topic)
1757
1758 ;;; gnus-topic.el ends here