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