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