Subject: bug#44973: Add a macOS global hotkey function

From: [email-masked]
Date: Tue, 29 Dec 2020 20:10:00 -0800

Hi Lars,

Here is the patch to bind a global hotkey in mac. As long as Emacs is set as
"trusted" in macOS preferences, the user can bind a two-key hotkey of the
form [modifier-key] or a single-key modifier [function key], for example
(mac-bind-global-hotkey [f1] 'tetris). Binding a three-key
combo is left to a future patch.

The code is copied from w32-register-hot-key as much as possible.

The routine intentionally does not focus the window after the hotkey is
hit; the
user must call a function like (x-focus-frame nil) to focus the frame.

I also wanted to mention that after discovering x-focus-frame, and testing
my
patch and shkd more, I realized that my patch is in fact very similar to
shkd in
functionality. The main differences are that my patch allows hotkeys to be
declared directly in emacs (which I like), and that this patch acts
directly on
an open window (e.g. making it easier to make a hotkey to yank into an
already-open buffer). On the other hand skhd would call emacsclient, which
interacts with the server, even if no frame is open, offering its own
advantages.

Anyways, I'll leave this patch for your consideration as to whether it's
the right
thing to include in emacs!

P.S. I looked into what other code in emacs might be using blocks. I believe
nsxwidget.m also has a block in nsxwidget_webkit_execute_script. I'm
guessing
it's working in our cases because we're ultimately building with the Xcode C
compiler.

Thanks

On Sun, Dec 20, 2020 at 8:35 PM Lars Ingebrigtsen <[email-masked]> wrote:

> [email-masked] writes:
>
> > But no matter what I do, it always crashes the program. I think my
> > first problem is not knowing how to call elisp (run_hooks, safe_call,
> > etc?) correctly, but second, I suspect if this is crashing due to a
> > threading issue.
>
> I am not at all familiar with the ns functions, but looking at the other
> .m files, it looks like you should be able to just say
>
>   call0 (intern ("some-function"));
>
> or something like that?  (Modulo threading stuff.)
>
> --
> (domestic pets only, the antidote for overdose, milk.)
>    bloggy blog: http://lars.ingebrigtsen.no
>