7fedd2f755395975bad593d568ea71a0d1ea3bdd
[riece] / lisp / riece-signal.el
1 ;;; riece-signal.el --- "signal-slot" abstraction for routing display events
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 ;;; This module implements Qt like "signal-slot" abstraction for
28 ;;; routing display events.
29
30 ;;; Code:
31
32 (defvar riece-signal-slot-obarray
33   (make-vector 31 0))
34
35 (defun riece-make-slot (function &optional filter handback)
36   "Make an instance of slot object.
37 Arguments are corresponding to callback function, filter function, and
38 a handback object, respectively.
39 This function is for internal use only."
40   (vector function filter handback))
41
42 (defun riece-slot-function (slot)
43   "Return the callback function of SLOT.
44 This function is for internal use only."
45   (aref slot 0))
46
47 (defun riece-slot-filter (slot)
48   "Return the filter function of SLOT.
49 This function is for internal use only."
50   (aref slot 1))
51
52 (defun riece-slot-handback (slot)
53   "Return the handback object of SLOT.
54 This function is for internal use only."
55   (aref slot 2))
56
57 (defun riece-make-signal (name args)
58   "Make an instance of signal object.
59 The 1st arguments is the name of the signal and the rest of arguments
60 are the data of the signal.
61 This function is for internal use only."
62   (vector name args))
63
64 (defun riece-signal-name (signal)
65   "Return the name of SIGNAL."
66   (aref signal 0))
67
68 (defun riece-signal-args (signal)
69   "Return the data of SIGNAL."
70   (aref signal 1))
71
72 (defun riece-connect-signal (signal-name function &optional filter handback)
73   "Add SLOT as a listener of a signal identified by SIGNAL-NAME."
74   (let ((symbol (intern (symbol-name signal-name) riece-signal-slot-obarray)))
75     (set symbol (cons (riece-make-slot function filter handback)
76                       (if (boundp symbol)
77                           (symbol-value symbol))))))
78
79 (defun riece-emit-signal (signal-name &rest args)
80   "Emit SIGNAL."
81   (let ((symbol (intern-soft (symbol-name signal-name)
82                              riece-signal-slot-obarray))
83         signal
84         slots)
85     (when symbol
86       (setq signal (riece-make-signal signal-name args)
87             slots (symbol-value symbol))
88       (while slots
89         (condition-case error
90             (if (or (null (riece-slot-filter (car slots)))
91                     (condition-case error
92                         (funcall (riece-slot-filter (car slots)) signal)
93                       (error
94                        (if riece-debug
95                            (message
96                             "Error occurred in signal filter for \"%S\": %S"
97                             signal-name error)))
98                       nil))
99                 (funcall (riece-slot-function (car slots))
100                          signal (riece-slot-handback (car slots))))
101           (error
102            (if riece-debug
103                (message "Error occurred in slot function for \"%S\": %S"
104                         signal-name error))))
105         (setq slots (cdr slots))))))
106
107 (provide 'riece-signal)
108
109 ;;; riece-signal.el ends here