(gnus-dribble-read-file): Quote file-precious-flag.
[gnus] / lisp / gnus-bcklg.el
1 ;;; gnus-bcklg.el --- backlog functions for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33
34 ;;;
35 ;;; Buffering of read articles.
36 ;;;
37
38 (defvar gnus-backlog-buffer " *Gnus Backlog*")
39 (defvar gnus-backlog-articles nil)
40 (defvar gnus-backlog-hashtb nil)
41
42 (defun gnus-backlog-buffer ()
43   "Return the backlog buffer."
44   (or (get-buffer gnus-backlog-buffer)
45       (save-excursion
46         (set-buffer (gnus-get-buffer-create gnus-backlog-buffer))
47         (buffer-disable-undo)
48         (setq buffer-read-only t)
49         (get-buffer gnus-backlog-buffer))))
50
51 (defun gnus-backlog-setup ()
52   "Initialize backlog variables."
53   (unless gnus-backlog-hashtb
54     (setq gnus-backlog-hashtb (gnus-make-hashtable 1024))))
55
56 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
57
58 (defun gnus-backlog-shutdown ()
59   "Clear all backlog variables and buffers."
60   (interactive)
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))
65
66 (defun gnus-backlog-enter-article (group number buffer)
67   (when (and (numberp number)
68              (not (string-match "^nnvirtual" group)))
69     (gnus-backlog-setup)
70     (let ((ident (intern (concat group ":" (int-to-string number))
71                          gnus-backlog-hashtb))
72           b)
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.
81         (save-excursion
82           (set-buffer (gnus-backlog-buffer))
83           (let (buffer-read-only)
84             (goto-char (point-max))
85             (unless (bolp)
86               (insert "\n"))
87             (setq b (point))
88             (insert-buffer-substring buffer)
89             ;; Tag the beginning of the article with the ident.
90             (if (> (point-max) b)
91               (gnus-put-text-property b (1+ b) 'gnus-backlog ident)
92               (gnus-error 3 "Article %d is blank" number))))))))
93
94 (defun gnus-backlog-remove-oldest-article ()
95   (save-excursion
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))
101             buffer-read-only)
102         ;; Remove the ident from the list of articles.
103         (when ident
104           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
105         ;; Delete the article itself.
106         (delete-region
107          (point) (next-single-property-change
108                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
109
110 (defun gnus-backlog-remove-article (group number)
111   "Remove article NUMBER in GROUP from the backlog."
112   (when (numberp number)
113     (gnus-backlog-setup)
114     (let ((ident (intern (concat group ":" (int-to-string number))
115                          gnus-backlog-hashtb))
116           beg end)
117       (when (memq ident gnus-backlog-articles)
118         ;; It was in the backlog.
119         (save-excursion
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
124                              ident))
125               ;; Find the end (i. e., the beginning of the next article).
126               (setq end
127                     (next-single-property-change
128                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
129               (delete-region beg end)
130               ;; Return success.
131               t))
132           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))))))
133
134 (defun gnus-backlog-request-article (group number &optional buffer)
135   (when (and (numberp number)
136              (not (string-match "^nnvirtual" group)))
137     (gnus-backlog-setup)
138     (let ((ident (intern (concat group ":" (int-to-string number))
139                          gnus-backlog-hashtb))
140           beg end)
141       (when (memq ident gnus-backlog-articles)
142         ;; It was in the backlog.
143         (save-excursion
144           (set-buffer (gnus-backlog-buffer))
145           (if (not (setq beg (text-property-any
146                               (point-min) (point-max) 'gnus-backlog
147                               ident)))
148               ;; It wasn't in the backlog after all.
149               (ignore
150                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
151             ;; Find the end (i. e., the beginning of the next article).
152             (setq end
153                   (next-single-property-change
154                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
155         (save-excursion
156           (and buffer (set-buffer buffer))
157           (let ((buffer-read-only nil))
158             (erase-buffer)
159             (insert-buffer-substring gnus-backlog-buffer beg end)))
160         t))))
161
162 (provide 'gnus-bcklg)
163
164 ;;; arch-tag: 66259e56-505a-4bba-8a0d-3552c5b94e39
165 ;;; gnus-bcklg.el ends here