Using Autohotkey for Emacs

2

1

I am using emacs in Cygwin and have used Autohotkey to re-map the lefthand 'Alt' key and the lefthand 'Windows' key to be (left)Ctrl and (left)Alt respectively.

This means that an emacs sequence like Ctrl-xCtrl-c is done with my left thumb pressing on the key labelled 'Alt' to produce the Ctrl. Ok.

All seems okay except that I cannot get M-SPC to work, so my question is to ask for help on that.

Here is the Autohotkey script I am using (by the way it is only for theoretical completeness that I re-map the lefthand Ctrl key to be the lefthand Windows button -- I have never actually pressed it when using emacs):

#IfWinActive ahk_class mintty
LWin::LAlt
LAlt::LCtrl
LCtrl::LWin

To be clear, when I say 'all is ok' I mean that within emacs I use the 'describe key sequence' feature (C-h then k) and then try out the key combination I want. For example, all of these show the emacs documentation for the sequence I expected:

  • Key labelled 'Alt' + g reports emacs documentation for C-g. Good.
  • Key labelled 'Alt' + SPACE reports the documentation for C-@, which emacs says is also mapped to C-SPC, so that seems okay. Good.
  • Key labelled 'MSWin Symbol' + x reports documentation for M-x. Good.
  • Key labelled 'MSWin Symbol' + key labelled 'Alt'+ k reports documentation for C-M-k. Good.

But:

  • Key labelled 'MSWin Symbol' + SPACE shows me the 'right-click' menu for the cygwin window rather than taking me to the documentation for M-SPC. Bad.
  • By the way, I can get to the documentation for M-SPC by instead doing ESC followed by SPACE.

Let me add another point which has come up in the process of reacting to comments on this question:

  • Key labelled 'MSWin Symbol' + key labelled 'Alt'+ SPACE takes me to the documentation for C-@, so I suspect it is being understood by emacs as C-SPC rather than as C-M-SPC as I thought my Autohotkey script would map it to. Bad.

Any ideas on why Autohotkey won't do the M-SPC as I would expect, or why C-M-SPC is understood by emacs as a C-SPC (I suspect)?

Thanks

Robert

Posted 2013-04-09T10:52:36.357

Reputation: 179

Funnily enough, in another SO question that I am involved in (see the first comment in http://stackoverflow.com/a/15884328/1243435), Thomas mentions that C-M-SPC can be problematic in some terminals and explains that for him it does exactly what I am seeing here -- it produces a right-click menu for the terminal itself.

– Robert – 2013-04-09T14:02:19.283

that's quite a weird remapping... Most people who do remap CTRL remap it to CAPS-LOCK. Also quite some expensive keyboards come with CTRL in place of CAPS-LOCK (which is where CTRL was on older keyboards btw), like the Happy Hacking Pro 2 for example. Note that if you're touch typing you'll find CTRL-x quite hard to do correctly, no matter where you put CTRL, so you can use Emacs and ctl-x-map to remap all the "C-x ..." shortcuts to something else (I'm using C-, instead of C-x). – user988052 – 2013-04-10T03:01:02.280

On my MacBook at home the CMD key sits next to the space-bar, ie. exactly where the Alt key is on my PC keyboard. On the MacBook the CMD key is often used in circumstances where Ctrl would be used on a PC. I got to like it. I did try the pinkie remapping of using the PC's CAPS-LOCK as the Ctrl but that hurts! My poor little finger was aching! Rather, with the PC's Alt as Ctrl you just tuck your (stronger) thumb under the arch of your fingers and bingo! there is Ctrl. Nicer, I think. On this one I would claim that Apple got the layout right (let's not discuss their placement of '#'). – Robert – 2013-04-10T08:08:47.473

You're complaining about "Key labelled 'Alt' + SPACE" being misunderstood; but you've made it clear that the Windows logo key is your Meta, so you should amend this to say "Key labelled 'MSWin Symbol' + SPACE". Unless I've really misunderstood something (not hard to do when you're talking about shifting so many keys around!) – echristopherson – 2013-04-11T21:01:24.817

@echristoperson You are right. I am about to correct that. Thank you. – Robert – 2013-04-17T16:31:23.817

Answers

2

Here are answers to your problem:

Issues remaping ctrl to alt and alt to ctrl for Emacs

And a discussion of why that happens

Plus to the script that can be found following first link you can add this code to work will all possible keyboard layouts.

#IfWinNotActive, ahk_group HaveEmacsHotkeys
#InputLevel 1
; Aditionally remaps Window keys and AppsKey
*RWin::
    SetKeyDelay, -1
    Send, {Blind}{Alt DownTemp}
    return 
*RWin up::
    SetKeyDelay, -1
    Send, {Blind}{Alt Up}
    return

*LWin::
    SetKeyDelay, -1
    Send, {Blind}{Alt DownTemp}
    return 
*LWin up::
    SetKeyDelay, -1
    Send, {Blind}{Alt Up}
    return

*AppsKey::
    SetKeyDelay, -1
    Send, {Blind}{Alt DownTemp}
    return 
*AppsKey up::
    SetKeyDelay, -1
    Send, {Blind}{Alt Up}
#IfWinActive

AntonLosev

Posted 2013-04-09T10:52:36.357

Reputation: 21

1

Alt+Space is a standard Windows shortcut for showing the window menu, which you can also get by left-clicking on the window icon or right-clicking on the titlebar.

You can disable that shortcut on the Keys page of the Cygwin terminal's options (together with the Alt+Enter shortcut for fullscreen, for which Alt+F11 can be used instead).

ak2

Posted 2013-04-09T10:52:36.357

Reputation: 3 387

Understood, and have un-set it in the terminal options. However, within emacs my 'test' of using C-h k to describe a key sequence tells me that C-M-SPC is interpreted as C-SPC (this was what Thomas actually explained in the comment I mentioned above). – Robert – 2013-04-09T14:09:53.460

You didn't say anything about C-M-SPC in your original question. FWIW, it works fine for me (not using AutoHotKey): "C-M-@ runs the command mark-sexp, which is an interactive compiled Lisp function. It is bound to C-M-@, C-M-SPC." – ak2 – 2013-04-09T14:38:48.317

My apologies, I had got a couple of sentences muddled in my original question. I am now updating it to correct those and to include mention of the problem with C-M-SPC only being seen as C-SPC by emacs. – Robert – 2013-04-09T15:05:48.493

Regarding C-M-SPC, does it work without the AHK script, pressing the actual Ctrl+Alt+SPC keys? – ak2 – 2013-04-10T08:29:16.850

Good point! Yes it does work: I turn off AHK and my C-h k test in emacs correctly takes me to the documentation for C-M-@ (bound to C-M-SPC as you will know) when I press Ctrl+Alt+SPACE. Thank you for the good question! That pretty-much proves that I do have a problem with my AHK script as I suspected (coz I am an AHK novice). – Robert – 2013-04-10T08:57:47.447