1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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))
31 (eval-when-compile (require 'gnus-clfns))
38 (defgroup gnus-topic nil
42 (defvar gnus-topic-mode nil
43 "Minor mode for Gnus group buffers.")
45 (defcustom gnus-topic-mode-hook nil
46 "Hook run in topic mode buffers."
50 (when (featurep 'xemacs)
51 (add-hook 'gnus-topic-mode-hook 'gnus-xmas-topic-menu-add))
53 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
54 "Format of topic lines.
55 It works along the same lines as a normal formatting string,
56 with some simple extensions.
58 %i Indentation based on topic level.
60 %v Nothing if the topic is visible, \"...\" otherwise.
61 %g Number of groups in the topic.
62 %a Number of unread articles in the groups in the topic.
63 %A Number of unread articles in the groups in the topic and its subtopics.
65 General format specifiers can also be used.
66 See Info node `(gnus)Formatting Variables'."
67 :link '(custom-manual "(gnus)Formatting Variables")
71 (defcustom gnus-topic-indent-level 2
72 "*How much each subtopic should be indented."
76 (defcustom gnus-topic-display-empty-topics t
77 "*If non-nil, display the topic lines even of topics that have no unread articles."
81 ;; Internal variables.
83 (defvar gnus-topic-active-topology nil)
84 (defvar gnus-topic-active-alist nil)
85 (defvar gnus-topic-unreads nil)
87 (defvar gnus-topology-checked-p nil
88 "Whether the topology has been checked in this session.")
90 (defvar gnus-topic-killed-topics nil)
91 (defvar gnus-topic-inhibit-change-level nil)
93 (defconst gnus-topic-line-format-alist
97 (?g number-of-groups ?d)
98 (?a (gnus-topic-articles-in-topic entries) ?d)
99 (?A total-number-of-articles ?d)
102 (defvar gnus-topic-line-format-spec nil)
104 ;;; Utility functions
106 (defun gnus-group-topic-name ()
107 "The name of the topic on the current line."
108 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
109 (and topic (symbol-name topic))))
111 (defun gnus-group-topic-level ()
112 "The level of the topic on the current line."
113 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
115 (defun gnus-group-topic-unread ()
116 "The number of unread articles in topic on the current line."
117 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
119 (defun gnus-topic-unread (topic)
120 "Return the number of unread articles in TOPIC."
121 (or (cdr (assoc topic gnus-topic-unreads))
124 (defun gnus-group-topic-p ()
125 "Return non-nil if the current line is a topic."
126 (gnus-group-topic-name))
128 (defun gnus-topic-visible-p ()
129 "Return non-nil if the current topic is visible."
130 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
132 (defun gnus-topic-articles-in-topic (entries)
136 (when (numberp (setq number (car (pop entries))))
137 (incf total number)))
140 (defun gnus-group-topic (group)
141 "Return the topic GROUP is a member of."
142 (let ((alist gnus-topic-alist)
145 (when (member group (cdar alist))
146 (setq out (caar alist)
148 (setq alist (cdr alist)))
151 (defun gnus-group-parent-topic (group)
152 "Return the topic GROUP is member of by looking at the group buffer."
154 (set-buffer gnus-group-buffer)
155 (if (gnus-group-goto-group group)
157 (gnus-group-topic group))))
159 (defun gnus-topic-goto-topic (topic)
161 (gnus-goto-char (text-property-any (point-min) (point-max)
162 'gnus-topic (intern topic)))))
164 (defun gnus-topic-jump-to-topic (topic)
167 (list (completing-read "Go to topic: "
168 (mapcar 'list (gnus-topic-list))
170 (dolist (topic (gnus-current-topics topic))
171 (gnus-topic-goto-topic topic)
173 (gnus-topic-goto-topic topic))
175 (defun gnus-current-topic ()
176 "Return the name of the current topic."
178 (or (get-text-property (point) 'gnus-topic)
180 (and (gnus-goto-char (previous-single-property-change
181 (point) 'gnus-topic))
182 (get-text-property (max (1- (point)) (point-min))
185 (symbol-name result))))
187 (defun gnus-current-topics (&optional topic)
188 "Return a list of all current topics, lowest in hierarchy first.
189 If TOPIC, start with that topic."
190 (let ((topic (or topic (gnus-current-topic)))
194 (setq topic (gnus-topic-parent-topic topic)))
197 (defun gnus-group-active-topic-p ()
198 "Say whether the current topic comes from the active topics."
201 (get-text-property (point) 'gnus-active)))
203 (defun gnus-topic-find-groups (topic &optional level all lowest recursive)
204 "Return entries for all visible groups in TOPIC.
205 If RECURSIVE is t, return groups in its subtopics too."
206 (let ((groups (cdr (assoc topic gnus-topic-alist)))
207 info clevel unread group params visible-groups entry active)
208 (setq lowest (or lowest 1))
209 (setq level (or level gnus-level-unsubscribed))
210 ;; We go through the newsrc to look for matches.
212 (when (setq group (pop groups))
213 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
215 params (gnus-info-params info)
216 active (gnus-active group)
217 unread (or (car entry)
218 (and (not (equal group "dummy.group"))
220 (- (1+ (cdr active)) (car active))))
221 clevel (or (gnus-info-level info)
222 (if (member group gnus-zombie-list)
223 gnus-level-zombie gnus-level-killed))))
225 info ; nil means that the group is dead.
227 (>= clevel lowest) ; Is inside the level we want.
229 (if (or (eq unread t)
231 gnus-group-list-inactive-groups
233 (and gnus-list-groups-with-ticked-articles
234 (cdr (assq 'tick (gnus-info-marks info))))
235 ;; Has right readedness.
236 ;; Check for permanent visibility.
237 (and gnus-permanently-visible-groups
238 (string-match gnus-permanently-visible-groups group))
239 (memq 'visible params)
240 (cdr (assq 'visible params)))
241 ;; Add this group to the list of visible groups.
242 (push (or entry group) visible-groups)))
243 (setq visible-groups (nreverse visible-groups))
246 (setq recursive (cdr (gnus-topic-find-topology topic))))
247 (mapcar (lambda (topic-topology)
249 (nconc visible-groups
250 (gnus-topic-find-groups
251 (caar topic-topology)
252 level all lowest topic-topology))))
256 (defun gnus-topic-goto-previous-topic (n)
257 "Go to the N'th previous topic."
259 (gnus-topic-goto-next-topic (- n)))
261 (defun gnus-topic-goto-next-topic (n)
262 "Go to the N'th next topic."
264 (let ((backward (< n 0))
266 (topic (gnus-current-topic)))
270 (gnus-topic-previous-topic topic)
271 (gnus-topic-next-topic topic))))
272 (gnus-topic-goto-topic topic)
275 (gnus-message 7 "No more topics"))
278 (defun gnus-topic-previous-topic (topic)
279 "Return the previous topic on the same level as TOPIC."
280 (let ((top (cddr (gnus-topic-find-topology
281 (gnus-topic-parent-topic topic)))))
282 (unless (equal topic (caaar top))
283 (while (and top (not (equal (caaadr top) topic)))
284 (setq top (cdr top)))
287 (defun gnus-topic-parent-topic (topic &optional topology)
288 "Return the parent of TOPIC."
290 (setq topology gnus-topic-topology))
291 (let ((parent (car (pop topology)))
294 (not (setq found (equal (caaar topology) topic)))
295 (not (setq result (gnus-topic-parent-topic
296 topic (car topology)))))
297 (setq topology (cdr topology)))
298 (or result (and found parent))))
300 (defun gnus-topic-next-topic (topic &optional previous)
301 "Return the next sibling of TOPIC."
302 (let ((parentt (cddr (gnus-topic-find-topology
303 (gnus-topic-parent-topic topic))))
306 (not (equal (caaar parentt) topic)))
307 (setq prev (caaar parentt)
308 parentt (cdr parentt)))
313 (defun gnus-topic-forward-topic (num)
314 "Go to the next topic on the same level as the current one."
315 (let* ((topic (gnus-current-topic))
316 (way (if (< num 0) 'gnus-topic-previous-topic
317 'gnus-topic-next-topic))
319 (while (and (not (zerop num))
320 (setq topic (funcall way topic)))
321 (when (gnus-topic-goto-topic topic)
324 (goto-char (point-max)))
327 (defun gnus-topic-find-topology (topic &optional topology level remove)
328 "Return the topology of TOPIC."
330 (setq topology gnus-topic-topology)
334 (if (equal (caar topology) topic)
337 (delq topology remove))
338 (cons level topology))
339 (setq topology (cdr topology))
341 (not (setq result (gnus-topic-find-topology
342 topic (car topology) (1+ level)
344 (setq topology (cdr topology)))
347 (defvar gnus-tmp-topics nil)
348 (defun gnus-topic-list (&optional topology)
349 "Return a list of all topics in the topology."
351 (setq topology gnus-topic-topology
352 gnus-tmp-topics nil))
353 (push (caar topology) gnus-tmp-topics)
354 (mapcar 'gnus-topic-list (cdr topology))
357 ;;; Topic parameter jazz
359 (defun gnus-topic-parameters (topic)
360 "Return the parameters for TOPIC."
361 (let ((top (gnus-topic-find-topology topic)))
363 (nth 3 (cadr top)))))
365 (defun gnus-topic-set-parameters (topic parameters)
366 "Set the topic parameters of TOPIC to PARAMETERS."
367 (let ((top (gnus-topic-find-topology topic)))
369 (error "No such topic: %s" topic))
370 ;; We may have to extend if there is no parameters here
372 (unless (nthcdr 2 (cadr top))
373 (nconc (cadr top) (list nil)))
374 (unless (nthcdr 3 (cadr top))
375 (nconc (cadr top) (list nil)))
376 (setcar (nthcdr 3 (cadr top)) parameters)
378 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
380 (defun gnus-group-topic-parameters (group)
381 "Compute the group parameters for GROUP taking into account inheritance from topics."
382 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
385 (gnus-topic-hierarchical-parameters
386 ;; First we try to go to the group within the group
387 ;; buffer and find the topic for the group that way.
388 ;; This hopefully copes well with groups that are in
389 ;; more than one topic. Failing that (i.e. when the
390 ;; group isn't visible in the group buffer) we find a
391 ;; topic for the group via gnus-group-topic.
392 (or (and (gnus-group-goto-group group)
393 (gnus-current-topic))
394 (gnus-group-topic group)))))))
396 (defun gnus-topic-hierarchical-parameters (topic)
397 "Return a topic list computed for TOPIC."
398 (let ((topics (gnus-current-topics topic))
399 params-list param out params)
401 (push (gnus-topic-parameters (pop topics)) params-list))
402 ;; We probably have lots of nil elements here, so
403 ;; we remove them. Probably faster than doing this "properly".
404 (setq params-list (delq nil params-list))
405 ;; Now we have all the parameters, so we go through them
406 ;; and do inheritance in the obvious way.
407 (while (setq params (pop params-list))
408 (while (setq param (pop params))
410 (setq param (cons param t)))
411 ;; Override any old versions of this param.
412 (gnus-pull (car param) out)
414 ;; Return the resulting parameter list.
417 ;;; General utility functions
419 (defun gnus-topic-enter-dribble ()
421 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
423 ;;; Generating group buffers
425 (defun gnus-group-prepare-topics (level &optional predicate lowest
426 regexp list-topic topic-level)
427 "List all newsgroups with unread articles of level LEVEL or lower.
428 Use the `gnus-group-topics' to sort the groups.
429 If PREDICTE is a function, list groups that the function returns non-nil;
430 if it is t, list groups that have no unread articles.
431 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
432 (set-buffer gnus-group-buffer)
433 (let ((buffer-read-only nil)
434 (lowest (or lowest 1))
436 (and gnus-group-listed-groups
437 (copy-sequence gnus-group-listed-groups))))
439 (when (or (not gnus-topic-alist)
440 (not gnus-topology-checked-p))
441 (gnus-topic-check-topology))
447 (when (or gnus-group-listed-groups
448 (and (>= level gnus-level-zombie)
449 (<= lowest gnus-level-zombie)))
450 (gnus-group-prepare-flat-list-dead
451 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
455 (when (or gnus-group-listed-groups
456 (and (>= level gnus-level-killed)
457 (<= lowest gnus-level-killed)))
458 (gnus-group-prepare-flat-list-dead
459 (setq gnus-killed-list (sort gnus-killed-list 'string<))
460 gnus-level-killed ?K regexp)
462 (unless gnus-killed-hashtb
463 (gnus-make-hashtable-from-killed))
464 (gnus-group-prepare-flat-list-dead
465 (gnus-delete-if (lambda (group)
466 (or (gnus-gethash group gnus-newsrc-hashtb)
467 (gnus-gethash group gnus-killed-hashtb)))
469 gnus-level-killed ?K regexp)))
473 (when (or (< lowest gnus-level-zombie)
474 gnus-group-listed-groups)
476 (let ((top (gnus-topic-find-topology list-topic)))
477 (gnus-topic-prepare-topic (cdr top) (car top)
478 (or topic-level level) predicate
480 (gnus-topic-prepare-topic gnus-topic-topology 0
481 (or topic-level level) predicate
483 (gnus-group-set-mode-line)
484 (setq gnus-group-list-mode (cons level predicate))
485 (gnus-run-hooks 'gnus-group-prepare-hook))))
487 (defun gnus-topic-prepare-topic (topicl level &optional list-level
490 "Insert TOPIC into the group buffer.
491 If SILENT, don't insert anything. Return the number of unread
492 articles in the topic and its subtopics."
493 (let* ((type (pop topicl))
494 (entries (gnus-topic-find-groups
496 (if gnus-group-listed-groups
499 (or predicate gnus-group-listed-groups
501 (gnus-topic-hierarchical-parameters
503 (if gnus-group-listed-groups 0 lowest)))
504 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
505 (gnus-group-indentation
506 (make-string (* gnus-topic-indent-level level) ? ))
507 (beg (progn (beginning-of-line) (point)))
508 (topicl (reverse topicl))
509 (all-entries entries)
510 (point-max (point-max))
513 info entry end active tick)
514 ;; Insert any sub-topics.
517 (gnus-topic-prepare-topic
518 (pop topicl) (1+ level) list-level predicate
519 (not visiblep) lowest regexp)))
522 ;; Insert all the groups that belong in this topic.
523 (while (setq entry (pop entries))
524 (when (if (stringp entry)
525 (gnus-group-prepare-logic
528 (or (not gnus-group-listed-groups)
529 (if (< list-level gnus-level-zombie) nil
531 (if (member entry gnus-zombie-list)
532 gnus-level-zombie gnus-level-killed)))
533 (and (<= entry-level list-level)
534 (>= entry-level lowest)))))
537 (string-match regexp entry))
539 (funcall regexp entry))
542 (setq info (nth 2 entry))
543 (gnus-group-prepare-logic
544 (gnus-info-group info)
545 (and (or (not gnus-group-listed-groups)
546 (let ((entry-level (gnus-info-level info)))
547 (and (<= entry-level list-level)
548 (>= entry-level lowest))))
549 (or (not (functionp predicate))
550 (funcall predicate info))
551 (or (not (stringp regexp))
552 (string-match regexp (gnus-info-group info))))))
556 (gnus-group-insert-group-line
557 entry (if (member entry gnus-zombie-list)
558 gnus-level-zombie gnus-level-killed)
559 nil (- (1+ (cdr (setq active (gnus-active entry))))
563 (when (setq info (nth 2 entry))
564 (gnus-group-insert-group-line
565 (gnus-info-group info)
566 (gnus-info-level info) (gnus-info-marks info)
567 (car entry) (gnus-info-method info)))))
568 (when (and (listp entry)
569 (numberp (car entry)))
570 (incf unread (car entry)))
574 ;; Insert the topic line.
575 (when (and (not silent)
576 (or gnus-topic-display-empty-topics ;We want empty topics
577 (not (zerop unread)) ;Non-empty
578 tick ;Ticked articles
579 (/= point-max (point-max)))) ;Unactivated groups
580 (gnus-extent-start-open (point))
581 (gnus-topic-insert-topic-line
583 (not (eq (nth 2 type) 'hidden))
584 level all-entries unread))
585 (gnus-topic-update-unreads (car type) unread)
589 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
590 "Remove the current topic."
591 (let ((topic (gnus-group-topic-name))
592 (level (gnus-group-topic-level))
593 (beg (progn (beginning-of-line) (point)))
596 (while (and (zerop (forward-line 1))
597 (> (or (gnus-group-topic-level) (1+ level)) level)))
598 (delete-region beg (point))
599 ;; Do the change in this rather odd manner because it has been
600 ;; reported that some topics share parts of some lists, for some
601 ;; reason. I have been unable to determine why this is the
602 ;; case, but this hack seems to take care of things.
603 (let ((data (cadr (gnus-topic-find-topology topic))))
605 (list (if insert 'visible 'invisible)
609 (setq gnus-topic-alist
610 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
611 (gnus-topic-insert-topic topic in-level)))))
613 (defun gnus-topic-insert-topic (topic &optional level)
615 (gnus-group-prepare-topics
616 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
617 nil nil topic level))
619 (defun gnus-topic-fold (&optional insert topic)
620 "Remove/insert the current topic."
621 (let ((topic (or topic (gnus-group-topic-name))))
624 (if (not (gnus-group-active-topic-p))
625 (gnus-topic-remove-topic
626 (or insert (not (gnus-topic-visible-p))))
627 (let ((gnus-topic-topology gnus-topic-active-topology)
628 (gnus-topic-alist gnus-topic-active-alist)
629 (gnus-group-list-mode (cons 5 t)))
630 (gnus-topic-remove-topic
631 (or insert (not (gnus-topic-visible-p))) nil nil 9)
632 (gnus-topic-enter-dribble)))))))
634 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
636 (let* ((visible (if visiblep "" "..."))
637 (indentation (make-string (* gnus-topic-indent-level level) ? ))
638 (total-number-of-articles unread)
639 (number-of-groups (length entries))
640 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
642 (gnus-topic-update-unreads name unread)
646 (gnus-add-text-properties
649 (eval gnus-topic-line-format-spec))
650 (list 'gnus-topic (intern name)
651 'gnus-topic-level level
652 'gnus-topic-unread unread
653 'gnus-active active-topic
654 'gnus-topic-visible visiblep)))))
656 (defun gnus-topic-update-unreads (topic unreads)
657 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
659 (push (cons topic unreads) gnus-topic-unreads))
661 (defun gnus-topic-update-topics-containing-group (group)
662 "Update all topics that have GROUP as a member."
663 (when (and (eq major-mode 'gnus-group-mode)
666 (let ((alist gnus-topic-alist))
667 ;; This is probably not entirely correct. If a topic
668 ;; isn't shown, then it's not updated. But the updating
669 ;; should be performed in any case, since the topic's
670 ;; parent should be updated. Pfft.
672 (when (and (member group (cdar alist))
673 (gnus-topic-goto-topic (caar alist)))
674 (gnus-topic-update-topic-line (caar alist)))
677 (defun gnus-topic-update-topic ()
678 "Update all parent topics to the current group."
679 (when (and (eq major-mode 'gnus-group-mode)
681 (let ((group (gnus-group-group-name))
683 (buffer-read-only nil))
685 (gnus-get-info group)
686 (gnus-topic-goto-topic (gnus-current-topic)))
687 (gnus-topic-update-topic-line (gnus-group-topic-name))
690 (gnus-group-position-point)))))
692 (defun gnus-topic-goto-missing-group (group)
693 "Place point where GROUP is supposed to be inserted."
694 (let* ((topic (gnus-group-topic group))
695 (groups (cdr (assoc topic gnus-topic-alist)))
696 (g (cdr (member group groups)))
699 ;; Try to jump to a visible group.
701 (not (gnus-group-goto-group (car g) t)))
703 ;; It wasn't visible, so we try to see where to insert it.
705 (setq g (cdr (member group (reverse groups))))
706 (while (and g unfound)
707 (when (gnus-group-goto-group (pop g) t)
712 (not (gnus-topic-goto-missing-topic topic)))
713 (gnus-topic-display-missing-topic topic)))))
715 (defun gnus-topic-display-missing-topic (topic)
716 "Insert topic lines recursively for missing topics."
717 (let ((parent (gnus-topic-find-topology
718 (gnus-topic-parent-topic topic))))
720 (not (gnus-topic-goto-missing-topic (caadr parent))))
721 (gnus-topic-display-missing-topic (caadr parent))))
722 (gnus-topic-goto-missing-topic topic)
723 (let* ((top (gnus-topic-find-topology topic))
724 (children (cddr top))
727 (entries (gnus-topic-find-groups
728 (car type) (car gnus-group-list-mode)
729 (cdr gnus-group-list-mode)))
732 (incf unread (gnus-topic-unread (caar (pop children)))))
733 (while (setq entry (pop entries))
734 (when (numberp (car entry))
735 (incf unread (car entry))))
736 (gnus-topic-insert-topic-line
737 topic t t (car (gnus-topic-find-topology topic)) nil unread)))
739 (defun gnus-topic-goto-missing-topic (topic)
740 (if (gnus-topic-goto-topic topic)
742 ;; Topic not displayed.
743 (let* ((top (gnus-topic-find-topology
744 (gnus-topic-parent-topic topic)))
745 (tp (reverse (cddr top))))
747 (gnus-topic-insert-topic-line
748 topic t t (car (gnus-topic-find-topology topic)) nil 0)
749 (while (not (equal (caaar tp) topic))
753 (not (gnus-topic-goto-topic (caaar tp))))
756 (gnus-topic-forward-topic 1)
757 (gnus-topic-goto-missing-topic (caadr top)))))
760 (defun gnus-topic-update-topic-line (topic-name &optional reads)
761 (let* ((top (gnus-topic-find-topology topic-name))
763 (children (cddr top))
764 (entries (gnus-topic-find-groups
765 (car type) (car gnus-group-list-mode)
766 (cdr gnus-group-list-mode)))
767 (parent (gnus-topic-parent-topic topic-name))
768 (all-entries entries)
770 old-unread entry new-unread)
771 (when (gnus-topic-goto-topic (car type))
772 ;; Tally all the groups that belong in this topic.
774 (setq unread (- (gnus-group-topic-unread) reads))
776 (incf unread (gnus-topic-unread (caar (pop children)))))
777 (while (setq entry (pop entries))
778 (when (numberp (car entry))
779 (incf unread (car entry)))))
780 (setq old-unread (gnus-group-topic-unread))
781 ;; Insert the topic line.
782 (gnus-topic-insert-topic-line
783 (car type) (gnus-topic-visible-p)
784 (not (eq (nth 2 type) 'hidden))
785 (gnus-group-topic-level) all-entries unread)
788 (setq new-unread (gnus-group-topic-unread)))
791 (gnus-topic-update-topic-line
793 (- (or old-unread 0) (or new-unread 0))))
796 (defun gnus-topic-group-indentation ()
798 (* gnus-topic-indent-level
801 (gnus-topic-goto-topic (gnus-current-topic))
802 (gnus-group-topic-level))
808 (gnus-add-shutdown 'gnus-topic-close 'gnus)
810 (defun gnus-topic-close ()
811 (setq gnus-topic-active-topology nil
812 gnus-topic-active-alist nil
813 gnus-topic-killed-topics nil
814 gnus-topology-checked-p nil))
816 (defun gnus-topic-check-topology ()
817 ;; The first time we set the topology to whatever we have
818 ;; gotten here, which can be rather random.
819 (unless gnus-topic-alist
820 (gnus-topic-init-alist))
822 (setq gnus-topology-checked-p t)
823 ;; Go through the topic alist and make sure that all topics
824 ;; are in the topic topology.
825 (let ((topics (gnus-topic-list))
826 (alist gnus-topic-alist)
829 (unless (member (caar alist) topics)
830 (nconc gnus-topic-topology
831 (list (list (list (caar alist) 'visible))))
833 (setq alist (cdr alist)))
835 (gnus-topic-enter-dribble))
836 ;; Conversely, go through the topology and make sure that all
837 ;; topologies have alists.
839 (unless (assoc (car topics) gnus-topic-alist)
840 (push (list (car topics)) gnus-topic-alist))
842 ;; Go through all living groups and make sure that
843 ;; they belong to some topic.
844 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
846 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
847 (newsrc (cdr gnus-newsrc-alist))
850 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
851 (setcdr entry (list group))
852 (setq entry (cdr entry)))))
853 ;; Go through all topics and make sure they contain only living groups.
854 (let ((alist gnus-topic-alist)
856 (while (setq topic (pop alist))
858 (if (and (cadr topic)
859 (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
860 (setq topic (cdr topic))
861 (setcdr topic (cddr topic)))))))
863 (defun gnus-topic-init-alist ()
864 "Initialize the topic structures."
865 (setq gnus-topic-topology
866 (cons (list "Gnus" 'visible)
867 (mapcar (lambda (topic)
868 (list (list (car topic) 'visible)))
870 (setq gnus-topic-alist
872 (mapcar (lambda (info) (gnus-info-group info))
873 (cdr gnus-newsrc-alist)))
875 (gnus-topic-enter-dribble))
879 (defun gnus-topic-clean-alist ()
880 "Remove bogus groups from the topic alist."
881 (let ((topic-alist gnus-topic-alist)
883 (unless gnus-killed-hashtb
884 (gnus-make-hashtable-from-killed))
885 (while (setq topic (pop topic-alist))
886 (let ((topic-name (pop topic))
887 group filtered-topic)
888 (while (setq group (pop topic))
889 (when (and (or (gnus-gethash group gnus-active-hashtb)
890 (gnus-info-method (gnus-get-info group)))
891 (not (gnus-gethash group gnus-killed-hashtb)))
892 (push group filtered-topic)))
893 (push (cons topic-name (nreverse filtered-topic)) result)))
894 (setq gnus-topic-alist (nreverse result))))
896 (defun gnus-topic-change-level (group level oldlevel &optional previous)
897 "Run when changing levels to enter/remove groups from topics."
899 (set-buffer gnus-group-buffer)
900 (let ((buffer-read-only nil))
901 (unless gnus-topic-inhibit-change-level
902 (gnus-group-goto-group (or (car (nth 2 previous)) group))
903 (when (and gnus-topic-mode
905 (not gnus-topic-inhibit-change-level))
906 ;; Remove the group from the topics.
907 (if (and (< oldlevel gnus-level-zombie)
908 (>= level gnus-level-zombie))
909 (let ((alist gnus-topic-alist))
910 (while (gnus-group-goto-group group)
913 (when (member group (car alist))
914 (setcdr (car alist) (delete group (cdar alist))))
916 ;; If the group is subscribed we enter it into the topics.
917 (when (and (< level gnus-level-zombie)
918 (>= oldlevel gnus-level-zombie))
919 (let* ((prev (gnus-group-group-name))
920 (gnus-topic-inhibit-change-level t)
921 (gnus-group-indentation
923 (* gnus-topic-indent-level
925 (gnus-topic-goto-topic (gnus-current-topic))
926 (gnus-group-topic-level))
929 (yanked (list group))
931 ;; Then we enter the yanked groups into the topics they belong
933 (when (setq alist (assoc (save-excursion
937 (caar gnus-topic-topology)))
940 (when (stringp yanked)
941 (setq yanked (list yanked)))
944 (if (not (cdr alist))
945 (setcdr alist (nconc yanked (cdr alist)))
946 (while (and (not end) (cdr alist))
947 (when (equal (cadr alist) prev)
948 (setcdr alist (nconc yanked (cdr alist)))
950 (setq alist (cdr alist)))
952 (nconc talist yanked))))))
953 (gnus-topic-update-topic))))))))
955 (defun gnus-topic-goto-next-group (group props)
956 "Go to group or the next group after group."
958 (if (not (memq 'gnus-topic props))
959 (goto-char (point-max))
960 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
961 (if (gnus-group-goto-group group)
963 ;; The group is no longer visible.
964 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
965 (after (cdr (member group (cdr list)))))
966 ;; First try to put point on a group after the current one.
968 (not (gnus-group-goto-group (car after))))
969 (setq after (cdr after)))
970 ;; Then try to put point on a group before point.
972 (setq after (cdr (member group (reverse (cdr list)))))
974 (not (gnus-group-goto-group (car after))))
975 (setq after (cdr after))))
976 ;; Finally, just put point on the topic.
978 (goto-char (point-min))
980 (gnus-topic-goto-topic (car list))
984 ;;; Topic-active functions
986 (defun gnus-topic-grok-active (&optional force)
987 "Parse all active groups and create topic structures for them."
988 ;; First we make sure that we have really read the active file.
990 (not gnus-topic-active-alist))
992 ;; Get a list of all groups available.
993 (mapatoms (lambda (g) (when (symbol-value g)
994 (push (symbol-name g) groups)))
996 (setq groups (sort groups 'string<))
997 ;; Init the variables.
998 (setq gnus-topic-active-topology (list (list "" 'visible)))
999 (setq gnus-topic-active-alist nil)
1000 ;; Descend the top-level hierarchy.
1001 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
1002 ;; Set the top-level topic names to something nice.
1003 (setcar (car gnus-topic-active-topology) "Gnus active")
1004 (setcar (car gnus-topic-active-alist) "Gnus active"))))
1006 (defun gnus-topic-grok-active-1 (topology groups)
1007 (let* ((name (caar topology))
1008 (prefix (concat "^" (regexp-quote name)))
1009 tgroups ntopology group)
1011 (string-match prefix (setq group (car groups))))
1012 (if (not (string-match "\\." group (match-end 0)))
1013 ;; There are no further hierarchies here, so we just
1014 ;; enter this group into the list belonging to this
1016 (push (pop groups) tgroups)
1017 ;; New sub-hierarchy, so we add it to the topology.
1018 (nconc topology (list (setq ntopology
1019 (list (list (substring
1020 group 0 (match-end 0))
1022 ;; Descend the hierarchy.
1023 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
1024 ;; We remove the trailing "." from the topic name.
1026 (if (string-match "\\.$" name)
1027 (substring name 0 (match-beginning 0))
1029 ;; Add this topic and its groups to the topic alist.
1030 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
1031 (setcar (car topology) name)
1032 ;; We return the rest of the groups that didn't belong
1036 ;;; Topic mode, commands and keymap.
1038 (defvar gnus-topic-mode-map nil)
1039 (defvar gnus-group-topic-map nil)
1041 (unless gnus-topic-mode-map
1042 (setq gnus-topic-mode-map (make-sparse-keymap))
1044 ;; Override certain group mode keys.
1045 (gnus-define-keys gnus-topic-mode-map
1046 "=" gnus-topic-select-group
1047 "\r" gnus-topic-select-group
1048 " " gnus-topic-read-group
1049 "\C-c\C-x" gnus-topic-expire-articles
1050 "c" gnus-topic-catchup-articles
1051 "\C-k" gnus-topic-kill-group
1052 "\C-y" gnus-topic-yank-group
1053 "\M-g" gnus-topic-get-new-news-this-topic
1054 "AT" gnus-topic-list-active
1055 "Gp" gnus-topic-edit-parameters
1056 "#" gnus-topic-mark-topic
1057 "\M-#" gnus-topic-unmark-topic
1058 [tab] gnus-topic-indent
1059 [(meta tab)] gnus-topic-unindent
1060 "\C-i" gnus-topic-indent
1061 "\M-\C-i" gnus-topic-unindent
1062 gnus-mouse-2 gnus-mouse-pick-topic)
1064 ;; Define a new submap.
1065 (gnus-define-keys (gnus-group-topic-map "T" gnus-topic-mode-map)
1066 "#" gnus-topic-mark-topic
1067 "\M-#" gnus-topic-unmark-topic
1068 "n" gnus-topic-create-topic
1069 "m" gnus-topic-move-group
1070 "D" gnus-topic-remove-group
1071 "c" gnus-topic-copy-group
1072 "h" gnus-topic-hide-topic
1073 "s" gnus-topic-show-topic
1074 "j" gnus-topic-jump-to-topic
1075 "M" gnus-topic-move-matching
1076 "C" gnus-topic-copy-matching
1077 "\M-p" gnus-topic-goto-previous-topic
1078 "\M-n" gnus-topic-goto-next-topic
1079 "\C-i" gnus-topic-indent
1080 [tab] gnus-topic-indent
1081 "r" gnus-topic-rename
1082 "\177" gnus-topic-delete
1083 [delete] gnus-topic-delete
1084 "H" gnus-topic-toggle-display-empty-topics)
1086 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1087 "s" gnus-topic-sort-groups
1088 "a" gnus-topic-sort-groups-by-alphabet
1089 "u" gnus-topic-sort-groups-by-unread
1090 "l" gnus-topic-sort-groups-by-level
1091 "e" gnus-topic-sort-groups-by-server
1092 "v" gnus-topic-sort-groups-by-score
1093 "r" gnus-topic-sort-groups-by-rank
1094 "m" gnus-topic-sort-groups-by-method))
1096 (defun gnus-topic-make-menu-bar ()
1097 (unless (boundp 'gnus-topic-menu)
1099 gnus-topic-menu gnus-topic-mode-map ""
1101 ["Toggle topics" gnus-topic-mode t]
1103 ["Copy..." gnus-topic-copy-group t]
1104 ["Move..." gnus-topic-move-group t]
1105 ["Remove" gnus-topic-remove-group t]
1106 ["Copy matching..." gnus-topic-copy-matching t]
1107 ["Move matching" gnus-topic-move-matching t])
1109 ["Goto..." gnus-topic-jump-to-topic t]
1110 ["Show" gnus-topic-show-topic t]
1111 ["Hide" gnus-topic-hide-topic t]
1112 ["Delete" gnus-topic-delete t]
1113 ["Rename..." gnus-topic-rename t]
1114 ["Create..." gnus-topic-create-topic t]
1115 ["Mark" gnus-topic-mark-topic t]
1116 ["Indent" gnus-topic-indent t]
1117 ["Sort" gnus-topic-sort-topics t]
1118 ["Previous topic" gnus-topic-goto-previous-topic t]
1119 ["Next topic" gnus-topic-goto-next-topic t]
1120 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1121 ["Edit parameters" gnus-topic-edit-parameters t])
1122 ["List active" gnus-topic-list-active t]))))
1124 (defun gnus-topic-mode (&optional arg redisplay)
1125 "Minor mode for topicsifying Gnus group buffers."
1126 (interactive (list current-prefix-arg t))
1127 (when (eq major-mode 'gnus-group-mode)
1128 (make-local-variable 'gnus-topic-mode)
1129 (setq gnus-topic-mode
1130 (if (null arg) (not gnus-topic-mode)
1131 (> (prefix-numeric-value arg) 0)))
1132 ;; Infest Gnus with topics.
1133 (if (not gnus-topic-mode)
1134 (setq gnus-goto-missing-group-function nil)
1135 (when (gnus-visual-p 'topic-menu 'menu)
1136 (gnus-topic-make-menu-bar))
1137 (gnus-set-format 'topic t)
1138 (gnus-add-minor-mode 'gnus-topic-mode " Topic"
1139 gnus-topic-mode-map nil (lambda (&rest junk)
1141 (gnus-topic-mode nil t)))
1142 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1143 (set (make-local-variable 'gnus-group-prepare-function)
1144 'gnus-group-prepare-topics)
1145 (set (make-local-variable 'gnus-group-get-parameter-function)
1146 'gnus-group-topic-parameters)
1147 (set (make-local-variable 'gnus-group-goto-next-group-function)
1148 'gnus-topic-goto-next-group)
1149 (set (make-local-variable 'gnus-group-indentation-function)
1150 'gnus-topic-group-indentation)
1151 (set (make-local-variable 'gnus-group-update-group-function)
1152 'gnus-topic-update-topics-containing-group)
1153 (set (make-local-variable 'gnus-group-sort-alist-function)
1154 'gnus-group-sort-topic)
1155 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1156 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1157 (make-local-hook 'gnus-check-bogus-groups-hook)
1158 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1160 (setq gnus-topology-checked-p nil)
1161 ;; We check the topology.
1162 (when gnus-newsrc-alist
1163 (gnus-topic-check-topology))
1164 (gnus-run-hooks 'gnus-topic-mode-hook))
1165 ;; Remove topic infestation.
1166 (unless gnus-topic-mode
1167 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1168 (setq gnus-group-change-level-function nil)
1169 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1170 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1171 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1173 (gnus-group-list-groups))))
1175 (defun gnus-topic-select-group (&optional all)
1176 "Select this newsgroup.
1177 No article is selected automatically.
1178 If the group is opened, just switch the summary buffer.
1179 If ALL is non-nil, already read articles become readable.
1180 If ALL is a number, fetch this number of articles.
1182 If performed over a topic line, toggle folding the topic."
1184 (if (gnus-group-topic-p)
1185 (let ((gnus-group-list-mode
1186 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1187 (gnus-topic-fold all)
1188 (gnus-dribble-touch))
1189 (gnus-group-select-group all)))
1191 (defun gnus-mouse-pick-topic (e)
1192 "Select the group or topic under the mouse pointer."
1195 (gnus-topic-read-group nil))
1197 (defun gnus-topic-expire-articles (topic)
1198 "Expire articles in this topic or group."
1199 (interactive (list (gnus-group-topic-name)))
1201 (call-interactively 'gnus-group-expire-articles)
1203 (gnus-message 5 "Expiring groups in %s..." topic)
1204 (let ((gnus-group-marked
1205 (mapcar (lambda (entry) (car (nth 2 entry)))
1206 (gnus-topic-find-groups topic gnus-level-killed t))))
1207 (gnus-group-expire-articles nil))
1208 (gnus-message 5 "Expiring groups in %s...done" topic))))
1210 (defun gnus-topic-catchup-articles (topic)
1211 "Catchup this topic or group.
1212 Also see `gnus-group-catchup'."
1213 (interactive (list (gnus-group-topic-name)))
1215 (call-interactively 'gnus-group-catchup-current)
1218 (mapcar (lambda (entry) (car (nth 2 entry)))
1219 (gnus-topic-find-groups topic gnus-level-killed t)))
1220 (buffer-read-only nil)
1221 (gnus-group-marked groups))
1222 (gnus-group-catchup-current)
1223 (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1225 (defun gnus-topic-read-group (&optional all no-article group)
1226 "Read news in this newsgroup.
1227 If the prefix argument ALL is non-nil, already read articles become
1228 readable. IF ALL is a number, fetch this number of articles. If the
1229 optional argument NO-ARTICLE is non-nil, no article will be
1230 auto-selected upon group entry. If GROUP is non-nil, fetch that
1233 If performed over a topic line, toggle folding the topic."
1235 (if (gnus-group-topic-p)
1236 (let ((gnus-group-list-mode
1237 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1238 (gnus-topic-fold all))
1239 (gnus-group-read-group all no-article group)))
1241 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1242 "Create a new TOPIC under PARENT.
1243 When used interactively, PARENT will be the topic under point."
1246 (read-string "New topic: ")
1247 (gnus-current-topic)))
1248 ;; Check whether this topic already exists.
1249 (when (gnus-topic-find-topology topic)
1250 (error "Topic already exists"))
1252 (setq parent (caar gnus-topic-topology)))
1253 (let ((top (cdr (gnus-topic-find-topology parent)))
1254 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1256 (error "No such parent topic: %s" parent))
1259 (while (and (cdr top)
1260 (not (equal (caaadr top) previous)))
1261 (setq top (cdr top)))
1262 (setcdr top (cons full-topic (cdr top))))
1263 (nconc top (list full-topic)))
1264 (unless (assoc topic gnus-topic-alist)
1265 (push (list topic) gnus-topic-alist)))
1266 (gnus-topic-enter-dribble)
1267 (gnus-group-list-groups)
1268 (gnus-topic-goto-topic topic))
1271 ;; 1. When the marked groups are overlapped with the process
1272 ;; region, the behavior of move or remove is not right.
1273 ;; 2. Can't process on several marked groups with a same name,
1274 ;; because gnus-group-marked only keeps one copy.
1276 (defun gnus-topic-move-group (n topic &optional copyp)
1277 "Move the next N groups to TOPIC.
1278 If COPYP, copy the groups instead."
1280 (list current-prefix-arg
1281 (gnus-completing-read "Move to topic" gnus-topic-alist nil t
1282 'gnus-topic-history)))
1283 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1284 gnus-group-marked t))
1285 (groups (gnus-group-process-prefix n))
1286 (topicl (assoc topic gnus-topic-alist))
1287 (start-topic (gnus-group-topic-name))
1288 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1290 (if (and (not groups) (not copyp) start-topic)
1291 (gnus-topic-move start-topic topic)
1294 (gnus-group-remove-mark g use-marked)
1296 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1298 (setcdr entry (gnus-delete-first g (cdr entry))))
1299 (nconc topicl (list g)))
1301 (gnus-topic-enter-dribble)
1303 (gnus-group-goto-group start-group)
1304 (gnus-topic-goto-topic start-topic))
1305 (gnus-group-list-groups))))
1307 (defun gnus-topic-remove-group (&optional n)
1308 "Remove the current group from the topic."
1310 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1311 gnus-group-marked t))
1312 (groups (gnus-group-process-prefix n)))
1315 (gnus-group-remove-mark group use-marked)
1316 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1317 (buffer-read-only nil))
1318 (when (and topicl group)
1320 (gnus-delete-first group topicl))
1321 (gnus-topic-update-topic)))
1323 (gnus-topic-enter-dribble)
1324 (gnus-group-position-point)))
1326 (defun gnus-topic-copy-group (n topic)
1327 "Copy the current group to a topic."
1329 (list current-prefix-arg
1330 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1331 (gnus-topic-move-group n topic t))
1333 (defun gnus-topic-kill-group (&optional n discard)
1334 "Kill the next N groups."
1336 (if (gnus-group-topic-p)
1337 (let ((topic (gnus-group-topic-name)))
1339 (gnus-topic-find-topology topic)
1340 (assoc topic gnus-topic-alist))
1341 gnus-topic-killed-topics)
1342 (gnus-topic-remove-topic nil t)
1343 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1344 (gnus-topic-enter-dribble))
1345 (gnus-group-kill-group n discard)
1346 (if (not (gnus-group-topic-p))
1347 (gnus-topic-update-topic)
1348 ;; Move up one line so that we update the right topic.
1350 (gnus-topic-update-topic)
1353 (defun gnus-topic-yank-group (&optional arg)
1354 "Yank the last topic."
1356 (if gnus-topic-killed-topics
1358 (or (gnus-group-topic-name)
1359 (gnus-topic-next-topic (gnus-current-topic))))
1360 (data (pop gnus-topic-killed-topics))
1363 (push alist gnus-topic-alist)
1364 (gnus-topic-create-topic
1365 (caar item) (gnus-topic-parent-topic previous) previous
1367 (gnus-topic-enter-dribble)
1368 (gnus-topic-goto-topic (caar item)))
1369 (let* ((prev (gnus-group-group-name))
1370 (gnus-topic-inhibit-change-level t)
1371 (gnus-group-indentation
1373 (* gnus-topic-indent-level
1375 (gnus-topic-goto-topic (gnus-current-topic))
1376 (gnus-group-topic-level))
1380 ;; We first yank the groups the normal way...
1381 (setq yanked (gnus-group-yank-group arg))
1382 ;; Then we enter the yanked groups into the topics they belong
1384 (setq alist (assoc (save-excursion
1386 (gnus-current-topic))
1388 (when (stringp yanked)
1389 (setq yanked (list yanked)))
1391 (nconc alist yanked)
1392 (if (not (cdr alist))
1393 (setcdr alist (nconc yanked (cdr alist)))
1395 (when (equal (cadr alist) prev)
1396 (setcdr alist (nconc yanked (cdr alist)))
1398 (setq alist (cdr alist))))))
1399 (gnus-topic-update-topic)))
1401 (defun gnus-topic-hide-topic (&optional permanent)
1402 "Hide the current topic.
1403 If PERMANENT, make it stay hidden in subsequent sessions as well."
1405 (when (gnus-current-topic)
1406 (gnus-topic-goto-topic (gnus-current-topic))
1410 (gnus-topic-find-topology (gnus-current-topic))))
1412 (gnus-topic-remove-topic nil nil)))
1414 (defun gnus-topic-show-topic (&optional permanent)
1415 "Show the hidden topic.
1416 If PERMANENT, make it stay shown in subsequent sessions as well."
1418 (when (gnus-group-topic-p)
1420 (gnus-topic-remove-topic t nil)
1422 (gnus-topic-find-topology
1423 (completing-read "Show topic: " gnus-topic-alist nil t))))
1424 (setcar (cddr (cadr topic)) nil)
1425 (setcar (cdr (cadr topic)) 'visible)
1426 (gnus-group-list-groups)))))
1428 (defun gnus-topic-mark-topic (topic &optional unmark recursive)
1429 "Mark all groups in the TOPIC with the process mark.
1430 If RECURSIVE is t, mark its subtopics too."
1431 (interactive (list (gnus-group-topic-name)
1433 (and current-prefix-arg t)))
1435 (call-interactively 'gnus-group-mark-group)
1437 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1440 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1441 (gnus-info-group (nth 2 (pop groups)))))))))
1443 (defun gnus-topic-unmark-topic (topic &optional dummy recursive)
1444 "Remove the process mark from all groups in the TOPIC.
1445 If RECURSIVE is t, unmark its subtopics too."
1446 (interactive (list (gnus-group-topic-name)
1448 (and current-prefix-arg t)))
1450 (call-interactively 'gnus-group-unmark-group)
1451 (gnus-topic-mark-topic topic t recursive)))
1453 (defun gnus-topic-get-new-news-this-topic (&optional n)
1454 "Check for new news in the current topic."
1456 (if (not (gnus-group-topic-p))
1457 (gnus-group-get-new-news-this-group n)
1458 (let* ((topic (gnus-group-topic-name))
1459 (data (cadr (gnus-topic-find-topology topic))))
1461 (gnus-topic-mark-topic topic nil (and n t))
1462 (gnus-group-get-new-news-this-group))
1463 (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1465 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1466 "Move all groups that match REGEXP to some topic."
1471 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1472 (read-string (format "Move to %s (regexp): " topic))))))
1473 (gnus-group-mark-regexp regexp)
1474 (gnus-topic-move-group nil topic copyp))
1476 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1477 "Copy all groups that match REGEXP to some topic."
1482 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1483 (read-string (format "Copy to %s (regexp): " topic))))))
1484 (gnus-topic-move-matching regexp topic t))
1486 (defun gnus-topic-delete (topic)
1488 (interactive (list (gnus-group-topic-name)))
1490 (error "No topic to be deleted"))
1491 (let ((entry (assoc topic gnus-topic-alist))
1492 (buffer-read-only nil))
1494 (error "Topic not empty"))
1495 ;; Delete if visible.
1496 (when (gnus-topic-goto-topic topic)
1498 ;; Remove from alist.
1499 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1500 ;; Remove from topology.
1501 (gnus-topic-find-topology topic nil nil 'delete)
1502 (gnus-dribble-touch)))
1504 (defun gnus-topic-rename (old-name new-name)
1507 (let ((topic (gnus-current-topic)))
1509 (read-string (format "Rename %s to: " topic) topic))))
1510 ;; Check whether the new name exists.
1511 (when (gnus-topic-find-topology new-name)
1512 (error "Topic '%s' already exists" new-name))
1513 ;; "nil" is an invalid name, for reasons I'd rather not go
1514 ;; into here. Trust me.
1515 (when (equal new-name "nil")
1516 (error "Invalid name: %s" nil))
1518 (let ((top (gnus-topic-find-topology old-name))
1519 (entry (assoc old-name gnus-topic-alist)))
1521 (setcar (cadr top) new-name))
1523 (setcar entry new-name))
1525 (gnus-dribble-touch)
1526 (gnus-group-list-groups)
1529 (defun gnus-topic-indent (&optional unindent)
1530 "Indent a topic -- make it a sub-topic of the previous topic.
1531 If UNINDENT, remove an indentation."
1534 (gnus-topic-unindent)
1535 (let* ((topic (gnus-current-topic))
1536 (parent (gnus-topic-previous-topic topic))
1537 (buffer-read-only nil))
1539 (error "Nothing to indent %s into" topic))
1541 (gnus-topic-goto-topic topic)
1542 (gnus-topic-kill-group)
1543 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1544 (gnus-topic-create-topic
1545 topic parent nil (cdar (car gnus-topic-killed-topics)))
1546 (pop gnus-topic-killed-topics)
1547 (or (gnus-topic-goto-topic topic)
1548 (gnus-topic-goto-topic parent))))))
1550 (defun gnus-topic-unindent ()
1553 (let* ((topic (gnus-current-topic))
1554 (parent (gnus-topic-parent-topic topic))
1555 (grandparent (gnus-topic-parent-topic parent)))
1557 (error "Nothing to indent %s into" topic))
1559 (gnus-topic-goto-topic topic)
1560 (gnus-topic-kill-group)
1561 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1562 (gnus-topic-create-topic
1563 topic grandparent (gnus-topic-next-topic parent)
1564 (cdar (car gnus-topic-killed-topics)))
1565 (pop gnus-topic-killed-topics)
1566 (gnus-topic-goto-topic topic))))
1568 (defun gnus-topic-list-active (&optional force)
1569 "List all groups that Gnus knows about in a topicsified fashion.
1570 If FORCE, always re-read the active file."
1573 (gnus-get-killed-groups))
1574 (gnus-topic-grok-active force)
1575 (let ((gnus-topic-topology gnus-topic-active-topology)
1576 (gnus-topic-alist gnus-topic-active-alist)
1577 gnus-killed-list gnus-zombie-list)
1578 (gnus-group-list-groups gnus-level-killed nil 1)))
1580 (defun gnus-topic-toggle-display-empty-topics ()
1581 "Show/hide topics that have no unread articles."
1583 (setq gnus-topic-display-empty-topics
1584 (not gnus-topic-display-empty-topics))
1585 (gnus-group-list-groups)
1586 (message "%s empty topics"
1587 (if gnus-topic-display-empty-topics
1588 "Showing" "Hiding")))
1590 ;;; Topic sorting functions
1592 (defun gnus-topic-edit-parameters (group)
1593 "Edit the group parameters of GROUP.
1594 If performed on a topic, edit the topic parameters instead."
1595 (interactive (list (gnus-group-group-name)))
1597 (gnus-group-edit-group-parameters group)
1598 (if (not (gnus-group-topic-p))
1599 (error "Nothing to edit on the current line")
1600 (let ((topic (gnus-group-topic-name)))
1602 (gnus-topic-parameters topic)
1603 (format "Editing the topic parameters for `%s'."
1606 (gnus-topic-set-parameters ,topic form)))))))
1608 (defun gnus-group-sort-topic (func reverse)
1609 "Sort groups in the topics according to FUNC and REVERSE."
1610 (let ((alist gnus-topic-alist))
1612 ;; !!!Sometimes nil elements sneak into the alist,
1613 ;; for some reason or other.
1614 (setcar alist (delq nil (car alist)))
1615 (setcar alist (delete "dummy.group" (car alist)))
1616 (gnus-topic-sort-topic (pop alist) func reverse))))
1618 (defun gnus-topic-sort-topic (topic func reverse)
1619 ;; Each topic only lists the name of the group, while
1620 ;; the sort predicates expect group infos as inputs.
1621 ;; So we first transform the group names into infos,
1622 ;; then sort, and then transform back into group names.
1626 (lambda (info) (gnus-info-group info))
1629 (lambda (group) (gnus-get-info group))
1632 ;; Do the reversal, if necessary.
1634 (setcdr topic (nreverse (cdr topic)))))
1636 (defun gnus-topic-sort-groups (func &optional reverse)
1637 "Sort the current topic according to FUNC.
1638 If REVERSE, reverse the sorting order."
1639 (interactive (list gnus-group-sort-function current-prefix-arg))
1640 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1641 (gnus-topic-sort-topic
1642 topic (gnus-make-sort-function func) reverse)
1643 (gnus-group-list-groups)))
1645 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1646 "Sort the current topic alphabetically by group name.
1647 If REVERSE, sort in reverse order."
1649 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1651 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1652 "Sort the current topic by number of unread articles.
1653 If REVERSE, sort in reverse order."
1655 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1657 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1658 "Sort the current topic by group level.
1659 If REVERSE, sort in reverse order."
1661 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1663 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1664 "Sort the current topic by group score.
1665 If REVERSE, sort in reverse order."
1667 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1669 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1670 "Sort the current topic by group rank.
1671 If REVERSE, sort in reverse order."
1673 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1675 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1676 "Sort the current topic alphabetically by backend name.
1677 If REVERSE, sort in reverse order."
1679 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1681 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1682 "Sort the current topic alphabetically by server name.
1683 If REVERSE, sort in reverse order."
1685 (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1687 (defun gnus-topic-sort-topics-1 (top reverse)
1690 (mapcar (gnus-byte-compile
1692 (gnus-topic-sort-topics-1 top ,reverse)))
1695 (string-lessp (caar t1) (caar t2)))))))
1696 (setcdr top (if reverse (reverse subtop) subtop))))
1699 (defun gnus-topic-sort-topics (&optional topic reverse)
1700 "Sort topics in TOPIC alphabetically by topic name.
1701 If REVERSE, reverse the sorting order."
1703 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1704 (gnus-current-topic))
1705 current-prefix-arg))
1706 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1707 gnus-topic-topology)))
1708 (gnus-topic-sort-topics-1 topic-topology reverse)
1709 (gnus-topic-enter-dribble)
1710 (gnus-group-list-groups)
1711 (gnus-topic-goto-topic topic)))
1713 (defun gnus-topic-move (current to)
1714 "Move the CURRENT topic to TO."
1717 (gnus-group-topic-name)
1718 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1719 (unless (and current to)
1720 (error "Can't find topic"))
1721 (let ((current-top (cdr (gnus-topic-find-topology current)))
1722 (to-top (cdr (gnus-topic-find-topology to))))
1724 (error "Can't find topic `%s'" current))
1726 (error "Can't find topic `%s'" to))
1727 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1728 (error "Can't move `%s' to its sub-level" current))
1729 (gnus-topic-find-topology current nil nil 'delete)
1731 (setq to-top (cdr to-top)))
1732 (setcdr to-top (list current-top))
1733 (gnus-topic-enter-dribble)
1734 (gnus-group-list-groups)
1735 (gnus-topic-goto-topic current)))
1737 (defun gnus-subscribe-topics (newsgroup)
1739 (let (match gnus-group-change-level-function)
1740 (dolist (topic (gnus-topic-list))
1741 (when (and (setq match (cdr (assq 'subscribe
1742 (gnus-topic-parameters topic))))
1743 (string-match match newsgroup))
1744 ;; Just subscribe the group.
1745 (gnus-subscribe-alphabetically newsgroup)
1746 ;; Add the group to the topic.
1747 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1748 ;; if this topic specifies a default level, use it
1749 (let ((subscribe-level (cdr (assq 'subscribe-level
1750 (gnus-topic-parameters topic)))))
1751 (when subscribe-level
1752 (gnus-group-change-level newsgroup subscribe-level
1753 gnus-level-default-subscribed)))
1757 (provide 'gnus-topic)
1759 ;;; gnus-topic.el ends here