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