1 ;;; erc-truncate.el --- Functions for truncating ERC buffers
3 ;; Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
5 ;; Author: Andreas Fuchs <asf@void.at>
6 ;; Keywords: IRC, chat, client, Internet, logging
8 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This implements buffer truncation (and optional log file writing
28 ;; support for the Emacs IRC client. Use `erc-truncate-mode' to switch
29 ;; on. Use `erc-enable-logging' to enable logging of the stuff which
30 ;; is getting truncated.
36 (defgroup erc-truncate nil
37 "Truncate buffers when they reach a certain size"
40 (defcustom erc-max-buffer-size
30000
41 "*Maximum size in chars of each ERC buffer.
42 Used only when auto-truncation is enabled.
43 \(see `erc-truncate-buffer' and `erc-insert-post-hook')."
47 ;;;###autoload (autoload 'erc-truncate-mode "erc-truncate" nil t)
48 (define-erc-module truncate nil
49 "Truncate a query buffer if it gets too large.
50 This prevents the query buffer from getting too large, which can
51 bring any grown emacs to its knees after a few days worth of
52 tracking heavy-traffic channels."
54 ((add-hook 'erc-insert-post-hook
'erc-truncate-buffer
))
56 ((remove-hook 'erc-insert-post-hook
'erc-truncate-buffer
)))
59 (defun erc-truncate-buffer-to-size (size &optional buffer
)
60 "Truncates the buffer to the size SIZE.
61 If BUFFER is not provided, the current buffer is assumed. The deleted
62 region is logged if `erc-logging-enabled' returns non-nil."
63 ;; If buffer is non-nil, but get-buffer does not return anything,
64 ;; then this is a bug. If buffer is a buffer name, get the buffer
65 ;; object. If buffer is nil, use the current buffer.
67 (setq buffer
(current-buffer))
68 (unless (get-buffer buffer
)
69 (error "erc-truncate-buffer-to-size: %S is not a buffer" buffer
)))
70 (when (> (buffer-size buffer
) (+ size
512))
73 ;; Note that when erc-insert-post-hook runs, the buffer is
74 ;; narrowed to the new message. So do this delicate widening.
75 ;; I am not sure, I think this was not recommended behaviour in
79 (let ((end (- erc-insert-marker size
)))
80 ;; truncate at line boundaries
84 ;; try to save the current buffer using
85 ;; `erc-save-buffer-in-logs'. We use this, in case the
86 ;; user has both `erc-save-buffer-in-logs' and
87 ;; `erc-truncate-buffer' in `erc-insert-post-hook'. If
88 ;; this is the case, only the non-saved part of the current
89 ;; buffer should be saved. Rather than appending the
90 ;; deleted part of the buffer to the log file.
92 ;; Alternatively this could be made conditional on:
93 ;; (not (memq 'erc-save-buffer-in-logs
94 ;; erc-insert-post-hook))
96 (when (and (boundp 'erc-enable-logging
)
98 (erc-logging-enabled buffer
))
99 (erc-save-buffer-in-logs))
100 ;; disable undoing for the truncating
101 (buffer-disable-undo)
102 (let ((inhibit-read-only t
))
103 (delete-region (point-min) end
)))
104 (buffer-enable-undo)))))
107 (defun erc-truncate-buffer ()
108 "Truncates the current buffer to `erc-max-buffer-size'.
109 Meant to be used in hooks, like `erc-insert-post-hook'."
111 (erc-truncate-buffer-to-size erc-max-buffer-size
))
113 (provide 'erc-truncate
)
114 ;;; erc-truncate.el ends here
117 ;; indent-tabs-mode: t
121 ;; arch-tag: 22a2ea78-871f-4870-8f1e-efe534170311