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.
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)
81 (defconst gnus-topic-line-format-alist
85 (?g number-of-groups ?d)
86 (?a (gnus-topic-articles-in-topic entries) ?d)
87 (?A total-number-of-articles ?d)
90 (defvar gnus-topic-line-format-spec nil)
94 (defun gnus-group-topic-name ()
95 "The name of the topic on the current line."
96 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
97 (and topic (symbol-name topic))))
99 (defun gnus-group-topic-level ()
100 "The level of the topic on the current line."
101 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
103 (defun gnus-group-topic-unread ()
104 "The number of unread articles in topic on the current line."
105 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
107 (defun gnus-topic-unread (topic)
108 "Return the number of unread articles in TOPIC."
110 (and (gnus-topic-goto-topic topic)
111 (gnus-group-topic-unread)))
114 (defun gnus-group-topic-p ()
115 "Return non-nil if the current line is a topic."
116 (gnus-group-topic-name))
118 (defun gnus-topic-visible-p ()
119 "Return non-nil if the current topic is visible."
120 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
122 (defun gnus-topic-articles-in-topic (entries)
126 (when (numberp (setq number (car (pop entries))))
127 (incf total number)))
130 (defun gnus-group-topic (group)
131 "Return the topic GROUP is a member of."
132 (let ((alist gnus-topic-alist)
135 (when (member group (cdar alist))
136 (setq out (caar alist)
138 (setq alist (cdr alist)))
141 (defun gnus-group-parent-topic (group)
142 "Return the topic GROUP is member of by looking at the group buffer."
144 (set-buffer gnus-group-buffer)
145 (if (gnus-group-goto-group group)
147 (gnus-group-topic group))))
149 (defun gnus-topic-goto-topic (topic)
152 (gnus-goto-char (text-property-any (point-min) (point-max)
153 'gnus-topic (intern topic)))))
155 (defun gnus-current-topic ()
156 "Return the name of the current topic."
158 (or (get-text-property (point) 'gnus-topic)
160 (and (gnus-goto-char (previous-single-property-change
161 (point) 'gnus-topic))
162 (get-text-property (max (1- (point)) (point-min))
165 (symbol-name result))))
167 (defun gnus-current-topics ()
168 "Return a list of all current topics, lowest in hierarchy first."
169 (let ((topic (gnus-current-topic))
173 (setq topic (gnus-topic-parent-topic topic)))
176 (defun gnus-group-active-topic-p ()
177 "Say whether the current topic comes from the active topics."
180 (get-text-property (point) 'gnus-active)))
182 (defun gnus-topic-find-groups (topic &optional level all)
183 "Return entries for all visible groups in TOPIC."
184 (let ((groups (cdr (assoc topic gnus-topic-alist)))
185 info clevel unread group lowest params visible-groups entry active)
186 (setq lowest (or lowest 1))
187 (setq level (or level 7))
188 ;; We go through the newsrc to look for matches.
190 (when (setq group (pop groups))
191 (setq entry (gnus-gethash group 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
239 topic (car topology)))))
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-forward-topic (num)
257 "Go to the next topic on the same level as the current one."
258 (let* ((topic (gnus-current-topic))
259 (way (if (< num 0) 'gnus-topic-previous-topic
260 'gnus-topic-next-topic))
262 (while (and (not (zerop num))
263 (setq topic (funcall way topic)))
264 (when (gnus-topic-goto-topic topic)
267 (goto-char (point-max)))
270 (defun gnus-topic-find-topology (topic &optional topology level remove)
271 "Return the topology of TOPIC."
273 (setq topology gnus-topic-topology)
277 (if (equal (caar topology) topic)
280 (delq topology remove))
281 (cons level topology))
282 (setq topology (cdr topology))
284 (not (setq result (gnus-topic-find-topology
285 topic (car topology) (1+ level)
287 (setq topology (cdr topology)))
290 (defvar gnus-tmp-topics nil)
291 (defun gnus-topic-list (&optional topology)
292 "Return a list of all topics in the topology."
294 (setq topology gnus-topic-topology
295 gnus-tmp-topics nil))
296 (push (caar topology) gnus-tmp-topics)
297 (mapcar 'gnus-topic-list (cdr topology))
300 ;;; Topic parameter jazz
302 (defun gnus-topic-parameters (topic)
303 "Return the parameters for TOPIC."
304 (let ((top (gnus-topic-find-topology topic)))
306 (nth 3 (cadr top)))))
308 (defun gnus-topic-set-parameters (topic parameters)
309 "Set the topic parameters of TOPIC to PARAMETERS."
310 (let ((top (gnus-topic-find-topology topic)))
312 (error "No such topic: %s" topic))
313 ;; We may have to extend if there is no parameters here
315 (unless (nthcdr 2 (cadr top))
316 (nconc (cadr top) (list nil)))
317 (unless (nthcdr 3 (cadr top))
318 (nconc (cadr top) (list nil)))
319 (setcar (nthcdr 3 (cadr top)) parameters)
321 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
323 (defun gnus-group-topic-parameters (group)
324 "Compute the group parameters for GROUP taking into account inheritance from topics."
325 (let ((params-list (list (gnus-group-get-parameter group)))
326 topics params param out)
328 (gnus-group-goto-group group)
329 (setq topics (gnus-current-topics))
331 (push (gnus-topic-parameters (pop topics)) params-list))
332 ;; We probably have lots of nil elements here, so
333 ;; we remove them. Probably faster than doing this "properly".
334 (setq params-list (delq nil params-list))
335 ;; Now we have all the parameters, so we go through them
336 ;; and do inheritance in the obvious way.
337 (while (setq params (pop params-list))
338 (while (setq param (pop params))
340 (setq param (cons param t)))
341 ;; Override any old versions of this param.
342 (setq out (delq (assq (car param) out) out))
344 ;; Return the resulting parameter list.
347 ;;; General utility functions
349 (defun gnus-topic-enter-dribble ()
351 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
353 ;;; Generating group buffers
355 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
356 "List all newsgroups with unread articles of level LEVEL or lower, and
357 use the `gnus-group-topics' to sort the groups.
358 If ALL is non-nil, list groups that have no unread articles.
359 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
360 (set-buffer gnus-group-buffer)
361 (let ((buffer-read-only nil)
362 (lowest (or lowest 1)))
364 (when (or (not gnus-topic-alist)
365 (not gnus-topology-checked-p))
366 (gnus-topic-check-topology))
372 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
373 (gnus-group-prepare-flat-list-dead
374 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
378 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
379 (gnus-group-prepare-flat-list-dead
380 (setq gnus-killed-list (sort gnus-killed-list 'string<))
386 (when (< lowest gnus-level-zombie)
388 (let ((top (gnus-topic-find-topology list-topic)))
389 (gnus-topic-prepare-topic (cdr top) (car top)
390 (or topic-level level) all))
391 (gnus-topic-prepare-topic gnus-topic-topology 0
392 (or topic-level level) all)))
394 (gnus-group-set-mode-line)
395 (setq gnus-group-list-mode (cons level all))
396 (run-hooks 'gnus-group-prepare-hook))))
398 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
399 "Insert TOPIC into the group buffer.
400 If SILENT, don't insert anything. Return the number of unread
401 articles in the topic and its subtopics."
402 (let* ((type (pop topicl))
403 (entries (gnus-topic-find-groups (car type) list-level all))
404 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
405 (gnus-group-indentation
406 (make-string (* gnus-topic-indent-level level) ? ))
407 (beg (progn (beginning-of-line) (point)))
408 (topicl (reverse topicl))
409 (all-entries entries)
410 (point-max (point-max))
413 info entry end active tick)
414 ;; Insert any sub-topics.
417 (gnus-topic-prepare-topic
418 (pop topicl) (1+ level) list-level all
422 ;; Insert all the groups that belong in this topic.
423 (while (setq entry (pop entries))
427 (gnus-group-insert-group-line
428 entry (if (member entry gnus-zombie-list) 8 9)
429 nil (- (1+ (cdr (setq active (gnus-active entry))))
433 (when (setq info (nth 2 entry))
434 (gnus-group-insert-group-line
435 (gnus-info-group info)
436 (gnus-info-level info) (gnus-info-marks info)
437 (car entry) (gnus-info-method info)))))
438 (when (and (listp entry)
439 (numberp (car entry)))
440 (incf unread (car entry)))
444 ;; Insert the topic line.
445 (when (and (not silent)
446 (or gnus-topic-display-empty-topics ;We want empty topics
447 (not (zerop unread)) ;Non-empty
448 tick ;Ticked articles
449 (/= point-max (point-max)))) ;Unactivated groups
450 (gnus-extent-start-open (point))
451 (gnus-topic-insert-topic-line
453 (not (eq (nth 2 type) 'hidden))
454 level all-entries unread))
458 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
459 "Remove the current topic."
460 (let ((topic (gnus-group-topic-name))
461 (level (gnus-group-topic-level))
462 (beg (progn (beginning-of-line) (point)))
465 (while (and (zerop (forward-line 1))
466 (> (or (gnus-group-topic-level) (1+ level)) level)))
467 (delete-region beg (point))
468 ;; Do the change in this rather odd manner because it has been
469 ;; reported that some topics share parts of some lists, for some
470 ;; reason. I have been unable to determine why this is the
471 ;; case, but this hack seems to take care of things.
472 (let ((data (cadr (gnus-topic-find-topology topic))))
474 (list (if insert 'visible 'invisible)
478 (setq gnus-topic-alist
479 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
480 (gnus-topic-insert-topic topic in-level)))))
482 (defun gnus-topic-insert-topic (topic &optional level)
484 (gnus-group-prepare-topics
485 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
486 nil nil topic level))
488 (defun gnus-topic-fold (&optional insert)
489 "Remove/insert the current topic."
490 (let ((topic (gnus-group-topic-name)))
493 (if (not (gnus-group-active-topic-p))
494 (gnus-topic-remove-topic
495 (or insert (not (gnus-topic-visible-p))))
496 (let ((gnus-topic-topology gnus-topic-active-topology)
497 (gnus-topic-alist gnus-topic-active-alist)
498 (gnus-group-list-mode (cons 5 t)))
499 (gnus-topic-remove-topic
500 (or insert (not (gnus-topic-visible-p))) nil nil 9)
501 (gnus-topic-enter-dribble)))))))
503 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
505 (let* ((visible (if visiblep "" "..."))
506 (indentation (make-string (* gnus-topic-indent-level level) ? ))
507 (total-number-of-articles unread)
508 (number-of-groups (length entries))
509 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
512 (gnus-add-text-properties
515 (eval gnus-topic-line-format-spec)
516 (gnus-topic-remove-excess-properties)1)
517 (list 'gnus-topic (intern name)
518 'gnus-topic-level level
519 'gnus-topic-unread unread
520 'gnus-active active-topic
521 'gnus-topic-visible visiblep))))
523 (defun gnus-topic-update-topics-containing-group (group)
524 "Update all topics that have GROUP as a member."
525 (when (and (eq major-mode 'gnus-group-mode)
528 (let ((alist gnus-topic-alist))
529 ;; This is probably not entirely correct. If a topic
530 ;; isn't shown, then it's not updated. But the updating
531 ;; should be performed in any case, since the topic's
532 ;; parent should be updated. Pfft.
534 (when (and (member group (cdar alist))
535 (gnus-topic-goto-topic (caar alist)))
536 (gnus-topic-update-topic-line (caar alist)))
539 (defun gnus-topic-update-topic ()
540 "Update all parent topics to the current group."
541 (when (and (eq major-mode 'gnus-group-mode)
543 (let ((group (gnus-group-group-name))
545 (buffer-read-only nil))
547 (gnus-get-info group)
548 (gnus-topic-goto-topic (gnus-current-topic)))
549 (gnus-topic-update-topic-line (gnus-group-topic-name))
552 (gnus-group-position-point)))))
554 (defun gnus-topic-goto-missing-group (group)
555 "Place point where GROUP is supposed to be inserted."
556 (let* ((topic (gnus-group-topic group))
557 (groups (cdr (assoc topic gnus-topic-alist)))
558 (g (cdr (member group groups)))
560 ;; Try to jump to a visible group.
561 (while (and g (not (gnus-group-goto-group (car g) t)))
563 ;; It wasn't visible, so we try to see where to insert it.
565 (setq g (cdr (member group (reverse groups))))
566 (while (and g unfound)
567 (when (gnus-group-goto-group (pop g) t)
572 (not (gnus-topic-goto-missing-topic topic)))
573 (gnus-topic-insert-topic-line
574 topic t t (car (gnus-topic-find-topology topic)) nil 0)))))
576 (defun gnus-topic-goto-missing-topic (topic)
577 (if (gnus-topic-goto-topic topic)
579 ;; Topic not displayed.
580 (let* ((top (gnus-topic-find-topology
581 (gnus-topic-parent-topic topic)))
582 (tp (reverse (cddr top))))
583 (while (not (equal (caaar tp) topic))
587 (not (gnus-topic-goto-topic (caaar tp))))
590 (gnus-topic-forward-topic 1)
591 (gnus-topic-goto-missing-topic (caadr top))))
594 (defun gnus-topic-update-topic-line (topic-name &optional reads)
595 (let* ((top (gnus-topic-find-topology topic-name))
597 (children (cddr top))
598 (entries (gnus-topic-find-groups
599 (car type) (car gnus-group-list-mode)
600 (cdr gnus-group-list-mode)))
601 (parent (gnus-topic-parent-topic topic-name))
602 (all-entries entries)
605 (when (gnus-topic-goto-topic (car type))
606 ;; Tally all the groups that belong in this topic.
608 (setq unread (- (gnus-group-topic-unread) reads))
610 (incf unread (gnus-topic-unread (caar (pop children)))))
611 (while (setq entry (pop entries))
612 (when (numberp (car entry))
613 (incf unread (car entry)))))
614 (setq old-unread (gnus-group-topic-unread))
615 ;; Insert the topic line.
616 (gnus-topic-insert-topic-line
617 (car type) (gnus-topic-visible-p)
618 (not (eq (nth 2 type) 'hidden))
619 (gnus-group-topic-level) all-entries unread)
623 (gnus-topic-update-topic-line
624 parent (- old-unread (gnus-group-topic-unread))))
627 (defun gnus-topic-group-indentation ()
629 (* gnus-topic-indent-level
632 (gnus-topic-goto-topic (gnus-current-topic))
633 (gnus-group-topic-level))
639 (gnus-add-shutdown 'gnus-topic-close 'gnus)
641 (defun gnus-topic-close ()
642 (setq gnus-topic-active-topology nil
643 gnus-topic-active-alist nil
644 gnus-topic-killed-topics nil
645 gnus-topology-checked-p nil))
647 (defun gnus-topic-check-topology ()
648 ;; The first time we set the topology to whatever we have
649 ;; gotten here, which can be rather random.
650 (unless gnus-topic-alist
651 (gnus-topic-init-alist))
653 (setq gnus-topology-checked-p t)
654 ;; Go through the topic alist and make sure that all topics
655 ;; are in the topic topology.
656 (let ((topics (gnus-topic-list))
657 (alist gnus-topic-alist)
660 (unless (member (caar alist) topics)
661 (nconc gnus-topic-topology
662 (list (list (list (caar alist) 'visible))))
664 (setq alist (cdr alist)))
666 (gnus-topic-enter-dribble))
667 ;; Conversely, go through the topology and make sure that all
668 ;; topologies have alists.
670 (unless (assoc (car topics) gnus-topic-alist)
671 (push (list (car topics)) gnus-topic-alist))
673 ;; Go through all living groups and make sure that
674 ;; they belong to some topic.
675 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
677 (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
678 (newsrc (cdr gnus-newsrc-alist))
681 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
682 (setcdr entry (cons group (cdr entry))))))
683 ;; Go through all topics and make sure they contain only living groups.
684 (let ((alist gnus-topic-alist)
686 (while (setq topic (pop alist))
688 (if (gnus-gethash (cadr topic) gnus-newsrc-hashtb)
689 (setq topic (cdr topic))
690 (setcdr topic (cddr topic)))))))
692 (defun gnus-topic-init-alist ()
693 "Initialize the topic structures."
694 (setq gnus-topic-topology
695 (cons (list "Gnus" 'visible)
696 (mapcar (lambda (topic)
697 (list (list (car topic) 'visible)))
699 (setq gnus-topic-alist
701 (mapcar (lambda (info) (gnus-info-group info))
702 (cdr gnus-newsrc-alist)))
704 (gnus-topic-enter-dribble))
708 (defun gnus-topic-clean-alist ()
709 "Remove bogus groups from the topic alist."
710 (let ((topic-alist gnus-topic-alist)
712 (unless gnus-killed-hashtb
713 (gnus-make-hashtable-from-killed))
714 (while (setq topic (pop topic-alist))
715 (let ((topic-name (pop topic))
716 group filtered-topic)
717 (while (setq group (pop topic))
718 (when (and (or (gnus-gethash group gnus-active-hashtb)
719 (gnus-info-method (gnus-get-info group)))
720 (not (gnus-gethash group gnus-killed-hashtb)))
721 (push group filtered-topic)))
722 (push (cons topic-name (nreverse filtered-topic)) result)))
723 (setq gnus-topic-alist (nreverse result))))
725 (defun gnus-topic-change-level (group level oldlevel)
726 "Run when changing levels to enter/remove groups from topics."
728 (set-buffer gnus-group-buffer)
729 (when (and gnus-topic-mode
731 (not gnus-topic-inhibit-change-level))
732 ;; Remove the group from the topics.
733 (when (and (< oldlevel gnus-level-zombie)
734 (>= level gnus-level-zombie))
737 (when (setq alist (assoc (gnus-current-topic) gnus-topic-alist))
738 (setcdr alist (gnus-delete-first group (cdr alist))))))
739 ;; If the group is subscribed we enter it into the topics.
740 (when (and (< level gnus-level-zombie)
741 (>= oldlevel gnus-level-zombie))
742 (let* ((prev (gnus-group-group-name))
743 (gnus-topic-inhibit-change-level t)
744 (gnus-group-indentation
746 (* gnus-topic-indent-level
748 (gnus-topic-goto-topic (gnus-current-topic))
749 (gnus-group-topic-level))
752 (yanked (list group))
754 ;; Then we enter the yanked groups into the topics they belong
756 (when (setq alist (assoc (save-excursion
760 (caar gnus-topic-topology)))
763 (when (stringp yanked)
764 (setq yanked (list yanked)))
767 (if (not (cdr alist))
768 (setcdr alist (nconc yanked (cdr alist)))
769 (while (and (not end) (cdr alist))
770 (when (equal (cadr alist) prev)
771 (setcdr alist (nconc yanked (cdr alist)))
773 (setq alist (cdr alist)))
775 (nconc talist yanked))))))
776 (gnus-topic-update-topic)))))
778 (defun gnus-topic-goto-next-group (group props)
779 "Go to group or the next group after group."
781 (if (not (memq 'gnus-topic props))
782 (goto-char (point-max))
783 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
784 (if (gnus-group-goto-group group)
786 ;; The group is no longer visible.
787 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
788 (after (cdr (member group (cdr list)))))
789 ;; First try to put point on a group after the current one.
791 (not (gnus-group-goto-group (car after))))
792 (setq after (cdr after)))
793 ;; Then try to put point on a group before point.
795 (setq after (cdr (member group (reverse (cdr list)))))
797 (not (gnus-group-goto-group (car after))))
798 (setq after (cdr after))))
799 ;; Finally, just put point on the topic.
801 (goto-char (point-min))
803 (gnus-topic-goto-topic (car list))
807 ;;; Topic-active functions
809 (defun gnus-topic-grok-active (&optional force)
810 "Parse all active groups and create topic structures for them."
811 ;; First we make sure that we have really read the active file.
813 (not gnus-topic-active-alist))
815 ;; Get a list of all groups available.
816 (mapatoms (lambda (g) (when (symbol-value g)
817 (push (symbol-name g) groups)))
819 (setq groups (sort groups 'string<))
820 ;; Init the variables.
821 (setq gnus-topic-active-topology (list (list "" 'visible)))
822 (setq gnus-topic-active-alist nil)
823 ;; Descend the top-level hierarchy.
824 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
825 ;; Set the top-level topic names to something nice.
826 (setcar (car gnus-topic-active-topology) "Gnus active")
827 (setcar (car gnus-topic-active-alist) "Gnus active"))))
829 (defun gnus-topic-grok-active-1 (topology groups)
830 (let* ((name (caar topology))
831 (prefix (concat "^" (regexp-quote name)))
832 tgroups ntopology group)
834 (string-match prefix (setq group (car groups))))
835 (if (not (string-match "\\." group (match-end 0)))
836 ;; There are no further hierarchies here, so we just
837 ;; enter this group into the list belonging to this
839 (push (pop groups) tgroups)
840 ;; New sub-hierarchy, so we add it to the topology.
841 (nconc topology (list (setq ntopology
842 (list (list (substring
843 group 0 (match-end 0))
845 ;; Descend the hierarchy.
846 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
847 ;; We remove the trailing "." from the topic name.
849 (if (string-match "\\.$" name)
850 (substring name 0 (match-beginning 0))
852 ;; Add this topic and its groups to the topic alist.
853 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
854 (setcar (car topology) name)
855 ;; We return the rest of the groups that didn't belong
859 ;;; Topic mode, commands and keymap.
861 (defvar gnus-topic-mode-map nil)
862 (defvar gnus-group-topic-map nil)
864 (unless gnus-topic-mode-map
865 (setq gnus-topic-mode-map (make-sparse-keymap))
867 ;; Override certain group mode keys.
868 (gnus-define-keys gnus-topic-mode-map
869 "=" gnus-topic-select-group
870 "\r" gnus-topic-select-group
871 " " gnus-topic-read-group
872 "\C-k" gnus-topic-kill-group
873 "\C-y" gnus-topic-yank-group
874 "\M-g" gnus-topic-get-new-news-this-topic
875 "AT" gnus-topic-list-active
876 "Gp" gnus-topic-edit-parameters
877 "#" gnus-topic-mark-topic
878 "\M-#" gnus-topic-unmark-topic
879 gnus-mouse-2 gnus-mouse-pick-topic)
881 ;; Define a new submap.
882 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
883 "#" gnus-topic-mark-topic
884 "\M-#" gnus-topic-unmark-topic
885 "n" gnus-topic-create-topic
886 "m" gnus-topic-move-group
887 "D" gnus-topic-remove-group
888 "c" gnus-topic-copy-group
889 "h" gnus-topic-hide-topic
890 "s" gnus-topic-show-topic
891 "M" gnus-topic-move-matching
892 "C" gnus-topic-copy-matching
893 "\C-i" gnus-topic-indent
894 [tab] gnus-topic-indent
895 "r" gnus-topic-rename
896 "\177" gnus-topic-delete
897 [delete] gnus-topic-delete
898 "h" gnus-topic-toggle-display-empty-topics)
900 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
901 "s" gnus-topic-sort-groups
902 "a" gnus-topic-sort-groups-by-alphabet
903 "u" gnus-topic-sort-groups-by-unread
904 "l" gnus-topic-sort-groups-by-level
905 "v" gnus-topic-sort-groups-by-score
906 "r" gnus-topic-sort-groups-by-rank
907 "m" gnus-topic-sort-groups-by-method))
909 (defun gnus-topic-make-menu-bar ()
910 (unless (boundp 'gnus-topic-menu)
912 gnus-topic-menu gnus-topic-mode-map ""
914 ["Toggle topics" gnus-topic-mode t]
916 ["Copy" gnus-topic-copy-group t]
917 ["Move" gnus-topic-move-group t]
918 ["Remove" gnus-topic-remove-group t]
919 ["Copy matching" gnus-topic-copy-matching t]
920 ["Move matching" gnus-topic-move-matching t])
922 ["Show" gnus-topic-show-topic t]
923 ["Hide" gnus-topic-hide-topic t]
924 ["Delete" gnus-topic-delete t]
925 ["Rename" gnus-topic-rename t]
926 ["Create" gnus-topic-create-topic t]
927 ["Mark" gnus-topic-mark-topic t]
928 ["Indent" gnus-topic-indent t]
929 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t])
930 ["List active" gnus-topic-list-active t]))))
932 (defun gnus-topic-mode (&optional arg redisplay)
933 "Minor mode for topicsifying Gnus group buffers."
934 (interactive (list current-prefix-arg t))
935 (when (eq major-mode 'gnus-group-mode)
936 (make-local-variable 'gnus-topic-mode)
937 (setq gnus-topic-mode
938 (if (null arg) (not gnus-topic-mode)
939 (> (prefix-numeric-value arg) 0)))
940 ;; Infest Gnus with topics.
941 (if (not gnus-topic-mode)
942 (setq gnus-goto-missing-group-function nil)
943 (when (gnus-visual-p 'topic-menu 'menu)
944 (gnus-topic-make-menu-bar))
945 (setq gnus-topic-line-format-spec
946 (gnus-parse-format gnus-topic-line-format
947 gnus-topic-line-format-alist t))
948 (unless (assq 'gnus-topic-mode minor-mode-alist)
949 (push '(gnus-topic-mode " Topic") minor-mode-alist))
950 (unless (assq 'gnus-topic-mode minor-mode-map-alist)
951 (push (cons 'gnus-topic-mode gnus-topic-mode-map)
952 minor-mode-map-alist))
953 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
954 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
955 (set (make-local-variable 'gnus-group-prepare-function)
956 'gnus-group-prepare-topics)
957 (set (make-local-variable 'gnus-group-get-parameter-function)
958 'gnus-group-topic-parameters)
959 (set (make-local-variable 'gnus-group-goto-next-group-function)
960 'gnus-topic-goto-next-group)
961 (set (make-local-variable 'gnus-group-indentation-function)
962 'gnus-topic-group-indentation)
963 (set (make-local-variable 'gnus-group-update-group-function)
964 'gnus-topic-update-topics-containing-group)
965 (set (make-local-variable 'gnus-group-sort-alist-function)
966 'gnus-group-sort-topic)
967 (setq gnus-group-change-level-function 'gnus-topic-change-level)
968 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
969 (make-local-hook 'gnus-check-bogus-groups-hook)
970 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
971 (setq gnus-topology-checked-p nil)
972 ;; We check the topology.
973 (when gnus-newsrc-alist
974 (gnus-topic-check-topology))
975 (run-hooks 'gnus-topic-mode-hook))
976 ;; Remove topic infestation.
977 (unless gnus-topic-mode
978 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
979 (remove-hook 'gnus-group-change-level-function
980 'gnus-topic-change-level)
981 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
982 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
983 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
985 (gnus-group-list-groups))))
987 (defun gnus-topic-select-group (&optional all)
988 "Select this newsgroup.
989 No article is selected automatically.
990 If ALL is non-nil, already read articles become readable.
991 If ALL is a number, fetch this number of articles.
993 If performed over a topic line, toggle folding the topic."
995 (if (gnus-group-topic-p)
996 (let ((gnus-group-list-mode
997 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
998 (gnus-topic-fold all))
999 (gnus-group-select-group all)))
1001 (defun gnus-mouse-pick-topic (e)
1002 "Select the group or topic under the mouse pointer."
1005 (gnus-topic-read-group nil))
1007 (defun gnus-topic-read-group (&optional all no-article group)
1008 "Read news in this newsgroup.
1009 If the prefix argument ALL is non-nil, already read articles become
1010 readable. IF ALL is a number, fetch this number of articles. If the
1011 optional argument NO-ARTICLE is non-nil, no article will be
1012 auto-selected upon group entry. If GROUP is non-nil, fetch that
1015 If performed over a topic line, toggle folding the topic."
1017 (if (gnus-group-topic-p)
1018 (let ((gnus-group-list-mode
1019 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1020 (gnus-topic-fold all))
1021 (gnus-group-read-group all no-article group)))
1023 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1024 "Create a new TOPIC under PARENT.
1025 When used interactively, PARENT will be the topic under point."
1028 (read-string "New topic: ")
1029 (gnus-current-topic)))
1030 ;; Check whether this topic already exists.
1031 (when (gnus-topic-find-topology topic)
1032 (error "Topic already exists"))
1034 (setq parent (caar gnus-topic-topology)))
1035 (let ((top (cdr (gnus-topic-find-topology parent)))
1036 (full-topic (or full-topic `((,topic visible)))))
1038 (error "No such parent topic: %s" parent))
1041 (while (and (cdr top)
1042 (not (equal (caaadr top) previous)))
1043 (setq top (cdr top)))
1044 (setcdr top (cons full-topic (cdr top))))
1045 (nconc top (list full-topic)))
1046 (unless (assoc topic gnus-topic-alist)
1047 (push (list topic) gnus-topic-alist)))
1048 (gnus-topic-enter-dribble)
1049 (gnus-group-list-groups)
1050 (gnus-topic-goto-topic topic))
1052 (defun gnus-topic-move-group (n topic &optional copyp)
1053 "Move the next N groups to TOPIC.
1054 If COPYP, copy the groups instead."
1056 (list current-prefix-arg
1057 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1058 (let ((groups (gnus-group-process-prefix n))
1059 (topicl (assoc topic gnus-topic-alist))
1060 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1061 (start-topic (gnus-group-topic-name))
1065 (gnus-group-remove-mark g)
1067 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1069 (setcdr entry (gnus-delete-first g (cdr entry))))
1070 (nconc topicl (list g)))
1072 (gnus-topic-enter-dribble)
1074 (gnus-group-goto-group start-group)
1075 (gnus-topic-goto-topic start-topic))
1076 (gnus-group-list-groups)))
1078 (defun gnus-topic-remove-group (&optional arg)
1079 "Remove the current group from the topic."
1081 (gnus-group-iterate arg
1083 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1084 (buffer-read-only nil))
1085 (when (and topicl group)
1087 (gnus-delete-first group topicl))
1088 (gnus-topic-update-topic)
1089 (gnus-group-position-point)))))
1091 (defun gnus-topic-copy-group (n topic)
1092 "Copy the current group to a topic."
1094 (list current-prefix-arg
1095 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1096 (gnus-topic-move-group n topic t))
1098 (defun gnus-topic-kill-group (&optional n discard)
1099 "Kill the next N groups."
1101 (if (gnus-group-topic-p)
1102 (let ((topic (gnus-group-topic-name)))
1104 (gnus-topic-find-topology topic)
1105 (assoc topic gnus-topic-alist))
1106 gnus-topic-killed-topics)
1107 (gnus-topic-remove-topic nil t)
1108 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1109 (gnus-topic-enter-dribble))
1110 (gnus-group-kill-group n discard)
1111 (gnus-topic-update-topic)))
1113 (defun gnus-topic-yank-group (&optional arg)
1114 "Yank the last topic."
1116 (if gnus-topic-killed-topics
1118 (or (gnus-group-topic-name)
1119 (gnus-topic-next-topic (gnus-current-topic))))
1120 (data (pop gnus-topic-killed-topics))
1123 (push alist gnus-topic-alist)
1124 (gnus-topic-create-topic
1125 (caar item) (gnus-topic-parent-topic previous) previous
1127 (gnus-topic-enter-dribble)
1128 (gnus-topic-goto-topic (caar item)))
1129 (let* ((prev (gnus-group-group-name))
1130 (gnus-topic-inhibit-change-level t)
1131 (gnus-group-indentation
1133 (* gnus-topic-indent-level
1135 (gnus-topic-goto-topic (gnus-current-topic))
1136 (gnus-group-topic-level))
1140 ;; We first yank the groups the normal way...
1141 (setq yanked (gnus-group-yank-group arg))
1142 ;; Then we enter the yanked groups into the topics they belong
1144 (setq alist (assoc (save-excursion
1146 (gnus-current-topic))
1148 (when (stringp yanked)
1149 (setq yanked (list yanked)))
1151 (nconc alist yanked)
1152 (if (not (cdr alist))
1153 (setcdr alist (nconc yanked (cdr alist)))
1155 (when (equal (cadr alist) prev)
1156 (setcdr alist (nconc yanked (cdr alist)))
1158 (setq alist (cdr alist))))))
1159 (gnus-topic-update-topic)))
1161 (defun gnus-topic-hide-topic ()
1162 "Hide the current topic."
1164 (when (gnus-current-topic)
1165 (gnus-topic-goto-topic (gnus-current-topic))
1166 (gnus-topic-remove-topic nil nil 'hidden)))
1168 (defun gnus-topic-show-topic ()
1169 "Show the hidden topic."
1171 (when (gnus-group-topic-p)
1172 (gnus-topic-remove-topic t nil 'shown)))
1174 (defun gnus-topic-mark-topic (topic &optional unmark)
1175 "Mark all groups in the topic with the process mark."
1176 (interactive (list (gnus-group-topic-name)))
1178 (call-interactively 'gnus-group-mark-group)
1180 (let ((groups (gnus-topic-find-groups topic 9 t)))
1182 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1183 (gnus-info-group (nth 2 (pop groups)))))))))
1185 (defun gnus-topic-unmark-topic (topic &optional unmark)
1186 "Remove the process mark from all groups in the topic."
1187 (interactive (list (gnus-group-topic-name)))
1189 (call-interactively 'gnus-group-unmark-group)
1190 (gnus-topic-mark-topic topic t)))
1192 (defun gnus-topic-get-new-news-this-topic (&optional n)
1193 "Check for new news in the current topic."
1195 (if (not (gnus-group-topic-p))
1196 (gnus-group-get-new-news-this-group n)
1197 (gnus-topic-mark-topic (gnus-group-topic-name))
1198 (gnus-group-get-new-news-this-group)))
1200 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1201 "Move all groups that match REGEXP to some topic."
1206 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1207 (read-string (format "Move to %s (regexp): " topic))))))
1208 (gnus-group-mark-regexp regexp)
1209 (gnus-topic-move-group nil topic copyp))
1211 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1212 "Copy all groups that match REGEXP to some topic."
1217 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1218 (read-string (format "Copy to %s (regexp): " topic))))))
1219 (gnus-topic-move-matching regexp topic t))
1221 (defun gnus-topic-delete (topic)
1223 (interactive (list (gnus-group-topic-name)))
1225 (error "No topic to be deleted"))
1226 (let ((entry (assoc topic gnus-topic-alist))
1227 (buffer-read-only nil))
1229 (error "Topic not empty"))
1230 ;; Delete if visible.
1231 (when (gnus-topic-goto-topic topic)
1233 ;; Remove from alist.
1234 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1235 ;; Remove from topology.
1236 (gnus-topic-find-topology topic nil nil 'delete)))
1238 (defun gnus-topic-rename (old-name new-name)
1241 (let ((topic (gnus-current-topic)))
1243 (read-string (format "Rename %s to: " topic)))))
1244 (let ((top (gnus-topic-find-topology old-name))
1245 (entry (assoc old-name gnus-topic-alist)))
1247 (setcar (cadr top) new-name))
1249 (setcar entry new-name))
1251 (gnus-dribble-touch)
1252 (gnus-group-list-groups)))
1254 (defun gnus-topic-indent (&optional unindent)
1255 "Indent a topic -- make it a sub-topic of the previous topic.
1256 If UNINDENT, remove an indentation."
1259 (gnus-topic-unindent)
1260 (let* ((topic (gnus-current-topic))
1261 (parent (gnus-topic-previous-topic topic))
1262 (buffer-read-only nil))
1264 (error "Nothing to indent %s into" topic))
1266 (gnus-topic-goto-topic topic)
1267 (gnus-topic-kill-group)
1268 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1269 (gnus-topic-create-topic
1270 topic parent nil (cdaar gnus-topic-killed-topics))
1271 (pop gnus-topic-killed-topics)
1272 (or (gnus-topic-goto-topic topic)
1273 (gnus-topic-goto-topic parent))))))
1275 (defun gnus-topic-unindent ()
1278 (let* ((topic (gnus-current-topic))
1279 (parent (gnus-topic-parent-topic topic))
1280 (grandparent (gnus-topic-parent-topic parent)))
1282 (error "Nothing to indent %s into" topic))
1284 (gnus-topic-goto-topic topic)
1285 (gnus-topic-kill-group)
1286 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1287 (gnus-topic-create-topic
1288 topic grandparent (gnus-topic-next-topic parent)
1289 (cdaar gnus-topic-killed-topics))
1290 (pop gnus-topic-killed-topics)
1291 (gnus-topic-goto-topic topic))))
1293 (defun gnus-topic-list-active (&optional force)
1294 "List all groups that Gnus knows about in a topicsified fashion.
1295 If FORCE, always re-read the active file."
1298 (gnus-get-killed-groups))
1299 (gnus-topic-grok-active force)
1300 (let ((gnus-topic-topology gnus-topic-active-topology)
1301 (gnus-topic-alist gnus-topic-active-alist)
1302 gnus-killed-list gnus-zombie-list)
1303 (gnus-group-list-groups 9 nil 1)))
1305 (defun gnus-topic-toggle-display-empty-topics ()
1306 "Show/hide topics that have no unread articles."
1308 (setq gnus-topic-display-empty-topics
1309 (not gnus-topic-display-empty-topics))
1310 (message "%s empty topics"
1311 (if gnus-topic-display-empty-topics
1312 "Showing" "Hiding")))
1314 ;;; Topic sorting functions
1316 (defun gnus-topic-edit-parameters (group)
1317 "Edit the group parameters of GROUP.
1318 If performed on a topic, edit the topic parameters instead."
1319 (interactive (list (gnus-group-group-name)))
1321 (gnus-group-edit-group-parameters group)
1322 (if (not (gnus-group-topic-p))
1323 (error "Nothing to edit on the current line.")
1324 (let ((topic (gnus-group-topic-name)))
1326 (gnus-topic-parameters topic)
1327 (format "Editing the topic parameters for `%s'."
1330 (gnus-topic-set-parameters ,topic form)))))))
1332 (defun gnus-group-sort-topic (func reverse)
1333 "Sort groups in the topics according to FUNC and REVERSE."
1334 (let ((alist gnus-topic-alist))
1336 ;; !!!Sometimes nil elements sneak into the alist,
1337 ;; for some reason or other.
1338 (setcar alist (delq nil (car alist)))
1339 (setcar alist (delete "dummy.group" (car alist)))
1340 (gnus-topic-sort-topic (pop alist) func reverse))))
1342 (defun gnus-topic-sort-topic (topic func reverse)
1343 ;; Each topic only lists the name of the group, while
1344 ;; the sort predicates expect group infos as inputs.
1345 ;; So we first transform the group names into infos,
1346 ;; then sort, and then transform back into group names.
1350 (lambda (info) (gnus-info-group info))
1353 (lambda (group) (gnus-get-info group))
1356 ;; Do the reversal, if necessary.
1358 (setcdr topic (nreverse (cdr topic)))))
1360 (defun gnus-topic-sort-groups (func &optional reverse)
1361 "Sort the current topic according to FUNC.
1362 If REVERSE, reverse the sorting order."
1363 (interactive (list gnus-group-sort-function current-prefix-arg))
1364 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1365 (gnus-topic-sort-topic
1366 topic (gnus-make-sort-function func) reverse)
1367 (gnus-group-list-groups)))
1369 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1370 "Sort the current topic alphabetically by group name.
1371 If REVERSE, sort in reverse order."
1373 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1375 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1376 "Sort the current topic by number of unread articles.
1377 If REVERSE, sort in reverse order."
1379 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1381 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1382 "Sort the current topic by group level.
1383 If REVERSE, sort in reverse order."
1385 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1387 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1388 "Sort the current topic by group score.
1389 If REVERSE, sort in reverse order."
1391 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1393 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1394 "Sort the current topic by group rank.
1395 If REVERSE, sort in reverse order."
1397 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1399 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1400 "Sort the current topic alphabetically by backend name.
1401 If REVERSE, sort in reverse order."
1403 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1405 (provide 'gnus-topic)
1407 ;;; gnus-topic.el ends here