* riece-keyword.el (riece-keyword-message-filter): Ignore messages
[riece] / lisp / riece-keyword.el
1 ;;; riece-keyword.el --- highlight keywords in channel buffers
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; To use, add the following line to your ~/.riece/init.el:
28 ;; (add-to-list 'riece-addons 'riece-keyword)
29
30 ;;; Code:
31
32 (require 'riece-message)
33
34 (defgroup riece-keyword nil
35   "Highlight keyword in IRC buffer."
36   :group 'riece-vars)
37
38 (defcustom riece-keywords nil
39   "Keywords to be highlightened."
40   :type '(repeat string)
41   :group 'riece-keyword)
42
43 (defcustom riece-notify-keyword-functions nil
44   "Functions used to notify keyword match."
45   :type '(list function)
46   :group 'riece-keyword)
47
48 (defface riece-keyword-face
49   '((((class color))
50      (:foreground "red" :underline t))
51     (t
52      ()))
53   "Face used for highlightening matching keyword."
54   :group 'riece-highlight-faces)
55 (defvar riece-keyword-face 'riece-keyword-face)
56
57 ;;; The old XEmacs package doesn't have autoload setting for regexp-opt.
58 (autoload 'regexp-opt "regexp-opt")
59 (defun riece-keyword-message-filter (message)
60   (if (and riece-keywords
61            ;; Ignore messages which belongs to myself.
62            (riece-identity-equal (riece-message-speaker message)
63                                  riece-current-nickname))
64       (let ((regexp (regexp-opt riece-keywords))
65             (index 0))
66         (while (string-match regexp (riece-message-text message) index)
67           (if (memq 'riece-highlight riece-addons)
68               (put-text-property (match-beginning 0) (match-end 0)
69                                  'riece-keyword t
70                                  (riece-message-text message)))
71           (save-match-data
72             (run-hook-with-args 'riece-notify-keyword-functions
73                                 (match-string 0 (riece-message-text message))))
74           (setq index (match-end 0)))))
75   message)
76
77 (defun riece-keyword-scan-region (start end)
78   (riece-scan-property-region
79    'riece-keyword
80    start end
81    (lambda (start end)
82      (riece-overlay-put (riece-make-overlay start end)
83                         'face riece-keyword-face))))
84
85 (defun riece-keyword-requires ()
86   (if (memq 'riece-highlight riece-addons)
87       '(riece-highlight)))
88
89 (defun riece-keyword-insinuate ()
90   (add-hook 'riece-message-filter-functions 'riece-keyword-message-filter)
91   (add-hook 'riece-after-insert-functions 'riece-keyword-scan-region))
92
93 (provide 'riece-keyword)
94
95 ;;; riece-keyword.el ends here