erc-replace.el 2.8 KB

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