*** empty log message ***
[gnus] / lisp / gnus-bcklg.el
1 ;;; gnus-bcklg.el --- backlog functions for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 (require 'gnus-load)
29
30 ;;;
31 ;;; Buffering of read articles.
32 ;;;
33
34 (defvar gnus-backlog-buffer " *Gnus Backlog*")
35 (defvar gnus-backlog-articles nil)
36 (defvar gnus-backlog-hashtb nil)
37
38 (defun gnus-backlog-buffer ()
39   "Return the backlog buffer."
40   (or (get-buffer gnus-backlog-buffer)
41       (save-excursion
42         (set-buffer (get-buffer-create gnus-backlog-buffer))
43         (buffer-disable-undo (current-buffer))
44         (setq buffer-read-only t)
45         (gnus-add-current-to-buffer-list)
46         (get-buffer gnus-backlog-buffer))))
47
48 (defun gnus-backlog-setup ()
49   "Initialize backlog variables."
50   (unless gnus-backlog-hashtb
51     (setq gnus-backlog-hashtb (make-vector 1023 0))))
52
53 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
54
55 (defun gnus-backlog-shutdown ()
56   "Clear all backlog variables and buffers."
57   (when (get-buffer gnus-backlog-buffer)
58     (kill-buffer gnus-backlog-buffer))
59   (setq gnus-backlog-hashtb nil
60         gnus-backlog-articles nil))
61
62 (defun gnus-backlog-enter-article (group number buffer)
63   (gnus-backlog-setup)
64   (let ((ident (intern (concat group ":" (int-to-string number))
65                        gnus-backlog-hashtb))
66         b)
67     (if (memq ident gnus-backlog-articles)
68         () ; It's already kept.
69       ;; Remove the oldest article, if necessary.
70       (and (numberp gnus-keep-backlog)
71            (>= (length gnus-backlog-articles) gnus-keep-backlog)
72            (gnus-backlog-remove-oldest-article))
73       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
74       ;; Insert the new article.
75       (save-excursion
76         (set-buffer (gnus-backlog-buffer))
77         (let (buffer-read-only)
78           (goto-char (point-max))
79           (or (bolp) (insert "\n"))
80           (setq b (point))
81           (insert-buffer-substring buffer)
82           ;; Tag the beginning of the article with the ident.
83           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
84
85 (defun gnus-backlog-remove-oldest-article ()
86   (save-excursion
87     (set-buffer (gnus-backlog-buffer))
88     (goto-char (point-min))
89     (if (zerop (buffer-size))
90         () ; The buffer is empty.
91       (let ((ident (get-text-property (point) 'gnus-backlog))
92             buffer-read-only)
93         ;; Remove the ident from the list of articles.
94         (when ident
95           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
96         ;; Delete the article itself.
97         (delete-region
98          (point) (next-single-property-change
99                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
100
101 (defun gnus-backlog-remove-article (group number)
102   "Remove article NUMBER in GROUP from the backlog."
103   (when (numberp number)
104     (gnus-backlog-setup)
105     (let ((ident (intern (concat group ":" (int-to-string number))
106                          gnus-backlog-hashtb))
107           beg end)
108       (when (memq ident gnus-backlog-articles)
109         ;; It was in the backlog.
110         (save-excursion
111           (set-buffer (gnus-backlog-buffer))
112           (let (buffer-read-only)
113             (when (setq beg (text-property-any
114                              (point-min) (point-max) 'gnus-backlog
115                              ident))
116               ;; Find the end (i. e., the beginning of the next article).
117               (setq end
118                     (next-single-property-change
119                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
120               (delete-region beg end)
121               ;; Return success.
122               t)))))))
123
124 (defun gnus-backlog-request-article (group number buffer)
125   (when (numberp number)
126     (gnus-backlog-setup)
127     (let ((ident (intern (concat group ":" (int-to-string number))
128                          gnus-backlog-hashtb))
129           beg end)
130       (when (memq ident gnus-backlog-articles)
131         ;; It was in the backlog.
132         (save-excursion
133           (set-buffer (gnus-backlog-buffer))
134           (if (not (setq beg (text-property-any
135                               (point-min) (point-max) 'gnus-backlog
136                               ident)))
137               ;; It wasn't in the backlog after all.
138               (ignore
139                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
140             ;; Find the end (i. e., the beginning of the next article).
141             (setq end
142                   (next-single-property-change
143                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
144         (let ((buffer-read-only nil))
145           (erase-buffer)
146           (insert-buffer-substring gnus-backlog-buffer beg end)
147           t)))))
148
149 (provide 'gnus-bcklg)
150
151 ;;; gnus-bcklg.el ends here