From e8ad765ff51303604dddeb253afc533a02138d2a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 15 Nov 2020 22:23:18 -0800 Subject: [PATCH] wezterm: add SelectionMode::SemanticZone This makes it possible to configure wezterm to eg: triple click on command input (or output) to select the entire input or output without messing around trying to find the bounds. The docs have an example of how to configure this; it requires setting up shell integration to define the appropriate semantic zones. --- config/src/keyassignment.rs | 1 + docs/changelog.markdown | 2 + .../keyassignment/SelectTextAtMouseCursor.md | 22 +++++++++ docs/shell-integration.markdown | 3 +- wezterm-gui/src/gui/selection.rs | 49 ++++++++++++++++++- wezterm-gui/src/gui/termwindow.rs | 20 ++++++++ 6 files changed, 95 insertions(+), 2 deletions(-) diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index 68734c5b3..d9bc64d52 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -12,6 +12,7 @@ pub enum SelectionMode { Cell, Word, Line, + SemanticZone, } #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] diff --git a/docs/changelog.markdown b/docs/changelog.markdown index ac0cb5c0e..6b7b26407 100644 --- a/docs/changelog.markdown +++ b/docs/changelog.markdown @@ -16,6 +16,8 @@ brief notes about them may accumulate here. * Add [`ScrollToPrompt`](config/lua/keyassignment/ScrollToPrompt.md) key assignment that scrolls the viewport to the prior/next shell prompt emitted using OSC 133 Semantic Prompt escapes. This assignment is not bound by default. * Fixed an issue where `SpawnWindow` didn't use the current working directory from the current pane to spawn the new window * Added `wezterm start --class CLASSNAME` option to specify the window class name under X11 and Windows, or the `app_id` under Wayland. See `wezterm start --help` for more information. +* Added shell integration for setting OSC 7 (working directory) and OSC 133 (semantic zones) for Zsh and Bash. [See Shell Integration docs](shell-integration.md). +* Added `SemanticZone` as a possible parameter for [SelectTextAtMouseCursor](config/lua/keyassignment/SelectTextAtMouseCursor.md), making it possible to conveniently select complete input or output regions. ### 20201101-103216-403d002d diff --git a/docs/config/lua/keyassignment/SelectTextAtMouseCursor.md b/docs/config/lua/keyassignment/SelectTextAtMouseCursor.md index 2c0f4f454..8b1899902 100644 --- a/docs/config/lua/keyassignment/SelectTextAtMouseCursor.md +++ b/docs/config/lua/keyassignment/SelectTextAtMouseCursor.md @@ -4,4 +4,26 @@ Initiates selection of text at the current mouse cursor position. The mode argument can be one of `Cell`, `Word` or `Line` to control the scope of the selection. +*Since: nightly builds only* + +The mode argument can be `SemanticZone` which causes the selection +to take the surrounding semantic zone. + +In this example, the triple-left-click mouse action is set to +automatically select the entire command output when clicking +on any character withing that region: + +```lua +return { + mouse_bindings = { + { event={Down={streak=3, button="Left"}}, + action={SelectTextAtMouseCursor="SemanticZone"}, + mods="NONE" + }, + }, +} +``` + +[See Shell Integration docs](../../../shell-integration.md) for more details on +how to set up your shell to define semantic zones. diff --git a/docs/shell-integration.markdown b/docs/shell-integration.markdown index 655310a91..05029bfac 100644 --- a/docs/shell-integration.markdown +++ b/docs/shell-integration.markdown @@ -7,7 +7,8 @@ wezterm supports integrating with the shell through the following means: These sequences enable some improved user experiences, such as being able to spawn new panes, tabs and windows with the same current working directory -as the current pane, or [jumping through the scrollback to the start of an earlier command](config/lua/keyassignment/ScrollToPrompt.md). +as the current pane, [jumping through the scrollback to the start of an earlier command](config/lua/keyassignment/ScrollToPrompt.md), +or [conveniently selecting the complete output from a command](config/lua/keyassignment/SelectTextAtMouseCursor.md). In order for these features to be enabled, you will need to configure your shell program to emit the escape sequences at the appropriate place. diff --git a/wezterm-gui/src/gui/selection.rs b/wezterm-gui/src/gui/selection.rs index 09dc77e87..783c3f588 100644 --- a/wezterm-gui/src/gui/selection.rs +++ b/wezterm-gui/src/gui/selection.rs @@ -2,9 +2,10 @@ // and inclusive range #![cfg_attr(feature = "cargo-clippy", allow(clippy::range_plus_one))] use mux::renderable::Renderable; +use std::cmp::Ordering; use std::ops::Range; use termwiz::surface::line::DoubleClickRange; -use wezterm_term::StableRowIndex; +use wezterm_term::{SemanticZone, StableRowIndex}; #[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] pub struct Selection { @@ -80,6 +81,52 @@ impl SelectionRange { } } + pub fn zone_around(start: SelectionCoordinate, pane: &dyn mux::pane::Pane) -> Self { + let zones = match pane.get_semantic_zones() { + Ok(z) => z, + Err(_) => return Self { start, end: start }, + }; + + fn find_zone(start: &SelectionCoordinate, zone: &SemanticZone) -> Ordering { + match zone.start_y.cmp(&start.y) { + Ordering::Greater => return Ordering::Greater, + // If the zone starts on the same line then check that the + // x position is within bounds + Ordering::Equal => match zone.start_x.cmp(&start.x) { + Ordering::Greater => return Ordering::Greater, + Ordering::Equal | Ordering::Less => {} + }, + Ordering::Less => {} + } + match zone.end_y.cmp(&start.y) { + Ordering::Less => Ordering::Less, + // If the zone ends on the same line then check that the + // x position is within bounds + Ordering::Equal => match zone.end_x.cmp(&start.x) { + Ordering::Less => Ordering::Less, + Ordering::Equal | Ordering::Greater => Ordering::Equal, + }, + Ordering::Greater => Ordering::Equal, + } + } + + if let Ok(idx) = zones.binary_search_by(|zone| find_zone(&start, zone)) { + let zone = &zones[idx]; + Self { + start: SelectionCoordinate { + x: zone.start_x, + y: zone.start_y, + }, + end: SelectionCoordinate { + x: zone.end_x, + y: zone.end_y, + }, + } + } else { + Self { start, end: start } + } + } + /// Computes the selection range for the word around the specified coords pub fn word_around(start: SelectionCoordinate, renderer: &mut dyn Renderable) -> Self { let (first, lines) = renderer.get_lines(start.y..start.y + 1); diff --git a/wezterm-gui/src/gui/termwindow.rs b/wezterm-gui/src/gui/termwindow.rs index 52c83b863..f6d36b6bd 100644 --- a/wezterm-gui/src/gui/termwindow.rs +++ b/wezterm-gui/src/gui/termwindow.rs @@ -3302,6 +3302,19 @@ impl TermWindow { let selection_range = start_line.extend_with(end_line); self.selection(pane.pane_id()).range = Some(selection_range); } + SelectionMode::SemanticZone => { + let end_word = SelectionRange::zone_around(SelectionCoordinate { x, y }, &**pane); + + let start_coord = self + .selection(pane.pane_id()) + .start + .clone() + .unwrap_or(end_word.start); + let start_word = SelectionRange::zone_around(start_coord, &**pane); + + let selection_range = start_word.extend_with(end_word); + self.selection(pane.pane_id()).range = Some(selection_range); + } } // When the mouse gets close enough to the top or bottom then scroll @@ -3354,6 +3367,13 @@ impl TermWindow { self.selection(pane.pane_id()).start = Some(selection_range.start); self.selection(pane.pane_id()).range = Some(selection_range); } + SelectionMode::SemanticZone => { + let selection_range = + SelectionRange::zone_around(SelectionCoordinate { x, y }, &**pane); + + self.selection(pane.pane_id()).start = Some(selection_range.start); + self.selection(pane.pane_id()).range = Some(selection_range); + } SelectionMode::Cell => { self.selection(pane.pane_id()) .begin(SelectionCoordinate { x, y });