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