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)
80 (defvar gnus-topic-tallied-groups nil)
82 (defconst gnus-topic-line-format-alist
86 (?g number-of-groups ?d)
87 (?a (gnus-topic-articles-in-topic entries) ?d)
88 (?A total-number-of-articles ?d)
91 (defvar gnus-topic-line-format-spec nil)
95 (defun gnus-group-topic-name ()
96 "The name of the topic on the current line."
97 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
98 (and topic (symbol-name topic))))
100 (defun gnus-group-topic-level ()
101 "The level of the topic on the current line."
102 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
104 (defun gnus-group-topic-unread ()
105 "The number of unread articles in topic on the current line."
106 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
108 (defun gnus-topic-unread (topic)
109 "Return the number of unread articles in TOPIC."
111 (and (gnus-topic-goto-topic topic)
112 (gnus-group-topic-unread)))
115 (defun gnus-group-topic-p ()
116 "Return non-nil if the current line is a topic."
117 (gnus-group-topic-name))
119 (defun gnus-topic-visible-p ()
120 "Return non-nil if the current topic is visible."
121 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
123 (defun gnus-topic-articles-in-topic (entries)
127 (when (numberp (setq number (car (pop entries))))
128 (incf total number)))
131 (defun gnus-group-topic (group)
132 "Return the topic GROUP is a member of."
133 (let ((alist gnus-topic-alist)
136 (when (member group (cdar alist))
137 (setq out (caar alist)
139 (setq alist (cdr alist)))
142 (defun gnus-group-parent-topic (group)
143 "Return the topic GROUP is member of by looking at the group buffer."
145 (set-buffer gnus-group-buffer)
146 (if (gnus-group-goto-group group)
148 (gnus-group-topic group))))
150 (defun gnus-topic-goto-topic (topic)
153 (gnus-goto-char (text-property-any (point-min) (point-max)
154 'gnus-topic (intern topic)))))
156 (defun gnus-current-topic ()
157 "Return the name of the current topic."
159 (or (get-text-property (point) 'gnus-topic)
161 (and (gnus-goto-char (previous-single-property-change
162 (point) 'gnus-topic))
163 (get-text-property (max (1- (point)) (point-min))
166 (symbol-name result))))
168 (defun gnus-current-topics ()
169 "Return a list of all current topics, lowest in hierarchy first."
170 (let ((topic (gnus-current-topic))
174 (setq topic (gnus-topic-parent-topic topic)))
177 (defun gnus-group-active-topic-p ()
178 "Say whether the current topic comes from the active topics."
181 (get-text-property (point) 'gnus-active)))
183 (defun gnus-topic-find-groups (topic &optional level all)
184 "Return entries for all visible groups in TOPIC."
185 (let ((groups (cdr (assoc topic gnus-topic-alist)))
186 info clevel unread group lowest params visible-groups entry active)
187 (setq lowest (or lowest 1))
188 (setq level (or level 7))
189 ;; We go through the newsrc to look for matches.
191 (when (setq group (pop groups))
192 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
194 params (gnus-info-params info)
195 active (gnus-active group)
196 unread (or (car entry)
197 (and (not (equal group "dummy.group"))
199 (- (1+ (cdr active)) (car active))))
200 clevel (or (gnus-info-level info)
201 (if (member group gnus-zombie-list) 8 9))))
203 unread ; nil means that the group is dead.
205 (>= clevel lowest) ; Is inside the level we want.
208 gnus-group-list-inactive-groups
210 (and gnus-list-groups-with-ticked-articles
211 (cdr (assq 'tick (gnus-info-marks info))))
212 ; Has right readedness.
213 ;; Check for permanent visibility.
214 (and gnus-permanently-visible-groups
215 (string-match gnus-permanently-visible-groups group))
216 (memq 'visible params)
217 (cdr (assq 'visible params)))
218 ;; Add this group to the list of visible groups.
219 (push (or entry group) visible-groups)))
220 (nreverse visible-groups)))
222 (defun gnus-topic-previous-topic (topic)
223 "Return the previous topic on the same level as TOPIC."
224 (let ((top (cddr (gnus-topic-find-topology
225 (gnus-topic-parent-topic topic)))))
226 (unless (equal topic (caaar top))
227 (while (and top (not (equal (caaadr top) topic)))
228 (setq top (cdr top)))
231 (defun gnus-topic-parent-topic (topic &optional topology)
232 "Return the parent of TOPIC."
234 (setq topology gnus-topic-topology))
235 (let ((parent (car (pop topology)))
238 (not (setq found (equal (caaar topology) topic)))
239 (not (setq result (gnus-topic-parent-topic topic
241 (setq topology (cdr topology)))
242 (or result (and found parent))))
244 (defun gnus-topic-next-topic (topic &optional previous)
245 "Return the next sibling of TOPIC."
246 (let ((parentt (cddr (gnus-topic-find-topology
247 (gnus-topic-parent-topic topic))))
250 (not (equal (caaar parentt) topic)))
251 (setq prev (caaar parentt)
252 parentt (cdr parentt)))
257 (defun gnus-topic-find-topology (topic &optional topology level remove)
258 "Return the topology of TOPIC."
260 (setq topology gnus-topic-topology)
264 (if (equal (caar topology) topic)
267 (delq topology remove))
268 (cons level topology))
269 (setq topology (cdr topology))
271 (not (setq result (gnus-topic-find-topology
272 topic (car topology) (1+ level)
274 (setq topology (cdr topology)))
277 (defvar gnus-tmp-topics nil)
278 (defun gnus-topic-list (&optional topology)
279 "Return a list of all topics in the topology."
281 (setq topology gnus-topic-topology
282 gnus-tmp-topics nil))
283 (push (caar topology) gnus-tmp-topics)
284 (mapcar 'gnus-topic-list (cdr topology))
287 ;;; Topic parameter jazz
289 (defun gnus-topic-parameters (topic)
290 "Return the parameters for TOPIC."
291 (let ((top (gnus-topic-find-topology topic)))
293 (nth 3 (cadr top)))))
295 (defun gnus-topic-set-parameters (topic parameters)
296 "Set the topic parameters of TOPIC to PARAMETERS."
297 (let ((top (gnus-topic-find-topology topic)))
299 (error "No such topic: %s" topic))
300 ;; We may have to extend if there is no parameters here
302 (unless (nthcdr 2 (cadr top))
303 (nconc (cadr top) (list nil)))
304 (unless (nthcdr 3 (cadr top))
305 (nconc (cadr top) (list nil)))
306 (setcar (nthcdr 3 (cadr top)) parameters)
308 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
310 (defun gnus-group-topic-parameters (group)
311 "Compute the group parameters for GROUP taking into account inheritance from topics."
312 (let ((params-list (list (gnus-group-get-parameter group)))
313 topics params param out)
315 (gnus-group-goto-group group)
316 (setq topics (gnus-current-topics))
318 (push (gnus-topic-parameters (pop topics)) params-list))
319 ;; We probably have lots of nil elements here, so
320 ;; we remove them. Probably faster than doing this "properly".
321 (setq params-list (delq nil params-list))
322 ;; Now we have all the parameters, so we go through them
323 ;; and do inheritance in the obvious way.
324 (while (setq params (pop params-list))
325 (while (setq param (pop params))
327 (setq param (cons param t)))
328 ;; Override any old versions of this param.
329 (setq out (delq (assq (car param) out) out))
331 ;; Return the resulting parameter list.
334 ;;; General utility functions
336 (defun gnus-topic-enter-dribble ()
338 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
340 ;;; Generating group buffers
342 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
343 "List all newsgroups with unread articles of level LEVEL or lower, and
344 use the `gnus-group-topics' to sort the groups.
345 If ALL is non-nil, list groups that have no unread articles.
346 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
347 (set-buffer gnus-group-buffer)
348 (let ((buffer-read-only nil)
349 (lowest (or lowest 1)))
351 (setq gnus-topic-tallied-groups nil)
353 (when (or (not gnus-topic-alist)
354 (not gnus-topology-checked-p))
355 (gnus-topic-check-topology))
361 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
362 (gnus-group-prepare-flat-list-dead
363 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
367 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
368 (gnus-group-prepare-flat-list-dead
369 (setq gnus-killed-list (sort gnus-killed-list 'string<))
375 (when (< lowest gnus-level-zombie)
377 (let ((top (gnus-topic-find-topology list-topic)))
378 (gnus-topic-prepare-topic (cdr top) (car top)
379 (or topic-level level) all))
380 (gnus-topic-prepare-topic gnus-topic-topology 0
381 (or topic-level level) all)))
383 (gnus-group-set-mode-line)
384 (setq gnus-group-list-mode (cons level all))
385 (run-hooks 'gnus-group-prepare-hook))))
387 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
388 "Insert TOPIC into the group buffer.
389 If SILENT, don't insert anything. Return the number of unread
390 articles in the topic and its subtopics."
391 (let* ((type (pop topicl))
392 (entries (gnus-topic-find-groups (car type) list-level all))
393 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
394 (gnus-group-indentation
395 (make-string (* gnus-topic-indent-level level) ? ))
396 (beg (progn (beginning-of-line) (point)))
397 (topicl (reverse topicl))
398 (all-entries entries)
399 (point-max (point-max))
402 info entry end active tick)
403 ;; Insert any sub-topics.
406 (gnus-topic-prepare-topic
407 (pop topicl) (1+ level) list-level all
411 ;; Insert all the groups that belong in this topic.
412 (while (setq entry (pop entries))
416 (gnus-group-insert-group-line
417 entry (if (member entry gnus-zombie-list) 8 9)
418 nil (- (1+ (cdr (setq active (gnus-active entry))))
422 (when (setq info (nth 2 entry))
423 (gnus-group-insert-group-line
424 (gnus-info-group info)
425 (gnus-info-level info) (gnus-info-marks info)
426 (car entry) (gnus-info-method info)))))
427 (when (and (listp entry)
428 (numberp (car entry))
429 (not (member (gnus-info-group (setq info (nth 2 entry)))
430 gnus-topic-tallied-groups)))
431 (push (gnus-info-group info) gnus-topic-tallied-groups)
432 (incf unread (car entry)))
436 ;; Insert the topic line.
437 (when (and (not silent)
438 (or gnus-topic-display-empty-topics ;We want empty topics
439 (not (zerop unread)) ;Non-empty
440 tick ;Ticked articles
441 (/= point-max (point-max)))) ;Unactivated groups
442 (gnus-extent-start-open (point))
443 (gnus-topic-insert-topic-line
445 (not (eq (nth 2 type) 'hidden))
446 level all-entries unread))
450 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
451 "Remove the current topic."
452 (let ((topic (gnus-group-topic-name))
453 (level (gnus-group-topic-level))
454 (beg (progn (beginning-of-line) (point)))
457 (while (and (zerop (forward-line 1))
458 (> (or (gnus-group-topic-level) (1+ level)) level)))
459 (delete-region beg (point))
460 ;; Do the change in this rather odd manner because it has been
461 ;; reported that some topics share parts of some lists, for some
462 ;; reason. I have been unable to determine why this is the
463 ;; case, but this hack seems to take care of things.
464 (let ((data (cadr (gnus-topic-find-topology topic))))
466 (list (if insert 'visible 'invisible)
470 (setq gnus-topic-alist
471 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
472 (gnus-topic-insert-topic topic in-level)))))
474 (defun gnus-topic-insert-topic (topic &optional level)
476 (gnus-group-prepare-topics
477 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
478 nil nil topic level))
480 (defun gnus-topic-fold (&optional insert)
481 "Remove/insert the current topic."
482 (let ((topic (gnus-group-topic-name)))
485 (if (not (gnus-group-active-topic-p))
486 (gnus-topic-remove-topic
487 (or insert (not (gnus-topic-visible-p))))
488 (let ((gnus-topic-topology gnus-topic-active-topology)
489 (gnus-topic-alist gnus-topic-active-alist)
490 (gnus-group-list-mode (cons 5 t)))
491 (gnus-topic-remove-topic
492 (or insert (not (gnus-topic-visible-p))) nil nil 9)
493 (gnus-topic-enter-dribble)))))))
495 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
497 (let* ((visible (if visiblep "" "..."))
498 (indentation (make-string (* gnus-topic-indent-level level) ? ))
499 (total-number-of-articles unread)
500 (number-of-groups (length entries))
501 (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
504 (gnus-add-text-properties
507 (eval gnus-topic-line-format-spec)
508 (gnus-topic-remove-excess-properties)1)
509 (list 'gnus-topic (intern name)
510 'gnus-topic-level level
511 'gnus-topic-unread unread
512 'gnus-active active-topic
513 'gnus-topic-visible visiblep))))
515 (defun gnus-topic-update-topics-containing-group (group)
516 "Update all topics that have GROUP as a member."
517 (when (and (eq major-mode 'gnus-group-mode)
520 (let ((alist gnus-topic-alist))
521 ;; This is probably not entirely correct. If a topic
522 ;; isn't shown, then it's not updated. But the updating
523 ;; should be performed in any case, since the topic's
524 ;; parent should be updated. Pfft.
526 (when (and (member group (cdar alist))
527 (gnus-topic-goto-topic (caar alist)))
528 (gnus-topic-update-topic-line (caar alist)))
531 (defun gnus-topic-update-topic ()
532 "Update all parent topics to the current group."
533 (when (and (eq major-mode 'gnus-group-mode)
535 (let ((group (gnus-group-group-name))
536 (buffer-read-only nil))
538 (gnus-get-info group)
539 (gnus-topic-goto-topic (gnus-current-topic)))
540 (gnus-topic-update-topic-line (gnus-group-topic-name))
541 (gnus-group-goto-group group)
542 (gnus-group-position-point)))))
544 (defun gnus-topic-goto-missing-group (group)
545 "Place point where GROUP is supposed to be inserted."
546 (let* ((topic (gnus-group-topic group))
547 (groups (cdr (assoc topic gnus-topic-alist)))
548 (g (cdr (member group groups)))
550 ;; Try to jump to a visible group.
551 (while (and g (not (gnus-group-goto-group (car g) t)))
553 ;; It wasn't visible, so we try to see where to insert it.
555 (setq g (cdr (member group (reverse groups))))
556 (while (and g unfound)
557 (when (gnus-group-goto-group (pop g) t)
561 (gnus-topic-goto-topic topic)
564 (defun gnus-topic-update-topic-line (topic-name &optional reads)
565 (let* ((top (gnus-topic-find-topology topic-name))
567 (children (cddr top))
568 (entries (gnus-topic-find-groups
569 (car type) (car gnus-group-list-mode)
570 (cdr gnus-group-list-mode)))
571 (parent (gnus-topic-parent-topic topic-name))
572 (all-entries entries)
575 (when (gnus-topic-goto-topic (car type))
576 ;; Tally all the groups that belong in this topic.
578 (setq unread (- (gnus-group-topic-unread) reads))
580 (incf unread (gnus-topic-unread (caar (pop children)))))
581 (while (setq entry (pop entries))
582 (when (numberp (car entry))
583 (incf unread (car entry)))))
584 (setq old-unread (gnus-group-topic-unread))
585 ;; Insert the topic line.
586 (gnus-topic-insert-topic-line
587 (car type) (gnus-topic-visible-p)
588 (not (eq (nth 2 type) 'hidden))
589 (gnus-group-topic-level) all-entries unread)
593 (gnus-topic-update-topic-line
594 parent (- old-unread (gnus-group-topic-unread))))
597 (defun gnus-topic-group-indentation ()
599 (* gnus-topic-indent-level
602 (gnus-topic-goto-topic (gnus-current-topic))
603 (gnus-group-topic-level))
609 (gnus-add-shutdown 'gnus-topic-close 'gnus)
611 (defun gnus-topic-close ()
612 (setq gnus-topic-active-topology nil
613 gnus-topic-active-alist nil
614 gnus-topic-killed-topics nil
615 gnus-topic-tallied-groups nil
616 gnus-topology-checked-p nil))
618 (defun gnus-topic-check-topology ()
619 ;; The first time we set the topology to whatever we have
620 ;; gotten here, which can be rather random.
621 (unless gnus-topic-alist
622 (gnus-topic-init-alist))
624 (setq gnus-topology-checked-p t)
625 ;; Go through the topic alist and make sure that all topics
626 ;; are in the topic topology.
627 (let ((topics (gnus-topic-list))
628 (alist gnus-topic-alist)
631 (unless (member (caar alist) topics)
632 (nconc gnus-topic-topology
633 (list (list (list (caar alist) 'visible))))
635 (setq alist (cdr alist)))
637 (gnus-topic-enter-dribble))
638 ;; Conversely, go through the topology and make sure that all
639 ;; topologies have alists.
641 (unless (assoc (car topics) gnus-topic-alist)
642 (push (list (car topics)) gnus-topic-alist))
644 ;; Go through all living groups and make sure that
645 ;; they belong to some topic.
646 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
648 (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
649 (newsrc (cdr gnus-newsrc-alist))
652 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
653 (setcdr entry (cons group (cdr entry))))))
654 ;; Go through all topics and make sure they contain only living groups.
655 (let ((alist gnus-topic-alist)
657 (while (setq topic (pop alist))
659 (if (gnus-gethash (cadr topic) gnus-newsrc-hashtb)
660 (setq topic (cdr topic))
661 (setcdr topic (cddr topic)))))))
663 (defun gnus-topic-init-alist ()
664 "Initialize the topic structures."
665 (setq gnus-topic-topology
666 (cons (list "Gnus" 'visible)
667 (mapcar (lambda (topic)
668 (list (list (car topic) 'visible)))
670 (setq gnus-topic-alist
672 (mapcar (lambda (info) (gnus-info-group info))
673 (cdr gnus-newsrc-alist)))
675 (gnus-topic-enter-dribble))
679 (defun gnus-topic-clean-alist ()
680 "Remove bogus groups from the topic alist."
681 (let ((topic-alist gnus-topic-alist)
683 (unless gnus-killed-hashtb
684 (gnus-make-hashtable-from-killed))
685 (while (setq topic (pop topic-alist))
686 (let ((topic-name (pop topic))
687 group filtered-topic)
688 (while (setq group (pop topic))
689 (when (and (or (gnus-gethash group gnus-active-hashtb)
690 (gnus-info-method (gnus-get-info group)))
691 (not (gnus-gethash group gnus-killed-hashtb)))
692 (push group filtered-topic)))
693 (push (cons topic-name (nreverse filtered-topic)) result)))
694 (setq gnus-topic-alist (nreverse result))))
696 (defun gnus-topic-change-level (group level oldlevel)
697 "Run when changing levels to enter/remove groups from topics."
699 (set-buffer gnus-group-buffer)
700 (when (and gnus-topic-mode
702 (not gnus-topic-inhibit-change-level))
703 ;; Remove the group from the topics.
704 (when (and (< oldlevel gnus-level-zombie)
705 (>= level gnus-level-zombie))
708 (when (setq alist (assoc (gnus-current-topic) gnus-topic-alist))
709 (setcdr alist (gnus-delete-first group (cdr alist))))))
710 ;; If the group is subscribed we enter it into the topics.
711 (when (and (< level gnus-level-zombie)
712 (>= oldlevel gnus-level-zombie))
713 (let* ((prev (gnus-group-group-name))
714 (gnus-topic-inhibit-change-level t)
715 (gnus-group-indentation
717 (* gnus-topic-indent-level
719 (gnus-topic-goto-topic (gnus-current-topic))
720 (gnus-group-topic-level))
723 (yanked (list group))
725 ;; Then we enter the yanked groups into the topics they belong
727 (when (setq alist (assoc (save-excursion
731 (caar gnus-topic-topology)))
734 (when (stringp yanked)
735 (setq yanked (list yanked)))
738 (if (not (cdr alist))
739 (setcdr alist (nconc yanked (cdr alist)))
740 (while (and (not end) (cdr alist))
741 (when (equal (cadr alist) prev)
742 (setcdr alist (nconc yanked (cdr alist)))
744 (setq alist (cdr alist)))
746 (nconc talist yanked))))))
747 (gnus-topic-update-topic)))))
749 (defun gnus-topic-goto-next-group (group props)
750 "Go to group or the next group after group."
752 (if (not (memq 'gnus-topic props))
753 (goto-char (point-max))
754 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
755 (if (gnus-group-goto-group group)
757 ;; The group is no longer visible.
758 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
759 (after (cdr (member group (cdr list)))))
760 ;; First try to put point on a group after the current one.
762 (not (gnus-group-goto-group (car after))))
763 (setq after (cdr after)))
764 ;; Then try to put point on a group before point.
766 (setq after (cdr (member group (reverse (cdr list)))))
768 (not (gnus-group-goto-group (car after))))
769 (setq after (cdr after))))
770 ;; Finally, just put point on the topic.
772 (goto-char (point-min))
774 (gnus-topic-goto-topic (car list))
778 ;;; Topic-active functions
780 (defun gnus-topic-grok-active (&optional force)
781 "Parse all active groups and create topic structures for them."
782 ;; First we make sure that we have really read the active file.
784 (not gnus-topic-active-alist))
786 ;; Get a list of all groups available.
787 (mapatoms (lambda (g) (when (symbol-value g)
788 (push (symbol-name g) groups)))
790 (setq groups (sort groups 'string<))
791 ;; Init the variables.
792 (setq gnus-topic-active-topology (list (list "" 'visible)))
793 (setq gnus-topic-active-alist nil)
794 ;; Descend the top-level hierarchy.
795 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
796 ;; Set the top-level topic names to something nice.
797 (setcar (car gnus-topic-active-topology) "Gnus active")
798 (setcar (car gnus-topic-active-alist) "Gnus active"))))
800 (defun gnus-topic-grok-active-1 (topology groups)
801 (let* ((name (caar topology))
802 (prefix (concat "^" (regexp-quote name)))
803 tgroups ntopology group)
805 (string-match prefix (setq group (car groups))))
806 (if (not (string-match "\\." group (match-end 0)))
807 ;; There are no further hierarchies here, so we just
808 ;; enter this group into the list belonging to this
810 (push (pop groups) tgroups)
811 ;; New sub-hierarchy, so we add it to the topology.
812 (nconc topology (list (setq ntopology
813 (list (list (substring
814 group 0 (match-end 0))
816 ;; Descend the hierarchy.
817 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
818 ;; We remove the trailing "." from the topic name.
820 (if (string-match "\\.$" name)
821 (substring name 0 (match-beginning 0))
823 ;; Add this topic and its groups to the topic alist.
824 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
825 (setcar (car topology) name)
826 ;; We return the rest of the groups that didn't belong
830 ;;; Topic mode, commands and keymap.
832 (defvar gnus-topic-mode-map nil)
833 (defvar gnus-group-topic-map nil)
835 (unless gnus-topic-mode-map
836 (setq gnus-topic-mode-map (make-sparse-keymap))
838 ;; Override certain group mode keys.
839 (gnus-define-keys gnus-topic-mode-map
840 "=" gnus-topic-select-group
841 "\r" gnus-topic-select-group
842 " " gnus-topic-read-group
843 "\C-k" gnus-topic-kill-group
844 "\C-y" gnus-topic-yank-group
845 "\M-g" gnus-topic-get-new-news-this-topic
846 "AT" gnus-topic-list-active
847 "Gp" gnus-topic-edit-parameters
848 "#" gnus-topic-mark-topic
849 "\M-#" gnus-topic-unmark-topic
850 gnus-mouse-2 gnus-mouse-pick-topic)
852 ;; Define a new submap.
853 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
854 "#" gnus-topic-mark-topic
855 "\M-#" gnus-topic-unmark-topic
856 "n" gnus-topic-create-topic
857 "m" gnus-topic-move-group
858 "D" gnus-topic-remove-group
859 "c" gnus-topic-copy-group
860 "h" gnus-topic-hide-topic
861 "s" gnus-topic-show-topic
862 "M" gnus-topic-move-matching
863 "C" gnus-topic-copy-matching
864 "\C-i" gnus-topic-indent
865 [tab] gnus-topic-indent
866 "r" gnus-topic-rename
867 "\177" gnus-topic-delete)
869 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
870 "s" gnus-topic-sort-groups
871 "a" gnus-topic-sort-groups-by-alphabet
872 "u" gnus-topic-sort-groups-by-unread
873 "l" gnus-topic-sort-groups-by-level
874 "v" gnus-topic-sort-groups-by-score
875 "r" gnus-topic-sort-groups-by-rank
876 "m" gnus-topic-sort-groups-by-method))
878 (defun gnus-topic-make-menu-bar ()
879 (unless (boundp 'gnus-topic-menu)
881 gnus-topic-menu gnus-topic-mode-map ""
883 ["Toggle topics" gnus-topic-mode t]
885 ["Copy" gnus-topic-copy-group t]
886 ["Move" gnus-topic-move-group t]
887 ["Remove" gnus-topic-remove-group t]
888 ["Copy matching" gnus-topic-copy-matching t]
889 ["Move matching" gnus-topic-move-matching t])
891 ["Show" gnus-topic-show-topic t]
892 ["Hide" gnus-topic-hide-topic t]
893 ["Delete" gnus-topic-delete t]
894 ["Rename" gnus-topic-rename t]
895 ["Create" gnus-topic-create-topic t]
896 ["Mark" gnus-topic-mark-topic t]
897 ["Indent" gnus-topic-indent t])
898 ["List active" gnus-topic-list-active t]))))
900 (defun gnus-topic-mode (&optional arg redisplay)
901 "Minor mode for topicsifying Gnus group buffers."
902 (interactive (list current-prefix-arg t))
903 (when (eq major-mode 'gnus-group-mode)
904 (make-local-variable 'gnus-topic-mode)
905 (setq gnus-topic-mode
906 (if (null arg) (not gnus-topic-mode)
907 (> (prefix-numeric-value arg) 0)))
908 ;; Infest Gnus with topics.
909 (when gnus-topic-mode
910 (when (gnus-visual-p 'topic-menu 'menu)
911 (gnus-topic-make-menu-bar))
912 (setq gnus-topic-line-format-spec
913 (gnus-parse-format gnus-topic-line-format
914 gnus-topic-line-format-alist t))
915 (unless (assq 'gnus-topic-mode minor-mode-alist)
916 (push '(gnus-topic-mode " Topic") minor-mode-alist))
917 (unless (assq 'gnus-topic-mode minor-mode-map-alist)
918 (push (cons 'gnus-topic-mode gnus-topic-mode-map)
919 minor-mode-map-alist))
920 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
921 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
922 (set (make-local-variable 'gnus-group-prepare-function)
923 'gnus-group-prepare-topics)
924 (set (make-local-variable 'gnus-group-get-parameter-function)
925 'gnus-group-topic-parameters)
926 (set (make-local-variable 'gnus-group-goto-next-group-function)
927 'gnus-topic-goto-next-group)
928 (set (make-local-variable 'gnus-group-indentation-function)
929 'gnus-topic-group-indentation)
930 (set (make-local-variable 'gnus-group-update-group-function)
931 'gnus-topic-update-topics-containing-group)
932 (set (make-local-variable 'gnus-group-sort-alist-function)
933 'gnus-group-sort-topic)
934 (setq gnus-group-change-level-function 'gnus-topic-change-level)
935 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
936 (make-local-hook 'gnus-check-bogus-groups-hook)
937 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
938 (setq gnus-topology-checked-p nil)
939 ;; We check the topology.
940 (when gnus-newsrc-alist
941 (gnus-topic-check-topology))
942 (run-hooks 'gnus-topic-mode-hook))
943 ;; Remove topic infestation.
944 (unless gnus-topic-mode
945 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
946 (remove-hook 'gnus-group-change-level-function
947 'gnus-topic-change-level)
948 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
949 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
950 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
952 (gnus-group-list-groups))))
954 (defun gnus-topic-select-group (&optional all)
955 "Select this newsgroup.
956 No article is selected automatically.
957 If ALL is non-nil, already read articles become readable.
958 If ALL is a number, fetch this number of articles.
960 If performed over a topic line, toggle folding the topic."
962 (if (gnus-group-topic-p)
963 (let ((gnus-group-list-mode
964 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
965 (gnus-topic-fold all))
966 (gnus-group-select-group all)))
968 (defun gnus-mouse-pick-topic (e)
969 "Select the group or topic under the mouse pointer."
972 (gnus-topic-read-group nil))
974 (defun gnus-topic-read-group (&optional all no-article group)
975 "Read news in this newsgroup.
976 If the prefix argument ALL is non-nil, already read articles become
977 readable. IF ALL is a number, fetch this number of articles. If the
978 optional argument NO-ARTICLE is non-nil, no article will be
979 auto-selected upon group entry. If GROUP is non-nil, fetch that
982 If performed over a topic line, toggle folding the topic."
984 (if (gnus-group-topic-p)
985 (let ((gnus-group-list-mode
986 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
987 (gnus-topic-fold all))
988 (gnus-group-read-group all no-article group)))
990 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
993 (read-string "New topic: ")
994 (gnus-current-topic)))
995 ;; Check whether this topic already exists.
996 (when (gnus-topic-find-topology topic)
997 (error "Topic already exists"))
999 (setq parent (caar gnus-topic-topology)))
1000 (let ((top (cdr (gnus-topic-find-topology parent)))
1001 (full-topic (or full-topic `((,topic visible)))))
1003 (error "No such parent topic: %s" parent))
1006 (while (and (cdr top)
1007 (not (equal (caaadr top) previous)))
1008 (setq top (cdr top)))
1009 (setcdr top (cons full-topic (cdr top))))
1010 (nconc top (list full-topic)))
1011 (unless (assoc topic gnus-topic-alist)
1012 (push (list topic) gnus-topic-alist)))
1013 (gnus-topic-enter-dribble)
1014 (gnus-group-list-groups)
1015 (gnus-topic-goto-topic topic))
1017 (defun gnus-topic-move-group (n topic &optional copyp)
1018 "Move the next N groups to TOPIC.
1019 If COPYP, copy the groups instead."
1021 (list current-prefix-arg
1022 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1023 (let ((groups (gnus-group-process-prefix n))
1024 (topicl (assoc topic gnus-topic-alist))
1025 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1026 (start-topic (gnus-group-topic-name))
1030 (gnus-group-remove-mark g)
1032 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1034 (setcdr entry (gnus-delete-first g (cdr entry))))
1035 (nconc topicl (list g)))
1037 (gnus-topic-enter-dribble)
1039 (gnus-group-goto-group start-group)
1040 (gnus-topic-goto-topic start-topic))
1041 (gnus-group-list-groups)))
1043 (defun gnus-topic-remove-group (&optional arg)
1044 "Remove the current group from the topic."
1046 (gnus-group-iterate arg
1048 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1049 (buffer-read-only nil))
1050 (when (and topicl group)
1052 (gnus-delete-first group topicl))
1053 (gnus-topic-update-topic)
1054 (gnus-group-position-point)))))
1056 (defun gnus-topic-copy-group (n topic)
1057 "Copy the current group to a topic."
1059 (list current-prefix-arg
1060 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1061 (gnus-topic-move-group n topic t))
1063 (defun gnus-topic-kill-group (&optional n discard)
1064 "Kill the next N groups."
1066 (if (gnus-group-topic-p)
1067 (let ((topic (gnus-group-topic-name)))
1069 (gnus-topic-find-topology topic)
1070 (assoc topic gnus-topic-alist))
1071 gnus-topic-killed-topics)
1072 (gnus-topic-remove-topic nil t)
1073 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1074 (gnus-topic-enter-dribble))
1075 (gnus-group-kill-group n discard)
1076 (gnus-topic-update-topic)))
1078 (defun gnus-topic-yank-group (&optional arg)
1079 "Yank the last topic."
1081 (if gnus-topic-killed-topics
1083 (or (gnus-group-topic-name)
1084 (gnus-topic-next-topic (gnus-current-topic))))
1085 (data (pop gnus-topic-killed-topics))
1088 (push alist gnus-topic-alist)
1089 (gnus-topic-create-topic
1090 (caar item) (gnus-topic-parent-topic previous) previous
1092 (gnus-topic-enter-dribble)
1093 (gnus-topic-goto-topic (caar item)))
1094 (let* ((prev (gnus-group-group-name))
1095 (gnus-topic-inhibit-change-level t)
1096 (gnus-group-indentation
1098 (* gnus-topic-indent-level
1100 (gnus-topic-goto-topic (gnus-current-topic))
1101 (gnus-group-topic-level))
1105 ;; We first yank the groups the normal way...
1106 (setq yanked (gnus-group-yank-group arg))
1107 ;; Then we enter the yanked groups into the topics they belong
1109 (setq alist (assoc (save-excursion
1111 (gnus-current-topic))
1113 (when (stringp yanked)
1114 (setq yanked (list yanked)))
1116 (nconc alist yanked)
1117 (if (not (cdr alist))
1118 (setcdr alist (nconc yanked (cdr alist)))
1120 (when (equal (cadr alist) prev)
1121 (setcdr alist (nconc yanked (cdr alist)))
1123 (setq alist (cdr alist))))))
1124 (gnus-topic-update-topic)))
1126 (defun gnus-topic-hide-topic ()
1127 "Hide the current topic."
1129 (when (gnus-current-topic)
1130 (gnus-topic-goto-topic (gnus-current-topic))
1131 (gnus-topic-remove-topic nil nil 'hidden)))
1133 (defun gnus-topic-show-topic ()
1134 "Show the hidden topic."
1136 (when (gnus-group-topic-p)
1137 (gnus-topic-remove-topic t nil 'shown)))
1139 (defun gnus-topic-mark-topic (topic &optional unmark)
1140 "Mark all groups in the topic with the process mark."
1141 (interactive (list (gnus-group-topic-name)))
1143 (call-interactively 'gnus-group-mark-group)
1145 (let ((groups (gnus-topic-find-groups topic 9 t)))
1147 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1148 (gnus-info-group (nth 2 (pop groups)))))))))
1150 (defun gnus-topic-unmark-topic (topic &optional unmark)
1151 "Remove the process mark from all groups in the topic."
1152 (interactive (list (gnus-group-topic-name)))
1154 (call-interactively 'gnus-group-unmark-group)
1155 (gnus-topic-mark-topic topic t)))
1157 (defun gnus-topic-get-new-news-this-topic (&optional n)
1158 "Check for new news in the current topic."
1160 (if (not (gnus-group-topic-p))
1161 (gnus-group-get-new-news-this-group n)
1162 (gnus-topic-mark-topic (gnus-group-topic-name))
1163 (gnus-group-get-new-news-this-group)))
1165 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1166 "Move all groups that match REGEXP to some topic."
1171 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1172 (read-string (format "Move to %s (regexp): " topic))))))
1173 (gnus-group-mark-regexp regexp)
1174 (gnus-topic-move-group nil topic copyp))
1176 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1177 "Copy all groups that match REGEXP to some topic."
1182 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1183 (read-string (format "Copy to %s (regexp): " topic))))))
1184 (gnus-topic-move-matching regexp topic t))
1186 (defun gnus-topic-delete (topic)
1188 (interactive (list (gnus-group-topic-name)))
1190 (error "No topic to be deleted"))
1191 (let ((entry (assoc topic gnus-topic-alist))
1192 (buffer-read-only nil))
1194 (error "Topic not empty"))
1195 ;; Delete if visible.
1196 (when (gnus-topic-goto-topic topic)
1198 ;; Remove from alist.
1199 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1200 ;; Remove from topology.
1201 (gnus-topic-find-topology topic nil nil 'delete)))
1203 (defun gnus-topic-rename (old-name new-name)
1206 (let ((topic (gnus-current-topic)))
1208 (read-string (format "Rename %s to: " topic)))))
1209 (let ((top (gnus-topic-find-topology old-name))
1210 (entry (assoc old-name gnus-topic-alist)))
1212 (setcar (cadr top) new-name))
1214 (setcar entry new-name))
1216 (gnus-dribble-touch)
1217 (gnus-group-list-groups)))
1219 (defun gnus-topic-indent (&optional unindent)
1220 "Indent a topic -- make it a sub-topic of the previous topic.
1221 If UNINDENT, remove an indentation."
1224 (gnus-topic-unindent)
1225 (let* ((topic (gnus-current-topic))
1226 (parent (gnus-topic-previous-topic topic))
1227 (buffer-read-only nil))
1229 (error "Nothing to indent %s into" topic))
1231 (gnus-topic-goto-topic topic)
1232 (gnus-topic-kill-group)
1233 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1234 (gnus-topic-create-topic
1235 topic parent nil (cdaar gnus-topic-killed-topics))
1236 (pop gnus-topic-killed-topics)
1237 (or (gnus-topic-goto-topic topic)
1238 (gnus-topic-goto-topic parent))))))
1240 (defun gnus-topic-unindent ()
1243 (let* ((topic (gnus-current-topic))
1244 (parent (gnus-topic-parent-topic topic))
1245 (grandparent (gnus-topic-parent-topic parent)))
1247 (error "Nothing to indent %s into" topic))
1249 (gnus-topic-goto-topic topic)
1250 (gnus-topic-kill-group)
1251 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1252 (gnus-topic-create-topic
1253 topic grandparent (gnus-topic-next-topic parent)
1254 (cdaar gnus-topic-killed-topics))
1255 (pop gnus-topic-killed-topics)
1256 (gnus-topic-goto-topic topic))))
1258 (defun gnus-topic-list-active (&optional force)
1259 "List all groups that Gnus knows about in a topicsified fashion.
1260 If FORCE, always re-read the active file."
1263 (gnus-get-killed-groups))
1264 (gnus-topic-grok-active force)
1265 (let ((gnus-topic-topology gnus-topic-active-topology)
1266 (gnus-topic-alist gnus-topic-active-alist)
1267 gnus-killed-list gnus-zombie-list)
1268 (gnus-group-list-groups 9 nil 1)))
1270 ;;; Topic sorting functions
1272 (defun gnus-topic-edit-parameters (group)
1273 "Edit the group parameters of GROUP.
1274 If performed on a topic, edit the topic parameters instead."
1275 (interactive (list (gnus-group-group-name)))
1277 (gnus-group-edit-group-parameters group)
1278 (if (not (gnus-group-topic-p))
1279 (error "Nothing to edit on the current line.")
1280 (let ((topic (gnus-group-topic-name)))
1282 (gnus-topic-parameters topic)
1283 (format "Editing the topic parameters for `%s'."
1286 (gnus-topic-set-parameters ,topic form)))))))
1288 (defun gnus-group-sort-topic (func reverse)
1289 "Sort groups in the topics according to FUNC and REVERSE."
1290 (let ((alist gnus-topic-alist))
1292 ;; !!!Sometimes nil elements sneak into the alist,
1293 ;; for some reason or other.
1294 (setcar alist (delq nil (car alist)))
1295 (setcar alist (delete "dummy.group" (car alist)))
1296 (gnus-topic-sort-topic (pop alist) func reverse))))
1298 (defun gnus-topic-sort-topic (topic func reverse)
1299 ;; Each topic only lists the name of the group, while
1300 ;; the sort predicates expect group infos as inputs.
1301 ;; So we first transform the group names into infos,
1302 ;; then sort, and then transform back into group names.
1306 (lambda (info) (gnus-info-group info))
1309 (lambda (group) (gnus-get-info group))
1312 ;; Do the reversal, if necessary.
1314 (setcdr topic (nreverse (cdr topic)))))
1316 (defun gnus-topic-sort-groups (func &optional reverse)
1317 "Sort the current topic according to FUNC.
1318 If REVERSE, reverse the sorting order."
1319 (interactive (list gnus-group-sort-function current-prefix-arg))
1320 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1321 (gnus-topic-sort-topic
1322 topic (gnus-make-sort-function func) reverse)
1323 (gnus-group-list-groups)))
1325 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1326 "Sort the current topic alphabetically by group name.
1327 If REVERSE, sort in reverse order."
1329 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1331 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1332 "Sort the current topic by number of unread articles.
1333 If REVERSE, sort in reverse order."
1335 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1337 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1338 "Sort the current topic by group level.
1339 If REVERSE, sort in reverse order."
1341 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1343 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1344 "Sort the current topic by group score.
1345 If REVERSE, sort in reverse order."
1347 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1349 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1350 "Sort the current topic by group rank.
1351 If REVERSE, sort in reverse order."
1353 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1355 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1356 "Sort the current topic alphabetically by backend name.
1357 If REVERSE, sort in reverse order."
1359 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1361 (provide 'gnus-topic)
1363 ;;; gnus-topic.el ends here