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)
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))
115 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
116 (gnus-group-prepare-flat-list-dead
117 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
121 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
122 (gnus-group-prepare-flat-list-dead
123 (setq gnus-killed-list (sort gnus-killed-list 'string<))
128 (when (< lowest gnus-level-zombie)
130 (let ((top (gnus-topic-find-topology list-topic)))
131 (gnus-topic-prepare-topic (cdr top) (car top) level all))
132 (gnus-topic-prepare-topic gnus-topic-topology 0 level all))))
134 (gnus-group-set-mode-line)
135 (setq gnus-group-list-mode (cons level all))
136 (run-hooks 'gnus-group-prepare-hook))
138 (defun gnus-topic-prepare-topic (topic level &optional list-level all silent)
139 "Insert TOPIC into the group buffer.
140 If SILENT, don't insert anything. Return the number of unread
141 articles in the topic and its subtopics."
142 (let* ((type (pop topic))
143 (entries (gnus-topic-find-groups (car type) list-level all))
144 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
145 (gnus-group-indentation
146 (make-string (* gnus-topic-indent-level level) ? ))
147 (beg (progn (beginning-of-line) (point)))
148 (topic (reverse topic))
149 (all-entries entries)
152 ;; Insert any sub-topics.
155 (gnus-topic-prepare-topic
156 (pop topic) (1+ level) list-level all
160 ;; Insert all the groups that belong in this topic.
161 (while (setq info (nth 2 (setq entry (pop entries))))
163 (gnus-group-insert-group-line
164 (gnus-info-group info)
165 (gnus-info-level info) (gnus-info-marks info)
166 (car entry) (gnus-info-method info)))
167 (when (numberp (car entry))
168 (incf unread (car entry))))
170 ;; Insert the topic line.
172 (gnus-extent-start-open (point))
173 (gnus-topic-insert-topic-line
175 (not (eq (nth 2 type) 'hidden))
176 level all-entries unread))
180 (defun gnus-topic-find-groups (topic &optional level all)
181 "Return entries for all visible groups in TOPIC."
182 (let ((groups (cdr (assoc topic gnus-topic-alist)))
183 info clevel unread group w lowest gtopic params visible-groups entry)
184 (setq lowest (or lowest 1))
185 (setq level (or level 7))
186 ;; We go through the newsrc to look for matches.
188 (setq entry (gnus-gethash (pop groups) gnus-newsrc-hashtb)
190 group (gnus-info-group info)
191 params (gnus-info-params info)
194 unread ; nil means that the group is dead.
195 (<= (setq clevel (gnus-info-level info)) level)
196 (>= clevel lowest) ; Is inside the level we want.
198 (and gnus-group-list-inactive-groups
201 (and gnus-list-groups-with-ticked-articles
202 (cdr (assq 'tick (gnus-info-marks info))))
203 ; Has right readedness.
204 ;; Check for permanent visibility.
205 (and gnus-permanently-visible-groups
206 (string-match gnus-permanently-visible-groups group))
207 (memq 'visible params)
208 (cdr (assq 'visible params)))
209 ;; Add this group to the list of visible groups.
210 (push entry visible-groups)))
211 (nreverse visible-groups)))
213 (defun gnus-topic-remove-topic (&optional insert total-remove hide)
214 "Remove the current topic."
215 (let ((topic (gnus-group-topic-name))
216 (level (gnus-group-topic-level))
217 (beg (progn (beginning-of-line) (point)))
220 (while (and (zerop (forward-line 1))
221 (> (or (gnus-group-topic-level) (1+ level)) level)))
222 (delete-region beg (point))
223 (setcar (cdr (car (cdr (gnus-topic-find-topology topic))))
224 (if insert 'visible 'invisible))
226 (setcdr (cdr (car (cdr (gnus-topic-find-topology topic))))
229 (gnus-topic-insert-topic topic)))))
231 (defun gnus-topic-insert-topic (topic)
233 (gnus-group-prepare-topics
234 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
237 (defun gnus-topic-fold (&optional insert)
238 "Remove/insert the current topic."
239 (let ((topic (gnus-group-topic-name)))
242 (if (not (gnus-group-active-topic-p))
243 (gnus-topic-remove-topic
244 (or insert (not (gnus-topic-visible-p))))
245 (let ((gnus-topic-topology gnus-topic-active-topology)
246 (gnus-topic-alist gnus-topic-active-alist))
247 (gnus-topic-remove-topic
248 (or insert (not (gnus-topic-visible-p))))))))))
250 (defun gnus-group-topic-p ()
251 "Return non-nil if the current line is a topic."
252 (get-text-property (gnus-point-at-bol) 'gnus-topic))
254 (defun gnus-topic-visible-p ()
255 "Return non-nil if the current topic is visible."
256 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
258 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
260 (let* ((visible (if visiblep "" "..."))
261 (indentation (make-string (* gnus-topic-indent-level level) ? ))
262 (total-number-of-articles unread)
263 (number-of-groups (length entries))
264 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
270 (eval gnus-topic-line-format-spec)
271 (gnus-topic-remove-excess-properties))
272 (list 'gnus-topic name
273 'gnus-topic-level level
274 'gnus-active active-topic
275 'gnus-topic-visible visiblep))))
277 (defun gnus-topic-previous-topic (topic)
278 "Return the previous topic on the same level as TOPIC."
279 (let ((top (cdr (cdr (gnus-topic-find-topology
280 (gnus-topic-parent-topic topic))))))
281 (unless (equal topic (car (car (car top))))
282 (while (and top (not (equal (car (car (car (cdr top)))) topic)))
283 (setq top (cdr top)))
284 (car (car (car top))))))
286 (defun gnus-topic-parent-topic (topic &optional topology)
287 "Return the parent of TOPIC."
289 (setq topology gnus-topic-topology))
290 (let ((parent (car (pop topology)))
293 (not (setq found (equal (car (car (car topology))) topic)))
294 (not (setq result (gnus-topic-parent-topic topic
296 (setq topology (cdr topology)))
297 (or result (and found parent))))
299 (defun gnus-topic-find-topology (topic &optional topology level remove)
300 "Return the topology of TOPIC."
302 (setq topology gnus-topic-topology)
306 (if (equal (car (car topology)) topic)
309 (delq topology remove))
310 (cons level topology))
311 (setq topology (cdr topology))
313 (not (setq result (gnus-topic-find-topology
314 topic (car topology) (1+ level)
316 (setq topology (cdr topology)))
319 (defun gnus-topic-check-topology ()
320 ;; The first time we set the topology to whatever we have
321 ;; gotten here, which can be rather random.
322 (unless gnus-topic-alist
323 (gnus-topic-init-alist))
325 (let ((topics (gnus-topic-list))
326 (alist gnus-topic-alist)
329 (unless (member (car (car alist)) topics)
330 (nconc gnus-topic-topology
331 (list (list (list (car (car alist)) 'visible))))
333 (setq alist (cdr alist)))
335 (gnus-topic-enter-dribble)))
336 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
338 (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
339 (newsrc gnus-newsrc-alist)
342 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
343 (setcdr entry (cons group (cdr entry)))))))
345 (defvar gnus-tmp-topics nil)
346 (defun gnus-topic-list (&optional topology)
348 (setq topology gnus-topic-topology
349 gnus-tmp-topics nil))
350 (push (car (car topology)) gnus-tmp-topics)
351 (mapcar 'gnus-topic-list (cdr topology))
354 (defun gnus-topic-enter-dribble ()
356 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
358 (defun gnus-topic-articles-in-topic (entries)
362 (when (numberp (setq number (car (pop entries))))
363 (incf total number)))
366 (defun gnus-group-parent-topic ()
367 "Return the topic the current group belongs in."
368 (let ((group (gnus-group-group-name)))
370 (gnus-group-topic group)
371 (gnus-group-topic-name))))
373 (defun gnus-group-topic (group)
374 "Return the topic GROUP is a member of."
375 (let ((alist gnus-topic-alist)
378 (when (member group (cdr (car alist)))
379 (setq out (car (car alist))
381 (setq alist (cdr alist)))
384 (defun gnus-topic-goto-topic (topic)
385 (let ((orig (point)))
386 (goto-char (point-min))
387 (while (and (not (equal topic (gnus-group-topic-name)))
388 (zerop (forward-line 1))))
389 (or (gnus-group-topic-name)
394 (defun gnus-topic-update-topic ()
395 (when (and (eq major-mode 'gnus-group-mode)
397 (let ((group (gnus-group-group-name)))
398 (gnus-topic-goto-topic (gnus-group-parent-topic))
399 (gnus-topic-update-topic-line)
400 (gnus-group-goto-group group)
401 (gnus-group-position-point))))
403 (defun gnus-topic-update-topic-line (&optional topic level)
405 (setq topic gnus-topic-topology)
407 (let* ((type (pop topic))
408 (buffer-read-only nil)
409 (entries (gnus-topic-find-groups
410 (car type) (car gnus-group-list-mode)
411 (cdr gnus-group-list-mode)))
412 (visiblep (eq (nth 1 type) 'visible))
413 (all-entries entries)
416 ;; Tally any sub-topics.
418 (incf unread (gnus-topic-update-topic-line (pop topic) (1+ level))))
419 ;; Tally all the groups that belong in this topic.
420 (while (setq info (nth 2 (setq entry (pop entries))))
421 (when (numberp (car entry))
422 (incf unread (car entry))))
423 ;; Insert the topic line.
424 (when (gnus-topic-goto-topic (car type))
425 (gnus-topic-insert-topic-line
427 (not (eq (nth 2 type) 'hidden))
428 level all-entries unread)
432 (defun gnus-topic-grok-active (&optional force)
433 "Parse all active groups and create topic structures for them."
434 ;; First we make sure that we have really read the active file.
436 (not gnus-topic-active-alist))
438 (not gnus-have-read-active-file))
439 (let ((gnus-read-active-file t))
440 (gnus-read-active-file)))
441 (let (topology groups alist)
442 ;; Get a list of all groups available.
443 (mapatoms (lambda (g) (when (symbol-value g)
444 (push (symbol-name g) groups)))
446 (setq groups (sort groups 'string<))
447 ;; Init the variables.
448 (setq gnus-topic-active-topology '(("" visible)))
449 (setq gnus-topic-active-alist nil)
450 ;; Descend the top-level hierarchy.
451 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
452 ;; Set the top-level topic names to something nice.
453 (setcar (car gnus-topic-active-topology) "Gnus active")
454 (setcar (car gnus-topic-active-alist) "Gnus active"))))
456 (defun gnus-topic-grok-active-1 (topology groups)
457 (let* ((name (caar topology))
458 (prefix (concat "^" (regexp-quote name)))
459 tgroups nprefix ntopology group)
461 (string-match prefix (setq group (car groups))))
462 (if (not (string-match "\\." group (match-end 0)))
463 ;; There are no further hierarchies here, so we just
464 ;; enter this group into the list belonging to this
466 (push (pop groups) tgroups)
467 ;; New sub-hierarchy, so we add it to the topology.
468 (nconc topology (list (setq ntopology
469 (list (list (substring
470 group 0 (match-end 0))
472 ;; Descend the hierarchy.
473 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
474 ;; We remove the trailing "." from the topic name.
476 (if (string-match "\\.$" name)
477 (substring name 0 (match-beginning 0))
479 ;; Add this topic and its groups to the topic alist.
480 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
481 (setcar (car topology) name)
482 ;; We return the rest of the groups that didn't belong
486 (defun gnus-group-active-topic-p ()
487 "Return whether the current active comes from the active topics."
490 (get-text-property (point) 'gnus-active)))
492 ;;; Topic mode, commands and keymap.
494 (defvar gnus-topic-mode-map nil)
495 (defvar gnus-group-topic-map nil)
497 (unless gnus-topic-mode-map
498 (setq gnus-topic-mode-map (make-sparse-keymap))
500 ;; Override certain group mode keys.
503 "=" gnus-topic-select-group
504 "\r" gnus-topic-select-group
505 " " gnus-topic-read-group
506 "\C-k" gnus-topic-kill-group
507 "\C-y" gnus-topic-yank-group
508 "\M-g" gnus-topic-get-new-news-this-topic
509 "\C-i" gnus-topic-indent
510 "AT" gnus-topic-list-active
511 gnus-mouse-2 gnus-mouse-pick-topic)
513 ;; Define a new submap.
515 (gnus-group-topic-map "T" gnus-group-mode-map)
516 "#" gnus-topic-mark-topic
517 "n" gnus-topic-create-topic
518 "m" gnus-topic-move-group
519 "c" gnus-topic-copy-group
520 "h" gnus-topic-hide-topic
521 "s" gnus-topic-show-topic
522 "M" gnus-topic-move-matching
523 "C" gnus-topic-copy-matching
524 "r" gnus-topic-rename
525 "\177" gnus-topic-delete))
527 (defun gnus-topic-make-menu-bar ()
528 (unless (boundp 'gnus-topic-menu)
530 gnus-topic-menu gnus-topic-mode-map ""
532 ["Toggle topics" gnus-topic-mode t]
534 ["Copy" gnus-topic-copy-group t]
535 ["Move" gnus-topic-move-group t]
536 ["Copy matching" gnus-topic-copy-matching t]
537 ["Move matching" gnus-topic-move-matching t])
539 ["Show" gnus-topic-show-topic t]
540 ["Hide" gnus-topic-hide-topic t]
541 ["Delete" gnus-topic-delete t]
542 ["Rename" gnus-topic-rename t]
543 ["Create" gnus-topic-create-topic t]
544 ["Mark" gnus-topic-mark-topic t]
545 ["Indent" gnus-topic-indent t])
546 ["List active" gnus-topic-list-active t]))))
549 (defun gnus-topic-mode (&optional arg redisplay)
550 "Minor mode for topicsifying Gnus group buffers."
551 (interactive (list current-prefix-arg t))
552 (when (eq major-mode 'gnus-group-mode)
553 (make-local-variable 'gnus-topic-mode)
554 (setq gnus-topic-mode
555 (if (null arg) (not gnus-topic-mode)
556 (> (prefix-numeric-value arg) 0)))
557 ;; Infest Gnus with topics.
558 (when gnus-topic-mode
559 (when (and menu-bar-mode
560 (gnus-visual-p 'topic-menu 'menu))
561 (gnus-topic-make-menu-bar))
562 (setq gnus-topic-line-format-spec
563 (gnus-parse-format gnus-topic-line-format
564 gnus-topic-line-format-alist t))
565 (unless (assq 'gnus-topic-mode minor-mode-alist)
566 (push '(gnus-topic-mode " Topic") minor-mode-alist))
567 (unless (assq 'gnus-topic-mode minor-mode-map-alist)
568 (push (cons 'gnus-topic-mode gnus-topic-mode-map)
569 minor-mode-map-alist))
570 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
571 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
572 (make-local-variable 'gnus-group-prepare-function)
573 (setq gnus-group-prepare-function 'gnus-group-prepare-topics)
574 (make-local-variable 'gnus-group-goto-next-group-function)
575 (setq gnus-group-goto-next-group-function
576 'gnus-topic-goto-next-group)
577 (setq gnus-group-change-level-function 'gnus-topic-change-level)
578 (run-hooks 'gnus-topic-mode-hook)
579 ;; We check the topology.
580 (gnus-topic-check-topology))
581 ;; Remove topic infestation.
582 (unless gnus-topic-mode
583 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
584 (remove-hook 'gnus-group-change-level-function
585 'gnus-topic-change-level)
586 (setq gnus-group-prepare-function 'gnus-group-prepare-flat))
588 (gnus-group-list-groups))))
590 (defun gnus-topic-select-group (&optional all)
591 "Select this newsgroup.
592 No article is selected automatically.
593 If ALL is non-nil, already read articles become readable.
594 If ALL is a number, fetch this number of articles."
596 (if (gnus-group-topic-p)
597 (let ((gnus-group-list-mode
598 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
599 (gnus-topic-fold all))
600 (gnus-group-select-group all)))
602 (defun gnus-mouse-pick-topic (e)
603 "Select the group or topic under the mouse pointer."
606 (gnus-topic-read-group nil))
608 (defun gnus-topic-read-group (&optional all no-article group)
609 "Read news in this newsgroup.
610 If the prefix argument ALL is non-nil, already read articles become
611 readable. IF ALL is a number, fetch this number of articles. If the
612 optional argument NO-ARTICLE is non-nil, no article will be
613 auto-selected upon group entry. If GROUP is non-nil, fetch that
616 (if (gnus-group-topic-p)
617 (let ((gnus-group-list-mode
618 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
619 (gnus-topic-fold all))
620 (gnus-group-read-group all no-article group)))
622 (defun gnus-topic-create-topic (topic parent &optional previous)
625 (read-string "Create topic: ")
626 (completing-read "Parent topic: " gnus-topic-alist nil t
627 (cons (gnus-group-parent-topic) 0))))
628 ;; Check whether this topic already exists.
629 (when (gnus-topic-find-topology topic)
630 (error "Topic aleady exists"))
632 (setq parent (car (car gnus-topic-topology))))
633 (let ((top (cdr (gnus-topic-find-topology parent))))
635 (error "No such parent topic: %s" parent))
638 (while (and (cdr top)
639 (not (equal (car (car (car (cdr top)))) previous)))
640 (setq top (cdr top)))
641 (setcdr top (cons (list (list topic 'visible)) (cdr top))))
642 (nconc top (list (list (list topic 'visible)))))
643 (unless (assoc topic gnus-topic-alist)
644 (push (list topic) gnus-topic-alist)))
645 (gnus-topic-enter-dribble)
646 (gnus-group-list-groups))
648 (defun gnus-topic-move-group (n topic &optional copyp)
649 "Move the current group to a topic."
651 (list current-prefix-arg
652 (completing-read "Move to topic: " gnus-topic-alist nil t)))
653 (let ((groups (gnus-group-process-prefix n))
654 (topicl (assoc topic gnus-topic-alist))
657 (error "No such topic: %s" topic))
659 (gnus-group-remove-mark g)
661 (setq entry (assoc (gnus-group-topic g) gnus-topic-alist))
663 (setcdr entry (delete g (cdr entry))))
664 (nconc topicl (list g)))
666 (gnus-group-position-point))
667 (gnus-topic-enter-dribble)
668 (gnus-group-list-groups))
670 (defun gnus-topic-copy-group (n topic)
671 "Copy the current group to a topic."
673 (list current-prefix-arg
674 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
675 (gnus-topic-move-group n topic t))
677 (defun gnus-topic-change-level (group level oldlevel)
678 "Run when changing levels to enter/remove groups from topics."
679 (when (and gnus-topic-mode
680 (not gnus-topic-inhibit-change-level))
681 ;; Remove the group from the topics.
682 (when (and (< oldlevel gnus-level-zombie)
683 (>= level gnus-level-zombie))
685 (when (setq alist (assoc (gnus-group-topic group) gnus-topic-alist))
686 (setcdr alist (delete group (cdr alist))))))
687 ;; If the group is subscribed. then we enter it into the topics.
688 (when (and (< level gnus-level-zombie)
689 (>= oldlevel gnus-level-zombie))
690 (let ((entry (assoc (caar gnus-topic-topology) gnus-topic-alist)))
691 (setcdr entry (cons group (cdr entry)))))))
693 (defun gnus-topic-goto-next-group (group props)
694 "Go to group or the next group after group."
696 (gnus-topic-goto-topic (cadr (memq 'gnus-topic props)))
697 (if (gnus-group-goto-group group)
699 ;; The group is no longer visible.
700 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
701 (after (cdr (member group (cdr list)))))
702 ;; First try to put point on a group after the current one.
704 (not (gnus-group-goto-group (car after))))
705 (setq after (cdr after)))
706 ;; Then try to put point on a group before point.
708 (setq after (cdr (member group (reverse (cdr list)))))
710 (not (gnus-group-goto-group (car after))))
711 (setq after (cdr after))))
712 ;; Finally, just put point on the topic.
714 (gnus-topic-goto-topic (car list))
718 (defun gnus-topic-kill-group (&optional n discard)
719 "Kill the next N groups."
721 (if (gnus-group-topic-p)
722 (let ((topic (gnus-group-topic-name)))
723 (gnus-topic-remove-topic nil t)
724 (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
725 gnus-topic-killed-topics))
726 (gnus-group-kill-group n discard)))
728 (defun gnus-topic-yank-group (&optional arg)
729 "Yank the last topic."
731 (if gnus-topic-killed-topics
732 (let ((previous (gnus-group-parent-topic))
733 (item (nth 1 (pop gnus-topic-killed-topics))))
734 (gnus-topic-create-topic
735 (car item) (gnus-topic-parent-topic previous) previous))
736 (let* ((prev (gnus-group-group-name))
737 (gnus-topic-inhibit-change-level t)
738 (gnus-group-indentation
740 (* gnus-topic-indent-level
742 (gnus-topic-goto-topic (gnus-group-parent-topic))
743 (gnus-group-topic-level)) 0)) ? ))
745 ;; We first yank the groups the normal way...
746 (setq yanked (gnus-group-yank-group arg))
747 ;; Then we enter the yanked groups into the topics they belong
749 (setq alist (assoc (save-excursion
751 (gnus-group-parent-topic))
753 (when (stringp yanked)
754 (setq yanked (list yanked)))
757 (if (not (cdr alist))
758 (setcdr alist (nconc yanked (cdr alist)))
760 (when (equal (car (cdr alist)) prev)
761 (setcdr alist (nconc yanked (cdr alist)))
763 (setq alist (cdr alist))))))))
765 (defun gnus-topic-hide-topic ()
766 "Hide all subtopics under the current topic."
768 (when (gnus-group-topic-p)
769 (gnus-topic-remove-topic nil nil 'hidden)))
771 (defun gnus-topic-show-topic ()
772 "Show the hidden topic."
774 (when (gnus-group-topic-p)
775 (gnus-topic-remove-topic t nil 'shown)))
777 (defun gnus-topic-mark-topic (topic)
778 "Mark all groups in the topic with the process mark."
779 (interactive (list (gnus-group-parent-topic)))
780 (let ((groups (cdr (gnus-topic-find-groups topic))))
782 (gnus-group-set-mark (gnus-info-group (nth 2 (pop groups)))))))
784 (defun gnus-topic-get-new-news-this-topic (&optional n)
785 "Check for new news in the current topic."
787 (if (not (gnus-group-topic-p))
788 (gnus-group-get-new-news-this-group n)
789 (gnus-topic-mark-topic (gnus-group-topic-name))
790 (gnus-group-get-new-news-this-group)))
792 (defun gnus-topic-move-matching (regexp topic &optional copyp)
793 "Move all groups that match REGEXP to some topic."
798 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
799 (read-string (format "Move to %s (regexp): " topic))))))
800 (gnus-group-mark-regexp regexp)
801 (gnus-topic-move-group nil topic copyp))
803 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
804 "Copy all groups that match REGEXP to some topic."
809 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
810 (read-string (format "Copy to %s (regexp): " topic))))))
811 (gnus-topic-move-matching regexp topic t))
813 (defun gnus-topic-delete (topic)
815 (interactive (list (gnus-group-topic-name)))
817 (error "No topic to be deleted"))
818 (let ((entry (assoc topic gnus-topic-alist))
819 (buffer-read-only nil))
821 (error "Topic not empty"))
822 ;; Delete if visible.
823 (when (gnus-topic-goto-topic topic)
825 ;; Remove from alist.
826 (setq gnus-topic-alist (delq entry gnus-topic-alist))
827 ;; Remove from topology.
828 (gnus-topic-find-topology topic nil nil 'delete)))
830 (defun gnus-topic-rename (old-name new-name)
835 (setq topic (completing-read "Rename topic: " gnus-topic-alist nil t
836 (cons (gnus-group-parent-topic) 0)))
837 (read-string (format "Rename %s to: " topic)))))
838 (let ((top (gnus-topic-find-topology old-name))
839 (entry (assoc old-name gnus-topic-alist)))
841 (setcar (car (cdr top)) new-name))
843 (setcar entry new-name))
844 (gnus-group-list-groups)))
846 (defun gnus-topic-indent (&optional unindent)
847 "Indent a topic -- make it a sub-topic of the previous topic.
848 If UNINDENT, remove an indentation."
851 (gnus-topic-unindent)
852 (let* ((topic (gnus-group-parent-topic))
853 (parent (gnus-topic-previous-topic topic)))
855 (error "Nothing to indent %s into" topic))
857 (gnus-topic-goto-topic topic)
858 (gnus-topic-kill-group)
859 (gnus-topic-create-topic topic parent)
860 (gnus-topic-goto-topic topic)))))
862 (defun gnus-topic-unindent ()
865 (let* ((topic (gnus-group-parent-topic))
866 (parent (gnus-topic-parent-topic topic))
867 (grandparent (gnus-topic-parent-topic parent)))
869 (error "Nothing to indent %s into" topic))
871 (gnus-topic-goto-topic topic)
872 (gnus-topic-kill-group)
873 (gnus-topic-create-topic topic grandparent)
874 (gnus-topic-goto-topic topic))))
876 (defun gnus-topic-list-active (&optional force)
877 "List all groups that Gnus knows about in a topicsified fashion.
878 If FORCE, always re-read the active file."
880 (gnus-topic-grok-active)
881 (let ((gnus-topic-topology gnus-topic-active-topology)
882 (gnus-topic-alist gnus-topic-active-alist))
883 (gnus-group-list-groups 9 nil 1)))
885 (provide 'gnus-topic)
887 ;;; gnus-topic.el ends here