Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / erc / erc-truncate.el
CommitLineData
597993cf
MB
1;;; erc-truncate.el --- Functions for truncating ERC buffers
2
49f70d46 3;; Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
597993cf
MB
4
5;; Author: Andreas Fuchs <asf@void.at>
6;; Keywords: IRC, chat, client, Internet, logging
7
8;; This file is part of GNU Emacs.
9
4ee57b2a 10;; GNU Emacs is free software: you can redistribute it and/or modify
597993cf 11;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
597993cf
MB
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
4ee57b2a 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
597993cf
MB
22
23;;; Commentary:
24
25;; This implements buffer truncation (and optional log file writing
26;; support for the Emacs IRC client. Use `erc-truncate-mode' to switch
27;; on. Use `erc-enable-logging' to enable logging of the stuff which
28;; is getting truncated.
29
30;;; Code:
31
32(require 'erc)
33
34(defgroup erc-truncate nil
35 "Truncate buffers when they reach a certain size"
36 :group 'erc)
37
38(defcustom erc-max-buffer-size 30000
39 "*Maximum size in chars of each ERC buffer.
40Used only when auto-truncation is enabled.
41\(see `erc-truncate-buffer' and `erc-insert-post-hook')."
42 :group 'erc-truncate
43 :type 'integer)
44
45;;;###autoload (autoload 'erc-truncate-mode "erc-truncate" nil t)
46(define-erc-module truncate nil
47 "Truncate a query buffer if it gets too large.
48This prevents the query buffer from getting too large, which can
fcf8c778 49bring any grown Emacs to its knees after a few days worth of
597993cf
MB
50tracking heavy-traffic channels."
51 ;;enable
52 ((add-hook 'erc-insert-post-hook 'erc-truncate-buffer))
53 ;; disable
54 ((remove-hook 'erc-insert-post-hook 'erc-truncate-buffer)))
55
56;;;###autoload
57(defun erc-truncate-buffer-to-size (size &optional buffer)
58 "Truncates the buffer to the size SIZE.
59If BUFFER is not provided, the current buffer is assumed. The deleted
60region is logged if `erc-logging-enabled' returns non-nil."
61 ;; If buffer is non-nil, but get-buffer does not return anything,
62 ;; then this is a bug. If buffer is a buffer name, get the buffer
63 ;; object. If buffer is nil, use the current buffer.
64 (if (not buffer)
65 (setq buffer (current-buffer))
66 (unless (get-buffer buffer)
67 (error "erc-truncate-buffer-to-size: %S is not a buffer" buffer)))
68 (when (> (buffer-size buffer) (+ size 512))
937e6a56 69 (with-current-buffer buffer
597993cf
MB
70 ;; Note that when erc-insert-post-hook runs, the buffer is
71 ;; narrowed to the new message. So do this delicate widening.
fffa137c 72 ;; I am not sure, I think this was not recommended behavior in
597993cf
MB
73 ;; Emacs 20.
74 (save-restriction
75 (widen)
76 (let ((end (- erc-insert-marker size)))
77 ;; truncate at line boundaries
78 (goto-char end)
79 (beginning-of-line)
80 (setq end (point))
81 ;; try to save the current buffer using
82 ;; `erc-save-buffer-in-logs'. We use this, in case the
83 ;; user has both `erc-save-buffer-in-logs' and
84 ;; `erc-truncate-buffer' in `erc-insert-post-hook'. If
85 ;; this is the case, only the non-saved part of the current
86 ;; buffer should be saved. Rather than appending the
87 ;; deleted part of the buffer to the log file.
88 ;;
89 ;; Alternatively this could be made conditional on:
90 ;; (not (memq 'erc-save-buffer-in-logs
91 ;; erc-insert-post-hook))
92 ;; Comments?
93 (when (and (boundp 'erc-enable-logging)
94 erc-enable-logging
95 (erc-logging-enabled buffer))
96 (erc-save-buffer-in-logs))
97 ;; disable undoing for the truncating
98 (buffer-disable-undo)
99 (let ((inhibit-read-only t))
100 (delete-region (point-min) end)))
101 (buffer-enable-undo)))))
102
103;;;###autoload
104(defun erc-truncate-buffer ()
105 "Truncates the current buffer to `erc-max-buffer-size'.
106Meant to be used in hooks, like `erc-insert-post-hook'."
107 (interactive)
108 (erc-truncate-buffer-to-size erc-max-buffer-size))
109
110(provide 'erc-truncate)
111;;; erc-truncate.el ends here
112;;
113;; Local Variables:
114;; indent-tabs-mode: t
115;; tab-width: 8
116;; End:
117
118;; arch-tag: 22a2ea78-871f-4870-8f1e-efe534170311