1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Ilja Weis <kult@uni-paderborn.de>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
29 (eval-when-compile (require 'cl))
35 (defgroup gnus-topic nil
39 (defvar gnus-topic-mode nil
40 "Minor mode for Gnus group buffers.")
42 (defcustom gnus-topic-mode-hook nil
43 "Hook run in topic mode buffers."
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.
52 %i Indentation based on topic level.
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.
62 (defcustom gnus-topic-indent-level 2
63 "*How much each subtopic should be indented."
67 (defcustom gnus-topic-display-empty-topics t
68 "*If non-nil, display the topic lines even of topics that have no unread articles."
72 ;; Internal variables.
74 (defvar gnus-topic-active-topology nil)
75 (defvar gnus-topic-active-alist nil)
77 (defvar gnus-topology-checked-p nil
78 "Whether the topology has been checked in this session.")
80 (defvar gnus-topic-killed-topics nil)
81 (defvar gnus-topic-inhibit-change-level nil)
83 (defconst gnus-topic-line-format-alist
87 (?g number-of-groups ?d)
88 (?a (gnus-topic-articles-in-topic entries) ?d)
89 (?A total-number-of-articles ?d)
92 (defvar gnus-topic-line-format-spec nil)
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))))
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))
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))
109 (defun gnus-topic-unread (topic)
110 "Return the number of unread articles in TOPIC."
112 (and (gnus-topic-goto-topic topic)
113 (gnus-group-topic-unread)))
116 (defun gnus-group-topic-p ()
117 "Return non-nil if the current line is a topic."
118 (gnus-group-topic-name))
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))
124 (defun gnus-topic-articles-in-topic (entries)
128 (when (numberp (setq number (car (pop entries))))
129 (incf total number)))
132 (defun gnus-group-topic (group)
133 "Return the topic GROUP is a member of."
134 (let ((alist gnus-topic-alist)
137 (when (member group (cdar alist))
138 (setq out (caar alist)
140 (setq alist (cdr alist)))
143 (defun gnus-group-parent-topic (group)
144 "Return the topic GROUP is member of by looking at the group buffer."
146 (set-buffer gnus-group-buffer)
147 (if (gnus-group-goto-group group)
149 (gnus-group-topic group))))
151 (defun gnus-topic-goto-topic (topic)
154 (gnus-goto-char (text-property-any (point-min) (point-max)
155 'gnus-topic (intern topic)))))
157 (defun gnus-current-topic ()
158 "Return the name of the current topic."
160 (or (get-text-property (point) 'gnus-topic)
162 (and (gnus-goto-char (previous-single-property-change
163 (point) 'gnus-topic))
164 (get-text-property (max (1- (point)) (point-min))
167 (symbol-name result))))
169 (defun gnus-current-topics ()
170 "Return a list of all current topics, lowest in hierarchy first."
171 (let ((topic (gnus-current-topic))
175 (setq topic (gnus-topic-parent-topic topic)))
178 (defun gnus-group-active-topic-p ()
179 "Say whether the current topic comes from the active topics."
182 (get-text-property (point) 'gnus-active)))
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.
192 (when (setq group (pop groups))
193 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
195 params (gnus-info-params info)
196 active (gnus-active group)
197 unread (or (car entry)
198 (and (not (equal group "dummy.group"))
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))))
204 unread ; nil means that the group is dead.
206 (>= clevel lowest) ; Is inside the level we want.
209 gnus-group-list-inactive-groups
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)))
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)))
232 (defun gnus-topic-parent-topic (topic &optional topology)
233 "Return the parent of TOPIC."
235 (setq topology gnus-topic-topology))
236 (let ((parent (car (pop 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))))
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))))
251 (not (equal (caaar parentt) topic)))
252 (setq prev (caaar parentt)
253 parentt (cdr parentt)))
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))
264 (while (and (not (zerop num))
265 (setq topic (funcall way topic)))
266 (when (gnus-topic-goto-topic topic)
269 (goto-char (point-max)))
272 (defun gnus-topic-find-topology (topic &optional topology level remove)
273 "Return the topology of TOPIC."
275 (setq topology gnus-topic-topology)
279 (if (equal (caar topology) topic)
282 (delq topology remove))
283 (cons level topology))
284 (setq topology (cdr topology))
286 (not (setq result (gnus-topic-find-topology
287 topic (car topology) (1+ level)
289 (setq topology (cdr topology)))
292 (defvar gnus-tmp-topics nil)
293 (defun gnus-topic-list (&optional topology)
294 "Return a list of all topics in the 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))
302 ;;; Topic parameter jazz
304 (defun gnus-topic-parameters (topic)
305 "Return the parameters for TOPIC."
306 (let ((top (gnus-topic-find-topology topic)))
308 (nth 3 (cadr top)))))
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)))
314 (error "No such topic: %s" topic))
315 ;; We may have to extend if there is no parameters here
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)
323 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
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)
330 (gnus-group-goto-group group)
331 (setq topics (gnus-current-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))
342 (setq param (cons param t)))
343 ;; Override any old versions of this param.
344 (setq out (delq (assq (car param) out) out))
346 ;; Return the resulting parameter list.
349 ;;; General utility functions
351 (defun gnus-topic-enter-dribble ()
353 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
355 ;;; Generating group buffers
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)))
366 (when (or (not gnus-topic-alist)
367 (not gnus-topology-checked-p))
368 (gnus-topic-check-topology))
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<))
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<))
389 (when (< lowest gnus-level-zombie)
391 (let ((top (gnus-topic-find-topology list-topic)))
392 (gnus-topic-prepare-topic (cdr top) (car top)
393 (or topic-level level) all
395 (gnus-topic-prepare-topic gnus-topic-topology 0
396 (or topic-level level) all
399 (gnus-group-set-mode-line)
400 (setq gnus-group-list-mode (cons level all))
401 (gnus-run-hooks 'gnus-group-prepare-hook))))
403 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent
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))
419 info entry end active tick)
420 ;; Insert any sub-topics.
423 (gnus-topic-prepare-topic
424 (pop topicl) (1+ level) list-level all
425 (not visiblep) lowest)))
428 ;; Insert all the groups that belong in this topic.
429 (while (setq entry (pop entries))
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))))
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)))