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