sagemath / sage-shell-mode

Emacs front end for SageMath
GNU General Public License v3.0
102 stars 16 forks source link

Usage with `use-prompt-toolkit t` and `<C-j>` exits the Sage process #11

Open johanrosenkilde opened 8 years ago

johanrosenkilde commented 8 years ago

I use Evil in Emacs which means I'm usually writing in "insert" state and not in "emacs" state.

In the Sage shell, pressing Enter when in Emacs state works as normal. If in insert state, it inserts a newline character (visually at least) and goes to the next line. Switching to Emacs state and then pressing Enter will now evaluate the command and then exit the Sage process:

┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 7.3, Release Date: 2016-08-04                     │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: 2+2 #typed in Insert state, followed by enter, followed by enter in Emacs state
4
sage: 
Exiting Sage (CPU time 0m0.07s, Wall time 3m0.40s).

Process Sage finished
johanrosenkilde commented 8 years ago

Actually, this error occurs without Evil at all: Type 2+2<C-j><Enter> and the sage shell will display 4 and then promptly exit.

stakemori commented 8 years ago

Actually, this error occurs without Evil at all: Type 2+2 and the sage shell will display 4 and then promptly exit.

Thanks for reporting. But I cannot reproduce this. Can you reproduce this with emacs -Q and only sage-shell-mode (and evil-mode) is loaded? Could you provide Emacs version and name of commands bound to those keys?

johanrosenkilde commented 8 years ago

Hmm, indeed. Sorry for the noise, I should have tested more myself. On a fresh emacs -Q requiring only sage-shell-mode and evil, all is fine. I'm trying to analyse what the difference between the two setups are, but it's not so clear.

johanrosenkilde commented 8 years ago

I'm running Emacs 24.5.1. I'm disabling evil mode in the Sage buffer before relaunching. Then I'm doing the following:

M-x run-sage
2+2<C-j><enter>

C-j is bound to (electric-newline-and-maybe-indent). <enter> is bound to your (sage-shell:send-input). I'm looking further into whether electric-newline-and-maybe-indent is the culprit.

johanrosenkilde commented 8 years ago

OK, got it: the problem is when use-prompt-toolkit is set to true. Try evaluating the following in an emacs -Q window:

(add-to-list 'load-path _"<your-path-to-deferred>")_
(add-to-list 'load-path  "<your-path-to-sage-shell-mode")
(require 'sage-shell-mode)
(setq sage-shell:use-prompt-toolkit t)

then run the following

M-x sage-shell:run-sage
sage: <C-j><enter>
<sage process quits>

I thought use-prompt-toolkit was required for sage-shell-mode to work with ipython 5, which is why I had it turned on. This seems to be a misunderstanding. When should it be used?

stakemori commented 8 years ago

I thought use-prompt-toolkit was required for sage-shell-mode to work with ipython 5, which is why I had it turned on. This seems to be a misunderstanding. When should it be used?

If you are using Sage 7.3 built from the master branch, then you should not turn it on. If you are an arch Linux user and install Sage by pacman, then probably you should turn it on.

If sage-shell:use-prompt-toolkit is nil but it should be non-nil, then the following message will be displayed.

To use sage-shell-mode properly, please set `sage-shell:use-prompt-toolkit' to t. And restart the SageMath process.

I use Sage 7.4 beta3 with sage-shell:use-prompt-toolkit turned on, but I still cannot reproduce this.

stakemori commented 8 years ago

I can reproduce this with Sage 7.2 and sage-shell:use-prompt-toolkit turned on. Probably, you should set it to nil.

stakemori commented 8 years ago

If the output of sage -c 'import IPython; print IPython.version_info[0]' is less than five, then you should set it to nil.

johanrosenkilde commented 8 years ago

OK, indeed: I confused my two installations of Sage, and forgot that my "stable" installation is still at 7.3. Following the docs everything and setting sage-shell:use-prompt-toolkit as one should, then everything works. Setting sage-shell:use-prompt-toolkit to t for Sage 7.3 or earlier seems to be the source of this problem. I presume that in a reasonably short while, sage-shell::use-prompt-toolkit t will be the default, at which point it's only retained for backward compatibility with older Sages. So I don't think it's really worth it to try and detect this.

stakemori commented 8 years ago

I presume that in a reasonably short while, sage-shell::use-prompt-toolkit t will be the default, at which point it's only retained for backward compatibility with older Sages.

If sage-shell::use-prompt-toolkit is set to non-nil in older Sages, it causes actual problems (as you described). So I think it is safer to set the default value to nil.

Maybe I should find a way to warn users if sage-shell:prompt-toolkit is set to non-nil but it should be nil.

johanrosenkilde commented 8 years ago

IMHO sage-shell:use-prompt-toolkit should start defaulting to t once Sage 7.4 has been stable for, say, 6 months. Then the Install documentation should just be modified to say that "If you're running an old Sage 7.3 or older, then you should set sage-shell:use-prompt-toolkit to nil".

This could coincide with a "release" of sage-shell-mode. In fact, it could seem that sage-mode is not going to be salvaged, in which case we should switch the "official Emacs sage mode" to sage-shell-mode. The change in default of sage-shell:use-prompt-toolkit could coincide with this shipping of sage-shell-mode with Sage.

stakemori commented 8 years ago

@jsrndk, I see. I agree with you. I will change the default value.

By the way, we can warn users if they should set sage-shell:use-prompt-toolkit to nil, but it is non-nil.

(deferred:$
  (deferred:process
    (sage-shell:sage-executable)
    "-c" "import IPython; print IPython.version_info[0]")
  (deferred:nextc it
    (lambda (x) (let ((version (string-to-number (sage-shell:trim-right x))))
              (when (and (< version 5) sage-shell:use-prompt-toolkit)
                (message "Set `sage-shell:use-prompt-toolkit' to `nil'"))))))

I used deferred because the command sage -c 'import IPython; print IPython.version_info[0]' takes so much time. I will add a similar code to sage-shell-mode.

johanrosenkilde commented 8 years ago

Hmm, but if you are going to run that check every time you start a Sage process anyway, why don't you just run it first and then set the value correctly, and start Sage? Because the two will be run in parallel? Alternatively, could you first start Sage and then run the command inside the living Sage process?

stakemori commented 8 years ago

Because the two will be run in parallel?

Yes. To reduce the start up time. I will also add a variable to disable the check.

Alternatively, could you first start Sage and then run the command inside the living Sage process?

It may be possible. But if sage-shell:use-prompt-toolkit is not correct, something may be broken. So I am not sure.