erc-truncate.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; erc-truncate.el --- Functions for truncating ERC buffers
  2. ;; Copyright (C) 2003-2004, 2006-2015 Free Software Foundation, Inc.
  3. ;; Author: Andreas Fuchs <asf@void.at>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: IRC, chat, client, Internet, logging
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This implements buffer truncation (and optional log file writing
  19. ;; support for the Emacs IRC client. Use `erc-truncate-mode' to switch
  20. ;; on. Use `erc-enable-logging' to enable logging of the stuff which
  21. ;; is getting truncated.
  22. ;;; Code:
  23. (require 'erc)
  24. (defgroup erc-truncate nil
  25. "Truncate buffers when they reach a certain size"
  26. :group 'erc)
  27. (defcustom erc-max-buffer-size 30000
  28. "Maximum size in chars of each ERC buffer.
  29. Used only when auto-truncation is enabled.
  30. \(see `erc-truncate-buffer' and `erc-insert-post-hook')."
  31. :group 'erc-truncate
  32. :type 'integer)
  33. ;;;###autoload (autoload 'erc-truncate-mode "erc-truncate" nil t)
  34. (define-erc-module truncate nil
  35. "Truncate a query buffer if it gets too large.
  36. This prevents the query buffer from getting too large, which can
  37. bring any grown Emacs to its knees after a few days worth of
  38. tracking heavy-traffic channels."
  39. ;;enable
  40. ((add-hook 'erc-insert-post-hook 'erc-truncate-buffer))
  41. ;; disable
  42. ((remove-hook 'erc-insert-post-hook 'erc-truncate-buffer)))
  43. ;;;###autoload
  44. (defun erc-truncate-buffer-to-size (size &optional buffer)
  45. "Truncates the buffer to the size SIZE.
  46. If BUFFER is not provided, the current buffer is assumed. The deleted
  47. region is logged if `erc-logging-enabled' returns non-nil."
  48. ;; If buffer is non-nil, but get-buffer does not return anything,
  49. ;; then this is a bug. If buffer is a buffer name, get the buffer
  50. ;; object. If buffer is nil, use the current buffer.
  51. (if (not buffer)
  52. (setq buffer (current-buffer))
  53. (unless (get-buffer buffer)
  54. (error "erc-truncate-buffer-to-size: %S is not a buffer" buffer)))
  55. (when (> (buffer-size buffer) (+ size 512))
  56. (with-current-buffer buffer
  57. ;; Note that when erc-insert-post-hook runs, the buffer is
  58. ;; narrowed to the new message. So do this delicate widening.
  59. ;; I am not sure, I think this was not recommended behavior in
  60. ;; Emacs 20.
  61. (save-restriction
  62. (widen)
  63. (let ((end (- erc-insert-marker size)))
  64. ;; truncate at line boundaries
  65. (goto-char end)
  66. (beginning-of-line)
  67. (setq end (point))
  68. ;; try to save the current buffer using
  69. ;; `erc-save-buffer-in-logs'. We use this, in case the
  70. ;; user has both `erc-save-buffer-in-logs' and
  71. ;; `erc-truncate-buffer' in `erc-insert-post-hook'. If
  72. ;; this is the case, only the non-saved part of the current
  73. ;; buffer should be saved. Rather than appending the
  74. ;; deleted part of the buffer to the log file.
  75. ;;
  76. ;; Alternatively this could be made conditional on:
  77. ;; (not (memq 'erc-save-buffer-in-logs
  78. ;; erc-insert-post-hook))
  79. ;; Comments?
  80. (when (and (boundp 'erc-enable-logging)
  81. erc-enable-logging
  82. (erc-logging-enabled buffer))
  83. (erc-save-buffer-in-logs))
  84. ;; disable undoing for the truncating
  85. (buffer-disable-undo)
  86. (let ((inhibit-read-only t))
  87. (delete-region (point-min) end)))
  88. (buffer-enable-undo)))))
  89. ;;;###autoload
  90. (defun erc-truncate-buffer ()
  91. "Truncates the current buffer to `erc-max-buffer-size'.
  92. Meant to be used in hooks, like `erc-insert-post-hook'."
  93. (interactive)
  94. (erc-truncate-buffer-to-size erc-max-buffer-size))
  95. (provide 'erc-truncate)
  96. ;;; erc-truncate.el ends here
  97. ;;
  98. ;; Local Variables:
  99. ;; indent-tabs-mode: t
  100. ;; tab-width: 8
  101. ;; End: