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

Can I tweak emacs Ctrl+h recognition? #1202

Open
PixelTwins opened this issue Feb 1, 2023 · 24 comments
Open

Can I tweak emacs Ctrl+h recognition? #1202

PixelTwins opened this issue Feb 1, 2023 · 24 comments
Labels

Comments

@PixelTwins
Copy link

I am trying to run an emacs -nw session in mintty for Msys2 in Windows 11.

One of the emacs keyboard shortcuts is "c-h v" for help on a variable description.

This doesn't work and I think it is because ctrl + h is a mintty key combination for backspace (or something).

I don't want this to happen. I have a backspace key.

How can I remove ctrl + h from the list of mintty key combination so I can use emacs?

@mintty
Copy link
Owner

mintty commented Feb 2, 2023

Ctrl+H is not a mintty shortcut but simply an ASCII control character.
You could use option CtrlExchangeShift to exchange them with the Shift+Ctrl shortcut combinations but then you'd probably loose other shortcut combinations.
You could reassigned with option KeyFunctions=C+h:"whatever" if you enable this with additional option ShootFoot=yes.
I don't know however, what (escape sequence) emacs would expect to interpret Ctrl+h in its own way.
Actually, emacs could also make use of modifyOtherKeys mode (with an escape sequence) to receive specific sequences for any control char. I'm surprised it doesn't.

@PixelTwins
Copy link
Author

OK. I am still puzzled though. Partly I am puzzled because this causes interference with emacs, so I am puzzled about why nobody else seems to have brought it up. emacs is an Msys2 package and mintty is the Msys terminal so it's odd others haven't found it or reported it.

In emacs c-h v invokes the "describe-variable" function.

https://www.gnu.org/software/emacs/manual/html_node/emacs/Name-Help.html

I use it frequently to find out what init file emacs used by first using c-h v then typing "user-init-file" because it will tell me both the name and location of the init file emacs used on startup without me needing to search through the directories it looks in for it. Very handy for checking out and changing emacs set ups.

What I also don't understand is the Msys2 mintty terminal is the only occasion when I face this problem of ctrl+h being undetected by emacs. It works in all other cases I have tried and those cases include the Windows GUI emacs, the cygwin console emacs, the cygwin GUI emacs, my WSL2 Linux installation and even the Msys2 GUI emacs.

So, ctrl+h works as expected in emacs on every emacs instance I can test it with except the mintty console emacs.

This is frustrating as it is mainly the Msys2 console emacs I want it to work with so I can develop in Msys2 using emacs in console mode.

Also, I don't actually know how to make the changes you suggest as I am a mintty novice.

@mintty
Copy link
Owner

mintty commented Feb 3, 2023

Ctrl+h v works for me in emacs-nw. It prompts "Describe variable: ".
Please send your mintty and emacs configuration files.

@mintty mintty changed the title Can I remove mintty keyboard shortcuts? Can I tweak emacs Ctrl+h recognition? Feb 3, 2023
@mintty
Copy link
Owner

mintty commented Feb 3, 2023

Changed title as Ctrl+h is not a shortcut but rather a plain ASCII character. Also no mintty issue identified so far, I suspect it's an emacs configuration issue.

@PixelTwins
Copy link
Author

OK, good.

Well, my .emacs file is the same as it usually is for a basic install. In only includes the stable melpa repo for package extensions:

