Importing Oort Gnus v0.01.
[elisp/gnus.git-] / lisp / gnus-bcklg.el
1 ;;; gnus-bcklg.el --- backlog functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31
32 ;;;
33 ;;; Buffering of read articles.
34 ;;;
35
36 (defvar gnus-backlog-buffer " *Gnus Backlog*")
37 (defvar gnus-backlog-articles nil)
38 (defvar gnus-backlog-hashtb nil)
39
40 (defun gnus-backlog-buffer ()
41   "Return the backlog buffer."
42   (or (get-buffer gnus-backlog-buffer)
43       (save-excursion
44         (set-buffer (gnus-get-buffer-create gnus-backlog-buffer))
45         (buffer-disable-undo)
46         (setq buffer-read-only t)
47         (get-buffer gnus-backlog-buffer))))
48
49 (defun gnus-backlog-setup ()
50   "Initialize backlog variables."
51   (unless gnus-backlog-hashtb
52     (setq gnus-backlog-hashtb (gnus-make-hashtable 1024))))
53
54 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
55
56 (defun gnus-backlog-shutdown ()
57   "Clear all backlog variables and buffers."
58   (when (get-buffer gnus-backlog-buffer)
59     (kill-buffer gnus-backlog-buffer))
60   (setq gnus-backlog-hashtb nil
61         gnus-backlog-articles nil))
62
63 (defun gnus-backlog-enter-article (group number buffer)
64   (when (and (numberp number)
65              (not (string-match "^nnvirtual" group)))
66     (gnus-backlog-setup)
67     (let ((ident (intern (concat group ":" (int-to-string number))
68                          gnus-backlog-hashtb))
69           b)
70       (if (memq ident gnus-backlog-articles)
71           ()                            ; It's already kept.
72       ;; Remove the oldest article, if necessary.
73         (and (numberp gnus-keep-backlog)
74              (>= (length gnus-backlog-articles) gnus-keep-backlog)
75            (gnus-backlog-remove-oldest-article))
76         (push ident gnus-backlog-articles)
77         ;; Insert the new article.
78         (save-excursion
79           (set-buffer (gnus-backlog-buffer))
80           (let (buffer-read-only)
81             (goto-char (point-max))
82             (unless (bolp)
83               (insert "\n"))
84             (setq b (point))
85             (insert-buffer-substring buffer)
86             ;; Tag the beginning of the article with the ident.
87             (if (> (point-max) b)
88               (gnus-put-text-property b (1+ b) 'gnus-backlog ident)
89               (gnus-error 3 "Article %d is blank" number))))))))
90
91 (defun gnus-backlog-remove-oldest-article ()
92   (save-excursion
93     (set-buffer (gnus-backlog-buffer))
94     (goto-char (point-min))
95     (if (zerop (buffer-size))
96         ()                              ; The buffer is empty.
97       (let ((ident (get-text-property (point) 'gnus-backlog))
98             buffer-read-only)
99         ;; Remove the ident from the list of articles.
100         (when ident
101           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
102         ;; Delete the article itself.
103         (delete-region
104          (point) (next-single-property-change
105                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
106
107 (defun gnus-backlog-remove-article (group number)
108   "Remove article NUMBER in GROUP from the backlog."
109   (when (numberp number)
110     (gnus-backlog-setup)
111     (let ((ident (intern (concat group ":" (int-to-string number))
112                          gnus-backlog-hashtb))
113           beg end)
114       (when (memq ident gnus-backlog-articles)
115         ;; It was in the backlog.
116         (save-excursion
117           (set-buffer (gnus-backlog-buffer))
118           (let (buffer-read-only)
119             (when (setq beg (text-property-any
120                              (point-min) (point-max) 'gnus-backlog
121                              ident))
122               ;; Find the end (i. e., the beginning of the next article).
123               (setq end
124                     (next-single-property-change
125                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
126               (delete-region beg end)
127               ;; Return success.
128               t))
129           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))))))
130
131 (defun gnus-backlog-request-article (group number &optional buffer)
132   (when (and (numberp number)
133              (not (string-match "^nnvirtual" group)))
134     (gnus-backlog-setup)
135     (let ((ident (intern (concat group ":" (int-to-string number))
136                          gnus-backlog-hashtb))
137           beg end)
138       (when (memq ident gnus-backlog-articles)
139         ;; It was in the backlog.
140         (save-excursion
141           (set-buffer (gnus-backlog-buffer))
142           (if (not (setq beg (text-property-any
143                               (point-min) (point-max) 'gnus-backlog
144                               ident)))
145               ;; It wasn't in the backlog after all.
146               (ignore
147                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
148             ;; Find the end (i. e., the beginning of the next article).
149             (setq end
150                   (next-single-property-change
151                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
152         (save-excursion
153           (and buffer (set-buffer buffer))
154           (let ((buffer-read-only nil))
155             (erase-buffer)
156             (insert-buffer-substring gnus-backlog-buffer beg end)))
157         t))))
158
159 (provide 'gnus-bcklg)
160
161 ;;; gnus-bcklg.el ends here