emacs-ipython-notebook/lisp/ein-events.el

64 lines
2 KiB
EmacsLisp
Raw Permalink Normal View History

2012-05-07 14:41:15 +02:00
;;; ein-events.el --- Event module
;; Copyright (C) 2012- Takafumi Arakaki
2012-07-01 20:18:05 +02:00
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
2012-05-07 14:41:15 +02:00
;; This file is NOT part of GNU Emacs.
;; ein-events.el is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; ein-events.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with ein-events.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'eieio)
2012-05-21 04:29:43 +02:00
(require 'ein-core)
(require 'ein-classes)
2012-05-07 14:41:15 +02:00
(require 'ein-log)
2012-05-21 04:29:43 +02:00
(defun ein:events-new ()
2012-05-25 02:02:31 +02:00
"Return a new event handler instance."
(make-instance 'ein:events))
2012-05-07 14:41:15 +02:00
2012-05-22 20:00:27 +02:00
(defun ein:events-trigger (events event-type &optional data)
"Trigger EVENT-TYPE and let event handler EVENTS handle that event."
(ein:log 'debug "Event: %S" event-type)
2019-11-24 00:17:52 -05:00
(aif (gethash event-type (slot-value events 'callbacks))
(mapc (lambda (cb-arg) (ein:funcall-packed cb-arg data)) it)
(ein:log 'info "Unknown event: %S" event-type)))
2012-05-07 14:41:15 +02:00
2012-05-22 20:00:27 +02:00
(cl-defmethod ein:events-on ((events ein:events) event-type
callback &optional arg)
2012-05-22 20:00:27 +02:00
"Set event trigger hook.
When EVENT-TYPE is triggered on the event handler EVENTS,
CALLBACK is called. CALLBACK must take two arguments:
ARG as the first argument and DATA, which is passed via
`ein:events-trigger', as the second."
(assert (symbolp event-type) t "%s not symbol" event-type)
(let* ((table (slot-value events 'callbacks))
2012-05-22 20:00:27 +02:00
(cbs (gethash event-type table)))
(push (cons callback arg) cbs)
(puthash event-type cbs table)))
2012-05-07 14:41:15 +02:00
(provide 'ein-events)
;;; ein-events.el ends here