* riece-xemacs.el (riece-make-overlay): New alias.
[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 (defgroup riece-keyword nil
33   "Highlight keyword in IRC buffer."
34   :group 'riece-vars)
35
36 (defcustom riece-keywords nil
37   "Keywords to be highlightened."
38   :type '(repeat string)
39   :group 'riece-keyword)
40
41 (defcustom riece-notify-keyword-functions nil
42   "Functions used to notify keyword match."
43   :type '(list function)
44   :group 'riece-keyword)
45
46 (defface riece-keyword-face
47   '((((class color))
48      (:foreground "red" :underline t))
49     (t
50      ()))
51   "Face used for highlightening matching keyword."
52   :group 'riece-highlight-faces)
53 (defvar riece-keyword-face 'riece-keyword-face)
54
55 ;;; The old XEmacs package doesn't have autoload setting for regexp-opt.
56 (autoload 'regexp-opt "regexp-opt")
57 (defun riece-keyword-message-filter (message)
58   (if riece-keywords
59       (let ((regexp (regexp-opt riece-keywords))
60             (index 0))
61         (while (string-match regexp (riece-message-text message) index)
62           (if (memq 'riece-highlight riece-addons)
63               (put-text-property (match-beginning 0) (match-end 0)
64                                  'riece-keyword t
65                                  (riece-message-text message)))
66           (save-match-data
67             (run-hook-with-args 'riece-notify-keyword-functions
68                                 (match-string 0 (riece-message-text message))))
69           (setq index (match-end 0)))))
70   message)
71
72 (defun riece-keyword-map-region (start end function)
73   (catch 'done
74     (while t
75       ;; Search for the beginning of the button region.
76       (unless (get-text-property start 'riece-keyword)
77         (setq start (next-single-property-change start 'riece-keyword
78                                                  nil end)))
79       (if (= start end)
80           (throw 'done nil))
81       ;; Search for the end of the button region.
82       (let ((button-end (next-single-property-change start 'riece-keyword
83                                                      nil end)))
84         (if (= button-end end)
85             (throw 'done nil))
86         (funcall function start button-end)
87         (setq start button-end)))))
88
89 (defun riece-keyword-scan-region (start end)
90   (riece-keyword-map-region
91    start end
92    (lambda (start end)
93      (riece-overlay-put (riece-make-overlay start end)
94                         'face riece-keyword-face))))
95
96 (defun riece-keyword-requires ()
97   (if (memq 'riece-highlight riece-addons)
98       '(riece-highlight)))
99
100 (defun riece-keyword-insinuate ()
101   (add-hook 'riece-message-filter-functions 'riece-keyword-message-filter)
102   (add-hook 'riece-after-insert-functions 'riece-keyword-scan-region))
103
104 (provide 'riece-keyword)
105
106 ;;; riece-keyword.el ends here