* riece-display.el (riece-display-connect-signals): Update
[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 (choice (string :tag "Keyword")
41                          (cons (string :tag "Regexp")
42                                (integer :tag "Match"))))
43   :group 'riece-keyword)
44
45 (defcustom riece-notify-keyword-functions nil
46   "Functions used to notify keyword match."
47   :type '(list function)
48   :group 'riece-keyword)
49
50 (make-obsolete-variable 'riece-notify-keyword-functions
51                         'riece-keyword-notify-functions)
52
53 (defcustom riece-keyword-notify-functions nil
54   "Functions used to notify keyword match.
55 Two arguments are passed to each function: the keyword used to match
56 and the matched message object."
57   :type '(list function)
58   :group 'riece-keyword)
59
60 (defface riece-keyword-face
61   '((((class color))
62      (:foreground "red" :underline t))
63     (t
64      ()))
65   "Face used for highlightening matching keyword."
66   :group 'riece-highlight-faces)
67 (defvar riece-keyword-face 'riece-keyword-face)
68
69 ;;; The old XEmacs package doesn't have autoload setting for regexp-opt.
70 (autoload 'regexp-opt "regexp-opt")
71 (defun riece-keyword-message-filter (message)
72   (if (and riece-keywords
73            ;; Ignore messages which belongs to myself.
74            (not (riece-message-own-p message)))
75       (let* (keywords
76              (alist
77               (nconc
78                (delq nil (mapcar
79                           (lambda (matcher)
80                             (if (stringp matcher)
81                                 (ignore
82                                  (setq keywords (cons matcher keywords)))
83                               matcher))
84                           riece-keywords))
85                (list (cons (regexp-opt keywords) 0))))
86              index)
87         (while alist
88           (setq index 0)
89           (while (string-match (car (car alist))
90                                (riece-message-text message) index)
91             (if (memq 'riece-highlight riece-addons)
92                 (put-text-property (match-beginning (cdr (car alist)))
93                                    (match-end (cdr (car alist)))
94                                    'riece-keyword t
95                                    (riece-message-text message)))
96             (run-hook-with-args 'riece-notify-keyword-functions
97                                 (match-string (cdr (car alist))
98                                               (riece-message-text message)))
99             (run-hook-with-args 'riece-keyword-notify-functions
100                                 (cdr (car alist))
101                                 message)
102             (setq index (match-end (cdr (car alist)))))
103           (setq alist (cdr alist)))))
104   message)
105
106 (defun riece-keyword-scan-region (start end)
107   (riece-scan-property-region
108    'riece-keyword
109    start end
110    (lambda (start end)
111      (riece-overlay-put (riece-make-overlay start end)
112                         'face riece-keyword-face))))
113
114 (defun riece-keyword-requires ()
115   (if (memq 'riece-highlight riece-addons)
116       '(riece-highlight)))
117
118 (defun riece-keyword-insinuate ()
119   (add-hook 'riece-message-filter-functions 'riece-keyword-message-filter)
120   (add-hook 'riece-after-insert-functions 'riece-keyword-scan-region))
121
122 (provide 'riece-keyword)
123
124 ;;; riece-keyword.el ends here