Sync up with Pterodactyl Gnus 0.40.
[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)
448                        gnus-level-zombie gnus-level-killed)
449              nil (- (1+ (cdr (setq active (gnus-active entry))))
450                     (car active))
451              nil)
452           ;; Living groups.
453           (when (setq info (nth 2 entry))
454             (gnus-group-insert-group-line
455              (gnus-info-group info)
456              (gnus-info-level info) (gnus-info-marks info)
457              (car entry) (gnus-info-method info)))))
458       (when (and (listp entry)
459                  (numberp (car entry)))
460         (incf unread (car entry)))
461       (when (listp entry)
462         (setq tick t)))
463     (goto-char beg)
464     ;; Insert the topic line.
465     (when (and (not silent)
466                (or gnus-topic-display-empty-topics ;We want empty topics
467                    (not (zerop unread)) ;Non-empty
468                    tick                 ;Ticked articles
469                    (/= point-max (point-max)))) ;Unactivated groups
470       (gnus-extent-start-open (point))
471       (gnus-topic-insert-topic-line
472        (car type) visiblep
473        (not (eq (nth 2 type) 'hidden))
474        level all-entries unread))
475     (gnus-topic-update-unreads (car type) unread)
476     (goto-char end)
477     unread))
478
479 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
480   "Remove the current topic."
481   (let ((topic (gnus-group-topic-name))
482         (level (gnus-group-topic-level))
483         (beg (progn (beginning-of-line) (point)))
484         buffer-read-only)
485     (when topic
486       (while (and (zerop (forward-line 1))
487                   (> (or (gnus-group-topic-level) (1+ level)) level)))
488       (delete-region beg (point))
489       ;; Do the change in this rather odd manner because it has been
490       ;; reported that some topics share parts of some lists, for some
491       ;; reason.  I have been unable to determine why this is the
492       ;; case, but this hack seems to take care of things.
493       (let ((data (cadr (gnus-topic-find-topology topic))))
494         (setcdr data
495                 (list (if insert 'visible 'invisible)
496                       (if hide 'hide nil)
497                       (cadddr data))))
498       (if total-remove
499           (setq gnus-topic-alist
500                 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
501         (gnus-topic-insert-topic topic in-level)))))
502
503 (defun gnus-topic-insert-topic (topic &optional level)
504   "Insert TOPIC."
505   (gnus-group-prepare-topics
506    (car gnus-group-list-mode) (cdr gnus-group-list-mode)
507    nil nil topic level))
508
509 (defun gnus-topic-fold (&optional insert)
510   "Remove/insert the current topic."
511   (let ((topic (gnus-group-topic-name)))
512     (when topic
513       (save-excursion
514         (if (not (gnus-group-active-topic-p))
515             (gnus-topic-remove-topic
516              (or insert (not (gnus-topic-visible-p))))
517           (let ((gnus-topic-topology gnus-topic-active-topology)
518                 (gnus-topic-alist gnus-topic-active-alist)
519                 (gnus-group-list-mode (cons 5 t)))
520             (gnus-topic-remove-topic
521              (or insert (not (gnus-topic-visible-p))) nil nil 9)
522             (gnus-topic-enter-dribble)))))))
523
524 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
525                                           &optional unread)
526   (let* ((visible (if visiblep "" "..."))
527          (indentation (make-string (* gnus-topic-indent-level level) ? ))
528          (total-number-of-articles unread)
529          (number-of-groups (length entries))
530          (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
531          gnus-tmp-header)
532     (gnus-topic-update-unreads name unread)
533     (beginning-of-line)
534     ;; Insert the text.
535     (gnus-add-text-properties
536      (point)
537      (prog1 (1+ (point))
538        (eval gnus-topic-line-format-spec))
539      (list 'gnus-topic (intern name)
540            'gnus-topic-level level
541            'gnus-topic-unread unread
542            'gnus-active active-topic
543            'gnus-topic-visible visiblep))))
544
545 (defun gnus-topic-update-unreads (topic unreads)
546   (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
547                                  gnus-topic-unreads))
548   (push (cons topic unreads) gnus-topic-unreads))
549
550 (defun gnus-topic-update-topics-containing-group (group)
551   "Update all topics that have GROUP as a member."
552   (when (and (eq major-mode 'gnus-group-mode)
553              gnus-topic-mode)
554     (save-excursion
555       (let ((alist gnus-topic-alist))
556         ;; This is probably not entirely correct.  If a topic
557         ;; isn't shown, then it's not updated.  But the updating
558         ;; should be performed in any case, since the topic's
559         ;; parent should be updated.  Pfft.
560         (while alist
561           (when (and (member group (cdar alist))
562                      (gnus-topic-goto-topic (caar alist)))
563             (gnus-topic-update-topic-line (caar alist)))
564           (pop alist))))))
565
566 (defun gnus-topic-update-topic ()
567   "Update all parent topics to the current group."
568   (when (and (eq major-mode 'gnus-group-mode)
569              gnus-topic-mode)
570     (let ((group (gnus-group-group-name))
571           (m (point-marker))
572           (buffer-read-only nil))
573       (when (and group
574                  (gnus-get-info group)
575                  (gnus-topic-goto-topic (gnus-current-topic)))
576         (gnus-topic-update-topic-line (gnus-group-topic-name))
577         (goto-char m)
578         (set-marker m nil)
579         (gnus-group-position-point)))))
580
581 (defun gnus-topic-goto-missing-group (group)
582   "Place point where GROUP is supposed to be inserted."
583   (let* ((topic (gnus-group-topic group))
584          (groups (cdr (assoc topic gnus-topic-alist)))
585          (g (cdr (member group groups)))
586          (unfound t))
587     ;; Try to jump to a visible group.
588     (while (and g (not (gnus-group-goto-group (car g) t)))
589       (pop g))
590     ;; It wasn't visible, so we try to see where to insert it.
591     (when (not g)
592       (setq g (cdr (member group (reverse groups))))
593       (while (and g unfound)
594         (when (gnus-group-goto-group (pop g) t)
595           (forward-line 1)
596           (setq unfound nil)))
597       (when (and unfound
598                  topic
599                  (not (gnus-topic-goto-missing-topic topic)))
600         (gnus-topic-insert-topic-line
601          topic t t (car (gnus-topic-find-topology topic)) nil 0)))))
602
603 (defun gnus-topic-goto-missing-topic (topic)
604   (if (gnus-topic-goto-topic topic)
605       (forward-line 1)
606     ;; Topic not displayed.
607     (let* ((top (gnus-topic-find-topology
608                  (gnus-topic-parent-topic topic)))
609            (tp (reverse (cddr top))))
610       (while (not (equal (caaar tp) topic))
611         (setq tp (cdr tp)))
612       (pop tp)
613       (while (and tp
614                   (not (gnus-topic-goto-topic (caaar tp))))
615         (pop tp))
616       (if tp
617           (gnus-topic-forward-topic 1)
618         (gnus-topic-goto-missing-topic (caadr top))))
619     nil))
620
621 (defun gnus-topic-update-topic-line (topic-name &optional reads)
622   (let* ((top (gnus-topic-find-topology topic-name))
623          (type (cadr top))
624          (children (cddr top))
625          (entries (gnus-topic-find-groups
626                    (car type) (car gnus-group-list-mode)
627                    (cdr gnus-group-list-mode)))
628          (parent (gnus-topic-parent-topic topic-name))
629          (all-entries entries)
630          (unread 0)
631          old-unread entry new-unread)
632     (when (gnus-topic-goto-topic (car type))
633       ;; Tally all the groups that belong in this topic.
634       (if reads
635           (setq unread (- (gnus-group-topic-unread) reads))
636         (while children
637           (incf unread (gnus-topic-unread (caar (pop children)))))
638         (while (setq entry (pop entries))
639           (when (numberp (car entry))
640             (incf unread (car entry)))))
641       (setq old-unread (gnus-group-topic-unread))
642       ;; Insert the topic line.
643       (gnus-topic-insert-topic-line
644        (car type) (gnus-topic-visible-p)
645        (not (eq (nth 2 type) 'hidden))
646        (gnus-group-topic-level) all-entries unread)
647       (gnus-delete-line)
648       (forward-line -1)
649       (setq new-unread (gnus-group-topic-unread)))
650     (when parent
651       (forward-line -1)
652       (gnus-topic-update-topic-line
653        parent
654        (- (or old-unread 0) (or new-unread 0))))
655     unread))
656
657 (defun gnus-topic-group-indentation ()
658   (make-string
659    (* gnus-topic-indent-level
660       (or (save-excursion
661             (forward-line -1)
662             (gnus-topic-goto-topic (gnus-current-topic))
663             (gnus-group-topic-level))
664           0))
665    ? ))
666
667 ;;; Initialization
668
669 (gnus-add-shutdown 'gnus-topic-close 'gnus)
670
671 (defun gnus-topic-close ()
672   (setq gnus-topic-active-topology nil
673         gnus-topic-active-alist nil
674         gnus-topic-killed-topics nil
675         gnus-topology-checked-p nil))
676
677 (defun gnus-topic-check-topology ()
678   ;; The first time we set the topology to whatever we have
679   ;; gotten here, which can be rather random.
680   (unless gnus-topic-alist
681     (gnus-topic-init-alist))
682
683   (setq gnus-topology-checked-p t)
684   ;; Go through the topic alist and make sure that all topics
685   ;; are in the topic topology.
686   (let ((topics (gnus-topic-list))
687         (alist gnus-topic-alist)
688         changed)
689     (while alist
690       (unless (member (caar alist) topics)
691         (nconc gnus-topic-topology
692                (list (list (list (caar alist) 'visible))))
693         (setq changed t))
694       (setq alist (cdr alist)))
695     (when changed
696       (gnus-topic-enter-dribble))
697     ;; Conversely, go through the topology and make sure that all
698     ;; topologies have alists.
699     (while topics
700       (unless (assoc (car topics) gnus-topic-alist)
701         (push (list (car topics)) gnus-topic-alist))
702       (pop topics)))
703   ;; Go through all living groups and make sure that
704   ;; they belong to some topic.
705   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
706                                          gnus-topic-alist)))
707          (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
708          (newsrc (cdr gnus-newsrc-alist))
709          group)
710     (while newsrc
711       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
712         (setcdr entry (list group))
713         (setq entry (cdr entry)))))
714   ;; Go through all topics and make sure they contain only living groups.
715   (let ((alist gnus-topic-alist)
716         topic)
717     (while (setq topic (pop alist))
718       (while (cdr topic)
719         (if (and (cadr topic)
720                  (gnus-gethash (cadr topic) gnus-newsrc-hashtb))
721             (setq topic (cdr topic))
722           (setcdr topic (cddr topic)))))))
723
724 (defun gnus-topic-init-alist ()
725   "Initialize the topic structures."
726   (setq gnus-topic-topology
727         (cons (list "Gnus" 'visible)
728               (mapcar (lambda (topic)
729                         (list (list (car topic) 'visible)))
730                       '(("misc")))))
731   (setq gnus-topic-alist
732         (list (cons "misc"
733                     (mapcar (lambda (info) (gnus-info-group info))
734                             (cdr gnus-newsrc-alist)))
735               (list "Gnus")))
736   (gnus-topic-enter-dribble))
737
738 ;;; Maintenance
739
740 (defun gnus-topic-clean-alist ()
741   "Remove bogus groups from the topic alist."
742   (let ((topic-alist gnus-topic-alist)
743         result topic)
744     (unless gnus-killed-hashtb
745       (gnus-make-hashtable-from-killed))
746     (while (setq topic (pop topic-alist))
747       (let ((topic-name (pop topic))
748             group filtered-topic)
749         (while (setq group (pop topic))
750           (when (and (or (gnus-gethash group gnus-active-hashtb)
751                          (gnus-info-method (gnus-get-info group)))
752                      (not (gnus-gethash group gnus-killed-hashtb)))
753             (push group filtered-topic)))
754         (push (cons topic-name (nreverse filtered-topic)) result)))
755     (setq gnus-topic-alist (nreverse result))))
756
757 (defun gnus-topic-change-level (group level oldlevel &optional previous)
758   "Run when changing levels to enter/remove groups from topics."
759   (save-excursion
760     (set-buffer gnus-group-buffer)
761     (let ((buffer-read-only nil))
762       (unless gnus-topic-inhibit-change-level
763         (gnus-group-goto-group (or (car (nth 2 previous)) group))
764         (when (and gnus-topic-mode
765                    gnus-topic-alist
766                    (not gnus-topic-inhibit-change-level))
767           ;; Remove the group from the topics.
768           (if (and (< oldlevel gnus-level-zombie)
769                    (>= level gnus-level-zombie))
770               (let ((alist gnus-topic-alist))
771                 (while (gnus-group-goto-group group)
772                   (gnus-delete-line))
773                 (while alist
774                   (when (member group (car alist))
775                     (setcdr (car alist) (delete group (cdar alist))))
776                   (pop alist)))
777             ;; If the group is subscribed we enter it into the topics.
778             (when (and (< level gnus-level-zombie)
779                        (>= oldlevel gnus-level-zombie))
780               (let* ((prev (gnus-group-group-name))
781                      (gnus-topic-inhibit-change-level t)
782                      (gnus-group-indentation
783                       (make-string
784                        (* gnus-topic-indent-level
785                           (or (save-excursion
786                                 (gnus-topic-goto-topic (gnus-current-topic))
787                                 (gnus-group-topic-level))
788                               0))
789                        ? ))
790                      (yanked (list group))
791                      alist talist end)
792                 ;; Then we enter the yanked groups into the topics they belong
793                 ;; to.
794                 (when (setq alist (assoc (save-excursion
795                                            (forward-line -1)
796                                            (or
797                                             (gnus-current-topic)
798                                             (caar gnus-topic-topology)))
799                                          gnus-topic-alist))
800                   (setq talist alist)
801                   (when (stringp yanked)
802                     (setq yanked (list yanked)))
803                   (if (not prev)
804                       (nconc alist yanked)
805                     (if (not (cdr alist))
806                         (setcdr alist (nconc yanked (cdr alist)))
807                       (while (and (not end) (cdr alist))
808                         (when (equal (cadr alist) prev)
809                           (setcdr alist (nconc yanked (cdr alist)))
810                           (setq end t))
811                         (setq alist (cdr alist)))
812                       (unless end
813                         (nconc talist yanked))))))
814               (gnus-topic-update-topic))))))))
815
816 (defun gnus-topic-goto-next-group (group props)
817   "Go to group or the next group after group."
818   (if (not group)
819       (if (not (memq 'gnus-topic props))
820           (goto-char (point-max))
821         (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props)))))
822     (if (gnus-group-goto-group group)
823         t
824       ;; The group is no longer visible.
825       (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
826              (after (cdr (member group (cdr list)))))
827         ;; First try to put point on a group after the current one.
828         (while (and after
829                     (not (gnus-group-goto-group (car after))))
830           (setq after (cdr after)))
831         ;; Then try to put point on a group before point.
832         (unless after
833           (setq after (cdr (member group (reverse (cdr list)))))
834           (while (and after
835                       (not (gnus-group-goto-group (car after))))
836             (setq after (cdr after))))
837         ;; Finally, just put point on the topic.
838         (if (not (car list))
839             (goto-char (point-min))
840           (unless after
841             (gnus-topic-goto-topic (car list))
842             (setq after nil)))
843         t))))
844
845 ;;; Topic-active functions
846
847 (defun gnus-topic-grok-active (&optional force)
848   "Parse all active groups and create topic structures for them."
849   ;; First we make sure that we have really read the active file.
850   (when (or force
851             (not gnus-topic-active-alist))
852     (let (groups)
853       ;; Get a list of all groups available.
854       (mapatoms (lambda (g) (when (symbol-value g)
855                               (push (symbol-name g) groups)))
856                 gnus-active-hashtb)
857       (setq groups (sort groups 'string<))
858       ;; Init the variables.
859       (setq gnus-topic-active-topology (list (list "" 'visible)))
860       (setq gnus-topic-active-alist nil)
861       ;; Descend the top-level hierarchy.
862       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
863       ;; Set the top-level topic names to something nice.
864       (setcar (car gnus-topic-active-topology) "Gnus active")
865       (setcar (car gnus-topic-active-alist) "Gnus active"))))
866
867 (defun gnus-topic-grok-active-1 (topology groups)
868   (let* ((name (caar topology))
869          (prefix (concat "^" (regexp-quote name)))
870          tgroups ntopology group)
871     (while (and groups
872                 (string-match prefix (setq group (car groups))))
873       (if (not (string-match "\\." group (match-end 0)))
874           ;; There are no further hierarchies here, so we just
875           ;; enter this group into the list belonging to this
876           ;; topic.
877           (push (pop groups) tgroups)
878         ;; New sub-hierarchy, so we add it to the topology.
879         (nconc topology (list (setq ntopology
880                                     (list (list (substring
881                                                  group 0 (match-end 0))
882                                                 'invisible)))))
883         ;; Descend the hierarchy.
884         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
885     ;; We remove the trailing "." from the topic name.
886     (setq name
887           (if (string-match "\\.$" name)
888               (substring name 0 (match-beginning 0))
889             name))
890     ;; Add this topic and its groups to the topic alist.
891     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
892     (setcar (car topology) name)
893     ;; We return the rest of the groups that didn't belong
894     ;; to this topic.
895     groups))
896
897 ;;; Topic mode, commands and keymap.
898
899 (defvar gnus-topic-mode-map nil)
900 (defvar gnus-group-topic-map nil)
901
902 (unless gnus-topic-mode-map
903   (setq gnus-topic-mode-map (make-sparse-keymap))
904
905   ;; Override certain group mode keys.
906   (gnus-define-keys gnus-topic-mode-map
907     "=" gnus-topic-select-group
908     "\r" gnus-topic-select-group
909     " " gnus-topic-read-group
910     "\C-k" gnus-topic-kill-group
911     "\C-y" gnus-topic-yank-group
912     "\M-g" gnus-topic-get-new-news-this-topic
913     "AT" gnus-topic-list-active
914     "Gp" gnus-topic-edit-parameters
915     "#" gnus-topic-mark-topic
916     "\M-#" gnus-topic-unmark-topic
917     [tab] gnus-topic-indent
918     [(meta tab)] gnus-topic-unindent
919     "\C-i" gnus-topic-indent
920     "\M-\C-i" gnus-topic-unindent
921     gnus-mouse-2 gnus-mouse-pick-topic)
922
923   ;; Define a new submap.
924   (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
925     "#" gnus-topic-mark-topic
926     "\M-#" gnus-topic-unmark-topic
927     "n" gnus-topic-create-topic
928     "m" gnus-topic-move-group
929     "D" gnus-topic-remove-group
930     "c" gnus-topic-copy-group
931     "h" gnus-topic-hide-topic
932     "s" gnus-topic-show-topic
933     "M" gnus-topic-move-matching
934     "C" gnus-topic-copy-matching
935     "\C-i" gnus-topic-indent
936     [tab] gnus-topic-indent
937     "r" gnus-topic-rename
938     "\177" gnus-topic-delete
939     [delete] gnus-topic-delete
940     "H" gnus-topic-toggle-display-empty-topics)
941
942   (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
943     "s" gnus-topic-sort-groups
944     "a" gnus-topic-sort-groups-by-alphabet
945     "u" gnus-topic-sort-groups-by-unread
946     "l" gnus-topic-sort-groups-by-level
947     "v" gnus-topic-sort-groups-by-score
948     "r" gnus-topic-sort-groups-by-rank
949     "m" gnus-topic-sort-groups-by-method))
950
951 (defun gnus-topic-make-menu-bar ()
952   (unless (boundp 'gnus-topic-menu)
953     (easy-menu-define
954      gnus-topic-menu gnus-topic-mode-map ""
955      '("Topics"
956        ["Toggle topics" gnus-topic-mode t]
957        ("Groups"
958         ["Copy" gnus-topic-copy-group t]
959         ["Move" gnus-topic-move-group t]
960         ["Remove" gnus-topic-remove-group t]
961         ["Copy matching" gnus-topic-copy-matching t]
962         ["Move matching" gnus-topic-move-matching t])
963        ("Topics"
964         ["Show" gnus-topic-show-topic t]
965         ["Hide" gnus-topic-hide-topic t]
966         ["Delete" gnus-topic-delete t]
967         ["Rename" gnus-topic-rename t]
968         ["Create" gnus-topic-create-topic t]
969         ["Mark" gnus-topic-mark-topic t]
970         ["Indent" gnus-topic-indent t]
971         ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
972         ["Edit parameters" gnus-topic-edit-parameters t])
973        ["List active" gnus-topic-list-active t]))))
974
975 (defun gnus-topic-mode (&optional arg redisplay)
976   "Minor mode for topicsifying Gnus group buffers."
977   (interactive (list current-prefix-arg t))
978   (when (eq major-mode 'gnus-group-mode)
979     (make-local-variable 'gnus-topic-mode)
980     (setq gnus-topic-mode
981           (if (null arg) (not gnus-topic-mode)
982             (> (prefix-numeric-value arg) 0)))
983     ;; Infest Gnus with topics.
984      (if (not gnus-topic-mode)
985         (setq gnus-goto-missing-group-function nil)
986       (when (gnus-visual-p 'topic-menu 'menu)
987         (gnus-topic-make-menu-bar))
988       (gnus-set-format 'topic t)
989       (gnus-add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
990       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
991       (set (make-local-variable 'gnus-group-prepare-function)
992            'gnus-group-prepare-topics)
993       (set (make-local-variable 'gnus-group-get-parameter-function)
994            'gnus-group-topic-parameters)
995       (set (make-local-variable 'gnus-group-goto-next-group-function)
996            'gnus-topic-goto-next-group)
997       (set (make-local-variable 'gnus-group-indentation-function)
998            'gnus-topic-group-indentation)
999       (set (make-local-variable 'gnus-group-update-group-function)
1000            'gnus-topic-update-topics-containing-group)
1001       (set (make-local-variable 'gnus-group-sort-alist-function)
1002            'gnus-group-sort-topic)
1003       (setq gnus-group-change-level-function 'gnus-topic-change-level)
1004       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
1005       (make-local-hook 'gnus-check-bogus-groups-hook)
1006       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1007       (setq gnus-topology-checked-p nil)
1008       ;; We check the topology.
1009       (when gnus-newsrc-alist
1010         (gnus-topic-check-topology))
1011       (gnus-run-hooks 'gnus-topic-mode-hook))
1012     ;; Remove topic infestation.
1013     (unless gnus-topic-mode
1014       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
1015       (remove-hook 'gnus-group-change-level-function
1016                    'gnus-topic-change-level)
1017       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
1018       (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
1019       (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
1020     (when redisplay
1021       (gnus-group-list-groups))))
1022
1023 (defun gnus-topic-select-group (&optional all)
1024   "Select this newsgroup.
1025 No article is selected automatically.
1026 If ALL is non-nil, already read articles become readable.
1027 If ALL is a number, fetch this number of articles.
1028
1029 If performed over a topic line, toggle folding the topic."
1030   (interactive "P")
1031   (if (gnus-group-topic-p)
1032       (let ((gnus-group-list-mode
1033              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1034         (gnus-topic-fold all))
1035     (gnus-group-select-group all)))
1036
1037 (defun gnus-mouse-pick-topic (e)
1038   "Select the group or topic under the mouse pointer."
1039   (interactive "e")
1040   (mouse-set-point e)
1041   (gnus-topic-read-group nil))
1042
1043 (defun gnus-topic-read-group (&optional all no-article group)
1044   "Read news in this newsgroup.
1045 If the prefix argument ALL is non-nil, already read articles become
1046 readable.  IF ALL is a number, fetch this number of articles.  If the
1047 optional argument NO-ARTICLE is non-nil, no article will be
1048 auto-selected upon group entry.  If GROUP is non-nil, fetch that
1049 group.
1050
1051 If performed over a topic line, toggle folding the topic."
1052   (interactive "P")
1053   (if (gnus-group-topic-p)
1054       (let ((gnus-group-list-mode
1055              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
1056         (gnus-topic-fold all))
1057     (gnus-group-read-group all no-article group)))
1058
1059 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
1060   "Create a new TOPIC under PARENT.
1061 When used interactively, PARENT will be the topic under point."
1062   (interactive
1063    (list
1064     (read-string "New topic: ")
1065     (gnus-current-topic)))
1066   ;; Check whether this topic already exists.
1067   (when (gnus-topic-find-topology topic)
1068     (error "Topic already exists"))
1069   (unless parent
1070     (setq parent (caar gnus-topic-topology)))
1071   (let ((top (cdr (gnus-topic-find-topology parent)))
1072         (full-topic (or full-topic `((,topic visible)))))
1073     (unless top
1074       (error "No such parent topic: %s" parent))
1075     (if previous
1076         (progn
1077           (while (and (cdr top)
1078                       (not (equal (caaadr top) previous)))
1079             (setq top (cdr top)))
1080           (setcdr top (cons full-topic (cdr top))))
1081       (nconc top (list full-topic)))
1082     (unless (assoc topic gnus-topic-alist)
1083       (push (list topic) gnus-topic-alist)))
1084   (gnus-topic-enter-dribble)
1085   (gnus-group-list-groups)
1086   (gnus-topic-goto-topic topic))
1087
1088 (defun gnus-topic-move-group (n topic &optional copyp)
1089   "Move the next N groups to TOPIC.
1090 If COPYP, copy the groups instead."
1091   (interactive
1092    (list current-prefix-arg
1093          (completing-read "Move to topic: " gnus-topic-alist nil t)))
1094   (let ((groups (gnus-group-process-prefix n))
1095         (topicl (assoc topic gnus-topic-alist))
1096         (start-group (progn (forward-line 1) (gnus-group-group-name)))
1097         (start-topic (gnus-group-topic-name))
1098         entry)
1099     (mapcar
1100      (lambda (g)
1101        (gnus-group-remove-mark g)
1102        (when (and
1103               (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
1104               (not copyp))
1105          (setcdr entry (gnus-delete-first g (cdr entry))))
1106        (nconc topicl (list g)))
1107      groups)
1108     (gnus-topic-enter-dribble)
1109     (if start-group
1110         (gnus-group-goto-group start-group)
1111       (gnus-topic-goto-topic start-topic))
1112     (gnus-group-list-groups)))
1113
1114 (defun gnus-topic-remove-group (&optional arg)
1115   "Remove the current group from the topic."
1116   (interactive "P")
1117   (gnus-group-iterate arg
1118     (lambda (group)
1119       (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
1120             (buffer-read-only nil))
1121         (when (and topicl group)
1122           (gnus-delete-line)
1123           (gnus-delete-first group topicl))
1124         (gnus-topic-update-topic)
1125         (gnus-group-position-point)))))
1126
1127 (defun gnus-topic-copy-group (n topic)
1128   "Copy the current group to a topic."
1129   (interactive
1130    (list current-prefix-arg
1131          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
1132   (gnus-topic-move-group n topic t))
1133
1134 (defun gnus-topic-kill-group (&optional n discard)
1135   "Kill the next N groups."
1136   (interactive "P")
1137   (if (gnus-group-topic-p)
1138       (let ((topic (gnus-group-topic-name)))
1139         (push (cons
1140                (gnus-topic-find-topology topic)
1141                (assoc topic gnus-topic-alist))
1142               gnus-topic-killed-topics)
1143         (gnus-topic-remove-topic nil t)
1144         (gnus-topic-find-topology topic nil nil gnus-topic-topology)
1145         (gnus-topic-enter-dribble))
1146     (gnus-group-kill-group n discard)
1147     (gnus-topic-update-topic)))
1148
1149 (defun gnus-topic-yank-group (&optional arg)
1150   "Yank the last topic."
1151   (interactive "p")
1152   (if gnus-topic-killed-topics
1153       (let* ((previous
1154               (or (gnus-group-topic-name)
1155                   (gnus-topic-next-topic (gnus-current-topic))))
1156              (data (pop gnus-topic-killed-topics))
1157              (alist (cdr data))
1158              (item (cdar data)))
1159         (push alist gnus-topic-alist)
1160         (gnus-topic-create-topic
1161          (caar item) (gnus-topic-parent-topic previous) previous
1162          item)
1163         (gnus-topic-enter-dribble)
1164         (gnus-topic-goto-topic (caar item)))
1165     (let* ((prev (gnus-group-group-name))
1166            (gnus-topic-inhibit-change-level t)
1167            (gnus-group-indentation
1168             (make-string
1169              (* gnus-topic-indent-level
1170                 (or (save-excursion
1171                       (gnus-topic-goto-topic (gnus-current-topic))
1172                       (gnus-group-topic-level))
1173                     0))
1174              ? ))
1175            yanked alist)
1176       ;; We first yank the groups the normal way...
1177       (setq yanked (gnus-group-yank-group arg))
1178       ;; Then we enter the yanked groups into the topics they belong
1179       ;; to.
1180       (setq alist (assoc (save-excursion
1181                            (forward-line -1)
1182                            (gnus-current-topic))
1183                          gnus-topic-alist))
1184       (when (stringp yanked)
1185         (setq yanked (list yanked)))
1186       (if (not prev)
1187           (nconc alist yanked)
1188         (if (not (cdr alist))
1189             (setcdr alist (nconc yanked (cdr alist)))
1190           (while (cdr alist)
1191             (when (equal (cadr alist) prev)
1192               (setcdr alist (nconc yanked (cdr alist)))
1193               (setq alist nil))
1194             (setq alist (cdr alist))))))
1195     (gnus-topic-update-topic)))
1196
1197 (defun gnus-topic-hide-topic ()
1198   "Hide the current topic."
1199   (interactive)
1200   (when (gnus-current-topic)
1201     (gnus-topic-goto-topic (gnus-current-topic))
1202     (gnus-topic-remove-topic nil nil 'hidden)))
1203
1204 (defun gnus-topic-show-topic ()
1205   "Show the hidden topic."
1206   (interactive)
1207   (when (gnus-group-topic-p)
1208     (gnus-topic-remove-topic t nil 'shown)))
1209
1210 (defun gnus-topic-mark-topic (topic &optional unmark)
1211   "Mark all groups in the topic with the process mark."
1212   (interactive (list (gnus-group-topic-name)))
1213   (if (not topic)
1214       (call-interactively 'gnus-group-mark-group)
1215     (save-excursion
1216       (let ((groups (gnus-topic-find-groups topic gnus-level-killed t)))
1217         (while groups
1218           (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
1219                    (gnus-info-group (nth 2 (pop groups)))))))))
1220
1221 (defun gnus-topic-unmark-topic (topic &optional unmark)
1222   "Remove the process mark from all groups in the topic."
1223   (interactive (list (gnus-group-topic-name)))
1224   (if (not topic)
1225       (call-interactively 'gnus-group-unmark-group)
1226     (gnus-topic-mark-topic topic t)))
1227
1228 (defun gnus-topic-get-new-news-this-topic (&optional n)
1229   "Check for new news in the current topic."
1230   (interactive "P")
1231   (if (not (gnus-group-topic-p))
1232       (gnus-group-get-new-news-this-group n)
1233     (gnus-topic-mark-topic (gnus-group-topic-name))
1234     (gnus-group-get-new-news-this-group)))
1235
1236 (defun gnus-topic-move-matching (regexp topic &optional copyp)
1237   "Move all groups that match REGEXP to some topic."
1238   (interactive
1239    (let (topic)
1240      (nreverse
1241       (list
1242        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
1243        (read-string (format "Move to %s (regexp): " topic))))))
1244   (gnus-group-mark-regexp regexp)
1245   (gnus-topic-move-group nil topic copyp))
1246
1247 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
1248   "Copy all groups that match REGEXP to some topic."
1249   (interactive
1250    (let (topic)
1251      (nreverse
1252       (list
1253        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
1254        (read-string (format "Copy to %s (regexp): " topic))))))
1255   (gnus-topic-move-matching regexp topic t))
1256
1257 (defun gnus-topic-delete (topic)
1258   "Delete a topic."
1259   (interactive (list (gnus-group-topic-name)))
1260   (unless topic
1261     (error "No topic to be deleted"))
1262   (let ((entry (assoc topic gnus-topic-alist))
1263         (buffer-read-only nil))
1264     (when (cdr entry)
1265       (error "Topic not empty"))
1266     ;; Delete if visible.
1267     (when (gnus-topic-goto-topic topic)
1268       (gnus-delete-line))
1269     ;; Remove from alist.
1270     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1271     ;; Remove from topology.
1272     (gnus-topic-find-topology topic nil nil 'delete)
1273     (gnus-dribble-touch)))
1274
1275 (defun gnus-topic-rename (old-name new-name)
1276   "Rename a topic."
1277   (interactive
1278    (let ((topic (gnus-current-topic)))
1279      (list topic
1280            (read-string (format "Rename %s to: " topic)))))
1281   ;; Check whether the new name exists.
1282   (when (gnus-topic-find-topology new-name)
1283     (error "Topic '%s' already exists" new-name))
1284   ;; "nil" is an invalid name, for reasons I'd rather not go
1285   ;; into here.  Trust me.
1286   (when (equal new-name "nil")
1287     (error "Invalid name: %s" nil))
1288   ;; Do the renaming.
1289   (let ((top (gnus-topic-find-topology old-name))
1290         (entry (assoc old-name gnus-topic-alist)))
1291     (when top
1292       (setcar (cadr top) new-name))
1293     (when entry
1294       (setcar entry new-name))
1295     (forward-line -1)
1296     (gnus-dribble-touch)
1297     (gnus-group-list-groups)
1298     (forward-line 1)))
1299
1300 (defun gnus-topic-indent (&optional unindent)
1301   "Indent a topic -- make it a sub-topic of the previous topic.
1302 If UNINDENT, remove an indentation."
1303   (interactive "P")
1304   (if unindent
1305       (gnus-topic-unindent)
1306     (let* ((topic (gnus-current-topic))
1307            (parent (gnus-topic-previous-topic topic))
1308            (buffer-read-only nil))
1309       (unless parent
1310         (error "Nothing to indent %s into" topic))
1311       (when topic
1312         (gnus-topic-goto-topic topic)
1313         (gnus-topic-kill-group)
1314         (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1315         (gnus-topic-create-topic
1316          topic parent nil (cdaar gnus-topic-killed-topics))
1317         (pop gnus-topic-killed-topics)
1318         (or (gnus-topic-goto-topic topic)
1319             (gnus-topic-goto-topic parent))))))
1320
1321 (defun gnus-topic-unindent ()
1322   "Unindent a topic."
1323   (interactive)
1324   (let* ((topic (gnus-current-topic))
1325          (parent (gnus-topic-parent-topic topic))
1326          (grandparent (gnus-topic-parent-topic parent)))
1327     (unless grandparent
1328       (error "Nothing to indent %s into" topic))
1329     (when topic
1330       (gnus-topic-goto-topic topic)
1331       (gnus-topic-kill-group)
1332       (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
1333       (gnus-topic-create-topic
1334        topic grandparent (gnus-topic-next-topic parent)
1335        (cdaar gnus-topic-killed-topics))
1336       (pop gnus-topic-killed-topics)
1337       (gnus-topic-goto-topic topic))))
1338
1339 (defun gnus-topic-list-active (&optional force)
1340   "List all groups that Gnus knows about in a topicsified fashion.
1341 If FORCE, always re-read the active file."
1342   (interactive "P")
1343   (when force
1344     (gnus-get-killed-groups))
1345   (gnus-topic-grok-active force)
1346   (let ((gnus-topic-topology gnus-topic-active-topology)
1347         (gnus-topic-alist gnus-topic-active-alist)
1348         gnus-killed-list gnus-zombie-list)
1349     (gnus-group-list-groups gnus-level-killed nil 1)))
1350
1351 (defun gnus-topic-toggle-display-empty-topics ()
1352   "Show/hide topics that have no unread articles."
1353   (interactive)
1354   (setq gnus-topic-display-empty-topics
1355         (not gnus-topic-display-empty-topics))
1356   (gnus-group-list-groups)
1357   (message "%s empty topics"
1358            (if gnus-topic-display-empty-topics
1359                "Showing" "Hiding")))
1360
1361 ;;; Topic sorting functions
1362
1363 (defun gnus-topic-edit-parameters (group)
1364   "Edit the group parameters of GROUP.
1365 If performed on a topic, edit the topic parameters instead."
1366   (interactive (list (gnus-group-group-name)))
1367   (if group
1368       (gnus-group-edit-group-parameters group)
1369     (if (not (gnus-group-topic-p))
1370         (error "Nothing to edit on the current line")
1371       (let ((topic (gnus-group-topic-name)))
1372         (gnus-edit-form
1373          (gnus-topic-parameters topic)
1374          (format "Editing the topic parameters for `%s'."
1375                  (or group topic))
1376          `(lambda (form)
1377             (gnus-topic-set-parameters ,topic form)))))))
1378
1379 (defun gnus-group-sort-topic (func reverse)
1380   "Sort groups in the topics according to FUNC and REVERSE."
1381   (let ((alist gnus-topic-alist))
1382     (while alist
1383       ;; !!!Sometimes nil elements sneak into the alist,
1384       ;; for some reason or other.
1385       (setcar alist (delq nil (car alist)))
1386       (setcar alist (delete "dummy.group" (car alist)))
1387       (gnus-topic-sort-topic (pop alist) func reverse))))
1388
1389 (defun gnus-topic-sort-topic (topic func reverse)
1390   ;; Each topic only lists the name of the group, while
1391   ;; the sort predicates expect group infos as inputs.
1392   ;; So we first transform the group names into infos,
1393   ;; then sort, and then transform back into group names.
1394   (setcdr
1395    topic
1396    (mapcar
1397     (lambda (info) (gnus-info-group info))
1398     (sort
1399      (mapcar
1400       (lambda (group) (gnus-get-info group))
1401       (cdr topic))
1402      func)))
1403   ;; Do the reversal, if necessary.
1404   (when reverse
1405     (setcdr topic (nreverse (cdr topic)))))
1406
1407 (defun gnus-topic-sort-groups (func &optional reverse)
1408   "Sort the current topic according to FUNC.
1409 If REVERSE, reverse the sorting order."
1410   (interactive (list gnus-group-sort-function current-prefix-arg))
1411   (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
1412     (gnus-topic-sort-topic
1413      topic (gnus-make-sort-function func) reverse)
1414     (gnus-group-list-groups)))
1415
1416 (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
1417   "Sort the current topic alphabetically by group name.
1418 If REVERSE, sort in reverse order."
1419   (interactive "P")
1420   (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
1421
1422 (defun gnus-topic-sort-groups-by-unread (&optional reverse)
1423   "Sort the current topic by number of unread articles.
1424 If REVERSE, sort in reverse order."
1425   (interactive "P")
1426   (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
1427
1428 (defun gnus-topic-sort-groups-by-level (&optional reverse)
1429   "Sort the current topic by group level.
1430 If REVERSE, sort in reverse order."
1431   (interactive "P")
1432   (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
1433
1434 (defun gnus-topic-sort-groups-by-score (&optional reverse)
1435   "Sort the current topic by group score.
1436 If REVERSE, sort in reverse order."
1437   (interactive "P")
1438   (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
1439
1440 (defun gnus-topic-sort-groups-by-rank (&optional reverse)
1441   "Sort the current topic by group rank.
1442 If REVERSE, sort in reverse order."
1443   (interactive "P")
1444   (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
1445
1446 (defun gnus-topic-sort-groups-by-method (&optional reverse)
1447   "Sort the current topic alphabetically by backend name.
1448 If REVERSE, sort in reverse order."
1449   (interactive "P")
1450   (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
1451
1452 (provide 'gnus-topic)
1453
1454 ;;; gnus-topic.el ends here