1 ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
2 ;; Copyright (C) 1995,96,97 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.
33 (defgroup gnus-topic nil
37 (defvar gnus-topic-mode nil
38 "Minor mode for Gnus group buffers.")
40 (defcustom gnus-topic-mode-hook nil
41 "Hook run in topic mode buffers."
45 (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
46 "Format of topic lines.
47 It works along the same lines as a normal formatting string,
48 with some simple extensions.
50 %i Indentation based on topic level.
52 %v Nothing if the topic is visible, \"...\" otherwise.
53 %g Number of groups in the topic.
54 %a Number of unread articles in the groups in the topic.
55 %A Number of unread articles in the groups in the topic and its subtopics.
60 (defcustom gnus-topic-indent-level 2
61 "*How much each subtopic should be indented."
65 (defcustom gnus-topic-display-empty-topics t
66 "*If non-nil, display the topic lines even of topics that have no unread articles."
70 ;; Internal variables.
72 (defvar gnus-topic-active-topology nil)
73 (defvar gnus-topic-active-alist nil)
75 (defvar gnus-topology-checked-p nil
76 "Whether the topology has been checked in this session.")
78 (defvar gnus-topic-killed-topics nil)
79 (defvar gnus-topic-inhibit-change-level nil)
80 (defvar gnus-topic-tallied-groups nil)
82 (defconst gnus-topic-line-format-alist
86 (?g number-of-groups ?d)
87 (?a (gnus-topic-articles-in-topic entries) ?d)
88 (?A total-number-of-articles ?d)
91 (defvar gnus-topic-line-format-spec nil)
95 (defun gnus-group-topic-name ()
96 "The name of the topic on the current line."
97 (let ((topic (get-text-property (gnus-point-at-bol) 'gnus-topic)))
98 (and topic (symbol-name topic))))
100 (defun gnus-group-topic-level ()
101 "The level of the topic on the current line."
102 (get-text-property (gnus-point-at-bol) 'gnus-topic-level))
104 (defun gnus-group-topic-unread ()
105 "The number of unread articles in topic on the current line."
106 (get-text-property (gnus-point-at-bol) 'gnus-topic-unread))
108 (defun gnus-topic-unread (topic)
109 "Return the number of unread articles in TOPIC."
111 (and (gnus-topic-goto-topic topic)
112 (gnus-group-topic-unread)))
115 (defun gnus-group-topic-p ()
116 "Return non-nil if the current line is a topic."
117 (gnus-group-topic-name))
119 (defun gnus-topic-visible-p ()
120 "Return non-nil if the current topic is visible."
121 (get-text-property (gnus-point-at-bol) 'gnus-topic-visible))
123 (defun gnus-topic-articles-in-topic (entries)
127 (when (numberp (setq number (car (pop entries))))
128 (incf total number)))
131 (defun gnus-group-topic (group)
132 "Return the topic GROUP is a member of."
133 (let ((alist gnus-topic-alist)
136 (when (member group (cdar alist))
137 (setq out (caar alist)
139 (setq alist (cdr alist)))
142 (defun gnus-group-parent-topic (group)
143 "Return the topic GROUP is member of by looking at the group buffer."
145 (set-buffer gnus-group-buffer)
146 (if (gnus-group-goto-group group)
148 (gnus-group-topic group))))
150 (defun gnus-topic-goto-topic (topic)
153 (gnus-goto-char (text-property-any (point-min) (point-max)
154 'gnus-topic (intern topic)))))
156 (defun gnus-current-topic ()
157 "Return the name of the current topic."
159 (or (get-text-property (point) 'gnus-topic)
161 (and (gnus-goto-char (previous-single-property-change
162 (point) 'gnus-topic))
163 (get-text-property (max (1- (point)) (point-min))
166 (symbol-name result))))
168 (defun gnus-current-topics ()
169 "Return a list of all current topics, lowest in hierarchy first."
170 (let ((topic (gnus-current-topic))
174 (setq topic (gnus-topic-parent-topic topic)))
177 (defun gnus-group-active-topic-p ()
178 "Say whether the current topic comes from the active topics."
181 (get-text-property (point) 'gnus-active)))
183 (defun gnus-topic-find-groups (topic &optional level all)
184 "Return entries for all visible groups in TOPIC."
185 (let ((groups (cdr (assoc topic gnus-topic-alist)))
186 info clevel unread group lowest params visible-groups entry active)
187 (setq lowest (or lowest 1))
188 (setq level (or level 7))
189 ;; We go through the newsrc to look for matches.
191 (when (setq group (pop groups))
192 (setq entry (gnus-gethash group gnus-newsrc-hashtb)
194 params (gnus-info-params info)
195 active (gnus-active group)
196 unread (or (car entry)
197 (and (not (equal group "dummy.group"))
199 (- (1+ (cdr active)) (car active))))
200 clevel (or (gnus-info-level info)
201 (if (member group gnus-zombie-list) 8 9))))
203 unread ; nil means that the group is dead.
205 (>= clevel lowest) ; Is inside the level we want.
208 gnus-group-list-inactive-groups
210 (and gnus-list-groups-with-ticked-articles
211 (cdr (assq 'tick (gnus-info-marks info))))
212 ; Has right readedness.
213 ;; Check for permanent visibility.
214 (and gnus-permanently-visible-groups
215 (string-match gnus-permanently-visible-groups group))
216 (memq 'visible params)
217 (cdr (assq 'visible params)))
218 ;; Add this group to the list of visible groups.
219 (push (or entry group) visible-groups)))
220 (nreverse visible-groups)))
222 (defun gnus-topic-previous-topic (topic)
223 "Return the previous topic on the same level as TOPIC."
224 (let ((top (cddr (gnus-topic-find-topology
225 (gnus-topic-parent-topic topic)))))
226 (unless (equal topic (caaar top))
227 (while (and top (not (equal (caaadr top) topic)))
228 (setq top (cdr top)))
231 (defun gnus-topic-parent-topic (topic &optional topology)
232 "Return the parent of TOPIC."
234 (setq topology gnus-topic-topology))
235 (let ((parent (car (pop topology)))
238 (not (setq found (equal (caaar topology) topic)))
239 (not (setq result (gnus-topic-parent-topic topic
241 (setq topology (cdr topology)))
242 (or result (and found parent))))
244 (defun gnus-topic-next-topic (topic &optional previous)
245 "Return the next sibling of TOPIC."
246 (let ((parentt (cddr (gnus-topic-find-topology
247 (gnus-topic-parent-topic topic))))
250 (not (equal (caaar parentt) topic)))
251 (setq prev (caaar parentt)
252 parentt (cdr parentt)))
257 (defun gnus-topic-find-topology (topic &optional topology level remove)
258 "Return the topology of TOPIC."
260 (setq topology gnus-topic-topology)
264 (if (equal (caar topology) topic)
267 (delq topology remove))
268 (cons level topology))
269 (setq topology (cdr topology))
271 (not (setq result (gnus-topic-find-topology
272 topic (car topology) (1+ level)
274 (setq topology (cdr topology)))
277 (defvar gnus-tmp-topics nil)
278 (defun gnus-topic-list (&optional topology)
279 "Return a list of all topics in the topology."
281 (setq topology gnus-topic-topology
282 gnus-tmp-topics nil))
283 (push (caar topology) gnus-tmp-topics)
284 (mapcar 'gnus-topic-list (cdr topology))
287 ;;; Topic parameter jazz
289 (defun gnus-topic-parameters (topic)
290 "Return the parameters for TOPIC."
291 (let ((top (gnus-topic-find-topology topic)))
293 (nth 3 (cadr top)))))
295 (defun gnus-topic-set-parameters (topic parameters)
296 "Set the topic parameters of TOPIC to PARAMETERS."
297 (let ((top (gnus-topic-find-topology topic)))
299 (error "No such topic: %s" topic))
300 ;; We may have to extend if there is no parameters here
302 (unless (nthcdr 2 (cadr top))
303 (nconc (cadr top) (list nil)))
304 (unless (nthcdr 3 (cadr top))
305 (nconc (cadr top) (list nil)))
306 (setcar (nthcdr 3 (cadr top)) parameters)
308 (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
310 (defun gnus-group-topic-parameters (group)
311 "Compute the group parameters for GROUP taking into account inheritance from topics."
312 (let ((params-list (list (gnus-group-get-parameter group)))
313 topics params param out)
315 (gnus-group-goto-group group)
316 (setq topics (gnus-current-topics))
318 (push (gnus-topic-parameters (pop topics)) params-list))
319 ;; We probably have lots of nil elements here, so
320 ;; we remove them. Probably faster than doing this "properly".
321 (setq params-list (delq nil params-list))
322 ;; Now we have all the parameters, so we go through them
323 ;; and do inheritance in the obvious way.
324 (while (setq params (pop params-list))
325 (while (setq param (pop params))
327 (setq param (cons param t)))
328 ;; Override any old versions of this param.
329 (setq out (delq (assq (car param) out) out))
331 ;; Return the resulting parameter list.
334 ;;; General utility functions
336 (defun gnus-topic-enter-dribble ()
338 (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
340 ;;; Generating group buffers
342 (defun gnus-group-prepare-topics (level &optional all lowest regexp list-topic topic-level)
343 "List all newsgroups with unread articles of level LEVEL or lower, and
344 use the `gnus-group-topics' to sort the groups.
345 If ALL is non-nil, list groups that have no unread articles.
346 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
347 (set-buffer gnus-group-buffer)
348 (let ((buffer-read-only nil)
349 (lowest (or lowest 1)))
351 (setq gnus-topic-tallied-groups nil)
353 (when (or (not gnus-topic-alist)
354 (not gnus-topology-checked-p))
355 (gnus-topic-check-topology))
361 (when (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie))
362 (gnus-group-prepare-flat-list-dead
363 (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
367 (when (and (>= level gnus-level-killed) (<= lowest gnus-level-killed))
368 (gnus-group-prepare-flat-list-dead
369 (setq gnus-killed-list (sort gnus-killed-list 'string<))
375 (when (< lowest gnus-level-zombie)
377 (let ((top (gnus-topic-find-topology list-topic)))
378 (gnus-topic-prepare-topic (cdr top) (car top)
379 (or topic-level level) all))
380 (gnus-topic-prepare-topic gnus-topic-topology 0
381 (or topic-level level) all)))
383 (gnus-group-set-mode-line)
384 (setq gnus-group-list-mode (cons level all))
385 (run-hooks 'gnus-group-prepare-hook))))
387 (defun gnus-topic-prepare-topic (topicl level &optional list-level all silent)
388 "Insert TOPIC into the group buffer.
389 If SILENT, don't insert anything. Return the number of unread
390 articles in the topic and its subtopics."
391 (let* ((type (pop topicl))
392 (entries (gnus-topic-find-groups (car type) list-level all))
393 (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
394 (gnus-group-indentation
395 (make-string (* gnus-topic-indent-level level) ? ))
396 (beg (progn (beginning-of-line) (point)))
397 (topicl (reverse topicl))
398 (all-entries entries)
399 (point-max (point-max))
402 info entry end active tick)
403 ;; Insert any sub-topics.
406 (gnus-topic-prepare-topic
407 (pop topicl) (1+ level) list-level all
411 ;; Insert all the groups that belong in this topic.
412 (while (setq entry (pop entries))
416 (gnus-group-insert-group-line
417 entry (if (member entry gnus-zombie-list) 8 9)
418 nil (- (1+ (cdr (setq active (gnus-active entry))))
422 (when (setq info (nth 2 entry))
423 (gnus-group-insert-group-line
424 (gnus-info-group info)
425 (gnus-info-level info) (gnus-info-marks info)
426 (car entry) (gnus-info-method info)))))
427 (when (and (listp entry)
428 (numberp (car entry))
429 (not (member (gnus-info-group (setq info (nth 2 entry)))
430 gnus-topic-tallied-groups)))
431 (push (gnus-info-group info) gnus-topic-tallied-groups)
432 (incf unread (car entry)))
433 (when (and (listp entry)
434 (numberp (car entry)))
437 ;; Insert the topic line.
438 (when (and (not silent)
439 (or gnus-topic-display-empty-topics ;We want empty topics
440 (not (zerop unread)) ;Non-empty
441 tick ;Ticked articles
442 (/= point-max (point-max)))) ;Unactivated groups
443 (gnus-extent-start-open (point))
444 (gnus-topic-insert-topic-line
446 (not (eq (nth 2 type) 'hidden))
447 level all-entries unread))
451 (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
452 "Remove the current topic."
453 (let ((topic (gnus-group-topic-name))
454 (level (gnus-group-topic-level))
455 (beg (progn (beginning-of-line) (point)))
458 (while (and (zerop (forward-line 1))
459 (> (or (gnus-group-topic-level) (1+ level)) level)))
460 (delete-region beg (point))
461 ;; Do the change in this rather odd manner because it has been
462 ;; reported that some topics share parts of some lists, for some
463 ;; reason. I have been unable to determine why this is the
464 ;; case, but this hack seems to take care of things.
465 (let ((data (cadr (gnus-topic-find-topology topic))))
467 (list (if insert 'visible 'invisible)
471 (setq gnus-topic-alist
472 (delq (assoc topic gnus-topic-alist) gnus-topic-alist))