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