1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96,97 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.
29 (eval-when-compile (require 'cl))
35 (defgroup gnus-topic nil
39 (defvar gnus-topic-mode nil
40 "Minor mode for Gnus group buffers.")
42 (defcustom gnus-topic-mode-hook nil
43 "Hook run in topic mode buffers."
47 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
48 "Format of topic lines.
49 It works along the same lines as a normal formatting string,
50 with some simple extensions.
52 %i Indentation based on topic level.
54 %v Nothing if the topic is visible, \"...\" otherwise.
55 %g Number of groups in the topic.
56 %a Number of unread articles in the groups in the topic.
57 %A Number of unread articles in the groups in the topic and its subtopics.
62 (defcustom gnus-topic-indent-level 2
63 "*How much each subtopic should be indented."
67 (defcustom gnus-topic-display-empty-topics t
68 "*If non-nil, display the topic lines even of topics that have no unread articles."
72 ;; Internal variables.
74 (defvar gnus-topic-active-topology nil)
75 (defvar gnus-topic-active-alist nil)
77 (defvar gnus-topology-checked-p nil
78 "Whether the topology has been checked in this session.")
80 (defvar gnus-topic-killed-topics nil)
81 (defvar gnus-topic-inhibit-change-level nil)
83 (defconst gnus-topic-line-format-alist
87 (?g number-of-groups ?d)
88 (?a (gnus-topic-articles-in-topic entries) ?d)
89 (?A total-number-of-articles ?d)
92 (defvar gnus-topic-line-format-spec nil)
96 (defun gnus-group-topic-name ()
97 "The name of the topic on the current line."
98 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
99 (and topic (symbol-name topic))))
101 (defun gnus-group-topic-level ()
102 "The level of the topic on the current line."
103 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
105 (defun gnus-group-topic-unread ()
106 "The number of unread articles in topic on the current line."
107 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
109 (defun gnus-topic-unread (topic)
110 "Return the number of unread articles in TOPIC."
112 (and (gnus-topic-goto-topic topic)
113 (gnus-group-topic-unread)))
116 (defun gnus-group-topic-p ()
117 "Return non-nil if the current line is a topic."
118 (gnus-group-topic-name))
120 (defun gnus-topic-visible-p ()
121 "Return non-nil if the current topic is visible."
122 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
124 (defun gnus-topic-articles-in-topic (entries)
128 (when (numberp (setq number (car (pop entries))))
129 (incf total number)))
132 (defun gnus-group-topic (group)
133 "Return the topic GROUP is a member of."
134 (let ((alist gnus-topic-alist)
137 (when (member group (cdar alist))
138 (setq out (caar alist)
140 (setq alist (cdr alist)))
143 (defun gnus-group-parent-topic (group)
144 "Return the topic GROUP is member of by looking at the group buffer."
146 (set-buffer gnus-group-buffer)
147 (if (gnus-group-goto-group group)
149 (gnus-group-topic group))))
151 (defun gnus-topic-goto-topic (topic)
154 (gnus-goto-char (text-property-any (point-min) (point-max)
155 'gnus-topic (intern topic)))))
157 (defun gnus-current-topic ()
158 "Return the name of the current topic."
160 (or (get-text-property (point) 'gnus-topic)
162 (and (gnus-goto-char (previous-single-property-change
163 (point) 'gnus-topic))
164 (get-text-property (max (1- (point)) (point-min))
167 (symbol-name result))))
169 (defun gnus-current-topics ()
170 "Return a list of all current topics, lowest in hierarchy first."
171 (let ((topic (gnus-current-topic))
175 (setq topic (gnus-topic-parent-topic topic)))
178 (defun gnus-group-active-topic-p ()
179 "Say whether the current topic comes from the active topics."
182 (get-text-property (point) 'gnus-active)))
184 (defun gnus-topic-find-groups (topic &optional level all)
185 "Return entries for all visible groups in TOPIC."
186 (let ((groups (cdr (assoc topic gnus-topic-alist)))
187 info clevel unread group lowest params visible-groups entry active)
188 (setq lowest (or lowest 1))
189 (setq level (or level 7))
190 ;; We go through the newsrc to look for matches.
192 (when (setq group (pop groups))
193 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
195 params (gnus-info-params info)
196 active (gnus-active group)
197 unread (or (car entry)
198 (and (not (equal group "dummy.group"))
200 (- (1+ (cdr active)) (car active))))
201 clevel (or (gnus-info-level info)
202 (if (member group gnus-zombie-list) 8 9))))
204 unread ; nil means that the group is dead.
206 (>= clevel lowest) ; Is inside the level we want.
209 gnus-group-list-inactive-groups
211 (and gnus-list-groups-with-ticked-articles
212 (cdr (assq 'tick (gnus-info-marks info))))
213 ; Has right readedness.
214 ;; Check for permanent visibility.
215 (and gnus-permanently-visible-groups
216 (string-match gnus-permanently-visible-groups group))
217 (memq 'visible params)
218 (cdr (assq 'visible params)))
219 ;; Add this group to the list of visible groups.
220 (push (or entry group) visible-groups)))
221 (nreverse visible-groups)))
223 (defun gnus-topic-previous-topic (topic)
224 "Return the previous topic on the same level as TOPIC."
225 (let ((top (cddr (gnus-topic-find-topology
226 (gnus-topic-parent-topic topic)))))
227 (unless (equal topic (caaar top))
228 (while (and top (not (equal (caaadr top) topic)))
229 (setq top (cdr top)))
232 (defun gnus-topic-parent-topic (topic &optional topology)
233 "Return the parent of TOPIC."
235 (setq topology gnus-topic-topology))
236 (let ((parent (car (pop topology)))
239 (not (setq found (equal (caaar topology) topic)))
240 (not (setq result (gnus-topic-parent-topic
241 topic (car topology)))))
242 (setq topology (cdr topology)))
243 (or result (and found parent))))
245 (defun gnus-topic-next-topic (topic &optional previous)
246 "Return the next sibling of TOPIC."
247 (let ((parentt (cddr (gnus-topic-find-topology
248 (gnus-topic-parent-topic topic))))
251 (not (equal (caaar parentt) topic)))
252 (setq prev (caaar parentt)
253 parentt (cdr parentt)))
258 (defun gnus-topic-forward-topic (num)
259 "Go to the next topic on the same level as the current one."
260 (let* ((topic (gnus-current-topic))
261 (way (if (< num 0) 'gnus-topic-previous-topic
262 'gnus-topic-next-topic))
264 (while (and (not (zerop num))
265 (setq topic (funcall way topic)))
266 (when (gnus-topic-goto-topic topic)
269 (goto-char (point-max)))
272 (defun gnus-topic-find-topology (topic &optional topology level remove)
273 "Return the topology of TOPIC."
275 (setq topology gnus-topic-topology)
279 (if (equal (caar topology) topic)
282 (delq topology remove))
283 (cons level topology))
284 (setq topology (cdr topology))
286 (not (setq result (gnus-topic-find-topology
287 topic (car topology) (1+ level)
289 (setq topology (cdr topology)))
292 (defvar gnus-tmp-topics nil)
293 (defun gnus-topic-list (&optional topology)
294 "Return a list of all topics in the topology."
296 (setq topology gnus-topic-topology
297 gnus-tmp-topics nil))
298 (push (caar topology) gnus-tmp-topics)
299 (mapcar 'gnus-topic-list (cdr topology))
302 ;;; Topic parameter jazz
304 (defun gnus-topic-parameters (topic)
305 "Return the parameters for TOPIC."
306 (let ((top (gnus-topic-find-topology topic)))
308 (nth 3 (cadr top)))))
310 (defun gnus-topic-set-parameters (topic parameters)
311 "Set the topic parameters of TOPIC to PARAMETERS."
312 (let ((top (gnus-topic-find-topology topic)))
314 (error "No such topic: %s" topic))
315 ;; We may have to extend if there is no parameters here
317 (unless (nthcdr 2 (cadr top))
318 (nconc (cadr top) (list nil)))
319 (unless (nthcdr 3 (cadr top))
320 (nconc (cadr top) (list nil)))
321 (setcar (nthcdr 3 (cadr top)) parameters)
323 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
325 (defun gnus-group-topic-parameters (group)
326 "Compute the group parameters for GROUP taking into account inheritance from topics."
327 (let ((params-list (list (gnus-group-get-parameter group)))
328 topics params param out)
330 (gnus-group-goto-group group)
331 (setq topics (gnus-current-topics))
333 (push (gnus-topic-parameters (pop topics)) params-list))
334 ;; We probably have lots of nil elements here, so
335 ;; we remove them. Probably faster than doing this "properly".
336 (setq params-list (delq nil params-list))
337 ;; Now we have all the parameters, so we go through them
338 ;; and do inheritance in the obvious way.
339 (while (setq params (pop params-list))
340 (while (setq param (pop params))
342 (setq param (cons param t)))
343 ;; Override any old versions of this param.
344 (setq out (delq (assq (car param) out) out))
346 ;; Return the resulting parameter list.
349 ;;; General utility functions
351 (defun gnus-topic-enter-dribble ()
353 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
355 ;;; Generating group buffers
357 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
358 "List all newsgroups with unread articles of level LEVEL or lower, and
359 use the `gnus-group-topics' to sort the groups.
360 If ALL is non-nil, list groups that have no unread articles.
361 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
362 (set-buffer gnus-group-buffer)
363 (let ((buffer-read-only nil)
364 (lowest (or lowest 1)))
366 (when (or (not gnus-topic-alist)
367 (not gnus-topology-checked-p))
368 (gnus-topic-check-topology))
374 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
375 (gnus-group-prepare-flat-list-dead
376 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
380 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
381 (gnus-group-prepare-flat-list-dead
382 (setq gnus-killed-list (sort gnus-killed-list 'string<))
388 (when (< lowest gnus-level-zombie)
390 (let ((top (gnus-topic-find-topology list-topic)))
391 (gnus-topic-prepare-topic (cdr top) (car top)
392 (or topic-level level) all))
393 (gnus-topic-prepare-topic gnus-topic-topology 0
394 (or topic-level level) all)))
396 (gnus-group-set-mode-line)
397 (setq gnus-group-list-mode (cons level all))
398 (run-hooks 'gnus-group-prepare-hook))))
400 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
401 "Insert TOPIC into the group buffer.
402 If SILENT, don't insert anything. Return the number of unread
403 articles in the topic and its subtopics."
404 (let* ((type (pop topicl))
405 (entries (gnus-topic-find-groups (car type) list-level all))
406 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
407 (gnus-group-indentation
408 (make-string (* gnus-topic-indent-level level) ? ))
409 (beg (progn (beginning-of-line) (point)))
410 (topicl (reverse topicl))
411 (all-entries entries)
412 (point-max (point-max))
415 info entry end active tick)
416 ;; Insert any sub-topics.
419 (gnus-topic-prepare-topic
420 (pop topicl) (1+ level) list-level all
424 ;; Insert all the groups that belong in this topic.
425 (while (setq entry (pop entries))
429 (gnus-group-insert-group-line
430 entry (if (member entry gnus-zombie-list) 8 9)
431 nil (- (1+ (cdr (setq active (gnus-active entry))))
435 (when (setq info (nth 2 entry))
436 (gnus-group-insert-group-line
437 (gnus-info-group info)
438 (gnus-info-level info) (gnus-info-marks info)
439 (car entry) (gnus-info-method info)))))
440 (when (and (listp entry)
441 (numberp (car entry)))
442 (incf unread (car entry)))
446 ;; Insert the topic line.
447 (when (and (not silent)
448 (or gnus-topic-display-empty-topics ;We want empty topics
449 (not (zerop unread)) ;Non-empty
450 tick ;Ticked articles
451 (/= point-max (point-max)))) ;Unactivated groups
452 (gnus-extent-start-open (point))
453 (gnus-topic-insert-topic-line
455 (not (eq (nth 2 type) 'hidden))
456 level all-entries unread))
460 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
461 "Remove the current topic."
462 (let ((topic (gnus-group-topic-name))
463 (level (gnus-group-topic-level))
464 (beg (progn (beginning-of-line) (point)))
467 (while (and (zerop (forward-line 1))
468 (> (or (gnus-group-topic-level) (1+ level)) level)))
469 (delete-region beg (point))
470 ;; Do the change in this rather odd manner because it has been
471 ;; reported that some topics share parts of some lists, for some
472 ;; reason. I have been unable to determine why this is the
473 ;; case, but this hack seems to take care of things.
474 (let ((data (cadr (gnus-topic-find-topology topic))))
476 (list (if insert 'visible 'invisible)
480 (setq gnus-topic-alist
481 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
482 (gnus-topic-insert-topic topic in-level)))))
484 (defun gnus-topic-insert-topic (topic &optional level)
486 (gnus-group-prepare-topics
487 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
488 nil nil topic level))
490 (defun gnus-topic-fold (&optional insert)
491 "Remove/insert the current topic."
492 (let ((topic (gnus-group-topic-name)))
495 (if (not (gnus-group-active-topic-p))
496 (gnus-topic-remove-topic
497 (or insert (not (gnus-topic-visible-p))))
498 (let ((gnus-topic-topology gnus-topic-active-topology)
499 (gnus-topic-alist gnus-topic-active-alist)
500 (gnus-group-list-mode (cons 5 t)))
501 (gnus-topic-remove-topic
502 (or insert (not (gnus-topic-visible-p))) nil nil 9)
503 (gnus-topic-enter-dribble)))))))
505 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
507 (let* ((visible (if visiblep "" "..."))
508 (indentation (make-string (* gnus-topic-indent-level level) ? ))
509 (total-number-of-articles unread)
510 (number-of-groups (length entries))
511 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
514 (gnus-add-text-properties
517 (eval gnus-topic-line-format-spec))
518 (list 'gnus-topic (intern name)
519 'gnus-topic-level level
520 'gnus-topic-unread unread
521 'gnus-active active-topic
522 'gnus-topic-visible visiblep))))
524 (defun gnus-topic-update-topics-containing-group (group)
525 "Update all topics that have GROUP as a member."
526 (when (and (eq major-mode 'gnus-group-mode)
529 (let ((alist gnus-topic-alist))
530 ;; This is probably not entirely correct. If a topic
531 ;; isn't shown, then it's not updated. But the updating
532 ;; should be performed in any case, since the topic's
533 ;; parent should be updated. Pfft.
535 (when (and (member group (cdar alist))
536 (gnus-topic-goto-topic (caar alist)))
537 (gnus-topic-update-topic-line (caar alist)))
540 (defun gnus-topic-update-topic ()
541 "Update all parent topics to the current group."
542 (when (and (eq major-mode 'gnus-group-mode)
544 (let ((group (gnus-group-group-name))
546 (buffer-read-only nil))
548 (gnus-get-info group)
549 (gnus-topic-goto-topic (gnus-current-topic)))
550 (gnus-topic-update-topic-line (gnus-group-topic-name))
553 (gnus-group-position-point)))))
555 (defun gnus-topic-goto-missing-group (group)
556 "Place point where GROUP is supposed to be inserted."
557 (let* ((topic (gnus-group-topic group))
558 (groups (cdr (assoc topic gnus-topic-alist)))
559 (g (cdr (member group groups)))
561 ;; Try to jump to a visible group.
562 (while (and g (not (gnus-group-goto-group (car g) t)))
564 ;; It wasn't visible, so we try to see where to insert it.
566 (setq g (cdr (member group (reverse groups))))
567 (while (and g unfound)
568 (when (gnus-group-goto-group (pop g) t)
573 (not (gnus-topic-goto-missing-topic topic)))
574 (gnus-topic-insert-topic-line
575 topic t t (car (gnus-topic-find-topology topic)) nil 0)))))
577 (defun gnus-topic-goto-missing-topic (topic)
578 (if (gnus-topic-goto-topic topic)
580 ;; Topic not displayed.
581 (let* ((top (gnus-topic-find-topology
582 (gnus-topic-parent-topic topic)))
583 (tp (reverse (cddr top))))
584 (while (not (equal (caaar tp) topic))
588 (not (gnus-topic-goto-topic (caaar tp))))
591 (gnus-topic-forward-topic 1)
592 (gnus-topic-goto-missing-topic (caadr top))))
595 (defun gnus-topic-update-topic-line (topic-name &optional reads)
596 (let* ((top (gnus-topic-find-topology topic-name))
598 (children (cddr top))
599 (entries (gnus-topic-find-groups
600 (car type) (car gnus-group-list-mode)
601 (cdr gnus-group-list-mode)))
602 (parent (gnus-topic-parent-topic topic-name))
603 (all-entries entries)
606 (when (gnus-topic-goto-topic (car type))
607 ;; Tally all the groups that belong in this topic.
609 (setq unread (- (gnus-group-topic-unread) reads))
611 (incf unread (gnus-topic-unread (caar (pop children)))))
612 (while (setq entry (pop entries))
613 (when (numberp (car entry))
614 (incf unread (car entry)))))
615 (setq old-unread (gnus-group-topic-unread))
616 ;; Insert the topic line.
617 (gnus-topic-insert-topic-line
618 (car type) (gnus-topic-visible-p)
619 (not (eq (nth 2 type) 'hidden))
620 (gnus-group-topic-level) all-entries unread)
624 (gnus-topic-update-topic-line
625 parent (- old-unread (gnus-group-topic-unread))))
628 (defun gnus-topic-group-indentation ()
630 (* gnus-topic-indent-level
633 (gnus-topic-goto-topic (gnus-current-topic))
634 (gnus-group-topic-level))
640 (gnus-add-shutdown 'gnus-topic-close 'gnus)
642 (defun gnus-topic-close ()
643 (setq gnus-topic-active-topology nil
644 gnus-topic-active-alist nil
645 gnus-topic-killed-topics nil
646 gnus-topology-checked-p nil))
648 (defun gnus-topic-check-topology ()
649 ;; The first time we set the topology to whatever we have
650 ;; gotten here, which can be rather random.
651 (unless gnus-topic-alist
652 (gnus-topic-init-alist))
654 (setq gnus-topology-checked-p t)
655 ;; Go through the topic alist and make sure that all topics
656 ;; are in the topic topology.
657 (let ((topics (gnus-topic-list))
658 (alist gnus-topic-alist)
661 (unless (member (caar alist) topics)
662 (nconc gnus-topic-topology
663 (list (list (list (caar alist) 'visible))))
665 (setq alist (cdr alist)))
667 (gnus-topic-enter-dribble))
668 ;; Conversely, go through the topology and make sure that all
669 ;; topologies have alists.
671 (unless (assoc (car topics) gnus-topic-alist)
672 (push (list (car topics)) gnus-topic-alist))
674 ;; Go through all living groups and make sure that
675 ;; they belong to some topic.
676 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
678 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
679 (newsrc (cdr gnus-newsrc-alist))
682 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
683 (setcdr entry (list group))
684 (setq entry (cdr entry)))))
685 ;; Go through all topics and make sure they contain only living groups.
686 (let ((alist gnus-topic-alist)
688 (while (setq topic (pop alist))
690 (if (and (cadr topic)
691 (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
692 (setq topic (cdr topic))
693 (setcdr topic (cddr topic)))))))
695 (defun gnus-topic-init-alist ()
696 "Initialize the topic structures."
697 (setq gnus-topic-topology
698 (cons (list "Gnus" 'visible)
699 (mapcar (lambda (topic)
700 (list (list (car topic) 'visible)))
702 (setq gnus-topic-alist
704 (mapcar (lambda (info) (gnus-info-group info))
705 (cdr gnus-newsrc-alist)))
707 (gnus-topic-enter-dribble))
711 (defun gnus-topic-clean-alist ()
712 "Remove bogus groups from the topic alist."
713 (let ((topic-alist gnus-topic-alist)
715 (unless gnus-killed-hashtb
716 (gnus-make-hashtable-from-killed))
717 (while (setq topic (pop topic-alist))
718 (let ((topic-name (pop topic))
719 group filtered-topic)
720 (while (setq group (pop topic))
721 (when (and (or (gnus-gethash group gnus-active-hashtb)
722 (gnus-info-method (gnus-get-info group)))
723 (not (gnus-gethash group gnus-killed-hashtb)))
724 (push group filtered-topic)))
725 (push (cons topic-name (nreverse filtered-topic)) result)))
726 (setq gnus-topic-alist (nreverse result))))
728 (defun gnus-topic-change-level (group level oldlevel &optional previous)
729 "Run when changing levels to enter/remove groups from topics."
731 (set-buffer gnus-group-buffer)
732 (gnus-group-goto-group (or (car (nth 2 previous)) group))
733 (when (and gnus-topic-mode
735 (not gnus-topic-inhibit-change-level))
736 ;; Remove the group from the topics.
737 (when (and (< oldlevel gnus-level-zombie)
738 (>= level gnus-level-zombie))
741 (when (setq alist (assoc (gnus-current-topic) gnus-topic-alist))
742 (setcdr alist (gnus-delete-first group (cdr alist))))))
743 ;; If the group is subscribed we enter it into the topics.
744 (when (and (< level gnus-level-zombie)
745 (>= oldlevel gnus-level-zombie))
746 (let* ((prev (gnus-group-group-name))
747 (gnus-topic-inhibit-change-level t)
748 (gnus-group-indentation
750 (* gnus-topic-indent-level
752 (gnus-topic-goto-topic (gnus-current-topic))
753 (gnus-group-topic-level))
756 (yanked (list group))
758 ;; Then we enter the yanked groups into the topics they belong
760 (when (setq alist (assoc (save-excursion
764 (caar gnus-topic-topology)))
767 (when (stringp yanked)
768 (setq yanked (list yanked)))
771 (if (not (cdr alist))
772 (setcdr alist (nconc yanked (cdr alist)))
773 (while (and (not end) (cdr alist))
774 (when (equal (cadr alist) prev)
775 (setcdr alist (nconc yanked (cdr alist)))
777 (setq alist (cdr alist)))
779 (nconc talist yanked))))))
780 (gnus-topic-update-topic)))))
782 (defun gnus-topic-goto-next-group (group props)
783 "Go to group or the next group after group."
785 (if (not (memq 'gnus-topic props))
786 (goto-char (point-max))
787 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
788 (if (gnus-group-goto-group group)
790 ;; The group is no longer visible.
791 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
792 (after (cdr (member group (cdr list)))))
793 ;; First try to put point on a group after the current one.
795 (not (gnus-group-goto-group (car after))))
796 (setq after (cdr after)))
797 ;; Then try to put point on a group before point.
799 (setq after (cdr (member group (reverse (cdr list)))))
801 (not (gnus-group-goto-group (car after))))
802 (setq after (cdr after))))
803 ;; Finally, just put point on the topic.
805 (goto-char (point-min))
807 (gnus-topic-goto-topic (car list))
811 ;;; Topic-active functions
813 (defun gnus-topic-grok-active (&optional force)
814 "Parse all active groups and create topic structures for them."
815 ;; First we make sure that we have really read the active file.
817 (not gnus-topic-active-alist))
819 ;; Get a list of all groups available.
820 (mapatoms (lambda (g) (when (symbol-value g)
821 (push (symbol-name g) groups)))
823 (setq groups (sort groups 'string<))
824 ;; Init the variables.
825 (setq gnus-topic-active-topology (list (list "" 'visible)))
826 (setq gnus-topic-active-alist nil)
827 ;; Descend the top-level hierarchy.
828 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
829 ;; Set the top-level topic names to something nice.
830 (setcar (car gnus-topic-active-topology) "Gnus active")
831 (setcar (car gnus-topic-active-alist) "Gnus active"))))
833 (defun gnus-topic-grok-active-1 (topology groups)
834 (let* ((name (caar topology))
835 (prefix (concat "^" (regexp-quote name)))
836 tgroups ntopology group)
838 (string-match prefix (setq group (car groups))))
839 (if (not (string-match "\\." group (match-end 0)))
840 ;; There are no further hierarchies here, so we just
841 ;; enter this group into the list belonging to this
843 (push (pop groups) tgroups)
844 ;; New sub-hierarchy, so we add it to the topology.
845 (nconc topology (list (setq ntopology
846 (list (list (substring
847 group 0 (match-end 0))
849 ;; Descend the hierarchy.
850 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
851 ;; We remove the trailing "." from the topic name.
853 (if (string-match "\\.$" name)
854 (substring name 0 (match-beginning 0))
856 ;; Add this topic and its groups to the topic alist.
857 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
858 (setcar (car topology) name)
859 ;; We return the rest of the groups that didn't belong
863 ;;; Topic mode, commands and keymap.
865 (defvar gnus-topic-mode-map nil)
866 (defvar gnus-group-topic-map nil)
868 (unless gnus-topic-mode-map
869 (setq gnus-topic-mode-map (make-sparse-keymap))
871 ;; Override certain group mode keys.
872 (gnus-define-keys gnus-topic-mode-map
873 "=" gnus-topic-select-group
874 "\r" gnus-topic-select-group
875 " " gnus-topic-read-group
876 "\C-k" gnus-topic-kill-group
877 "\C-y" gnus-topic-yank-group
878 "\M-g" gnus-topic-get-new-news-this-topic
879 "AT" gnus-topic-list-active
880 "Gp" gnus-topic-edit-parameters
881 "#" gnus-topic-mark-topic
882 "\M-#" gnus-topic-unmark-topic
883 gnus-mouse-2 gnus-mouse-pick-topic)
885 ;; Define a new submap.
886 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
887 "#" gnus-topic-mark-topic
888 "\M-#" gnus-topic-unmark-topic
889 "n" gnus-topic-create-topic
890 "m" gnus-topic-move-group
891 "D" gnus-topic-remove-group
892 "c" gnus-topic-copy-group
893 "h" gnus-topic-hide-topic
894 "s" gnus-topic-show-topic
895 "M" gnus-topic-move-matching
896 "C" gnus-topic-copy-matching
897 "\C-i" gnus-topic-indent
898 [tab] gnus-topic-indent
899 "r" gnus-topic-rename
900 "\177" gnus-topic-delete
901 [delete] gnus-topic-delete
902 "h" gnus-topic-toggle-display-empty-topics)
904 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
905 "s" gnus-topic-sort-groups
906 "a" gnus-topic-sort-groups-by-alphabet
907 "u" gnus-topic-sort-groups-by-unread
908 "l" gnus-topic-sort-groups-by-level
909 "v" gnus-topic-sort-groups-by-score
910 "r" gnus-topic-sort-groups-by-rank
911 "m" gnus-topic-sort-groups-by-method))
913 (defun gnus-topic-make-menu-bar ()
914 (unless (boundp 'gnus-topic-menu)
916 gnus-topic-menu gnus-topic-mode-map ""
918 ["Toggle topics" gnus-topic-mode t]
920 ["Copy" gnus-topic-copy-group t]
921 ["Move" gnus-topic-move-group t]
922 ["Remove" gnus-topic-remove-group t]
923 ["Copy matching" gnus-topic-copy-matching t]
924 ["Move matching" gnus-topic-move-matching t])
926 ["Show" gnus-topic-show-topic t]
927 ["Hide" gnus-topic-hide-topic t]
928 ["Delete" gnus-topic-delete t]
929 ["Rename" gnus-topic-rename t]
930 ["Create" gnus-topic-create-topic t]
931 ["Mark" gnus-topic-mark-topic t]
932 ["Indent" gnus-topic-indent t]
933 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
934 ["Edit parameters" gnus-topic-edit-parameters t])
935 ["List active" gnus-topic-list-active t]))))
937 (defun gnus-topic-mode (&optional arg redisplay)
938 "Minor mode for topicsifying Gnus group buffers."
939 (interactive (list current-prefix-arg t))
940 (when (eq major-mode 'gnus-group-mode)
941 (make-local-variable 'gnus-topic-mode)
942 (setq gnus-topic-mode
943 (if (null arg) (not gnus-topic-mode)
944 (> (prefix-numeric-value arg) 0)))
945 ;; Infest Gnus with topics.
946 (if (not gnus-topic-mode)
947 (setq gnus-goto-missing-group-function nil)
948 (when (gnus-visual-p 'topic-menu 'menu)
949 (gnus-topic-make-menu-bar))
950 (setq gnus-topic-line-format-spec
951 (gnus-parse-format gnus-topic-line-format
952 gnus-topic-line-format-alist t))
953 (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
954 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
955 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
956 (set (make-local-variable 'gnus-group-prepare-function)
957 'gnus-group-prepare-topics)
958 (set (make-local-variable 'gnus-group-get-parameter-function)
959 'gnus-group-topic-parameters)
960 (set (make-local-variable 'gnus-group-goto-next-group-function)
961 'gnus-topic-goto-next-group)
962 (set (make-local-variable 'gnus-group-indentation-function)
963 'gnus-topic-group-indentation)
964 (set (make-local-variable 'gnus-group-update-group-function)
965 'gnus-topic-update-topics-containing-group)
966 (set (make-local-variable 'gnus-group-sort-alist-function)
967 'gnus-group-sort-topic)
968 (setq gnus-group-change-level-function 'gnus-topic-change-level)
969 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
970 (make-local-hook 'gnus-check-bogus-groups-hook)
971 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
972 (setq gnus-topology-checked-p nil)
973 ;; We check the topology.
974 (when gnus-newsrc-alist
975 (gnus-topic-check-topology))
976 (run-hooks 'gnus-topic-mode-hook))
977 ;; Remove topic infestation.
978 (unless gnus-topic-mode
979 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
980 (remove-hook 'gnus-group-change-level-function
981 'gnus-topic-change-level)
982 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
983 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
984 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
986 (gnus-group-list-groups))))
988 (defun gnus-topic-select-group (&optional all)
989 "Select this newsgroup.
990 No article is selected automatically.
991 If ALL is non-nil, already read articles become readable.
992 If ALL is a number, fetch this number of articles.
994 If performed over a topic line, toggle folding the topic."
996 (if (gnus-group-topic-p)
997 (let ((gnus-group-list-mode
998 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
999 (gnus-topic-fold all))
1000 (gnus-group-select-group all)))
1002 (defun gnus-mouse-pick-topic (e)
1003 "Select the group or topic under the mouse pointer."
1006 (gnus-topic-read-group nil))
1008 (defun gnus-topic-read-group (&optional all no-article group)
1009 "Read news in this newsgroup.
1010 If the prefix argument ALL is non-nil, already read articles become
1011 readable. IF ALL is a number, fetch this number of articles. If the
1012 optional argument NO-ARTICLE is non-nil, no article will be
1013 auto-selected upon group entry. If GROUP is non-nil, fetch that
1016 If performed over a topic line, toggle folding the topic."
1018 (if (gnus-group-topic-p)
1019 (let ((gnus-group-list-mode
1020 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1021 (gnus-topic-fold all))
1022 (gnus-group-read-group all no-article group)))
1024 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1025 "Create a new TOPIC under PARENT.
1026 When used interactively, PARENT will be the topic under point."
1029 (read-string "New topic: ")
1030 (gnus-current-topic)))
1031 ;; Check whether this topic already exists.
1032 (when (gnus-topic-find-topology topic)
1033 (error "Topic already exists"))
1035 (setq parent (caar gnus-topic-topology)))
1036 (let ((top (cdr (gnus-topic-find-topology parent)))
1037 (full-topic (or full-topic `((,topic visible)))))
1039 (error "No such parent topic: %s" parent))
1042 (while (and (cdr top)
1043 (not (equal (caaadr top) previous)))
1044 (setq top (cdr top)))
1045 (setcdr top (cons full-topic (cdr top))))
1046 (nconc top (list full-topic)))
1047 (unless (assoc topic gnus-topic-alist)
1048 (push (list topic) gnus-topic-alist)))
1049 (gnus-topic-enter-dribble)
1050 (gnus-group-list-groups)
1051 (gnus-topic-goto-topic topic))
1053 (defun gnus-topic-move-group (n topic &optional copyp)
1054 "Move the next N groups to TOPIC.
1055 If COPYP, copy the groups instead."
1057 (list current-prefix-arg
1058 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1059 (let ((groups (gnus-group-process-prefix n))
1060 (topicl (assoc topic gnus-topic-alist))
1061 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1062 (start-topic (gnus-group-topic-name))
1066 (gnus-group-remove-mark g)
1068 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1070 (setcdr entry (gnus-delete-first g (cdr entry))))
1071 (nconc topicl (list g)))
1073 (gnus-topic-enter-dribble)
1075 (gnus-group-goto-group start-group)
1076 (gnus-topic-goto-topic start-topic))
1077 (gnus-group-list-groups)))
1079 (defun gnus-topic-remove-group (&optional arg)
1080 "Remove the current group from the topic."
1082 (gnus-group-iterate arg
1084 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1085 (buffer-read-only nil))
1086 (when (and topicl group)
1088 (gnus-delete-first group topicl))
1089 (gnus-topic-update-topic)
1090 (gnus-group-position-point)))))
1092 (defun gnus-topic-copy-group (n topic)
1093 "Copy the current group to a topic."
1095 (list current-prefix-arg
1096 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1097 (gnus-topic-move-group n topic t))
1099 (defun gnus-topic-kill-group (&optional n discard)
1100 "Kill the next N groups."
1102 (if (gnus-group-topic-p)
1103 (let ((topic (gnus-group-topic-name)))
1105 (gnus-topic-find-topology topic)
1106 (assoc topic gnus-topic-alist))
1107 gnus-topic-killed-topics)
1108 (gnus-topic-remove-topic nil t)
1109 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1110 (gnus-topic-enter-dribble))
1111 (gnus-group-kill-group n discard)
1112 (gnus-topic-update-topic)))
1114 (defun gnus-topic-yank-group (&optional arg)
1115 "Yank the last topic."
1117 (if gnus-topic-killed-topics
1119 (or (gnus-group-topic-name)
1120 (gnus-topic-next-topic (gnus-current-topic))))
1121 (data (pop gnus-topic-killed-topics))
1124 (push alist gnus-topic-alist)
1125 (gnus-topic-create-topic
1126 (caar item) (gnus-topic-parent-topic previous) previous
1128 (gnus-topic-enter-dribble)
1129 (gnus-topic-goto-topic (caar item)))
1130 (let* ((prev (gnus-group-group-name))
1131 (gnus-topic-inhibit-change-level t)
1132 (gnus-group-indentation
1134 (* gnus-topic-indent-level
1136 (gnus-topic-goto-topic (gnus-current-topic))
1137 (gnus-group-topic-level))
1141 ;; We first yank the groups the normal way...
1142 (setq yanked (gnus-group-yank-group arg))
1143 ;; Then we enter the yanked groups into the topics they belong
1145 (setq alist (assoc (save-excursion
1147 (gnus-current-topic))
1149 (when (stringp yanked)
1150 (setq yanked (list yanked)))
1152 (nconc alist yanked)
1153 (if (not (cdr alist))
1154 (setcdr alist (nconc yanked (cdr alist)))
1156 (when (equal (cadr alist) prev)
1157 (setcdr alist (nconc yanked (cdr alist)))
1159 (setq alist (cdr alist))))))
1160 (gnus-topic-update-topic)))
1162 (defun gnus-topic-hide-topic ()
1163 "Hide the current topic."
1165 (when (gnus-current-topic)
1166 (gnus-topic-goto-topic (gnus-current-topic))
1167 (gnus-topic-remove-topic nil nil 'hidden)))
1169 (defun gnus-topic-show-topic ()
1170 "Show the hidden topic."
1172 (when (gnus-group-topic-p)
1173 (gnus-topic-remove-topic t nil 'shown)))
1175 (defun gnus-topic-mark-topic (topic &optional unmark)
1176 "Mark all groups in the topic with the process mark."
1177 (interactive (list (gnus-group-topic-name)))
1179 (call-interactively 'gnus-group-mark-group)
1181 (let ((groups (gnus-topic-find-groups topic 9 t)))
1183 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1184 (gnus-info-group (nth 2 (pop groups)))))))))
1186 (defun gnus-topic-unmark-topic (topic &optional unmark)
1187 "Remove the process mark from all groups in the topic."
1188 (interactive (list (gnus-group-topic-name)))
1190 (call-interactively 'gnus-group-unmark-group)
1191 (gnus-topic-mark-topic topic t)))
1193 (defun gnus-topic-get-new-news-this-topic (&optional n)
1194 "Check for new news in the current topic."
1196 (if (not (gnus-group-topic-p))
1197 (gnus-group-get-new-news-this-group n)
1198 (gnus-topic-mark-topic (gnus-group-topic-name))
1199 (gnus-group-get-new-news-this-group)))
1201 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1202 "Move all groups that match REGEXP to some topic."
1207 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1208 (read-string (format "Move to %s (regexp): " topic))))))
1209 (gnus-group-mark-regexp regexp)
1210 (gnus-topic-move-group nil topic copyp))
1212 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1213 "Copy all groups that match REGEXP to some topic."
1218 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1219 (read-string (format "Copy to %s (regexp): " topic))))))
1220 (gnus-topic-move-matching regexp topic t))
1222 (defun gnus-topic-delete (topic)
1224 (interactive (list (gnus-group-topic-name)))
1226 (error "No topic to be deleted"))
1227 (let ((entry (assoc topic gnus-topic-alist))
1228 (buffer-read-only nil))
1230 (error "Topic not empty"))
1231 ;; Delete if visible.
1232 (when (gnus-topic-goto-topic topic)
1234 ;; Remove from alist.
1235 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1236 ;; Remove from topology.
1237 (gnus-topic-find-topology topic nil nil 'delete)
1238 (gnus-dribble-touch)))
1240 (defun gnus-topic-rename (old-name new-name)
1243 (let ((topic (gnus-current-topic)))
1245 (read-string (format "Rename %s to: " topic)))))
1246 (let ((top (gnus-topic-find-topology old-name))
1247 (entry (assoc old-name gnus-topic-alist)))
1249 (setcar (cadr top) new-name))
1251 (setcar entry new-name))
1253 (gnus-dribble-touch)
1254 (gnus-group-list-groups)))
1256 (defun gnus-topic-indent (&optional unindent)
1257 "Indent a topic -- make it a sub-topic of the previous topic.
1258 If UNINDENT, remove an indentation."
1261 (gnus-topic-unindent)
1262 (let* ((topic (gnus-current-topic))
1263 (parent (gnus-topic-previous-topic topic))
1264 (buffer-read-only nil))
1266 (error "Nothing to indent %s into" topic))
1268 (gnus-topic-goto-topic topic)
1269 (gnus-topic-kill-group)
1270 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1271 (gnus-topic-create-topic
1272 topic parent nil (cdaar gnus-topic-killed-topics))
1273 (pop gnus-topic-killed-topics)
1274 (or (gnus-topic-goto-topic topic)
1275 (gnus-topic-goto-topic parent))))))
1277 (defun gnus-topic-unindent ()
1280 (let* ((topic (gnus-current-topic))
1281 (parent (gnus-topic-parent-topic topic))
1282 (grandparent (gnus-topic-parent-topic parent)))
1284 (error "Nothing to indent %s into" topic))
1286 (gnus-topic-goto-topic topic)
1287 (gnus-topic-kill-group)
1288 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1289 (gnus-topic-create-topic
1290 topic grandparent (gnus-topic-next-topic parent)
1291 (cdaar gnus-topic-killed-topics))
1292 (pop gnus-topic-killed-topics)
1293 (gnus-topic-goto-topic topic))))
1295 (defun gnus-topic-list-active (&optional force)
1296 "List all groups that Gnus knows about in a topicsified fashion.
1297 If FORCE, always re-read the active file."
1300 (gnus-get-killed-groups))
1301 (gnus-topic-grok-active force)
1302 (let ((gnus-topic-topology gnus-topic-active-topology)
1303 (gnus-topic-alist gnus-topic-active-alist)
1304 gnus-killed-list gnus-zombie-list)
1305 (gnus-group-list-groups 9 nil 1)))
1307 (defun gnus-topic-toggle-display-empty-topics ()
1308 "Show/hide topics that have no unread articles."
1310 (setq gnus-topic-display-empty-topics
1311 (not gnus-topic-display-empty-topics))
1312 (gnus-group-list-groups)
1313 (message "%s empty topics"
1314 (if gnus-topic-display-empty-topics
1315 "Showing" "Hiding")))
1317 ;;; Topic sorting functions
1319 (defun gnus-topic-edit-parameters (group)
1320 "Edit the group parameters of GROUP.
1321 If performed on a topic, edit the topic parameters instead."
1322 (interactive (list (gnus-group-group-name)))
1324 (gnus-group-edit-group-parameters group)
1325 (if (not (gnus-group-topic-p))
1326 (error "Nothing to edit on the current line")
1327 (let ((topic (gnus-group-topic-name)))
1329 (gnus-topic-parameters topic)
1330 (format "Editing the topic parameters for `%s'."
1333 (gnus-topic-set-parameters ,topic form)))))))
1335 (defun gnus-group-sort-topic (func reverse)
1336 "Sort groups in the topics according to FUNC and REVERSE."
1337 (let ((alist gnus-topic-alist))
1339 ;; !!!Sometimes nil elements sneak into the alist,
1340 ;; for some reason or other.
1341 (setcar alist (delq nil (car alist)))
1342 (setcar alist (delete "dummy.group" (car alist)))
1343 (gnus-topic-sort-topic (pop alist) func reverse))))
1345 (defun gnus-topic-sort-topic (topic func reverse)
1346 ;; Each topic only lists the name of the group, while
1347 ;; the sort predicates expect group infos as inputs.
1348 ;; So we first transform the group names into infos,
1349 ;; then sort, and then transform back into group names.
1353 (lambda (info) (gnus-info-group info))
1356 (lambda (group) (gnus-get-info group))
1359 ;; Do the reversal, if necessary.
1361 (setcdr topic (nreverse (cdr topic)))))
1363 (defun gnus-topic-sort-groups (func &optional reverse)
1364 "Sort the current topic according to FUNC.
1365 If REVERSE, reverse the sorting order."
1366 (interactive (list gnus-group-sort-function current-prefix-arg))
1367 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1368 (gnus-topic-sort-topic
1369 topic (gnus-make-sort-function func) reverse)
1370 (gnus-group-list-groups)))
1372 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1373 "Sort the current topic alphabetically by group name.
1374 If REVERSE, sort in reverse order."
1376 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1378 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1379 "Sort the current topic by number of unread articles.
1380 If REVERSE, sort in reverse order."
1382 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1384 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1385 "Sort the current topic by group level.
1386 If REVERSE, sort in reverse order."
1388 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1390 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1391 "Sort the current topic by group score.
1392 If REVERSE, sort in reverse order."
1394 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1396 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1397 "Sort the current topic by group rank.
1398 If REVERSE, sort in reverse order."
1400 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1402 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1403 "Sort the current topic alphabetically by backend name.
1404 If REVERSE, sort in reverse order."
1406 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1408 (provide 'gnus-topic)
1410 ;;; gnus-topic.el ends here