Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support alt+enter key signal by default #2718

Merged
merged 6 commits into from
Apr 11, 2020

Conversation

kumaran-14
Copy link
Contributor

Fixes #2498.

@kumaran-14
Copy link
Contributor Author

@jerch If this implementation is correct, we need add the macOptionIsMeta case too.

Copy link
Member

@jerch jerch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thats almost there 👍. I think we should change a few things though:

  • rename the new flag to something like altEscMode
  • extend the flag evaluation to other keys as well
  • macOptionIsMeta handling
  • needs a test for flag on/off

@Tyriar How does macOptionIsMeta play into this? Will it always treat Option as Alt if set or just for some key combinations? What does Alt in that case on macs?

Comment on lines 107 to 112
if (altEnterMode && modifiers === 2) {
result.key = C0.ESC + C0.CR;
}
else {
result.key = C0.CR;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not tested xterm's behavior, but I think DECSET 1039 is meant to switch the leading ESC on/off for any key? Thus we would have to extend the flag to other keys as well.

@Tyriar
Copy link
Member

Tyriar commented Feb 14, 2020

alt = option on macs, I guess to answer the question we'd be best to check other terminals that support this.

extend the flag evaluation to other keys as well

This feature has always confused me a little, we could really do with a solid use case of where this is actually used so we can study the behavior of other terminals. #2364 mentions it's used in "some REPLs".

@jerch
Copy link
Member

jerch commented Feb 16, 2020

@Tyriar @kumaran-14 Yes we should try to get a hold of the uses, maybe start with a simple script to check what keys are affected by that setting in xterm. A second test maybe can eval what macOptionIsMeta is all about (affected keys and such).

@NHDaly, @pfitzseb Do you have some insights for us, how \e\ŗ is used by REPLs? Also are there other keys involved in Alt+key usage? Note that we already support that for some other keys, but the support is kinda spread over Keyboard.ts and not really backed up by real world cases.

@PerBothner
Copy link
Contributor

See note about how Alt+Enter is used in Fish shell.

@pfitzseb
Copy link
Contributor

Julia's REPL works the same way -- Alt-Enter inserts a newline regardless of whether a block is closed or not.

@jerch
Copy link
Member

jerch commented Feb 18, 2020

@PerBothner, @pfitzseb Thx for the pointers. Yes xterm has by default this 8bit mode active for Alt+key and for Alt+Enter the fullscreen switch, at least in Ubuntu/Debian. Those key setup settings are somewhat complicated in xterm due to its additional 8bit key mode.

@Tyriar, @kumaran-14
I just tested gnome-terminal, konsole and rxvt - they all support Alt+ for various keys but most dont expose any special DECSET mode for it. I wonder if we could just hardcode Alt+Enter as most do and forget about DECSET 1039/1036? xterm seems to be kinda special here, but since we are not interested in the 8bit key mode, we probably dont need to switch Alt+key mode on/off at all.

So my current suggestion is to go with a slimmer implementation:

  • hardcode the Alt+Enter behavior in Keyboard.ts
  • no new DECSET mode
  • map option key if macOptionIsMeta is set (needs to be checked in other mac terminals, still unclear whether it applies here)

Any other ideas?

@kumaran-14
Copy link
Contributor Author

@jerch I just saw the recent comment.
Can I go ahead with removing the DECSET mode and prepend \e for other keys?

@jerch
Copy link
Member

jerch commented Feb 19, 2020

@kumaran-14 Yes I think we currently dont need the additional DECSET mode. It just complicates things. Sorry for the the forth and back, the needed bits are abit unclear and differ alot across emulators.

@Tyriar
Copy link
Member

Tyriar commented Feb 21, 2020

map option key if macOptionIsMeta is set (needs to be checked in other mac terminals, still unclear whether it applies here)

We should check how iTerm in particular handles this case.

@kumaran-14
Copy link
Contributor Author

@Tyriar @jerch I tried to find out how few terminals behave.
alacritty

command executed : showkey -a

KeyPress        Ascii output

Enter            ^M      13 0015 0x0d
Ctrl + Enter     ^M      13 0015 0x0d
Shift + Enter    ^M      13 0015 0x0d
Windows + Enter   ^M     13 0015 0x0d
Alt + Enter      ^[^M    27 0033 0x1b
                         13 0015 0x0d
                         
command executed : from executable built from http://catb.org/esr/showkey/

KeyPress        output

Enter            <CTL-J=LF>
Ctrl + Enter     <CTL-J=LF>
Shift + Enter    <CTL-J=LF>
Windows + Enter  <CTL-J=LF>
Alt + Enter      <ESC><CTL-J=LF>


command executed : ctrl + v and keypress

KeyPress        output

Enter            ^M 
Ctrl + Enter     ^M 
Shift + Enter    ^M 
Windows + Enter   ^M
Alt + Enter      ^[^M

iterm2


command executed : from executable built from http://catb.org/esr/showkey/

KeyPress        output

Enter            <CTL-J=LF>
Ctrl + Enter     <CTL-J=LF>
Shift + Enter    <CTL-J=LF>
// Option key in Normal mode
Option + Enter      <CTL-J=LF>
// Option key in Meta mode
Option + Enter      <ALT-CTL-M=CR>
// Option key in Esc mode
Option + Enter      <ESC><CTL-J=LF>




command executed : ctrl + v and keypress

KeyPress        output

Enter            ^M 
Ctrl + Enter     ^M 
Shift + Enter    ^M 
// Option key in Normal mode
Option + Enter      ^M
// Option key in Meta mode
Option + Enter      <ffffffff>
// Option key in Esc mode
Option + Enter      ^[^M

@jerch
Copy link
Member

jerch commented Feb 22, 2020

@kumaran-14 Thx for testing. Well, this is quite a naming mess. I think our macOptionIsMeta comes from Terminal.app, which indeed puts ESC in front of the pressed key char:

  • macOptionIsMeta is unset: 'a' --> 3rd level shift char of key 'a' (layout dependent)
  • macOptionIsMeta is set: 'a' --> 'ESC a'

iTerm2 calls this "Esc+" mode, I have no clue what iTerm's "Meta" mode is about, so the name is abit misleading here. We are after "Esc+" in iTerm's terms.

Also this seems to be in line with emacs docs and their recommendations regarding keyboard setup (https://www.emacswiki.org/emacs/MetaKeyProblems#toc15).

But now the global macOptionIsMeta settings raises another question - how is a user supposed to access 3rd level shifts if this option is set? iTerm has an elegant solution for it - it separates the left and the right option key (btw this is pretty similar to PC keyboards, where Alt and AltGr are treated separately). My gut feeling tells me that several of our issues with other keyboard layouts on Macs could be solved, if we would allow this separation as well (in particular #2661, #2479 ff). This is nothing to be solved in this PR though, but should be investigated in one of those issues.

Basically we should follow here iTerm's behavior:

  • macOptionIsMeta is set: output the same as option+Enter in "Esc+" mode
  • macOptionIsMetais unset: output the same as option+Enter in default mode

What is still unclear to me - is there any other key active on mac keyboards as Meta key, if macOptionIsMetais unset?

@Tyriar Plz correct my assumptions/ideas above, the whole macOptionIsMeta stuff is pretty opaque to me.

@Tyriar
Copy link
Member

Tyriar commented Apr 10, 2020

Sorry about super slow response.

@jerch making macOptionIsMeta act like it does in iTerm sounds good to me. So we don't need the mode we were talking about here, just alt+enter always add the esc?

@kumaran-14
Copy link
Contributor Author

@Tyriar if it is so, i think the changes in the pr now should be right.

@@ -9,6 +9,7 @@ import { IDecPrivateModes, ICharset } from 'common/Types';
import { clone } from 'common/Clone';

const DEFAULT_DEC_PRIVATE_MODES: IDecPrivateModes = Object.freeze({
altEscMode: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kumaran-14 yep, except for this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tyriar updated.

@Tyriar Tyriar requested a review from jerch April 10, 2020 19:53
@Tyriar
Copy link
Member

Tyriar commented Apr 10, 2020

Good to merge when @jerch signs off.

Copy link
Member

@jerch jerch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to go from my side 👍
Thx for bearing with us @kumaran-14 and the bumpy ride. Opened another issue to track the Meta/option key handling for Mac (#2831).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support "alt+enter" sending \e\r
5 participants