1 ;;; gnus-bcklg.el --- backlog functions for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
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)
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.
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl))
35 ;;; Buffering of read articles.
38 (defvar gnus-backlog-buffer " *Gnus Backlog*")
39 (defvar gnus-backlog-articles nil)
40 (defvar gnus-backlog-hashtb nil)
42 (defun gnus-backlog-buffer ()
43 "Return the backlog buffer."
44 (or (get-buffer gnus-backlog-buffer)
46 (set-buffer (gnus-get-buffer-create gnus-backlog-buffer))
48 (setq buffer-read-only t)
49 (get-buffer gnus-backlog-buffer))))
51 (defun gnus-backlog-setup ()
52 "Initialize backlog variables."
53 (unless gnus-backlog-hashtb
54 (setq gnus-backlog-hashtb (gnus-make-hashtable 1024))))
56 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
58 (defun gnus-backlog-shutdown ()
59 "Clear all backlog variables and buffers."
61 (when (get-buffer gnus-backlog-buffer)
62 (gnus-kill-buffer gnus-backlog-buffer))
63 (setq gnus-backlog-hashtb nil
64 gnus-backlog-articles nil))
66 (defun gnus-backlog-enter-article (group number buffer)
67 (when (and (numberp number)
68 (not (string-match "^nnvirtual" group)))
70 (let ((ident (intern (concat group ":" (int-to-string number))
73 (if (memq ident gnus-backlog-articles)
74 () ; It's already kept.
75 ;; Remove the oldest article, if necessary.
76 (and (numberp gnus-keep-backlog)
77 (>= (length gnus-backlog-articles) gnus-keep-backlog)
78 (gnus-backlog-remove-oldest-article))
79 (push ident gnus-backlog-articles)
80 ;; Insert the new article.
82 (set-buffer (gnus-backlog-buffer))
83 (let (buffer-read-only)
84 (goto-char (point-max))
88 (insert-buffer-substring buffer)
89 ;; Tag the beginning of the article with the ident.
91 (gnus-put-text-property b (1+ b) 'gnus-backlog ident)
92 (gnus-error 3 "Article %d is blank" number))))))))
94 (defun gnus-backlog-remove-oldest-article ()
96 (set-buffer (gnus-backlog-buffer))
97 (goto-char (point-min))
98 (if (zerop (buffer-size))
99 () ; The buffer is empty.
100 (let ((ident (get-text-property (point) 'gnus-backlog))
102 ;; Remove the ident from the list of articles.
104 (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
105 ;; Delete the article itself.
107 (point) (next-single-property-change
108 (1+ (point)) 'gnus-backlog nil (point-max)))))))
110 (defun gnus-backlog-remove-article (group number)
111 "Remove article NUMBER in GROUP from the backlog."
112 (when (numberp number)
114 (let ((ident (intern (concat group ":" (int-to-string number))
115 gnus-backlog-hashtb))
117 (when (memq ident gnus-backlog-articles)
118 ;; It was in the backlog.
120 (set-buffer (gnus-backlog-buffer))
121 (let (buffer-read-only)
122 (when (setq beg (text-property-any
123 (point-min) (point-max) 'gnus-backlog
125 ;; Find the end (i. e., the beginning of the next article).
127 (next-single-property-change
128 (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
129 (delete-region beg end)
132 (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))))))
134 (defun gnus-backlog-request-article (group number &optional buffer)
135 (when (and (numberp number)
136 (not (string-match "^nnvirtual" group)))
138 (let ((ident (intern (concat group ":" (int-to-string number))
139 gnus-backlog-hashtb))
141 (when (memq ident gnus-backlog-articles)
142 ;; It was in the backlog.
144 (set-buffer (gnus-backlog-buffer))
145 (if (not (setq beg (text-property-any
146 (point-min) (point-max) 'gnus-backlog
148 ;; It wasn't in the backlog after all.
150 (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
151 ;; Find the end (i. e., the beginning of the next article).
153 (next-single-property-change
154 (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
156 (and buffer (set-buffer buffer))
157 (let ((buffer-read-only nil))
159 (insert-buffer-substring gnus-backlog-buffer beg end)))
162 (provide 'gnus-bcklg)
164 ;;; gnus-bcklg.el ends here