millejoh / emacs-ipython-notebook

Jupyter notebook client in Emacs
http://millejoh.github.io/emacs-ipython-notebook/
GNU General Public License v3.0
1.47k stars 122 forks source link

ein:notebook-mode gets disabled inside a code cell when using elpy #658

Closed OldIMP closed 4 years ago

OldIMP commented 4 years ago

Whenever the cursor is inside a code cell, the Notebook minor mode will disappear and only appear again if the cursor is outside the cell or in a markdown cell.

I know this might be a feature but it makes the notebook unusable since all the keybindings belonging to the Notebook mode are replaced by the elpy ones:

EIN version is 20200205.1547

Elpy Configuration

Emacs.............: 26.3 Elpy..............: 1.32.0 Virtualenv........: None Interactive Python: jupyter 4.6.1 (c:/Users/shouh/Anaconda3/envs/tf1/Scripts/jupyter.exe) RPC virtualenv....: rpc-venv (c:/Users/shouh/AppData/Roaming/.emacs.d/elpy/rpc-venv) Python...........: c:/Users/shouh/Anaconda3/envs/tf1/pythonw.exe 3.7.6 (c:/Users/shouh/Anaconda3/envs/tf1/pythonw.exe) Jedi.............: 0.16.0 Rope.............: 0.16.0 Autopep8.........: 1.5 Yapf.............: 0.29.0 Black............: 19.10b0 Syntax checker....: flake8.exe (c:/Users/shouh/Anaconda3/envs/tf1/Scripts/flake8.exe)

I'm not sure if it's an issue here or in elpy.

Thanks for any fix / workaround!

dickmao commented 4 years ago

https://github.com/millejoh/emacs-ipython-notebook/blob/cb2270ae3f445ebe015f6075059312449a1025ff/lisp/poly-ein.el#L16

Could you put a (message "got here") on this line?

Working assumption is that it doesn't get there. In the notebook, C-h v after-change-major-mode-hook verify poly-ein--neuter-markdown-mode is there.

OldIMP commented 4 years ago

I've added the message and another one at the beginning of poly-ein--neuter-markdown-mode to debug minor-mode-map-alist. Following are the messages that I got (some noises were stripped):

[yas] Prepared just-in-time loading of snippets successfully.
Turning on magit-auto-revert-mode...done (0.324s, 9 buffers checked)
(caar minor-mode-map-alist): magit-blame-read-only-mode
got here
(caar minor-mode-map-alist): ein:notebook-mode [5 times]
Restoring clock data
(caar minor-mode-map-alist): gnus-dead-summary-mode
got here
Desktop: 1 frame, 5 buffers restored.
(caar minor-mode-map-alist): ein:notebook-mode
(caar minor-mode-map-alist): ein:notebook-mode [2 times]
Formats have changed, recompiling...done
(caar minor-mode-map-alist): ein:notebook-mode
Updating buffer list...done
(caar minor-mode-map-alist): ein:notebook-mode [3 times]
ein: [info] ein:jupyter-server--run: c:/Users/shouh/Anaconda3/envs/tf1/Scripts/jupyter.exe notebook --notebook-dir=c:/dev/handson-ml --no-browser
ein: [info] Login to http://127.0.0.1:8888 complete.
(caar minor-mode-map-alist): ein:notebook-mode
ein: [info] Worksheet exercises_3.ipynb is ready
(caar minor-mode-map-alist): ein:notebook-mode [11 times]
dickmao commented 4 years ago

That looks right. To be sure, can you run the following:

emacs -Q --batch "Worksheet exercises_3.ipynb" -f package-initialize \
      --eval "(require 'ein)" \
      --eval "(require 'ein-process)" \
      --eval "(setq done-p nil)" \
      --eval "(setq hooked-p nil)" \
      --eval "(cl-loop until done-p \
                  do (ein:process-open-notebook* (expand-file-name buffer-file-name) \
                        (lambda (nb &rest _args) \
                           (with-current-buffer (ein:notebook-buffer nb) \
                               (goto-char (point-min)) \
                               (cl-assert (string= major-mode \"fundamental-mode\")) \
                               (princ (format \"%s %s \n\" major-mode (buffer-name))) \
                               (cl-assert (memq (function poly-ein--neuter-markdown-mode) after-change-major-mode-hook)) \
                               (add-hook (quote after-change-major-mode-hook) (lambda () (setf hooked-p t)) t) \
                               (call-interactively (function ein:worksheet-goto-next-input)) \
                               (pm-switch-to-buffer) \
                               (princ (format \"%s %s\n\" major-mode (buffer-name))) \
                               ) \
                           (setq done-p t))) \
                  do (sleep-for 0 3000) \
                  finally (princ (format \"all %s\n\" (if hooked-p \"good\" \"bad\"))))"

When I run it, I get something like:

ein: [info] Login to http://127.0.0.1:8888 complete.
ein: [info] Worksheet Untitled.ipynb is ready
Can’t guess python-indent-offset, using defaults: 4
fundamental-mode *ein: http://127.0.0.1:8888/Untitled.ipynb* 
ein:markdown-mode  *ein: http://127.0.0.1:8888/Untitled.ipynb*[ein:markdown]

ein: [info] Notebook Untitled.ipynb is ready
all good
OldIMP commented 4 years ago
ein: [info] Login to http://127.0.0.1:8888 complete.
(caar minor-mode-map-alist): magit-popup-help-mode
got here
ein: [info] Worksheet exercises_3.ipynb is ready
(caar minor-mode-map-alist): ein:notebook-mode
(caar minor-mode-map-alist): outline-minor-mode
got here
ein: [info] Notebook exercises_3.ipynb is already open
fundamental-mode *ein: http://127.0.0.1:8888/exercises_3.ipynb*
python-mode  *ein: http://127.0.0.1:8888/exercises_3.ipynb*[python]
fundamental-mode *ein: http://127.0.0.1:8888/exercises_3.ipynb*
python-mode  *ein: http://127.0.0.1:8888/exercises_3.ipynb*[python]

