Remove `$Id$' tag.
[bpt/emacs.git] / lisp / gnus / gnus-topic.el
CommitLineData
eec82323 1;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
6748645f 2;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
eec82323
LMI
3
4;; Author: Ilja Weis <kult@uni-paderborn.de>
6748645f 5;; Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
6;; Keywords: news
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;;; Code:
28
5ab7173c
RS
29(eval-when-compile (require 'cl))
30
6748645f
LMI
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
50(defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
51 "Format of topic lines.
52It works along the same lines as a normal formatting string,
53with some simple extensions.
54
55%i Indentation based on topic level.
56%n Topic name.
57%v Nothing if the topic is visible, \"...\" otherwise.
58%g Number of groups in the topic.
59%a Number of unread articles in the groups in the topic.
60%A Number of unread articles in the groups in the topic and its subtopics.
61"
62 :type 'string
63 :group 'gnus-topic)
64
65(defcustom gnus-topic-indent-level 2
66 "*How much each subtopic should be indented."
67 :type 'integer
68 :group 'gnus-topic)
69
70(defcustom gnus-topic-display-empty-topics t
71 "*If non-nil, display the topic lines even of topics that have no unread articles."
72 :type 'boolean
73 :group 'gnus-topic)
74
75;; Internal variables.
76
77(defvar gnus-topic-active-topology nil)
78(defvar gnus-topic-active-alist nil)
6748645f 79(defvar gnus-topic-unreads nil)
eec82323
LMI
80
81(defvar gnus-topology-checked-p nil
82 "Whether the topology has been checked in this session.")
83
84(defvar gnus-topic-killed-topics nil)
85(defvar gnus-topic-inhibit-change-level nil)
eec82323
LMI
86
87(defconst gnus-topic-line-format-alist
88 `((?n name ?s)
89 (?v visible ?s)
90 (?i indentation ?s)
91 (?g number-of-groups ?d)
92 (?a (gnus-topic-articles-in-topic entries) ?d)
93 (?A total-number-of-articles ?d)
94 (?l level ?d)))
95
96(defvar gnus-topic-line-format-spec nil)
97
98;;; Utility functions
99
100(defun gnus-group-topic-name ()
101 "The name of the topic on the current line."
102 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
103 (and topic (symbol-name topic))))
104
105(defun gnus-group-topic-level ()
106 "The level of the topic on the current line."
107 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
108
109(defun gnus-group-topic-unread ()
110 "The number of unread articles in topic on the current line."
111 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
112
113(defun gnus-topic-unread (topic)
114 "Return the number of unread articles in TOPIC."
6748645f 115 (or (cdr (assoc topic gnus-topic-unreads))
eec82323
LMI
116 0))
117
118(defun gnus-group-topic-p ()
119 "Return non-nil if the current line is a topic."
120 (gnus-group-topic-name))
121
122(defun gnus-topic-visible-p ()
123 "Return non-nil if the current topic is visible."
124 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
125
126(defun gnus-topic-articles-in-topic (entries)
127 (let ((total 0)
128 number)
129 (while entries
130 (when (numberp (setq number (car (pop entries))))
131 (incf total number)))
132 total))
133
134(defun gnus-group-topic (group)
135 "Return the topic GROUP is a member of."
136 (let ((alist gnus-topic-alist)
137 out)
138 (while alist
139 (when (member group (cdar alist))
140 (setq out (caar alist)
141 alist nil))
142 (setq alist (cdr alist)))
143 out))
144
145(defun gnus-group-parent-topic (group)
146 "Return the topic GROUP is member of by looking at the group buffer."
147 (save-excursion
148 (set-buffer gnus-group-buffer)
149 (if (gnus-group-goto-group group)
150 (gnus-current-topic)
151 (gnus-group-topic group))))
152
153(defun gnus-topic-goto-topic (topic)
154 "Go to TOPIC."
155 (when topic
156 (gnus-goto-char (text-property-any (point-min) (point-max)
157 'gnus-topic (intern topic)))))
158
159(defun gnus-current-topic ()
160 "Return the name of the current topic."
161 (let ((result
162 (or (get-text-property (point) 'gnus-topic)
163 (save-excursion
164 (and (gnus-goto-char (previous-single-property-change
165 (point) 'gnus-topic))
166 (get-text-property (max (1- (point)) (point-min))
167 'gnus-topic))))))
168 (when result
169 (symbol-name result))))
170
6748645f
LMI
171(defun gnus-current-topics (&optional topic)
172 "Return a list of all current topics, lowest in hierarchy first.
173If TOPIC, start with that topic."
174 (let ((topic (or topic (gnus-current-topic)))
eec82323
LMI
175 topics)
176 (while topic
177 (push topic topics)
178 (setq topic (gnus-topic-parent-topic topic)))
179 (nreverse topics)))
180
181(defun gnus-group-active-topic-p ()
182 "Say whether the current topic comes from the active topics."
183 (save-excursion
184 (beginning-of-line)
185 (get-text-property (point) 'gnus-active)))
186
6748645f 187(defun gnus-topic-find-groups (topic &optional level all lowest)
eec82323
LMI
188 "Return entries for all visible groups in TOPIC."
189 (let ((groups (cdr (assoc topic gnus-topic-alist)))
6748645f 190 info clevel unread group params visible-groups entry active)
eec82323 191 (setq lowest (or lowest 1))
6748645f 192 (setq level (or level gnus-level-unsubscribed))
eec82323
LMI
193 ;; We go through the newsrc to look for matches.
194 (while groups
195 (when (setq group (pop groups))
196 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
197 info (nth 2 entry)
198 params (gnus-info-params info)
199 active (gnus-active group)
200 unread (or (car entry)
201 (and (not (equal group "dummy.group"))
202 active
203 (- (1+ (cdr active)) (car active))))
204 clevel (or (gnus-info-level info)
6748645f
LMI
205 (if (member group gnus-zombie-list)
206 gnus-level-zombie gnus-level-killed))))
eec82323
LMI
207 (and
208 unread ; nil means that the group is dead.
209 (<= clevel level)
210 (>= clevel lowest) ; Is inside the level we want.
211 (or all
212 (if (eq unread t)
213 gnus-group-list-inactive-groups
214 (> unread 0))
215 (and gnus-list-groups-with-ticked-articles
216 (cdr (assq 'tick (gnus-info-marks info))))
217 ; Has right readedness.
218 ;; Check for permanent visibility.
219 (and gnus-permanently-visible-groups
220 (string-match gnus-permanently-visible-groups group))
221 (memq 'visible params)
222 (cdr (assq 'visible params)))
223 ;; Add this group to the list of visible groups.
224 (push (or entry group) visible-groups)))
225 (nreverse visible-groups)))
226
227(defun gnus-topic-previous-topic (topic)
228 "Return the previous topic on the same level as TOPIC."
229 (let ((top (cddr (gnus-topic-find-topology
230 (gnus-topic-parent-topic topic)))))
231 (unless (equal topic (caaar top))
232 (while (and top (not (equal (caaadr top) topic)))
233 (setq top (cdr top)))
234 (caaar top))))
235
236(defun gnus-topic-parent-topic (topic &optional topology)
237 "Return the parent of TOPIC."
238 (unless topology
239 (setq topology gnus-topic-topology))
240 (let ((parent (car (pop topology)))
241 result found)
242 (while (and topology
243 (not (setq found (equal (caaar topology) topic)))
244 (not (setq result (gnus-topic-parent-topic
245 topic (car topology)))))
246 (setq topology (cdr topology)))
247 (or result (and found parent))))
248
249(defun gnus-topic-next-topic (topic &optional previous)
250 "Return the next sibling of TOPIC."
251 (let ((parentt (cddr (gnus-topic-find-topology
252 (gnus-topic-parent-topic topic))))
253 prev)
254 (while (and parentt
255 (not (equal (caaar parentt) topic)))
256 (setq prev (caaar parentt)
257 parentt (cdr parentt)))
258 (if previous
259 prev
260 (caaadr parentt))))
261
262(defun gnus-topic-forward-topic (num)
263 "Go to the next topic on the same level as the current one."
264 (let* ((topic (gnus-current-topic))
265 (way (if (< num 0) 'gnus-topic-previous-topic
266 'gnus-topic-next-topic))
267 (num (abs num)))
268 (while (and (not (zerop num))
269 (setq topic (funcall way topic)))
270 (when (gnus-topic-goto-topic topic)
271 (decf num)))
272 (unless (zerop num)
273 (goto-char (point-max)))
274 num))
275
276(defun gnus-topic-find-topology (topic &optional topology level remove)
277 "Return the topology of TOPIC."
278 (unless topology
279 (setq topology gnus-topic-topology)
280 (setq level 0))
281 (let ((top topology)
282 result)
283 (if (equal (caar topology) topic)
284 (progn
285 (when remove
286 (delq topology remove))
287 (cons level topology))
288 (setq topology (cdr topology))
289 (while (and topology
290 (not (setq result (gnus-topic-find-topology
291 topic (car topology) (1+ level)
292 (and remove top)))))
293 (setq topology (cdr topology)))
294 result)))
295
296(defvar gnus-tmp-topics nil)
297(defun gnus-topic-list (&optional topology)
298 "Return a list of all topics in the topology."
299 (unless topology
300 (setq topology gnus-topic-topology
301 gnus-tmp-topics nil))
302 (push (caar topology) gnus-tmp-topics)
303 (mapcar 'gnus-topic-list (cdr topology))
304 gnus-tmp-topics)
305
306;;; Topic parameter jazz
307
308(defun gnus-topic-parameters (topic)
309 "Return the parameters for TOPIC."
310 (let ((top (gnus-topic-find-topology topic)))
311 (when top
312 (nth 3 (cadr top)))))
313
314(defun gnus-topic-set-parameters (topic parameters)
315 "Set the topic parameters of TOPIC to PARAMETERS."
316 (let ((top (gnus-topic-find-topology topic)))
317 (unless top
318 (error "No such topic: %s" topic))
319 ;; We may have to extend if there is no parameters here
320 ;; to begin with.
321 (unless (nthcdr 2 (cadr top))
322 (nconc (cadr top) (list nil)))
323 (unless (nthcdr 3 (cadr top))
324 (nconc (cadr top) (list nil)))
325 (setcar (nthcdr 3 (cadr top)) parameters)
326 (gnus-dribble-enter
327 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
328
329(defun gnus-group-topic-parameters (group)
330 "Compute the group parameters for GROUP taking into account inheritance from topics."
6748645f 331 (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
eec82323
LMI
332 (save-excursion
333 (gnus-group-goto-group group)
6748645f
LMI
334 (nconc params-list
335 (gnus-topic-hierarchical-parameters (gnus-current-topic))))))
336
337(defun gnus-topic-hierarchical-parameters (topic)
338 "Return a topic list computed for TOPIC."
339 (let ((topics (gnus-current-topics topic))
340 params-list param out params)
341 (while topics
342 (push (gnus-topic-parameters (pop topics)) params-list))
343 ;; We probably have lots of nil elements here, so
344 ;; we remove them. Probably faster than doing this "properly".
345 (setq params-list (delq nil params-list))
346 ;; Now we have all the parameters, so we go through them
347 ;; and do inheritance in the obvious way.
348 (while (setq params (pop params-list))
349 (while (setq param (pop params))
350 (when (atom param)
351 (setq param (cons param t)))
352 ;; Override any old versions of this param.
353 (gnus-pull (car param) out)
354 (push param out)))
355 ;; Return the resulting parameter list.
356 out))
eec82323
LMI
357
358;;; General utility functions
359
360(defun gnus-topic-enter-dribble ()
361 (gnus-dribble-enter
362 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
363
364;;; Generating group buffers
365
366(defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
6748645f
LMI
367 "List all newsgroups with unread articles of level LEVEL or lower.
368Use the `gnus-group-topics' to sort the groups.
eec82323
LMI
369If ALL is non-nil, list groups that have no unread articles.
370If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
371 (set-buffer gnus-group-buffer)
372 (let ((buffer-read-only nil)
373 (lowest (or lowest 1)))
374
eec82323
LMI
375 (when (or (not gnus-topic-alist)
376 (not gnus-topology-checked-p))
377 (gnus-topic-check-topology))
378
379 (unless list-topic
380 (erase-buffer))
381
382 ;; List dead groups?
6748645f
LMI
383 (when (and (>= level gnus-level-zombie)
384 (<= lowest gnus-level-zombie))
eec82323
LMI
385 (gnus-group-prepare-flat-list-dead
386 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
387 gnus-level-zombie ?Z
388 regexp))
389
390 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
391 (gnus-group-prepare-flat-list-dead
392 (setq gnus-killed-list (sort gnus-killed-list 'string<))
393 gnus-level-killed ?K
394 regexp))
395
396 ;; Use topics.
397 (prog1
398 (when (< lowest gnus-level-zombie)
399 (if list-topic
400 (let ((top (gnus-topic-find-topology list-topic)))
401 (gnus-topic-prepare-topic (cdr top) (car top)
6748645f
LMI
402 (or topic-level level) all
403 nil lowest))
eec82323 404 (gnus-topic-prepare-topic gnus-topic-topology 0
6748645f
LMI
405 (or topic-level level) all
406 nil lowest)))
eec82323
LMI
407
408 (gnus-group-set-mode-line)
409 (setq gnus-group-list-mode (cons level all))
6748645f 410 (gnus-run-hooks 'gnus-group-prepare-hook))))
eec82323 411
6748645f
LMI
412(defun gnus-topic-prepare-topic (topicl level &optional list-level all silent
413 lowest)
eec82323
LMI
414 "Insert TOPIC into the group buffer.
415If SILENT, don't insert anything. Return the number of unread
416articles in the topic and its subtopics."
417 (let* ((type (pop topicl))
6748645f
LMI
418 (entries (gnus-topic-find-groups
419 (car type) list-level
420 (or all
421 (cdr (assq 'visible
422 (gnus-topic-hierarchical-parameters
423 (car type)))))
424 lowest))
eec82323
LMI
425 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
426 (gnus-group-indentation
427 (make-string (* gnus-topic-indent-level level) ? ))
428 (beg (progn (beginning-of-line) (point)))
429 (topicl (reverse topicl))
430 (all-entries entries)
431 (point-max (point-max))
432 (unread 0)
433 (topic (car type))
434 info entry end active tick)
435 ;; Insert any sub-topics.
436 (while topicl
437 (incf unread
438 (gnus-topic-prepare-topic
439 (pop topicl) (1+ level) list-level all
6748645f 440 (not visiblep) lowest)))
eec82323
LMI
441 (setq end (point))
442 (goto-char beg)
443 ;; Insert all the groups that belong in this topic.
444 (while (setq entry (pop entries))
445 (when visiblep
446 (if (stringp entry)
447 ;; Dead groups.
448 (gnus-group-insert-group-line
6748645f 449 entry (if (member entry gnus-zombie-list) gnus-level-zombie gnus-level-killed)
eec82323
LMI
450 nil (- (1+ (cdr (setq active (gnus-active entry))))
451 (car active))
452 nil)
453 ;; Living groups.
454 (when (setq info (nth 2 entry))
455 (gnus-group-insert-group-line
456 (gnus-info-group info)
457 (gnus-info-level info) (gnus-info-marks info)
458 (car entry) (gnus-info-method info)))))
459 (when (and (listp entry)
a8151ef7 460 (numberp (car entry)))
eec82323
LMI
461 (incf unread (car entry)))
462 (when (listp entry)
463 (setq tick t)))
464 (goto-char beg)
465 ;; Insert the topic line.
466 (when (and (not silent)
467 (or gnus-topic-display-empty-topics ;We want empty topics
468 (not (zerop unread)) ;Non-empty
469 tick ;Ticked articles
470 (/= point-max (point-max)))) ;Unactivated groups
471 (gnus-extent-start-open (point))
472 (gnus-topic-insert-topic-line
473 (car type) visiblep
474 (not (eq (nth 2 type) 'hidden))
475 level all-entries unread))
6748645f 476 (gnus-topic-update-unreads (car type) unread)
eec82323
LMI
477 (goto-char end)
478 unread))
479
480(defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
481 "Remove the current topic."
482 (let ((topic (gnus-group-topic-name))
483 (level (gnus-group-topic-level))
484 (beg (progn (beginning-of-line) (point)))
485 buffer-read-only)
486 (when topic
487 (while (and (zerop (forward-line 1))
488 (> (or (gnus-group-topic-level) (1+ level)) level)))
489 (delete-region beg (point))
490 ;; Do the change in this rather odd manner because it has been
491 ;; reported that some topics share parts of some lists, for some
492 ;; reason. I have been unable to determine why this is the
493 ;; case, but this hack seems to take care of things.
494 (let ((data (cadr (gnus-topic-find-topology topic))))
495 (setcdr data
496 (list (if insert 'visible 'invisible)
497 (if hide 'hide nil)
498 (cadddr data))))
499 (if total-remove
500 (setq gnus-topic-alist
501 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
502 (gnus-topic-insert-topic topic in-level)))))
503
504(defun gnus-topic-insert-topic (topic &optional level)
505 "Insert TOPIC."
506 (gnus-group-prepare-topics
507 (car gnus-group-list-mode) (cdr gnus-group-list-mode)
508 nil nil topic level))
509
510(defun gnus-topic-fold (&optional insert)
511 "Remove/insert the current topic."
512 (let ((topic (gnus-group-topic-name)))
513 (when topic
514 (save-excursion
515 (if (not (gnus-group-active-topic-p))
516 (gnus-topic-remove-topic
517 (or insert (not (gnus-topic-visible-p))))
518 (let ((gnus-topic-topology gnus-topic-active-topology)
519 (gnus-topic-alist gnus-topic-active-alist)
520 (gnus-group-list-mode (cons 5 t)))
521 (gnus-topic-remove-topic
522 (or insert (not (gnus-topic-visible-p))) nil nil 9)
523 (gnus-topic-enter-dribble)))))))
524
525(defun gnus-topic-insert-topic-line (name visiblep shownp level entries
526 &optional unread)
527 (let* ((visible (if visiblep "" "..."))
528 (indentation (make-string (* gnus-topic-indent-level level) ? ))
529 (total-number-of-articles unread)
530 (number-of-groups (length entries))
6748645f
LMI
531 (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
532 gnus-tmp-header)
533 (gnus-topic-update-unreads name unread)
eec82323
LMI
534 (beginning-of-line)
535 ;; Insert the text.
536 (gnus-add-text-properties
537 (point)
538 (prog1 (1+ (point))
a8151ef7 539 (eval gnus-topic-line-format-spec))
eec82323
LMI
540 (list 'gnus-topic (intern name)
541 'gnus-topic-level level
542 'gnus-topic-unread unread
543 'gnus-active active-topic
544 'gnus-topic-visible visiblep))))
545
6748645f
LMI
546(defun gnus-topic-update-unreads (topic unreads)
547 (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
548 gnus-topic-unreads))
549 (push (cons topic unreads) gnus-topic-unreads))
550
eec82323
LMI
551(defun gnus-topic-update-topics-containing-group (group)
552 "Update all topics that have GROUP as a member."
553 (when (and (eq major-mode 'gnus-group-mode)
554 gnus-topic-mode)
555 (save-excursion
556 (let ((alist gnus-topic-alist))
557 ;; This is probably not entirely correct. If a topic
558 ;; isn't shown, then it's not updated. But the updating
559 ;; should be performed in any case, since the topic's
560 ;; parent should be updated. Pfft.
561 (while alist
562 (when (and (member group (cdar alist))
563 (gnus-topic-goto-topic (caar alist)))
564 (gnus-topic-update-topic-line (caar alist)))
565 (pop alist))))))
566
567(defun gnus-topic-update-topic ()
568 "Update all parent topics to the current group."
569 (when (and (eq major-mode 'gnus-group-mode)
570 gnus-topic-mode)
571 (let ((group (gnus-group-group-name))
a8151ef7 572 (m (point-marker))
eec82323
LMI
573 (buffer-read-only nil))
574 (when (and group
575 (gnus-get-info group)
576 (gnus-topic-goto-topic (gnus-current-topic)))
577 (gnus-topic-update-topic-line (gnus-group-topic-name))
a8151ef7
LMI
578 (goto-char m)
579 (set-marker m nil)
eec82323
LMI
580 (gnus-group-position-point)))))
581
582(defun gnus-topic-goto-missing-group (group)
583 "Place point where GROUP is supposed to be inserted."
584 (let* ((topic (gnus-group-topic group))
585 (groups (cdr (assoc topic gnus-topic-alist)))
586 (g (cdr (member group groups)))
587 (unfound t))
588 ;; Try to jump to a visible group.
589 (while (and g (not (gnus-group-goto-group (car g) t)))
590 (pop g))
591 ;; It wasn't visible, so we try to see where to insert it.
592 (when (not g)
593 (setq g (cdr (member group (reverse groups))))
594 (while (and g unfound)
595 (when (gnus-group-goto-group (pop g) t)
596 (forward-line 1)
597 (setq unfound nil)))
598 (when (and unfound
599 topic
600 (not (gnus-topic-goto-missing-topic topic)))
601 (gnus-topic-insert-topic-line
602 topic t t (car (gnus-topic-find-topology topic)) nil 0)))))
603
604(defun gnus-topic-goto-missing-topic (topic)
605 (if (gnus-topic-goto-topic topic)
606 (forward-line 1)
607 ;; Topic not displayed.
608 (let* ((top (gnus-topic-find-topology
609 (gnus-topic-parent-topic topic)))
610 (tp (reverse (cddr top))))
611 (while (not (equal (caaar tp) topic))
612 (setq tp (cdr tp)))
613 (pop tp)
614 (while (and tp
615 (not (gnus-topic-goto-topic (caaar tp))))
616 (pop tp))
617 (if tp
618 (gnus-topic-forward-topic 1)
619 (gnus-topic-goto-missing-topic (caadr top))))
620 nil))
621
622(defun gnus-topic-update-topic-line (topic-name &optional reads)
623 (let* ((top (gnus-topic-find-topology topic-name))
624 (type (cadr top))
625 (children (cddr top))
626 (entries (gnus-topic-find-groups
627 (car type) (car gnus-group-list-mode)
628 (cdr gnus-group-list-mode)))
629 (parent (gnus-topic-parent-topic topic-name))
630 (all-entries entries)
631 (unread 0)
6748645f 632 old-unread entry new-unread)
eec82323
LMI
633 (when (gnus-topic-goto-topic (car type))
634 ;; Tally all the groups that belong in this topic.
635 (if reads
636 (setq unread (- (gnus-group-topic-unread) reads))
637 (while children
638 (incf unread (gnus-topic-unread (caar (pop children)))))
639 (while (setq entry (pop entries))
640 (when (numberp (car entry))
641 (incf unread (car entry)))))
642 (setq old-unread (gnus-group-topic-unread))
643 ;; Insert the topic line.
644 (gnus-topic-insert-topic-line
645 (car type) (gnus-topic-visible-p)
646 (not (eq (nth 2 type) 'hidden))
647 (gnus-group-topic-level) all-entries unread)
6748645f
LMI
648 (gnus-delete-line)
649 (forward-line -1)
650 (setq new-unread (gnus-group-topic-unread)))
eec82323
LMI
651 (when parent
652 (forward-line -1)
653 (gnus-topic-update-topic-line
6748645f
LMI
654 parent
655 (- (or old-unread 0) (or new-unread 0))))
eec82323
LMI
656 unread))
657
658(defun gnus-topic-group-indentation ()
659 (make-string
660 (* gnus-topic-indent-level
661 (or (save-excursion
662 (forward-line -1)
663 (gnus-topic-goto-topic (gnus-current-topic))
664 (gnus-group-topic-level))
665 0))
666 ? ))
667
668;;; Initialization
669
670(gnus-add-shutdown 'gnus-topic-close 'gnus)
671
672(defun gnus-topic-close ()
673 (setq gnus-topic-active-topology nil
674 gnus-topic-active-alist nil
675 gnus-topic-killed-topics nil
eec82323
LMI
676 gnus-topology-checked-p nil))
677
678(defun gnus-topic-check-topology ()
679 ;; The first time we set the topology to whatever we have
680 ;; gotten here, which can be rather random.
681 (unless gnus-topic-alist
682 (gnus-topic-init-alist))
683
684 (setq gnus-topology-checked-p t)
685 ;; Go through the topic alist and make sure that all topics
686 ;; are in the topic topology.
687 (let ((topics (gnus-topic-list))
688 (alist gnus-topic-alist)
689 changed)
690 (while alist
691 (unless (member (caar alist) topics)
692 (nconc gnus-topic-topology
693 (list (list (list (caar alist) 'visible))))
694 (setq changed t))
695 (setq alist (cdr alist)))
696 (when changed
697 (gnus-topic-enter-dribble))
698 ;; Conversely, go through the topology and make sure that all
699 ;; topologies have alists.
700 (while topics
701 (unless (assoc (car topics) gnus-topic-alist)
702 (push (list (car topics)) gnus-topic-alist))
703 (pop topics)))
704 ;; Go through all living groups and make sure that
705 ;; they belong to some topic.
706 (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
707 gnus-topic-alist)))
a8151ef7 708 (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
eec82323
LMI
709 (newsrc (cdr gnus-newsrc-alist))
710 group)
711 (while newsrc
712 (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
a8151ef7
LMI
713 (setcdr entry (list group))
714 (setq entry (cdr entry)))))
eec82323
LMI
715 ;; Go through all topics and make sure they contain only living groups.
716 (let ((alist gnus-topic-alist)
717 topic)
718 (while (setq topic (pop alist))
719 (while (cdr topic)
a8151ef7
LMI
720 (if (and (cadr topic)
721 (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
eec82323
LMI
722 (setq topic (cdr topic))
723 (setcdr topic (cddr topic)))))))
724
725(defun gnus-topic-init-alist ()
726 "Initialize the topic structures."
727 (setq gnus-topic-topology
728 (cons (list "Gnus" 'visible)
729 (mapcar (lambda (topic)
730 (list (list (car topic) 'visible)))
731 '(("misc")))))
732 (setq gnus-topic-alist
733 (list (cons "misc"
734 (mapcar (lambda (info) (gnus-info-group info))
735 (cdr gnus-newsrc-alist)))
736 (list "Gnus")))
737 (gnus-topic-enter-dribble))
738
739;;; Maintenance
740
741(defun gnus-topic-clean-alist ()
742 "Remove bogus groups from the topic alist."
743 (let ((topic-alist gnus-topic-alist)
744 result topic)
745 (unless gnus-killed-hashtb
746 (gnus-make-hashtable-from-killed))
747 (while (setq topic (pop topic-alist))
748 (let ((topic-name (pop topic))
749 group filtered-topic)
750 (while (setq group (pop topic))
751 (when (and (or (gnus-gethash group gnus-active-hashtb)
752 (gnus-info-method (gnus-get-info group)))
753 (not (gnus-gethash group gnus-killed-hashtb)))
754 (push group filtered-topic)))
755 (push (cons topic-name (nreverse filtered-topic)) result)))
756 (setq gnus-topic-alist (nreverse result))))
757
a8151ef7 758(defun gnus-topic-change-level (group level oldlevel &optional previous)
eec82323
LMI
759 "Run when changing levels to enter/remove groups from topics."
760 (save-excursion
761 (set-buffer gnus-group-buffer)
6748645f
LMI
762 (let ((buffer-read-only nil))
763 (unless gnus-topic-inhibit-change-level
764 (gnus-group-goto-group (or (car (nth 2 previous)) group))
765 (when (and gnus-topic-mode
766 gnus-topic-alist
767 (not gnus-topic-inhibit-change-level))
768 ;; Remove the group from the topics.
769 (if (and (< oldlevel gnus-level-zombie)
770 (>= level gnus-level-zombie))
771 (let ((alist gnus-topic-alist))
772 (while (gnus-group-goto-group group)
773 (gnus-delete-line))
774 (while alist
775 (when (member group (car alist))
776 (setcdr (car alist) (delete group (cdar alist))))
777 (pop alist)))
778 ;; If the group is subscribed we enter it into the topics.
779 (when (and (< level gnus-level-zombie)
780 (>= oldlevel gnus-level-zombie))
781 (let* ((prev (gnus-group-group-name))
782 (gnus-topic-inhibit-change-level t)
783 (gnus-group-indentation
784 (make-string
785 (* gnus-topic-indent-level
786 (or (save-excursion
787 (gnus-topic-goto-topic (gnus-current-topic))
788 (gnus-group-topic-level))
789 0))
790 ? ))
791 (yanked (list group))
792 alist talist end)
793 ;; Then we enter the yanked groups into the topics they belong
794 ;; to.
795 (when (setq alist (assoc (save-excursion
796 (forward-line -1)
797 (or
798 (gnus-current-topic)
799 (caar gnus-topic-topology)))
800 gnus-topic-alist))
801 (setq talist alist)
802 (when (stringp yanked)
803 (setq yanked (list yanked)))
804 (if (not prev)
805 (nconc alist yanked)
806 (if (not (cdr alist))
807 (setcdr alist (nconc yanked (cdr alist)))
808 (while (and (not end) (cdr alist))
809 (when (equal (cadr alist) prev)
810 (setcdr alist (nconc yanked (cdr alist)))
811 (setq end t))
812 (setq alist (cdr alist)))
813 (unless end
814 (nconc talist yanked))))))
815 (gnus-topic-update-topic))))))))
eec82323
LMI
816
817(defun gnus-topic-goto-next-group (group props)
818 "Go to group or the next group after group."
819 (if (not group)
820 (if (not (memq 'gnus-topic props))
821 (goto-char (point-max))
822 (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
823 (if (gnus-group-goto-group group)
824 t
825 ;; The group is no longer visible.
826 (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
827 (after (cdr (member group (cdr list)))))
828 ;; First try to put point on a group after the current one.
829 (while (and after
830 (not (gnus-group-goto-group (car after))))
831 (setq after (cdr after)))
832 ;; Then try to put point on a group before point.
833 (unless after
834 (setq after (cdr (member group (reverse (cdr list)))))
835 (while (and after
836 (not (gnus-group-goto-group (car after))))
837 (setq after (cdr after))))
838 ;; Finally, just put point on the topic.
839 (if (not (car list))
840 (goto-char (point-min))
841 (unless after
842 (gnus-topic-goto-topic (car list))
843 (setq after nil)))
844 t))))
845
846;;; Topic-active functions
847
848(defun gnus-topic-grok-active (&optional force)
849 "Parse all active groups and create topic structures for them."
850 ;; First we make sure that we have really read the active file.
851 (when (or force
852 (not gnus-topic-active-alist))
853 (let (groups)
854 ;; Get a list of all groups available.
855 (mapatoms (lambda (g) (when (symbol-value g)
856 (push (symbol-name g) groups)))
857 gnus-active-hashtb)
858 (setq groups (sort groups 'string<))
859 ;; Init the variables.
860 (setq gnus-topic-active-topology (list (list "" 'visible)))
861 (setq gnus-topic-active-alist nil)
862 ;; Descend the top-level hierarchy.
863 (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
864 ;; Set the top-level topic names to something nice.
865 (setcar (car gnus-topic-active-topology) "Gnus active")
866 (setcar (car gnus-topic-active-alist) "Gnus active"))))
867
868(defun gnus-topic-grok-active-1 (topology groups)
869 (let* ((name (caar topology))
870 (prefix (concat "^" (regexp-quote name)))
871 tgroups ntopology group)
872 (while (and groups
873 (string-match prefix (setq group (car groups))))
874 (if (not (string-match "\\." group (match-end 0)))
875 ;; There are no further hierarchies here, so we just
876 ;; enter this group into the list belonging to this
877 ;; topic.
878 (push (pop groups) tgroups)
879 ;; New sub-hierarchy, so we add it to the topology.
880 (nconc topology (list (setq ntopology
881 (list (list (substring
882 group 0 (match-end 0))
883 'invisible)))))
884 ;; Descend the hierarchy.
885 (setq groups (gnus-topic-grok-active-1 ntopology groups))))
886 ;; We remove the trailing "." from the topic name.
887 (setq name
888 (if (string-match "\\.$" name)
889 (substring name 0 (match-beginning 0))
890 name))
891 ;; Add this topic and its groups to the topic alist.
892 (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
893 (setcar (car topology) name)
894 ;; We return the rest of the groups that didn't belong
895 ;; to this topic.
896 groups))
897
898;;; Topic mode, commands and keymap.
899
900(defvar gnus-topic-mode-map nil)
901(defvar gnus-group-topic-map nil)
902
903(unless gnus-topic-mode-map
904 (setq gnus-topic-mode-map (make-sparse-keymap))
905
906 ;; Override certain group mode keys.
907 (gnus-define-keys gnus-topic-mode-map
908 "=" gnus-topic-select-group
909 "\r" gnus-topic-select-group
910 " " gnus-topic-read-group
911 "\C-k" gnus-topic-kill-group
912 "\C-y" gnus-topic-yank-group
913 "\M-g" gnus-topic-get-new-news-this-topic
914 "AT" gnus-topic-list-active
915 "Gp" gnus-topic-edit-parameters
916 "#" gnus-topic-mark-topic
917 "\M-#" gnus-topic-unmark-topic
6748645f
LMI
918 [tab] gnus-topic-indent
919 [(meta tab)] gnus-topic-unindent
920 "\C-i" gnus-topic-indent
921 "\M-\C-i" gnus-topic-unindent
eec82323
LMI
922 gnus-mouse-2 gnus-mouse-pick-topic)
923
924 ;; Define a new submap.
925 (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
926 "#" gnus-topic-mark-topic
927 "\M-#" gnus-topic-unmark-topic
928 "n" gnus-topic-create-topic
929 "m" gnus-topic-move-group
930 "D" gnus-topic-remove-group
931 "c" gnus-topic-copy-group
932 "h" gnus-topic-hide-topic
933 "s" gnus-topic-show-topic
934 "M" gnus-topic-move-matching
935 "C" gnus-topic-copy-matching
936 "\C-i" gnus-topic-indent
937 [tab] gnus-topic-indent
938 "r" gnus-topic-rename
a8151ef7
LMI
939 "\177" gnus-topic-delete
940 [delete] gnus-topic-delete
6748645f 941 "H" gnus-topic-toggle-display-empty-topics)
eec82323
LMI
942
943 (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
944 "s" gnus-topic-sort-groups
945 "a" gnus-topic-sort-groups-by-alphabet
946 "u" gnus-topic-sort-groups-by-unread
947 "l" gnus-topic-sort-groups-by-level
948 "v" gnus-topic-sort-groups-by-score
949 "r" gnus-topic-sort-groups-by-rank
950 "m" gnus-topic-sort-groups-by-method))
951
952(defun gnus-topic-make-menu-bar ()
953 (unless (boundp 'gnus-topic-menu)
954 (easy-menu-define
955 gnus-topic-menu gnus-topic-mode-map ""
956 '("Topics"
957 ["Toggle topics" gnus-topic-mode t]
958 ("Groups"
959 ["Copy" gnus-topic-copy-group t]
960 ["Move" gnus-topic-move-group t]
961 ["Remove" gnus-topic-remove-group t]
962 ["Copy matching" gnus-topic-copy-matching t]
963 ["Move matching" gnus-topic-move-matching t])
964 ("Topics"
965 ["Show" gnus-topic-show-topic t]
966 ["Hide" gnus-topic-hide-topic t]
967 ["Delete" gnus-topic-delete t]
968 ["Rename" gnus-topic-rename t]
969 ["Create" gnus-topic-create-topic t]
970 ["Mark" gnus-topic-mark-topic t]
a8151ef7
LMI
971 ["Indent" gnus-topic-indent t]
972 ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
973 ["Edit parameters" gnus-topic-edit-parameters t])
eec82323
LMI
974 ["List active" gnus-topic-list-active t]))))
975
976(defun gnus-topic-mode (&optional arg redisplay)
977 "Minor mode for topicsifying Gnus group buffers."
978 (interactive (list current-prefix-arg t))
979 (when (eq major-mode 'gnus-group-mode)
980 (make-local-variable 'gnus-topic-mode)
981 (setq gnus-topic-mode
982 (if (null arg) (not gnus-topic-mode)
983 (> (prefix-numeric-value arg) 0)))
984 ;; Infest Gnus with topics.
6748645f
LMI
985 (if (not gnus-topic-mode)
986 (setq gnus-goto-missing-group-function nil)
eec82323
LMI
987 (when (gnus-visual-p 'topic-menu 'menu)
988 (gnus-topic-make-menu-bar))
6748645f 989 (gnus-set-format 'topic t)
a8151ef7 990 (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
eec82323
LMI
991 (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
992 (set (make-local-variable 'gnus-group-prepare-function)
993 'gnus-group-prepare-topics)
994 (set (make-local-variable 'gnus-group-get-parameter-function)
995 'gnus-group-topic-parameters)
996 (set (make-local-variable 'gnus-group-goto-next-group-function)
997 'gnus-topic-goto-next-group)
998 (set (make-local-variable 'gnus-group-indentation-function)
999 'gnus-topic-group-indentation)
1000 (set (make-local-variable 'gnus-group-update-group-function)
1001 'gnus-topic-update-topics-containing-group)
1002 (set (make-local-variable 'gnus-group-sort-alist-function)
1003 'gnus-group-sort-topic)
1004 (setq gnus-group-change-level-function 'gnus-topic-change-level)
1005 (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1006 (make-local-hook 'gnus-check-bogus-groups-hook)
1007 (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1008 (setq gnus-topology-checked-p nil)
1009 ;; We check the topology.
1010 (when gnus-newsrc-alist
1011 (gnus-topic-check-topology))
6748645f 1012 (gnus-run-hooks 'gnus-topic-mode-hook))
eec82323
LMI
1013 ;; Remove topic infestation.
1014 (unless gnus-topic-mode
1015 (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1016 (remove-hook 'gnus-group-change-level-function
1017 'gnus-topic-change-level)
1018 (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1019 (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1020 (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1021 (when redisplay
1022 (gnus-group-list-groups))))
1023
1024(defun gnus-topic-select-group (&optional all)
1025 "Select this newsgroup.
1026No article is selected automatically.
1027If ALL is non-nil, already read articles become readable.
1028If ALL is a number, fetch this number of articles.
1029
1030If performed over a topic line, toggle folding the topic."
1031 (interactive "P")
1032 (if (gnus-group-topic-p)
1033 (let ((gnus-group-list-mode
1034 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1035 (gnus-topic-fold all))
1036 (gnus-group-select-group all)))
1037
1038(defun gnus-mouse-pick-topic (e)
1039 "Select the group or topic under the mouse pointer."
1040 (interactive "e")
1041 (mouse-set-point e)
1042 (gnus-topic-read-group nil))
1043
1044(defun gnus-topic-read-group (&optional all no-article group)
1045 "Read news in this newsgroup.
1046If the prefix argument ALL is non-nil, already read articles become
1047readable. IF ALL is a number, fetch this number of articles. If the
1048optional argument NO-ARTICLE is non-nil, no article will be
1049auto-selected upon group entry. If GROUP is non-nil, fetch that
1050group.
1051
1052If performed over a topic line, toggle folding the topic."
1053 (interactive "P")
1054 (if (gnus-group-topic-p)
1055 (let ((gnus-group-list-mode
1056 (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1057 (gnus-topic-fold all))
1058 (gnus-group-read-group all no-article group)))
1059
1060(defun gnus-topic-create-topic (topic parent &optional previous full-topic)
a8151ef7
LMI
1061 "Create a new TOPIC under PARENT.
1062When used interactively, PARENT will be the topic under point."
eec82323
LMI
1063 (interactive
1064 (list
1065 (read-string "New topic: ")
1066 (gnus-current-topic)))
1067 ;; Check whether this topic already exists.
1068 (when (gnus-topic-find-topology topic)
1069 (error "Topic already exists"))
1070 (unless parent
1071 (setq parent (caar gnus-topic-topology)))
1072 (let ((top (cdr (gnus-topic-find-topology parent)))
1073 (full-topic (or full-topic `((,topic visible)))))
1074 (unless top
1075 (error "No such parent topic: %s" parent))
1076 (if previous
1077 (progn
1078 (while (and (cdr top)
1079 (not (equal (caaadr top) previous)))
1080 (setq top (cdr top)))
1081 (setcdr top (cons full-topic (cdr top))))
1082 (nconc top (list full-topic)))
1083 (unless (assoc topic gnus-topic-alist)
1084 (push (list topic) gnus-topic-alist)))
1085 (gnus-topic-enter-dribble)
1086 (gnus-group-list-groups)
1087 (gnus-topic-goto-topic topic))
1088
1089(defun gnus-topic-move-group (n topic &optional copyp)
1090 "Move the next N groups to TOPIC.
1091If COPYP, copy the groups instead."
1092 (interactive
1093 (list current-prefix-arg
1094 (completing-read "Move to topic: " gnus-topic-alist nil t)))
1095 (let ((groups (gnus-group-process-prefix n))
1096 (topicl (assoc topic gnus-topic-alist))
1097 (start-group (progn (forward-line 1) (gnus-group-group-name)))
1098 (start-topic (gnus-group-topic-name))
1099 entry)
1100 (mapcar
1101 (lambda (g)
1102 (gnus-group-remove-mark g)
1103 (when (and
1104 (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1105 (not copyp))
1106 (setcdr entry (gnus-delete-first g (cdr entry))))
1107 (nconc topicl (list g)))
1108 groups)
1109 (gnus-topic-enter-dribble)
1110 (if start-group
1111 (gnus-group-goto-group start-group)
1112 (gnus-topic-goto-topic start-topic))
1113 (gnus-group-list-groups)))
1114
1115(defun gnus-topic-remove-group (&optional arg)
1116 "Remove the current group from the topic."
1117 (interactive "P")
1118 (gnus-group-iterate arg
1119 (lambda (group)
1120 (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1121 (buffer-read-only nil))
1122 (when (and topicl group)
1123 (gnus-delete-line)
1124 (gnus-delete-first group topicl))
1125 (gnus-topic-update-topic)
1126 (gnus-group-position-point)))))
1127
1128(defun gnus-topic-copy-group (n topic)
1129 "Copy the current group to a topic."
1130 (interactive
1131 (list current-prefix-arg
1132 (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1133 (gnus-topic-move-group n topic t))
1134
1135(defun gnus-topic-kill-group (&optional n discard)
1136 "Kill the next N groups."
1137 (interactive "P")
1138 (if (gnus-group-topic-p)
1139 (let ((topic (gnus-group-topic-name)))
1140 (push (cons
1141 (gnus-topic-find-topology topic)
1142 (assoc topic gnus-topic-alist))
1143 gnus-topic-killed-topics)
1144 (gnus-topic-remove-topic nil t)
1145 (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1146 (gnus-topic-enter-dribble))
1147 (gnus-group-kill-group n discard)
1148 (gnus-topic-update-topic)))
1149
1150(defun gnus-topic-yank-group (&optional arg)
1151 "Yank the last topic."
1152 (interactive "p")
1153 (if gnus-topic-killed-topics
1154 (let* ((previous
1155 (or (gnus-group-topic-name)
1156 (gnus-topic-next-topic (gnus-current-topic))))
1157 (data (pop gnus-topic-killed-topics))
1158 (alist (cdr data))
1159 (item (cdar data)))
1160 (push alist gnus-topic-alist)
1161 (gnus-topic-create-topic
1162 (caar item) (gnus-topic-parent-topic previous) previous
1163 item)
1164 (gnus-topic-enter-dribble)
1165 (gnus-topic-goto-topic (caar item)))
1166 (let* ((prev (gnus-group-group-name))
1167 (gnus-topic-inhibit-change-level t)
1168 (gnus-group-indentation
1169 (make-string
1170 (* gnus-topic-indent-level
1171 (or (save-excursion
1172 (gnus-topic-goto-topic (gnus-current-topic))
1173 (gnus-group-topic-level))
1174 0))
1175 ? ))
1176 yanked alist)
1177 ;; We first yank the groups the normal way...
1178 (setq yanked (gnus-group-yank-group arg))
1179 ;; Then we enter the yanked groups into the topics they belong
1180 ;; to.
1181 (setq alist (assoc (save-excursion
1182 (forward-line -1)
1183 (gnus-current-topic))
1184 gnus-topic-alist))
1185 (when (stringp yanked)
1186 (setq yanked (list yanked)))
1187 (if (not prev)
1188 (nconc alist yanked)
1189 (if (not (cdr alist))
1190 (setcdr alist (nconc yanked (cdr alist)))
1191 (while (cdr alist)
1192 (when (equal (cadr alist) prev)
1193 (setcdr alist (nconc yanked (cdr alist)))
1194 (setq alist nil))
1195 (setq alist (cdr alist))))))
1196 (gnus-topic-update-topic)))
1197
1198(defun gnus-topic-hide-topic ()
1199 "Hide the current topic."
1200 (interactive)
1201 (when (gnus-current-topic)
1202 (gnus-topic-goto-topic (gnus-current-topic))
1203 (gnus-topic-remove-topic nil nil 'hidden)))
1204
1205(defun gnus-topic-show-topic ()
1206 "Show the hidden topic."
1207 (interactive)
1208 (when (gnus-group-topic-p)
1209 (gnus-topic-remove-topic t nil 'shown)))
1210
1211(defun gnus-topic-mark-topic (topic &optional unmark)
1212 "Mark all groups in the topic with the process mark."
1213 (interactive (list (gnus-group-topic-name)))
1214 (if (not topic)
1215 (call-interactively 'gnus-group-mark-group)
1216 (save-excursion
6748645f 1217 (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
eec82323
LMI
1218 (while groups
1219 (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1220 (gnus-info-group (nth 2 (pop groups)))))))))
1221
1222(defun gnus-topic-unmark-topic (topic &optional unmark)
1223 "Remove the process mark from all groups in the topic."
1224 (interactive (list (gnus-group-topic-name)))
1225 (if (not topic)
1226 (call-interactively 'gnus-group-unmark-group)
1227 (gnus-topic-mark-topic topic t)))
1228
1229(defun gnus-topic-get-new-news-this-topic (&optional n)
1230 "Check for new news in the current topic."
1231 (interactive "P")
1232 (if (not (gnus-group-topic-p))
1233 (gnus-group-get-new-news-this-group n)
1234 (gnus-topic-mark-topic (gnus-group-topic-name))
1235 (gnus-group-get-new-news-this-group)))
1236
1237(defun gnus-topic-move-matching (regexp topic &optional copyp)
1238 "Move all groups that match REGEXP to some topic."
1239 (interactive
1240 (let (topic)
1241 (nreverse
1242 (list
1243 (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1244 (read-string (format "Move to %s (regexp): " topic))))))
1245 (gnus-group-mark-regexp regexp)
1246 (gnus-topic-move-group nil topic copyp))
1247
1248(defun gnus-topic-copy-matching (regexp topic &optional copyp)
1249 "Copy all groups that match REGEXP to some topic."
1250 (interactive
1251 (let (topic)
1252 (nreverse
1253 (list
1254 (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1255 (read-string (format "Copy to %s (regexp): " topic))))))
1256 (gnus-topic-move-matching regexp topic t))
1257
1258(defun gnus-topic-delete (topic)
1259 "Delete a topic."
1260 (interactive (list (gnus-group-topic-name)))
1261 (unless topic
1262 (error "No topic to be deleted"))
1263 (let ((entry (assoc topic gnus-topic-alist))
1264 (buffer-read-only nil))
1265 (when (cdr entry)
1266 (error "Topic not empty"))
1267 ;; Delete if visible.
1268 (when (gnus-topic-goto-topic topic)
1269 (gnus-delete-line))
1270 ;; Remove from alist.
1271 (setq gnus-topic-alist (delq entry gnus-topic-alist))
1272 ;; Remove from topology.
a8151ef7
LMI
1273 (gnus-topic-find-topology topic nil nil 'delete)
1274 (gnus-dribble-touch)))
eec82323
LMI
1275
1276(defun gnus-topic-rename (old-name new-name)
1277 "Rename a topic."
1278 (interactive
1279 (let ((topic (gnus-current-topic)))
1280 (list topic
1281 (read-string (format "Rename %s to: " topic)))))
6748645f
LMI
1282 ;; Check whether the new name exists.
1283 (when (gnus-topic-find-topology new-name)
1284 (error "Topic '%s' already exists" new-name))
1285 ;; "nil" is an invalid name, for reasons I'd rather not go
1286 ;; into here. Trust me.
1287 (when (equal new-name "nil")
1288 (error "Invalid name: %s" nil))
1289 ;; Do the renaming.
eec82323
LMI
1290 (let ((top (gnus-topic-find-topology old-name))
1291 (entry (assoc old-name gnus-topic-alist)))
1292 (when top
1293 (setcar (cadr top) new-name))
1294 (when entry
1295 (setcar entry new-name))
1296 (forward-line -1)
1297 (gnus-dribble-touch)
6748645f
LMI
1298 (gnus-group-list-groups)
1299 (forward-line 1)))
eec82323
LMI
1300
1301(defun gnus-topic-indent (&optional unindent)
1302 "Indent a topic -- make it a sub-topic of the previous topic.
1303If UNINDENT, remove an indentation."
1304 (interactive "P")
1305 (if unindent
1306 (gnus-topic-unindent)
1307 (let* ((topic (gnus-current-topic))
1308 (parent (gnus-topic-previous-topic topic))
1309 (buffer-read-only nil))
1310 (unless parent
1311 (error "Nothing to indent %s into" topic))
1312 (when topic
1313 (gnus-topic-goto-topic topic)
1314 (gnus-topic-kill-group)
1315 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1316 (gnus-topic-create-topic
1317 topic parent nil (cdaar gnus-topic-killed-topics))
1318 (pop gnus-topic-killed-topics)
1319 (or (gnus-topic-goto-topic topic)
1320 (gnus-topic-goto-topic parent))))))
1321
1322(defun gnus-topic-unindent ()
1323 "Unindent a topic."
1324 (interactive)
1325 (let* ((topic (gnus-current-topic))
1326 (parent (gnus-topic-parent-topic topic))
1327 (grandparent (gnus-topic-parent-topic parent)))
1328 (unless grandparent
1329 (error "Nothing to indent %s into" topic))
1330 (when topic
1331 (gnus-topic-goto-topic topic)
1332 (gnus-topic-kill-group)
1333 (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1334 (gnus-topic-create-topic
1335 topic grandparent (gnus-topic-next-topic parent)
1336 (cdaar gnus-topic-killed-topics))
1337 (pop gnus-topic-killed-topics)
1338 (gnus-topic-goto-topic topic))))
1339
1340(defun gnus-topic-list-active (&optional force)
1341 "List all groups that Gnus knows about in a topicsified fashion.
1342If FORCE, always re-read the active file."
1343 (interactive "P")
1344 (when force
1345 (gnus-get-killed-groups))
1346 (gnus-topic-grok-active force)
1347 (let ((gnus-topic-topology gnus-topic-active-topology)
1348 (gnus-topic-alist gnus-topic-active-alist)
1349 gnus-killed-list gnus-zombie-list)
6748645f 1350 (gnus-group-list-groups gnus-level-killed nil 1)))
eec82323 1351
a8151ef7
LMI
1352(defun gnus-topic-toggle-display-empty-topics ()
1353 "Show/hide topics that have no unread articles."
1354 (interactive)
1355 (setq gnus-topic-display-empty-topics
1356 (not gnus-topic-display-empty-topics))
1357 (gnus-group-list-groups)
1358 (message "%s empty topics"
1359 (if gnus-topic-display-empty-topics
1360 "Showing" "Hiding")))
1361
eec82323
LMI
1362;;; Topic sorting functions
1363
1364(defun gnus-topic-edit-parameters (group)
1365 "Edit the group parameters of GROUP.
1366If performed on a topic, edit the topic parameters instead."
1367 (interactive (list (gnus-group-group-name)))
1368 (if group
1369 (gnus-group-edit-group-parameters group)
1370 (if (not (gnus-group-topic-p))
a8151ef7 1371 (error "Nothing to edit on the current line")
eec82323
LMI
1372 (let ((topic (gnus-group-topic-name)))
1373 (gnus-edit-form
1374 (gnus-topic-parameters topic)
1375 (format "Editing the topic parameters for `%s'."
1376 (or group topic))
1377 `(lambda (form)
1378 (gnus-topic-set-parameters ,topic form)))))))
1379
1380(defun gnus-group-sort-topic (func reverse)
1381 "Sort groups in the topics according to FUNC and REVERSE."
1382 (let ((alist gnus-topic-alist))
1383 (while alist
1384 ;; !!!Sometimes nil elements sneak into the alist,
1385 ;; for some reason or other.
1386 (setcar alist (delq nil (car alist)))
1387 (setcar alist (delete "dummy.group" (car alist)))
1388 (gnus-topic-sort-topic (pop alist) func reverse))))
1389
1390(defun gnus-topic-sort-topic (topic func reverse)
1391 ;; Each topic only lists the name of the group, while
1392 ;; the sort predicates expect group infos as inputs.
1393 ;; So we first transform the group names into infos,
1394 ;; then sort, and then transform back into group names.
1395 (setcdr
1396 topic
1397 (mapcar
1398 (lambda (info) (gnus-info-group info))
1399 (sort
1400 (mapcar
1401 (lambda (group) (gnus-get-info group))
1402 (cdr topic))
1403 func)))
1404 ;; Do the reversal, if necessary.
1405 (when reverse
1406 (setcdr topic (nreverse (cdr topic)))))
1407
1408(defun gnus-topic-sort-groups (func &optional reverse)
1409 "Sort the current topic according to FUNC.
1410If REVERSE, reverse the sorting order."
1411 (interactive (list gnus-group-sort-function current-prefix-arg))
1412 (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1413 (gnus-topic-sort-topic
1414 topic (gnus-make-sort-function func) reverse)
1415 (gnus-group-list-groups)))
1416
1417(defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1418 "Sort the current topic alphabetically by group name.
1419If REVERSE, sort in reverse order."
1420 (interactive "P")
1421 (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1422
1423(defun gnus-topic-sort-groups-by-unread (&optional reverse)
1424 "Sort the current topic by number of unread articles.
1425If REVERSE, sort in reverse order."
1426 (interactive "P")
1427 (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1428
1429(defun gnus-topic-sort-groups-by-level (&optional reverse)
1430 "Sort the current topic by group level.
1431If REVERSE, sort in reverse order."
1432 (interactive "P")
1433 (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1434
1435(defun gnus-topic-sort-groups-by-score (&optional reverse)
1436 "Sort the current topic by group score.
1437If REVERSE, sort in reverse order."
1438 (interactive "P")
1439 (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1440
1441(defun gnus-topic-sort-groups-by-rank (&optional reverse)
1442 "Sort the current topic by group rank.
1443If REVERSE, sort in reverse order."
1444 (interactive "P")
1445 (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1446
1447(defun gnus-topic-sort-groups-by-method (&optional reverse)
1448 "Sort the current topic alphabetically by backend name.
1449If REVERSE, sort in reverse order."
1450 (interactive "P")
1451 (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1452
1453(provide 'gnus-topic)
1454
1455;;; gnus-topic.el ends here