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.
34 (defvar gnus-topic-mode nil
35 "Minor mode for Gnus group buffers.")
37 (defvar gnus-topic-mode-hook nil
38 "Hook run in topic mode buffers.")
40 (defvar gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
41 "Format of topic lines.
42 It works along the same lines as a normal formatting string,
43 with some simple extensions.
45 %i Indentation based on topic level.
47 %v Nothing if the topic is visible, \"...\" otherwise.
48 %g Number of groups in the topic.
49 %a Number of unread articles in the groups in the topic.
50 %A Number of unread articles in the groups in the topic and its subtopics.
53 (defvar gnus-topic-indent-level 2
54 "*How much each subtopic should be indented.")
56 (defvar gnus-topic-display-empty-topics t
57 "*If non-nil, display the topic lines even of topics that have no unread articles.")
59 ;; Internal variables.
61 (defvar gnus-topic-active-topology nil)
62 (defvar gnus-topic-active-alist nil)
64 (defvar gnus-topology-checked-p nil
65 "Whether the topology has been checked in this session.")
67 (defvar gnus-topic-killed-topics nil)
68 (defvar gnus-topic-inhibit-change-level nil)
69 (defvar gnus-topic-tallied-groups nil)
71 (defconst gnus-topic-line-format-alist
75 (?g number-of-groups ?d)
76 (?a (gnus-topic-articles-in-topic entries) ?d)
77 (?A total-number-of-articles ?d)
80 (defvar gnus-topic-line-format-spec nil)
84 (defun gnus-group-topic-name ()
85 "The name of the topic on the current line."
86 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
87 (and topic (symbol-name topic))))
89 (defun gnus-group-topic-level ()
90 "The level of the topic on the current line."
91 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
93 (defun gnus-group-topic-unread ()
94 "The number of unread articles in topic on the current line."
95 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
97 (defun gnus-topic-unread (topic)
98 "Return the number of unread articles in TOPIC."
100 (and (gnus-topic-goto-topic topic)
101 (gnus-group-topic-unread)))
104 (defun gnus-group-topic-p ()
105 "Return non-nil if the current line is a topic."
106 (gnus-group-topic-name))
108 (defun gnus-topic-visible-p ()
109 "Return non-nil if the current topic is visible."
110 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
112 (defun gnus-topic-articles-in-topic (entries)
116 (when (numberp (setq number (car (pop entries))))
117 (incf total number)))
120 (defun gnus-group-topic (group)
121 "Return the topic GROUP is a member of."
122 (let ((alist gnus-topic-alist)
125 (when (member group (cdar alist))
126 (setq out (caar alist)
128 (setq alist (cdr alist)))
131 (defun gnus-group-parent-topic (group)
132 "Return the topic GROUP is member of by looking at the group buffer."
134 (set-buffer gnus-group-buffer)
135 (if (gnus-group-goto-group group)
137 (gnus-group-topic group))))
139 (defun gnus-topic-goto-topic (topic)
142 (gnus-goto-char (text-property-any (point-min) (point-max)
143 'gnus-topic (intern topic)))))
145 (defun gnus-current-topic ()
146 "Return the name of the current topic."
148 (or (get-text-property (point) 'gnus-topic)
150 (and (gnus-goto-char (previous-single-property-change
151 (point) 'gnus-topic))
152 (get-text-property (max (1- (point)) (point-min))
155 (symbol-name result))))
157 (defun gnus-current-topics ()
158 "Return a list of all current topics, lowest in hierarchy first."
159 (let ((topic (gnus-current-topic))
163 (setq topic (gnus-topic-parent-topic topic)))
166 (defun gnus-group-active-topic-p ()
167 "Say whether the current topic comes from the active topics."
170 (get-text-property (point) 'gnus-active)))
172 (defun gnus-topic-find-groups (topic &optional level all)
173 "Return entries for all visible groups in TOPIC."
174 (let ((groups (cdr (assoc topic gnus-topic-alist)))
175 info clevel unread group lowest params visible-groups entry active)
176 (setq lowest (or lowest 1))
177 (setq level (or level 7))
178 ;; We go through the newsrc to look for matches.
180 (setq entry (gnus-gethash (setq group (pop groups)) gnus-newsrc-hashtb)
182 params (gnus-info-params info)
183 active (gnus-active group)
184 unread (or (car entry)
185 (and (not (equal group "dummy.group"))
187 (- (1+ (cdr active)) (car active))))
188 clevel (or (gnus-info-level info)
189 (if (member group gnus-zombie-list) 8 9)))
191 unread ; nil means that the group is dead.
193 (>= clevel lowest) ; Is inside the level we want.
196 gnus-group-list-inactive-groups
198 (and gnus-list-groups-with-ticked-articles
199 (cdr (assq 'tick (gnus-info-marks info))))
200 ; Has right readedness.
201 ;; Check for permanent visibility.
202 (and gnus-permanently-visible-groups
203 (string-match gnus-permanently-visible-groups group))
204 (memq 'visible params)
205 (cdr (assq 'visible params)))
206 ;; Add this group to the list of visible groups.
207 (push (or entry group) visible-groups)))
208 (nreverse visible-groups)))
210 (defun gnus-topic-previous-topic (topic)
211 "Return the previous topic on the same level as TOPIC."
212 (let ((top (cddr (gnus-topic-find-topology
213 (gnus-topic-parent-topic topic)))))
214 (unless (equal topic (caaar top))
215 (while (and top (not (equal (caaadr top) topic)))
216 (setq top (cdr top)))
219 (defun gnus-topic-parent-topic (topic &optional topology)
220 "Return the parent of TOPIC."
222 (setq topology gnus-topic-topology))
223 (let ((parent (car (pop topology)))
226 (not (setq found (equal (caaar topology) topic)))
227 (not (setq result (gnus-topic-parent-topic topic
229 (setq topology (cdr topology)))
230 (or result (and found parent))))
232 (defun gnus-topic-next-topic (topic &optional previous)
233 "Return the next sibling of TOPIC."
234 (let ((parentt (cddr (gnus-topic-find-topology
235 (gnus-topic-parent-topic topic))))
238 (not (equal (caaar parentt) topic)))
239 (setq prev (caaar parentt)
240 parentt (cdr parentt)))
245 (defun gnus-topic-find-topology (topic &optional topology level remove)
246 "Return the topology of TOPIC."
248 (setq topology gnus-topic-topology)
252 (if (equal (caar topology) topic)
255 (delq topology remove))
256 (cons level topology))
257 (setq topology (cdr topology))
259 (not (setq result (gnus-topic-find-topology
260 topic (car topology) (1+ level)
262 (setq topology (cdr topology)))
265 (defvar gnus-tmp-topics nil)
266 (defun gnus-topic-list (&optional topology)
267 "Return a list of all topics in the topology."
269 (setq topology gnus-topic-topology
270 gnus-tmp-topics nil))
271 (push (caar topology) gnus-tmp-topics)
272 (mapcar 'gnus-topic-list (cdr topology))
275 ;;; Topic parameter jazz
277 (defun gnus-topic-parameters (topic)
278 "Return the parameters for TOPIC."
279 (let ((top (gnus-topic-find-topology topic)))
281 (error "No such topic: %s" topic))
284 (defun gnus-topic-set-parameters (topic parameters)
285 "Set the topic parameters of TOPIC to PARAMETERS."
286 (let ((top (gnus-topic-find-topology topic)))
288 (error "No such topic: %s" topic))
289 ;; We may have to extend if there is no parameters here
291 (unless (nthcdr 2 (cadr top))
292 (nconc (cadr top) (list nil)))
293 (unless (nthcdr 3 (cadr top))
294 (nconc (cadr top) (list nil)))
295 (setcar (nthcdr 3 (cadr top)) parameters)))
297 (defun gnus-group-topic-parameters (group)
298 "Compute the group parameters for GROUP taking into account inheretance from topics."
299 (let ((params-list (list (gnus-group-get-parameter group)))
300 topics params param out)
302 (gnus-group-goto-group group)
303 (setq topics (gnus-current-topics))
305 (push (gnus-topic-parameters (pop topics)) params-list))
306 ;; We probably have lots of nil elements here, so
307 ;; we remove them. Probably faster than doing this "properly".
308 (setq params-list (delq nil params-list))
309 ;; Now we have all the parameters, so we go through them
310 ;; and do inheretance in the obvious way.
311 (while (setq params (pop params-list))
312 (while (setq param (pop params))
314 (setq param (cons param t)))
315 ;; Override any old versions of this param.
316 (setq out (delq (assq (car param) out) out))
318 ;; Return the resulting parameter list.
321 ;;; General utility funtions
323 (defun gnus-topic-enter-dribble ()
325 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
327 ;;; Generating group buffers
329 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
330 "List all newsgroups with unread articles of level LEVEL or lower, and
331 use the `gnus-group-topics' to sort the groups.
332 If ALL is non-nil, list groups that have no unread articles.
333 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
334 (set-buffer gnus-group-buffer)
335 (let ((buffer-read-only nil)
336 (lowest (or lowest 1)))
338 (setq gnus-topic-tallied-groups nil)
340 (when (or (not gnus-topic-alist)
341 (not gnus-topology-checked-p))
342 (gnus-topic-check-topology))
348 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
349 (gnus-group-prepare-flat-list-dead
350 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
354 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
355 (gnus-group-prepare-flat-list-dead
356 (setq gnus-killed-list (sort gnus-killed-list 'string<))
361 (when (< lowest gnus-level-zombie)
363 (let ((top (gnus-topic-find-topology list-topic)))
364 (gnus-topic-prepare-topic (cdr top) (car top)
365 (or topic-level level) all))
366 (gnus-topic-prepare-topic gnus-topic-topology 0
367 (or topic-level level) all))))
369 (gnus-group-set-mode-line)
370 (setq gnus-group-list-mode (cons level all))
371 (run-hooks 'gnus-group-prepare-hook))
373 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
374 "Insert TOPIC into the group buffer.
375 If SILENT, don't insert anything. Return the number of unread
376 articles in the topic and its subtopics."
377 (let* ((type (pop topicl))
378 (entries (gnus-topic-find-groups (car type) list-level all))
379 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
380 (gnus-group-indentation
381 (make-string (* gnus-topic-indent-level level) ? ))
382 (beg (progn (beginning-of-line) (point)))
383 (topicl (reverse topicl))
384 (all-entries entries)
387 info entry end active)
388 ;; Insert any sub-topics.
391 (gnus-topic-prepare-topic
392 (pop topicl) (1+ level) list-level all
396 ;; Insert all the groups that belong in this topic.
397 (while (setq entry (pop entries))
401 (gnus-group-insert-group-line
402 entry (if (member entry gnus-zombie-list) 8 9)
403 nil (- (1+ (cdr (setq active (gnus-active entry))))
406 (when (setq info (nth 2 entry))
407 (gnus-group-insert-group-line
408 (gnus-info-group info)
409 (gnus-info-level info) (gnus-info-marks info)
410 (car entry) (gnus-info-method info)))))
411 (when (and (listp entry)
412 (numberp (car entry))
413 (not (member (gnus-info-group (setq info (nth 2 entry)))
414 gnus-topic-tallied-groups)))
415 (push (gnus-info-group info) gnus-topic-tallied-groups)
416 (incf unread (car entry))))
418 ;; Insert the topic line.
419 (when (and (not silent)
420 (or gnus-topic-display-empty-topics
421 (not (zerop unread))))
422 (gnus-extent-start-open (point))
423 (gnus-topic-insert-topic-line
425 (not (eq (nth 2 type) 'hidden))
426 level all-entries unread))
430 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
431 "Remove the current topic."
432 (let ((topic (gnus-group-topic-name))
433 (level (gnus-group-topic-level))
434 (beg (progn (beginning-of-line) (point)))
437 (while (and (zerop (forward-line 1))
438 (> (or (gnus-group-topic-level) (1+ level)) level)))
439 (delete-region beg (point))
440 (setcar (cdadr (gnus-topic-find-topology topic))
441 (if insert 'visible 'invisible))
443 (setcdr (cdadr (gnus-topic-find-topology topic))
446 (gnus-topic-insert-topic topic in-level)))))
448 (defun gnus-topic-insert-topic (topic &optional level)
450 (gnus-group-prepare-topics
451 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
452 nil nil topic level))
454 (defun gnus-topic-fold (&optional insert)
455 "Remove/insert the current topic."
456 (let ((topic (gnus-group-topic-name)))
459 (if (not (gnus-group-active-topic-p))
460 (gnus-topic-remove-topic
461 (or insert (not (gnus-topic-visible-p))))
462 (let ((gnus-topic-topology gnus-topic-active-topology)
463 (gnus-topic-alist gnus-topic-active-alist)
464 (gnus-group-list-mode (cons 5 t)))
465 (gnus-topic-remove-topic
466 (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
468 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
470 (let* ((visible (if visiblep "" "..."))
471 (indentation (make-string (* gnus-topic-indent-level level) ? ))
472 (total-number-of-articles unread)
473 (number-of-groups (length entries))
474 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
477 (gnus-add-text-properties
480 (eval gnus-topic-line-format-spec)
481 (gnus-topic-remove-excess-properties)1)
482 (list 'gnus-topic (intern name)
483 'gnus-topic-level level
484 'gnus-topic-unread unread
485 'gnus-active active-topic
486 'gnus-topic-visible visiblep))))
488 (defun gnus-topic-update-topics-containing-group (group)
489 "Update all topics that have GROUP as a member."
490 (when (and (eq major-mode 'gnus-group-mode)
493 (let ((alist gnus-topic-alist))
494 ;; This is probably not entirely correct. If a topic
495 ;; isn't shown, then it's not updated. But the updating
496 ;; should be performed in any case, since the topic's
497 ;; parent should be updated. Pfft.
499 (when (and (member group (cdar alist))
500 (gnus-topic-goto-topic (caar alist)))
501 (gnus-topic-update-topic-line (caar alist)))
504 (defun gnus-topic-update-topic ()
505 "Update all parent topics to the current group."
506 (when (and (eq major-mode 'gnus-group-mode)
508 (let ((group (gnus-group-group-name))
509 (buffer-read-only nil))
511 (gnus-get-info group)
512 (gnus-topic-goto-topic (gnus-current-topic)))
513 (gnus-topic-update-topic-line (gnus-group-topic-name))
514 (gnus-group-goto-group group)
515 (gnus-group-position-point)))))
517 (defun gnus-topic-goto-missing-group (group)
518 "Place point where GROUP is supposed to be inserted."
519 (let* ((topic (gnus-group-topic group))
520 (groups (cdr (assoc topic gnus-topic-alist)))
521 (g (cdr (member group groups)))
523 ;; Try to jump to a visible group.
524 (while (and g (not (gnus-group-goto-group (car g) t)))
526 ;; It wasn't visible, so we try to see where to insert it.
528 (setq g (cdr (member group (reverse groups))))
529 (while (and g unfound)
530 (when (gnus-group-goto-group (pop g) t)
534 (gnus-topic-goto-topic topic)
537 (defun gnus-topic-update-topic-line (topic-name &optional reads)
538 (let* ((top (gnus-topic-find-topology topic-name))
540 (children (cddr top))
541 (entries (gnus-topic-find-groups
542 (car type) (car gnus-group-list-mode)
543 (cdr gnus-group-list-mode)))
544 (parent (gnus-topic-parent-topic topic-name))
545 (all-entries entries)
548 (when (gnus-topic-goto-topic (car type))
549 ;; Tally all the groups that belong in this topic.
551 (setq unread (- (gnus-group-topic-unread) reads))
553 (incf unread (gnus-topic-unread (caar (pop children)))))
554 (while (setq entry (pop entries))
555 (when (numberp (car entry))
556 (incf unread (car entry)))))
557 (setq old-unread (gnus-group-topic-unread))
558 ;; Insert the topic line.
559 (gnus-topic-insert-topic-line
560 (car type) (gnus-topic-visible-p)
561 (not (eq (nth 2 type) 'hidden))
562 (gnus-group-topic-level) all-entries unread)
566 (gnus-topic-update-topic-line
567 parent (- old-unread (gnus-group-topic-unread))))
570 (defun gnus-topic-group-indentation ()
572 (* gnus-topic-indent-level
574 (gnus-topic-goto-topic (gnus-current-topic))
575 (gnus-group-topic-level)) 0)) ? ))
579 (gnus-add-shutdown 'gnus-topic-close 'gnus)
581 (defun gnus-topic-close ()
582 (setq gnus-topic-active-topology nil
583 gnus-topic-active-alist nil
584 gnus-topic-killed-topics nil
585 gnus-topic-tallied-groups nil
586 gnus-topology-checked-p nil))
588 (defun gnus-topic-check-topology ()
589 ;; The first time we set the topology to whatever we have
590 ;; gotten here, which can be rather random.
591 (unless gnus-topic-alist
592 (gnus-topic-init-alist))
594 (setq gnus-topology-checked-p t)
595 ;; Go through the topic alist and make sure that all topics
596 ;; are in the topic topology.
597 (let ((topics (gnus-topic-list))
598 (alist gnus-topic-alist)
601 (unless (member (caar alist) topics)
602 (nconc gnus-topic-topology
603 (list (list (list (caar alist) 'visible))))
605 (setq alist (cdr alist)))
607 (gnus-topic-enter-dribble))
608 ;; Conversely, go through the topology and make sure that all
609 ;; topologies have alists.
611 (unless (assoc (car topics) gnus-topic-alist)
612 (push (list (car topics)) gnus-topic-alist))
614 ;; Go through all living groups and make sure that
615 ;; they belong to some topic.
616 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
618 (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
619 (newsrc gnus-newsrc-alist)
622 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
623 (setcdr entry (cons group (cdr entry))))))
624 ;; Go through all topics and make sure they contain only living groups.
625 (let ((alist gnus-topic-alist)
627 (while (setq topic (pop alist))
629 (if (gnus-gethash (cadr topic) gnus-newsrc-hashtb)
630 (setq topic (cdr topic))
631 (setcdr topic (cddr topic)))))))
633 (defun gnus-topic-init-alist ()
634 "Initialize the topic structures."
635 (setq gnus-topic-topology
636 (cons (list "Gnus" 'visible)
637 (mapcar (lambda (topic)
638 (list (list (car topic) 'visible)))
640 (setq gnus-topic-alist
642 (mapcar (lambda (info) (gnus-info-group info))
643 (cdr gnus-newsrc-alist)))
645 (gnus-topic-enter-dribble))
649 (defun gnus-topic-clean-alist ()
650 "Remove bogus groups from the topic alist."
651 (let ((topic-alist gnus-topic-alist)
653 (unless gnus-killed-hashtb
654 (gnus-make-hashtable-from-killed))
655 (while (setq topic (pop topic-alist))
656 (let ((topic-name (pop topic))
657 group filtered-topic)
658 (while (setq group (pop topic))
659 (if (and (or (gnus-gethash group gnus-active-hashtb)
660 (gnus-info-method (gnus-get-info group)))
661 (not (gnus-gethash group gnus-killed-hashtb)))
662 (push group filtered-topic)))
663 (push (cons topic-name (nreverse filtered-topic)) result)))
664 (setq gnus-topic-alist (nreverse result))))
666 (defun gnus-topic-change-level (group level oldlevel)
667 "Run when changing levels to enter/remove groups from topics."
669 (set-buffer gnus-group-buffer)
670 (when (and gnus-topic-mode
672 (not gnus-topic-inhibit-change-level))
673 ;; Remove the group from the topics.
674 (when (and (< oldlevel gnus-level-zombie)
675 (>= level gnus-level-zombie))
678 (when (setq alist (assoc (gnus-current-topic) gnus-topic-alist))
679 (setcdr alist (gnus-delete-first group (cdr alist))))))
680 ;; If the group is subscribed. then we enter it into the topics.
681 (when (and (< level gnus-level-zombie)
682 (>= oldlevel gnus-level-zombie))
683 (let* ((prev (gnus-group-group-name))
684 (gnus-topic-inhibit-change-level t)
685 (gnus-group-indentation
687 (* gnus-topic-indent-level
689 (gnus-topic-goto-topic (gnus-current-topic))
690 (gnus-group-topic-level)) 0)) ? ))
691 (yanked (list group))
693 ;; Then we enter the yanked groups into the topics they belong
695 (when (setq alist (assoc (save-excursion
699 (caar gnus-topic-topology)))
702 (when (stringp yanked)
703 (setq yanked (list yanked)))
706 (if (not (cdr alist))
707 (setcdr alist (nconc yanked (cdr alist)))
708 (while (and (not end) (cdr alist))
709 (when (equal (cadr alist) prev)
710 (setcdr alist (nconc yanked (cdr alist)))
712 (setq alist (cdr alist)))
714 (nconc talist yanked))))))
715 (gnus-topic-update-topic)))))
717 (defun gnus-topic-goto-next-group (group props)
718 "Go to group or the next group after group."
720 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props))))
721 (if (gnus-group-goto-group group)
723 ;; The group is no longer visible.
724 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
725 (after (cdr (member group (cdr list)))))
726 ;; First try to put point on a group after the current one.
728 (not (gnus-group-goto-group (car after))))
729 (setq after (cdr after)))
730 ;; Then try to put point on a group before point.
732 (setq after (cdr (member group (reverse (cdr list)))))
734 (not (gnus-group-goto-group (car after))))
735 (setq after (cdr after))))
736 ;; Finally, just put point on the topic.
738 (gnus-topic-goto-topic (car list))
742 ;;; Topic-active functions
744 (defun gnus-topic-grok-active (&optional force)
745 "Parse all active groups and create topic structures for them."
746 ;; First we make sure that we have really read the active file.
748 (not gnus-topic-active-alist))
750 ;; Get a list of all groups available.
751 (mapatoms (lambda (g) (when (symbol-value g)
752 (push (symbol-name g) groups)))
754 (setq groups (sort groups 'string<))
755 ;; Init the variables.
756 (setq gnus-topic-active-topology (list (list "" 'visible)))
757 (setq gnus-topic-active-alist nil)
758 ;; Descend the top-level hierarchy.
759 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
760 ;; Set the top-level topic names to something nice.
761 (setcar (car gnus-topic-active-topology) "Gnus active")
762 (setcar (car gnus-topic-active-alist) "Gnus active"))))
764 (defun gnus-topic-grok-active-1 (topology groups)
765 (let* ((name (caar topology))
766 (prefix (concat "^" (regexp-quote name)))
767 tgroups ntopology group)
769 (string-match prefix (setq group (car groups))))
770 (if (not (string-match "\\." group (match-end 0)))
771 ;; There are no further hierarchies here, so we just
772 ;; enter this group into the list belonging to this
774 (push (pop groups) tgroups)
775 ;; New sub-hierarchy, so we add it to the topology.
776 (nconc topology (list (setq ntopology
777 (list (list (substring
778 group 0 (match-end 0))
780 ;; Descend the hierarchy.
781 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
782 ;; We remove the trailing "." from the topic name.
784 (if (string-match "\\.$" name)
785 (substring name 0 (match-beginning 0))
787 ;; Add this topic and its groups to the topic alist.
788 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
789 (setcar (car topology) name)
790 ;; We return the rest of the groups that didn't belong
794 ;;; Topic mode, commands and keymap.
796 (defvar gnus-topic-mode-map nil)
797 (defvar gnus-group-topic-map nil)
799 (unless gnus-topic-mode-map
800 (setq gnus-topic-mode-map (make-sparse-keymap))
802 ;; Override certain group mode keys.
805 "=" gnus-topic-select-group
806 "\r" gnus-topic-select-group
807 " " gnus-topic-read-group
808 "\C-k" gnus-topic-kill-group
809 "\C-y" gnus-topic-yank-group
810 "\M-g" gnus-topic-get-new-news-this-topic
811 "AT" gnus-topic-list-active
812 "Gp" gnus-topic-edit-parameters
813 gnus-mouse-2 gnus-mouse-pick-topic)
815 ;; Define a new submap.
817 (gnus-group-topic-map "T" gnus-group-mode-map)
818 "#" gnus-topic-mark-topic
819 "\M-#" gnus-topic-unmark-topic
820 "n" gnus-topic-create-topic
821 "m" gnus-topic-move-group
822 "D" gnus-topic-remove-group
823 "c" gnus-topic-copy-group
824 "h" gnus-topic-hide-topic
825 "s" gnus-topic-show-topic
826 "M" gnus-topic-move-matching
827 "C" gnus-topic-copy-matching
828 "\C-i" gnus-topic-indent
829 [tab] gnus-topic-indent
830 "r" gnus-topic-rename
831 "\177" gnus-topic-delete))
833 (defun gnus-topic-make-menu-bar ()
834 (unless (boundp 'gnus-topic-menu)
836 gnus-topic-menu gnus-topic-mode-map ""
838 ["Toggle topics" gnus-topic-mode t]
840 ["Copy" gnus-topic-copy-group t]
841 ["Move" gnus-topic-move-group t]
842 ["Remove" gnus-topic-remove-group t]
843 ["Copy matching" gnus-topic-copy-matching t]
844 ["Move matching" gnus-topic-move-matching t])
846 ["Show" gnus-topic-show-topic t]
847 ["Hide" gnus-topic-hide-topic t]
848 ["Delete" gnus-topic-delete t]
849 ["Rename" gnus-topic-rename t]
850 ["Create" gnus-topic-create-topic t]
851 ["Mark" gnus-topic-mark-topic t]
852 ["Indent" gnus-topic-indent t])
853 ["List active" gnus-topic-list-active t]))))
855 (defun gnus-topic-mode (&optional arg redisplay)
856 "Minor mode for topicsifying Gnus group buffers."
857 (interactive (list current-prefix-arg t))
858 (when (eq major-mode 'gnus-group-mode)
859 (make-local-variable 'gnus-topic-mode)
860 (setq gnus-topic-mode
861 (if (null arg) (not gnus-topic-mode)
862 (> (prefix-numeric-value arg) 0)))
863 ;; Infest Gnus with topics.
864 (when gnus-topic-mode
865 (when (and menu-bar-mode
866 (gnus-visual-p 'topic-menu 'menu))
867 (gnus-topic-make-menu-bar))
868 (setq gnus-topic-line-format-spec
869 (gnus-parse-format gnus-topic-line-format
870 gnus-topic-line-format-alist t))
871 (unless (assq 'gnus-topic-mode minor-mode-alist)
872 (push '(gnus-topic-mode " Topic") minor-mode-alist))
873 (unless (assq 'gnus-topic-mode minor-mode-map-alist)
874 (push (cons 'gnus-topic-mode gnus-topic-mode-map)
875 minor-mode-map-alist))
876 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
877 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
878 (set (make-local-variable 'gnus-group-prepare-function)
879 'gnus-group-prepare-topics)
880 (set (make-local-variable 'gnus-group-get-parameter-function)
881 'gnus-group-topic-parameters)
882 (set (make-local-variable 'gnus-group-goto-next-group-function)
883 'gnus-topic-goto-next-group)
884 (set (make-local-variable 'gnus-group-indentation-function)
885 'gnus-topic-group-indentation)
886 (set (make-local-variable 'gnus-group-update-group-function)
887 'gnus-topic-update-topics-containing-group)
888 (setq gnus-group-change-level-function 'gnus-topic-change-level)
889 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
890 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
891 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
892 (setq gnus-topology-checked-p nil)
893 ;; We check the topology.
894 (when gnus-newsrc-alist
895 (gnus-topic-check-topology))
896 (run-hooks 'gnus-topic-mode-hook))
897 ;; Remove topic infestation.
898 (unless gnus-topic-mode
899 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
900 (remove-hook 'gnus-group-change-level-function
901 'gnus-topic-change-level)
902 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
903 (setq gnus-group-prepare-function 'gnus-group-prepare-flat))
905 (gnus-group-list-groups))))
907 (defun gnus-topic-select-group (&optional all)
908 "Select this newsgroup.
909 No article is selected automatically.
910 If ALL is non-nil, already read articles become readable.
911 If ALL is a number, fetch this number of articles.
913 If performed over a topic line, toggle folding the topic."
915 (if (gnus-group-topic-p)
916 (let ((gnus-group-list-mode
917 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
918 (gnus-topic-fold all))
919 (gnus-group-select-group all)))
921 (defun gnus-mouse-pick-topic (e)
922 "Select the group or topic under the mouse pointer."
925 (gnus-topic-read-group nil))
927 (defun gnus-topic-read-group (&optional all no-article group)
928 "Read news in this newsgroup.
929 If the prefix argument ALL is non-nil, already read articles become
930 readable. IF ALL is a number, fetch this number of articles. If the
931 optional argument NO-ARTICLE is non-nil, no article will be
932 auto-selected upon group entry. If GROUP is non-nil, fetch that
935 If performed over a topic line, toggle folding the topic."
937 (if (gnus-group-topic-p)
938 (let ((gnus-group-list-mode
939 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
940 (gnus-topic-fold all))
941 (gnus-group-read-group all no-article group)))
943 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
946 (read-string "New topic: ")
947 (gnus-current-topic)))
948 ;; Check whether this topic already exists.
949 (when (gnus-topic-find-topology topic)
950 (error "Topic aleady exists"))
952 (setq parent (caar gnus-topic-topology)))
953 (let ((top (cdr (gnus-topic-find-topology parent)))
954 (full-topic (or full-topic `((,topic visible)))))
956 (error "No such parent topic: %s" parent))
959 (while (and (cdr top)
960 (not (equal (caaadr top) previous)))
961 (setq top (cdr top)))
962 (setcdr top (cons full-topic (cdr top))))
963 (nconc top (list full-topic)))
964 (unless (assoc topic gnus-topic-alist)
965 (push (list topic) gnus-topic-alist)))
966 (gnus-topic-enter-dribble)
967 (gnus-group-list-groups)
968 (gnus-topic-goto-topic topic))
970 (defun gnus-topic-move-group (n topic &optional copyp)
971 "Move the next N groups to TOPIC.
972 If COPYP, copy the groups instead."
974 (list current-prefix-arg
975 (completing-read "Move to topic: " gnus-topic-alist nil t)))
976 (let ((groups (gnus-group-process-prefix n))
977 (topicl (assoc topic gnus-topic-alist))
978 (start-group (progn (forward-line 1) (gnus-group-group-name)))
979 (start-topic (gnus-group-topic-name))
983 (gnus-group-remove-mark g)
985 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
987 (setcdr entry (gnus-delete-first g (cdr entry))))
988 (nconc topicl (list g)))
990 (gnus-topic-enter-dribble)
992 (gnus-group-goto-group start-group)
993 (gnus-topic-goto-topic start-topic))
994 (gnus-group-list-groups)))
996 (defun gnus-topic-remove-group ()
997 "Remove the current group from the topic."
999 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1000 (group (gnus-group-group-name))
1001 (buffer-read-only nil))
1002 (when (and topicl group)
1004 (gnus-delete-first group topicl))
1005 (gnus-topic-update-topic)
1006 (gnus-group-position-point)))
1008 (defun gnus-topic-copy-group (n topic)
1009 "Copy the current group to a topic."
1011 (list current-prefix-arg
1012 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1013 (gnus-topic-move-group n topic t))
1015 (defun gnus-topic-kill-group (&optional n discard)
1016 "Kill the next N groups."
1018 (if (gnus-group-topic-p)
1019 (let ((topic (gnus-group-topic-name)))
1020 (gnus-topic-remove-topic nil t)
1021 (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1022 gnus-topic-killed-topics))
1023 (gnus-group-kill-group n discard)
1024 (gnus-topic-update-topic)))
1026 (defun gnus-topic-yank-group (&optional arg)
1027 "Yank the last topic."
1029 (if gnus-topic-killed-topics
1031 (or (gnus-group-topic-name)
1032 (gnus-topic-next-topic (gnus-current-topic))))
1033 (item (cdr (pop gnus-topic-killed-topics))))
1034 (gnus-topic-create-topic
1035 (caar item) (gnus-topic-parent-topic previous) previous
1037 (gnus-topic-goto-topic (caar item)))
1038 (let* ((prev (gnus-group-group-name))
1039 (gnus-topic-inhibit-change-level t)
1040 (gnus-group-indentation
1042 (* gnus-topic-indent-level
1044 (gnus-topic-goto-topic (gnus-current-topic))
1045 (gnus-group-topic-level)) 0)) ? ))
1047 ;; We first yank the groups the normal way...
1048 (setq yanked (gnus-group-yank-group arg))
1049 ;; Then we enter the yanked groups into the topics they belong
1051 (setq alist (assoc (save-excursion
1053 (gnus-current-topic))
1055 (when (stringp yanked)
1056 (setq yanked (list yanked)))
1058 (nconc alist yanked)
1059 (if (not (cdr alist))
1060 (setcdr alist (nconc yanked (cdr alist)))
1062 (when (equal (cadr alist) prev)
1063 (setcdr alist (nconc yanked (cdr alist)))
1065 (setq alist (cdr alist))))))
1066 (gnus-topic-update-topic)))
1068 (defun gnus-topic-hide-topic ()
1069 "Hide the current topic."
1071 (when (gnus-current-topic)
1072 (gnus-topic-goto-topic (gnus-current-topic))
1073 (gnus-topic-remove-topic nil nil 'hidden)))
1075 (defun gnus-topic-show-topic ()
1076 "Show the hidden topic."
1078 (when (gnus-group-topic-p)
1079 (gnus-topic-remove-topic t nil 'shown)))
1081 (defun gnus-topic-mark-topic (topic &optional unmark)
1082 "Mark all groups in the topic with the process mark."
1083 (interactive (list (gnus-current-topic)))
1085 (let ((groups (gnus-topic-find-groups topic 9 t)))
1087 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1088 (gnus-info-group (nth 2 (pop groups))))))))
1090 (defun gnus-topic-unmark-topic (topic &optional unmark)
1091 "Remove the process mark from all groups in the topic."
1092 (interactive (list (gnus-current-topic)))
1093 (gnus-topic-mark-topic topic t))
1095 (defun gnus-topic-get-new-news-this-topic (&optional n)
1096 "Check for new news in the current topic."
1098 (if (not (gnus-group-topic-p))
1099 (gnus-group-get-new-news-this-group n)
1100 (gnus-topic-mark-topic (gnus-group-topic-name))
1101 (gnus-group-get-new-news-this-group)))
1103 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1104 "Move all groups that match REGEXP to some topic."
1109 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1110 (read-string (format "Move to %s (regexp): " topic))))))
1111 (gnus-group-mark-regexp regexp)
1112 (gnus-topic-move-group nil topic copyp))
1114 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1115 "Copy all groups that match REGEXP to some topic."
1120 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1121 (read-string (format "Copy to %s (regexp): " topic))))))
1122 (gnus-topic-move-matching regexp topic t))
1124 (defun gnus-topic-delete (topic)
1126 (interactive (list (gnus-group-topic-name)))
1128 (error "No topic to be deleted"))
1129 (let ((entry (assoc topic gnus-topic-alist))
1130 (buffer-read-only nil))
1132 (error "Topic not empty"))
1133 ;; Delete if visible.
1134 (when (gnus-topic-goto-topic topic)
1136 ;; Remove from alist.
1137 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1138 ;; Remove from topology.
1139 (gnus-topic-find-topology topic nil nil 'delete)))
1141 (defun gnus-topic-rename (old-name new-name)
1144 (let ((topic (gnus-current-topic)))
1146 (read-string (format "Rename %s to: " topic)))))
1147 (let ((top (gnus-topic-find-topology old-name))
1148 (entry (assoc old-name gnus-topic-alist)))
1150 (setcar (cadr top) new-name))
1152 (setcar entry new-name))
1154 (gnus-group-list-groups)))
1156 (defun gnus-topic-indent (&optional unindent)
1157 "Indent a topic -- make it a sub-topic of the previous topic.
1158 If UNINDENT, remove an indentation."
1161 (gnus-topic-unindent)
1162 (let* ((topic (gnus-current-topic))
1163 (parent (gnus-topic-previous-topic topic)))
1165 (error "Nothing to indent %s into" topic))
1167 (gnus-topic-goto-topic topic)
1168 (gnus-topic-kill-group)
1169 (gnus-topic-create-topic
1170 topic parent nil (cdr (pop gnus-topic-killed-topics)))
1171 (or (gnus-topic-goto-topic topic)
1172 (gnus-topic-goto-topic parent))))))
1174 (defun gnus-topic-unindent ()
1177 (let* ((topic (gnus-current-topic))
1178 (parent (gnus-topic-parent-topic topic))
1179 (grandparent (gnus-topic-parent-topic parent)))
1181 (error "Nothing to indent %s into" topic))
1183 (gnus-topic-goto-topic topic)
1184 (gnus-topic-kill-group)
1185 (gnus-topic-create-topic
1186 topic grandparent (gnus-topic-next-topic parent)
1187 (cdr (pop gnus-topic-killed-topics)))
1188 (gnus-topic-goto-topic topic))))
1190 (defun gnus-topic-list-active (&optional force)
1191 "List all groups that Gnus knows about in a topicsified fashion.
1192 If FORCE, always re-read the active file."
1195 (gnus-get-killed-groups))
1196 (gnus-topic-grok-active force)
1197 (let ((gnus-topic-topology gnus-topic-active-topology)
1198 (gnus-topic-alist gnus-topic-active-alist)
1199 gnus-killed-list gnus-zombie-list)
1200 (gnus-group-list-groups 9 nil 1)))
1202 (defun gnus-topic-edit-parameters (group)
1203 "Edit the group parameters of GROUP.
1204 If performed on a topic, edit the topic parameters instead."
1205 (interactive (list (gnus-group-group-name)))
1207 (gnus-group-edit-group-parameters group)
1208 (if (not (gnus-group-topic-p))
1209 (error "Nothing to edit on the current line.")
1210 (let ((topic (gnus-group-topic-name)))
1212 (gnus-topic-parameters topic)
1213 "Editing the topic parameters."
1215 (gnus-topic-set-parameters ,topic form)))))))
1217 (provide 'gnus-topic)
1219 ;;; gnus-topic.el ends here