ein: [info] Notebook exercises_3.ipynb is ready
(caar minor-mode-map-alist): ein:notebook-mode
(caar minor-mode-map-alist): ein:notebook-mode
all good

ein: [info] Signaled ein server with pid 17972
dickmao commented 4 years ago

Try this then:

emacs "exercises_3.ipynb" \
--eval "(defun oldimp-print () \
            (princ (format \"%s %s\n\" major-mode (buffer-name)) (function external-debugging-output)) \
            (mapc (lambda (x) (when x (princ (format \"%s: %s\n\" (symbol-name x) (symbol-value x)) (function external-debugging-output)))) \
              (mapcar (function intern-soft) (split-string \"overriding-terminal-local-map overriding-local-map emulation-mode-map-alists minor-mode-overriding-map-alist\"))) \
            (princ (format \"char-prop: %s\" (get-char-property (point) (quote keymap))) (function external-debugging-output)) \
            (princ (format \"local-map: %s\" (get-text-property (point) (quote local-map))) (function external-debugging-output)) \
            (princ (format \"current-local-map: %s\" (current-local-map)) (function external-debugging-output)) \
            (princ (format \"current-global-map: %s\" (current-global-map)) (function external-debugging-output)))" > oldimp-print 2>&1

Then C-c C-o exercises_3.ipynb. Move the cursor to the python cell. Invoke M-: (oldimp-print). Exit emacs. Look for "elpy" in the file oldimp-print.

OldIMP commented 4 years ago

Sry but "elpy" is not in it: oldimp-print.txt

OldIMP commented 4 years ago

Problem description

ein:notebook-mode gets disabled inside a code cell when using elpy

Steps to reproduce the problem

  1. emacs -Q -f package-initialize
  2. install elpy & (elpy-enable)
  3. open a Jupyter notebook and go to a code cell
  4. Notebook minor mode disappears

System info:

("EIN system info"
 :emacs-version "GNU Emacs 26.3 (build 1, x86_64-w64-mingw32)
 of 2019-08-29"
 :emacs-bzr-version "96dd0196c28bc36779584e47fffcca433c9309cd"
 :window-system w32
 :emacs-variant nil
 :os
 (:uname nil
     :lsb-release nil)
 :notebook
 "Name: notebook
Version: 6.0.3
Summary: A web-based notebook environment for interactive computing
Home-page: http://jupyter.org
Author: Jupyter Development Team
Author-email: jupyter@googlegroups.com
License: BSD
Location: c:\\users\\shouh\\anaconda3\\envs\\tf1\\lib\\site-packages
Requires: ipython-genutils, pyzmq, ipykernel, tornado, nbformat, traitlets, Send2Trash, terminado, prometheus-client, jupyter-core, jupyter-client, jinja2, nbconvert
Required-by: widgetsnbextension, nbdime, jupyterlab, jupyterlab-server
"
 :ipython "7.10.1
"
 :image-types
 (svg png gif tiff jpeg xpm xbm pbm)
 :image-types-available
 (svg png gif tiff jpeg xpm xbm pbm)
 :request
 (:backend curl)
 :ein
 (:version "20200209.35"
       :source-dir "c:/Users/shouh/AppData/Roaming/.emacs.d/elpa/ein-20200209.35/")
 :lib
 ((:name "websocket"
     :path "~/.emacs.d/elpa/websocket-20200102.637/websocket.elc"
     :featurep t
     :version-var websocket-version
     :version "1.12")
  (:name "anaphora"
     :path "~/.emacs.d/elpa/anaphora-20180618.2200/anaphora.elc"
     :featurep t
     :version-var nil
     :version nil)
  (:name "request"
     :path "~/.emacs.d/elpa/request-20200127.21/request.elc"
     :featurep t
     :version-var request-version
     :version "0.3.0")
  (:name "deferred"
     :path "~/.emacs.d/elpa/deferred-20170901.1330/deferred.elc"
     :featurep t
     :version-var deferred:version
     :version "0.5.0")
  (:name "polymode"
     :path "~/.emacs.d/elpa/polymode-20191208.1239/polymode.elc"
     :featurep t
     :version-var nil
     :version nil)
  (:name "kubernetes"
     :path "~/.emacs.d/elpa/kubernetes-20200114.436/kubernetes.elc"
     :featurep t
     :version-var nil
     :version nil)
  (:name "dash"
     :path "~/.emacs.d/elpa/dash-20200119.2310/dash.elc"
     :featurep t
     :version-var nil
     :version nil)
  (:name "s"
     :path "~/.emacs.d/elpa/s-20180406.808/s.elc"
     :featurep t
     :version-var nil
     :version nil)))
dickmao commented 4 years ago

Thank you for reporting this bug and staying with it.

I was able to finally reproduce the error with emacs -Q -f package-initialize --eval "(setq debug-on-error t)" --eval "(elpy-enable)". Sorry I should have tried this much earlier -- I had assumed it was a keymap issue, and not a general bug.

I hope the above PR fixes. Thanks again.

OldIMP commented 4 years ago

Verified that bug was fixed in 20200210.452.

Thx for the professional work! This fixed lots of other problems w/ elpy, too, e.g. now the notebook is opened in the front ground.