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