d1c62b208e73f1c8655812ad84d5db8229a2bae0
[gnus] / lisp / gnus-topic.el
1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Ilja Weis <kult@uni-paderborn.de>
5 ;;      Lars Magne Ingebrigtsen <larsi@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 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-start)
34
35 (defgroup gnus-topic nil
36   "Group topics."
37   :group 'gnus-group)
38
39 (defvar gnus-topic-mode nil
40   "Minor mode for Gnus group buffers.")
41
42 (defcustom gnus-topic-mode-hook nil
43   "Hook run in topic mode buffers."
44   :type 'hook
45   :group 'gnus-topic)
46
47 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
48   "Format of topic lines.
49 It works along the same lines as a normal formatting string,
50 with some simple extensions.
51
52 %i  Indentation based on topic level.
53 %n  Topic name.
54 %v  Nothing if the topic is visible, \"...\" otherwise.
55 %g  Number of groups in the topic.
56 %a  Number of unread articles in the groups in the topic.
57 %A  Number of unread articles in the groups in the topic and its subtopics.
58 "
59   :type 'string
60   :group 'gnus-topic)
61
62 (defcustom gnus-topic-indent-level 2
63   "*How much each subtopic should be indented."
64   :type 'integer
65   :group 'gnus-topic)
66
67 (defcustom gnus-topic-display-empty-topics t
68   "*If non-nil, display the topic lines even of topics that have no unread articles."
69   :type 'boolean
70   :group 'gnus-topic)
71
72 ;; Internal variables.
73
74 (defvar gnus-topic-active-topology nil)
75 (defvar gnus-topic-active-alist nil)
76
77 (defvar gnus-topology-checked-p nil
78   "Whether the topology has been checked in this session.")
79
80 (defvar gnus-topic-killed-topics nil)
81 (defvar gnus-topic-inhibit-change-level nil)
82
83 (defconst gnus-topic-line-format-alist
84   `((?n name ?s)
85     (?v visible ?s)
86     (?i indentation ?s)
87     (?g number-of-groups ?d)
88     (?a (gnus-topic-articles-in-topic entries) ?d)
89     (?A total-number-of-articles ?d)
90     (?l level ?d)))
91
92 (defvar gnus-topic-line-format-spec nil)
93
94 ;;; Utility functions
95
96 (defun gnus-group-topic-name ()
97   "The name of the topic on the current line."
98   (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
99     (and topic (symbol-name topic))))
100
101 (defun gnus-group-topic-level ()
102   "The level of the topic on the current line."
103   (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
104
105 (defun gnus-group-topic-unread ()
106   "The number of unread articles in topic on the current line."
107   (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
108
109 (defun gnus-topic-unread (topic)
110   "Return the number of unread articles in TOPIC."
111   (or (save-excursion
112         (and (gnus-topic-goto-topic topic)
113              (gnus-group-topic-unread)))
114       0))
115
116 (defun gnus-group-topic-p ()
117   "Return non-nil if the current line is a topic."
118   (gnus-group-topic-name))
119
120 (defun gnus-topic-visible-p ()
121   "Return non-nil if the current topic is visible."
122   (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
123
124 (defun gnus-topic-articles-in-topic (entries)
125   (let ((total 0)
126         number)
127     (while entries
128       (when (numberp (setq number (car (pop entries))))
129         (incf total number)))
130     total))
131
132 (defun gnus-group-topic (group)
133   "Return the topic GROUP is a member of."
134   (let ((alist gnus-topic-alist)
135         out)
136     (while alist
137       (when (member group (cdar alist))
138         (setq out (caar alist)
139               alist nil))
140       (setq alist (cdr alist)))
141     out))
142
143 (defun gnus-group-parent-topic (group)
144   "Return the topic GROUP is member of by looking at the group buffer."
145   (save-excursion
146     (set-buffer gnus-group-buffer)
147     (if (gnus-group-goto-group group)
148         (gnus-current-topic)
149       (gnus-group-topic group))))
150
151 (defun gnus-topic-goto-topic (topic)
152   "Go to TOPIC."
153   (when topic
154     (gnus-goto-char (text-property-any (point-min) (point-max)
155                                        'gnus-topic (intern topic)))))
156
157 (defun gnus-current-topic ()
158   "Return the name of the current topic."
159   (let ((result
160          (or (get-text-property (point) 'gnus-topic)
161              (save-excursion
162                (and (gnus-goto-char (previous-single-property-change
163                                      (point) 'gnus-topic))
164                     (get-text-property (max (1- (point)) (point-min))
165                                        'gnus-topic))))))
166     (when result
167       (symbol-name result))))
168
169 (defun gnus-current-topics ()
170   "Return a list of all current topics, lowest in hierarchy first."
171   (let ((topic (gnus-current-topic))
172         topics)
173     (while topic
174       (push topic topics)
175       (setq topic (gnus-topic-parent-topic topic)))
176     (nreverse topics)))
177
178 (defun gnus-group-active-topic-p ()
179   "Say whether the current topic comes from the active topics."
180   (save-excursion
181     (beginning-of-line)
182     (get-text-property (point) 'gnus-active)))
183
184 (defun gnus-topic-find-groups (topic &optional level all lowest)
185   "Return entries for all visible groups in TOPIC."
186   (let ((groups (cdr (assoc topic gnus-topic-alist)))
187         info clevel unread group params visible-groups entry active)
188     (setq lowest (or lowest 1))
189     (setq level (or level gnus-level-unsubscribed))
190     ;; We go through the newsrc to look for matches.
191     (while groups
192       (when (setq group (pop groups))
193         (setq entry (gnus-gethash group gnus-newsrc-hashtb)
194               info (nth 2 entry)
195               params (gnus-info-params info)
196               active (gnus-active group)
197               unread (or (car entry)
198                          (and (not (equal group "dummy.group"))
199                               active
200                               (- (1+ (cdr active)) (car active))))
201               clevel (or (gnus-info-level info)
202                          (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed))))
203       (and
204        unread                           ; nil means that the group is dead.
205        (<= clevel level)
206        (>= clevel lowest)               ; Is inside the level we want.
207        (or all
208            (if (eq unread t)
209                gnus-group-list-inactive-groups
210              (> unread 0))
211            (and gnus-list-groups-with-ticked-articles
212                 (cdr (assq 'tick (gnus-info-marks info))))
213                                         ; Has right readedness.
214            ;; Check for permanent visibility.
215            (and gnus-permanently-visible-groups
216                 (string-match gnus-permanently-visible-groups group))
217            (memq 'visible params)
218            (cdr (assq 'visible params)))
219        ;; Add this group to the list of visible groups.
220        (push (or entry group) visible-groups)))
221     (nreverse visible-groups)))
222
223 (defun gnus-topic-previous-topic (topic)
224   "Return the previous topic on the same level as TOPIC."
225   (let ((top (cddr (gnus-topic-find-topology
226                     (gnus-topic-parent-topic topic)))))
227     (unless (equal topic (caaar top))
228       (while (and top (not (equal (caaadr top) topic)))
229         (setq top (cdr top)))
230       (caaar top))))
231
232 (defun gnus-topic-parent-topic (topic &optional topology)
233   "Return the parent of TOPIC."
234   (unless topology
235     (setq topology gnus-topic-topology))
236   (let ((parent (car (pop topology)))
237         result found)
238     (while (and topology
239                 (not (setq found (equal (caaar topology) topic)))
240                 (not (setq result (gnus-topic-parent-topic
241                                    topic (car topology)))))
242       (setq topology (cdr topology)))
243     (or result (and found parent))))
244
245 (defun gnus-topic-next-topic (topic &optional previous)
246   "Return the next sibling of TOPIC."
247   (let ((parentt (cddr (gnus-topic-find-topology
248                         (gnus-topic-parent-topic topic))))
249         prev)
250     (while (and parentt
251                 (not (equal (caaar parentt) topic)))
252       (setq prev (caaar parentt)
253             parentt (cdr parentt)))
254     (if previous
255         prev
256       (caaadr parentt))))
257
258 (defun gnus-topic-forward-topic (num)
259   "Go to the next topic on the same level as the current one."
260   (let* ((topic (gnus-current-topic))
261          (way (if (< num 0) 'gnus-topic-previous-topic
262                 'gnus-topic-next-topic))
263          (num (abs num)))
264     (while (and (not (zerop num))
265                 (setq topic (funcall way topic)))
266       (when (gnus-topic-goto-topic topic)
267         (decf num)))
268     (unless (zerop num)
269       (goto-char (point-max)))
270     num))
271
272 (defun gnus-topic-find-topology (topic &optional topology level remove)
273   "Return the topology of TOPIC."
274   (unless topology
275     (setq topology gnus-topic-topology)
276     (setq level 0))
277   (let ((top topology)
278         result)
279     (if (equal (caar topology) topic)
280         (progn
281           (when remove
282             (delq topology remove))
283           (cons level topology))
284       (setq topology (cdr topology))
285       (while (and topology
286                   (not (setq result (gnus-topic-find-topology
287                                      topic (car topology) (1+ level)
288                                      (and remove top)))))
289         (setq topology (cdr topology)))
290       result)))
291
292 (defvar gnus-tmp-topics nil)
293 (defun gnus-topic-list (&optional topology)
294   "Return a list of all topics in the topology."
295   (unless topology
296     (setq topology gnus-topic-topology
297           gnus-tmp-topics nil))
298   (push (caar topology) gnus-tmp-topics)
299   (mapcar 'gnus-topic-list (cdr topology))
300   gnus-tmp-topics)
301
302 ;;; Topic parameter jazz
303
304 (defun gnus-topic-parameters (topic)
305   "Return the parameters for TOPIC."
306   (let ((top (gnus-topic-find-topology topic)))
307     (when top
308       (nth 3 (cadr top)))))
309
310 (defun gnus-topic-set-parameters (topic parameters)
311   "Set the topic parameters of TOPIC to PARAMETERS."
312   (let ((top (gnus-topic-find-topology topic)))
313     (unless top
314       (error "No such topic: %s" topic))
315     ;; We may have to extend if there is no parameters here
316     ;; to begin with.
317     (unless (nthcdr 2 (cadr top))
318       (nconc (cadr top) (list nil)))
319     (unless (nthcdr 3 (cadr top))
320       (nconc (cadr top) (list nil)))
321     (setcar (nthcdr 3 (cadr top)) parameters)
322     (gnus-dribble-enter
323      (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
324
325 (defun gnus-group-topic-parameters (group)
326   "Compute the group parameters for GROUP taking into account inheritance from topics."
327   (let ((params-list (list (gnus-group-get-parameter group)))
328         topics params param out)
329     (save-excursion
330       (gnus-group-goto-group group)
331       (setq topics (gnus-current-topics))
332       (while topics
333         (push (gnus-topic-parameters (pop topics)) params-list))
334       ;; We probably have lots of nil elements here, so
335       ;; we remove them.  Probably faster than doing this "properly".
336       (setq params-list (delq nil params-list))
337       ;; Now we have all the parameters, so we go through them
338       ;; and do inheritance in the obvious way.
339       (while (setq params (pop params-list))
340         (while (setq param (pop params))
341           (when (atom param)
342             (setq param (cons param t)))
343           ;; Override any old versions of this param.
344           (setq out (delq (assq (car param) out) out))
345           (push param out)))
346       ;; Return the resulting parameter list.
347       out)))
348
349 ;;; General utility functions
350
351 (defun gnus-topic-enter-dribble ()
352   (gnus-dribble-enter
353    (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
354
355 ;;; Generating group buffers
356
357 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
358   "List all newsgroups with unread articles of level LEVEL or lower, and
359 use the `gnus-group-topics' to sort the groups.
360 If ALL is non-nil, list groups that have no unread articles.
361 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
362   (set-buffer gnus-group-buffer)
363   (let ((buffer-read-only nil)
364         (lowest (or lowest 1)))
365
366     (when (or (not gnus-topic-alist)
367               (not gnus-topology-checked-p))
368       (gnus-topic-check-topology))
369
370     (unless list-topic
371       (erase-buffer))
372
373     ;; List dead groups?
374     (when (and (>= level gnus-level-zombie)
375                (<= lowest gnus-level-zombie))
376       (gnus-group-prepare-flat-list-dead
377        (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
378        gnus-level-zombie ?Z
379        regexp))
380
381     (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
382       (gnus-group-prepare-flat-list-dead
383        (setq gnus-killed-list (sort gnus-killed-list 'string<))
384        gnus-level-killed ?K
385        regexp))
386
387     ;; Use topics.
388     (prog1
389         (when (< lowest gnus-level-zombie)
390           (if list-topic
391               (let ((top (gnus-topic-find-topology list-topic)))
392                 (gnus-topic-prepare-topic (cdr top) (car top)
393                                           (or topic-level level) all
394                                           nil lowest))
395             (gnus-topic-prepare-topic gnus-topic-topology 0
396                                       (or topic-level level) all
397                                       nil lowest)))
398
399       (gnus-group-set-mode-line)
400       (setq gnus-group-list-mode (cons level all))
401       (gnus-run-hooks 'gnus-group-prepare-hook))))
402
403 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent
404                                         lowest)
405   "Insert TOPIC into the group buffer.
406 If SILENT, don't insert anything.  Return the number of unread
407 articles in the topic and its subtopics."
408   (let* ((type (pop topicl))
409          (entries (gnus-topic-find-groups (car type) list-level all lowest))
410          (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
411          (gnus-group-indentation
412           (make-string (* gnus-topic-indent-level level) ? ))
413          (beg (progn (beginning-of-line) (point)))
414          (topicl (reverse topicl))
415          (all-entries entries)
416          (point-max (point-max))
417          (unread 0)
418          (topic (car type))
419          info entry end active tick)
420     ;; Insert any sub-topics.
421     (while topicl
422       (incf unread
423             (gnus-topic-prepare-topic
424              (pop topicl) (1+ level) list-level all
425              (not visiblep) lowest)))
426     (setq end (point))
427     (goto-char beg)
428     ;; Insert all the groups that belong in this topic.
429     (while (setq entry (pop entries))
430       (when visiblep
431         (if (stringp entry)
432             ;; Dead groups.
433             (gnus-group-insert-group-line
434              entry (if (member entry gnus-zombie-list) gnus-level-zombie gnus-level-killed)
435              nil (- (1+ (cdr (setq active (gnus-active entry))))
436                     (car active))
437              nil)
438           ;; Living groups.
439           (when (setq info (nth 2 entry))
440             (gnus-group-insert-group-line
441              (gnus-info-group info)
442              (gnus-info-level info) (gnus-info-marks info)
443              (car entry) (gnus-info-method info)))))
444       (when (and (listp entry)
445                  (numberp (car entry)))
446         (incf unread (car entry)))