emacs-ipython-notebook/ein-events.el
Takafumi Arakaki b66832ca58 Encapsulate event handling in a class
Various `ein:events-*' function was seeing buffer local variables
which causes the problem when triggering events from non-notebook
buffer.  To fix that problem, and prepare for the future change
which may increase event channels, the status variable used for
header line is encapsulated in a class.
2012-05-22 14:20:08 +02:00

106 lines
3.4 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)
(eval-when-compile (defvar ein:notebook))
(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:notebook
(and (boundp 'mumamo-multi-major-mode)
(eval '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
(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")
(status-notebook :initarg :status-notebook :initform nil :type symbol)
(status-kernel :initarg :status-kernel :initform nil :type symbol))
"Event handler class.")
(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))
(defun ein:events-header-message-notebook ()
(case (oref ein:@events :status-notebook)
(notebook_saving.Notebook "Saving Notebook...")
(notebook_saved.Notebook "Notebook is saved")
(notebook_save_failed.Notebook "Failed to save Notebook!")))
(defun ein:events-header-message-kernel ()
(case (oref ein:@events :status-kernel)
(status_idle.Kernel nil)
(status_busy.Kernel "Kernel is busy...")
(status_dead.Kernel "Kernel is dead. Need restart.")))
(defun ein:events-trigger (events event-type)
(ein:log 'debug "Event: %s" event-type)
(case event-type
((status_busy.Kernel status_idle.Kernel status_dead.Kernel)
(oset events :status-kernel event-type))
((notebook_saving.Notebook
notebook_saved.Notebook
notebook_save_failed.Notebook)
(oset events :status-notebook event-type))
(t
(ein:log 'info "Unknown event: %s" event-type))))
(provide 'ein-events)
;;; ein-events.el ends here