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