1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Ilja Weis <kult@uni-paderborn.de>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 (when (featurep 'xemacs
)
49 (add-hook 'gnus-topic-mode-hook
'gnus-xmas-topic-menu-add
))
51 (defcustom gnus-topic-line-format
"%i[ %(%{%n%}%) -- %A ]%v\n"
52 "Format of topic lines.
53 It works along the same lines as a normal formatting string,
54 with some simple extensions.
56 %i Indentation based on topic level.
58 %v Nothing if the topic is visible, \"...\" otherwise.
59 %g Number of groups in the topic.
60 %a Number of unread articles in the groups in the topic.
61 %A Number of unread articles in the groups in the topic and its subtopics.
63 General format specifiers can also be used.
64 See Info node `(gnus)Formatting Variables'."
65 :link
'(custom-manual "(gnus)Formatting Variables")
69 (defcustom gnus-topic-indent-level
2
70 "*How much each subtopic should be indented."
74 (defcustom gnus-topic-display-empty-topics t
75 "*If non-nil, display the topic lines even of topics that have no unread articles."
79 ;; Internal variables.
81 (defvar gnus-topic-active-topology nil
)
82 (defvar gnus-topic-active-alist nil
)
83 (defvar gnus-topic-unreads nil
)
85 (defvar gnus-topology-checked-p nil
86 "Whether the topology has been checked in this session.")
88 (defvar gnus-topic-killed-topics nil
)
89 (defvar gnus-topic-inhibit-change-level nil
)
91 (defconst gnus-topic-line-format-alist
95 (?g number-of-groups ?d
)
96 (?a
(gnus-topic-articles-in-topic entries
) ?d
)
97 (?A total-number-of-articles ?d
)
100 (defvar gnus-topic-line-format-spec nil
)
102 ;;; Utility functions
104 (defun gnus-group-topic-name ()
105 "The name of the topic on the current line."
106 (let ((topic (get-text-property (point-at-bol) 'gnus-topic
)))
107 (and topic
(symbol-name topic
))))
109 (defun gnus-group-topic-level ()
110 "The level of the topic on the current line."
111 (get-text-property (point-at-bol) 'gnus-topic-level
))
113 (defun gnus-group-topic-unread ()
114 "The number of unread articles in topic on the current line."
115 (get-text-property (point-at-bol) 'gnus-topic-unread
))
117 (defun gnus-topic-unread (topic)
118 "Return the number of unread articles in TOPIC."
119 (or (cdr (assoc topic gnus-topic-unreads
))
122 (defun gnus-group-topic-p ()
123 "Return non-nil if the current line is a topic."
124 (gnus-group-topic-name))
126 (defun gnus-topic-visible-p ()
127 "Return non-nil if the current topic is visible."
128 (get-text-property (point-at-bol) 'gnus-topic-visible
))
130 (defun gnus-topic-articles-in-topic (entries)
134 (when (numberp (setq number
(car (pop entries
))))
135 (incf total number
)))
138 (defun gnus-group-topic (group)
139 "Return the topic GROUP is a member of."
140 (let ((alist gnus-topic-alist
)
143 (when (member group
(cdar alist
))
144 (setq out
(caar alist
)
146 (setq alist
(cdr alist
)))
149 (defun gnus-group-parent-topic (group)
150 "Return the topic GROUP is member of by looking at the group buffer."
152 (set-buffer gnus-group-buffer
)
153 (if (gnus-group-goto-group group
)
155 (gnus-group-topic group
))))
157 (defun gnus-topic-goto-topic (topic)
159 (gnus-goto-char (text-property-any (point-min) (point-max)
160 'gnus-topic
(intern topic
)))))
162 (defun gnus-topic-jump-to-topic (topic)
165 (list (completing-read "Go to topic: "
166 (mapcar 'list
(gnus-topic-list))
168 (let ((buffer-read-only nil
))
169 (dolist (topic (gnus-current-topics topic
))
170 (unless (gnus-topic-goto-topic topic
)
171 (gnus-topic-goto-missing-topic topic
)
172 (gnus-topic-display-missing-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."
199 (get-text-property (point-at-bol) 'gnus-active
))
201 (defun gnus-topic-find-groups (topic &optional level all lowest recursive
)
202 "Return entries for all visible groups in TOPIC.
203 If RECURSIVE is t, return groups in its subtopics too."
204 (let ((groups (cdr (assoc topic gnus-topic-alist
)))
205 info clevel unread group params visible-groups entry active
)
206 (setq lowest
(or lowest
1))
207 (setq level
(or level gnus-level-unsubscribed
))
208 ;; We go through the newsrc to look for matches.
210 (when (setq group
(pop groups
))
211 (setq entry
(gnus-group-entry group
)
213 params
(gnus-info-params info
)
214 active
(gnus-active group
)
215 unread
(or (car entry
)
216 (and (not (equal group
"dummy.group"))
218 (- (1+ (cdr active
)) (car active
))))
219 clevel
(or (gnus-info-level info
)
220 (if (member group gnus-zombie-list
)
221 gnus-level-zombie gnus-level-killed
))))
223 info
; nil means that the group is dead.
225 (>= clevel lowest
) ; Is inside the level we want.
227 (if (or (eq unread t
)
229 gnus-group-list-inactive-groups
231 (and gnus-list-groups-with-ticked-articles
232 (cdr (assq 'tick
(gnus-info-marks info
))))
233 ;; Has right readedness.
234 ;; Check for permanent visibility.
235 (and gnus-permanently-visible-groups
236 (string-match gnus-permanently-visible-groups group
))
237 (memq 'visible params
)
238 (cdr (assq 'visible params
)))
239 ;; Add this group to the list of visible groups.
240 (push (or entry group
) visible-groups
)))
241 (setq visible-groups
(nreverse visible-groups
))
244 (setq recursive
(cdr (gnus-topic-find-topology topic
))))
245 (dolist (topic-topology (cdr recursive
))
247 (nconc visible-groups
248 (gnus-topic-find-groups
249 (caar topic-topology
)
250 level all lowest topic-topology
)))))
253 (defun gnus-topic-goto-previous-topic (n)
254 "Go to the N'th previous topic."
256 (gnus-topic-goto-next-topic (- n
)))
258 (defun gnus-topic-goto-next-topic (n)
259 "Go to the N'th next topic."
261 (let ((backward (< n
0))
263 (topic (gnus-current-topic)))
267 (gnus-topic-previous-topic topic
)
268 (gnus-topic-next-topic topic
))))
269 (gnus-topic-goto-topic topic
)
272 (gnus-message 7 "No more topics"))
275 (defun gnus-topic-previous-topic (topic)
276 "Return the previous topic on the same level as TOPIC."
277 (let ((top (cddr (gnus-topic-find-topology
278 (gnus-topic-parent-topic topic
)))))
279 (unless (equal topic
(caaar top
))
280 (while (and top
(not (equal (caaadr top
) topic
)))
281 (setq top
(cdr top
)))
284 (defun gnus-topic-parent-topic (topic &optional topology
)
285 "Return the parent of TOPIC."
287 (setq topology gnus-topic-topology
))
288 (let ((parent (car (pop topology
)))
291 (not (setq found
(equal (caaar topology
) topic
)))
292 (not (setq result
(gnus-topic-parent-topic
293 topic
(car topology
)))))
294 (setq topology
(cdr topology
)))
295 (or result
(and found parent
))))
297 (defun gnus-topic-next-topic (topic &optional previous
)
298 "Return the next sibling of TOPIC."
299 (let ((parentt (cddr (gnus-topic-find-topology
300 (gnus-topic-parent-topic topic
))))
303 (not (equal (caaar parentt
) topic
)))
304 (setq prev
(caaar parentt
)
305 parentt
(cdr parentt
)))
310 (defun gnus-topic-forward-topic (num)
311 "Go to the next topic on the same level as the current one."
312 (let* ((topic (gnus-current-topic))
313 (way (if (< num
0) 'gnus-topic-previous-topic
314 'gnus-topic-next-topic
))
316 (while (and (not (zerop num
))
317 (setq topic
(funcall way topic
)))
318 (when (gnus-topic-goto-topic topic
)
321 (goto-char (point-max)))
324 (defun gnus-topic-find-topology (topic &optional topology level remove
)
325 "Return the topology of TOPIC."
327 (setq topology gnus-topic-topology
)
331 (if (equal (caar topology
) topic
)
334 (delq topology remove
))
335 (cons level topology
))
336 (setq topology
(cdr topology
))
338 (not (setq result
(gnus-topic-find-topology
339 topic
(car topology
) (1+ level
)
341 (setq topology
(cdr topology
)))
344 (defvar gnus-tmp-topics nil
)
345 (defun gnus-topic-list (&optional topology
)
346 "Return a list of all topics in the topology."
348 (setq topology gnus-topic-topology
349 gnus-tmp-topics nil
))
350 (push (caar topology
) gnus-tmp-topics
)
351 (mapc 'gnus-topic-list
(cdr topology
))
354 ;;; Topic parameter jazz
356 (defun gnus-topic-parameters (topic)
357 "Return the parameters for TOPIC."
358 (let ((top (gnus-topic-find-topology topic
)))
360 (nth 3 (cadr top
)))))
362 (defun gnus-topic-set-parameters (topic parameters
)
363 "Set the topic parameters of TOPIC to PARAMETERS."
364 (let ((top (gnus-topic-find-topology topic
)))
366 (error "No such topic: %s" topic
))
367 ;; We may have to extend if there is no parameters here
369 (unless (nthcdr 2 (cadr top
))
370 (nconc (cadr top
) (list nil
)))
371 (unless (nthcdr 3 (cadr top
))
372 (nconc (cadr top
) (list nil
)))
373 (setcar (nthcdr 3 (cadr top
)) parameters
)
375 (format "(gnus-topic-set-parameters %S '%S)" topic parameters
))))
377 (defun gnus-group-topic-parameters (group)
378 "Compute the group parameters for GROUP in topic mode.
379 Possibly inherit parameters from topics above GROUP."
380 (let ((params-list (copy-sequence (gnus-group-get-parameter group
))))
382 (gnus-topic-hierarchical-parameters
383 ;; First we try to go to the group within the group buffer and find the
384 ;; topic for the group that way. This hopefully copes well with groups
385 ;; that are in more than one topic. Failing that (i.e. when the group
386 ;; isn't visible in the group buffer) we find a topic for the group via
388 (or (and (gnus-group-goto-group group
)
389 (gnus-current-topic))
390 (gnus-group-topic group
))
393 (defun gnus-topic-hierarchical-parameters (topic &optional group-params-list
)
394 "Compute the topic parameters for TOPIC.
395 Possibly inherit parameters from topics above TOPIC.
396 If optional argument GROUP-PARAMS-LIST is non-nil, use it as the basis for
399 ;; We probably have lots of nil elements here, so we remove them.
400 ;; Probably faster than doing this "properly".
401 (delq nil
(cons group-params-list
402 (mapcar 'gnus-topic-parameters
403 (gnus-current-topics topic
)))))
405 ;; Now we have all the parameters, so we go through them
406 ;; and do inheritance in the obvious way.
408 (while (setq params
(pop params-list
))
409 (while (setq param
(pop params
))
411 (setq param
(cons param t
)))
412 (cond ((eq (car param
) 'posting-style
)
413 (let ((param (cdr param
))
415 (while (setq elt
(pop param
))
416 (unless (assoc (car elt
) posting-style
)
417 (push elt posting-style
)))))
419 (unless (assq (car param
) out
)
420 (push param out
))))))
421 (and posting-style
(push (cons 'posting-style posting-style
) out
)))
422 ;; Return the resulting parameter list.
425 ;;; General utility functions
427 (defun gnus-topic-enter-dribble ()
429 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology
)))
431 ;;; Generating group buffers
433 (defun gnus-group-prepare-topics (level &optional predicate lowest
434 regexp list-topic topic-level
)
435 "List all newsgroups with unread articles of level LEVEL or lower.
436 Use the `gnus-group-topics' to sort the groups.
437 If PREDICTE is a function, list groups that the function returns non-nil;
438 if it is t, list groups that have no unread articles.
439 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
440 (set-buffer gnus-group-buffer
)
441 (let ((buffer-read-only nil
)
442 (lowest (or lowest
1))
444 (and gnus-group-listed-groups
445 (copy-sequence gnus-group-listed-groups
))))
447 (gnus-update-format-specifications nil
'topic
)
449 (when (or (not gnus-topic-alist
)
450 (not gnus-topology-checked-p
))
451 (gnus-topic-check-topology))
457 (when (or gnus-group-listed-groups
458 (and (>= level gnus-level-zombie
)
459 (<= lowest gnus-level-zombie
)))
460 (gnus-group-prepare-flat-list-dead
461 (setq gnus-zombie-list
(sort gnus-zombie-list
'string
<))
465 (when (or gnus-group-listed-groups
466 (and (>= level gnus-level-killed
)
467 (<= lowest gnus-level-killed
)))
468 (gnus-group-prepare-flat-list-dead
469 (setq gnus-killed-list
(sort gnus-killed-list
'string
<))
470 gnus-level-killed ?K regexp
)
472 (unless gnus-killed-hashtb
473 (gnus-make-hashtable-from-killed))
474 (gnus-group-prepare-flat-list-dead
475 (gnus-remove-if (lambda (group)
476 (or (gnus-group-entry group
)
477 (gnus-gethash group gnus-killed-hashtb
)))
479 gnus-level-killed ?K regexp
)))
483 (when (or (< lowest gnus-level-zombie
)
484 gnus-group-listed-groups
)
486 (let ((top (gnus-topic-find-topology list-topic
)))
487 (gnus-topic-prepare-topic (cdr top
) (car top
)
488 (or topic-level level
) predicate
490 (gnus-topic-prepare-topic gnus-topic-topology
0
491 (or topic-level level
) predicate
493 (gnus-group-set-mode-line)
494 (setq gnus-group-list-mode
(cons level predicate
))
495 (gnus-run-hooks 'gnus-group-prepare-hook
))))
497 (defun gnus-topic-prepare-topic (topicl level
&optional list-level
500 "Insert TOPIC into the group buffer.
501 If SILENT, don't insert anything. Return the number of unread
502 articles in the topic and its subtopics."
503 (let* ((type (pop topicl
))
504 (entries (gnus-topic-find-groups
506 (if gnus-group-listed-groups
509 (or predicate gnus-group-listed-groups
511 (gnus-topic-hierarchical-parameters
513 (if gnus-group-listed-groups
0 lowest
)))
514 (visiblep (and (eq (nth 1 type
) 'visible
) (not silent
)))
515 (gnus-group-indentation
516 (make-string (* gnus-topic-indent-level level
) ?
))
517 (beg (progn (beginning-of-line) (point)))
518 (topicl (reverse topicl
))
519 (all-entries entries
)
520 (point-max (point-max))
523 info entry end active tick
)
524 ;; Insert any sub-topics.
527 (gnus-topic-prepare-topic
528 (pop topicl
) (1+ level
) list-level predicate
529 (not visiblep
) lowest regexp
)))
532 ;; Insert all the groups that belong in this topic.
533 (while (setq entry
(pop entries
))
534 (when (if (stringp entry
)
535 (gnus-group-prepare-logic
538 (or (not gnus-group-listed-groups
)
539 (if (< list-level gnus-level-zombie
) nil
541 (if (member entry gnus-zombie-list
)
542 gnus-level-zombie gnus-level-killed
)))
543 (and (<= entry-level list-level
)
544 (>= entry-level lowest
)))))
547 (string-match regexp entry
))
549 (funcall regexp entry
))
552 (setq info
(nth 2 entry
))
553 (gnus-group-prepare-logic
554 (gnus-info-group info
)
555 (and (or (not gnus-group-listed-groups
)
556 (let ((entry-level (gnus-info-level info
)))
557 (and (<= entry-level list-level
)
558 (>= entry-level lowest
))))
559 (or (not (functionp predicate
))
560 (funcall predicate info
))
561 (or (not (stringp regexp
))
562 (string-match regexp
(gnus-info-group info
))))))
566 (gnus-group-insert-group-line
567 entry
(if (member entry gnus-zombie-list
)
568 gnus-level-zombie gnus-level-killed
)
569 nil
(- (1+ (cdr (setq active
(gnus-active entry
))))
573 (when (setq info
(nth 2 entry
))
574 (gnus-group-insert-group-line
575 (gnus-info-group info
)
576 (gnus-info-level info
) (gnus-info-marks info
)
577 (car entry
) (gnus-info-method info
)))))
578 (when (and (listp entry
)
579 (numberp (car entry
)))
580 (incf unread
(car entry
)))
584 ;; Insert the topic line.
585 (when (and (not silent
)
586 (or gnus-topic-display-empty-topics
;We want empty topics
587 (not (zerop unread
)) ;Non-empty
588 tick
;Ticked articles
589 (/= point-max
(point-max)))) ;Unactivated groups
590 (gnus-extent-start-open (point))
591 (gnus-topic-insert-topic-line
593 (not (eq (nth 2 type
) 'hidden
))
594 level all-entries unread
))
595 (gnus-topic-update-unreads (car type
) unread
)
596 (when gnus-group-update-tool-bar
597 (gnus-put-text-property beg end
'point-entered
598 'gnus-tool-bar-update
)
599 (gnus-put-text-property beg end
'point-left
600 'gnus-tool-bar-update
))
604 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level
)
605 "Remove the current topic."
606 (let ((topic (gnus-group-topic-name))
607 (level (gnus-group-topic-level))
608 (beg (progn (beginning-of-line) (point)))
611 (while (and (zerop (forward-line 1))
612 (> (or (gnus-group-topic-level) (1+ level
)) level
)))
613 (delete-region beg
(point))
614 ;; Do the change in this rather odd manner because it has been
615 ;; reported that some topics share parts of some lists, for some
616 ;; reason. I have been unable to determine why this is the
617 ;; case, but this hack seems to take care of things.
618 (let ((data (cadr (gnus-topic-find-topology topic
))))
620 (list (if insert
'visible
'invisible
)
624 (setq gnus-topic-alist
625 (delq (assoc topic gnus-topic-alist
) gnus-topic-alist
))
626 (gnus-topic-insert-topic topic in-level
)))))
628 (defun gnus-topic-insert-topic (topic &optional level
)
630 (gnus-group-prepare-topics
631 (car gnus-group-list-mode
) (cdr gnus-group-list-mode
)
632 nil nil topic level
))
634 (defun gnus-topic-fold (&optional insert topic
)
635 "Remove/insert the current topic."
636 (let ((topic (or topic
(gnus-group-topic-name))))
639 (if (not (gnus-group-active-topic-p))
640 (gnus-topic-remove-topic
641 (or insert
(not (gnus-topic-visible-p))))
642 (let ((gnus-topic-topology gnus-topic-active-topology
)
643 (gnus-topic-alist gnus-topic-active-alist
)
644 (gnus-group-list-mode (cons 5 t
)))
645 (gnus-topic-remove-topic
646 (or insert
(not (gnus-topic-visible-p))) nil nil
9)
647 (gnus-topic-enter-dribble)))))))
649 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
651 (let* ((visible (if visiblep
"" "..."))
652 (indentation (make-string (* gnus-topic-indent-level level
) ?
))
653 (total-number-of-articles unread
)
654 (number-of-groups (length entries
))
655 (active-topic (eq gnus-topic-alist gnus-topic-active-alist
))
657 (gnus-topic-update-unreads name unread
)
661 (gnus-add-text-properties
664 (eval gnus-topic-line-format-spec
))
665 (list 'gnus-topic
(intern name
)
666 'gnus-topic-level level
667 'gnus-topic-unread unread
668 'gnus-active active-topic
669 'gnus-topic-visible visiblep
)))))
671 (defun gnus-topic-update-unreads (topic unreads
)
672 (setq gnus-topic-unreads
(delq (assoc topic gnus-topic-unreads
)
674 (push (cons topic unreads
) gnus-topic-unreads
))
676 (defun gnus-topic-update-topics-containing-group (group)
677 "Update all topics that have GROUP as a member."
678 (when (and (eq major-mode
'gnus-group-mode
)
681 (let ((alist gnus-topic-alist
))
682 ;; This is probably not entirely correct. If a topic
683 ;; isn't shown, then it's not updated. But the updating
684 ;; should be performed in any case, since the topic's
685 ;; parent should be updated. Pfft.
687 (when (and (member group
(cdar alist
))
688 (gnus-topic-goto-topic (caar alist
)))
689 (gnus-topic-update-topic-line (caar alist
)))
692 (defun gnus-topic-update-topic ()
693 "Update all parent topics to the current group."
694 (when (and (eq major-mode
'gnus-group-mode
)
696 (let ((group (gnus-group-group-name))
698 (buffer-read-only nil
))
700 (gnus-get-info group
)
701 (gnus-topic-goto-topic (gnus-current-topic)))
702 (gnus-topic-update-topic-line (gnus-group-topic-name))
705 (gnus-group-position-point)))))
707 (defun gnus-topic-goto-missing-group (group)
708 "Place point where GROUP is supposed to be inserted."
709 (let* ((topic (gnus-group-topic group
))
710 (groups (cdr (assoc topic gnus-topic-alist
)))
711 (g (cdr (member group groups
)))
714 ;; Try to jump to a visible group.
716 (not (gnus-group-goto-group (car g
) t
)))
718 ;; It wasn't visible, so we try to see where to insert it.
720 (setq g
(cdr (member group
(reverse groups
))))
721 (while (and g unfound
)
722 (when (gnus-group-goto-group (pop g
) t
)
727 (not (gnus-topic-goto-missing-topic topic
)))
728 (gnus-topic-display-missing-topic topic
)))))
730 (defun gnus-topic-display-missing-topic (topic)
731 "Insert topic lines recursively for missing topics."
732 (let ((parent (gnus-topic-find-topology
733 (gnus-topic-parent-topic topic
))))
735 (not (gnus-topic-goto-missing-topic (caadr parent
))))
736 (gnus-topic-display-missing-topic (caadr parent
))))
737 (gnus-topic-goto-missing-topic topic
)
738 ;; Skip past all groups in the topic we're in.
739 (while (gnus-group-group-name)
741 (let* ((top (gnus-topic-find-topology topic
))
742 (children (cddr top
))
745 (entries (gnus-topic-find-groups
746 (car type
) (car gnus-group-list-mode
)
747 (cdr gnus-group-list-mode
)))
750 (incf unread
(gnus-topic-unread (caar (pop children
)))))
751 (while (setq entry
(pop entries
))
752 (when (numberp (car entry
))
753 (incf unread
(car entry
))))
754 (gnus-topic-insert-topic-line
755 topic t t
(car (gnus-topic-find-topology topic
)) nil unread
)))
757 (defun gnus-topic-goto-missing-topic (topic)
758 (if (gnus-topic-goto-topic topic
)
760 ;; Topic not displayed.
761 (let* ((top (gnus-topic-find-topology
762 (gnus-topic-parent-topic topic
)))
763 (tp (reverse (cddr top
))))
765 (gnus-topic-insert-topic-line
766 topic t t
(car (gnus-topic-find-topology topic
)) nil
0)
767 (while (not (equal (caaar tp
) topic
))
771 (not (gnus-topic-goto-topic (caaar tp
))))
774 (gnus-topic-forward-topic 1)
775 (gnus-topic-goto-missing-topic (caadr top
)))))
778 (defun gnus-topic-update-topic-line (topic-name &optional reads
)
779 (let* ((top (gnus-topic-find-topology topic-name
))
781 (children (cddr top
))
782 (entries (gnus-topic-find-groups
783 (car type
) (car gnus-group-list-mode
)
784 (cdr gnus-group-list-mode
)))
785 (parent (gnus-topic-parent-topic topic-name
))
786 (all-entries entries
)
788 old-unread entry new-unread
)
789 (when (gnus-topic-goto-topic (car type
))
790 ;; Tally all the groups that belong in this topic.
792 (setq unread
(- (gnus-group-topic-unread) reads
))
794 (incf unread
(gnus-topic-unread (caar (pop children
)))))
795 (while (setq entry
(pop entries
))
796 (when (numberp (car entry
))
797 (incf unread
(car entry
)))))
798 (setq old-unread
(gnus-group-topic-unread))
799 ;; Insert the topic line.
800 (gnus-topic-insert-topic-line
801 (car type
) (gnus-topic-visible-p)
802 (not (eq (nth 2 type
) 'hidden
))
803 (gnus-group-topic-level) all-entries unread
)
806 (setq new-unread
(gnus-group-topic-unread)))
809 (gnus-topic-update-topic-line
811 (- (or old-unread
0) (or new-unread
0))))
814 (defun gnus-topic-group-indentation ()
816 (* gnus-topic-indent-level
819 (gnus-topic-goto-topic (gnus-current-topic))
820 (gnus-group-topic-level))
826 (gnus-add-shutdown 'gnus-topic-close
'gnus
)
828 (defun gnus-topic-close ()
829 (setq gnus-topic-active-topology nil
830 gnus-topic-active-alist nil
831 gnus-topic-killed-topics nil
832 gnus-topology-checked-p nil
))
834 (defun gnus-topic-check-topology ()
835 ;; The first time we set the topology to whatever we have
836 ;; gotten here, which can be rather random.
837 (unless gnus-topic-alist
838 (gnus-topic-init-alist))
840 (setq gnus-topology-checked-p t
)
841 ;; Go through the topic alist and make sure that all topics
842 ;; are in the topic topology.
843 (let ((topics (gnus-topic-list))
844 (alist gnus-topic-alist
)
847 (unless (member (caar alist
) topics
)
848 (nconc gnus-topic-topology
849 (list (list (list (caar alist
) 'visible
))))
851 (setq alist
(cdr alist
)))
853 (gnus-topic-enter-dribble))
854 ;; Conversely, go through the topology and make sure that all
855 ;; topologies have alists.
857 (unless (assoc (car topics
) gnus-topic-alist
)
858 (push (list (car topics
)) gnus-topic-alist
))
860 ;; Go through all living groups and make sure that
861 ;; they belong to some topic.
862 (let* ((tgroups (apply 'append
(mapcar 'cdr gnus-topic-alist
)))
863 (entry (last (assoc (caar gnus-topic-topology
) gnus-topic-alist
)))
864 (newsrc (cdr gnus-newsrc-alist
))
867 (unless (member (setq group
(gnus-info-group (pop newsrc
))) tgroups
)
868 (setcdr entry
(list group
))
869 (setq entry
(cdr entry
)))))
870 ;; Go through all topics and make sure they contain only living groups.
871 (let ((alist gnus-topic-alist
)
873 (while (setq topic
(pop alist
))
875 (if (and (cadr topic
)
876 (gnus-group-entry (cadr topic
)))
877 (setq topic
(cdr topic
))
878 (setcdr topic
(cddr topic
)))))))
880 (defun gnus-topic-init-alist ()
881 "Initialize the topic structures."
882 (setq gnus-topic-topology
883 (cons (list "Gnus" 'visible
)
884 (mapcar (lambda (topic)
885 (list (list (car topic
) 'visible
)))
887 (setq gnus-topic-alist
889 (mapcar (lambda (info) (gnus-info-group info
))
890 (cdr gnus-newsrc-alist
)))
892 (gnus-topic-enter-dribble))
896 (defun gnus-topic-clean-alist ()
897 "Remove bogus groups from the topic alist."
898 (let ((topic-alist gnus-topic-alist
)
900 (unless gnus-killed-hashtb
901 (gnus-make-hashtable-from-killed))
902 (while (setq topic
(pop topic-alist
))
903 (let ((topic-name (pop topic
))
904 group filtered-topic
)
905 (while (setq group
(pop topic
))
906 (when (and (or (gnus-active group
)
907 (gnus-info-method (gnus-get-info group
)))
908 (not (gnus-gethash group gnus-killed-hashtb
)))
909 (push group filtered-topic
)))
910 (push (cons topic-name
(nreverse filtered-topic
)) result
)))
911 (setq gnus-topic-alist
(nreverse result
))))
913 (defun gnus-topic-change-level (group level oldlevel
&optional previous
)
914 "Run when changing levels to enter/remove groups from topics."
916 (set-buffer gnus-group-buffer
)
917 (let ((buffer-read-only nil
))
918 (unless gnus-topic-inhibit-change-level
919 (gnus-group-goto-group (or (car (nth 2 previous
)) group
))
920 (when (and gnus-topic-mode
922 (not gnus-topic-inhibit-change-level
))
923 ;; Remove the group from the topics.
924 (if (and (< oldlevel gnus-level-zombie
)
925 (>= level gnus-level-zombie
))
926 (let ((alist gnus-topic-alist
))
927 (while (gnus-group-goto-group group
)
930 (when (member group
(car alist
))
931 (setcdr (car alist
) (delete group
(cdar alist
))))
933 ;; If the group is subscribed we enter it into the topics.
934 (when (and (< level gnus-level-zombie
)
935 (>= oldlevel gnus-level-zombie
))
936 (let* ((prev (gnus-group-group-name))
937 (gnus-topic-inhibit-change-level t
)
938 (gnus-group-indentation
940 (* gnus-topic-indent-level
942 (gnus-topic-goto-topic (gnus-current-topic))
943 (gnus-group-topic-level))
946 (yanked (list group
))
948 ;; Then we enter the yanked groups into the topics
950 (when (setq alist
(assoc (save-excursion
954 (caar gnus-topic-topology
)))
957 (when (stringp yanked
)
958 (setq yanked
(list yanked
)))
961 (if (not (cdr alist
))
962 (setcdr alist
(nconc yanked
(cdr alist
)))
963 (while (and (not end
) (cdr alist
))
964 (when (equal (cadr alist
) prev
)
965 (setcdr alist
(nconc yanked
(cdr alist
)))
967 (setq alist
(cdr alist
)))
969 (nconc talist yanked
))))))
970 (gnus-topic-update-topic))))))))
972 (defun gnus-topic-goto-next-group (group props
)
973 "Go to group or the next group after group."
975 (if (not (memq 'gnus-topic props
))
976 (goto-char (point-max))
977 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props
)))))
978 (if (gnus-group-goto-group group
)
980 ;; The group is no longer visible.
981 (let* ((list (assoc (gnus-group-topic group
) gnus-topic-alist
))
982 (after (cdr (member group
(cdr list
)))))
983 ;; First try to put point on a group after the current one.
985 (not (gnus-group-goto-group (car after
))))
986 (setq after
(cdr after
)))
987 ;; Then try to put point on a group before point.
989 (setq after
(cdr (member group
(reverse (cdr list
)))))
991 (not (gnus-group-goto-group (car after
))))
992 (setq after
(cdr after
))))
993 ;; Finally, just put point on the topic.
995 (goto-char (point-min))
997 (gnus-topic-goto-topic (car list
))
1001 ;;; Topic-active functions
1003 (defun gnus-topic-grok-active (&optional force
)
1004 "Parse all active groups and create topic structures for them."
1005 ;; First we make sure that we have really read the active file.
1007 (not gnus-topic-active-alist
))
1009 ;; Get a list of all groups available.
1010 (mapatoms (lambda (g) (when (symbol-value g
)
1011 (push (symbol-name g
) groups
)))
1013 (setq groups
(sort groups
'string
<))
1014 ;; Init the variables.
1015 (setq gnus-topic-active-topology
(list (list "" 'visible
)))
1016 (setq gnus-topic-active-alist nil
)
1017 ;; Descend the top-level hierarchy.
1018 (gnus-topic-grok-active-1 gnus-topic-active-topology groups
)
1019 ;; Set the top-level topic names to something nice.
1020 (setcar (car gnus-topic-active-topology
) "Gnus active")
1021 (setcar (car gnus-topic-active-alist
) "Gnus active"))))
1023 (defun gnus-topic-grok-active-1 (topology groups
)
1024 (let* ((name (caar topology
))
1025 (prefix (concat "^" (regexp-quote name
)))
1026 tgroups ntopology group
)
1028 (string-match prefix
(setq group
(car groups
))))
1029 (if (not (string-match "\\." group
(match-end 0)))
1030 ;; There are no further hierarchies here, so we just
1031 ;; enter this group into the list belonging to this
1033 (push (pop groups
) tgroups
)
1034 ;; New sub-hierarchy, so we add it to the topology.
1035 (nconc topology
(list (setq ntopology
1036 (list (list (substring
1037 group
0 (match-end 0))
1039 ;; Descend the hierarchy.
1040 (setq groups
(gnus-topic-grok-active-1 ntopology groups
))))
1041 ;; We remove the trailing "." from the topic name.
1043 (if (string-match "\\.$" name
)
1044 (substring name
0 (match-beginning 0))
1046 ;; Add this topic and its groups to the topic alist.
1047 (push (cons name
(nreverse tgroups
)) gnus-topic-active-alist
)
1048 (setcar (car topology
) name
)
1049 ;; We return the rest of the groups that didn't belong
1053 ;;; Topic mode, commands and keymap.
1055 (defvar gnus-topic-mode-map nil
)
1056 (defvar gnus-group-topic-map nil
)
1058 (unless gnus-topic-mode-map
1059 (setq gnus-topic-mode-map
(make-sparse-keymap))
1061 ;; Override certain group mode keys.
1062 (gnus-define-keys gnus-topic-mode-map
1063 "=" gnus-topic-select-group
1064 "\r" gnus-topic-select-group
1065 " " gnus-topic-read-group
1066 "\C-c\C-x" gnus-topic-expire-articles
1067 "c" gnus-topic-catchup-articles
1068 "\C-k" gnus-topic-kill-group
1069 "\C-y" gnus-topic-yank-group
1070 "\M-g" gnus-topic-get-new-news-this-topic
1071 "AT" gnus-topic-list-active
1072 "Gp" gnus-topic-edit-parameters
1073 "#" gnus-topic-mark-topic
1074 "\M-#" gnus-topic-unmark-topic
1075 [tab] gnus-topic-indent
1076 [(meta tab)] gnus-topic-unindent
1077 "\C-i" gnus-topic-indent
1078 "\M-\C-i" gnus-topic-unindent
1079 gnus-mouse-2 gnus-mouse-pick-topic)
1081 ;; Define a new submap.
1082 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
1083 "#" gnus-topic-mark-topic
1084 "\M-#" gnus-topic-unmark-topic
1085 "n" gnus-topic-create-topic
1086 "m" gnus-topic-move-group
1087 "D" gnus-topic-remove-group
1088 "c" gnus-topic-copy-group
1089 "h" gnus-topic-hide-topic
1090 "s" gnus-topic-show-topic
1091 "j" gnus-topic-jump-to-topic
1092 "M" gnus-topic-move-matching
1093 "C" gnus-topic-copy-matching
1094 "\M-p" gnus-topic-goto-previous-topic
1095 "\M-n" gnus-topic-goto-next-topic
1096 "\C-i" gnus-topic-indent
1097 [tab] gnus-topic-indent
1098 "r" gnus-topic-rename
1099 "\177" gnus-topic-delete
1100 [delete] gnus-topic-delete
1101 "H" gnus-topic-toggle-display-empty-topics)
1103 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1104 "s" gnus-topic-sort-groups
1105 "a" gnus-topic-sort-groups-by-alphabet
1106 "u" gnus-topic-sort-groups-by-unread
1107 "l" gnus-topic-sort-groups-by-level
1108 "e" gnus-topic-sort-groups-by-server
1109 "v" gnus-topic-sort-groups-by-score
1110 "r" gnus-topic-sort-groups-by-rank
1111 "m" gnus-topic-sort-groups-by-method))
1113 (defun gnus-topic-make-menu-bar ()
1114 (unless (boundp 'gnus-topic-menu)
1116 gnus-topic-menu gnus-topic-mode-map ""
1118 ["Toggle topics" gnus-topic-mode t]
1120 ["Copy..." gnus-topic-copy-group t]
1121 ["Move..." gnus-topic-move-group t]
1122 ["Remove" gnus-topic-remove-group t]
1123 ["Copy matching..." gnus-topic-copy-matching t]
1124 ["Move matching..." gnus-topic-move-matching t])
1126 ["Goto..." gnus-topic-jump-to-topic t]
1127 ["Show" gnus-topic-show-topic t]
1128 ["Hide" gnus-topic-hide-topic t]
1129 ["Delete" gnus-topic-delete t]
1130 ["Rename..." gnus-topic-rename t]
1131 ["Create..." gnus-topic-create-topic t]
1132 ["Mark" gnus-topic-mark-topic t]
1133 ["Indent" gnus-topic-indent t]
1134 ["Sort" gnus-topic-sort-topics t]
1135 ["Previous topic" gnus-topic-goto-previous-topic t]
1136 ["Next topic" gnus-topic-goto-next-topic t]
1137 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1138 ["Edit parameters" gnus-topic-edit-parameters t])
1139 ["List active" gnus-topic-list-active t]))))
1141 (defun gnus-topic-mode (&optional arg redisplay)
1142 "Minor mode for topicsifying Gnus group buffers."
1143 (interactive (list current-prefix-arg t))
1144 (when (eq major-mode 'gnus-group-mode)
1145 (make-local-variable 'gnus-topic-mode)
1146 (setq gnus-topic-mode
1147 (if (null arg) (not gnus-topic-mode)
1148 (> (prefix-numeric-value arg) 0)))
1149 ;; Infest Gnus with topics.
1150 (if (not gnus-topic-mode)
1151 (setq gnus-goto-missing-group-function nil)
1152 (when (gnus-visual-p 'topic-menu 'menu)
1153 (gnus-topic-make-menu-bar))
1154 (gnus-set-format 'topic t)
1155 (add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
1156 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1157 (set (make-local-variable 'gnus-group-prepare-function)
1158 'gnus-group-prepare-topics)
1159 (set (make-local-variable 'gnus-group-get-parameter-function)
1160 'gnus-group-topic-parameters)
1161 (set (make-local-variable 'gnus-group-goto-next-group-function)
1162 'gnus-topic-goto-next-group)
1163 (set (make-local-variable 'gnus-group-indentation-function)
1164 'gnus-topic-group-indentation)
1165 (set (make-local-variable 'gnus-group-update-group-function)
1166 'gnus-topic-update-topics-containing-group)
1167 (set (make-local-variable 'gnus-group-sort-alist-function)
1168 'gnus-group-sort-topic)
1169 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1170 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1171 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
1172 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1174 (setq gnus-topology-checked-p nil)
1175 ;; We check the topology.
1176 (when gnus-newsrc-alist
1177 (gnus-topic-check-topology))
1178 (gnus-run-hooks 'gnus-topic-mode-hook))
1179 ;; Remove topic infestation.
1180 (unless gnus-topic-mode
1181 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1182 (setq gnus-group-change-level-function nil)
1183 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1184 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1185 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1187 (gnus-group-list-groups))))
1189 (defun gnus-topic-select-group (&optional all)
1190 "Select this newsgroup.
1191 No article is selected automatically.
1192 If the group is opened, just switch the summary buffer.
1193 If ALL is non-nil, already read articles become readable.
1195 If ALL is a positive number, fetch this number of the latest
1196 articles in the group. If ALL is a negative number, fetch this
1197 number of the earliest articles in the group.
1199 If performed over a topic line, toggle folding the topic."
1201 (when (and (eobp) (not (gnus-group-group-name)))
1203 (if (gnus-group-topic-p)
1204 (let ((gnus-group-list-mode
1205 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1206 (gnus-topic-fold all)
1207 (gnus-dribble-touch))
1208 (gnus-group-select-group all)))
1210 (defun gnus-mouse-pick-topic (e)
1211 "Select the group or topic under the mouse pointer."
1214 (gnus-topic-read-group nil))
1216 (defun gnus-topic-expire-articles (topic)
1217 "Expire articles in this topic or group."
1218 (interactive (list (gnus-group-topic-name)))
1220 (call-interactively 'gnus-group-expire-articles)
1222 (gnus-message 5 "Expiring groups in %s..." topic)
1223 (let ((gnus-group-marked
1224 (mapcar (lambda (entry) (car (nth 2 entry)))
1225 (gnus-topic-find-groups topic gnus-level-killed t
1227 (gnus-group-expire-articles nil))
1228 (gnus-message 5 "Expiring groups in %s...done" topic))))
1230 (defun gnus-topic-catchup-articles (topic)
1231 "Catchup this topic or group.
1232 Also see `gnus-group-catchup'."
1233 (interactive (list (gnus-group-topic-name)))
1235 (call-interactively 'gnus-group-catchup-current)
1238 (mapcar (lambda (entry) (car (nth 2 entry)))
1239 (gnus-topic-find-groups topic gnus-level-killed t
1241 (buffer-read-only nil)
1242 (gnus-group-marked groups))
1243 (gnus-group-catchup-current)
1244 (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1246 (defun gnus-topic-read-group (&optional all no-article group)
1247 "Read news in this newsgroup.
1248 If the prefix argument ALL is non-nil, already read articles become
1251 If ALL is a positive number, fetch this number of the latest
1252 articles in the group. If ALL is a negative number, fetch this
1253 number of the earliest articles in the group.
1255 If the optional argument NO-ARTICLE is non-nil, no article will
1256 be auto-selected upon group entry. If GROUP is non-nil, fetch
1259 If performed over a topic line, toggle folding the topic."
1261 (if (gnus-group-topic-p)
1262 (let ((gnus-group-list-mode
1263 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1264 (gnus-topic-fold all))
1265 (gnus-group-read-group all no-article group)))
1267 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1268 "Create a new TOPIC under PARENT.
1269 When used interactively, PARENT will be the topic under point."
1272 (read-string "New topic: ")
1273 (gnus-current-topic)))
1274 ;; Check whether this topic already exists.
1275 (when (gnus-topic-find-topology topic)
1276 (error "Topic already exists"))
1278 (setq parent (caar gnus-topic-topology)))
1279 (let ((top (cdr (gnus-topic-find-topology parent)))
1280 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1282 (error "No such parent topic: %s" parent))
1285 (while (and (cdr top)
1286 (not (equal (caaadr top) previous)))
1287 (setq top (cdr top)))
1288 (setcdr top (cons full-topic (cdr top))))
1289 (nconc top (list full-topic)))
1290 (unless (assoc topic gnus-topic-alist)
1291 (push (list topic) gnus-topic-alist)))
1292 (gnus-topic-enter-dribble)
1293 (gnus-group-list-groups)
1294 (gnus-topic-goto-topic topic))
1297 ;; 1. When the marked groups are overlapped with the process
1298 ;; region, the behavior of move or remove is not right.
1299 ;; 2. Can't process on several marked groups with a same name,
1300 ;; because gnus-group-marked only keeps one copy.
1302 (defun gnus-topic-move-group (n topic &optional copyp)
1303 "Move the next N groups to TOPIC.
1304 If COPYP, copy the groups instead."
1306 (list current-prefix-arg
1307 (gnus-completing-read "Move to topic" gnus-topic-alist nil t
1308 'gnus-topic-history)))
1309 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1310 gnus-group-marked t))
1311 (groups (gnus-group-process-prefix n))
1312 (topicl (assoc topic gnus-topic-alist))
1313 (start-topic (gnus-group-topic-name))
1314 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1316 (if (and (not groups) (not copyp) start-topic)
1317 (gnus-topic-move start-topic topic)
1319 (gnus-group-remove-mark g use-marked)
1321 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1323 (setcdr entry (gnus-delete-first g (cdr entry))))
1324 (nconc topicl (list g)))
1325 (gnus-topic-enter-dribble)
1327 (gnus-group-goto-group start-group)
1328 (gnus-topic-goto-topic start-topic))
1329 (gnus-group-list-groups))))
1331 (defun gnus-topic-remove-group (&optional n)
1332 "Remove the current group from the topic."
1334 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1335 gnus-group-marked t))
1336 (groups (gnus-group-process-prefix n)))
1339 (gnus-group-remove-mark group use-marked)
1340 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1341 (buffer-read-only nil))
1342 (when (and topicl group)
1344 (gnus-delete-first group topicl))
1345 (gnus-topic-update-topic)))
1347 (gnus-topic-enter-dribble)
1348 (gnus-group-position-point)))
1350 (defun gnus-topic-copy-group (n topic)
1351 "Copy the current group to a topic."
1353 (list current-prefix-arg
1354 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1355 (gnus-topic-move-group n topic t))
1357 (defun gnus-topic-kill-group (&optional n discard)
1358 "Kill the next N groups."
1360 (if (gnus-group-topic-p)
1361 (let ((topic (gnus-group-topic-name)))
1363 (gnus-topic-find-topology topic)
1364 (assoc topic gnus-topic-alist))
1365 gnus-topic-killed-topics)
1366 (gnus-topic-remove-topic nil t)
1367 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1368 (gnus-topic-enter-dribble))
1369 (gnus-group-kill-group n discard)
1370 (if (not (gnus-group-topic-p))
1371 (gnus-topic-update-topic)
1372 ;; Move up one line so that we update the right topic.
1374 (gnus-topic-update-topic)
1377 (defun gnus-topic-yank-group (&optional arg)
1378 "Yank the last topic."
1380 (if gnus-topic-killed-topics
1382 (or (gnus-group-topic-name)
1383 (gnus-topic-next-topic (gnus-current-topic))))
1384 (data (pop gnus-topic-killed-topics))
1387 (push alist gnus-topic-alist)
1388 (gnus-topic-create-topic
1389 (caar item) (gnus-topic-parent-topic previous) previous
1391 (gnus-topic-enter-dribble)
1392 (gnus-topic-goto-topic (caar item)))
1393 (let* ((prev (gnus-group-group-name))
1394 (gnus-topic-inhibit-change-level t)
1395 (gnus-group-indentation
1397 (* gnus-topic-indent-level
1399 (gnus-topic-goto-topic (gnus-current-topic))
1400 (gnus-group-topic-level))
1404 ;; We first yank the groups the normal way...
1405 (setq yanked (gnus-group-yank-group arg))
1406 ;; Then we enter the yanked groups into the topics they belong
1408 (setq alist (assoc (save-excursion
1410 (gnus-current-topic))
1412 (when (stringp yanked)
1413 (setq yanked (list yanked)))
1415 (nconc alist yanked)
1416 (if (not (cdr alist))
1417 (setcdr alist (nconc yanked (cdr alist)))
1419 (when (equal (cadr alist) prev)
1420 (setcdr alist (nconc yanked (cdr alist)))
1422 (setq alist (cdr alist))))))
1423 (gnus-topic-update-topic)))
1425 (defun gnus-topic-hide-topic (&optional permanent)
1426 "Hide the current topic.
1427 If PERMANENT, make it stay hidden in subsequent sessions as well."
1429 (when (gnus-current-topic)
1430 (gnus-topic-goto-topic (gnus-current-topic))
1434 (gnus-topic-find-topology (gnus-current-topic))))
1436 (gnus-topic-remove-topic nil nil)))
1438 (defun gnus-topic-show-topic (&optional permanent)
1439 "Show the hidden topic.
1440 If PERMANENT, make it stay shown in subsequent sessions as well."
1442 (when (gnus-group-topic-p)
1444 (gnus-topic-remove-topic t nil)
1446 (gnus-topic-find-topology
1447 (completing-read "Show topic: " gnus-topic-alist nil t))))
1448 (setcar (cddr (cadr topic)) nil)
1449 (setcar (cdr (cadr topic)) 'visible)
1450 (gnus-group-list-groups)))))
1452 (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
1453 "Mark all groups in the TOPIC with the process mark.
1454 If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
1455 (interactive (list (gnus-group-topic-name)
1457 (and current-prefix-arg t)))
1459 (call-interactively 'gnus-group-mark-group)
1461 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1462 (not non-recursive))))
1464 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1465 (gnus-info-group (nth 2 (pop groups)))))))))
1467 (defun gnus-topic-unmark-topic (topic &optional dummy non-recursive)
1468 "Remove the process mark from all groups in the TOPIC.
1469 If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
1470 (interactive (list (gnus-group-topic-name)
1472 (and current-prefix-arg t)))
1474 (call-interactively 'gnus-group-unmark-group)
1475 (gnus-topic-mark-topic topic t non-recursive)))
1477 (defun gnus-topic-get-new-news-this-topic (&optional n)
1478 "Check for new news in the current topic."
1480 (if (not (gnus-group-topic-p))
1481 (gnus-group-get-new-news-this-group n)
1482 (let* ((topic (gnus-group-topic-name))
1483 (data (cadr (gnus-topic-find-topology topic))))
1485 (gnus-topic-mark-topic topic nil (and n t))
1486 (gnus-group-get-new-news-this-group))
1487 (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1489 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1490 "Move all groups that match REGEXP to some topic."
1495 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1496 (read-string (format "Move to %s (regexp): " topic))))))
1497 (gnus-group-mark-regexp regexp)
1498 (gnus-topic-move-group nil topic copyp))
1500 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1501 "Copy all groups that match REGEXP to some topic."
1506 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1507 (read-string (format "Copy to %s (regexp): " topic))))))
1508 (gnus-topic-move-matching regexp topic t))
1510 (defun gnus-topic-delete (topic)
1512 (interactive (list (gnus-group-topic-name)))
1514 (error "No topic to be deleted"))
1515 (let ((entry (assoc topic gnus-topic-alist))
1516 (buffer-read-only nil))
1518 (error "Topic not empty"))
1519 ;; Delete if visible.
1520 (when (gnus-topic-goto-topic topic)
1522 ;; Remove from alist.
1523 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1524 ;; Remove from topology.
1525 (gnus-topic-find-topology topic nil nil 'delete)
1526 (gnus-dribble-touch)))
1528 (defun gnus-topic-rename (old-name new-name)
1531 (let ((topic (gnus-current-topic)))
1533 (read-string (format "Rename %s to: " topic) topic))))
1534 ;; Check whether the new name exists.
1535 (when (gnus-topic-find-topology new-name)
1536 (error "Topic '%s' already exists" new-name))
1537 ;; "nil" is an invalid name, for reasons I'd rather not go
1538 ;; into here. Trust me.
1539 (when (equal new-name "nil")
1540 (error "Invalid name: %s" nil))
1542 (let ((top (gnus-topic-find-topology old-name))
1543 (entry (assoc old-name gnus-topic-alist)))
1545 (setcar (cadr top) new-name))
1547 (setcar entry new-name))
1549 (gnus-dribble-touch)
1550 (gnus-group-list-groups)
1553 (defun gnus-topic-indent (&optional unindent)
1554 "Indent a topic -- make it a sub-topic of the previous topic.
1555 If UNINDENT, remove an indentation."
1558 (gnus-topic-unindent)
1559 (let* ((topic (gnus-current-topic))
1560 (parent (gnus-topic-previous-topic topic))
1561 (buffer-read-only nil))
1563 (error "Nothing to indent %s into" topic))
1565 (gnus-topic-goto-topic topic)
1566 (gnus-topic-kill-group)
1567 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1568 (gnus-topic-create-topic
1569 topic parent nil (cdar (car gnus-topic-killed-topics)))
1570 (pop gnus-topic-killed-topics)
1571 (or (gnus-topic-goto-topic topic)
1572 (gnus-topic-goto-topic parent))))))
1574 (defun gnus-topic-unindent ()
1577 (let* ((topic (gnus-current-topic))
1578 (parent (gnus-topic-parent-topic topic))
1579 (grandparent (gnus-topic-parent-topic parent)))
1581 (error "Nothing to indent %s into" topic))
1583 (gnus-topic-goto-topic topic)
1584 (gnus-topic-kill-group)
1585 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1586 (gnus-topic-create-topic
1587 topic grandparent (gnus-topic-next-topic parent)
1588 (cdar (car gnus-topic-killed-topics)))
1589 (pop gnus-topic-killed-topics)
1590 (gnus-topic-goto-topic topic))))
1592 (defun gnus-topic-list-active (&optional force)
1593 "List all groups that Gnus knows about in a topicsified fashion.
1594 If FORCE, always re-read the active file."
1597 (gnus-get-killed-groups))
1598 (gnus-topic-grok-active force)
1599 (let ((gnus-topic-topology gnus-topic-active-topology)
1600 (gnus-topic-alist gnus-topic-active-alist)
1601 gnus-killed-list gnus-zombie-list)
1602 (gnus-group-list-groups gnus-level-killed nil 1)))
1604 (defun gnus-topic-toggle-display-empty-topics ()
1605 "Show/hide topics that have no unread articles."
1607 (setq gnus-topic-display-empty-topics
1608 (not gnus-topic-display-empty-topics))
1609 (gnus-group-list-groups)
1610 (message "%s empty topics"
1611 (if gnus-topic-display-empty-topics
1612 "Showing" "Hiding")))
1614 ;;; Topic sorting functions
1616 (defun gnus-topic-edit-parameters (group)
1617 "Edit the group parameters of GROUP.
1618 If performed on a topic, edit the topic parameters instead."
1619 (interactive (list (gnus-group-group-name)))
1621 (gnus-group-edit-group-parameters group)
1622 (if (not (gnus-group-topic-p))
1623 (error "Nothing to edit on the current line")
1624 (let ((topic (gnus-group-topic-name)))
1626 (gnus-topic-parameters topic)
1627 (format "Editing the topic parameters for `%s'."
1630 (gnus-topic-set-parameters ,topic form)))))))
1632 (defun gnus-group-sort-topic (func reverse)
1633 "Sort groups in the topics according to FUNC and REVERSE."
1634 (let ((alist gnus-topic-alist))
1636 ;; !!!Sometimes nil elements sneak into the alist,
1637 ;; for some reason or other.
1638 (setcar alist (delq nil (car alist)))
1639 (setcar alist (delete "dummy.group" (car alist)))
1640 (gnus-topic-sort-topic (pop alist) func reverse))))
1642 (defun gnus-topic-sort-topic (topic func reverse)
1643 ;; Each topic only lists the name of the group, while
1644 ;; the sort predicates expect group infos as inputs.
1645 ;; So we first transform the group names into infos,
1646 ;; then sort, and then transform back into group names.
1650 (lambda (info) (gnus-info-group info))
1653 (lambda (group) (gnus-get-info group))
1656 ;; Do the reversal, if necessary.
1658 (setcdr topic (nreverse (cdr topic)))))
1660 (defun gnus-topic-sort-groups (func &optional reverse)
1661 "Sort the current topic according to FUNC.
1662 If REVERSE, reverse the sorting order."
1663 (interactive (list gnus-group-sort-function current-prefix-arg))
1664 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1665 (gnus-topic-sort-topic
1666 topic (gnus-make-sort-function func) reverse)
1667 (gnus-group-list-groups)))
1669 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1670 "Sort the current topic alphabetically by group name.
1671 If REVERSE, sort in reverse order."
1673 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1675 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1676 "Sort the current topic by number of unread articles.
1677 If REVERSE, sort in reverse order."
1679 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1681 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1682 "Sort the current topic by group level.
1683 If REVERSE, sort in reverse order."
1685 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1687 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1688 "Sort the current topic by group score.
1689 If REVERSE, sort in reverse order."
1691 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1693 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1694 "Sort the current topic by group rank.
1695 If REVERSE, sort in reverse order."
1697 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1699 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1700 "Sort the current topic alphabetically by backend name.
1701 If REVERSE, sort in reverse order."
1703 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1705 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1706 "Sort the current topic alphabetically by server name.
1707 If REVERSE, sort in reverse order."
1709 (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1711 (defun gnus-topic-sort-topics-1 (top reverse)
1714 (mapcar (gnus-byte-compile
1716 (gnus-topic-sort-topics-1 top ,reverse)))
1719 (string-lessp (caar t1) (caar t2)))))))
1720 (setcdr top (if reverse (reverse subtop) subtop))))
1723 (defun gnus-topic-sort-topics (&optional topic reverse)
1724 "Sort topics in TOPIC alphabetically by topic name.
1725 If REVERSE, reverse the sorting order."
1727 (list (completing-read "Sort topics in : " gnus-topic-alist nil t
1728 (gnus-current-topic))
1729 current-prefix-arg))
1730 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1731 gnus-topic-topology)))
1732 (gnus-topic-sort-topics-1 topic-topology reverse)
1733 (gnus-topic-enter-dribble)
1734 (gnus-group-list-groups)
1735 (gnus-topic-goto-topic topic)))
1737 (defun gnus-topic-move (current to)
1738 "Move the CURRENT topic to TO."
1741 (gnus-group-topic-name)
1742 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1743 (unless (and current to)
1744 (error "Can't find topic"))
1745 (let ((current-top (cdr (gnus-topic-find-topology current)))
1746 (to-top (cdr (gnus-topic-find-topology to))))
1748 (error "Can't find topic `%s'" current))
1750 (error "Can't find topic `%s'" to))
1751 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1752 (error "Can't move `%s' to its sub-level" current))
1753 (gnus-topic-find-topology current nil nil 'delete)
1754 (setcdr (last to-top) (list current-top))
1755 (gnus-topic-enter-dribble)
1756 (gnus-group-list-groups)
1757 (gnus-topic-goto-topic current)))
1759 (defun gnus-subscribe-topics (newsgroup)
1761 (let (match gnus-group-change-level-function)
1762 (dolist (topic (gnus-topic-list))
1763 (when (and (setq match (cdr (assq 'subscribe
1764 (gnus-topic-parameters topic))))
1765 (string-match match newsgroup))
1766 ;; Just subscribe the group.
1767 (gnus-subscribe-alphabetically newsgroup)
1768 ;; Add the group to the topic.
1769 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1770 ;; if this topic specifies a default level, use it
1771 (let ((subscribe-level (cdr (assq 'subscribe-level
1772 (gnus-topic-parameters topic)))))
1773 (when subscribe-level
1774 (gnus-group-change-level newsgroup subscribe-level
1775 gnus-level-default-subscribed)))
1779 (provide 'gnus-topic)
1781 ;; arch-tag: bf176856-f30c-40f0-ae77-e41529a1134c
1782 ;;; gnus-topic.el ends here