nndoc.el (nndoc-dissect-mime-parts-sub): Fix last change
[gnus] / lisp / gnus-bcklg.el
1 ;;; gnus-bcklg.el --- backlog functions for Gnus
2
3 ;; Copyright (C) 1996-2013 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 3 of the License, or
13 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28
29 (require 'gnus)
30
31 ;;;
32 ;;; Buffering of read articles.
33 ;;;
34
35 (defvar gnus-backlog-buffer " *Gnus Backlog*")
36 (defvar gnus-backlog-articles nil)
37 (defvar gnus-backlog-hashtb nil)
38
39 (defun gnus-backlog-buffer ()
40   "Return the backlog buffer."
41   (or (get-buffer gnus-backlog-buffer)
42       (with-current-buffer (gnus-get-buffer-create gnus-backlog-buffer)
43         (buffer-disable-undo)
44         (setq buffer-read-only t)
45         (get-buffer gnus-backlog-buffer))))
46
47 (defun gnus-backlog-setup ()
48   "Initialize backlog variables."
49   (unless gnus-backlog-hashtb
50     (setq gnus-backlog-hashtb (gnus-make-hashtable 1024))))
51
52 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
53
54 (defun gnus-backlog-shutdown ()
55   "Clear all backlog variables and buffers."
56   (interactive)
57   (when (get-buffer gnus-backlog-buffer)
58     (gnus-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   (when (and (numberp number)
64              (not (string-match "^nnvirtual" group)))
65     (gnus-backlog-setup)
66     (let ((ident (intern (concat group ":" (int-to-string number))
67                          gnus-backlog-hashtb))
68           b)
69       (if (memq ident gnus-backlog-articles)
70           ()                            ; It's already kept.
71       ;; Remove the oldest article, if necessary.
72         (and (numberp gnus-keep-backlog)
73              (>= (length gnus-backlog-articles) gnus-keep-backlog)
74            (gnus-backlog-remove-oldest-article))
75         (push ident gnus-backlog-articles)
76         ;; Insert the new article.
77         (with-current-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             (if (> (point-max) b)
86               (gnus-put-text-property b (1+ b) 'gnus-backlog ident)
87               (gnus-error 3 "Article %d is blank" number))))))))
88
89 (defun gnus-backlog-remove-oldest-article ()
90   (with-current-buffer (gnus-backlog-buffer)
91     (goto-char (point-min))
92     (if (zerop (buffer-size))
93         ()                              ; The buffer is empty.
94       (let ((ident (get-text-property (point) 'gnus-backlog))
95             buffer-read-only)
96         ;; Remove the ident from the list of articles.
97         (when ident
98           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
99         ;; Delete the article itself.
100         (delete-region
101          (point) (next-single-property-change
102                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
103
104 (defun gnus-backlog-remove-article (group number)
105   "Remove article NUMBER in GROUP from the backlog."
106   (when (numberp number)
107     (gnus-backlog-setup)
108     (let ((ident (intern (concat group ":" (int-to-string number))
109                          gnus-backlog-hashtb))
110           beg end)
111       (when (memq ident gnus-backlog-articles)
112         ;; It was in the backlog.
113         (with-current-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 &optional buffer)
128   (when (and (numberp number)
129              (not (string-match "^nnvirtual" group)))
130     (gnus-backlog-setup)
131     (let ((ident (intern (concat group ":" (int-to-string number))
132                          gnus-backlog-hashtb))
133           beg end)
134       (when (memq ident gnus-backlog-articles)
135         ;; It was in the backlog.
136         (with-current-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         (with-current-buffer (or (current-buffer) buffer)
148           (let ((buffer-read-only nil))
149             (erase-buffer)
150             (insert-buffer-substring gnus-backlog-buffer beg end)))
151         t))))
152
153 (provide 'gnus-bcklg)
154
155 ;;; gnus-bcklg.el ends here