Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / gnus / gnus-bcklg.el
CommitLineData
eec82323 1;;; gnus-bcklg.el --- backlog functions for Gnus
e84b4b86
TTN
2
3;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5df4f04c 4;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
7;; Keywords: news
8
9;; This file is part of GNU Emacs.
10
5e809f55 11;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 12;; it under the terms of the GNU General Public License as published by
5e809f55
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
eec82323
LMI
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
5e809f55 18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eec82323
LMI
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
5e809f55 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
23
24;;; Commentary:
25
26;;; Code:
27
7df7482d
RS
28(eval-when-compile (require 'cl))
29
eec82323
LMI
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
6748645f 44 (set-buffer (gnus-get-buffer-create gnus-backlog-buffer))
16409b0b 45 (buffer-disable-undo)
eec82323 46 (setq buffer-read-only t)
eec82323
LMI
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."
23f87bed 58 (interactive)
eec82323 59 (when (get-buffer gnus-backlog-buffer)
23f87bed 60 (gnus-kill-buffer gnus-backlog-buffer))
eec82323
LMI
61 (setq gnus-backlog-hashtb nil
62 gnus-backlog-articles nil))
63
64(defun gnus-backlog-enter-article (group number buffer)
23f87bed
MB
65 (when (and (numberp number)
66 (not (string-match "^nnvirtual" group)))
67 (gnus-backlog-setup)
68 (let ((ident (intern (concat group ":" (int-to-string number))
69 gnus-backlog-hashtb))
70 b)
71 (if (memq ident gnus-backlog-articles)
72 () ; It's already kept.
eec82323 73 ;; Remove the oldest article, if necessary.
23f87bed
MB
74 (and (numberp gnus-keep-backlog)
75 (>= (length gnus-backlog-articles) gnus-keep-backlog)
eec82323 76 (gnus-backlog-remove-oldest-article))
23f87bed
MB
77 (push ident gnus-backlog-articles)
78 ;; Insert the new article.
79 (save-excursion
80 (set-buffer (gnus-backlog-buffer))
81 (let (buffer-read-only)
82 (goto-char (point-max))
83 (unless (bolp)
84 (insert "\n"))
85 (setq b (point))
86 (insert-buffer-substring buffer)
87 ;; Tag the beginning of the article with the ident.
88 (if (> (point-max) b)
16409b0b 89 (gnus-put-text-property b (1+ b) 'gnus-backlog ident)
23f87bed 90 (gnus-error 3 "Article %d is blank" number))))))))
eec82323
LMI
91
92(defun gnus-backlog-remove-oldest-article ()
93 (save-excursion
94 (set-buffer (gnus-backlog-buffer))
95 (goto-char (point-min))
96 (if (zerop (buffer-size))
97 () ; The buffer is empty.
98 (let ((ident (get-text-property (point) 'gnus-backlog))
99 buffer-read-only)
100 ;; Remove the ident from the list of articles.
101 (when ident
102 (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
103 ;; Delete the article itself.
104 (delete-region
105 (point) (next-single-property-change
106 (1+ (point)) 'gnus-backlog nil (point-max)))))))
107
108(defun gnus-backlog-remove-article (group number)
109 "Remove article NUMBER in GROUP from the backlog."
110 (when (numberp number)
111 (gnus-backlog-setup)
112 (let ((ident (intern (concat group ":" (int-to-string number))
113 gnus-backlog-hashtb))
114 beg end)
115 (when (memq ident gnus-backlog-articles)
116 ;; It was in the backlog.
117 (save-excursion
118 (set-buffer (gnus-backlog-buffer))
119 (let (buffer-read-only)
120 (when (setq beg (text-property-any
121 (point-min) (point-max) 'gnus-backlog
122 ident))
123 ;; Find the end (i. e., the beginning of the next article).
124 (setq end
125 (next-single-property-change
126 (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
127 (delete-region beg end)
128 ;; Return success.
6748645f
LMI
129 t))
130 (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))))))
eec82323 131
16409b0b 132(defun gnus-backlog-request-article (group number &optional buffer)
23f87bed
MB
133 (when (and (numberp number)
134 (not (string-match "^nnvirtual" group)))
eec82323
LMI
135 (gnus-backlog-setup)
136 (let ((ident (intern (concat group ":" (int-to-string number))
137 gnus-backlog-hashtb))
138 beg end)
139 (when (memq ident gnus-backlog-articles)
140 ;; It was in the backlog.
141 (save-excursion
142 (set-buffer (gnus-backlog-buffer))
143 (if (not (setq beg (text-property-any
144 (point-min) (point-max) 'gnus-backlog
145 ident)))
146 ;; It wasn't in the backlog after all.
147 (ignore
148 (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
149 ;; Find the end (i. e., the beginning of the next article).
150 (setq end
151 (next-single-property-change
152 (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16409b0b
GM
153 (save-excursion
154 (and buffer (set-buffer buffer))
155 (let ((buffer-read-only nil))
156 (erase-buffer)
157 (insert-buffer-substring gnus-backlog-buffer beg end)))
158 t))))
eec82323
LMI
159
160(provide 'gnus-bcklg)
161
cbee283d 162;; arch-tag: 66259e56-505a-4bba-8a0d-3552c5b94e39
eec82323 163;;; gnus-bcklg.el ends here