gnus-dup.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ;;; gnus-dup.el --- suppression of duplicate articles in 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. ;; This package tries to mark articles as read the second time the
  18. ;; user reads a copy. This is useful if the server doesn't support
  19. ;; Xref properly, or if the user reads the same group from several
  20. ;; servers.
  21. ;;; Code:
  22. (eval-when-compile (require 'cl))
  23. (require 'gnus)
  24. (require 'gnus-art)
  25. (defgroup gnus-duplicate nil
  26. "Suppression of duplicate articles."
  27. :group 'gnus)
  28. (defcustom gnus-save-duplicate-list nil
  29. "*If non-nil, save the duplicate list when shutting down Gnus.
  30. If nil, duplicate suppression will only work on duplicates
  31. seen in the same session."
  32. :group 'gnus-duplicate
  33. :type 'boolean)
  34. (defcustom gnus-duplicate-list-length 10000
  35. "*The number of Message-IDs to keep in the duplicate suppression list."
  36. :group 'gnus-duplicate
  37. :type 'integer)
  38. (defcustom gnus-duplicate-file (nnheader-concat gnus-directory "suppression")
  39. "*The name of the file to store the duplicate suppression list."
  40. :group 'gnus-duplicate
  41. :type 'file)
  42. ;;; Internal variables
  43. (defvar gnus-dup-list nil)
  44. (defvar gnus-dup-hashtb nil)
  45. (defvar gnus-dup-list-dirty nil)
  46. ;;;
  47. ;;; Starting and stopping
  48. ;;;
  49. (gnus-add-shutdown 'gnus-dup-close 'gnus)
  50. (defun gnus-dup-close ()
  51. "Possibly save the duplicate suppression list and shut down the subsystem."
  52. (gnus-dup-save)
  53. (setq gnus-dup-list nil
  54. gnus-dup-hashtb nil
  55. gnus-dup-list-dirty nil))
  56. (defun gnus-dup-open ()
  57. "Possibly read the duplicate suppression list and start the subsystem."
  58. (if gnus-save-duplicate-list
  59. (gnus-dup-read)
  60. (setq gnus-dup-list nil))
  61. (setq gnus-dup-hashtb (gnus-make-hashtable gnus-duplicate-list-length))
  62. ;; Enter all Message-IDs into the hash table.
  63. (let ((obarray gnus-dup-hashtb))
  64. (mapc 'intern gnus-dup-list)))
  65. (defun gnus-dup-read ()
  66. "Read the duplicate suppression list."
  67. (setq gnus-dup-list nil)
  68. (when (file-exists-p gnus-duplicate-file)
  69. (load gnus-duplicate-file t t t)))
  70. (defun gnus-dup-save ()
  71. "Save the duplicate suppression list."
  72. (when (and gnus-save-duplicate-list
  73. gnus-dup-list-dirty)
  74. (with-temp-file gnus-duplicate-file
  75. (gnus-prin1 `(setq gnus-dup-list ',gnus-dup-list))))
  76. (setq gnus-dup-list-dirty nil))
  77. ;;;
  78. ;;; Interface functions
  79. ;;;
  80. (defun gnus-dup-enter-articles ()
  81. "Enter articles from the current group for future duplicate suppression."
  82. (unless gnus-dup-list
  83. (gnus-dup-open))
  84. (setq gnus-dup-list-dirty t) ; mark list for saving
  85. (let (msgid)
  86. ;; Enter the Message-IDs of all read articles into the list
  87. ;; and hash table.
  88. (dolist (datum gnus-newsgroup-data)
  89. (when (and (not (gnus-data-pseudo-p datum))
  90. (> (gnus-data-number datum) 0)
  91. (not (memq (gnus-data-number datum) gnus-newsgroup-unreads))
  92. (not (= (gnus-data-mark datum) gnus-canceled-mark))
  93. (setq msgid (mail-header-id (gnus-data-header datum)))
  94. (not (nnheader-fake-message-id-p msgid))
  95. (not (intern-soft msgid gnus-dup-hashtb)))
  96. (push msgid gnus-dup-list)
  97. (intern msgid gnus-dup-hashtb))))
  98. ;; Chop off excess Message-IDs from the list.
  99. (let ((end (nthcdr gnus-duplicate-list-length gnus-dup-list)))
  100. (when end
  101. (mapc (lambda (id) (unintern id gnus-dup-hashtb)) (cdr end))
  102. (setcdr end nil))))
  103. (defun gnus-dup-suppress-articles ()
  104. "Mark duplicate articles as read."
  105. (unless gnus-dup-list
  106. (gnus-dup-open))
  107. (gnus-message 8 "Suppressing duplicates...")
  108. (let ((auto (and gnus-newsgroup-auto-expire
  109. (memq gnus-duplicate-mark gnus-auto-expirable-marks)))
  110. number)
  111. (dolist (header gnus-newsgroup-headers)
  112. (when (and (intern-soft (mail-header-id header) gnus-dup-hashtb)
  113. (gnus-summary-article-unread-p (mail-header-number header)))
  114. (setq gnus-newsgroup-unreads
  115. (delq (setq number (mail-header-number header))
  116. gnus-newsgroup-unreads))
  117. (if (not auto)
  118. (push (cons number gnus-duplicate-mark) gnus-newsgroup-reads)
  119. (push number gnus-newsgroup-expirable)
  120. (push (cons number gnus-expirable-mark) gnus-newsgroup-reads)))))
  121. (gnus-message 8 "Suppressing duplicates...done"))
  122. (defun gnus-dup-unsuppress-article (article)
  123. "Stop suppression of ARTICLE."
  124. (let* ((header (gnus-data-header (gnus-data-find article)))
  125. (id (when header (mail-header-id header))))
  126. (when id
  127. (setq gnus-dup-list-dirty t)
  128. (setq gnus-dup-list (delete id gnus-dup-list))
  129. (unintern id gnus-dup-hashtb))))
  130. (provide 'gnus-dup)
  131. ;;; gnus-dup.el ends here