Added Riece pkg subtree
[packages] / xemacs-packages / riece / lisp / riece-keyword.el
1 ;;; riece-keyword.el --- detect keywords in IRC buffers -*- lexical-binding: t -*-
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; NOTE: This is an add-on module for Riece.
28
29 ;;; Code:
30
31 (require 'riece-message)
32
33 (defgroup riece-keyword nil
34   "Detect keywords in IRC buffers."
35   :prefix "riece-"
36   :group 'riece)
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 '(repeat function)
48   :group 'riece-keyword)
49
50 (make-obsolete-variable 'riece-notify-keyword-functions
51                         'riece-keyword-notify-functions
52                         "2003-12-22")
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 '(repeat 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 (defconst riece-keyword-description
71   "Detect keywords in IRC buffers.")
72
73 ;;; The old XEmacs package doesn't have autoload setting for regexp-opt.
74 (autoload 'regexp-opt "regexp-opt")
75 (defun riece-keyword-message-filter (message)
76   (if (and (get 'riece-keyword 'riece-addon-enabled)
77            riece-keywords
78            ;; Ignore messages which belongs to myself.
79            (not (riece-message-own-p message)))
80       (let* (keywords
81              (alist
82               (nconc
83                (delq nil (mapcar
84                           (lambda (matcher)
85                             (if (stringp matcher)
86                                 (ignore
87                                  (setq keywords (cons matcher keywords)))
88                               matcher))
89                           riece-keywords))
90                (if keywords
91                    (list (cons (regexp-opt keywords) 0)))))
92              index)
93         (while alist
94           (setq index 0)
95           (while (and (< index (length (riece-message-text message)))
96                       (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             (save-match-data
103               (run-hook-with-args 'riece-notify-keyword-functions
104                                   (match-string (cdr (car alist))
105                                                 (riece-message-text message)))
106               (run-hook-with-args 'riece-keyword-notify-functions
107                                   (cdr (car alist))
108                                   message))
109             (setq index (1+ (match-end (cdr (car alist))))))
110           (setq alist (cdr alist)))))
111   message)
112
113 (defun riece-keyword-requires ()
114   (if (memq 'riece-highlight riece-addons)
115       '(riece-highlight)))
116
117 (defun riece-keyword-insinuate ()
118   (add-hook 'riece-message-filter-functions 'riece-keyword-message-filter))
119
120 (defun riece-keyword-uninstall ()
121   (remove-hook 'riece-message-filter-functions 'riece-keyword-message-filter))
122
123 (provide 'riece-keyword)
124
125 ;;; riece-keyword.el ends here