erc-replace.el 2.8 KB

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