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