1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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))
37 (defgroup gnus-topic nil
41 (defvar gnus-topic-mode nil
42 "Minor mode for Gnus group buffers.")
44 (defcustom gnus-topic-mode-hook nil
45 "Hook run in topic mode buffers."
49 (when (featurep 'xemacs)
50 (add-hook 'gnus-topic-mode-hook 'gnus-xmas-topic-menu-add))
52 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
53 "Format of topic lines.
54 It works along the same lines as a normal formatting string,
55 with some simple extensions.
57 %i Indentation based on topic level.
59 %v Nothing if the topic is visible, \"...\" otherwise.
60 %g Number of groups in the topic.
61 %a Number of unread articles in the groups in the topic.
62 %A Number of unread articles in the groups in the topic and its subtopics.
64 General format specifiers can also be used.
65 See Info node `(gnus)Formatting Variables'."
66 :link '(custom-manual "(gnus)Formatting Variables")
70 (defcustom gnus-topic-indent-level 2
71 "*How much each subtopic should be indented."
75 (defcustom gnus-topic-display-empty-topics t
76 "*If non-nil, display the topic lines even of topics that have no unread articles."
80 ;; Internal variables.
82 (defvar gnus-topic-active-topology nil)
83 (defvar gnus-topic-active-alist nil)
84 (defvar gnus-topic-unreads nil)
86 (defvar gnus-topology-checked-p nil
87 "Whether the topology has been checked in this session.")
89 (defvar gnus-topic-killed-topics nil)
90 (defvar gnus-topic-inhibit-change-level nil)
92 (defconst gnus-topic-line-format-alist
96 (?g number-of-groups ?d)
97 (?a (gnus-topic-articles-in-topic entries) ?d)
98 (?A total-number-of-articles ?d)
101 (defvar gnus-topic-line-format-spec nil)
103 ;;; Utility functions
105 (defun gnus-group-topic-name ()
106 "The name of the topic on the current line."
107 (let ((topic (get-text-property (point-at-bol) 'gnus-topic)))
108 (and topic (symbol-name topic))))
110 (defun gnus-group-topic-level ()
111 "The level of the topic on the current line."
112 (get-text-property (point-at-bol) 'gnus-topic-level))
114 (defun gnus-group-topic-unread ()
115 "The number of unread articles in topic on the current line."
116 (get-text-property (point-at-bol) 'gnus-topic-unread))
118 (defun gnus-topic-unread (topic)
119 "Return the number of unread articles in TOPIC."
120 (or (cdr (assoc topic gnus-topic-unreads))
123 (defun gnus-group-topic-p ()
124 "Return non-nil if the current line is a topic."
125 (gnus-group-topic-name))
127 (defun gnus-topic-visible-p ()
128 "Return non-nil if the current topic is visible."
129 (get-text-property (point-at-bol) 'gnus-topic-visible))
131 (defun gnus-topic-articles-in-topic (entries)
135 (when (numberp (setq number (car (pop entries))))
136 (incf total number)))
139 (defun gnus-group-topic (group)
140 "Return the topic GROUP is a member of."
141 (let ((alist gnus-topic-alist)
144 (when (member group (cdar alist))
145 (setq out (caar alist)
147 (setq alist (cdr alist)))
150 (defun gnus-group-parent-topic (group)
151 "Return the topic GROUP is member of by looking at the group buffer."
153 (set-buffer gnus-group-buffer)
154 (if (gnus-group-goto-group group)
156 (gnus-group-topic group))))
158 (defun gnus-topic-goto-topic (topic)
160 (gnus-goto-char (text-property-any (point-min) (point-max)
161 'gnus-topic (intern topic)))))
163 (defun gnus-topic-jump-to-topic (topic)
166 (list (completing-read "Go to topic: "
167 (mapcar 'list (gnus-topic-list))
169 (dolist (topic (gnus-current-topics topic))
170 (gnus-topic-goto-topic topic)
172 (gnus-topic-goto-topic topic))
174 (defun gnus-current-topic ()
175 "Return the name of the current topic."
177 (or (get-text-property (point) 'gnus-topic)
179 (and (gnus-goto-char (previous-single-property-change
180 (point) 'gnus-topic))
181 (get-text-property (max (1- (point)) (point-min))
184 (symbol-name result))))
186 (defun gnus-current-topics (&optional topic)
187 "Return a list of all current topics, lowest in hierarchy first.
188 If TOPIC, start with that topic."
189 (let ((topic (or topic (gnus-current-topic)))
193 (setq topic (gnus-topic-parent-topic topic)))
196 (defun gnus-group-active-topic-p ()
197 "Say whether the current topic comes from the active topics."
198 (get-text-property (point-at-bol) 'gnus-active))
200 (defun gnus-topic-find-groups (topic &optional level all lowest recursive)
201 "Return entries for all visible groups in TOPIC.
202 If RECURSIVE is t, return groups in its subtopics too."
203 (let ((groups (cdr (assoc topic gnus-topic-alist)))
204 info clevel unread group params visible-groups entry active)
205 (setq lowest (or lowest 1))
206 (setq level (or level gnus-level-unsubscribed))
207 ;; We go through the newsrc to look for matches.
209 (when (setq group (pop groups))
210 (setq entry (gnus-group-entry group)
212 params (gnus-info-params info)
213 active (gnus-active group)
214 unread (or (car entry)
215 (and (not (equal group "dummy.group"))
217 (- (1+ (cdr active)) (car active))))
218 clevel (or (gnus-info-level info)
219 (if (member group gnus-zombie-list)
220 gnus-level-zombie gnus-level-killed))))
222 info ; nil means that the group is dead.
224 (>= clevel lowest) ; Is inside the level we want.
226 (if (or (eq unread t)
228 gnus-group-list-inactive-groups
230 (and gnus-list-groups-with-ticked-articles
231 (cdr (assq 'tick (gnus-info-marks info))))
232 ;; Has right readedness.
233 ;; Check for permanent visibility.
234 (and gnus-permanently-visible-groups
235 (string-match gnus-permanently-visible-groups group))
236 (memq 'visible params)
237 (cdr (assq 'visible params)))
238 ;; Add this group to the list of visible groups.
239 (push (or entry group) visible-groups)))
240 (setq visible-groups (nreverse visible-groups))
243 (setq recursive (cdr (gnus-topic-find-topology topic))))
244 (mapcar (lambda (topic-topology)
246 (nconc visible-groups
247 (gnus-topic-find-groups
248 (caar topic-topology)
249 level all lowest topic-topology))))
253 (defun gnus-topic-goto-previous-topic (n)
254 "Go to the N'th previous topic."
256 (gnus-topic-goto-next-topic (- n)))
258 (defun gnus-topic-goto-next-topic (n)
259 "Go to the N'th next topic."
261 (let ((backward (< n 0))
263 (topic (gnus-current-topic)))
267 (gnus-topic-previous-topic topic)
268 (gnus-topic-next-topic topic))))
269 (gnus-topic-goto-topic topic)
272 (gnus-message 7 "No more topics"))
275 (defun gnus-topic-previous-topic (topic)
276 "Return the previous topic on the same level as TOPIC."
277 (let ((top (cddr (gnus-topic-find-topology
278 (gnus-topic-parent-topic topic)))))
279 (unless (equal topic (caaar top))
280 (while (and top (not (equal (caaadr top) topic)))
281 (setq top (cdr top)))
284 (defun gnus-topic-parent-topic (topic &optional topology)
285 "Return the parent of TOPIC."
287 (setq topology gnus-topic-topology))
288 (let ((parent (car (pop topology)))
291 (not (setq found (equal (caaar topology) topic)))
292 (not (setq result (gnus-topic-parent-topic
293 topic (car topology)))))
294 (setq topology (cdr topology)))
295 (or result (and found parent))))
297 (defun gnus-topic-next-topic (topic &optional previous)
298 "Return the next sibling of TOPIC."
299 (let ((parentt (cddr (gnus-topic-find-topology
300 (gnus-topic-parent-topic topic))))
303 (not (equal (caaar parentt) topic)))
304 (setq prev (caaar parentt)
305 parentt (cdr parentt)))
310 (defun gnus-topic-forward-topic (num)
311 "Go to the next topic on the same level as the current one."
312 (let* ((topic (gnus-current-topic))
313 (way (if (< num 0) 'gnus-topic-previous-topic
314 'gnus-topic-next-topic))
316 (while (and (not (zerop num))
317 (setq topic (funcall way topic)))
318 (when (gnus-topic-goto-topic topic)
321 (goto-char (point-max)))
324 (defun gnus-topic-find-topology (topic &optional topology level remove)
325 "Return the topology of TOPIC."
327 (setq topology gnus-topic-topology)
331 (if (equal (caar topology) topic)
334 (delq topology remove))
335 (cons level topology))
336 (setq topology (cdr topology))
338 (not (setq result (gnus-topic-find-topology
339 topic (car topology) (1+ level)
341 (setq topology (cdr topology)))
344 (defvar gnus-tmp-topics nil)
345 (defun gnus-topic-list (&optional topology)
346 "Return a list of all topics in the topology."
348 (setq topology gnus-topic-topology
349 gnus-tmp-topics nil))
350 (push (caar topology) gnus-tmp-topics)
351 (mapcar 'gnus-topic-list (cdr topology))
354 ;;; Topic parameter jazz
356 (defun gnus-topic-parameters (topic)
357 "Return the parameters for TOPIC."
358 (let ((top (gnus-topic-find-topology topic)))
360 (nth 3 (cadr top)))))
362 (defun gnus-topic-set-parameters (topic parameters)
363 "Set the topic parameters of TOPIC to PARAMETERS."
364 (let ((top (gnus-topic-find-topology topic)))
366 (error "No such topic: %s" topic))
367 ;; We may have to extend if there is no parameters here
369 (unless (nthcdr 2 (cadr top))
370 (nconc (cadr top) (list nil)))
371 (unless (nthcdr 3 (cadr top))
372 (nconc (cadr top) (list nil)))
373 (setcar (nthcdr 3 (cadr top)) parameters)
375 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
377 (defun gnus-group-topic-parameters (group)
378 "Compute the group parameters for GROUP taking into account inheritance from topics."
379 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
382 (gnus-topic-hierarchical-parameters
383 ;; First we try to go to the group within the group
384 ;; buffer and find the topic for the group that way.
385 ;; This hopefully copes well with groups that are in
386 ;; more than one topic. Failing that (i.e. when the
387 ;; group isn't visible in the group buffer) we find a
388 ;; topic for the group via gnus-group-topic.
389 (or (and (gnus-group-goto-group group)
390 (gnus-current-topic))
391 (gnus-group-topic group)))))))
393 (defun gnus-topic-hierarchical-parameters (topic)
394 "Return a topic list computed for TOPIC."
395 (let ((topics (gnus-current-topics topic))
396 params-list param out params)
398 (push (gnus-topic-parameters (pop topics)) params-list))
399 ;; We probably have lots of nil elements here, so
400 ;; we remove them. Probably faster than doing this "properly".
401 (setq params-list (delq nil params-list))
402 ;; Now we have all the parameters, so we go through them
403 ;; and do inheritance in the obvious way.
404 (while (setq params (pop params-list))
405 (while (setq param (pop params))
407 (setq param (cons param t)))
408 ;; Override any old versions of this param.
409 (gnus-pull (car param) out)
411 ;; Return the resulting parameter list.
414 ;;; General utility functions
416 (defun gnus-topic-enter-dribble ()
418 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
420 ;;; Generating group buffers
422 (defun gnus-group-prepare-topics (level &optional predicate lowest
423 regexp list-topic topic-level)
424 "List all newsgroups with unread articles of level LEVEL or lower.
425 Use the `gnus-group-topics' to sort the groups.
426 If PREDICTE is a function, list groups that the function returns non-nil;
427 if it is t, list groups that have no unread articles.
428 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
429 (set-buffer gnus-group-buffer)
430 (let ((buffer-read-only nil)
431 (lowest (or lowest 1))
433 (and gnus-group-listed-groups
434 (copy-sequence gnus-group-listed-groups))))
436 (gnus-update-format-specifications nil 'topic)
438 (when (or (not gnus-topic-alist)
439 (not gnus-topology-checked-p))
440 (gnus-topic-check-topology))
446 (when (or gnus-group-listed-groups
447 (and (>= level gnus-level-zombie)
448 (<= lowest gnus-level-zombie)))
449 (gnus-group-prepare-flat-list-dead
450 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
454 (when (or gnus-group-listed-groups
455 (and (>= level gnus-level-killed)
456 (<= lowest gnus-level-killed)))
457 (gnus-group-prepare-flat-list-dead
458 (setq gnus-killed-list (sort gnus-killed-list 'string<))
459 gnus-level-killed ?K regexp)
461 (unless gnus-killed-hashtb
462 (gnus-make-hashtable-from-killed))
463 (gnus-group-prepare-flat-list-dead
464 (gnus-remove-if (lambda (group)
465 (or (gnus-group-entry group)
466 (gnus-gethash group gnus-killed-hashtb)))
468 gnus-level-killed ?K regexp)))
472 (when (or (< lowest gnus-level-zombie)
473 gnus-group-listed-groups)
475 (let ((top (gnus-topic-find-topology list-topic)))
476 (gnus-topic-prepare-topic (cdr top) (car top)
477 (or topic-level level) predicate
479 (gnus-topic-prepare-topic gnus-topic-topology 0
480 (or topic-level level) predicate
482 (gnus-group-set-mode-line)
483 (setq gnus-group-list-mode (cons level predicate))
484 (gnus-run-hooks 'gnus-group-prepare-hook))))
486 (defun gnus-topic-prepare-topic (topicl level &optional list-level
489 "Insert TOPIC into the group buffer.
490 If SILENT, don't insert anything. Return the number of unread
491 articles in the topic and its subtopics."
492 (let* ((type (pop topicl))
493 (entries (gnus-topic-find-groups
495 (if gnus-group-listed-groups
498 (or predicate gnus-group-listed-groups
500 (gnus-topic-hierarchical-parameters
502 (if gnus-group-listed-groups 0 lowest)))
503 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
504 (gnus-group-indentation
505 (make-string (* gnus-topic-indent-level level) ? ))
506 (beg (progn (beginning-of-line) (point)))
507 (topicl (reverse topicl))
508 (all-entries entries)
509 (point-max (point-max))
512 info entry end active tick)
513 ;; Insert any sub-topics.
516 (gnus-topic-prepare-topic
517 (pop topicl) (1+ level) list-level predicate
518 (not visiblep) lowest regexp)))
521 ;; Insert all the groups that belong in this topic.
522 (while (setq entry (pop entries))
523 (when (if (stringp entry)
524 (gnus-group-prepare-logic
527 (or (not gnus-group-listed-groups)
528 (if (< list-level gnus-level-zombie) nil
530 (if (member entry gnus-zombie-list)
531 gnus-level-zombie gnus-level-killed)))
532 (and (<= entry-level list-level)
533 (>= entry-level lowest)))))
536 (string-match regexp entry))
538 (funcall regexp entry))
541 (setq info (nth 2 entry))
542 (gnus-group-prepare-logic
543 (gnus-info-group info)
544 (and (or (not gnus-group-listed-groups)
545 (let ((entry-level (gnus-info-level info)))
546 (and (<= entry-level list-level)
547 (>= entry-level lowest))))
548 (or (not (functionp predicate))
549 (funcall predicate info))
550 (or (not (stringp regexp))
551 (string-match regexp (gnus-info-group info))))))
555 (gnus-group-insert-group-line
556 entry (if (member entry gnus-zombie-list)
557 gnus-level-zombie gnus-level-killed)
558 nil (- (1+ (cdr (setq active (gnus-active entry))))
562 (when (setq info (nth 2 entry))
563 (gnus-group-insert-group-line
564 (gnus-info-group info)
565 (gnus-info-level info) (gnus-info-marks info)
566 (car entry) (gnus-info-method info)))))
567 (when (and (listp entry)
568 (numberp (car entry)))
569 (incf unread (car entry)))
573 ;; Insert the topic line.
574 (when (and (not silent)
575 (or gnus-topic-display-empty-topics ;We want empty topics
576 (not (zerop unread)) ;Non-empty
577 tick ;Ticked articles
578 (/= point-max (point-max)))) ;Unactivated groups
579 (gnus-extent-start-open (point))
580 (gnus-topic-insert-topic-line
582 (not (eq (nth 2 type) 'hidden))
583 level all-entries unread))
584 (gnus-topic-update-unreads (car type) unread)
588 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
589 "Remove the current topic."
590 (let ((topic (gnus-group-topic-name))
591 (level (gnus-group-topic-level))
592 (beg (progn (beginning-of-line) (point)))
595 (while (and (zerop (forward-line 1))
596 (> (or (gnus-group-topic-level) (1+ level)) level)))
597 (delete-region beg (point))
598 ;; Do the change in this rather odd manner because it has been
599 ;; reported that some topics share parts of some lists, for some
600 ;; reason. I have been unable to determine why this is the
601 ;; case, but this hack seems to take care of things.
602 (let ((data (cadr (gnus-topic-find-topology topic))))
604 (list (if insert 'visible 'invisible)
608 (setq gnus-topic-alist
609 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
610 (gnus-topic-insert-topic topic in-level)))))
612 (defun gnus-topic-insert-topic (topic &optional level)
614 (gnus-group-prepare-topics
615 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
616 nil nil topic level))
618 (defun gnus-topic-fold (&optional insert topic)
619 "Remove/insert the current topic."
620 (let ((topic (or topic (gnus-group-topic-name))))
623 (if (not (gnus-group-active-topic-p))
624 (gnus-topic-remove-topic
625 (or insert (not (gnus-topic-visible-p))))
626 (let ((gnus-topic-topology gnus-topic-active-topology)
627 (gnus-topic-alist gnus-topic-active-alist)
628 (gnus-group-list-mode (cons 5 t)))
629 (gnus-topic-remove-topic
630 (or insert (not (gnus-topic-visible-p))) nil nil 9)
631 (gnus-topic-enter-dribble)))))))
633 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
635 (let* ((visible (if visiblep "" "..."))
636 (indentation (make-string (* gnus-topic-indent-level level) ? ))
637 (total-number-of-articles unread)
638 (number-of-groups (length entries))
639 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
641 (gnus-topic-update-unreads name unread)
645 (gnus-add-text-properties
648 (eval gnus-topic-line-format-spec))
649 (list 'gnus-topic (intern name)
650 'gnus-topic-level level
651 'gnus-topic-unread unread
652 'gnus-active active-topic
653 'gnus-topic-visible visiblep)))))
655 (defun gnus-topic-update-unreads (topic unreads)
656 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
658 (push (cons topic unreads) gnus-topic-unreads))
660 (defun gnus-topic-update-topics-containing-group (group)
661 "Update all topics that have GROUP as a member."
662 (when (and (eq major-mode 'gnus-group-mode)
665 (let ((alist gnus-topic-alist))
666 ;; This is probably not entirely correct. If a topic
667 ;; isn't shown, then it's not updated. But the updating
668 ;; should be performed in any case, since the topic's
669 ;; parent should be updated. Pfft.
671 (when (and (member group (cdar alist))
672 (gnus-topic-goto-topic (caar alist)))
673 (gnus-topic-update-topic-line (caar alist)))
676 (defun gnus-topic-update-topic ()
677 "Update all parent topics to the current group."
678 (when (and (eq major-mode 'gnus-group-mode)
680 (let ((group (gnus-group-group-name))
682 (buffer-read-only nil))
684 (gnus-get-info group)
685 (gnus-topic-goto-topic (gnus-current-topic)))
686 (gnus-topic-update-topic-line (gnus-group-topic-name))
689 (gnus-group-position-point)))))
691 (defun gnus-topic-goto-missing-group (group)
692 "Place point where GROUP is supposed to be inserted."
693 (let* ((topic (gnus-group-topic group))
694 (groups (cdr (assoc topic gnus-topic-alist)))
695 (g (cdr (member group groups)))
698 ;; Try to jump to a visible group.
700 (not (gnus-group-goto-group (car g) t)))
702 ;; It wasn't visible, so we try to see where to insert it.
704 (setq g (cdr (member group (reverse groups))))
705 (while (and g unfound)
706 (when (gnus-group-goto-group (pop g) t)
711 (not (gnus-topic-goto-missing-topic topic)))
712 (gnus-topic-display-missing-topic topic)))))
714 (defun gnus-topic-display-missing-topic (topic)
715 "Insert topic lines recursively for missing topics."
716 (let ((parent (gnus-topic-find-topology
717 (gnus-topic-parent-topic topic))))
719 (not (gnus-topic-goto-missing-topic (caadr parent))))
720 (gnus-topic-display-missing-topic (caadr parent))))
721 (gnus-topic-goto-missing-topic topic)
722 (let* ((top (gnus-topic-find-topology topic))
723 (children (cddr top))
726 (entries (gnus-topic-find-groups
727 (car type) (car gnus-group-list-mode)
728 (cdr gnus-group-list-mode)))
731 (incf unread (gnus-topic-unread (caar (pop children)))))
732 (while (setq entry (pop entries))
733 (when (numberp (car entry))
734 (incf unread (car entry))))
735 (gnus-topic-insert-topic-line
736 topic t t (car (gnus-topic-find-topology topic)) nil unread)))
738 (defun gnus-topic-goto-missing-topic (topic)
739 (if (gnus-topic-goto-topic topic)
741 ;; Topic not displayed.
742 (let* ((top (gnus-topic-find-topology
743 (gnus-topic-parent-topic topic)))
744 (tp (reverse (cddr top))))
746 (gnus-topic-insert-topic-line
747 topic t t (car (gnus-topic-find-topology topic)) nil 0)
748 (while (not (equal (caaar tp) topic))
752 (not (gnus-topic-goto-topic (caaar tp))))
755 (gnus-topic-forward-topic 1)
756 (gnus-topic-goto-missing-topic (caadr top)))))
759 (defun gnus-topic-update-topic-line (topic-name &optional reads)
760 (let* ((top (gnus-topic-find-topology topic-name))
762 (children (cddr top))
763 (entries (gnus-topic-find-groups
764 (car type) (car gnus-group-list-mode)
765 (cdr gnus-group-list-mode)))
766 (parent (gnus-topic-parent-topic topic-name))
767 (all-entries entries)
769 old-unread entry new-unread)
770 (when (gnus-topic-goto-topic (car type))
771 ;; Tally all the groups that belong in this topic.
773 (setq unread (- (gnus-group-topic-unread) reads))
775 (incf unread (gnus-topic-unread (caar (pop children)))))
776 (while (setq entry (pop entries))
777 (when (numberp (car entry))
778 (incf unread (car entry)))))
779 (setq old-unread (gnus-group-topic-unread))
780 ;; Insert the topic line.
781 (gnus-topic-insert-topic-line
782 (car type) (gnus-topic-visible-p)
783 (not (eq (nth 2 type) 'hidden))
784 (gnus-group-topic-level) all-entries unread)
787 (setq new-unread (gnus-group-topic-unread)))
790 (gnus-topic-update-topic-line
792 (- (or old-unread 0) (or new-unread 0))))
795 (defun gnus-topic-group-indentation ()
797 (* gnus-topic-indent-level
800 (gnus-topic-goto-topic (gnus-current-topic))
801 (gnus-group-topic-level))
807 (gnus-add-shutdown 'gnus-topic-close 'gnus)
809 (defun gnus-topic-close ()
810 (setq gnus-topic-active-topology nil
811 gnus-topic-active-alist nil
812 gnus-topic-killed-topics nil
813 gnus-topology-checked-p nil))
815 (defun gnus-topic-check-topology ()
816 ;; The first time we set the topology to whatever we have
817 ;; gotten here, which can be rather random.
818 (unless gnus-topic-alist
819 (gnus-topic-init-alist))
821 (setq gnus-topology-checked-p t)
822 ;; Go through the topic alist and make sure that all topics
823 ;; are in the topic topology.
824 (let ((topics (gnus-topic-list))
825 (alist gnus-topic-alist)
828 (unless (member (caar alist) topics)
829 (nconc gnus-topic-topology
830 (list (list (list (caar alist) 'visible))))
832 (setq alist (cdr alist)))
834 (gnus-topic-enter-dribble))
835 ;; Conversely, go through the topology and make sure that all
836 ;; topologies have alists.
838 (unless (assoc (car topics) gnus-topic-alist)
839 (push (list (car topics)) gnus-topic-alist))
841 ;; Go through all living groups and make sure that
842 ;; they belong to some topic.
843 (let* ((tgroups (apply 'append (mapcar 'cdr gnus-topic-alist)))
844 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
845 (newsrc (cdr gnus-newsrc-alist))
848 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
849 (setcdr entry (list group))
850 (setq entry (cdr entry)))))
851 ;; Go through all topics and make sure they contain only living groups.
852 (let ((alist gnus-topic-alist)
854 (while (setq topic (pop alist))
856 (if (and (cadr topic)
857 (gnus-group-entry (cadr topic)))
858 (setq topic (cdr topic))
859 (setcdr topic (cddr topic)))))))
861 (defun gnus-topic-init-alist ()
862 "Initialize the topic structures."
863 (setq gnus-topic-topology
864 (cons (list "Gnus" 'visible)
865 (mapcar (lambda (topic)
866 (list (list (car topic) 'visible)))
868 (setq gnus-topic-alist
870 (mapcar (lambda (info) (gnus-info-group info))
871 (cdr gnus-newsrc-alist)))
873 (gnus-topic-enter-dribble))
877 (defun gnus-topic-clean-alist ()
878 "Remove bogus groups from the topic alist."
879 (let ((topic-alist gnus-topic-alist)
881 (unless gnus-killed-hashtb
882 (gnus-make-hashtable-from-killed))
883 (while (setq topic (pop topic-alist))
884 (let ((topic-name (pop topic))
885 group filtered-topic)
886 (while (setq group (pop topic))
887 (when (and (or (gnus-active group)
888 (gnus-info-method (gnus-get-info group)))
889 (not (gnus-gethash group gnus-killed-hashtb)))
890 (push group filtered-topic)))
891 (push (cons topic-name (nreverse filtered-topic)) result)))
892 (setq gnus-topic-alist (nreverse result))))
894 (defun gnus-topic-change-level (group level oldlevel &optional previous)
895 "Run when changing levels to enter/remove groups from topics."
897 (set-buffer gnus-group-buffer)
898 (let ((buffer-read-only nil))
899 (unless gnus-topic-inhibit-change-level
900 (gnus-group-goto-group (or (car (nth 2 previous)) group))
901 (when (and gnus-topic-mode
903 (not gnus-topic-inhibit-change-level))
904 ;; Remove the group from the topics.
905 (if (and (< oldlevel gnus-level-zombie)
906 (>= level gnus-level-zombie))
907 (let ((alist gnus-topic-alist))
908 (while (gnus-group-goto-group group)
911 (when (member group (car alist))
912 (setcdr (car alist) (delete group (cdar alist))))
914 ;; If the group is subscribed we enter it into the topics.
915 (when (and (< level gnus-level-zombie)
916 (>= oldlevel gnus-level-zombie))
917 (let* ((prev (gnus-group-group-name))
918 (gnus-topic-inhibit-change-level t)
919 (gnus-group-indentation
921 (* gnus-topic-indent-level
923 (gnus-topic-goto-topic (gnus-current-topic))
924 (gnus-group-topic-level))
927 (yanked (list group))
929 ;; Then we enter the yanked groups into the topics
931 (when (setq alist (assoc (save-excursion
935 (caar gnus-topic-topology)))
938 (when (stringp yanked)
939 (setq yanked (list yanked)))
942 (if (not (cdr alist))
943 (setcdr alist (nconc yanked (cdr alist)))
944 (while (and (not end) (cdr alist))
945 (when (equal (cadr alist) prev)
946 (setcdr alist (nconc yanked (cdr alist)))
948 (setq alist (cdr alist)))
950 (nconc talist yanked))))))
951 (gnus-topic-update-topic))))))))
953 (defun gnus-topic-goto-next-group (group props)
954 "Go to group or the next group after group."
956 (if (not (memq 'gnus-topic props))
957 (goto-char (point-max))
958 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
959 (if (gnus-group-goto-group group)
961 ;; The group is no longer visible.
962 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
963 (after (cdr (member group (cdr list)))))
964 ;; First try to put point on a group after the current one.
966 (not (gnus-group-goto-group (car after))))
967 (setq after (cdr after)))
968 ;; Then try to put point on a group before point.
970 (setq after (cdr (member group (reverse (cdr list)))))
972 (not (gnus-group-goto-group (car after))))
973 (setq after (cdr after))))
974 ;; Finally, just put point on the topic.
976 (goto-char (point-min))
978 (gnus-topic-goto-topic (car list))
982 ;;; Topic-active functions
984 (defun gnus-topic-grok-active (&optional force)
985 "Parse all active groups and create topic structures for them."
986 ;; First we make sure that we have really read the active file.
988 (not gnus-topic-active-alist))
990 ;; Get a list of all groups available.
991 (mapatoms (lambda (g) (when (symbol-value g)
992 (push (symbol-name g) groups)))
994 (setq groups (sort groups 'string<))
995 ;; Init the variables.
996 (setq gnus-topic-active-topology (list (list "" 'visible)))
997 (setq gnus-topic-active-alist nil)
998 ;; Descend the top-level hierarchy.
999 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
1000 ;; Set the top-level topic names to something nice.
1001 (setcar (car gnus-topic-active-topology) "Gnus active")
1002 (setcar (car gnus-topic-active-alist) "Gnus active"))))
1004 (defun gnus-topic-grok-active-1 (topology groups)
1005 (let* ((name (caar topology))
1006 (prefix (concat "^" (regexp-quote name)))
1007 tgroups ntopology group)
1009 (string-match prefix (setq group (car groups))))
1010 (if (not (string-match "\\." group (match-end 0)))
1011 ;; There are no further hierarchies here, so we just
1012 ;; enter this group into the list belonging to this
1014 (push (pop groups) tgroups)
1015 ;; New sub-hierarchy, so we add it to the topology.
1016 (nconc topology (list (setq ntopology
1017 (list (list (substring
1018 group 0 (match-end 0))
1020 ;; Descend the hierarchy.
1021 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
1022 ;; We remove the trailing "." from the topic name.
1024 (if (string-match "\\.$" name)
1025 (substring name 0 (match-beginning 0))
1027 ;; Add this topic and its groups to the topic alist.
1028 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
1029 (setcar (car topology) name)
1030 ;; We return the rest of the groups that didn't belong
1034 ;;; Topic mode, commands and keymap.
1036 (defvar gnus-topic-mode-map nil)
1037 (defvar gnus-group-topic-map nil)
1039 (unless gnus-topic-mode-map
1040 (setq gnus-topic-mode-map (make-sparse-keymap))
1042 ;; Override certain group mode keys.
1043 (gnus-define-keys gnus-topic-mode-map
1044 "=" gnus-topic-select-group
1045 "\r" gnus-topic-select-group
1046 " " gnus-topic-read-group
1047 "\C-c\C-x" gnus-topic-expire-articles
1048 "c" gnus-topic-catchup-articles
1049 "\C-k" gnus-topic-kill-group
1050 "\C-y" gnus-topic-yank-group
1051 "\M-g" gnus-topic-get-new-news-this-topic
1052 "AT" gnus-topic-list-active
1053 "Gp" gnus-topic-edit-parameters
1054 "#" gnus-topic-mark-topic
1055 "\M-#" gnus-topic-unmark-topic
1056 [tab] gnus-topic-indent
1057 [(meta tab)] gnus-topic-unindent
1058 "\C-i" gnus-topic-indent
1059 "\M-\C-i" gnus-topic-unindent
1060 gnus-mouse-2 gnus-mouse-pick-topic)
1062 ;; Define a new submap.
1063 (gnus-define-keys (gnus-group-topic-map "T" gnus-topic-mode-map)
1064 "#" gnus-topic-mark-topic
1065 "\M-#" gnus-topic-unmark-topic
1066 "n" gnus-topic-create-topic
1067 "m" gnus-topic-move-group
1068 "D" gnus-topic-remove-group
1069 "c" gnus-topic-copy-group
1070 "h" gnus-topic-hide-topic
1071 "s" gnus-topic-show-topic
1072 "j" gnus-topic-jump-to-topic
1073 "M" gnus-topic-move-matching
1074 "C" gnus-topic-copy-matching
1075 "\M-p" gnus-topic-goto-previous-topic
1076 "\M-n" gnus-topic-goto-next-topic
1077 "\C-i" gnus-topic-indent
1078 [tab] gnus-topic-indent
1079 "r" gnus-topic-rename
1080 "\177" gnus-topic-delete
1081 [delete] gnus-topic-delete
1082 "H" gnus-topic-toggle-display-empty-topics)
1084 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1085 "s" gnus-topic-sort-groups
1086 "a" gnus-topic-sort-groups-by-alphabet
1087 "u" gnus-topic-sort-groups-by-unread
1088 "l" gnus-topic-sort-groups-by-level
1089 "e" gnus-topic-sort-groups-by-server
1090 "v" gnus-topic-sort-groups-by-score
1091 "r" gnus-topic-sort-groups-by-rank
1092 "m" gnus-topic-sort-groups-by-method))
1094 (defun gnus-topic-make-menu-bar ()
1095 (unless (boundp 'gnus-topic-menu)
1097 gnus-topic-menu gnus-topic-mode-map ""
1099 ["Toggle topics" gnus-topic-mode t]
1101 ["Copy..." gnus-topic-copy-group t]
1102 ["Move..." gnus-topic-move-group t]
1103 ["Remove" gnus-topic-remove-group t]
1104 ["Copy matching..." gnus-topic-copy-matching t]
1105 ["Move matching..." gnus-topic-move-matching t])
1107 ["Goto..." gnus-topic-jump-to-topic t]
1108 ["Show" gnus-topic-show-topic t]
1109 ["Hide" gnus-topic-hide-topic t]
1110 ["Delete" gnus-topic-delete t]
1111 ["Rename..." gnus-topic-rename t]
1112 ["Create..." gnus-topic-create-topic t]
1113 ["Mark" gnus-topic-mark-topic t]
1114 ["Indent" gnus-topic-indent t]
1115 ["Sort" gnus-topic-sort-topics t]
1116 ["Previous topic" gnus-topic-goto-previous-topic t]
1117 ["Next topic" gnus-topic-goto-next-topic t]
1118 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1119 ["Edit parameters" gnus-topic-edit-parameters t])
1120 ["List active" gnus-topic-list-active t]))))
1122 (defun gnus-topic-mode (&optional arg redisplay)
1123 "Minor mode for topicsifying Gnus group buffers."
1124 (interactive (list current-prefix-arg t))
1125 (when (eq major-mode 'gnus-group-mode)
1126 (make-local-variable 'gnus-topic-mode)
1127 (setq gnus-topic-mode
1128 (if (null arg) (not gnus-topic-mode)
1129 (> (prefix-numeric-value arg) 0)))
1130 ;; Infest Gnus with topics.
1131 (if (not gnus-topic-mode)
1132 (setq gnus-goto-missing-group-function nil)
1133 (when (gnus-visual-p 'topic-menu 'menu)
1134 (gnus-topic-make-menu-bar))
1135 (gnus-set-format 'topic t)
1136 (add-minor-mode 'gnus-topic-mode " Topic"
1137 gnus-topic-mode-map nil (lambda (&rest junk)
1139 (gnus-topic-mode nil t)))
1140 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1141 (set (make-local-variable 'gnus-group-prepare-function)
1142 'gnus-group-prepare-topics)
1143 (set (make-local-variable 'gnus-group-get-parameter-function)
1144 'gnus-group-topic-parameters)
1145 (set (make-local-variable 'gnus-group-goto-next-group-function)
1146 'gnus-topic-goto-next-group)
1147 (set (make-local-variable 'gnus-group-indentation-function)
1148 'gnus-topic-group-indentation)
1149 (set (make-local-variable 'gnus-group-update-group-function)
1150 'gnus-topic-update-topics-containing-group)
1151 (set (make-local-variable 'gnus-group-sort-alist-function)
1152 'gnus-group-sort-topic)
1153 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1154 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1155 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
1156 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1158 (setq gnus-topology-checked-p nil)
1159 ;; We check the topology.
1160 (when gnus-newsrc-alist
1161 (gnus-topic-check-topology))
1162 (gnus-run-hooks 'gnus-topic-mode-hook))
1163 ;; Remove topic infestation.
1164 (unless gnus-topic-mode
1165 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1166 (setq gnus-group-change-level-function nil)
1167 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1168 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1169 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1171 (gnus-group-list-groups))))
1173 (defun gnus-topic-select-group (&optional all)
1174 "Select this newsgroup.
1175 No article is selected automatically.
1176 If the group is opened, just switch the summary buffer.
1177 If ALL is non-nil, already read articles become readable.
1178 If ALL is a number, fetch this number of articles.
1180 If performed over a topic line, toggle folding the topic."
1182 (when (and (eobp) (not (gnus-group-group-name)))
1184 (if (gnus-group-topic-p)
1185 (let ((gnus-group-list-mode
1186 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1187 (gnus-topic-fold all)
1188 (gnus-dribble-touch))
1189 (gnus-group-select-group all)))
1191 (defun gnus-mouse-pick-topic (e)
1192 "Select the group or topic under the mouse pointer."
1195 (gnus-topic-read-group nil))
1197 (defun gnus-topic-expire-articles (topic)
1198 "Expire articles in this topic or group."
1199 (interactive (list (gnus-group-topic-name)))
1201 (call-interactively 'gnus-group-expire-articles)
1203 (gnus-message 5 "Expiring groups in %s..." topic)
1204 (let ((gnus-group-marked
1205 (mapcar (lambda (entry) (car (nth 2 entry)))
1206 (gnus-topic-find-groups topic gnus-level-killed t
1208 (gnus-group-expire-articles nil))
1209 (gnus-message 5 "Expiring groups in %s...done" topic))))
1211 (defun gnus-topic-catchup-articles (topic)
1212 "Catchup this topic or group.
1213 Also see `gnus-group-catchup'."
1214 (interactive (list (gnus-group-topic-name)))
1216 (call-interactively 'gnus-group-catchup-current)
1219 (mapcar (lambda (entry) (car (nth 2 entry)))
1220 (gnus-topic-find-groups topic gnus-level-killed t
1222 (buffer-read-only nil)
1223 (gnus-group-marked groups))
1224 (gnus-group-catchup-current)
1225 (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1227 (defun gnus-topic-read-group (&optional all no-article group)
1228 "Read news in this newsgroup.
1229 If the prefix argument ALL is non-nil, already read articles become
1230 readable. IF ALL is a number, fetch this number of articles. If the
1231 optional argument NO-ARTICLE is non-nil, no article will be
1232 auto-selected upon group entry. If GROUP is non-nil, fetch that
1235 If performed over a topic line, toggle folding the topic."
1237 (if (gnus-group-topic-p)
1238 (let ((gnus-group-list-mode
1239 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1240 (gnus-topic-fold all))
1241 (gnus-group-read-group all no-article group)))
1243 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1244 "Create a new TOPIC under PARENT.
1245 When used interactively, PARENT will be the topic under point."
1248 (read-string "New topic: ")
1249 (gnus-current-topic)))
1250 ;; Check whether this topic already exists.
1251 (when (gnus-topic-find-topology topic)
1252 (error "Topic already exists"))
1254 (setq parent (caar gnus-topic-topology)))
1255 (let ((top (cdr (gnus-topic-find-topology parent)))
1256 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1258 (error "No such parent topic: %s" parent))
1261 (while (and (cdr top)
1262 (not (equal (caaadr top) previous)))
1263 (setq top (cdr top)))
1264 (setcdr top (cons full-topic (cdr top))))
1265 (nconc top (list full-topic)))
1266 (unless (assoc topic gnus-topic-alist)
1267 (push (list topic) gnus-topic-alist)))
1268 (gnus-topic-enter-dribble)
1269 (gnus-group-list-groups)
1270 (gnus-topic-goto-topic topic))
1273 ;; 1. When the marked groups are overlapped with the process
1274 ;; region, the behavior of move or remove is not right.
1275 ;; 2. Can't process on several marked groups with a same name,
1276 ;; because gnus-group-marked only keeps one copy.
1278 (defun gnus-topic-move-group (n topic &optional copyp)
1279 "Move the next N groups to TOPIC.
1280 If COPYP, copy the groups instead."
1282 (list current-prefix-arg
1283 (gnus-completing-read "Move to topic" gnus-topic-alist nil t
1284 'gnus-topic-history)))
1285 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1286 gnus-group-marked t))
1287 (groups (gnus-group-process-prefix n))
1288 (topicl (assoc topic gnus-topic-alist))
1289 (start-topic (gnus-group-topic-name))
1290 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1292 (if (and (not groups) (not copyp) start-topic)
1293 (gnus-topic-move start-topic topic)
1296 (gnus-group-remove-mark g use-marked)
1298 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1300 (setcdr entry (gnus-delete-first g (cdr entry))))
1301 (nconc topicl (list g)))
1303 (gnus-topic-enter-dribble)
1305 (gnus-group-goto-group start-group)
1306 (gnus-topic-goto-topic start-topic))
1307 (gnus-group-list-groups))))
1309 (defun gnus-topic-remove-group (&optional n)
1310 "Remove the current group from the topic."
1312 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1313 gnus-group-marked t))
1314 (groups (gnus-group-process-prefix n)))
1317 (gnus-group-remove-mark group use-marked)
1318 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1319 (buffer-read-only nil))
1320 (when (and topicl group)
1322 (gnus-delete-first group topicl))
1323 (gnus-topic-update-topic)))
1325 (gnus-topic-enter-dribble)
1326 (gnus-group-position-point)))
1328 (defun gnus-topic-copy-group (n topic)
1329 "Copy the current group to a topic."
1331 (list current-prefix-arg
1332 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1333 (gnus-topic-move-group n topic t))
1335 (defun gnus-topic-kill-group (&optional n discard)
1336 "Kill the next N groups."
1338 (if (gnus-group-topic-p)
1339 (let ((topic (gnus-group-topic-name)))
1341 (gnus-topic-find-topology topic)
1342 (assoc topic gnus-topic-alist))
1343 gnus-topic-killed-topics)
1344 (gnus-topic-remove-topic nil t)
1345 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1346 (gnus-topic-enter-dribble))
1347 (gnus-group-kill-group n discard)
1348 (if (not (gnus-group-topic-p))
1349 (gnus-topic-update-topic)
1350 ;; Move up one line so that we update the right topic.
1352 (gnus-topic-update-topic)
1355 (defun gnus-topic-yank-group (&optional arg)
1356 "Yank the last topic."
1358 (if gnus-topic-killed-topics
1360 (or (gnus-group-topic-name)
1361 (gnus-topic-next-topic (gnus-current-topic))))
1362 (data (pop gnus-topic-killed-topics))
1365 (push alist gnus-topic-alist)
1366 (gnus-topic-create-topic
1367 (caar item) (gnus-topic-parent-topic previous) previous
1369 (gnus-topic-enter-dribble)
1370 (gnus-topic-goto-topic (caar item)))
1371 (let* ((prev (gnus-group-group-name))
1372 (gnus-topic-inhibit-change-level t)
1373 (gnus-group-indentation
1375 (* gnus-topic-indent-level
1377 (gnus-topic-goto-topic (gnus-current-topic))
1378 (gnus-group-topic-level))
1382 ;; We first yank the groups the normal way...
1383 (setq yanked (gnus-group-yank-group arg))
1384 ;; Then we enter the yanked groups into the topics they belong
1386 (setq alist (assoc (save-excursion
1388 (gnus-current-topic))
1390 (when (stringp yanked)
1391 (setq yanked (list yanked)))
1393 (nconc alist yanked)
1394 (if (not (cdr alist))
1395 (setcdr alist (nconc yanked (cdr alist)))
1397 (when (equal (cadr alist) prev)
1398 (setcdr alist (nconc yanked (cdr alist)))
1400 (setq alist (cdr alist))))))
1401 (gnus-topic-update-topic)))
1403 (defun gnus-topic-hide-topic (&optional permanent)
1404 "Hide the current topic.
1405 If PERMANENT, make it stay hidden in subsequent sessions as well."
1407 (when (gnus-current-topic)
1408 (gnus-topic-goto-topic (gnus-current-topic))
1412 (gnus-topic-find-topology (gnus-current-topic))))
1414 (gnus-topic-remove-topic nil nil)))
1416 (defun gnus-topic-show-topic (&optional permanent)
1417 "Show the hidden topic.
1418 If PERMANENT, make it stay shown in subsequent sessions as well."
1420 (when (gnus-group-topic-p)
1422 (gnus-topic-remove-topic t nil)
1424 (gnus-topic-find-topology
1425 (completing-read "Show topic: " gnus-topic-alist nil t))))
1426 (setcar (cddr (cadr topic)) nil)
1427 (setcar (cdr (cadr topic)) 'visible)
1428 (gnus-group-list-groups)))))
1430 (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
1431 "Mark all groups in the TOPIC with the process mark.
1432 If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
1433 (interactive (list (gnus-group-topic-name)
1435 (and current-prefix-arg t)))
1437 (call-interactively 'gnus-group-mark-group)
1439 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1440 (not non-recursive))))
1442 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1443 (gnus-info-group (nth 2 (pop groups)))))))))
1445 (defun gnus-topic-unmark-topic (topic &optional dummy non-recursive)
1446 "Remove the process mark from all groups in the TOPIC.
1447 If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
1448 (interactive (list (gnus-group-topic-name)
1450 (and current-prefix-arg t)))
1452 (call-interactively 'gnus-group-unmark-group)
1453 (gnus-topic-mark-topic topic t non-recursive)))
1455 (defun gnus-topic-get-new-news-this-topic (&optional n)
1456 "Check for new news in the current topic."
1458 (if (not (gnus-group-topic-p))
1459 (gnus-group-get-new-news-this-group n)
1460 (let* ((topic (gnus-group-topic-name))
1461 (data (cadr (gnus-topic-find-topology topic))))
1463 (gnus-topic-mark-topic topic nil (and n t))
1464 (gnus-group-get-new-news-this-group))
1465 (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1467 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1468 "Move all groups that match REGEXP to some topic."
1473 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1474 (read-string (format "Move to %s (regexp): " topic))))))
1475 (gnus-group-mark-regexp regexp)
1476 (gnus-topic-move-group nil topic copyp))
1478 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1479 "Copy all groups that match REGEXP to some topic."
1484 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1485 (read-string (format "Copy to %s (regexp): " topic))))))
1486 (gnus-topic-move-matching regexp topic t))
1488 (defun gnus-topic-delete (topic)
1490 (interactive (list (gnus-group-topic-name)))
1492 (error "No topic to be deleted"))
1493 (let ((entry (assoc topic gnus-topic-alist))
1494 (buffer-read-only nil))
1496 (error "Topic not empty"))
1497 ;; Delete if visible.
1498 (when (gnus-topic-goto-topic topic)
1500 ;; Remove from alist.
1501 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1502 ;; Remove from topology.
1503 (gnus-topic-find-topology topic nil nil 'delete)
1504 (gnus-dribble-touch)))
1506 (defun gnus-topic-rename (old-name new-name)
1509 (let ((topic (gnus-current-topic)))
1511 (read-string (format "Rename %s to: " topic) topic))))
1512 ;; Check whether the new name exists.
1513 (when (gnus-topic-find-topology new-name)
1514 (error "Topic '%s' already exists" new-name))
1515 ;; "nil" is an invalid name, for reasons I'd rather not go
1516 ;; into here. Trust me.
1517 (when (equal new-name "nil")
1518 (error "Invalid name: %s" nil))
1520 (let ((top (gnus-topic-find-topology old-name))
1521 (entry (assoc old-name gnus-topic-alist)))
1523 (setcar (cadr top) new-name))
1525 (setcar entry new-name))
1527 (gnus-dribble-touch)
1528 (gnus-group-list-groups)
1531 (defun gnus-topic-indent (&optional unindent)
1532 "Indent a topic -- make it a sub-topic of the previous topic.
1533 If UNINDENT, remove an indentation."
1536 (gnus-topic-unindent)
1537 (let* ((topic (gnus-current-topic))
1538 (parent (gnus-topic-previous-topic topic))
1539 (buffer-read-only nil))
1541 (error "Nothing to indent %s into" topic))
1543 (gnus-topic-goto-topic topic)
1544 (gnus-topic-kill-group)
1545 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1546 (gnus-topic-create-topic
1547 topic parent nil (cdar (car gnus-topic-killed-topics)))
1548 (pop gnus-topic-killed-topics)
1549 (or (gnus-topic-goto-topic topic)
1550 (gnus-topic-goto-topic parent))))))
1552 (defun gnus-topic-unindent ()
1555 (let* ((topic (gnus-current-topic))
1556 (parent (gnus-topic-parent-topic topic))
1557 (grandparent (gnus-topic-parent-topic parent)))
1559 (error "Nothing to indent %s into" topic))
1561 (gnus-topic-goto-topic topic)
1562 (gnus-topic-kill-group)
1563 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1564 (gnus-topic-create-topic
1565 topic grandparent (gnus-topic-next-topic parent)
1566 (cdar (car gnus-topic-killed-topics)))
1567 (pop gnus-topic-killed-topics)
1568 (gnus-topic-goto-topic topic))))
1570 (defun gnus-topic-list-active (&optional force)
1571 "List all groups that Gnus knows about in a topicsified fashion.
1572 If FORCE, always re-read the active file."
1575 (gnus-get-killed-groups))
1576 (gnus-topic-grok-active force)
1577 (let ((gnus-topic-topology gnus-topic-active-topology)
1578 (gnus-topic-alist gnus-topic-active-alist)
1579 gnus-killed-list gnus-zombie-list)
1580 (gnus-group-list-groups gnus-level-killed nil 1)))
1582 (defun gnus-topic-toggle-display-empty-topics ()
1583 "Show/hide topics that have no unread articles."
1585 (setq gnus-topic-display-empty-topics
1586 (not gnus-topic-display-empty-topics))
1587 (gnus-group-list-groups)
1588 (message "%s empty topics"
1589 (if gnus-topic-display-empty-topics
1590 "Showing" "Hiding")))
1592 ;;; Topic sorting functions
1594 (defun gnus-topic-edit-parameters (group)
1595 "Edit the group parameters of GROUP.
1596 If performed on a topic, edit the topic parameters instead."
1597 (interactive (list (gnus-group-group-name)))
1599 (gnus-group-edit-group-parameters group)
1600 (if (not (gnus-group-topic-p))
1601 (error "Nothing to edit on the current line")
1602 (let ((topic (gnus-group-topic-name)))
1604 (gnus-topic-parameters topic)
1605 (format "Editing the topic parameters for `%s'."
1608 (gnus-topic-set-parameters ,topic form)))))))
1610 (defun gnus-group-sort-topic (func reverse)
1611 "Sort groups in the topics according to FUNC and REVERSE."
1612 (let ((alist gnus-topic-alist))
1614 ;; !!!Sometimes nil elements sneak into the alist,
1615 ;; for some reason or other.
1616 (setcar alist (delq nil (car alist)))
1617 (setcar alist (delete "dummy.group" (car alist)))
1618 (gnus-topic-sort-topic (pop alist) func reverse))))
1620 (defun gnus-topic-sort-topic (topic func reverse)
1621 ;; Each topic only lists the name of the group, while
1622 ;; the sort predicates expect group infos as inputs.
1623 ;; So we first transform the group names into infos,
1624 ;; then sort, and then transform back into group names.
1628 (lambda (info) (gnus-info-group info))
1631 (lambda (group) (gnus-get-info group))
1634 ;; Do the reversal, if necessary.
1636 (setcdr topic (nreverse (cdr topic)))))
1638 (defun gnus-topic-sort-groups (func &optional reverse)
1639 "Sort the current topic according to FUNC.
1640 If REVERSE, reverse the sorting order."
1641 (interactive (list gnus-group-sort-function current-prefix-arg))
1642 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1643 (gnus-topic-sort-topic
1644 topic (gnus-make-sort-function func) reverse)
1645 (gnus-group-list-groups)))
1647 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1648 "Sort the current topic alphabetically by group name.
1649 If REVERSE, sort in reverse order."
1651 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1653 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1654 "Sort the current topic by number of unread articles.
1655 If REVERSE, sort in reverse order."
1657 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1659 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1660 "Sort the current topic by group level.
1661 If REVERSE, sort in reverse order."
1663 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1665 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1666 "Sort the current topic by group score.
1667 If REVERSE, sort in reverse order."
1669 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1671 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1672 "Sort the current topic by group rank.
1673 If REVERSE, sort in reverse order."
1675 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1677 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1678 "Sort the current topic alphabetically by backend name.
1679 If REVERSE, sort in reverse order."
1681 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1683 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1684 "Sort the current topic alphabetically by server name.
1685 If REVERSE, sort in reverse order."
1687 (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1689 (defun gnus-topic-sort-topics-1 (top reverse)
1692 (mapcar (gnus-byte-compile
1694 (gnus-topic-sort-topics-1 top ,reverse)))
1697 (string-lessp (caar t1) (caar t2)))))))
1698 (setcdr top (if reverse (reverse subtop) subtop))))
1701 (defun gnus-topic-sort-topics (&optional topic reverse)
1702 "Sort topics in TOPIC alphabetically by topic name.
1703 If REVERSE, reverse the sorting order."
1705 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1706 (gnus-current-topic))
1707 current-prefix-arg))
1708 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1709 gnus-topic-topology)))
1710 (gnus-topic-sort-topics-1 topic-topology reverse)
1711 (gnus-topic-enter-dribble)
1712 (gnus-group-list-groups)
1713 (gnus-topic-goto-topic topic)))
1715 (defun gnus-topic-move (current to)
1716 "Move the CURRENT topic to TO."
1719 (gnus-group-topic-name)
1720 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1721 (unless (and current to)
1722 (error "Can't find topic"))
1723 (let ((current-top (cdr (gnus-topic-find-topology current)))
1724 (to-top (cdr (gnus-topic-find-topology to))))
1726 (error "Can't find topic `%s'" current))
1728 (error "Can't find topic `%s'" to))
1729 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1730 (error "Can't move `%s' to its sub-level" current))
1731 (gnus-topic-find-topology current nil nil 'delete)
1733 (setq to-top (cdr to-top)))
1734 (setcdr to-top (list current-top))
1735 (gnus-topic-enter-dribble)
1736 (gnus-group-list-groups)
1737 (gnus-topic-goto-topic current)))
1739 (defun gnus-subscribe-topics (newsgroup)
1741 (let (match gnus-group-change-level-function)
1742 (dolist (topic (gnus-topic-list))
1743 (when (and (setq match (cdr (assq 'subscribe
1744 (gnus-topic-parameters topic))))
1745 (string-match match newsgroup))
1746 ;; Just subscribe the group.
1747 (gnus-subscribe-alphabetically newsgroup)
1748 ;; Add the group to the topic.
1749 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1750 ;; if this topic specifies a default level, use it
1751 (let ((subscribe-level (cdr (assq 'subscribe-level
1752 (gnus-topic-parameters topic)))))
1753 (when subscribe-level
1754 (gnus-group-change-level newsgroup subscribe-level
1755 gnus-level-default-subscribed)))
1759 (provide 'gnus-topic)
1761 ;;; gnus-topic.el ends here