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