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 (defvar gnus-topic-mode nil
34 "Minor mode for Gnus group buffers.")
36 (defvar gnus-topic-mode-hook nil
37 "Hook run in topic mode buffers.")
39 (defvar gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
40 "Format of topic lines.
41 It works along the same lines as a normal formatting string,
42 with some simple extensions.
44 %i Indentation based on topic level.
46 %v Nothing if the topic is visible, \"...\" otherwise.
47 %g Number of groups in the topic.
48 %a Number of unread articles in the groups in the topic.
49 %A Number of unread articles in the groups in the topic and its subtopics.
52 (defvar gnus-topic-indent-level 2
53 "*How much each subtopic should be indented.")
55 (defvar gnus-topic-display-empty-topics t
56 "*If non-nil, display the topic lines even of topics that have no unread articles.")
58 ;; Internal variables.
60 (defvar gnus-topic-active-topology nil)
61 (defvar gnus-topic-active-alist nil)
63 (defvar gnus-topology-checked-p nil
64 "Whether the topology has been checked in this session.")
66 (defvar gnus-topic-killed-topics nil)
67 (defvar gnus-topic-inhibit-change-level nil)
68 (defvar gnus-topic-tallied-groups nil)
70 (defconst gnus-topic-line-format-alist
74 (?g number-of-groups ?d)
75 (?a (gnus-topic-articles-in-topic entries) ?d)
76 (?A total-number-of-articles ?d)
79 (defvar gnus-topic-line-format-spec nil)
83 (defun gnus-group-topic-name ()
84 "The name of the topic on the current line."
85 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
86 (and topic (symbol-name topic))))
88 (defun gnus-group-topic-level ()
89 "The level of the topic on the current line."
90 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
92 (defun gnus-group-topic-unread ()
93 "The number of unread articles in topic on the current line."
94 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
96 (defun gnus-topic-unread (topic)
97 "Return the number of unread articles in TOPIC."
99 (and (gnus-topic-goto-topic topic)
100 (gnus-group-topic-unread)))
103 (defun gnus-group-topic-p ()
104 "Return non-nil if the current line is a topic."
105 (gnus-group-topic-name))
107 (defun gnus-topic-visible-p ()
108 "Return non-nil if the current topic is visible."
109 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
111 (defun gnus-topic-articles-in-topic (entries)
115 (when (numberp (setq number (car (pop entries))))
116 (incf total number)))
119 (defun gnus-group-topic (group)
120 "Return the topic GROUP is a member of."
121 (let ((alist gnus-topic-alist)
124 (when (member group (cdar alist))
125 (setq out (caar alist)
127 (setq alist (cdr alist)))
130 (defun gnus-group-parent-topic (group)
131 "Return the topic GROUP is member of by looking at the group buffer."
133 (set-buffer gnus-group-buffer)
134 (if (gnus-group-goto-group group)
136 (gnus-group-topic group))))
138 (defun gnus-topic-goto-topic (topic)
141 (gnus-goto-char (text-property-any (point-min) (point-max)
142 'gnus-topic (intern topic)))))
144 (defun gnus-current-topic ()
145 "Return the name of the current topic."
147 (or (get-text-property (point) 'gnus-topic)
149 (and (gnus-goto-char (previous-single-property-change
150 (point) 'gnus-topic))
151 (get-text-property (max (1- (point)) (point-min))
154 (symbol-name result))))
156 (defun gnus-current-topics ()
157 "Return a list of all current topics, lowest in hierarchy first."
158 (let ((topic (gnus-current-topic))
162 (setq topic (gnus-topic-parent-topic topic)))
165 (defun gnus-group-active-topic-p ()
166 "Say whether the current topic comes from the active topics."
169 (get-text-property (point) 'gnus-active)))
171 (defun gnus-topic-find-groups (topic &optional level all)
172 "Return entries for all visible groups in TOPIC."
173 (let ((groups (cdr (assoc topic gnus-topic-alist)))
174 info clevel unread group lowest params visible-groups entry active)
175 (setq lowest (or lowest 1))
176 (setq level (or level 7))
177 ;; We go through the newsrc to look for matches.
179 (setq entry (gnus-gethash (setq group (pop groups)) gnus-newsrc-hashtb)
181 params (gnus-info-params info)
182 active (gnus-active group)
183 unread (or (car entry)
184 (and (not (equal group "dummy.group"))
186 (- (1+ (cdr active)) (car active))))
187 clevel (or (gnus-info-level info)
188 (if (member group gnus-zombie-list) 8 9)))
190 unread ; nil means that the group is dead.
192 (>= clevel lowest) ; Is inside the level we want.
195 gnus-group-list-inactive-groups
197 (and gnus-list-groups-with-ticked-articles
198 (cdr (assq 'tick (gnus-info-marks info))))
199 ; Has right readedness.
200 ;; Check for permanent visibility.
201 (and gnus-permanently-visible-groups
202 (string-match gnus-permanently-visible-groups group))
203 (memq 'visible params)
204 (cdr (assq 'visible params)))
205 ;; Add this group to the list of visible groups.
206 (push (or entry group) visible-groups)))
207 (nreverse visible-groups)))
209 (defun gnus-topic-previous-topic (topic)
210 "Return the previous topic on the same level as TOPIC."
211 (let ((top (cddr (gnus-topic-find-topology
212 (gnus-topic-parent-topic topic)))))
213 (unless (equal topic (caaar top))
214 (while (and top (not (equal (caaadr top) topic)))
215 (setq top (cdr top)))
218 (defun gnus-topic-parent-topic (topic &optional topology)
219 "Return the parent of TOPIC."
221 (setq topology gnus-topic-topology))
222 (let ((parent (car (pop topology)))
225 (not (setq found (equal (caaar topology) topic)))
226 (not (setq result (gnus-topic-parent-topic topic
228 (setq topology (cdr topology)))
229 (or result (and found parent))))
231 (defun gnus-topic-next-topic (topic &optional previous)
232 "Return the next sibling of TOPIC."
233 (let ((parentt (cddr (gnus-topic-find-topology
234 (gnus-topic-parent-topic topic))))
237 (not (equal (caaar parentt) topic)))
238 (setq prev (caaar parentt)
239 parentt (cdr parentt)))
244 (defun gnus-topic-find-topology (topic &optional topology level remove)
245 "Return the topology of TOPIC."
247 (setq topology gnus-topic-topology)
251 (if (equal (caar topology) topic)
254 (delq topology remove))
255 (cons level topology))
256 (setq topology (cdr topology))
258 (not (setq result (gnus-topic-find-topology
259 topic (car topology) (1+ level)
261 (setq topology (cdr topology)))
264 (defvar gnus-tmp-topics nil)
265 (defun gnus-topic-list (&optional topology)
266 "Return a list of all topics in the topology."
268 (setq topology gnus-topic-topology
269 gnus-tmp-topics nil))
270 (push (caar topology) gnus-tmp-topics)
271 (mapcar 'gnus-topic-list (cdr topology))
274 ;;; Topic parameter jazz
276 (defun gnus-topic-parameters (topic)
277 "Return the parameters for TOPIC."
278 (let ((top (gnus-topic-find-topology topic)))
280 (error "No such topic: %s" topic))
283 (defun gnus-topic-set-parameters (topic parameters)
284 "Set the topic parameters of TOPIC to PARAMETERS."
285 (let ((top (gnus-topic-find-topology topic)))
287 (error "No such topic: %s" topic))
288 ;; We may have to extend if there is no parameters here
290 (unless (nthcdr 2 (cadr top))
291 (nconc (cadr top) (list nil)))
292 (unless (nthcdr 3 (cadr top))
293 (nconc (cadr top) (list nil)))
294 (setcar (nthcdr 3 (cadr top)) parameters)))
296 (defun gnus-group-topic-parameters (group)
297 "Compute the group parameters for GROUP taking into account inheritance from topics."
298 (let ((params-list (list (gnus-group-get-parameter group)))
299 topics params param out)
301 (gnus-group-goto-group group)
302 (setq topics (gnus-current-topics))
304 (push (gnus-topic-parameters (pop topics)) params-list))
305 ;; We probably have lots of nil elements here, so
306 ;; we remove them. Probably faster than doing this "properly".
307 (setq params-list (delq nil params-list))
308 ;; Now we have all the parameters, so we go through them
309 ;; and do inheritance in the obvious way.
310 (while (setq params (pop params-list))
311 (while (setq param (pop params))
313 (setq param (cons param t)))
314 ;; Override any old versions of this param.
315 (setq out (delq (assq (car param) out) out))
317 ;; Return the resulting parameter list.
320 ;;; General utility functions
322 (defun gnus-topic-enter-dribble ()
324 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
326 ;;; Generating group buffers
328 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
329 "List all newsgroups with unread articles of level LEVEL or lower, and
330 use the `gnus-group-topics' to sort the groups.
331 If ALL is non-nil, list groups that have no unread articles.
332 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
333 (set-buffer gnus-group-buffer)
334 (let ((buffer-read-only nil)
335 (lowest (or lowest 1)))
337 (setq gnus-topic-tallied-groups nil)
339 (when (or (not gnus-topic-alist)
340 (not gnus-topology-checked-p))
341 (gnus-topic-check-topology))
347 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
348 (gnus-group-prepare-flat-list-dead
349 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
353 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
354 (gnus-group-prepare-flat-list-dead
355 (setq gnus-killed-list (sort gnus-killed-list 'string<))
360 (when (< lowest gnus-level-zombie)
362 (let ((top (gnus-topic-find-topology list-topic)))
363 (gnus-topic-prepare-topic (cdr top) (car top)
364 (or topic-level level) all))
365 (gnus-topic-prepare-topic gnus-topic-topology 0
366 (or topic-level level) all))))
368 (gnus-group-set-mode-line)
369 (setq gnus-group-list-mode (cons level all))
370 (run-hooks 'gnus-group-prepare-hook))
372 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
373 "Insert TOPIC into the group buffer.
374 If SILENT, don't insert anything. Return the number of unread
375 articles in the topic and its subtopics."
376 (let* ((type (pop topicl))
377 (entries (gnus-topic-find-groups (car type) list-level all))
378 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
379 (gnus-group-indentation
380 (make-string (* gnus-topic-indent-level level) ? ))
381 (beg (progn (beginning-of-line) (point)))
382 (topicl (reverse topicl))
383 (all-entries entries)
386 info entry end active)
387 ;; Insert any sub-topics.
390 (gnus-topic-prepare-topic
391 (pop topicl) (1+ level) list-level all
395 ;; Insert all the groups that belong in this topic.
396 (while (setq entry (pop entries))
400 (gnus-group-insert-group-line
401 entry (if (member entry gnus-zombie-list) 8 9)
402 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 ;; Do the change in this rather odd manner because it has been
441 ;; reported that some topics share parts of some lists, for some
442 ;; reason. I have been unable to determine why this is the
443 ;; case, but this hack seems to take care of things.
444 (let ((data (cadr (gnus-topic-find-topology topic))))
446 (list (if insert 'visible 'invisible)
450 (setq gnus-topic-alist
451 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
452 (gnus-topic-insert-topic topic in-level)))))
454 (defun gnus-topic-insert-topic (topic &optional level)
456 (gnus-group-prepare-topics
457 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
458 nil nil topic level))
460 (defun gnus-topic-fold (&optional insert)
461 "Remove/insert the current topic."
462 (let ((topic (gnus-group-topic-name)))
465 (if (not (gnus-group-active-topic-p))
466 (gnus-topic-remove-topic
467 (or insert (not (gnus-topic-visible-p))))
468 (let ((gnus-topic-topology gnus-topic-active-topology)
469 (gnus-topic-alist gnus-topic-active-alist)
470 (gnus-group-list-mode (cons 5 t)))
471 (gnus-topic-remove-topic
472 (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
474 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
476 (let* ((visible (if visiblep "" "..."))
477 (indentation (make-string (* gnus-topic-indent-level level) ? ))
478 (total-number-of-articles unread)
479 (number-of-groups (length entries))
480 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
483 (gnus-add-text-properties
486 (eval gnus-topic-line-format-spec)
487 (gnus-topic-remove-excess-properties)1)
488 (list 'gnus-topic (intern name)
489 'gnus-topic-level level
490 'gnus-topic-unread unread
491 'gnus-active active-topic
492 'gnus-topic-visible visiblep))))
494 (defun gnus-topic-update-topics-containing-group (group)
495 "Update all topics that have GROUP as a member."
496 (when (and (eq major-mode 'gnus-group-mode)
499 (let ((alist gnus-topic-alist))
500 ;; This is probably not entirely correct. If a topic
501 ;; isn't shown, then it's not updated. But the updating
502 ;; should be performed in any case, since the topic's
503 ;; parent should be updated. Pfft.
505 (when (and (member group (cdar alist))
506 (gnus-topic-goto-topic (caar alist)))
507 (gnus-topic-update-topic-line (caar alist)))
510 (defun gnus-topic-update-topic ()
511 "Update all parent topics to the current group."
512 (when (and (eq major-mode 'gnus-group-mode)
514 (let ((group (gnus-group-group-name))
515 (buffer-read-only nil))
517 (gnus-get-info group)
518 (gnus-topic-goto-topic (gnus-current-topic)))
519 (gnus-topic-update-topic-line (gnus-group-topic-name))
520 (gnus-group-goto-group group)
521 (gnus-group-position-point)))))
523 (defun gnus-topic-goto-missing-group (group)
524 "Place point where GROUP is supposed to be inserted."
525 (let* ((topic (gnus-group-topic group))
526 (groups (cdr (assoc topic gnus-topic-alist)))
527 (g (cdr (member group groups)))
529 ;; Try to jump to a visible group.
530 (while (and g (not (gnus-group-goto-group (car g) t)))
532 ;; It wasn't visible, so we try to see where to insert it.
534 (setq g (cdr (member group (reverse groups))))
535 (while (and g unfound)
536 (when (gnus-group-goto-group (pop g) t)
540 (gnus-topic-goto-topic topic)
543 (defun gnus-topic-update-topic-line (topic-name &optional reads)
544 (let* ((top (gnus-topic-find-topology topic-name))
546 (children (cddr top))
547 (entries (gnus-topic-find-groups
548 (car type) (car gnus-group-list-mode)
549 (cdr gnus-group-list-mode)))
550 (parent (gnus-topic-parent-topic topic-name))
551 (all-entries entries)
554 (when (gnus-topic-goto-topic (car type))
555 ;; Tally all the groups that belong in this topic.
557 (setq unread (- (gnus-group-topic-unread) reads))
559 (incf unread (gnus-topic-unread (caar (pop children)))))
560 (while (setq entry (pop entries))
561 (when (numberp (car entry))
562 (incf unread (car entry)))))
563 (setq old-unread (gnus-group-topic-unread))
564 ;; Insert the topic line.
565 (gnus-topic-insert-topic-line
566 (car type) (gnus-topic-visible-p)
567 (not (eq (nth 2 type) 'hidden))
568 (gnus-group-topic-level) all-entries unread)
572 (gnus-topic-update-topic-line
573 parent (- old-unread (gnus-group-topic-unread))))
576 (defun gnus-topic-group-indentation ()
578 (* gnus-topic-indent-level
581 (gnus-topic-goto-topic (gnus-current-topic))
582 (gnus-group-topic-level))
588 (gnus-add-shutdown 'gnus-topic-close 'gnus)
590 (defun gnus-topic-close ()
591 (setq gnus-topic-active-topology nil
592 gnus-topic-active-alist nil
593 gnus-topic-killed-topics nil
594 gnus-topic-tallied-groups nil
595 gnus-topology-checked-p nil))
597 (defun gnus-topic-check-topology ()
598 ;; The first time we set the topology to whatever we have
599 ;; gotten here, which can be rather random.
600 (unless gnus-topic-alist
601 (gnus-topic-init-alist))
603 (setq gnus-topology-checked-p t)
604 ;; Go through the topic alist and make sure that all topics
605 ;; are in the topic topology.
606 (let ((topics (gnus-topic-list))
607 (alist gnus-topic-alist)
610 (unless (member (caar alist) topics)
611 (nconc gnus-topic-topology
612 (list (list (list (caar alist) 'visible))))
614 (setq alist (cdr alist)))
616 (gnus-topic-enter-dribble))
617 ;; Conversely, go through the topology and make sure that all
618 ;; topologies have alists.
620 (unless (assoc (car topics) gnus-topic-alist)
621 (push (list (car topics)) gnus-topic-alist))
623 ;; Go through all living groups and make sure that
624 ;; they belong to some topic.
625 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
627 (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
628 (newsrc gnus-newsrc-alist)
631 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
632 (setcdr entry (cons group (cdr entry))))))
633 ;; Go through all topics and make sure they contain only living groups.
634 (let ((alist gnus-topic-alist)
636 (while (setq topic (pop alist))
638 (if (gnus-gethash (cadr topic) gnus-newsrc-hashtb)
639 (setq topic (cdr topic))
640 (setcdr topic (cddr topic)))))))
642 (defun gnus-topic-init-alist ()
643 "Initialize the topic structures."
644 (setq gnus-topic-topology
645 (cons (list "Gnus" 'visible)
646 (mapcar (lambda (topic)
647 (list (list (car topic) 'visible)))
649 (setq gnus-topic-alist
651 (mapcar (lambda (info) (gnus-info-group info))
652 (cdr gnus-newsrc-alist)))
654 (gnus-topic-enter-dribble))
658 (defun gnus-topic-clean-alist ()
659 "Remove bogus groups from the topic alist."
660 (let ((topic-alist gnus-topic-alist)
662 (unless gnus-killed-hashtb
663 (gnus-make-hashtable-from-killed))
664 (while (setq topic (pop topic-alist))
665 (let ((topic-name (pop topic))
666 group filtered-topic)
667 (while (setq group (pop topic))
668 (when (and (or (gnus-gethash group gnus-active-hashtb)
669 (gnus-info-method (gnus-get-info group)))
670 (not (gnus-gethash group gnus-killed-hashtb)))
671 (push group filtered-topic)))
672 (push (cons topic-name (nreverse filtered-topic)) result)))
673 (setq gnus-topic-alist (nreverse result))))
675 (defun gnus-topic-change-level (group level oldlevel)
676 "Run when changing levels to enter/remove groups from topics."
678 (set-buffer gnus-group-buffer)
679 (when (and gnus-topic-mode
681 (not gnus-topic-inhibit-change-level))
682 ;; Remove the group from the topics.
683 (when (and (< oldlevel gnus-level-zombie)
684 (>= level gnus-level-zombie))
687 (when (setq alist (assoc (gnus-current-topic) gnus-topic-alist))
688 (setcdr alist (gnus-delete-first group (cdr alist))))))
689 ;; If the group is subscribed. then we enter it into the topics.
690 (when (and (< level gnus-level-zombie)
691 (>= oldlevel gnus-level-zombie))
692 (let* ((prev (gnus-group-group-name))
693 (gnus-topic-inhibit-change-level t)
694 (gnus-group-indentation
696 (* gnus-topic-indent-level
698 (gnus-topic-goto-topic (gnus-current-topic))
699 (gnus-group-topic-level))
702 (yanked (list group))
704 ;; Then we enter the yanked groups into the topics they belong
706 (when (setq alist (assoc (save-excursion
710 (caar gnus-topic-topology)))
713 (when (stringp yanked)
714 (setq yanked (list yanked)))
717 (if (not (cdr alist))
718 (setcdr alist (nconc yanked (cdr alist)))
719 (while (and (not end) (cdr alist))
720 (when (equal (cadr alist) prev)
721 (setcdr alist (nconc yanked (cdr alist)))
723 (setq alist (cdr alist)))
725 (nconc talist yanked))))))
726 (gnus-topic-update-topic)))))
728 (defun gnus-topic-goto-next-group (group props)
729 "Go to group or the next group after group."
731 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props))))
732 (if (gnus-group-goto-group group)
734 ;; The group is no longer visible.
735 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
736 (after (cdr (member group (cdr list)))))
737 ;; First try to put point on a group after the current one.
739 (not (gnus-group-goto-group (car after))))
740 (setq after (cdr after)))
741 ;; Then try to put point on a group before point.
743 (setq after (cdr (member group (reverse (cdr list)))))
745 (not (gnus-group-goto-group (car after))))
746 (setq after (cdr after))))
747 ;; Finally, just put point on the topic.
749 (gnus-topic-goto-topic (car list))
753 ;;; Topic-active functions
755 (defun gnus-topic-grok-active (&optional force)
756 "Parse all active groups and create topic structures for them."
757 ;; First we make sure that we have really read the active file.
759 (not gnus-topic-active-alist))
761 ;; Get a list of all groups available.
762 (mapatoms (lambda (g) (when (symbol-value g)
763 (push (symbol-name g) groups)))
765 (setq groups (sort groups 'string<))
766 ;; Init the variables.
767 (setq gnus-topic-active-topology (list (list "" 'visible)))
768 (setq gnus-topic-active-alist nil)
769 ;; Descend the top-level hierarchy.
770 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
771 ;; Set the top-level topic names to something nice.
772 (setcar (car gnus-topic-active-topology) "Gnus active")
773 (setcar (car gnus-topic-active-alist) "Gnus active"))))
775 (defun gnus-topic-grok-active-1 (topology groups)
776 (let* ((name (caar topology))
777 (prefix (concat "^" (regexp-quote name)))
778 tgroups ntopology group)
780 (string-match prefix (setq group (car groups))))
781 (if (not (string-match "\\." group (match-end 0)))
782 ;; There are no further hierarchies here, so we just
783 ;; enter this group into the list belonging to this
785 (push (pop groups) tgroups)
786 ;; New sub-hierarchy, so we add it to the topology.
787 (nconc topology (list (setq ntopology
788 (list (list (substring
789 group 0 (match-end 0))
791 ;; Descend the hierarchy.
792 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
793 ;; We remove the trailing "." from the topic name.
795 (if (string-match "\\.$" name)
796 (substring name 0 (match-beginning 0))
798 ;; Add this topic and its groups to the topic alist.
799 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
800 (setcar (car topology) name)
801 ;; We return the rest of the groups that didn't belong
805 ;;; Topic mode, commands and keymap.
807 (defvar gnus-topic-mode-map nil)
808 (defvar gnus-group-topic-map nil)
810 (unless gnus-topic-mode-map
811 (setq gnus-topic-mode-map (make-sparse-keymap))
813 ;; Override certain group mode keys.
814 (gnus-define-keys gnus-topic-mode-map
815 "=" gnus-topic-select-group
816 "\r" gnus-topic-select-group
817 " " gnus-topic-read-group
818 "\C-k" gnus-topic-kill-group
819 "\C-y" gnus-topic-yank-group
820 "\M-g" gnus-topic-get-new-news-this-topic
821 "AT" gnus-topic-list-active
822 "Gp" gnus-topic-edit-parameters
823 gnus-mouse-2 gnus-mouse-pick-topic)
825 ;; Define a new submap.
826 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
827 "#" gnus-topic-mark-topic
828 "\M-#" gnus-topic-unmark-topic
829 "n" gnus-topic-create-topic
830 "m" gnus-topic-move-group
831 "D" gnus-topic-remove-group
832 "c" gnus-topic-copy-group
833 "h" gnus-topic-hide-topic
834 "s" gnus-topic-show-topic
835 "M" gnus-topic-move-matching
836 "C" gnus-topic-copy-matching
837 "\C-i" gnus-topic-indent
838 [tab] gnus-topic-indent
839 "r" gnus-topic-rename
840 "\177" gnus-topic-delete)
842 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
843 "s" gnus-topic-sort-groups
844 "a" gnus-topic-sort-groups-by-alphabet
845 "u" gnus-topic-sort-groups-by-unread
846 "l" gnus-topic-sort-groups-by-level
847 "v" gnus-topic-sort-groups-by-score
848 "r" gnus-topic-sort-groups-by-rank
849 "m" gnus-topic-sort-groups-by-method))
851 (defun gnus-topic-make-menu-bar ()
852 (unless (boundp 'gnus-topic-menu)
854 gnus-topic-menu gnus-topic-mode-map ""
856 ["Toggle topics" gnus-topic-mode t]
858 ["Copy" gnus-topic-copy-group t]
859 ["Move" gnus-topic-move-group t]
860 ["Remove" gnus-topic-remove-group t]
861 ["Copy matching" gnus-topic-copy-matching t]
862 ["Move matching" gnus-topic-move-matching t])
864 ["Show" gnus-topic-show-topic t]
865 ["Hide" gnus-topic-hide-topic t]
866 ["Delete" gnus-topic-delete t]
867 ["Rename" gnus-topic-rename t]
868 ["Create" gnus-topic-create-topic t]
869 ["Mark" gnus-topic-mark-topic t]
870 ["Indent" gnus-topic-indent t])
871 ["List active" gnus-topic-list-active t]))))
873 (defun gnus-topic-mode (&optional arg redisplay)
874 "Minor mode for topicsifying Gnus group buffers."
875 (interactive (list current-prefix-arg t))
876 (when (eq major-mode 'gnus-group-mode)
877 (make-local-variable 'gnus-topic-mode)
878 (setq gnus-topic-mode
879 (if (null arg) (not gnus-topic-mode)
880 (> (prefix-numeric-value arg) 0)))
881 ;; Infest Gnus with topics.
882 (when gnus-topic-mode
883 (when (and menu-bar-mode
884 (gnus-visual-p 'topic-menu 'menu))
885 (gnus-topic-make-menu-bar))
886 (setq gnus-topic-line-format-spec
887 (gnus-parse-format gnus-topic-line-format
888 gnus-topic-line-format-alist t))
889 (unless (assq 'gnus-topic-mode minor-mode-alist)
890 (push '(gnus-topic-mode " Topic") minor-mode-alist))
891 (unless (assq 'gnus-topic-mode minor-mode-map-alist)
892 (push (cons 'gnus-topic-mode gnus-topic-mode-map)
893 minor-mode-map-alist))
894 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
895 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
896 (set (make-local-variable 'gnus-group-prepare-function)
897 'gnus-group-prepare-topics)
898 (set (make-local-variable 'gnus-group-get-parameter-function)
899 'gnus-group-topic-parameters)
900 (set (make-local-variable 'gnus-group-goto-next-group-function)
901 'gnus-topic-goto-next-group)
902 (set (make-local-variable 'gnus-group-indentation-function)
903 'gnus-topic-group-indentation)
904 (set (make-local-variable 'gnus-group-update-group-function)
905 'gnus-topic-update-topics-containing-group)
906 (set (make-local-variable 'gnus-group-sort-alist-function)
907 'gnus-group-sort-topic)
908 (setq gnus-group-change-level-function 'gnus-topic-change-level)
909 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
910 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
911 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
912 (setq gnus-topology-checked-p nil)
913 ;; We check the topology.
914 (when gnus-newsrc-alist
915 (gnus-topic-check-topology))
916 (run-hooks 'gnus-topic-mode-hook))
917 ;; Remove topic infestation.
918 (unless gnus-topic-mode
919 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
920 (remove-hook 'gnus-group-change-level-function
921 'gnus-topic-change-level)
922 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
923 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
924 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
926 (gnus-group-list-groups))))
928 (defun gnus-topic-select-group (&optional all)
929 "Select this newsgroup.
930 No article is selected automatically.
931 If ALL is non-nil, already read articles become readable.
932 If ALL is a number, fetch this number of articles.
934 If performed over a topic line, toggle folding the topic."
936 (if (gnus-group-topic-p)
937 (let ((gnus-group-list-mode
938 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
939 (gnus-topic-fold all))
940 (gnus-group-select-group all)))
942 (defun gnus-mouse-pick-topic (e)
943 "Select the group or topic under the mouse pointer."
946 (gnus-topic-read-group nil))
948 (defun gnus-topic-read-group (&optional all no-article group)
949 "Read news in this newsgroup.
950 If the prefix argument ALL is non-nil, already read articles become
951 readable. IF ALL is a number, fetch this number of articles. If the
952 optional argument NO-ARTICLE is non-nil, no article will be
953 auto-selected upon group entry. If GROUP is non-nil, fetch that
956 If performed over a topic line, toggle folding the topic."
958 (if (gnus-group-topic-p)
959 (let ((gnus-group-list-mode
960 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
961 (gnus-topic-fold all))
962 (gnus-group-read-group all no-article group)))
964 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
967 (read-string "New topic: ")
968 (gnus-current-topic)))
969 ;; Check whether this topic already exists.
970 (when (gnus-topic-find-topology topic)
971 (error "Topic already exists"))
973 (setq parent (caar gnus-topic-topology)))
974 (let ((top (cdr (gnus-topic-find-topology parent)))
975 (full-topic (or full-topic `((,topic visible)))))
977 (error "No such parent topic: %s" parent))
980 (while (and (cdr top)
981 (not (equal (caaadr top) previous)))
982 (setq top (cdr top)))
983 (setcdr top (cons full-topic (cdr top))))
984 (nconc top (list full-topic)))
985 (unless (assoc topic gnus-topic-alist)
986 (push (list topic) gnus-topic-alist)))
987 (gnus-topic-enter-dribble)
988 (gnus-group-list-groups)
989 (gnus-topic-goto-topic topic))
991 (defun gnus-topic-move-group (n topic &optional copyp)
992 "Move the next N groups to TOPIC.
993 If COPYP, copy the groups instead."
995 (list current-prefix-arg
996 (completing-read "Move to topic: " gnus-topic-alist nil t)))
997 (let ((groups (gnus-group-process-prefix n))
998 (topicl (assoc topic gnus-topic-alist))
999 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1000 (start-topic (gnus-group-topic-name))
1004 (gnus-group-remove-mark g)
1006 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1008 (setcdr entry (gnus-delete-first g (cdr entry))))
1009 (nconc topicl (list g)))
1011 (gnus-topic-enter-dribble)
1013 (gnus-group-goto-group start-group)
1014 (gnus-topic-goto-topic start-topic))
1015 (gnus-group-list-groups)))
1017 (defun gnus-topic-remove-group (&optional arg)
1018 "Remove the current group from the topic."
1020 (gnus-group-iterate arg
1022 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1023 (buffer-read-only nil))
1024 (when (and topicl group)
1026 (gnus-delete-first group topicl))
1027 (gnus-topic-update-topic)
1028 (gnus-group-position-point)))))
1030 (defun gnus-topic-copy-group (n topic)
1031 "Copy the current group to a topic."
1033 (list current-prefix-arg
1034 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1035 (gnus-topic-move-group n topic t))
1037 (defun gnus-topic-kill-group (&optional n discard)
1038 "Kill the next N groups."
1040 (if (gnus-group-topic-p)
1041 (let ((topic (gnus-group-topic-name)))
1042 (gnus-topic-remove-topic nil t)
1043 (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1044 gnus-topic-killed-topics)
1045 (gnus-topic-enter-dribble))
1046 (gnus-group-kill-group n discard)
1047 (gnus-topic-update-topic)))
1049 (defun gnus-topic-yank-group (&optional arg)
1050 "Yank the last topic."
1052 (if gnus-topic-killed-topics
1054 (or (gnus-group-topic-name)
1055 (gnus-topic-next-topic (gnus-current-topic))))
1056 (item (cdr (pop gnus-topic-killed-topics))))
1057 (gnus-topic-create-topic
1058 (caar item) (gnus-topic-parent-topic previous) previous
1060 (gnus-topic-enter-dribble)
1061 (gnus-topic-goto-topic (caar item)))
1062 (let* ((prev (gnus-group-group-name))
1063 (gnus-topic-inhibit-change-level t)
1064 (gnus-group-indentation
1066 (* gnus-topic-indent-level
1068 (gnus-topic-goto-topic (gnus-current-topic))
1069 (gnus-group-topic-level))
1073 ;; We first yank the groups the normal way...
1074 (setq yanked (gnus-group-yank-group arg))
1075 ;; Then we enter the yanked groups into the topics they belong
1077 (setq alist (assoc (save-excursion
1079 (gnus-current-topic))
1081 (when (stringp yanked)
1082 (setq yanked (list yanked)))
1084 (nconc alist yanked)
1085 (if (not (cdr alist))
1086 (setcdr alist (nconc yanked (cdr alist)))
1088 (when (equal (cadr alist) prev)
1089 (setcdr alist (nconc yanked (cdr alist)))
1091 (setq alist (cdr alist))))))
1092 (gnus-topic-update-topic)))
1094 (defun gnus-topic-hide-topic ()
1095 "Hide the current topic."
1097 (when (gnus-current-topic)
1098 (gnus-topic-goto-topic (gnus-current-topic))
1099 (gnus-topic-remove-topic nil nil 'hidden)))
1101 (defun gnus-topic-show-topic ()
1102 "Show the hidden topic."
1104 (when (gnus-group-topic-p)
1105 (gnus-topic-remove-topic t nil 'shown)))
1107 (defun gnus-topic-mark-topic (topic &optional unmark)
1108 "Mark all groups in the topic with the process mark."
1109 (interactive (list (gnus-current-topic)))
1111 (let ((groups (gnus-topic-find-groups topic 9 t)))
1113 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1114 (gnus-info-group (nth 2 (pop groups))))))))
1116 (defun gnus-topic-unmark-topic (topic &optional unmark)
1117 "Remove the process mark from all groups in the topic."
1118 (interactive (list (gnus-current-topic)))
1119 (gnus-topic-mark-topic topic t))
1121 (defun gnus-topic-get-new-news-this-topic (&optional n)
1122 "Check for new news in the current topic."
1124 (if (not (gnus-group-topic-p))
1125 (gnus-group-get-new-news-this-group n)
1126 (gnus-topic-mark-topic (gnus-group-topic-name))
1127 (gnus-group-get-new-news-this-group)))
1129 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1130 "Move all groups that match REGEXP to some topic."
1135 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1136 (read-string (format "Move to %s (regexp): " topic))))))
1137 (gnus-group-mark-regexp regexp)
1138 (gnus-topic-move-group nil topic copyp))
1140 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1141 "Copy all groups that match REGEXP to some topic."
1146 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1147 (read-string (format "Copy to %s (regexp): " topic))))))
1148 (gnus-topic-move-matching regexp topic t))
1150 (defun gnus-topic-delete (topic)
1152 (interactive (list (gnus-group-topic-name)))
1154 (error "No topic to be deleted"))
1155 (let ((entry (assoc topic gnus-topic-alist))
1156 (buffer-read-only nil))
1158 (error "Topic not empty"))
1159 ;; Delete if visible.
1160 (when (gnus-topic-goto-topic topic)
1162 ;; Remove from alist.
1163 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1164 ;; Remove from topology.
1165 (gnus-topic-find-topology topic nil nil 'delete)))
1167 (defun gnus-topic-rename (old-name new-name)
1170 (let ((topic (gnus-current-topic)))
1172 (read-string (format "Rename %s to: " topic)))))
1173 (let ((top (gnus-topic-find-topology old-name))
1174 (entry (assoc old-name gnus-topic-alist)))
1176 (setcar (cadr top) new-name))
1178 (setcar entry new-name))
1180 (gnus-group-list-groups)))
1182 (defun gnus-topic-indent (&optional unindent)
1183 "Indent a topic -- make it a sub-topic of the previous topic.
1184 If UNINDENT, remove an indentation."
1187 (gnus-topic-unindent)
1188 (let* ((topic (gnus-current-topic))
1189 (parent (gnus-topic-previous-topic topic)))
1191 (error "Nothing to indent %s into" topic))
1193 (gnus-topic-goto-topic topic)
1194 (gnus-topic-kill-group)
1195 (gnus-topic-create-topic
1196 topic parent nil (cdr (pop gnus-topic-killed-topics)))
1197 (or (gnus-topic-goto-topic topic)
1198 (gnus-topic-goto-topic parent))))))
1200 (defun gnus-topic-unindent ()
1203 (let* ((topic (gnus-current-topic))
1204 (parent (gnus-topic-parent-topic topic))
1205 (grandparent (gnus-topic-parent-topic parent)))
1207 (error "Nothing to indent %s into" topic))
1209 (gnus-topic-goto-topic topic)
1210 (gnus-topic-kill-group)
1211 (gnus-topic-create-topic
1212 topic grandparent (gnus-topic-next-topic parent)
1213 (cdr (pop gnus-topic-killed-topics)))
1214 (gnus-topic-goto-topic topic))))
1216 (defun gnus-topic-list-active (&optional force)
1217 "List all groups that Gnus knows about in a topicsified fashion.
1218 If FORCE, always re-read the active file."
1221 (gnus-get-killed-groups))
1222 (gnus-topic-grok-active force)
1223 (let ((gnus-topic-topology gnus-topic-active-topology)
1224 (gnus-topic-alist gnus-topic-active-alist)
1225 gnus-killed-list gnus-zombie-list)
1226 (gnus-group-list-groups 9 nil 1)))
1228 ;;; Topic sorting functions
1230 (defun gnus-topic-edit-parameters (group)
1231 "Edit the group parameters of GROUP.
1232 If performed on a topic, edit the topic parameters instead."
1233 (interactive (list (gnus-group-group-name)))
1235 (gnus-group-edit-group-parameters group)
1236 (if (not (gnus-group-topic-p))
1237 (error "Nothing to edit on the current line.")
1238 (let ((topic (gnus-group-topic-name)))
1240 (gnus-topic-parameters topic)
1241 "Editing the topic parameters."
1243 (gnus-topic-set-parameters ,topic form)))))))
1245 (defun gnus-group-sort-topic (func reverse)
1246 "Sort groups in the topics according to FUNC and REVERSE."
1247 (let ((alist gnus-topic-alist))
1249 (gnus-topic-sort-topic (pop alist) func reverse))))
1251 (defun gnus-topic-sort-topic (topic func reverse)
1252 ;; Each topic only lists the name of the group, while
1253 ;; the sort predicates expect group infos as inputs.
1254 ;; So we first transform the group names into infos,
1255 ;; then sort, and then transform back into group names.
1259 (lambda (info) (gnus-info-group info))
1262 (lambda (group) (gnus-get-info group))
1265 ;; Do the reversal, if necessary.
1267 (setcdr topic (nreverse (cdr topic)))))
1269 (defun gnus-topic-sort-groups (func &optional reverse)
1270 "Sort the current topic according to FUNC.
1271 If REVERSE, reverse the sorting order."
1272 (interactive (list gnus-group-sort-function current-prefix-arg))
1273 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1274 (gnus-topic-sort-topic
1275 topic (gnus-make-sort-function func) reverse)
1276 (gnus-group-list-groups)))
1278 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1279 "Sort the current topic alphabetically by group name.
1280 If REVERSE, sort in reverse order."
1282 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1284 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1285 "Sort the current topic by number of unread articles.
1286 If REVERSE, sort in reverse order."
1288 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1290 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1291 "Sort the current topic by group level.
1292 If REVERSE, sort in reverse order."
1294 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1296 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1297 "Sort the current topic by group score.
1298 If REVERSE, sort in reverse order."
1300 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1302 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1303 "Sort the current topic by group rank.
1304 If REVERSE, sort in reverse order."
1306 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1308 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1309 "Sort the current topic alphabetically by backend name.
1310 If REVERSE, sort in reverse order."
1312 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1314 (provide 'gnus-topic)
1316 ;;; gnus-topic.el ends here