1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96,97,98,99 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))
36 (defgroup gnus-topic nil
40 (defvar gnus-topic-mode nil
41 "Minor mode for Gnus group buffers.")
43 (defcustom gnus-topic-mode-hook nil
44 "Hook run in topic mode buffers."
48 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
49 "Format of topic lines.
50 It works along the same lines as a normal formatting string,
51 with some simple extensions.
53 %i Indentation based on topic level.
55 %v Nothing if the topic is visible, \"...\" otherwise.
56 %g Number of groups in the topic.
57 %a Number of unread articles in the groups in the topic.
58 %A Number of unread articles in the groups in the topic and its subtopics.
63 (defcustom gnus-topic-indent-level 2
64 "*How much each subtopic should be indented."
68 (defcustom gnus-topic-display-empty-topics t
69 "*If non-nil, display the topic lines even of topics that have no unread articles."
73 ;; Internal variables.
75 (defvar gnus-topic-active-topology nil)
76 (defvar gnus-topic-active-alist nil)
77 (defvar gnus-topic-unreads nil)
79 (defvar gnus-topology-checked-p nil
80 "Whether the topology has been checked in this session.")
82 (defvar gnus-topic-killed-topics nil)
83 (defvar gnus-topic-inhibit-change-level nil)
85 (defconst gnus-topic-line-format-alist
89 (?g number-of-groups ?d)
90 (?a (gnus-topic-articles-in-topic entries) ?d)
91 (?A total-number-of-articles ?d)
94 (defvar gnus-topic-line-format-spec nil)
98 (defun gnus-group-topic-name ()
99 "The name of the topic on the current line."
100 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
101 (and topic (symbol-name topic))))
103 (defun gnus-group-topic-level ()
104 "The level of the topic on the current line."
105 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
107 (defun gnus-group-topic-unread ()
108 "The number of unread articles in topic on the current line."
109 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
111 (defun gnus-topic-unread (topic)
112 "Return the number of unread articles in TOPIC."
113 (or (cdr (assoc topic gnus-topic-unreads))
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)
153 (gnus-goto-char (text-property-any (point-min) (point-max)
154 'gnus-topic (intern topic)))))
156 (defun gnus-topic-jump-to-topic (topic)
159 (list (completing-read "Go to topic: "
160 (mapcar 'list (gnus-topic-list))
162 (dolist (topic (gnus-current-topics topic))
164 (gnus-topic-goto-topic topic))
166 (defun gnus-current-topic ()
167 "Return the name of the current topic."
169 (or (get-text-property (point) 'gnus-topic)
171 (and (gnus-goto-char (previous-single-property-change
172 (point) 'gnus-topic))
173 (get-text-property (max (1- (point)) (point-min))
176 (symbol-name result))))
178 (defun gnus-current-topics (&optional topic)
179 "Return a list of all current topics, lowest in hierarchy first.
180 If TOPIC, start with that topic."
181 (let ((topic (or topic (gnus-current-topic)))
185 (setq topic (gnus-topic-parent-topic topic)))
188 (defun gnus-group-active-topic-p ()
189 "Say whether the current topic comes from the active topics."
192 (get-text-property (point) 'gnus-active)))
194 (defun gnus-topic-find-groups (topic &optional level all lowest)
195 "Return entries for all visible groups in TOPIC."
196 (let ((groups (cdr (assoc topic gnus-topic-alist)))
197 info clevel unread group params visible-groups entry active)
198 (setq lowest (or lowest 1))
199 (setq level (or level gnus-level-unsubscribed))
200 ;; We go through the newsrc to look for matches.
202 (when (setq group (pop groups))
203 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
205 params (gnus-info-params info)
206 active (gnus-active group)
207 unread (or (car entry)
208 (and (not (equal group "dummy.group"))
210 (- (1+ (cdr active)) (car active))))
211 clevel (or (gnus-info-level info)
212 (if (member group gnus-zombie-list)
213 gnus-level-zombie gnus-level-killed))))
215 info ; nil means that the group is dead.
217 (>= clevel lowest) ; Is inside the level we want.
219 (if (or (eq unread t)
221 gnus-group-list-inactive-groups
223 (and gnus-list-groups-with-ticked-articles
224 (cdr (assq 'tick (gnus-info-marks info))))
225 ; Has right readedness.
226 ;; Check for permanent visibility.
227 (and gnus-permanently-visible-groups
228 (string-match gnus-permanently-visible-groups group))
229 (memq 'visible params)
230 (cdr (assq 'visible params)))
231 ;; Add this group to the list of visible groups.
232 (push (or entry group) visible-groups)))
233 (nreverse visible-groups)))
235 (defun gnus-topic-previous-topic (topic)
236 "Return the previous topic on the same level as TOPIC."
237 (let ((top (cddr (gnus-topic-find-topology
238 (gnus-topic-parent-topic topic)))))
239 (unless (equal topic (caaar top))
240 (while (and top (not (equal (caaadr top) topic)))
241 (setq top (cdr top)))
244 (defun gnus-topic-parent-topic (topic &optional topology)
245 "Return the parent of TOPIC."
247 (setq topology gnus-topic-topology))
248 (let ((parent (car (pop topology)))
251 (not (setq found (equal (caaar topology) topic)))
252 (not (setq result (gnus-topic-parent-topic
253 topic (car topology)))))
254 (setq topology (cdr topology)))
255 (or result (and found parent))))
257 (defun gnus-topic-next-topic (topic &optional previous)
258 "Return the next sibling of TOPIC."
259 (let ((parentt (cddr (gnus-topic-find-topology
260 (gnus-topic-parent-topic topic))))
263 (not (equal (caaar parentt) topic)))
264 (setq prev (caaar parentt)
265 parentt (cdr parentt)))
270 (defun gnus-topic-forward-topic (num)
271 "Go to the next topic on the same level as the current one."
272 (let* ((topic (gnus-current-topic))
273 (way (if (< num 0) 'gnus-topic-previous-topic
274 'gnus-topic-next-topic))
276 (while (and (not (zerop num))
277 (setq topic (funcall way topic)))
278 (when (gnus-topic-goto-topic topic)
281 (goto-char (point-max)))
284 (defun gnus-topic-find-topology (topic &optional topology level remove)
285 "Return the topology of TOPIC."
287 (setq topology gnus-topic-topology)
291 (if (equal (caar topology) topic)
294 (delq topology remove))
295 (cons level topology))
296 (setq topology (cdr topology))
298 (not (setq result (gnus-topic-find-topology
299 topic (car topology) (1+ level)
301 (setq topology (cdr topology)))
304 (defvar gnus-tmp-topics nil)
305 (defun gnus-topic-list (&optional topology)
306 "Return a list of all topics in the topology."
308 (setq topology gnus-topic-topology
309 gnus-tmp-topics nil))
310 (push (caar topology) gnus-tmp-topics)
311 (mapcar 'gnus-topic-list (cdr topology))
314 ;;; Topic parameter jazz
316 (defun gnus-topic-parameters (topic)
317 "Return the parameters for TOPIC."
318 (let ((top (gnus-topic-find-topology topic)))
320 (nth 3 (cadr top)))))
322 (defun gnus-topic-set-parameters (topic parameters)
323 "Set the topic parameters of TOPIC to PARAMETERS."
324 (let ((top (gnus-topic-find-topology topic)))
326 (error "No such topic: %s" topic))
327 ;; We may have to extend if there is no parameters here
329 (unless (nthcdr 2 (cadr top))
330 (nconc (cadr top) (list nil)))
331 (unless (nthcdr 3 (cadr top))
332 (nconc (cadr top) (list nil)))
333 (setcar (nthcdr 3 (cadr top)) parameters)
335 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
337 (defun gnus-group-topic-parameters (group)
338 "Compute the group parameters for GROUP taking into account inheritance from topics."
339 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
341 (gnus-group-goto-group group)
343 (gnus-topic-hierarchical-parameters (gnus-current-topic))))))
345 (defun gnus-topic-hierarchical-parameters (topic)
346 "Return a topic list computed for TOPIC."
347 (let ((topics (gnus-current-topics topic))
348 params-list param out params)
350 (push (gnus-topic-parameters (pop topics)) params-list))
351 ;; We probably have lots of nil elements here, so
352 ;; we remove them. Probably faster than doing this "properly".
353 (setq params-list (delq nil params-list))
354 ;; Now we have all the parameters, so we go through them
355 ;; and do inheritance in the obvious way.
356 (while (setq params (pop params-list))
357 (while (setq param (pop params))
359 (setq param (cons param t)))
360 ;; Override any old versions of this param.
361 (gnus-pull (car param) out)
363 ;; Return the resulting parameter list.
366 ;;; General utility functions
368 (defun gnus-topic-enter-dribble ()
370 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
372 ;;; Generating group buffers
374 (defun gnus-group-prepare-topics (level &optional all lowest
375 regexp list-topic topic-level)
376 "List all newsgroups with unread articles of level LEVEL or lower.
377 Use the `gnus-group-topics' to sort the groups.
378 If ALL is non-nil, list groups that have no unread articles.
379 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
380 (set-buffer gnus-group-buffer)
381 (let ((buffer-read-only nil)
382 (lowest (or lowest 1)))
384 (when (or (not gnus-topic-alist)
385 (not gnus-topology-checked-p))
386 (gnus-topic-check-topology))
392 (when (and (>= level gnus-level-zombie)
393 (<= lowest gnus-level-zombie))
394 (gnus-group-prepare-flat-list-dead
395 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
399 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
400 (gnus-group-prepare-flat-list-dead
401 (setq gnus-killed-list (sort gnus-killed-list 'string<))
407 (when (< lowest gnus-level-zombie)
409 (let ((top (gnus-topic-find-topology list-topic)))
410 (gnus-topic-prepare-topic (cdr top) (car top)
411 (or topic-level level) all
413 (gnus-topic-prepare-topic gnus-topic-topology 0
414 (or topic-level level) all
417 (gnus-group-set-mode-line)
418 (setq gnus-group-list-mode (cons level all))
419 (gnus-run-hooks 'gnus-group-prepare-hook))))
421 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent
423 "Insert TOPIC into the group buffer.
424 If SILENT, don't insert anything. Return the number of unread
425 articles in the topic and its subtopics."
426 (let* ((type (pop topicl))
427 (entries (gnus-topic-find-groups
428 (car type) list-level
431 (gnus-topic-hierarchical-parameters
434 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
435 (gnus-group-indentation
436 (make-string (* gnus-topic-indent-level level) ? ))
437 (beg (progn (beginning-of-line) (point)))
438 (topicl (reverse topicl))
439 (all-entries entries)
440 (point-max (point-max))
443 info entry end active tick)
444 ;; Insert any sub-topics.
447 (gnus-topic-prepare-topic
448 (pop topicl) (1+ level) list-level all
449 (not visiblep) lowest)))
452 ;; Insert all the groups that belong in this topic.
453 (while (setq entry (pop entries))
457 (gnus-group-insert-group-line
458 entry (if (member entry gnus-zombie-list)
459 gnus-level-zombie gnus-level-killed)
460 nil (- (1+ (cdr (setq active (gnus-active entry))))
464 (when (setq info (nth 2 entry))
465 (gnus-group-insert-group-line
466 (gnus-info-group info)
467 (gnus-info-level info) (gnus-info-marks info)
468 (car entry) (gnus-info-method info)))))
469 (when (and (listp entry)
470 (numberp (car entry)))
471 (incf unread (car entry)))
475 ;; Insert the topic line.
476 (when (and (not silent)
477 (or gnus-topic-display-empty-topics ;We want empty topics
478 (not (zerop unread)) ;Non-empty
479 tick ;Ticked articles
480 (/= point-max (point-max)))) ;Unactivated groups
481 (gnus-extent-start-open (point))
482 (gnus-topic-insert-topic-line
484 (not (eq (nth 2 type) 'hidden))
485 level all-entries unread))
486 (gnus-topic-update-unreads (car type) unread)
490 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
491 "Remove the current topic."
492 (let ((topic (gnus-group-topic-name))
493 (level (gnus-group-topic-level))
494 (beg (progn (beginning-of-line) (point)))
497 (while (and (zerop (forward-line 1))
498 (> (or (gnus-group-topic-level) (1+ level)) level)))
499 (delete-region beg (point))
500 ;; Do the change in this rather odd manner because it has been
501 ;; reported that some topics share parts of some lists, for some
502 ;; reason. I have been unable to determine why this is the
503 ;; case, but this hack seems to take care of things.
504 (let ((data (cadr (gnus-topic-find-topology topic))))
506 (list (if insert 'visible 'invisible)
510 (setq gnus-topic-alist
511 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
512 (gnus-topic-insert-topic topic in-level)))))
514 (defun gnus-topic-insert-topic (topic &optional level)
516 (gnus-group-prepare-topics
517 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
518 nil nil topic level))
520 (defun gnus-topic-fold (&optional insert topic)
521 "Remove/insert the current topic."
522 (let ((topic (or topic (gnus-group-topic-name))))
525 (if (not (gnus-group-active-topic-p))
526 (gnus-topic-remove-topic
527 (or insert (not (gnus-topic-visible-p))))
528 (let ((gnus-topic-topology gnus-topic-active-topology)
529 (gnus-topic-alist gnus-topic-active-alist)
530 (gnus-group-list-mode (cons 5 t)))
531 (gnus-topic-remove-topic
532 (or insert (not (gnus-topic-visible-p))) nil nil 9)
533 (gnus-topic-enter-dribble)))))))
535 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
537 (let* ((visible (if visiblep "" "..."))
538 (indentation (make-string (* gnus-topic-indent-level level) ? ))
539 (total-number-of-articles unread)
540 (number-of-groups (length entries))
541 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
543 (gnus-topic-update-unreads name unread)
546 (gnus-add-text-properties
549 (eval gnus-topic-line-format-spec))
550 (list 'gnus-topic (intern name)
551 'gnus-topic-level level
552 'gnus-topic-unread unread
553 'gnus-active active-topic
554 'gnus-topic-visible visiblep))))
556 (defun gnus-topic-update-unreads (topic unreads)
557 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
559 (push (cons topic unreads) gnus-topic-unreads))
561 (defun gnus-topic-update-topics-containing-group (group)
562 "Update all topics that have GROUP as a member."
563 (when (and (eq major-mode 'gnus-group-mode)
566 (let ((alist gnus-topic-alist))
567 ;; This is probably not entirely correct. If a topic
568 ;; isn't shown, then it's not updated. But the updating
569 ;; should be performed in any case, since the topic's
570 ;; parent should be updated. Pfft.
572 (when (and (member group (cdar alist))
573 (gnus-topic-goto-topic (caar alist)))
574 (gnus-topic-update-topic-line (caar alist)))
577 (defun gnus-topic-update-topic ()
578 "Update all parent topics to the current group."
579 (when (and (eq major-mode 'gnus-group-mode)
581 (let ((group (gnus-group-group-name))
583 (buffer-read-only nil))
585 (gnus-get-info group)
586 (gnus-topic-goto-topic (gnus-current-topic)))
587 (gnus-topic-update-topic-line (gnus-group-topic-name))
590 (gnus-group-position-point)))))
592 (defun gnus-topic-goto-missing-group (group)
593 "Place point where GROUP is supposed to be inserted."
594 (let* ((topic (gnus-group-topic group))
595 (groups (cdr (assoc topic gnus-topic-alist)))
596 (g (cdr (member group groups)))
598 ;; Try to jump to a visible group.
599 (while (and g (not (gnus-group-goto-group (car g) t)))
601 ;; It wasn't visible, so we try to see where to insert it.
603 (setq g (cdr (member group (reverse groups))))
604 (while (and g unfound)
605 (when (gnus-group-goto-group (pop g) t)
610 (not (gnus-topic-goto-missing-topic topic)))
611 (gnus-topic-insert-topic-line
612 topic t t (car (gnus-topic-find-topology topic)) nil 0)))))
614 (defun gnus-topic-goto-missing-topic (topic)
615 (if (gnus-topic-goto-topic topic)
617 ;; Topic not displayed.
618 (let* ((top (gnus-topic-find-topology
619 (gnus-topic-parent-topic topic)))
620 (tp (reverse (cddr top))))
621 (while (not (equal (caaar tp) topic))
625 (not (gnus-topic-goto-topic (caaar tp))))
628 (gnus-topic-forward-topic 1)
629 (gnus-topic-goto-missing-topic (caadr top))))
632 (defun gnus-topic-update-topic-line (topic-name &optional reads)
633 (let* ((top (gnus-topic-find-topology topic-name))
635 (children (cddr top))
636 (entries (gnus-topic-find-groups
637 (car type) (car gnus-group-list-mode)
638 (cdr gnus-group-list-mode)))
639 (parent (gnus-topic-parent-topic topic-name))
640 (all-entries entries)
642 old-unread entry new-unread)
643 (when (gnus-topic-goto-topic (car type))
644 ;; Tally all the groups that belong in this topic.
646 (setq unread (- (gnus-group-topic-unread) reads))
648 (incf unread (gnus-topic-unread (caar (pop children)))))
649 (while (setq entry (pop entries))
650 (when (numberp (car entry))
651 (incf unread (car entry)))))
652 (setq old-unread (gnus-group-topic-unread))
653 ;; Insert the topic line.
654 (gnus-topic-insert-topic-line
655 (car type) (gnus-topic-visible-p)
656 (not (eq (nth 2 type) 'hidden))
657 (gnus-group-topic-level) all-entries unread)
660 (setq new-unread (gnus-group-topic-unread)))
663 (gnus-topic-update-topic-line
665 (- (or old-unread 0) (or new-unread 0))))
668 (defun gnus-topic-group-indentation ()
670 (* gnus-topic-indent-level
673 (gnus-topic-goto-topic (gnus-current-topic))
674 (gnus-group-topic-level))
680 (gnus-add-shutdown 'gnus-topic-close 'gnus)
682 (defun gnus-topic-close ()
683 (setq gnus-topic-active-topology nil
684 gnus-topic-active-alist nil
685 gnus-topic-killed-topics nil
686 gnus-topology-checked-p nil))
688 (defun gnus-topic-check-topology ()
689 ;; The first time we set the topology to whatever we have
690 ;; gotten here, which can be rather random.
691 (unless gnus-topic-alist
692 (gnus-topic-init-alist))
694 (setq gnus-topology-checked-p t)
695 ;; Go through the topic alist and make sure that all topics
696 ;; are in the topic topology.
697 (let ((topics (gnus-topic-list))
698 (alist gnus-topic-alist)
701 (unless (member (caar alist) topics)
702 (nconc gnus-topic-topology
703 (list (list (list (caar alist) 'visible))))
705 (setq alist (cdr alist)))
707 (gnus-topic-enter-dribble))
708 ;; Conversely, go through the topology and make sure that all
709 ;; topologies have alists.
711 (unless (assoc (car topics) gnus-topic-alist)
712 (push (list (car topics)) gnus-topic-alist))
714 ;; Go through all living groups and make sure that
715 ;; they belong to some topic.
716 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
718 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
719 (newsrc (cdr gnus-newsrc-alist))
722 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
723 (setcdr entry (list group))
724 (setq entry (cdr entry)))))
725 ;; Go through all topics and make sure they contain only living groups.
726 (let ((alist gnus-topic-alist)
728 (while (setq topic (pop alist))
730 (if (and (cadr topic)
731 (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
732 (setq topic (cdr topic))
733 (setcdr topic (cddr topic)))))))
735 (defun gnus-topic-init-alist ()
736 "Initialize the topic structures."
737 (setq gnus-topic-topology
738 (cons (list "Gnus" 'visible)
739 (mapcar (lambda (topic)
740 (list (list (car topic) 'visible)))
742 (setq gnus-topic-alist
744 (mapcar (lambda (info) (gnus-info-group info))
745 (cdr gnus-newsrc-alist)))
747 (gnus-topic-enter-dribble))
751 (defun gnus-topic-clean-alist ()
752 "Remove bogus groups from the topic alist."
753 (let ((topic-alist gnus-topic-alist)
755 (unless gnus-killed-hashtb
756 (gnus-make-hashtable-from-killed))
757 (while (setq topic (pop topic-alist))
758 (let ((topic-name (pop topic))
759 group filtered-topic)
760 (while (setq group (pop topic))
761 (when (and (or (gnus-gethash group gnus-active-hashtb)
762 (gnus-info-method (gnus-get-info group)))
763 (not (gnus-gethash group gnus-killed-hashtb)))
764 (push group filtered-topic)))
765 (push (cons topic-name (nreverse filtered-topic)) result)))
766 (setq gnus-topic-alist (nreverse result))))
768 (defun gnus-topic-change-level (group level oldlevel &optional previous)
769 "Run when changing levels to enter/remove groups from topics."
771 (set-buffer gnus-group-buffer)
772 (let ((buffer-read-only nil))
773 (unless gnus-topic-inhibit-change-level
774 (gnus-group-goto-group (or (car (nth 2 previous)) group))
775 (when (and gnus-topic-mode
777 (not gnus-topic-inhibit-change-level))
778 ;; Remove the group from the topics.
779 (if (and (< oldlevel gnus-level-zombie)
780 (>= level gnus-level-zombie))
781 (let ((alist gnus-topic-alist))
782 (while (gnus-group-goto-group group)
785 (when (member group (car alist))
786 (setcdr (car alist) (delete group (cdar alist))))
788 ;; If the group is subscribed we enter it into the topics.
789 (when (and (< level gnus-level-zombie)
790 (>= oldlevel gnus-level-zombie))
791 (let* ((prev (gnus-group-group-name))
792 (gnus-topic-inhibit-change-level t)
793 (gnus-group-indentation
795 (* gnus-topic-indent-level
797 (gnus-topic-goto-topic (gnus-current-topic))
798 (gnus-group-topic-level))
801 (yanked (list group))
803 ;; Then we enter the yanked groups into the topics they belong
805 (when (setq alist (assoc (save-excursion
809 (caar gnus-topic-topology)))
812 (when (stringp yanked)
813 (setq yanked (list yanked)))
816 (if (not (cdr alist))
817 (setcdr alist (nconc yanked (cdr alist)))
818 (while (and (not end) (cdr alist))
819 (when (equal (cadr alist) prev)
820 (setcdr alist (nconc yanked (cdr alist)))
822 (setq alist (cdr alist)))
824 (nconc talist yanked))))))
825 (gnus-topic-update-topic))))))))
827 (defun gnus-topic-goto-next-group (group props)
828 "Go to group or the next group after group."
830 (if (not (memq 'gnus-topic props))
831 (goto-char (point-max))
832 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
833 (if (gnus-group-goto-group group)
835 ;; The group is no longer visible.
836 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
837 (after (cdr (member group (cdr list)))))
838 ;; First try to put point on a group after the current one.
840 (not (gnus-group-goto-group (car after))))
841 (setq after (cdr after)))
842 ;; Then try to put point on a group before point.
844 (setq after (cdr (member group (reverse (cdr list)))))
846 (not (gnus-group-goto-group (car after))))
847 (setq after (cdr after))))
848 ;; Finally, just put point on the topic.
850 (goto-char (point-min))
852 (gnus-topic-goto-topic (car list))
856 ;;; Topic-active functions
858 (defun gnus-topic-grok-active (&optional force)
859 "Parse all active groups and create topic structures for them."
860 ;; First we make sure that we have really read the active file.
862 (not gnus-topic-active-alist))
864 ;; Get a list of all groups available.
865 (mapatoms (lambda (g) (when (symbol-value g)
866 (push (symbol-name g) groups)))
868 (setq groups (sort groups 'string<))
869 ;; Init the variables.
870 (setq gnus-topic-active-topology (list (list "" 'visible)))
871 (setq gnus-topic-active-alist nil)
872 ;; Descend the top-level hierarchy.
873 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
874 ;; Set the top-level topic names to something nice.
875 (setcar (car gnus-topic-active-topology) "Gnus active")
876 (setcar (car gnus-topic-active-alist) "Gnus active"))))
878 (defun gnus-topic-grok-active-1 (topology groups)
879 (let* ((name (caar topology))
880 (prefix (concat "^" (regexp-quote name)))
881 tgroups ntopology group)
883 (string-match prefix (setq group (car groups))))
884 (if (not (string-match "\\." group (match-end 0)))
885 ;; There are no further hierarchies here, so we just
886 ;; enter this group into the list belonging to this
888 (push (pop groups) tgroups)
889 ;; New sub-hierarchy, so we add it to the topology.
890 (nconc topology (list (setq ntopology
891 (list (list (substring
892 group 0 (match-end 0))
894 ;; Descend the hierarchy.
895 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
896 ;; We remove the trailing "." from the topic name.
898 (if (string-match "\\.$" name)
899 (substring name 0 (match-beginning 0))
901 ;; Add this topic and its groups to the topic alist.
902 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
903 (setcar (car topology) name)
904 ;; We return the rest of the groups that didn't belong
908 ;;; Topic mode, commands and keymap.
910 (defvar gnus-topic-mode-map nil)
911 (defvar gnus-group-topic-map nil)
913 (unless gnus-topic-mode-map
914 (setq gnus-topic-mode-map (make-sparse-keymap))
916 ;; Override certain group mode keys.
917 (gnus-define-keys gnus-topic-mode-map
918 "=" gnus-topic-select-group
919 "\r" gnus-topic-select-group
920 " " gnus-topic-read-group
921 "\C-c\C-x" gnus-topic-expire-articles
922 "\C-k" gnus-topic-kill-group
923 "\C-y" gnus-topic-yank-group
924 "\M-g" gnus-topic-get-new-news-this-topic
925 "AT" gnus-topic-list-active
926 "Gp" gnus-topic-edit-parameters
927 "#" gnus-topic-mark-topic
928 "\M-#" gnus-topic-unmark-topic
929 [tab] gnus-topic-indent
930 [(meta tab)] gnus-topic-unindent
931 "\C-i" gnus-topic-indent
932 "\M-\C-i" gnus-topic-unindent
933 gnus-mouse-2 gnus-mouse-pick-topic)
935 ;; Define a new submap.
936 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
937 "#" gnus-topic-mark-topic
938 "\M-#" gnus-topic-unmark-topic
939 "n" gnus-topic-create-topic
940 "m" gnus-topic-move-group
941 "D" gnus-topic-remove-group
942 "c" gnus-topic-copy-group
943 "h" gnus-topic-hide-topic
944 "s" gnus-topic-show-topic
945 "j" gnus-topic-jump-to-topic
946 "M" gnus-topic-move-matching
947 "C" gnus-topic-copy-matching
948 "\C-i" gnus-topic-indent
949 [tab] gnus-topic-indent
950 "r" gnus-topic-rename
951 "\177" gnus-topic-delete
952 [delete] gnus-topic-delete
953 "H" gnus-topic-toggle-display-empty-topics)
955 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
956 "s" gnus-topic-sort-groups
957 "a" gnus-topic-sort-groups-by-alphabet
958 "u" gnus-topic-sort-groups-by-unread
959 "l" gnus-topic-sort-groups-by-level
960 "v" gnus-topic-sort-groups-by-score
961 "r" gnus-topic-sort-groups-by-rank
962 "m" gnus-topic-sort-groups-by-method))
964 (defun gnus-topic-make-menu-bar ()
965 (unless (boundp 'gnus-topic-menu)
967 gnus-topic-menu gnus-topic-mode-map ""
969 ["Toggle topics" gnus-topic-mode t]
971 ["Copy" gnus-topic-copy-group t]
972 ["Move" gnus-topic-move-group t]
973 ["Remove" gnus-topic-remove-group t]
974 ["Copy matching" gnus-topic-copy-matching t]
975 ["Move matching" gnus-topic-move-matching t])
977 ["Goto" gnus-topic-jump-to-topic t]
978 ["Show" gnus-topic-show-topic t]
979 ["Hide" gnus-topic-hide-topic t]
980 ["Delete" gnus-topic-delete t]
981 ["Rename" gnus-topic-rename t]
982 ["Create" gnus-topic-create-topic t]
983 ["Mark" gnus-topic-mark-topic t]
984 ["Indent" gnus-topic-indent t]
985 ["Sort" gnus-topic-sort-topics t]
986 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
987 ["Edit parameters" gnus-topic-edit-parameters t])
988 ["List active" gnus-topic-list-active t]))))
990 (defun gnus-topic-mode (&optional arg redisplay)
991 "Minor mode for topicsifying Gnus group buffers."
992 (interactive (list current-prefix-arg t))
993 (when (eq major-mode 'gnus-group-mode)
994 (make-local-variable 'gnus-topic-mode)
995 (setq gnus-topic-mode
996 (if (null arg) (not gnus-topic-mode)
997 (> (prefix-numeric-value arg) 0)))
998 ;; Infest Gnus with topics.
999 (if (not gnus-topic-mode)
1000 (setq gnus-goto-missing-group-function nil)
1001 (when (gnus-visual-p 'topic-menu 'menu)
1002 (gnus-topic-make-menu-bar))
1003 (gnus-set-format 'topic t)
1004 (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
1005 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1006 (set (make-local-variable 'gnus-group-prepare-function)
1007 'gnus-group-prepare-topics)
1008 (set (make-local-variable 'gnus-group-get-parameter-function)
1009 'gnus-group-topic-parameters)
1010 (set (make-local-variable 'gnus-group-goto-next-group-function)
1011 'gnus-topic-goto-next-group)
1012 (set (make-local-variable 'gnus-group-indentation-function)
1013 'gnus-topic-group-indentation)
1014 (set (make-local-variable 'gnus-group-update-group-function)
1015 'gnus-topic-update-topics-containing-group)
1016 (set (make-local-variable 'gnus-group-sort-alist-function)
1017 'gnus-group-sort-topic)
1018 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1019 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1020 (make-local-hook 'gnus-check-bogus-groups-hook)
1021 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1022 (setq gnus-topology-checked-p nil)
1023 ;; We check the topology.
1024 (when gnus-newsrc-alist
1025 (gnus-topic-check-topology))
1026 (gnus-run-hooks 'gnus-topic-mode-hook))
1027 ;; Remove topic infestation.
1028 (unless gnus-topic-mode
1029 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1030 (remove-hook 'gnus-group-change-level-function
1031 'gnus-topic-change-level)
1032 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1033 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1034 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1036 (gnus-group-list-groups))))
1038 (defun gnus-topic-select-group (&optional all)
1039 "Select this newsgroup.
1040 No article is selected automatically.
1041 If ALL is non-nil, already read articles become readable.
1042 If ALL is a number, fetch this number of articles.
1044 If performed over a topic line, toggle folding the topic."
1046 (if (gnus-group-topic-p)
1047 (let ((gnus-group-list-mode
1048 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1049 (gnus-topic-fold all))
1050 (gnus-group-select-group all)))
1052 (defun gnus-mouse-pick-topic (e)
1053 "Select the group or topic under the mouse pointer."
1056 (gnus-topic-read-group nil))
1058 (defun gnus-topic-expire-articles (topic)
1059 "Expire articles in this topic or group."
1060 (interactive (list (gnus-group-topic-name)))
1062 (call-interactively 'gnus-group-expire-articles)
1064 (gnus-message 5 "Expiring groups in %s..." topic)
1065 (let ((gnus-group-marked
1066 (mapcar (lambda (entry) (car (nth 2 entry)))
1067 (gnus-topic-find-groups topic gnus-level-killed t))))
1068 (gnus-group-expire-articles nil))
1069 (gnus-message 5 "Expiring groups in %s...done" topic))))
1071 (defun gnus-topic-read-group (&optional all no-article group)
1072 "Read news in this newsgroup.
1073 If the prefix argument ALL is non-nil, already read articles become
1074 readable. IF ALL is a number, fetch this number of articles. If the
1075 optional argument NO-ARTICLE is non-nil, no article will be
1076 auto-selected upon group entry. If GROUP is non-nil, fetch that
1079 If performed over a topic line, toggle folding the topic."
1081 (if (gnus-group-topic-p)
1082 (let ((gnus-group-list-mode
1083 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1084 (gnus-topic-fold all))
1085 (gnus-group-read-group all no-article group)))
1087 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1088 "Create a new TOPIC under PARENT.
1089 When used interactively, PARENT will be the topic under point."
1092 (read-string "New topic: ")
1093 (gnus-current-topic)))
1094 ;; Check whether this topic already exists.
1095 (when (gnus-topic-find-topology topic)
1096 (error "Topic already exists"))
1098 (setq parent (caar gnus-topic-topology)))
1099 (let ((top (cdr (gnus-topic-find-topology parent)))
1100 (full-topic (or full-topic `((,topic visible)))))
1102 (error "No such parent topic: %s" parent))
1105 (while (and (cdr top)
1106 (not (equal (caaadr top) previous)))
1107 (setq top (cdr top)))
1108 (setcdr top (cons full-topic (cdr top))))
1109 (nconc top (list full-topic)))
1110 (unless (assoc topic gnus-topic-alist)
1111 (push (list topic) gnus-topic-alist)))
1112 (gnus-topic-enter-dribble)
1113 (gnus-group-list-groups)
1114 (gnus-topic-goto-topic topic))
1116 (defun gnus-topic-move-group (n topic &optional copyp)
1117 "Move the next N groups to TOPIC.
1118 If COPYP, copy the groups instead."
1120 (list current-prefix-arg
1121 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1122 (let ((groups (gnus-group-process-prefix n))
1123 (topicl (assoc topic gnus-topic-alist))
1124 (start-topic (gnus-group-topic-name))
1125 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1127 (if (and (not groups) (not copyp) start-topic)
1128 (gnus-topic-move start-topic topic)
1131 (gnus-group-remove-mark g)
1133 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1135 (setcdr entry (gnus-delete-first g (cdr entry))))
1136 (nconc topicl (list g)))
1138 (gnus-topic-enter-dribble)
1140 (gnus-group-goto-group start-group)
1141 (gnus-topic-goto-topic start-topic))
1142 (gnus-group-list-groups))))
1144 (defun gnus-topic-remove-group (&optional arg)
1145 "Remove the current group from the topic."
1147 (gnus-group-iterate arg
1149 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1150 (buffer-read-only nil))
1151 (when (and topicl group)
1153 (gnus-delete-first group topicl))
1154 (gnus-topic-update-topic)
1155 (gnus-group-position-point)))))
1157 (defun gnus-topic-copy-group (n topic)
1158 "Copy the current group to a topic."
1160 (list current-prefix-arg
1161 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1162 (gnus-topic-move-group n topic t))
1164 (defun gnus-topic-kill-group (&optional n discard)
1165 "Kill the next N groups."
1167 (if (gnus-group-topic-p)
1168 (let ((topic (gnus-group-topic-name)))
1170 (gnus-topic-find-topology topic)
1171 (assoc topic gnus-topic-alist))
1172 gnus-topic-killed-topics)
1173 (gnus-topic-remove-topic nil t)
1174 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1175 (gnus-topic-enter-dribble))
1176 (gnus-group-kill-group n discard)
1177 (gnus-topic-update-topic)))
1179 (defun gnus-topic-yank-group (&optional arg)
1180 "Yank the last topic."
1182 (if gnus-topic-killed-topics
1184 (or (gnus-group-topic-name)
1185 (gnus-topic-next-topic (gnus-current-topic))))
1186 (data (pop gnus-topic-killed-topics))
1189 (push alist gnus-topic-alist)
1190 (gnus-topic-create-topic
1191 (caar item) (gnus-topic-parent-topic previous) previous
1193 (gnus-topic-enter-dribble)
1194 (gnus-topic-goto-topic (caar item)))
1195 (let* ((prev (gnus-group-group-name))
1196 (gnus-topic-inhibit-change-level t)
1197 (gnus-group-indentation
1199 (* gnus-topic-indent-level
1201 (gnus-topic-goto-topic (gnus-current-topic))
1202 (gnus-group-topic-level))
1206 ;; We first yank the groups the normal way...
1207 (setq yanked (gnus-group-yank-group arg))
1208 ;; Then we enter the yanked groups into the topics they belong
1210 (setq alist (assoc (save-excursion
1212 (gnus-current-topic))
1214 (when (stringp yanked)
1215 (setq yanked (list yanked)))
1217 (nconc alist yanked)
1218 (if (not (cdr alist))
1219 (setcdr alist (nconc yanked (cdr alist)))
1221 (when (equal (cadr alist) prev)
1222 (setcdr alist (nconc yanked (cdr alist)))
1224 (setq alist (cdr alist))))))
1225 (gnus-topic-update-topic)))
1227 (defun gnus-topic-hide-topic ()
1228 "Hide the current topic."
1230 (when (gnus-current-topic)
1231 (gnus-topic-goto-topic (gnus-current-topic))
1232 (gnus-topic-remove-topic nil nil 'hidden)))
1234 (defun gnus-topic-show-topic ()
1235 "Show the hidden topic."
1237 (when (gnus-group-topic-p)
1238 (gnus-topic-remove-topic t nil 'shown)))
1240 (defun gnus-topic-mark-topic (topic &optional unmark)
1241 "Mark all groups in the topic with the process mark."
1242 (interactive (list (gnus-group-topic-name)))
1244 (call-interactively 'gnus-group-mark-group)
1246 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
1248 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1249 (gnus-info-group (nth 2 (pop groups)))))))))
1251 (defun gnus-topic-unmark-topic (topic &optional unmark)
1252 "Remove the process mark from all groups in the topic."
1253 (interactive (list (gnus-group-topic-name)))
1255 (call-interactively 'gnus-group-unmark-group)
1256 (gnus-topic-mark-topic topic t)))
1258 (defun gnus-topic-get-new-news-this-topic (&optional n)
1259 "Check for new news in the current topic."
1261 (if (not (gnus-group-topic-p))
1262 (gnus-group-get-new-news-this-group n)
1263 (gnus-topic-mark-topic (gnus-group-topic-name))
1264 (gnus-group-get-new-news-this-group)))
1266 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1267 "Move all groups that match REGEXP to some topic."
1272 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1273 (read-string (format "Move to %s (regexp): " topic))))))
1274 (gnus-group-mark-regexp regexp)
1275 (gnus-topic-move-group nil topic copyp))
1277 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1278 "Copy all groups that match REGEXP to some topic."
1283 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1284 (read-string (format "Copy to %s (regexp): " topic))))))
1285 (gnus-topic-move-matching regexp topic t))
1287 (defun gnus-topic-delete (topic)
1289 (interactive (list (gnus-group-topic-name)))
1291 (error "No topic to be deleted"))
1292 (let ((entry (assoc topic gnus-topic-alist))
1293 (buffer-read-only nil))
1295 (error "Topic not empty"))
1296 ;; Delete if visible.
1297 (when (gnus-topic-goto-topic topic)
1299 ;; Remove from alist.
1300 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1301 ;; Remove from topology.
1302 (gnus-topic-find-topology topic nil nil 'delete)
1303 (gnus-dribble-touch)))
1305 (defun gnus-topic-rename (old-name new-name)
1308 (let ((topic (gnus-current-topic)))
1310 (read-string (format "Rename %s to: " topic)))))
1311 ;; Check whether the new name exists.
1312 (when (gnus-topic-find-topology new-name)
1313 (error "Topic '%s' already exists" new-name))
1314 ;; "nil" is an invalid name, for reasons I'd rather not go
1315 ;; into here. Trust me.
1316 (when (equal new-name "nil")
1317 (error "Invalid name: %s" nil))
1319 (let ((top (gnus-topic-find-topology old-name))
1320 (entry (assoc old-name gnus-topic-alist)))
1322 (setcar (cadr top) new-name))
1324 (setcar entry new-name))
1326 (gnus-dribble-touch)
1327 (gnus-group-list-groups)
1330 (defun gnus-topic-indent (&optional unindent)
1331 "Indent a topic -- make it a sub-topic of the previous topic.
1332 If UNINDENT, remove an indentation."
1335 (gnus-topic-unindent)
1336 (let* ((topic (gnus-current-topic))
1337 (parent (gnus-topic-previous-topic topic))
1338 (buffer-read-only nil))
1340 (error "Nothing to indent %s into" topic))
1342 (gnus-topic-goto-topic topic)
1343 (gnus-topic-kill-group)
1344 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1345 (gnus-topic-create-topic
1346 topic parent nil (cdaar gnus-topic-killed-topics))
1347 (pop gnus-topic-killed-topics)
1348 (or (gnus-topic-goto-topic topic)
1349 (gnus-topic-goto-topic parent))))))
1351 (defun gnus-topic-unindent ()
1354 (let* ((topic (gnus-current-topic))
1355 (parent (gnus-topic-parent-topic topic))
1356 (grandparent (gnus-topic-parent-topic parent)))
1358 (error "Nothing to indent %s into" topic))
1360 (gnus-topic-goto-topic topic)
1361 (gnus-topic-kill-group)
1362 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1363 (gnus-topic-create-topic
1364 topic grandparent (gnus-topic-next-topic parent)
1365 (cdaar gnus-topic-killed-topics))
1366 (pop gnus-topic-killed-topics)
1367 (gnus-topic-goto-topic topic))))
1369 (defun gnus-topic-list-active (&optional force)
1370 "List all groups that Gnus knows about in a topicsified fashion.
1371 If FORCE, always re-read the active file."
1374 (gnus-get-killed-groups))
1375 (gnus-topic-grok-active force)
1376 (let ((gnus-topic-topology gnus-topic-active-topology)
1377 (gnus-topic-alist gnus-topic-active-alist)
1378 gnus-killed-list gnus-zombie-list)
1379 (gnus-group-list-groups gnus-level-killed nil 1)))
1381 (defun gnus-topic-toggle-display-empty-topics ()
1382 "Show/hide topics that have no unread articles."
1384 (setq gnus-topic-display-empty-topics
1385 (not gnus-topic-display-empty-topics))
1386 (gnus-group-list-groups)
1387 (message "%s empty topics"
1388 (if gnus-topic-display-empty-topics
1389 "Showing" "Hiding")))
1391 ;;; Topic sorting functions
1393 (defun gnus-topic-edit-parameters (group)
1394 "Edit the group parameters of GROUP.
1395 If performed on a topic, edit the topic parameters instead."
1396 (interactive (list (gnus-group-group-name)))
1398 (gnus-group-edit-group-parameters group)
1399 (if (not (gnus-group-topic-p))
1400 (error "Nothing to edit on the current line")
1401 (let ((topic (gnus-group-topic-name)))
1403 (gnus-topic-parameters topic)
1404 (format "Editing the topic parameters for `%s'."
1407 (gnus-topic-set-parameters ,topic form)))))))
1409 (defun gnus-group-sort-topic (func reverse)
1410 "Sort groups in the topics according to FUNC and REVERSE."
1411 (let ((alist gnus-topic-alist))
1413 ;; !!!Sometimes nil elements sneak into the alist,
1414 ;; for some reason or other.
1415 (setcar alist (delq nil (car alist)))
1416 (setcar alist (delete "dummy.group" (car alist)))
1417 (gnus-topic-sort-topic (pop alist) func reverse))))
1419 (defun gnus-topic-sort-topic (topic func reverse)
1420 ;; Each topic only lists the name of the group, while
1421 ;; the sort predicates expect group infos as inputs.
1422 ;; So we first transform the group names into infos,
1423 ;; then sort, and then transform back into group names.
1427 (lambda (info) (gnus-info-group info))
1430 (lambda (group) (gnus-get-info group))
1433 ;; Do the reversal, if necessary.
1435 (setcdr topic (nreverse (cdr topic)))))
1437 (defun gnus-topic-sort-groups (func &optional reverse)
1438 "Sort the current topic according to FUNC.
1439 If REVERSE, reverse the sorting order."
1440 (interactive (list gnus-group-sort-function current-prefix-arg))
1441 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1442 (gnus-topic-sort-topic
1443 topic (gnus-make-sort-function func) reverse)
1444 (gnus-group-list-groups)))
1446 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1447 "Sort the current topic alphabetically by group name.
1448 If REVERSE, sort in reverse order."
1450 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1452 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1453 "Sort the current topic by number of unread articles.
1454 If REVERSE, sort in reverse order."
1456 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1458 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1459 "Sort the current topic by group level.
1460 If REVERSE, sort in reverse order."
1462 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1464 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1465 "Sort the current topic by group score.
1466 If REVERSE, sort in reverse order."
1468 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1470 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1471 "Sort the current topic by group rank.
1472 If REVERSE, sort in reverse order."
1474 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1476 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1477 "Sort the current topic alphabetically by backend name.
1478 If REVERSE, sort in reverse order."
1480 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1482 (defun gnus-topic-sort-topics-1 (top reverse)
1485 (mapcar `(lambda (top)
1486 (gnus-topic-sort-topics-1 top ,reverse))
1489 (string-lessp (caar t1) (caar t2)))))))
1490 (setcdr top (if reverse (reverse subtop) subtop))))
1493 (defun gnus-topic-sort-topics (&optional topic reverse)
1494 "Sort topics in TOPIC alphabeticaly by topic name.
1495 If REVERSE, reverse the sorting order."
1497 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1498 (gnus-current-topic))
1499 current-prefix-arg))
1500 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1501 gnus-topic-topology)))
1502 (gnus-topic-sort-topics-1 topic-topology reverse)
1503 (gnus-topic-enter-dribble)
1504 (gnus-group-list-groups)
1505 (gnus-topic-goto-topic topic)))
1507 (defun gnus-topic-move (current to)
1508 "Move the CURRENT topic to TO."
1511 (gnus-group-topic-name)
1512 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1513 (unless (and current to)
1514 (error "Can't find topic"))
1515 (let ((current-top (cdr (gnus-topic-find-topology current)))
1516 (to-top (cdr (gnus-topic-find-topology to))))
1518 (error "Can't find topic `%s'" current))
1520 (error "Can't find topic `%s'" to))
1521 (if (gnus-topic-find-topology to current-top 0) ;; Don't care the level
1522 (error "Can't move `%s' to its sub-level" current))
1523 (gnus-topic-find-topology current nil nil 'delete)
1525 (setq to-top (cdr to-top)))
1526 (setcdr to-top (list current-top))
1527 (gnus-topic-enter-dribble)
1528 (gnus-group-list-groups)
1529 (gnus-topic-goto-topic current)))
1531 (provide 'gnus-topic)
1533 ;;; gnus-topic.el ends here