1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Ilja Weis <kult@uni-paderborn.de>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 (eval-when-compile (require 'cl))
36 (defgroup gnus-topic nil
40 (defvar gnus-topic-mode nil
41 "Minor mode for Gnus group buffers.")
43 (defcustom gnus-topic-mode-hook nil
44 "Hook run in topic mode buffers."
48 (when (featurep 'xemacs)
49 (add-hook 'gnus-topic-mode-hook 'gnus-xmas-topic-menu-add))
51 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
52 "Format of topic lines.
53 It works along the same lines as a normal formatting string,
54 with some simple extensions.
56 %i Indentation based on topic level.
58 %v Nothing if the topic is visible, \"...\" otherwise.
59 %g Number of groups in the topic.
60 %a Number of unread articles in the groups in the topic.
61 %A Number of unread articles in the groups in the topic and its subtopics.
63 General format specifiers can also be used.
64 See Info node `(gnus)Formatting Variables'."
65 :link '(custom-manual "(gnus)Formatting Variables")
69 (defcustom gnus-topic-indent-level 2
70 "*How much each subtopic should be indented."
74 (defcustom gnus-topic-display-empty-topics t
75 "*If non-nil, display the topic lines even of topics that have no unread articles."
79 ;; Internal variables.
81 (defvar gnus-topic-active-topology nil)
82 (defvar gnus-topic-active-alist nil)
83 (defvar gnus-topic-unreads nil)
85 (defvar gnus-topology-checked-p nil
86 "Whether the topology has been checked in this session.")
88 (defvar gnus-topic-killed-topics nil)
89 (defvar gnus-topic-inhibit-change-level nil)
91 (defconst gnus-topic-line-format-alist
95 (?g number-of-groups ?d)
96 (?a (gnus-topic-articles-in-topic entries) ?d)
97 (?A total-number-of-articles ?d)
100 (defvar gnus-topic-line-format-spec nil)
102 ;;; Utility functions
104 (defun gnus-group-topic-name ()
105 "The name of the topic on the current line."
106 (let ((topic (get-text-property (point-at-bol) 'gnus-topic)))
107 (and topic (symbol-name topic))))
109 (defun gnus-group-topic-level ()
110 "The level of the topic on the current line."
111 (get-text-property (point-at-bol) 'gnus-topic-level))
113 (defun gnus-group-topic-unread ()
114 "The number of unread articles in topic on the current line."
115 (get-text-property (point-at-bol) 'gnus-topic-unread))
117 (defun gnus-topic-unread (topic)
118 "Return the number of unread articles in TOPIC."
119 (or (cdr (assoc topic gnus-topic-unreads))
122 (defun gnus-group-topic-p ()
123 "Return non-nil if the current line is a topic."
124 (gnus-group-topic-name))
126 (defun gnus-topic-visible-p ()
127 "Return non-nil if the current topic is visible."
128 (get-text-property (point-at-bol) 'gnus-topic-visible))
130 (defun gnus-topic-articles-in-topic (entries)
134 (when (numberp (setq number (car (pop entries))))
135 (incf total number)))
138 (defun gnus-group-topic (group)
139 "Return the topic GROUP is a member of."
140 (let ((alist gnus-topic-alist)
143 (when (member group (cdar alist))
144 (setq out (caar alist)
146 (setq alist (cdr alist)))
149 (defun gnus-group-parent-topic (group)
150 "Return the topic GROUP is member of by looking at the group buffer."
151 (with-current-buffer gnus-group-buffer
152 (if (gnus-group-goto-group group)
154 (gnus-group-topic group))))
156 (defun gnus-topic-goto-topic (topic)
158 (gnus-goto-char (text-property-any (point-min) (point-max)
159 'gnus-topic (intern topic)))))
161 (defun gnus-topic-jump-to-topic (topic)
164 (list (completing-read "Go to topic: "
165 (mapcar 'list (gnus-topic-list))
167 (let ((buffer-read-only nil))
168 (dolist (topic (gnus-current-topics topic))
169 (unless (gnus-topic-goto-topic topic)
170 (gnus-topic-goto-missing-topic topic)
171 (gnus-topic-display-missing-topic topic))))
172 (gnus-topic-goto-topic topic))
174 (defun gnus-current-topic ()
175 "Return the name of the current topic."
177 (or (get-text-property (point) 'gnus-topic)
179 (and (gnus-goto-char (previous-single-property-change
180 (point) 'gnus-topic))
181 (get-text-property (max (1- (point)) (point-min))
184 (symbol-name result))))
186 (defun gnus-current-topics (&optional topic)
187 "Return a list of all current topics, lowest in hierarchy first.
188 If TOPIC, start with that topic."
189 (let ((topic (or topic (gnus-current-topic)))
193 (setq topic (gnus-topic-parent-topic topic)))
196 (defun gnus-group-active-topic-p ()
197 "Say whether the current topic comes from the active topics."
198 (get-text-property (point-at-bol) 'gnus-active))
200 (defun gnus-topic-find-groups (topic &optional level all lowest recursive)
201 "Return entries for all visible groups in TOPIC.
202 If RECURSIVE is t, return groups in its subtopics too."
203 (let ((groups (cdr (assoc topic gnus-topic-alist)))
204 info clevel unread group params visible-groups entry active)
205 (setq lowest (or lowest 1))
206 (setq level (or level gnus-level-unsubscribed))
207 ;; We go through the newsrc to look for matches.
209 (when (setq group (pop groups))
210 (setq entry (gnus-group-entry group)
212 params (gnus-info-params info)
213 active (gnus-active group)
214 unread (or (car entry)
215 (and (not (equal group "dummy.group"))
217 (- (1+ (cdr active)) (car active))))
218 clevel (or (gnus-info-level info)
219 (if (member group gnus-zombie-list)
220 gnus-level-zombie gnus-level-killed))))
222 info ; nil means that the group is dead.
224 (>= clevel lowest) ; Is inside the level we want.
226 (if (or (eq unread t)
228 gnus-group-list-inactive-groups
230 (and gnus-list-groups-with-ticked-articles
231 (cdr (assq 'tick (gnus-info-marks info))))
232 ;; Has right readedness.
233 ;; Check for permanent visibility.
234 (and gnus-permanently-visible-groups
235 (string-match gnus-permanently-visible-groups group))
236 (memq 'visible params)
237 (cdr (assq 'visible params)))
238 ;; Add this group to the list of visible groups.
239 (push (or entry group) visible-groups)))
240 (setq visible-groups (nreverse visible-groups))
243 (setq recursive (cdr (gnus-topic-find-topology topic))))
244 (dolist (topic-topology (cdr recursive))
246 (nconc visible-groups
247 (gnus-topic-find-groups
248 (caar topic-topology)
249 level all lowest topic-topology)))))
252 (defun gnus-topic-goto-previous-topic (n)
253 "Go to the N'th previous topic."
255 (gnus-topic-goto-next-topic (- n)))
257 (defun gnus-topic-goto-next-topic (n)
258 "Go to the N'th next topic."
260 (let ((backward (< n 0))
262 (topic (gnus-current-topic)))
266 (gnus-topic-previous-topic topic)
267 (gnus-topic-next-topic topic))))
268 (gnus-topic-goto-topic topic)
271 (gnus-message 7 "No more topics"))
274 (defun gnus-topic-previous-topic (topic)
275 "Return the previous topic on the same level as TOPIC."
276 (let ((top (cddr (gnus-topic-find-topology
277 (gnus-topic-parent-topic topic)))))
278 (unless (equal topic (caaar top))
279 (while (and top (not (equal (caaadr top) topic)))
280 (setq top (cdr top)))
283 (defun gnus-topic-parent-topic (topic &optional topology)
284 "Return the parent of TOPIC."
286 (setq topology gnus-topic-topology))
287 (let ((parent (car (pop topology)))
290 (not (setq found (equal (caaar topology) topic)))
291 (not (setq result (gnus-topic-parent-topic
292 topic (car topology)))))
293 (setq topology (cdr topology)))
294 (or result (and found parent))))
296 (defun gnus-topic-next-topic (topic &optional previous)
297 "Return the next sibling of TOPIC."
298 (let ((parentt (cddr (gnus-topic-find-topology
299 (gnus-topic-parent-topic topic))))
302 (not (equal (caaar parentt) topic)))
303 (setq prev (caaar parentt)
304 parentt (cdr parentt)))
309 (defun gnus-topic-forward-topic (num)
310 "Go to the next topic on the same level as the current one."
311 (let* ((topic (gnus-current-topic))
312 (way (if (< num 0) 'gnus-topic-previous-topic
313 'gnus-topic-next-topic))
315 (while (and (not (zerop num))
316 (setq topic (funcall way topic)))
317 (when (gnus-topic-goto-topic topic)
320 (goto-char (point-max)))
323 (defun gnus-topic-find-topology (topic &optional topology level remove)
324 "Return the topology of TOPIC."
326 (setq topology gnus-topic-topology)
330 (if (equal (caar topology) topic)
333 (delq topology remove))
334 (cons level topology))
335 (setq topology (cdr topology))
337 (not (setq result (gnus-topic-find-topology
338 topic (car topology) (1+ level)
340 (setq topology (cdr topology)))
343 (defvar gnus-tmp-topics nil)
344 (defun gnus-topic-list (&optional topology)
345 "Return a list of all topics in the topology."
347 (setq topology gnus-topic-topology
348 gnus-tmp-topics nil))
349 (push (caar topology) gnus-tmp-topics)
350 (mapc 'gnus-topic-list (cdr topology))
353 ;;; Topic parameter jazz
355 (defun gnus-topic-parameters (topic)
356 "Return the parameters for TOPIC."
357 (let ((top (gnus-topic-find-topology topic)))
359 (nth 3 (cadr top)))))
361 (defun gnus-topic-set-parameters (topic parameters)
362 "Set the topic parameters of TOPIC to PARAMETERS."
363 (let ((top (gnus-topic-find-topology topic)))
365 (error "No such topic: %s" topic))
366 ;; We may have to extend if there is no parameters here
368 (unless (nthcdr 2 (cadr top))
369 (nconc (cadr top) (list nil)))
370 (unless (nthcdr 3 (cadr top))
371 (nconc (cadr top) (list nil)))
372 (setcar (nthcdr 3 (cadr top)) parameters)
374 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
376 (defun gnus-group-topic-parameters (group)
377 "Compute the group parameters for GROUP in topic mode.
378 Possibly inherit parameters from topics above GROUP."
379 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
381 (gnus-topic-hierarchical-parameters
382 ;; First we try to go to the group within the group buffer and find the
383 ;; topic for the group that way. This hopefully copes well with groups
384 ;; that are in more than one topic. Failing that (i.e. when the group
385 ;; isn't visible in the group buffer) we find a topic for the group via
387 (or (and (gnus-group-goto-group group)
388 (gnus-current-topic))
389 (gnus-group-topic group))
392 (defun gnus-topic-hierarchical-parameters (topic &optional group-params-list)
393 "Compute the topic parameters for TOPIC.
394 Possibly inherit parameters from topics above TOPIC.
395 If optional argument GROUP-PARAMS-LIST is non-nil, use it as the basis for
398 ;; We probably have lots of nil elements here, so we remove them.
399 ;; Probably faster than doing this "properly".
400 (delq nil (cons group-params-list
401 (mapcar 'gnus-topic-parameters
402 (gnus-current-topics topic)))))
404 ;; Now we have all the parameters, so we go through them
405 ;; and do inheritance in the obvious way.
407 (while (setq params (pop params-list))
408 (while (setq param (pop params))
410 (setq param (cons param t)))
411 (cond ((eq (car param) 'posting-style)
412 (let ((param (cdr param))
414 (while (setq elt (pop param))
415 (unless (assoc (car elt) posting-style)
416 (push elt posting-style)))))
418 (unless (assq (car param) out)
419 (push param out))))))
420 (and posting-style (push (cons 'posting-style posting-style) out)))
421 ;; Return the resulting parameter list.
424 ;;; General utility functions
426 (defun gnus-topic-enter-dribble ()
428 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
430 ;;; Generating group buffers
432 (defun gnus-group-prepare-topics (level &optional predicate lowest
433 regexp list-topic topic-level)
434 "List all newsgroups with unread articles of level LEVEL or lower.
435 Use the `gnus-group-topics' to sort the groups.
436 If PREDICTE is a function, list groups that the function returns non-nil;
437 if it is t, list groups that have no unread articles.
438 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
439 (set-buffer gnus-group-buffer)
440 (let ((buffer-read-only nil)
441 (lowest (or lowest 1))
443 (and gnus-group-listed-groups
444 (copy-sequence gnus-group-listed-groups))))
446 (gnus-update-format-specifications nil 'topic)
448 (when (or (not gnus-topic-alist)
449 (not gnus-topology-checked-p))
450 (gnus-topic-check-topology))
456 (when (or gnus-group-listed-groups
457 (and (>= level gnus-level-zombie)
458 (<= lowest gnus-level-zombie)))
459 (gnus-group-prepare-flat-list-dead
460 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
464 (when (or gnus-group-listed-groups
465 (and (>= level gnus-level-killed)
466 (<= lowest gnus-level-killed)))
467 (gnus-group-prepare-flat-list-dead
468 (setq gnus-killed-list (sort gnus-killed-list 'string<))
469 gnus-level-killed ?K regexp)
471 (unless gnus-killed-hashtb
472 (gnus-make-hashtable-from-killed))
473 (gnus-group-prepare-flat-list-dead
474 (gnus-remove-if (lambda (group)
475 (or (gnus-group-entry group)
476 (gnus-gethash group gnus-killed-hashtb)))
478 gnus-level-killed ?K regexp)))
482 (when (or (< lowest gnus-level-zombie)
483 gnus-group-listed-groups)
485 (let ((top (gnus-topic-find-topology list-topic)))
486 (gnus-topic-prepare-topic (cdr top) (car top)
487 (or topic-level level) predicate
489 (gnus-topic-prepare-topic gnus-topic-topology 0
490 (or topic-level level) predicate
492 (gnus-group-set-mode-line)
493 (setq gnus-group-list-mode (cons level predicate))
494 (gnus-run-hooks 'gnus-group-prepare-hook))))
496 (defun gnus-topic-prepare-topic (topicl level &optional list-level
499 "Insert TOPIC into the group buffer.
500 If SILENT, don't insert anything. Return the number of unread
501 articles in the topic and its subtopics."
502 (let* ((type (pop topicl))
503 (entries (gnus-topic-find-groups
505 (if gnus-group-listed-groups
508 (or predicate gnus-group-listed-groups
510 (gnus-topic-hierarchical-parameters
512 (if gnus-group-listed-groups 0 lowest)))
513 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
514 (gnus-group-indentation
515 (make-string (* gnus-topic-indent-level level) ? ))
516 (beg (progn (beginning-of-line) (point)))
517 (topicl (reverse topicl))
518 (all-entries entries)
519 (point-max (point-max))
522 info entry end active tick)
523 ;; Insert any sub-topics.
526 (gnus-topic-prepare-topic
527 (pop topicl) (1+ level) list-level predicate
528 (not visiblep) lowest regexp)))
531 ;; Insert all the groups that belong in this topic.
532 (while (setq entry (pop entries))
533 (when (if (stringp entry)
534 (gnus-group-prepare-logic
537 (or (not gnus-group-listed-groups)
538 (if (< list-level gnus-level-zombie) nil
540 (if (member entry gnus-zombie-list)
541 gnus-level-zombie gnus-level-killed)))
542 (and (<= entry-level list-level)
543 (>= entry-level lowest)))))
546 (string-match regexp entry))
548 (funcall regexp entry))
551 (setq info (nth 2 entry))
552 (gnus-group-prepare-logic
553 (gnus-info-group info)
554 (and (or (not gnus-group-listed-groups)
555 (let ((entry-level (gnus-info-level info)))
556 (and (<= entry-level list-level)
557 (>= entry-level lowest))))
558 (or (not (functionp predicate))
559 (funcall predicate info))
560 (or (not (stringp regexp))
561 (string-match regexp (gnus-info-group info))))))
565 (gnus-group-insert-group-line
566 entry (if (member entry gnus-zombie-list)
567 gnus-level-zombie gnus-level-killed)
568 nil (- (1+ (cdr (setq active (gnus-active entry))))
572 (when (setq info (nth 2 entry))
573 (gnus-group-insert-group-line
574 (gnus-info-group info)
575 (gnus-info-level info) (gnus-info-marks info)
576 (car entry) (gnus-info-method info)))))
577 (when (and (listp entry)
578 (numberp (car entry)))
579 (incf unread (car entry)))
583 ;; Insert the topic line.
584 (when (and (not silent)
585 (or gnus-topic-display-empty-topics ;We want empty topics
586 (not (zerop unread)) ;Non-empty
587 tick ;Ticked articles
588 (/= point-max (point-max)))) ;Unactivated groups
589 (gnus-extent-start-open (point))
590 (gnus-topic-insert-topic-line
592 (not (eq (nth 2 type) 'hidden))
593 level all-entries unread))
594 (gnus-topic-update-unreads (car type) unread)
595 (when gnus-group-update-tool-bar
596 (gnus-put-text-property beg end 'point-entered
597 'gnus-tool-bar-update)
598 (gnus-put-text-property beg end 'point-left
599 'gnus-tool-bar-update))
603 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
604 "Remove the current topic."
605 (let ((topic (gnus-group-topic-name))
606 (level (gnus-group-topic-level))
607 (beg (progn (beginning-of-line) (point)))
610 (while (and (zerop (forward-line 1))
611 (> (or (gnus-group-topic-level) (1+ level)) level)))
612 (delete-region beg (point))
613 ;; Do the change in this rather odd manner because it has been
614 ;; reported that some topics share parts of some lists, for some
615 ;; reason. I have been unable to determine why this is the
616 ;; case, but this hack seems to take care of things.
617 (let ((data (cadr (gnus-topic-find-topology topic))))
619 (list (if insert 'visible 'invisible)
623 (setq gnus-topic-alist
624 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
625 (gnus-topic-insert-topic topic in-level)))))
627 (defun gnus-topic-insert-topic (topic &optional level)
629 (gnus-group-prepare-topics
630 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
631 nil nil topic level))
633 (defun gnus-topic-fold (&optional insert topic)
634 "Remove/insert the current topic."
635 (let ((topic (or topic (gnus-group-topic-name))))
638 (if (not (gnus-group-active-topic-p))
639 (gnus-topic-remove-topic
640 (or insert (not (gnus-topic-visible-p))))
641 (let ((gnus-topic-topology gnus-topic-active-topology)
642 (gnus-topic-alist gnus-topic-active-alist)
643 (gnus-group-list-mode (cons 5 t)))
644 (gnus-topic-remove-topic
645 (or insert (not (gnus-topic-visible-p))) nil nil 9)
646 (gnus-topic-enter-dribble)))))))
648 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
650 (let* ((visible (if visiblep "" "..."))
651 (indentation (make-string (* gnus-topic-indent-level level) ? ))
652 (total-number-of-articles unread)
653 (number-of-groups (length entries))
654 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
656 (gnus-topic-update-unreads name unread)
660 (gnus-add-text-properties
663 (eval gnus-topic-line-format-spec))
664 (list 'gnus-topic (intern name)
665 'gnus-topic-level level
666 'gnus-topic-unread unread
667 'gnus-active active-topic
668 'gnus-topic-visible visiblep)))))
670 (defun gnus-topic-update-unreads (topic unreads)
671 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
673 (push (cons topic unreads) gnus-topic-unreads))
675 (defun gnus-topic-update-topics-containing-group (group)
676 "Update all topics that have GROUP as a member."
677 (when (and (eq major-mode 'gnus-group-mode)
680 (let ((alist gnus-topic-alist))
681 ;; This is probably not entirely correct. If a topic
682 ;; isn't shown, then it's not updated. But the updating
683 ;; should be performed in any case, since the topic's
684 ;; parent should be updated. Pfft.
686 (when (and (member group (cdar alist))
687 (gnus-topic-goto-topic (caar alist)))
688 (gnus-topic-update-topic-line (caar alist)))
691 (defun gnus-topic-update-topic ()
692 "Update all parent topics to the current group."
693 (when (and (eq major-mode 'gnus-group-mode)
695 (let ((group (gnus-group-group-name))
697 (buffer-read-only nil))
699 (gnus-get-info group)
700 (gnus-topic-goto-topic (gnus-current-topic)))
701 (gnus-topic-update-topic-line (gnus-group-topic-name))
704 (gnus-group-position-point)))))
706 (defun gnus-topic-goto-missing-group (group)
707 "Place point where GROUP is supposed to be inserted."
708 (let* ((topic (gnus-group-topic group))
709 (groups (cdr (assoc topic gnus-topic-alist)))
710 (g (cdr (member group groups)))
713 ;; Try to jump to a visible group.
715 (not (gnus-group-goto-group (car g) t)))
717 ;; It wasn't visible, so we try to see where to insert it.
719 (setq g (cdr (member group (reverse groups))))
720 (while (and g unfound)
721 (when (gnus-group-goto-group (pop g) t)
726 (not (gnus-topic-goto-missing-topic topic)))
727 (gnus-topic-display-missing-topic topic)))))
729 (defun gnus-topic-display-missing-topic (topic)
730 "Insert topic lines recursively for missing topics."
731 (let ((parent (gnus-topic-find-topology
732 (gnus-topic-parent-topic topic))))
734 (not (gnus-topic-goto-missing-topic (caadr parent))))
735 (gnus-topic-display-missing-topic (caadr parent))))
736 (gnus-topic-goto-missing-topic topic)
737 ;; Skip past all groups in the topic we're in.
738 (while (gnus-group-group-name)
740 (let* ((top (gnus-topic-find-topology topic))
741 (children (cddr top))
744 (entries (gnus-topic-find-groups
745 (car type) (car gnus-group-list-mode)
746 (cdr gnus-group-list-mode)))
749 (incf unread (gnus-topic-unread (caar (pop children)))))
750 (while (setq entry (pop entries))
751 (when (numberp (car entry))
752 (incf unread (car entry))))
753 (gnus-topic-insert-topic-line
754 topic t t (car (gnus-topic-find-topology topic)) nil unread)))
756 (defun gnus-topic-goto-missing-topic (topic)
757 (if (gnus-topic-goto-topic topic)
759 ;; Topic not displayed.
760 (let* ((top (gnus-topic-find-topology
761 (gnus-topic-parent-topic topic)))
762 (tp (reverse (cddr top))))
764 (gnus-topic-insert-topic-line
765 topic t t (car (gnus-topic-find-topology topic)) nil 0)
766 (while (not (equal (caaar tp) topic))
770 (not (gnus-topic-goto-topic (caaar tp))))
773 (gnus-topic-forward-topic 1)
774 (gnus-topic-goto-missing-topic (caadr top)))))
777 (defun gnus-topic-update-topic-line (topic-name &optional reads)
778 (let* ((top (gnus-topic-find-topology topic-name))
780 (children (cddr top))
781 (entries (gnus-topic-find-groups
782 (car type) (car gnus-group-list-mode)
783 (cdr gnus-group-list-mode)))
784 (parent (gnus-topic-parent-topic topic-name))
785 (all-entries entries)
787 old-unread entry new-unread)
788 (when (gnus-topic-goto-topic (car type))
789 ;; Tally all the groups that belong in this topic.
791 (setq unread (- (gnus-group-topic-unread) reads))
793 (incf unread (gnus-topic-unread (caar (pop children)))))
794 (while (setq entry (pop entries))
795 (when (numberp (car entry))
796 (incf unread (car entry)))))
797 (setq old-unread (gnus-group-topic-unread))
798 ;; Insert the topic line.
799 (gnus-topic-insert-topic-line
800 (car type) (gnus-topic-visible-p)
801 (not (eq (nth 2 type) 'hidden))
802 (gnus-group-topic-level) all-entries unread)
805 (setq new-unread (gnus-group-topic-unread)))
808 (gnus-topic-update-topic-line
810 (- (or old-unread 0) (or new-unread 0))))
813 (defun gnus-topic-group-indentation ()
815 (* gnus-topic-indent-level
818 (gnus-topic-goto-topic (gnus-current-topic))
819 (gnus-group-topic-level))
825 (gnus-add-shutdown 'gnus-topic-close 'gnus)
827 (defun gnus-topic-close ()
828 (setq gnus-topic-active-topology nil
829 gnus-topic-active-alist nil
830 gnus-topic-killed-topics nil
831 gnus-topology-checked-p nil))
833 (defun gnus-topic-check-topology ()
834 ;; The first time we set the topology to whatever we have
835 ;; gotten here, which can be rather random.
836 (unless gnus-topic-alist
837 (gnus-topic-init-alist))
839 (setq gnus-topology-checked-p t)
840 ;; Go through the topic alist and make sure that all topics
841 ;; are in the topic topology.
842 (let ((topics (gnus-topic-list))
843 (alist gnus-topic-alist)
846 (unless (member (caar alist) topics)
847 (nconc gnus-topic-topology
848 (list (list (list (caar alist) 'visible))))
850 (setq alist (cdr alist)))
852 (gnus-topic-enter-dribble))
853 ;; Conversely, go through the topology and make sure that all
854 ;; topologies have alists.
856 (unless (assoc (car topics) gnus-topic-alist)
857 (push (list (car topics)) gnus-topic-alist))
859 ;; Go through all living groups and make sure that
860 ;; they belong to some topic.
861 (let* ((tgroups (apply 'append (mapcar 'cdr gnus-topic-alist)))
862 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
863 (newsrc (cdr gnus-newsrc-alist))
866 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
867 (setcdr entry (list group))
868 (setq entry (cdr entry)))))
869 ;; Go through all topics and make sure they contain only living groups.
870 (let ((alist gnus-topic-alist)
872 (while (setq topic (pop alist))
874 (if (and (cadr topic)
875 (gnus-group-entry (cadr topic)))
876 (setq topic (cdr topic))
877 (setcdr topic (cddr topic)))))))
879 (defun gnus-topic-init-alist ()
880 "Initialize the topic structures."
881 (setq gnus-topic-topology
882 (cons (list "Gnus" 'visible)
883 (mapcar (lambda (topic)
884 (list (list (car topic) 'visible)))
886 (setq gnus-topic-alist
888 (mapcar (lambda (info) (gnus-info-group info))
889 (cdr gnus-newsrc-alist)))
891 (gnus-topic-enter-dribble))
895 (defun gnus-topic-clean-alist ()
896 "Remove bogus groups from the topic alist."
897 (let ((topic-alist gnus-topic-alist)
899 (unless gnus-killed-hashtb
900 (gnus-make-hashtable-from-killed))
901 (while (setq topic (pop topic-alist))
902 (let ((topic-name (pop topic))
903 group filtered-topic)
904 (while (setq group (pop topic))
905 (when (and (or (gnus-active group)
906 (gnus-info-method (gnus-get-info group)))
907 (not (gnus-gethash group gnus-killed-hashtb)))
908 (push group filtered-topic)))
909 (push (cons topic-name (nreverse filtered-topic)) result)))
910 (setq gnus-topic-alist (nreverse result))))
912 (defun gnus-topic-change-level (group level oldlevel &optional previous)
913 "Run when changing levels to enter/remove groups from topics."
914 (with-current-buffer gnus-group-buffer
915 (let ((buffer-read-only nil))
916 (unless gnus-topic-inhibit-change-level
917 (gnus-group-goto-group (or (car (nth 2 previous)) group))
918 (when (and gnus-topic-mode
920 (not gnus-topic-inhibit-change-level))
921 ;; Remove the group from the topics.
922 (if (and (< oldlevel gnus-level-zombie)
923 (>= level gnus-level-zombie))
924 (let ((alist gnus-topic-alist))
925 (while (gnus-group-goto-group group)
928 (when (member group (car alist))
929 (setcdr (car alist) (delete group (cdar alist))))
931 ;; If the group is subscribed we enter it into the topics.
932 (when (and (< level gnus-level-zombie)
933 (>= oldlevel gnus-level-zombie))
934 (let* ((prev (gnus-group-group-name))
935 (gnus-topic-inhibit-change-level t)
936 (gnus-group-indentation
938 (* gnus-topic-indent-level
940 (gnus-topic-goto-topic (gnus-current-topic))
941 (gnus-group-topic-level))
944 (yanked (list group))
946 ;; Then we enter the yanked groups into the topics
948 (when (setq alist (assoc (save-excursion
952 (caar gnus-topic-topology)))
955 (when (stringp yanked)
956 (setq yanked (list yanked)))
959 (if (not (cdr alist))
960 (setcdr alist (nconc yanked (cdr alist)))
961 (while (and (not end) (cdr alist))
962 (when (equal (cadr alist) prev)
963 (setcdr alist (nconc yanked (cdr alist)))
965 (setq alist (cdr alist)))
967 (nconc talist yanked))))))
968 (gnus-topic-update-topic))))))))
970 (defun gnus-topic-goto-next-group (group props)
971 "Go to group or the next group after group."
973 (if (not (memq 'gnus-topic props))
974 (goto-char (point-max))
975 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
976 (if (gnus-group-goto-group group)
978 ;; The group is no longer visible.
979 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
980 (after (cdr (member group (cdr list)))))
981 ;; First try to put point on a group after the current one.
983 (not (gnus-group-goto-group (car after))))
984 (setq after (cdr after)))
985 ;; Then try to put point on a group before point.
987 (setq after (cdr (member group (reverse (cdr list)))))
989 (not (gnus-group-goto-group (car after))))
990 (setq after (cdr after))))
991 ;; Finally, just put point on the topic.
993 (goto-char (point-min))
995 (gnus-topic-goto-topic (car list))
999 ;;; Topic-active functions
1001 (defun gnus-topic-grok-active (&optional force)
1002 "Parse all active groups and create topic structures for them."
1003 ;; First we make sure that we have really read the active file.
1005 (not gnus-topic-active-alist))
1007 ;; Get a list of all groups available.
1008 (mapatoms (lambda (g) (when (symbol-value g)
1009 (push (symbol-name g) groups)))
1011 (setq groups (sort groups 'string<))
1012 ;; Init the variables.
1013 (setq gnus-topic-active-topology (list (list "" 'visible)))
1014 (setq gnus-topic-active-alist nil)
1015 ;; Descend the top-level hierarchy.
1016 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
1017 ;; Set the top-level topic names to something nice.
1018 (setcar (car gnus-topic-active-topology) "Gnus active")
1019 (setcar (car gnus-topic-active-alist) "Gnus active"))))
1021 (defun gnus-topic-grok-active-1 (topology groups)
1022 (let* ((name (caar topology))
1023 (prefix (concat "^" (regexp-quote name)))
1024 tgroups ntopology group)
1026 (string-match prefix (setq group (car groups))))
1027 (if (not (string-match "\\." group (match-end 0)))
1028 ;; There are no further hierarchies here, so we just
1029 ;; enter this group into the list belonging to this
1031 (push (pop groups) tgroups)
1032 ;; New sub-hierarchy, so we add it to the topology.
1033 (nconc topology (list (setq ntopology
1034 (list (list (substring
1035 group 0 (match-end 0))
1037 ;; Descend the hierarchy.
1038 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
1039 ;; We remove the trailing "." from the topic name.
1041 (if (string-match "\\.$" name)
1042 (substring name 0 (match-beginning 0))
1044 ;; Add this topic and its groups to the topic alist.
1045 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
1046 (setcar (car topology) name)
1047 ;; We return the rest of the groups that didn't belong
1051 ;;; Topic mode, commands and keymap.
1053 (defvar gnus-topic-mode-map nil)
1054 (defvar gnus-group-topic-map nil)
1056 (unless gnus-topic-mode-map
1057 (setq gnus-topic-mode-map (make-sparse-keymap))
1059 ;; Override certain group mode keys.
1060 (gnus-define-keys gnus-topic-mode-map
1061 "=" gnus-topic-select-group
1062 "\r" gnus-topic-select-group
1063 " " gnus-topic-read-group
1064 "\C-c\C-x" gnus-topic-expire-articles
1065 "c" gnus-topic-catchup-articles
1066 "\C-k" gnus-topic-kill-group
1067 "\C-y" gnus-topic-yank-group
1068 "\M-g" gnus-topic-get-new-news-this-topic
1069 "AT" gnus-topic-list-active
1070 "Gp" gnus-topic-edit-parameters
1071 "#" gnus-topic-mark-topic
1072 "\M-#" gnus-topic-unmark-topic
1073 [tab] gnus-topic-indent
1074 [(meta tab)] gnus-topic-unindent
1075 "\C-i" gnus-topic-indent
1076 "\M-\C-i" gnus-topic-unindent
1077 gnus-mouse-2 gnus-mouse-pick-topic)
1079 ;; Define a new submap.
1080 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
1081 "#" gnus-topic-mark-topic
1082 "\M-#" gnus-topic-unmark-topic
1083 "n" gnus-topic-create-topic
1084 "m" gnus-topic-move-group
1085 "D" gnus-topic-remove-group
1086 "c" gnus-topic-copy-group
1087 "h" gnus-topic-hide-topic
1088 "s" gnus-topic-show-topic
1089 "j" gnus-topic-jump-to-topic
1090 "M" gnus-topic-move-matching
1091 "C" gnus-topic-copy-matching
1092 "\M-p" gnus-topic-goto-previous-topic
1093 "\M-n" gnus-topic-goto-next-topic
1094 "\C-i" gnus-topic-indent
1095 [tab] gnus-topic-indent
1096 "r" gnus-topic-rename
1097 "\177" gnus-topic-delete
1098 [delete] gnus-topic-delete
1099 "H" gnus-topic-toggle-display-empty-topics)
1101 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1102 "s" gnus-topic-sort-groups
1103 "a" gnus-topic-sort-groups-by-alphabet
1104 "u" gnus-topic-sort-groups-by-unread
1105 "l" gnus-topic-sort-groups-by-level
1106 "e" gnus-topic-sort-groups-by-server
1107 "v" gnus-topic-sort-groups-by-score
1108 "r" gnus-topic-sort-groups-by-rank
1109 "m" gnus-topic-sort-groups-by-method))
1111 (defun gnus-topic-make-menu-bar ()
1112 (unless (boundp 'gnus-topic-menu)
1114 gnus-topic-menu gnus-topic-mode-map ""
1116 ["Toggle topics" gnus-topic-mode t]
1118 ["Copy..." gnus-topic-copy-group t]
1119 ["Move..." gnus-topic-move-group t]
1120 ["Remove" gnus-topic-remove-group t]
1121 ["Copy matching..." gnus-topic-copy-matching t]
1122 ["Move matching..." gnus-topic-move-matching t])
1124 ["Goto..." gnus-topic-jump-to-topic t]
1125 ["Show" gnus-topic-show-topic t]
1126 ["Hide" gnus-topic-hide-topic t]
1127 ["Delete" gnus-topic-delete t]
1128 ["Rename..." gnus-topic-rename t]
1129 ["Create..." gnus-topic-create-topic t]
1130 ["Mark" gnus-topic-mark-topic t]
1131 ["Indent" gnus-topic-indent t]
1132 ["Sort" gnus-topic-sort-topics t]
1133 ["Previous topic" gnus-topic-goto-previous-topic t]
1134 ["Next topic" gnus-topic-goto-next-topic t]
1135 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1136 ["Edit parameters" gnus-topic-edit-parameters t])
1137 ["List active" gnus-topic-list-active t]))))
1139 (defun gnus-topic-mode (&optional arg redisplay)
1140 "Minor mode for topicsifying Gnus group buffers."
1141 ;; FIXME: Use define-minor-mode.
1142 (interactive (list current-prefix-arg t))
1143 (when (eq major-mode 'gnus-group-mode)
1144 (make-local-variable 'gnus-topic-mode)
1145 (setq gnus-topic-mode
1146 (if (null arg) (not gnus-topic-mode)
1147 (> (prefix-numeric-value arg) 0)))
1148 ;; Infest Gnus with topics.
1149 (if (not gnus-topic-mode)
1150 (setq gnus-goto-missing-group-function nil)
1151 (when (gnus-visual-p 'topic-menu 'menu)
1152 (gnus-topic-make-menu-bar))
1153 (gnus-set-format 'topic t)
1154 (add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
1155 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1156 (set (make-local-variable 'gnus-group-prepare-function)
1157 'gnus-group-prepare-topics)
1158 (set (make-local-variable 'gnus-group-get-parameter-function)
1159 'gnus-group-topic-parameters)
1160 (set (make-local-variable 'gnus-group-goto-next-group-function)
1161 'gnus-topic-goto-next-group)
1162 (set (make-local-variable 'gnus-group-indentation-function)
1163 'gnus-topic-group-indentation)
1164 (set (make-local-variable 'gnus-group-update-group-function)
1165 'gnus-topic-update-topics-containing-group)
1166 (set (make-local-variable 'gnus-group-sort-alist-function)
1167 'gnus-group-sort-topic)
1168 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1169 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1170 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
1171 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1173 (setq gnus-topology-checked-p nil)
1174 ;; We check the topology.
1175 (when gnus-newsrc-alist
1176 (gnus-topic-check-topology))
1177 (gnus-run-hooks 'gnus-topic-mode-hook))
1178 ;; Remove topic infestation.
1179 (unless gnus-topic-mode
1180 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1181 (setq gnus-group-change-level-function nil)
1182 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1183 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1184 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1186 (gnus-group-list-groups))))
1188 (defun gnus-topic-select-group (&optional all)
1189 "Select this newsgroup.
1190 No article is selected automatically.
1191 If the group is opened, just switch the summary buffer.
1192 If ALL is non-nil, already read articles become readable.
1194 If ALL is a positive number, fetch this number of the latest
1195 articles in the group. If ALL is a negative number, fetch this
1196 number of the earliest articles in the group.
1198 If performed over a topic line, toggle folding the topic."
1200 (when (and (eobp) (not (gnus-group-group-name)))
1202 (if (gnus-group-topic-p)
1203 (let ((gnus-group-list-mode
1204 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1205 (gnus-topic-fold all)
1206 (gnus-dribble-touch))
1207 (gnus-group-select-group all)))
1209 (defun gnus-mouse-pick-topic (e)
1210 "Select the group or topic under the mouse pointer."
1213 (gnus-topic-read-group nil))
1215 (defun gnus-topic-expire-articles (topic)
1216 "Expire articles in this topic or group."
1217 (interactive (list (gnus-group-topic-name)))
1219 (call-interactively 'gnus-group-expire-articles)
1221 (gnus-message 5 "Expiring groups in %s..." topic)
1222 (let ((gnus-group-marked
1223 (mapcar (lambda (entry) (car (nth 2 entry)))
1224 (gnus-topic-find-groups topic gnus-level-killed t
1226 (gnus-group-expire-articles nil))
1227 (gnus-message 5 "Expiring groups in %s...done" topic))))
1229 (defun gnus-topic-catchup-articles (topic)
1230 "Catchup this topic or group.
1231 Also see `gnus-group-catchup'."
1232 (interactive (list (gnus-group-topic-name)))
1234 (call-interactively 'gnus-group-catchup-current)
1237 (mapcar (lambda (entry) (car (nth 2 entry)))
1238 (gnus-topic-find-groups topic gnus-level-killed t
1240 (buffer-read-only nil)
1241 (gnus-group-marked groups))
1242 (gnus-group-catchup-current)
1243 (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1245 (defun gnus-topic-read-group (&optional all no-article group)
1246 "Read news in this newsgroup.
1247 If the prefix argument ALL is non-nil, already read articles become
1250 If ALL is a positive number, fetch this number of the latest
1251 articles in the group. If ALL is a negative number, fetch this
1252 number of the earliest articles in the group.
1254 If the optional argument NO-ARTICLE is non-nil, no article will
1255 be auto-selected upon group entry. If GROUP is non-nil, fetch
1258 If performed over a topic line, toggle folding the topic."
1260 (if (gnus-group-topic-p)
1261 (let ((gnus-group-list-mode
1262 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1263 (gnus-topic-fold all))
1264 (gnus-group-read-group all no-article group)))
1266 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1267 "Create a new TOPIC under PARENT.
1268 When used interactively, PARENT will be the topic under point."
1271 (read-string "New topic: ")
1272 (gnus-current-topic)))
1273 ;; Check whether this topic already exists.
1274 (when (gnus-topic-find-topology topic)
1275 (error "Topic already exists"))
1277 (setq parent (caar gnus-topic-topology)))
1278 (let ((top (cdr (gnus-topic-find-topology parent)))
1279 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1281 (error "No such parent topic: %s" parent))
1284 (while (and (cdr top)
1285 (not (equal (caaadr top) previous)))
1286 (setq top (cdr top)))
1287 (setcdr top (cons full-topic (cdr top))))
1288 (nconc top (list full-topic)))
1289 (unless (assoc topic gnus-topic-alist)
1290 (push (list topic) gnus-topic-alist)))
1291 (gnus-topic-enter-dribble)
1292 (gnus-group-list-groups)
1293 (gnus-topic-goto-topic topic))
1296 ;; 1. When the marked groups are overlapped with the process
1297 ;; region, the behavior of move or remove is not right.
1298 ;; 2. Can't process on several marked groups with a same name,
1299 ;; because gnus-group-marked only keeps one copy.
1301 (defun gnus-topic-move-group (n topic &optional copyp)
1302 "Move the next N groups to TOPIC.
1303 If COPYP, copy the groups instead."
1305 (list current-prefix-arg
1306 (gnus-completing-read "Move to topic" gnus-topic-alist nil t
1307 'gnus-topic-history)))
1308 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1309 gnus-group-marked t))
1310 (groups (gnus-group-process-prefix n))
1311 (topicl (assoc topic gnus-topic-alist))
1312 (start-topic (gnus-group-topic-name))
1313 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1315 (if (and (not groups) (not copyp) start-topic)
1316 (gnus-topic-move start-topic topic)
1318 (gnus-group-remove-mark g use-marked)
1320 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1322 (setcdr entry (gnus-delete-first g (cdr entry))))
1323 (nconc topicl (list g)))
1324 (gnus-topic-enter-dribble)
1326 (gnus-group-goto-group start-group)
1327 (gnus-topic-goto-topic start-topic))
1328 (gnus-group-list-groups))))
1330 (defun gnus-topic-remove-group (&optional n)
1331 "Remove the current group from the topic."
1333 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1334 gnus-group-marked t))
1335 (groups (gnus-group-process-prefix n)))
1338 (gnus-group-remove-mark group use-marked)
1339 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1340 (buffer-read-only nil))
1341 (when (and topicl group)
1343 (gnus-delete-first group topicl))
1344 (gnus-topic-update-topic)))
1346 (gnus-topic-enter-dribble)
1347 (gnus-group-position-point)))
1349 (defun gnus-topic-copy-group (n topic)
1350 "Copy the current group to a topic."
1352 (list current-prefix-arg
1353 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1354 (gnus-topic-move-group n topic t))
1356 (defun gnus-topic-kill-group (&optional n discard)
1357 "Kill the next N groups."
1359 (if (gnus-group-topic-p)
1360 (let ((topic (gnus-group-topic-name)))
1362 (gnus-topic-find-topology topic)
1363 (assoc topic gnus-topic-alist))
1364 gnus-topic-killed-topics)
1365 (gnus-topic-remove-topic nil t)
1366 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1367 (gnus-topic-enter-dribble))
1368 (gnus-group-kill-group n discard)
1369 (if (not (gnus-group-topic-p))
1370 (gnus-topic-update-topic)
1371 ;; Move up one line so that we update the right topic.
1373 (gnus-topic-update-topic)
1376 (defun gnus-topic-yank-group (&optional arg)
1377 "Yank the last topic."
1379 (if gnus-topic-killed-topics
1381 (or (gnus-group-topic-name)
1382 (gnus-topic-next-topic (gnus-current-topic))))
1383 (data (pop gnus-topic-killed-topics))
1386 (push alist gnus-topic-alist)
1387 (gnus-topic-create-topic
1388 (caar item) (gnus-topic-parent-topic previous) previous
1390 (gnus-topic-enter-dribble)
1391 (gnus-topic-goto-topic (caar item)))
1392 (let* ((prev (gnus-group-group-name))
1393 (gnus-topic-inhibit-change-level t)
1394 (gnus-group-indentation
1396 (* gnus-topic-indent-level
1398 (gnus-topic-goto-topic (gnus-current-topic))
1399 (gnus-group-topic-level))
1403 ;; We first yank the groups the normal way...
1404 (setq yanked (gnus-group-yank-group arg))
1405 ;; Then we enter the yanked groups into the topics they belong
1407 (setq alist (assoc (save-excursion
1409 (gnus-current-topic))
1411 (when (stringp yanked)
1412 (setq yanked (list yanked)))
1414 (nconc alist yanked)
1415 (if (not (cdr alist))
1416 (setcdr alist (nconc yanked (cdr alist)))
1418 (when (equal (cadr alist) prev)
1419 (setcdr alist (nconc yanked (cdr alist)))
1421 (setq alist (cdr alist))))))
1422 (gnus-topic-update-topic)))
1424 (defun gnus-topic-hide-topic (&optional permanent)
1425 "Hide the current topic.
1426 If PERMANENT, make it stay hidden in subsequent sessions as well."
1428 (when (gnus-current-topic)
1429 (gnus-topic-goto-topic (gnus-current-topic))
1433 (gnus-topic-find-topology (gnus-current-topic))))
1435 (gnus-topic-remove-topic nil nil)))
1437 (defun gnus-topic-show-topic (&optional permanent)
1438 "Show the hidden topic.
1439 If PERMANENT, make it stay shown in subsequent sessions as well."
1441 (when (gnus-group-topic-p)
1443 (gnus-topic-remove-topic t nil)
1445 (gnus-topic-find-topology
1446 (completing-read "Show topic: " gnus-topic-alist nil t))))
1447 (setcar (cddr (cadr topic)) nil)
1448 (setcar (cdr (cadr topic)) 'visible)
1449 (gnus-group-list-groups)))))
1451 (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
1452 "Mark all groups in the TOPIC with the process mark.
1453 If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
1454 (interactive (list (gnus-group-topic-name)
1456 (and current-prefix-arg t)))
1458 (call-interactively 'gnus-group-mark-group)
1460 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1461 (not non-recursive))))
1463 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1464 (gnus-info-group (nth 2 (pop groups)))))))))
1466 (defun gnus-topic-unmark-topic (topic &optional dummy non-recursive)
1467 "Remove the process mark from all groups in the TOPIC.
1468 If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
1469 (interactive (list (gnus-group-topic-name)
1471 (and current-prefix-arg t)))
1473 (call-interactively 'gnus-group-unmark-group)
1474 (gnus-topic-mark-topic topic t non-recursive)))
1476 (defun gnus-topic-get-new-news-this-topic (&optional n)
1477 "Check for new news in the current topic."
1479 (if (not (gnus-group-topic-p))
1480 (gnus-group-get-new-news-this-group n)
1481 (let* ((topic (gnus-group-topic-name))
1482 (data (cadr (gnus-topic-find-topology topic))))
1484 (gnus-topic-mark-topic topic nil (and n t))
1485 (gnus-group-get-new-news-this-group))
1486 (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1488 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1489 "Move all groups that match REGEXP to some topic."
1494 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1495 (read-string (format "Move to %s (regexp): " topic))))))
1496 (gnus-group-mark-regexp regexp)
1497 (gnus-topic-move-group nil topic copyp))
1499 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1500 "Copy all groups that match REGEXP to some topic."
1505 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1506 (read-string (format "Copy to %s (regexp): " topic))))))
1507 (gnus-topic-move-matching regexp topic t))
1509 (defun gnus-topic-delete (topic)
1511 (interactive (list (gnus-group-topic-name)))
1513 (error "No topic to be deleted"))
1514 (let ((entry (assoc topic gnus-topic-alist))
1515 (buffer-read-only nil))
1517 (error "Topic not empty"))
1518 ;; Delete if visible.
1519 (when (gnus-topic-goto-topic topic)
1521 ;; Remove from alist.
1522 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1523 ;; Remove from topology.
1524 (gnus-topic-find-topology topic nil nil 'delete)
1525 (gnus-dribble-touch)))
1527 (defun gnus-topic-rename (old-name new-name)
1530 (let ((topic (gnus-current-topic)))
1532 (read-string (format "Rename %s to: " topic) topic))))
1533 ;; Check whether the new name exists.
1534 (when (gnus-topic-find-topology new-name)
1535 (error "Topic '%s' already exists" new-name))
1536 ;; "nil" is an invalid name, for reasons I'd rather not go
1537 ;; into here. Trust me.
1538 (when (equal new-name "nil")
1539 (error "Invalid name: %s" nil))
1541 (let ((top (gnus-topic-find-topology old-name))
1542 (entry (assoc old-name gnus-topic-alist)))
1544 (setcar (cadr top) new-name))
1546 (setcar entry new-name))
1548 (gnus-dribble-touch)
1549 (gnus-group-list-groups)
1552 (defun gnus-topic-indent (&optional unindent)
1553 "Indent a topic -- make it a sub-topic of the previous topic.
1554 If UNINDENT, remove an indentation."
1557 (gnus-topic-unindent)
1558 (let* ((topic (gnus-current-topic))
1559 (parent (gnus-topic-previous-topic topic))
1560 (buffer-read-only nil))
1562 (error "Nothing to indent %s into" topic))
1564 (gnus-topic-goto-topic topic)
1565 (gnus-topic-kill-group)
1566 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1567 (gnus-topic-create-topic
1568 topic parent nil (cdar (car gnus-topic-killed-topics)))
1569 (pop gnus-topic-killed-topics)
1570 (or (gnus-topic-goto-topic topic)
1571 (gnus-topic-goto-topic parent))))))
1573 (defun gnus-topic-unindent ()
1576 (let* ((topic (gnus-current-topic))
1577 (parent (gnus-topic-parent-topic topic))
1578 (grandparent (gnus-topic-parent-topic parent)))
1580 (error "Nothing to indent %s into" topic))
1582 (gnus-topic-goto-topic topic)
1583 (gnus-topic-kill-group)
1584 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1585 (gnus-topic-create-topic
1586 topic grandparent (gnus-topic-next-topic parent)
1587 (cdar (car gnus-topic-killed-topics)))
1588 (pop gnus-topic-killed-topics)
1589 (gnus-topic-goto-topic topic))))
1591 (defun gnus-topic-list-active (&optional force)
1592 "List all groups that Gnus knows about in a topicsified fashion.
1593 If FORCE, always re-read the active file."
1596 (gnus-get-killed-groups))
1597 (gnus-topic-grok-active force)
1598 (let ((gnus-topic-topology gnus-topic-active-topology)
1599 (gnus-topic-alist gnus-topic-active-alist)
1600 gnus-killed-list gnus-zombie-list)
1601 (gnus-group-list-groups gnus-level-killed nil 1)))
1603 (defun gnus-topic-toggle-display-empty-topics ()
1604 "Show/hide topics that have no unread articles."
1606 (setq gnus-topic-display-empty-topics
1607 (not gnus-topic-display-empty-topics))
1608 (gnus-group-list-groups)
1609 (message "%s empty topics"
1610 (if gnus-topic-display-empty-topics
1611 "Showing" "Hiding")))
1613 ;;; Topic sorting functions
1615 (defun gnus-topic-edit-parameters (group)
1616 "Edit the group parameters of GROUP.
1617 If performed on a topic, edit the topic parameters instead."
1618 (interactive (list (gnus-group-group-name)))
1620 (gnus-group-edit-group-parameters group)
1621 (if (not (gnus-group-topic-p))
1622 (error "Nothing to edit on the current line")
1623 (let ((topic (gnus-group-topic-name)))
1625 (gnus-topic-parameters topic)
1626 (format "Editing the topic parameters for `%s'."
1629 (gnus-topic-set-parameters ,topic form)))))))
1631 (defun gnus-group-sort-topic (func reverse)
1632 "Sort groups in the topics according to FUNC and REVERSE."
1633 (let ((alist gnus-topic-alist))
1635 ;; !!!Sometimes nil elements sneak into the alist,
1636 ;; for some reason or other.
1637 (setcar alist (delq nil (car alist)))
1638 (setcar alist (delete "dummy.group" (car alist)))
1639 (gnus-topic-sort-topic (pop alist) func reverse))))
1641 (defun gnus-topic-sort-topic (topic func reverse)
1642 ;; Each topic only lists the name of the group, while
1643 ;; the sort predicates expect group infos as inputs.
1644 ;; So we first transform the group names into infos,
1645 ;; then sort, and then transform back into group names.
1649 (lambda (info) (gnus-info-group info))
1652 (lambda (group) (gnus-get-info group))
1655 ;; Do the reversal, if necessary.
1657 (setcdr topic (nreverse (cdr topic)))))
1659 (defun gnus-topic-sort-groups (func &optional reverse)
1660 "Sort the current topic according to FUNC.
1661 If REVERSE, reverse the sorting order."
1662 (interactive (list gnus-group-sort-function current-prefix-arg))
1663 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1664 (gnus-topic-sort-topic
1665 topic (gnus-make-sort-function func) reverse)
1666 (gnus-group-list-groups)))
1668 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1669 "Sort the current topic alphabetically by group name.
1670 If REVERSE, sort in reverse order."
1672 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1674 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1675 "Sort the current topic by number of unread articles.
1676 If REVERSE, sort in reverse order."
1678 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1680 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1681 "Sort the current topic by group level.
1682 If REVERSE, sort in reverse order."
1684 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1686 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1687 "Sort the current topic by group score.
1688 If REVERSE, sort in reverse order."
1690 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1692 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1693 "Sort the current topic by group rank.
1694 If REVERSE, sort in reverse order."
1696 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1698 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1699 "Sort the current topic alphabetically by backend name.
1700 If REVERSE, sort in reverse order."
1702 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1704 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1705 "Sort the current topic alphabetically by server name.
1706 If REVERSE, sort in reverse order."
1708 (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1710 (defun gnus-topic-sort-topics-1 (top reverse)
1713 (mapcar (gnus-byte-compile
1715 (gnus-topic-sort-topics-1 top ,reverse)))
1718 (string-lessp (caar t1) (caar t2)))))))
1719 (setcdr top (if reverse (reverse subtop) subtop))))
1722 (defun gnus-topic-sort-topics (&optional topic reverse)
1723 "Sort topics in TOPIC alphabetically by topic name.
1724 If REVERSE, reverse the sorting order."
1726 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1727 (gnus-current-topic))
1728 current-prefix-arg))
1729 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1730 gnus-topic-topology)))
1731 (gnus-topic-sort-topics-1 topic-topology reverse)
1732 (gnus-topic-enter-dribble)
1733 (gnus-group-list-groups)
1734 (gnus-topic-goto-topic topic)))
1736 (defun gnus-topic-move (current to)
1737 "Move the CURRENT topic to TO."
1740 (gnus-group-topic-name)
1741 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1742 (unless (and current to)
1743 (error "Can't find topic"))
1744 (let ((current-top (cdr (gnus-topic-find-topology current)))
1745 (to-top (cdr (gnus-topic-find-topology to))))
1747 (error "Can't find topic `%s'" current))
1749 (error "Can't find topic `%s'" to))
1750 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1751 (error "Can't move `%s' to its sub-level" current))
1752 (gnus-topic-find-topology current nil nil 'delete)
1753 (setcdr (last to-top) (list current-top))
1754 (gnus-topic-enter-dribble)
1755 (gnus-group-list-groups)
1756 (gnus-topic-goto-topic current)))
1758 (defun gnus-subscribe-topics (newsgroup)
1760 (let (match gnus-group-change-level-function)
1761 (dolist (topic (gnus-topic-list))
1762 (when (and (setq match (cdr (assq 'subscribe
1763 (gnus-topic-parameters topic))))
1764 (string-match match newsgroup))
1765 ;; Just subscribe the group.
1766 (gnus-subscribe-alphabetically newsgroup)
1767 ;; Add the group to the topic.
1768 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1769 ;; if this topic specifies a default level, use it
1770 (let ((subscribe-level (cdr (assq 'subscribe-level
1771 (gnus-topic-parameters topic)))))
1772 (when subscribe-level
1773 (gnus-group-change-level newsgroup subscribe-level
1774 gnus-level-default-subscribed)))
1778 (provide 'gnus-topic)
1780 ;;; gnus-topic.el ends here