Initial Commit
[packages] / xemacs-packages / erc / erc-replace.el
1 ;; erc-replace.el -- wash and massage messages inserted into the buffer
2
3 ;; Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
4
5 ;; Author: Andreas Fuchs <asf@void.at>
6 ;; Maintainer: Mario Lang (mlang@delysid.org)
7 ;; Keywords: IRC, client, Internet
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcReplace
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 2, 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 (defconst erc-replace-version "$Revision: 1.8.2.1 $"
41   "Revision of the ERC replace module.")
42
43 (defgroup erc-replace nil
44   "Replace text from incoming messages"
45   :group 'erc)
46
47 (defcustom erc-replace-alist nil
48   "Alist describing text to be replaced in incoming messages.
49 This is useful for filters.
50
51 The alist has elements of the form (FROM . TO).  FROM can be a regular
52 expression or a variable, or any sexp, TO can be a string or a
53 function to call, or any sexp.  If a function, it will be called with
54 one argument, the string to be replaced, and it should return a
55 replacement string."
56   :group 'erc-replace
57   :type '(repeat (cons :tag "Search & Replace"
58                        (choice :tag "From"
59                                regexp
60                                variable
61                                sexp)
62                        (choice :tag "To"
63                                string
64                                function
65                                sexp))))
66
67 (defun erc-replace-insert ()
68   "Function to run from `erc-insert-modify-hook'.
69 It replaces text according to `erc-replace-alist'."
70   (mapcar (lambda (elt)
71             (goto-char (point-min))
72             (let ((from (car elt))
73                   (to (cdr elt)))
74               (unless (stringp from)
75                 (setq from (eval from)))
76               (while (re-search-forward from nil t)
77                 (cond ((stringp to)
78                        (replace-match to))
79                       ((and (symbolp to) (fboundp to))
80                        (replace-match (funcall to (match-string 0))))
81                       (t
82                        (eval to))))))
83           erc-replace-alist))
84
85 ;;;###autoload (autoload 'erc-replace-mode "erc-replace")
86 (define-erc-module replace nil
87   "This mode replaces incoming text according to `erc-replace-alist'."
88   ((add-hook 'erc-insert-modify-hook
89              'erc-replace-insert))
90   ((remove-hook 'erc-insert-modify-hook
91                 'erc-replace-insert)))
92
93 (provide 'erc-replace)
94
95 ;; arch-tag: dd904a59-d8a6-47f8-ac3a-76b698289a18
96 ;;; erc-replace.el ends here