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