feat: Select Links On Right Click If Present #9298
Open
+9
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is at least a partial solution to #2107. (It's not totally clear to me whether we'd want to leave #2107 open even after this change to support additional right-click actions for OSC8 URLs...)
AI Disclosure: I used Gemini CLI to help me with this PR because while I have many years of programming experience, this is my first time writing Zig. I prototyped a couple different approaches with AI before landing on this one, so AI generated various prototypes and I chose the final imlementation. I've verified that my code compiles and works as intended.
When a user right-clicks, and there's no existing selection, the existing behavior is to try to select the word under the cursor:
ghostty/src/Surface.zig
Lines 3740 to 3742 in 3548acf
This PR proposes tweaking that behavior slightly: If there's a link under our cursor, as determined by
linkAtPos, select the link. Otherwise, select the word as before.I think this behavior provides a better user experience because it seems exceptionally rare that a user is right-clicking on a link but would prefer to take action on the word rather than the (underlined) link. The new behavior also matches other terminals like iTerm and Gnome Terminal, as shown in #2107.
It's worth noting that
linkAtPosalready does the right thing in terms of checking thelinksfrom config and their highlight/hover states (modified by Ctrl or Super depending on platform). (Docs)ghostty/src/Surface.zig
Lines 3896 to 3901 in 3548acf
It also therefore respects
link-urlfrom config.ghostty/src/config/Config.zig
Lines 3411 to 3416 in 3548acf
By using
linkAtPos, we get all that behavior for free. In practical terms, that means:linkAtPosrequires that the render state mutex is held. I believe it's safe to call because we're inside a block holding the mutex:ghostty/src/Surface.zig
Lines 3702 to 3704 in 3548acf
Original Behavior:
(first without ctrl, then with ctrl)
existing_behavior.mp4
New Behavior:
(first without ctrl, then with ctrl, then pasting)
new_behavior.mp4