1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Ilja Weis <kult@uni-paderborn.de>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
31 (eval-when-compile (require 'gnus-clfns))
38 (defgroup gnus-topic nil
42 (defvar gnus-topic-mode nil
43 "Minor mode for Gnus group buffers.")
45 (defcustom gnus-topic-mode-hook nil
46 "Hook run in topic mode buffers."
50 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
51 "Format of topic lines.
52 It works along the same lines as a normal formatting string,
53 with some simple extensions.
55 %i Indentation based on topic level.
57 %v Nothing if the topic is visible, \"...\" otherwise.
58 %g Number of groups in the topic.
59 %a Number of unread articles in the groups in the topic.
60 %A Number of unread articles in the groups in the topic and its subtopics.
65 (defcustom gnus-topic-indent-level 2
66 "*How much each subtopic should be indented."
70 (defcustom gnus-topic-display-empty-topics t
71 "*If non-nil, display the topic lines even of topics that have no unread articles."
75 ;; Internal variables.
77 (defvar gnus-topic-active-topology nil)
78 (defvar gnus-topic-active-alist nil)
79 (defvar gnus-topic-unreads nil)
81 (defvar gnus-topology-checked-p nil
82 "Whether the topology has been checked in this session.")
84 (defvar gnus-topic-killed-topics nil)
85 (defvar gnus-topic-inhibit-change-level nil)
87 (defconst gnus-topic-line-format-alist
91 (?g number-of-groups ?d)
92 (?a (gnus-topic-articles-in-topic entries) ?d)
93 (?A total-number-of-articles ?d)
96 (defvar gnus-topic-line-format-spec nil)
100 (defun gnus-group-topic-name ()
101 "The name of the topic on the current line."
102 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
103 (and topic (symbol-name topic))))
105 (defun gnus-group-topic-level ()
106 "The level of the topic on the current line."
107 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
109 (defun gnus-group-topic-unread ()
110 "The number of unread articles in topic on the current line."
111 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
113 (defun gnus-topic-unread (topic)
114 "Return the number of unread articles in TOPIC."
115 (or (cdr (assoc topic gnus-topic-unreads))
118 (defun gnus-group-topic-p ()
119 "Return non-nil if the current line is a topic."
120 (gnus-group-topic-name))
122 (defun gnus-topic-visible-p ()
123 "Return non-nil if the current topic is visible."
124 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
126 (defun gnus-topic-articles-in-topic (entries)
130 (when (numberp (setq number (car (pop entries))))
131 (incf total number)))
134 (defun gnus-group-topic (group)
135 "Return the topic GROUP is a member of."
136 (let ((alist gnus-topic-alist)
139 (when (member group (cdar alist))
140 (setq out (caar alist)
142 (setq alist (cdr alist)))
145 (defun gnus-group-parent-topic (group)
146 "Return the topic GROUP is member of by looking at the group buffer."
148 (set-buffer gnus-group-buffer)
149 (if (gnus-group-goto-group group)
151 (gnus-group-topic group))))
153 (defun gnus-topic-goto-topic (topic)
155 (gnus-goto-char (text-property-any (point-min) (point-max)
156 'gnus-topic (intern topic)))))
158 (defun gnus-topic-jump-to-topic (topic)
161 (list (completing-read "Go to topic: "
162 (mapcar 'list (gnus-topic-list))
164 (dolist (topic (gnus-current-topics topic))
166 (gnus-topic-goto-topic topic))
168 (defun gnus-current-topic ()
169 "Return the name of the current topic."
171 (or (get-text-property (point) 'gnus-topic)
173 (and (gnus-goto-char (previous-single-property-change
174 (point) 'gnus-topic))
175 (get-text-property (max (1- (point)) (point-min))
178 (symbol-name result))))
180 (defun gnus-current-topics (&optional topic)
181 "Return a list of all current topics, lowest in hierarchy first.
182 If TOPIC, start with that topic."
183 (let ((topic (or topic (gnus-current-topic)))
187 (setq topic (gnus-topic-parent-topic topic)))
190 (defun gnus-group-active-topic-p ()
191 "Say whether the current topic comes from the active topics."
194 (get-text-property (point) 'gnus-active)))
196 (defun gnus-topic-find-groups (topic &optional level all lowest recursive)
197 "Return entries for all visible groups in TOPIC.
198 If RECURSIVE is t, return groups in its subtopics too."
199 (let ((groups (cdr (assoc topic gnus-topic-alist)))
200 info clevel unread group params visible-groups entry active)
201 (setq lowest (or lowest 1))
202 (setq level (or level gnus-level-unsubscribed))
203 ;; We go through the newsrc to look for matches.
205 (when (setq group (pop groups))
206 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
208 params (gnus-info-params info)
209 active (gnus-active group)
210 unread (or (car entry)
211 (and (not (equal group "dummy.group"))
213 (- (1+ (cdr active)) (car active))))
214 clevel (or (gnus-info-level info)
215 (if (member group gnus-zombie-list)
216 gnus-level-zombie gnus-level-killed))))
218 info ; nil means that the group is dead.
220 (>= clevel lowest) ; Is inside the level we want.
222 (if (or (eq unread t)
224 gnus-group-list-inactive-groups
226 (and gnus-list-groups-with-ticked-articles
227 (cdr (assq 'tick (gnus-info-marks info))))
228 ;; Has right readedness.
229 ;; Check for permanent visibility.
230 (and gnus-permanently-visible-groups
231 (string-match gnus-permanently-visible-groups group))
232 (memq 'visible params)
233 (cdr (assq 'visible params)))
234 ;; Add this group to the list of visible groups.
235 (push (or entry group) visible-groups)))
236 (setq visible-groups (nreverse visible-groups))
239 (setq recursive (cdr (gnus-topic-find-topology topic))))
240 (mapcar (lambda (topic-topology)
242 (nconc visible-groups
243 (gnus-topic-find-groups
244 (caar topic-topology)
245 level all lowest topic-topology))))
249 (defun gnus-topic-previous-topic (topic)
250 "Return the previous topic on the same level as TOPIC."
251 (let ((top (cddr (gnus-topic-find-topology
252 (gnus-topic-parent-topic topic)))))
253 (unless (equal topic (caaar top))
254 (while (and top (not (equal (caaadr top) topic)))
255 (setq top (cdr top)))
258 (defun gnus-topic-parent-topic (topic &optional topology)
259 "Return the parent of TOPIC."
261 (setq topology gnus-topic-topology))
262 (let ((parent (car (pop topology)))
265 (not (setq found (equal (caaar topology) topic)))
266 (not (setq result (gnus-topic-parent-topic
267 topic (car topology)))))
268 (setq topology (cdr topology)))
269 (or result (and found parent))))
271 (defun gnus-topic-next-topic (topic &optional previous)
272 "Return the next sibling of TOPIC."
273 (let ((parentt (cddr (gnus-topic-find-topology
274 (gnus-topic-parent-topic topic))))
277 (not (equal (caaar parentt) topic)))
278 (setq prev (caaar parentt)
279 parentt (cdr parentt)))
284 (defun gnus-topic-forward-topic (num)
285 "Go to the next topic on the same level as the current one."
286 (let* ((topic (gnus-current-topic))
287 (way (if (< num 0) 'gnus-topic-previous-topic
288 'gnus-topic-next-topic))
290 (while (and (not (zerop num))
291 (setq topic (funcall way topic)))
292 (when (gnus-topic-goto-topic topic)
295 (goto-char (point-max)))
298 (defun gnus-topic-find-topology (topic &optional topology level remove)
299 "Return the topology of TOPIC."
301 (setq topology gnus-topic-topology)
305 (if (equal (caar topology) topic)
308 (delq topology remove))
309 (cons level topology))
310 (setq topology (cdr topology))
312 (not (setq result (gnus-topic-find-topology
313 topic (car topology) (1+ level)
315 (setq topology (cdr topology)))
318 (defvar gnus-tmp-topics nil)
319 (defun gnus-topic-list (&optional topology)
320 "Return a list of all topics in the topology."
322 (setq topology gnus-topic-topology
323 gnus-tmp-topics nil))
324 (push (caar topology) gnus-tmp-topics)
325 (mapcar 'gnus-topic-list (cdr topology))
328 ;;; Topic parameter jazz
330 (defun gnus-topic-parameters (topic)
331 "Return the parameters for TOPIC."
332 (let ((top (gnus-topic-find-topology topic)))
334 (nth 3 (cadr top)))))
336 (defun gnus-topic-set-parameters (topic parameters)
337 "Set the topic parameters of TOPIC to PARAMETERS."
338 (let ((top (gnus-topic-find-topology topic)))
340 (error "No such topic: %s" topic))
341 ;; We may have to extend if there is no parameters here
343 (unless (nthcdr 2 (cadr top))
344 (nconc (cadr top) (list nil)))
345 (unless (nthcdr 3 (cadr top))
346 (nconc (cadr top) (list nil)))
347 (setcar (nthcdr 3 (cadr top)) parameters)
349 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
351 (defun gnus-group-topic-parameters (group)
352 "Compute the group parameters for GROUP taking into account inheritance from topics."
353 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
355 (gnus-group-goto-group group)
357 (gnus-topic-hierarchical-parameters (gnus-current-topic))))))
359 (defun gnus-topic-hierarchical-parameters (topic)
360 "Return a topic list computed for TOPIC."
361 (let ((topics (gnus-current-topics topic))
362 params-list param out params)
364 (push (gnus-topic-parameters (pop topics)) params-list))
365 ;; We probably have lots of nil elements here, so
366 ;; we remove them. Probably faster than doing this "properly".
367 (setq params-list (delq nil params-list))
368 ;; Now we have all the parameters, so we go through them
369 ;; and do inheritance in the obvious way.
370 (while (setq params (pop params-list))
371 (while (setq param (pop params))
373 (setq param (cons param t)))
374 ;; Override any old versions of this param.
375 (gnus-pull (car param) out)
377 ;; Return the resulting parameter list.
380 ;;; General utility functions
382 (defun gnus-topic-enter-dribble ()
384 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
386 ;;; Generating group buffers
388 (defun gnus-group-prepare-topics (level &optional predicate lowest
389 regexp list-topic topic-level)
390 "List all newsgroups with unread articles of level LEVEL or lower.
391 Use the `gnus-group-topics' to sort the groups.
392 If PREDICTE is a function, list groups that the function returns non-nil;
393 if it is t, list groups that have no unread articles.
394 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
395 (set-buffer gnus-group-buffer)
396 (let ((buffer-read-only nil)
397 (lowest (or lowest 1))
399 (and gnus-group-listed-groups
400 (copy-sequence gnus-group-listed-groups))))
402 (when (or (not gnus-topic-alist)
403 (not gnus-topology-checked-p))
404 (gnus-topic-check-topology))
410 (when (or gnus-group-listed-groups
411 (and (>= level gnus-level-zombie)
412 (<= lowest gnus-level-zombie)))
413 (gnus-group-prepare-flat-list-dead
414 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
418 (when (or gnus-group-listed-groups
419 (and (>= level gnus-level-killed)
420 (<= lowest gnus-level-killed)))
421 (gnus-group-prepare-flat-list-dead
424 (gnus-delete-if (lambda (group)
425 (< (gnus-group-level group) gnus-level-killed))
427 (setq gnus-killed-list (sort gnus-killed-list 'string<)))
433 (when (or (< lowest gnus-level-zombie)
434 gnus-group-listed-groups)
436 (let ((top (gnus-topic-find-topology list-topic)))
437 (gnus-topic-prepare-topic (cdr top) (car top)
438 (or topic-level level) predicate
440 (gnus-topic-prepare-topic gnus-topic-topology 0
441 (or topic-level level) predicate
443 (gnus-group-set-mode-line)
444 (setq gnus-group-list-mode (cons level predicate))
445 (gnus-run-hooks 'gnus-group-prepare-hook))))
447 (defun gnus-topic-prepare-topic (topicl level &optional list-level
450 "Insert TOPIC into the group buffer.
451 If SILENT, don't insert anything. Return the number of unread
452 articles in the topic and its subtopics."
453 (let* ((type (pop topicl))
454 (entries (gnus-topic-find-groups
456 (if gnus-group-listed-groups
459 (or predicate gnus-group-listed-groups
461 (gnus-topic-hierarchical-parameters
463 (if gnus-group-listed-groups 0 lowest)))
464 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
465 (gnus-group-indentation
466 (make-string (* gnus-topic-indent-level level) ? ))
467 (beg (progn (beginning-of-line) (point)))
468 (topicl (reverse topicl))
469 (all-entries entries)
470 (point-max (point-max))
473 info entry end active tick)
474 ;; Insert any sub-topics.
477 (gnus-topic-prepare-topic
478 (pop topicl) (1+ level) list-level predicate
479 (not visiblep) lowest regexp)))
482 ;; Insert all the groups that belong in this topic.
483 (while (setq entry (pop entries))
484 (when (if (stringp entry)
485 (gnus-group-prepare-logic
488 (or (not gnus-group-listed-groups)
489 (if (< list-level gnus-level-zombie) nil
491 (if (member entry gnus-zombie-list)
492 gnus-level-zombie gnus-level-killed)))
493 (and (<= entry-level list-level)
494 (>= entry-level lowest)))))
497 (string-match regexp entry))
499 (funcall regexp entry))
502 (setq info (nth 2 entry))
503 (gnus-group-prepare-logic
504 (gnus-info-group info)
505 (and (or (not gnus-group-listed-groups)
506 (let ((entry-level (gnus-info-level info)))
507 (and (<= entry-level list-level)
508 (>= entry-level lowest))))
509 (or (not (functionp predicate))
510 (funcall predicate info))
511 (or (not (stringp regexp))
512 (string-match regexp (gnus-info-group info))))))
516 (gnus-group-insert-group-line
517 entry (if (member entry gnus-zombie-list)
518 gnus-level-zombie gnus-level-killed)
519 nil (- (1+ (cdr (setq active (gnus-active entry))))
523 (when (setq info (nth 2 entry))
524 (gnus-group-insert-group-line
525 (gnus-info-group info)
526 (gnus-info-level info) (gnus-info-marks info)
527 (car entry) (gnus-info-method info)))))
528 (when (and (listp entry)
529 (numberp (car entry)))
530 (incf unread (car entry)))
534 ;; Insert the topic line.
535 (when (and (not silent)
536 (or gnus-topic-display-empty-topics ;We want empty topics
537 (not (zerop unread)) ;Non-empty
538 tick ;Ticked articles
539 (/= point-max (point-max)))) ;Unactivated groups
540 (gnus-extent-start-open (point))
541 (gnus-topic-insert-topic-line
543 (not (eq (nth 2 type) 'hidden))
544 level all-entries unread))
545 (gnus-topic-update-unreads (car type) unread)
549 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
550 "Remove the current topic."
551 (let ((topic (gnus-group-topic-name))
552 (level (gnus-group-topic-level))
553 (beg (progn (beginning-of-line) (point)))
556 (while (and (zerop (forward-line 1))
557 (> (or (gnus-group-topic-level) (1+ level)) level)))
558 (delete-region beg (point))
559 ;; Do the change in this rather odd manner because it has been
560 ;; reported that some topics share parts of some lists, for some
561 ;; reason. I have been unable to determine why this is the
562 ;; case, but this hack seems to take care of things.
563 (let ((data (cadr (gnus-topic-find-topology topic))))
565 (list (if insert 'visible 'invisible)
569 (setq gnus-topic-alist
570 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
571 (gnus-topic-insert-topic topic in-level)))))
573 (defun gnus-topic-insert-topic (topic &optional level)
575 (gnus-group-prepare-topics
576 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
577 nil nil topic level))
579 (defun gnus-topic-fold (&optional insert topic)
580 "Remove/insert the current topic."
581 (let ((topic (or topic (gnus-group-topic-name))))
584 (if (not (gnus-group-active-topic-p))
585 (gnus-topic-remove-topic
586 (or insert (not (gnus-topic-visible-p))))
587 (let ((gnus-topic-topology gnus-topic-active-topology)
588 (gnus-topic-alist gnus-topic-active-alist)
589 (gnus-group-list-mode (cons 5 t)))
590 (gnus-topic-remove-topic
591 (or insert (not (gnus-topic-visible-p))) nil nil 9)
592 (gnus-topic-enter-dribble)))))))
594 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
596 (let* ((visible (if visiblep "" "..."))
597 (indentation (make-string (* gnus-topic-indent-level level) ? ))
598 (total-number-of-articles unread)
599 (number-of-groups (length entries))
600 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
602 (gnus-topic-update-unreads name unread)
606 (gnus-add-text-properties
609 (eval gnus-topic-line-format-spec))
610 (list 'gnus-topic (intern name)
611 'gnus-topic-level level
612 'gnus-topic-unread unread
613 'gnus-active active-topic
614 'gnus-topic-visible visiblep)))))
616 (defun gnus-topic-update-unreads (topic unreads)
617 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
619 (push (cons topic unreads) gnus-topic-unreads))
621 (defun gnus-topic-update-topics-containing-group (group)
622 "Update all topics that have GROUP as a member."
623 (when (and (eq major-mode 'gnus-group-mode)
626 (let ((alist gnus-topic-alist))
627 ;; This is probably not entirely correct. If a topic
628 ;; isn't shown, then it's not updated. But the updating
629 ;; should be performed in any case, since the topic's
630 ;; parent should be updated. Pfft.
632 (when (and (member group (cdar alist))
633 (gnus-topic-goto-topic (caar alist)))
634 (gnus-topic-update-topic-line (caar alist)))
637 (defun gnus-topic-update-topic ()
638 "Update all parent topics to the current group."
639 (when (and (eq major-mode 'gnus-group-mode)
641 (let ((group (gnus-group-group-name))
643 (buffer-read-only nil))
645 (gnus-get-info group)
646 (gnus-topic-goto-topic (gnus-current-topic)))
647 (gnus-topic-update-topic-line (gnus-group-topic-name))
650 (gnus-group-position-point)))))
652 (defun gnus-topic-goto-missing-group (group)
653 "Place point where GROUP is supposed to be inserted."
654 (let* ((topic (gnus-group-topic group))
655 (groups (cdr (assoc topic gnus-topic-alist)))
656 (g (cdr (member group groups)))
659 ;; Try to jump to a visible group.
660 (while (and g (not (gnus-group-goto-group (car g) t)))
662 ;; It wasn't visible, so we try to see where to insert it.
664 (setq g (cdr (member group (reverse groups))))
665 (while (and g unfound)
666 (when (gnus-group-goto-group (pop g) t)
671 (not (gnus-topic-goto-missing-topic topic)))
672 (let* ((top (gnus-topic-find-topology topic))
673 (children (cddr top))
676 (entries (gnus-topic-find-groups
677 (car type) (car gnus-group-list-mode)
678 (cdr gnus-group-list-mode))))
680 (incf unread (gnus-topic-unread (caar (pop children)))))
681 (while (setq entry (pop entries))
682 (when (numberp (car entry))
683 (incf unread (car entry))))
684 (gnus-topic-insert-topic-line
685 topic t t (car (gnus-topic-find-topology topic)) nil unread))))))
687 (defun gnus-topic-goto-missing-topic (topic)
688 (if (gnus-topic-goto-topic topic)
690 ;; Topic not displayed.
691 (let* ((top (gnus-topic-find-topology
692 (gnus-topic-parent-topic topic)))
693 (tp (reverse (cddr top))))
695 (gnus-topic-insert-topic-line
696 topic t t (car (gnus-topic-find-topology topic)) nil 0)
697 (while (not (equal (caaar tp) topic))
701 (not (gnus-topic-goto-topic (caaar tp))))
704 (gnus-topic-forward-topic 1)
705 (gnus-topic-goto-missing-topic (caadr top)))))
708 (defun gnus-topic-update-topic-line (topic-name &optional reads)
709 (let* ((top (gnus-topic-find-topology topic-name))
711 (children (cddr top))
712 (entries (gnus-topic-find-groups
713 (car type) (car gnus-group-list-mode)
714 (cdr gnus-group-list-mode)))
715 (parent (gnus-topic-parent-topic topic-name))
716 (all-entries entries)
718 old-unread entry new-unread)
719 (when (gnus-topic-goto-topic (car type))
720 ;; Tally all the groups that belong in this topic.
722 (setq unread (- (gnus-group-topic-unread) reads))
724 (incf unread (gnus-topic-unread (caar (pop children)))))
725 (while (setq entry (pop entries))
726 (when (numberp (car entry))
727 (incf unread (car entry)))))
728 (setq old-unread (gnus-group-topic-unread))
729 ;; Insert the topic line.
730 (gnus-topic-insert-topic-line
731 (car type) (gnus-topic-visible-p)
732 (not (eq (nth 2 type) 'hidden))
733 (gnus-group-topic-level) all-entries unread)
736 (setq new-unread (gnus-group-topic-unread)))
739 (gnus-topic-update-topic-line
741 (- (or old-unread 0) (or new-unread 0))))
744 (defun gnus-topic-group-indentation ()
746 (* gnus-topic-indent-level
749 (gnus-topic-goto-topic (gnus-current-topic))
750 (gnus-group-topic-level))
756 (gnus-add-shutdown 'gnus-topic-close 'gnus)
758 (defun gnus-topic-close ()
759 (setq gnus-topic-active-topology nil
760 gnus-topic-active-alist nil
761 gnus-topic-killed-topics nil
762 gnus-topology-checked-p nil))
764 (defun gnus-topic-check-topology ()
765 ;; The first time we set the topology to whatever we have
766 ;; gotten here, which can be rather random.
767 (unless gnus-topic-alist
768 (gnus-topic-init-alist))
770 (setq gnus-topology-checked-p t)
771 ;; Go through the topic alist and make sure that all topics
772 ;; are in the topic topology.
773 (let ((topics (gnus-topic-list))
774 (alist gnus-topic-alist)
777 (unless (member (caar alist) topics)
778 (nconc gnus-topic-topology
779 (list (list (list (caar alist) 'visible))))
781 (setq alist (cdr alist)))
783 (gnus-topic-enter-dribble))
784 ;; Conversely, go through the topology and make sure that all
785 ;; topologies have alists.
787 (unless (assoc (car topics) gnus-topic-alist)
788 (push (list (car topics)) gnus-topic-alist))
790 ;; Go through all living groups and make sure that
791 ;; they belong to some topic.
792 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
794 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
795 (newsrc (cdr gnus-newsrc-alist))
798 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
799 (setcdr entry (list group))
800 (setq entry (cdr entry)))))
801 ;; Go through all topics and make sure they contain only living groups.
802 (let ((alist gnus-topic-alist)
804 (while (setq topic (pop alist))
806 (if (and (cadr topic)
807 (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
808 (setq topic (cdr topic))
809 (setcdr topic (cddr topic)))))))
811 (defun gnus-topic-init-alist ()
812 "Initialize the topic structures."
813 (setq gnus-topic-topology
814 (cons (list "Gnus" 'visible)
815 (mapcar (lambda (topic)
816 (list (list (car topic) 'visible)))
818 (setq gnus-topic-alist
820 (mapcar (lambda (info) (gnus-info-group info))
821 (cdr gnus-newsrc-alist)))
823 (gnus-topic-enter-dribble))
827 (defun gnus-topic-clean-alist ()
828 "Remove bogus groups from the topic alist."
829 (let ((topic-alist gnus-topic-alist)
831 (unless gnus-killed-hashtb
832 (gnus-make-hashtable-from-killed))
833 (while (setq topic (pop topic-alist))
834 (let ((topic-name (pop topic))
835 group filtered-topic)
836 (while (setq group (pop topic))
837 (when (and (or (gnus-gethash group gnus-active-hashtb)
838 (gnus-info-method (gnus-get-info group)))
839 (not (gnus-gethash group gnus-killed-hashtb)))
840 (push group filtered-topic)))
841 (push (cons topic-name (nreverse filtered-topic)) result)))
842 (setq gnus-topic-alist (nreverse result))))
844 (defun gnus-topic-change-level (group level oldlevel &optional previous)
845 "Run when changing levels to enter/remove groups from topics."
847 (set-buffer gnus-group-buffer)
848 (let ((buffer-read-only nil))
849 (unless gnus-topic-inhibit-change-level
850 (gnus-group-goto-group (or (car (nth 2 previous)) group))
851 (when (and gnus-topic-mode
853 (not gnus-topic-inhibit-change-level))
854 ;; Remove the group from the topics.
855 (if (and (< oldlevel gnus-level-zombie)
856 (>= level gnus-level-zombie))
857 (let ((alist gnus-topic-alist))
858 (while (gnus-group-goto-group group)
861 (when (member group (car alist))
862 (setcdr (car alist) (delete group (cdar alist))))
864 ;; If the group is subscribed we enter it into the topics.
865 (when (and (< level gnus-level-zombie)
866 (>= oldlevel gnus-level-zombie))
867 (let* ((prev (gnus-group-group-name))
868 (gnus-topic-inhibit-change-level t)
869 (gnus-group-indentation
871 (* gnus-topic-indent-level
873 (gnus-topic-goto-topic (gnus-current-topic))
874 (gnus-group-topic-level))
877 (yanked (list group))
879 ;; Then we enter the yanked groups into the topics they belong
881 (when (setq alist (assoc (save-excursion
885 (caar gnus-topic-topology)))
888 (when (stringp yanked)
889 (setq yanked (list yanked)))
892 (if (not (cdr alist))
893 (setcdr alist (nconc yanked (cdr alist)))
894 (while (and (not end) (cdr alist))
895 (when (equal (cadr alist) prev)
896 (setcdr alist (nconc yanked (cdr alist)))
898 (setq alist (cdr alist)))
900 (nconc talist yanked))))))
901 (gnus-topic-update-topic))))))))
903 (defun gnus-topic-goto-next-group (group props)
904 "Go to group or the next group after group."
906 (if (not (memq 'gnus-topic props))
907 (goto-char (point-max))
908 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
909 (if (gnus-group-goto-group group)
911 ;; The group is no longer visible.
912 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
913 (after (cdr (member group (cdr list)))))
914 ;; First try to put point on a group after the current one.
916 (not (gnus-group-goto-group (car after))))
917 (setq after (cdr after)))
918 ;; Then try to put point on a group before point.
920 (setq after (cdr (member group (reverse (cdr list)))))
922 (not (gnus-group-goto-group (car after))))
923 (setq after (cdr after))))
924 ;; Finally, just put point on the topic.
926 (goto-char (point-min))
928 (gnus-topic-goto-topic (car list))
932 ;;; Topic-active functions
934 (defun gnus-topic-grok-active (&optional force)
935 "Parse all active groups and create topic structures for them."
936 ;; First we make sure that we have really read the active file.
938 (not gnus-topic-active-alist))
940 ;; Get a list of all groups available.
941 (mapatoms (lambda (g) (when (symbol-value g)
942 (push (symbol-name g) groups)))
944 (setq groups (sort groups 'string<))
945 ;; Init the variables.
946 (setq gnus-topic-active-topology (list (list "" 'visible)))
947 (setq gnus-topic-active-alist nil)
948 ;; Descend the top-level hierarchy.
949 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
950 ;; Set the top-level topic names to something nice.
951 (setcar (car gnus-topic-active-topology) "Gnus active")
952 (setcar (car gnus-topic-active-alist) "Gnus active"))))
954 (defun gnus-topic-grok-active-1 (topology groups)
955 (let* ((name (caar topology))
956 (prefix (concat "^" (regexp-quote name)))
957 tgroups ntopology group)
959 (string-match prefix (setq group (car groups))))
960 (if (not (string-match "\\." group (match-end 0)))
961 ;; There are no further hierarchies here, so we just
962 ;; enter this group into the list belonging to this
964 (push (pop groups) tgroups)
965 ;; New sub-hierarchy, so we add it to the topology.
966 (nconc topology (list (setq ntopology
967 (list (list (substring
968 group 0 (match-end 0))
970 ;; Descend the hierarchy.
971 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
972 ;; We remove the trailing "." from the topic name.
974 (if (string-match "\\.$" name)
975 (substring name 0 (match-beginning 0))
977 ;; Add this topic and its groups to the topic alist.
978 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
979 (setcar (car topology) name)
980 ;; We return the rest of the groups that didn't belong
984 ;;; Topic mode, commands and keymap.
986 (defvar gnus-topic-mode-map nil)
987 (defvar gnus-group-topic-map nil)
989 (unless gnus-topic-mode-map
990 (setq gnus-topic-mode-map (make-sparse-keymap))
992 ;; Override certain group mode keys.
993 (gnus-define-keys gnus-topic-mode-map
994 "=" gnus-topic-select-group
995 "\r" gnus-topic-select-group
996 " " gnus-topic-read-group
997 "\C-c\C-x" gnus-topic-expire-articles
998 "\C-k" gnus-topic-kill-group
999 "\C-y" gnus-topic-yank-group
1000 "\M-g" gnus-topic-get-new-news-this-topic
1001 "AT" gnus-topic-list-active
1002 "Gp" gnus-topic-edit-parameters
1003 "#" gnus-topic-mark-topic
1004 "\M-#" gnus-topic-unmark-topic
1005 [tab] gnus-topic-indent
1006 [(meta tab)] gnus-topic-unindent
1007 "\C-i" gnus-topic-indent
1008 "\M-\C-i" gnus-topic-unindent
1009 gnus-mouse-2 gnus-mouse-pick-topic)
1011 ;; Define a new submap.
1012 (gnus-define-keys (gnus-group-topic-map "T" gnus-topic-mode-map)
1013 "#" gnus-topic-mark-topic
1014 "\M-#" gnus-topic-unmark-topic
1015 "n" gnus-topic-create-topic
1016 "m" gnus-topic-move-group
1017 "D" gnus-topic-remove-group
1018 "c" gnus-topic-copy-group
1019 "h" gnus-topic-hide-topic
1020 "s" gnus-topic-show-topic
1021 "j" gnus-topic-jump-to-topic
1022 "M" gnus-topic-move-matching
1023 "C" gnus-topic-copy-matching
1024 "\C-i" gnus-topic-indent
1025 [tab] gnus-topic-indent
1026 "r" gnus-topic-rename
1027 "\177" gnus-topic-delete
1028 [delete] gnus-topic-delete
1029 "H" gnus-topic-toggle-display-empty-topics)
1031 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1032 "s" gnus-topic-sort-groups
1033 "a" gnus-topic-sort-groups-by-alphabet
1034 "u" gnus-topic-sort-groups-by-unread
1035 "l" gnus-topic-sort-groups-by-level
1036 "v" gnus-topic-sort-groups-by-score
1037 "r" gnus-topic-sort-groups-by-rank
1038 "m" gnus-topic-sort-groups-by-method))
1040 (defun gnus-topic-make-menu-bar ()
1041 (unless (boundp 'gnus-topic-menu)
1043 gnus-topic-menu gnus-topic-mode-map ""
1045 ["Toggle topics" gnus-topic-mode t]
1047 ["Copy" gnus-topic-copy-group t]
1048 ["Move" gnus-topic-move-group t]
1049 ["Remove" gnus-topic-remove-group t]
1050 ["Copy matching" gnus-topic-copy-matching t]
1051 ["Move matching" gnus-topic-move-matching t])
1053 ["Goto" gnus-topic-jump-to-topic t]
1054 ["Show" gnus-topic-show-topic t]
1055 ["Hide" gnus-topic-hide-topic t]
1056 ["Delete" gnus-topic-delete t]
1057 ["Rename" gnus-topic-rename t]
1058 ["Create" gnus-topic-create-topic t]
1059 ["Mark" gnus-topic-mark-topic t]
1060 ["Indent" gnus-topic-indent t]
1061 ["Sort" gnus-topic-sort-topics t]
1062 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1063 ["Edit parameters" gnus-topic-edit-parameters t])
1064 ["List active" gnus-topic-list-active t]))))
1066 (defun gnus-topic-mode (&optional arg redisplay)
1067 "Minor mode for topicsifying Gnus group buffers."
1068 (interactive (list current-prefix-arg t))
1069 (when (eq major-mode 'gnus-group-mode)
1070 (make-local-variable 'gnus-topic-mode)
1071 (setq gnus-topic-mode
1072 (if (null arg) (not gnus-topic-mode)
1073 (> (prefix-numeric-value arg) 0)))
1074 ;; Infest Gnus with topics.
1075 (if (not gnus-topic-mode)
1076 (setq gnus-goto-missing-group-function nil)
1077 (when (gnus-visual-p 'topic-menu 'menu)
1078 (gnus-topic-make-menu-bar))
1079 (gnus-set-format 'topic t)
1080 (gnus-add-minor-mode 'gnus-topic-mode " Topic"
1081 gnus-topic-mode-map nil (lambda (&rest junk)
1083 (gnus-topic-mode nil t)))
1084 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1085 (set (make-local-variable 'gnus-group-prepare-function)
1086 'gnus-group-prepare-topics)
1087 (set (make-local-variable 'gnus-group-get-parameter-function)
1088 'gnus-group-topic-parameters)
1089 (set (make-local-variable 'gnus-group-goto-next-group-function)
1090 'gnus-topic-goto-next-group)
1091 (set (make-local-variable 'gnus-group-indentation-function)
1092 'gnus-topic-group-indentation)
1093 (set (make-local-variable 'gnus-group-update-group-function)
1094 'gnus-topic-update-topics-containing-group)
1095 (set (make-local-variable 'gnus-group-sort-alist-function)
1096 'gnus-group-sort-topic)
1097 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1098 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1099 (make-local-hook 'gnus-check-bogus-groups-hook)
1100 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1101 (setq gnus-topology-checked-p nil)
1102 ;; We check the topology.
1103 (when gnus-newsrc-alist
1104 (gnus-topic-check-topology))
1105 (gnus-run-hooks 'gnus-topic-mode-hook))
1106 ;; Remove topic infestation.
1107 (unless gnus-topic-mode
1108 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1109 (setq gnus-group-change-level-function nil)
1110 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1111 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1112 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1114 (gnus-group-list-groups))))
1116 (defun gnus-topic-select-group (&optional all)
1117 "Select this newsgroup.
1118 No article is selected automatically.
1119 If ALL is non-nil, already read articles become readable.
1120 If ALL is a number, fetch this number of articles.
1122 If performed over a topic line, toggle folding the topic."
1124 (if (gnus-group-topic-p)
1125 (let ((gnus-group-list-mode
1126 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1127 (gnus-topic-fold all)
1128 (gnus-dribble-touch))
1129 (gnus-group-select-group all)))
1131 (defun gnus-mouse-pick-topic (e)
1132 "Select the group or topic under the mouse pointer."
1135 (gnus-topic-read-group nil))
1137 (defun gnus-topic-expire-articles (topic)
1138 "Expire articles in this topic or group."
1139 (interactive (list (gnus-group-topic-name)))
1141 (call-interactively 'gnus-group-expire-articles)
1143 (gnus-message 5 "Expiring groups in %s..." topic)
1144 (let ((gnus-group-marked
1145 (mapcar (lambda (entry) (car (nth 2 entry)))
1146 (gnus-topic-find-groups topic gnus-level-killed t))))
1147 (gnus-group-expire-articles nil))
1148 (gnus-message 5 "Expiring groups in %s...done" topic))))
1150 (defun gnus-topic-read-group (&optional all no-article group)
1151 "Read news in this newsgroup.
1152 If the prefix argument ALL is non-nil, already read articles become
1153 readable. IF ALL is a number, fetch this number of articles. If the
1154 optional argument NO-ARTICLE is non-nil, no article will be
1155 auto-selected upon group entry. If GROUP is non-nil, fetch that
1158 If performed over a topic line, toggle folding the topic."
1160 (if (gnus-group-topic-p)
1161 (let ((gnus-group-list-mode
1162 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1163 (gnus-topic-fold all))
1164 (gnus-group-read-group all no-article group)))
1166 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1167 "Create a new TOPIC under PARENT.
1168 When used interactively, PARENT will be the topic under point."
1171 (read-string "New topic: ")
1172 (gnus-current-topic)))
1173 ;; Check whether this topic already exists.
1174 (when (gnus-topic-find-topology topic)
1175 (error "Topic already exists"))
1177 (setq parent (caar gnus-topic-topology)))
1178 (let ((top (cdr (gnus-topic-find-topology parent)))
1179 (full-topic (or full-topic `((,topic visible)))))
1181 (error "No such parent topic: %s" parent))
1184 (while (and (cdr top)
1185 (not (equal (caaadr top) previous)))
1186 (setq top (cdr top)))
1187 (setcdr top (cons full-topic (cdr top))))
1188 (nconc top (list full-topic)))
1189 (unless (assoc topic gnus-topic-alist)
1190 (push (list topic) gnus-topic-alist)))
1191 (gnus-topic-enter-dribble)
1192 (gnus-group-list-groups)
1193 (gnus-topic-goto-topic topic))
1196 ;; 1. When the marked groups are overlapped with the process
1197 ;; region, the behavior of move or remove is not right.
1198 ;; 2. Can't process on several marked groups with a same name,
1199 ;; because gnus-group-marked only keeps one copy.
1201 (defun gnus-topic-move-group (n topic &optional copyp)
1202 "Move the next N groups to TOPIC.
1203 If COPYP, copy the groups instead."
1205 (list current-prefix-arg
1206 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1207 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1208 gnus-group-marked t))
1209 (groups (gnus-group-process-prefix n))
1210 (topicl (assoc topic gnus-topic-alist))
1211 (start-topic (gnus-group-topic-name))
1212 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1214 (if (and (not groups) (not copyp) start-topic)
1215 (gnus-topic-move start-topic topic)
1218 (gnus-group-remove-mark g use-marked)
1220 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1222 (setcdr entry (gnus-delete-first g (cdr entry))))
1223 (nconc topicl (list g)))
1225 (gnus-topic-enter-dribble)
1227 (gnus-group-goto-group start-group)
1228 (gnus-topic-goto-topic start-topic))
1229 (gnus-group-list-groups))))
1231 (defun gnus-topic-remove-group (&optional n)
1232 "Remove the current group from the topic."
1234 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1235 gnus-group-marked t))
1236 (groups (gnus-group-process-prefix n)))
1239 (gnus-group-remove-mark group use-marked)
1240 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1241 (buffer-read-only nil))
1242 (when (and topicl group)
1244 (gnus-delete-first group topicl))
1245 (gnus-topic-update-topic)))
1247 (gnus-topic-enter-dribble)
1248 (gnus-group-position-point)))
1250 (defun gnus-topic-copy-group (n topic)
1251 "Copy the current group to a topic."
1253 (list current-prefix-arg
1254 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1255 (gnus-topic-move-group n topic t))
1257 (defun gnus-topic-kill-group (&optional n discard)
1258 "Kill the next N groups."
1260 (if (gnus-group-topic-p)
1261 (let ((topic (gnus-group-topic-name)))
1263 (gnus-topic-find-topology topic)
1264 (assoc topic gnus-topic-alist))
1265 gnus-topic-killed-topics)
1266 (gnus-topic-remove-topic nil t)
1267 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1268 (gnus-topic-enter-dribble))
1269 (gnus-group-kill-group n discard)
1270 (if (not (gnus-group-topic-p))
1271 (gnus-topic-update-topic)
1272 ;; Move up one line so that we update the right topic.
1274 (gnus-topic-update-topic)
1277 (defun gnus-topic-yank-group (&optional arg)
1278 "Yank the last topic."
1280 (if gnus-topic-killed-topics
1282 (or (gnus-group-topic-name)
1283 (gnus-topic-next-topic (gnus-current-topic))))
1284 (data (pop gnus-topic-killed-topics))
1287 (push alist gnus-topic-alist)
1288 (gnus-topic-create-topic
1289 (caar item) (gnus-topic-parent-topic previous) previous
1291 (gnus-topic-enter-dribble)
1292 (gnus-topic-goto-topic (caar item)))
1293 (let* ((prev (gnus-group-group-name))
1294 (gnus-topic-inhibit-change-level t)
1295 (gnus-group-indentation
1297 (* gnus-topic-indent-level
1299 (gnus-topic-goto-topic (gnus-current-topic))
1300 (gnus-group-topic-level))
1304 ;; We first yank the groups the normal way...
1305 (setq yanked (gnus-group-yank-group arg))
1306 ;; Then we enter the yanked groups into the topics they belong
1308 (setq alist (assoc (save-excursion
1310 (gnus-current-topic))
1312 (when (stringp yanked)
1313 (setq yanked (list yanked)))
1315 (nconc alist yanked)
1316 (if (not (cdr alist))
1317 (setcdr alist (nconc yanked (cdr alist)))
1319 (when (equal (cadr alist) prev)
1320 (setcdr alist (nconc yanked (cdr alist)))
1322 (setq alist (cdr alist))))))
1323 (gnus-topic-update-topic)))
1325 (defun gnus-topic-hide-topic (&optional permanent)
1326 "Hide the current topic.
1327 If PERMANENT, make it stay hidden in subsequent sessions as well."
1329 (when (gnus-current-topic)
1330 (gnus-topic-goto-topic (gnus-current-topic))
1334 (gnus-topic-find-topology (gnus-current-topic))))
1336 (gnus-topic-remove-topic nil nil)))
1338 (defun gnus-topic-show-topic (&optional permanent)
1339 "Show the hidden topic.
1340 If PERMANENT, make it stay shown in subsequent sessions as well."
1342 (when (gnus-group-topic-p)
1344 (gnus-topic-remove-topic t nil)
1346 (gnus-topic-find-topology
1347 (completing-read "Show topic: " gnus-topic-alist nil t))))
1348 (setcar (cddr (cadr topic)) nil)
1349 (setcar (cdr (cadr topic)) 'visible)
1350 (gnus-group-list-groups)))))
1352 (defun gnus-topic-mark-topic (topic &optional unmark recursive)
1353 "Mark all groups in the TOPIC with the process mark.
1354 If RECURSIVE is t, mark its subtopics too."
1355 (interactive (list (gnus-group-topic-name)
1357 (and current-prefix-arg t)))
1359 (call-interactively 'gnus-group-mark-group)
1361 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1364 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1365 (gnus-info-group (nth 2 (pop groups)))))))))
1367 (defun gnus-topic-unmark-topic (topic &optional dummy recursive)
1368 "Remove the process mark from all groups in the TOPIC.
1369 If RECURSIVE is t, unmark its subtopics too."
1370 (interactive (list (gnus-group-topic-name)
1372 (and current-prefix-arg t)))
1374 (call-interactively 'gnus-group-unmark-group)
1375 (gnus-topic-mark-topic topic t recursive)))
1377 (defun gnus-topic-get-new-news-this-topic (&optional n)
1378 "Check for new news in the current topic."
1380 (if (not (gnus-group-topic-p))
1381 (gnus-group-get-new-news-this-group n)
1382 (gnus-topic-mark-topic (gnus-group-topic-name) nil (and n t))
1383 (gnus-group-get-new-news-this-group)))
1385 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1386 "Move all groups that match REGEXP to some topic."
1391 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1392 (read-string (format "Move to %s (regexp): " topic))))))
1393 (gnus-group-mark-regexp regexp)
1394 (gnus-topic-move-group nil topic copyp))
1396 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1397 "Copy all groups that match REGEXP to some topic."
1402 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1403 (read-string (format "Copy to %s (regexp): " topic))))))
1404 (gnus-topic-move-matching regexp topic t))
1406 (defun gnus-topic-delete (topic)
1408 (interactive (list (gnus-group-topic-name)))
1410 (error "No topic to be deleted"))
1411 (let ((entry (assoc topic gnus-topic-alist))
1412 (buffer-read-only nil))
1414 (error "Topic not empty"))
1415 ;; Delete if visible.
1416 (when (gnus-topic-goto-topic topic)
1418 ;; Remove from alist.
1419 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1420 ;; Remove from topology.
1421 (gnus-topic-find-topology topic nil nil 'delete)
1422 (gnus-dribble-touch)))
1424 (defun gnus-topic-rename (old-name new-name)
1427 (let ((topic (gnus-current-topic)))
1429 (read-string "Rename topic to: " topic))))
1430 ;; Check whether the new name exists.
1431 (when (gnus-topic-find-topology new-name)
1432 (error "Topic '%s' already exists" new-name))
1433 ;; "nil" is an invalid name, for reasons I'd rather not go
1434 ;; into here. Trust me.
1435 (when (equal new-name "nil")
1436 (error "Invalid name: %s" nil))
1438 (let ((top (gnus-topic-find-topology old-name))
1439 (entry (assoc old-name gnus-topic-alist)))
1441 (setcar (cadr top) new-name))
1443 (setcar entry new-name))
1445 (gnus-dribble-touch)
1446 (gnus-group-list-groups)
1449 (defun gnus-topic-indent (&optional unindent)
1450 "Indent a topic -- make it a sub-topic of the previous topic.
1451 If UNINDENT, remove an indentation."
1454 (gnus-topic-unindent)
1455 (let* ((topic (gnus-current-topic))
1456 (parent (gnus-topic-previous-topic topic))
1457 (buffer-read-only nil))
1459 (error "Nothing to indent %s into" topic))
1461 (gnus-topic-goto-topic topic)
1462 (gnus-topic-kill-group)
1463 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1464 (gnus-topic-create-topic
1465 topic parent nil (cdaar gnus-topic-killed-topics))
1466 (pop gnus-topic-killed-topics)
1467 (or (gnus-topic-goto-topic topic)
1468 (gnus-topic-goto-topic parent))))))
1470 (defun gnus-topic-unindent ()
1473 (let* ((topic (gnus-current-topic))
1474 (parent (gnus-topic-parent-topic topic))
1475 (grandparent (gnus-topic-parent-topic parent)))
1477 (error "Nothing to indent %s into" topic))
1479 (gnus-topic-goto-topic topic)
1480 (gnus-topic-kill-group)
1481 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1482 (gnus-topic-create-topic
1483 topic grandparent (gnus-topic-next-topic parent)
1484 (cdaar gnus-topic-killed-topics))
1485 (pop gnus-topic-killed-topics)
1486 (gnus-topic-goto-topic topic))))
1488 (defun gnus-topic-list-active (&optional force)
1489 "List all groups that Gnus knows about in a topicsified fashion.
1490 If FORCE, always re-read the active file."
1493 (gnus-get-killed-groups))
1494 (gnus-topic-grok-active force)
1495 (let ((gnus-topic-topology gnus-topic-active-topology)
1496 (gnus-topic-alist gnus-topic-active-alist)
1497 gnus-killed-list gnus-zombie-list)
1498 (gnus-group-list-groups gnus-level-killed nil 1)))
1500 (defun gnus-topic-toggle-display-empty-topics ()
1501 "Show/hide topics that have no unread articles."
1503 (setq gnus-topic-display-empty-topics
1504 (not gnus-topic-display-empty-topics))
1505 (gnus-group-list-groups)
1506 (message "%s empty topics"
1507 (if gnus-topic-display-empty-topics
1508 "Showing" "Hiding")))
1510 ;;; Topic sorting functions
1512 (defun gnus-topic-edit-parameters (group)
1513 "Edit the group parameters of GROUP.
1514 If performed on a topic, edit the topic parameters instead."
1515 (interactive (list (gnus-group-group-name)))
1517 (gnus-group-edit-group-parameters group)
1518 (if (not (gnus-group-topic-p))
1519 (error "Nothing to edit on the current line")
1520 (let ((topic (gnus-group-topic-name)))
1522 (gnus-topic-parameters topic)
1523 (format "Editing the topic parameters for `%s'."
1526 (gnus-topic-set-parameters ,topic form)))))))
1528 (defun gnus-group-sort-topic (func reverse)
1529 "Sort groups in the topics according to FUNC and REVERSE."
1530 (let ((alist gnus-topic-alist))
1532 ;; !!!Sometimes nil elements sneak into the alist,
1533 ;; for some reason or other.
1534 (setcar alist (delq nil (car alist)))
1535 (setcar alist (delete "dummy.group" (car alist)))
1536 (gnus-topic-sort-topic (pop alist) func reverse))))
1538 (defun gnus-topic-sort-topic (topic func reverse)
1539 ;; Each topic only lists the name of the group, while
1540 ;; the sort predicates expect group infos as inputs.
1541 ;; So we first transform the group names into infos,
1542 ;; then sort, and then transform back into group names.
1546 (lambda (info) (gnus-info-group info))
1549 (lambda (group) (gnus-get-info group))
1552 ;; Do the reversal, if necessary.
1554 (setcdr topic (nreverse (cdr topic)))))
1556 (defun gnus-topic-sort-groups (func &optional reverse)
1557 "Sort the current topic according to FUNC.
1558 If REVERSE, reverse the sorting order."
1559 (interactive (list gnus-group-sort-function current-prefix-arg))
1560 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1561 (gnus-topic-sort-topic
1562 topic (gnus-make-sort-function func) reverse)
1563 (gnus-group-list-groups)))
1565 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1566 "Sort the current topic alphabetically by group name.
1567 If REVERSE, sort in reverse order."
1569 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1571 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1572 "Sort the current topic by number of unread articles.
1573 If REVERSE, sort in reverse order."
1575 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1577 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1578 "Sort the current topic by group level.
1579 If REVERSE, sort in reverse order."
1581 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1583 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1584 "Sort the current topic by group score.
1585 If REVERSE, sort in reverse order."
1587 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1589 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1590 "Sort the current topic by group rank.
1591 If REVERSE, sort in reverse order."
1593 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1595 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1596 "Sort the current topic alphabetically by backend name.
1597 If REVERSE, sort in reverse order."
1599 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1601 (defun gnus-topic-sort-topics-1 (top reverse)
1604 (mapcar `(lambda (top)
1605 (gnus-topic-sort-topics-1 top ,reverse))
1608 (string-lessp (caar t1) (caar t2)))))))
1609 (setcdr top (if reverse (reverse subtop) subtop))))
1612 (defun gnus-topic-sort-topics (&optional topic reverse)
1613 "Sort topics in TOPIC alphabeticaly by topic name.
1614 If REVERSE, reverse the sorting order."
1616 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1617 (gnus-current-topic))
1618 current-prefix-arg))
1619 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1620 gnus-topic-topology)))
1621 (gnus-topic-sort-topics-1 topic-topology reverse)
1622 (gnus-topic-enter-dribble)
1623 (gnus-group-list-groups)
1624 (gnus-topic-goto-topic topic)))
1626 (defun gnus-topic-move (current to)
1627 "Move the CURRENT topic to TO."
1630 (gnus-group-topic-name)
1631 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1632 (unless (and current to)
1633 (error "Can't find topic"))
1634 (let ((current-top (cdr (gnus-topic-find-topology current)))
1635 (to-top (cdr (gnus-topic-find-topology to))))
1637 (error "Can't find topic `%s'" current))
1639 (error "Can't find topic `%s'" to))
1640 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1641 (error "Can't move `%s' to its sub-level" current))
1642 (gnus-topic-find-topology current nil nil 'delete)
1644 (setq to-top (cdr to-top)))
1645 (setcdr to-top (list current-top))
1646 (gnus-topic-enter-dribble)
1647 (gnus-group-list-groups)
1648 (gnus-topic-goto-topic current)))
1650 (defun gnus-subscribe-topics (newsgroup)
1652 (let (match gnus-group-change-level-function)
1653 (dolist (topic (gnus-topic-list))
1654 (when (and (setq match (cdr (assq 'subscribe
1655 (gnus-topic-parameters topic))))
1656 (string-match match newsgroup))
1657 ;; Just subscribe the group.
1658 (gnus-subscribe-alphabetically newsgroup)
1659 ;; Add the group to the topic.
1660 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1663 (provide 'gnus-topic)
1665 ;;; gnus-topic.el ends here