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