From 0643e8b0edf065e6095503bfd76067c6347d8d2d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 25 Mar 2021 22:10:29 -0700 Subject: [PATCH] add window:get_selection_text_for_pane refs: https://github.com/wez/wezterm/issues/575 --- docs/changelog.md | 1 + .../lua/window/get_selection_text_for_pane.md | 29 +++++++++++++++++++ wezterm-gui/src/scripting/guiwin.rs | 9 ++++++ 3 files changed, 39 insertions(+) create mode 100644 docs/config/lua/window/get_selection_text_for_pane.md diff --git a/docs/changelog.md b/docs/changelog.md index 2990bf893..1ba1219a2 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -26,6 +26,7 @@ brief notes about them may accumulate here. * Fixed: triggering the search overlay again while the search overlay is active no longer closes the underlying pane [#572](https://github.com/wez/wezterm/issues/572) * Fixed: X10 mouse coordinate reporting encoding could produce invalid outputs for large windows. Capped coordinate values to the maximum value that is representable in UTF-8 encoding * Fixed: font fallback now happens asynchronously from painting [#508](https://github.com/wez/wezterm/issues/508) +* New: added [window:get_selection_text_for_pane](config/lua/window/get_selection_text_for_pane.md) method [#575](https://github.com/wez/wezterm/issues/575) ### 20210314-114017-04b7cedd diff --git a/docs/config/lua/window/get_selection_text_for_pane.md b/docs/config/lua/window/get_selection_text_for_pane.md new file mode 100644 index 000000000..200f217c5 --- /dev/null +++ b/docs/config/lua/window/get_selection_text_for_pane.md @@ -0,0 +1,29 @@ +# `window:get_selection_text_for_pane(pane)` + +*Since: nightly builds only* + +Returns the text that is currently selected within the specified pane within +the specified window. This is the same text that would be copied to the +clipboard if the [CopyTo](../keyassignment/CopyTo.md) action were to be +performed. + +Why isn't this simply a method of the `pane` object? The reason is that the +selection is an attribute of the containing window, and a given pane can +potentially be mapped into multiple windows. + +This example logs the current selection when a CTRL+SHIFT+E is pressed: + +```lua +local wezterm = require 'wezterm'; + +wezterm.on("log-selection", function(window, pane) + local sel = window:get_selection_text_for_pane(pane); + wezterm.log_info("selection is: " .. sel) +end) + +return { + keys = { + {key="E", mods="CTRL", action=wezterm.action{EmitEvent="log-selection"}}, + } +} +``` diff --git a/wezterm-gui/src/scripting/guiwin.rs b/wezterm-gui/src/scripting/guiwin.rs index de99940ea..99ab495d4 100644 --- a/wezterm-gui/src/scripting/guiwin.rs +++ b/wezterm-gui/src/scripting/guiwin.rs @@ -79,6 +79,15 @@ impl UserData for GuiWin { }) .await }); + methods.add_async_method( + "get_selection_text_for_pane", + |_, this, pane: PaneObject| async move { + this.with_term_window(move |term_window, _ops| { + Ok(term_window.selection_text(&pane.pane()?)) + }) + .await + }, + ); methods.add_async_method( "perform_action", |_, this, (assignment, pane): (KeyAssignment, PaneObject)| async move {