Cut/paste chording outside Acme on macOS

54 views
Skip to first unread message

Colin B

unread,
Feb 27, 2025, 12:07:07 PMFeb 27
to plan9port-dev
Hi all,

I've used a macOS automation tool, Hammerspoon, to simulate Acme's cut/paste mouse chords system wide (so they may be used in other apps, like Safari, Stickies, etc.). The goal is to reduce that bit of cognitive friction or context switching when moving between cut/paste paradigms. Sharing here in case it is of interest to any other macOS plan9port users:


At a high level, Hammerspoon uses the macOS accessibility API to expose the OS to a Lua scripting engine. This Lua script listens to mouse button down/up events and simulates keystrokes (CMD-C, DEL, and CMD-V) to manipulate the focused application and system pasteboard. When Acme itself has input, it stops all its event listeners so as to not interfere with native chords.

I'm new to Hammerspoon and Lua, so there may bone-headed mistakes in this script, but my experience so far has been positive. Happy to discuss Qs off list too.

Colin

P.S. Welcome feedback if this is off topic. It's the only plan9port discussion community I follow, so sharing here even though this is broadly about p9p users rather than dev.

Morton Muggsy

unread,
Mar 3, 2025, 7:58:07 AMMar 3
to plan9port-dev
Very nice, I like it although I mostly write on my linux machine. I normally just highlight (button 1) and then button 3 without leaving the button 3 I select copy.
would It work on terminals like xterm/st for example?
Is it okay if I try and rewrite a version for linux?
I'm thinking maybe a bash script that acts as a daemon will do. we'll see.

Morton

Colin Bortner

unread,
Mar 3, 2025, 10:49:37 AMMar 3
to plan9p...@googlegroups.com
Hi Morton - response below.

On Mon, Mar 3, 2025, at 13:31, Morton Muggsy wrote:
Very nice, I like it although I mostly write on my linux machine. I normally just highlight (button 1) and then button 3 without leaving the button 3 I select copy.
would It work on terminals like xterm/st for example?

I know very little about the post-Wayland linux desktop environment. But some thoughts in case they are helpful:

- In X11, there is/was a concept of "selections", and in particular the "primary" and "clipboard" selections. In general, the primary selection is passively set (updated any time you selected text) and that selection is what you're requesting with a middle-click paste, on the other hand the clipboard selection was intended to only be explicitly set and requested with copy/paste operations.

- X11 applications vary in how they request selections. That's why not all applications have the same hotkeys for copy-paste operations. I expect modern xterm is very flexible, and could work with whatever you came up with, but I would guess st (from having encountered dwm from suckless) may take some tinkering (patching/compiling) to respond to the same input as the rest of your desktop system.

Is it okay if I try and rewrite a version for linux?

Please do - but I think it may be harder in certain ways due to the heterogeneity of the Linux desktop. In any case: Off the top of my head:

- The first questions are: which X11 selection are we using for this, and how are we triggering applications to set and request that selection? But if you solve those two things, and ensure all your applications respond to that trigger (which can be very high level like simulated key presses or lower level like interacting with desktop frameworks, X11), then seems entirely doable.

- The second question is, how can you "intercept" mouse events (implement logic to detect the chord so cancel the normal mouse click events). I have a vague memory of how this works on X11, but that's all probably out of date with Wayland desktops. In the X11 days, in some ways doing this would be easier on Linux than macOS because of the security model, but in my case Hammerspoon solves that.

I'm thinking maybe a bash script that acts as a daemon will do. we'll see.

Good luck. Feel free to ping me directly (off-list) if there's anything you think I may quick answer to.


Morton
El jueves, 27 de febrero de 2025 a las 18:07:07 UTC+1, Colin B escribió:
Hi all,

I've used a macOS automation tool, Hammerspoon, to simulate Acme's cut/paste mouse chords system wide (so they may be used in other apps, like Safari, Stickies, etc.). The goal is to reduce that bit of cognitive friction or context switching when moving between cut/paste paradigms. Sharing here in case it is of interest to any other macOS plan9port users:


At a high level, Hammerspoon uses the macOS accessibility API to expose the OS to a Lua scripting engine. This Lua script listens to mouse button down/up events and simulates keystrokes (CMD-C, DEL, and CMD-V) to manipulate the focused application and system pasteboard. When Acme itself has input, it stops all its event listeners so as to not interfere with native chords.

I'm new to Hammerspoon and Lua, so there may bone-headed mistakes in this script, but my experience so far has been positive. Happy to discuss Qs off list too.

Colin

P.S. Welcome feedback if this is off topic. It's the only plan9port discussion community I follow, so sharing here even though this is broadly about p9p users rather than dev.


--

---
You received this message because you are subscribed to the Google Groups "plan9port-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plan9port-de...@googlegroups.com.

sqweek E.

unread,
Mar 3, 2025, 6:41:51 PMMar 3
to plan9p...@googlegroups.com, googl...@chb.xyz
Very cool!
 
I tried to do this on windows but never figured out the correct incantation to reliably simulate cut/paste events.
 
(I did get plan9's ALT-compose-key working everywhere in windows using a "low-level keyboard hook", which remains a source of regular satisfaction https://github.com/sqweek/9ime )

-sqweek
Reply all
Reply to author
Forward
0 new messages