*** empty log message ***
[gnus] / lisp / gnus-bcklg.el
1 ;;; gnus-bcklg.el --- backlog functions for Gnus
2 ;; Copyright (C) 1996,97,98 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   (gnus-backlog-setup)
65   (let ((ident (intern (concat group ":" (int-to-string number))
66                        gnus-backlog-hashtb))
67         b)
68     (if (memq ident gnus-backlog-articles)
69         ()                              ; It's already kept.
70       ;; Remove the oldest article, if necessary.
71       (and (numberp gnus-keep-backlog)
72            (>= (length gnus-backlog-articles) gnus-keep-backlog)
73            (gnus-backlog-remove-oldest-article))
74       (push ident gnus-backlog-articles)
75       ;; Insert the new article.
76       (save-excursion
77         (set-buffer (gnus-backlog-buffer))
78         (let (buffer-read-only)
79           (goto-char (point-max))
80           (unless (bolp)
81             (insert "\n"))
82           (setq b (point))
83           (insert-buffer-substring buffer)
84           ;; Tag the beginning of the article with the ident.
85           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
86
87 (defun gnus-backlog-remove-oldest-article ()
88   (save-excursion
89     (set-buffer (gnus-backlog-buffer))
90     (goto-char (point-min))
91     (if (zerop (buffer-size))
92         ()                              ; The buffer is empty.
93       (let ((ident (get-text-property (point) 'gnus-backlog))
94             buffer-read-only)
95         ;; Remove the ident from the list of articles.
96         (when ident
97           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
98         ;; Delete the article itself.
99         (delete-region
100          (point) (next-single-property-change
101                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
102
103 (defun gnus-backlog-remove-article (group number)
104   "Remove article NUMBER in GROUP from the backlog."
105   (when (numberp number)
106     (gnus-backlog-setup)
107     (let ((ident (intern (concat group ":" (int-to-string number))
108                          gnus-backlog-hashtb))
109           beg end)
110       (when (memq ident gnus-backlog-articles)
111         ;; It was in the backlog.
112         (save-excursion
113           (set-buffer (gnus-backlog-buffer))
114           (let (buffer-read-only)
115             (when (setq beg (text-property-any
116                              (point-min) (point-max) 'gnus-backlog
117                              ident))
118               ;; Find the end (i. e., the beginning of the next article).
119               (setq end
120                     (next-single-property-change
121                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
122               (delete-region beg end)
123               ;; Return success.
124               t))
125           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))))))
126
127 (defun gnus-backlog-request-article (group number buffer)
128   (when (numberp number)
129     (gnus-backlog-setup)
130     (let ((ident (intern (concat group ":" (int-to-string number))
131                          gnus-backlog-hashtb))
132           beg end)
133       (when (memq ident gnus-backlog-articles)
134         ;; It was in the backlog.
135         (save-excursion
136           (set-buffer (gnus-backlog-buffer))
137           (if (not (setq beg (text-property-any
138                               (point-min) (point-max) 'gnus-backlog
139                               ident)))
140               ;; It wasn't in the backlog after all.
141               (ignore
142                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
143             ;; Find the end (i. e., the beginning of the next article).
144             (setq end
145                   (next-single-property-change
146                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
147         (let ((buffer-read-only nil))
148           (erase-buffer)
149           (insert-buffer-substring gnus-backlog-buffer beg end)
150           t)))))
151
152 (provide 'gnus-bcklg)
153
154 ;;; gnus-bcklg.el ends here