*** empty log message ***
[gnus] / lisp / gnus-topic.el
1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Ilja Weis <kult@uni-paderborn.de>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'gnus)
30 (eval-when-compile (require 'cl))
31
32 (defvar gnus-topic-mode nil
33   "Minor mode for Gnus group buffers.")
34
35 (defvar gnus-topic-mode-hook nil
36   "Hook run in topic mode buffers.")
37
38 (defvar gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
39   "Format of topic lines.
40 It works along the same lines as a normal formatting string,
41 with some simple extensions.
42
43 %i  Indentation based on topic level.
44 %n  Topic name.
45 %v  Nothing if the topic is visible, \"...\" otherwise.
46 %g  Number of groups in the topic.
47 %a  Number of unread articles in the groups in the topic.
48 %A  Number of unread articles in the groups in the topic and its subtopics.
49 ")
50
51 (defvar gnus-topic-indent-level 2
52   "*How much each subtopic should be indented.")
53
54 ;; Internal variables.
55
56 (defvar gnus-topic-active-topology nil)
57 (defvar gnus-topic-active-alist nil)
58
59 (defvar gnus-topology-checked-p nil
60   "Whether the topology has been checked in this session.")
61
62 (defvar gnus-topic-killed-topics nil)
63 (defvar gnus-topic-inhibit-change-level nil)
64 (defvar gnus-topic-tallied-groups nil)
65
66 (defconst gnus-topic-line-format-alist
67   `((?n name ?s)
68     (?v visible ?s)
69     (?i indentation ?s)
70     (?g number-of-groups ?d)
71     (?a (gnus-topic-articles-in-topic entries) ?d)
72     (?A total-number-of-articles ?d)
73     (?l level ?d)))
74
75 (defvar gnus-topic-line-format-spec nil)
76
77 ;; Functions.
78
79 (defun gnus-group-topic-name ()
80   "The name of the topic on the current line."
81   (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
82     (and topic (symbol-name topic))))
83
84 (defun gnus-group-topic-level ()
85   "The level of the topic on the current line."
86   (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
87
88 (defun gnus-group-topic-unread ()
89   "The number of unread articles in topic on the current line."
90   (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
91
92 (defun gnus-topic-unread (topic)
93   "Return the number of unread articles in TOPIC."
94   (or (save-excursion
95         (and (gnus-topic-goto-topic topic)
96              (gnus-group-topic-unread)))
97       0))
98
99 (defun gnus-topic-init-alist ()
100   "Initialize the topic structures."
101   (setq gnus-topic-topology
102         (cons (list "Gnus" 'visible)
103               (mapcar (lambda (topic)
104                         (list (list (car topic) 'visible)))
105                       '(("misc")))))
106   (setq gnus-topic-alist
107         (list (cons "misc"
108                     (mapcar (lambda (info) (gnus-info-group info))
109                             (cdr gnus-newsrc-alist)))
110               (list "Gnus")))
111   (gnus-topic-enter-dribble))
112
113 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
114   "List all newsgroups with unread articles of level LEVEL or lower, and
115 use the `gnus-group-topics' to sort the groups.
116 If ALL is non-nil, list groups that have no unread articles.
117 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
118   (set-buffer gnus-group-buffer)
119   (let ((buffer-read-only nil)
120         (lowest (or lowest 1)))
121
122     (setq gnus-topic-tallied-groups nil)
123
124     (when (or (not gnus-topic-alist)
125               (not gnus-topology-checked-p))
126       (gnus-topic-check-topology))
127
128     (unless list-topic 
129       (erase-buffer))
130     
131     ;; List dead groups?
132     (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
133       (gnus-group-prepare-flat-list-dead 
134        (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
135        gnus-level-zombie ?Z
136        regexp))
137     
138     (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
139       (gnus-group-prepare-flat-list-dead 
140        (setq gnus-killed-list (sort gnus-killed-list 'string<))
141        gnus-level-killed ?K
142        regexp))
143
144     ;; Use topics.
145     (when (< lowest gnus-level-zombie)
146       (if list-topic
147           (let ((top (gnus-topic-find-topology list-topic)))
148             (gnus-topic-prepare-topic (cdr top) (car top)
149                                       (or topic-level level) all))
150         (gnus-topic-prepare-topic gnus-topic-topology 0
151                                   (or topic-level level) all))))
152
153   (gnus-group-set-mode-line)
154   (setq gnus-group-list-mode (cons level all))
155   (run-hooks 'gnus-group-prepare-hook))
156
157 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
158   "Insert TOPIC into the group buffer.
159 If SILENT, don't insert anything.  Return the number of unread
160 articles in the topic and its subtopics."
161   (let* ((type (pop topicl))
162          (entries (gnus-topic-find-groups (car type) list-level all))
163          (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
164          (gnus-group-indentation 
165           (make-string (* gnus-topic-indent-level level) ? ))
166          (beg (progn (beginning-of-line) (point)))
167          (topicl (reverse topicl))
168          (all-entries entries)
169          (unread 0)
170          (topic (car type))
171          info entry end active)
172     ;; Insert any sub-topics.
173     (while topicl
174       (incf unread
175             (gnus-topic-prepare-topic 
176              (pop topicl) (1+ level) list-level all
177              (not visiblep))))
178     (setq end (point))
179     (goto-char beg)
180     ;; Insert all the groups that belong in this topic.
181     (while (setq entry (pop entries))
182       (when visiblep 
183         (if (stringp entry)
184             ;; Dead groups.
185             (gnus-group-insert-group-line
186              entry (if (member entry gnus-zombie-list) 8 9)
187              nil (- (1+ (cdr (setq active (gnus-active entry))))
188                     (car active)) nil)
189           ;; Living groups.
190           (when (setq info (nth 2 entry))
191             (gnus-group-insert-group-line 
192              (gnus-info-group info)
193              (gnus-info-level info) (gnus-info-marks info) 
194              (car entry) (gnus-info-method info)))))
195       (when (and (listp entry)
196                  (numberp (car entry))
197                  (not (member (gnus-info-group (setq info (nth 2 entry)))
198                               gnus-topic-tallied-groups)))
199         (push (gnus-info-group info) gnus-topic-tallied-groups)
200         (incf unread (car entry))))
201     (goto-char beg)
202     ;; Insert the topic line.
203     (unless silent
204       (gnus-extent-start-open (point))
205       (gnus-topic-insert-topic-line 
206        (car type) visiblep
207        (not (eq (nth 2 type) 'hidden))
208        level all-entries unread))
209     (goto-char end)
210     unread))
211
212 (defun gnus-topic-find-groups (topic &optional level all)
213   "Return entries for all visible groups in TOPIC."
214   (let ((groups (cdr (assoc topic gnus-topic-alist)))
215         info clevel unread group lowest params visible-groups entry active)
216     (setq lowest (or lowest 1))
217     (setq level (or level 7))
218     ;; We go through the newsrc to look for matches.
219     (while groups
220       (setq entry (gnus-gethash (setq group (pop groups)) gnus-newsrc-hashtb)
221             info (nth 2 entry)
222             params (gnus-info-params info)
223             active (gnus-active group)
224             unread (or (car entry)
225                        (and (not (equal group "dummy.group"))
226                             active
227                             (- (1+ (cdr active)) (car active))))
228             clevel (or (gnus-info-level info)
229                        (if (member group gnus-zombie-list) 8 9)))
230       (and 
231        unread                           ; nil means that the group is dead.
232        (<= clevel level) 
233        (>= clevel lowest)               ; Is inside the level we want.
234        (or all
235            (if (eq unread t)
236                gnus-group-list-inactive-groups
237              (> unread 0))
238            (and gnus-list-groups-with-ticked-articles
239                 (cdr (assq 'tick (gnus-info-marks info))))
240                                         ; Has right readedness.
241            ;; Check for permanent visibility.
242            (and gnus-permanently-visible-groups
243                 (string-match gnus-permanently-visible-groups group))
244            (memq 'visible params)
245            (cdr (assq 'visible params)))
246        ;; Add this group to the list of visible groups.
247        (push (or entry group) visible-groups)))
248     (nreverse visible-groups)))
249
250 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
251   "Remove the current topic."
252   (let ((topic (gnus-group-topic-name))
253         (level (gnus-group-topic-level))
254         (beg (progn (beginning-of-line) (point)))
255         buffer-read-only)
256     (when topic
257       (while (and (zerop (forward-line 1))
258                   (> (or (gnus-group-topic-level) (1+ level)) level)))
259       (delete-region beg (point))
260       (setcar (cdadr (gnus-topic-find-topology topic))
261               (if insert 'visible 'invisible))
262       (when hide
263         (setcdr (cdadr (gnus-topic-find-topology topic))
264                 (list hide)))
265       (unless total-remove
266         (gnus-topic-insert-topic topic in-level)))))
267
268 (defun gnus-topic-insert-topic (topic &optional level)
269   "Insert TOPIC."
270   (gnus-group-prepare-topics 
271    (car gnus-group-list-mode) (cdr gnus-group-list-mode)
272    nil nil topic level))
273   
274 (defun gnus-topic-fold (&optional insert)
275   "Remove/insert the current topic."
276   (let ((topic (gnus-group-topic-name))) 
277     (when topic
278       (save-excursion
279         (if (not (gnus-group-active-topic-p))
280             (gnus-topic-remove-topic
281              (or insert (not (gnus-topic-visible-p))))
282           (let ((gnus-topic-topology gnus-topic-active-topology)
283                 (gnus-topic-alist gnus-topic-active-alist)
284                 (gnus-group-list-mode (cons 5 t)))
285             (gnus-topic-remove-topic
286              (or insert (not (gnus-topic-visible-p))) nil nil 9)))))))
287
288 (defun gnus-group-topic-p ()
289   "Return non-nil if the current line is a topic."
290   (gnus-group-topic-name))
291
292 (defun gnus-topic-visible-p ()
293   "Return non-nil if the current topic is visible."
294   (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
295
296 (defun gnus-topic-insert-topic-line (name visiblep shownp level entries 
297                                           &optional unread)
298   (let* ((visible (if visiblep "" "..."))
299          (indentation (make-string (* gnus-topic-indent-level level) ? ))
300          (total-number-of-articles unread)
301          (number-of-groups (length entries))
302          (active-topic (eq gnus-topic-alist gnus-topic-active-alist)))
303     (beginning-of-line)
304     ;; Insert the text.
305     (gnus-add-text-properties 
306      (point)
307      (prog1 (1+ (point)) 
308        (eval gnus-topic-line-format-spec)
309        (gnus-topic-remove-excess-properties)1)
310      (list 'gnus-topic (intern name)
311            'gnus-topic-level level
312            'gnus-topic-unread unread
313            'gnus-active active-topic
314            'gnus-topic-visible visiblep))))
315
316 (defun gnus-topic-previous-topic (topic)
317   "Return the previous topic on the same level as TOPIC."
318   (let ((top (cddr (gnus-topic-find-topology
319                     (gnus-topic-parent-topic topic)))))
320     (unless (equal topic (caaar top))
321       (while (and top (not (equal (caaadr top) topic)))
322         (setq top (cdr top)))
323       (caaar top))))
324
325 (defun gnus-topic-parent-topic (topic &optional topology)
326   "Return the parent of TOPIC."
327   (unless topology
328     (setq topology gnus-topic-topology))
329   (let ((parent (car (pop topology)))
330         result found)
331     (while (and topology
332                 (not (setq found (equal (caaar topology) topic)))
333                 (not (setq result (gnus-topic-parent-topic topic 
334                                                            (car topology)))))
335       (setq topology (cdr topology)))
336     (or result (and found parent))))
337
338 (defun gnus-topic-next-topic (topic &optional previous)
339   "Return the next sibling of TOPIC."
340   (let ((topology gnus-topic-topology)
341         (parentt (cddr (gnus-topic-find-topology 
342                         (gnus-topic-parent-topic topic))))
343         prev)
344     (while (and parentt
345                 (not (equal (caaar parentt) topic)))
346       (setq prev (caaar parentt)
347             parentt (cdr parentt)))
348     (if previous
349         prev
350       (caaadr parentt))))
351
352 (defun gnus-topic-find-topology (topic &optional topology level remove)
353   "Return the topology of TOPIC."
354   (unless topology
355     (setq topology gnus-topic-topology)
356     (setq level 0))
357   (let ((top topology)
358         result)
359     (if (equal (caar topology) topic)
360         (progn
361           (when remove
362             (delq topology remove))
363           (cons level topology))
364       (setq topology (cdr topology))
365       (while (and topology
366                   (not (setq result (gnus-topic-find-topology
367                                      topic (car topology) (1+ level)
368                                      (and remove top)))))
369         (setq topology (cdr topology)))
370       result)))
371
372 (gnus-add-shutdown 'gnus-topic-close 'gnus)
373
374 (defun gnus-topic-close ()
375   (setq gnus-topic-active-topology nil
376         gnus-topic-active-alist nil
377         gnus-topic-killed-topics nil
378         gnus-topic-tallied-groups nil
379         gnus-topology-checked-p nil))
380
381 (defun gnus-topic-check-topology ()  
382   ;; The first time we set the topology to whatever we have
383   ;; gotten here, which can be rather random.
384   (unless gnus-topic-alist
385     (gnus-topic-init-alist))
386
387   (setq gnus-topology-checked-p t)
388   (let ((topics (gnus-topic-list))
389         (alist gnus-topic-alist)
390         changed)
391     (while alist
392       (unless (member (caar alist) topics)
393         (nconc gnus-topic-topology
394                (list (list (list (caar alist) 'visible))))
395         (setq changed t))
396       (setq alist (cdr alist)))
397     (when changed
398       (gnus-topic-enter-dribble)))
399   (let* ((tgroups (apply 'append (mapcar (lambda (entry) (cdr entry))
400                                          gnus-topic-alist)))
401          (entry (assoc (caar gnus-topic-topology) gnus-topic-alist))
402          (newsrc gnus-newsrc-alist)
403          group)
404     (while newsrc
405       (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
406         (setcdr entry (cons group (cdr entry)))))))
407
408 (defvar gnus-tmp-topics nil)
409 (defun gnus-topic-list (&optional topology)
410   (unless topology
411     (setq topology gnus-topic-topology 
412           gnus-tmp-topics nil))
413   (push (caar topology) gnus-tmp-topics)
414   (mapcar 'gnus-topic-list (cdr topology))
415   gnus-tmp-topics)
416
417 (defun gnus-topic-enter-dribble ()
418   (gnus-dribble-enter
419    (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
420
421 (defun gnus-topic-articles-in-topic (entries)
422   (let ((total 0)
423         number)
424     (while entries
425       (when (numberp (setq number (car (pop entries))))
426         (incf total number)))
427     total))
428
429 (defun gnus-group-topic (group)
430   "Return the topic GROUP is a member of."
431   (let ((alist gnus-topic-alist)
432         out)
433     (while alist
434       (when (member group (cdar alist))
435         (setq out (caar alist)
436               alist nil))
437       (setq alist (cdr alist)))
438     out))
439
440 (defun gnus-topic-goto-topic (topic)
441   "Go to TOPIC."
442   (when topic
443     (gnus-goto-char (text-property-any (point-min) (point-max)
444                                        'gnus-topic (intern topic)))))
445
446 (defun gnus-group-parent-topic ()
447   "Return the name of the current topic."
448   (let ((result
449          (or (get-text-property (point) 'gnus-topic)
450              (save-excursion
451                (and (gnus-goto-char (previous-single-property-change
452                                      (point) 'gnus-topic))
453                     (get-text-property (max (1- (point)) (point-min))
454                                        'gnus-topic))))))
455     (when result
456       (symbol-name result))))
457   
458 (defun gnus-topic-update-topic ()
459   "Update all parent topics to the current group."
460   (when (and (eq major-mode 'gnus-group-mode)
461              gnus-topic-mode)
462     (let ((group (gnus-group-group-name))
463           (buffer-read-only nil))
464       (when (and group (gnus-get-info group)
465                  (gnus-topic-goto-topic (gnus-group-parent-topic)))
466         (gnus-topic-update-topic-line (gnus-group-topic-name))
467         (gnus-group-goto-group group)
468         (gnus-group-position-point)))))
469
470 (defun gnus-topic-goto-missing-group (group) 
471   "Place point where GROUP is supposed to be inserted."
472   (let* ((topic (gnus-group-topic group))
473          (groups (cdr (assoc topic gnus-topic-alist)))
474          (g (cdr (member group groups)))
475          (unfound t))
476     (while (and g unfound)
477       (when (gnus-group-goto-group (pop g))
478         (beginning-of-line)
479         (setq unfound nil)))
480     (when unfound
481       (setq g (cdr (member group (reverse groups))))
482       (while (and g unfound)
483         (when (gnus-group-goto-group (pop g))
484           (forward-line 1)
485           (setq unfound nil)))
486       (when unfound
487         (gnus-topic-goto-topic topic)
488         (forward-line 1)))))
489
490 (defun gnus-topic-update-topic-line (topic-name &optional reads)
491   (let* ((top (gnus-topic-find-topology topic-name))
492          (type (cadr top))
493          (children (cddr top))
494          (entries (gnus-topic-find-groups 
495                    (car type) (car gnus-group-list-mode)
496                    (cdr gnus-group-list-mode)))
497          (parent (gnus-topic-parent-topic topic-name))
498          (all-entries entries)
499          (unread 0)
500          old-unread entry)
501     (when (gnus-topic-goto-topic (car type))
502       ;; Tally all the groups that belong in this topic.
503       (if reads
504           (setq unread (- (gnus-group-topic-unread) reads))
505         (while children
506           (incf unread (gnus-topic-unread (caar (pop children)))))
507         (while (setq entry (pop entries))
508           (when (numberp (car entry))
509             (incf unread (car entry)))))
510       (setq old-unread (gnus-group-topic-unread))
511       ;; Insert the topic line.
512       (gnus-topic-insert-topic-line 
513        (car type) (gnus-topic-visible-p)
514        (not (eq (nth 2 type) 'hidden))
515        (gnus-group-topic-level) all-entries unread)
516       (gnus-delete-line))
517     (when parent
518       (forward-line -1)
519       (gnus-topic-update-topic-line
520        parent (- old-unread (gnus-group-topic-unread))))
521     unread))
522
523 (defun gnus-topic-grok-active (&optional force)
524   "Parse all active groups and create topic structures for them."
525   ;; First we make sure that we have really read the active file. 
526   (when (or force
527             (not gnus-topic-active-alist))
528     (let (groups)
529       ;; Get a list of all groups available.
530       (mapatoms (lambda (g) (when (symbol-value g)
531                               (push (symbol-name g) groups)))
532                 gnus-active-hashtb)
533       (setq groups (sort groups 'string<))
534       ;; Init the variables.
535       (setq gnus-topic-active-topology (list (list "" 'visible)))
536       (setq gnus-topic-active-alist nil)
537       ;; Descend the top-level hierarchy.
538       (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
539       ;; Set the top-level topic names to something nice.
540       (setcar (car gnus-topic-active-topology) "Gnus active")
541       (setcar (car gnus-topic-active-alist) "Gnus active"))))
542
543 (defun gnus-topic-grok-active-1 (topology groups)
544   (let* ((name (caar topology))
545          (prefix (concat "^" (regexp-quote name)))
546          tgroups ntopology group)
547     (while (and groups
548                 (string-match prefix (setq group (car groups))))
549       (if (not (string-match "\\." group (match-end 0)))
550           ;; There are no further hierarchies here, so we just
551           ;; enter this group into the list belonging to this
552           ;; topic.
553           (push (pop groups) tgroups)
554         ;; New sub-hierarchy, so we add it to the topology.
555         (nconc topology (list (setq ntopology 
556                                     (list (list (substring 
557                                                  group 0 (match-end 0))
558                                                 'invisible)))))
559         ;; Descend the hierarchy.
560         (setq groups (gnus-topic-grok-active-1 ntopology groups))))
561     ;; We remove the trailing "." from the topic name.
562     (setq name
563           (if (string-match "\\.$" name)
564               (substring name 0 (match-beginning 0))
565             name))
566     ;; Add this topic and its groups to the topic alist.
567     (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
568     (setcar (car topology) name)
569     ;; We return the rest of the groups that didn't belong
570     ;; to this topic.
571     groups))
572
573 (defun gnus-group-active-topic-p ()
574   "Return whether the current active comes from the active topics."
575   (save-excursion
576     (beginning-of-line)
577     (get-text-property (point) 'gnus-active)))
578
579 ;;; Topic mode, commands and keymap.
580
581 (defvar gnus-topic-mode-map nil)
582 (defvar gnus-group-topic-map nil)
583
584 (unless gnus-topic-mode-map
585   (setq gnus-topic-mode-map (make-sparse-keymap))
586
587   ;; Override certain group mode keys.
588   (gnus-define-keys
589    gnus-topic-mode-map
590    "=" gnus-topic-select-group
591    "\r" gnus-topic-select-group
592    " " gnus-topic-read-group
593    "\C-k" gnus-topic-kill-group
594    "\C-y" gnus-topic-yank-group
595    "\M-g" gnus-topic-get-new-news-this-topic
596    "AT" gnus-topic-list-active
597    gnus-mouse-2 gnus-mouse-pick-topic)
598
599   ;; Define a new submap.
600   (gnus-define-keys
601    (gnus-group-topic-map "T" gnus-group-mode-map)
602    "#" gnus-topic-mark-topic
603    "\M-#" gnus-topic-unmark-topic
604    "n" gnus-topic-create-topic
605    "m" gnus-topic-move-group
606    "D" gnus-topic-remove-group
607    "c" gnus-topic-copy-group
608    "h" gnus-topic-hide-topic
609    "s" gnus-topic-show-topic
610    "M" gnus-topic-move-matching
611    "C" gnus-topic-copy-matching
612    "\C-i" gnus-topic-indent
613    [tab] gnus-topic-indent
614    "r" gnus-topic-rename
615    "\177" gnus-topic-delete))
616
617 (defun gnus-topic-make-menu-bar ()
618   (unless (boundp 'gnus-topic-menu)
619     (easy-menu-define
620      gnus-topic-menu gnus-topic-mode-map ""
621      '("Topics"
622        ["Toggle topics" gnus-topic-mode t]
623        ("Groups"
624         ["Copy" gnus-topic-copy-group t]
625         ["Move" gnus-topic-move-group t]
626         ["Remove" gnus-topic-remove-group t]
627         ["Copy matching" gnus-topic-copy-matching t]
628         ["Move matching" gnus-topic-move-matching t])
629        ("Topics"
630         ["Show" gnus-topic-show-topic t]
631         ["Hide" gnus-topic-hide-topic t]
632         ["Delete" gnus-topic-delete t]
633         ["Rename" gnus-topic-rename t]
634         ["Create" gnus-topic-create-topic t]
635         ["Mark" gnus-topic-mark-topic t]
636         ["Indent" gnus-topic-indent t])
637        ["List active" gnus-topic-list-active t]))))
638
639 (defun gnus-topic-mode (&optional arg redisplay)
640   "Minor mode for topicsifying Gnus group buffers."
641   (interactive (list current-prefix-arg t))
642   (when (eq major-mode 'gnus-group-mode)
643     (make-local-variable 'gnus-topic-mode)
644     (setq gnus-topic-mode 
645           (if (null arg) (not gnus-topic-mode)
646             (> (prefix-numeric-value arg) 0)))
647     ;; Infest Gnus with topics.
648     (when gnus-topic-mode
649       (when (and menu-bar-mode
650                  (gnus-visual-p 'topic-menu 'menu))
651         (gnus-topic-make-menu-bar))
652       (setq gnus-topic-line-format-spec 
653             (gnus-parse-format gnus-topic-line-format 
654                                gnus-topic-line-format-alist t))
655       (unless (assq 'gnus-topic-mode minor-mode-alist)
656         (push '(gnus-topic-mode " Topic") minor-mode-alist))
657       (unless (assq 'gnus-topic-mode minor-mode-map-alist)
658         (push (cons 'gnus-topic-mode gnus-topic-mode-map)
659               minor-mode-map-alist))
660       (add-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
661       (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
662       (add-hook 'gnus-group-update-group-hook 'gnus-topic-update-topic)
663       (make-local-variable 'gnus-group-prepare-function)
664       (setq gnus-group-prepare-function 'gnus-group-prepare-topics)
665       (make-local-variable 'gnus-group-goto-next-group-function)
666       (setq gnus-group-goto-next-group-function 
667             'gnus-topic-goto-next-group)
668       (setq gnus-group-change-level-function 'gnus-topic-change-level)
669       (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
670       (make-local-variable 'gnus-group-indentation-function)
671       (setq gnus-group-indentation-function
672             'gnus-topic-group-indentation)
673       (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
674       (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
675       (setq gnus-topology-checked-p nil)
676       ;; We check the topology.
677       (when gnus-newsrc-alist
678         (gnus-topic-check-topology))
679       (run-hooks 'gnus-topic-mode-hook))
680     ;; Remove topic infestation.
681     (unless gnus-topic-mode
682       (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
683       (remove-hook 'gnus-group-change-level-function 
684                    'gnus-topic-change-level)
685       (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
686       (setq gnus-group-prepare-function 'gnus-group-prepare-flat))
687     (when redisplay
688       (gnus-group-list-groups))))
689     
690 (defun gnus-topic-select-group (&optional all)
691   "Select this newsgroup.
692 No article is selected automatically.
693 If ALL is non-nil, already read articles become readable.
694 If ALL is a number, fetch this number of articles.
695
696 If performed over a topic line, toggle folding the topic."
697   (interactive "P")
698   (if (gnus-group-topic-p)
699       (let ((gnus-group-list-mode 
700              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
701         (gnus-topic-fold all))
702     (gnus-group-select-group all)))
703
704 (defun gnus-mouse-pick-topic (e)
705   "Select the group or topic under the mouse pointer."
706   (interactive "e")
707   (mouse-set-point e)
708   (gnus-topic-read-group nil))
709
710 (defun gnus-topic-read-group (&optional all no-article group)
711   "Read news in this newsgroup.
712 If the prefix argument ALL is non-nil, already read articles become
713 readable.  IF ALL is a number, fetch this number of articles.  If the
714 optional argument NO-ARTICLE is non-nil, no article will be
715 auto-selected upon group entry.  If GROUP is non-nil, fetch that
716 group.
717
718 If performed over a topic line, toggle folding the topic."
719   (interactive "P")
720   (if (gnus-group-topic-p)
721       (let ((gnus-group-list-mode 
722              (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
723         (gnus-topic-fold all))
724     (gnus-group-read-group all no-article group)))
725
726 (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
727   (interactive 
728    (list
729     (read-string "New topic: ")
730     (gnus-group-parent-topic)))
731   ;; Check whether this topic already exists.
732   (when (gnus-topic-find-topology topic)
733     (error "Topic aleady exists"))
734   (unless parent
735     (setq parent (caar gnus-topic-topology)))
736   (let ((top (cdr (gnus-topic-find-topology parent)))
737         (full-topic (or full-topic `((,topic visible)))))
738     (unless top
739       (error "No such parent topic: %s" parent))
740     (if previous
741         (progn
742           (while (and (cdr top)
743                       (not (equal (caaadr top) previous)))
744             (setq top (cdr top)))
745           (setcdr top (cons full-topic (cdr top))))
746       (nconc top (list full-topic)))
747     (unless (assoc topic gnus-topic-alist)
748       (push (list topic) gnus-topic-alist)))
749   (gnus-topic-enter-dribble)
750   (gnus-group-list-groups)
751   (gnus-topic-goto-topic topic))
752
753 (defun gnus-topic-move-group (n topic &optional copyp)
754   "Move the next N groups to TOPIC.
755 If COPYP, copy the groups instead."
756   (interactive
757    (list current-prefix-arg
758          (completing-read "Move to topic: " gnus-topic-alist nil t)))
759   (let ((groups (gnus-group-process-prefix n))
760         (topicl (assoc topic gnus-topic-alist))
761         entry)
762     (mapcar (lambda (g) 
763               (gnus-group-remove-mark g)
764               (when (and
765                      (setq entry (assoc (gnus-group-parent-topic)
766                                         gnus-topic-alist))
767                      (not copyp))
768                 (setcdr entry (gnus-delete-first g (cdr entry))))
769               (nconc topicl (list g)))
770             groups)
771     (gnus-group-position-point))
772   (gnus-topic-enter-dribble)
773   (gnus-group-list-groups))
774
775 (defun gnus-topic-remove-group ()
776   "Remove the current group from the topic."
777   (interactive)
778   (let ((topicl (assoc (gnus-group-parent-topic) gnus-topic-alist))
779         (group (gnus-group-group-name))
780         (buffer-read-only nil))
781     (when (and topicl group)
782       (gnus-delete-line)
783       (gnus-delete-first group topicl))
784     (gnus-group-position-point)))
785
786 (defun gnus-topic-copy-group (n topic)
787   "Copy the current group to a topic."
788   (interactive
789    (list current-prefix-arg
790          (completing-read "Copy to topic: " gnus-topic-alist nil t)))
791   (gnus-topic-move-group n topic t))
792
793 (defun gnus-topic-group-indentation ()
794   (make-string 
795    (* gnus-topic-indent-level
796       (or (save-excursion
797             (gnus-topic-goto-topic (gnus-group-parent-topic))
798             (gnus-group-topic-level)) 0)) ? ))
799
800 (defun gnus-topic-clean-alist ()
801   "Remove bogus groups from the topic alist."
802   (let ((topic-alist gnus-topic-alist)
803         result topic)
804     (unless gnus-killed-hashtb
805       (gnus-make-hashtable-from-killed))
806     (while (setq topic (pop topic-alist))
807       (let ((topic-name (pop topic))
808             group filtered-topic)
809         (while (setq group (pop topic))
810           (if (and (gnus-gethash group gnus-active-hashtb)
811                    (not (gnus-gethash group gnus-killed-hashtb)))
812               (push group filtered-topic)))
813         (push (cons topic-name (nreverse filtered-topic)) result)))
814     (setq gnus-topic-alist (nreverse result))))
815
816 (defun gnus-topic-change-level (group level oldlevel)
817   "Run when changing levels to enter/remove groups from topics."
818   (save-excursion
819     (set-buffer gnus-group-buffer)
820     (when (and gnus-topic-mode 
821                gnus-topic-alist
822                (not gnus-topic-inhibit-change-level))
823       ;; Remove the group from the topics.
824       (when (and (< oldlevel gnus-level-zombie)
825                  (>= level gnus-level-zombie))
826         (let (alist)
827           (forward-line -1)
828           (when (setq alist (assoc (gnus-group-parent-topic) gnus-topic-alist))
829             (setcdr alist (gnus-delete-first group (cdr alist))))))
830       ;; If the group is subscribed. then we enter it into the topics.
831       (when (and (< level gnus-level-zombie)
832                  (>= oldlevel gnus-level-zombie))
833         (let* ((prev (gnus-group-group-name))
834                (gnus-topic-inhibit-change-level t)
835                (gnus-group-indentation
836                 (make-string 
837                  (* gnus-topic-indent-level
838                     (or (save-excursion
839                           (gnus-topic-goto-topic (gnus-group-parent-topic))
840                           (gnus-group-topic-level)) 0)) ? ))
841                (yanked (list group))
842                alist talist end)
843           ;; Then we enter the yanked groups into the topics they belong
844           ;; to. 
845           (when (setq alist (assoc (save-excursion
846                                      (forward-line -1)
847                                      (or
848                                       (gnus-group-parent-topic)
849                                       (caar gnus-topic-topology)))
850                                    gnus-topic-alist))
851             (setq talist alist)
852             (when (stringp yanked)
853               (setq yanked (list yanked)))
854             (if (not prev)
855                 (nconc alist yanked)
856               (if (not (cdr alist))
857                   (setcdr alist (nconc yanked (cdr alist)))
858                 (while (and (not end) (cdr alist))
859                   (when (equal (cadr alist) prev)
860                     (setcdr alist (nconc yanked (cdr alist)))
861                     (setq end t))
862                   (setq alist (cdr alist)))
863                 (unless end
864                   (nconc talist yanked))))))
865         (gnus-topic-update-topic)))))
866
867 (defun gnus-topic-goto-next-group (group props)
868   "Go to group or the next group after group."
869   (if (null group)
870       (gnus-topic-goto-topic (symbol-name (cadr (memq 'gnus-topic props))))
871     (if (gnus-group-goto-group group)
872         t
873       ;; The group is no longer visible.
874       (let* ((list (assoc (gnus-group-parent-topic) gnus-topic-alist))
875              (after (cdr (member group (cdr list)))))
876         ;; First try to put point on a group after the current one.
877         (while (and after
878                     (not (gnus-group-goto-group (car after))))
879           (setq after (cdr after)))
880         ;; Then try to put point on a group before point.
881         (unless after
882           (setq after (cdr (member group (reverse (cdr list)))))
883           (while (and after 
884                       (not (gnus-group-goto-group (car after))))
885             (setq after (cdr after))))
886         ;; Finally, just put point on the topic.
887         (unless after
888           (gnus-topic-goto-topic (car list))
889           (setq after nil))
890         t))))
891
892 (defun gnus-topic-kill-group (&optional n discard)
893   "Kill the next N groups."
894   (interactive "P")
895   (if (gnus-group-topic-p)
896       (let ((topic (gnus-group-topic-name)))
897         (gnus-topic-remove-topic nil t)
898         (push (gnus-topic-find-topology topic nil nil gnus-topic-topology)
899               gnus-topic-killed-topics))
900     (gnus-group-kill-group n discard)
901     (gnus-topic-update-topic)))
902   
903 (defun gnus-topic-yank-group (&optional arg)
904   "Yank the last topic."
905   (interactive "p")
906   (if gnus-topic-killed-topics
907       (let ((previous 
908              (or (gnus-group-topic-name)
909                  (gnus-topic-next-topic (gnus-group-parent-topic))))
910             (item (cdr (pop gnus-topic-killed-topics))))
911         (gnus-topic-create-topic
912          (caar item) (gnus-topic-parent-topic previous) previous
913          item)
914         (gnus-topic-goto-topic (caar item)))
915     (let* ((prev (gnus-group-group-name))
916            (gnus-topic-inhibit-change-level t)
917            (gnus-group-indentation
918             (make-string 
919              (* gnus-topic-indent-level
920                 (or (save-excursion
921                       (gnus-topic-goto-topic (gnus-group-parent-topic))
922                       (gnus-group-topic-level)) 0)) ? ))
923            yanked alist)
924       ;; We first yank the groups the normal way...
925       (setq yanked (gnus-group-yank-group arg))
926       ;; Then we enter the yanked groups into the topics they belong
927       ;; to. 
928       (setq alist (assoc (save-excursion
929                            (forward-line -1)
930                            (gnus-group-parent-topic))
931                          gnus-topic-alist))
932       (when (stringp yanked)
933         (setq yanked (list yanked)))
934       (if (not prev)
935           (nconc alist yanked)
936         (if (not (cdr alist))
937             (setcdr alist (nconc yanked (cdr alist)))
938           (while (cdr alist)
939             (when (equal (cadr alist) prev)
940               (setcdr alist (nconc yanked (cdr alist)))
941               (setq alist nil))
942             (setq alist (cdr alist))))))
943     (gnus-topic-update-topic)))
944
945 (defun gnus-topic-hide-topic ()
946   "Hide the current topic."
947   (interactive)
948   (when (gnus-group-parent-topic)
949     (gnus-topic-goto-topic (gnus-group-parent-topic))
950     (gnus-topic-remove-topic nil nil 'hidden)))
951
952 (defun gnus-topic-show-topic ()
953   "Show the hidden topic."
954   (interactive)
955   (when (gnus-group-topic-p)
956     (gnus-topic-remove-topic t nil 'shown)))
957
958 (defun gnus-topic-mark-topic (topic &optional unmark)
959   "Mark all groups in the topic with the process mark."
960   (interactive (list (gnus-group-parent-topic)))
961   (save-excursion
962     (let ((groups (gnus-topic-find-groups topic 9 t)))
963       (while groups
964         (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
965                  (gnus-info-group (nth 2 (pop groups))))))))
966
967 (defun gnus-topic-unmark-topic (topic &optional unmark)
968   "Remove the process mark from all groups in the topic."
969   (interactive (list (gnus-group-parent-topic)))
970   (gnus-topic-mark-topic topic t))
971
972 (defun gnus-topic-get-new-news-this-topic (&optional n)
973   "Check for new news in the current topic."
974   (interactive "P")
975   (if (not (gnus-group-topic-p))
976       (gnus-group-get-new-news-this-group n)
977     (gnus-topic-mark-topic (gnus-group-topic-name))
978     (gnus-group-get-new-news-this-group)))
979
980 (defun gnus-topic-move-matching (regexp topic &optional copyp)
981   "Move all groups that match REGEXP to some topic."
982   (interactive
983    (let (topic)
984      (nreverse
985       (list
986        (setq topic (completing-read "Move to topic: " gnus-topic-alist nil t))
987        (read-string (format "Move to %s (regexp): " topic))))))
988   (gnus-group-mark-regexp regexp)
989   (gnus-topic-move-group nil topic copyp))
990
991 (defun gnus-topic-copy-matching (regexp topic &optional copyp)
992   "Copy all groups that match REGEXP to some topic."
993   (interactive
994    (let (topic)
995      (nreverse
996       (list
997        (setq topic (completing-read "Copy to topic: " gnus-topic-alist nil t))
998        (read-string (format "Copy to %s (regexp): " topic))))))
999   (gnus-topic-move-matching regexp topic t))
1000
1001 (defun gnus-topic-delete (topic)
1002   "Delete a topic."
1003   (interactive (list (gnus-group-topic-name)))
1004   (unless topic
1005     (error "No topic to be deleted"))
1006   (let ((entry (assoc topic gnus-topic-alist))
1007         (buffer-read-only nil))
1008     (when (cdr entry)
1009       (error "Topic not empty"))
1010     ;; Delete if visible.
1011     (when (gnus-topic-goto-topic topic)
1012       (gnus-delete-line))
1013     ;; Remove from alist.
1014     (setq gnus-topic-alist (delq entry gnus-topic-alist))
1015     ;; Remove from topology.
1016     (gnus-topic-find-topology topic nil nil 'delete)))
1017
1018 (defun gnus-topic-rename (old-name new-name)
1019   "Rename a topic."
1020   (interactive
1021    (let ((topic (gnus-group-parent-topic)))
1022      (list topic
1023            (read-string (format "Rename %s to: " topic)))))
1024   (let ((top (gnus-topic-find-topology old-name))
1025         (entry (assoc old-name gnus-topic-alist)))
1026     (when top
1027       (setcar (cadr top) new-name))
1028     (when entry 
1029       (setcar entry new-name))
1030     (forward-line -1)
1031     (gnus-group-list-groups)))
1032
1033 (defun gnus-topic-indent (&optional unindent)
1034   "Indent a topic -- make it a sub-topic of the previous topic.
1035 If UNINDENT, remove an indentation."
1036   (interactive "P")
1037   (if unindent
1038       (gnus-topic-unindent)
1039     (let* ((topic (gnus-group-parent-topic))
1040            (parent (gnus-topic-previous-topic topic)))
1041       (unless parent
1042         (error "Nothing to indent %s into" topic))
1043       (when topic
1044         (gnus-topic-goto-topic topic)
1045         (gnus-topic-kill-group)
1046         (gnus-topic-create-topic
1047          topic parent nil (cdr (pop gnus-topic-killed-topics)))
1048         (or (gnus-topic-goto-topic topic)
1049             (gnus-topic-goto-topic parent))))))
1050
1051 (defun gnus-topic-unindent ()
1052   "Unindent a topic."
1053   (interactive)
1054   (let* ((topic (gnus-group-parent-topic))
1055          (parent (gnus-topic-parent-topic topic))
1056          (grandparent (gnus-topic-parent-topic parent)))
1057     (unless grandparent
1058       (error "Nothing to indent %s into" topic))
1059     (when topic
1060       (gnus-topic-goto-topic topic)
1061       (gnus-topic-kill-group)
1062       (gnus-topic-create-topic
1063        topic grandparent (gnus-topic-next-topic parent)
1064        (cdr (pop gnus-topic-killed-topics)))
1065       (gnus-topic-goto-topic topic))))
1066
1067 (defun gnus-topic-list-active (&optional force)
1068   "List all groups that Gnus knows about in a topicsified fashion.
1069 If FORCE, always re-read the active file."
1070   (interactive "P")
1071   (when force
1072     (gnus-get-killed-groups))
1073   (gnus-topic-grok-active force)
1074   (let ((gnus-topic-topology gnus-topic-active-topology)
1075         (gnus-topic-alist gnus-topic-active-alist)
1076         gnus-killed-list gnus-zombie-list)
1077     (gnus-group-list-groups 9 nil 1)))
1078
1079 (provide 'gnus-topic)
1080
1081 ;;; gnus-topic.el ends here