1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Ilja Weis <kult@uni-paderborn.de>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
37 (defgroup gnus-topic nil
41 (defvar gnus-topic-mode nil
42 "Minor mode for Gnus group buffers.")
44 (defcustom gnus-topic-mode-hook nil
45 "Hook run in topic mode buffers."
49 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
50 "Format of topic lines.
51 It works along the same lines as a normal formatting string,
52 with some simple extensions.
54 %i Indentation based on topic level.
56 %v Nothing if the topic is visible, \"...\" otherwise.
57 %g Number of groups in the topic.
58 %a Number of unread articles in the groups in the topic.
59 %A Number of unread articles in the groups in the topic and its subtopics.
64 (defcustom gnus-topic-indent-level 2
65 "*How much each subtopic should be indented."
69 (defcustom gnus-topic-display-empty-topics t
70 "*If non-nil, display the topic lines even of topics that have no unread articles."
74 ;; Internal variables.
76 (defvar gnus-topic-active-topology nil)
77 (defvar gnus-topic-active-alist nil)
78 (defvar gnus-topic-unreads nil)
80 (defvar gnus-topology-checked-p nil
81 "Whether the topology has been checked in this session.")
83 (defvar gnus-topic-killed-topics nil)
84 (defvar gnus-topic-inhibit-change-level nil)
86 (defconst gnus-topic-line-format-alist
90 (?g number-of-groups ?d)
91 (?a (gnus-topic-articles-in-topic entries) ?d)
92 (?A total-number-of-articles ?d)
95 (defvar gnus-topic-line-format-spec nil)
99 (defun gnus-group-topic-name ()
100 "The name of the topic on the current line."
101 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
102 (and topic (symbol-name topic))))
104 (defun gnus-group-topic-level ()
105 "The level of the topic on the current line."
106 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
108 (defun gnus-group-topic-unread ()
109 "The number of unread articles in topic on the current line."
110 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
112 (defun gnus-topic-unread (topic)
113 "Return the number of unread articles in TOPIC."
114 (or (cdr (assoc topic gnus-topic-unreads))
117 (defun gnus-group-topic-p ()
118 "Return non-nil if the current line is a topic."
119 (gnus-group-topic-name))
121 (defun gnus-topic-visible-p ()
122 "Return non-nil if the current topic is visible."
123 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
125 (defun gnus-topic-articles-in-topic (entries)
129 (when (numberp (setq number (car (pop entries))))
130 (incf total number)))
133 (defun gnus-group-topic (group)
134 "Return the topic GROUP is a member of."
135 (let ((alist gnus-topic-alist)
138 (when (member group (cdar alist))
139 (setq out (caar alist)
141 (setq alist (cdr alist)))
144 (defun gnus-group-parent-topic (group)
145 "Return the topic GROUP is member of by looking at the group buffer."
147 (set-buffer gnus-group-buffer)
148 (if (gnus-group-goto-group group)
150 (gnus-group-topic group))))
152 (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-topic-jump-to-topic (topic)
160 (list (completing-read "Go to topic: "
161 (mapcar 'list (gnus-topic-list))
163 (dolist (topic (gnus-current-topics topic))
165 (gnus-topic-goto-topic topic))
167 (defun gnus-current-topic ()
168 "Return the name of the current topic."
170 (or (get-text-property (point) 'gnus-topic)
172 (and (gnus-goto-char (previous-single-property-change
173 (point) 'gnus-topic))
174 (get-text-property (max (1- (point)) (point-min))
177 (symbol-name result))))
179 (defun gnus-current-topics (&optional topic)
180 "Return a list of all current topics, lowest in hierarchy first.
181 If TOPIC, start with that topic."
182 (let ((topic (or topic (gnus-current-topic)))
186 (setq topic (gnus-topic-parent-topic topic)))
189 (defun gnus-group-active-topic-p ()
190 "Say whether the current topic comes from the active topics."
193 (get-text-property (point) 'gnus-active)))
195 (defun gnus-topic-find-groups (topic &optional level all lowest)
196 "Return entries for all visible groups in TOPIC."
197 (let ((groups (cdr (assoc topic gnus-topic-alist)))
198 info clevel unread group params visible-groups entry active)
199 (setq lowest (or lowest 1))
200 (setq level (or level gnus-level-unsubscribed))
201 ;; We go through the newsrc to look for matches.
203 (when (setq group (pop groups))
204 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
206 params (gnus-info-params info)
207 active (gnus-active group)
208 unread (or (car entry)
209 (and (not (equal group "dummy.group"))
211 (- (1+ (cdr active)) (car active))))
212 clevel (or (gnus-info-level info)
213 (if (member group gnus-zombie-list)
214 gnus-level-zombie gnus-level-killed))))
216 info ; nil means that the group is dead.
218 (>= clevel lowest) ; Is inside the level we want.
220 (if (or (eq unread t)
222 gnus-group-list-inactive-groups
224 (and gnus-list-groups-with-ticked-articles
225 (cdr (assq 'tick (gnus-info-marks info))))
226 ;; Has right readedness.
227 ;; Check for permanent visibility.
228 (and gnus-permanently-visible-groups
229 (string-match gnus-permanently-visible-groups group))
230 (memq 'visible params)
231 (cdr (assq 'visible params)))
232 ;; Add this group to the list of visible groups.
233 (push (or entry group) visible-groups)))
234 (nreverse visible-groups)))
236 (defun gnus-topic-previous-topic (topic)
237 "Return the previous topic on the same level as TOPIC."
238 (let ((top (cddr (gnus-topic-find-topology
239 (gnus-topic-parent-topic topic)))))
240 (unless (equal topic (caaar top))
241 (while (and top (not (equal (caaadr top) topic)))
242 (setq top (cdr top)))
245 (defun gnus-topic-parent-topic (topic &optional topology)
246 "Return the parent of TOPIC."
248 (setq topology gnus-topic-topology))
249 (let ((parent (car (pop topology)))
252 (not (setq found (equal (caaar topology) topic)))
253 (not (setq result (gnus-topic-parent-topic
254 topic (car topology)))))
255 (setq topology (cdr topology)))
256 (or result (and found parent))))
258 (defun gnus-topic-next-topic (topic &optional previous)
259 "Return the next sibling of TOPIC."
260 (let ((parentt (cddr (gnus-topic-find-topology
261 (gnus-topic-parent-topic topic))))
264 (not (equal (caaar parentt) topic)))
265 (setq prev (caaar parentt)
266 parentt (cdr parentt)))
271 (defun gnus-topic-forward-topic (num)
272 "Go to the next topic on the same level as the current one."
273 (let* ((topic (gnus-current-topic))
274 (way (if (< num 0) 'gnus-topic-previous-topic
275 'gnus-topic-next-topic))
277 (while (and (not (zerop num))
278 (setq topic (funcall way topic)))
279 (when (gnus-topic-goto-topic topic)
282 (goto-char (point-max)))
285 (defun gnus-topic-find-topology (topic &optional topology level remove)
286 "Return the topology of TOPIC."
288 (setq topology gnus-topic-topology)
292 (if (equal (caar topology) topic)
295 (delq topology remove))
296 (cons level topology))
297 (setq topology (cdr topology))
299 (not (setq result (gnus-topic-find-topology
300 topic (car topology) (1+ level)
302 (setq topology (cdr topology)))
305 (defvar gnus-tmp-topics nil)
306 (defun gnus-topic-list (&optional topology)
307 "Return a list of all topics in the topology."
309 (setq topology gnus-topic-topology
310 gnus-tmp-topics nil))
311 (push (caar topology) gnus-tmp-topics)
312 (mapcar 'gnus-topic-list (cdr topology))
315 ;;; Topic parameter jazz
317 (defun gnus-topic-parameters (topic)
318 "Return the parameters for TOPIC."
319 (let ((top (gnus-topic-find-topology topic)))
321 (nth 3 (cadr top)))))
323 (defun gnus-topic-set-parameters (topic parameters)
324 "Set the topic parameters of TOPIC to PARAMETERS."
325 (let ((top (gnus-topic-find-topology topic)))
327 (error "No such topic: %s" topic))
328 ;; We may have to extend if there is no parameters here
330 (unless (nthcdr 2 (cadr top))
331 (nconc (cadr top) (list nil)))
332 (unless (nthcdr 3 (cadr top))
333 (nconc (cadr top) (list nil)))
334 (setcar (nthcdr 3 (cadr top)) parameters)
336 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
338 (defun gnus-group-topic-parameters (group)
339 "Compute the group parameters for GROUP taking into account inheritance from topics."
340 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
342 (gnus-group-goto-group group)
344 (gnus-topic-hierarchical-parameters (gnus-current-topic))))))
346 (defun gnus-topic-hierarchical-parameters (topic)
347 "Return a topic list computed for TOPIC."
348 (let ((topics (gnus-current-topics topic))
349 params-list param out params)
351 (push (gnus-topic-parameters (pop topics)) params-list))
352 ;; We probably have lots of nil elements here, so
353 ;; we remove them. Probably faster than doing this "properly".
354 (setq params-list (delq nil params-list))
355 ;; Now we have all the parameters, so we go through them
356 ;; and do inheritance in the obvious way.
357 (while (setq params (pop params-list))
358 (while (setq param (pop params))
360 (setq param (cons param t)))
361 ;; Override any old versions of this param.
362 (gnus-pull (car param) out)
364 ;; Return the resulting parameter list.
367 ;;; General utility functions
369 (defun gnus-topic-enter-dribble ()
371 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
373 ;;; Generating group buffers
375 (defun gnus-group-prepare-topics (level &optional all lowest
376 regexp list-topic topic-level)
377 "List all newsgroups with unread articles of level LEVEL or lower.
378 Use the `gnus-group-topics' to sort the groups.
379 If ALL is non-nil, list groups that have no unread articles.
380 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
381 (set-buffer gnus-group-buffer)
382 (let ((buffer-read-only nil)
383 (lowest (or lowest 1)))
385 (when (or (not gnus-topic-alist)
386 (not gnus-topology-checked-p))
387 (gnus-topic-check-topology))
393 (when (and (>= level gnus-level-zombie)
394 (<= lowest gnus-level-zombie))
395 (gnus-group-prepare-flat-list-dead
396 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
400 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
401 (gnus-group-prepare-flat-list-dead
402 (setq gnus-killed-list (sort gnus-killed-list 'string<))
408 (when (< lowest gnus-level-zombie)
410 (let ((top (gnus-topic-find-topology list-topic)))
411 (gnus-topic-prepare-topic (cdr top) (car top)
412 (or topic-level level) all
414 (gnus-topic-prepare-topic gnus-topic-topology 0
415 (or topic-level level) all
418 (gnus-group-set-mode-line)
419 (setq gnus-group-list-mode (cons level all))
420 (gnus-run-hooks 'gnus-group-prepare-hook))))
422 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent
424 "Insert TOPIC into the group buffer.
425 If SILENT, don't insert anything. Return the number of unread
426 articles in the topic and its subtopics."
427 (let* ((type (pop topicl))
428 (entries (gnus-topic-find-groups
429 (car type) list-level
432 (gnus-topic-hierarchical-parameters
435 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
436 (gnus-group-indentation
437 (make-string (* gnus-topic-indent-level level) ? ))
438 (beg (progn (beginning-of-line) (point)))
439 (topicl (reverse topicl))
440 (all-entries entries)
441 (point-max (point-max))
444 info entry end active tick)
445 ;; Insert any sub-topics.
448 (gnus-topic-prepare-topic
449 (pop topicl) (1+ level) list-level all
450 (not visiblep) lowest)))
453 ;; Insert all the groups that belong in this topic.
454 (while (setq entry (pop entries))
458 (gnus-group-insert-group-line
459 entry (if (member entry gnus-zombie-list)
460 gnus-level-zombie gnus-level-killed)
461 nil (- (1+ (cdr (setq active (gnus-active entry))))
465 (when (setq info (nth 2 entry))
466 (gnus-group-insert-group-line
467 (gnus-info-group info)
468 (gnus-info-level info) (gnus-info-marks info)
469 (car entry) (gnus-info-method info)))))
470 (when (and (listp entry)
471 (numberp (car entry)))
472 (incf unread (car entry)))
476 ;; Insert the topic line.
477 (when (and (not silent)
478 (or gnus-topic-display-empty-topics ;We want empty topics
479 (not (zerop unread)) ;Non-empty
480 tick ;Ticked articles
481 (/= point-max (point-max)))) ;Unactivated groups
482 (gnus-extent-start-open (point))
483 (gnus-topic-insert-topic-line
485 (not (eq (nth 2 type) 'hidden))
486 level all-entries unread))
487 (gnus-topic-update-unreads (car type) unread)
491 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
492 "Remove the current topic."
493 (let ((topic (gnus-group-topic-name))
494 (level (gnus-group-topic-level))
495 (beg (progn (beginning-of-line) (point)))
498 (while (and (zerop (forward-line 1))
499 (> (or (gnus-group-topic-level) (1+ level)) level)))
500 (delete-region beg (point))
501 ;; Do the change in this rather odd manner because it has been
502 ;; reported that some topics share parts of some lists, for some
503 ;; reason. I have been unable to determine why this is the
504 ;; case, but this hack seems to take care of things.
505 (let ((data (cadr (gnus-topic-find-topology topic))))
507 (list (if insert 'visible 'invisible)
511 (setq gnus-topic-alist
512 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
513 (gnus-topic-insert-topic topic in-level)))))
515 (defun gnus-topic-insert-topic (topic &optional level)
517 (gnus-group-prepare-topics
518 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
519 nil nil topic level))
521 (defun gnus-topic-fold (&optional insert topic)
522 "Remove/insert the current topic."
523 (let ((topic (or topic (gnus-group-topic-name))))
526 (if (not (gnus-group-active-topic-p))
527 (gnus-topic-remove-topic
528 (or insert (not (gnus-topic-visible-p))))
529 (let ((gnus-topic-topology gnus-topic-active-topology)
530 (gnus-topic-alist gnus-topic-active-alist)
531 (gnus-group-list-mode (cons 5 t)))
532 (gnus-topic-remove-topic
533 (or insert (not (gnus-topic-visible-p))) nil nil 9)
534 (gnus-topic-enter-dribble)))))))
536 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
538 (let* ((visible (if visiblep "" "..."))
539 (indentation (make-string (* gnus-topic-indent-level level) ? ))
540 (total-number-of-articles unread)
541 (number-of-groups (length entries))
542 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
544 (gnus-topic-update-unreads name unread)
547 (gnus-add-text-properties
550 (eval gnus-topic-line-format-spec))
551 (list 'gnus-topic (intern name)
552 'gnus-topic-level level
553 'gnus-topic-unread unread
554 'gnus-active active-topic
555 'gnus-topic-visible visiblep))))
557 (defun gnus-topic-update-unreads (topic unreads)
558 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
560 (push (cons topic unreads) gnus-topic-unreads))
562 (defun gnus-topic-update-topics-containing-group (group)
563 "Update all topics that have GROUP as a member."
564 (when (and (eq major-mode 'gnus-group-mode)
567 (let ((alist gnus-topic-alist))
568 ;; This is probably not entirely correct. If a topic
569 ;; isn't shown, then it's not updated. But the updating
570 ;; should be performed in any case, since the topic's
571 ;; parent should be updated. Pfft.
573 (when (and (member group (cdar alist))
574 (gnus-topic-goto-topic (caar alist)))
575 (gnus-topic-update-topic-line (caar alist)))
578 (defun gnus-topic-update-topic ()
579 "Update all parent topics to the current group."
580 (when (and (eq major-mode 'gnus-group-mode)
582 (let ((group (gnus-group-group-name))
584 (buffer-read-only nil))
586 (gnus-get-info group)
587 (gnus-topic-goto-topic (gnus-current-topic)))
588 (gnus-topic-update-topic-line (gnus-group-topic-name))
591 (gnus-group-position-point)))))
593 (defun gnus-topic-goto-missing-group (group)
594 "Place point where GROUP is supposed to be inserted."
595 (let* ((topic (gnus-group-topic group))
596 (groups (cdr (assoc topic gnus-topic-alist)))
597 (g (cdr (member group groups)))
600 ;; Try to jump to a visible group.
601 (while (and g (not (gnus-group-goto-group (car g) t)))
603 ;; It wasn't visible, so we try to see where to insert it.
605 (setq g (cdr (member group (reverse groups))))
606 (while (and g unfound)
607 (when (gnus-group-goto-group (pop g) t)
612 (not (gnus-topic-goto-missing-topic topic)))
613 (let* ((top (gnus-topic-find-topology topic))
614 (children (cddr top))
617 (entries (gnus-topic-find-groups
618 (car type) (car gnus-group-list-mode)
619 (cdr gnus-group-list-mode))))
621 (incf unread (gnus-topic-unread (caar (pop children)))))
622 (while (setq entry (pop entries))
623 (when (numberp (car entry))
624 (incf unread (car entry))))
625 (gnus-topic-insert-topic-line
626 topic t t (car (gnus-topic-find-topology topic)) nil unread))))))
628 (defun gnus-topic-goto-missing-topic (topic)
629 (if (gnus-topic-goto-topic topic)
631 ;; Topic not displayed.
632 (let* ((top (gnus-topic-find-topology
633 (gnus-topic-parent-topic topic)))
634 (tp (reverse (cddr top))))
636 (gnus-topic-insert-topic-line
637 topic t t (car (gnus-topic-find-topology topic)) nil 0)
638 (while (not (equal (caaar tp) topic))
642 (not (gnus-topic-goto-topic (caaar tp))))
645 (gnus-topic-forward-topic 1)
646 (gnus-topic-goto-missing-topic (caadr top)))))
649 (defun gnus-topic-update-topic-line (topic-name &optional reads)
650 (let* ((top (gnus-topic-find-topology topic-name))
652 (children (cddr top))
653 (entries (gnus-topic-find-groups
654 (car type) (car gnus-group-list-mode)
655 (cdr gnus-group-list-mode)))
656 (parent (gnus-topic-parent-topic topic-name))
657 (all-entries entries)
659 old-unread entry new-unread)
660 (when (gnus-topic-goto-topic (car type))
661 ;; Tally all the groups that belong in this topic.
663 (setq unread (- (gnus-group-topic-unread) reads))
665 (incf unread (gnus-topic-unread (caar (pop children)))))
666 (while (setq entry (pop entries))
667 (when (numberp (car entry))
668 (incf unread (car entry)))))
669 (setq old-unread (gnus-group-topic-unread))
670 ;; Insert the topic line.
671 (gnus-topic-insert-topic-line
672 (car type) (gnus-topic-visible-p)
673 (not (eq (nth 2 type) 'hidden))
674 (gnus-group-topic-level) all-entries unread)
677 (setq new-unread (gnus-group-topic-unread)))
680 (gnus-topic-update-topic-line
682 (- (or old-unread 0) (or new-unread 0))))
685 (defun gnus-topic-group-indentation ()
687 (* gnus-topic-indent-level
690 (gnus-topic-goto-topic (gnus-current-topic))
691 (gnus-group-topic-level))
697 (gnus-add-shutdown 'gnus-topic-close 'gnus)
699 (defun gnus-topic-close ()
700 (setq gnus-topic-active-topology nil
701 gnus-topic-active-alist nil
702 gnus-topic-killed-topics nil
703 gnus-topology-checked-p nil))
705 (defun gnus-topic-check-topology ()
706 ;; The first time we set the topology to whatever we have
707 ;; gotten here, which can be rather random.
708 (unless gnus-topic-alist
709 (gnus-topic-init-alist))
711 (setq gnus-topology-checked-p t)
712 ;; Go through the topic alist and make sure that all topics
713 ;; are in the topic topology.
714 (let ((topics (gnus-topic-list))
715 (alist gnus-topic-alist)
718 (unless (member (caar alist) topics)
719 (nconc gnus-topic-topology
720 (list (list (list (caar alist) 'visible))))
722 (setq alist (cdr alist)))
724 (gnus-topic-enter-dribble))
725 ;; Conversely, go through the topology and make sure that all
726 ;; topologies have alists.
728 (unless (assoc (car topics) gnus-topic-alist)
729 (push (list (car topics)) gnus-topic-alist))
731 ;; Go through all living groups and make sure that
732 ;; they belong to some topic.
733 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
735 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
736 (newsrc (cdr gnus-newsrc-alist))
739 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
740 (setcdr entry (list group))
741 (setq entry (cdr entry)))))
742 ;; Go through all topics and make sure they contain only living groups.
743 (let ((alist gnus-topic-alist)
745 (while (setq topic (pop alist))
747 (if (and (cadr topic)
748 (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
749 (setq topic (cdr topic))
750 (setcdr topic (cddr topic)))))))
752 (defun gnus-topic-init-alist ()
753 "Initialize the topic structures."
754 (setq gnus-topic-topology
755 (cons (list "Gnus" 'visible)
756 (mapcar (lambda (topic)
757 (list (list (car topic) 'visible)))
759 (setq gnus-topic-alist
761 (mapcar (lambda (info) (gnus-info-group info))
762 (cdr gnus-newsrc-alist)))
764 (gnus-topic-enter-dribble))
768 (defun gnus-topic-clean-alist ()
769 "Remove bogus groups from the topic alist."
770 (let ((topic-alist gnus-topic-alist)
772 (unless gnus-killed-hashtb
773 (gnus-make-hashtable-from-killed))
774 (while (setq topic (pop topic-alist))
775 (let ((topic-name (pop topic))
776 group filtered-topic)
777 (while (setq group (pop topic))
778 (when (and (or (gnus-gethash group gnus-active-hashtb)
779 (gnus-info-method (gnus-get-info group)))
780 (not (gnus-gethash group gnus-killed-hashtb)))
781 (push group filtered-topic)))
782 (push (cons topic-name (nreverse filtered-topic)) result)))
783 (setq gnus-topic-alist (nreverse result))))
785 (defun gnus-topic-change-level (group level oldlevel &optional previous)
786 "Run when changing levels to enter/remove groups from topics."
788 (set-buffer gnus-group-buffer)
789 (let ((buffer-read-only nil))
790 (unless gnus-topic-inhibit-change-level
791 (gnus-group-goto-group (or (car (nth 2 previous)) group))
792 (when (and gnus-topic-mode
794 (not gnus-topic-inhibit-change-level))
795 ;; Remove the group from the topics.
796 (if (and (< oldlevel gnus-level-zombie)
797 (>= level gnus-level-zombie))
798 (let ((alist gnus-topic-alist))
799 (while (gnus-group-goto-group group)
802 (when (member group (car alist))
803 (setcdr (car alist) (delete group (cdar alist))))
805 ;; If the group is subscribed we enter it into the topics.
806 (when (and (< level gnus-level-zombie)
807 (>= oldlevel gnus-level-zombie))
808 (let* ((prev (gnus-group-group-name))
809 (gnus-topic-inhibit-change-level t)
810 (gnus-group-indentation
812 (* gnus-topic-indent-level
814 (gnus-topic-goto-topic (gnus-current-topic))
815 (gnus-group-topic-level))
818 (yanked (list group))
820 ;; Then we enter the yanked groups into the topics they belong
822 (when (setq alist (assoc (save-excursion
826 (caar gnus-topic-topology)))
829 (when (stringp yanked)
830 (setq yanked (list yanked)))
833 (if (not (cdr alist))
834 (setcdr alist (nconc yanked (cdr alist)))
835 (while (and (not end) (cdr alist))
836 (when (equal (cadr alist) prev)
837 (setcdr alist (nconc yanked (cdr alist)))
839 (setq alist (cdr alist)))
841 (nconc talist yanked))))))
842 (gnus-topic-update-topic))))))))
844 (defun gnus-topic-goto-next-group (group props)
845 "Go to group or the next group after group."
847 (if (not (memq 'gnus-topic props))
848 (goto-char (point-max))
849 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
850 (if (gnus-group-goto-group group)
852 ;; The group is no longer visible.
853 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
854 (after (cdr (member group (cdr list)))))
855 ;; First try to put point on a group after the current one.
857 (not (gnus-group-goto-group (car after))))
858 (setq after (cdr after)))
859 ;; Then try to put point on a group before point.
861 (setq after (cdr (member group (reverse (cdr list)))))
863 (not (gnus-group-goto-group (car after))))
864 (setq after (cdr after))))
865 ;; Finally, just put point on the topic.
867 (goto-char (point-min))
869 (gnus-topic-goto-topic (car list))
873 ;;; Topic-active functions
875 (defun gnus-topic-grok-active (&optional force)
876 "Parse all active groups and create topic structures for them."
877 ;; First we make sure that we have really read the active file.
879 (not gnus-topic-active-alist))
881 ;; Get a list of all groups available.
882 (mapatoms (lambda (g) (when (symbol-value g)
883 (push (symbol-name g) groups)))
885 (setq groups (sort groups 'string<))
886 ;; Init the variables.
887 (setq gnus-topic-active-topology (list (list "" 'visible)))
888 (setq gnus-topic-active-alist nil)
889 ;; Descend the top-level hierarchy.
890 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
891 ;; Set the top-level topic names to something nice.
892 (setcar (car gnus-topic-active-topology) "Gnus active")
893 (setcar (car gnus-topic-active-alist) "Gnus active"))))
895 (defun gnus-topic-grok-active-1 (topology groups)
896 (let* ((name (caar topology))
897 (prefix (concat "^" (regexp-quote name)))
898 tgroups ntopology group)
900 (string-match prefix (setq group (car groups))))
901 (if (not (string-match "\\." group (match-end 0)))
902 ;; There are no further hierarchies here, so we just
903 ;; enter this group into the list belonging to this
905 (push (pop groups) tgroups)
906 ;; New sub-hierarchy, so we add it to the topology.
907 (nconc topology (list (setq ntopology
908 (list (list (substring
909 group 0 (match-end 0))
911 ;; Descend the hierarchy.
912 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
913 ;; We remove the trailing "." from the topic name.
915 (if (string-match "\\.$" name)
916 (substring name 0 (match-beginning 0))
918 ;; Add this topic and its groups to the topic alist.
919 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
920 (setcar (car topology) name)
921 ;; We return the rest of the groups that didn't belong
925 ;;; Topic mode, commands and keymap.
927 (defvar gnus-topic-mode-map nil)
928 (defvar gnus-group-topic-map nil)
930 (unless gnus-topic-mode-map
931 (setq gnus-topic-mode-map (make-sparse-keymap))
933 ;; Override certain group mode keys.
934 (gnus-define-keys gnus-topic-mode-map
935 "=" gnus-topic-select-group
936 "\r" gnus-topic-select-group
937 " " gnus-topic-read-group
938 "\C-c\C-x" gnus-topic-expire-articles
939 "\C-k" gnus-topic-kill-group
940 "\C-y" gnus-topic-yank-group
941 "\M-g" gnus-topic-get-new-news-this-topic
942 "AT" gnus-topic-list-active
943 "Gp" gnus-topic-edit-parameters
944 "#" gnus-topic-mark-topic
945 "\M-#" gnus-topic-unmark-topic
946 [tab] gnus-topic-indent
947 [(meta tab)] gnus-topic-unindent
948 "\C-i" gnus-topic-indent
949 "\M-\C-i" gnus-topic-unindent
950 gnus-mouse-2 gnus-mouse-pick-topic)
952 ;; Define a new submap.
953 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
954 "#" gnus-topic-mark-topic
955 "\M-#" gnus-topic-unmark-topic
956 "n" gnus-topic-create-topic
957 "m" gnus-topic-move-group
958 "D" gnus-topic-remove-group
959 "c" gnus-topic-copy-group
960 "h" gnus-topic-hide-topic
961 "s" gnus-topic-show-topic
962 "j" gnus-topic-jump-to-topic
963 "M" gnus-topic-move-matching
964 "C" gnus-topic-copy-matching
965 "\C-i" gnus-topic-indent
966 [tab] gnus-topic-indent
967 "r" gnus-topic-rename
968 "\177" gnus-topic-delete
969 [delete] gnus-topic-delete
970 "H" gnus-topic-toggle-display-empty-topics)
972 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
973 "s" gnus-topic-sort-groups
974 "a" gnus-topic-sort-groups-by-alphabet
975 "u" gnus-topic-sort-groups-by-unread
976 "l" gnus-topic-sort-groups-by-level
977 "v" gnus-topic-sort-groups-by-score
978 "r" gnus-topic-sort-groups-by-rank
979 "m" gnus-topic-sort-groups-by-method))
981 (defun gnus-topic-make-menu-bar ()
982 (unless (boundp 'gnus-topic-menu)
984 gnus-topic-menu gnus-topic-mode-map ""
986 ["Toggle topics" gnus-topic-mode t]
988 ["Copy" gnus-topic-copy-group t]
989 ["Move" gnus-topic-move-group t]
990 ["Remove" gnus-topic-remove-group t]
991 ["Copy matching" gnus-topic-copy-matching t]
992 ["Move matching" gnus-topic-move-matching t])
994 ["Goto" gnus-topic-jump-to-topic t]
995 ["Show" gnus-topic-show-topic t]
996 ["Hide" gnus-topic-hide-topic t]
997 ["Delete" gnus-topic-delete t]
998 ["Rename" gnus-topic-rename t]
999 ["Create" gnus-topic-create-topic t]
1000 ["Mark" gnus-topic-mark-topic t]
1001 ["Indent" gnus-topic-indent t]
1002 ["Sort" gnus-topic-sort-topics t]
1003 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1004 ["Edit parameters" gnus-topic-edit-parameters t])
1005 ["List active" gnus-topic-list-active t]))))
1007 (defun gnus-topic-mode (&optional arg redisplay)
1008 "Minor mode for topicsifying Gnus group buffers."
1009 (interactive (list current-prefix-arg t))
1010 (when (eq major-mode 'gnus-group-mode)
1011 (make-local-variable 'gnus-topic-mode)
1012 (setq gnus-topic-mode
1013 (if (null arg) (not gnus-topic-mode)
1014 (> (prefix-numeric-value arg) 0)))
1015 ;; Infest Gnus with topics.
1016 (if (not gnus-topic-mode)
1017 (setq gnus-goto-missing-group-function nil)
1018 (when (gnus-visual-p 'topic-menu 'menu)
1019 (gnus-topic-make-menu-bar))
1020 (gnus-set-format 'topic t)
1021 (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
1022 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1023 (set (make-local-variable 'gnus-group-prepare-function)
1024 'gnus-group-prepare-topics)
1025 (set (make-local-variable 'gnus-group-get-parameter-function)
1026 'gnus-group-topic-parameters)
1027 (set (make-local-variable 'gnus-group-goto-next-group-function)
1028 'gnus-topic-goto-next-group)
1029 (set (make-local-variable 'gnus-group-indentation-function)
1030 'gnus-topic-group-indentation)
1031 (set (make-local-variable 'gnus-group-update-group-function)
1032 'gnus-topic-update-topics-containing-group)
1033 (set (make-local-variable 'gnus-group-sort-alist-function)
1034 'gnus-group-sort-topic)
1035 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1036 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1037 (make-local-hook 'gnus-check-bogus-groups-hook)
1038 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1039 (setq gnus-topology-checked-p nil)
1040 ;; We check the topology.
1041 (when gnus-newsrc-alist
1042 (gnus-topic-check-topology))
1043 (gnus-run-hooks 'gnus-topic-mode-hook))
1044 ;; Remove topic infestation.
1045 (unless gnus-topic-mode
1046 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1047 (remove-hook 'gnus-group-change-level-function
1048 'gnus-topic-change-level)
1049 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1050 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1051 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1053 (gnus-group-list-groups))))
1055 (defun gnus-topic-select-group (&optional all)
1056 "Select this newsgroup.
1057 No article is selected automatically.
1058 If ALL is non-nil, already read articles become readable.
1059 If ALL is a number, fetch this number of articles.
1061 If performed over a topic line, toggle folding the topic."
1063 (if (gnus-group-topic-p)
1064 (let ((gnus-group-list-mode
1065 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1066 (gnus-topic-fold all))
1067 (gnus-group-select-group all)))
1069 (defun gnus-mouse-pick-topic (e)
1070 "Select the group or topic under the mouse pointer."
1073 (gnus-topic-read-group nil))
1075 (defun gnus-topic-expire-articles (topic)
1076 "Expire articles in this topic or group."
1077 (interactive (list (gnus-group-topic-name)))
1079 (call-interactively 'gnus-group-expire-articles)
1081 (gnus-message 5 "Expiring groups in %s..." topic)
1082 (let ((gnus-group-marked
1083 (mapcar (lambda (entry) (car (nth 2 entry)))
1084 (gnus-topic-find-groups topic gnus-level-killed t))))
1085 (gnus-group-expire-articles nil))
1086 (gnus-message 5 "Expiring groups in %s...done" topic))))
1088 (defun gnus-topic-read-group (&optional all no-article group)
1089 "Read news in this newsgroup.
1090 If the prefix argument ALL is non-nil, already read articles become
1091 readable. IF ALL is a number, fetch this number of articles. If the
1092 optional argument NO-ARTICLE is non-nil, no article will be
1093 auto-selected upon group entry. If GROUP is non-nil, fetch that
1096 If performed over a topic line, toggle folding the topic."
1098 (if (gnus-group-topic-p)
1099 (let ((gnus-group-list-mode
1100 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1101 (gnus-topic-fold all))
1102 (gnus-group-read-group all no-article group)))
1104 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1105 "Create a new TOPIC under PARENT.
1106 When used interactively, PARENT will be the topic under point."
1109 (read-string "New topic: ")
1110 (gnus-current-topic)))
1111 ;; Check whether this topic already exists.
1112 (when (gnus-topic-find-topology topic)
1113 (error "Topic already exists"))
1115 (setq parent (caar gnus-topic-topology)))
1116 (let ((top (cdr (gnus-topic-find-topology parent)))
1117 (full-topic (or full-topic `((,topic visible)))))
1119 (error "No such parent topic: %s" parent))
1122 (while (and (cdr top)
1123 (not (equal (caaadr top) previous)))
1124 (setq top (cdr top)))
1125 (setcdr top (cons full-topic (cdr top))))
1126 (nconc top (list full-topic)))
1127 (unless (assoc topic gnus-topic-alist)
1128 (push (list topic) gnus-topic-alist)))
1129 (gnus-topic-enter-dribble)
1130 (gnus-group-list-groups)
1131 (gnus-topic-goto-topic topic))
1133 (defun gnus-topic-move-group (n topic &optional copyp)
1134 "Move the next N groups to TOPIC.
1135 If COPYP, copy the groups instead."
1137 (list current-prefix-arg
1138 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1139 (let ((groups (gnus-group-process-prefix n))
1140 (topicl (assoc topic gnus-topic-alist))
1141 (start-topic (gnus-group-topic-name))
1142 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1144 (if (and (not groups) (not copyp) start-topic)
1145 (gnus-topic-move start-topic topic)
1148 (gnus-group-remove-mark g)
1150 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1152 (setcdr entry (gnus-delete-first g (cdr entry))))
1153 (nconc topicl (list g)))
1155 (gnus-topic-enter-dribble)
1157 (gnus-group-goto-group start-group)
1158 (gnus-topic-goto-topic start-topic))
1159 (gnus-group-list-groups))))
1161 (defun gnus-topic-remove-group (&optional arg)
1162 "Remove the current group from the topic."
1164 (gnus-group-iterate arg
1166 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1167 (buffer-read-only nil))
1168 (when (and topicl group)
1170 (gnus-delete-first group topicl))
1171 (gnus-topic-update-topic)
1172 (gnus-group-position-point)))))
1174 (defun gnus-topic-copy-group (n topic)
1175 "Copy the current group to a topic."
1177 (list current-prefix-arg
1178 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1179 (gnus-topic-move-group n topic t))
1181 (defun gnus-topic-kill-group (&optional n discard)
1182 "Kill the next N groups."
1184 (if (gnus-group-topic-p)
1185 (let ((topic (gnus-group-topic-name)))
1187 (gnus-topic-find-topology topic)
1188 (assoc topic gnus-topic-alist))
1189 gnus-topic-killed-topics)
1190 (gnus-topic-remove-topic nil t)
1191 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1192 (gnus-topic-enter-dribble))
1193 (gnus-group-kill-group n discard)
1194 (gnus-topic-update-topic)))
1196 (defun gnus-topic-yank-group (&optional arg)
1197 "Yank the last topic."
1199 (if gnus-topic-killed-topics
1201 (or (gnus-group-topic-name)
1202 (gnus-topic-next-topic (gnus-current-topic))))
1203 (data (pop gnus-topic-killed-topics))
1206 (push alist gnus-topic-alist)
1207 (gnus-topic-create-topic
1208 (caar item) (gnus-topic-parent-topic previous) previous
1210 (gnus-topic-enter-dribble)
1211 (gnus-topic-goto-topic (caar item)))
1212 (let* ((prev (gnus-group-group-name))
1213 (gnus-topic-inhibit-change-level t)
1214 (gnus-group-indentation
1216 (* gnus-topic-indent-level
1218 (gnus-topic-goto-topic (gnus-current-topic))
1219 (gnus-group-topic-level))
1223 ;; We first yank the groups the normal way...
1224 (setq yanked (gnus-group-yank-group arg))
1225 ;; Then we enter the yanked groups into the topics they belong
1227 (setq alist (assoc (save-excursion
1229 (gnus-current-topic))
1231 (when (stringp yanked)
1232 (setq yanked (list yanked)))
1234 (nconc alist yanked)
1235 (if (not (cdr alist))
1236 (setcdr alist (nconc yanked (cdr alist)))
1238 (when (equal (cadr alist) prev)
1239 (setcdr alist (nconc yanked (cdr alist)))
1241 (setq alist (cdr alist))))))
1242 (gnus-topic-update-topic)))
1244 (defun gnus-topic-hide-topic ()
1245 "Hide the current topic."
1247 (when (gnus-current-topic)
1248 (gnus-topic-goto-topic (gnus-current-topic))
1249 (gnus-topic-remove-topic nil nil 'hidden)))
1251 (defun gnus-topic-show-topic ()
1252 "Show the hidden topic."
1254 (when (gnus-group-topic-p)
1255 (gnus-topic-remove-topic t nil 'shown)))
1257 (defun gnus-topic-mark-topic (topic &optional unmark)
1258 "Mark all groups in the topic with the process mark."
1259 (interactive (list (gnus-group-topic-name)))
1261 (call-interactively 'gnus-group-mark-group)
1263 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
1265 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1266 (gnus-info-group (nth 2 (pop groups)))))))))
1268 (defun gnus-topic-unmark-topic (topic &optional unmark)
1269 "Remove the process mark from all groups in the topic."
1270 (interactive (list (gnus-group-topic-name)))
1272 (call-interactively 'gnus-group-unmark-group)
1273 (gnus-topic-mark-topic topic t)))
1275 (defun gnus-topic-get-new-news-this-topic (&optional n)
1276 "Check for new news in the current topic."
1278 (if (not (gnus-group-topic-p))
1279 (gnus-group-get-new-news-this-group n)
1280 (gnus-topic-mark-topic (gnus-group-topic-name))
1281 (gnus-group-get-new-news-this-group)))
1283 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1284 "Move all groups that match REGEXP to some topic."
1289 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1290 (read-string (format "Move to %s (regexp): " topic))))))
1291 (gnus-group-mark-regexp regexp)
1292 (gnus-topic-move-group nil topic copyp))
1294 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1295 "Copy all groups that match REGEXP to some topic."
1300 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1301 (read-string (format "Copy to %s (regexp): " topic))))))
1302 (gnus-topic-move-matching regexp topic t))
1304 (defun gnus-topic-delete (topic)
1306 (interactive (list (gnus-group-topic-name)))
1308 (error "No topic to be deleted"))
1309 (let ((entry (assoc topic gnus-topic-alist))
1310 (buffer-read-only nil))
1312 (error "Topic not empty"))
1313 ;; Delete if visible.
1314 (when (gnus-topic-goto-topic topic)
1316 ;; Remove from alist.
1317 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1318 ;; Remove from topology.
1319 (gnus-topic-find-topology topic nil nil 'delete)
1320 (gnus-dribble-touch)))
1322 (defun gnus-topic-rename (old-name new-name)
1325 (let ((topic (gnus-current-topic)))
1327 (read-string (format "Rename %s to: " topic)))))
1328 ;; Check whether the new name exists.
1329 (when (gnus-topic-find-topology new-name)
1330 (error "Topic '%s' already exists" new-name))
1331 ;; "nil" is an invalid name, for reasons I'd rather not go
1332 ;; into here. Trust me.
1333 (when (equal new-name "nil")
1334 (error "Invalid name: %s" nil))
1336 (let ((top (gnus-topic-find-topology old-name))
1337 (entry (assoc old-name gnus-topic-alist)))
1339 (setcar (cadr top) new-name))
1341 (setcar entry new-name))
1343 (gnus-dribble-touch)
1344 (gnus-group-list-groups)
1347 (defun gnus-topic-indent (&optional unindent)
1348 "Indent a topic -- make it a sub-topic of the previous topic.
1349 If UNINDENT, remove an indentation."
1352 (gnus-topic-unindent)
1353 (let* ((topic (gnus-current-topic))
1354 (parent (gnus-topic-previous-topic topic))
1355 (buffer-read-only nil))
1357 (error "Nothing to indent %s into" topic))
1359 (gnus-topic-goto-topic topic)
1360 (gnus-topic-kill-group)
1361 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1362 (gnus-topic-create-topic
1363 topic parent nil (cdaar gnus-topic-killed-topics))
1364 (pop gnus-topic-killed-topics)
1365 (or (gnus-topic-goto-topic topic)
1366 (gnus-topic-goto-topic parent))))))
1368 (defun gnus-topic-unindent ()
1371 (let* ((topic (gnus-current-topic))
1372 (parent (gnus-topic-parent-topic topic))
1373 (grandparent (gnus-topic-parent-topic parent)))
1375 (error "Nothing to indent %s into" topic))
1377 (gnus-topic-goto-topic topic)
1378 (gnus-topic-kill-group)
1379 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1380 (gnus-topic-create-topic
1381 topic grandparent (gnus-topic-next-topic parent)
1382 (cdaar gnus-topic-killed-topics))
1383 (pop gnus-topic-killed-topics)
1384 (gnus-topic-goto-topic topic))))
1386 (defun gnus-topic-list-active (&optional force)
1387 "List all groups that Gnus knows about in a topicsified fashion.
1388 If FORCE, always re-read the active file."
1391 (gnus-get-killed-groups))
1392 (gnus-topic-grok-active force)
1393 (let ((gnus-topic-topology gnus-topic-active-topology)
1394 (gnus-topic-alist gnus-topic-active-alist)
1395 gnus-killed-list gnus-zombie-list)
1396 (gnus-group-list-groups gnus-level-killed nil 1)))
1398 (defun gnus-topic-toggle-display-empty-topics ()
1399 "Show/hide topics that have no unread articles."
1401 (setq gnus-topic-display-empty-topics
1402 (not gnus-topic-display-empty-topics))
1403 (gnus-group-list-groups)
1404 (message "%s empty topics"
1405 (if gnus-topic-display-empty-topics
1406 "Showing" "Hiding")))
1408 ;;; Topic sorting functions
1410 (defun gnus-topic-edit-parameters (group)
1411 "Edit the group parameters of GROUP.
1412 If performed on a topic, edit the topic parameters instead."
1413 (interactive (list (gnus-group-group-name)))
1415 (gnus-group-edit-group-parameters group)
1416 (if (not (gnus-group-topic-p))
1417 (error "Nothing to edit on the current line")
1418 (let ((topic (gnus-group-topic-name)))
1420 (gnus-topic-parameters topic)
1421 (format "Editing the topic parameters for `%s'."
1424 (gnus-topic-set-parameters ,topic form)))))))
1426 (defun gnus-group-sort-topic (func reverse)
1427 "Sort groups in the topics according to FUNC and REVERSE."
1428 (let ((alist gnus-topic-alist))
1430 ;; !!!Sometimes nil elements sneak into the alist,
1431 ;; for some reason or other.
1432 (setcar alist (delq nil (car alist)))
1433 (setcar alist (delete "dummy.group" (car alist)))
1434 (gnus-topic-sort-topic (pop alist) func reverse))))
1436 (defun gnus-topic-sort-topic (topic func reverse)
1437 ;; Each topic only lists the name of the group, while
1438 ;; the sort predicates expect group infos as inputs.
1439 ;; So we first transform the group names into infos,
1440 ;; then sort, and then transform back into group names.
1444 (lambda (info) (gnus-info-group info))
1447 (lambda (group) (gnus-get-info group))
1450 ;; Do the reversal, if necessary.
1452 (setcdr topic (nreverse (cdr topic)))))
1454 (defun gnus-topic-sort-groups (func &optional reverse)
1455 "Sort the current topic according to FUNC.
1456 If REVERSE, reverse the sorting order."
1457 (interactive (list gnus-group-sort-function current-prefix-arg))
1458 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1459 (gnus-topic-sort-topic
1460 topic (gnus-make-sort-function func) reverse)
1461 (gnus-group-list-groups)))
1463 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1464 "Sort the current topic alphabetically by group name.
1465 If REVERSE, sort in reverse order."
1467 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1469 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1470 "Sort the current topic by number of unread articles.
1471 If REVERSE, sort in reverse order."
1473 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1475 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1476 "Sort the current topic by group level.
1477 If REVERSE, sort in reverse order."
1479 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1481 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1482 "Sort the current topic by group score.
1483 If REVERSE, sort in reverse order."
1485 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1487 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1488 "Sort the current topic by group rank.
1489 If REVERSE, sort in reverse order."
1491 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1493 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1494 "Sort the current topic alphabetically by backend name.
1495 If REVERSE, sort in reverse order."
1497 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1499 (defun gnus-topic-sort-topics-1 (top reverse)
1502 (mapcar `(lambda (top)
1503 (gnus-topic-sort-topics-1 top ,reverse))
1506 (string-lessp (caar t1) (caar t2)))))))
1507 (setcdr top (if reverse (reverse subtop) subtop))))
1510 (defun gnus-topic-sort-topics (&optional topic reverse)
1511 "Sort topics in TOPIC alphabeticaly by topic name.
1512 If REVERSE, reverse the sorting order."
1514 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1515 (gnus-current-topic))
1516 current-prefix-arg))
1517 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1518 gnus-topic-topology)))
1519 (gnus-topic-sort-topics-1 topic-topology reverse)
1520 (gnus-topic-enter-dribble)
1521 (gnus-group-list-groups)
1522 (gnus-topic-goto-topic topic)))
1524 (defun gnus-topic-move (current to)
1525 "Move the CURRENT topic to TO."
1528 (gnus-group-topic-name)
1529 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1530 (unless (and current to)
1531 (error "Can't find topic"))
1532 (let ((current-top (cdr (gnus-topic-find-topology current)))
1533 (to-top (cdr (gnus-topic-find-topology to))))
1535 (error "Can't find topic `%s'" current))
1537 (error "Can't find topic `%s'" to))
1538 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1539 (error "Can't move `%s' to its sub-level" current))
1540 (gnus-topic-find-topology current nil nil 'delete)
1542 (setq to-top (cdr to-top)))
1543 (setcdr to-top (list current-top))
1544 (gnus-topic-enter-dribble)
1545 (gnus-group-list-groups)
1546 (gnus-topic-goto-topic current)))
1548 (defun gnus-subscribe-topics (newsgroup)
1550 (let (match gnus-group-change-level-function)
1551 (dolist (topic (gnus-topic-list))
1552 (when (and (setq match (cdr (assq 'subscribe
1553 (gnus-topic-parameters topic))))
1554 (string-match match newsgroup))
1555 ;; Just subscribe the group.
1556 (gnus-subscribe-alphabetically newsgroup)
1557 ;; Add the group to the topic.
1558 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1561 (provide 'gnus-topic)
1563 ;;; gnus-topic.el ends here