(require 'package)
(add-to-list 'package-archives
	     '("melpa-stable" . "https://stable.melpa.org/packages/") t)
(package-initialize)

My .minttyrc file, which I have never looked at before, nor tampered with, looks like this:

CtrlShiftShortcuts=yes
Term=mintty
BackspaceSendsBS=yes
CursorType=underscore
ThemeFile=dracula
Transparency=off
OpaqueWhenFocused=yes
ClickTargetMod=shift
Language=en

Incidentally, using the same .emacs file if I open an emacs session with a GUI invoking it in mintty with emacs then it's fine. It's only if I use emacs -nw and use it in a mintty console it doesn't work.

@mintty
Copy link
Owner

mintty commented Feb 4, 2023

Your .minttrc file was tampered with, by whoever. If it wasn't, it wouldn't even exist in the first place (in cygwin), not have setting Theme=dracula etc.
In fact it is the setting BackspaceSendsBS=yes that makes the difference. It triggers the corresponding stty setting to be adapted, which apparently tells emacs that it needs to accept Ctrl+h from the Backarrow key (rather than the Linux default DEL) and can thus not interpret it as a command. I'll add a hint about this problem to the manual page.

@BrianInglis
Copy link

Most "tampering" of .minttyrc files is likely from twiddling or "oops" in the Options... dialogue!
I keep my file well commented, but sometimes click something when viewing Options..., forget to Cancel, and have to restore the commented source.

@mintty
Copy link
Owner

mintty commented Feb 4, 2023

Backspace mode can be changed via escape sequences: DECRST 67 to make it emacs-friendly, DECRQM and DECSET or XTSAVE and XTRESTORE to restore.
These could be put into a shell wrapper for invoking emacs, or emacs itself could apply them if someone submits them an issue (bug-gnu-emacs@gnu.org).

@PixelTwins
Copy link
Author

PixelTwins commented Feb 5, 2023

OK, thanks. As per BrianInglis' note the .minttyrc file exists because I played with the options. I didn't tamper with .minttyrc directly, nor did I create it, whatever saves out the options did that for me.

Also, thank you for the explanation but I still don't know how to fix it. On my system, if I remove the BackspaceSendsBS=yes line, or change it to BackspaceSendsBS=no it still doesn't work.

I have tried changing the .minttyrc filename to .minttyrcbkp and emacs still doesn't appear to capture c-h, even though mintty forgets its settings, as expected.

c-x c-c still quits emacs in every case so I think it is come ctrl codes but not all.

What do I do to get it to work?

@mintty
Copy link
Owner

mintty commented Feb 5, 2023

Did you start a new mintty window after the change?
For a running mintty, you can check or switch the setting in the Options dialog, section Keys, first option there. Options are saved with the Save button, or Enter.

@PixelTwins
Copy link
Author

PixelTwins commented Feb 8, 2023

Yes. I have restarted mintty since and restarted the computer too. On my PC it doesn't matter what I set Backarrow sends ^H to, I always get "v is undefined" when trying c-h v in a MING64 terminal after starting emacs with the -nw switch. As before, under all other conditions c-h v works. It is only when using -nw with MSYS mintty that it doesn't work.

My .minttyrc file reads:

BackspaceSendsBS=no
DeleteSendsDEL=no

Oh. I have just noticed something. It works fine with cygwin and cygwin also uses mintty and it uses the same version too.

I have just tried duplicating all my setting from the cygwin console to MING64 using the options menus and it doesn't help. c-h v still works with emacs -nw in cygwin and not MSYS.

@mintty
Copy link
Owner

mintty commented Feb 8, 2023

You quoted your .minttyrc file to contain
BackspaceSendsBS=yes
in a previous comment. It's hard to analyse the issue remotely. What does command stty -a say about the erase char? If the tty settings are configured to erase = ^H it's likely that emacs assumes Backarrow to send ^H (it could alternatively enquire the terminal in case its behaviour is inconsistent). So if emacs assume ^H to be used by Backarrow, it's quite clear that it doesn't interpret ^H as a command.

@PixelTwins
Copy link
Author

PixelTwins commented Feb 9, 2023

You quoted your .minttyrc file to contain
BackspaceSendsBS=yes
in a previous comment.

Well, yes, but clearly I have changed it since then, to the setting you recommended, hence my point: I have changed that setting to no but c-h v still doesn't work.

I have typed stty -a and I get this:

speed 38400 baud; rows 27; columns 100; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = ^Z;
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O;
min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel
iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -tostop echoctl echoke -flusho

My .emacs is as before:

(require 'package)
(add-to-list 'package-archives
	     '("melpa-stable" . "https://stable.melpa.org/packages/") t)
(package-initialize)

I am even more puzzled since discovering it works perfectly well with cygwin but not with MSYS2/MING64.

@mintty
Copy link
Owner

mintty commented Feb 9, 2023

I've just replayed the case in MSYS2 and ^H v prompts "Describe variable" for me.

@PixelTwins
Copy link
Author

I have asked on Stack Exchange too and the question has received no response.

I am at a loss.

@mintty
Copy link
Owner

mintty commented Feb 14, 2023

Revisiting this issue, some more questions:
In the mintty where emacs fails on ^H, what does the command printf '\e[?67$p' do?
You don't run emacs in a wrapper with some additional layer like tmux or remote invocation/ssh?
What's the link to the StackExchange issue?

@PixelTwins
Copy link
Author

Sorry about the delay. I had temporarily sidestepped the issue and decided to not use Msys instead.

If I type printf '\e[?67$p' in the Msys mintty where it fails then I see this:

67;2$y

This is the same output I see if I type the same command for the Cygwin mintty where it works.

You don't run emacs in a wrapper with some additional layer like tmux or remote invocation/ssh?

Not that I am aware of. I have only started it using the ming64.exe from the MSYS2 installation.

What's the link to the StackExchange issue?

The link to the Stack Exchange issue I started is here below. Nobody has responded to it in the two months since I posted it. It has only 24 views:

https://emacs.stackexchange.com/questions/75630/how-can-i-get-the-msys2-cli-to-recognise-the-emacs-ctrl-h-key-chord

@mintty
Copy link
Owner

mintty commented Mar 8, 2023

I missed to ask a very basic question: Is it the native emacs package of your environment? What does type emacs say?

@PixelTwins
Copy link
Author

type emacs says emacs is /mingw64/bin/emacs

@mintty
Copy link
Owner

mintty commented Mar 8, 2023

For me it says emacs is /usr/bin/emacs. That may well explain a difference.
How did you install your package?

@Biswa96
Copy link
Contributor

Biswa96 commented Mar 8, 2023

There are two emacs packages in msys2.

  • emacs: Linked with msys2, similar as cygwin.
  • mingw-w64-emacs: uses native Win32 APIs, not linked with msys2.

List can be found here https://packages.msys2.org/search?q=emacs

I am not familiar with emacs. But I can ask for help if needed.

@PixelTwins
Copy link
Author

PixelTwins commented Mar 9, 2023

You know what? I think we're on to something.

Biswa96 is right. There are two emacs packages and mintty is right our type emacs return something different.

I had used the emacs installation process detailed here:

https://packages.msys2.org/package/mingw-w64-x86_64-emacs

It turns out there is another emacs package here:

https://packages.msys2.org/base/emacs

Since reading the messages above I have installed the older base package version. Now I also have emacs executables in /usr/bin which were not there before. If I now first navigate to /usr/bin and run emacs with ./emacs -nw from within the /usr/bin directory then the older emacs runs and.... ctrl + h v works!

I'd describe that as a big step forwards. Thanks!

OK, I had paused after the line above to try an obvious step: Remove the mingw-w64-x86_64-emacs package.

After uninstalling the other emacs package and restarting the console type emacs matches mintty's and says emacs is /usr/bin/emacs. After I run emacs it says emacs is hashed (/usr/bin/emacs).

Either way, now when I run emacs, from anywhere, with emacs it runs the older 27.2 revision in console mode and... it works! I can now type c-h v to launch the describe-variable function at which point I can type user-init-file to find the location of the emacs initialisation file.

Thanks! :D

It only leaves me wondering why the w64 version does not work and if it can it be fixed with options?

@listenerri
Copy link

I also encountered this problem, when I mapped the key in vim, vim could not receive ctrl-h.

The vim executable I use is a windows native application:

which vim
/c/Program Files/Vim/vim90/vim

When I installed the msys/vim package and tested it, everything worked fine.

Windows native vim also does not work properly in wsl.

@mintty
Copy link
Owner

mintty commented Nov 27, 2023

If you mean the Backspace key, it sends the DEL character by default, not BS, in cygwin, like in Linux. Can be reconfigured.

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

No branches or pull requests

5 participants