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