Add isearch-yank-symbol-or-char
[emacs.git] / lisp / gnus / gnus-topic.el
blobddaace9a24db015b7f2b633df42599341566aa30
1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
3 ;; Copyright (C) 1995-2018 Free Software Foundation, Inc.
5 ;; Author: Ilja Weis <kult@uni-paderborn.de>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile (require 'cl))
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-start)
33 (require 'gnus-util)
35 (defgroup gnus-topic nil
36 "Group topics."
37 :group 'gnus-group)
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."
44 :type 'hook
45 :group 'gnus-topic)
47 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
48 "Format of topic lines.
49 It works along the same lines as a normal formatting string,
50 with some simple extensions.
52 %i Indentation based on topic level.
53 %n Topic name.
54 %v Nothing if the topic is visible, \"...\" otherwise.
55 %g Number of groups in the topic.
56 %a Number of unread articles in the groups in the topic.
57 %A Number of unread articles in the groups in the topic and its subtopics.
59 General format specifiers can also be used.
60 See Info node `(gnus)Formatting Variables'."
61 :link '(custom-manual "(gnus)Formatting Variables")
62 :type 'string
63 :group 'gnus-topic)
65 (defcustom gnus-topic-indent-level 2
66 "How much each subtopic should be indented."
67 :type 'integer
68 :group 'gnus-topic)
70 (defcustom gnus-topic-display-empty-topics t
71 "If non-nil, display the topic lines even of topics that have no unread articles."
72 :type 'boolean
73 :group 'gnus-topic)
75 ;; Internal variables.
77 (defvar gnus-topic-active-topology nil)
78 (defvar gnus-topic-active-alist nil)
79 (defvar gnus-topic-unreads nil)
81 (defvar gnus-topology-checked-p nil
82 "Whether the topology has been checked in this session.")
84 (defvar gnus-topic-killed-topics nil)
85 (defvar gnus-topic-inhibit-change-level nil)
87 (defconst gnus-topic-line-format-alist
88 `((?n name ?s)
89 (?v visible ?s)
90 (?i indentation ?s)
91 (?g number-of-groups ?d)
92 (?a (gnus-topic-articles-in-topic entries) ?d)
93 (?A total-number-of-articles ?d)
94 (?l level ?d)))
96 (defvar gnus-topic-line-format-spec nil)
98 ;;; Utility functions
100 (defun gnus-group-topic-name ()
101 "The name of the topic on the current line."
102 (let ((topic (get-text-property (point-at-bol) 'gnus-topic)))
103 (and topic (symbol-name topic))))
105 (defun gnus-group-topic-level ()
106 "The level of the topic on the current line."
107 (get-text-property (point-at-bol) 'gnus-topic-level))
109 (defun gnus-group-topic-unread ()
110 "The number of unread articles in topic on the current line."
111 (get-text-property (point-at-bol) 'gnus-topic-unread))
113 (defun gnus-topic-unread (topic)
114 "Return the number of unread articles in TOPIC."
115 (or (cdr (assoc topic gnus-topic-unreads))
118 (defun gnus-group-topic-p ()
119 "Return non-nil if the current line is a topic."
120 (gnus-group-topic-name))
122 (defun gnus-topic-visible-p ()
123 "Return non-nil if the current topic is visible."
124 (get-text-property (point-at-bol) 'gnus-topic-visible))
126 (defun gnus-topic-articles-in-topic (entries)
127 (let ((total 0)
128 number)
129 (while entries
130 (when (numberp (setq number (car (pop entries))))
131 (incf total number)))
132 total))
134 (defun gnus-group-topic (group)
135 "Return the topic GROUP is a member of."
136 (let ((alist gnus-topic-alist)
137 out)
138 (while alist
139 (when (member group (cdar alist))
140 (setq out (caar alist)
141 alist nil))
142 (setq alist (cdr alist)))
143 out))
145 (defun gnus-topic-goto-topic (topic)
146 (when topic
147 (gnus-goto-char (text-property-any (point-min) (point-max)
148 'gnus-topic (intern topic)))))
150 (defun gnus-topic-jump-to-topic (topic)
151 "Go to TOPIC."
152 (interactive
153 (list (gnus-completing-read "Go to topic" (gnus-topic-list) t)))
154 (let ((inhibit-read-only t))
155 (dolist (topic (gnus-current-topics topic))
156 (unless (gnus-topic-goto-topic topic)
157 (gnus-topic-goto-missing-topic topic)
158 (gnus-topic-display-missing-topic topic))))
159 (gnus-topic-goto-topic topic))
161 (defun gnus-current-topic ()
162 "Return the name of the current topic."
163 (let ((result
164 (or (get-text-property (point) 'gnus-topic)
165 (save-excursion
166 (and (gnus-goto-char (previous-single-property-change
167 (point) 'gnus-topic))
168 (get-text-property (max (1- (point)) (point-min))
169 'gnus-topic))))))
170 (when result
171 (symbol-name result))))
173 (defun gnus-current-topics (&optional topic)
174 "Return a list of all current topics, lowest in hierarchy first.
175 If TOPIC, start with that topic."
176 (let ((topic (or topic (gnus-current-topic)))
177 topics)
178 (while topic
179 (push topic topics)
180 (setq topic (gnus-topic-parent-topic topic)))
181 (nreverse topics)))
183 (defun gnus-group-active-topic-p ()
184 "Say whether the current topic comes from the active topics."
185 (get-text-property (point-at-bol) 'gnus-active))
187 (defun gnus-topic-find-groups (topic &optional level all lowest recursive)
188 "Return entries for all visible groups in TOPIC.
189 If RECURSIVE is t, return groups in its subtopics too."
190 (let ((groups (cdr (assoc topic gnus-topic-alist)))
191 info clevel unread group params visible-groups entry active)
192 (setq lowest (or lowest 1))
193 (setq level (or level gnus-level-unsubscribed))
194 ;; We go through the newsrc to look for matches.
195 (while groups
196 (when (setq group (pop groups))
197 (setq entry (gnus-group-entry group)
198 info (nth 2 entry)
199 params (gnus-info-params info)
200 active (gnus-active group)
201 unread (or (car entry)
202 (and (not (equal group "dummy.group"))
203 active
204 (- (1+ (cdr active)) (car active))))
205 clevel (or (gnus-info-level info)
206 (if (member group gnus-zombie-list)
207 gnus-level-zombie gnus-level-killed))))
208 (and
209 info ; nil means that the group is dead.
210 (<= clevel level)
211 (>= clevel lowest) ; Is inside the level we want.
212 (or all
213 (if (or (eq unread t)
214 (eq unread nil))
215 gnus-group-list-inactive-groups
216 (> unread 0))
217 (and gnus-list-groups-with-ticked-articles
218 (cdr (assq 'tick (gnus-info-marks info))))
219 ;; Has right readedness.
220 ;; Check for permanent visibility.
221 (and gnus-permanently-visible-groups
222 (string-match gnus-permanently-visible-groups group))
223 ;; Marked groups are always visible.
224 (member group gnus-group-marked)
225 (memq 'visible params)
226 (cdr (assq 'visible params)))
227 ;; Add this group to the list of visible groups.
228 (push (or entry group) visible-groups)))
229 (setq visible-groups (nreverse visible-groups))
230 (when recursive
231 (if (eq recursive t)
232 (setq recursive (cdr (gnus-topic-find-topology topic))))
233 (dolist (topic-topology (cdr recursive))
234 (setq visible-groups
235 (nconc visible-groups
236 (gnus-topic-find-groups
237 (caar topic-topology)
238 level all lowest topic-topology)))))
239 visible-groups))
241 (defun gnus-topic-goto-previous-topic (n)
242 "Go to the N'th previous topic."
243 (interactive "p")
244 (gnus-topic-goto-next-topic (- n)))
246 (defun gnus-topic-goto-next-topic (n)
247 "Go to the N'th next topic."
248 (interactive "p")
249 (let ((backward (< n 0))
250 (n (abs n))
251 (topic (gnus-current-topic)))
252 (while (and (> n 0)
253 (setq topic
254 (if backward
255 (gnus-topic-previous-topic topic)
256 (gnus-topic-next-topic topic))))
257 (gnus-topic-goto-topic topic)
258 (setq n (1- n)))
259 (when (/= 0 n)
260 (gnus-message 7 "No more topics"))
263 (defun gnus-topic-previous-topic (topic)
264 "Return the previous topic on the same level as TOPIC."
265 (let ((top (cddr (gnus-topic-find-topology
266 (gnus-topic-parent-topic topic)))))
267 (unless (equal topic (caaar top))
268 (while (and top (not (equal (caaadr top) topic)))
269 (setq top (cdr top)))
270 (caaar top))))
272 (defun gnus-topic-parent-topic (topic &optional topology)
273 "Return the parent of TOPIC."
274 (unless topology
275 (setq topology gnus-topic-topology))
276 (let ((parent (car (pop topology)))
277 result found)
278 (while (and topology
279 (not (setq found (equal (caaar topology) topic)))
280 (not (setq result (gnus-topic-parent-topic
281 topic (car topology)))))
282 (setq topology (cdr topology)))
283 (or result (and found parent))))
285 (defun gnus-topic-next-topic (topic &optional previous)
286 "Return the next sibling of TOPIC."
287 (let ((parentt (cddr (gnus-topic-find-topology
288 (gnus-topic-parent-topic topic))))
289 prev)
290 (while (and parentt
291 (not (equal (caaar parentt) topic)))
292 (setq prev (caaar parentt)
293 parentt (cdr parentt)))
294 (if previous
295 prev
296 (caaadr parentt))))
298 (defun gnus-topic-forward-topic (num)
299 "Go to the next topic on the same level as the current one."
300 (let* ((topic (gnus-current-topic))
301 (way (if (< num 0) 'gnus-topic-previous-topic
302 'gnus-topic-next-topic))
303 (num (abs num)))
304 (while (and (not (zerop num))
305 (setq topic (funcall way topic)))
306 (when (gnus-topic-goto-topic topic)
307 (decf num)))
308 (unless (zerop num)
309 (goto-char (point-max)))
310 num))
312 (defun gnus-topic-find-topology (topic &optional topology level remove)
313 "Return the topology of TOPIC."
314 (unless topology
315 (setq topology gnus-topic-topology)
316 (setq level 0))
317 (let ((top topology)
318 result)
319 (if (equal (caar topology) topic)
320 (progn
321 (when remove
322 (delq topology remove))
323 (cons level topology))
324 (setq topology (cdr topology))
325 (while (and topology
326 (not (setq result (gnus-topic-find-topology
327 topic (car topology) (1+ level)
328 (and remove top)))))
329 (setq topology (cdr topology)))
330 result)))
332 (defvar gnus-tmp-topics nil)
333 (defun gnus-topic-list (&optional topology)
334 "Return a list of all topics in the topology."
335 (unless topology
336 (setq topology gnus-topic-topology
337 gnus-tmp-topics nil))
338 (push (caar topology) gnus-tmp-topics)
339 (mapc 'gnus-topic-list (cdr topology))
340 gnus-tmp-topics)
342 ;;; Topic parameter jazz
344 (defun gnus-topic-parameters (topic)
345 "Return the parameters for TOPIC."
346 (let ((top (gnus-topic-find-topology topic)))
347 (when top
348 (nth 3 (cadr top)))))
350 (defun gnus-topic-set-parameters (topic parameters)
351 "Set the topic parameters of TOPIC to PARAMETERS."
352 (let ((top (gnus-topic-find-topology topic)))
353 (unless top
354 (error "No such topic: %s" topic))
355 ;; We may have to extend if there is no parameters here
356 ;; to begin with.
357 (unless (nthcdr 2 (cadr top))
358 (nconc (cadr top) (list nil)))
359 (unless (nthcdr 3 (cadr top))
360 (nconc (cadr top) (list nil)))
361 (setcar (nthcdr 3 (cadr top)) parameters)
362 (gnus-dribble-enter
363 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
365 (defun gnus-group-topic-parameters (group)
366 "Compute the group parameters for GROUP in topic mode.
367 Possibly inherit parameters from topics above GROUP."
368 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
369 (save-excursion
370 (gnus-topic-hierarchical-parameters
371 ;; First we try to go to the group within the group buffer and find the
372 ;; topic for the group that way. This hopefully copes well with groups
373 ;; that are in more than one topic. Failing that (i.e. when the group
374 ;; isn't visible in the group buffer) we find a topic for the group via
375 ;; gnus-group-topic.
376 (or (and (gnus-group-goto-group group)
377 (gnus-current-topic))
378 (gnus-group-topic group))
379 params-list))))
381 (defun gnus-topic-hierarchical-parameters (topic &optional group-params-list)
382 "Compute the topic parameters for TOPIC.
383 Possibly inherit parameters from topics above TOPIC.
384 If optional argument GROUP-PARAMS-LIST is non-nil, use it as the basis for
385 inheritance."
386 (let ((params-list
387 ;; We probably have lots of nil elements here, so we remove them.
388 ;; Probably faster than doing this "properly".
389 (delq nil (cons group-params-list
390 (mapcar 'gnus-topic-parameters
391 (gnus-current-topics topic)))))
392 param out params)
393 ;; Now we have all the parameters, so we go through them
394 ;; and do inheritance in the obvious way.
395 (let (posting-style)
396 (while (setq params (pop params-list))
397 (while (setq param (pop params))
398 (when (atom param)
399 (setq param (cons param t)))
400 (cond ((eq (car param) 'posting-style)
401 (let ((param (cdr param))
402 elt)
403 (while (setq elt (pop param))
404 (unless (assoc (car elt) posting-style)
405 (push elt posting-style)))))
407 (unless (assq (car param) out)
408 (push param out))))))
409 (and posting-style (push (cons 'posting-style posting-style) out)))
410 ;; Return the resulting parameter list.
411 out))
413 ;;; General utility functions
415 (defun gnus-topic-enter-dribble ()
416 (gnus-dribble-enter
417 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
419 ;;; Generating group buffers
421 (defun gnus-group-prepare-topics (level &optional predicate lowest
422 regexp list-topic topic-level)
423 "List all newsgroups with unread articles of level LEVEL or lower.
424 Use the `gnus-group-topics' to sort the groups.
425 If PREDICATE is a function, list groups that the function returns non-nil;
426 if it is t, list groups that have no unread articles.
427 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
428 (set-buffer gnus-group-buffer)
429 (let ((inhibit-read-only t)
430 (lowest (or lowest 1))
431 (not-in-list
432 (and gnus-group-listed-groups
433 (copy-sequence gnus-group-listed-groups))))
435 (gnus-update-format-specifications nil 'topic)
437 (when (or (not gnus-topic-alist)
438 (not gnus-topology-checked-p))
439 (gnus-topic-check-topology))
441 (unless list-topic
442 (erase-buffer))
444 ;; List dead groups?
445 (when (or gnus-group-listed-groups
446 (and (>= level gnus-level-zombie)
447 (<= lowest gnus-level-zombie)))
448 (gnus-group-prepare-flat-list-dead
449 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
450 gnus-level-zombie ?Z
451 regexp))
453 (when (or gnus-group-listed-groups
454 (and (>= level gnus-level-killed)
455 (<= lowest gnus-level-killed)))
456 (gnus-group-prepare-flat-list-dead
457 (setq gnus-killed-list (sort gnus-killed-list 'string<))
458 gnus-level-killed ?K regexp)
459 (when not-in-list
460 (unless gnus-killed-hashtb
461 (gnus-make-hashtable-from-killed))
462 (gnus-group-prepare-flat-list-dead
463 (seq-remove (lambda (group)
464 (or (gnus-group-entry group)
465 (gnus-gethash group gnus-killed-hashtb)))
466 not-in-list)
467 gnus-level-killed ?K regexp)))
469 ;; Use topics.
470 (prog1
471 (when (or (< lowest gnus-level-zombie)
472 gnus-group-listed-groups)
473 (if list-topic
474 (let ((top (gnus-topic-find-topology list-topic)))
475 (gnus-topic-prepare-topic (cdr top) (car top)
476 (or topic-level level) predicate
477 nil lowest regexp))
478 (gnus-topic-prepare-topic gnus-topic-topology 0
479 (or topic-level level) predicate
480 nil lowest regexp)))
481 (gnus-group-set-mode-line)
482 (setq gnus-group-list-mode (cons level predicate))
483 (gnus-run-hooks 'gnus-group-prepare-hook))))
485 (defun gnus-topic-prepare-topic (topicl level &optional list-level
486 predicate silent
487 lowest regexp)
488 "Insert TOPIC into the group buffer.
489 If SILENT, don't insert anything. Return the number of unread
490 articles in the topic and its subtopics."
491 (let* ((type (pop topicl))
492 (entries (gnus-topic-find-groups
493 (car type)
494 (if gnus-group-listed-groups
495 gnus-level-killed
496 list-level)
497 (or predicate gnus-group-listed-groups
498 (cdr (assq 'visible
499 (gnus-topic-hierarchical-parameters
500 (car type)))))
501 (if gnus-group-listed-groups 0 lowest)))
502 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
503 (gnus-group-indentation
504 (make-string (* gnus-topic-indent-level level) ? ))
505 (beg (progn (beginning-of-line) (point)))
506 (topicl (reverse topicl))
507 (all-entries entries)
508 (point-max (point-max))
509 (unread 0)
510 info entry end active tick)
511 ;; Insert any sub-topics.
512 (while topicl
513 (incf unread
514 (gnus-topic-prepare-topic
515 (pop topicl) (1+ level) list-level predicate
516 (not visiblep) lowest regexp)))
517 (setq end (point))
518 (goto-char beg)
519 ;; Insert all the groups that belong in this topic.
520 (while (setq entry (pop entries))
521 (when (if (stringp entry)
522 (gnus-group-prepare-logic
523 entry
524 (and
525 (or (not gnus-group-listed-groups)
526 (if (< list-level gnus-level-zombie) nil
527 (let ((entry-level
528 (if (member entry gnus-zombie-list)
529 gnus-level-zombie gnus-level-killed)))
530 (and (<= entry-level list-level)
531 (>= entry-level lowest)))))
532 (cond
533 ((stringp regexp)
534 (string-match regexp entry))
535 ((functionp regexp)
536 (funcall regexp entry))
537 ((null regexp) t)
538 (t nil))))
539 (setq info (nth 2 entry))
540 (gnus-group-prepare-logic
541 (gnus-info-group info)
542 (and (or (not gnus-group-listed-groups)
543 (let ((entry-level (gnus-info-level info)))
544 (and (<= entry-level list-level)
545 (>= entry-level lowest))))
546 (or (not (functionp predicate))
547 (funcall predicate info))
548 (or (not (stringp regexp))
549 (string-match regexp (gnus-info-group info))))))
550 (when visiblep
551 (if (stringp entry)
552 ;; Dead groups.
553 (gnus-group-insert-group-line
554 entry (if (member entry gnus-zombie-list)
555 gnus-level-zombie gnus-level-killed)
556 nil (- (1+ (cdr (setq active (gnus-active entry))))
557 (car active))
558 nil)
559 ;; Living groups.
560 (when (setq info (nth 2 entry))
561 (gnus-group-insert-group-line
562 (gnus-info-group info)
563 (gnus-info-level info) (gnus-info-marks info)
564 (car entry) (gnus-info-method info)))))
565 (when (and (listp entry)
566 (numberp (car entry)))
567 (incf unread (car entry)))
568 (when (listp entry)
569 (setq tick t))))
570 (goto-char beg)
571 ;; Insert the topic line.
572 (when (and (not silent)
573 (or gnus-topic-display-empty-topics ;We want empty topics
574 (not (zerop unread)) ;Non-empty
575 tick ;Ticked articles
576 (/= point-max (point-max)))) ;Inactive groups
577 (gnus-topic-insert-topic-line
578 (car type) visiblep
579 (not (eq (nth 2 type) 'hidden))
580 level all-entries unread))
581 (gnus-topic-update-unreads (car type) unread)
582 (gnus-group--setup-tool-bar-update beg end)
583 (goto-char end)
584 unread))
586 (defun gnus-topic-remove-topic (&optional insert total-remove _hide in-level)
587 "Remove the current topic."
588 (let ((topic (gnus-group-topic-name))
589 (level (gnus-group-topic-level))
590 (beg (progn (beginning-of-line) (point)))
591 buffer-read-only)
592 (when topic
593 (while (and (zerop (forward-line 1))
594 (> (or (gnus-group-topic-level) (1+ level)) level)))
595 (delete-region beg (point))
596 ;; Do the change in this rather odd manner because it has been
597 ;; reported that some topics share parts of some lists, for some
598 ;; reason. I have been unable to determine why this is the
599 ;; case, but this hack seems to take care of things.
600 (let ((data (cadr (gnus-topic-find-topology topic))))
601 (setcdr data
602 (list (if insert 'visible 'invisible)
603 (caddr data)
604 (cadddr data))))
605 (if total-remove
606 (setq gnus-topic-alist
607 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
608 (gnus-topic-insert-topic topic in-level)))))
610 (defun gnus-topic-insert-topic (topic &optional level)
611 "Insert TOPIC."
612 (gnus-group-prepare-topics
613 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
614 nil nil topic level))
616 (defun gnus-topic-fold (&optional insert topic)
617 "Remove/insert the current topic."
618 (let ((topic (or topic (gnus-group-topic-name))))
619 (when topic
620 (save-excursion
621 (if (not (gnus-group-active-topic-p))
622 (gnus-topic-remove-topic
623 (or insert (not (gnus-topic-visible-p))))
624 (let ((gnus-topic-topology gnus-topic-active-topology)
625 (gnus-topic-alist gnus-topic-active-alist)
626 (gnus-group-list-mode (cons 5 t)))
627 (gnus-topic-remove-topic
628 (or insert (not (gnus-topic-visible-p))) nil nil 9)
629 (gnus-topic-enter-dribble)))))))
631 (defvar gnus-tmp-header)
633 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
634 &optional unread)
635 (let* ((visible (if visiblep "" "..."))
636 (indentation (make-string (* gnus-topic-indent-level level) ? ))
637 (total-number-of-articles unread)
638 (number-of-groups (length entries))
639 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
640 gnus-tmp-header)
641 (gnus-topic-update-unreads name unread)
642 (beginning-of-line)
643 ;; Insert the text.
644 (if shownp
645 (add-text-properties
646 (point)
647 (prog1 (1+ (point))
648 (eval gnus-topic-line-format-spec))
649 (list 'gnus-topic (intern name)
650 'gnus-topic-level level
651 'gnus-topic-unread unread
652 'gnus-active active-topic
653 'gnus-topic-visible visiblep)))))
655 (defun gnus-topic-update-unreads (topic unreads)
656 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
657 gnus-topic-unreads))
658 (push (cons topic unreads) gnus-topic-unreads))
660 (defun gnus-topic-update-topics-containing-group (group)
661 "Update all topics that have GROUP as a member."
662 (when (and (eq major-mode 'gnus-group-mode)
663 gnus-topic-mode)
664 (save-excursion
665 (let ((alist gnus-topic-alist))
666 ;; This is probably not entirely correct. If a topic
667 ;; isn't shown, then it's not updated. But the updating
668 ;; should be performed in any case, since the topic's
669 ;; parent should be updated. Pfft.
670 (while alist
671 (when (and (member group (cdar alist))
672 (gnus-topic-goto-topic (caar alist)))
673 (gnus-topic-update-topic-line (caar alist)))
674 (pop alist))))))
676 (defun gnus-topic-update-topic ()
677 "Update all parent topics to the current group."
678 (when (and (eq major-mode 'gnus-group-mode)
679 gnus-topic-mode)
680 (let ((group (gnus-group-group-name))
681 (m (point-marker))
682 (inhibit-read-only t))
683 (when (and group
684 (gnus-get-info group)
685 (gnus-topic-goto-topic (gnus-current-topic)))
686 (gnus-topic-update-topic-line (gnus-group-topic-name))
687 (goto-char m)
688 (set-marker m nil)
689 (gnus-group-position-point)))))
691 (defun gnus-topic-goto-missing-group (group)
692 "Place point where GROUP is supposed to be inserted."
693 (let* ((topic (gnus-group-topic group))
694 (groups (cdr (assoc topic gnus-topic-alist)))
695 (g (cdr (member group groups)))
696 (unfound t))
697 ;; Try to jump to a visible group.
698 (while (and g
699 (not (gnus-group-goto-group (car g) t)))
700 (pop g))
701 ;; It wasn't visible, so we try to see where to insert it.
702 (when (not g)
703 (setq g (cdr (member group (reverse groups))))
704 (while (and g unfound)
705 (when (gnus-group-goto-group (pop g) t)
706 (forward-line 1)
707 (setq unfound nil)))
708 (when (and unfound
709 topic
710 (not (gnus-topic-goto-missing-topic topic)))
711 (gnus-topic-display-missing-topic topic)))))
713 (defun gnus-topic-display-missing-topic (topic)
714 "Insert topic lines recursively for missing topics."
715 (let ((parent (gnus-topic-find-topology
716 (gnus-topic-parent-topic topic))))
717 (when (and parent
718 (not (gnus-topic-goto-missing-topic (caadr parent))))
719 (gnus-topic-display-missing-topic (caadr parent))))
720 (gnus-topic-goto-missing-topic topic)
721 ;; Skip past all groups in the topic we're in.
722 (while (gnus-group-group-name)
723 (forward-line 1))
724 (let* ((top (gnus-topic-find-topology topic))
725 (children (cddr top))
726 (type (cadr top))
727 (unread 0)
728 (entries (gnus-topic-find-groups
729 (car type) (car gnus-group-list-mode)
730 (cdr gnus-group-list-mode)))
731 entry)
732 (while children
733 (incf unread (gnus-topic-unread (caar (pop children)))))
734 (while (setq entry (pop entries))
735 (when (numberp (car entry))
736 (incf unread (car entry))))
737 (gnus-topic-insert-topic-line
738 topic t t (car (gnus-topic-find-topology topic)) nil unread)))
740 (defun gnus-topic-goto-missing-topic (topic)
741 (if (gnus-topic-goto-topic topic)
742 (forward-line 1)
743 ;; Topic not displayed.
744 (let* ((top (gnus-topic-find-topology
745 (gnus-topic-parent-topic topic)))
746 (tp (reverse (cddr top))))
747 (if (not top)
748 (gnus-topic-insert-topic-line
749 topic t t (car (gnus-topic-find-topology topic)) nil 0)
750 (while (not (equal (caaar tp) topic))
751 (setq tp (cdr tp)))
752 (pop tp)
753 (while (and tp
754 (not (gnus-topic-goto-topic (caaar tp))))
755 (pop tp))
756 (if tp
757 (gnus-topic-forward-topic 1)
758 (gnus-topic-goto-missing-topic (caadr top)))))
759 nil))
761 (defun gnus-topic-update-topic-line (topic-name &optional reads)
762 (let* ((top (gnus-topic-find-topology topic-name))
763 (type (cadr top))
764 (children (cddr top))
765 (entries (gnus-topic-find-groups
766 (car type) (car gnus-group-list-mode)
767 (cdr gnus-group-list-mode)))
768 (parent (gnus-topic-parent-topic topic-name))
769 (all-entries entries)
770 (unread 0)
771 old-unread entry new-unread)
772 (when (gnus-topic-goto-topic (car type))
773 ;; Tally all the groups that belong in this topic.
774 (if reads
775 (setq unread (- (gnus-group-topic-unread) reads))
776 (while children
777 (incf unread (gnus-topic-unread (caar (pop children)))))
778 (while (setq entry (pop entries))
779 (when (numberp (car entry))
780 (incf unread (car entry)))))
781 (setq old-unread (gnus-group-topic-unread))
782 ;; Insert the topic line.
783 (gnus-topic-insert-topic-line
784 (car type) (gnus-topic-visible-p)
785 (not (eq (nth 2 type) 'hidden))
786 (gnus-group-topic-level) all-entries unread)
787 (gnus-delete-line)
788 (forward-line -1)
789 (setq new-unread (gnus-group-topic-unread)))
790 (when parent
791 (forward-line -1)
792 (gnus-topic-update-topic-line
793 parent
794 (- (or old-unread 0) (or new-unread 0))))
795 unread))
797 (defun gnus-topic-group-indentation ()
798 (make-string
799 (* gnus-topic-indent-level
800 (or (save-excursion
801 (forward-line -1)
802 (gnus-topic-goto-topic (gnus-current-topic))
803 (gnus-group-topic-level))
805 ? ))
807 ;;; Initialization
809 (gnus-add-shutdown 'gnus-topic-close 'gnus)
811 (defun gnus-topic-close ()
812 (setq gnus-topic-active-topology nil
813 gnus-topic-active-alist nil
814 gnus-topic-killed-topics nil
815 gnus-topology-checked-p nil))
817 (defun gnus-topic-check-topology ()
818 ;; The first time we set the topology to whatever we have
819 ;; gotten here, which can be rather random.
820 (unless gnus-topic-alist
821 (gnus-topic-init-alist))
823 (setq gnus-topology-checked-p t)
824 ;; Go through the topic alist and make sure that all topics
825 ;; are in the topic topology.
826 (let ((topics (gnus-topic-list))
827 (alist gnus-topic-alist)
828 changed)
829 (while alist
830 (unless (member (caar alist) topics)
831 (nconc gnus-topic-topology
832 (list (list (list (caar alist) 'visible))))
833 (setq changed t))
834 (setq alist (cdr alist)))
835 (when changed
836 (gnus-topic-enter-dribble))
837 ;; Conversely, go through the topology and make sure that all
838 ;; topologies have alists.
839 (while topics
840 (unless (assoc (car topics) gnus-topic-alist)
841 (push (list (car topics)) gnus-topic-alist))
842 (pop topics)))
843 ;; Go through all living groups and make sure that
844 ;; they belong to some topic.
845 (let* ((tgroups (apply 'append (mapcar 'cdr gnus-topic-alist)))
846 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
847 (newsrc (cdr gnus-newsrc-alist))
848 group)
849 (while newsrc
850 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
851 (setcdr entry (list group))
852 (setq entry (cdr entry)))))
853 ;; Go through all topics and make sure they contain only living groups.
854 (let ((alist gnus-topic-alist)
855 topic)
856 (while (setq topic (pop alist))
857 (while (cdr topic)
858 (if (and (cadr topic)
859 (gnus-group-entry (cadr topic)))
860 (setq topic (cdr topic))
861 (setcdr topic (cddr topic)))))))
863 (defun gnus-topic-init-alist ()
864 "Initialize the topic structures."
865 (setq gnus-topic-topology
866 (cons (list "Gnus" 'visible)
867 (mapcar (lambda (topic)
868 (list (list (car topic) 'visible)))
869 '(("misc")))))
870 (setq gnus-topic-alist
871 (list (cons "misc"
872 (mapcar (lambda (info) (gnus-info-group info))
873 (cdr gnus-newsrc-alist)))
874 (list "Gnus")))
875 (gnus-topic-enter-dribble))
877 ;;; Maintenance
879 (defun gnus-topic-clean-alist ()
880 "Remove bogus groups from the topic alist."
881 (let ((topic-alist gnus-topic-alist)
882 result topic)
883 (unless gnus-killed-hashtb
884 (gnus-make-hashtable-from-killed))
885 (while (setq topic (pop topic-alist))
886 (let ((topic-name (pop topic))
887 group filtered-topic)
888 (while (setq group (pop topic))
889 (when (and (or (gnus-active group)
890 (gnus-info-method (gnus-get-info group)))
891 (not (gnus-gethash group gnus-killed-hashtb)))
892 (push group filtered-topic)))
893 (push (cons topic-name (nreverse filtered-topic)) result)))
894 (setq gnus-topic-alist (nreverse result))))
896 (defun gnus-topic-change-level (group level oldlevel &optional previous)
897 "Run when changing levels to enter/remove groups from topics."
898 (with-current-buffer gnus-group-buffer
899 (let ((inhibit-read-only t))
900 (unless gnus-topic-inhibit-change-level
901 (gnus-group-goto-group (or (car (nth 2 previous)) group))
902 (when (and gnus-topic-mode
903 gnus-topic-alist
904 (not gnus-topic-inhibit-change-level))
905 ;; Remove the group from the topics.
906 (if (and (< oldlevel gnus-level-zombie)
907 (>= level gnus-level-zombie))
908 (let ((alist gnus-topic-alist))
909 (while (gnus-group-goto-group group)
910 (gnus-delete-line))
911 (while alist
912 (when (member group (car alist))
913 (setcdr (car alist) (delete group (cdar alist))))
914 (pop alist)))
915 ;; If the group is subscribed we enter it into the topics.
916 (when (and (< level gnus-level-zombie)
917 (>= oldlevel gnus-level-zombie))
918 (let* ((prev (gnus-group-group-name))
919 (gnus-topic-inhibit-change-level t)
920 (gnus-group-indentation
921 (make-string
922 (* gnus-topic-indent-level
923 (or (save-excursion
924 (gnus-topic-goto-topic (gnus-current-topic))
925 (gnus-group-topic-level))
927 ? ))
928 (yanked (list group))
929 alist talist end)
930 ;; Then we enter the yanked groups into the topics
931 ;; they belong to.
932 (when (setq alist (assoc (save-excursion
933 (forward-line -1)
935 (gnus-current-topic)
936 (caar gnus-topic-topology)))
937 gnus-topic-alist))
938 (setq talist alist)
939 (when (stringp yanked)
940 (setq yanked (list yanked)))
941 (if (not prev)
942 (nconc alist yanked)
943 (if (not (cdr alist))
944 (setcdr alist (nconc yanked (cdr alist)))
945 (while (and (not end) (cdr alist))
946 (when (equal (cadr alist) prev)
947 (setcdr alist (nconc yanked (cdr alist)))
948 (setq end t))
949 (setq alist (cdr alist)))
950 (unless end
951 (nconc talist yanked))))))
952 (gnus-topic-update-topic))))))))
954 (defun gnus-topic-goto-next-group (group props)
955 "Go to group or the next group after group."
956 (if (not group)
957 (if (not (memq 'gnus-topic props))
958 (goto-char (point-max))
959 (let ((topic (symbol-name (cadr (memq 'gnus-topic props)))))
960 (or (gnus-topic-goto-topic topic)
961 (gnus-topic-goto-topic (gnus-topic-next-topic topic)))))
962 (if (gnus-group-goto-group group)
964 ;; The group is no longer visible.
965 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
966 (topic-visible (save-excursion (gnus-topic-goto-topic (car list))))
967 (after (and topic-visible (cdr (member group (cdr list))))))
968 ;; First try to put point on a group after the current one.
969 (while (and after
970 (not (gnus-group-goto-group (car after))))
971 (setq after (cdr after)))
972 ;; Then try to put point on a group before point.
973 (unless after
974 (setq after (cdr (member group (reverse (cdr list)))))
975 (while (and after
976 (not (gnus-group-goto-group (car after))))
977 (setq after (cdr after))))
978 ;; Finally, just put point on the topic.
979 (if (not (car list))
980 (goto-char (point-min))
981 (unless after
982 (if topic-visible
983 (gnus-goto-char topic-visible)
984 (gnus-topic-goto-topic (gnus-topic-next-topic (car list))))
985 (setq after nil)))
986 t))))
988 ;;; Topic-active functions
990 (defun gnus-topic-grok-active (&optional force)
991 "Parse all active groups and create topic structures for them."
992 ;; First we make sure that we have really read the active file.
993 (when (or force
994 (not gnus-topic-active-alist))
995 (let (groups)
996 ;; Get a list of all groups available.
997 (mapatoms (lambda (g) (when (symbol-value g)
998 (push (symbol-name g) groups)))
999 gnus-active-hashtb)
1000 (setq groups (sort groups 'string<))
1001 ;; Init the variables.
1002 (setq gnus-topic-active-topology (list (list "" 'visible)))
1003 (setq gnus-topic-active-alist nil)
1004 ;; Descend the top-level hierarchy.
1005 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
1006 ;; Set the top-level topic names to something nice.
1007 (setcar (car gnus-topic-active-topology) "Gnus active")
1008 (setcar (car gnus-topic-active-alist) "Gnus active"))))
1010 (defun gnus-topic-grok-active-1 (topology groups)
1011 (let* ((name (caar topology))
1012 (prefix (concat "^" (regexp-quote name)))
1013 tgroups ntopology group)
1014 (while (and groups
1015 (string-match prefix (setq group (car groups))))
1016 (if (not (string-match "\\." group (match-end 0)))
1017 ;; There are no further hierarchies here, so we just
1018 ;; enter this group into the list belonging to this
1019 ;; topic.
1020 (push (pop groups) tgroups)
1021 ;; New sub-hierarchy, so we add it to the topology.
1022 (nconc topology (list (setq ntopology
1023 (list (list (substring
1024 group 0 (match-end 0))
1025 'invisible)))))
1026 ;; Descend the hierarchy.
1027 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
1028 ;; We remove the trailing "." from the topic name.
1029 (setq name
1030 (if (string-match "\\.$" name)
1031 (substring name 0 (match-beginning 0))
1032 name))
1033 ;; Add this topic and its groups to the topic alist.
1034 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
1035 (setcar (car topology) name)
1036 ;; We return the rest of the groups that didn't belong
1037 ;; to this topic.
1038 groups))
1040 ;;; Topic mode, commands and keymap.
1042 (defvar gnus-topic-mode-map nil)
1043 (defvar gnus-group-topic-map nil)
1045 (unless gnus-topic-mode-map
1046 (setq gnus-topic-mode-map (make-sparse-keymap))
1048 ;; Override certain group mode keys.
1049 (gnus-define-keys gnus-topic-mode-map
1050 "=" gnus-topic-select-group
1051 "\r" gnus-topic-select-group
1052 " " gnus-topic-read-group
1053 "\C-c\C-x" gnus-topic-expire-articles
1054 "c" gnus-topic-catchup-articles
1055 "\C-k" gnus-topic-kill-group
1056 "\C-y" gnus-topic-yank-group
1057 "\M-g" gnus-topic-get-new-news-this-topic
1058 "AT" gnus-topic-list-active
1059 "Gp" gnus-topic-edit-parameters
1060 "#" gnus-topic-mark-topic
1061 "\M-#" gnus-topic-unmark-topic
1062 [tab] gnus-topic-indent
1063 [(meta tab)] gnus-topic-unindent
1064 "\C-i" gnus-topic-indent
1065 "\M-\C-i" gnus-topic-unindent
1066 [mouse-2] gnus-mouse-pick-topic)
1068 ;; Define a new submap.
1069 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
1070 "#" gnus-topic-mark-topic
1071 "\M-#" gnus-topic-unmark-topic
1072 "n" gnus-topic-create-topic
1073 "m" gnus-topic-move-group
1074 "D" gnus-topic-remove-group
1075 "c" gnus-topic-copy-group
1076 "h" gnus-topic-hide-topic
1077 "s" gnus-topic-show-topic
1078 "j" gnus-topic-jump-to-topic
1079 "M" gnus-topic-move-matching
1080 "C" gnus-topic-copy-matching
1081 "\M-p" gnus-topic-goto-previous-topic
1082 "\M-n" gnus-topic-goto-next-topic
1083 "\C-i" gnus-topic-indent
1084 [tab] gnus-topic-indent
1085 "r" gnus-topic-rename
1086 "\177" gnus-topic-delete
1087 [delete] gnus-topic-delete
1088 "H" gnus-topic-toggle-display-empty-topics)
1090 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
1091 "s" gnus-topic-sort-groups
1092 "a" gnus-topic-sort-groups-by-alphabet
1093 "u" gnus-topic-sort-groups-by-unread
1094 "l" gnus-topic-sort-groups-by-level
1095 "e" gnus-topic-sort-groups-by-server
1096 "v" gnus-topic-sort-groups-by-score
1097 "r" gnus-topic-sort-groups-by-rank
1098 "m" gnus-topic-sort-groups-by-method))
1100 (defun gnus-topic-make-menu-bar ()
1101 (unless (boundp 'gnus-topic-menu)
1102 (easy-menu-define
1103 gnus-topic-menu gnus-topic-mode-map ""
1104 '("Topics"
1105 ["Toggle topics" gnus-topic-mode t]
1106 ("Groups"
1107 ["Copy..." gnus-topic-copy-group t]
1108 ["Move..." gnus-topic-move-group t]
1109 ["Remove" gnus-topic-remove-group t]
1110 ["Copy matching..." gnus-topic-copy-matching t]
1111 ["Move matching..." gnus-topic-move-matching t])
1112 ("Topics"
1113 ["Goto..." gnus-topic-jump-to-topic t]
1114 ["Show" gnus-topic-show-topic t]
1115 ["Hide" gnus-topic-hide-topic t]
1116 ["Delete" gnus-topic-delete t]
1117 ["Rename..." gnus-topic-rename t]
1118 ["Create..." gnus-topic-create-topic t]
1119 ["Mark" gnus-topic-mark-topic t]
1120 ["Indent" gnus-topic-indent t]
1121 ["Sort" gnus-topic-sort-topics t]
1122 ["Previous topic" gnus-topic-goto-previous-topic t]
1123 ["Next topic" gnus-topic-goto-next-topic t]
1124 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
1125 ["Edit parameters" gnus-topic-edit-parameters t])
1126 ["List active" gnus-topic-list-active t]))))
1128 (define-minor-mode gnus-topic-mode
1129 "Minor mode for topicsifying Gnus group buffers."
1130 :lighter " Topic" :keymap gnus-topic-mode-map
1131 (if (not (derived-mode-p 'gnus-group-mode))
1132 (setq gnus-topic-mode nil)
1133 ;; Infest Gnus with topics.
1134 (if (not gnus-topic-mode)
1135 (setq gnus-goto-missing-group-function nil)
1136 (when (gnus-visual-p 'topic-menu 'menu)
1137 (gnus-topic-make-menu-bar))
1138 (gnus-set-format 'topic t)
1139 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
1140 (set (make-local-variable 'gnus-group-prepare-function)
1141 'gnus-group-prepare-topics)
1142 (set (make-local-variable 'gnus-group-get-parameter-function)
1143 'gnus-group-topic-parameters)
1144 (set (make-local-variable 'gnus-group-goto-next-group-function)
1145 'gnus-topic-goto-next-group)
1146 (set (make-local-variable 'gnus-group-indentation-function)
1147 'gnus-topic-group-indentation)
1148 (set (make-local-variable 'gnus-group-update-group-function)
1149 'gnus-topic-update-topics-containing-group)
1150 (set (make-local-variable 'gnus-group-sort-alist-function)
1151 'gnus-group-sort-topic)
1152 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1153 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1154 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
1155 nil 'local)
1156 (setq gnus-topology-checked-p nil)
1157 ;; We check the topology.
1158 (when gnus-newsrc-alist
1159 (gnus-topic-check-topology)))
1160 ;; Remove topic infestation.
1161 (unless gnus-topic-mode
1162 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1163 (setq gnus-group-change-level-function nil)
1164 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1165 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1166 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1167 (when (called-interactively-p 'any)
1168 (gnus-group-list-groups))))
1170 (defun gnus-topic-select-group (&optional all)
1171 "Select this newsgroup.
1172 No article is selected automatically.
1173 If the group is opened, just switch the summary buffer.
1174 If ALL is non-nil, already read articles become readable.
1176 If ALL is a positive number, fetch this number of the latest
1177 articles in the group. If ALL is a negative number, fetch this
1178 number of the earliest articles in the group.
1180 If performed over a topic line, toggle folding the topic."
1181 (interactive "P")
1182 (when (and (eobp) (not (gnus-group-group-name)))
1183 (forward-line -1))
1184 (if (gnus-group-topic-p)
1185 (let ((gnus-group-list-mode
1186 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1187 (gnus-topic-fold all)
1188 (gnus-dribble-touch))
1189 (gnus-group-select-group all)))
1191 (defun gnus-mouse-pick-topic (e)
1192 "Select the group or topic under the mouse pointer."
1193 (interactive "e")
1194 (mouse-set-point e)
1195 (gnus-topic-read-group nil))
1197 (defun gnus-topic-expire-articles (topic)
1198 "Expire articles in this topic or group."
1199 (interactive (list (gnus-group-topic-name)))
1200 (if (not topic)
1201 (call-interactively 'gnus-group-expire-articles)
1202 (save-excursion
1203 (gnus-message 5 "Expiring groups in %s..." topic)
1204 (let ((gnus-group-marked
1205 (mapcar (lambda (entry) (car (nth 2 entry)))
1206 (gnus-topic-find-groups topic gnus-level-killed t
1207 nil t))))
1208 (gnus-group-expire-articles nil))
1209 (gnus-message 5 "Expiring groups in %s...done" topic))))
1211 (defun gnus-topic-catchup-articles (topic)
1212 "Catchup this topic or group.
1213 Also see `gnus-group-catchup'."
1214 (interactive (list (gnus-group-topic-name)))
1215 (if (not topic)
1216 (call-interactively 'gnus-group-catchup-current)
1217 (save-excursion
1218 (let* ((groups
1219 (mapcar (lambda (entry) (car (nth 2 entry)))
1220 (gnus-topic-find-groups topic gnus-level-killed t
1221 nil t)))
1222 (inhibit-read-only t)
1223 (gnus-group-marked groups))
1224 (gnus-group-catchup-current)
1225 (mapcar 'gnus-topic-update-topics-containing-group groups)))))
1227 (defun gnus-topic-read-group (&optional all no-article group)
1228 "Read news in this newsgroup.
1229 If the prefix argument ALL is non-nil, already read articles become
1230 readable.
1232 If ALL is a positive number, fetch this number of the latest
1233 articles in the group. If ALL is a negative number, fetch this
1234 number of the earliest articles in the group.
1236 If the optional argument NO-ARTICLE is non-nil, no article will
1237 be auto-selected upon group entry. If GROUP is non-nil, fetch
1238 that group.
1240 If performed over a topic line, toggle folding the topic."
1241 (interactive "P")
1242 (when (and (eobp) (not (gnus-group-group-name)))
1243 (forward-line -1))
1244 (if (gnus-group-topic-p)
1245 (let ((gnus-group-list-mode
1246 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1247 (gnus-topic-fold all))
1248 (gnus-group-read-group all no-article group)))
1250 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1251 "Create a new TOPIC under PARENT.
1252 When used interactively, PARENT will be the topic under point."
1253 (interactive
1254 (list
1255 (read-string "New topic: ")
1256 (gnus-current-topic)))
1257 ;; Check whether this topic already exists.
1258 (when (gnus-topic-find-topology topic)
1259 (error "Topic already exists"))
1260 (unless parent
1261 (setq parent (caar gnus-topic-topology)))
1262 (let ((top (cdr (gnus-topic-find-topology parent)))
1263 (full-topic (or full-topic (list (list topic 'visible nil nil)))))
1264 (unless top
1265 (error "No such parent topic: %s" parent))
1266 (if previous
1267 (progn
1268 (while (and (cdr top)
1269 (not (equal (caaadr top) previous)))
1270 (setq top (cdr top)))
1271 (setcdr top (cons full-topic (cdr top))))
1272 (nconc top (list full-topic)))
1273 (unless (assoc topic gnus-topic-alist)
1274 (push (list topic) gnus-topic-alist)))
1275 (gnus-topic-enter-dribble)
1276 (gnus-group-list-groups)
1277 (gnus-topic-goto-topic topic))
1279 ;; FIXME:
1280 ;; 1. When the marked groups are overlapped with the process
1281 ;; region, the behavior of move or remove is not right.
1282 ;; 2. Can't process on several marked groups with a same name,
1283 ;; because gnus-group-marked only keeps one copy.
1285 (defvar gnus-topic-history nil)
1287 (defun gnus-topic-move-group (n topic &optional copyp)
1288 "Move the next N groups to TOPIC.
1289 If COPYP, copy the groups instead."
1290 (interactive
1291 (list current-prefix-arg
1292 (gnus-completing-read "Move to topic" (mapcar 'car gnus-topic-alist) t
1293 nil 'gnus-topic-history)))
1294 (let ((use-marked (and (not n) (not (and transient-mark-mode mark-active))
1295 gnus-group-marked t))
1296 (groups (gnus-group-process-prefix n))
1297 (topicl (assoc topic gnus-topic-alist))
1298 (start-topic (gnus-group-topic-name))
1299 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1300 entry)
1301 (if (and (not groups) (not copyp) start-topic)
1302 (gnus-topic-move start-topic topic)
1303 (dolist (g groups)
1304 (gnus-group-remove-mark g use-marked)
1305 (when (and
1306 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1307 (not copyp))
1308 (setcdr entry (gnus-delete-first g (cdr entry))))
1309 (nconc topicl (list g)))
1310 (gnus-topic-enter-dribble)
1311 (if start-group
1312 (gnus-group-goto-group start-group)
1313 (gnus-topic-goto-topic start-topic))
1314 (gnus-group-list-groups))))
1316 (defun gnus-topic-remove-group (&optional n)
1317 "Remove the current group from the topic."
1318 (interactive "P")
1319 (let ((use-marked (and (not n) (not (and transient-mark-mode mark-active))
1320 gnus-group-marked t))
1321 (groups (gnus-group-process-prefix n)))
1322 (mapc
1323 (lambda (group)
1324 (gnus-group-remove-mark group use-marked)
1325 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1326 (inhibit-read-only t))
1327 (when (and topicl group)
1328 (gnus-delete-line)
1329 (gnus-delete-first group topicl))
1330 (gnus-topic-update-topic)))
1331 groups)
1332 (gnus-topic-enter-dribble)
1333 (gnus-group-position-point)))
1335 (defun gnus-topic-copy-group (n topic)
1336 "Copy the current group to a topic."
1337 (interactive
1338 (list current-prefix-arg
1339 (gnus-completing-read
1340 "Copy to topic" (mapcar 'car gnus-topic-alist) t)))
1341 (gnus-topic-move-group n topic t))
1343 (defun gnus-topic-kill-group (&optional n discard)
1344 "Kill the next N groups."
1345 (interactive "P")
1346 (if (gnus-group-topic-p)
1347 (let ((topic (gnus-group-topic-name)))
1348 (push (cons
1349 (gnus-topic-find-topology topic)
1350 (assoc topic gnus-topic-alist))
1351 gnus-topic-killed-topics)
1352 (gnus-topic-remove-topic nil t)
1353 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1354 (gnus-topic-enter-dribble))
1355 (gnus-group-kill-group n discard)
1356 (if (not (gnus-group-topic-p))
1357 (gnus-topic-update-topic)
1358 ;; Move up one line so that we update the right topic.
1359 (forward-line -1)
1360 (gnus-topic-update-topic)
1361 (forward-line 1))))
1363 (defun gnus-topic-yank-group (&optional arg)
1364 "Yank the last topic."
1365 (interactive "p")
1366 (if gnus-topic-killed-topics
1367 (let* ((previous
1368 (or (gnus-group-topic-name)
1369 (gnus-topic-next-topic (gnus-current-topic))))
1370 (data (pop gnus-topic-killed-topics))
1371 (alist (cdr data))
1372 (item (cdar data)))
1373 (push alist gnus-topic-alist)
1374 (gnus-topic-create-topic
1375 (caar item) (gnus-topic-parent-topic previous) previous
1376 item)
1377 (gnus-topic-enter-dribble)
1378 (gnus-topic-goto-topic (caar item)))
1379 (let* ((prev (gnus-group-group-name))
1380 (gnus-topic-inhibit-change-level t)
1381 (gnus-group-indentation
1382 (make-string
1383 (* gnus-topic-indent-level
1384 (or (save-excursion
1385 (gnus-topic-goto-topic (gnus-current-topic))
1386 (gnus-group-topic-level))
1388 ? ))
1389 yanked alist)
1390 ;; We first yank the groups the normal way...
1391 (setq yanked (gnus-group-yank-group arg))
1392 ;; Then we enter the yanked groups into the topics they belong
1393 ;; to.
1394 (setq alist (assoc (save-excursion
1395 (forward-line -1)
1396 (gnus-current-topic))
1397 gnus-topic-alist))
1398 (when (stringp yanked)
1399 (setq yanked (list yanked)))
1400 (if (not prev)
1401 (nconc alist yanked)
1402 (if (not (cdr alist))
1403 (setcdr alist (nconc yanked (cdr alist)))
1404 (while (cdr alist)
1405 (when (equal (cadr alist) prev)
1406 (setcdr alist (nconc yanked (cdr alist)))
1407 (setq alist nil))
1408 (setq alist (cdr alist))))))
1409 (gnus-topic-update-topic)))
1411 (defun gnus-topic-hide-topic (&optional permanent)
1412 "Hide the current topic.
1413 If PERMANENT, make it stay hidden in subsequent sessions as well."
1414 (interactive "P")
1415 (when (gnus-current-topic)
1416 (gnus-topic-goto-topic (gnus-current-topic))
1417 (if permanent
1418 (setcar (cddr
1419 (cadr
1420 (gnus-topic-find-topology (gnus-current-topic))))
1421 'hidden))
1422 (gnus-topic-remove-topic nil nil)))
1424 (defun gnus-topic-show-topic (&optional permanent)
1425 "Show the hidden topic.
1426 If PERMANENT, make it stay shown in subsequent sessions as well."
1427 (interactive "P")
1428 (when (gnus-group-topic-p)
1429 (if (not permanent)
1430 (gnus-topic-remove-topic t nil)
1431 (let ((topic
1432 (gnus-topic-find-topology
1433 (gnus-completing-read "Show topic"
1434 (mapcar 'car gnus-topic-alist) t))))
1435 (setcar (cddr (cadr topic)) nil)
1436 (setcar (cdr (cadr topic)) 'visible)
1437 (gnus-group-list-groups)))))
1439 (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
1440 "Mark all groups in the TOPIC with the process mark.
1441 If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
1442 (interactive (list (gnus-group-topic-name)
1444 (and current-prefix-arg t)))
1445 (if (not topic)
1446 (call-interactively 'gnus-group-mark-group)
1447 (save-excursion
1448 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
1449 (not non-recursive))))
1450 (while groups
1451 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1452 (gnus-info-group (nth 2 (pop groups)))))))))
1454 (defun gnus-topic-unmark-topic (topic &optional _dummy non-recursive)
1455 "Remove the process mark from all groups in the TOPIC.
1456 If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
1457 (interactive (list (gnus-group-topic-name)
1459 (and current-prefix-arg t)))
1460 (if (not topic)
1461 (call-interactively 'gnus-group-unmark-group)
1462 (gnus-topic-mark-topic topic t non-recursive)))
1464 (defun gnus-topic-get-new-news-this-topic (&optional n)
1465 "Check for new news in the current topic."
1466 (interactive "P")
1467 (if (not (gnus-group-topic-p))
1468 (gnus-group-get-new-news-this-group n)
1469 (let* ((topic (gnus-group-topic-name))
1470 (data (cadr (gnus-topic-find-topology topic))))
1471 (save-excursion
1472 (gnus-topic-mark-topic topic nil (and n t))
1473 (gnus-group-get-new-news-this-group))
1474 (gnus-topic-remove-topic (eq 'visible (cadr data))))))
1476 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1477 "Move all groups that match REGEXP to some topic."
1478 (interactive
1479 (let (topic)
1480 (nreverse
1481 (list
1482 (setq topic (gnus-completing-read "Move to topic"
1483 (mapcar 'car gnus-topic-alist) t))
1484 (read-string (format "Move to %s (regexp): " topic))))))
1485 (gnus-group-mark-regexp regexp)
1486 (gnus-topic-move-group nil topic copyp))
1488 (defun gnus-topic-copy-matching (regexp topic &optional _copyp)
1489 "Copy all groups that match REGEXP to some topic."
1490 (interactive
1491 (let ((topic (gnus-completing-read "Copy to topic"
1492 (mapcar #'car gnus-topic-alist) t)))
1493 (nreverse
1494 (list topic
1495 (read-string (format "Copy to %s (regexp): " topic))))))
1496 (gnus-topic-move-matching regexp topic t))
1498 (defun gnus-topic-delete (topic)
1499 "Delete a topic."
1500 (interactive (list (gnus-group-topic-name)))
1501 (unless topic
1502 (error "No topic to be deleted"))
1503 (let ((entry (assoc topic gnus-topic-alist))
1504 (inhibit-read-only t))
1505 (when (cdr entry)
1506 (error "Topic not empty"))
1507 ;; Delete if visible.
1508 (when (gnus-topic-goto-topic topic)
1509 (gnus-delete-line))
1510 ;; Remove from alist.
1511 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1512 ;; Remove from topology.
1513 (gnus-topic-find-topology topic nil nil 'delete)
1514 (gnus-dribble-touch)))
1516 (defun gnus-topic-rename (old-name new-name)
1517 "Rename a topic."
1518 (interactive
1519 (let ((topic (gnus-current-topic)))
1520 (list topic
1521 (read-string (format "Rename %s to: " topic) topic))))
1522 ;; Check whether the new name exists.
1523 (when (gnus-topic-find-topology new-name)
1524 (error "Topic `%s' already exists" new-name))
1525 ;; "nil" is an invalid name, for reasons I'd rather not go
1526 ;; into here. Trust me.
1527 (when (equal new-name "nil")
1528 (error "Invalid name: %s" nil))
1529 ;; Do the renaming.
1530 (let ((top (gnus-topic-find-topology old-name))
1531 (entry (assoc old-name gnus-topic-alist)))
1532 (when top
1533 (setcar (cadr top) new-name))
1534 (when entry
1535 (setcar entry new-name))
1536 (forward-line -1)
1537 (gnus-dribble-touch)
1538 (gnus-group-list-groups)
1539 (forward-line 1)))
1541 (defun gnus-topic-indent (&optional unindent)
1542 "Indent a topic -- make it a sub-topic of the previous topic.
1543 If UNINDENT, remove an indentation."
1544 (interactive "P")
1545 (if unindent
1546 (gnus-topic-unindent)
1547 (let* ((topic (gnus-current-topic))
1548 (parent (gnus-topic-previous-topic topic))
1549 (inhibit-read-only t))
1550 (unless parent
1551 (error "Nothing to indent %s into" topic))
1552 (when topic
1553 (gnus-topic-goto-topic topic)
1554 (gnus-topic-kill-group)
1555 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1556 (gnus-topic-create-topic
1557 topic parent nil (cdar (car gnus-topic-killed-topics)))
1558 (pop gnus-topic-killed-topics)
1559 (or (gnus-topic-goto-topic topic)
1560 (gnus-topic-goto-topic parent))))))
1562 (defun gnus-topic-unindent ()
1563 "Unindent a topic."
1564 (interactive)
1565 (let* ((topic (gnus-current-topic))
1566 (parent (gnus-topic-parent-topic topic))
1567 (grandparent (gnus-topic-parent-topic parent)))
1568 (unless grandparent
1569 (error "Can't unindent %s further" topic))
1570 (when topic
1571 (gnus-topic-goto-topic topic)
1572 (gnus-topic-kill-group)
1573 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1574 (gnus-topic-create-topic
1575 topic grandparent (gnus-topic-next-topic parent)
1576 (cdar (car gnus-topic-killed-topics)))
1577 (pop gnus-topic-killed-topics)
1578 (gnus-topic-goto-topic topic))))
1580 (defun gnus-topic-list-active (&optional force)
1581 "List all groups that Gnus knows about in a topicsified fashion.
1582 If FORCE, always re-read the active file."
1583 (interactive "P")
1584 (when force
1585 (gnus-get-killed-groups))
1586 (gnus-topic-grok-active force)
1587 (let ((gnus-topic-topology gnus-topic-active-topology)
1588 (gnus-topic-alist gnus-topic-active-alist)
1589 gnus-killed-list gnus-zombie-list)
1590 (gnus-group-list-groups gnus-level-killed nil 1)))
1592 (defun gnus-topic-toggle-display-empty-topics ()
1593 "Show/hide topics that have no unread articles."
1594 (interactive)
1595 (setq gnus-topic-display-empty-topics
1596 (not gnus-topic-display-empty-topics))
1597 (gnus-group-list-groups)
1598 (message "%s empty topics"
1599 (if gnus-topic-display-empty-topics
1600 "Showing" "Hiding")))
1602 ;;; Topic sorting functions
1604 (defun gnus-topic-edit-parameters (group)
1605 "Edit the group parameters of GROUP.
1606 If performed on a topic, edit the topic parameters instead."
1607 (interactive (list (gnus-group-group-name)))
1608 (if group
1609 (gnus-group-edit-group-parameters group)
1610 (if (not (gnus-group-topic-p))
1611 (error "Nothing to edit on the current line")
1612 (let ((topic (gnus-group-topic-name)))
1613 (gnus-edit-form
1614 (gnus-topic-parameters topic)
1615 (format-message "Editing the topic parameters for `%s'."
1616 (or group topic))
1617 `(lambda (form)
1618 (gnus-topic-set-parameters ,topic form)))))))
1620 (defun gnus-group-sort-topic (func reverse)
1621 "Sort groups in the topics according to FUNC and REVERSE."
1622 (let ((alist gnus-topic-alist))
1623 (while alist
1624 ;; !!!Sometimes nil elements sneak into the alist,
1625 ;; for some reason or other.
1626 (setcar alist (delq nil (car alist)))
1627 (setcar alist (delete "dummy.group" (car alist)))
1628 (gnus-topic-sort-topic (pop alist) func reverse))))
1630 (defun gnus-topic-sort-topic (topic func reverse)
1631 ;; Each topic only lists the name of the group, while
1632 ;; the sort predicates expect group infos as inputs.
1633 ;; So we first transform the group names into infos,
1634 ;; then sort, and then transform back into group names.
1635 (setcdr
1636 topic
1637 (mapcar
1638 (lambda (info) (gnus-info-group info))
1639 (sort
1640 (mapcar
1641 (lambda (group) (gnus-get-info group))
1642 (cdr topic))
1643 func)))
1644 ;; Do the reversal, if necessary.
1645 (when reverse
1646 (setcdr topic (nreverse (cdr topic)))))
1648 (defun gnus-topic-sort-groups (func &optional reverse)
1649 "Sort the current topic according to FUNC.
1650 If REVERSE, reverse the sorting order."
1651 (interactive (list gnus-group-sort-function current-prefix-arg))
1652 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1653 (gnus-topic-sort-topic
1654 topic (gnus-make-sort-function func) reverse)
1655 (gnus-group-list-groups)))
1657 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1658 "Sort the current topic alphabetically by group name.
1659 If REVERSE, sort in reverse order."
1660 (interactive "P")
1661 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1663 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1664 "Sort the current topic by number of unread articles.
1665 If REVERSE, sort in reverse order."
1666 (interactive "P")
1667 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1669 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1670 "Sort the current topic by group level.
1671 If REVERSE, sort in reverse order."
1672 (interactive "P")
1673 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1675 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1676 "Sort the current topic by group score.
1677 If REVERSE, sort in reverse order."
1678 (interactive "P")
1679 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1681 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1682 "Sort the current topic by group rank.
1683 If REVERSE, sort in reverse order."
1684 (interactive "P")
1685 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1687 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1688 "Sort the current topic alphabetically by backend name.
1689 If REVERSE, sort in reverse order."
1690 (interactive "P")
1691 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1693 (defun gnus-topic-sort-groups-by-server (&optional reverse)
1694 "Sort the current topic alphabetically by server name.
1695 If REVERSE, sort in reverse order."
1696 (interactive "P")
1697 (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
1699 (defun gnus-topic-sort-topics-1 (top reverse)
1700 (if (cdr top)
1701 (let ((subtop
1702 (mapcar (gnus-byte-compile
1703 `(lambda (top)
1704 (gnus-topic-sort-topics-1 top ,reverse)))
1705 (sort (cdr top)
1706 (lambda (t1 t2)
1707 (string-lessp (caar t1) (caar t2)))))))
1708 (setcdr top (if reverse (reverse subtop) subtop))))
1709 top)
1711 (defun gnus-topic-sort-topics (&optional topic reverse)
1712 "Sort topics in TOPIC alphabetically by topic name.
1713 If REVERSE, reverse the sorting order."
1714 (interactive
1715 (list (gnus-completing-read "Sort topics in"
1716 (mapcar 'car gnus-topic-alist) t
1717 (gnus-current-topic))
1718 current-prefix-arg))
1719 (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
1720 gnus-topic-topology)))
1721 (gnus-topic-sort-topics-1 topic-topology reverse)
1722 (gnus-topic-enter-dribble)
1723 (gnus-group-list-groups)
1724 (gnus-topic-goto-topic topic)))
1726 (defun gnus-topic-move (current to)
1727 "Move the CURRENT topic to TO."
1728 (interactive
1729 (list
1730 (gnus-group-topic-name)
1731 (gnus-completing-read "Move to topic" (mapcar 'car gnus-topic-alist) t)))
1732 (unless (and current to)
1733 (error "Can't find topic"))
1734 (let ((current-top (cdr (gnus-topic-find-topology current)))
1735 (to-top (cdr (gnus-topic-find-topology to))))
1736 (unless current-top
1737 (error "Can't find topic `%s'" current))
1738 (unless to-top
1739 (error "Can't find topic `%s'" to))
1740 (if (gnus-topic-find-topology to current-top 0);; Don't care the level
1741 (error "Can't move `%s' to its sub-level" current))
1742 (gnus-topic-find-topology current nil nil 'delete)
1743 (setcdr (last to-top) (list current-top))
1744 (gnus-topic-enter-dribble)
1745 (gnus-group-list-groups)
1746 (gnus-topic-goto-topic current)))
1748 (defun gnus-subscribe-topics (newsgroup)
1749 (catch 'end
1750 (let (match gnus-group-change-level-function)
1751 (dolist (topic (gnus-topic-list))
1752 (when (and (setq match (cdr (assq 'subscribe
1753 (gnus-topic-parameters topic))))
1754 (string-match match newsgroup))
1755 ;; Just subscribe the group.
1756 (gnus-subscribe-alphabetically newsgroup)
1757 ;; Add the group to the topic.
1758 (nconc (assoc topic gnus-topic-alist) (list newsgroup))
1759 ;; if this topic specifies a default level, use it
1760 (let ((subscribe-level (cdr (assq 'subscribe-level
1761 (gnus-topic-parameters topic)))))
1762 (when subscribe-level
1763 (gnus-group-change-level newsgroup subscribe-level
1764 gnus-level-default-subscribed)))
1765 (throw 'end t)))
1766 nil)))
1768 (provide 'gnus-topic)
1770 ;;; gnus-topic.el ends here