1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Ilja Weis <kult@uni-paderborn.de>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
31 (eval-when-compile (require 'cl))
38 (defgroup gnus-topic nil
42 (defvar gnus-topic-mode nil
43 "Minor mode for Gnus group buffers.")
45 (defcustom gnus-topic-mode-hook nil
46 "Hook run in topic mode buffers."
50 (when (featurep 'xemacs)
51 (add-hook 'gnus-topic-mode-hook 'gnus-xmas-topic-menu-add))
53 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
54 "Format of topic lines.
55 It works along the same lines as a normal formatting string,
56 with some simple extensions.
58 %i Indentation based on topic level.
60 %v Nothing if the topic is visible, \"...\" otherwise.
61 %g Number of groups in the topic.
62 %a Number of unread articles in the groups in the topic.
63 %A Number of unread articles in the groups in the topic and its subtopics.
65 General format specifiers can also be used.
66 See Info node `(gnus)Formatting Variables'."
67 :link '(custom-manual "(gnus)Formatting Variables")
71 (defcustom gnus-topic-indent-level 2
72 "*How much each subtopic should be indented."
76 (defcustom gnus-topic-display-empty-topics t
77 "*If non-nil, display the topic lines even of topics that have no unread articles."
81 ;; Internal variables.
83 (defvar gnus-topic-active-topology nil)
84 (defvar gnus-topic-active-alist nil)
85 (defvar gnus-topic-unreads nil)
87 (defvar gnus-topology-checked-p nil
88 "Whether the topology has been checked in this session.")
90 (defvar gnus-topic-killed-topics nil)
91 (defvar gnus-topic-inhibit-change-level nil)
93 (defconst gnus-topic-line-format-alist
97 (?g number-of-groups ?d)
98 (?a (gnus-topic-articles-in-topic entries) ?d)
99 (?A total-number-of-articles ?d)
102 (defvar gnus-topic-line-format-spec nil)
104 ;;; Utility functions
106 (defun gnus-group-topic-name ()
107 "The name of the topic on the current line."
108 (let ((topic (get-text-property (point-at-bol) 'gnus-topic)))
109 (and topic (symbol-name topic))))
111 (defun gnus-group-topic-level ()
112 "The level of the topic on the current line."
113 (get-text-property (point-at-bol) 'gnus-topic-level))
115 (defun gnus-group-topic-unread ()
116 "The number of unread articles in topic on the current line."
117 (get-text-property (point-at-bol) 'gnus-topic-unread))
119 (defun gnus-topic-unread (topic)
120 "Return the number of unread articles in TOPIC."
121 (or (cdr (assoc topic gnus-topic-unreads))
124 (defun gnus-group-topic-p ()
125 "Return non-nil if the current line is a topic."
126 (gnus-group-topic-name))
128 (defun gnus-topic-visible-p ()
129 "Return non-nil if the current topic is visible."
130 (get-text-property (point-at-bol) 'gnus-topic-visible))
132 (defun gnus-topic-articles-in-topic (entries)
136 (when (numberp (setq number (car (pop entries))))
137 (incf total number)))
140 (defun gnus-group-topic (group)
141 "Return the topic GROUP is a member of."
142 (let ((alist gnus-topic-alist)
145 (when (member group (cdar alist))
146 (setq out (caar alist)
148 (setq alist (cdr alist)))
151 (defun gnus-group-parent-topic (group)
152 "Return the topic GROUP is member of by looking at the group buffer."
154 (set-buffer gnus-group-buffer)
155 (if (gnus-group-goto-group group)
157 (gnus-group-topic group))))
159 (defun gnus-topic-goto-topic (topic)
161 (gnus-goto-char (text-property-any (point-min) (point-max)
162 'gnus-topic (intern topic)))))
164 (defun gnus-topic-jump-to-topic (topic)
167 (list (completing-read "Go to topic: "
168 (mapcar 'list (gnus-topic-list))
170 (let ((buffer-read-only nil))
171 (dolist (topic (gnus-current-topics topic))
172 (unless (gnus-topic-goto-topic topic)
173 (gnus-topic-goto-missing-topic topic)
174 (gnus-topic-display-missing-topic topic))))
175 (gnus-topic-goto-topic topic))
177 (defun gnus-current-topic ()
178 "Return the name of the current topic."
180 (or (get-text-property (point) 'gnus-topic)
182 (and (gnus-goto-char (previous-single-property-change
183 (point) 'gnus-topic))
184 (get-text-property (max (1- (point)) (point-min))
187 (symbol-name result))))
189 (defun gnus-current-topics (&optional topic)
190 "Return a list of all current topics, lowest in hierarchy first.
191 If TOPIC, start with that topic."
192 (let ((topic (or topic (gnus-current-topic)))
196 (setq topic (gnus-topic-parent-topic topic)))
199 (defun gnus-group-active-topic-p ()
200 "Say whether the current topic comes from the active topics."
201 (get-text-property (point-at-bol) 'gnus-active))
203 (defun gnus-topic-find-groups (topic &optional level all lowest recursive)
204 "Return entries for all visible groups in TOPIC.
205 If RECURSIVE is t, return groups in its subtopics too."
206 (let ((groups (cdr (assoc topic gnus-topic-alist)))
207 info clevel unread group params visible-groups entry active)
208 (setq lowest (or lowest 1))
209 (setq level (or level gnus-level-unsubscribed))
210 ;; We go through the newsrc to look for matches.
212 (when (setq group (pop groups))
213 (setq entry (gnus-group-entry group)
215 params (gnus-info-params info)
216 active (gnus-active group)
217 unread (or (car entry)
218 (and (not (equal group "dummy.group"))
220 (- (1+ (cdr active)) (car active))))
221 clevel (or (gnus-info-level info)
222 (if (member group gnus-zombie-list)
223 gnus-level-zombie gnus-level-killed))))
225 info ; nil means that the group is dead.
227 (>= clevel lowest) ; Is inside the level we want.
229 (if (or (eq unread t)
231 gnus-group-list-inactive-groups
233 (and gnus-list-groups-with-ticked-articles
234 (cdr (assq 'tick (gnus-info-marks info))))
235 ;; Has right readedness.
236 ;; Check for permanent visibility.
237 (and gnus-permanently-visible-groups
238 (string-match gnus-permanently-visible-groups group))
239 (memq 'visible params)
240 (cdr (assq 'visible params)))
241 ;; Add this group to the list of visible groups.
242 (push (or entry group) visible-groups)))
243 (setq visible-groups (nreverse visible-groups))
246 (setq recursive (cdr (gnus-topic-find-topology topic))))
247 (mapcar (lambda (topic-topology)
249 (nconc visible-groups
250 (gnus-topic-find-groups
251 (caar topic-topology)
252 level all lowest topic-topology))))
256 (defun gnus-topic-goto-previous-topic (n)
257 "Go to the N'th previous topic."
259 (gnus-topic-goto-next-topic (- n)))
261 (defun gnus-topic-goto-next-topic (n)
262 "Go to the N'th next topic."
264 (let ((backward (< n 0))
266 (topic (gnus-current-topic)))
270 (gnus-topic-previous-topic topic)
271 (gnus-topic-next-topic topic))))
272 (gnus-topic-goto-topic topic)
275 (gnus-message 7 "No more topics"))
278 (defun gnus-topic-previous-topic (topic)
279 "Return the previous topic on the same level as TOPIC."
280 (let ((top (cddr (gnus-topic-find-topology
281 (gnus-topic-parent-topic topic)))))
282 (unless (equal topic (caaar top))
283 (while (and top (not (equal (caaadr top) topic)))
284 (setq top (cdr top)))
287 (defun gnus-topic-parent-topic (topic &optional topology)
288 "Return the parent of TOPIC."
290 (setq topology gnus-topic-topology))
291 (let ((parent (car (pop topology)))
294 (not (setq found (equal (caaar topology) topic)))
295 (not (setq result (gnus-topic-parent-topic
296 topic (car topology)))))
297 (setq topology (cdr topology)))
298 (or result (and found parent))))
300 (defun gnus-topic-next-topic (topic &optional previous)
301 "Return the next sibling of TOPIC."
302 (let ((parentt (cddr (gnus-topic-find-topology
303 (gnus-topic-parent-topic topic))))
306 (not (equal (caaar parentt) topic)))
307 (setq prev (caaar parentt)
308 parentt (cdr parentt)))
313 (defun gnus-topic-forward-topic (num)
314 "Go to the next topic on the same level as the current one."
315 (let* ((topic (gnus-current-topic))
316 (way (if (< num 0) 'gnus-topic-previous-topic
317 'gnus-topic-next-topic))
319 (while (and (not (zerop num))
320 (setq topic (funcall way topic)))
321 (when (gnus-topic-goto-topic topic)
324 (goto-char (point-max)))
327 (defun gnus-topic-find-topology (topic &optional topology level remove)
328 "Return the topology of TOPIC."
330 (setq topology gnus-topic-topology)
334 (if (equal (caar topology) topic)
337 (delq topology remove))
338 (cons level topology))
339 (setq topology (cdr topology))
341 (not (setq result (gnus-topic-find-topology
342 topic (car topology) (1+ level)
344 (setq topology (cdr topology)))
347 (defvar gnus-tmp-topics nil)
348 (defun gnus-topic-list (&optional topology)
349 "Return a list of all topics in the topology."
351 (setq topology gnus-topic-topology
352 gnus-tmp-topics nil))
353 (push (caar topology) gnus-tmp-topics)
354 (mapcar 'gnus-topic-list (cdr topology))
357 ;;; Topic parameter jazz
359 (defun gnus-topic-parameters (topic)
360 "Return the parameters for TOPIC."
361 (let ((top (gnus-topic-find-topology topic)))
363 (nth 3 (cadr top)))))
365 (defun gnus-topic-set-parameters (topic parameters)
366 "Set the topic parameters of TOPIC to PARAMETERS."
367 (let ((top (gnus-topic-find-topology topic)))
369 (error "No such topic: %s" topic))
370 ;; We may have to extend if there is no parameters here
372 (unless (nthcdr 2 (cadr top))
373 (nconc (cadr top) (list nil)))
374 (unless (nthcdr 3 (cadr top))
375 (nconc (cadr top) (list nil)))
376 (setcar (nthcdr 3 (cadr top)) parameters)
378 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
380 (defun gnus-group-topic-parameters (group)
381 "Compute the group parameters for GROUP in topic mode.
382 Possibly inherit parameters from topics above GROUP."
383 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
385 (gnus-topic-hierarchical-parameters
386 ;; First we try to go to the group within the group buffer and find the
387 ;; topic for the group that way. This hopefully copes well with groups
388 ;; that are in more than one topic. Failing that (i.e. when the group
389 ;; isn't visible in the group buffer) we find a topic for the group via
391 (or (and (gnus-group-goto-group group)
392 (gnus-current-topic))
393 (gnus-group-topic group))
396 (defun gnus-topic-hierarchical-parameters (topic &optional group-params-list)
397 "Compute the topic parameters for TOPIC.
398 Possibly inherit parameters from topics above TOPIC.
399 If optional argument GROUP-PARAMS-LIST is non-nil, use it as the basis for
402 ;; We probably have lots of nil elements here, so we remove them.
403 ;; Probably faster than doing this "properly".
404 (delq nil (cons group-params-list
405 (mapcar 'gnus-topic-parameters
406 (gnus-current-topics topic)))))
408 ;; Now we have all the parameters, so we go through them
409 ;; and do inheritance in the obvious way.
411 (while (setq params (pop params-list))
412 (while (setq param (pop params))
414 (setq param (cons param t)))
415 (cond ((eq (car param) 'posting-style)
416 (let ((param (cdr param))
418 (while (setq elt (pop param))
419 (unless (assoc (car elt) posting-style)
420 (push elt posting-style)))))
422 (unless (assq (car param) out)
423 (push param out))))))
424 (and posting-style (push (cons 'posting-style posting-style) out)))
425 ;; Return the resulting parameter list.
428 ;;; General utility functions
430 (defun gnus-topic-enter-dribble ()
432 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
434 ;;; Generating group buffers
436 (defun gnus-group-prepare-topics (level &optional predicate lowest
437 regexp list-topic topic-level)
438 "List all newsgroups with unread articles of level LEVEL or lower.
439 Use the `gnus-group-topics' to sort the groups.
440 If PREDICTE is a function, list groups that the function returns non-nil;
441 if it is t, list groups that have no unread articles.
442 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
443 (set-buffer gnus-group-buffer)
444 (let ((buffer-read-only nil)
445 (lowest (or lowest 1))
447 (and gnus-group-listed-groups
448 (copy-sequence gnus-group-listed-groups))))
450 (gnus-update-format-specifications nil 'topic)
452 (when (or (not gnus-topic-alist)
453 (not gnus-topology-checked-p))
454 (gnus-topic-check-topology))
460 (when (or gnus-group-listed-groups
461 (and (>= level gnus-level-zombie)
462 (<= lowest gnus-level-zombie)))
463 (gnus-group-prepare-flat-list-dead
464 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
468 (when (or gnus-group-listed-groups
469 (and (>= level gnus-level-killed)
470 (<= lowest gnus-level-killed)))
471 (gnus-group-prepare-flat-list-dead
472 (setq gnus-killed-list (sort gnus-killed-list 'string<))
473 gnus-level-killed ?K regexp)
475 (unless gnus-killed-hashtb
476 (gnus-make-hashtable-from-killed))
477 (gnus-group-prepare-flat-list-dead
478 (gnus-remove-if (lambda (group)
479 (or (gnus-group-entry group)
480 (gnus-gethash group gnus-killed-hashtb)))
482 gnus-level-killed ?K regexp)))
486 (when (or (< lowest gnus-level-zombie)
487 gnus-group-listed-groups)
489 (let ((top (gnus-topic-find-topology list-topic)))
490 (gnus-topic-prepare-topic (cdr top) (car top)
491 (or topic-level level) predicate
493 (gnus-topic-prepare-topic gnus-topic-topology 0
494 (or topic-level level) predicate
496 (gnus-group-set-mode-line)
497 (setq gnus-group-list-mode (cons level predicate))
498 (gnus-run-hooks 'gnus-group-prepare-hook))))
500 (defun gnus-topic-prepare-topic (topicl level &optional list-level
503 "Insert TOPIC into the group buffer.
504 If SILENT, don't insert anything. Return the number of unread
505 articles in the topic and its subtopics."
506 (let* ((type (pop topicl))
507 (entries (gnus-topic-find-groups
509 (if gnus-group-listed-groups
512 (or predicate gnus-group-listed-groups
514 (gnus-topic-hierarchical-parameters
516 (if gnus-group-listed-groups 0 lowest)))
517 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
518 (gnus-group-indentation
519 (make-string (* gnus-topic-indent-level level) ? ))
520 (beg (progn (beginning-of-line) (point)))
521 (topicl (reverse topicl))
522 (all-entries entries)
523 (point-max (point-max))
526 info entry end active tick)
527 ;; Insert any sub-topics.
530 (gnus-topic-prepare-topic
531 (pop topicl) (1+ level) list-level predicate
532 (not visiblep) lowest regexp)))
535 ;; Insert all the groups that belong in this topic.
536 (while (setq entry (pop entries))
537 (when (if (stringp entry)
538 (gnus-group-prepare-logic
541 (or (not gnus-group-listed-groups)
542 (if (< list-level gnus-level-zombie) nil
544 (if (member entry gnus-zombie-list)
545 gnus-level-zombie gnus-level-killed)))
546 (and (<= entry-level list-level)
547 (>= entry-level lowest)))))
550 (string-match regexp entry))
552 (funcall regexp entry))
555 (setq info (nth 2 entry))
556 (gnus-group-prepare-logic
557 (gnus-info-group info)
558 (and (or (not gnus-group-listed-groups)
559 (let ((entry-level (gnus-info-level info)))
560 (and (<= entry-level list-level)
561 (>= entry-level lowest))))
562 (or (not (functionp predicate))
563 (funcall predicate info))
564 (or (not (stringp regexp))
565 (string-match regexp (gnus-info-group info))))))
569 (gnus-group-insert-group-line
570 entry (if (member entry gnus-zombie-list)
571 gnus-level-zombie gnus-level-killed)
572 nil (- (1+ (cdr (setq active (gnus-active entry))))
576 (when (setq info (nth 2 entry))
577 (gnus-group-insert-group-line
578 (gnus-info-group info)
579 (gnus-info-level info) (gnus-info-marks info)
580 (car entry) (gnus-info-method info)))))
581 (when (and (listp entry)
582 (numberp (car entry)))
583 (incf unread (car entry)))
587 ;; Insert the topic line.
588 (when (and (not silent)
589 (or gnus-topic-display-empty-topics ;We want empty topics
590 (not (zerop unread)) ;Non-empty
591 tick ;Ticked articles
592 (/= point-max (point-max)))) ;Unactivated groups
593 (gnus-extent-start-open (point))
594 (gnus-topic-insert-topic-line
596 (not (eq (nth 2 type) 'hidden))
597 level all-entries unread))
598 (gnus-topic-update-unreads (car type) unread)
599 (when gnus-group-update-tool-bar
600 (gnus-put-text-property beg end 'point-entered
601 'gnus-tool-bar-update)
602 (gnus-put-text-property beg end 'point-left
603 'gnus-tool-bar-update))
607 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
608 "Remove the current topic."
609 (let ((topic (gnus-group-topic-name))
610 (level (gnus-group-topic-level))
611 (beg (progn (beginning-of-line) (point)))
614 (while (and (zerop (forward-line 1))
615 (> (or (gnus-group-topic-level) (1+ level)) level)))
616 (delete-region beg (point))
617 ;; Do the change in this rather odd manner because it has been
618 ;; reported that some topics share parts of some lists, for some
619 ;; reason. I have been unable to determine why this is the
620 ;; case, but this hack seems to take care of things.
621 (let ((data (cadr (gnus-topic-find-topology topic))))
623 (list (if insert 'visible 'invisible)
627 (setq gnus-topic-alist
628 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
629 (gnus-topic-insert-topic topic in-level)))))
631 (defun gnus-topic-insert-topic (topic &optional level)
633 (gnus-group-prepare-topics
634 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
635 nil nil topic level))
637 (defun gnus-topic-fold (&optional insert topic)
638 "Remove/insert the current topic."
639 (let ((topic (or topic (gnus-group-topic-name))))
642 (if (not (gnus-group-active-topic-p))
643 (gnus-topic-remove-topic
644 (or insert (not (gnus-topic-visible-p))))
645 (let ((gnus-topic-topology gnus-topic-active-topology)
646 (gnus-topic-alist gnus-topic-active-alist)
647 (gnus-group-list-mode (cons 5 t)))
648 (gnus-topic-remove-topic
649 (or insert (not (gnus-topic-visible-p))) nil nil 9)
650 (gnus-topic-enter-dribble)))))))
652 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
654 (let* ((visible (if visiblep "" "..."))
655 (indentation (make-string (* gnus-topic-indent-level level) ? ))
656 (total-number-of-articles unread)
657 (number-of-groups (length entries))
658 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
660 (gnus-topic-update-unreads name unread)
664 (gnus-add-text-properties
667 (eval gnus-topic-line-format-spec))
668 (list 'gnus-topic (intern name)
669 'gnus-topic-level level
670 'gnus-topic-unread unread
671 'gnus-active active-topic
672 'gnus-topic-visible visiblep)))))
674 (defun gnus-topic-update-unreads (topic unreads)
675 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
677 (push (cons topic unreads) gnus-topic-unreads))
679 (defun gnus-topic-update-topics-containing-group (group)
680 "Update all topics that have GROUP as a member."
681 (when (and (eq major-mode 'gnus-group-mode)
684 (let ((alist gnus-topic-alist))
685 ;; This is probably not entirely correct. If a topic
686 ;; isn't shown, then it's not updated. But the updating
687 ;; should be performed in any case, since the topic's
688 ;; parent should be updated. Pfft.
690 (when (and (member group (cdar alist))
691 (gnus-topic-goto-topic (caar alist)))
692 (gnus-topic-update-topic-line (caar alist)))
695 (defun gnus-topic-update-topic ()
696 "Update all parent topics to the current group."
697 (when (and (eq major-mode 'gnus-group-mode)
699 (let ((group (gnus-group-group-name))
701 (buffer-read-only nil))
703 (gnus-get-info group)
704 (gnus-topic-goto-topic (gnus-current-topic)))
705 (gnus-topic-update-topic-line (gnus-group-topic-name))
708 (gnus-group-position-point)))))
710 (defun gnus-topic-goto-missing-group (group)
711 "Place point where GROUP is supposed to be inserted."
712 (let* ((topic (gnus-group-topic group))
713 (groups (cdr (assoc topic gnus-topic-alist)))
714 (g (cdr (member group groups)))
717 ;; Try to jump to a visible group.
719 (not (gnus-group-goto-group (car g) t)))
721 ;; It wasn't visible, so we try to see where to insert it.
723 (setq g (cdr (member group (reverse groups))))
724 (while (and g unfound)
725 (when (gnus-group-goto-group (pop g) t)
730 (not (gnus-topic-goto-missing-topic topic)))
731 (gnus-topic-display-missing-topic topic)))))
733 (defun gnus-topic-display-missing-topic (topic)
734 "Insert topic lines recursively for missing topics."
735 (let ((parent (gnus-topic-find-topology
736 (gnus-topic-parent-topic topic))))
738 (not (gnus-topic-goto-missing-topic (caadr parent))))
739 (gnus-topic-display-missing-topic (caadr parent))))
740 (gnus-topic-goto-missing-topic topic)
741 ;; Skip past all groups in the topic we're in.
742 (while (gnus-group-group-name)
744 (let* ((top (gnus-topic-find-topology topic))
745 (children (cddr top))
748 (entries (gnus-topic-find-groups
749 (car type) (car gnus-group-list-mode)
750 (cdr gnus-group-list-mode)))
753 (incf unread (gnus-topic-unread (caar (pop children)))))
754 (while (setq entry (pop entries))
755 (when (numberp (car entry))
756 (incf unread (car entry))))
757 (gnus-topic-insert-topic-line
758 topic t t (car (gnus-topic-find-topology topic)) nil unread)))
760 (defun gnus-topic-goto-missing-topic (topic)
761 (if (gnus-topic-goto-topic topic)
763 ;; Topic not displayed.
764 (let* ((top (gnus-topic-find-topology
765 (gnus-topic-parent-topic topic)))
766 (tp (reverse (cddr top))))
768 (gnus-topic-insert-topic-line
769 topic t t (car (gnus-topic-find-topology topic)) nil 0)
770 (while (not (equal (caaar tp) topic))
774 (not (gnus-topic-goto-topic (caaar tp))))
777 (gnus-topic-forward-topic 1)
778 (gnus-topic-goto-missing-topic (caadr top)))))
781 (defun gnus-topic-update-topic-line (topic-name &optional reads)
782 (let* ((top (gnus-topic-find-topology topic-name))
784 (children (cddr top))
785 (entries (gnus-topic-find-groups
786 (car type) (car gnus-group-list-mode)
787 (cdr gnus-group-list-mode)))
788 (parent (gnus-topic-parent-topic topic-name))
789 (all-entries entries)
791 old-unread entry new-unread)
792 (when (gnus-topic-goto-topic (car type))
793 ;; Tally all the groups that belong in this topic.
795 (setq unread (- (gnus-group-topic-unread) reads))
797 (incf unread (gnus-topic-unread (caar (pop children)))))
798 (while (setq entry (pop entries))
799 (when (numberp (car entry))
800 (incf unread (car entry)))))
801 (setq old-unread (gnus-group-topic-unread))
802 ;; Insert the topic line.
803 (gnus-topic-insert-topic-line
804 (car type) (gnus-topic-visible-p)
805 (not (eq (nth 2 type) 'hidden))
806 (gnus-group-topic-level) all-entries unread)
809 (setq new-unread (gnus-group-topic-unread)))
812 (gnus-topic-update-topic-line
814 (- (or old-unread 0) (or new-unread 0))))
817 (defun gnus-topic-group-indentation ()
819 (* gnus-topic-indent-level
822 (gnus-topic-goto-topic (gnus-current-topic))
823 (gnus-group-topic-level))
829 (gnus-add-shutdown 'gnus-topic-close 'gnus)
831 (defun gnus-topic-close ()
832 (setq gnus-topic-active-topology nil
833 gnus-topic-active-alist nil
834 gnus-topic-killed-topics nil
835 gnus-topology-checked-p nil))
837 (defun gnus-topic-check-topology ()
838 ;; The first time we set the topology to whatever we have
839 ;; gotten here, which can be rather random.
840 (unless gnus-topic-alist
841 (gnus-topic-init-alist))
843 (setq gnus-topology-checked-p t)
844 ;; Go through the topic alist and make sure that all topics
845 ;; are in the topic topology.
846 (let ((topics (gnus-topic-list))
847 (alist gnus-topic-alist)
850 (unless (member (caar alist) topics)
851 (nconc gnus-topic-topology
852 (list (list (list (caar alist) 'visible))))
854 (setq alist (cdr alist)))
856 (gnus-topic-enter-dribble))
857 ;; Conversely, go through the topology and make sure that all
858 ;; topologies have alists.
860 (unless (assoc (car topics) gnus-topic-alist)
861 (push (list (car topics)) gnus-topic-alist))
863 ;; Go through all living groups and make sure that
864 ;; they belong to some topic.
865 (let* ((tgroups (apply 'append (mapcar 'cdr gnus-topic-alist)))
866 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
867 (newsrc (cdr gnus-newsrc-alist))
870 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
871 (setcdr entry (list group))
872 (setq entry (cdr entry)))))
873 ;; Go through all topics and make sure they contain only living groups.
874 (let ((alist gnus-topic-alist)
876 (while (setq topic (pop alist))
878 (if (and (cadr topic)
879 (gnus-group-entry (cadr topic)))
880 (setq topic (cdr topic))
881 (setcdr topic (cddr topic)))))))
883 (defun gnus-topic-init-alist ()
884 "Initialize the topic structures."
885 (setq gnus-topic-topology
886 (cons (list "Gnus" 'visible)
887 (mapcar (lambda (topic)
888 (list (list (car topic) 'visible)))
890 (setq gnus-topic-alist
892 (mapcar (lambda (info) (gnus-info-group info))
893 (cdr gnus-newsrc-alist)))
895 (gnus-topic-enter-dribble))
899 (defun gnus-topic-clean-alist ()
900 "Remove bogus groups from the topic alist."
901 (let ((topic-alist gnus-topic-alist)
903 (unless gnus-killed-hashtb
904 (gnus-make-hashtable-from-killed))
905 (while (setq topic (pop topic-alist))
906 (let ((topic-name (pop topic))
907 group filtered-topic)
908 (while (setq group (pop topic))
909 (when (and (or (gnus-active group)
910 (gnus-info-method (gnus-get-info group)))
911 (not (gnus-gethash group gnus-killed-hashtb)))
912 (push group filtered-topic)))
913 (push (cons topic-name (nreverse filtered-topic)) result)))
914 (setq gnus-topic-alist (nreverse result))))
916 (defun gnus-topic-change-level (group level oldlevel &optional previous)
917 "Run when changing levels to enter/remove groups from topics."
919 (set-buffer gnus-group-buffer)
920 (let ((buffer-read-only nil))
921 (unless gnus-topic-inhibit-change-level
922 (gnus-group-goto-group (or (car (nth 2 previous)) group))
923 (when (and gnus-topic-mode
925 (not gnus-topic-inhibit-change-level))
926 ;; Remove the group from the topics.
927 (if (and (< oldlevel gnus-level-zombie)
928 (>= level gnus-level-zombie))
929 (let ((alist gnus-topic-alist))
930 (while (gnus-group-goto-group group)
933 (when (member group (car alist))
934 (setcdr (car alist) (delete group (cdar alist))))
936 ;; If the group is subscribed we enter it into the topics.
937 (when (and (< level gnus-level-zombie)
938 (>= oldlevel gnus-level-zombie))
939 (let* ((prev (gnus-group-group-name))
940 (gnus-topic-inhibit-change-level t)
941 (gnus-group-indentation
943 (* gnus-topic-indent-level
945 (gnus-topic-goto-topic (gnus-current-topic))
946 (gnus-group-topic-level))
949 (yanked (list group))
951 ;; Then we enter the yanked groups into the topics
953 (when (setq alist (assoc (save-excursion
957 (caar gnus-topic-topology)))
960 (when (stringp yanked)
961 (setq yanked (list yanked)))
964 (if (not (cdr alist))
965 (setcdr alist (nconc yanked (cdr alist)))
966 (while (and (not end) (cdr alist))
967 (when (equal (cadr alist) prev)
968 (setcdr alist (nconc yanked (cdr alist)))
970 (setq alist (cdr alist)))
972 (nconc talist yanked))))))
973 (gnus-topic-update-topic))))))))
975 (defun gnus-topic-goto-next-group (group props)
976 "Go to group or the next group after group."
978 (if (not (memq 'gnus-topic props))
979 (goto-char (point-max))
980 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
981 (if (gnus-group-goto-group group)
983 ;; The group is no longer visible.
984 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
985 (after (cdr (member group (cdr list)))))
986 ;; First try to put point on a group after the current one.
988 (not (gnus-group-goto-group (car after))))
989 (setq after (cdr after)))
990 ;; Then try to put point on a group before point.
992 (setq after (cdr (member group (reverse (cdr list)))))
994 (not (gnus-group-goto-group (car after))))
995 (setq after (cdr after))))
996 ;; Finally, just put point on the topic.
998 (goto-char (point-min))
1000 (gnus-topic-goto-topic (car list))
1004 ;;; Topic-active functions
1006 (defun gnus-topic-grok-active (&optional force)
1007 "Parse all active groups and create topic structures for them."
1008 ;; First we make sure that we have really read the active file.
1010 (not gnus-topic-active-alist))
1012 ;; Get a list of all groups available.
1013 (mapatoms (lambda (g) (when (symbol-value g)
1014 (push (symbol-name g) groups)))
1016 (setq groups (sort groups 'string<))
1017 ;; Init the variables.
1018 (setq gnus-topic-active-topology (list (list "" 'visible)))
1019 (setq gnus-topic-active-alist nil)
1020 ;; Descend the top-level hierarchy.
1021 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
1022 ;; Set the top-level topic names to something nice.
1023 (setcar (car gnus-topic-active-topology) "Gnus active")
1024 (setcar (car gnus-topic-active-alist) "Gnus active"))))
1026 (defun gnus-topic-grok-active-1 (topology groups)
1027 (let* ((name (caar topology))
1028 (prefix (concat "^" (regexp-quote name)))
1029 tgroups ntopology group)
1031 (string-match prefix (setq group (car groups))))
1032 (if (not (string-match "\\." group (match-end 0)))
1033 ;; There are no further hierarchies here, so we just
1034 ;; enter this group into the list belonging to this
1036 (push (pop groups) tgroups)
1037 ;; New sub-hierarchy, so we add it to the topology.
1038 (nconc topology (list (setq ntopology
1039 (list (list (substring
1040 group 0 (match-end 0))
1042 ;; Descend the hierarchy.
1043 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
1044 ;; We remove the trailing "." from the topic name.
1046 (if (string-match "\\.$" name)
1047 (substring name 0 (match-beginning 0))
1049 ;; Add this topic and its groups to the topic alist.
1050 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
1051 (setcar (car topology) name)
1052 ;; We return the rest of the groups that didn't belong
1056 ;;; Topic mode, commands and keymap.
1058 (defvar gnus-topic-mode-map nil)
1059 (defvar gnus-group-topic-map nil)
1061 (unless gnus-topic-mode-map
1062 (setq gnus-topic-mode-map (make-sparse-keymap))
1064 ;; Override certain group mode keys.
1065 (gnus-define-keys gnus-topic-mode-map
1066 "=" gnus-topic-select-group
1067 "\r" gnus-topic-select-group
1068 " " gnus-topic-read-group
1069 "\C-c\C-x" gnus-topic-expire-articles
1070 "c" gnus-topic-catchup-articles
1071 "\C-k" gnus-topic-kill-group
1072 "\C-y" gnus-topic-yank-group
1073 "\M-g" gnus-topic-get-new-news-this-topic
1074 "AT" gnus-topic-list-active
1075 "Gp" gnus-topic-edit-parameters
1076 "#" gnus-topic-mark-topic
1077 "\M-#" gnus-topic-unmark-topic
1078 [tab] gnus-topic-indent
1079 [(meta tab)] gnus-topic-unindent
1080 "\C-i" gnus-topic-indent
1081 "\M-\C-i" gnus-topic-unindent
1082 gnus-mouse-2 gnus-mouse-pick-topic)
1084 ;; Define a new submap.
1085 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
1086 "#" gnus-topic-mark-topic
1087 "\M-#" gnus-topic-unmark-topic
1088 "n" gnus-topic-create-topic
1089 "m" gnus-topic-move-group
1090 "D" gnus-topic-remove-group
1091 "c" gnus-topic-copy-group
1092 "h" gnus-topic-hide-topic
1093 "s" gnus-topic-show-topic
1094 "j" gnus-topic-jump-to-topic
1095 "M" gnus-topic-move-matching
1096 "C" gnus-topic-copy-matching
1097 "\M-p" gnus-topic-goto-previous-topic
1098 "\M-n" gnus-topic-goto-next-topic
1099 "\C-i" gnus-topic-indent
1100 [tab] gnus-topic-indent
1101 "r" gnus-topic-rename
1102 "\177" gnus-topic-delete
1103 [delete] gnus-topic-delete
1104 "H" gnus-topic-toggle-display-empty-topics)
1106 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1107 "s" gnus-topic-sort-groups
1108 "a" gnus-topic-sort-groups-by-alphabet
1109 "u" gnus-topic-sort-groups-by-unread
1110 "l" gnus-topic-sort-groups-by-level
1111 "e" gnus-topic-sort-groups-by-server
1112 "v" gnus-topic-sort-groups-by-score
1113 "r" gnus-topic-sort-groups-by-rank
1114 "m" gnus-topic-sort-groups-by-method))
1116 (defun gnus-topic-make-menu-bar ()
1117 (unless (boundp 'gnus-topic-menu)
1119 gnus-topic-menu gnus-topic-mode-map ""
1121 ["Toggle topics" gnus-topic-mode t]
1123 ["Copy..." gnus-topic-copy-group t]
1124 ["Move..." gnus-topic-move-group t]
1125 ["Remove" gnus-topic-remove-group t]
1126 ["Copy matching..." gnus-topic-copy-matching t]
1127 ["Move matching..." gnus-topic-move-matching t])
1129 ["Goto..." gnus-topic-jump-to-topic t]
1130 ["Show" gnus-topic-show-topic t]
1131 ["Hide" gnus-topic-hide-topic t]
1132 ["Delete" gnus-topic-delete t]
1133 ["Rename..." gnus-topic-rename t]
1134 ["Create..." gnus-topic-create-topic t]
1135 ["Mark" gnus-topic-mark-topic t]
1136 ["Indent" gnus-topic-indent t]
1137 ["Sort" gnus-topic-sort-topics t]
1138 ["Previous topic" gnus-topic-goto-previous-topic t]
1139 ["Next topic" gnus-topic-goto-next-topic t]
1140 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1141 ["Edit parameters" gnus-topic-edit-parameters t])
1142 ["List active" gnus-topic-list-active t]))))
1144 (defun gnus-topic-mode (&optional arg redisplay)
1145 "Minor mode for topicsifying Gnus group buffers."
1146 (interactive (list current-prefix-arg t))
1147 (when (eq major-mode 'gnus-group-mode)
1148 (make-local-variable 'gnus-topic-mode)
1149 (setq gnus-topic-mode
1150 (if (null arg) (not gnus-topic-mode)
1151 (> (prefix-numeric-value arg) 0)))
1152 ;; Infest Gnus with topics.
1153 (if (not gnus-topic-mode)
1154 (setq gnus-goto-missing-group-function nil)
1155 (when (gnus-visual-p 'topic-menu 'menu)
1156 (gnus-topic-make-menu-bar))
1157 (gnus-set-format 'topic t)
1158 (add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
1159 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1160 (set (make-local-variable 'gnus-group-prepare-function)
1161 'gnus-group-prepare-topics)
1162 (set (make-local-variable 'gnus-group-get-parameter-function)
1163 'gnus-group-topic-parameters)
1164 (set (make-local-variable 'gnus-group-goto-next-group-function)
1165 'gnus-topic-goto-next-group)
1166 (set (make-local-variable 'gnus-group-indentation-function)
1167 'gnus-topic-group-indentation)
1168 (set (make-local-variable 'gnus-group-update-group-function)
1169 'gnus-topic-update-topics-containing-group)
1170 (set (make-local-variable 'gnus-group-sort-alist-function)
1171 'gnus-group-sort-topic)
1172 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1173 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1174 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
1175 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1177 (setq gnus-topology-checked-p nil)
1178 ;; We check the topology.
1179 (when gnus-newsrc-alist
1180 (gnus-topic-check-topology))
1181 (gnus-run-hooks 'gnus-topic-mode-hook))
1182 ;; Remove topic infestation.
1183 (unless gnus-topic-mode
1184 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1185 (setq gnus-group-change-level-function nil)
1186 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1187 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1188 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1190 (gnus-group-list-groups))))
1192 (defun gnus-topic-select-group (&optional all)
1193 "Select this newsgroup.
1194 No article is selected automatically.
1195 If the group is opened, just switch the summary buffer.
1196 If ALL is non-nil, already read articles become readable.
1197 If ALL is a number, fetch this number of articles.
1199 If performed over a topic line, toggle folding the topic."
1201 (when (and (eobp) (not (gnus-group-group-name)))
1203 (if (gnus-group-topic-p)
1204 (let ((gnus-group-list-mode
1205 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1206 (gnus-topic-fold all)
1207 (gnus-dribble-touch))
1208 (gnus-group-select-group all)))
1210 (defun gnus-mouse-pick-topic (e)
1211 "Select the group or topic under the mouse pointer."
1214 (gnus-topic-read-group nil))
1216 (defun gnus-topic-expire-articles (topic)
1217 "Expire articles in this topic or group."
1218 (interactive (list (gnus-group-topic-name)))
1220 (call-interactively 'gnus-group-expire-articles)
1222 (gnus-message 5 "Expiring groups in %s..." topic)
1223 (let ((gnus-group-marked
1224 (mapcar (lambda (entry) (car (nth 2 entry)))
1225 (gnus-topic-find-groups topic gnus-level-killed t
1227 (gnus-group-expire-articles nil))
1228 (gnus-message 5 "Expiring groups in %s...done" topic))))
1230 (defun gnus-topic-catchup-articles (topic)
1231 "Catchup this topic or group.
1232 Also see `gnus-group-catchup'."
1233 (interactive (list (gnus-group-topic-name)))
1235 (call-interactively 'gnus-group-catchup-current)
1238 (mapcar (lambda (entry) (car (nth 2 entry)))
1239 (gnus-topic-find-groups topic gnus-level-killed t
1241 (buffer-read-only nil)
1242 (gnus-group-marked groups))
1243 (gnus-group-catchup-current)
1244 (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1246 (defun gnus-topic-read-group (&optional all no-article group)
1247 "Read news in this newsgroup.
1248 If the prefix argument ALL is non-nil, already read articles become
1249 readable. IF ALL is a number, fetch this number of articles. If the
1250 optional argument NO-ARTICLE is non-nil, no article will be
1251 auto-selected upon group entry. If GROUP is non-nil, fetch that
1254 If performed over a topic line, toggle folding the topic."
1256 (if (gnus-group-topic-p)
1257 (let ((gnus-group-list-mode
1258 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1259 (gnus-topic-fold all))
1260 (gnus-group-read-group all no-article group)))
1262 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1263 "Create a new TOPIC under PARENT.
1264 When used interactively, PARENT will be the topic under point."
1267 (read-string "New topic: ")
1268 (gnus-current-topic)))
1269 ;; Check whether this topic already exists.
1270 (when (gnus-topic-find-topology topic)
1271 (error "Topic already exists"))
1273 (setq parent (caar gnus-topic-topology)))
1274 (let ((top (cdr (gnus-topic-find-topology parent)))
1275 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1277 (error "No such parent topic: %s" parent))
1280 (while (and (cdr top)
1281 (not (equal (caaadr top) previous)))
1282 (setq top (cdr top)))
1283 (setcdr top (cons full-topic (cdr top))))
1284 (nconc top (list full-topic)))
1285 (unless (assoc topic gnus-topic-alist)
1286 (push (list topic) gnus-topic-alist)))
1287 (gnus-topic-enter-dribble)
1288 (gnus-group-list-groups)
1289 (gnus-topic-goto-topic topic))
1292 ;; 1. When the marked groups are overlapped with the process
1293 ;; region, the behavior of move or remove is not right.
1294 ;; 2. Can't process on several marked groups with a same name,
1295 ;; because gnus-group-marked only keeps one copy.
1297 (defun gnus-topic-move-group (n topic &optional copyp)
1298 "Move the next N groups to TOPIC.
1299 If COPYP, copy the groups instead."
1301 (list current-prefix-arg
1302 (gnus-completing-read "Move to topic" gnus-topic-alist nil t
1303 'gnus-topic-history)))
1304 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1305 gnus-group-marked t))
1306 (groups (gnus-group-process-prefix n))
1307 (topicl (assoc topic gnus-topic-alist))
1308 (start-topic (gnus-group-topic-name))
1309 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1311 (if (and (not groups) (not copyp) start-topic)
1312 (gnus-topic-move start-topic topic)
1315 (gnus-group-remove-mark g use-marked)
1317 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1319 (setcdr entry (gnus-delete-first g (cdr entry))))
1320 (nconc topicl (list g)))
1322 (gnus-topic-enter-dribble)
1324 (gnus-group-goto-group start-group)
1325 (gnus-topic-goto-topic start-topic))
1326 (gnus-group-list-groups))))
1328 (defun gnus-topic-remove-group (&optional n)
1329 "Remove the current group from the topic."
1331 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1332 gnus-group-marked t))
1333 (groups (gnus-group-process-prefix n)))
1336 (gnus-group-remove-mark group use-marked)
1337 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1338 (buffer-read-only nil))
1339 (when (and topicl group)
1341 (gnus-delete-first group topicl))
1342 (gnus-topic-update-topic)))
1344 (gnus-topic-enter-dribble)
1345 (gnus-group-position-point)))
1347 (defun gnus-topic-copy-group (n topic)
1348 "Copy the current group to a topic."
1350 (list current-prefix-arg
1351 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1352 (gnus-topic-move-group n topic t))
1354 (defun gnus-topic-kill-group (&optional n discard)
1355 "Kill the next N groups."
1357 (if (gnus-group-topic-p)
1358 (let ((topic (gnus-group-topic-name)))
1360 (gnus-topic-find-topology topic)
1361 (assoc topic gnus-topic-alist))
1362 gnus-topic-killed-topics)
1363 (gnus-topic-remove-topic nil t)
1364 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1365 (gnus-topic-enter-dribble))
1366 (gnus-group-kill-group n discard)
1367 (if (not (gnus-group-topic-p))
1368 (gnus-topic-update-topic)
1369 ;; Move up one line so that we update the right topic.
1371 (gnus-topic-update-topic)
1374 (defun gnus-topic-yank-group (&optional arg)
1375 "Yank the last topic."
1377 (if gnus-topic-killed-topics
1379 (or (gnus-group-topic-name)
1380 (gnus-topic-next-topic (gnus-current-topic))))
1381 (data (pop gnus-topic-killed-topics))
1384 (push alist gnus-topic-alist)
1385 (gnus-topic-create-topic
1386 (caar item) (gnus-topic-parent-topic previous) previous
1388 (gnus-topic-enter-dribble)
1389 (gnus-topic-goto-topic (caar item)))
1390 (let* ((prev (gnus-group-group-name))
1391 (gnus-topic-inhibit-change-level t)
1392 (gnus-group-indentation
1394 (* gnus-topic-indent-level
1396 (gnus-topic-goto-topic (gnus-current-topic))
1397 (gnus-group-topic-level))
1401 ;; We first yank the groups the normal way...
1402 (setq yanked (gnus-group-yank-group arg))
1403 ;; Then we enter the yanked groups into the topics they belong
1405 (setq alist (assoc (save-excursion
1407 (gnus-current-topic))
1409 (when (stringp yanked)
1410 (setq yanked (list yanked)))
1412 (nconc alist yanked)
1413 (if (not (cdr alist))
1414 (setcdr alist (nconc yanked (cdr alist)))
1416 (when (equal (cadr alist) prev)
1417 (setcdr alist (nconc yanked (cdr alist)))
1419 (setq alist (cdr alist))))))
1420 (gnus-topic-update-topic)))
1422 (defun gnus-topic-hide-topic (&optional permanent)
1423 "Hide the current topic.
1424 If PERMANENT, make it stay hidden in subsequent sessions as well."
1426 (when (gnus-current-topic)
1427 (gnus-topic-goto-topic (gnus-current-topic))
1431 (gnus-topic-find-topology (gnus-current-topic))))
1433 (gnus-topic-remove-topic nil nil)))
1435 (defun gnus-topic-show-topic (&optional permanent)
1436 "Show the hidden topic.
1437 If PERMANENT, make it stay shown in subsequent sessions as well."
1439 (when (gnus-group-topic-p)
1441 (gnus-topic-remove-topic t nil)
1443 (gnus-topic-find-topology
1444 (completing-read "Show topic: " gnus-topic-alist nil t))))
1445 (setcar (cddr (cadr topic)) nil)
1446 (setcar (cdr (cadr topic)) 'visible)
1447 (gnus-group-list-groups)))))
1449 (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
1450 "Mark all groups in the TOPIC with the process mark.
1451 If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
1452 (interactive (list (gnus-group-topic-name)
1454 (and current-prefix-arg t)))
1456 (call-interactively 'gnus-group-mark-group)
1458 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1459 (not non-recursive))))
1461 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1462 (gnus-info-group (nth 2 (pop groups)))))))))
1464 (defun gnus-topic-unmark-topic (topic &optional dummy non-recursive)
1465 "Remove the process mark from all groups in the TOPIC.
1466 If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
1467 (interactive (list (gnus-group-topic-name)
1469 (and current-prefix-arg t)))
1471 (call-interactively 'gnus-group-unmark-group)
1472 (gnus-topic-mark-topic topic t non-recursive)))
1474 (defun gnus-topic-get-new-news-this-topic (&optional n)
1475 "Check for new news in the current topic."
1477 (if (not (gnus-group-topic-p))
1478 (gnus-group-get-new-news-this-group n)
1479 (let* ((topic (gnus-group-topic-name))
1480 (data (cadr (gnus-topic-find-topology topic))))
1482 (gnus-topic-mark-topic topic nil (and n t))
1483 (gnus-group-get-new-news-this-group))
1484 (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1486 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1487 "Move all groups that match REGEXP to some topic."
1492 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1493 (read-string (format "Move to %s (regexp): " topic))))))
1494 (gnus-group-mark-regexp regexp)
1495 (gnus-topic-move-group nil topic copyp))
1497 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1498 "Copy all groups that match REGEXP to some topic."
1503 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1504 (read-string (format "Copy to %s (regexp): " topic))))))
1505 (gnus-topic-move-matching regexp topic t))
1507 (defun gnus-topic-delete (topic)
1509 (interactive (list (gnus-group-topic-name)))
1511 (error "No topic to be deleted"))
1512 (let ((entry (assoc topic gnus-topic-alist))
1513 (buffer-read-only nil))
1515 (error "Topic not empty"))
1516 ;; Delete if visible.
1517 (when (gnus-topic-goto-topic topic)
1519 ;; Remove from alist.
1520 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1521 ;; Remove from topology.
1522 (gnus-topic-find-topology topic nil nil 'delete)
1523 (gnus-dribble-touch)))
1525 (defun gnus-topic-rename (old-name new-name)
1528 (let ((topic (gnus-current-topic)))
1530 (read-string (format "Rename %s to: " topic) topic))))
1531 ;; Check whether the new name exists.
1532 (when (gnus-topic-find-topology new-name)
1533 (error "Topic '%s' already exists" new-name))
1534 ;; "nil" is an invalid name, for reasons I'd rather not go
1535 ;; into here. Trust me.
1536 (when (equal new-name "nil")
1537 (error "Invalid name: %s" nil))
1539 (let ((top (gnus-topic-find-topology old-name))
1540 (entry (assoc old-name gnus-topic-alist)))
1542 (setcar (cadr top) new-name))
1544 (setcar entry new-name))
1546 (gnus-dribble-touch)
1547 (gnus-group-list-groups)
1550 (defun gnus-topic-indent (&optional unindent)
1551 "Indent a topic -- make it a sub-topic of the previous topic.
1552 If UNINDENT, remove an indentation."
1555 (gnus-topic-unindent)
1556 (let* ((topic (gnus-current-topic))
1557 (parent (gnus-topic-previous-topic topic))
1558 (buffer-read-only nil))
1560 (error "Nothing to indent %s into" topic))
1562 (gnus-topic-goto-topic topic)
1563 (gnus-topic-kill-group)
1564 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1565 (gnus-topic-create-topic
1566 topic parent nil (cdar (car gnus-topic-killed-topics)))
1567 (pop gnus-topic-killed-topics)
1568 (or (gnus-topic-goto-topic topic)
1569 (gnus-topic-goto-topic parent))))))
1571 (defun gnus-topic-unindent ()
1574 (let* ((topic (gnus-current-topic))
1575 (parent (gnus-topic-parent-topic topic))
1576 (grandparent (gnus-topic-parent-topic parent)))
1578 (error "Nothing to indent %s into" topic))
1580 (gnus-topic-goto-topic topic)
1581 (gnus-topic-kill-group)
1582 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1583 (gnus-topic-create-topic
1584 topic grandparent (gnus-topic-next-topic parent)
1585 (cdar (car gnus-topic-killed-topics)))
1586 (pop gnus-topic-killed-topics)
1587 (gnus-topic-goto-topic topic))))
1589 (defun gnus-topic-list-active (&optional force)
1590 "List all groups that Gnus knows about in a topicsified fashion.
1591 If FORCE, always re-read the active file."
1594 (gnus-get-killed-groups))
1595 (gnus-topic-grok-active force)
1596 (let ((gnus-topic-topology gnus-topic-active-topology)
1597 (gnus-topic-alist gnus-topic-active-alist)
1598 gnus-killed-list gnus-zombie-list)
1599 (gnus-group-list-groups gnus-level-killed nil 1)))
1601 (defun gnus-topic-toggle-display-empty-topics ()
1602 "Show/hide topics that have no unread articles."
1604 (setq gnus-topic-display-empty-topics
1605 (not gnus-topic-display-empty-topics))
1606 (gnus-group-list-groups)
1607 (message "%s empty topics"
1608 (if gnus-topic-display-empty-topics
1609 "Showing" "Hiding")))
1611 ;;; Topic sorting functions
1613 (defun gnus-topic-edit-parameters (group)
1614 "Edit the group parameters of GROUP.
1615 If performed on a topic, edit the topic parameters instead."
1616 (interactive (list (gnus-group-group-name)))
1618 (gnus-group-edit-group-parameters group)
1619 (if (not (gnus-group-topic-p))
1620 (error "Nothing to edit on the current line")
1621 (let ((topic (gnus-group-topic-name)))
1623 (gnus-topic-parameters topic)
1624 (format "Editing the topic parameters for `%s'."
1627 (gnus-topic-set-parameters ,topic form)))))))
1629 (defun gnus-group-sort-topic (func reverse)
1630 "Sort groups in the topics according to FUNC and REVERSE."
1631 (let ((alist gnus-topic-alist))
1633 ;; !!!Sometimes nil elements sneak into the alist,
1634 ;; for some reason or other.
1635 (setcar alist (delq nil (car alist)))
1636 (setcar alist (delete "dummy.group" (car alist)))
1637 (gnus-topic-sort-topic (pop alist) func reverse))))
1639 (defun gnus-topic-sort-topic (topic func reverse)
1640 ;; Each topic only lists the name of the group, while
1641 ;; the sort predicates expect group infos as inputs.
1642 ;; So we first transform the group names into infos,
1643 ;; then sort, and then transform back into group names.
1647 (lambda (info) (gnus-info-group info))
1650 (lambda (group) (gnus-get-info group))
1653 ;; Do the reversal, if necessary.
1655 (setcdr topic (nreverse (cdr topic)))))
1657 (defun gnus-topic-sort-groups (func &optional reverse)
1658 "Sort the current topic according to FUNC.
1659 If REVERSE, reverse the sorting order."
1660 (interactive (list gnus-group-sort-function current-prefix-arg))
1661 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1662 (gnus-topic-sort-topic
1663 topic (gnus-make-sort-function func) reverse)
1664 (gnus-group-list-groups)))
1666 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1667 "Sort the current topic alphabetically by group name.
1668 If REVERSE, sort in reverse order."
1670 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1672 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1673 "Sort the current topic by number of unread articles.
1674 If REVERSE, sort in reverse order."
1676 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1678 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1679 "Sort the current topic by group level.
1680 If REVERSE, sort in reverse order."
1682 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1684 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1685 "Sort the current topic by group score.
1686 If REVERSE, sort in reverse order."
1688 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1690 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1691 "Sort the current topic by group rank.
1692 If REVERSE, sort in reverse order."
1694 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1696 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1697 "Sort the current topic alphabetically by backend name.
1698 If REVERSE, sort in reverse order."
1700 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1702 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1703 "Sort the current topic alphabetically by server name.
1704 If REVERSE, sort in reverse order."
1706 (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1708 (defun gnus-topic-sort-topics-1 (top reverse)
1711 (mapcar (gnus-byte-compile
1713 (gnus-topic-sort-topics-1 top ,reverse)))
1716 (string-lessp (caar t1) (caar t2)))))))
1717 (setcdr top (if reverse (reverse subtop) subtop))))
1720 (defun gnus-topic-sort-topics (&optional topic reverse)
1721 "Sort topics in TOPIC alphabetically by topic name.
1722 If REVERSE, reverse the sorting order."
1724 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1725 (gnus-current-topic))
1726 current-prefix-arg))
1727 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1728 gnus-topic-topology)))
1729 (gnus-topic-sort-topics-1 topic-topology reverse)
1730 (gnus-topic-enter-dribble)
1731 (gnus-group-list-groups)
1732 (gnus-topic-goto-topic topic)))
1734 (defun gnus-topic-move (current to)
1735 "Move the CURRENT topic to TO."
1738 (gnus-group-topic-name)
1739 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1740 (unless (and current to)
1741 (error "Can't find topic"))
1742 (let ((current-top (cdr (gnus-topic-find-topology current)))
1743 (to-top (cdr (gnus-topic-find-topology to))))
1745 (error "Can't find topic `%s'" current))
1747 (error "Can't find topic `%s'" to))
1748 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1749 (error "Can't move `%s' to its sub-level" current))
1750 (gnus-topic-find-topology current nil nil 'delete)
1751 (setcdr (last to-top) (list current-top))
1752 (gnus-topic-enter-dribble)
1753 (gnus-group-list-groups)
1754 (gnus-topic-goto-topic current)))
1756 (defun gnus-subscribe-topics (newsgroup)
1758 (let (match gnus-group-change-level-function)
1759 (dolist (topic (gnus-topic-list))
1760 (when (and (setq match (cdr (assq 'subscribe
1761 (gnus-topic-parameters topic))))
1762 (string-match match newsgroup))
1763 ;; Just subscribe the group.
1764 (gnus-subscribe-alphabetically newsgroup)
1765 ;; Add the group to the topic.
1766 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1767 ;; if this topic specifies a default level, use it
1768 (let ((subscribe-level (cdr (assq 'subscribe-level
1769 (gnus-topic-parameters topic)))))
1770 (when subscribe-level
1771 (gnus-group-change-level newsgroup subscribe-level
1772 gnus-level-default-subscribed)))
1776 (provide 'gnus-topic)
1778 ;;; arch-tag: bf176856-f30c-40f0-ae77-e41529a1134c
1779 ;;; gnus-topic.el ends here