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 (defcustom 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.
60 (defcustom gnus-topic-indent-level 2
61 "*How much each subtopic should be indented."
65 (defcustom gnus-topic-display-empty-topics t
66 "*If non-nil, display the topic lines even of topics that have no unread articles."
70 ;; Internal variables.
72 (defvar gnus-topic-active-topology nil)
73 (defvar gnus-topic-active-alist nil)
75 (defvar gnus-topology-checked-p nil
76 "Whether the topology has been checked in this session.")
78 (defvar gnus-topic-killed-topics nil)
79 (defvar gnus-topic-inhibit-change-level nil)
80 (defvar gnus-topic-tallied-groups nil)
82 (defconst gnus-topic-line-format-alist
86 (?g number-of-groups ?d)
87 (?a (gnus-topic-articles-in-topic entries) ?d)
88 (?A total-number-of-articles ?d)
91 (defvar gnus-topic-line-format-spec nil)
95 (defun gnus-group-topic-name ()
96 "The name of the topic on the current line."
97 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
98 (and topic (symbol-name topic))))
100 (defun gnus-group-topic-level ()
101 "The level of the topic on the current line."
102 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
104 (defun gnus-group-topic-unread ()
105 "The number of unread articles in topic on the current line."
106 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
108 (defun gnus-topic-unread (topic)
109 "Return the number of unread articles in TOPIC."
111 (and (gnus-topic-goto-topic topic)
112 (gnus-group-topic-unread)))
115 (defun gnus-group-topic-p ()
116 "Return non-nil if the current line is a topic."
117 (gnus-group-topic-name))
119 (defun gnus-topic-visible-p ()
120 "Return non-nil if the current topic is visible."
121 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
123 (defun gnus-topic-articles-in-topic (entries)
127 (when (numberp (setq number (car (pop entries))))
128 (incf total number)))
131 (defun gnus-group-topic (group)
132 "Return the topic GROUP is a member of."
133 (let ((alist gnus-topic-alist)
136 (when (member group (cdar alist))
137 (setq out (caar alist)
139 (setq alist (cdr alist)))
142 (defun gnus-group-parent-topic (group)
143 "Return the topic GROUP is member of by looking at the group buffer."
145 (set-buffer gnus-group-buffer)
146 (if (gnus-group-goto-group group)
148 (gnus-group-topic group))))
150 (defun gnus-topic-goto-topic (topic)
153 (gnus-goto-char (text-property-any (point-min) (point-max)
154 'gnus-topic (intern topic)))))
156 (defun gnus-current-topic ()
157 "Return the name of the current topic."
159 (or (get-text-property (point) 'gnus-topic)
161 (and (gnus-goto-char (previous-single-property-change
162 (point) 'gnus-topic))
163 (get-text-property (max (1- (point)) (point-min))
166 (symbol-name result))))
168 (defun gnus-current-topics ()
169 "Return a list of all current topics, lowest in hierarchy first."
170 (let ((topic (gnus-current-topic))
174 (setq topic (gnus-topic-parent-topic topic)))
177 (defun gnus-group-active-topic-p ()
178 "Say whether the current topic comes from the active topics."
181 (get-text-property (point) 'gnus-active)))
183 (defun gnus-topic-find-groups (topic &optional level all)
184 "Return entries for all visible groups in TOPIC."
185 (let ((groups (cdr (assoc topic gnus-topic-alist)))
186 info clevel unread group lowest params visible-groups entry active)
187 (setq lowest (or lowest 1))
188 (setq level (or level 7))
189 ;; We go through the newsrc to look for matches.
191 (setq entry (gnus-gethash (setq group (pop groups)) gnus-newsrc-hashtb)
193 params (gnus-info-params info)
194 active (gnus-active group)
195 unread (or (car entry)
196 (and (not (equal group "dummy.group"))
198 (- (1+ (cdr active)) (car active))))
199 clevel (or (gnus-info-level info)
200 (if (member group gnus-zombie-list) 8 9)))
202 unread ; nil means that the group is dead.
204 (>= clevel lowest) ; Is inside the level we want.
207 gnus-group-list-inactive-groups
209 (and gnus-list-groups-with-ticked-articles
210 (cdr (assq 'tick (gnus-info-marks info))))
211 ; Has right readedness.
212 ;; Check for permanent visibility.
213 (and gnus-permanently-visible-groups
214 (string-match gnus-permanently-visible-groups group))
215 (memq 'visible params)
216 (cdr (assq 'visible params)))
217 ;; Add this group to the list of visible groups.
218 (push (or entry group) visible-groups)))
219 (nreverse visible-groups)))
221 (defun gnus-topic-previous-topic (topic)
222 "Return the previous topic on the same level as TOPIC."
223 (let ((top (cddr (gnus-topic-find-topology
224 (gnus-topic-parent-topic topic)))))
225 (unless (equal topic (caaar top))
226 (while (and top (not (equal (caaadr top) topic)))
227 (setq top (cdr top)))
230 (defun gnus-topic-parent-topic (topic &optional topology)
231 "Return the parent of TOPIC."
233 (setq topology gnus-topic-topology))
234 (let ((parent (car (pop topology)))
237 (not (setq found (equal (caaar topology) topic)))
238 (not (setq result (gnus-topic-parent-topic topic
240 (setq topology (cdr topology)))
241 (or result (and found parent))))
243 (defun gnus-topic-next-topic (topic &optional previous)
244 "Return the next sibling of TOPIC."
245 (let ((parentt (cddr (gnus-topic-find-topology
246 (gnus-topic-parent-topic topic))))
249 (not (equal (caaar parentt) topic)))
250 (setq prev (caaar parentt)
251 parentt (cdr parentt)))
256 (defun gnus-topic-find-topology (topic &optional topology level remove)
257 "Return the topology of TOPIC."
259 (setq topology gnus-topic-topology)
263 (if (equal (caar topology) topic)
266 (delq topology remove))
267 (cons level topology))
268 (setq topology (cdr topology))
270 (not (setq result (gnus-topic-find-topology
271 topic (car topology) (1+ level)
273 (setq topology (cdr topology)))
276 (defvar gnus-tmp-topics nil)
277 (defun gnus-topic-list (&optional topology)
278 "Return a list of all topics in the topology."
280 (setq topology gnus-topic-topology
281 gnus-tmp-topics nil))
282 (push (caar topology) gnus-tmp-topics)
283 (mapcar 'gnus-topic-list (cdr topology))
286 ;;; Topic parameter jazz
288 (defun gnus-topic-parameters (topic)
289 "Return the parameters for TOPIC."
290 (let ((top (gnus-topic-find-topology topic)))
292 (nth 3 (cadr top)))))
294 (defun gnus-topic-set-parameters (topic parameters)
295 "Set the topic parameters of TOPIC to PARAMETERS."
296 (let ((top (gnus-topic-find-topology topic)))
298 (error "No such topic: %s" topic))
299 ;; We may have to extend if there is no parameters here
301 (unless (nthcdr 2 (cadr top))
302 (nconc (cadr top) (list nil)))
303 (unless (nthcdr 3 (cadr top))
304 (nconc (cadr top) (list nil)))
305 (setcar (nthcdr 3 (cadr top)) parameters)))
307 (defun gnus-group-topic-parameters (group)
308 "Compute the group parameters for GROUP taking into account inheritance from topics."
309 (let ((params-list (list (gnus-group-get-parameter group)))
310 topics params param out)
312 (gnus-group-goto-group group)
313 (setq topics (gnus-current-topics))
315 (push (gnus-topic-parameters (pop topics)) params-list))
316 ;; We probably have lots of nil elements here, so
317 ;; we remove them. Probably faster than doing this "properly".
318 (setq params-list (delq nil params-list))
319 ;; Now we have all the parameters, so we go through them
320 ;; and do inheritance in the obvious way.
321 (while (setq params (pop params-list))
322 (while (setq param (pop params))
324 (setq param (cons param t)))
325 ;; Override any old versions of this param.
326 (setq out (delq (assq (car param) out) out))
328 ;; Return the resulting parameter list.
331 ;;; General utility functions
333 (defun gnus-topic-enter-dribble ()
335 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
337 ;;; Generating group buffers
339 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
340 "List all newsgroups with unread articles of level LEVEL or lower, and
341 use the `gnus-group-topics' to sort the groups.
342 If ALL is non-nil, list groups that have no unread articles.
343 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
344 (set-buffer gnus-group-buffer)
345 (let ((buffer-read-only nil)
346 (lowest (or lowest 1)))
348 (setq gnus-topic-tallied-groups nil)
350 (when (or (not gnus-topic-alist)
351 (not gnus-topology-checked-p))
352 (gnus-topic-check-topology))
358 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
359 (gnus-group-prepare-flat-list-dead
360 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
364 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
365 (gnus-group-prepare-flat-list-dead
366 (setq gnus-killed-list (sort gnus-killed-list 'string<))
371 (when (< lowest gnus-level-zombie)
373 (let ((top (gnus-topic-find-topology list-topic)))
374 (gnus-topic-prepare-topic (cdr top) (car top)
375 (or topic-level level) all))
376 (gnus-topic-prepare-topic gnus-topic-topology 0
377 (or topic-level level) all))))
379 (gnus-group-set-mode-line)
380 (setq gnus-group-list-mode (cons level all))
381 (run-hooks 'gnus-group-prepare-hook))
383 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
384 "Insert TOPIC into the group buffer.
385 If SILENT, don't insert anything. Return the number of unread
386 articles in the topic and its subtopics."
387 (let* ((type (pop topicl))
388 (entries (gnus-topic-find-groups (car type) list-level all))
389 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
390 (gnus-group-indentation
391 (make-string (* gnus-topic-indent-level level) ? ))
392 (beg (progn (beginning-of-line) (point)))
393 (topicl (reverse topicl))
394 (all-entries entries)
395 (point-max (point-max))
398 info entry end active)
399 ;; Insert any sub-topics.
402 (gnus-topic-prepare-topic
403 (pop topicl) (1+ level) list-level all
407 ;; Insert all the groups that belong in this topic.
408 (while (setq entry (pop entries))
412 (gnus-group-insert-group-line
413 entry (if (member entry gnus-zombie-list) 8 9)
414 nil (- (1+ (cdr (setq active (gnus-active entry))))
418 (when (setq info (nth 2 entry))
419 (gnus-group-insert-group-line
420 (gnus-info-group info)
421 (gnus-info-level info) (gnus-info-marks info)
422 (car entry) (gnus-info-method info)))))
423 (when (and (listp entry)
424 (numberp (car entry))
425 (not (member (gnus-info-group (setq info (nth 2 entry)))
426 gnus-topic-tallied-groups)))
427 (push (gnus-info-group info) gnus-topic-tallied-groups)
428 (incf unread (car entry))))
430 ;; Insert the topic line.
431 (when (and (not silent)
432 (or gnus-topic-display-empty-topics
434 (/= point-max (point-max))))
435 (gnus-extent-start-open (point))
436 (gnus-topic-insert-topic-line
438 (not (eq (nth 2 type) 'hidden))
439 level all-entries unread))
443 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
444 "Remove the current topic."
445 (let ((topic (gnus-group-topic-name))
446 (level (gnus-group-topic-level))
447 (beg (progn (beginning-of-line) (point)))
450 (while (and (zerop (forward-line 1))
451 (> (or (gnus-group-topic-level) (1+ level)) level)))
452 (delete-region beg (point))
453 ;; Do the change in this rather odd manner because it has been
454 ;; reported that some topics share parts of some lists, for some
455 ;; reason. I have been unable to determine why this is the
456 ;; case, but this hack seems to take care of things.
457 (let ((data (cadr (gnus-topic-find-topology topic))))
459 (list (if insert 'visible 'invisible)
463 (setq gnus-topic-alist
464 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
465 (gnus-topic-insert-topic topic in-level)))))
467 (defun gnus-topic-insert-topic (topic &optional level)
469 (gnus-group-prepare-topics
470 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
471 nil nil topic level))
473 (defun gnus-topic-fold (&optional insert)
474 "Remove/insert the current topic."
475 (let ((topic (gnus-group-topic-name)))
478 (if (not (gnus-group-active-topic-p))
479 (gnus-topic-remove-topic
480 (or insert (not (gnus-topic-visible-p))))
481 (let ((gnus-topic-topology gnus-topic-active-topology)
482 (gnus-topic-alist gnus-topic-active-alist)
483 (gnus-group-list-mode (cons 5 t)))
484 (gnus-topic-remove-topic
485 (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
487 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
489 (let* ((visible (if visiblep "" "..."))
490 (indentation (make-string (* gnus-topic-indent-level level) ? ))
491 (total-number-of-articles unread)
492 (number-of-groups (length entries))
493 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
496 (gnus-add-text-properties
499 (eval gnus-topic-line-format-spec)
500 (gnus-topic-remove-excess-properties)1)
501 (list 'gnus-topic (intern name)
502 'gnus-topic-level level
503 'gnus-topic-unread unread
504 'gnus-active active-topic
505 'gnus-topic-visible visiblep))))
507 (defun gnus-topic-update-topics-containing-group (group)
508 "Update all topics that have GROUP as a member."
509 (when (and (eq major-mode 'gnus-group-mode)
512 (let ((alist gnus-topic-alist))
513 ;; This is probably not entirely correct. If a topic
514 ;; isn't shown, then it's not updated. But the updating
515 ;; should be performed in any case, since the topic's
516 ;; parent should be updated. Pfft.
518 (when (and (member group (cdar alist))
519 (gnus-topic-goto-topic (caar alist)))
520 (gnus-topic-update-topic-line (caar alist)))
523 (defun gnus-topic-update-topic ()
524 "Update all parent topics to the current group."
525 (when (and (eq major-mode 'gnus-group-mode)
527 (let ((group (gnus-group-group-name))
528 (buffer-read-only nil))
530 (gnus-get-info group)
531 (gnus-topic-goto-topic (gnus-current-topic)))
532 (gnus-topic-update-topic-line (gnus-group-topic-name))
533 (gnus-group-goto-group group)
534 (gnus-group-position-point)))))
536 (defun gnus-topic-goto-missing-group (group)
537 "Place point where GROUP is supposed to be inserted."
538 (let* ((topic (gnus-group-topic group))
539 (groups (cdr (assoc topic gnus-topic-alist)))
540 (g (cdr (member group groups)))
542 ;; Try to jump to a visible group.
543 (while (and g (not (gnus-group-goto-group (car g) t)))
545 ;; It wasn't visible, so we try to see where to insert it.
547 (setq g (cdr (member group (reverse groups))))
548 (while (and g unfound)
549 (when (gnus-group-goto-group (pop g) t)
553 (gnus-topic-goto-topic topic)
556 (defun gnus-topic-update-topic-line (topic-name &optional reads)
557 (let* ((top (gnus-topic-find-topology topic-name))
559 (children (cddr top))
560 (entries (gnus-topic-find-groups
561 (car type) (car gnus-group-list-mode)
562 (cdr gnus-group-list-mode)))
563 (parent (gnus-topic-parent-topic topic-name))
564 (all-entries entries)
567 (when (gnus-topic-goto-topic (car type))
568 ;; Tally all the groups that belong in this topic.
570 (setq unread (- (gnus-group-topic-unread) reads))
572 (incf unread (gnus-topic-unread (caar (pop children)))))
573 (while (setq entry (pop entries))
574 (when (numberp (car entry))
575 (incf unread (car entry)))))
576 (setq old-unread (gnus-group-topic-unread))
577 ;; Insert the topic line.
578 (gnus-topic-insert-topic-line
579 (car type) (gnus-topic-visible-p)
580 (not (eq (nth 2 type) 'hidden))
581 (gnus-group-topic-level) all-entries unread)
585 (gnus-topic-update-topic-line
586 parent (- old-unread (gnus-group-topic-unread))))
589 (defun gnus-topic-group-indentation ()
591 (* gnus-topic-indent-level
594 (gnus-topic-goto-topic (gnus-current-topic))
595 (gnus-group-topic-level))
601 (gnus-add-shutdown 'gnus-topic-close 'gnus)
603 (defun gnus-topic-close ()
604 (setq gnus-topic-active-topology nil
605 gnus-topic-active-alist nil
606 gnus-topic-killed-topics nil
607 gnus-topic-tallied-groups nil
608 gnus-topology-checked-p nil))
610 (defun gnus-topic-check-topology ()
611 ;; The first time we set the topology to whatever we have
612 ;; gotten here, which can be rather random.
613 (unless gnus-topic-alist
614 (gnus-topic-init-alist))
616 (setq gnus-topology-checked-p t)
617 ;; Go through the topic alist and make sure that all topics
618 ;; are in the topic topology.
619 (let ((topics (gnus-topic-list))
620 (alist gnus-topic-alist)
623 (unless (member (caar alist) topics)
624 (nconc gnus-topic-topology
625 (list (list (list (caar alist) 'visible))))
627 (setq alist (cdr alist)))
629 (gnus-topic-enter-dribble))
630 ;; Conversely, go through the topology and make sure that all
631 ;; topologies have alists.
633 (unless (assoc (car topics) gnus-topic-alist)
634 (push (list (car topics)) gnus-topic-alist))
636 ;; Go through all living groups and make sure that
637 ;; they belong to some topic.
638 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
640 (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
641 (newsrc gnus-newsrc-alist)
644 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
645 (setcdr entry (cons group (cdr entry))))))
646 ;; Go through all topics and make sure they contain only living groups.
647 (let ((alist gnus-topic-alist)
649 (while (setq topic (pop alist))
651 (if (gnus-gethash (cadr topic) gnus-newsrc-hashtb)
652 (setq topic (cdr topic))
653 (setcdr topic (cddr topic)))))))
655 (defun gnus-topic-init-alist ()
656 "Initialize the topic structures."
657 (setq gnus-topic-topology
658 (cons (list "Gnus" 'visible)
659 (mapcar (lambda (topic)
660 (list (list (car topic) 'visible)))
662 (setq gnus-topic-alist
664 (mapcar (lambda (info) (gnus-info-group info))
665 (cdr gnus-newsrc-alist)))
667 (gnus-topic-enter-dribble))
671 (defun gnus-topic-clean-alist ()
672 "Remove bogus groups from the topic alist."
673 (let ((topic-alist gnus-topic-alist)
675 (unless gnus-killed-hashtb
676 (gnus-make-hashtable-from-killed))
677 (while (setq topic (pop topic-alist))
678 (let ((topic-name (pop topic))
679 group filtered-topic)
680 (while (setq group (pop topic))
681 (when (and (or (gnus-gethash group gnus-active-hashtb)
682 (gnus-info-method (gnus-get-info group)))
683 (not (gnus-gethash group gnus-killed-hashtb)))
684 (push group filtered-topic)))
685 (push (cons topic-name (nreverse filtered-topic)) result)))
686 (setq gnus-topic-alist (nreverse result))))
688 (defun gnus-topic-change-level (group level oldlevel)
689 "Run when changing levels to enter/remove groups from topics."
691 (set-buffer gnus-group-buffer)
692 (when (and gnus-topic-mode
694 (not gnus-topic-inhibit-change-level))
695 ;; Remove the group from the topics.
696 (when (and (< oldlevel gnus-level-zombie)
697 (>= level gnus-level-zombie))
700 (when (setq alist (assoc (gnus-current-topic) gnus-topic-alist))
701 (setcdr alist (gnus-delete-first group (cdr alist))))))
702 ;; If the group is subscribed. then we enter it into the topics.
703 (when (and (< level gnus-level-zombie)
704 (>= oldlevel gnus-level-zombie))
705 (let* ((prev (gnus-group-group-name))
706 (gnus-topic-inhibit-change-level t)
707 (gnus-group-indentation
709 (* gnus-topic-indent-level
711 (gnus-topic-goto-topic (gnus-current-topic))
712 (gnus-group-topic-level))
715 (yanked (list group))
717 ;; Then we enter the yanked groups into the topics they belong
719 (when (setq alist (assoc (save-excursion
723 (caar gnus-topic-topology)))
726 (when (stringp yanked)
727 (setq yanked (list yanked)))
730 (if (not (cdr alist))
731 (setcdr alist (nconc yanked (cdr alist)))
732 (while (and (not end) (cdr alist))
733 (when (equal (cadr alist) prev)
734 (setcdr alist (nconc yanked (cdr alist)))
736 (setq alist (cdr alist)))
738 (nconc talist yanked))))))
739 (gnus-topic-update-topic)))))
741 (defun gnus-topic-goto-next-group (group props)
742 "Go to group or the next group after group."
744 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props))))
745 (if (gnus-group-goto-group group)
747 ;; The group is no longer visible.
748 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
749 (after (cdr (member group (cdr list)))))
750 ;; First try to put point on a group after the current one.
752 (not (gnus-group-goto-group (car after))))
753 (setq after (cdr after)))
754 ;; Then try to put point on a group before point.
756 (setq after (cdr (member group (reverse (cdr list)))))
758 (not (gnus-group-goto-group (car after))))
759 (setq after (cdr after))))
760 ;; Finally, just put point on the topic.
762 (gnus-topic-goto-topic (car list))
766 ;;; Topic-active functions
768 (defun gnus-topic-grok-active (&optional force)
769 "Parse all active groups and create topic structures for them."
770 ;; First we make sure that we have really read the active file.
772 (not gnus-topic-active-alist))
774 ;; Get a list of all groups available.
775 (mapatoms (lambda (g) (when (symbol-value g)
776 (push (symbol-name g) groups)))
778 (setq groups (sort groups 'string<))
779 ;; Init the variables.
780 (setq gnus-topic-active-topology (list (list "" 'visible)))
781 (setq gnus-topic-active-alist nil)
782 ;; Descend the top-level hierarchy.
783 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
784 ;; Set the top-level topic names to something nice.
785 (setcar (car gnus-topic-active-topology) "Gnus active")
786 (setcar (car gnus-topic-active-alist) "Gnus active"))))
788 (defun gnus-topic-grok-active-1 (topology groups)
789 (let* ((name (caar topology))
790 (prefix (concat "^" (regexp-quote name)))
791 tgroups ntopology group)
793 (string-match prefix (setq group (car groups))))
794 (if (not (string-match "\\." group (match-end 0)))
795 ;; There are no further hierarchies here, so we just
796 ;; enter this group into the list belonging to this
798 (push (pop groups) tgroups)
799 ;; New sub-hierarchy, so we add it to the topology.
800 (nconc topology (list (setq ntopology
801 (list (list (substring
802 group 0 (match-end 0))
804 ;; Descend the hierarchy.
805 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
806 ;; We remove the trailing "." from the topic name.
808 (if (string-match "\\.$" name)
809 (substring name 0 (match-beginning 0))
811 ;; Add this topic and its groups to the topic alist.
812 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
813 (setcar (car topology) name)
814 ;; We return the rest of the groups that didn't belong
818 ;;; Topic mode, commands and keymap.
820 (defvar gnus-topic-mode-map nil)
821 (defvar gnus-group-topic-map nil)
823 (unless gnus-topic-mode-map
824 (setq gnus-topic-mode-map (make-sparse-keymap))
826 ;; Override certain group mode keys.
827 (gnus-define-keys gnus-topic-mode-map
828 "=" gnus-topic-select-group
829 "\r" gnus-topic-select-group
830 " " gnus-topic-read-group
831 "\C-k" gnus-topic-kill-group
832 "\C-y" gnus-topic-yank-group
833 "\M-g" gnus-topic-get-new-news-this-topic
834 "AT" gnus-topic-list-active
835 "Gp" gnus-topic-edit-parameters
836 gnus-mouse-2 gnus-mouse-pick-topic)
838 ;; Define a new submap.
839 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
840 "#" gnus-topic-mark-topic
841 "\M-#" gnus-topic-unmark-topic
842 "n" gnus-topic-create-topic
843 "m" gnus-topic-move-group
844 "D" gnus-topic-remove-group
845 "c" gnus-topic-copy-group
846 "h" gnus-topic-hide-topic
847 "s" gnus-topic-show-topic
848 "M" gnus-topic-move-matching
849 "C" gnus-topic-copy-matching
850 "\C-i" gnus-topic-indent
851 [tab] gnus-topic-indent
852 "r" gnus-topic-rename
853 "\177" gnus-topic-delete)
855 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
856 "s" gnus-topic-sort-groups
857 "a" gnus-topic-sort-groups-by-alphabet
858 "u" gnus-topic-sort-groups-by-unread
859 "l" gnus-topic-sort-groups-by-level
860 "v" gnus-topic-sort-groups-by-score
861 "r" gnus-topic-sort-groups-by-rank
862 "m" gnus-topic-sort-groups-by-method))
864 (defun gnus-topic-make-menu-bar ()
865 (unless (boundp 'gnus-topic-menu)
867 gnus-topic-menu gnus-topic-mode-map ""
869 ["Toggle topics" gnus-topic-mode t]
871 ["Copy" gnus-topic-copy-group t]
872 ["Move" gnus-topic-move-group t]
873 ["Remove" gnus-topic-remove-group t]
874 ["Copy matching" gnus-topic-copy-matching t]
875 ["Move matching" gnus-topic-move-matching t])
877 ["Show" gnus-topic-show-topic t]
878 ["Hide" gnus-topic-hide-topic t]
879 ["Delete" gnus-topic-delete t]
880 ["Rename" gnus-topic-rename t]
881 ["Create" gnus-topic-create-topic t]
882 ["Mark" gnus-topic-mark-topic t]
883 ["Indent" gnus-topic-indent t])
884 ["List active" gnus-topic-list-active t]))))
886 (defun gnus-topic-mode (&optional arg redisplay)
887 "Minor mode for topicsifying Gnus group buffers."
888 (interactive (list current-prefix-arg t))
889 (when (eq major-mode 'gnus-group-mode)
890 (make-local-variable 'gnus-topic-mode)
891 (setq gnus-topic-mode
892 (if (null arg) (not gnus-topic-mode)
893 (> (prefix-numeric-value arg) 0)))
894 ;; Infest Gnus with topics.
895 (when gnus-topic-mode
896 (when (gnus-visual-p 'topic-menu 'menu)
897 (gnus-topic-make-menu-bar))
898 (setq gnus-topic-line-format-spec
899 (gnus-parse-format gnus-topic-line-format
900 gnus-topic-line-format-alist t))
901 (unless (assq 'gnus-topic-mode minor-mode-alist)
902 (push '(gnus-topic-mode " Topic") minor-mode-alist))
903 (unless (assq 'gnus-topic-mode minor-mode-map-alist)
904 (push (cons 'gnus-topic-mode gnus-topic-mode-map)
905 minor-mode-map-alist))
906 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
907 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
908 (set (make-local-variable 'gnus-group-prepare-function)
909 'gnus-group-prepare-topics)
910 (set (make-local-variable 'gnus-group-get-parameter-function)
911 'gnus-group-topic-parameters)
912 (set (make-local-variable 'gnus-group-goto-next-group-function)
913 'gnus-topic-goto-next-group)
914 (set (make-local-variable 'gnus-group-indentation-function)
915 'gnus-topic-group-indentation)
916 (set (make-local-variable 'gnus-group-update-group-function)
917 'gnus-topic-update-topics-containing-group)
918 (set (make-local-variable 'gnus-group-sort-alist-function)
919 'gnus-group-sort-topic)
920 (setq gnus-group-change-level-function 'gnus-topic-change-level)
921 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
922 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
923 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
924 (setq gnus-topology-checked-p nil)
925 ;; We check the topology.
926 (when gnus-newsrc-alist
927 (gnus-topic-check-topology))
928 (run-hooks 'gnus-topic-mode-hook))
929 ;; Remove topic infestation.
930 (unless gnus-topic-mode
931 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
932 (remove-hook 'gnus-group-change-level-function
933 'gnus-topic-change-level)
934 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
935 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
936 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
938 (gnus-group-list-groups))))
940 (defun gnus-topic-select-group (&optional all)
941 "Select this newsgroup.
942 No article is selected automatically.
943 If ALL is non-nil, already read articles become readable.
944 If ALL is a number, fetch this number of articles.
946 If performed over a topic line, toggle folding the topic."
948 (if (gnus-group-topic-p)
949 (let ((gnus-group-list-mode
950 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
951 (gnus-topic-fold all))
952 (gnus-group-select-group all)))
954 (defun gnus-mouse-pick-topic (e)
955 "Select the group or topic under the mouse pointer."
958 (gnus-topic-read-group nil))
960 (defun gnus-topic-read-group (&optional all no-article group)
961 "Read news in this newsgroup.
962 If the prefix argument ALL is non-nil, already read articles become
963 readable. IF ALL is a number, fetch this number of articles. If the
964 optional argument NO-ARTICLE is non-nil, no article will be
965 auto-selected upon group entry. If GROUP is non-nil, fetch that
968 If performed over a topic line, toggle folding the topic."
970 (if (gnus-group-topic-p)
971 (let ((gnus-group-list-mode
972 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
973 (gnus-topic-fold all))
974 (gnus-group-read-group all no-article group)))
976 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
979 (read-string "New topic: ")
980 (gnus-current-topic)))
981 ;; Check whether this topic already exists.
982 (when (gnus-topic-find-topology topic)
983 (error "Topic already exists"))
985 (setq parent (caar gnus-topic-topology)))
986 (let ((top (cdr (gnus-topic-find-topology parent)))
987 (full-topic (or full-topic `((,topic visible)))))
989 (error "No such parent topic: %s" parent))
992 (while (and (cdr top)
993 (not (equal (caaadr top) previous)))
994 (setq top (cdr top)))
995 (setcdr top (cons full-topic (cdr top))))
996 (nconc top (list full-topic)))
997 (unless (assoc topic gnus-topic-alist)
998 (push (list topic) gnus-topic-alist)))
999 (gnus-topic-enter-dribble)
1000 (gnus-group-list-groups)
1001 (gnus-topic-goto-topic topic))
1003 (defun gnus-topic-move-group (n topic &optional copyp)
1004 "Move the next N groups to TOPIC.
1005 If COPYP, copy the groups instead."
1007 (list current-prefix-arg
1008 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1009 (let ((groups (gnus-group-process-prefix n))
1010 (topicl (assoc topic gnus-topic-alist))
1011 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1012 (start-topic (gnus-group-topic-name))
1016 (gnus-group-remove-mark g)
1018 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1020 (setcdr entry (gnus-delete-first g (cdr entry))))
1021 (nconc topicl (list g)))
1023 (gnus-topic-enter-dribble)
1025 (gnus-group-goto-group start-group)
1026 (gnus-topic-goto-topic start-topic))
1027 (gnus-group-list-groups)))
1029 (defun gnus-topic-remove-group (&optional arg)
1030 "Remove the current group from the topic."
1032 (gnus-group-iterate arg
1034 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1035 (buffer-read-only nil))
1036 (when (and topicl group)
1038 (gnus-delete-first group topicl))
1039 (gnus-topic-update-topic)
1040 (gnus-group-position-point)))))
1042 (defun gnus-topic-copy-group (n topic)
1043 "Copy the current group to a topic."
1045 (list current-prefix-arg
1046 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1047 (gnus-topic-move-group n topic t))
1049 (defun gnus-topic-kill-group (&optional n discard)
1050 "Kill the next N groups."
1052 (if (gnus-group-topic-p)
1053 (let ((topic (gnus-group-topic-name)))
1055 (gnus-topic-find-topology topic)
1056 (assoc topic gnus-topic-alist))
1057 gnus-topic-killed-topics)
1058 (gnus-topic-remove-topic nil t)
1059 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1060 (gnus-topic-enter-dribble))
1061 (gnus-group-kill-group n discard)
1062 (gnus-topic-update-topic)))
1064 (defun gnus-topic-yank-group (&optional arg)
1065 "Yank the last topic."
1067 (if gnus-topic-killed-topics
1069 (or (gnus-group-topic-name)
1070 (gnus-topic-next-topic (gnus-current-topic))))
1071 (data (pop gnus-topic-killed-topics))
1074 (push alist gnus-topic-alist)
1075 (gnus-topic-create-topic
1076 (caar item) (gnus-topic-parent-topic previous) previous
1078 (gnus-topic-enter-dribble)
1079 (gnus-topic-goto-topic (caar item)))
1080 (let* ((prev (gnus-group-group-name))
1081 (gnus-topic-inhibit-change-level t)
1082 (gnus-group-indentation
1084 (* gnus-topic-indent-level
1086 (gnus-topic-goto-topic (gnus-current-topic))
1087 (gnus-group-topic-level))
1091 ;; We first yank the groups the normal way...
1092 (setq yanked (gnus-group-yank-group arg))
1093 ;; Then we enter the yanked groups into the topics they belong
1095 (setq alist (assoc (save-excursion
1097 (gnus-current-topic))
1099 (when (stringp yanked)
1100 (setq yanked (list yanked)))
1102 (nconc alist yanked)
1103 (if (not (cdr alist))
1104 (setcdr alist (nconc yanked (cdr alist)))
1106 (when (equal (cadr alist) prev)
1107 (setcdr alist (nconc yanked (cdr alist)))
1109 (setq alist (cdr alist))))))
1110 (gnus-topic-update-topic)))
1112 (defun gnus-topic-hide-topic ()
1113 "Hide the current topic."
1115 (when (gnus-current-topic)
1116 (gnus-topic-goto-topic (gnus-current-topic))
1117 (gnus-topic-remove-topic nil nil 'hidden)))
1119 (defun gnus-topic-show-topic ()
1120 "Show the hidden topic."
1122 (when (gnus-group-topic-p)
1123 (gnus-topic-remove-topic t nil 'shown)))
1125 (defun gnus-topic-mark-topic (topic &optional unmark)
1126 "Mark all groups in the topic with the process mark."
1127 (interactive (list (gnus-current-topic)))
1129 (let ((groups (gnus-topic-find-groups topic 9 t)))
1131 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1132 (gnus-info-group (nth 2 (pop groups))))))))
1134 (defun gnus-topic-unmark-topic (topic &optional unmark)
1135 "Remove the process mark from all groups in the topic."
1136 (interactive (list (gnus-current-topic)))
1137 (gnus-topic-mark-topic topic t))
1139 (defun gnus-topic-get-new-news-this-topic (&optional n)
1140 "Check for new news in the current topic."
1142 (if (not (gnus-group-topic-p))
1143 (gnus-group-get-new-news-this-group n)
1144 (gnus-topic-mark-topic (gnus-group-topic-name))
1145 (gnus-group-get-new-news-this-group)))
1147 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1148 "Move all groups that match REGEXP to some topic."
1153 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1154 (read-string (format "Move to %s (regexp): " topic))))))
1155 (gnus-group-mark-regexp regexp)
1156 (gnus-topic-move-group nil topic copyp))
1158 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1159 "Copy all groups that match REGEXP to some topic."
1164 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1165 (read-string (format "Copy to %s (regexp): " topic))))))
1166 (gnus-topic-move-matching regexp topic t))
1168 (defun gnus-topic-delete (topic)
1170 (interactive (list (gnus-group-topic-name)))
1172 (error "No topic to be deleted"))
1173 (let ((entry (assoc topic gnus-topic-alist))
1174 (buffer-read-only nil))
1176 (error "Topic not empty"))
1177 ;; Delete if visible.
1178 (when (gnus-topic-goto-topic topic)
1180 ;; Remove from alist.
1181 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1182 ;; Remove from topology.
1183 (gnus-topic-find-topology topic nil nil 'delete)))
1185 (defun gnus-topic-rename (old-name new-name)
1188 (let ((topic (gnus-current-topic)))
1190 (read-string (format "Rename %s to: " topic)))))
1191 (let ((top (gnus-topic-find-topology old-name))
1192 (entry (assoc old-name gnus-topic-alist)))
1194 (setcar (cadr top) new-name))
1196 (setcar entry new-name))
1198 (gnus-group-list-groups)))
1200 (defun gnus-topic-indent (&optional unindent)
1201 "Indent a topic -- make it a sub-topic of the previous topic.
1202 If UNINDENT, remove an indentation."
1205 (gnus-topic-unindent)
1206 (let* ((topic (gnus-current-topic))
1207 (parent (gnus-topic-previous-topic topic)))
1209 (error "Nothing to indent %s into" topic))
1211 (gnus-topic-goto-topic topic)
1212 (gnus-topic-kill-group)
1213 (gnus-topic-create-topic
1214 topic parent nil (cdar (pop gnus-topic-killed-topics)))
1215 (or (gnus-topic-goto-topic topic)
1216 (gnus-topic-goto-topic parent))))))
1218 (defun gnus-topic-unindent ()
1221 (let* ((topic (gnus-current-topic))
1222 (parent (gnus-topic-parent-topic topic))
1223 (grandparent (gnus-topic-parent-topic parent)))
1225 (error "Nothing to indent %s into" topic))
1227 (gnus-topic-goto-topic topic)
1228 (gnus-topic-kill-group)
1229 (gnus-topic-create-topic
1230 topic grandparent (gnus-topic-next-topic parent)
1231 (cdar (pop gnus-topic-killed-topics)))
1232 (gnus-topic-goto-topic topic))))
1234 (defun gnus-topic-list-active (&optional force)
1235 "List all groups that Gnus knows about in a topicsified fashion.
1236 If FORCE, always re-read the active file."
1239 (gnus-get-killed-groups))
1240 (gnus-topic-grok-active force)
1241 (let ((gnus-topic-topology gnus-topic-active-topology)
1242 (gnus-topic-alist gnus-topic-active-alist)
1243 gnus-killed-list gnus-zombie-list)
1244 (gnus-group-list-groups 9 nil 1)))
1246 ;;; Topic sorting functions
1248 (defun gnus-topic-edit-parameters (group)
1249 "Edit the group parameters of GROUP.
1250 If performed on a topic, edit the topic parameters instead."
1251 (interactive (list (gnus-group-group-name)))
1253 (gnus-group-edit-group-parameters group)
1254 (if (not (gnus-group-topic-p))
1255 (error "Nothing to edit on the current line.")
1256 (let ((topic (gnus-group-topic-name)))
1258 (gnus-topic-parameters topic)
1259 "Editing the topic parameters."
1261 (gnus-topic-set-parameters ,topic form)))))))
1263 (defun gnus-group-sort-topic (func reverse)
1264 "Sort groups in the topics according to FUNC and REVERSE."
1265 (let ((alist gnus-topic-alist))
1267 (gnus-topic-sort-topic (pop alist) func reverse))))
1269 (defun gnus-topic-sort-topic (topic func reverse)
1270 ;; Each topic only lists the name of the group, while
1271 ;; the sort predicates expect group infos as inputs.
1272 ;; So we first transform the group names into infos,
1273 ;; then sort, and then transform back into group names.
1277 (lambda (info) (gnus-info-group info))
1280 (lambda (group) (gnus-get-info group))
1283 ;; Do the reversal, if necessary.
1285 (setcdr topic (nreverse (cdr topic)))))
1287 (defun gnus-topic-sort-groups (func &optional reverse)
1288 "Sort the current topic according to FUNC.
1289 If REVERSE, reverse the sorting order."
1290 (interactive (list gnus-group-sort-function current-prefix-arg))
1291 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1292 (gnus-topic-sort-topic
1293 topic (gnus-make-sort-function func) reverse)
1294 (gnus-group-list-groups)))
1296 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1297 "Sort the current topic alphabetically by group name.
1298 If REVERSE, sort in reverse order."
1300 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1302 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1303 "Sort the current topic by number of unread articles.
1304 If REVERSE, sort in reverse order."
1306 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1308 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1309 "Sort the current topic by group level.
1310 If REVERSE, sort in reverse order."
1312 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1314 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1315 "Sort the current topic by group score.
1316 If REVERSE, sort in reverse order."
1318 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1320 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1321 "Sort the current topic by group rank.
1322 If REVERSE, sort in reverse order."
1324 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1326 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1327 "Sort the current topic alphabetically by backend name.
1328 If REVERSE, sort in reverse order."
1330 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1332 (provide 'gnus-topic)
1334 ;;; gnus-topic.el ends here