From 39f53161be4ee6f415fe6e18736ef691ec1ccd30 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 5 May 2022 21:10:06 -0700 Subject: [PATCH] copy mode: ctrl-v enables rectangular selection refs: https://github.com/wez/wezterm/issues/1656 --- config/src/keyassignment.rs | 1 + docs/changelog.md | 1 + docs/copymode.md | 2 +- wezterm-gui/src/overlay/copy.rs | 34 ++++++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index d881eb634..1759fd520 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -385,6 +385,7 @@ pub enum CopyModeAssignment { MoveToScrollbackTop, MoveToScrollbackBottom, ToggleSelectionByCell, + SetSelectionMode(Option), MoveToStartOfLineContent, MoveToEndOfLineContent, MoveToStartOfLine, diff --git a/docs/changelog.md b/docs/changelog.md index eee1e8add..114f436fa 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -26,6 +26,7 @@ As features stabilize some brief notes about them will accumulate here. * macOS: you may now drag and drop files from other programs and have their paths paste into the terminal. The new [quote_dropped_files](config/lua/config/quote_dropped_files.md) option controls how the file names are quoted. Thanks to [@junnplus](https://github.com/junnplus)! [#1868](https://github.com/wez/wezterm/pull/1868) * The mouse scroll wheel now cycles between tabs when hovering over the tab tab. Thanks to [@junnplus](https://github.com/junnplus)! [#1726](https://github.com/wez/wezterm/issues/1726) * Holding down `ALT` while dragging the left button will select a rectangular block. [ExtendSelectionToMouseCursor](config/lua/keyassignment/ExtendSelectionToMouseCursor.md) now accepts `"Block"` as a selection mode. [#1361](https://github.com/wez/wezterm/issues/1361) +* In Copy Mode, `CTRL-v` will enable rectangular block selection mode. [#1656](https://github.com/wez/wezterm/issues/1656) #### Updated * Bundled harfbuzz to 4.2.1 diff --git a/docs/copymode.md b/docs/copymode.md index 479787bed..48495cb3e 100644 --- a/docs/copymode.md +++ b/docs/copymode.md @@ -33,6 +33,7 @@ reassignable. | | `CTRL-g` | | | `q` | | Toggle cell selection mode | `v` | +| Rectangular selection | `CTRL-v` (*since: nightly builds only*)| | Move Left | `LeftArrow`| | | `h` | | Move Down | `DownArrow`| @@ -64,4 +65,3 @@ reassignable. | Move down one screen | `PageDown` | | | `CTRL-f` | - diff --git a/wezterm-gui/src/overlay/copy.rs b/wezterm-gui/src/overlay/copy.rs index 821073223..ddb08fa58 100644 --- a/wezterm-gui/src/overlay/copy.rs +++ b/wezterm-gui/src/overlay/copy.rs @@ -1,7 +1,7 @@ use crate::selection::{SelectionCoordinate, SelectionRange}; use crate::termwindow::{TermWindow, TermWindowNotif}; use config::keyassignment::{ - CopyModeAssignment, KeyAssignment, KeyTable, KeyTableEntry, ScrollbackEraseMode, + CopyModeAssignment, KeyAssignment, KeyTable, KeyTableEntry, ScrollbackEraseMode, SelectionMode, }; use mux::domain::DomainId; use mux::pane::{Pane, PaneId, Pattern, SearchResult}; @@ -37,6 +37,7 @@ struct CopyRenderable { cursor: StableCursorPosition, delegate: Rc, start: Option, + selection_mode: SelectionMode, viewport: Option, /// We use this to cancel ourselves later window: ::window::Window, @@ -105,6 +106,7 @@ impl CopyOverlay { }, editing_search: params.editing_search, result_pos: None, + selection_mode: SelectionMode::Cell, }; let search_row = render.compute_search_row(); @@ -324,11 +326,13 @@ impl CopyRenderable { fn adjust_selection(&self, start: SelectionCoordinate, range: SelectionRange) { let pane_id = self.delegate.pane_id(); let window = self.window.clone(); + let mode = self.selection_mode; self.window .notify(TermWindowNotif::Apply(Box::new(move |term_window| { let mut selection = term_window.selection(pane_id); selection.origin = Some(start); selection.range = Some(range); + selection.rectangular = mode == SelectionMode::Block; window.invalidate(); }))); self.adjust_viewport_for_cursor_position(); @@ -672,6 +676,26 @@ impl CopyRenderable { }; self.start.replace(coord); self.select_to_cursor_pos(); + self.selection_mode = SelectionMode::Cell; + } + } + + fn set_selection_mode(&mut self, mode: &Option) { + match mode { + None => { + self.start.take(); + } + Some(mode) => { + if self.start.is_none() { + let coord = SelectionCoordinate { + x: self.cursor.x, + y: self.cursor.y, + }; + self.start.replace(coord); + self.select_to_cursor_pos(); + } + self.selection_mode = *mode; + } } } } @@ -764,6 +788,7 @@ impl Pane for CopyOverlay { ClearPattern => render.clear_pattern(), EditPattern => render.edit_pattern(), AcceptPattern => render.accept_pattern(), + SetSelectionMode(mode) => render.set_selection_mode(mode), } true } @@ -1126,6 +1151,13 @@ pub fn copy_key_table() -> KeyTable { Modifiers::NONE, KeyAssignment::CopyMode(CopyModeAssignment::ToggleSelectionByCell), ), + ( + WKeyCode::Char('v'), + Modifiers::CTRL, + KeyAssignment::CopyMode(CopyModeAssignment::SetSelectionMode(Some( + SelectionMode::Block, + ))), + ), ( WKeyCode::Char('G'), Modifiers::SHIFT,