emacs-ipython-notebook/ein-events.el

137 lines
4.7 KiB
EmacsLisp
Raw Normal View History

2012-05-07 14:41:15 +02:00
;;; 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:
2012-05-21 04:29:43 +02:00
(eval-when-compile (require 'cl))
(require 'eieio)
2012-05-21 04:29:43 +02:00
2012-05-07 14:41:15 +02:00
(require 'ein-log)
2012-05-21 04:29:43 +02:00
(require 'ein-utils)
2012-05-24 19:38:31 +02:00
;; FIXME: header-line stuff must be moved to the notification widget module.
2012-05-21 04:29:43 +02:00
(defvar ein:header-line-format '(:eval (ein:header-line)))
(defun ein:header-line ()
(format
"IP[y]: %s"
(ein:join-str
" | "
(ein:filter
'identity
(list (ein:events-header-message-notebook)
(ein:events-header-message-kernel))))))
(defun ein:header-line-setup-maybe ()
"Setup `header-line-format' for mumamo.
As `header-line-format' is buffer local variable, it must be set
for each chunk when in
See also `ein:ac-setup-maybe'."
(and (ein:eval-if-bound 'ein:notebook)
(ein:eval-if-bound 'mumamo-multi-major-mode)
(setq header-line-format ein:header-line-format)))
(add-hook 'after-change-major-mode-hook 'ein:header-line-setup-maybe)
;;; Events handling class
2012-05-24 19:38:31 +02:00
;; FIXME: After moving header-line stuff to the notification widget,
;; this buffer local variable will not be needed.
(ein:deflocal ein:@events nil
"Buffer local variable to hold an instance of `ein:events'.")
(defclass ein:events ()
((buffer :initarg :buffer :type buffer :document "Notebook buffer")
2012-05-22 20:00:27 +02:00
(callbacks :initarg :callbacks :type hash-table
:initform (make-hash-table :test 'equal))
(status-notebook :initarg :status-notebook :initform nil :type symbol)
(status-kernel :initarg :status-kernel :initform nil :type symbol))
"Event handler class.")
2012-05-24 19:38:31 +02:00
;; FIXME: This function must be moved to the notification widget.
(defun ein:events-setup (buffer)
"Make a new event handler instance and setup local variable in the BUFFER.
The newly created instance is returned by this function. Event
handler user must *not* use the buffer local variable directly.
Use the variable returned by this function instead."
(with-current-buffer buffer
(setq ein:@events (ein:events "Events" :buffer buffer))
(setq header-line-format ein:header-line-format)
ein:@events))
2012-05-21 04:29:43 +02:00
(defun ein:events-header-message-notebook ()
(case (oref ein:@events :status-notebook)
(notebook_saving "Saving Notebook...")
(notebook_saved "Notebook is saved")
(notebook_save_failed "Failed to save Notebook!")))
2012-05-21 04:29:43 +02:00
(defun ein:events-header-message-kernel ()
(case (oref ein:@events :status-kernel)
(status_idle nil)
(status_busy "Kernel is busy...")
(status_dead "Kernel is dead. Need restart.")))
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.
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)
2012-05-24 19:13:47 +02:00
;; 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))
;; FIXME! the following must be put together in the event frame work.
(case (cdr event-type)
(Kernel
(oset events :status-kernel (car event-type)))
(Notebook
(oset events :status-notebook (car event-type)))
(t
(ein:log 'info "Unknown event: %S" event-type)))))
2012-05-07 14:41:15 +02:00
2012-05-22 20:00:27 +02:00
(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)))
2012-05-07 14:41:15 +02:00
(provide 'ein-events)
;;; ein-events.el ends here