From 8738fa5e0aa07ae6faefa23b249e36a67ec2973c Mon Sep 17 00:00:00 2001 From: dickmao Date: Thu, 16 May 2019 09:47:24 -0400 Subject: [PATCH] If you see something, say something Avoid silently failing as in #530 where kernelspec language is unrecognized. --- lisp/ein-multilang.el | 5 ++- lisp/ein-notebook.el | 73 ++++++++++++++++++++++++------------------- lisp/poly-ein.el | 3 ++ 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/lisp/ein-multilang.el b/lisp/ein-multilang.el index 982ae90..66f6a71 100644 --- a/lisp/ein-multilang.el +++ b/lisp/ein-multilang.el @@ -162,7 +162,10 @@ This function may raise an error." (set-keymap-parent ein:notebook-multilang-mode-map ess-r-mode-map)))) (defun ein:ml-lang-setup (kernelspec) - (funcall (intern (concat "ein:ml-lang-setup-" (ein:$kernelspec-language kernelspec))))) + (let ((setup-func (intern (concat "ein:ml-lang-setup-" (ein:$kernelspec-language kernelspec))))) + (if (fboundp setup-func) + (funcall setup-func) + (error "ein:ml-lang-setup: kernelspec language '%s' unsupported" (ein:$kernelspec-language kernelspec))))) ;; (defun ein:ml-lang-setup-markdown () ;; "Use `markdown-mode-map'. NOTE: This function is not used now." diff --git a/lisp/ein-notebook.el b/lisp/ein-notebook.el index 0fe74dd..2117a6b 100644 --- a/lisp/ein-notebook.el +++ b/lisp/ein-notebook.el @@ -403,22 +403,25 @@ where `created' indicates a new notebook or an existing one. (ein:notebook-maybe-set-kernelspec notebook (plist-get (ein:$content-raw-content content) :metadata)) (ein:notebook-install-kernel notebook) (ein:notebook-from-json notebook (ein:$content-raw-content content)) - ;; Start websocket only after worksheet is rendered - ;; because ein:notification-bind-events only gets called after worksheet's - ;; buffer local notification widget is instantiated - (ein:kernel-retrieve-session (ein:$notebook-kernel notebook)) - (setf (ein:$notebook-kernelinfo notebook) - (ein:kernelinfo-new (ein:$notebook-kernel notebook) - (cons #'ein:notebook-buffer-list notebook) - (symbol-name (ein:get-mode-for-kernel (ein:$notebook-kernelspec notebook))))) - (ein:notebook-put-opened-notebook notebook) - (ein:notebook--check-nbformat (ein:$content-raw-content content)) - (setf (ein:$notebook-q-checkpoints notebook) q-checkpoints) - (ein:notebook-enable-autosaves notebook) - (ein:gc-complete-operation) - (ein:log 'info "Notebook %s is ready" (ein:$notebook-notebook-name notebook))) - (when callback0 - (funcall callback0))) + (if (not (with-current-buffer (ein:notebook-buffer notebook) + (ein:get-notebook))) + (error "ein:notebook-open--callback: notebook instantiation failed") + ;; Start websocket only after worksheet is rendered + ;; because ein:notification-bind-events only gets called after worksheet's + ;; buffer local notification widget is instantiated + (ein:kernel-retrieve-session (ein:$notebook-kernel notebook)) + (setf (ein:$notebook-kernelinfo notebook) + (ein:kernelinfo-new (ein:$notebook-kernel notebook) + (cons #'ein:notebook-buffer-list notebook) + (symbol-name (ein:get-mode-for-kernel (ein:$notebook-kernelspec notebook))))) + (ein:notebook-put-opened-notebook notebook) + (ein:notebook--check-nbformat (ein:$content-raw-content content)) + (setf (ein:$notebook-q-checkpoints notebook) q-checkpoints) + (ein:notebook-enable-autosaves notebook) + (ein:gc-complete-operation) + (ein:log 'info "Notebook %s is ready" (ein:$notebook-notebook-name notebook)) + (when callback0 + (funcall callback0))))) (defun ein:notebook-maybe-set-kernelspec (notebook content-metadata) (ein:aif (plist-get content-metadata :kernelspec) @@ -666,22 +669,28 @@ This is equivalent to do ``C-c`` in the console program." (defun ein:notebook--worksheet-render (notebook ws) (ein:worksheet-render ws) (with-current-buffer (ein:worksheet-buffer ws) - (if ein:polymode - (poly-ein-mode) - ;; Changing major mode here is super dangerous as it - ;; kill-all-local-variables. - ;; Our saviour has been `ein:deflocal' which applies 'permanent-local - ;; to variables assigned up to this point, but we ought not rely on it - (funcall (ein:notebook-choose-mode)) - (ein:worksheet-reinstall-undo-hooks ws) - (ein:aif (ein:$notebook-kernelspec notebook) - (ein:ml-lang-setup it))) - (ein:notebook-mode) - (ein:notebook--notification-setup notebook) - (ein:notebook-setup-kill-buffer-hook) - (setq ein:%notebook% notebook) - (when ein:polymode - (poly-ein-fontify-buffer notebook)))) + (let (multilang-failed) + (if ein:polymode + (poly-ein-mode) + ;; Changing major mode here is super dangerous as it + ;; kill-all-local-variables. + ;; Our saviour has been `ein:deflocal' which applies 'permanent-local + ;; to variables assigned up to this point, but we ought not rely on it + (funcall (ein:notebook-choose-mode)) + (ein:worksheet-reinstall-undo-hooks ws) + (condition-case err + (ein:aif (ein:$notebook-kernelspec notebook) + (ein:ml-lang-setup it) + (error "ein:notebook--worksheet-render: No kernelspec found")) + (error (ein:log 'error (error-message-string err)) + (setq multilang-failed t)))) + (unless multilang-failed + (ein:notebook-mode) + (ein:notebook--notification-setup notebook) + (ein:notebook-setup-kill-buffer-hook) + (setq ein:%notebook% notebook) + (when ein:polymode + (poly-ein-fontify-buffer notebook)))))) (defun ein:notebook--notification-setup (notebook) (ein:notification-setup diff --git a/lisp/poly-ein.el b/lisp/poly-ein.el index ff71552..3ad5f4e 100644 --- a/lisp/poly-ein.el +++ b/lisp/poly-ein.el @@ -50,6 +50,9 @@ TYPE can be 'body, nil." ((ein:markdowncell-p cell) "markdown") (t "fundamental")))) ((not (equal mode (ein:oref-safe cm :mode))))) + (when (eq mode 'poly-fallback-mode) + (ein:display-warning + (format "pm:get-span: no major mode for kernelspec language '%s'" lang))) (setq result-cm (loop for ocm in (eieio-oref pm/polymode '-auto-innermodes) when (equal mode (ein:oref-safe ocm :mode))