emacs-ipython-notebook/ein-events.el
2012-05-25 02:04:07 +02:00

80 lines
2.6 KiB
EmacsLisp
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; ein-events.el --- Event module
;; Copyright (C) 2012- Takafumi Arakaki
;; Author: Takafumi Arakaki
;; 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:
(eval-when-compile (require 'cl))
(require 'eieio)
(require 'ein-log)
(require 'ein-utils)
;;; Events handling class
(defclass ein:events ()
((buffer :initarg :buffer :type buffer :document "Notebook buffer")
(callbacks :initarg :callbacks :type hash-table
:initform (make-hash-table :test 'equal)))
"Event handler class.")
(defun ein:events-new (buffer)
"Return a new event handler instance."
(ein:events "Events" :buffer buffer))
(defun ein:events-trigger (events event-type &optional data)
"Trigger EVENT-TYPE and let event handler EVENTS handle that event.
EVENT-TYPE is a cons like \(notebook_saved . Notebook), which is
a direct translation of \"notebook_saved.Notebook\" from the
IPython notebook client JS."
(ein:log 'debug "Event: %S" event-type)
;; Ensure that event is handled in the related buffer.
;; This helps logging by `ein:log' (and maybe EWOC?).
(with-current-buffer (oref events :buffer)
(ein:aif (gethash event-type (oref events :callbacks))
(mapc (lambda (cb-arg) (ein:funcall-packed cb-arg data)) it)
(ein:log 'info "Unknown event: %S" event-type))))
(defmethod ein:events-on ((events ein:events) event-type
callback &optional arg)
"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 (and (consp event-type)
(symbolp (car event-type))
(symbolp (cdr event-type))))
(let* ((table (oref events :callbacks))
(cbs (gethash event-type table)))
(push (cons callback arg) cbs)
(puthash event-type cbs table)))
(provide 'ein-events)
;;; ein-events.el ends here