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.
33 (defgroup gnus-topic nil
37 (defvar gnus-topic-mode nil
38 "Minor mode for Gnus group buffers.")
40 (defcustom gnus-topic-mode-hook nil
41 "Hook run in topic mode buffers."
45 (defvar gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
46 "Format of topic lines.
47 It works along the same lines as a normal formatting string,
48 with some simple extensions.
50 %i Indentation based on topic level.
52 %v Nothing if the topic is visible, \"...\" otherwise.
53 %g Number of groups in the topic.
54 %a Number of unread articles in the groups in the topic.
55 %A Number of unread articles in the groups in the topic and its subtopics.
58 (defcustom gnus-topic-indent-level 2
59 "*How much each subtopic should be indented."
63 (defcustom gnus-topic-display-empty-topics t
64 "*If non-nil, display the topic lines even of topics that have no unread articles."
68 ;; Internal variables.
70 (defvar gnus-topic-active-topology nil)
71 (defvar gnus-topic-active-alist nil)
73 (defvar gnus-topology-checked-p nil
74 "Whether the topology has been checked in this session.")
76 (defvar gnus-topic-killed-topics nil)
77 (defvar gnus-topic-inhibit-change-level nil)
78 (defvar gnus-topic-tallied-groups nil)
80 (defconst gnus-topic-line-format-alist
84 (?g number-of-groups ?d)
85 (?a (gnus-topic-articles-in-topic entries) ?d)
86 (?A total-number-of-articles ?d)
89 (defvar gnus-topic-line-format-spec nil)
93 (defun gnus-group-topic-name ()
94 "The name of the topic on the current line."
95 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
96 (and topic (symbol-name topic))))
98 (defun gnus-group-topic-level ()
99 "The level of the topic on the current line."
100 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
102 (defun gnus-group-topic-unread ()
103 "The number of unread articles in topic on the current line."
104 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
106 (defun gnus-topic-unread (topic)
107 "Return the number of unread articles in TOPIC."
109 (and (gnus-topic-goto-topic topic)
110 (gnus-group-topic-unread)))
113 (defun gnus-group-topic-p ()
114 "Return non-nil if the current line is a topic."
115 (gnus-group-topic-name))
117 (defun gnus-topic-visible-p ()
118 "Return non-nil if the current topic is visible."
119 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
121 (defun gnus-topic-articles-in-topic (entries)
125 (when (numberp (setq number (car (pop entries))))
126 (incf total number)))
129 (defun gnus-group-topic (group)
130 "Return the topic GROUP is a member of."
131 (let ((alist gnus-topic-alist)
134 (when (member group (cdar alist))
135 (setq out (caar alist)
137 (setq alist (cdr alist)))
140 (defun gnus-group-parent-topic (group)
141 "Return the topic GROUP is member of by looking at the group buffer."
143 (set-buffer gnus-group-buffer)
144 (if (gnus-group-goto-group group)
146 (gnus-group-topic group))))
148 (defun gnus-topic-goto-topic (topic)
151 (gnus-goto-char (text-property-any (point-min) (point-max)
152 'gnus-topic (intern topic)))))
154 (defun gnus-current-topic ()
155 "Return the name of the current topic."
157 (or (get-text-property (point) 'gnus-topic)
159 (and (gnus-goto-char (previous-single-property-change
160 (point) 'gnus-topic))
161 (get-text-property (max (1- (point)) (point-min))
164 (symbol-name result))))
166 (defun gnus-current-topics ()
167 "Return a list of all current topics, lowest in hierarchy first."
168 (let ((topic (gnus-current-topic))
172 (setq topic (gnus-topic-parent-topic topic)))
175 (defun gnus-group-active-topic-p ()
176 "Say whether the current topic comes from the active topics."
179 (get-text-property (point) 'gnus-active)))
181 (defun gnus-topic-find-groups (topic &optional level all)
182 "Return entries for all visible groups in TOPIC."
183 (let ((groups (cdr (assoc topic gnus-topic-alist)))
184 info clevel unread group lowest params visible-groups entry active)
185 (setq lowest (or lowest 1))
186 (setq level (or level 7))
187 ;; We go through the newsrc to look for matches.
189 (setq entry (gnus-gethash (setq group (pop groups)) gnus-newsrc-hashtb)
191 params (gnus-info-params info)
192 active (gnus-active group)
193 unread (or (car entry)
194 (and (not (equal group "dummy.group"))
196 (- (1+ (cdr active)) (car active))))
197 clevel (or (gnus-info-level info)
198 (if (member group gnus-zombie-list) 8 9)))
200 unread ; nil means that the group is dead.
202 (>= clevel lowest) ; Is inside the level we want.
205 gnus-group-list-inactive-groups
207 (and gnus-list-groups-with-ticked-articles
208 (cdr (assq 'tick (gnus-info-marks info))))
209 ; Has right readedness.
210 ;; Check for permanent visibility.
211 (and gnus-permanently-visible-groups
212 (string-match gnus-permanently-visible-groups group))
213 (memq 'visible params)
214 (cdr (assq 'visible params)))
215 ;; Add this group to the list of visible groups.
216 (push (or entry group) visible-groups)))
217 (nreverse visible-groups)))
219 (defun gnus-topic-previous-topic (topic)
220 "Return the previous topic on the same level as TOPIC."
221 (let ((top (cddr (gnus-topic-find-topology
222 (gnus-topic-parent-topic topic)))))
223 (unless (equal topic (caaar top))
224 (while (and top (not (equal (caaadr top) topic)))
225 (setq top (cdr top)))
228 (defun gnus-topic-parent-topic (topic &optional topology)
229 "Return the parent of TOPIC."
231 (setq topology gnus-topic-topology))
232 (let ((parent (car (pop topology)))
235 (not (setq found (equal (caaar topology) topic)))
236 (not (setq result (gnus-topic-parent-topic topic
238 (setq topology (cdr topology)))
239 (or result (and found parent))))
241 (defun gnus-topic-next-topic (topic &optional previous)
242 "Return the next sibling of TOPIC."
243 (let ((parentt (cddr (gnus-topic-find-topology
244 (gnus-topic-parent-topic topic))))
247 (not (equal (caaar parentt) topic)))
248 (setq prev (caaar parentt)
249 parentt (cdr parentt)))
254 (defun gnus-topic-find-topology (topic &optional topology level remove)
255 "Return the topology of TOPIC."
257 (setq topology gnus-topic-topology)
261 (if (equal (caar topology) topic)
264 (delq topology remove))
265 (cons level topology))
266 (setq topology (cdr topology))
268 (not (setq result (gnus-topic-find-topology
269 topic (car topology) (1+ level)
271 (setq topology (cdr topology)))
274 (defvar gnus-tmp-topics nil)
275 (defun gnus-topic-list (&optional topology)
276 "Return a list of all topics in the topology."
278 (setq topology gnus-topic-topology
279 gnus-tmp-topics nil))
280 (push (caar topology) gnus-tmp-topics)
281 (mapcar 'gnus-topic-list (cdr topology))
284 ;;; Topic parameter jazz
286 (defun gnus-topic-parameters (topic)
287 "Return the parameters for TOPIC."
288 (let ((top (gnus-topic-find-topology topic)))
290 (nth 3 (cadr top)))))
292 (defun gnus-topic-set-parameters (topic parameters)
293 "Set the topic parameters of TOPIC to PARAMETERS."
294 (let ((top (gnus-topic-find-topology topic)))
296 (error "No such topic: %s" topic))
297 ;; We may have to extend if there is no parameters here
299 (unless (nthcdr 2 (cadr top))
300 (nconc (cadr top) (list nil)))
301 (unless (nthcdr 3 (cadr top))
302 (nconc (cadr top) (list nil)))
303 (setcar (nthcdr 3 (cadr top)) parameters)))
305 (defun gnus-group-topic-parameters (group)
306 "Compute the group parameters for GROUP taking into account inheritance from topics."
307 (let ((params-list (list (gnus-group-get-parameter group)))
308 topics params param out)
310 (gnus-group-goto-group group)
311 (setq topics (gnus-current-topics))
313 (push (gnus-topic-parameters (pop topics)) params-list))
314 ;; We probably have lots of nil elements here, so
315 ;; we remove them. Probably faster than doing this "properly".
316 (setq params-list (delq nil params-list))
317 ;; Now we have all the parameters, so we go through them
318 ;; and do inheritance in the obvious way.
319 (while (setq params (pop params-list))
320 (while (setq param (pop params))
322 (setq param (cons param t)))
323 ;; Override any old versions of this param.
324 (setq out (delq (assq (car param) out) out))
326 ;; Return the resulting parameter list.
329 ;;; General utility functions
331 (defun gnus-topic-enter-dribble ()
333 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
335 ;;; Generating group buffers
337 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
338 "List all newsgroups with unread articles of level LEVEL or lower, and
339 use the `gnus-group-topics' to sort the groups.
340 If ALL is non-nil, list groups that have no unread articles.
341 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
342 (set-buffer gnus-group-buffer)
343 (let ((buffer-read-only nil)
344 (lowest (or lowest 1)))
346 (setq gnus-topic-tallied-groups nil)
348 (when (or (not gnus-topic-alist)
349 (not gnus-topology-checked-p))
350 (gnus-topic-check-topology))
356 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
357 (gnus-group-prepare-flat-list-dead
358 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
362 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
363 (gnus-group-prepare-flat-list-dead
364 (setq gnus-killed-list (sort gnus-killed-list 'string<))
369 (when (< lowest gnus-level-zombie)
371 (let ((top (gnus-topic-find-topology list-topic)))
372 (gnus-topic-prepare-topic (cdr top) (car top)
373 (or topic-level level) all))
374 (gnus-topic-prepare-topic gnus-topic-topology 0
375 (or topic-level level) all))))
377 (gnus-group-set-mode-line)
378 (setq gnus-group-list-mode (cons level all))
379 (run-hooks 'gnus-group-prepare-hook))
381 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
382 "Insert TOPIC into the group buffer.
383 If SILENT, don't insert anything. Return the number of unread
384 articles in the topic and its subtopics."
385 (let* ((type (pop topicl))
386 (entries (gnus-topic-find-groups (car type) list-level all))
387 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
388 (gnus-group-indentation
389 (make-string (* gnus-topic-indent-level level) ? ))
390 (beg (progn (beginning-of-line) (point)))
391 (topicl (reverse topicl))
392 (all-entries entries)
393 (point-max (point-max))
396 info entry end active)
397 ;; Insert any sub-topics.
400 (gnus-topic-prepare-topic
401 (pop topicl) (1+ level) list-level all
405 ;; Insert all the groups that belong in this topic.
406 (while (setq entry (pop entries))
410 (gnus-group-insert-group-line
411 entry (if (member entry gnus-zombie-list) 8 9)
412 nil (- (1+ (cdr (setq active (gnus-active entry))))
416 (when (setq info (nth 2 entry))
417 (gnus-group-insert-group-line
418 (gnus-info-group info)
419 (gnus-info-level info) (gnus-info-marks info)
420 (car entry) (gnus-info-method info)))))
421 (when (and (listp entry)
422 (numberp (car entry))
423 (not (member (gnus-info-group (setq info (nth 2 entry)))
424 gnus-topic-tallied-groups)))
425 (push (gnus-info-group info) gnus-topic-tallied-groups)
426 (incf unread (car entry))))
428 ;; Insert the topic line.
429 (when (and (not silent)
430 (or gnus-topic-display-empty-topics
432 (/= point-max (point-max))))
433 (gnus-extent-start-open (point))
434 (gnus-topic-insert-topic-line
436 (not (eq (nth 2 type) 'hidden))
437 level all-entries unread))
441 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
442 "Remove the current topic."
443 (let ((topic (gnus-group-topic-name))
444 (level (gnus-group-topic-level))
445 (beg (progn (beginning-of-line) (point)))
448 (while (and (zerop (forward-line 1))
449 (> (or (gnus-group-topic-level) (1+ level)) level)))
450 (delete-region beg (point))
451 ;; Do the change in this rather odd manner because it has been
452 ;; reported that some topics share parts of some lists, for some
453 ;; reason. I have been unable to determine why this is the
454 ;; case, but this hack seems to take care of things.
455 (let ((data (cadr (gnus-topic-find-topology topic))))
457 (list (if insert 'visible 'invisible)
461 (setq gnus-topic-alist
462 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
463 (gnus-topic-insert-topic topic in-level)))))
465 (defun gnus-topic-insert-topic (topic &optional level)
467 (gnus-group-prepare-topics
468 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
469 nil nil topic level))
471 (defun gnus-topic-fold (&optional insert)
472 "Remove/insert the current topic."
473 (let ((topic (gnus-group-topic-name)))
476 (if (not (gnus-group-active-topic-p))
477 (gnus-topic-remove-topic
478 (or insert (not (gnus-topic-visible-p))))
479 (let ((gnus-topic-topology gnus-topic-active-topology)
480 (gnus-topic-alist gnus-topic-active-alist)
481 (gnus-group-list-mode (cons 5 t)))
482 (gnus-topic-remove-topic
483 (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
485 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
487 (let* ((visible (if visiblep "" "..."))
488 (indentation (make-string (* gnus-topic-indent-level level) ? ))
489 (total-number-of-articles unread)
490 (number-of-groups (length entries))
491 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
494 (gnus-add-text-properties
497 (eval gnus-topic-line-format-spec)
498 (gnus-topic-remove-excess-properties)1)
499 (list 'gnus-topic (intern name)
500 'gnus-topic-level level
501 'gnus-topic-unread unread
502 'gnus-active active-topic
503 'gnus-topic-visible visiblep))))
505 (defun gnus-topic-update-topics-containing-group (group)
506 "Update all topics that have GROUP as a member."
507 (when (and (eq major-mode 'gnus-group-mode)
510 (let ((alist gnus-topic-alist))
511 ;; This is probably not entirely correct. If a topic
512 ;; isn't shown, then it's not updated. But the updating
513 ;; should be performed in any case, since the topic's
514 ;; parent should be updated. Pfft.
516 (when (and (member group (cdar alist))
517 (gnus-topic-goto-topic (caar alist)))
518 (gnus-topic-update-topic-line (caar alist)))
521 (defun gnus-topic-update-topic ()
522 "Update all parent topics to the current group."
523 (when (and (eq major-mode 'gnus-group-mode)
525 (let ((group (gnus-group-group-name))
526 (buffer-read-only nil))
528 (gnus-get-info group)
529 (gnus-topic-goto-topic (gnus-current-topic)))
530 (gnus-topic-update-topic-line (gnus-group-topic-name))
531 (gnus-group-goto-group group)
532 (gnus-group-position-point)))))
534 (defun gnus-topic-goto-missing-group (group)
535 "Place point where GROUP is supposed to be inserted."
536 (let* ((topic (gnus-group-topic group))
537 (groups (cdr (assoc topic gnus-topic-alist)))
538 (g (cdr (member group groups)))
540 ;; Try to jump to a visible group.
541 (while (and g (not (gnus-group-goto-group (car g) t)))
543 ;; It wasn't visible, so we try to see where to insert it.
545 (setq g (cdr (member group (reverse groups))))
546 (while (and g unfound)
547 (when (gnus-group-goto-group (pop g) t)
551 (gnus-topic-goto-topic topic)
554 (defun gnus-topic-update-topic-line (topic-name &optional reads)
555 (let* ((top (gnus-topic-find-topology topic-name))
557 (children (cddr top))
558 (entries (gnus-topic-find-groups
559 (car type) (car gnus-group-list-mode)
560 (cdr gnus-group-list-mode)))
561 (parent (gnus-topic-parent-topic topic-name))
562 (all-entries entries)
565 (when (gnus-topic-goto-topic (car type))
566 ;; Tally all the groups that belong in this topic.
568 (setq unread (- (gnus-group-topic-unread) reads))
570 (incf unread (gnus-topic-unread (caar (pop children)))))
571 (while (setq entry (pop entries))
572 (when (numberp (car entry))
573 (incf unread (car entry)))))
574 (setq old-unread (gnus-group-topic-unread))
575 ;; Insert the topic line.
576 (gnus-topic-insert-topic-line
577 (car type) (gnus-topic-visible-p)
578 (not (eq (nth 2 type) 'hidden))
579 (gnus-group-topic-level) all-entries unread)
583 (gnus-topic-update-topic-line
584 parent (- old-unread (gnus-group-topic-unread))))
587 (defun gnus-topic-group-indentation ()
589 (* gnus-topic-indent-level
592 (gnus-topic-goto-topic (gnus-current-topic))
593 (gnus-group-topic-level))
599 (gnus-add-shutdown 'gnus-topic-close 'gnus)
601 (defun gnus-topic-close ()
602 (setq gnus-topic-active-topology nil
603 gnus-topic-active-alist nil
604 gnus-topic-killed-topics nil
605 gnus-topic-tallied-groups nil
606 gnus-topology-checked-p nil))
608 (defun gnus-topic-check-topology ()
609 ;; The first time we set the topology to whatever we have
610 ;; gotten here, which can be rather random.
611 (unless gnus-topic-alist
612 (gnus-topic-init-alist))
614 (setq gnus-topology-checked-p t)
615 ;; Go through the topic alist and make sure that all topics
616 ;; are in the topic topology.
617 (let ((topics (gnus-topic-list))
618 (alist gnus-topic-alist)
621 (unless (member (caar alist) topics)
622 (nconc gnus-topic-topology
623 (list (list (list (caar alist) 'visible))))
625 (setq alist (cdr alist)))
627 (gnus-topic-enter-dribble))
628 ;; Conversely, go through the topology and make sure that all
629 ;; topologies have alists.
631 (unless (assoc (car topics) gnus-topic-alist)
632 (push (list (car topics)) gnus-topic-alist))
634 ;; Go through all living groups and make sure that
635 ;; they belong to some topic.
636 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
638 (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
639 (newsrc gnus-newsrc-alist)
642 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
643 (setcdr entry (cons group (cdr entry))))))
644 ;; Go through all topics and make sure they contain only living groups.
645 (let ((alist gnus-topic-alist)
647 (while (setq topic (pop alist))
649 (if (gnus-gethash (cadr topic) gnus-newsrc-hashtb)
650 (setq topic (cdr topic))
651 (setcdr topic (cddr topic)))))))
653 (defun gnus-topic-init-alist ()
654 "Initialize the topic structures."
655 (setq gnus-topic-topology
656 (cons (list "Gnus" 'visible)
657 (mapcar (lambda (topic)
658 (list (list (car topic) 'visible)))
660 (setq gnus-topic-alist
662 (mapcar (lambda (info) (gnus-info-group info))
663 (cdr gnus-newsrc-alist)))
665 (gnus-topic-enter-dribble))
669 (defun gnus-topic-clean-alist ()
670 "Remove bogus groups from the topic alist."
671 (let ((topic-alist gnus-topic-alist)
673 (unless gnus-killed-hashtb
674 (gnus-make-hashtable-from-killed))
675 (while (setq topic (pop topic-alist))
676 (let ((topic-name (pop topic))
677 group filtered-topic)
678 (while (setq group (pop topic))
679 (when (and (or (gnus-gethash group gnus-active-hashtb)
680 (gnus-info-method (gnus-get-info group)))
681 (not (gnus-gethash group gnus-killed-hashtb)))
682 (push group filtered-topic)))
683 (push (cons topic-name (nreverse filtered-topic)) result)))
684 (setq gnus-topic-alist (nreverse result))))
686 (defun gnus-topic-change-level (group level oldlevel)
687 "Run when changing levels to enter/remove groups from topics."
689 (set-buffer gnus-group-buffer)
690 (when (and gnus-topic-mode
692 (not gnus-topic-inhibit-change-level))
693 ;; Remove the group from the topics.
694 (when (and (< oldlevel gnus-level-zombie)
695 (>= level gnus-level-zombie))
698 (when (setq alist (assoc (gnus-current-topic) gnus-topic-alist))
699 (setcdr alist (gnus-delete-first group (cdr alist))))))
700 ;; If the group is subscribed. then we enter it into the topics.
701 (when (and (< level gnus-level-zombie)
702 (>= oldlevel gnus-level-zombie))
703 (let* ((prev (gnus-group-group-name))
704 (gnus-topic-inhibit-change-level t)
705 (gnus-group-indentation
707 (* gnus-topic-indent-level
709 (gnus-topic-goto-topic (gnus-current-topic))
710 (gnus-group-topic-level))
713 (yanked (list group))
715 ;; Then we enter the yanked groups into the topics they belong
717 (when (setq alist (assoc (save-excursion
721 (caar gnus-topic-topology)))
724 (when (stringp yanked)
725 (setq yanked (list yanked)))
728 (if (not (cdr alist))
729 (setcdr alist (nconc yanked (cdr alist)))
730 (while (and (not end) (cdr alist))
731 (when (equal (cadr alist) prev)
732 (setcdr alist (nconc yanked (cdr alist)))
734 (setq alist (cdr alist)))
736 (nconc talist yanked))))))
737 (gnus-topic-update-topic)))))
739 (defun gnus-topic-goto-next-group (group props)
740 "Go to group or the next group after group."
742 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props))))
743 (if (gnus-group-goto-group group)
745 ;; The group is no longer visible.
746 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
747 (after (cdr (member group (cdr list)))))
748 ;; First try to put point on a group after the current one.
750 (not (gnus-group-goto-group (car after))))
751 (setq after (cdr after)))
752 ;; Then try to put point on a group before point.
754 (setq after (cdr (member group (reverse (cdr list)))))
756 (not (gnus-group-goto-group (car after))))
757 (setq after (cdr after))))
758 ;; Finally, just put point on the topic.
760 (gnus-topic-goto-topic (car list))
764 ;;; Topic-active functions
766 (defun gnus-topic-grok-active (&optional force)
767 "Parse all active groups and create topic structures for them."
768 ;; First we make sure that we have really read the active file.
770 (not gnus-topic-active-alist))
772 ;; Get a list of all groups available.
773 (mapatoms (lambda (g) (when (symbol-value g)
774 (push (symbol-name g) groups)))
776 (setq groups (sort groups 'string<))
777 ;; Init the variables.
778 (setq gnus-topic-active-topology (list (list "" 'visible)))
779 (setq gnus-topic-active-alist nil)
780 ;; Descend the top-level hierarchy.
781 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
782 ;; Set the top-level topic names to something nice.
783 (setcar (car gnus-topic-active-topology) "Gnus active")
784 (setcar (car gnus-topic-active-alist) "Gnus active"))))
786 (defun gnus-topic-grok-active-1 (topology groups)
787 (let* ((name (caar topology))
788 (prefix (concat "^" (regexp-quote name)))
789 tgroups ntopology group)
791 (string-match prefix (setq group (car groups))))
792 (if (not (string-match "\\." group (match-end 0)))
793 ;; There are no further hierarchies here, so we just
794 ;; enter this group into the list belonging to this
796 (push (pop groups) tgroups)
797 ;; New sub-hierarchy, so we add it to the topology.
798 (nconc topology (list (setq ntopology
799 (list (list (substring
800 group 0 (match-end 0))
802 ;; Descend the hierarchy.
803 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
804 ;; We remove the trailing "." from the topic name.
806 (if (string-match "\\.$" name)
807 (substring name 0 (match-beginning 0))
809 ;; Add this topic and its groups to the topic alist.
810 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
811 (setcar (car topology) name)
812 ;; We return the rest of the groups that didn't belong
816 ;;; Topic mode, commands and keymap.
818 (defvar gnus-topic-mode-map nil)
819 (defvar gnus-group-topic-map nil)
821 (unless gnus-topic-mode-map
822 (setq gnus-topic-mode-map (make-sparse-keymap))
824 ;; Override certain group mode keys.
825 (gnus-define-keys gnus-topic-mode-map
826 "=" gnus-topic-select-group
827 "\r" gnus-topic-select-group
828 " " gnus-topic-read-group
829 "\C-k" gnus-topic-kill-group
830 "\C-y" gnus-topic-yank-group
831 "\M-g" gnus-topic-get-new-news-this-topic
832 "AT" gnus-topic-list-active
833 "Gp" gnus-topic-edit-parameters
834 gnus-mouse-2 gnus-mouse-pick-topic)
836 ;; Define a new submap.
837 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
838 "#" gnus-topic-mark-topic
839 "\M-#" gnus-topic-unmark-topic
840 "n" gnus-topic-create-topic
841 "m" gnus-topic-move-group
842 "D" gnus-topic-remove-group
843 "c" gnus-topic-copy-group
844 "h" gnus-topic-hide-topic
845 "s" gnus-topic-show-topic
846 "M" gnus-topic-move-matching
847 "C" gnus-topic-copy-matching
848 "\C-i" gnus-topic-indent
849 [tab] gnus-topic-indent
850 "r" gnus-topic-rename
851 "\177" gnus-topic-delete)
853 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
854 "s" gnus-topic-sort-groups
855 "a" gnus-topic-sort-groups-by-alphabet
856 "u" gnus-topic-sort-groups-by-unread
857 "l" gnus-topic-sort-groups-by-level
858 "v" gnus-topic-sort-groups-by-score
859 "r" gnus-topic-sort-groups-by-rank
860 "m" gnus-topic-sort-groups-by-method))
862 (defun gnus-topic-make-menu-bar ()
863 (unless (boundp 'gnus-topic-menu)
865 gnus-topic-menu gnus-topic-mode-map ""
867 ["Toggle topics" gnus-topic-mode t]
869 ["Copy" gnus-topic-copy-group t]
870 ["Move" gnus-topic-move-group t]
871 ["Remove" gnus-topic-remove-group t]
872 ["Copy matching" gnus-topic-copy-matching t]
873 ["Move matching" gnus-topic-move-matching t])
875 ["Show" gnus-topic-show-topic t]
876 ["Hide" gnus-topic-hide-topic t]
877 ["Delete" gnus-topic-delete t]
878 ["Rename" gnus-topic-rename t]
879 ["Create" gnus-topic-create-topic t]
880 ["Mark" gnus-topic-mark-topic t]
881 ["Indent" gnus-topic-indent t])
882 ["List active" gnus-topic-list-active t]))))
884 (defun gnus-topic-mode (&optional arg redisplay)
885 "Minor mode for topicsifying Gnus group buffers."
886 (interactive (list current-prefix-arg t))
887 (when (eq major-mode 'gnus-group-mode)
888 (make-local-variable 'gnus-topic-mode)
889 (setq gnus-topic-mode
890 (if (null arg) (not gnus-topic-mode)
891 (> (prefix-numeric-value arg) 0)))
892 ;; Infest Gnus with topics.
893 (when gnus-topic-mode
894 (when (and menu-bar-mode
895 (gnus-visual-p 'topic-menu 'menu))
896 (gnus-topic-make-menu-bar))
897 (setq gnus-topic-line-format-spec
898 (gnus-parse-format gnus-topic-line-format
899 gnus-topic-line-format-alist t))
900 (unless (assq 'gnus-topic-mode minor-mode-alist)
901 (push '(gnus-topic-mode " Topic") minor-mode-alist))
902 (unless (assq 'gnus-topic-mode minor-mode-map-alist)
903 (push (cons 'gnus-topic-mode gnus-topic-mode-map)
904 minor-mode-map-alist))
905 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
906 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
907 (set (make-local-variable 'gnus-group-prepare-function)
908 'gnus-group-prepare-topics)
909 (set (make-local-variable 'gnus-group-get-parameter-function)
910 'gnus-group-topic-parameters)
911 (set (make-local-variable 'gnus-group-goto-next-group-function)
912 'gnus-topic-goto-next-group)
913 (set (make-local-variable 'gnus-group-indentation-function)
914 'gnus-topic-group-indentation)
915 (set (make-local-variable 'gnus-group-update-group-function)
916 'gnus-topic-update-topics-containing-group)
917 (set (make-local-variable 'gnus-group-sort-alist-function)
918 'gnus-group-sort-topic)
919 (setq gnus-group-change-level-function 'gnus-topic-change-level)
920 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
921 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
922 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
923 (setq gnus-topology-checked-p nil)
924 ;; We check the topology.
925 (when gnus-newsrc-alist
926 (gnus-topic-check-topology))
927 (run-hooks 'gnus-topic-mode-hook))
928 ;; Remove topic infestation.
929 (unless gnus-topic-mode
930 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
931 (remove-hook 'gnus-group-change-level-function
932 'gnus-topic-change-level)
933 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
934 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
935 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
937 (gnus-group-list-groups))))
939 (defun gnus-topic-select-group (&optional all)
940 "Select this newsgroup.
941 No article is selected automatically.
942 If ALL is non-nil, already read articles become readable.
943 If ALL is a number, fetch this number of articles.
945 If performed over a topic line, toggle folding the topic."
947 (if (gnus-group-topic-p)
948 (let ((gnus-group-list-mode
949 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
950 (gnus-topic-fold all))
951 (gnus-group-select-group all)))
953 (defun gnus-mouse-pick-topic (e)
954 "Select the group or topic under the mouse pointer."
957 (gnus-topic-read-group nil))
959 (defun gnus-topic-read-group (&optional all no-article group)
960 "Read news in this newsgroup.
961 If the prefix argument ALL is non-nil, already read articles become
962 readable. IF ALL is a number, fetch this number of articles. If the
963 optional argument NO-ARTICLE is non-nil, no article will be
964 auto-selected upon group entry. If GROUP is non-nil, fetch that
967 If performed over a topic line, toggle folding the topic."
969 (if (gnus-group-topic-p)
970 (let ((gnus-group-list-mode
971 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
972 (gnus-topic-fold all))
973 (gnus-group-read-group all no-article group)))
975 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
978 (read-string "New topic: ")
979 (gnus-current-topic)))
980 ;; Check whether this topic already exists.
981 (when (gnus-topic-find-topology topic)
982 (error "Topic already exists"))
984 (setq parent (caar gnus-topic-topology)))
985 (let ((top (cdr (gnus-topic-find-topology parent)))
986 (full-topic (or full-topic `((,topic visible)))))
988 (error "No such parent topic: %s" parent))
991 (while (and (cdr top)
992 (not (equal (caaadr top) previous)))
993 (setq top (cdr top)))
994 (setcdr top (cons full-topic (cdr top))))
995 (nconc top (list full-topic)))
996 (unless (assoc topic gnus-topic-alist)
997 (push (list topic) gnus-topic-alist)))
998 (gnus-topic-enter-dribble)
999 (gnus-group-list-groups)
1000 (gnus-topic-goto-topic topic))
1002 (defun gnus-topic-move-group (n topic &optional copyp)
1003 "Move the next N groups to TOPIC.
1004 If COPYP, copy the groups instead."
1006 (list current-prefix-arg
1007 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1008 (let ((groups (gnus-group-process-prefix n))
1009 (topicl (assoc topic gnus-topic-alist))
1010 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1011 (start-topic (gnus-group-topic-name))
1015 (gnus-group-remove-mark g)
1017 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1019 (setcdr entry (gnus-delete-first g (cdr entry))))
1020 (nconc topicl (list g)))
1022 (gnus-topic-enter-dribble)
1024 (gnus-group-goto-group start-group)
1025 (gnus-topic-goto-topic start-topic))
1026 (gnus-group-list-groups)))
1028 (defun gnus-topic-remove-group (&optional arg)
1029 "Remove the current group from the topic."
1031 (gnus-group-iterate arg
1033 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1034 (buffer-read-only nil))
1035 (when (and topicl group)
1037 (gnus-delete-first group topicl))
1038 (gnus-topic-update-topic)
1039 (gnus-group-position-point)))))
1041 (defun gnus-topic-copy-group (n topic)
1042 "Copy the current group to a topic."
1044 (list current-prefix-arg
1045 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1046 (gnus-topic-move-group n topic t))
1048 (defun gnus-topic-kill-group (&optional n discard)
1049 "Kill the next N groups."
1051 (if (gnus-group-topic-p)
1052 (let ((topic (gnus-group-topic-name)))
1053 (gnus-topic-remove-topic nil t)
1054 (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1055 gnus-topic-killed-topics)
1056 (gnus-topic-enter-dribble))
1057 (gnus-group-kill-group n discard)
1058 (gnus-topic-update-topic)))
1060 (defun gnus-topic-yank-group (&optional arg)
1061 "Yank the last topic."
1063 (if gnus-topic-killed-topics
1065 (or (gnus-group-topic-name)
1066 (gnus-topic-next-topic (gnus-current-topic))))
1067 (item (cdr (pop gnus-topic-killed-topics))))
1068 (gnus-topic-create-topic
1069 (caar item) (gnus-topic-parent-topic previous) previous
1071 (gnus-topic-enter-dribble)
1072 (gnus-topic-goto-topic (caar item)))
1073 (let* ((prev (gnus-group-group-name))
1074 (gnus-topic-inhibit-change-level t)
1075 (gnus-group-indentation
1077 (* gnus-topic-indent-level
1079 (gnus-topic-goto-topic (gnus-current-topic))
1080 (gnus-group-topic-level))
1084 ;; We first yank the groups the normal way...
1085 (setq yanked (gnus-group-yank-group arg))
1086 ;; Then we enter the yanked groups into the topics they belong
1088 (setq alist (assoc (save-excursion
1090 (gnus-current-topic))
1092 (when (stringp yanked)
1093 (setq yanked (list yanked)))
1095 (nconc alist yanked)
1096 (if (not (cdr alist))
1097 (setcdr alist (nconc yanked (cdr alist)))
1099 (when (equal (cadr alist) prev)
1100 (setcdr alist (nconc yanked (cdr alist)))
1102 (setq alist (cdr alist))))))
1103 (gnus-topic-update-topic)))
1105 (defun gnus-topic-hide-topic ()
1106 "Hide the current topic."
1108 (when (gnus-current-topic)
1109 (gnus-topic-goto-topic (gnus-current-topic))
1110 (gnus-topic-remove-topic nil nil 'hidden)))
1112 (defun gnus-topic-show-topic ()
1113 "Show the hidden topic."
1115 (when (gnus-group-topic-p)
1116 (gnus-topic-remove-topic t nil 'shown)))
1118 (defun gnus-topic-mark-topic (topic &optional unmark)
1119 "Mark all groups in the topic with the process mark."
1120 (interactive (list (gnus-current-topic)))
1122 (let ((groups (gnus-topic-find-groups topic 9 t)))
1124 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1125 (gnus-info-group (nth 2 (pop groups))))))))
1127 (defun gnus-topic-unmark-topic (topic &optional unmark)
1128 "Remove the process mark from all groups in the topic."
1129 (interactive (list (gnus-current-topic)))
1130 (gnus-topic-mark-topic topic t))
1132 (defun gnus-topic-get-new-news-this-topic (&optional n)
1133 "Check for new news in the current topic."
1135 (if (not (gnus-group-topic-p))
1136 (gnus-group-get-new-news-this-group n)
1137 (gnus-topic-mark-topic (gnus-group-topic-name))
1138 (gnus-group-get-new-news-this-group)))
1140 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1141 "Move all groups that match REGEXP to some topic."
1146 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1147 (read-string (format "Move to %s (regexp): " topic))))))
1148 (gnus-group-mark-regexp regexp)
1149 (gnus-topic-move-group nil topic copyp))
1151 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1152 "Copy all groups that match REGEXP to some topic."
1157 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1158 (read-string (format "Copy to %s (regexp): " topic))))))
1159 (gnus-topic-move-matching regexp topic t))
1161 (defun gnus-topic-delete (topic)
1163 (interactive (list (gnus-group-topic-name)))
1165 (error "No topic to be deleted"))
1166 (let ((entry (assoc topic gnus-topic-alist))
1167 (buffer-read-only nil))
1169 (error "Topic not empty"))
1170 ;; Delete if visible.
1171 (when (gnus-topic-goto-topic topic)
1173 ;; Remove from alist.
1174 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1175 ;; Remove from topology.
1176 (gnus-topic-find-topology topic nil nil 'delete)))
1178 (defun gnus-topic-rename (old-name new-name)
1181 (let ((topic (gnus-current-topic)))
1183 (read-string (format "Rename %s to: " topic)))))
1184 (let ((top (gnus-topic-find-topology old-name))
1185 (entry (assoc old-name gnus-topic-alist)))
1187 (setcar (cadr top) new-name))
1189 (setcar entry new-name))
1191 (gnus-group-list-groups)))
1193 (defun gnus-topic-indent (&optional unindent)
1194 "Indent a topic -- make it a sub-topic of the previous topic.
1195 If UNINDENT, remove an indentation."
1198 (gnus-topic-unindent)
1199 (let* ((topic (gnus-current-topic))
1200 (parent (gnus-topic-previous-topic topic)))
1202 (error "Nothing to indent %s into" topic))
1204 (gnus-topic-goto-topic topic)
1205 (gnus-topic-kill-group)
1206 (gnus-topic-create-topic
1207 topic parent nil (cdr (pop gnus-topic-killed-topics)))
1208 (or (gnus-topic-goto-topic topic)
1209 (gnus-topic-goto-topic parent))))))
1211 (defun gnus-topic-unindent ()
1214 (let* ((topic (gnus-current-topic))
1215 (parent (gnus-topic-parent-topic topic))
1216 (grandparent (gnus-topic-parent-topic parent)))
1218 (error "Nothing to indent %s into" topic))
1220 (gnus-topic-goto-topic topic)
1221 (gnus-topic-kill-group)
1222 (gnus-topic-create-topic
1223 topic grandparent (gnus-topic-next-topic parent)
1224 (cdr (pop gnus-topic-killed-topics)))
1225 (gnus-topic-goto-topic topic))))
1227 (defun gnus-topic-list-active (&optional force)
1228 "List all groups that Gnus knows about in a topicsified fashion.
1229 If FORCE, always re-read the active file."
1232 (gnus-get-killed-groups))
1233 (gnus-topic-grok-active force)
1234 (let ((gnus-topic-topology gnus-topic-active-topology)
1235 (gnus-topic-alist gnus-topic-active-alist)
1236 gnus-killed-list gnus-zombie-list)
1237 (gnus-group-list-groups 9 nil 1)))
1239 ;;; Topic sorting functions
1241 (defun gnus-topic-edit-parameters (group)
1242 "Edit the group parameters of GROUP.
1243 If performed on a topic, edit the topic parameters instead."
1244 (interactive (list (gnus-group-group-name)))
1246 (gnus-group-edit-group-parameters group)
1247 (if (not (gnus-group-topic-p))
1248 (error "Nothing to edit on the current line.")
1249 (let ((topic (gnus-group-topic-name)))
1251 (gnus-topic-parameters topic)
1252 "Editing the topic parameters."
1254 (gnus-topic-set-parameters ,topic form)))))))
1256 (defun gnus-group-sort-topic (func reverse)
1257 "Sort groups in the topics according to FUNC and REVERSE."
1258 (let ((alist gnus-topic-alist))
1260 (gnus-topic-sort-topic (pop alist) func reverse))))
1262 (defun gnus-topic-sort-topic (topic func reverse)
1263 ;; Each topic only lists the name of the group, while
1264 ;; the sort predicates expect group infos as inputs.
1265 ;; So we first transform the group names into infos,
1266 ;; then sort, and then transform back into group names.
1270 (lambda (info) (gnus-info-group info))
1273 (lambda (group) (gnus-get-info group))
1276 ;; Do the reversal, if necessary.
1278 (setcdr topic (nreverse (cdr topic)))))
1280 (defun gnus-topic-sort-groups (func &optional reverse)
1281 "Sort the current topic according to FUNC.
1282 If REVERSE, reverse the sorting order."
1283 (interactive (list gnus-group-sort-function current-prefix-arg))
1284 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1285 (gnus-topic-sort-topic
1286 topic (gnus-make-sort-function func) reverse)
1287 (gnus-group-list-groups)))
1289 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1290 "Sort the current topic alphabetically by group name.
1291 If REVERSE, sort in reverse order."
1293 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1295 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1296 "Sort the current topic by number of unread articles.
1297 If REVERSE, sort in reverse order."
1299 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1301 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1302 "Sort the current topic by group level.
1303 If REVERSE, sort in reverse order."
1305 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1307 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1308 "Sort the current topic by group score.
1309 If REVERSE, sort in reverse order."
1311 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1313 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1314 "Sort the current topic by group rank.
1315 If REVERSE, sort in reverse order."
1317 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1319 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1320 "Sort the current topic alphabetically by backend name.
1321 If REVERSE, sort in reverse order."
1323 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1325 (provide 'gnus-topic)
1327 ;;; gnus-topic.el ends here