1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Ilja Weis <kult@uni-paderborn.de>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
37 (defgroup gnus-topic nil
41 (defvar gnus-topic-mode nil
42 "Minor mode for Gnus group buffers.")
44 (defcustom gnus-topic-mode-hook nil
45 "Hook run in topic mode buffers."
49 (when (featurep 'xemacs)
50 (add-hook 'gnus-topic-mode-hook 'gnus-xmas-topic-menu-add))
52 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
53 "Format of topic lines.
54 It works along the same lines as a normal formatting string,
55 with some simple extensions.
57 %i Indentation based on topic level.
59 %v Nothing if the topic is visible, \"...\" otherwise.
60 %g Number of groups in the topic.
61 %a Number of unread articles in the groups in the topic.
62 %A Number of unread articles in the groups in the topic and its subtopics.
64 General format specifiers can also be used.
65 See Info node `(gnus)Formatting Variables'."
66 :link '(custom-manual "(gnus)Formatting Variables")
70 (defcustom gnus-topic-indent-level 2
71 "*How much each subtopic should be indented."
75 (defcustom gnus-topic-display-empty-topics t
76 "*If non-nil, display the topic lines even of topics that have no unread articles."
80 ;; Internal variables.
82 (defvar gnus-topic-active-topology nil)
83 (defvar gnus-topic-active-alist nil)
84 (defvar gnus-topic-unreads nil)
86 (defvar gnus-topology-checked-p nil
87 "Whether the topology has been checked in this session.")
89 (defvar gnus-topic-killed-topics nil)
90 (defvar gnus-topic-inhibit-change-level nil)
92 (defconst gnus-topic-line-format-alist
96 (?g number-of-groups ?d)
97 (?a (gnus-topic-articles-in-topic entries) ?d)
98 (?A total-number-of-articles ?d)
101 (defvar gnus-topic-line-format-spec nil)
103 ;;; Utility functions
105 (defun gnus-group-topic-name ()
106 "The name of the topic on the current line."
107 (let ((topic (get-text-property (point-at-bol) 'gnus-topic)))
108 (and topic (symbol-name topic))))
110 (defun gnus-group-topic-level ()
111 "The level of the topic on the current line."
112 (get-text-property (point-at-bol) 'gnus-topic-level))
114 (defun gnus-group-topic-unread ()
115 "The number of unread articles in topic on the current line."
116 (get-text-property (point-at-bol) 'gnus-topic-unread))
118 (defun gnus-topic-unread (topic)
119 "Return the number of unread articles in TOPIC."
120 (or (cdr (assoc topic gnus-topic-unreads))
123 (defun gnus-group-topic-p ()
124 "Return non-nil if the current line is a topic."
125 (gnus-group-topic-name))
127 (defun gnus-topic-visible-p ()
128 "Return non-nil if the current topic is visible."
129 (get-text-property (point-at-bol) 'gnus-topic-visible))
131 (defun gnus-topic-articles-in-topic (entries)
135 (when (numberp (setq number (car (pop entries))))
136 (incf total number)))
139 (defun gnus-group-topic (group)
140 "Return the topic GROUP is a member of."
141 (let ((alist gnus-topic-alist)
144 (when (member group (cdar alist))
145 (setq out (caar alist)
147 (setq alist (cdr alist)))
150 (defun gnus-group-parent-topic (group)
151 "Return the topic GROUP is member of by looking at the group buffer."
153 (set-buffer gnus-group-buffer)
154 (if (gnus-group-goto-group group)
156 (gnus-group-topic group))))
158 (defun gnus-topic-goto-topic (topic)
160 (gnus-goto-char (text-property-any (point-min) (point-max)
161 'gnus-topic (intern topic)))))
163 (defun gnus-topic-jump-to-topic (topic)
166 (list (completing-read "Go to topic: "
167 (mapcar 'list (gnus-topic-list))
169 (let ((buffer-read-only nil))
170 (dolist (topic (gnus-current-topics topic))
171 (unless (gnus-topic-goto-topic topic)
172 (gnus-topic-goto-missing-topic topic)
173 (gnus-topic-display-missing-topic topic))))
174 (gnus-topic-goto-topic topic))
176 (defun gnus-current-topic ()
177 "Return the name of the current topic."
179 (or (get-text-property (point) 'gnus-topic)
181 (and (gnus-goto-char (previous-single-property-change
182 (point) 'gnus-topic))
183 (get-text-property (max (1- (point)) (point-min))
186 (symbol-name result))))
188 (defun gnus-current-topics (&optional topic)
189 "Return a list of all current topics, lowest in hierarchy first.
190 If TOPIC, start with that topic."
191 (let ((topic (or topic (gnus-current-topic)))
195 (setq topic (gnus-topic-parent-topic topic)))
198 (defun gnus-group-active-topic-p ()
199 "Say whether the current topic comes from the active topics."
200 (get-text-property (point-at-bol) 'gnus-active))
202 (defun gnus-topic-find-groups (topic &optional level all lowest recursive)
203 "Return entries for all visible groups in TOPIC.
204 If RECURSIVE is t, return groups in its subtopics too."
205 (let ((groups (cdr (assoc topic gnus-topic-alist)))
206 info clevel unread group params visible-groups entry active)
207 (setq lowest (or lowest 1))
208 (setq level (or level gnus-level-unsubscribed))
209 ;; We go through the newsrc to look for matches.
211 (when (setq group (pop groups))
212 (setq entry (gnus-group-entry group)
214 params (gnus-info-params info)
215 active (gnus-active group)
216 unread (or (car entry)
217 (and (not (equal group "dummy.group"))
219 (- (1+ (cdr active)) (car active))))
220 clevel (or (gnus-info-level info)
221 (if (member group gnus-zombie-list)
222 gnus-level-zombie gnus-level-killed))))
224 info ; nil means that the group is dead.
226 (>= clevel lowest) ; Is inside the level we want.
228 (if (or (eq unread t)
230 gnus-group-list-inactive-groups
232 (and gnus-list-groups-with-ticked-articles
233 (cdr (assq 'tick (gnus-info-marks info))))
234 ;; Has right readedness.
235 ;; Check for permanent visibility.
236 (and gnus-permanently-visible-groups
237 (string-match gnus-permanently-visible-groups group))
238 (memq 'visible params)
239 (cdr (assq 'visible params)))
240 ;; Add this group to the list of visible groups.
241 (push (or entry group) visible-groups)))
242 (setq visible-groups (nreverse visible-groups))
245 (setq recursive (cdr (gnus-topic-find-topology topic))))
246 (mapcar (lambda (topic-topology)
248 (nconc visible-groups
249 (gnus-topic-find-groups
250 (caar topic-topology)
251 level all lowest topic-topology))))
255 (defun gnus-topic-goto-previous-topic (n)
256 "Go to the N'th previous topic."
258 (gnus-topic-goto-next-topic (- n)))
260 (defun gnus-topic-goto-next-topic (n)
261 "Go to the N'th next topic."
263 (let ((backward (< n 0))
265 (topic (gnus-current-topic)))
269 (gnus-topic-previous-topic topic)
270 (gnus-topic-next-topic topic))))
271 (gnus-topic-goto-topic topic)
274 (gnus-message 7 "No more topics"))
277 (defun gnus-topic-previous-topic (topic)
278 "Return the previous topic on the same level as TOPIC."
279 (let ((top (cddr (gnus-topic-find-topology
280 (gnus-topic-parent-topic topic)))))
281 (unless (equal topic (caaar top))
282 (while (and top (not (equal (caaadr top) topic)))
283 (setq top (cdr top)))
286 (defun gnus-topic-parent-topic (topic &optional topology)
287 "Return the parent of TOPIC."
289 (setq topology gnus-topic-topology))
290 (let ((parent (car (pop topology)))
293 (not (setq found (equal (caaar topology) topic)))
294 (not (setq result (gnus-topic-parent-topic
295 topic (car topology)))))
296 (setq topology (cdr topology)))
297 (or result (and found parent))))
299 (defun gnus-topic-next-topic (topic &optional previous)
300 "Return the next sibling of TOPIC."
301 (let ((parentt (cddr (gnus-topic-find-topology
302 (gnus-topic-parent-topic topic))))
305 (not (equal (caaar parentt) topic)))
306 (setq prev (caaar parentt)
307 parentt (cdr parentt)))
312 (defun gnus-topic-forward-topic (num)
313 "Go to the next topic on the same level as the current one."
314 (let* ((topic (gnus-current-topic))
315 (way (if (< num 0) 'gnus-topic-previous-topic
316 'gnus-topic-next-topic))
318 (while (and (not (zerop num))
319 (setq topic (funcall way topic)))
320 (when (gnus-topic-goto-topic topic)
323 (goto-char (point-max)))
326 (defun gnus-topic-find-topology (topic &optional topology level remove)
327 "Return the topology of TOPIC."
329 (setq topology gnus-topic-topology)
333 (if (equal (caar topology) topic)
336 (delq topology remove))
337 (cons level topology))
338 (setq topology (cdr topology))
340 (not (setq result (gnus-topic-find-topology
341 topic (car topology) (1+ level)
343 (setq topology (cdr topology)))
346 (defvar gnus-tmp-topics nil)
347 (defun gnus-topic-list (&optional topology)
348 "Return a list of all topics in the topology."
350 (setq topology gnus-topic-topology
351 gnus-tmp-topics nil))
352 (push (caar topology) gnus-tmp-topics)
353 (mapcar 'gnus-topic-list (cdr topology))
356 ;;; Topic parameter jazz
358 (defun gnus-topic-parameters (topic)
359 "Return the parameters for TOPIC."
360 (let ((top (gnus-topic-find-topology topic)))
362 (nth 3 (cadr top)))))
364 (defun gnus-topic-set-parameters (topic parameters)
365 "Set the topic parameters of TOPIC to PARAMETERS."
366 (let ((top (gnus-topic-find-topology topic)))
368 (error "No such topic: %s" topic))
369 ;; We may have to extend if there is no parameters here
371 (unless (nthcdr 2 (cadr top))
372 (nconc (cadr top) (list nil)))
373 (unless (nthcdr 3 (cadr top))
374 (nconc (cadr top) (list nil)))
375 (setcar (nthcdr 3 (cadr top)) parameters)
377 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
379 (defun gnus-group-topic-parameters (group)
380 "Compute the group parameters for GROUP taking into account inheritance from topics."
381 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
384 (gnus-topic-hierarchical-parameters
385 ;; First we try to go to the group within the group
386 ;; buffer and find the topic for the group that way.
387 ;; This hopefully copes well with groups that are in
388 ;; more than one topic. Failing that (i.e. when the
389 ;; group isn't visible in the group buffer) we find a
390 ;; topic for the group via gnus-group-topic.
391 (or (and (gnus-group-goto-group group)
392 (gnus-current-topic))
393 (gnus-group-topic group)))))))
395 (defun gnus-topic-hierarchical-parameters (topic)
396 "Return a topic list computed for TOPIC."
397 (let ((topics (gnus-current-topics topic))
398 params-list param out params)
400 (push (gnus-topic-parameters (pop topics)) params-list))
401 ;; We probably have lots of nil elements here, so
402 ;; we remove them. Probably faster than doing this "properly".
403 (setq params-list (delq nil params-list))
404 ;; Now we have all the parameters, so we go through them
405 ;; and do inheritance in the obvious way.
406 (while (setq params (pop params-list))
407 (while (setq param (pop params))
409 (setq param (cons param t)))
410 ;; Override any old versions of this param.
411 (gnus-pull (car param) out)
413 ;; Return the resulting parameter list.
416 ;;; General utility functions
418 (defun gnus-topic-enter-dribble ()
420 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
422 ;;; Generating group buffers
424 (defun gnus-group-prepare-topics (level &optional predicate lowest
425 regexp list-topic topic-level)
426 "List all newsgroups with unread articles of level LEVEL or lower.
427 Use the `gnus-group-topics' to sort the groups.
428 If PREDICTE is a function, list groups that the function returns non-nil;
429 if it is t, list groups that have no unread articles.
430 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
431 (set-buffer gnus-group-buffer)
432 (let ((buffer-read-only nil)
433 (lowest (or lowest 1))
435 (and gnus-group-listed-groups
436 (copy-sequence gnus-group-listed-groups))))
438 (gnus-update-format-specifications nil 'topic)
440 (when (or (not gnus-topic-alist)
441 (not gnus-topology-checked-p))
442 (gnus-topic-check-topology))
448 (when (or gnus-group-listed-groups
449 (and (>= level gnus-level-zombie)
450 (<= lowest gnus-level-zombie)))
451 (gnus-group-prepare-flat-list-dead
452 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
456 (when (or gnus-group-listed-groups
457 (and (>= level gnus-level-killed)
458 (<= lowest gnus-level-killed)))
459 (gnus-group-prepare-flat-list-dead
460 (setq gnus-killed-list (sort gnus-killed-list 'string<))
461 gnus-level-killed ?K regexp)
463 (unless gnus-killed-hashtb
464 (gnus-make-hashtable-from-killed))
465 (gnus-group-prepare-flat-list-dead
466 (gnus-remove-if (lambda (group)
467 (or (gnus-group-entry group)
468 (gnus-gethash group gnus-killed-hashtb)))
470 gnus-level-killed ?K regexp)))
474 (when (or (< lowest gnus-level-zombie)
475 gnus-group-listed-groups)
477 (let ((top (gnus-topic-find-topology list-topic)))
478 (gnus-topic-prepare-topic (cdr top) (car top)
479 (or topic-level level) predicate
481 (gnus-topic-prepare-topic gnus-topic-topology 0
482 (or topic-level level) predicate
484 (gnus-group-set-mode-line)
485 (setq gnus-group-list-mode (cons level predicate))
486 (gnus-run-hooks 'gnus-group-prepare-hook))))
488 (defun gnus-topic-prepare-topic (topicl level &optional list-level
491 "Insert TOPIC into the group buffer.
492 If SILENT, don't insert anything. Return the number of unread
493 articles in the topic and its subtopics."
494 (let* ((type (pop topicl))
495 (entries (gnus-topic-find-groups
497 (if gnus-group-listed-groups
500 (or predicate gnus-group-listed-groups
502 (gnus-topic-hierarchical-parameters
504 (if gnus-group-listed-groups 0 lowest)))
505 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
506 (gnus-group-indentation
507 (make-string (* gnus-topic-indent-level level) ? ))
508 (beg (progn (beginning-of-line) (point)))
509 (topicl (reverse topicl))
510 (all-entries entries)
511 (point-max (point-max))
514 info entry end active tick)
515 ;; Insert any sub-topics.
518 (gnus-topic-prepare-topic
519 (pop topicl) (1+ level) list-level predicate
520 (not visiblep) lowest regexp)))
523 ;; Insert all the groups that belong in this topic.
524 (while (setq entry (pop entries))
525 (when (if (stringp entry)
526 (gnus-group-prepare-logic
529 (or (not gnus-group-listed-groups)
530 (if (< list-level gnus-level-zombie) nil
532 (if (member entry gnus-zombie-list)
533 gnus-level-zombie gnus-level-killed)))
534 (and (<= entry-level list-level)
535 (>= entry-level lowest)))))
538 (string-match regexp entry))
540 (funcall regexp entry))
543 (setq info (nth 2 entry))
544 (gnus-group-prepare-logic
545 (gnus-info-group info)
546 (and (or (not gnus-group-listed-groups)
547 (let ((entry-level (gnus-info-level info)))
548 (and (<= entry-level list-level)
549 (>= entry-level lowest))))
550 (or (not (functionp predicate))
551 (funcall predicate info))
552 (or (not (stringp regexp))
553 (string-match regexp (gnus-info-group info))))))
557 (gnus-group-insert-group-line
558 entry (if (member entry gnus-zombie-list)
559 gnus-level-zombie gnus-level-killed)
560 nil (- (1+ (cdr (setq active (gnus-active entry))))
564 (when (setq info (nth 2 entry))
565 (gnus-group-insert-group-line
566 (gnus-info-group info)
567 (gnus-info-level info) (gnus-info-marks info)
568 (car entry) (gnus-info-method info)))))
569 (when (and (listp entry)
570 (numberp (car entry)))
571 (incf unread (car entry)))
575 ;; Insert the topic line.
576 (when (and (not silent)
577 (or gnus-topic-display-empty-topics ;We want empty topics
578 (not (zerop unread)) ;Non-empty
579 tick ;Ticked articles
580 (/= point-max (point-max)))) ;Unactivated groups
581 (gnus-extent-start-open (point))
582 (gnus-topic-insert-topic-line
584 (not (eq (nth 2 type) 'hidden))
585 level all-entries unread))
586 (gnus-topic-update-unreads (car type) unread)
590 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
591 "Remove the current topic."
592 (let ((topic (gnus-group-topic-name))
593 (level (gnus-group-topic-level))
594 (beg (progn (beginning-of-line) (point)))
597 (while (and (zerop (forward-line 1))
598 (> (or (gnus-group-topic-level) (1+ level)) level)))
599 (delete-region beg (point))
600 ;; Do the change in this rather odd manner because it has been
601 ;; reported that some topics share parts of some lists, for some
602 ;; reason. I have been unable to determine why this is the
603 ;; case, but this hack seems to take care of things.
604 (let ((data (cadr (gnus-topic-find-topology topic))))
606 (list (if insert 'visible 'invisible)
610 (setq gnus-topic-alist
611 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
612 (gnus-topic-insert-topic topic in-level)))))
614 (defun gnus-topic-insert-topic (topic &optional level)
616 (gnus-group-prepare-topics
617 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
618 nil nil topic level))
620 (defun gnus-topic-fold (&optional insert topic)
621 "Remove/insert the current topic."
622 (let ((topic (or topic (gnus-group-topic-name))))
625 (if (not (gnus-group-active-topic-p))
626 (gnus-topic-remove-topic
627 (or insert (not (gnus-topic-visible-p))))
628 (let ((gnus-topic-topology gnus-topic-active-topology)
629 (gnus-topic-alist gnus-topic-active-alist)
630 (gnus-group-list-mode (cons 5 t)))
631 (gnus-topic-remove-topic
632 (or insert (not (gnus-topic-visible-p))) nil nil 9)
633 (gnus-topic-enter-dribble)))))))
635 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
637 (let* ((visible (if visiblep "" "..."))
638 (indentation (make-string (* gnus-topic-indent-level level) ? ))
639 (total-number-of-articles unread)
640 (number-of-groups (length entries))
641 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
643 (gnus-topic-update-unreads name unread)
647 (gnus-add-text-properties
650 (eval gnus-topic-line-format-spec))
651 (list 'gnus-topic (intern name)
652 'gnus-topic-level level
653 'gnus-topic-unread unread
654 'gnus-active active-topic
655 'gnus-topic-visible visiblep)))))
657 (defun gnus-topic-update-unreads (topic unreads)
658 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
660 (push (cons topic unreads) gnus-topic-unreads))
662 (defun gnus-topic-update-topics-containing-group (group)
663 "Update all topics that have GROUP as a member."
664 (when (and (eq major-mode 'gnus-group-mode)
667 (let ((alist gnus-topic-alist))
668 ;; This is probably not entirely correct. If a topic
669 ;; isn't shown, then it's not updated. But the updating
670 ;; should be performed in any case, since the topic's
671 ;; parent should be updated. Pfft.
673 (when (and (member group (cdar alist))
674 (gnus-topic-goto-topic (caar alist)))
675 (gnus-topic-update-topic-line (caar alist)))
678 (defun gnus-topic-update-topic ()
679 "Update all parent topics to the current group."
680 (when (and (eq major-mode 'gnus-group-mode)
682 (let ((group (gnus-group-group-name))
684 (buffer-read-only nil))
686 (gnus-get-info group)
687 (gnus-topic-goto-topic (gnus-current-topic)))
688 (gnus-topic-update-topic-line (gnus-group-topic-name))
691 (gnus-group-position-point)))))
693 (defun gnus-topic-goto-missing-group (group)
694 "Place point where GROUP is supposed to be inserted."
695 (let* ((topic (gnus-group-topic group))
696 (groups (cdr (assoc topic gnus-topic-alist)))
697 (g (cdr (member group groups)))
700 ;; Try to jump to a visible group.
702 (not (gnus-group-goto-group (car g) t)))
704 ;; It wasn't visible, so we try to see where to insert it.
706 (setq g (cdr (member group (reverse groups))))
707 (while (and g unfound)
708 (when (gnus-group-goto-group (pop g) t)
713 (not (gnus-topic-goto-missing-topic topic)))
714 (gnus-topic-display-missing-topic topic)))))
716 (defun gnus-topic-display-missing-topic (topic)
717 "Insert topic lines recursively for missing topics."
718 (let ((parent (gnus-topic-find-topology
719 (gnus-topic-parent-topic topic))))
721 (not (gnus-topic-goto-missing-topic (caadr parent))))
722 (gnus-topic-display-missing-topic (caadr parent))))
723 (gnus-topic-goto-missing-topic topic)
724 (let* ((top (gnus-topic-find-topology topic))
725 (children (cddr top))
728 (entries (gnus-topic-find-groups
729 (car type) (car gnus-group-list-mode)
730 (cdr gnus-group-list-mode)))
733 (incf unread (gnus-topic-unread (caar (pop children)))))
734 (while (setq entry (pop entries))
735 (when (numberp (car entry))
736 (incf unread (car entry))))
737 (gnus-topic-insert-topic-line
738 topic t t (car (gnus-topic-find-topology topic)) nil unread)))
740 (defun gnus-topic-goto-missing-topic (topic)
741 (if (gnus-topic-goto-topic topic)
743 ;; Topic not displayed.
744 (let* ((top (gnus-topic-find-topology
745 (gnus-topic-parent-topic topic)))
746 (tp (reverse (cddr top))))
748 (gnus-topic-insert-topic-line
749 topic t t (car (gnus-topic-find-topology topic)) nil 0)
750 (while (not (equal (caaar tp) topic))
754 (not (gnus-topic-goto-topic (caaar tp))))
757 (gnus-topic-forward-topic 1)
758 (gnus-topic-goto-missing-topic (caadr top)))))
761 (defun gnus-topic-update-topic-line (topic-name &optional reads)
762 (let* ((top (gnus-topic-find-topology topic-name))
764 (children (cddr top))
765 (entries (gnus-topic-find-groups
766 (car type) (car gnus-group-list-mode)
767 (cdr gnus-group-list-mode)))
768 (parent (gnus-topic-parent-topic topic-name))
769 (all-entries entries)
771 old-unread entry new-unread)
772 (when (gnus-topic-goto-topic (car type))
773 ;; Tally all the groups that belong in this topic.
775 (setq unread (- (gnus-group-topic-unread) reads))
777 (incf unread (gnus-topic-unread (caar (pop children)))))
778 (while (setq entry (pop entries))
779 (when (numberp (car entry))
780 (incf unread (car entry)))))
781 (setq old-unread (gnus-group-topic-unread))
782 ;; Insert the topic line.
783 (gnus-topic-insert-topic-line
784 (car type) (gnus-topic-visible-p)
785 (not (eq (nth 2 type) 'hidden))
786 (gnus-group-topic-level) all-entries unread)
789 (setq new-unread (gnus-group-topic-unread)))
792 (gnus-topic-update-topic-line
794 (- (or old-unread 0) (or new-unread 0))))
797 (defun gnus-topic-group-indentation ()
799 (* gnus-topic-indent-level
802 (gnus-topic-goto-topic (gnus-current-topic))
803 (gnus-group-topic-level))
809 (gnus-add-shutdown 'gnus-topic-close 'gnus)
811 (defun gnus-topic-close ()
812 (setq gnus-topic-active-topology nil
813 gnus-topic-active-alist nil
814 gnus-topic-killed-topics nil
815 gnus-topology-checked-p nil))
817 (defun gnus-topic-check-topology ()
818 ;; The first time we set the topology to whatever we have
819 ;; gotten here, which can be rather random.
820 (unless gnus-topic-alist
821 (gnus-topic-init-alist))
823 (setq gnus-topology-checked-p t)
824 ;; Go through the topic alist and make sure that all topics
825 ;; are in the topic topology.
826 (let ((topics (gnus-topic-list))
827 (alist gnus-topic-alist)
830 (unless (member (caar alist) topics)
831 (nconc gnus-topic-topology
832 (list (list (list (caar alist) 'visible))))
834 (setq alist (cdr alist)))
836 (gnus-topic-enter-dribble))
837 ;; Conversely, go through the topology and make sure that all
838 ;; topologies have alists.
840 (unless (assoc (car topics) gnus-topic-alist)
841 (push (list (car topics)) gnus-topic-alist))
843 ;; Go through all living groups and make sure that
844 ;; they belong to some topic.
845 (let* ((tgroups (apply 'append (mapcar 'cdr gnus-topic-alist)))
846 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
847 (newsrc (cdr gnus-newsrc-alist))
850 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
851 (setcdr entry (list group))
852 (setq entry (cdr entry)))))
853 ;; Go through all topics and make sure they contain only living groups.
854 (let ((alist gnus-topic-alist)
856 (while (setq topic (pop alist))
858 (if (and (cadr topic)
859 (gnus-group-entry (cadr topic)))
860 (setq topic (cdr topic))
861 (setcdr topic (cddr topic)))))))
863 (defun gnus-topic-init-alist ()
864 "Initialize the topic structures."
865 (setq gnus-topic-topology
866 (cons (list "Gnus" 'visible)
867 (mapcar (lambda (topic)
868 (list (list (car topic) 'visible)))
870 (setq gnus-topic-alist
872 (mapcar (lambda (info) (gnus-info-group info))
873 (cdr gnus-newsrc-alist)))
875 (gnus-topic-enter-dribble))
879 (defun gnus-topic-clean-alist ()
880 "Remove bogus groups from the topic alist."
881 (let ((topic-alist gnus-topic-alist)
883 (unless gnus-killed-hashtb
884 (gnus-make-hashtable-from-killed))
885 (while (setq topic (pop topic-alist))
886 (let ((topic-name (pop topic))
887 group filtered-topic)
888 (while (setq group (pop topic))
889 (when (and (or (gnus-active group)
890 (gnus-info-method (gnus-get-info group)))
891 (not (gnus-gethash group gnus-killed-hashtb)))
892 (push group filtered-topic)))
893 (push (cons topic-name (nreverse filtered-topic)) result)))
894 (setq gnus-topic-alist (nreverse result))))
896 (defun gnus-topic-change-level (group level oldlevel &optional previous)
897 "Run when changing levels to enter/remove groups from topics."
899 (set-buffer gnus-group-buffer)
900 (let ((buffer-read-only nil))
901 (unless gnus-topic-inhibit-change-level
902 (gnus-group-goto-group (or (car (nth 2 previous)) group))
903 (when (and gnus-topic-mode
905 (not gnus-topic-inhibit-change-level))
906 ;; Remove the group from the topics.
907 (if (and (< oldlevel gnus-level-zombie)
908 (>= level gnus-level-zombie))
909 (let ((alist gnus-topic-alist))
910 (while (gnus-group-goto-group group)
913 (when (member group (car alist))
914 (setcdr (car alist) (delete group (cdar alist))))
916 ;; If the group is subscribed we enter it into the topics.
917 (when (and (< level gnus-level-zombie)
918 (>= oldlevel gnus-level-zombie))
919 (let* ((prev (gnus-group-group-name))
920 (gnus-topic-inhibit-change-level t)
921 (gnus-group-indentation
923 (* gnus-topic-indent-level
925 (gnus-topic-goto-topic (gnus-current-topic))
926 (gnus-group-topic-level))
929 (yanked (list group))
931 ;; Then we enter the yanked groups into the topics
933 (when (setq alist (assoc (save-excursion
937 (caar gnus-topic-topology)))
940 (when (stringp yanked)
941 (setq yanked (list yanked)))
944 (if (not (cdr alist))
945 (setcdr alist (nconc yanked (cdr alist)))
946 (while (and (not end) (cdr alist))
947 (when (equal (cadr alist) prev)
948 (setcdr alist (nconc yanked (cdr alist)))
950 (setq alist (cdr alist)))
952 (nconc talist yanked))))))
953 (gnus-topic-update-topic))))))))
955 (defun gnus-topic-goto-next-group (group props)
956 "Go to group or the next group after group."
958 (if (not (memq 'gnus-topic props))
959 (goto-char (point-max))
960 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
961 (if (gnus-group-goto-group group)
963 ;; The group is no longer visible.
964 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
965 (after (cdr (member group (cdr list)))))
966 ;; First try to put point on a group after the current one.
968 (not (gnus-group-goto-group (car after))))
969 (setq after (cdr after)))
970 ;; Then try to put point on a group before point.
972 (setq after (cdr (member group (reverse (cdr list)))))
974 (not (gnus-group-goto-group (car after))))
975 (setq after (cdr after))))
976 ;; Finally, just put point on the topic.
978 (goto-char (point-min))
980 (gnus-topic-goto-topic (car list))
984 ;;; Topic-active functions
986 (defun gnus-topic-grok-active (&optional force)
987 "Parse all active groups and create topic structures for them."
988 ;; First we make sure that we have really read the active file.
990 (not gnus-topic-active-alist))
992 ;; Get a list of all groups available.
993 (mapatoms (lambda (g) (when (symbol-value g)
994 (push (symbol-name g) groups)))
996 (setq groups (sort groups 'string<))
997 ;; Init the variables.
998 (setq gnus-topic-active-topology (list (list "" 'visible)))
999 (setq gnus-topic-active-alist nil)
1000 ;; Descend the top-level hierarchy.
1001 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
1002 ;; Set the top-level topic names to something nice.
1003 (setcar (car gnus-topic-active-topology) "Gnus active")
1004 (setcar (car gnus-topic-active-alist) "Gnus active"))))
1006 (defun gnus-topic-grok-active-1 (topology groups)
1007 (let* ((name (caar topology))
1008 (prefix (concat "^" (regexp-quote name)))
1009 tgroups ntopology group)
1011 (string-match prefix (setq group (car groups))))
1012 (if (not (string-match "\\." group (match-end 0)))
1013 ;; There are no further hierarchies here, so we just
1014 ;; enter this group into the list belonging to this
1016 (push (pop groups) tgroups)
1017 ;; New sub-hierarchy, so we add it to the topology.
1018 (nconc topology (list (setq ntopology
1019 (list (list (substring
1020 group 0 (match-end 0))
1022 ;; Descend the hierarchy.
1023 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
1024 ;; We remove the trailing "." from the topic name.
1026 (if (string-match "\\.$" name)
1027 (substring name 0 (match-beginning 0))
1029 ;; Add this topic and its groups to the topic alist.
1030 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
1031 (setcar (car topology) name)
1032 ;; We return the rest of the groups that didn't belong
1036 ;;; Topic mode, commands and keymap.
1038 (defvar gnus-topic-mode-map nil)
1039 (defvar gnus-group-topic-map nil)
1041 (unless gnus-topic-mode-map
1042 (setq gnus-topic-mode-map (make-sparse-keymap))
1044 ;; Override certain group mode keys.
1045 (gnus-define-keys gnus-topic-mode-map
1046 "=" gnus-topic-select-group
1047 "\r" gnus-topic-select-group
1048 " " gnus-topic-read-group
1049 "\C-c\C-x" gnus-topic-expire-articles
1050 "c" gnus-topic-catchup-articles
1051 "\C-k" gnus-topic-kill-group
1052 "\C-y" gnus-topic-yank-group
1053 "\M-g" gnus-topic-get-new-news-this-topic
1054 "AT" gnus-topic-list-active
1055 "Gp" gnus-topic-edit-parameters
1056 "#" gnus-topic-mark-topic
1057 "\M-#" gnus-topic-unmark-topic
1058 [tab] gnus-topic-indent
1059 [(meta tab)] gnus-topic-unindent
1060 "\C-i" gnus-topic-indent
1061 "\M-\C-i" gnus-topic-unindent
1062 gnus-mouse-2 gnus-mouse-pick-topic)
1064 ;; Define a new submap.
1065 (gnus-define-keys (gnus-group-topic-map "T" gnus-topic-mode-map)
1066 "#" gnus-topic-mark-topic
1067 "\M-#" gnus-topic-unmark-topic
1068 "n" gnus-topic-create-topic
1069 "m" gnus-topic-move-group
1070 "D" gnus-topic-remove-group
1071 "c" gnus-topic-copy-group
1072 "h" gnus-topic-hide-topic
1073 "s" gnus-topic-show-topic
1074 "j" gnus-topic-jump-to-topic
1075 "M" gnus-topic-move-matching
1076 "C" gnus-topic-copy-matching
1077 "\M-p" gnus-topic-goto-previous-topic
1078 "\M-n" gnus-topic-goto-next-topic
1079 "\C-i" gnus-topic-indent
1080 [tab] gnus-topic-indent
1081 "r" gnus-topic-rename
1082 "\177" gnus-topic-delete
1083 [delete] gnus-topic-delete
1084 "H" gnus-topic-toggle-display-empty-topics)
1086 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1087 "s" gnus-topic-sort-groups
1088 "a" gnus-topic-sort-groups-by-alphabet
1089 "u" gnus-topic-sort-groups-by-unread
1090 "l" gnus-topic-sort-groups-by-level
1091 "e" gnus-topic-sort-groups-by-server
1092 "v" gnus-topic-sort-groups-by-score
1093 "r" gnus-topic-sort-groups-by-rank
1094 "m" gnus-topic-sort-groups-by-method))
1096 (defun gnus-topic-make-menu-bar ()
1097 (unless (boundp 'gnus-topic-menu)
1099 gnus-topic-menu gnus-topic-mode-map ""
1101 ["Toggle topics" gnus-topic-mode t]
1103 ["Copy..." gnus-topic-copy-group t]
1104 ["Move..." gnus-topic-move-group t]
1105 ["Remove" gnus-topic-remove-group t]
1106 ["Copy matching..." gnus-topic-copy-matching t]
1107 ["Move matching..." gnus-topic-move-matching t])
1109 ["Goto..." gnus-topic-jump-to-topic t]
1110 ["Show" gnus-topic-show-topic t]
1111 ["Hide" gnus-topic-hide-topic t]
1112 ["Delete" gnus-topic-delete t]
1113 ["Rename..." gnus-topic-rename t]
1114 ["Create..." gnus-topic-create-topic t]
1115 ["Mark" gnus-topic-mark-topic t]
1116 ["Indent" gnus-topic-indent t]
1117 ["Sort" gnus-topic-sort-topics t]
1118 ["Previous topic" gnus-topic-goto-previous-topic t]
1119 ["Next topic" gnus-topic-goto-next-topic t]
1120 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1121 ["Edit parameters" gnus-topic-edit-parameters t])
1122 ["List active" gnus-topic-list-active t]))))
1124 (defun gnus-topic-mode (&optional arg redisplay)
1125 "Minor mode for topicsifying Gnus group buffers."
1126 (interactive (list current-prefix-arg t))
1127 (when (eq major-mode 'gnus-group-mode)
1128 (make-local-variable 'gnus-topic-mode)
1129 (setq gnus-topic-mode
1130 (if (null arg) (not gnus-topic-mode)
1131 (> (prefix-numeric-value arg) 0)))
1132 ;; Infest Gnus with topics.
1133 (if (not gnus-topic-mode)
1134 (setq gnus-goto-missing-group-function nil)
1135 (when (gnus-visual-p 'topic-menu 'menu)
1136 (gnus-topic-make-menu-bar))
1137 (gnus-set-format 'topic t)
1138 (add-minor-mode 'gnus-topic-mode " Topic"
1139 gnus-topic-mode-map nil (lambda (&rest junk)
1141 (gnus-topic-mode nil t)))
1142 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1143 (set (make-local-variable 'gnus-group-prepare-function)
1144 'gnus-group-prepare-topics)
1145 (set (make-local-variable 'gnus-group-get-parameter-function)
1146 'gnus-group-topic-parameters)
1147 (set (make-local-variable 'gnus-group-goto-next-group-function)
1148 'gnus-topic-goto-next-group)
1149 (set (make-local-variable 'gnus-group-indentation-function)
1150 'gnus-topic-group-indentation)
1151 (set (make-local-variable 'gnus-group-update-group-function)
1152 'gnus-topic-update-topics-containing-group)
1153 (set (make-local-variable 'gnus-group-sort-alist-function)
1154 'gnus-group-sort-topic)
1155 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1156 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1157 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
1158 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1160 (setq gnus-topology-checked-p nil)
1161 ;; We check the topology.
1162 (when gnus-newsrc-alist
1163 (gnus-topic-check-topology))
1164 (gnus-run-hooks 'gnus-topic-mode-hook))
1165 ;; Remove topic infestation.
1166 (unless gnus-topic-mode
1167 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1168 (setq gnus-group-change-level-function nil)
1169 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1170 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1171 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1173 (gnus-group-list-groups))))
1175 (defun gnus-topic-select-group (&optional all)
1176 "Select this newsgroup.
1177 No article is selected automatically.
1178 If the group is opened, just switch the summary buffer.
1179 If ALL is non-nil, already read articles become readable.
1180 If ALL is a number, fetch this number of articles.
1182 If performed over a topic line, toggle folding the topic."
1184 (when (and (eobp) (not (gnus-group-group-name)))
1186 (if (gnus-group-topic-p)
1187 (let ((gnus-group-list-mode
1188 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1189 (gnus-topic-fold all)
1190 (gnus-dribble-touch))
1191 (gnus-group-select-group all)))
1193 (defun gnus-mouse-pick-topic (e)
1194 "Select the group or topic under the mouse pointer."
1197 (gnus-topic-read-group nil))
1199 (defun gnus-topic-expire-articles (topic)
1200 "Expire articles in this topic or group."
1201 (interactive (list (gnus-group-topic-name)))
1203 (call-interactively 'gnus-group-expire-articles)
1205 (gnus-message 5 "Expiring groups in %s..." topic)
1206 (let ((gnus-group-marked
1207 (mapcar (lambda (entry) (car (nth 2 entry)))
1208 (gnus-topic-find-groups topic gnus-level-killed t
1210 (gnus-group-expire-articles nil))
1211 (gnus-message 5 "Expiring groups in %s...done" topic))))
1213 (defun gnus-topic-catchup-articles (topic)
1214 "Catchup this topic or group.
1215 Also see `gnus-group-catchup'."
1216 (interactive (list (gnus-group-topic-name)))
1218 (call-interactively 'gnus-group-catchup-current)
1221 (mapcar (lambda (entry) (car (nth 2 entry)))
1222 (gnus-topic-find-groups topic gnus-level-killed t
1224 (buffer-read-only nil)
1225 (gnus-group-marked groups))
1226 (gnus-group-catchup-current)
1227 (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1229 (defun gnus-topic-read-group (&optional all no-article group)
1230 "Read news in this newsgroup.
1231 If the prefix argument ALL is non-nil, already read articles become
1232 readable. IF ALL is a number, fetch this number of articles. If the
1233 optional argument NO-ARTICLE is non-nil, no article will be
1234 auto-selected upon group entry. If GROUP is non-nil, fetch that
1237 If performed over a topic line, toggle folding the topic."
1239 (if (gnus-group-topic-p)
1240 (let ((gnus-group-list-mode
1241 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1242 (gnus-topic-fold all))
1243 (gnus-group-read-group all no-article group)))
1245 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1246 "Create a new TOPIC under PARENT.
1247 When used interactively, PARENT will be the topic under point."
1250 (read-string "New topic: ")
1251 (gnus-current-topic)))
1252 ;; Check whether this topic already exists.
1253 (when (gnus-topic-find-topology topic)
1254 (error "Topic already exists"))
1256 (setq parent (caar gnus-topic-topology)))
1257 (let ((top (cdr (gnus-topic-find-topology parent)))
1258 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1260 (error "No such parent topic: %s" parent))
1263 (while (and (cdr top)
1264 (not (equal (caaadr top) previous)))
1265 (setq top (cdr top)))
1266 (setcdr top (cons full-topic (cdr top))))
1267 (nconc top (list full-topic)))
1268 (unless (assoc topic gnus-topic-alist)
1269 (push (list topic) gnus-topic-alist)))
1270 (gnus-topic-enter-dribble)
1271 (gnus-group-list-groups)
1272 (gnus-topic-goto-topic topic))
1275 ;; 1. When the marked groups are overlapped with the process
1276 ;; region, the behavior of move or remove is not right.
1277 ;; 2. Can't process on several marked groups with a same name,
1278 ;; because gnus-group-marked only keeps one copy.
1280 (defun gnus-topic-move-group (n topic &optional copyp)
1281 "Move the next N groups to TOPIC.
1282 If COPYP, copy the groups instead."
1284 (list current-prefix-arg
1285 (gnus-completing-read "Move to topic" gnus-topic-alist nil t
1286 'gnus-topic-history)))
1287 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1288 gnus-group-marked t))
1289 (groups (gnus-group-process-prefix n))
1290 (topicl (assoc topic gnus-topic-alist))
1291 (start-topic (gnus-group-topic-name))
1292 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1294 (if (and (not groups) (not copyp) start-topic)
1295 (gnus-topic-move start-topic topic)
1298 (gnus-group-remove-mark g use-marked)
1300 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1302 (setcdr entry (gnus-delete-first g (cdr entry))))
1303 (nconc topicl (list g)))
1305 (gnus-topic-enter-dribble)
1307 (gnus-group-goto-group start-group)
1308 (gnus-topic-goto-topic start-topic))
1309 (gnus-group-list-groups))))
1311 (defun gnus-topic-remove-group (&optional n)
1312 "Remove the current group from the topic."
1314 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1315 gnus-group-marked t))
1316 (groups (gnus-group-process-prefix n)))
1319 (gnus-group-remove-mark group use-marked)
1320 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1321 (buffer-read-only nil))
1322 (when (and topicl group)
1324 (gnus-delete-first group topicl))
1325 (gnus-topic-update-topic)))
1327 (gnus-topic-enter-dribble)
1328 (gnus-group-position-point)))
1330 (defun gnus-topic-copy-group (n topic)
1331 "Copy the current group to a topic."
1333 (list current-prefix-arg
1334 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1335 (gnus-topic-move-group n topic t))
1337 (defun gnus-topic-kill-group (&optional n discard)
1338 "Kill the next N groups."
1340 (if (gnus-group-topic-p)
1341 (let ((topic (gnus-group-topic-name)))
1343 (gnus-topic-find-topology topic)
1344 (assoc topic gnus-topic-alist))
1345 gnus-topic-killed-topics)
1346 (gnus-topic-remove-topic nil t)
1347 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1348 (gnus-topic-enter-dribble))
1349 (gnus-group-kill-group n discard)
1350 (if (not (gnus-group-topic-p))
1351 (gnus-topic-update-topic)
1352 ;; Move up one line so that we update the right topic.
1354 (gnus-topic-update-topic)
1357 (defun gnus-topic-yank-group (&optional arg)
1358 "Yank the last topic."
1360 (if gnus-topic-killed-topics
1362 (or (gnus-group-topic-name)
1363 (gnus-topic-next-topic (gnus-current-topic))))
1364 (data (pop gnus-topic-killed-topics))
1367 (push alist gnus-topic-alist)
1368 (gnus-topic-create-topic
1369 (caar item) (gnus-topic-parent-topic previous) previous
1371 (gnus-topic-enter-dribble)
1372 (gnus-topic-goto-topic (caar item)))
1373 (let* ((prev (gnus-group-group-name))
1374 (gnus-topic-inhibit-change-level t)
1375 (gnus-group-indentation
1377 (* gnus-topic-indent-level
1379 (gnus-topic-goto-topic (gnus-current-topic))
1380 (gnus-group-topic-level))
1384 ;; We first yank the groups the normal way...
1385 (setq yanked (gnus-group-yank-group arg))
1386 ;; Then we enter the yanked groups into the topics they belong
1388 (setq alist (assoc (save-excursion
1390 (gnus-current-topic))
1392 (when (stringp yanked)
1393 (setq yanked (list yanked)))
1395 (nconc alist yanked)
1396 (if (not (cdr alist))
1397 (setcdr alist (nconc yanked (cdr alist)))
1399 (when (equal (cadr alist) prev)
1400 (setcdr alist (nconc yanked (cdr alist)))
1402 (setq alist (cdr alist))))))
1403 (gnus-topic-update-topic)))
1405 (defun gnus-topic-hide-topic (&optional permanent)
1406 "Hide the current topic.
1407 If PERMANENT, make it stay hidden in subsequent sessions as well."
1409 (when (gnus-current-topic)
1410 (gnus-topic-goto-topic (gnus-current-topic))
1414 (gnus-topic-find-topology (gnus-current-topic))))
1416 (gnus-topic-remove-topic nil nil)))
1418 (defun gnus-topic-show-topic (&optional permanent)
1419 "Show the hidden topic.
1420 If PERMANENT, make it stay shown in subsequent sessions as well."
1422 (when (gnus-group-topic-p)
1424 (gnus-topic-remove-topic t nil)
1426 (gnus-topic-find-topology
1427 (completing-read "Show topic: " gnus-topic-alist nil t))))
1428 (setcar (cddr (cadr topic)) nil)
1429 (setcar (cdr (cadr topic)) 'visible)
1430 (gnus-group-list-groups)))))
1432 (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
1433 "Mark all groups in the TOPIC with the process mark.
1434 If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
1435 (interactive (list (gnus-group-topic-name)
1437 (and current-prefix-arg t)))
1439 (call-interactively 'gnus-group-mark-group)
1441 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1442 (not non-recursive))))
1444 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1445 (gnus-info-group (nth 2 (pop groups)))))))))
1447 (defun gnus-topic-unmark-topic (topic &optional dummy non-recursive)
1448 "Remove the process mark from all groups in the TOPIC.
1449 If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
1450 (interactive (list (gnus-group-topic-name)
1452 (and current-prefix-arg t)))
1454 (call-interactively 'gnus-group-unmark-group)
1455 (gnus-topic-mark-topic topic t non-recursive)))
1457 (defun gnus-topic-get-new-news-this-topic (&optional n)
1458 "Check for new news in the current topic."
1460 (if (not (gnus-group-topic-p))
1461 (gnus-group-get-new-news-this-group n)
1462 (let* ((topic (gnus-group-topic-name))
1463 (data (cadr (gnus-topic-find-topology topic))))
1465 (gnus-topic-mark-topic topic nil (and n t))
1466 (gnus-group-get-new-news-this-group))
1467 (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1469 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1470 "Move all groups that match REGEXP to some topic."
1475 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1476 (read-string (format "Move to %s (regexp): " topic))))))
1477 (gnus-group-mark-regexp regexp)
1478 (gnus-topic-move-group nil topic copyp))
1480 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1481 "Copy all groups that match REGEXP to some topic."
1486 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1487 (read-string (format "Copy to %s (regexp): " topic))))))
1488 (gnus-topic-move-matching regexp topic t))
1490 (defun gnus-topic-delete (topic)
1492 (interactive (list (gnus-group-topic-name)))
1494 (error "No topic to be deleted"))
1495 (let ((entry (assoc topic gnus-topic-alist))
1496 (buffer-read-only nil))
1498 (error "Topic not empty"))
1499 ;; Delete if visible.
1500 (when (gnus-topic-goto-topic topic)
1502 ;; Remove from alist.
1503 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1504 ;; Remove from topology.
1505 (gnus-topic-find-topology topic nil nil 'delete)
1506 (gnus-dribble-touch)))
1508 (defun gnus-topic-rename (old-name new-name)
1511 (let ((topic (gnus-current-topic)))
1513 (read-string (format "Rename %s to: " topic) topic))))
1514 ;; Check whether the new name exists.
1515 (when (gnus-topic-find-topology new-name)
1516 (error "Topic '%s' already exists" new-name))
1517 ;; "nil" is an invalid name, for reasons I'd rather not go
1518 ;; into here. Trust me.
1519 (when (equal new-name "nil")
1520 (error "Invalid name: %s" nil))
1522 (let ((top (gnus-topic-find-topology old-name))
1523 (entry (assoc old-name gnus-topic-alist)))
1525 (setcar (cadr top) new-name))
1527 (setcar entry new-name))
1529 (gnus-dribble-touch)
1530 (gnus-group-list-groups)
1533 (defun gnus-topic-indent (&optional unindent)
1534 "Indent a topic -- make it a sub-topic of the previous topic.
1535 If UNINDENT, remove an indentation."
1538 (gnus-topic-unindent)
1539 (let* ((topic (gnus-current-topic))
1540 (parent (gnus-topic-previous-topic topic))
1541 (buffer-read-only nil))
1543 (error "Nothing to indent %s into" topic))
1545 (gnus-topic-goto-topic topic)
1546 (gnus-topic-kill-group)
1547 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1548 (gnus-topic-create-topic
1549 topic parent nil (cdar (car gnus-topic-killed-topics)))
1550 (pop gnus-topic-killed-topics)
1551 (or (gnus-topic-goto-topic topic)
1552 (gnus-topic-goto-topic parent))))))
1554 (defun gnus-topic-unindent ()
1557 (let* ((topic (gnus-current-topic))
1558 (parent (gnus-topic-parent-topic topic))
1559 (grandparent (gnus-topic-parent-topic parent)))
1561 (error "Nothing to indent %s into" topic))
1563 (gnus-topic-goto-topic topic)
1564 (gnus-topic-kill-group)
1565 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1566 (gnus-topic-create-topic
1567 topic grandparent (gnus-topic-next-topic parent)
1568 (cdar (car gnus-topic-killed-topics)))
1569 (pop gnus-topic-killed-topics)
1570 (gnus-topic-goto-topic topic))))
1572 (defun gnus-topic-list-active (&optional force)
1573 "List all groups that Gnus knows about in a topicsified fashion.
1574 If FORCE, always re-read the active file."
1577 (gnus-get-killed-groups))
1578 (gnus-topic-grok-active force)
1579 (let ((gnus-topic-topology gnus-topic-active-topology)
1580 (gnus-topic-alist gnus-topic-active-alist)
1581 gnus-killed-list gnus-zombie-list)
1582 (gnus-group-list-groups gnus-level-killed nil 1)))
1584 (defun gnus-topic-toggle-display-empty-topics ()
1585 "Show/hide topics that have no unread articles."
1587 (setq gnus-topic-display-empty-topics
1588 (not gnus-topic-display-empty-topics))
1589 (gnus-group-list-groups)
1590 (message "%s empty topics"
1591 (if gnus-topic-display-empty-topics
1592 "Showing" "Hiding")))
1594 ;;; Topic sorting functions
1596 (defun gnus-topic-edit-parameters (group)
1597 "Edit the group parameters of GROUP.
1598 If performed on a topic, edit the topic parameters instead."
1599 (interactive (list (gnus-group-group-name)))
1601 (gnus-group-edit-group-parameters group)
1602 (if (not (gnus-group-topic-p))
1603 (error "Nothing to edit on the current line")
1604 (let ((topic (gnus-group-topic-name)))
1606 (gnus-topic-parameters topic)
1607 (format "Editing the topic parameters for `%s'."
1610 (gnus-topic-set-parameters ,topic form)))))))
1612 (defun gnus-group-sort-topic (func reverse)
1613 "Sort groups in the topics according to FUNC and REVERSE."
1614 (let ((alist gnus-topic-alist))
1616 ;; !!!Sometimes nil elements sneak into the alist,
1617 ;; for some reason or other.
1618 (setcar alist (delq nil (car alist)))
1619 (setcar alist (delete "dummy.group" (car alist)))
1620 (gnus-topic-sort-topic (pop alist) func reverse))))
1622 (defun gnus-topic-sort-topic (topic func reverse)
1623 ;; Each topic only lists the name of the group, while
1624 ;; the sort predicates expect group infos as inputs.
1625 ;; So we first transform the group names into infos,
1626 ;; then sort, and then transform back into group names.
1630 (lambda (info) (gnus-info-group info))
1633 (lambda (group) (gnus-get-info group))
1636 ;; Do the reversal, if necessary.
1638 (setcdr topic (nreverse (cdr topic)))))
1640 (defun gnus-topic-sort-groups (func &optional reverse)
1641 "Sort the current topic according to FUNC.
1642 If REVERSE, reverse the sorting order."
1643 (interactive (list gnus-group-sort-function current-prefix-arg))
1644 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1645 (gnus-topic-sort-topic
1646 topic (gnus-make-sort-function func) reverse)
1647 (gnus-group-list-groups)))
1649 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1650 "Sort the current topic alphabetically by group name.
1651 If REVERSE, sort in reverse order."
1653 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1655 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1656 "Sort the current topic by number of unread articles.
1657 If REVERSE, sort in reverse order."
1659 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1661 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1662 "Sort the current topic by group level.
1663 If REVERSE, sort in reverse order."
1665 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1667 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1668 "Sort the current topic by group score.
1669 If REVERSE, sort in reverse order."
1671 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1673 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1674 "Sort the current topic by group rank.
1675 If REVERSE, sort in reverse order."
1677 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1679 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1680 "Sort the current topic alphabetically by backend name.
1681 If REVERSE, sort in reverse order."
1683 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1685 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1686 "Sort the current topic alphabetically by server name.
1687 If REVERSE, sort in reverse order."
1689 (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1691 (defun gnus-topic-sort-topics-1 (top reverse)
1694 (mapcar (gnus-byte-compile
1696 (gnus-topic-sort-topics-1 top ,reverse)))
1699 (string-lessp (caar t1) (caar t2)))))))
1700 (setcdr top (if reverse (reverse subtop) subtop))))
1703 (defun gnus-topic-sort-topics (&optional topic reverse)
1704 "Sort topics in TOPIC alphabetically by topic name.
1705 If REVERSE, reverse the sorting order."
1707 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1708 (gnus-current-topic))
1709 current-prefix-arg))
1710 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1711 gnus-topic-topology)))
1712 (gnus-topic-sort-topics-1 topic-topology reverse)
1713 (gnus-topic-enter-dribble)
1714 (gnus-group-list-groups)
1715 (gnus-topic-goto-topic topic)))
1717 (defun gnus-topic-move (current to)
1718 "Move the CURRENT topic to TO."
1721 (gnus-group-topic-name)
1722 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1723 (unless (and current to)
1724 (error "Can't find topic"))
1725 (let ((current-top (cdr (gnus-topic-find-topology current)))
1726 (to-top (cdr (gnus-topic-find-topology to))))
1728 (error "Can't find topic `%s'" current))
1730 (error "Can't find topic `%s'" to))
1731 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1732 (error "Can't move `%s' to its sub-level" current))
1733 (gnus-topic-find-topology current nil nil 'delete)
1735 (setq to-top (cdr to-top)))
1736 (setcdr to-top (list current-top))
1737 (gnus-topic-enter-dribble)
1738 (gnus-group-list-groups)
1739 (gnus-topic-goto-topic current)))
1741 (defun gnus-subscribe-topics (newsgroup)
1743 (let (match gnus-group-change-level-function)
1744 (dolist (topic (gnus-topic-list))
1745 (when (and (setq match (cdr (assq 'subscribe
1746 (gnus-topic-parameters topic))))
1747 (string-match match newsgroup))
1748 ;; Just subscribe the group.
1749 (gnus-subscribe-alphabetically newsgroup)
1750 ;; Add the group to the topic.
1751 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1752 ;; if this topic specifies a default level, use it
1753 (let ((subscribe-level (cdr (assq 'subscribe-level
1754 (gnus-topic-parameters topic)))))
1755 (when subscribe-level
1756 (gnus-group-change-level newsgroup subscribe-level
1757 gnus-level-default-subscribed)))
1761 (provide 'gnus-topic)
1763 ;;; gnus-topic.el ends here