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))
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.
67 (defcustom gnus-topic-indent-level 2
68 "*How much each subtopic should be indented."
72 (defcustom gnus-topic-display-empty-topics t
73 "*If non-nil, display the topic lines even of topics that have no unread articles."
77 ;; Internal variables.
79 (defvar gnus-topic-active-topology nil)
80 (defvar gnus-topic-active-alist nil)
81 (defvar gnus-topic-unreads nil)
83 (defvar gnus-topology-checked-p nil
84 "Whether the topology has been checked in this session.")
86 (defvar gnus-topic-killed-topics nil)
87 (defvar gnus-topic-inhibit-change-level nil)
89 (defconst gnus-topic-line-format-alist
93 (?g number-of-groups ?d)
94 (?a (gnus-topic-articles-in-topic entries) ?d)
95 (?A total-number-of-articles ?d)
98 (defvar gnus-topic-line-format-spec nil)
100 ;;; Utility functions
102 (defun gnus-group-topic-name ()
103 "The name of the topic on the current line."
104 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
105 (and topic (symbol-name topic))))
107 (defun gnus-group-topic-level ()
108 "The level of the topic on the current line."
109 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
111 (defun gnus-group-topic-unread ()
112 "The number of unread articles in topic on the current line."
113 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
115 (defun gnus-topic-unread (topic)
116 "Return the number of unread articles in TOPIC."
117 (or (cdr (assoc topic gnus-topic-unreads))
120 (defun gnus-group-topic-p ()
121 "Return non-nil if the current line is a topic."
122 (gnus-group-topic-name))
124 (defun gnus-topic-visible-p ()
125 "Return non-nil if the current topic is visible."
126 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
128 (defun gnus-topic-articles-in-topic (entries)
132 (when (numberp (setq number (car (pop entries))))
133 (incf total number)))
136 (defun gnus-group-topic (group)
137 "Return the topic GROUP is a member of."
138 (let ((alist gnus-topic-alist)
141 (when (member group (cdar alist))
142 (setq out (caar alist)
144 (setq alist (cdr alist)))
147 (defun gnus-group-parent-topic (group)
148 "Return the topic GROUP is member of by looking at the group buffer."
150 (set-buffer gnus-group-buffer)
151 (if (gnus-group-goto-group group)
153 (gnus-group-topic group))))
155 (defun gnus-topic-goto-topic (topic)
157 (gnus-goto-char (text-property-any (point-min) (point-max)
158 'gnus-topic (intern topic)))))
160 (defun gnus-topic-jump-to-topic (topic)
163 (list (completing-read "Go to topic: "
164 (mapcar 'list (gnus-topic-list))
166 (dolist (topic (gnus-current-topics topic))
168 (gnus-topic-goto-topic topic))
170 (defun gnus-current-topic ()
171 "Return the name of the current topic."
173 (or (get-text-property (point) 'gnus-topic)
175 (and (gnus-goto-char (previous-single-property-change
176 (point) 'gnus-topic))
177 (get-text-property (max (1- (point)) (point-min))
180 (symbol-name result))))
182 (defun gnus-current-topics (&optional topic)
183 "Return a list of all current topics, lowest in hierarchy first.
184 If TOPIC, start with that topic."
185 (let ((topic (or topic (gnus-current-topic)))
189 (setq topic (gnus-topic-parent-topic topic)))
192 (defun gnus-group-active-topic-p ()
193 "Say whether the current topic comes from the active topics."
196 (get-text-property (point) 'gnus-active)))
198 (defun gnus-topic-find-groups (topic &optional level all lowest recursive)
199 "Return entries for all visible groups in TOPIC.
200 If RECURSIVE is t, return groups in its subtopics too."
201 (let ((groups (cdr (assoc topic gnus-topic-alist)))
202 info clevel unread group params visible-groups entry active)
203 (setq lowest (or lowest 1))
204 (setq level (or level gnus-level-unsubscribed))
205 ;; We go through the newsrc to look for matches.
207 (when (setq group (pop groups))
208 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
210 params (gnus-info-params info)
211 active (gnus-active group)
212 unread (or (car entry)
213 (and (not (equal group "dummy.group"))
215 (- (1+ (cdr active)) (car active))))
216 clevel (or (gnus-info-level info)
217 (if (member group gnus-zombie-list)
218 gnus-level-zombie gnus-level-killed))))
220 info ; nil means that the group is dead.
222 (>= clevel lowest) ; Is inside the level we want.
224 (if (or (eq unread t)
226 gnus-group-list-inactive-groups
228 (and gnus-list-groups-with-ticked-articles
229 (cdr (assq 'tick (gnus-info-marks info))))
230 ;; Has right readedness.
231 ;; Check for permanent visibility.
232 (and gnus-permanently-visible-groups
233 (string-match gnus-permanently-visible-groups group))
234 (memq 'visible params)
235 (cdr (assq 'visible params)))
236 ;; Add this group to the list of visible groups.
237 (push (or entry group) visible-groups)))
238 (setq visible-groups (nreverse visible-groups))
241 (setq recursive (cdr (gnus-topic-find-topology topic))))
242 (mapcar (lambda (topic-topology)
244 (nconc visible-groups
245 (gnus-topic-find-groups
246 (caar topic-topology)
247 level all lowest topic-topology))))
251 (defun gnus-topic-previous-topic (topic)
252 "Return the previous topic on the same level as TOPIC."
253 (let ((top (cddr (gnus-topic-find-topology
254 (gnus-topic-parent-topic topic)))))
255 (unless (equal topic (caaar top))
256 (while (and top (not (equal (caaadr top) topic)))
257 (setq top (cdr top)))
260 (defun gnus-topic-parent-topic (topic &optional topology)
261 "Return the parent of TOPIC."
263 (setq topology gnus-topic-topology))
264 (let ((parent (car (pop topology)))
267 (not (setq found (equal (caaar topology) topic)))
268 (not (setq result (gnus-topic-parent-topic
269 topic (car topology)))))
270 (setq topology (cdr topology)))
271 (or result (and found parent))))
273 (defun gnus-topic-next-topic (topic &optional previous)
274 "Return the next sibling of TOPIC."
275 (let ((parentt (cddr (gnus-topic-find-topology
276 (gnus-topic-parent-topic topic))))
279 (not (equal (caaar parentt) topic)))
280 (setq prev (caaar parentt)
281 parentt (cdr parentt)))
286 (defun gnus-topic-forward-topic (num)
287 "Go to the next topic on the same level as the current one."
288 (let* ((topic (gnus-current-topic))
289 (way (if (< num 0) 'gnus-topic-previous-topic
290 'gnus-topic-next-topic))
292 (while (and (not (zerop num))
293 (setq topic (funcall way topic)))
294 (when (gnus-topic-goto-topic topic)
297 (goto-char (point-max)))
300 (defun gnus-topic-find-topology (topic &optional topology level remove)
301 "Return the topology of TOPIC."
303 (setq topology gnus-topic-topology)
307 (if (equal (caar topology) topic)
310 (delq topology remove))
311 (cons level topology))
312 (setq topology (cdr topology))
314 (not (setq result (gnus-topic-find-topology
315 topic (car topology) (1+ level)
317 (setq topology (cdr topology)))
320 (defvar gnus-tmp-topics nil)
321 (defun gnus-topic-list (&optional topology)
322 "Return a list of all topics in the topology."
324 (setq topology gnus-topic-topology
325 gnus-tmp-topics nil))
326 (push (caar topology) gnus-tmp-topics)
327 (mapcar 'gnus-topic-list (cdr topology))
330 ;;; Topic parameter jazz
332 (defun gnus-topic-parameters (topic)
333 "Return the parameters for TOPIC."
334 (let ((top (gnus-topic-find-topology topic)))
336 (nth 3 (cadr top)))))
338 (defun gnus-topic-set-parameters (topic parameters)
339 "Set the topic parameters of TOPIC to PARAMETERS."
340 (let ((top (gnus-topic-find-topology topic)))
342 (error "No such topic: %s" topic))
343 ;; We may have to extend if there is no parameters here
345 (unless (nthcdr 2 (cadr top))
346 (nconc (cadr top) (list nil)))
347 (unless (nthcdr 3 (cadr top))
348 (nconc (cadr top) (list nil)))
349 (setcar (nthcdr 3 (cadr top)) parameters)
351 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
353 (defun gnus-group-topic-parameters (group)
354 "Compute the group parameters for GROUP taking into account inheritance from topics."
355 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
357 (gnus-group-goto-group group)
359 (gnus-topic-hierarchical-parameters (gnus-current-topic))))))
361 (defun gnus-topic-hierarchical-parameters (topic)
362 "Return a topic list computed for TOPIC."
363 (let ((topics (gnus-current-topics topic))
364 params-list param out params)
366 (push (gnus-topic-parameters (pop topics)) params-list))
367 ;; We probably have lots of nil elements here, so
368 ;; we remove them. Probably faster than doing this "properly".
369 (setq params-list (delq nil params-list))
370 ;; Now we have all the parameters, so we go through them
371 ;; and do inheritance in the obvious way.
372 (while (setq params (pop params-list))
373 (while (setq param (pop params))
375 (setq param (cons param t)))
376 ;; Override any old versions of this param.
377 (gnus-pull (car param) out)
379 ;; Return the resulting parameter list.
382 ;;; General utility functions
384 (defun gnus-topic-enter-dribble ()
386 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
388 ;;; Generating group buffers
390 (defun gnus-group-prepare-topics (level &optional predicate lowest
391 regexp list-topic topic-level)
392 "List all newsgroups with unread articles of level LEVEL or lower.
393 Use the `gnus-group-topics' to sort the groups.
394 If PREDICTE is a function, list groups that the function returns non-nil;
395 if it is t, list groups that have no unread articles.
396 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
397 (set-buffer gnus-group-buffer)
398 (let ((buffer-read-only nil)
399 (lowest (or lowest 1))
401 (and gnus-group-listed-groups
402 (copy-sequence gnus-group-listed-groups))))
404 (when (or (not gnus-topic-alist)
405 (not gnus-topology-checked-p))
406 (gnus-topic-check-topology))
412 (when (or gnus-group-listed-groups
413 (and (>= level gnus-level-zombie)
414 (<= lowest gnus-level-zombie)))
415 (gnus-group-prepare-flat-list-dead
416 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
420 (when (or gnus-group-listed-groups
421 (and (>= level gnus-level-killed)
422 (<= lowest gnus-level-killed)))
423 (gnus-group-prepare-flat-list-dead
426 (gnus-delete-if (lambda (group)
427 (< (gnus-group-level group) gnus-level-killed))
429 (setq gnus-killed-list (sort gnus-killed-list 'string<)))
435 (when (or (< lowest gnus-level-zombie)
436 gnus-group-listed-groups)
438 (let ((top (gnus-topic-find-topology list-topic)))
439 (gnus-topic-prepare-topic (cdr top) (car top)
440 (or topic-level level) predicate
442 (gnus-topic-prepare-topic gnus-topic-topology 0
443 (or topic-level level) predicate
445 (gnus-group-set-mode-line)
446 (setq gnus-group-list-mode (cons level predicate))
447 (gnus-run-hooks 'gnus-group-prepare-hook))))
449 (defun gnus-topic-prepare-topic (topicl level &optional list-level
452 "Insert TOPIC into the group buffer.
453 If SILENT, don't insert anything. Return the number of unread
454 articles in the topic and its subtopics."
455 (let* ((type (pop topicl))
456 (entries (gnus-topic-find-groups
458 (if gnus-group-listed-groups
461 (or predicate gnus-group-listed-groups
463 (gnus-topic-hierarchical-parameters
465 (if gnus-group-listed-groups 0 lowest)))
466 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
467 (gnus-group-indentation
468 (make-string (* gnus-topic-indent-level level) ? ))
469 (beg (progn (beginning-of-line) (point)))
470 (topicl (reverse topicl))
471 (all-entries entries)
472 (point-max (point-max))
475 info entry end active tick)
476 ;; Insert any sub-topics.
479 (gnus-topic-prepare-topic
480 (pop topicl) (1+ level) list-level predicate
481 (not visiblep) lowest regexp)))
484 ;; Insert all the groups that belong in this topic.
485 (while (setq entry (pop entries))
486 (when (if (stringp entry)
487 (gnus-group-prepare-logic
490 (or (not gnus-group-listed-groups)
491 (if (< list-level gnus-level-zombie) nil
493 (if (member entry gnus-zombie-list)
494 gnus-level-zombie gnus-level-killed)))
495 (and (<= entry-level list-level)
496 (>= entry-level lowest)))))
499 (string-match regexp entry))
501 (funcall regexp entry))
504 (setq info (nth 2 entry))
505 (gnus-group-prepare-logic
506 (gnus-info-group info)
507 (and (or (not gnus-group-listed-groups)
508 (let ((entry-level (gnus-info-level info)))
509 (and (<= entry-level list-level)
510 (>= entry-level lowest))))
511 (or (not (functionp predicate))
512 (funcall predicate info))
513 (or (not (stringp regexp))
514 (string-match regexp (gnus-info-group info))))))
518 (gnus-group-insert-group-line
519 entry (if (member entry gnus-zombie-list)
520 gnus-level-zombie gnus-level-killed)
521 nil (- (1+ (cdr (setq active (gnus-active entry))))
525 (when (setq info (nth 2 entry))
526 (gnus-group-insert-group-line
527 (gnus-info-group info)
528 (gnus-info-level info) (gnus-info-marks info)
529 (car entry) (gnus-info-method info)))))
530 (when (and (listp entry)
531 (numberp (car entry)))
532 (incf unread (car entry)))
536 ;; Insert the topic line.
537 (when (and (not silent)
538 (or gnus-topic-display-empty-topics ;We want empty topics
539 (not (zerop unread)) ;Non-empty
540 tick ;Ticked articles
541 (/= point-max (point-max)))) ;Unactivated groups
542 (gnus-extent-start-open (point))
543 (gnus-topic-insert-topic-line
545 (not (eq (nth 2 type) 'hidden))
546 level all-entries unread))
547 (gnus-topic-update-unreads (car type) unread)
551 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
552 "Remove the current topic."
553 (let ((topic (gnus-group-topic-name))
554 (level (gnus-group-topic-level))
555 (beg (progn (beginning-of-line) (point)))
558 (while (and (zerop (forward-line 1))
559 (> (or (gnus-group-topic-level) (1+ level)) level)))
560 (delete-region beg (point))
561 ;; Do the change in this rather odd manner because it has been
562 ;; reported that some topics share parts of some lists, for some
563 ;; reason. I have been unable to determine why this is the
564 ;; case, but this hack seems to take care of things.
565 (let ((data (cadr (gnus-topic-find-topology topic))))
567 (list (if insert 'visible 'invisible)
571 (setq gnus-topic-alist
572 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
573 (gnus-topic-insert-topic topic in-level)))))
575 (defun gnus-topic-insert-topic (topic &optional level)
577 (gnus-group-prepare-topics
578 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
579 nil nil topic level))
581 (defun gnus-topic-fold (&optional insert topic)
582 "Remove/insert the current topic."
583 (let ((topic (or topic (gnus-group-topic-name))))
586 (if (not (gnus-group-active-topic-p))
587 (gnus-topic-remove-topic
588 (or insert (not (gnus-topic-visible-p))))
589 (let ((gnus-topic-topology gnus-topic-active-topology)
590 (gnus-topic-alist gnus-topic-active-alist)
591 (gnus-group-list-mode (cons 5 t)))
592 (gnus-topic-remove-topic
593 (or insert (not (gnus-topic-visible-p))) nil nil 9)
594 (gnus-topic-enter-dribble)))))))
596 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
598 (let* ((visible (if visiblep "" "..."))
599 (indentation (make-string (* gnus-topic-indent-level level) ? ))
600 (total-number-of-articles unread)
601 (number-of-groups (length entries))
602 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
604 (gnus-topic-update-unreads name unread)
608 (gnus-add-text-properties
611 (eval gnus-topic-line-format-spec))
612 (list 'gnus-topic (intern name)
613 'gnus-topic-level level
614 'gnus-topic-unread unread
615 'gnus-active active-topic
616 'gnus-topic-visible visiblep)))))
618 (defun gnus-topic-update-unreads (topic unreads)
619 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
621 (push (cons topic unreads) gnus-topic-unreads))
623 (defun gnus-topic-update-topics-containing-group (group)
624 "Update all topics that have GROUP as a member."
625 (when (and (eq major-mode 'gnus-group-mode)
628 (let ((alist gnus-topic-alist))
629 ;; This is probably not entirely correct. If a topic
630 ;; isn't shown, then it's not updated. But the updating
631 ;; should be performed in any case, since the topic's
632 ;; parent should be updated. Pfft.
634 (when (and (member group (cdar alist))
635 (gnus-topic-goto-topic (caar alist)))
636 (gnus-topic-update-topic-line (caar alist)))
639 (defun gnus-topic-update-topic ()
640 "Update all parent topics to the current group."
641 (when (and (eq major-mode 'gnus-group-mode)
643 (let ((group (gnus-group-group-name))
645 (buffer-read-only nil))
647 (gnus-get-info group)
648 (gnus-topic-goto-topic (gnus-current-topic)))
649 (gnus-topic-update-topic-line (gnus-group-topic-name))
652 (gnus-group-position-point)))))
654 (defun gnus-topic-goto-missing-group (group)
655 "Place point where GROUP is supposed to be inserted."
656 (let* ((topic (gnus-group-topic group))
657 (groups (cdr (assoc topic gnus-topic-alist)))
658 (g (cdr (member group groups)))
661 ;; Try to jump to a visible group.
662 (while (and g (not (gnus-group-goto-group (car g) t)))
664 ;; It wasn't visible, so we try to see where to insert it.
666 (setq g (cdr (member group (reverse groups))))
667 (while (and g unfound)
668 (when (gnus-group-goto-group (pop g) t)
673 (not (gnus-topic-goto-missing-topic topic)))
674 (let* ((top (gnus-topic-find-topology topic))
675 (children (cddr top))
678 (entries (gnus-topic-find-groups
679 (car type) (car gnus-group-list-mode)
680 (cdr gnus-group-list-mode))))
682 (incf unread (gnus-topic-unread (caar (pop children)))))
683 (while (setq entry (pop entries))
684 (when (numberp (car entry))
685 (incf unread (car entry))))
686 (gnus-topic-insert-topic-line
687 topic t t (car (gnus-topic-find-topology topic)) nil unread))))))
689 (defun gnus-topic-goto-missing-topic (topic)
690 (if (gnus-topic-goto-topic topic)
692 ;; Topic not displayed.
693 (let* ((top (gnus-topic-find-topology
694 (gnus-topic-parent-topic topic)))
695 (tp (reverse (cddr top))))
697 (gnus-topic-insert-topic-line
698 topic t t (car (gnus-topic-find-topology topic)) nil 0)
699 (while (not (equal (caaar tp) topic))
703 (not (gnus-topic-goto-topic (caaar tp))))
706 (gnus-topic-forward-topic 1)
707 (gnus-topic-goto-missing-topic (caadr top)))))
710 (defun gnus-topic-update-topic-line (topic-name &optional reads)
711 (let* ((top (gnus-topic-find-topology topic-name))
713 (children (cddr top))
714 (entries (gnus-topic-find-groups
715 (car type) (car gnus-group-list-mode)
716 (cdr gnus-group-list-mode)))
717 (parent (gnus-topic-parent-topic topic-name))
718 (all-entries entries)
720 old-unread entry new-unread)
721 (when (gnus-topic-goto-topic (car type))
722 ;; Tally all the groups that belong in this topic.
724 (setq unread (- (gnus-group-topic-unread) reads))
726 (incf unread (gnus-topic-unread (caar (pop children)))))
727 (while (setq entry (pop entries))
728 (when (numberp (car entry))
729 (incf unread (car entry)))))
730 (setq old-unread (gnus-group-topic-unread))
731 ;; Insert the topic line.
732 (gnus-topic-insert-topic-line
733 (car type) (gnus-topic-visible-p)
734 (not (eq (nth 2 type) 'hidden))
735 (gnus-group-topic-level) all-entries unread)
738 (setq new-unread (gnus-group-topic-unread)))
741 (gnus-topic-update-topic-line
743 (- (or old-unread 0) (or new-unread 0))))
746 (defun gnus-topic-group-indentation ()
748 (* gnus-topic-indent-level
751 (gnus-topic-goto-topic (gnus-current-topic))
752 (gnus-group-topic-level))
758 (gnus-add-shutdown 'gnus-topic-close 'gnus)
760 (defun gnus-topic-close ()
761 (setq gnus-topic-active-topology nil
762 gnus-topic-active-alist nil
763 gnus-topic-killed-topics nil
764 gnus-topology-checked-p nil))
766 (defun gnus-topic-check-topology ()
767 ;; The first time we set the topology to whatever we have
768 ;; gotten here, which can be rather random.
769 (unless gnus-topic-alist
770 (gnus-topic-init-alist))
772 (setq gnus-topology-checked-p t)
773 ;; Go through the topic alist and make sure that all topics
774 ;; are in the topic topology.
775 (let ((topics (gnus-topic-list))
776 (alist gnus-topic-alist)
779 (unless (member (caar alist) topics)
780 (nconc gnus-topic-topology
781 (list (list (list (caar alist) 'visible))))
783 (setq alist (cdr alist)))
785 (gnus-topic-enter-dribble))
786 ;; Conversely, go through the topology and make sure that all
787 ;; topologies have alists.
789 (unless (assoc (car topics) gnus-topic-alist)
790 (push (list (car topics)) gnus-topic-alist))
792 ;; Go through all living groups and make sure that
793 ;; they belong to some topic.
794 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
796 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
797 (newsrc (cdr gnus-newsrc-alist))
800 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
801 (setcdr entry (list group))
802 (setq entry (cdr entry)))))
803 ;; Go through all topics and make sure they contain only living groups.
804 (let ((alist gnus-topic-alist)
806 (while (setq topic (pop alist))
808 (if (and (cadr topic)
809 (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
810 (setq topic (cdr topic))
811 (setcdr topic (cddr topic)))))))
813 (defun gnus-topic-init-alist ()
814 "Initialize the topic structures."
815 (setq gnus-topic-topology
816 (cons (list "Gnus" 'visible)
817 (mapcar (lambda (topic)
818 (list (list (car topic) 'visible)))
820 (setq gnus-topic-alist
822 (mapcar (lambda (info) (gnus-info-group info))
823 (cdr gnus-newsrc-alist)))
825 (gnus-topic-enter-dribble))
829 (defun gnus-topic-clean-alist ()
830 "Remove bogus groups from the topic alist."
831 (let ((topic-alist gnus-topic-alist)
833 (unless gnus-killed-hashtb
834 (gnus-make-hashtable-from-killed))
835 (while (setq topic (pop topic-alist))
836 (let ((topic-name (pop topic))
837 group filtered-topic)
838 (while (setq group (pop topic))
839 (when (and (or (gnus-gethash group gnus-active-hashtb)
840 (gnus-info-method (gnus-get-info group)))
841 (not (gnus-gethash group gnus-killed-hashtb)))
842 (push group filtered-topic)))
843 (push (cons topic-name (nreverse filtered-topic)) result)))
844 (setq gnus-topic-alist (nreverse result))))
846 (defun gnus-topic-change-level (group level oldlevel &optional previous)
847 "Run when changing levels to enter/remove groups from topics."
849 (set-buffer gnus-group-buffer)
850 (let ((buffer-read-only nil))
851 (unless gnus-topic-inhibit-change-level
852 (gnus-group-goto-group (or (car (nth 2 previous)) group))
853 (when (and gnus-topic-mode
855 (not gnus-topic-inhibit-change-level))
856 ;; Remove the group from the topics.
857 (if (and (< oldlevel gnus-level-zombie)
858 (>= level gnus-level-zombie))
859 (let ((alist gnus-topic-alist))
860 (while (gnus-group-goto-group group)
863 (when (member group (car alist))
864 (setcdr (car alist) (delete group (cdar alist))))
866 ;; If the group is subscribed we enter it into the topics.
867 (when (and (< level gnus-level-zombie)
868 (>= oldlevel gnus-level-zombie))
869 (let* ((prev (gnus-group-group-name))
870 (gnus-topic-inhibit-change-level t)
871 (gnus-group-indentation
873 (* gnus-topic-indent-level
875 (gnus-topic-goto-topic (gnus-current-topic))
876 (gnus-group-topic-level))
879 (yanked (list group))
881 ;; Then we enter the yanked groups into the topics they belong
883 (when (setq alist (assoc (save-excursion
887 (caar gnus-topic-topology)))
890 (when (stringp yanked)
891 (setq yanked (list yanked)))
894 (if (not (cdr alist))
895 (setcdr alist (nconc yanked (cdr alist)))
896 (while (and (not end) (cdr alist))
897 (when (equal (cadr alist) prev)
898 (setcdr alist (nconc yanked (cdr alist)))
900 (setq alist (cdr alist)))
902 (nconc talist yanked))))))
903 (gnus-topic-update-topic))))))))
905 (defun gnus-topic-goto-next-group (group props)
906 "Go to group or the next group after group."
908 (if (not (memq 'gnus-topic props))
909 (goto-char (point-max))
910 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
911 (if (gnus-group-goto-group group)
913 ;; The group is no longer visible.
914 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
915 (after (cdr (member group (cdr list)))))
916 ;; First try to put point on a group after the current one.
918 (not (gnus-group-goto-group (car after))))
919 (setq after (cdr after)))
920 ;; Then try to put point on a group before point.
922 (setq after (cdr (member group (reverse (cdr list)))))
924 (not (gnus-group-goto-group (car after))))
925 (setq after (cdr after))))
926 ;; Finally, just put point on the topic.
928 (goto-char (point-min))
930 (gnus-topic-goto-topic (car list))
934 ;;; Topic-active functions
936 (defun gnus-topic-grok-active (&optional force)
937 "Parse all active groups and create topic structures for them."
938 ;; First we make sure that we have really read the active file.
940 (not gnus-topic-active-alist))
942 ;; Get a list of all groups available.
943 (mapatoms (lambda (g) (when (symbol-value g)
944 (push (symbol-name g) groups)))
946 (setq groups (sort groups 'string<))
947 ;; Init the variables.
948 (setq gnus-topic-active-topology (list (list "" 'visible)))
949 (setq gnus-topic-active-alist nil)
950 ;; Descend the top-level hierarchy.
951 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
952 ;; Set the top-level topic names to something nice.
953 (setcar (car gnus-topic-active-topology) "Gnus active")
954 (setcar (car gnus-topic-active-alist) "Gnus active"))))
956 (defun gnus-topic-grok-active-1 (topology groups)
957 (let* ((name (caar topology))
958 (prefix (concat "^" (regexp-quote name)))
959 tgroups ntopology group)
961 (string-match prefix (setq group (car groups))))
962 (if (not (string-match "\\." group (match-end 0)))
963 ;; There are no further hierarchies here, so we just
964 ;; enter this group into the list belonging to this
966 (push (pop groups) tgroups)
967 ;; New sub-hierarchy, so we add it to the topology.
968 (nconc topology (list (setq ntopology
969 (list (list (substring
970 group 0 (match-end 0))
972 ;; Descend the hierarchy.
973 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
974 ;; We remove the trailing "." from the topic name.
976 (if (string-match "\\.$" name)
977 (substring name 0 (match-beginning 0))
979 ;; Add this topic and its groups to the topic alist.
980 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
981 (setcar (car topology) name)
982 ;; We return the rest of the groups that didn't belong
986 ;;; Topic mode, commands and keymap.
988 (defvar gnus-topic-mode-map nil)
989 (defvar gnus-group-topic-map nil)
991 (unless gnus-topic-mode-map
992 (setq gnus-topic-mode-map (make-sparse-keymap))
994 ;; Override certain group mode keys.
995 (gnus-define-keys gnus-topic-mode-map
996 "=" gnus-topic-select-group
997 "\r" gnus-topic-select-group
998 " " gnus-topic-read-group
999 "\C-c\C-x" gnus-topic-expire-articles
1000 "\C-k" gnus-topic-kill-group
1001 "\C-y" gnus-topic-yank-group
1002 "\M-g" gnus-topic-get-new-news-this-topic
1003 "AT" gnus-topic-list-active
1004 "Gp" gnus-topic-edit-parameters
1005 "#" gnus-topic-mark-topic
1006 "\M-#" gnus-topic-unmark-topic
1007 [tab] gnus-topic-indent
1008 [(meta tab)] gnus-topic-unindent
1009 "\C-i" gnus-topic-indent
1010 "\M-\C-i" gnus-topic-unindent
1011 gnus-mouse-2 gnus-mouse-pick-topic)
1013 ;; Define a new submap.
1014 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
1015 "#" gnus-topic-mark-topic
1016 "\M-#" gnus-topic-unmark-topic
1017 "n" gnus-topic-create-topic
1018 "m" gnus-topic-move-group
1019 "D" gnus-topic-remove-group
1020 "c" gnus-topic-copy-group
1021 "h" gnus-topic-hide-topic
1022 "s" gnus-topic-show-topic
1023 "j" gnus-topic-jump-to-topic
1024 "M" gnus-topic-move-matching
1025 "C" gnus-topic-copy-matching
1026 "\C-i" gnus-topic-indent
1027 [tab] gnus-topic-indent
1028 "r" gnus-topic-rename
1029 "\177" gnus-topic-delete
1030 [delete] gnus-topic-delete
1031 "H" gnus-topic-toggle-display-empty-topics)
1033 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1034 "s" gnus-topic-sort-groups
1035 "a" gnus-topic-sort-groups-by-alphabet
1036 "u" gnus-topic-sort-groups-by-unread
1037 "l" gnus-topic-sort-groups-by-level
1038 "v" gnus-topic-sort-groups-by-score
1039 "r" gnus-topic-sort-groups-by-rank
1040 "m" gnus-topic-sort-groups-by-method))
1042 (defun gnus-topic-make-menu-bar ()
1043 (unless (boundp 'gnus-topic-menu)
1045 gnus-topic-menu gnus-topic-mode-map ""
1047 ["Toggle topics" gnus-topic-mode t]
1049 ["Copy" gnus-topic-copy-group t]
1050 ["Move" gnus-topic-move-group t]
1051 ["Remove" gnus-topic-remove-group t]
1052 ["Copy matching" gnus-topic-copy-matching t]
1053 ["Move matching" gnus-topic-move-matching t])
1055 ["Goto" gnus-topic-jump-to-topic t]
1056 ["Show" gnus-topic-show-topic t]
1057 ["Hide" gnus-topic-hide-topic t]
1058 ["Delete" gnus-topic-delete t]
1059 ["Rename" gnus-topic-rename t]
1060 ["Create" gnus-topic-create-topic t]
1061 ["Mark" gnus-topic-mark-topic t]
1062 ["Indent" gnus-topic-indent t]
1063 ["Sort" gnus-topic-sort-topics t]
1064 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1065 ["Edit parameters" gnus-topic-edit-parameters t])
1066 ["List active" gnus-topic-list-active t]))))
1068 (defun gnus-topic-mode (&optional arg redisplay)
1069 "Minor mode for topicsifying Gnus group buffers."
1070 (interactive (list current-prefix-arg t))
1071 (when (eq major-mode 'gnus-group-mode)
1072 (make-local-variable 'gnus-topic-mode)
1073 (setq gnus-topic-mode
1074 (if (null arg) (not gnus-topic-mode)
1075 (> (prefix-numeric-value arg) 0)))
1076 ;; Infest Gnus with topics.
1077 (if (not gnus-topic-mode)
1078 (setq gnus-goto-missing-group-function nil)
1079 (when (gnus-visual-p 'topic-menu 'menu)
1080 (gnus-topic-make-menu-bar))
1081 (gnus-set-format 'topic t)
1082 (gnus-add-minor-mode 'gnus-topic-mode " Topic"
1083 gnus-topic-mode-map nil (lambda (&rest junk)
1085 (gnus-topic-mode nil t)))
1086 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1087 (set (make-local-variable 'gnus-group-prepare-function)
1088 'gnus-group-prepare-topics)
1089 (set (make-local-variable 'gnus-group-get-parameter-function)
1090 'gnus-group-topic-parameters)
1091 (set (make-local-variable 'gnus-group-goto-next-group-function)
1092 'gnus-topic-goto-next-group)
1093 (set (make-local-variable 'gnus-group-indentation-function)
1094 'gnus-topic-group-indentation)
1095 (set (make-local-variable 'gnus-group-update-group-function)
1096 'gnus-topic-update-topics-containing-group)
1097 (set (make-local-variable 'gnus-group-sort-alist-function)
1098 'gnus-group-sort-topic)
1099 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1100 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1101 (make-local-hook 'gnus-check-bogus-groups-hook)
1102 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1103 (setq gnus-topology-checked-p nil)
1104 ;; We check the topology.
1105 (when gnus-newsrc-alist
1106 (gnus-topic-check-topology))
1107 (gnus-run-hooks 'gnus-topic-mode-hook))
1108 ;; Remove topic infestation.
1109 (unless gnus-topic-mode
1110 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1111 (setq gnus-group-change-level-function nil)
1112 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1113 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1114 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1116 (gnus-group-list-groups))))
1118 (defun gnus-topic-select-group (&optional all)
1119 "Select this newsgroup.
1120 No article is selected automatically.
1121 If ALL is non-nil, already read articles become readable.
1122 If ALL is a number, fetch this number of articles.
1124 If performed over a topic line, toggle folding the topic."
1126 (if (gnus-group-topic-p)
1127 (let ((gnus-group-list-mode
1128 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1129 (gnus-topic-fold all)
1130 (gnus-dribble-touch))
1131 (gnus-group-select-group all)))
1133 (defun gnus-mouse-pick-topic (e)
1134 "Select the group or topic under the mouse pointer."
1137 (gnus-topic-read-group nil))
1139 (defun gnus-topic-expire-articles (topic)
1140 "Expire articles in this topic or group."
1141 (interactive (list (gnus-group-topic-name)))
1143 (call-interactively 'gnus-group-expire-articles)
1145 (gnus-message 5 "Expiring groups in %s..." topic)
1146 (let ((gnus-group-marked
1147 (mapcar (lambda (entry) (car (nth 2 entry)))
1148 (gnus-topic-find-groups topic gnus-level-killed t))))
1149 (gnus-group-expire-articles nil))
1150 (gnus-message 5 "Expiring groups in %s...done" topic))))
1152 (defun gnus-topic-read-group (&optional all no-article group)
1153 "Read news in this newsgroup.
1154 If the prefix argument ALL is non-nil, already read articles become
1155 readable. IF ALL is a number, fetch this number of articles. If the
1156 optional argument NO-ARTICLE is non-nil, no article will be
1157 auto-selected upon group entry. If GROUP is non-nil, fetch that
1160 If performed over a topic line, toggle folding the topic."
1162 (if (gnus-group-topic-p)
1163 (let ((gnus-group-list-mode
1164 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1165 (gnus-topic-fold all))
1166 (gnus-group-read-group all no-article group)))
1168 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1169 "Create a new TOPIC under PARENT.
1170 When used interactively, PARENT will be the topic under point."
1173 (read-string "New topic: ")
1174 (gnus-current-topic)))
1175 ;; Check whether this topic already exists.
1176 (when (gnus-topic-find-topology topic)
1177 (error "Topic already exists"))
1179 (setq parent (caar gnus-topic-topology)))
1180 (let ((top (cdr (gnus-topic-find-topology parent)))
1181 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1183 (error "No such parent topic: %s" parent))
1186 (while (and (cdr top)
1187 (not (equal (caaadr top) previous)))
1188 (setq top (cdr top)))
1189 (setcdr top (cons full-topic (cdr top))))
1190 (nconc top (list full-topic)))
1191 (unless (assoc topic gnus-topic-alist)
1192 (push (list topic) gnus-topic-alist)))
1193 (gnus-topic-enter-dribble)
1194 (gnus-group-list-groups)
1195 (gnus-topic-goto-topic topic))
1198 ;; 1. When the marked groups are overlapped with the process
1199 ;; region, the behavior of move or remove is not right.
1200 ;; 2. Can't process on several marked groups with a same name,
1201 ;; because gnus-group-marked only keeps one copy.
1203 (defun gnus-topic-move-group (n topic &optional copyp)
1204 "Move the next N groups to TOPIC.
1205 If COPYP, copy the groups instead."
1207 (list current-prefix-arg
1208 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1209 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1210 gnus-group-marked t))
1211 (groups (gnus-group-process-prefix n))
1212 (topicl (assoc topic gnus-topic-alist))
1213 (start-topic (gnus-group-topic-name))
1214 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1216 (if (and (not groups) (not copyp) start-topic)
1217 (gnus-topic-move start-topic topic)
1220 (gnus-group-remove-mark g use-marked)
1222 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1224 (setcdr entry (gnus-delete-first g (cdr entry))))
1225 (nconc topicl (list g)))
1227 (gnus-topic-enter-dribble)
1229 (gnus-group-goto-group start-group)
1230 (gnus-topic-goto-topic start-topic))
1231 (gnus-group-list-groups))))
1233 (defun gnus-topic-remove-group (&optional n)
1234 "Remove the current group from the topic."
1236 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1237 gnus-group-marked t))
1238 (groups (gnus-group-process-prefix n)))
1241 (gnus-group-remove-mark group use-marked)
1242 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1243 (buffer-read-only nil))
1244 (when (and topicl group)
1246 (gnus-delete-first group topicl))
1247 (gnus-topic-update-topic)))
1249 (gnus-topic-enter-dribble)
1250 (gnus-group-position-point)))
1252 (defun gnus-topic-copy-group (n topic)
1253 "Copy the current group to a topic."
1255 (list current-prefix-arg
1256 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1257 (gnus-topic-move-group n topic t))
1259 (defun gnus-topic-kill-group (&optional n discard)
1260 "Kill the next N groups."
1262 (if (gnus-group-topic-p)
1263 (let ((topic (gnus-group-topic-name)))
1265 (gnus-topic-find-topology topic)
1266 (assoc topic gnus-topic-alist))
1267 gnus-topic-killed-topics)
1268 (gnus-topic-remove-topic nil t)
1269 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1270 (gnus-topic-enter-dribble))
1271 (gnus-group-kill-group n discard)
1272 (if (not (gnus-group-topic-p))
1273 (gnus-topic-update-topic)
1274 ;; Move up one line so that we update the right topic.
1276 (gnus-topic-update-topic)
1279 (defun gnus-topic-yank-group (&optional arg)
1280 "Yank the last topic."
1282 (if gnus-topic-killed-topics
1284 (or (gnus-group-topic-name)
1285 (gnus-topic-next-topic (gnus-current-topic))))
1286 (data (pop gnus-topic-killed-topics))
1289 (push alist gnus-topic-alist)
1290 (gnus-topic-create-topic
1291 (caar item) (gnus-topic-parent-topic previous) previous
1293 (gnus-topic-enter-dribble)
1294 (gnus-topic-goto-topic (caar item)))
1295 (let* ((prev (gnus-group-group-name))
1296 (gnus-topic-inhibit-change-level t)
1297 (gnus-group-indentation
1299 (* gnus-topic-indent-level
1301 (gnus-topic-goto-topic (gnus-current-topic))
1302 (gnus-group-topic-level))
1306 ;; We first yank the groups the normal way...
1307 (setq yanked (gnus-group-yank-group arg))
1308 ;; Then we enter the yanked groups into the topics they belong
1310 (setq alist (assoc (save-excursion
1312 (gnus-current-topic))
1314 (when (stringp yanked)
1315 (setq yanked (list yanked)))
1317 (nconc alist yanked)
1318 (if (not (cdr alist))
1319 (setcdr alist (nconc yanked (cdr alist)))
1321 (when (equal (cadr alist) prev)
1322 (setcdr alist (nconc yanked (cdr alist)))
1324 (setq alist (cdr alist))))))
1325 (gnus-topic-update-topic)))
1327 (defun gnus-topic-hide-topic (&optional permanent)
1328 "Hide the current topic.
1329 If PERMANENT, make it stay hidden in subsequent sessions as well."
1331 (when (gnus-current-topic)
1332 (gnus-topic-goto-topic (gnus-current-topic))
1336 (gnus-topic-find-topology (gnus-current-topic))))
1338 (gnus-topic-remove-topic nil nil)))
1340 (defun gnus-topic-show-topic (&optional permanent)
1341 "Show the hidden topic.
1342 If PERMANENT, make it stay shown in subsequent sessions as well."
1344 (when (gnus-group-topic-p)
1346 (gnus-topic-remove-topic t nil)
1348 (gnus-topic-find-topology
1349 (completing-read "Show topic: " gnus-topic-alist nil t))))
1350 (setcar (cddr (cadr topic)) nil)
1351 (setcar (cdr (cadr topic)) 'visible)
1352 (gnus-group-list-groups)))))
1354 (defun gnus-topic-mark-topic (topic &optional unmark recursive)
1355 "Mark all groups in the TOPIC with the process mark.
1356 If RECURSIVE is t, mark its subtopics too."
1357 (interactive (list (gnus-group-topic-name)
1359 (and current-prefix-arg t)))
1361 (call-interactively 'gnus-group-mark-group)
1363 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1366 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1367 (gnus-info-group (nth 2 (pop groups)))))))))
1369 (defun gnus-topic-unmark-topic (topic &optional dummy recursive)
1370 "Remove the process mark from all groups in the TOPIC.
1371 If RECURSIVE is t, unmark its subtopics too."
1372 (interactive (list (gnus-group-topic-name)
1374 (and current-prefix-arg t)))
1376 (call-interactively 'gnus-group-unmark-group)
1377 (gnus-topic-mark-topic topic t recursive)))
1379 (defun gnus-topic-get-new-news-this-topic (&optional n)
1380 "Check for new news in the current topic."
1382 (if (not (gnus-group-topic-p))
1383 (gnus-group-get-new-news-this-group n)
1384 (gnus-topic-mark-topic (gnus-group-topic-name) nil (and n t))
1385 (gnus-group-get-new-news-this-group)))
1387 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1388 "Move all groups that match REGEXP to some topic."
1393 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1394 (read-string (format "Move to %s (regexp): " topic))))))
1395 (gnus-group-mark-regexp regexp)
1396 (gnus-topic-move-group nil topic copyp))
1398 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1399 "Copy all groups that match REGEXP to some topic."
1404 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1405 (read-string (format "Copy to %s (regexp): " topic))))))
1406 (gnus-topic-move-matching regexp topic t))
1408 (defun gnus-topic-delete (topic)
1410 (interactive (list (gnus-group-topic-name)))
1412 (error "No topic to be deleted"))
1413 (let ((entry (assoc topic gnus-topic-alist))
1414 (buffer-read-only nil))
1416 (error "Topic not empty"))
1417 ;; Delete if visible.
1418 (when (gnus-topic-goto-topic topic)
1420 ;; Remove from alist.
1421 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1422 ;; Remove from topology.
1423 (gnus-topic-find-topology topic nil nil 'delete)
1424 (gnus-dribble-touch)))
1426 (defun gnus-topic-rename (old-name new-name)
1429 (let ((topic (gnus-current-topic)))
1431 (read-string (format "Rename %s to: " topic)))))
1432 ;; Check whether the new name exists.
1433 (when (gnus-topic-find-topology new-name)
1434 (error "Topic '%s' already exists" new-name))
1435 ;; "nil" is an invalid name, for reasons I'd rather not go
1436 ;; into here. Trust me.
1437 (when (equal new-name "nil")
1438 (error "Invalid name: %s" nil))
1440 (let ((top (gnus-topic-find-topology old-name))
1441 (entry (assoc old-name gnus-topic-alist)))
1443 (setcar (cadr top) new-name))
1445 (setcar entry new-name))
1447 (gnus-dribble-touch)
1448 (gnus-group-list-groups)
1451 (defun gnus-topic-indent (&optional unindent)
1452 "Indent a topic -- make it a sub-topic of the previous topic.
1453 If UNINDENT, remove an indentation."
1456 (gnus-topic-unindent)
1457 (let* ((topic (gnus-current-topic))
1458 (parent (gnus-topic-previous-topic topic))
1459 (buffer-read-only nil))
1461 (error "Nothing to indent %s into" topic))
1463 (gnus-topic-goto-topic topic)
1464 (gnus-topic-kill-group)
1465 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1466 (gnus-topic-create-topic
1467 topic parent nil (cdaar gnus-topic-killed-topics))
1468 (pop gnus-topic-killed-topics)
1469 (or (gnus-topic-goto-topic topic)
1470 (gnus-topic-goto-topic parent))))))
1472 (defun gnus-topic-unindent ()
1475 (let* ((topic (gnus-current-topic))
1476 (parent (gnus-topic-parent-topic topic))
1477 (grandparent (gnus-topic-parent-topic parent)))
1479 (error "Nothing to indent %s into" topic))
1481 (gnus-topic-goto-topic topic)
1482 (gnus-topic-kill-group)
1483 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1484 (gnus-topic-create-topic
1485 topic grandparent (gnus-topic-next-topic parent)
1486 (cdaar gnus-topic-killed-topics))
1487 (pop gnus-topic-killed-topics)
1488 (gnus-topic-goto-topic topic))))
1490 (defun gnus-topic-list-active (&optional force)
1491 "List all groups that Gnus knows about in a topicsified fashion.
1492 If FORCE, always re-read the active file."
1495 (gnus-get-killed-groups))
1496 (gnus-topic-grok-active force)
1497 (let ((gnus-topic-topology gnus-topic-active-topology)
1498 (gnus-topic-alist gnus-topic-active-alist)
1499 gnus-killed-list gnus-zombie-list)
1500 (gnus-group-list-groups gnus-level-killed nil 1)))
1502 (defun gnus-topic-toggle-display-empty-topics ()
1503 "Show/hide topics that have no unread articles."
1505 (setq gnus-topic-display-empty-topics
1506 (not gnus-topic-display-empty-topics))
1507 (gnus-group-list-groups)
1508 (message "%s empty topics"
1509 (if gnus-topic-display-empty-topics
1510 "Showing" "Hiding")))
1512 ;;; Topic sorting functions
1514 (defun gnus-topic-edit-parameters (group)
1515 "Edit the group parameters of GROUP.
1516 If performed on a topic, edit the topic parameters instead."
1517 (interactive (list (gnus-group-group-name)))
1519 (gnus-group-edit-group-parameters group)
1520 (if (not (gnus-group-topic-p))
1521 (error "Nothing to edit on the current line")
1522 (let ((topic (gnus-group-topic-name)))
1524 (gnus-topic-parameters topic)
1525 (format "Editing the topic parameters for `%s'."
1528 (gnus-topic-set-parameters ,topic form)))))))
1530 (defun gnus-group-sort-topic (func reverse)
1531 "Sort groups in the topics according to FUNC and REVERSE."
1532 (let ((alist gnus-topic-alist))
1534 ;; !!!Sometimes nil elements sneak into the alist,
1535 ;; for some reason or other.
1536 (setcar alist (delq nil (car alist)))
1537 (setcar alist (delete "dummy.group" (car alist)))
1538 (gnus-topic-sort-topic (pop alist) func reverse))))
1540 (defun gnus-topic-sort-topic (topic func reverse)
1541 ;; Each topic only lists the name of the group, while
1542 ;; the sort predicates expect group infos as inputs.
1543 ;; So we first transform the group names into infos,
1544 ;; then sort, and then transform back into group names.
1548 (lambda (info) (gnus-info-group info))
1551 (lambda (group) (gnus-get-info group))
1554 ;; Do the reversal, if necessary.
1556 (setcdr topic (nreverse (cdr topic)))))
1558 (defun gnus-topic-sort-groups (func &optional reverse)
1559 "Sort the current topic according to FUNC.
1560 If REVERSE, reverse the sorting order."
1561 (interactive (list gnus-group-sort-function current-prefix-arg))
1562 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1563 (gnus-topic-sort-topic
1564 topic (gnus-make-sort-function func) reverse)
1565 (gnus-group-list-groups)))
1567 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1568 "Sort the current topic alphabetically by group name.
1569 If REVERSE, sort in reverse order."
1571 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1573 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1574 "Sort the current topic by number of unread articles.
1575 If REVERSE, sort in reverse order."
1577 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1579 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1580 "Sort the current topic by group level.
1581 If REVERSE, sort in reverse order."
1583 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1585 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1586 "Sort the current topic by group score.
1587 If REVERSE, sort in reverse order."
1589 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1591 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1592 "Sort the current topic by group rank.
1593 If REVERSE, sort in reverse order."
1595 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1597 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1598 "Sort the current topic alphabetically by backend name.
1599 If REVERSE, sort in reverse order."
1601 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1603 (defun gnus-topic-sort-topics-1 (top reverse)
1606 (mapcar `(lambda (top)
1607 (gnus-topic-sort-topics-1 top ,reverse))
1610 (string-lessp (caar t1) (caar t2)))))))
1611 (setcdr top (if reverse (reverse subtop) subtop))))
1614 (defun gnus-topic-sort-topics (&optional topic reverse)
1615 "Sort topics in TOPIC alphabeticaly by topic name.
1616 If REVERSE, reverse the sorting order."
1618 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1619 (gnus-current-topic))
1620 current-prefix-arg))
1621 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1622 gnus-topic-topology)))
1623 (gnus-topic-sort-topics-1 topic-topology reverse)
1624 (gnus-topic-enter-dribble)
1625 (gnus-group-list-groups)
1626 (gnus-topic-goto-topic topic)))
1628 (defun gnus-topic-move (current to)
1629 "Move the CURRENT topic to TO."
1632 (gnus-group-topic-name)
1633 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1634 (unless (and current to)
1635 (error "Can't find topic"))
1636 (let ((current-top (cdr (gnus-topic-find-topology current)))
1637 (to-top (cdr (gnus-topic-find-topology to))))
1639 (error "Can't find topic `%s'" current))
1641 (error "Can't find topic `%s'" to))
1642 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1643 (error "Can't move `%s' to its sub-level" current))
1644 (gnus-topic-find-topology current nil nil 'delete)
1646 (setq to-top (cdr to-top)))
1647 (setcdr to-top (list current-top))
1648 (gnus-topic-enter-dribble)
1649 (gnus-group-list-groups)
1650 (gnus-topic-goto-topic current)))
1652 (defun gnus-subscribe-topics (newsgroup)
1654 (let (match gnus-group-change-level-function)
1655 (dolist (topic (gnus-topic-list))
1656 (when (and (setq match (cdr (assq 'subscribe
1657 (gnus-topic-parameters topic))))
1658 (string-match match newsgroup))
1659 ;; Just subscribe the group.
1660 (gnus-subscribe-alphabetically newsgroup)
1661 ;; Add the group to the topic.
1662 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1665 (provide 'gnus-topic)
1667 ;;; gnus-topic.el ends here