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