Macaulay2 / M2-emacs

Macaulay2 emacs files
GNU General Public License v3.0
5 stars 3 forks source link

Make `C-0 C-k` work when `comint-prompt-read-only` is `t` #24

Closed d-torrance closed 2 years ago

d-torrance commented 2 years ago

If you fire up Macaulay2 in emacs and then hit backspace a few times (or C-0 C-k), you can end up without a prompt:

+ M2 --no-readline --print-width 79
Macaulay2, version 1.18.0.1
with packages: ConwayPolynomials, Elimination, IntegralClosure, InverseSystems,
               LLLBases, MinimalPrimes, PrimaryDecomposition, ReesAlgebra,
               Saturation, TangentCone

"I just pressed backspace 5 times before typing this string"

o1 = I just pressed backspace 5 times before typing this string

i2 :

It would be nice if M2-comint-mode behaved like running M2 in a terminal, or other interpreter major modes (eshell-mode, inferior-python-mode, slime-repl-mode, etc.), and prevented the i1 : prompt from being deleted.

DanGrayson commented 2 years ago

I turned that off intentionally a while back in commit 310601c59196cd70cc105191ce8771676c25f5b4. The idea is that whether prompts are read-only is a global user preference, and they can easily set a value for comint-prompt-read-only in their .emacs file. (I like to be able to edit the text in buffers, so I leave it off.)

d-torrance commented 2 years ago

I turned that off intentionally a while back in commit 310601c. The idea is that whether prompts are read-only is a global user preference, and they can easily set a value for comint-prompt-read-only in their .emacs file. (I like to be able to edit the text in buffers, so I leave it off.)

Ah, I see! Setting comint-prompt-read-only to t definitely does the trick for backspace, but C-0 C-k doesn't work as expected. I'll rename this issue accordingly.

mahrud commented 2 years ago

I still think the prompt should be read-only by default in M2-comint-mode. For every expect like Dan who enjoys the freedom to edit the prompt (why?!), there are dozens of M2 users who get confused and lost in opaque errors when the accidentally press backspace one time too many.

I think the mistake I made when I initially made it read-only was that the regex for the prompt included an empty line: https://github.com/Macaulay2/M2-emacs/blob/01af405c5c92f90e4009ff580a1b1dcefe43a714/M2-symbols.el.in#L22-L23 Which then lead to https://github.com/Macaulay2/M2-emacs/issues/14. But a correct regex that doesn't block ctrl-C on an empty line should be good. You can also try looking at how eshell or other comint based modes do this.

DanGrayson commented 2 years ago

Emacs does provide a way to delete prompts:

If you set this option to t, then the safe way to temporarily
override the read-only-ness of comint prompts is to call
‘comint-kill-whole-line’ or ‘comint-kill-region’ with no
narrowing in effect.  This way you will be certain that none of
the remaining prompts will be accidentally messed up.  You may
wish to put something like the following in your init file:

(add-hook 'comint-mode-hook
          (lambda ()
            (define-key comint-mode-map [remap kill-region] 'comint-kill-region)
            (define-key comint-mode-map [remap kill-whole-line]
              'comint-kill-whole-line)))

If you sometimes use comint-mode on text-only terminals or with ‘emacs -nw’,
you might wish to use another binding for ‘comint-kill-whole-line’.
DanGrayson commented 2 years ago

... in other words, there is no reason to expect C-0 C-k to work.

DanGrayson commented 2 years ago

If we made the prompts read-only, I could switch to comint-kill-region and that would probably be okay for me. (I wasn't aware of this before.). I'll experiment ...

DanGrayson commented 2 years ago

We should figure out how eshell does it without setting a value for comint-prompt-read-only. We prefer not to set a value for that, because it's used by more than just M2-comint-mode.

d-torrance commented 2 years ago

I figured out how to make C-0 C-k (and C-a) work as expected -- set comint-use-prompt-regexp to nil. That's actually the default value in comint.el, but we set it to t. Is there a reason?

https://github.com/Macaulay2/M2-emacs/blob/01af405c5c92f90e4009ff580a1b1dcefe43a714/M2.el#L55

d-torrance commented 2 years ago

Is there a reason?

Ah, there is! From 6449756:

The problem was that pressing ENTER in a previous output field would send everything from the beginning of that line to the end of the output field. Now it sends just one line.

For example:

Macaulay2, version 1.18.0.1
with packages: ConwayPolynomials, Elimination, IntegralClosure, InverseSystems,
               LLLBases, MinimalPrimes, PrimaryDecomposition, ReesAlgebra,
               Saturation, TangentCone

i1 : 2 + 2

o1 = 4

And then if I scroll up and press Enter:

i2 : o1 = 4

o2 = 4
d-torrance commented 2 years ago

Not sure why this didn't close automatically when #25 was merged... Closing