gnus-bcklg.el 5.0 KB

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