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