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))
165 (gnus-group-insert-group-line
166 entry (if (member entry gnus-zombie-list) 8 9)
167 nil (- (1+ (cdr (setq active (gnus-active entry))))
169 (when (setq info (nth 2 entry))
170 (gnus-group-insert-group-line
171 (gnus-info-group info)
172 (gnus-info-level info) (gnus-info-marks info)
173 (car entry) (gnus-info-method info)))))
174 (when (numberp (car entry))
175 (incf unread (car entry))))
177 ;; Insert the topic line.
179 (gnus-extent-start-open (point))
180 (gnus-topic-insert-topic-line
182 (not (eq (nth 2 type) 'hidden))
183 level all-entries unread))
187 (defun gnus-topic-find-groups (topic &optional level all)
188 "Return entries for all visible groups in TOPIC."
189 (let ((groups (cdr (assoc topic gnus-topic-alist)))
190 info clevel unread group lowest params visible-groups entry active)
191 (setq lowest (or lowest 1))
192 (setq level (or level 7))
193 ;; We go through the newsrc to look for matches.
195 (setq entry (gnus-gethash (setq group (pop groups)) gnus-newsrc-hashtb)
197 params (gnus-info-params info)
198 active (gnus-active group)
199 unread (or (car entry)
200 (and (not (equal group "dummy.group"))
202 (- (1+ (cdr active)) (car active))))
203 clevel (or (gnus-info-level info)
204 (if (member group gnus-zombie-list) 8 9)))
206 unread ; nil means that the group is dead.
208 (>= clevel lowest) ; Is inside the level we want.
210 (and gnus-group-list-inactive-groups
213 (and gnus-list-groups-with-ticked-articles
214 (cdr (assq 'tick (gnus-info-marks info))))
215 ; Has right readedness.
216 ;; Check for permanent visibility.
217 (and gnus-permanently-visible-groups
218 (string-match gnus-permanently-visible-groups group))
219 (memq 'visible params)
220 (cdr (assq 'visible params)))
221 ;; Add this group to the list of visible groups.
222 (push (or entry group) visible-groups)))
223 (nreverse visible-groups)))
225 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
226 "Remove the current topic."
227 (let ((topic (gnus-group-topic-name))
228 (level (gnus-group-topic-level))
229 (beg (progn (beginning-of-line) (point)))
232 (while (and (zerop (forward-line 1))
233 (> (or (gnus-group-topic-level) (1+ level)) level)))
234 (delete-region beg (point))
235 (setcar (cdr (car (cdr (gnus-topic-find-topology topic))))
236 (if insert 'visible 'invisible))
238 (setcdr (cdr (car (cdr (gnus-topic-find-topology topic))))
241 (gnus-topic-insert-topic topic in-level)))))
243 (defun gnus-topic-insert-topic (topic &optional level)
245 (gnus-group-prepare-topics
246 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
247 nil nil topic level))
249 (defun gnus-topic-fold (&optional insert)
250 "Remove/insert the current topic."
251 (let ((topic (gnus-group-topic-name)))
254 (if (not (gnus-group-active-topic-p))
255 (gnus-topic-remove-topic
256 (or insert (not (gnus-topic-visible-p))))
257 (let ((gnus-topic-topology gnus-topic-active-topology)
258 (gnus-topic-alist gnus-topic-active-alist)
259 (gnus-group-list-mode (cons 5 t)))
260 (gnus-topic-remove-topic
261 (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
263 (defun gnus-group-topic-p ()
264 "Return non-nil if the current line is a topic."
265 (get-text-property (gnus-point-at-bol) 'gnus-topic))
267 (defun gnus-topic-visible-p ()
268 "Return non-nil if the current topic is visible."
269 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
271 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
273 (let* ((visible (if visiblep "" "..."))
274 (indentation (make-string (* gnus-topic-indent-level level) ? ))
275 (total-number-of-articles unread)
276 (number-of-groups (length entries))
277 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
283 (eval gnus-topic-line-format-spec)
284 (gnus-topic-remove-excess-properties))
285 (list 'gnus-topic name
286 'gnus-topic-level level
287 'gnus-active active-topic
288 'gnus-topic-visible visiblep))))
290 (defun gnus-topic-previous-topic (topic)
291 "Return the previous topic on the same level as TOPIC."
292 (let ((top (cdr (cdr (gnus-topic-find-topology
293 (gnus-topic-parent-topic topic))))))
294 (unless (equal topic (car (car (car top))))
295 (while (and top (not (equal (car (car (car (cdr top)))) topic)))
296 (setq top (cdr top)))
297 (car (car (car top))))))
299 (defun gnus-topic-parent-topic (topic &optional topology)
300 "Return the parent of TOPIC."
302 (setq topology gnus-topic-topology))
303 (let ((parent (car (pop topology)))
306 (not (setq found (equal (car (car (car topology))) topic)))
307 (not (setq result (gnus-topic-parent-topic topic
309 (setq topology (cdr topology)))
310 (or result (and found parent))))
312 (defun gnus-topic-find-topology (topic &optional topology level remove)
313 "Return the topology of TOPIC."
315 (setq topology gnus-topic-topology)
319 (if (equal (car (car topology)) topic)
322 (delq topology remove))
323 (cons level topology))
324 (setq topology (cdr topology))
326 (not (setq result (gnus-topic-find-topology
327 topic (car topology) (1+ level)
329 (setq topology (cdr topology)))
332 (defun gnus-topic-check-topology ()
333 ;; The first time we set the topology to whatever we have
334 ;; gotten here, which can be rather random.
335 (unless gnus-topic-alist
336 (gnus-topic-init-alist))
338 (let ((topics (gnus-topic-list))
339 (alist gnus-topic-alist)
342 (unless (member (car (car alist)) topics)
343 (nconc gnus-topic-topology
344 (list (list (list (car (car alist)) 'visible))))
346 (setq alist (cdr alist)))
348 (gnus-topic-enter-dribble)))
349 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
351 (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
352 (newsrc gnus-newsrc-alist)
355 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
356 (setcdr entry (cons group (cdr entry)))))))
358 (defvar gnus-tmp-topics nil)
359 (defun gnus-topic-list (&optional topology)
361 (setq topology gnus-topic-topology
362 gnus-tmp-topics nil))
363 (push (car (car topology)) gnus-tmp-topics)
364 (mapcar 'gnus-topic-list (cdr topology))
367 (defun gnus-topic-enter-dribble ()
369 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
371 (defun gnus-topic-articles-in-topic (entries)
375 (when (numberp (setq number (car (pop entries))))
376 (incf total number)))
379 (defun gnus-group-parent-topic ()
380 "Return the topic the current group belongs in."
381 (let ((group (gnus-group-group-name)))
383 (gnus-group-topic group)
384 (gnus-group-topic-name))))
386 (defun gnus-group-topic (group)
387 "Return the topic GROUP is a member of."
388 (let ((alist gnus-topic-alist)
391 (when (member group (cdr (car alist)))
392 (setq out (car (car alist))
394 (setq alist (cdr alist)))
397 (defun gnus-topic-goto-topic (topic)
398 (let ((orig (point)))
399 (goto-char (point-min))
400 (while (and (not (equal topic (gnus-group-topic-name)))
401 (zerop (forward-line 1))))
402 (or (gnus-group-topic-name)
407 (defun gnus-topic-update-topic ()
408 "Update all parent topics to the current group."
409 (when (and (eq major-mode 'gnus-group-mode)
411 (let ((group (gnus-group-group-name)))
412 (gnus-topic-goto-topic (gnus-group-parent-topic))
413 (gnus-topic-update-topic-line)
414 (gnus-group-goto-group group)
415 (gnus-group-position-point))))
417 (defun gnus-topic-goto-missing-group (group)
418 "Place point where GROUP is supposed to be inserted."
419 (let* ((topic (gnus-group-topic group))
420 (groups (cdr (assoc topic gnus-topic-alist)))
421 (g (cdr (member group groups)))
423 (while (and g unfound)
424 (when (gnus-group-goto-group (pop g))
428 (setq g (cdr (member group (reverse groups))))
429 (while (and g unfound)
430 (when (gnus-group-goto-group (pop g))
434 (gnus-topic-goto-topic topic)
437 (defun gnus-topic-update-topic-line (&optional topic level)
439 (setq topic gnus-topic-topology)
441 (let* ((type (pop topic))
442 (buffer-read-only nil)
443 (entries (gnus-topic-find-groups
444 (car type) (car gnus-group-list-mode)
445 (cdr gnus-group-list-mode)))
446 (visiblep (eq (nth 1 type) 'visible))
447 (all-entries entries)
450 ;; Tally any sub-topics.
452 (incf unread (gnus-topic-update-topic-line (pop topic) (1+ level))))
453 ;; Tally all the groups that belong in this topic.
454 (while (setq info (nth 2 (setq entry (pop entries))))
455 (when (numberp (car entry))
456 (incf unread (car entry))))
457 ;; Insert the topic line.
458 (when (gnus-topic-goto-topic (car type))
459 (gnus-topic-insert-topic-line
461 (not (eq (nth 2 type) 'hidden))
462 level all-entries unread)
466 (defun gnus-topic-grok-active (&optional force)
467 "Parse all active groups and create topic structures for them."
468 ;; First we make sure that we have really read the active file.
470 (not gnus-topic-active-alist))
472 (not gnus-have-read-active-file))
473 (let ((gnus-read-active-file t))
474 (gnus-read-active-file)))
475 (let (topology groups alist)
476 ;; Get a list of all groups available.
477 (mapatoms (lambda (g) (when (symbol-value g)
478 (push (symbol-name g) groups)))
480 (setq groups (sort groups 'string<))
481 ;; Init the variables.
482 (setq gnus-topic-active-topology '(("" visible)))
483 (setq gnus-topic-active-alist nil)
484 ;; Descend the top-level hierarchy.
485 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
486 ;; Set the top-level topic names to something nice.
487 (setcar (car gnus-topic-active-topology) "Gnus active")
488 (setcar (car gnus-topic-active-alist) "Gnus active"))))
490 (defun gnus-topic-grok-active-1 (topology groups)
491 (let* ((name (caar topology))
492 (prefix (concat "^" (regexp-quote name)))
493 tgroups nprefix ntopology group)
495 (string-match prefix (setq group (car groups))))
496 (if (not (string-match "\\." group (match-end 0)))
497 ;; There are no further hierarchies here, so we just
498 ;; enter this group into the list belonging to this
500 (push (pop groups) tgroups)
501 ;; New sub-hierarchy, so we add it to the topology.
502 (nconc topology (list (setq ntopology
503 (list (list (substring
504 group 0 (match-end 0))
506 ;; Descend the hierarchy.
507 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
508 ;; We remove the trailing "." from the topic name.
510 (if (string-match "\\.$" name)
511 (substring name 0 (match-beginning 0))
513 ;; Add this topic and its groups to the topic alist.
514 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
515 (setcar (car topology) name)
516 ;; We return the rest of the groups that didn't belong
520 (defun gnus-group-active-topic-p ()
521 "Return whether the current active comes from the active topics."
524 (get-text-property (point) 'gnus-active)))
526 ;;; Topic mode, commands and keymap.
528 (defvar gnus-topic-mode-map nil)
529 (defvar gnus-group-topic-map nil)
531 (unless gnus-topic-mode-map
532 (setq gnus-topic-mode-map (make-sparse-keymap))
534 ;; Override certain group mode keys.
537 "=" gnus-topic-select-group
538 "\r" gnus-topic-select-group
539 " " gnus-topic-read-group
540 "\C-k" gnus-topic-kill-group
541 "\C-y" gnus-topic-yank-group
542 "\M-g" gnus-topic-get-new-news-this-topic
543 "\C-i" gnus-topic-indent
544 "AT" gnus-topic-list-active
545 gnus-mouse-2 gnus-mouse-pick-topic)
547 ;; Define a new submap.
549 (gnus-group-topic-map "T" gnus-group-mode-map)
550 "#" gnus-topic-mark-topic
551 "n" gnus-topic-create-topic
552 "m" gnus-topic-move-group
553 "D" gnus-topic-remove-group
554 "c" gnus-topic-copy-group
555 "h" gnus-topic-hide-topic
556 "s" gnus-topic-show-topic
557 "M" gnus-topic-move-matching
558 "C" gnus-topic-copy-matching
559 "r" gnus-topic-rename
560 "\177" gnus-topic-delete))
562 (defun gnus-topic-make-menu-bar ()
563 (unless (boundp 'gnus-topic-menu)
565 gnus-topic-menu gnus-topic-mode-map ""
567 ["Toggle topics" gnus-topic-mode t]
569 ["Copy" gnus-topic-copy-group t]
570 ["Move" gnus-topic-move-group t]
571 ["Remove" gnus-topic-remove-group t]
572 ["Copy matching" gnus-topic-copy-matching t]
573 ["Move matching" gnus-topic-move-matching t])
575 ["Show" gnus-topic-show-topic t]
576 ["Hide" gnus-topic-hide-topic t]
577 ["Delete" gnus-topic-delete t]
578 ["Rename" gnus-topic-rename t]
579 ["Create" gnus-topic-create-topic t]
580 ["Mark" gnus-topic-mark-topic t]
581 ["Indent" gnus-topic-indent t])
582 ["List active" gnus-topic-list-active t]))))
585 (defun gnus-topic-mode (&optional arg redisplay)
586 "Minor mode for topicsifying Gnus group buffers."
587 (interactive (list current-prefix-arg t))
588 (when (eq major-mode 'gnus-group-mode)
589 (make-local-variable 'gnus-topic-mode)
590 (setq gnus-topic-mode
591 (if (null arg) (not gnus-topic-mode)
592 (> (prefix-numeric-value arg) 0)))
593 ;; Infest Gnus with topics.
594 (when gnus-topic-mode
595 (when (and menu-bar-mode
596 (gnus-visual-p 'topic-menu 'menu))
597 (gnus-topic-make-menu-bar))
598 (setq gnus-topic-line-format-spec
599 (gnus-parse-format gnus-topic-line-format
600 gnus-topic-line-format-alist t))
601 (unless (assq 'gnus-topic-mode minor-mode-alist)
602 (push '(gnus-topic-mode " Topic") minor-mode-alist))
603 (unless (assq 'gnus-topic-mode minor-mode-map-alist)
604 (push (cons 'gnus-topic-mode gnus-topic-mode-map)
605 minor-mode-map-alist))
606 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
607 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
608 (make-local-variable 'gnus-group-prepare-function)
609 (setq gnus-group-prepare-function 'gnus-group-prepare-topics)
610 (make-local-variable 'gnus-group-goto-next-group-function)
611 (setq gnus-group-goto-next-group-function
612 'gnus-topic-goto-next-group)
613 (setq gnus-group-change-level-function 'gnus-topic-change-level)
614 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
615 (run-hooks 'gnus-topic-mode-hook)
616 ;; We check the topology.
617 (gnus-topic-check-topology))
618 ;; Remove topic infestation.
619 (unless gnus-topic-mode
620 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
621 (remove-hook 'gnus-group-change-level-function
622 'gnus-topic-change-level)
623 (setq gnus-group-prepare-function 'gnus-group-prepare-flat))
625 (gnus-group-list-groups))))
627 (defun gnus-topic-select-group (&optional all)
628 "Select this newsgroup.
629 No article is selected automatically.
630 If ALL is non-nil, already read articles become readable.
631 If ALL is a number, fetch this number of articles."
633 (if (gnus-group-topic-p)
634 (let ((gnus-group-list-mode
635 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
636 (gnus-topic-fold all))
637 (gnus-group-select-group all)))
639 (defun gnus-mouse-pick-topic (e)
640 "Select the group or topic under the mouse pointer."
643 (gnus-topic-read-group nil))
645 (defun gnus-topic-read-group (&optional all no-article group)
646 "Read news in this newsgroup.
647 If the prefix argument ALL is non-nil, already read articles become
648 readable. IF ALL is a number, fetch this number of articles. If the
649 optional argument NO-ARTICLE is non-nil, no article will be
650 auto-selected upon group entry. If GROUP is non-nil, fetch that
653 (if (gnus-group-topic-p)
654 (let ((gnus-group-list-mode
655 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
656 (gnus-topic-fold all))
657 (gnus-group-read-group all no-article group)))
659 (defun gnus-topic-create-topic (topic parent &optional previous)
662 (read-string "Create topic: ")
663 (completing-read "Parent topic: " gnus-topic-alist nil t
664 (cons (gnus-group-parent-topic) 0))))
665 ;; Check whether this topic already exists.
666 (when (gnus-topic-find-topology topic)
667 (error "Topic aleady exists"))
669 (setq parent (car (car gnus-topic-topology))))
670 (let ((top (cdr (gnus-topic-find-topology parent))))
672 (error "No such parent topic: %s" parent))
675 (while (and (cdr top)
676 (not (equal (car (car (car (cdr top)))) previous)))
677 (setq top (cdr top)))
678 (setcdr top (cons (list (list topic 'visible)) (cdr top))))
679 (nconc top (list (list (list topic 'visible)))))
680 (unless (assoc topic gnus-topic-alist)
681 (push (list topic) gnus-topic-alist)))
682 (gnus-topic-enter-dribble)
683 (gnus-group-list-groups))
685 (defun gnus-topic-move-group (n topic &optional copyp)
686 "Move the current group to a topic."
688 (list current-prefix-arg
689 (completing-read "Move to topic: " gnus-topic-alist nil t)))
690 (let ((groups (gnus-group-process-prefix n))
691 (topicl (assoc topic gnus-topic-alist))
694 (gnus-group-remove-mark g)
696 (setq entry (assoc (gnus-group-topic g) gnus-topic-alist))
698 (setcdr entry (delete g (cdr entry))))
700 (nconc topicl (list g))))
702 (gnus-group-position-point))
703 (gnus-topic-enter-dribble)
704 (gnus-group-list-groups))
706 (defun gnus-topic-remove-group (n)
707 "Remove the current group the topic."
709 (gnus-topic-move-group n nil))
711 (defun gnus-topic-copy-group (n topic)
712 "Copy the current group to a topic."
714 (list current-prefix-arg
715 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
716 (gnus-topic-move-group n topic t))
718 (defun gnus-topic-change-level (group level oldlevel)
719 "Run when changing levels to enter/remove groups from topics."
720 (when (and gnus-topic-mode
721 (not gnus-topic-inhibit-change-level))
722 ;; Remove the group from the topics.
723 (when (and (< oldlevel gnus-level-zombie)
724 (>= level gnus-level-zombie))
726 (when (setq alist (assoc (gnus-group-topic group) gnus-topic-alist))
727 (setcdr alist (delete group (cdr alist))))))
728 ;; If the group is subscribed. then we enter it into the topics.
729 (when (and (< level gnus-level-zombie)
730 (>= oldlevel gnus-level-zombie))
731 (let ((entry (assoc (caar gnus-topic-topology) gnus-topic-alist)))
732 (setcdr entry (cons group (cdr entry)))))))
734 (defun gnus-topic-goto-next-group (group props)
735 "Go to group or the next group after group."
737 (gnus-topic-goto-topic (cadr (memq 'gnus-topic props)))
738 (if (gnus-group-goto-group group)
740 ;; The group is no longer visible.
741 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
742 (after (cdr (member group (cdr list)))))
743 ;; First try to put point on a group after the current one.
745 (not (gnus-group-goto-group (car after))))
746 (setq after (cdr after)))
747 ;; Then try to put point on a group before point.
749 (setq after (cdr (member group (reverse (cdr list)))))
751 (not (gnus-group-goto-group (car after))))
752 (setq after (cdr after))))
753 ;; Finally, just put point on the topic.
755 (gnus-topic-goto-topic (car list))
759 (defun gnus-topic-kill-group (&optional n discard)
760 "Kill the next N groups."
762 (if (gnus-group-topic-p)
763 (let ((topic (gnus-group-topic-name)))
764 (gnus-topic-remove-topic nil t)
765 (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
766 gnus-topic-killed-topics))
767 (gnus-group-kill-group n discard)
768 (gnus-topic-update-topic)))
770 (defun gnus-topic-yank-group (&optional arg)
771 "Yank the last topic."
773 (if gnus-topic-killed-topics
774 (let ((previous (gnus-group-parent-topic))
775 (item (nth 1 (pop gnus-topic-killed-topics))))
776 (gnus-topic-create-topic
777 (car item) (gnus-topic-parent-topic previous) previous))
778 (let* ((prev (gnus-group-group-name))
779 (gnus-topic-inhibit-change-level t)
780 (gnus-group-indentation
782 (* gnus-topic-indent-level
784 (gnus-topic-goto-topic (gnus-group-parent-topic))
785 (gnus-group-topic-level)) 0)) ? ))
787 ;; We first yank the groups the normal way...
788 (setq yanked (gnus-group-yank-group arg))
789 ;; Then we enter the yanked groups into the topics they belong
791 (setq alist (assoc (save-excursion
793 (gnus-group-parent-topic))
795 (when (stringp yanked)
796 (setq yanked (list yanked)))
799 (if (not (cdr alist))
800 (setcdr alist (nconc yanked (cdr alist)))
802 (when (equal (car (cdr alist)) prev)
803 (setcdr alist (nconc yanked (cdr alist)))
805 (setq alist (cdr alist))))))
806 (gnus-topic-update-topic)))
808 (defun gnus-topic-hide-topic ()
809 "Hide all subtopics under the current topic."
811 (when (gnus-group-topic-p)
812 (gnus-topic-remove-topic nil nil 'hidden)))
814 (defun gnus-topic-show-topic ()
815 "Show the hidden topic."
817 (when (gnus-group-topic-p)
818 (gnus-topic-remove-topic t nil 'shown)))
820 (defun gnus-topic-mark-topic (topic)
821 "Mark all groups in the topic with the process mark."
822 (interactive (list (gnus-group-parent-topic)))
823 (let ((groups (cdr (gnus-topic-find-groups topic))))
825 (gnus-group-set-mark (gnus-info-group (nth 2 (pop groups)))))))
827 (defun gnus-topic-get-new-news-this-topic (&optional n)
828 "Check for new news in the current topic."
830 (if (not (gnus-group-topic-p))
831 (gnus-group-get-new-news-this-group n)
832 (gnus-topic-mark-topic (gnus-group-topic-name))
833 (gnus-group-get-new-news-this-group)))
835 (defun gnus-topic-move-matching (regexp topic &optional copyp)
836 "Move all groups that match REGEXP to some topic."
841 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
842 (read-string (format "Move to %s (regexp): " topic))))))
843 (gnus-group-mark-regexp regexp)
844 (gnus-topic-move-group nil topic copyp))
846 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
847 "Copy all groups that match REGEXP to some topic."
852 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
853 (read-string (format "Copy to %s (regexp): " topic))))))
854 (gnus-topic-move-matching regexp topic t))
856 (defun gnus-topic-delete (topic)
858 (interactive (list (gnus-group-topic-name)))
860 (error "No topic to be deleted"))
861 (let ((entry (assoc topic gnus-topic-alist))
862 (buffer-read-only nil))
864 (error "Topic not empty"))
865 ;; Delete if visible.
866 (when (gnus-topic-goto-topic topic)
868 ;; Remove from alist.
869 (setq gnus-topic-alist (delq entry gnus-topic-alist))
870 ;; Remove from topology.
871 (gnus-topic-find-topology topic nil nil 'delete)))
873 (defun gnus-topic-rename (old-name new-name)
878 (setq topic (completing-read "Rename topic: " gnus-topic-alist nil t
879 (cons (gnus-group-parent-topic) 0)))
880 (read-string (format "Rename %s to: " topic)))))
881 (let ((top (gnus-topic-find-topology old-name))
882 (entry (assoc old-name gnus-topic-alist)))
884 (setcar (car (cdr top)) new-name))
886 (setcar entry new-name))
887 (gnus-group-list-groups)))
889 (defun gnus-topic-indent (&optional unindent)
890 "Indent a topic -- make it a sub-topic of the previous topic.
891 If UNINDENT, remove an indentation."
894 (gnus-topic-unindent)
895 (let* ((topic (gnus-group-parent-topic))
896 (parent (gnus-topic-previous-topic topic)))
898 (error "Nothing to indent %s into" topic))
900 (gnus-topic-goto-topic topic)
901 (gnus-topic-kill-group)
902 (gnus-topic-create-topic topic parent)
903 (gnus-topic-goto-topic topic)))))
905 (defun gnus-topic-unindent ()
908 (let* ((topic (gnus-group-parent-topic))
909 (parent (gnus-topic-parent-topic topic))
910 (grandparent (gnus-topic-parent-topic parent)))
912 (error "Nothing to indent %s into" topic))
914 (gnus-topic-goto-topic topic)
915 (gnus-topic-kill-group)
916 (gnus-topic-create-topic topic grandparent)
917 (gnus-topic-goto-topic topic))))
919 (defun gnus-topic-list-active (&optional force)
920 "List all groups that Gnus knows about in a topicsified fashion.
921 If FORCE, always re-read the active file."
923 (gnus-topic-grok-active)
924 (let ((gnus-topic-topology gnus-topic-active-topology)
925 (gnus-topic-alist gnus-topic-active-alist)
926 gnus-killed-list gnus-zombie-list)
927 (gnus-group-list-groups 9 nil 1)))
929 (provide 'gnus-topic)
931 ;;; gnus-topic.el ends here