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 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 2, 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 taking into account inheritance from topics."
382 (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
387 ;; buffer and find the topic for the group that way.
388 ;; This hopefully copes well with groups that are in
389 ;; more than one topic. Failing that (i.e. when the
390 ;; group isn't visible in the group buffer) we find a
391 ;; topic for the group via gnus-group-topic.
392 (or (and (gnus-group-goto-group group)
393 (gnus-current-topic))
394 (gnus-group-topic group)))))))
396 (defun gnus-topic-hierarchical-parameters (topic)
397 "Return a topic list computed for TOPIC."
398 (let ((params-list (nreverse (mapcar 'gnus-topic-parameters
399 (gnus-current-topics topic))))
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)
587 (when gnus-group-update-tool-bar
588 (gnus-put-text-property beg end 'point-entered
589 'gnus-tool-bar-update)
590 (gnus-put-text-property beg end 'point-left
591 'gnus-tool-bar-update))
595 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
596 "Remove the current topic."
597 (let ((topic (gnus-group-topic-name))
598 (level (gnus-group-topic-level))
599 (beg (progn (beginning-of-line) (point)))
602 (while (and (zerop (forward-line 1))
603 (> (or (gnus-group-topic-level) (1+ level)) level)))
604 (delete-region beg (point))
605 ;; Do the change in this rather odd manner because it has been
606 ;; reported that some topics share parts of some lists, for some
607 ;; reason. I have been unable to determine why this is the
608 ;; case, but this hack seems to take care of things.
609 (let ((data (cadr (gnus-topic-find-topology topic))))
611 (list (if insert 'visible 'invisible)
615 (setq gnus-topic-alist
616 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
617 (gnus-topic-insert-topic topic in-level)))))
619 (defun gnus-topic-insert-topic (topic &optional level)
621 (gnus-group-prepare-topics
622 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
623 nil nil topic level))
625 (defun gnus-topic-fold (&optional insert topic)
626 "Remove/insert the current topic."
627 (let ((topic (or topic (gnus-group-topic-name))))
630 (if (not (gnus-group-active-topic-p))
631 (gnus-topic-remove-topic
632 (or insert (not (gnus-topic-visible-p))))
633 (let ((gnus-topic-topology gnus-topic-active-topology)
634 (gnus-topic-alist gnus-topic-active-alist)
635 (gnus-group-list-mode (cons 5 t)))
636 (gnus-topic-remove-topic
637 (or insert (not (gnus-topic-visible-p))) nil nil 9)
638 (gnus-topic-enter-dribble)))))))
640 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
642 (let* ((visible (if visiblep "" "..."))
643 (indentation (make-string (* gnus-topic-indent-level level) ? ))
644 (total-number-of-articles unread)
645 (number-of-groups (length entries))
646 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
648 (gnus-topic-update-unreads name unread)
652 (gnus-add-text-properties
655 (eval gnus-topic-line-format-spec))
656 (list 'gnus-topic (intern name)
657 'gnus-topic-level level
658 'gnus-topic-unread unread
659 'gnus-active active-topic
660 'gnus-topic-visible visiblep)))))
662 (defun gnus-topic-update-unreads (topic unreads)
663 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
665 (push (cons topic unreads) gnus-topic-unreads))
667 (defun gnus-topic-update-topics-containing-group (group)
668 "Update all topics that have GROUP as a member."
669 (when (and (eq major-mode 'gnus-group-mode)
672 (let ((alist gnus-topic-alist))
673 ;; This is probably not entirely correct. If a topic
674 ;; isn't shown, then it's not updated. But the updating
675 ;; should be performed in any case, since the topic's
676 ;; parent should be updated. Pfft.
678 (when (and (member group (cdar alist))
679 (gnus-topic-goto-topic (caar alist)))
680 (gnus-topic-update-topic-line (caar alist)))
683 (defun gnus-topic-update-topic ()
684 "Update all parent topics to the current group."
685 (when (and (eq major-mode 'gnus-group-mode)
687 (let ((group (gnus-group-group-name))
689 (buffer-read-only nil))
691 (gnus-get-info group)
692 (gnus-topic-goto-topic (gnus-current-topic)))
693 (gnus-topic-update-topic-line (gnus-group-topic-name))
696 (gnus-group-position-point)))))
698 (defun gnus-topic-goto-missing-group (group)
699 "Place point where GROUP is supposed to be inserted."
700 (let* ((topic (gnus-group-topic group))
701 (groups (cdr (assoc topic gnus-topic-alist)))
702 (g (cdr (member group groups)))
705 ;; Try to jump to a visible group.
707 (not (gnus-group-goto-group (car g) t)))
709 ;; It wasn't visible, so we try to see where to insert it.
711 (setq g (cdr (member group (reverse groups))))
712 (while (and g unfound)
713 (when (gnus-group-goto-group (pop g) t)
718 (not (gnus-topic-goto-missing-topic topic)))
719 (gnus-topic-display-missing-topic topic)))))
721 (defun gnus-topic-display-missing-topic (topic)
722 "Insert topic lines recursively for missing topics."
723 (let ((parent (gnus-topic-find-topology
724 (gnus-topic-parent-topic topic))))
726 (not (gnus-topic-goto-missing-topic (caadr parent))))
727 (gnus-topic-display-missing-topic (caadr parent))))
728 (gnus-topic-goto-missing-topic topic)
729 ;; Skip past all groups in the topic we're in.
730 (while (gnus-group-group-name)
732 (let* ((top (gnus-topic-find-topology topic))
733 (children (cddr top))
736 (entries (gnus-topic-find-groups
737 (car type) (car gnus-group-list-mode)
738 (cdr gnus-group-list-mode)))
741 (incf unread (gnus-topic-unread (caar (pop children)))))
742 (while (setq entry (pop entries))
743 (when (numberp (car entry))
744 (incf unread (car entry))))
745 (gnus-topic-insert-topic-line
746 topic t t (car (gnus-topic-find-topology topic)) nil unread)))
748 (defun gnus-topic-goto-missing-topic (topic)
749 (if (gnus-topic-goto-topic topic)
751 ;; Topic not displayed.
752 (let* ((top (gnus-topic-find-topology
753 (gnus-topic-parent-topic topic)))
754 (tp (reverse (cddr top))))
756 (gnus-topic-insert-topic-line
757 topic t t (car (gnus-topic-find-topology topic)) nil 0)
758 (while (not (equal (caaar tp) topic))
762 (not (gnus-topic-goto-topic (caaar tp))))
765 (gnus-topic-forward-topic 1)
766 (gnus-topic-goto-missing-topic (caadr top)))))
769 (defun gnus-topic-update-topic-line (topic-name &optional reads)
770 (let* ((top (gnus-topic-find-topology topic-name))
772 (children (cddr top))
773 (entries (gnus-topic-find-groups
774 (car type) (car gnus-group-list-mode)
775 (cdr gnus-group-list-mode)))
776 (parent (gnus-topic-parent-topic topic-name))
777 (all-entries entries)
779 old-unread entry new-unread)
780 (when (gnus-topic-goto-topic (car type))
781 ;; Tally all the groups that belong in this topic.
783 (setq unread (- (gnus-group-topic-unread) reads))
785 (incf unread (gnus-topic-unread (caar (pop children)))))
786 (while (setq entry (pop entries))
787 (when (numberp (car entry))
788 (incf unread (car entry)))))
789 (setq old-unread (gnus-group-topic-unread))
790 ;; Insert the topic line.
791 (gnus-topic-insert-topic-line
792 (car type) (gnus-topic-visible-p)
793 (not (eq (nth 2 type) 'hidden))
794 (gnus-group-topic-level) all-entries unread)
797 (setq new-unread (gnus-group-topic-unread)))
800 (gnus-topic-update-topic-line
802 (- (or old-unread 0) (or new-unread 0))))
805 (defun gnus-topic-group-indentation ()
807 (* gnus-topic-indent-level
810 (gnus-topic-goto-topic (gnus-current-topic))
811 (gnus-group-topic-level))
817 (gnus-add-shutdown 'gnus-topic-close 'gnus)
819 (defun gnus-topic-close ()
820 (setq gnus-topic-active-topology nil
821 gnus-topic-active-alist nil
822 gnus-topic-killed-topics nil
823 gnus-topology-checked-p nil))
825 (defun gnus-topic-check-topology ()
826 ;; The first time we set the topology to whatever we have
827 ;; gotten here, which can be rather random.
828 (unless gnus-topic-alist
829 (gnus-topic-init-alist))
831 (setq gnus-topology-checked-p t)
832 ;; Go through the topic alist and make sure that all topics
833 ;; are in the topic topology.
834 (let ((topics (gnus-topic-list))
835 (alist gnus-topic-alist)
838 (unless (member (caar alist) topics)
839 (nconc gnus-topic-topology
840 (list (list (list (caar alist) 'visible))))
842 (setq alist (cdr alist)))
844 (gnus-topic-enter-dribble))
845 ;; Conversely, go through the topology and make sure that all
846 ;; topologies have alists.
848 (unless (assoc (car topics) gnus-topic-alist)
849 (push (list (car topics)) gnus-topic-alist))
851 ;; Go through all living groups and make sure that
852 ;; they belong to some topic.
853 (let* ((tgroups (apply 'append (mapcar 'cdr gnus-topic-alist)))
854 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
855 (newsrc (cdr gnus-newsrc-alist))
858 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
859 (setcdr entry (list group))
860 (setq entry (cdr entry)))))
861 ;; Go through all topics and make sure they contain only living groups.
862 (let ((alist gnus-topic-alist)
864 (while (setq topic (pop alist))
866 (if (and (cadr topic)
867 (gnus-group-entry (cadr topic)))
868 (setq topic (cdr topic))
869 (setcdr topic (cddr topic)))))))
871 (defun gnus-topic-init-alist ()
872 "Initialize the topic structures."
873 (setq gnus-topic-topology
874 (cons (list "Gnus" 'visible)
875 (mapcar (lambda (topic)
876 (list (list (car topic) 'visible)))
878 (setq gnus-topic-alist
880 (mapcar (lambda (info) (gnus-info-group info))
881 (cdr gnus-newsrc-alist)))
883 (gnus-topic-enter-dribble))
887 (defun gnus-topic-clean-alist ()
888 "Remove bogus groups from the topic alist."
889 (let ((topic-alist gnus-topic-alist)
891 (unless gnus-killed-hashtb
892 (gnus-make-hashtable-from-killed))
893 (while (setq topic (pop topic-alist))
894 (let ((topic-name (pop topic))
895 group filtered-topic)
896 (while (setq group (pop topic))
897 (when (and (or (gnus-active group)
898 (gnus-info-method (gnus-get-info group)))
899 (not (gnus-gethash group gnus-killed-hashtb)))
900 (push group filtered-topic)))
901 (push (cons topic-name (nreverse filtered-topic)) result)))
902 (setq gnus-topic-alist (nreverse result))))
904 (defun gnus-topic-change-level (group level oldlevel &optional previous)
905 "Run when changing levels to enter/remove groups from topics."
907 (set-buffer gnus-group-buffer)
908 (let ((buffer-read-only nil))
909 (unless gnus-topic-inhibit-change-level
910 (gnus-group-goto-group (or (car (nth 2 previous)) group))
911 (when (and gnus-topic-mode
913 (not gnus-topic-inhibit-change-level))
914 ;; Remove the group from the topics.
915 (if (and (< oldlevel gnus-level-zombie)
916 (>= level gnus-level-zombie))
917 (let ((alist gnus-topic-alist))
918 (while (gnus-group-goto-group group)
921 (when (member group (car alist))
922 (setcdr (car alist) (delete group (cdar alist))))
924 ;; If the group is subscribed we enter it into the topics.
925 (when (and (< level gnus-level-zombie)
926 (>= oldlevel gnus-level-zombie))
927 (let* ((prev (gnus-group-group-name))
928 (gnus-topic-inhibit-change-level t)
929 (gnus-group-indentation
931 (* gnus-topic-indent-level
933 (gnus-topic-goto-topic (gnus-current-topic))
934 (gnus-group-topic-level))
937 (yanked (list group))
939 ;; Then we enter the yanked groups into the topics
941 (when (setq alist (assoc (save-excursion
945 (caar gnus-topic-topology)))
948 (when (stringp yanked)
949 (setq yanked (list yanked)))
952 (if (not (cdr alist))
953 (setcdr alist (nconc yanked (cdr alist)))
954 (while (and (not end) (cdr alist))
955 (when (equal (cadr alist) prev)
956 (setcdr alist (nconc yanked (cdr alist)))
958 (setq alist (cdr alist)))
960 (nconc talist yanked))))))
961 (gnus-topic-update-topic))))))))
963 (defun gnus-topic-goto-next-group (group props)
964 "Go to group or the next group after group."
966 (if (not (memq 'gnus-topic props))
967 (goto-char (point-max))
968 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
969 (if (gnus-group-goto-group group)
971 ;; The group is no longer visible.
972 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
973 (after (cdr (member group (cdr list)))))
974 ;; First try to put point on a group after the current one.
976 (not (gnus-group-goto-group (car after))))
977 (setq after (cdr after)))
978 ;; Then try to put point on a group before point.
980 (setq after (cdr (member group (reverse (cdr list)))))
982 (not (gnus-group-goto-group (car after))))
983 (setq after (cdr after))))
984 ;; Finally, just put point on the topic.
986 (goto-char (point-min))
988 (gnus-topic-goto-topic (car list))
992 ;;; Topic-active functions
994 (defun gnus-topic-grok-active (&optional force)
995 "Parse all active groups and create topic structures for them."
996 ;; First we make sure that we have really read the active file.
998 (not gnus-topic-active-alist))
1000 ;; Get a list of all groups available.
1001 (mapatoms (lambda (g) (when (symbol-value g)
1002 (push (symbol-name g) groups)))
1004 (setq groups (sort groups 'string<))
1005 ;; Init the variables.
1006 (setq gnus-topic-active-topology (list (list "" 'visible)))
1007 (setq gnus-topic-active-alist nil)
1008 ;; Descend the top-level hierarchy.
1009 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
1010 ;; Set the top-level topic names to something nice.
1011 (setcar (car gnus-topic-active-topology) "Gnus active")
1012 (setcar (car gnus-topic-active-alist) "Gnus active"))))
1014 (defun gnus-topic-grok-active-1 (topology groups)
1015 (let* ((name (caar topology))
1016 (prefix (concat "^" (regexp-quote name)))
1017 tgroups ntopology group)
1019 (string-match prefix (setq group (car groups))))
1020 (if (not (string-match "\\." group (match-end 0)))
1021 ;; There are no further hierarchies here, so we just
1022 ;; enter this group into the list belonging to this
1024 (push (pop groups) tgroups)
1025 ;; New sub-hierarchy, so we add it to the topology.
1026 (nconc topology (list (setq ntopology
1027 (list (list (substring
1028 group 0 (match-end 0))
1030 ;; Descend the hierarchy.
1031 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
1032 ;; We remove the trailing "." from the topic name.
1034 (if (string-match "\\.$" name)
1035 (substring name 0 (match-beginning 0))
1037 ;; Add this topic and its groups to the topic alist.
1038 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
1039 (setcar (car topology) name)
1040 ;; We return the rest of the groups that didn't belong
1044 ;;; Topic mode, commands and keymap.
1046 (defvar gnus-topic-mode-map nil)
1047 (defvar gnus-group-topic-map nil)
1049 (unless gnus-topic-mode-map
1050 (setq gnus-topic-mode-map (make-sparse-keymap))
1052 ;; Override certain group mode keys.
1053 (gnus-define-keys gnus-topic-mode-map
1054 "=" gnus-topic-select-group
1055 "\r" gnus-topic-select-group
1056 " " gnus-topic-read-group
1057 "\C-c\C-x" gnus-topic-expire-articles
1058 "c" gnus-topic-catchup-articles
1059 "\C-k" gnus-topic-kill-group
1060 "\C-y" gnus-topic-yank-group
1061 "\M-g" gnus-topic-get-new-news-this-topic
1062 "AT" gnus-topic-list-active
1063 "Gp" gnus-topic-edit-parameters
1064 "#" gnus-topic-mark-topic
1065 "\M-#" gnus-topic-unmark-topic
1066 [tab] gnus-topic-indent
1067 [(meta tab)] gnus-topic-unindent
1068 "\C-i" gnus-topic-indent
1069 "\M-\C-i" gnus-topic-unindent
1070 gnus-mouse-2 gnus-mouse-pick-topic)
1072 ;; Define a new submap.
1073 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
1074 "#" gnus-topic-mark-topic
1075 "\M-#" gnus-topic-unmark-topic
1076 "n" gnus-topic-create-topic
1077 "m" gnus-topic-move-group
1078 "D" gnus-topic-remove-group
1079 "c" gnus-topic-copy-group
1080 "h" gnus-topic-hide-topic
1081 "s" gnus-topic-show-topic
1082 "j" gnus-topic-jump-to-topic
1083 "M" gnus-topic-move-matching
1084 "C" gnus-topic-copy-matching
1085 "\M-p" gnus-topic-goto-previous-topic
1086 "\M-n" gnus-topic-goto-next-topic
1087 "\C-i" gnus-topic-indent
1088 [tab] gnus-topic-indent
1089 "r" gnus-topic-rename
1090 "\177" gnus-topic-delete
1091 [delete] gnus-topic-delete
1092 "H" gnus-topic-toggle-display-empty-topics)
1094 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1095 "s" gnus-topic-sort-groups
1096 "a" gnus-topic-sort-groups-by-alphabet
1097 "u" gnus-topic-sort-groups-by-unread
1098 "l" gnus-topic-sort-groups-by-level
1099 "e" gnus-topic-sort-groups-by-server
1100 "v" gnus-topic-sort-groups-by-score
1101 "r" gnus-topic-sort-groups-by-rank
1102 "m" gnus-topic-sort-groups-by-method))
1104 (defun gnus-topic-make-menu-bar ()
1105 (unless (boundp 'gnus-topic-menu)
1107 gnus-topic-menu gnus-topic-mode-map ""
1109 ["Toggle topics" gnus-topic-mode t]
1111 ["Copy..." gnus-topic-copy-group t]
1112 ["Move..." gnus-topic-move-group t]
1113 ["Remove" gnus-topic-remove-group t]
1114 ["Copy matching..." gnus-topic-copy-matching t]
1115 ["Move matching..." gnus-topic-move-matching t])
1117 ["Goto..." gnus-topic-jump-to-topic t]
1118 ["Show" gnus-topic-show-topic t]
1119 ["Hide" gnus-topic-hide-topic t]
1120 ["Delete" gnus-topic-delete t]
1121 ["Rename..." gnus-topic-rename t]
1122 ["Create..." gnus-topic-create-topic t]
1123 ["Mark" gnus-topic-mark-topic t]
1124 ["Indent" gnus-topic-indent t]
1125 ["Sort" gnus-topic-sort-topics t]
1126 ["Previous topic" gnus-topic-goto-previous-topic t]
1127 ["Next topic" gnus-topic-goto-next-topic t]
1128 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1129 ["Edit parameters" gnus-topic-edit-parameters t])
1130 ["List active" gnus-topic-list-active t]))))
1132 (defun gnus-topic-mode (&optional arg redisplay)
1133 "Minor mode for topicsifying Gnus group buffers."
1134 (interactive (list current-prefix-arg t))
1135 (when (eq major-mode 'gnus-group-mode)
1136 (make-local-variable 'gnus-topic-mode)
1137 (setq gnus-topic-mode
1138 (if (null arg) (not gnus-topic-mode)
1139 (> (prefix-numeric-value arg) 0)))
1140 ;; Infest Gnus with topics.
1141 (if (not gnus-topic-mode)
1142 (setq gnus-goto-missing-group-function nil)
1143 (when (gnus-visual-p 'topic-menu 'menu)
1144 (gnus-topic-make-menu-bar))
1145 (gnus-set-format 'topic t)
1146 (add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
1147 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1148 (set (make-local-variable 'gnus-group-prepare-function)
1149 'gnus-group-prepare-topics)
1150 (set (make-local-variable 'gnus-group-get-parameter-function)
1151 'gnus-group-topic-parameters)
1152 (set (make-local-variable 'gnus-group-goto-next-group-function)
1153 'gnus-topic-goto-next-group)
1154 (set (make-local-variable 'gnus-group-indentation-function)
1155 'gnus-topic-group-indentation)
1156 (set (make-local-variable 'gnus-group-update-group-function)
1157 'gnus-topic-update-topics-containing-group)
1158 (set (make-local-variable 'gnus-group-sort-alist-function)
1159 'gnus-group-sort-topic)
1160 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1161 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1162 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
1163 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1165 (setq gnus-topology-checked-p nil)
1166 ;; We check the topology.
1167 (when gnus-newsrc-alist
1168 (gnus-topic-check-topology))
1169 (gnus-run-hooks 'gnus-topic-mode-hook))
1170 ;; Remove topic infestation.
1171 (unless gnus-topic-mode
1172 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1173 (setq gnus-group-change-level-function nil)
1174 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1175 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1176 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1178 (gnus-group-list-groups))))
1180 (defun gnus-topic-select-group (&optional all)
1181 "Select this newsgroup.
1182 No article is selected automatically.
1183 If the group is opened, just switch the summary buffer.
1184 If ALL is non-nil, already read articles become readable.
1185 If ALL is a number, fetch this number of articles.
1187 If performed over a topic line, toggle folding the topic."
1189 (when (and (eobp) (not (gnus-group-group-name)))
1191 (if (gnus-group-topic-p)
1192 (let ((gnus-group-list-mode
1193 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1194 (gnus-topic-fold all)
1195 (gnus-dribble-touch))
1196 (gnus-group-select-group all)))
1198 (defun gnus-mouse-pick-topic (e)
1199 "Select the group or topic under the mouse pointer."
1202 (gnus-topic-read-group nil))
1204 (defun gnus-topic-expire-articles (topic)
1205 "Expire articles in this topic or group."
1206 (interactive (list (gnus-group-topic-name)))
1208 (call-interactively 'gnus-group-expire-articles)
1210 (gnus-message 5 "Expiring groups in %s..." topic)
1211 (let ((gnus-group-marked
1212 (mapcar (lambda (entry) (car (nth 2 entry)))
1213 (gnus-topic-find-groups topic gnus-level-killed t
1215 (gnus-group-expire-articles nil))
1216 (gnus-message 5 "Expiring groups in %s...done" topic))))
1218 (defun gnus-topic-catchup-articles (topic)
1219 "Catchup this topic or group.
1220 Also see `gnus-group-catchup'."
1221 (interactive (list (gnus-group-topic-name)))
1223 (call-interactively 'gnus-group-catchup-current)
1226 (mapcar (lambda (entry) (car (nth 2 entry)))
1227 (gnus-topic-find-groups topic gnus-level-killed t
1229 (buffer-read-only nil)
1230 (gnus-group-marked groups))
1231 (gnus-group-catchup-current)
1232 (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1234 (defun gnus-topic-read-group (&optional all no-article group)
1235 "Read news in this newsgroup.
1236 If the prefix argument ALL is non-nil, already read articles become
1237 readable. IF ALL is a number, fetch this number of articles. If the
1238 optional argument NO-ARTICLE is non-nil, no article will be
1239 auto-selected upon group entry. If GROUP is non-nil, fetch that
1242 If performed over a topic line, toggle folding the topic."
1244 (if (gnus-group-topic-p)
1245 (let ((gnus-group-list-mode
1246 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1247 (gnus-topic-fold all))
1248 (gnus-group-read-group all no-article group)))
1250 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1251 "Create a new TOPIC under PARENT.
1252 When used interactively, PARENT will be the topic under point."
1255 (read-string "New topic: ")
1256 (gnus-current-topic)))
1257 ;; Check whether this topic already exists.
1258 (when (gnus-topic-find-topology topic)
1259 (error "Topic already exists"))
1261 (setq parent (caar gnus-topic-topology)))
1262 (let ((top (cdr (gnus-topic-find-topology parent)))
1263 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1265 (error "No such parent topic: %s" parent))
1268 (while (and (cdr top)
1269 (not (equal (caaadr top) previous)))
1270 (setq top (cdr top)))
1271 (setcdr top (cons full-topic (cdr top))))
1272 (nconc top (list full-topic)))
1273 (unless (assoc topic gnus-topic-alist)
1274 (push (list topic) gnus-topic-alist)))
1275 (gnus-topic-enter-dribble)
1276 (gnus-group-list-groups)
1277 (gnus-topic-goto-topic topic))
1280 ;; 1. When the marked groups are overlapped with the process
1281 ;; region, the behavior of move or remove is not right.
1282 ;; 2. Can't process on several marked groups with a same name,
1283 ;; because gnus-group-marked only keeps one copy.
1285 (defun gnus-topic-move-group (n topic &optional copyp)
1286 "Move the next N groups to TOPIC.
1287 If COPYP, copy the groups instead."
1289 (list current-prefix-arg
1290 (gnus-completing-read "Move to topic" gnus-topic-alist nil t
1291 'gnus-topic-history)))
1292 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1293 gnus-group-marked t))
1294 (groups (gnus-group-process-prefix n))
1295 (topicl (assoc topic gnus-topic-alist))
1296 (start-topic (gnus-group-topic-name))
1297 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1299 (if (and (not groups) (not copyp) start-topic)
1300 (gnus-topic-move start-topic topic)
1303 (gnus-group-remove-mark g use-marked)
1305 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1307 (setcdr entry (gnus-delete-first g (cdr entry))))
1308 (nconc topicl (list g)))
1310 (gnus-topic-enter-dribble)
1312 (gnus-group-goto-group start-group)
1313 (gnus-topic-goto-topic start-topic))
1314 (gnus-group-list-groups))))
1316 (defun gnus-topic-remove-group (&optional n)
1317 "Remove the current group from the topic."
1319 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1320 gnus-group-marked t))
1321 (groups (gnus-group-process-prefix n)))
1324 (gnus-group-remove-mark group use-marked)
1325 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1326 (buffer-read-only nil))
1327 (when (and topicl group)
1329 (gnus-delete-first group topicl))
1330 (gnus-topic-update-topic)))
1332 (gnus-topic-enter-dribble)
1333 (gnus-group-position-point)))
1335 (defun gnus-topic-copy-group (n topic)
1336 "Copy the current group to a topic."
1338 (list current-prefix-arg
1339 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1340 (gnus-topic-move-group n topic t))
1342 (defun gnus-topic-kill-group (&optional n discard)
1343 "Kill the next N groups."
1345 (if (gnus-group-topic-p)
1346 (let ((topic (gnus-group-topic-name)))
1348 (gnus-topic-find-topology topic)
1349 (assoc topic gnus-topic-alist))
1350 gnus-topic-killed-topics)
1351 (gnus-topic-remove-topic nil t)
1352 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1353 (gnus-topic-enter-dribble))
1354 (gnus-group-kill-group n discard)
1355 (if (not (gnus-group-topic-p))
1356 (gnus-topic-update-topic)
1357 ;; Move up one line so that we update the right topic.
1359 (gnus-topic-update-topic)
1362 (defun gnus-topic-yank-group (&optional arg)
1363 "Yank the last topic."
1365 (if gnus-topic-killed-topics
1367 (or (gnus-group-topic-name)
1368 (gnus-topic-next-topic (gnus-current-topic))))
1369 (data (pop gnus-topic-killed-topics))
1372 (push alist gnus-topic-alist)
1373 (gnus-topic-create-topic
1374 (caar item) (gnus-topic-parent-topic previous) previous
1376 (gnus-topic-enter-dribble)
1377 (gnus-topic-goto-topic (caar item)))
1378 (let* ((prev (gnus-group-group-name))
1379 (gnus-topic-inhibit-change-level t)
1380 (gnus-group-indentation
1382 (* gnus-topic-indent-level
1384 (gnus-topic-goto-topic (gnus-current-topic))
1385 (gnus-group-topic-level))
1389 ;; We first yank the groups the normal way...
1390 (setq yanked (gnus-group-yank-group arg))
1391 ;; Then we enter the yanked groups into the topics they belong
1393 (setq alist (assoc (save-excursion
1395 (gnus-current-topic))
1397 (when (stringp yanked)
1398 (setq yanked (list yanked)))
1400 (nconc alist yanked)
1401 (if (not (cdr alist))
1402 (setcdr alist (nconc yanked (cdr alist)))
1404 (when (equal (cadr alist) prev)
1405 (setcdr alist (nconc yanked (cdr alist)))
1407 (setq alist (cdr alist))))))
1408 (gnus-topic-update-topic)))
1410 (defun gnus-topic-hide-topic (&optional permanent)
1411 "Hide the current topic.
1412 If PERMANENT, make it stay hidden in subsequent sessions as well."
1414 (when (gnus-current-topic)
1415 (gnus-topic-goto-topic (gnus-current-topic))
1419 (gnus-topic-find-topology (gnus-current-topic))))
1421 (gnus-topic-remove-topic nil nil)))
1423 (defun gnus-topic-show-topic (&optional permanent)
1424 "Show the hidden topic.
1425 If PERMANENT, make it stay shown in subsequent sessions as well."
1427 (when (gnus-group-topic-p)
1429 (gnus-topic-remove-topic t nil)
1431 (gnus-topic-find-topology
1432 (completing-read "Show topic: " gnus-topic-alist nil t))))
1433 (setcar (cddr (cadr topic)) nil)
1434 (setcar (cdr (cadr topic)) 'visible)
1435 (gnus-group-list-groups)))))
1437 (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
1438 "Mark all groups in the TOPIC with the process mark.
1439 If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
1440 (interactive (list (gnus-group-topic-name)
1442 (and current-prefix-arg t)))
1444 (call-interactively 'gnus-group-mark-group)
1446 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1447 (not non-recursive))))
1449 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1450 (gnus-info-group (nth 2 (pop groups)))))))))
1452 (defun gnus-topic-unmark-topic (topic &optional dummy non-recursive)
1453 "Remove the process mark from all groups in the TOPIC.
1454 If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
1455 (interactive (list (gnus-group-topic-name)
1457 (and current-prefix-arg t)))
1459 (call-interactively 'gnus-group-unmark-group)
1460 (gnus-topic-mark-topic topic t non-recursive)))
1462 (defun gnus-topic-get-new-news-this-topic (&optional n)
1463 "Check for new news in the current topic."
1465 (if (not (gnus-group-topic-p))
1466 (gnus-group-get-new-news-this-group n)
1467 (let* ((topic (gnus-group-topic-name))
1468 (data (cadr (gnus-topic-find-topology topic))))
1470 (gnus-topic-mark-topic topic nil (and n t))
1471 (gnus-group-get-new-news-this-group))
1472 (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1474 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1475 "Move all groups that match REGEXP to some topic."
1480 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1481 (read-string (format "Move to %s (regexp): " topic))))))
1482 (gnus-group-mark-regexp regexp)
1483 (gnus-topic-move-group nil topic copyp))
1485 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1486 "Copy all groups that match REGEXP to some topic."
1491 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1492 (read-string (format "Copy to %s (regexp): " topic))))))
1493 (gnus-topic-move-matching regexp topic t))
1495 (defun gnus-topic-delete (topic)
1497 (interactive (list (gnus-group-topic-name)))
1499 (error "No topic to be deleted"))
1500 (let ((entry (assoc topic gnus-topic-alist))
1501 (buffer-read-only nil))
1503 (error "Topic not empty"))
1504 ;; Delete if visible.
1505 (when (gnus-topic-goto-topic topic)
1507 ;; Remove from alist.
1508 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1509 ;; Remove from topology.
1510 (gnus-topic-find-topology topic nil nil 'delete)
1511 (gnus-dribble-touch)))
1513 (defun gnus-topic-rename (old-name new-name)
1516 (let ((topic (gnus-current-topic)))
1518 (read-string (format "Rename %s to: " topic) topic))))
1519 ;; Check whether the new name exists.
1520 (when (gnus-topic-find-topology new-name)
1521 (error "Topic '%s' already exists" new-name))
1522 ;; "nil" is an invalid name, for reasons I'd rather not go
1523 ;; into here. Trust me.
1524 (when (equal new-name "nil")
1525 (error "Invalid name: %s" nil))
1527 (let ((top (gnus-topic-find-topology old-name))
1528 (entry (assoc old-name gnus-topic-alist)))
1530 (setcar (cadr top) new-name))
1532 (setcar entry new-name))
1534 (gnus-dribble-touch)
1535 (gnus-group-list-groups)
1538 (defun gnus-topic-indent (&optional unindent)
1539 "Indent a topic -- make it a sub-topic of the previous topic.
1540 If UNINDENT, remove an indentation."
1543 (gnus-topic-unindent)
1544 (let* ((topic (gnus-current-topic))
1545 (parent (gnus-topic-previous-topic topic))
1546 (buffer-read-only nil))
1548 (error "Nothing to indent %s into" topic))
1550 (gnus-topic-goto-topic topic)
1551 (gnus-topic-kill-group)
1552 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1553 (gnus-topic-create-topic
1554 topic parent nil (cdar (car gnus-topic-killed-topics)))
1555 (pop gnus-topic-killed-topics)
1556 (or (gnus-topic-goto-topic topic)
1557 (gnus-topic-goto-topic parent))))))
1559 (defun gnus-topic-unindent ()
1562 (let* ((topic (gnus-current-topic))
1563 (parent (gnus-topic-parent-topic topic))
1564 (grandparent (gnus-topic-parent-topic parent)))
1566 (error "Nothing to indent %s into" topic))
1568 (gnus-topic-goto-topic topic)
1569 (gnus-topic-kill-group)
1570 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1571 (gnus-topic-create-topic
1572 topic grandparent (gnus-topic-next-topic parent)
1573 (cdar (car gnus-topic-killed-topics)))
1574 (pop gnus-topic-killed-topics)
1575 (gnus-topic-goto-topic topic))))
1577 (defun gnus-topic-list-active (&optional force)
1578 "List all groups that Gnus knows about in a topicsified fashion.
1579 If FORCE, always re-read the active file."
1582 (gnus-get-killed-groups))
1583 (gnus-topic-grok-active force)
1584 (let ((gnus-topic-topology gnus-topic-active-topology)
1585 (gnus-topic-alist gnus-topic-active-alist)
1586 gnus-killed-list gnus-zombie-list)
1587 (gnus-group-list-groups gnus-level-killed nil 1)))
1589 (defun gnus-topic-toggle-display-empty-topics ()
1590 "Show/hide topics that have no unread articles."
1592 (setq gnus-topic-display-empty-topics
1593 (not gnus-topic-display-empty-topics))
1594 (gnus-group-list-groups)
1595 (message "%s empty topics"
1596 (if gnus-topic-display-empty-topics
1597 "Showing" "Hiding")))
1599 ;;; Topic sorting functions
1601 (defun gnus-topic-edit-parameters (group)
1602 "Edit the group parameters of GROUP.
1603 If performed on a topic, edit the topic parameters instead."
1604 (interactive (list (gnus-group-group-name)))
1606 (gnus-group-edit-group-parameters group)
1607 (if (not (gnus-group-topic-p))
1608 (error "Nothing to edit on the current line")
1609 (let ((topic (gnus-group-topic-name)))
1611 (gnus-topic-parameters topic)
1612 (format "Editing the topic parameters for `%s'."
1615 (gnus-topic-set-parameters ,topic form)))))))
1617 (defun gnus-group-sort-topic (func reverse)
1618 "Sort groups in the topics according to FUNC and REVERSE."
1619 (let ((alist gnus-topic-alist))
1621 ;; !!!Sometimes nil elements sneak into the alist,
1622 ;; for some reason or other.
1623 (setcar alist (delq nil (car alist)))
1624 (setcar alist (delete "dummy.group" (car alist)))
1625 (gnus-topic-sort-topic (pop alist) func reverse))))
1627 (defun gnus-topic-sort-topic (topic func reverse)
1628 ;; Each topic only lists the name of the group, while
1629 ;; the sort predicates expect group infos as inputs.
1630 ;; So we first transform the group names into infos,
1631 ;; then sort, and then transform back into group names.
1635 (lambda (info) (gnus-info-group info))
1638 (lambda (group) (gnus-get-info group))
1641 ;; Do the reversal, if necessary.
1643 (setcdr topic (nreverse (cdr topic)))))
1645 (defun gnus-topic-sort-groups (func &optional reverse)
1646 "Sort the current topic according to FUNC.
1647 If REVERSE, reverse the sorting order."
1648 (interactive (list gnus-group-sort-function current-prefix-arg))
1649 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1650 (gnus-topic-sort-topic
1651 topic (gnus-make-sort-function func) reverse)
1652 (gnus-group-list-groups)))
1654 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1655 "Sort the current topic alphabetically by group name.
1656 If REVERSE, sort in reverse order."
1658 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1660 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1661 "Sort the current topic by number of unread articles.
1662 If REVERSE, sort in reverse order."
1664 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1666 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1667 "Sort the current topic by group level.
1668 If REVERSE, sort in reverse order."
1670 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1672 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1673 "Sort the current topic by group score.
1674 If REVERSE, sort in reverse order."
1676 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1678 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1679 "Sort the current topic by group rank.
1680 If REVERSE, sort in reverse order."
1682 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1684 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1685 "Sort the current topic alphabetically by backend name.
1686 If REVERSE, sort in reverse order."
1688 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1690 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1691 "Sort the current topic alphabetically by server name.
1692 If REVERSE, sort in reverse order."
1694 (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1696 (defun gnus-topic-sort-topics-1 (top reverse)
1699 (mapcar (gnus-byte-compile
1701 (gnus-topic-sort-topics-1 top ,reverse)))
1704 (string-lessp (caar t1) (caar t2)))))))
1705 (setcdr top (if reverse (reverse subtop) subtop))))
1708 (defun gnus-topic-sort-topics (&optional topic reverse)
1709 "Sort topics in TOPIC alphabetically by topic name.
1710 If REVERSE, reverse the sorting order."
1712 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1713 (gnus-current-topic))
1714 current-prefix-arg))
1715 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1716 gnus-topic-topology)))
1717 (gnus-topic-sort-topics-1 topic-topology reverse)
1718 (gnus-topic-enter-dribble)
1719 (gnus-group-list-groups)
1720 (gnus-topic-goto-topic topic)))
1722 (defun gnus-topic-move (current to)
1723 "Move the CURRENT topic to TO."
1726 (gnus-group-topic-name)
1727 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1728 (unless (and current to)
1729 (error "Can't find topic"))
1730 (let ((current-top (cdr (gnus-topic-find-topology current)))
1731 (to-top (cdr (gnus-topic-find-topology to))))
1733 (error "Can't find topic `%s'" current))
1735 (error "Can't find topic `%s'" to))
1736 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1737 (error "Can't move `%s' to its sub-level" current))
1738 (gnus-topic-find-topology current nil nil 'delete)
1739 (setcdr (last to-top) (list current-top))
1740 (gnus-topic-enter-dribble)
1741 (gnus-group-list-groups)
1742 (gnus-topic-goto-topic current)))
1744 (defun gnus-subscribe-topics (newsgroup)
1746 (let (match gnus-group-change-level-function)
1747 (dolist (topic (gnus-topic-list))
1748 (when (and (setq match (cdr (assq 'subscribe
1749 (gnus-topic-parameters topic))))
1750 (string-match match newsgroup))
1751 ;; Just subscribe the group.
1752 (gnus-subscribe-alphabetically newsgroup)
1753 ;; Add the group to the topic.
1754 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1755 ;; if this topic specifies a default level, use it
1756 (let ((subscribe-level (cdr (assq 'subscribe-level
1757 (gnus-topic-parameters topic)))))
1758 (when subscribe-level
1759 (gnus-group-change-level newsgroup subscribe-level
1760 gnus-level-default-subscribed)))
1764 (provide 'gnus-topic)
1766 ;;; arch-tag: bf176856-f30c-40f0-ae77-e41529a1134c
1767 ;;; gnus-topic.el ends here