]> code.delx.au - gnu-emacs/blob - lisp/erc/erc-replace.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / erc / erc-replace.el
1 ;; erc-replace.el -- wash and massage messages inserted into the buffer
2
3 ;; Copyright (C) 2001, 2002, 2004, 2006, 2007,
4 ;; 2008 Free Software Foundation, Inc.
5
6 ;; Author: Andreas Fuchs <asf@void.at>
7 ;; Maintainer: Mario Lang (mlang@delysid.org)
8 ;; Keywords: IRC, client, Internet
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This module allows you to systematically replace text in incoming
30 ;; messages. Load erc-replace, and customize `erc-replace-alist'.
31 ;; Then add to your ~/.emacs:
32
33 ;; (require 'erc-replace)
34 ;; (erc-replace-mode 1)
35
36 ;;; Code:
37
38 (require 'erc)
39
40 (defgroup erc-replace nil
41 "Replace text from incoming messages"
42 :group 'erc)
43
44 (defcustom erc-replace-alist nil
45 "Alist describing text to be replaced in incoming messages.
46 This is useful for filters.
47
48 The alist has elements of the form (FROM . TO). FROM can be a regular
49 expression or a variable, or any sexp, TO can be a string or a
50 function to call, or any sexp. If a function, it will be called with
51 one argument, the string to be replaced, and it should return a
52 replacement string."
53 :group 'erc-replace
54 :type '(repeat (cons :tag "Search & Replace"
55 (choice :tag "From"
56 regexp
57 variable
58 sexp)
59 (choice :tag "To"
60 string
61 function
62 sexp))))
63
64 (defun erc-replace-insert ()
65 "Function to run from `erc-insert-modify-hook'.
66 It replaces text according to `erc-replace-alist'."
67 (mapcar (lambda (elt)
68 (goto-char (point-min))
69 (let ((from (car elt))
70 (to (cdr elt)))
71 (unless (stringp from)
72 (setq from (eval from)))
73 (while (re-search-forward from nil t)
74 (cond ((stringp to)
75 (replace-match to))
76 ((and (symbolp to) (fboundp to))
77 (replace-match (funcall to (match-string 0))))
78 (t
79 (eval to))))))
80 erc-replace-alist))
81
82 ;;;###autoload (autoload 'erc-replace-mode "erc-replace")
83 (define-erc-module replace nil
84 "This mode replaces incoming text according to `erc-replace-alist'."
85 ((add-hook 'erc-insert-modify-hook
86 'erc-replace-insert))
87 ((remove-hook 'erc-insert-modify-hook
88 'erc-replace-insert)))
89
90 (provide 'erc-replace)
91
92 ;;; erc-replace.el ends here
93 ;;
94 ;; Local Variables:
95 ;; indent-tabs-mode: t
96 ;; tab-width: 8
97 ;; End:
98
99 ;; arch-tag: dd904a59-d8a6-47f8-ac3a-76b698289a18