1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
3 ;; Copyright (C) 1995-2015 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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'cl
))
35 (defgroup gnus-topic nil
39 (defvar gnus-topic-mode nil
40 "Minor mode for Gnus group buffers.")
42 (defcustom gnus-topic-mode-hook nil
43 "Hook run in topic mode buffers."
47 (when (featurep 'xemacs
)
48 (add-hook 'gnus-topic-mode-hook
'gnus-xmas-topic-menu-add
))
50 (defcustom gnus-topic-line-format
"%i[ %(%{%n%}%) -- %A ]%v\n"
51 "Format of topic lines.
52 It works along the same lines as a normal formatting string,
53 with some simple extensions.
55 %i Indentation based on topic level.
57 %v Nothing if the topic is visible, \"...\" otherwise.
58 %g Number of groups in the topic.
59 %a Number of unread articles in the groups in the topic.
60 %A Number of unread articles in the groups in the topic and its subtopics.
62 General format specifiers can also be used.
63 See Info node `(gnus)Formatting Variables'."
64 :link
'(custom-manual "(gnus)Formatting Variables")
68 (defcustom gnus-topic-indent-level
2
69 "*How much each subtopic should be indented."
73 (defcustom gnus-topic-display-empty-topics t
74 "*If non-nil, display the topic lines even of topics that have no unread articles."
78 ;; Internal variables.
80 (defvar gnus-topic-active-topology nil
)
81 (defvar gnus-topic-active-alist nil
)
82 (defvar gnus-topic-unreads nil
)
84 (defvar gnus-topology-checked-p nil
85 "Whether the topology has been checked in this session.")
87 (defvar gnus-topic-killed-topics nil
)
88 (defvar gnus-topic-inhibit-change-level nil
)
90 (defconst gnus-topic-line-format-alist
94 (?g number-of-groups ?d
)
95 (?a
(gnus-topic-articles-in-topic entries
) ?d
)
96 (?A total-number-of-articles ?d
)
99 (defvar gnus-topic-line-format-spec nil
)
101 ;;; Utility functions
103 (defun gnus-group-topic-name ()
104 "The name of the topic on the current line."
105 (let ((topic (get-text-property (point-at-bol) 'gnus-topic
)))
106 (and topic
(symbol-name topic
))))
108 (defun gnus-group-topic-level ()
109 "The level of the topic on the current line."
110 (get-text-property (point-at-bol) 'gnus-topic-level
))
112 (defun gnus-group-topic-unread ()
113 "The number of unread articles in topic on the current line."
114 (get-text-property (point-at-bol) 'gnus-topic-unread
))
116 (defun gnus-topic-unread (topic)
117 "Return the number of unread articles in TOPIC."
118 (or (cdr (assoc topic gnus-topic-unreads
))
121 (defun gnus-group-topic-p ()
122 "Return non-nil if the current line is a topic."
123 (gnus-group-topic-name))
125 (defun gnus-topic-visible-p ()
126 "Return non-nil if the current topic is visible."
127 (get-text-property (point-at-bol) 'gnus-topic-visible
))
129 (defun gnus-topic-articles-in-topic (entries)
133 (when (numberp (setq number
(car (pop entries
))))
134 (incf total number
)))
137 (defun gnus-group-topic (group)
138 "Return the topic GROUP is a member of."
139 (let ((alist gnus-topic-alist
)
142 (when (member group
(cdar alist
))
143 (setq out
(caar alist
)
145 (setq alist
(cdr alist
)))
148 (defun gnus-topic-goto-topic (topic)
150 (gnus-goto-char (text-property-any (point-min) (point-max)
151 'gnus-topic
(intern topic
)))))
153 (defun gnus-topic-jump-to-topic (topic)
156 (list (gnus-completing-read "Go to topic" (gnus-topic-list) t
)))
157 (let ((inhibit-read-only t
))
158 (dolist (topic (gnus-current-topics topic
))
159 (unless (gnus-topic-goto-topic topic
)
160 (gnus-topic-goto-missing-topic topic
)
161 (gnus-topic-display-missing-topic topic
))))
162 (gnus-topic-goto-topic topic
))
164 (defun gnus-current-topic ()
165 "Return the name of the current topic."
167 (or (get-text-property (point) 'gnus-topic
)
169 (and (gnus-goto-char (previous-single-property-change
170 (point) 'gnus-topic
))
171 (get-text-property (max (1- (point)) (point-min))
174 (symbol-name result
))))
176 (defun gnus-current-topics (&optional topic
)
177 "Return a list of all current topics, lowest in hierarchy first.
178 If TOPIC, start with that topic."
179 (let ((topic (or topic
(gnus-current-topic)))
183 (setq topic
(gnus-topic-parent-topic topic
)))
186 (defun gnus-group-active-topic-p ()
187 "Say whether the current topic comes from the active topics."
188 (get-text-property (point-at-bol) 'gnus-active
))
190 (defun gnus-topic-find-groups (topic &optional level all lowest recursive
)
191 "Return entries for all visible groups in TOPIC.
192 If RECURSIVE is t, return groups in its subtopics too."
193 (let ((groups (cdr (assoc topic gnus-topic-alist
)))
194 info clevel unread group params visible-groups entry active
)
195 (setq lowest
(or lowest
1))
196 (setq level
(or level gnus-level-unsubscribed
))
197 ;; We go through the newsrc to look for matches.
199 (when (setq group
(pop groups
))
200 (setq entry
(gnus-group-entry group
)
202 params
(gnus-info-params info
)
203 active
(gnus-active group
)
204 unread
(or (car entry
)
205 (and (not (equal group
"dummy.group"))
207 (- (1+ (cdr active
)) (car active
))))
208 clevel
(or (gnus-info-level info
)
209 (if (member group gnus-zombie-list
)
210 gnus-level-zombie gnus-level-killed
))))
212 info
; nil means that the group is dead.
214 (>= clevel lowest
) ; Is inside the level we want.
216 (if (or (eq unread t
)
218 gnus-group-list-inactive-groups
220 (and gnus-list-groups-with-ticked-articles
221 (cdr (assq 'tick
(gnus-info-marks info
))))
222 ;; Has right readedness.
223 ;; Check for permanent visibility.
224 (and gnus-permanently-visible-groups
225 (string-match gnus-permanently-visible-groups group
))
226 (memq 'visible params
)
227 (cdr (assq 'visible params
)))
228 ;; Add this group to the list of visible groups.
229 (push (or entry group
) visible-groups
)))
230 (setq visible-groups
(nreverse visible-groups
))
233 (setq recursive
(cdr (gnus-topic-find-topology topic
))))
234 (dolist (topic-topology (cdr recursive
))
236 (nconc visible-groups
237 (gnus-topic-find-groups
238 (caar topic-topology
)
239 level all lowest topic-topology
)))))
242 (defun gnus-topic-goto-previous-topic (n)
243 "Go to the N'th previous topic."
245 (gnus-topic-goto-next-topic (- n
)))
247 (defun gnus-topic-goto-next-topic (n)
248 "Go to the N'th next topic."
250 (let ((backward (< n
0))
252 (topic (gnus-current-topic)))
256 (gnus-topic-previous-topic topic
)
257 (gnus-topic-next-topic topic
))))
258 (gnus-topic-goto-topic topic
)
261 (gnus-message 7 "No more topics"))
264 (defun gnus-topic-previous-topic (topic)
265 "Return the previous topic on the same level as TOPIC."
266 (let ((top (cddr (gnus-topic-find-topology
267 (gnus-topic-parent-topic topic
)))))
268 (unless (equal topic
(caaar top
))
269 (while (and top
(not (equal (caaadr top
) topic
)))
270 (setq top
(cdr top
)))
273 (defun gnus-topic-parent-topic (topic &optional topology
)
274 "Return the parent of TOPIC."
276 (setq topology gnus-topic-topology
))
277 (let ((parent (car (pop topology
)))
280 (not (setq found
(equal (caaar topology
) topic
)))
281 (not (setq result
(gnus-topic-parent-topic
282 topic
(car topology
)))))
283 (setq topology
(cdr topology
)))
284 (or result
(and found parent
))))
286 (defun gnus-topic-next-topic (topic &optional previous
)
287 "Return the next sibling of TOPIC."
288 (let ((parentt (cddr (gnus-topic-find-topology
289 (gnus-topic-parent-topic topic
))))
292 (not (equal (caaar parentt
) topic
)))
293 (setq prev
(caaar parentt
)
294 parentt
(cdr parentt
)))
299 (defun gnus-topic-forward-topic (num)
300 "Go to the next topic on the same level as the current one."
301 (let* ((topic (gnus-current-topic))
302 (way (if (< num
0) 'gnus-topic-previous-topic
303 'gnus-topic-next-topic
))
305 (while (and (not (zerop num
))
306 (setq topic
(funcall way topic
)))
307 (when (gnus-topic-goto-topic topic
)
310 (goto-char (point-max)))
313 (defun gnus-topic-find-topology (topic &optional topology level remove
)
314 "Return the topology of TOPIC."
316 (setq topology gnus-topic-topology
)
320 (if (equal (caar topology
) topic
)
323 (delq topology remove
))
324 (cons level topology
))
325 (setq topology
(cdr topology
))
327 (not (setq result
(gnus-topic-find-topology
328 topic
(car topology
) (1+ level
)
330 (setq topology
(cdr topology
)))
333 (defvar gnus-tmp-topics nil
)
334 (defun gnus-topic-list (&optional topology
)
335 "Return a list of all topics in the topology."
337 (setq topology gnus-topic-topology
338 gnus-tmp-topics nil
))
339 (push (caar topology
) gnus-tmp-topics
)
340 (mapc 'gnus-topic-list
(cdr topology
))
343 ;;; Topic parameter jazz
345 (defun gnus-topic-parameters (topic)
346 "Return the parameters for TOPIC."
347 (let ((top (gnus-topic-find-topology topic
)))
349 (nth 3 (cadr top
)))))
351 (defun gnus-topic-set-parameters (topic parameters
)
352 "Set the topic parameters of TOPIC to PARAMETERS."
353 (let ((top (gnus-topic-find-topology topic
)))
355 (error "No such topic: %s" topic
))
356 ;; We may have to extend if there is no parameters here
358 (unless (nthcdr 2 (cadr top
))
359 (nconc (cadr top
) (list nil
)))
360 (unless (nthcdr 3 (cadr top
))
361 (nconc (cadr top
) (list nil
)))
362 (setcar (nthcdr 3 (cadr top
)) parameters
)
364 (format "(gnus-topic-set-parameters %S '%S)" topic parameters
))))
366 (defun gnus-group-topic-parameters (group)
367 "Compute the group parameters for GROUP in topic mode.
368 Possibly inherit parameters from topics above GROUP."
369 (let ((params-list (copy-sequence (gnus-group-get-parameter group
))))
371 (gnus-topic-hierarchical-parameters
372 ;; First we try to go to the group within the group buffer and find the
373 ;; topic for the group that way. This hopefully copes well with groups
374 ;; that are in more than one topic. Failing that (i.e. when the group
375 ;; isn't visible in the group buffer) we find a topic for the group via
377 (or (and (gnus-group-goto-group group
)
378 (gnus-current-topic))
379 (gnus-group-topic group
))
382 (defun gnus-topic-hierarchical-parameters (topic &optional group-params-list
)
383 "Compute the topic parameters for TOPIC.
384 Possibly inherit parameters from topics above TOPIC.
385 If optional argument GROUP-PARAMS-LIST is non-nil, use it as the basis for
388 ;; We probably have lots of nil elements here, so we remove them.
389 ;; Probably faster than doing this "properly".
390 (delq nil
(cons group-params-list
391 (mapcar 'gnus-topic-parameters
392 (gnus-current-topics topic
)))))
394 ;; Now we have all the parameters, so we go through them
395 ;; and do inheritance in the obvious way.
397 (while (setq params
(pop params-list
))
398 (while (setq param
(pop params
))
400 (setq param
(cons param t
)))
401 (cond ((eq (car param
) 'posting-style
)
402 (let ((param (cdr param
))
404 (while (setq elt
(pop param
))
405 (unless (assoc (car elt
) posting-style
)
406 (push elt posting-style
)))))
408 (unless (assq (car param
) out
)
409 (push param out
))))))
410 (and posting-style
(push (cons 'posting-style posting-style
) out
)))
411 ;; Return the resulting parameter list.
414 ;;; General utility functions
416 (defun gnus-topic-enter-dribble ()
418 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology
)))
420 ;;; Generating group buffers
422 (defun gnus-group-prepare-topics (level &optional predicate lowest
423 regexp list-topic topic-level
)
424 "List all newsgroups with unread articles of level LEVEL or lower.
425 Use the `gnus-group-topics' to sort the groups.
426 If PREDICATE is a function, list groups that the function returns non-nil;
427 if it is t, list groups that have no unread articles.
428 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
429 (set-buffer gnus-group-buffer
)
430 (let ((inhibit-read-only t
)
431 (lowest (or lowest
1))
433 (and gnus-group-listed-groups
434 (copy-sequence gnus-group-listed-groups
))))
436 (gnus-update-format-specifications nil
'topic
)
438 (when (or (not gnus-topic-alist
)
439 (not gnus-topology-checked-p
))
440 (gnus-topic-check-topology))
446 (when (or gnus-group-listed-groups
447 (and (>= level gnus-level-zombie
)
448 (<= lowest gnus-level-zombie
)))
449 (gnus-group-prepare-flat-list-dead
450 (setq gnus-zombie-list
(sort gnus-zombie-list
'string
<))
454 (when (or gnus-group-listed-groups
455 (and (>= level gnus-level-killed
)
456 (<= lowest gnus-level-killed
)))
457 (gnus-group-prepare-flat-list-dead
458 (setq gnus-killed-list
(sort gnus-killed-list
'string
<))
459 gnus-level-killed ?K regexp
)
461 (unless gnus-killed-hashtb
462 (gnus-make-hashtable-from-killed))
463 (gnus-group-prepare-flat-list-dead
464 (gnus-remove-if (lambda (group)
465 (or (gnus-group-entry group
)
466 (gnus-gethash group gnus-killed-hashtb
)))
468 gnus-level-killed ?K regexp
)))
472 (when (or (< lowest gnus-level-zombie
)
473 gnus-group-listed-groups
)
475 (let ((top (gnus-topic-find-topology list-topic
)))
476 (gnus-topic-prepare-topic (cdr top
) (car top
)
477 (or topic-level level
) predicate
479 (gnus-topic-prepare-topic gnus-topic-topology
0
480 (or topic-level level
) predicate
482 (gnus-group-set-mode-line)
483 (setq gnus-group-list-mode
(cons level predicate
))
484 (gnus-run-hooks 'gnus-group-prepare-hook
))))
486 (defun gnus-topic-prepare-topic (topicl level
&optional list-level
489 "Insert TOPIC into the group buffer.
490 If SILENT, don't insert anything. Return the number of unread
491 articles in the topic and its subtopics."
492 (let* ((type (pop topicl
))
493 (entries (gnus-topic-find-groups
495 (if gnus-group-listed-groups
498 (or predicate gnus-group-listed-groups
500 (gnus-topic-hierarchical-parameters
502 (if gnus-group-listed-groups
0 lowest
)))
503 (visiblep (and (eq (nth 1 type
) 'visible
) (not silent
)))
504 (gnus-group-indentation
505 (make-string (* gnus-topic-indent-level level
) ?
))
506 (beg (progn (beginning-of-line) (point)))
507 (topicl (reverse topicl
))
508 (all-entries entries
)
509 (point-max (point-max))
512 info entry end active tick
)
513 ;; Insert any sub-topics.
516 (gnus-topic-prepare-topic
517 (pop topicl
) (1+ level
) list-level predicate
518 (not visiblep
) lowest regexp
)))
521 ;; Insert all the groups that belong in this topic.
522 (while (setq entry
(pop entries
))
523 (when (if (stringp entry
)
524 (gnus-group-prepare-logic
527 (or (not gnus-group-listed-groups
)
528 (if (< list-level gnus-level-zombie
) nil
530 (if (member entry gnus-zombie-list
)
531 gnus-level-zombie gnus-level-killed
)))
532 (and (<= entry-level list-level
)
533 (>= entry-level lowest
)))))
536 (string-match regexp entry
))
538 (funcall regexp entry
))
541 (setq info
(nth 2 entry
))
542 (gnus-group-prepare-logic
543 (gnus-info-group info
)
544 (and (or (not gnus-group-listed-groups
)
545 (let ((entry-level (gnus-info-level info
)))
546 (and (<= entry-level list-level
)
547 (>= entry-level lowest
))))
548 (or (not (functionp predicate
))
549 (funcall predicate info
))
550 (or (not (stringp regexp
))
551 (string-match regexp
(gnus-info-group info
))))))
555 (gnus-group-insert-group-line
556 entry
(if (member entry gnus-zombie-list
)
557 gnus-level-zombie gnus-level-killed
)
558 nil
(- (1+ (cdr (setq active
(gnus-active entry
))))
562 (when (setq info
(nth 2 entry
))
563 (gnus-group-insert-group-line
564 (gnus-info-group info
)
565 (gnus-info-level info
) (gnus-info-marks info
)
566 (car entry
) (gnus-info-method info
)))))
567 (when (and (listp entry
)
568 (numberp (car entry
)))
569 (incf unread
(car entry
)))
573 ;; Insert the topic line.
574 (when (and (not silent
)
575 (or gnus-topic-display-empty-topics
;We want empty topics
576 (not (zerop unread
)) ;Non-empty
577 tick
;Ticked articles
578 (/= point-max
(point-max)))) ;Inactive groups
579 (gnus-extent-start-open (point))
580 (gnus-topic-insert-topic-line
582 (not (eq (nth 2 type
) 'hidden
))
583 level all-entries unread
))
584 (gnus-topic-update-unreads (car type
) unread
)
585 (gnus-group--setup-tool-bar-update beg end
)
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 (inhibit-read-only t
))
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 ;; Skip past all groups in the topic we're in.
724 (while (gnus-group-group-name)
726 (let* ((top (gnus-topic-find-topology topic
))
727 (children (cddr top
))
730 (entries (gnus-topic-find-groups
731 (car type
) (car gnus-group-list-mode
)
732 (cdr gnus-group-list-mode
)))
735 (incf unread
(gnus-topic-unread (caar (pop children
)))))
736 (while (setq entry
(pop entries
))
737 (when (numberp (car entry
))
738 (incf unread
(car entry
))))
739 (gnus-topic-insert-topic-line
740 topic t t
(car (gnus-topic-find-topology topic
)) nil unread
)))
742 (defun gnus-topic-goto-missing-topic (topic)
743 (if (gnus-topic-goto-topic topic
)
745 ;; Topic not displayed.
746 (let* ((top (gnus-topic-find-topology
747 (gnus-topic-parent-topic topic
)))
748 (tp (reverse (cddr top
))))
750 (gnus-topic-insert-topic-line
751 topic t t
(car (gnus-topic-find-topology topic
)) nil
0)
752 (while (not (equal (caaar tp
) topic
))
756 (not (gnus-topic-goto-topic (caaar tp
))))
759 (gnus-topic-forward-topic 1)
760 (gnus-topic-goto-missing-topic (caadr top
)))))
763 (defun gnus-topic-update-topic-line (topic-name &optional reads
)
764 (let* ((top (gnus-topic-find-topology topic-name
))
766 (children (cddr top
))
767 (entries (gnus-topic-find-groups
768 (car type
) (car gnus-group-list-mode
)
769 (cdr gnus-group-list-mode
)))
770 (parent (gnus-topic-parent-topic topic-name
))
771 (all-entries entries
)
773 old-unread entry new-unread
)
774 (when (gnus-topic-goto-topic (car type
))
775 ;; Tally all the groups that belong in this topic.
777 (setq unread
(- (gnus-group-topic-unread) reads
))
779 (incf unread
(gnus-topic-unread (caar (pop children
)))))
780 (while (setq entry
(pop entries
))
781 (when (numberp (car entry
))
782 (incf unread
(car entry
)))))
783 (setq old-unread
(gnus-group-topic-unread))
784 ;; Insert the topic line.
785 (gnus-topic-insert-topic-line
786 (car type
) (gnus-topic-visible-p)
787 (not (eq (nth 2 type
) 'hidden
))
788 (gnus-group-topic-level) all-entries unread
)
791 (setq new-unread
(gnus-group-topic-unread)))
794 (gnus-topic-update-topic-line
796 (- (or old-unread
0) (or new-unread
0))))
799 (defun gnus-topic-group-indentation ()
801 (* gnus-topic-indent-level
804 (gnus-topic-goto-topic (gnus-current-topic))
805 (gnus-group-topic-level))
811 (gnus-add-shutdown 'gnus-topic-close
'gnus
)
813 (defun gnus-topic-close ()
814 (setq gnus-topic-active-topology nil
815 gnus-topic-active-alist nil
816 gnus-topic-killed-topics nil
817 gnus-topology-checked-p nil
))
819 (defun gnus-topic-check-topology ()
820 ;; The first time we set the topology to whatever we have
821 ;; gotten here, which can be rather random.
822 (unless gnus-topic-alist
823 (gnus-topic-init-alist))
825 (setq gnus-topology-checked-p t
)
826 ;; Go through the topic alist and make sure that all topics
827 ;; are in the topic topology.
828 (let ((topics (gnus-topic-list))
829 (alist gnus-topic-alist
)
832 (unless (member (caar alist
) topics
)
833 (nconc gnus-topic-topology
834 (list (list (list (caar alist
) 'visible
))))
836 (setq alist
(cdr alist
)))
838 (gnus-topic-enter-dribble))
839 ;; Conversely, go through the topology and make sure that all
840 ;; topologies have alists.
842 (unless (assoc (car topics
) gnus-topic-alist
)
843 (push (list (car topics
)) gnus-topic-alist
))
845 ;; Go through all living groups and make sure that
846 ;; they belong to some topic.
847 (let* ((tgroups (apply 'append
(mapcar 'cdr gnus-topic-alist
)))
848 (entry (last (assoc (caar gnus-topic-topology
) gnus-topic-alist
)))
849 (newsrc (cdr gnus-newsrc-alist
))
852 (unless (member (setq group
(gnus-info-group (pop newsrc
))) tgroups
)
853 (setcdr entry
(list group
))
854 (setq entry
(cdr entry
)))))
855 ;; Go through all topics and make sure they contain only living groups.
856 (let ((alist gnus-topic-alist
)
858 (while (setq topic
(pop alist
))
860 (if (and (cadr topic
)
861 (gnus-group-entry (cadr topic
)))
862 (setq topic
(cdr topic
))
863 (setcdr topic
(cddr topic
)))))))
865 (defun gnus-topic-init-alist ()
866 "Initialize the topic structures."
867 (setq gnus-topic-topology
868 (cons (list "Gnus" 'visible
)
869 (mapcar (lambda (topic)
870 (list (list (car topic
) 'visible
)))
872 (setq gnus-topic-alist
874 (mapcar (lambda (info) (gnus-info-group info
))
875 (cdr gnus-newsrc-alist
)))
877 (gnus-topic-enter-dribble))
881 (defun gnus-topic-clean-alist ()
882 "Remove bogus groups from the topic alist."
883 (let ((topic-alist gnus-topic-alist
)
885 (unless gnus-killed-hashtb
886 (gnus-make-hashtable-from-killed))
887 (while (setq topic
(pop topic-alist
))
888 (let ((topic-name (pop topic
))
889 group filtered-topic
)
890 (while (setq group
(pop topic
))
891 (when (and (or (gnus-active group
)
892 (gnus-info-method (gnus-get-info group
)))
893 (not (gnus-gethash group gnus-killed-hashtb
)))
894 (push group filtered-topic
)))
895 (push (cons topic-name
(nreverse filtered-topic
)) result
)))
896 (setq gnus-topic-alist
(nreverse result
))))
898 (defun gnus-topic-change-level (group level oldlevel
&optional previous
)
899 "Run when changing levels to enter/remove groups from topics."
900 (with-current-buffer gnus-group-buffer
901 (let ((inhibit-read-only t
))
902 (unless gnus-topic-inhibit-change-level
903 (gnus-group-goto-group (or (car (nth 2 previous
)) group
))
904 (when (and gnus-topic-mode
906 (not gnus-topic-inhibit-change-level
))
907 ;; Remove the group from the topics.
908 (if (and (< oldlevel gnus-level-zombie
)
909 (>= level gnus-level-zombie
))
910 (let ((alist gnus-topic-alist
))
911 (while (gnus-group-goto-group group
)
914 (when (member group
(car alist
))
915 (setcdr (car alist
) (delete group
(cdar alist
))))
917 ;; If the group is subscribed we enter it into the topics.
918 (when (and (< level gnus-level-zombie
)
919 (>= oldlevel gnus-level-zombie
))
920 (let* ((prev (gnus-group-group-name))
921 (gnus-topic-inhibit-change-level t
)
922 (gnus-group-indentation
924 (* gnus-topic-indent-level
926 (gnus-topic-goto-topic (gnus-current-topic))
927 (gnus-group-topic-level))
930 (yanked (list group
))
932 ;; Then we enter the yanked groups into the topics
934 (when (setq alist
(assoc (save-excursion
938 (caar gnus-topic-topology
)))
941 (when (stringp yanked
)
942 (setq yanked
(list yanked
)))
945 (if (not (cdr alist
))
946 (setcdr alist
(nconc yanked
(cdr alist
)))
947 (while (and (not end
) (cdr alist
))
948 (when (equal (cadr alist
) prev
)
949 (setcdr alist
(nconc yanked
(cdr alist
)))
951 (setq alist
(cdr alist
)))
953 (nconc talist yanked
))))))
954 (gnus-topic-update-topic))))))))
956 (defun gnus-topic-goto-next-group (group props
)
957 "Go to group or the next group after group."
959 (if (not (memq 'gnus-topic props
))
960 (goto-char (point-max))
961 (let ((topic (symbol-name (cadr (memq 'gnus-topic props
)))))
962 (or (gnus-topic-goto-topic topic
)
963 (gnus-topic-goto-topic (gnus-topic-next-topic topic
)))))
964 (if (gnus-group-goto-group group
)
966 ;; The group is no longer visible.
967 (let* ((list (assoc (gnus-group-topic group
) gnus-topic-alist
))
968 (topic-visible (save-excursion (gnus-topic-goto-topic (car list
))))
969 (after (and topic-visible
(cdr (member group
(cdr list
))))))
970 ;; First try to put point on a group after the current one.
972 (not (gnus-group-goto-group (car after
))))
973 (setq after
(cdr after
)))
974 ;; Then try to put point on a group before point.
976 (setq after
(cdr (member group
(reverse (cdr list
)))))
978 (not (gnus-group-goto-group (car after
))))
979 (setq after
(cdr after
))))
980 ;; Finally, just put point on the topic.
982 (goto-char (point-min))
985 (gnus-goto-char topic-visible
)
986 (gnus-topic-goto-topic (gnus-topic-next-topic (car list
))))
990 ;;; Topic-active functions
992 (defun gnus-topic-grok-active (&optional force
)
993 "Parse all active groups and create topic structures for them."
994 ;; First we make sure that we have really read the active file.
996 (not gnus-topic-active-alist
))
998 ;; Get a list of all groups available.
999 (mapatoms (lambda (g) (when (symbol-value g
)
1000 (push (symbol-name g
) groups
)))
1002 (setq groups
(sort groups
'string
<))
1003 ;; Init the variables.
1004 (setq gnus-topic-active-topology
(list (list "" 'visible
)))
1005 (setq gnus-topic-active-alist nil
)
1006 ;; Descend the top-level hierarchy.
1007 (gnus-topic-grok-active-1 gnus-topic-active-topology groups
)
1008 ;; Set the top-level topic names to something nice.
1009 (setcar (car gnus-topic-active-topology
) "Gnus active")
1010 (setcar (car gnus-topic-active-alist
) "Gnus active"))))
1012 (defun gnus-topic-grok-active-1 (topology groups
)
1013 (let* ((name (caar topology
))
1014 (prefix (concat "^" (regexp-quote name
)))
1015 tgroups ntopology group
)
1017 (string-match prefix
(setq group
(car groups
))))
1018 (if (not (string-match "\\." group
(match-end 0)))
1019 ;; There are no further hierarchies here, so we just
1020 ;; enter this group into the list belonging to this
1022 (push (pop groups
) tgroups
)
1023 ;; New sub-hierarchy, so we add it to the topology.
1024 (nconc topology
(list (setq ntopology
1025 (list (list (substring
1026 group
0 (match-end 0))
1028 ;; Descend the hierarchy.
1029 (setq groups
(gnus-topic-grok-active-1 ntopology groups
))))
1030 ;; We remove the trailing "." from the topic name.
1032 (if (string-match "\\.$" name
)
1033 (substring name
0 (match-beginning 0))
1035 ;; Add this topic and its groups to the topic alist.
1036 (push (cons name
(nreverse tgroups
)) gnus-topic-active-alist
)
1037 (setcar (car topology
) name
)
1038 ;; We return the rest of the groups that didn't belong
1042 ;;; Topic mode, commands and keymap.
1044 (defvar gnus-topic-mode-map nil
)
1045 (defvar gnus-group-topic-map nil
)
1047 (unless gnus-topic-mode-map
1048 (setq gnus-topic-mode-map
(make-sparse-keymap))
1050 ;; Override certain group mode keys.
1051 (gnus-define-keys gnus-topic-mode-map
1052 "=" gnus-topic-select-group
1053 "\r" gnus-topic-select-group
1054 " " gnus-topic-read-group
1055 "\C-c\C-x" gnus-topic-expire-articles
1056 "c" gnus-topic-catchup-articles
1057 "\C-k" gnus-topic-kill-group
1058 "\C-y" gnus-topic-yank-group
1059 "\M-g" gnus-topic-get-new-news-this-topic
1060 "AT" gnus-topic-list-active
1061 "Gp" gnus-topic-edit-parameters
1062 "#" gnus-topic-mark-topic
1063 "\M-#" gnus-topic-unmark-topic
1064 [tab] gnus-topic-indent
1065 [(meta tab)] gnus-topic-unindent
1066 "\C-i" gnus-topic-indent
1067 "\M-\C-i" gnus-topic-unindent
1068 gnus-mouse-2 gnus-mouse-pick-topic)
1070 ;; Define a new submap.
1071 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
1072 "#" gnus-topic-mark-topic
1073 "\M-#" gnus-topic-unmark-topic
1074 "n" gnus-topic-create-topic
1075 "m" gnus-topic-move-group
1076 "D" gnus-topic-remove-group
1077 "c" gnus-topic-copy-group
1078 "h" gnus-topic-hide-topic
1079 "s" gnus-topic-show-topic
1080 "j" gnus-topic-jump-to-topic
1081 "M" gnus-topic-move-matching
1082 "C" gnus-topic-copy-matching
1083 "\M-p" gnus-topic-goto-previous-topic
1084 "\M-n" gnus-topic-goto-next-topic
1085 "\C-i" gnus-topic-indent
1086 [tab] gnus-topic-indent
1087 "r" gnus-topic-rename
1088 "\177" gnus-topic-delete
1089 [delete] gnus-topic-delete
1090 "H" gnus-topic-toggle-display-empty-topics)
1092 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1093 "s" gnus-topic-sort-groups
1094 "a" gnus-topic-sort-groups-by-alphabet
1095 "u" gnus-topic-sort-groups-by-unread
1096 "l" gnus-topic-sort-groups-by-level
1097 "e" gnus-topic-sort-groups-by-server
1098 "v" gnus-topic-sort-groups-by-score
1099 "r" gnus-topic-sort-groups-by-rank
1100 "m" gnus-topic-sort-groups-by-method))
1102 (defun gnus-topic-make-menu-bar ()
1103 (unless (boundp 'gnus-topic-menu)
1105 gnus-topic-menu gnus-topic-mode-map ""
1107 ["Toggle topics" gnus-topic-mode t]
1109 ["Copy..." gnus-topic-copy-group t]
1110 ["Move..." gnus-topic-move-group t]
1111 ["Remove" gnus-topic-remove-group t]
1112 ["Copy matching..." gnus-topic-copy-matching t]
1113 ["Move matching..." gnus-topic-move-matching t])
1115 ["Goto..." gnus-topic-jump-to-topic t]
1116 ["Show" gnus-topic-show-topic t]
1117 ["Hide" gnus-topic-hide-topic t]
1118 ["Delete" gnus-topic-delete t]
1119 ["Rename..." gnus-topic-rename t]
1120 ["Create..." gnus-topic-create-topic t]
1121 ["Mark" gnus-topic-mark-topic t]
1122 ["Indent" gnus-topic-indent t]
1123 ["Sort" gnus-topic-sort-topics t]
1124 ["Previous topic" gnus-topic-goto-previous-topic t]
1125 ["Next topic" gnus-topic-goto-next-topic t]
1126 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1127 ["Edit parameters" gnus-topic-edit-parameters t])
1128 ["List active" gnus-topic-list-active t]))))
1130 (define-minor-mode gnus-topic-mode
1131 "Minor mode for topicsifying Gnus group buffers."
1132 :lighter " Topic" :keymap gnus-topic-mode-map
1133 (if (not (derived-mode-p 'gnus-group-mode))
1134 (setq gnus-topic-mode nil)
1135 ;; Infest Gnus with topics.
1136 (if (not gnus-topic-mode)
1137 (setq gnus-goto-missing-group-function nil)
1138 (when (gnus-visual-p 'topic-menu 'menu)
1139 (gnus-topic-make-menu-bar))
1140 (gnus-set-format 'topic t)
1141 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1142 (set (make-local-variable 'gnus-group-prepare-function)
1143 'gnus-group-prepare-topics)
1144 (set (make-local-variable 'gnus-group-get-parameter-function)
1145 'gnus-group-topic-parameters)
1146 (set (make-local-variable 'gnus-group-goto-next-group-function)
1147 'gnus-topic-goto-next-group)
1148 (set (make-local-variable 'gnus-group-indentation-function)
1149 'gnus-topic-group-indentation)
1150 (set (make-local-variable 'gnus-group-update-group-function)
1151 'gnus-topic-update-topics-containing-group)
1152 (set (make-local-variable 'gnus-group-sort-alist-function)
1153 'gnus-group-sort-topic)
1154 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1155 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1156 (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
1157 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1159 (setq gnus-topology-checked-p nil)
1160 ;; We check the topology.
1161 (when gnus-newsrc-alist
1162 (gnus-topic-check-topology)))
1163 ;; Remove topic infestation.
1164 (unless gnus-topic-mode
1165 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1166 (setq gnus-group-change-level-function nil)
1167 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1168 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1169 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1170 (when (gmm-called-interactively-p 'any)
1171 (gnus-group-list-groups))))
1173 (defun gnus-topic-select-group (&optional all)
1174 "Select this newsgroup.
1175 No article is selected automatically.
1176 If the group is opened, just switch the summary buffer.
1177 If ALL is non-nil, already read articles become readable.
1179 If ALL is a positive number, fetch this number of the latest
1180 articles in the group. If ALL is a negative number, fetch this
1181 number of the earliest articles in the group.
1183 If performed over a topic line, toggle folding the topic."
1185 (when (and (eobp) (not (gnus-group-group-name)))
1187 (if (gnus-group-topic-p)
1188 (let ((gnus-group-list-mode
1189 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1190 (gnus-topic-fold all)
1191 (gnus-dribble-touch))
1192 (gnus-group-select-group all)))
1194 (defun gnus-mouse-pick-topic (e)
1195 "Select the group or topic under the mouse pointer."
1198 (gnus-topic-read-group nil))
1200 (defun gnus-topic-expire-articles (topic)
1201 "Expire articles in this topic or group."
1202 (interactive (list (gnus-group-topic-name)))
1204 (call-interactively 'gnus-group-expire-articles)
1206 (gnus-message 5 "Expiring groups in %s..." topic)
1207 (let ((gnus-group-marked
1208 (mapcar (lambda (entry) (car (nth 2 entry)))
1209 (gnus-topic-find-groups topic gnus-level-killed t
1211 (gnus-group-expire-articles nil))
1212 (gnus-message 5 "Expiring groups in %s...done" topic))))
1214 (defun gnus-topic-catchup-articles (topic)
1215 "Catchup this topic or group.
1216 Also see `gnus-group-catchup'."
1217 (interactive (list (gnus-group-topic-name)))
1219 (call-interactively 'gnus-group-catchup-current)
1222 (mapcar (lambda (entry) (car (nth 2 entry)))
1223 (gnus-topic-find-groups topic gnus-level-killed t
1225 (inhibit-read-only t)
1226 (gnus-group-marked groups))
1227 (gnus-group-catchup-current)
1228 (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1230 (defun gnus-topic-read-group (&optional all no-article group)
1231 "Read news in this newsgroup.
1232 If the prefix argument ALL is non-nil, already read articles become
1235 If ALL is a positive number, fetch this number of the latest
1236 articles in the group. If ALL is a negative number, fetch this
1237 number of the earliest articles in the group.
1239 If the optional argument NO-ARTICLE is non-nil, no article will
1240 be auto-selected upon group entry. If GROUP is non-nil, fetch
1243 If performed over a topic line, toggle folding the topic."
1245 (when (and (eobp) (not (gnus-group-group-name)))
1247 (if (gnus-group-topic-p)
1248 (let ((gnus-group-list-mode
1249 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1250 (gnus-topic-fold all))
1251 (gnus-group-read-group all no-article group)))
1253 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1254 "Create a new TOPIC under PARENT.
1255 When used interactively, PARENT will be the topic under point."
1258 (read-string "New topic: ")
1259 (gnus-current-topic)))
1260 ;; Check whether this topic already exists.
1261 (when (gnus-topic-find-topology topic)
1262 (error "Topic already exists"))
1264 (setq parent (caar gnus-topic-topology)))
1265 (let ((top (cdr (gnus-topic-find-topology parent)))
1266 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1268 (error "No such parent topic: %s" parent))
1271 (while (and (cdr top)
1272 (not (equal (caaadr top) previous)))
1273 (setq top (cdr top)))
1274 (setcdr top (cons full-topic (cdr top))))
1275 (nconc top (list full-topic)))
1276 (unless (assoc topic gnus-topic-alist)
1277 (push (list topic) gnus-topic-alist)))
1278 (gnus-topic-enter-dribble)
1279 (gnus-group-list-groups)
1280 (gnus-topic-goto-topic topic))
1283 ;; 1. When the marked groups are overlapped with the process
1284 ;; region, the behavior of move or remove is not right.
1285 ;; 2. Can't process on several marked groups with a same name,
1286 ;; because gnus-group-marked only keeps one copy.
1288 (defvar gnus-topic-history nil)
1290 (defun gnus-topic-move-group (n topic &optional copyp)
1291 "Move the next N groups to TOPIC.
1292 If COPYP, copy the groups instead."
1294 (list current-prefix-arg
1295 (gnus-completing-read "Move to topic" (mapcar 'car gnus-topic-alist) t
1296 nil 'gnus-topic-history)))
1297 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1298 gnus-group-marked t))
1299 (groups (gnus-group-process-prefix n))
1300 (topicl (assoc topic gnus-topic-alist))
1301 (start-topic (gnus-group-topic-name))
1302 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1304 (if (and (not groups) (not copyp) start-topic)
1305 (gnus-topic-move start-topic topic)
1307 (gnus-group-remove-mark g use-marked)
1309 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1311 (setcdr entry (gnus-delete-first g (cdr entry))))
1312 (nconc topicl (list g)))
1313 (gnus-topic-enter-dribble)
1315 (gnus-group-goto-group start-group)
1316 (gnus-topic-goto-topic start-topic))
1317 (gnus-group-list-groups))))
1319 (defun gnus-topic-remove-group (&optional n)
1320 "Remove the current group from the topic."
1322 (let ((use-marked (and (not n) (not (gnus-region-active-p))
1323 gnus-group-marked t))
1324 (groups (gnus-group-process-prefix n)))
1327 (gnus-group-remove-mark group use-marked)
1328 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1329 (inhibit-read-only t))
1330 (when (and topicl group)
1332 (gnus-delete-first group topicl))
1333 (gnus-topic-update-topic)))
1335 (gnus-topic-enter-dribble)
1336 (gnus-group-position-point)))
1338 (defun gnus-topic-copy-group (n topic)
1339 "Copy the current group to a topic."
1341 (list current-prefix-arg
1342 (gnus-completing-read
1343 "Copy to topic" (mapcar 'car gnus-topic-alist) t)))
1344 (gnus-topic-move-group n topic t))
1346 (defun gnus-topic-kill-group (&optional n discard)
1347 "Kill the next N groups."
1349 (if (gnus-group-topic-p)
1350 (let ((topic (gnus-group-topic-name)))
1352 (gnus-topic-find-topology topic)
1353 (assoc topic gnus-topic-alist))
1354 gnus-topic-killed-topics)
1355 (gnus-topic-remove-topic nil t)
1356 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1357 (gnus-topic-enter-dribble))
1358 (gnus-group-kill-group n discard)
1359 (if (not (gnus-group-topic-p))
1360 (gnus-topic-update-topic)
1361 ;; Move up one line so that we update the right topic.
1363 (gnus-topic-update-topic)
1366 (defun gnus-topic-yank-group (&optional arg)
1367 "Yank the last topic."
1369 (if gnus-topic-killed-topics
1371 (or (gnus-group-topic-name)
1372 (gnus-topic-next-topic (gnus-current-topic))))
1373 (data (pop gnus-topic-killed-topics))
1376 (push alist gnus-topic-alist)
1377 (gnus-topic-create-topic
1378 (caar item) (gnus-topic-parent-topic previous) previous
1380 (gnus-topic-enter-dribble)
1381 (gnus-topic-goto-topic (caar item)))
1382 (let* ((prev (gnus-group-group-name))
1383 (gnus-topic-inhibit-change-level t)
1384 (gnus-group-indentation
1386 (* gnus-topic-indent-level
1388 (gnus-topic-goto-topic (gnus-current-topic))
1389 (gnus-group-topic-level))
1393 ;; We first yank the groups the normal way...
1394 (setq yanked (gnus-group-yank-group arg))
1395 ;; Then we enter the yanked groups into the topics they belong
1397 (setq alist (assoc (save-excursion
1399 (gnus-current-topic))
1401 (when (stringp yanked)
1402 (setq yanked (list yanked)))
1404 (nconc alist yanked)
1405 (if (not (cdr alist))
1406 (setcdr alist (nconc yanked (cdr alist)))
1408 (when (equal (cadr alist) prev)
1409 (setcdr alist (nconc yanked (cdr alist)))
1411 (setq alist (cdr alist))))))
1412 (gnus-topic-update-topic)))
1414 (defun gnus-topic-hide-topic (&optional permanent)
1415 "Hide the current topic.
1416 If PERMANENT, make it stay hidden in subsequent sessions as well."
1418 (when (gnus-current-topic)
1419 (gnus-topic-goto-topic (gnus-current-topic))
1423 (gnus-topic-find-topology (gnus-current-topic))))
1425 (gnus-topic-remove-topic nil nil)))
1427 (defun gnus-topic-show-topic (&optional permanent)
1428 "Show the hidden topic.
1429 If PERMANENT, make it stay shown in subsequent sessions as well."
1431 (when (gnus-group-topic-p)
1433 (gnus-topic-remove-topic t nil)
1435 (gnus-topic-find-topology
1436 (gnus-completing-read "Show topic"
1437 (mapcar 'car gnus-topic-alist) t))))
1438 (setcar (cddr (cadr topic)) nil)
1439 (setcar (cdr (cadr topic)) 'visible)
1440 (gnus-group-list-groups)))))
1442 (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
1443 "Mark all groups in the TOPIC with the process mark.
1444 If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
1445 (interactive (list (gnus-group-topic-name)
1447 (and current-prefix-arg t)))
1449 (call-interactively 'gnus-group-mark-group)
1451 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1452 (not non-recursive))))
1454 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1455 (gnus-info-group (nth 2 (pop groups)))))))))
1457 (defun gnus-topic-unmark-topic (topic &optional dummy non-recursive)
1458 "Remove the process mark from all groups in the TOPIC.
1459 If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
1460 (interactive (list (gnus-group-topic-name)
1462 (and current-prefix-arg t)))
1464 (call-interactively 'gnus-group-unmark-group)
1465 (gnus-topic-mark-topic topic t non-recursive)))
1467 (defun gnus-topic-get-new-news-this-topic (&optional n)
1468 "Check for new news in the current topic."
1470 (if (not (gnus-group-topic-p))
1471 (gnus-group-get-new-news-this-group n)
1472 (let* ((topic (gnus-group-topic-name))
1473 (data (cadr (gnus-topic-find-topology topic))))
1475 (gnus-topic-mark-topic topic nil (and n t))
1476 (gnus-group-get-new-news-this-group))
1477 (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1479 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1480 "Move all groups that match REGEXP to some topic."
1485 (setq topic (gnus-completing-read "Move to topic"
1486 (mapcar 'car gnus-topic-alist) t))
1487 (read-string (format "Move to %s (regexp): " topic))))))
1488 (gnus-group-mark-regexp regexp)
1489 (gnus-topic-move-group nil topic copyp))
1491 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1492 "Copy all groups that match REGEXP to some topic."
1497 (setq topic (gnus-completing-read "Copy to topic"
1498 (mapcar 'car gnus-topic-alist) t))
1499 (read-string (format "Copy to %s (regexp): " topic))))))
1500 (gnus-topic-move-matching regexp topic t))
1502 (defun gnus-topic-delete (topic)
1504 (interactive (list (gnus-group-topic-name)))
1506 (error "No topic to be deleted"))
1507 (let ((entry (assoc topic gnus-topic-alist))
1508 (inhibit-read-only t))
1510 (error "Topic not empty"))
1511 ;; Delete if visible.
1512 (when (gnus-topic-goto-topic topic)
1514 ;; Remove from alist.
1515 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1516 ;; Remove from topology.
1517 (gnus-topic-find-topology topic nil nil 'delete)
1518 (gnus-dribble-touch)))
1520 (defun gnus-topic-rename (old-name new-name)
1523 (let ((topic (gnus-current-topic)))
1525 (read-string (format "Rename %s to: " topic) topic))))
1526 ;; Check whether the new name exists.
1527 (when (gnus-topic-find-topology new-name)
1528 (error "Topic '%s' already exists" new-name))
1529 ;; "nil" is an invalid name, for reasons I'd rather not go
1530 ;; into here. Trust me.
1531 (when (equal new-name "nil")
1532 (error "Invalid name: %s" nil))
1534 (let ((top (gnus-topic-find-topology old-name))
1535 (entry (assoc old-name gnus-topic-alist)))
1537 (setcar (cadr top) new-name))
1539 (setcar entry new-name))
1541 (gnus-dribble-touch)
1542 (gnus-group-list-groups)
1545 (defun gnus-topic-indent (&optional unindent)
1546 "Indent a topic -- make it a sub-topic of the previous topic.
1547 If UNINDENT, remove an indentation."
1550 (gnus-topic-unindent)
1551 (let* ((topic (gnus-current-topic))
1552 (parent (gnus-topic-previous-topic topic))
1553 (inhibit-read-only t))
1555 (error "Nothing to indent %s into" topic))
1557 (gnus-topic-goto-topic topic)
1558 (gnus-topic-kill-group)
1559 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1560 (gnus-topic-create-topic
1561 topic parent nil (cdar (car gnus-topic-killed-topics)))
1562 (pop gnus-topic-killed-topics)
1563 (or (gnus-topic-goto-topic topic)
1564 (gnus-topic-goto-topic parent))))))
1566 (defun gnus-topic-unindent ()
1569 (let* ((topic (gnus-current-topic))
1570 (parent (gnus-topic-parent-topic topic))
1571 (grandparent (gnus-topic-parent-topic parent)))
1573 (error "Nothing to indent %s into" topic))
1575 (gnus-topic-goto-topic topic)
1576 (gnus-topic-kill-group)
1577 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1578 (gnus-topic-create-topic
1579 topic grandparent (gnus-topic-next-topic parent)
1580 (cdar (car gnus-topic-killed-topics)))
1581 (pop gnus-topic-killed-topics)
1582 (gnus-topic-goto-topic topic))))
1584 (defun gnus-topic-list-active (&optional force)
1585 "List all groups that Gnus knows about in a topicsified fashion.
1586 If FORCE, always re-read the active file."
1589 (gnus-get-killed-groups))
1590 (gnus-topic-grok-active force)
1591 (let ((gnus-topic-topology gnus-topic-active-topology)
1592 (gnus-topic-alist gnus-topic-active-alist)
1593 gnus-killed-list gnus-zombie-list)
1594 (gnus-group-list-groups gnus-level-killed nil 1)))
1596 (defun gnus-topic-toggle-display-empty-topics ()
1597 "Show/hide topics that have no unread articles."
1599 (setq gnus-topic-display-empty-topics
1600 (not gnus-topic-display-empty-topics))
1601 (gnus-group-list-groups)
1602 (message "%s empty topics"
1603 (if gnus-topic-display-empty-topics
1604 "Showing" "Hiding")))
1606 ;;; Topic sorting functions
1608 (defun gnus-topic-edit-parameters (group)
1609 "Edit the group parameters of GROUP.
1610 If performed on a topic, edit the topic parameters instead."
1611 (interactive (list (gnus-group-group-name)))
1613 (gnus-group-edit-group-parameters group)
1614 (if (not (gnus-group-topic-p))
1615 (error "Nothing to edit on the current line")
1616 (let ((topic (gnus-group-topic-name)))
1618 (gnus-topic-parameters topic)
1619 (format "Editing the topic parameters for `%s'."
1622 (gnus-topic-set-parameters ,topic form)))))))
1624 (defun gnus-group-sort-topic (func reverse)
1625 "Sort groups in the topics according to FUNC and REVERSE."
1626 (let ((alist gnus-topic-alist))
1628 ;; !!!Sometimes nil elements sneak into the alist,
1629 ;; for some reason or other.
1630 (setcar alist (delq nil (car alist)))
1631 (setcar alist (delete "dummy.group" (car alist)))
1632 (gnus-topic-sort-topic (pop alist) func reverse))))
1634 (defun gnus-topic-sort-topic (topic func reverse)
1635 ;; Each topic only lists the name of the group, while
1636 ;; the sort predicates expect group infos as inputs.
1637 ;; So we first transform the group names into infos,
1638 ;; then sort, and then transform back into group names.
1642 (lambda (info) (gnus-info-group info))
1645 (lambda (group) (gnus-get-info group))
1648 ;; Do the reversal, if necessary.
1650 (setcdr topic (nreverse (cdr topic)))))
1652 (defun gnus-topic-sort-groups (func &optional reverse)
1653 "Sort the current topic according to FUNC.
1654 If REVERSE, reverse the sorting order."
1655 (interactive (list gnus-group-sort-function current-prefix-arg))
1656 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1657 (gnus-topic-sort-topic
1658 topic (gnus-make-sort-function func) reverse)
1659 (gnus-group-list-groups)))
1661 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1662 "Sort the current topic alphabetically by group name.
1663 If REVERSE, sort in reverse order."
1665 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1667 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1668 "Sort the current topic by number of unread articles.
1669 If REVERSE, sort in reverse order."
1671 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1673 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1674 "Sort the current topic by group level.
1675 If REVERSE, sort in reverse order."
1677 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1679 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1680 "Sort the current topic by group score.
1681 If REVERSE, sort in reverse order."
1683 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1685 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1686 "Sort the current topic by group rank.
1687 If REVERSE, sort in reverse order."
1689 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1691 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1692 "Sort the current topic alphabetically by backend name.
1693 If REVERSE, sort in reverse order."
1695 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1697 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1698 "Sort the current topic alphabetically by server name.
1699 If REVERSE, sort in reverse order."
1701 (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1703 (defun gnus-topic-sort-topics-1 (top reverse)
1706 (mapcar (gnus-byte-compile
1708 (gnus-topic-sort-topics-1 top ,reverse)))
1711 (string-lessp (caar t1) (caar t2)))))))
1712 (setcdr top (if reverse (reverse subtop) subtop))))
1715 (defun gnus-topic-sort-topics (&optional topic reverse)
1716 "Sort topics in TOPIC alphabetically by topic name.
1717 If REVERSE, reverse the sorting order."
1719 (list (gnus-completing-read "Sort topics in"
1720 (mapcar 'car gnus-topic-alist) t
1721 (gnus-current-topic))
1722 current-prefix-arg))
1723 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1724 gnus-topic-topology)))
1725 (gnus-topic-sort-topics-1 topic-topology reverse)
1726 (gnus-topic-enter-dribble)
1727 (gnus-group-list-groups)
1728 (gnus-topic-goto-topic topic)))
1730 (defun gnus-topic-move (current to)
1731 "Move the CURRENT topic to TO."
1734 (gnus-group-topic-name)
1735 (gnus-completing-read "Move to topic" (mapcar 'car gnus-topic-alist) t)))
1736 (unless (and current to)
1737 (error "Can't find topic"))
1738 (let ((current-top (cdr (gnus-topic-find-topology current)))
1739 (to-top (cdr (gnus-topic-find-topology to))))
1741 (error "Can't find topic `%s'" current))
1743 (error "Can't find topic `%s'" to))
1744 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1745 (error "Can't move `%s' to its sub-level" current))
1746 (gnus-topic-find-topology current nil nil 'delete)
1747 (setcdr (last to-top) (list current-top))
1748 (gnus-topic-enter-dribble)
1749 (gnus-group-list-groups)
1750 (gnus-topic-goto-topic current)))
1752 (defun gnus-subscribe-topics (newsgroup)
1754 (let (match gnus-group-change-level-function)
1755 (dolist (topic (gnus-topic-list))
1756 (when (and (setq match (cdr (assq 'subscribe
1757 (gnus-topic-parameters topic))))
1758 (string-match match newsgroup))
1759 ;; Just subscribe the group.
1760 (gnus-subscribe-alphabetically newsgroup)
1761 ;; Add the group to the topic.
1762 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1763 ;; if this topic specifies a default level, use it
1764 (let ((subscribe-level (cdr (assq 'subscribe-level
1765 (gnus-topic-parameters topic)))))
1766 (when subscribe-level
1767 (gnus-group-change-level newsgroup subscribe-level
1768 gnus-level-default-subscribed)))
1772 (provide 'gnus-topic)
1774 ;;; gnus-topic.el ends here