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