macOS: Selection for Find feature

Adds the `selection_for_search` action, with Cmd+E keybind by default.
This action inputs the currently selected text into the search
field without changing focus, matching standard macOS behavior.
This commit is contained in:
Aaron Ruan
2026-01-06 22:15:19 +08:00
parent c5bc6bb2ce
commit 9b6a3be993
14 changed files with 134 additions and 5 deletions

View File

@@ -5163,6 +5163,15 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
);
},
.selection_for_search => {
const selection = try self.selectionString(self.alloc) orelse return false;
return try self.rt_app.performAction(
.{ .surface = self },
.selection_for_search,
.{ .text = selection },
);
},
.end_search => {
// We only return that this was performed if we actually
// stopped a search, but we also send the apprt end_search so

View File

@@ -316,6 +316,9 @@ pub const Action = union(Key) {
/// Start the search overlay with an optional initial needle.
start_search: StartSearch,
/// Input the selected text into the search field.
selection_for_search: SelectionForSearch,
/// End the search overlay, clearing the search state and hiding it.
end_search,
@@ -389,6 +392,7 @@ pub const Action = union(Key) {
show_on_screen_keyboard,
command_finished,
start_search,
selection_for_search,
end_search,
search_total,
search_selected,
@@ -914,3 +918,18 @@ pub const SearchSelected = struct {
};
}
};
pub const SelectionForSearch = struct {
text: [:0]const u8,
// Sync with: ghostty_action_selection_for_search_s
pub const C = extern struct {
text: [*:0]const u8,
};
pub fn cval(self: SelectionForSearch) C {
return .{
.text = self.text.ptr,
};
}
};

View File

@@ -6585,6 +6585,12 @@ pub const Keybinds = struct {
.start_search,
.{ .performable = true },
);
try self.set.putFlags(
alloc,
.{ .key = .{ .unicode = 'e' }, .mods = .{ .super = true } },
.selection_for_search,
.{ .performable = true },
);
try self.set.putFlags(
alloc,
.{ .key = .{ .unicode = 'f' }, .mods = .{ .super = true, .shift = true } },

View File

@@ -368,6 +368,9 @@ pub const Action = union(enum) {
/// If a previous search is active, it is replaced.
search: []const u8,
/// Input the selected text into the search field.
selection_for_search,
/// Navigate the search results. If there is no active search, this
/// is not performed.
navigate_search: NavigateSearch,
@@ -1284,6 +1287,7 @@ pub const Action = union(enum) {
.cursor_key,
.search,
.navigate_search,
.selection_for_search,
.start_search,
.end_search,
.reset,

View File

@@ -189,6 +189,12 @@ fn actionCommands(action: Action.Key) []const Command {
.description = "Start a search if one isn't already active.",
}},
.selection_for_search => comptime &.{.{
.action = .selection_for_search,
.title = "Selection for Search",
.description = "Input the selected text into the search field.",
}},
.end_search => comptime &.{.{
.action = .end_search,
.title = "End Search",