1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Ilja Weis <kult@uni-paderborn.de>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
32 (defvar gnus-topic-mode nil
33 "Minor mode for Gnus group buffers.")
35 (defvar gnus-topic-mode-hook nil
36 "Hook run in topic mode buffers.")
38 (defvar gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
39 "Format of topic lines.
40 It works along the same lines as a normal formatting string,
41 with some simple extensions.
43 %i Indentation based on topic level.
45 %v Nothing if the topic is visible, \"...\" otherwise.
46 %g Number of groups in the topic.
47 %a Number of unread articles in the groups in the topic.
48 %A Number of unread articles in the groups in the topic and its subtopics.
51 (defvar gnus-topic-unique t
52 "*If non-nil, each group will only belong to one topic.")
54 (defvar gnus-topic-indent-level 2
55 "*How much each subtopic should be indented.")
57 ;; Internal variables.
59 (defvar gnus-topic-killed-topics nil)
60 (defvar gnus-topic-inhibit-change-level nil)
63 (defconst gnus-topic-line-format-alist
67 (?g number-of-groups ?d)
68 (?a (gnus-topic-articles-in-topic entries) ?d)
69 (?A total-number-of-articles ?d)
72 (defvar gnus-topic-line-format-spec nil)
73 (defvar gnus-topic-active-topology nil)
74 (defvar gnus-topic-active-alist nil)
78 (defun gnus-group-topic-name ()
79 "The name of the topic on the current line."
80 (get-text-property (gnus-point-at-bol) 'gnus-topic))
82 (defun gnus-group-topic-level ()
83 "The level of the topic on the current line."
84 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
86 (defun gnus-topic-init-alist ()
87 "Initialize the topic structures."
88 (setq gnus-topic-topology
89 (cons (list "Gnus" 'visible)
90 (mapcar (lambda (topic)
91 (list (list (car topic) 'visible)))
93 (setq gnus-topic-alist
95 (mapcar (lambda (info) (gnus-info-group info))
96 (cdr gnus-newsrc-alist)))
98 (gnus-topic-enter-dribble))
100 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
101 "List all newsgroups with unread articles of level LEVEL or lower, and
102 use the `gnus-group-topics' to sort the groups.
103 If ALL is non-nil, list groups that have no unread articles.
104 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
105 (set-buffer gnus-group-buffer)
106 (let ((buffer-read-only nil)
107 (lowest (or lowest 1))
114 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
115 (gnus-group-prepare-flat-list-dead
116 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
120 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
121 (gnus-group-prepare-flat-list-dead
122 (setq gnus-killed-list (sort gnus-killed-list 'string<))
127 (when (< lowest gnus-level-zombie)
129 (let ((top (gnus-topic-find-topology list-topic)))
130 (gnus-topic-prepare-topic (cdr top) (car top)
131 (or topic-level level) all))
132 (gnus-topic-prepare-topic gnus-topic-topology 0
133 (or topic-level level) all))))
135 (gnus-group-set-mode-line)
136 (setq gnus-group-list-mode (cons level all))
137 (run-hooks 'gnus-group-prepare-hook))
139 (defun gnus-topic-prepare-topic (topic level &optional list-level all silent)
140 "Insert TOPIC into the group buffer.
141 If SILENT, don't insert anything. Return the number of unread
142 articles in the topic and its subtopics."
143 (let* ((type (pop topic))
144 (entries (gnus-topic-find-groups (car type) list-level all))
145 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
146 (gnus-group-indentation
147 (make-string (* gnus-topic-indent-level level) ? ))
148 (beg (progn (beginning-of-line) (point)))
149 (topic (reverse topic))
150 (all-entries entries)
152 info entry end active)
153 ;; Insert any sub-topics.
156 (gnus-topic-prepare-topic
157 (pop topic) (1+ level) list-level all
161 ;; Insert all the groups that belong in this topic.
162 (while (setq entry (pop entries))
166 (gnus-group-insert-group-line
167 entry (if (member entry gnus-zombie-list) 8 9)
168 nil (- (1+ (cdr (setq active (gnus-active entry))))
171 (when (setq info (nth 2 entry))
172 (gnus-group-insert-group-line
173 (gnus-info-group info)
174 (gnus-info-level info) (gnus-info-marks info)
175 (car entry) (gnus-info-method info)))))
176 (when (and (listp entry)
177 (numberp (car entry)))
178 (incf unread (car entry))))
180 ;; Insert the topic line.
182 (gnus-extent-start-open (point))
183 (gnus-topic-insert-topic-line
185 (not (eq (nth 2 type) 'hidden))
186 level all-entries unread))
190 (defun gnus-topic-find-groups (topic &optional level all)
191 "Return entries for all visible groups in TOPIC."
192 (let ((groups (cdr (assoc topic gnus-topic-alist)))
193 info clevel unread group lowest params visible-groups entry active)
194 (setq lowest (or lowest 1))
195 (setq level (or level 7))
196 ;; We go through the newsrc to look for matches.
198 (setq entry (gnus-gethash (setq group (pop groups)) gnus-newsrc-hashtb)
200 params (gnus-info-params info)
201 active (gnus-active group)
202 unread (or (car entry)
203 (and (not (equal group "dummy.group"))
205 (- (1+ (cdr active)) (car active))))
206 clevel (or (gnus-info-level info)
207 (if (member group gnus-zombie-list) 8 9)))
209 unread ; nil means that the group is dead.
211 (>= clevel lowest) ; Is inside the level we want.
213 (and gnus-group-list-inactive-groups
216 (and gnus-list-groups-with-ticked-articles
217 (cdr (assq 'tick (gnus-info-marks info))))
218 ; Has right readedness.
219 ;; Check for permanent visibility.
220 (and gnus-permanently-visible-groups
221 (string-match gnus-permanently-visible-groups group))
222 (memq 'visible params)
223 (cdr (assq 'visible params)))
224 ;; Add this group to the list of visible groups.
225 (push (or entry group) visible-groups)))
226 (nreverse visible-groups)))
228 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
229 "Remove the current topic."
230 (let ((topic (gnus-group-topic-name))
231 (level (gnus-group-topic-level))
232 (beg (progn (beginning-of-line) (point)))
235 (while (and (zerop (forward-line 1))
236 (> (or (gnus-group-topic-level) (1+ level)) level)))
237 (delete-region beg (point))
238 (setcar (cdadr (gnus-topic-find-topology topic))
239 (if insert 'visible 'invisible))
241 (setcdr (cdadr (gnus-topic-find-topology topic))
244 (gnus-topic-insert-topic topic in-level)))))
246 (defun gnus-topic-insert-topic (topic &optional level)
248 (gnus-group-prepare-topics
249 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
250 nil nil topic level))
252 (defun gnus-topic-fold (&optional insert)
253 "Remove/insert the current topic."
254 (let ((topic (gnus-group-topic-name)))
257 (if (not (gnus-group-active-topic-p))
258 (gnus-topic-remove-topic
259 (or insert (not (gnus-topic-visible-p))))
260 (let ((gnus-topic-topology gnus-topic-active-topology)
261 (gnus-topic-alist gnus-topic-active-alist)
262 (gnus-group-list-mode (cons 5 t)))
263 (gnus-topic-remove-topic
264 (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
266 (defun gnus-group-topic-p ()
267 "Return non-nil if the current line is a topic."
268 (get-text-property (gnus-point-at-bol) 'gnus-topic))
270 (defun gnus-topic-visible-p ()
271 "Return non-nil if the current topic is visible."
272 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
274 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
276 (let* ((visible (if visiblep "" "..."))
277 (indentation (make-string (* gnus-topic-indent-level level) ? ))
278 (total-number-of-articles unread)
279 (number-of-groups (length entries))
280 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
286 (eval gnus-topic-line-format-spec)
287 (gnus-topic-remove-excess-properties))
288 (list 'gnus-topic name
289 'gnus-topic-level level
290 'gnus-active active-topic
291 'gnus-topic-visible visiblep))))
293 (defun gnus-topic-previous-topic (topic)
294 "Return the previous topic on the same level as TOPIC."
295 (let ((top (cddr (gnus-topic-find-topology
296 (gnus-topic-parent-topic topic)))))
297 (unless (equal topic (caaar top))
298 (while (and top (not (equal (caaadr top) topic)))
299 (setq top (cdr top)))
302 (defun gnus-topic-parent-topic (topic &optional topology)
303 "Return the parent of TOPIC."
305 (setq topology gnus-topic-topology))
306 (let ((parent (car (pop topology)))
309 (not (setq found (equal (caaar topology) topic)))
310 (not (setq result (gnus-topic-parent-topic topic
312 (setq topology (cdr topology)))
313 (or result (and found parent))))
315 (defun gnus-topic-find-topology (topic &optional topology level remove)
316 "Return the topology of TOPIC."
318 (setq topology gnus-topic-topology)
322 (if (equal (caar topology) topic)
325 (delq topology remove))
326 (cons level topology))
327 (setq topology (cdr topology))
329 (not (setq result (gnus-topic-find-topology
330 topic (car topology) (1+ level)
332 (setq topology (cdr topology)))
335 (defun gnus-topic-check-topology ()
336 ;; The first time we set the topology to whatever we have
337 ;; gotten here, which can be rather random.
338 (unless gnus-topic-alist
339 (gnus-topic-init-alist))
341 (let ((topics (gnus-topic-list))
342 (alist gnus-topic-alist)
345 (unless (member (caar alist) topics)
346 (nconc gnus-topic-topology
347 (list (list (list (caar alist) 'visible))))
349 (setq alist (cdr alist)))
351 (gnus-topic-enter-dribble)))
352 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
354 (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
355 (newsrc gnus-newsrc-alist)
358 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
359 (setcdr entry (cons group (cdr entry)))))))
361 (defvar gnus-tmp-topics nil)
362 (defun gnus-topic-list (&optional topology)
364 (setq topology gnus-topic-topology
365 gnus-tmp-topics nil))
366 (push (caar topology) gnus-tmp-topics)
367 (mapcar 'gnus-topic-list (cdr topology))
370 (defun gnus-topic-enter-dribble ()
372 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
374 (defun gnus-topic-articles-in-topic (entries)
378 (when (numberp (setq number (car (pop entries))))
379 (incf total number)))
382 (defun gnus-group-parent-topic ()
383 "Return the topic the current group belongs in."
384 (let ((group (gnus-group-group-name)))
386 (gnus-group-topic group)
387 (gnus-group-topic-name))))
389 (defun gnus-group-topic (group)
390 "Return the topic GROUP is a member of."
391 (let ((alist gnus-topic-alist)
394 (when (member group (cdar alist))
395 (setq out (caar alist)
397 (setq alist (cdr alist)))
400 (defun gnus-topic-goto-topic (topic)
401 (let ((orig (point)))
402 (goto-char (point-min))
403 (while (and (not (equal topic (gnus-group-topic-name)))
404 (zerop (forward-line 1))))
405 (or (gnus-group-topic-name)
410 (defun gnus-topic-update-topic ()
411 "Update all parent topics to the current group."
412 (when (and (eq major-mode 'gnus-group-mode)
414 (let ((group (gnus-group-group-name)))
415 (gnus-topic-goto-topic (gnus-group-parent-topic))
416 (gnus-topic-update-topic-line)
417 (gnus-group-goto-group group)
418 (gnus-group-position-point))))
420 (defun gnus-topic-goto-missing-group (group)
421 "Place point where GROUP is supposed to be inserted."
422 (let* ((topic (gnus-group-topic group))
423 (groups (cdr (assoc topic gnus-topic-alist)))
424 (g (cdr (member group groups)))
426 (while (and g unfound)
427 (when (gnus-group-goto-group (pop g))
431 (setq g (cdr (member group (reverse groups))))
432 (while (and g unfound)
433 (when (gnus-group-goto-group (pop g))
437 (gnus-topic-goto-topic topic)
440 (defun gnus-topic-update-topic-line (&optional topic level)
442 (setq topic gnus-topic-topology)
444 (let* ((type (pop topic))
445 (buffer-read-only nil)
446 (entries (gnus-topic-find-groups
447 (car type) (car gnus-group-list-mode)
448 (cdr gnus-group-list-mode)))
449 (visiblep (eq (nth 1 type) 'visible))
450 (all-entries entries)
453 ;; Tally any sub-topics.
455 (incf unread (gnus-topic-update-topic-line (pop topic) (1+ level))))
456 ;; Tally all the groups that belong in this topic.
457 (while (setq info (nth 2 (setq entry (pop entries))))
458 (when (numberp (car entry))
459 (incf unread (car entry))))
460 ;; Insert the topic line.
461 (when (gnus-topic-goto-topic (car type))
462 (gnus-topic-insert-topic-line
464 (not (eq (nth 2 type) 'hidden))
465 level all-entries unread)
469 (defun gnus-topic-grok-active (&optional force)
470 "Parse all active groups and create topic structures for them."
471 ;; First we make sure that we have really read the active file.
473 (not gnus-topic-active-alist))
475 (not (member gnus-select-method gnus-have-read-active-file)))
476 (let ((gnus-read-active-file t))
477 (gnus-read-active-file)))
478 (let (topology groups alist)
479 ;; Get a list of all groups available.
480 (mapatoms (lambda (g) (when (symbol-value g)
481 (push (symbol-name g) groups)))
483 (setq groups (sort groups 'string<))
484 ;; Init the variables.
485 (setq gnus-topic-active-topology '(("" visible)))
486 (setq gnus-topic-active-alist nil)
487 ;; Descend the top-level hierarchy.
488 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
489 ;; Set the top-level topic names to something nice.
490 (setcar (car gnus-topic-active-topology) "Gnus active")
491 (setcar (car gnus-topic-active-alist) "Gnus active"))))
493 (defun gnus-topic-grok-active-1 (topology groups)
494 (let* ((name (caar topology))
495 (prefix (concat "^" (regexp-quote name)))
496 tgroups nprefix ntopology group)
498 (string-match prefix (setq group (car groups))))
499 (if (not (string-match "\\." group (match-end 0)))
500 ;; There are no further hierarchies here, so we just
501 ;; enter this group into the list belonging to this
503 (push (pop groups) tgroups)
504 ;; New sub-hierarchy, so we add it to the topology.
505 (nconc topology (list (setq ntopology
506 (list (list (substring
507 group 0 (match-end 0))
509 ;; Descend the hierarchy.
510 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
511 ;; We remove the trailing "." from the topic name.
513 (if (string-match "\\.$" name)
514 (substring name 0 (match-beginning 0))
516 ;; Add this topic and its groups to the topic alist.
517 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
518 (setcar (car topology) name)
519 ;; We return the rest of the groups that didn't belong
523 (defun gnus-group-active-topic-p ()
524 "Return whether the current active comes from the active topics."
527 (get-text-property (point) 'gnus-active)))
529 ;;; Topic mode, commands and keymap.
531 (defvar gnus-topic-mode-map nil)
532 (defvar gnus-group-topic-map nil)
534 (unless gnus-topic-mode-map
535 (setq gnus-topic-mode-map (make-sparse-keymap))
537 ;; Override certain group mode keys.
540 "=" gnus-topic-select-group
541 "\r" gnus-topic-select-group
542 " " gnus-topic-read-group
543 "\C-k" gnus-topic-kill-group
544 "\C-y" gnus-topic-yank-group
545 "\M-g" gnus-topic-get-new-news-this-topic
546 "\C-i" gnus-topic-indent
547 "AT" gnus-topic-list-active
548 gnus-mouse-2 gnus-mouse-pick-topic)
550 ;; Define a new submap.
552 (gnus-group-topic-map "T" gnus-group-mode-map)
553 "#" gnus-topic-mark-topic
554 "n" gnus-topic-create-topic
555 "m" gnus-topic-move-group
556 "D" gnus-topic-remove-group
557 "c" gnus-topic-copy-group
558 "h" gnus-topic-hide-topic
559 "s" gnus-topic-show-topic
560 "M" gnus-topic-move-matching
561 "C" gnus-topic-copy-matching
562 "r" gnus-topic-rename
563 "\177" gnus-topic-delete))
565 (defun gnus-topic-make-menu-bar ()
566 (unless (boundp 'gnus-topic-menu)
568 gnus-topic-menu gnus-topic-mode-map ""
570 ["Toggle topics" gnus-topic-mode t]
572 ["Copy" gnus-topic-copy-group t]
573 ["Move" gnus-topic-move-group t]
574 ["Remove" gnus-topic-remove-group t]
575 ["Copy matching" gnus-topic-copy-matching t]
576 ["Move matching" gnus-topic-move-matching t])
578 ["Show" gnus-topic-show-topic t]
579 ["Hide" gnus-topic-hide-topic t]
580 ["Delete" gnus-topic-delete t]
581 ["Rename" gnus-topic-rename t]
582 ["Create" gnus-topic-create-topic t]
583 ["Mark" gnus-topic-mark-topic t]
584 ["Indent" gnus-topic-indent t])
585 ["List active" gnus-topic-list-active t]))))
588 (defun gnus-topic-mode (&optional arg redisplay)
589 "Minor mode for topicsifying Gnus group buffers."
590 (interactive (list current-prefix-arg t))
591 (when (eq major-mode 'gnus-group-mode)
592 (make-local-variable 'gnus-topic-mode)
593 (setq gnus-topic-mode
594 (if (null arg) (not gnus-topic-mode)
595 (> (prefix-numeric-value arg) 0)))
596 ;; Infest Gnus with topics.
597 (when gnus-topic-mode
598 (when (and menu-bar-mode
599 (gnus-visual-p 'topic-menu 'menu))
600 (gnus-topic-make-menu-bar))
601 (setq gnus-topic-line-format-spec
602 (gnus-parse-format gnus-topic-line-format
603 gnus-topic-line-format-alist t))
604 (unless (assq 'gnus-topic-mode minor-mode-alist)
605 (push '(gnus-topic-mode " Topic") minor-mode-alist))
606 (unless (assq 'gnus-topic-mode minor-mode-map-alist)
607 (push (cons 'gnus-topic-mode gnus-topic-mode-map)
608 minor-mode-map-alist))
609 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
610 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
611 (make-local-variable 'gnus-group-prepare-function)
612 (setq gnus-group-prepare-function 'gnus-group-prepare-topics)
613 (make-local-variable 'gnus-group-goto-next-group-function)
614 (setq gnus-group-goto-next-group-function
615 'gnus-topic-goto-next-group)
616 (setq gnus-group-change-level-function 'gnus-topic-change-level)
617 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
618 (run-hooks 'gnus-topic-mode-hook)
619 ;; We check the topology.
620 (gnus-topic-check-topology))
621 ;; Remove topic infestation.
622 (unless gnus-topic-mode
623 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
624 (remove-hook 'gnus-group-change-level-function
625 'gnus-topic-change-level)
626 (setq gnus-group-prepare-function 'gnus-group-prepare-flat))
628 (gnus-group-list-groups))))
630 (defun gnus-topic-select-group (&optional all)
631 "Select this newsgroup.
632 No article is selected automatically.
633 If ALL is non-nil, already read articles become readable.
634 If ALL is a number, fetch this number of articles."
636 (if (gnus-group-topic-p)
637 (let ((gnus-group-list-mode
638 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
639 (gnus-topic-fold all))
640 (gnus-group-select-group all)))
642 (defun gnus-mouse-pick-topic (e)
643 "Select the group or topic under the mouse pointer."
646 (gnus-topic-read-group nil))
648 (defun gnus-topic-read-group (&optional all no-article group)
649 "Read news in this newsgroup.
650 If the prefix argument ALL is non-nil, already read articles become
651 readable. IF ALL is a number, fetch this number of articles. If the
652 optional argument NO-ARTICLE is non-nil, no article will be
653 auto-selected upon group entry. If GROUP is non-nil, fetch that
656 (if (gnus-group-topic-p)
657 (let ((gnus-group-list-mode
658 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
659 (gnus-topic-fold all))
660 (gnus-group-read-group all no-article group)))
662 (defun gnus-topic-create-topic (topic parent &optional previous)
665 (read-string "Create topic: ")
666 (completing-read "Parent topic: " gnus-topic-alist nil t
667 (cons (gnus-group-parent-topic) 0))))
668 ;; Check whether this topic already exists.
669 (when (gnus-topic-find-topology topic)
670 (error "Topic aleady exists"))
672 (setq parent (caar gnus-topic-topology)))
673 (let ((top (cdr (gnus-topic-find-topology parent))))
675 (error "No such parent topic: %s" parent))
678 (while (and (cdr top)
679 (not (equal (caaadr top) previous)))
680 (setq top (cdr top)))
681 (setcdr top (cons (list (list topic 'visible)) (cdr top))))
682 (nconc top (list (list (list topic 'visible)))))
683 (unless (assoc topic gnus-topic-alist)
684 (push (list topic) gnus-topic-alist)))
685 (gnus-topic-enter-dribble)
686 (gnus-group-list-groups))
688 (defun gnus-topic-move-group (n topic &optional copyp)
689 "Move the current group to a topic."
691 (list current-prefix-arg
692 (completing-read "Move to topic: " gnus-topic-alist nil t)))
693 (let ((groups (gnus-group-process-prefix n))
694 (topicl (assoc topic gnus-topic-alist))
697 (gnus-group-remove-mark g)
699 (setq entry (assoc (gnus-group-topic g) gnus-topic-alist))
701 (setcdr entry (delete g (cdr entry))))
703 (nconc topicl (list g))))
705 (gnus-group-position-point))
706 (gnus-topic-enter-dribble)
707 (gnus-group-list-groups))
709 (defun gnus-topic-remove-group (n)
710 "Remove the current group the topic."
712 (gnus-topic-move-group n nil))
714 (defun gnus-topic-copy-group (n topic)
715 "Copy the current group to a topic."
717 (list current-prefix-arg
718 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
719 (gnus-topic-move-group n topic t))
721 (defun gnus-topic-change-level (group level oldlevel)
722 "Run when changing levels to enter/remove groups from topics."
723 (when (and gnus-topic-mode
724 (not gnus-topic-inhibit-change-level))
725 ;; Remove the group from the topics.
726 (when (and (< oldlevel gnus-level-zombie)
727 (>= level gnus-level-zombie))
729 (when (setq alist (assoc (gnus-group-topic group) gnus-topic-alist))
730 (setcdr alist (delete group (cdr alist))))))
731 ;; If the group is subscribed. then we enter it into the topics.
732 (when (and (< level gnus-level-zombie)
733 (>= oldlevel gnus-level-zombie))
734 (let ((entry (assoc (caar gnus-topic-topology) gnus-topic-alist)))
735 (setcdr entry (cons group (cdr entry)))))))
737 (defun gnus-topic-goto-next-group (group props)
738 "Go to group or the next group after group."
740 (gnus-topic-goto-topic (cadr (memq 'gnus-topic props)))
741 (if (gnus-group-goto-group group)
743 ;; The group is no longer visible.
744 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
745 (after (cdr (member group (cdr list)))))
746 ;; First try to put point on a group after the current one.
748 (not (gnus-group-goto-group (car after))))
749 (setq after (cdr after)))
750 ;; Then try to put point on a group before point.
752 (setq after (cdr (member group (reverse (cdr list)))))
754 (not (gnus-group-goto-group (car after))))
755 (setq after (cdr after))))
756 ;; Finally, just put point on the topic.
758 (gnus-topic-goto-topic (car list))
762 (defun gnus-topic-kill-group (&optional n discard)
763 "Kill the next N groups."
765 (if (gnus-group-topic-p)
766 (let ((topic (gnus-group-topic-name)))
767 (gnus-topic-remove-topic nil t)
768 (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
769 gnus-topic-killed-topics))
770 (gnus-group-kill-group n discard)
771 (gnus-topic-update-topic)))
773 (defun gnus-topic-yank-group (&optional arg)
774 "Yank the last topic."
776 (if gnus-topic-killed-topics
777 (let ((previous (gnus-group-parent-topic))
778 (item (nth 1 (pop gnus-topic-killed-topics))))
779 (gnus-topic-create-topic
780 (car item) (gnus-topic-parent-topic previous) previous))
781 (let* ((prev (gnus-group-group-name))
782 (gnus-topic-inhibit-change-level t)
783 (gnus-group-indentation
785 (* gnus-topic-indent-level
787 (gnus-topic-goto-topic (gnus-group-parent-topic))
788 (gnus-group-topic-level)) 0)) ? ))
790 ;; We first yank the groups the normal way...
791 (setq yanked (gnus-group-yank-group arg))
792 ;; Then we enter the yanked groups into the topics they belong
794 (setq alist (assoc (save-excursion
796 (gnus-group-parent-topic))
798 (when (stringp yanked)
799 (setq yanked (list yanked)))
802 (if (not (cdr alist))
803 (setcdr alist (nconc yanked (cdr alist)))
805 (when (equal (cadr alist) prev)
806 (setcdr alist (nconc yanked (cdr alist)))
808 (setq alist (cdr alist))))))
809 (gnus-topic-update-topic)))
811 (defun gnus-topic-hide-topic ()
812 "Hide all subtopics under the current topic."
814 (when (gnus-group-topic-p)
815 (gnus-topic-remove-topic nil nil 'hidden)))
817 (defun gnus-topic-show-topic ()
818 "Show the hidden topic."
820 (when (gnus-group-topic-p)
821 (gnus-topic-remove-topic t nil 'shown)))
823 (defun gnus-topic-mark-topic (topic)
824 "Mark all groups in the topic with the process mark."
825 (interactive (list (gnus-group-parent-topic)))
826 (let ((groups (cdr (gnus-topic-find-groups topic))))
828 (gnus-group-set-mark (gnus-info-group (nth 2 (pop groups)))))))
830 (defun gnus-topic-get-new-news-this-topic (&optional n)
831 "Check for new news in the current topic."
833 (if (not (gnus-group-topic-p))
834 (gnus-group-get-new-news-this-group n)
835 (gnus-topic-mark-topic (gnus-group-topic-name))
836 (gnus-group-get-new-news-this-group)))
838 (defun gnus-topic-move-matching (regexp topic &optional copyp)
839 "Move all groups that match REGEXP to some topic."
844 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
845 (read-string (format "Move to %s (regexp): " topic))))))
846 (gnus-group-mark-regexp regexp)
847 (gnus-topic-move-group nil topic copyp))
849 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
850 "Copy all groups that match REGEXP to some topic."
855 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
856 (read-string (format "Copy to %s (regexp): " topic))))))
857 (gnus-topic-move-matching regexp topic t))
859 (defun gnus-topic-delete (topic)
861 (interactive (list (gnus-group-topic-name)))
863 (error "No topic to be deleted"))
864 (let ((entry (assoc topic gnus-topic-alist))
865 (buffer-read-only nil))
867 (error "Topic not empty"))
868 ;; Delete if visible.
869 (when (gnus-topic-goto-topic topic)
871 ;; Remove from alist.
872 (setq gnus-topic-alist (delq entry gnus-topic-alist))
873 ;; Remove from topology.
874 (gnus-topic-find-topology topic nil nil 'delete)))
876 (defun gnus-topic-rename (old-name new-name)
881 (setq topic (completing-read "Rename topic: " gnus-topic-alist nil t
882 (cons (gnus-group-parent-topic) 0)))
883 (read-string (format "Rename %s to: " topic)))))
884 (let ((top (gnus-topic-find-topology old-name))
885 (entry (assoc old-name gnus-topic-alist)))
887 (setcar (cadr top) new-name))
889 (setcar entry new-name))
890 (gnus-group-list-groups)))
892 (defun gnus-topic-indent (&optional unindent)
893 "Indent a topic -- make it a sub-topic of the previous topic.
894 If UNINDENT, remove an indentation."
897 (gnus-topic-unindent)
898 (let* ((topic (gnus-group-parent-topic))
899 (parent (gnus-topic-previous-topic topic)))
901 (error "Nothing to indent %s into" topic))
903 (gnus-topic-goto-topic topic)
904 (gnus-topic-kill-group)
905 (gnus-topic-create-topic topic parent)
906 (gnus-topic-goto-topic topic)))))
908 (defun gnus-topic-unindent ()
911 (let* ((topic (gnus-group-parent-topic))
912 (parent (gnus-topic-parent-topic topic))
913 (grandparent (gnus-topic-parent-topic parent)))
915 (error "Nothing to indent %s into" topic))
917 (gnus-topic-goto-topic topic)
918 (gnus-topic-kill-group)
919 (gnus-topic-create-topic topic grandparent)
920 (gnus-topic-goto-topic topic))))
922 (defun gnus-topic-list-active (&optional force)
923 "List all groups that Gnus knows about in a topicsified fashion.
924 If FORCE, always re-read the active file."
926 (gnus-topic-grok-active)
927 (let ((gnus-topic-topology gnus-topic-active-topology)
928 (gnus-topic-alist gnus-topic-active-alist)
929 gnus-killed-list gnus-zombie-list)
930 (gnus-group-list-groups 9 nil 1)))
932 (provide 'gnus-topic)
934 ;;; gnus-topic.el ends here