sagemath / sage-shell-mode

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

Strange behaviour when using `RET` on a line earlier in the Sage buffer #17

Closed johanrosenkilde closed 7 years ago

johanrosenkilde commented 7 years ago

In sage-mode moving the point to some earlier prompt line and pressing RET will copy that earlier prompt to the current one and evaluate it. This is quite convenient sometimes.

Doing the same in sage-shell-mode (using Sage 7.4 and sage-shell:use-python-toolkit t) results in weird behaviour: the previous prompt is reevaluated, but the intermediate buffer contents are deleted and the prompt input is visually copied twice.

<SAGE BANNER>
sage: 2+2
4
sage: 3+3
6

now moving the point to the line 2+2 and pressing RET results in the following buffer contents:

<SAGE BANNER>
sage: 2+2 2+2
4
sage: 
stakemori commented 7 years ago

I didn't know the feature of sage-mode and didn't expect that users do such a thing. I will implement the feature. Thanks.

johanrosenkilde commented 7 years ago

Fantastic, thanks! sage-shell-mode is really nice :-)

stakemori commented 7 years ago

I fixed this in ae72052dc00688215cf5466665ca34dedd0b2e87. If there is no problem, I will merge it to master later.

If sage-shell:use-prompt-toolkit is non-nil, this feature may not work well. It is because prompt_toolkit splits long input line to fit the window size.

For example if I input a number (output of factorial(100)), then input line is split as follows:

sage: 9332621544394415268169923885626670049071596826438162146859296389521759999322991560894146397615651828625369792082
....: 7223758251185210916864000000000000000000000000

sage-shell-mode joins the lines, but it does not work well always. For example, the input line is as follows,

a = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000; print "....: foo"

Then sage-shell-mode is confused and thinks the input line ended with foo. I guess it rarely happens.

If you are using helm, you can also use M-x helm-comint-input-ring to reuse the previous input (helm-sage also provides a similar command).

johanrosenkilde commented 7 years ago

Thank you, this is great! I'll try it out tomorrow and report back.

johanrosenkilde commented 7 years ago

I confirm that this works as described. Thanks a lot.

An issue related to what you mentioned comes up in trying to replay a multi-line command:

sage: l = [  n^2 for n in range(5) ]
sage: for e in l:
....:     print e
....:
0
1
4
9
16
sage: l = [ n^3 for n in range(5) ]
sage: <move point and try to replay "for e in l: ...">
sage: for e in l:
....:      <point here>

I can live with that behaviour no problem.

A related user-question: is it possible to search through previous history, like in bash?

Best, Johan

stakemori commented 7 years ago

A related user-question: is it possible to search through previous history, like in bash?

I guess M-r (comint-history-isearch-backward-regexp) works. But this may be unstable because it depends on global state. I may replace it with a command which uses ido.

mantepse commented 7 years ago

Is it necessary that long input lines are split to fit the window size? More precisely: is this required by ipython or is this a sage-shell-mode feature?

If it is a sage-shell-mode feature, I'd like to be able to turn it off, because I tend to resize my window frequently...

stakemori commented 7 years ago

@mantepse, sage-shell-mode doesn't split long input lines. I guess this is a feature of prompt_toolkit or ipytohn. sage-shell-mode has to tell the window size (window height and window width) to the process, otherwise no prompts are displayed. I don't know how to disable the feature. I guess there are at least two solutions.

  1. Tell a very large window width to the process.
  2. Tell the actual window size to the process when the window configuration is changed.

If you like the first one, I will add a variable for it. If you would like to try the second one, please try the following code (this may be unstable):

(add-hook 'window-configuration-change-hook #'sage-shell:-adjust-window-size)
stakemori commented 7 years ago

@mantepse, since this is automatically closed, I've opened a new issue(#21).