Mercurial > emacs
comparison lisp/erc/erc-truncate.el @ 68451:fc745b05e928
Revision: emacs@sv.gnu.org/emacs--devo--0--patch-22
Creator: Michael Olson <mwolson@gnu.org>
Install ERC.
author | Miles Bader <miles@gnu.org> |
---|---|
date | Sun, 29 Jan 2006 13:08:58 +0000 |
parents | |
children | 7010bb070445 |
comparison
equal
deleted
inserted
replaced
68450:a3ba4ef5d590 | 68451:fc745b05e928 |
---|---|
1 ;;; erc-truncate.el --- Functions for truncating ERC buffers | |
2 | |
3 ;; Copyright (C) 2003, 2004 Free Software Foundation, Inc. | |
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 | |
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) | |
13 ;; any later version. | |
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 | |
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. | |
24 | |
25 ;;; Commentary: | |
26 | |
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. | |
31 | |
32 ;;; Code: | |
33 | |
34 (require 'erc) | |
35 | |
36 (defgroup erc-truncate nil | |
37 "Truncate buffers when they reach a certain size" | |
38 :group 'erc) | |
39 | |
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')." | |
44 :group 'erc-truncate | |
45 :type 'integer) | |
46 | |
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." | |
53 ;;enable | |
54 ((add-hook 'erc-insert-post-hook 'erc-truncate-buffer)) | |
55 ;; disable | |
56 ((remove-hook 'erc-insert-post-hook 'erc-truncate-buffer))) | |
57 | |
58 ;;;###autoload | |
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. | |
66 (if (not 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)) | |
71 (save-excursion | |
72 (set-buffer buffer) | |
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 | |
76 ;; Emacs 20. | |
77 (save-restriction | |
78 (widen) | |
79 (let ((end (- erc-insert-marker size))) | |
80 ;; truncate at line boundaries | |
81 (goto-char end) | |
82 (beginning-of-line) | |
83 (setq end (point)) | |
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. | |
91 ;; | |
92 ;; Alternatively this could be made conditional on: | |
93 ;; (not (memq 'erc-save-buffer-in-logs | |
94 ;; erc-insert-post-hook)) | |
95 ;; Comments? | |
96 (when (and (boundp 'erc-enable-logging) | |
97 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))))) | |
105 | |
106 ;;;###autoload | |
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'." | |
110 (interactive) | |
111 (erc-truncate-buffer-to-size erc-max-buffer-size)) | |
112 | |
113 (provide 'erc-truncate) | |
114 ;;; erc-truncate.el ends here | |
115 ;; | |
116 ;; Local Variables: | |
117 ;; indent-tabs-mode: t | |
118 ;; tab-width: 8 | |
119 ;; End: | |
120 | |
121 ;; arch-tag: 22a2ea78-871f-4870-8f1e-efe534170311 |