1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Ilja Weis <kult@uni-paderborn.de>
5 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
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 lowest)
185 "Return entries for all visible groups in TOPIC."
186 (let ((groups (cdr (assoc topic gnus-topic-alist)))
187 info clevel unread group params visible-groups entry active)
188 (setq lowest (or lowest 1))
189 (setq level (or level gnus-level-unsubscribed))
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) gnus-level-zombie gnus-level-killed))))
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.
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)
375 (<= lowest gnus-level-zombie))
376 (gnus-group-prepare-flat-list-dead
377 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
381 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
382 (gnus-group-prepare-flat-list-dead
383 (setq gnus-killed-list (sort gnus-killed-list 'string<))
389 (when (< lowest gnus-level-zombie)
391 (let ((top (gnus-topic-find-topology list-topic)))
392 (gnus-topic-prepare-topic (cdr top) (car top)
393 (or topic-level level) all
395 (gnus-topic-prepare-topic gnus-topic-topology 0
396 (or topic-level level) all
399 (gnus-group-set-mode-line)
400 (setq gnus-group-list-mode (cons level all))
401 (gnus-run-hooks 'gnus-group-prepare-hook))))
403 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent
405 "Insert TOPIC into the group buffer.
406 If SILENT, don't insert anything. Return the number of unread
407 articles in the topic and its subtopics."
408 (let* ((type (pop topicl))
409 (entries (gnus-topic-find-groups (car type) list-level all lowest))
410 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
411 (gnus-group-indentation
412 (make-string (* gnus-topic-indent-level level) ? ))
413 (beg (progn (beginning-of-line) (point)))
414 (topicl (reverse topicl))
415 (all-entries entries)
416 (point-max (point-max))
419 info entry end active tick)
420 ;; Insert any sub-topics.
423 (gnus-topic-prepare-topic
424 (pop topicl) (1+ level) list-level all
425 (not visiblep) lowest)))
428 ;; Insert all the groups that belong in this topic.
429 (while (setq entry (pop entries))
433 (gnus-group-insert-group-line
434 entry (if (member entry gnus-zombie-list) gnus-level-zombie gnus-level-killed)
435 nil (- (1+ (cdr (setq active (gnus-active entry))))
439 (when (setq info (nth 2 entry))
440 (gnus-group-insert-group-line
441 (gnus-info-group info)
442 (gnus-info-level info) (gnus-info-marks info)
443 (car entry) (gnus-info-method info)))))
444 (when (and (listp entry)
445 (numberp (car entry)))
446 (incf unread (car entry)))
450 ;; Insert the topic line.
451 (when (and (not silent)
452 (or gnus-topic-display-empty-topics ;We want empty topics
453 (not (zerop unread)) ;Non-empty
454 tick ;Ticked articles
455 (/= point-max (point-max)))) ;Unactivated groups
456 (gnus-extent-start-open (point))
457 (gnus-topic-insert-topic-line
459 (not (eq (nth 2 type) 'hidden))
460 level all-entries unread))
464 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
465 "Remove the current topic."
466 (let ((topic (gnus-group-topic-name))
467 (level (gnus-group-topic-level))
468 (beg (progn (beginning-of-line) (point)))
471 (while (and (zerop (forward-line 1))
472 (> (or (gnus-group-topic-level) (1+ level)) level)))
473 (delete-region beg (point))
474 ;; Do the change in this rather odd manner because it has been
475 ;; reported that some topics share parts of some lists, for some
476 ;; reason. I have been unable to determine why this is the
477 ;; case, but this hack seems to take care of things.
478 (let ((data (cadr (gnus-topic-find-topology topic))))
480 (list (if insert 'visible 'invisible)
484 (setq gnus-topic-alist
485 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
486 (gnus-topic-insert-topic topic in-level)))))
488 (defun gnus-topic-insert-topic (topic &optional level)
490 (gnus-group-prepare-topics
491 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
492 nil nil topic level))
494 (defun gnus-topic-fold (&optional insert)
495 "Remove/insert the current topic."
496 (let ((topic (gnus-group-topic-name)))
499 (if (not (gnus-group-active-topic-p))
500 (gnus-topic-remove-topic
501 (or insert (not (gnus-topic-visible-p))))
502 (let ((gnus-topic-topology gnus-topic-active-topology)
503 (gnus-topic-alist gnus-topic-active-alist)
504 (gnus-group-list-mode (cons 5 t)))
505 (gnus-topic-remove-topic
506 (or insert (not (gnus-topic-visible-p))) nil nil 9)
507 (gnus-topic-enter-dribble)))))))
509 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
511 (let* ((visible (if visiblep "" "..."))
512 (indentation (make-string (* gnus-topic-indent-level level) ? ))
513 (total-number-of-articles unread)
514 (number-of-groups (length entries))
515 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
519 (gnus-add-text-properties
522 (eval gnus-topic-line-format-spec))
523 (list 'gnus-topic (intern name)
524 'gnus-topic-level level
525 'gnus-topic-unread unread
526 'gnus-active active-topic
527 'gnus-topic-visible visiblep))))
529 (defun gnus-topic-update-topics-containing-group (group)
530 "Update all topics that have GROUP as a member."
531 (when (and (eq major-mode 'gnus-group-mode)
534 (let ((alist gnus-topic-alist))
535 ;; This is probably not entirely correct. If a topic
536 ;; isn't shown, then it's not updated. But the updating
537 ;; should be performed in any case, since the topic's
538 ;; parent should be updated. Pfft.
540 (when (and (member group (cdar alist))
541 (gnus-topic-goto-topic (caar alist)))
542 (gnus-topic-update-topic-line (caar alist)))
545 (defun gnus-topic-update-topic ()
546 "Update all parent topics to the current group."
547 (when (and (eq major-mode 'gnus-group-mode)
549 (let ((group (gnus-group-group-name))
551 (buffer-read-only nil))
553 (gnus-get-info group)
554 (gnus-topic-goto-topic (gnus-current-topic)))
555 (gnus-topic-update-topic-line (gnus-group-topic-name))
558 (gnus-group-position-point)))))
560 (defun gnus-topic-goto-missing-group (group)
561 "Place point where GROUP is supposed to be inserted."
562 (let* ((topic (gnus-group-topic group))
563 (groups (cdr (assoc topic gnus-topic-alist)))
564 (g (cdr (member group groups)))
566 ;; Try to jump to a visible group.
567 (while (and g (not (gnus-group-goto-group (car g) t)))
569 ;; It wasn't visible, so we try to see where to insert it.
571 (setq g (cdr (member group (reverse groups))))
572 (while (and g unfound)
573 (when (gnus-group-goto-group (pop g) t)
578 (not (gnus-topic-goto-missing-topic topic)))
579 (gnus-topic-insert-topic-line
580 topic t t (car (gnus-topic-find-topology topic)) nil 0)))))
582 (defun gnus-topic-goto-missing-topic (topic)
583 (if (gnus-topic-goto-topic topic)
585 ;; Topic not displayed.
586 (let* ((top (gnus-topic-find-topology
587 (gnus-topic-parent-topic topic)))
588 (tp (reverse (cddr top))))
589 (while (not (equal (caaar tp) topic))
593 (not (gnus-topic-goto-topic (caaar tp))))
596 (gnus-topic-forward-topic 1)
597 (gnus-topic-goto-missing-topic (caadr top))))
600 (defun gnus-topic-update-topic-line (topic-name &optional reads)
601 (let* ((top (gnus-topic-find-topology topic-name))
603 (children (cddr top))
604 (entries (gnus-topic-find-groups
605 (car type) (car gnus-group-list-mode)
606 (cdr gnus-group-list-mode)))
607 (parent (gnus-topic-parent-topic topic-name))
608 (all-entries entries)
611 (when (gnus-topic-goto-topic (car type))
612 ;; Tally all the groups that belong in this topic.
614 (setq unread (- (gnus-group-topic-unread) reads))
616 (incf unread (gnus-topic-unread (caar (pop children)))))
617 (while (setq entry (pop entries))
618 (when (numberp (car entry))
619 (incf unread (car entry)))))
620 (setq old-unread (gnus-group-topic-unread))
621 ;; Insert the topic line.
622 (gnus-topic-insert-topic-line
623 (car type) (gnus-topic-visible-p)
624 (not (eq (nth 2 type) 'hidden))
625 (gnus-group-topic-level) all-entries unread)
629 (gnus-topic-update-topic-line
630 parent (- (or old-unread 0) (or (gnus-group-topic-unread) 0))))
633 (defun gnus-topic-group-indentation ()
635 (* gnus-topic-indent-level
638 (gnus-topic-goto-topic (gnus-current-topic))
639 (gnus-group-topic-level))
645 (gnus-add-shutdown 'gnus-topic-close 'gnus)
647 (defun gnus-topic-close ()
648 (setq gnus-topic-active-topology nil
649 gnus-topic-active-alist nil
650 gnus-topic-killed-topics nil
651 gnus-topology-checked-p nil))
653 (defun gnus-topic-check-topology ()
654 ;; The first time we set the topology to whatever we have
655 ;; gotten here, which can be rather random.
656 (unless gnus-topic-alist
657 (gnus-topic-init-alist))
659 (setq gnus-topology-checked-p t)
660 ;; Go through the topic alist and make sure that all topics
661 ;; are in the topic topology.
662 (let ((topics (gnus-topic-list))
663 (alist gnus-topic-alist)
666 (unless (member (caar alist) topics)
667 (nconc gnus-topic-topology
668 (list (list (list (caar alist) 'visible))))
670 (setq alist (cdr alist)))
672 (gnus-topic-enter-dribble))
673 ;; Conversely, go through the topology and make sure that all
674 ;; topologies have alists.
676 (unless (assoc (car topics) gnus-topic-alist)
677 (push (list (car topics)) gnus-topic-alist))
679 ;; Go through all living groups and make sure that
680 ;; they belong to some topic.
681 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
683 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
684 (newsrc (cdr gnus-newsrc-alist))
687 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
688 (setcdr entry (list group))
689 (setq entry (cdr entry)))))
690 ;; Go through all topics and make sure they contain only living groups.
691 (let ((alist gnus-topic-alist)
693 (while (setq topic (pop alist))
695 (if (and (cadr topic)
696 (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
697 (setq topic (cdr topic))
698 (setcdr topic (cddr topic)))))))
700 (defun gnus-topic-init-alist ()
701 "Initialize the topic structures."
702 (setq gnus-topic-topology
703 (cons (list "Gnus" 'visible)
704 (mapcar (lambda (topic)
705 (list (list (car topic) 'visible)))
707 (setq gnus-topic-alist
709 (mapcar (lambda (info) (gnus-info-group info))
710 (cdr gnus-newsrc-alist)))
712 (gnus-topic-enter-dribble))
716 (defun gnus-topic-clean-alist ()
717 "Remove bogus groups from the topic alist."
718 (let ((topic-alist gnus-topic-alist)
720 (unless gnus-killed-hashtb
721 (gnus-make-hashtable-from-killed))
722 (while (setq topic (pop topic-alist))
723 (let ((topic-name (pop topic))
724 group filtered-topic)
725 (while (setq group (pop topic))
726 (when (and (or (gnus-gethash group gnus-active-hashtb)
727 (gnus-info-method (gnus-get-info group)))
728 (not (gnus-gethash group gnus-killed-hashtb)))
729 (push group filtered-topic)))
730 (push (cons topic-name (nreverse filtered-topic)) result)))
731 (setq gnus-topic-alist (nreverse result))))
733 (defun gnus-topic-change-level (group level oldlevel &optional previous)
734 "Run when changing levels to enter/remove groups from topics."
736 (set-buffer gnus-group-buffer)
737 (let ((buffer-read-only nil))
738 (unless gnus-topic-inhibit-change-level
739 (gnus-group-goto-group (or (car (nth 2 previous)) group))
740 (when (and gnus-topic-mode
742 (not gnus-topic-inhibit-change-level))
743 ;; Remove the group from the topics.
744 (if (and (< oldlevel gnus-level-zombie)
745 (>= level gnus-level-zombie))
746 (let ((alist gnus-topic-alist))
747 (while (gnus-group-goto-group group)
750 (when (member group (car alist))
751 (setcdr (car alist) (delete group (cdar alist))))
753 ;; If the group is subscribed we enter it into the topics.
754 (when (and (< level gnus-level-zombie)
755 (>= oldlevel gnus-level-zombie))
756 (let* ((prev (gnus-group-group-name))
757 (gnus-topic-inhibit-change-level t)
758 (gnus-group-indentation
760 (* gnus-topic-indent-level
762 (gnus-topic-goto-topic (gnus-current-topic))
763 (gnus-group-topic-level))
766 (yanked (list group))
768 ;; Then we enter the yanked groups into the topics they belong
770 (when (setq alist (assoc (save-excursion
774 (caar gnus-topic-topology)))
777 (when (stringp yanked)
778 (setq yanked (list yanked)))
781 (if (not (cdr alist))
782 (setcdr alist (nconc yanked (cdr alist)))
783 (while (and (not end) (cdr alist))
784 (when (equal (cadr alist) prev)
785 (setcdr alist (nconc yanked (cdr alist)))
787 (setq alist (cdr alist)))
789 (nconc talist yanked))))))
790 (gnus-topic-update-topic))))))))
792 (defun gnus-topic-goto-next-group (group props)
793 "Go to group or the next group after group."
795 (if (not (memq 'gnus-topic props))
796 (goto-char (point-max))
797 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
798 (if (gnus-group-goto-group group)
800 ;; The group is no longer visible.
801 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
802 (after (cdr (member group (cdr list)))))
803 ;; First try to put point on a group after the current one.
805 (not (gnus-group-goto-group (car after))))
806 (setq after (cdr after)))
807 ;; Then try to put point on a group before point.
809 (setq after (cdr (member group (reverse (cdr list)))))
811 (not (gnus-group-goto-group (car after))))
812 (setq after (cdr after))))
813 ;; Finally, just put point on the topic.
815 (goto-char (point-min))
817 (gnus-topic-goto-topic (car list))
821 ;;; Topic-active functions
823 (defun gnus-topic-grok-active (&optional force)
824 "Parse all active groups and create topic structures for them."
825 ;; First we make sure that we have really read the active file.
827 (not gnus-topic-active-alist))
829 ;; Get a list of all groups available.
830 (mapatoms (lambda (g) (when (symbol-value g)
831 (push (symbol-name g) groups)))
833 (setq groups (sort groups 'string<))
834 ;; Init the variables.
835 (setq gnus-topic-active-topology (list (list "" 'visible)))
836 (setq gnus-topic-active-alist nil)
837 ;; Descend the top-level hierarchy.
838 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
839 ;; Set the top-level topic names to something nice.
840 (setcar (car gnus-topic-active-topology) "Gnus active")
841 (setcar (car gnus-topic-active-alist) "Gnus active"))))
843 (defun gnus-topic-grok-active-1 (topology groups)
844 (let* ((name (caar topology))
845 (prefix (concat "^" (regexp-quote name)))
846 tgroups ntopology group)
848 (string-match prefix (setq group (car groups))))
849 (if (not (string-match "\\." group (match-end 0)))
850 ;; There are no further hierarchies here, so we just
851 ;; enter this group into the list belonging to this
853 (push (pop groups) tgroups)
854 ;; New sub-hierarchy, so we add it to the topology.
855 (nconc topology (list (setq ntopology
856 (list (list (substring
857 group 0 (match-end 0))
859 ;; Descend the hierarchy.
860 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
861 ;; We remove the trailing "." from the topic name.
863 (if (string-match "\\.$" name)
864 (substring name 0 (match-beginning 0))
866 ;; Add this topic and its groups to the topic alist.
867 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
868 (setcar (car topology) name)
869 ;; We return the rest of the groups that didn't belong
873 ;;; Topic mode, commands and keymap.
875 (defvar gnus-topic-mode-map nil)
876 (defvar gnus-group-topic-map nil)
878 (unless gnus-topic-mode-map
879 (setq gnus-topic-mode-map (make-sparse-keymap))
881 ;; Override certain group mode keys.
882 (gnus-define-keys gnus-topic-mode-map
883 "=" gnus-topic-select-group
884 "\r" gnus-topic-select-group
885 " " gnus-topic-read-group
886 "\C-k" gnus-topic-kill-group
887 "\C-y" gnus-topic-yank-group
888 "\M-g" gnus-topic-get-new-news-this-topic
889 "AT" gnus-topic-list-active
890 "Gp" gnus-topic-edit-parameters
891 "#" gnus-topic-mark-topic
892 "\M-#" gnus-topic-unmark-topic
893 gnus-mouse-2 gnus-mouse-pick-topic)
895 ;; Define a new submap.
896 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
897 "#" gnus-topic-mark-topic
898 "\M-#" gnus-topic-unmark-topic
899 "n" gnus-topic-create-topic
900 "m" gnus-topic-move-group
901 "D" gnus-topic-remove-group
902 "c" gnus-topic-copy-group
903 "h" gnus-topic-hide-topic
904 "s" gnus-topic-show-topic
905 "M" gnus-topic-move-matching
906 "C" gnus-topic-copy-matching
907 "\C-i" gnus-topic-indent
908 [tab] gnus-topic-indent
909 "r" gnus-topic-rename
910 "\177" gnus-topic-delete
911 [delete] gnus-topic-delete
912 "h" gnus-topic-toggle-display-empty-topics)
914 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
915 "s" gnus-topic-sort-groups
916 "a" gnus-topic-sort-groups-by-alphabet
917 "u" gnus-topic-sort-groups-by-unread
918 "l" gnus-topic-sort-groups-by-level
919 "v" gnus-topic-sort-groups-by-score
920 "r" gnus-topic-sort-groups-by-rank
921 "m" gnus-topic-sort-groups-by-method))
923 (defun gnus-topic-make-menu-bar ()
924 (unless (boundp 'gnus-topic-menu)
926 gnus-topic-menu gnus-topic-mode-map ""
928 ["Toggle topics" gnus-topic-mode t]
930 ["Copy" gnus-topic-copy-group t]
931 ["Move" gnus-topic-move-group t]
932 ["Remove" gnus-topic-remove-group t]
933 ["Copy matching" gnus-topic-copy-matching t]
934 ["Move matching" gnus-topic-move-matching t])
936 ["Show" gnus-topic-show-topic t]
937 ["Hide" gnus-topic-hide-topic t]
938 ["Delete" gnus-topic-delete t]
939 ["Rename" gnus-topic-rename t]
940 ["Create" gnus-topic-create-topic t]
941 ["Mark" gnus-topic-mark-topic t]
942 ["Indent" gnus-topic-indent t]
943 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
944 ["Edit parameters" gnus-topic-edit-parameters t])
945 ["List active" gnus-topic-list-active t]))))
947 (defun gnus-topic-mode (&optional arg redisplay)
948 "Minor mode for topicsifying Gnus group buffers."
949 (interactive (list current-prefix-arg t))
950 (when (eq major-mode 'gnus-group-mode)
951 (make-local-variable 'gnus-topic-mode)
952 (setq gnus-topic-mode
953 (if (null arg) (not gnus-topic-mode)
954 (> (prefix-numeric-value arg) 0)))
955 ;; Infest Gnus with topics.
956 (if (not gnus-topic-mode)
957 (setq gnus-goto-missing-group-function nil)
958 (when (gnus-visual-p 'topic-menu 'menu)
959 (gnus-topic-make-menu-bar))
960 (gnus-set-format 'topic t)
961 (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
962 (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
963 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
964 (set (make-local-variable 'gnus-group-prepare-function)
965 'gnus-group-prepare-topics)
966 (set (make-local-variable 'gnus-group-get-parameter-function)
967 'gnus-group-topic-parameters)
968 (set (make-local-variable 'gnus-group-goto-next-group-function)
969 'gnus-topic-goto-next-group)
970 (set (make-local-variable 'gnus-group-indentation-function)
971 'gnus-topic-group-indentation)
972 (set (make-local-variable 'gnus-group-update-group-function)
973 'gnus-topic-update-topics-containing-group)
974 (set (make-local-variable 'gnus-group-sort-alist-function)
975 'gnus-group-sort-topic)
976 (setq gnus-group-change-level-function 'gnus-topic-change-level)
977 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
978 (make-local-hook 'gnus-check-bogus-groups-hook)
979 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
980 (setq gnus-topology-checked-p nil)
981 ;; We check the topology.
982 (when gnus-newsrc-alist
983 (gnus-topic-check-topology))
984 (gnus-run-hooks 'gnus-topic-mode-hook))
985 ;; Remove topic infestation.
986 (unless gnus-topic-mode
987 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
988 (remove-hook 'gnus-group-change-level-function
989 'gnus-topic-change-level)
990 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
991 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
992 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
994 (gnus-group-list-groups))))
996 (defun gnus-topic-select-group (&optional all)
997 "Select this newsgroup.
998 No article is selected automatically.
999 If ALL is non-nil, already read articles become readable.
1000 If ALL is a number, fetch this number of articles.
1002 If performed over a topic line, toggle folding the topic."
1004 (if (gnus-group-topic-p)
1005 (let ((gnus-group-list-mode
1006 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1007 (gnus-topic-fold all))
1008 (gnus-group-select-group all)))
1010 (defun gnus-mouse-pick-topic (e)
1011 "Select the group or topic under the mouse pointer."
1014 (gnus-topic-read-group nil))
1016 (defun gnus-topic-read-group (&optional all no-article group)
1017 "Read news in this newsgroup.
1018 If the prefix argument ALL is non-nil, already read articles become
1019 readable. IF ALL is a number, fetch this number of articles. If the
1020 optional argument NO-ARTICLE is non-nil, no article will be
1021 auto-selected upon group entry. If GROUP is non-nil, fetch that
1024 If performed over a topic line, toggle folding the topic."
1026 (if (gnus-group-topic-p)
1027 (let ((gnus-group-list-mode
1028 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1029 (gnus-topic-fold all))
1030 (gnus-group-read-group all no-article group)))
1032 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1033 "Create a new TOPIC under PARENT.
1034 When used interactively, PARENT will be the topic under point."
1037 (read-string "New topic: ")
1038 (gnus-current-topic)))
1039 ;; Check whether this topic already exists.
1040 (when (gnus-topic-find-topology topic)
1041 (error "Topic already exists"))
1043 (setq parent (caar gnus-topic-topology)))
1044 (let ((top (cdr (gnus-topic-find-topology parent)))
1045 (full-topic (or full-topic `((,topic visible)))))
1047 (error "No such parent topic: %s" parent))
1050 (while (and (cdr top)
1051 (not (equal (caaadr top) previous)))
1052 (setq top (cdr top)))
1053 (setcdr top (cons full-topic (cdr top))))
1054 (nconc top (list full-topic)))
1055 (unless (assoc topic gnus-topic-alist)
1056 (push (list topic) gnus-topic-alist)))
1057 (gnus-topic-enter-dribble)
1058 (gnus-group-list-groups)
1059 (gnus-topic-goto-topic topic))
1061 (defun gnus-topic-move-group (n topic &optional copyp)
1062 "Move the next N groups to TOPIC.
1063 If COPYP, copy the groups instead."
1065 (list current-prefix-arg
1066 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1067 (let ((groups (gnus-group-process-prefix n))
1068 (topicl (assoc topic gnus-topic-alist))
1069 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1070 (start-topic (gnus-group-topic-name))
1074 (gnus-group-remove-mark g)
1076 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1078 (setcdr entry (gnus-delete-first g (cdr entry))))
1079 (nconc topicl (list g)))
1081 (gnus-topic-enter-dribble)
1083 (gnus-group-goto-group start-group)
1084 (gnus-topic-goto-topic start-topic))
1085 (gnus-group-list-groups)))
1087 (defun gnus-topic-remove-group (&optional arg)
1088 "Remove the current group from the topic."
1090 (gnus-group-iterate arg
1092 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1093 (buffer-read-only nil))
1094 (when (and topicl group)
1096 (gnus-delete-first group topicl))
1097 (gnus-topic-update-topic)
1098 (gnus-group-position-point)))))
1100 (defun gnus-topic-copy-group (n topic)
1101 "Copy the current group to a topic."
1103 (list current-prefix-arg
1104 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1105 (gnus-topic-move-group n topic t))
1107 (defun gnus-topic-kill-group (&optional n discard)
1108 "Kill the next N groups."
1110 (if (gnus-group-topic-p)
1111 (let ((topic (gnus-group-topic-name)))
1113 (gnus-topic-find-topology topic)
1114 (assoc topic gnus-topic-alist))
1115 gnus-topic-killed-topics)
1116 (gnus-topic-remove-topic nil t)
1117 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1118 (gnus-topic-enter-dribble))
1119 (gnus-group-kill-group n discard)
1120 (gnus-topic-update-topic)))
1122 (defun gnus-topic-yank-group (&optional arg)
1123 "Yank the last topic."
1125 (if gnus-topic-killed-topics
1127 (or (gnus-group-topic-name)
1128 (gnus-topic-next-topic (gnus-current-topic))))
1129 (data (pop gnus-topic-killed-topics))
1132 (push alist gnus-topic-alist)
1133 (gnus-topic-create-topic
1134 (caar item) (gnus-topic-parent-topic previous) previous
1136 (gnus-topic-enter-dribble)
1137 (gnus-topic-goto-topic (caar item)))
1138 (let* ((prev (gnus-group-group-name))
1139 (gnus-topic-inhibit-change-level t)
1140 (gnus-group-indentation
1142 (* gnus-topic-indent-level
1144 (gnus-topic-goto-topic (gnus-current-topic))
1145 (gnus-group-topic-level))
1149 ;; We first yank the groups the normal way...
1150 (setq yanked (gnus-group-yank-group arg))
1151 ;; Then we enter the yanked groups into the topics they belong
1153 (setq alist (assoc (save-excursion
1155 (gnus-current-topic))
1157 (when (stringp yanked)
1158 (setq yanked (list yanked)))
1160 (nconc alist yanked)
1161 (if (not (cdr alist))
1162 (setcdr alist (nconc yanked (cdr alist)))
1164 (when (equal (cadr alist) prev)
1165 (setcdr alist (nconc yanked (cdr alist)))
1167 (setq alist (cdr alist))))))
1168 (gnus-topic-update-topic)))
1170 (defun gnus-topic-hide-topic ()
1171 "Hide the current topic."
1173 (when (gnus-current-topic)
1174 (gnus-topic-goto-topic (gnus-current-topic))
1175 (gnus-topic-remove-topic nil nil 'hidden)))
1177 (defun gnus-topic-show-topic ()
1178 "Show the hidden topic."
1180 (when (gnus-group-topic-p)
1181 (gnus-topic-remove-topic t nil 'shown)))
1183 (defun gnus-topic-mark-topic (topic &optional unmark)
1184 "Mark all groups in the topic with the process mark."
1185 (interactive (list (gnus-group-topic-name)))
1187 (call-interactively 'gnus-group-mark-group)
1189 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
1191 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1192 (gnus-info-group (nth 2 (pop groups)))))))))
1194 (defun gnus-topic-unmark-topic (topic &optional unmark)
1195 "Remove the process mark from all groups in the topic."
1196 (interactive (list (gnus-group-topic-name)))
1198 (call-interactively 'gnus-group-unmark-group)
1199 (gnus-topic-mark-topic topic t)))
1201 (defun gnus-topic-get-new-news-this-topic (&optional n)
1202 "Check for new news in the current topic."
1204 (if (not (gnus-group-topic-p))
1205 (gnus-group-get-new-news-this-group n)
1206 (gnus-topic-mark-topic (gnus-group-topic-name))
1207 (gnus-group-get-new-news-this-group)))
1209 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1210 "Move all groups that match REGEXP to some topic."
1215 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1216 (read-string (format "Move to %s (regexp): " topic))))))
1217 (gnus-group-mark-regexp regexp)
1218 (gnus-topic-move-group nil topic copyp))
1220 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1221 "Copy all groups that match REGEXP to some topic."
1226 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1227 (read-string (format "Copy to %s (regexp): " topic))))))
1228 (gnus-topic-move-matching regexp topic t))
1230 (defun gnus-topic-delete (topic)
1232 (interactive (list (gnus-group-topic-name)))
1234 (error "No topic to be deleted"))
1235 (let ((entry (assoc topic gnus-topic-alist))
1236 (buffer-read-only nil))
1238 (error "Topic not empty"))
1239 ;; Delete if visible.
1240 (when (gnus-topic-goto-topic topic)
1242 ;; Remove from alist.
1243 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1244 ;; Remove from topology.
1245 (gnus-topic-find-topology topic nil nil 'delete)
1246 (gnus-dribble-touch)))
1248 (defun gnus-topic-rename (old-name new-name)
1251 (let ((topic (gnus-current-topic)))
1253 (read-string (format "Rename %s to: " topic)))))
1254 ;; Check whether the new name exists.
1255 (when (gnus-topic-find-topology new-name)
1256 (error "Topic '%s' already exists"))
1258 (let ((top (gnus-topic-find-topology old-name))
1259 (entry (assoc old-name gnus-topic-alist)))
1261 (setcar (cadr top) new-name))
1263 (setcar entry new-name))
1265 (gnus-dribble-touch)
1266 (gnus-group-list-groups)))
1268 (defun gnus-topic-indent (&optional unindent)
1269 "Indent a topic -- make it a sub-topic of the previous topic.
1270 If UNINDENT, remove an indentation."
1273 (gnus-topic-unindent)
1274 (let* ((topic (gnus-current-topic))
1275 (parent (gnus-topic-previous-topic topic))
1276 (buffer-read-only nil))
1278 (error "Nothing to indent %s into" topic))
1280 (gnus-topic-goto-topic topic)
1281 (gnus-topic-kill-group)
1282 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1283 (gnus-topic-create-topic
1284 topic parent nil (cdaar gnus-topic-killed-topics))
1285 (pop gnus-topic-killed-topics)
1286 (or (gnus-topic-goto-topic topic)
1287 (gnus-topic-goto-topic parent))))))
1289 (defun gnus-topic-unindent ()
1292 (let* ((topic (gnus-current-topic))
1293 (parent (gnus-topic-parent-topic topic))
1294 (grandparent (gnus-topic-parent-topic parent)))
1296 (error "Nothing to indent %s into" topic))
1298 (gnus-topic-goto-topic topic)
1299 (gnus-topic-kill-group)
1300 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1301 (gnus-topic-create-topic
1302 topic grandparent (gnus-topic-next-topic parent)
1303 (cdaar gnus-topic-killed-topics))
1304 (pop gnus-topic-killed-topics)
1305 (gnus-topic-goto-topic topic))))
1307 (defun gnus-topic-list-active (&optional force)
1308 "List all groups that Gnus knows about in a topicsified fashion.
1309 If FORCE, always re-read the active file."
1312 (gnus-get-killed-groups))
1313 (gnus-topic-grok-active force)
1314 (let ((gnus-topic-topology gnus-topic-active-topology)
1315 (gnus-topic-alist gnus-topic-active-alist)
1316 gnus-killed-list gnus-zombie-list)
1317 (gnus-group-list-groups gnus-level-killed nil 1)))
1319 (defun gnus-topic-toggle-display-empty-topics ()
1320 "Show/hide topics that have no unread articles."
1322 (setq gnus-topic-display-empty-topics
1323 (not gnus-topic-display-empty-topics))
1324 (gnus-group-list-groups)
1325 (message "%s empty topics"
1326 (if gnus-topic-display-empty-topics
1327 "Showing" "Hiding")))
1329 ;;; Topic sorting functions
1331 (defun gnus-topic-edit-parameters (group)
1332 "Edit the group parameters of GROUP.
1333 If performed on a topic, edit the topic parameters instead."
1334 (interactive (list (gnus-group-group-name)))
1336 (gnus-group-edit-group-parameters group)
1337 (if (not (gnus-group-topic-p))
1338 (error "Nothing to edit on the current line")
1339 (let ((topic (gnus-group-topic-name)))
1341 (gnus-topic-parameters topic)
1342 (format "Editing the topic parameters for `%s'."
1345 (gnus-topic-set-parameters ,topic form)))))))
1347 (defun gnus-group-sort-topic (func reverse)
1348 "Sort groups in the topics according to FUNC and REVERSE."
1349 (let ((alist gnus-topic-alist))
1351 ;; !!!Sometimes nil elements sneak into the alist,
1352 ;; for some reason or other.
1353 (setcar alist (delq nil (car alist)))
1354 (setcar alist (delete "dummy.group" (car alist)))
1355 (gnus-topic-sort-topic (pop alist) func reverse))))
1357 (defun gnus-topic-sort-topic (topic func reverse)
1358 ;; Each topic only lists the name of the group, while
1359 ;; the sort predicates expect group infos as inputs.
1360 ;; So we first transform the group names into infos,
1361 ;; then sort, and then transform back into group names.
1365 (lambda (info) (gnus-info-group info))
1368 (lambda (group) (gnus-get-info group))
1371 ;; Do the reversal, if necessary.
1373 (setcdr topic (nreverse (cdr topic)))))
1375 (defun gnus-topic-sort-groups (func &optional reverse)
1376 "Sort the current topic according to FUNC.
1377 If REVERSE, reverse the sorting order."
1378 (interactive (list gnus-group-sort-function current-prefix-arg))
1379 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1380 (gnus-topic-sort-topic
1381 topic (gnus-make-sort-function func) reverse)
1382 (gnus-group-list-groups)))
1384 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1385 "Sort the current topic alphabetically by group name.
1386 If REVERSE, sort in reverse order."
1388 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1390 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1391 "Sort the current topic by number of unread articles.
1392 If REVERSE, sort in reverse order."
1394 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1396 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1397 "Sort the current topic by group level.
1398 If REVERSE, sort in reverse order."
1400 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1402 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1403 "Sort the current topic by group score.
1404 If REVERSE, sort in reverse order."
1406 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1408 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1409 "Sort the current topic by group rank.
1410 If REVERSE, sort in reverse order."
1412 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1414 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1415 "Sort the current topic alphabetically by backend name.
1416 If REVERSE, sort in reverse order."
1418 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1420 (provide 'gnus-topic)
1422 ;;; gnus-topic.el ends here