* riece-signal.el (riece-connect-signal): Fixed doc.
[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 FUNCTION 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-disconnect-signal (signal-name function)
82   "Remove FUNCTION from the listener of the signal identified by SIGNAL-NAME."
83   (let* ((symbol (intern-soft (symbol-name signal-name)
84                              riece-signal-slot-obarray))
85          (slots (symbol-value symbol)))
86     (while slots
87       (if (eq (riece-slot-function (car slots))
88               function)
89           (set symbol (delq (car slots) (symbol-value symbol))))
90       (setq slots (cdr slots)))))
91
92 (defun riece-emit-signal (signal-name &rest args)
93   "Emit SIGNAL."
94   (let ((symbol (intern-soft (symbol-name signal-name)
95                              riece-signal-slot-obarray))
96         signal
97         slots)
98     (when symbol
99       (setq signal (riece-make-signal signal-name args)
100             slots (symbol-value symbol))
101       (while slots
102         (condition-case error
103             (if (or (null (riece-slot-filter (car slots)))
104                     (condition-case error
105                         (funcall (riece-slot-filter (car slots)) signal)
106                       (error
107                        (if riece-debug
108                            (message
109                             "Error in signal filter for \"%S\": %S"
110                             signal-name error)))
111                       nil))
112                 (funcall (riece-slot-function (car slots))
113                          signal (riece-slot-handback (car slots))))
114           (error
115            (if riece-debug
116                (message "Error in slot function for \"%S\": %S"
117                         signal-name error))))
118         (setq slots (cdr slots))))))
119
120 (provide 'riece-signal)
121
122 ;;; riece-signal.el ends here