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