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