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