diff --git a/ci/generate-docs.py b/ci/generate-docs.py index 3a9cdf915..192ac91d8 100644 --- a/ci/generate-docs.py +++ b/ci/generate-docs.py @@ -140,6 +140,7 @@ TOC = [ ], ), Page("Scrollback", "scrollback.md"), + Page("Quick Select Mode", "quickselect.md"), Page("Copy Mode", "copymode.md"), Page("Hyperlinks", "hyperlinks.md"), Page("Shell Integration", "shell-integration.md"), diff --git a/config/src/lib.rs b/config/src/lib.rs index 82cb52b43..d7d02daf4 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -843,6 +843,13 @@ pub struct Config { pub disable_default_key_bindings: bool, pub leader: Option, + #[serde(default)] + pub disable_default_quick_select_patterns: bool, + #[serde(default)] + pub quick_select_patterns: Vec, + #[serde(default = "default_alphabet")] + pub quick_select_alphabet: String, + #[serde(default)] pub mouse_bindings: Vec, #[serde(default)] @@ -1108,6 +1115,10 @@ fn default_alternate_buffer_wheel_scroll_speed() -> u8 { 3 } +fn default_alphabet() -> String { + "asdfqwerzxcvjklmiuopghtybn".to_string() +} + fn default_word_boundary() -> String { " \t\n{[}]()\"'`".to_string() } diff --git a/docs/changelog.md b/docs/changelog.md index 13efeee23..64620155e 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -41,6 +41,7 @@ As features stabilize some brief notes about them will accumulate here. * New: [format-tab-title](config/lua/window-events/format-tab-title.md) hook for customizing the text in tab titles. [#647](https://github.com/wez/wezterm/issues/647) * New: [force_reverse_video_cursor](config/lua/config/force_reverse_video_cursor.md) setting to override the cursor color scheme settings. [#706](https://github.com/wez/wezterm/issues/706) * Fixed: ssh config parsing now expands `~` to your home directory for appropriate options; previously only `%d` and `${HOME}` were substituted. [#729](https://github.com/wez/wezterm/issues/729) +* New: [Quick Select Mode](quickselect.md) for a tmux-fingers/tmux-thumbs style mouse-less select and copy flow [#732](https://github.com/wez/wezterm/issues/732) ### 20210405-110924-a5bb5be8 diff --git a/docs/config/keys.md b/docs/config/keys.md index ca824311c..1bc7b859c 100644 --- a/docs/config/keys.md +++ b/docs/config/keys.md @@ -172,6 +172,7 @@ The default key bindings are: | `SUPER` | `f` | `Search={CaseSensitiveString=""}` | | `CTRL+SHIFT` | `F` | `Search={CaseSensitiveString=""}` | | `CTRL+SHIFT` | `X` | `ActivateCopyMode` | +| `CTRL+SHIFT` | ` ` | `QuickSelect` (*since: nightly builds only*) | | `CTRL+SHIFT+ALT` | `"` | `SplitVertical={domain="CurrentPaneDomain"}` | | `CTRL+SHIFT+ALT` | `%` | `SplitHorizontal={domain="CurrentPaneDomain"}` | | `CTRL+SHIFT+ALT` | `LeftArrow` | `AdjustPaneSize={"Left", 1}` | diff --git a/docs/config/lua/config/disable_default_quick_select_patterns.md b/docs/config/lua/config/disable_default_quick_select_patterns.md new file mode 100644 index 000000000..1b5e9aff1 --- /dev/null +++ b/docs/config/lua/config/disable_default_quick_select_patterns.md @@ -0,0 +1,7 @@ +# disable_default_quick_select_patterns + +*Since: nightly builds only* + +When set to `true`, the default set of quick select patterns +are omitted, and your [quick_select_patterns](quick_select_patterns.md) +config specifies the total set of patterns used for [quick select mode](../../../quickselect.md). diff --git a/docs/config/lua/config/quick_select_alphabet.md b/docs/config/lua/config/quick_select_alphabet.md new file mode 100644 index 000000000..0259e16c8 --- /dev/null +++ b/docs/config/lua/config/quick_select_alphabet.md @@ -0,0 +1,21 @@ +# quick_select_alphabet + +*Since: nightly builds only* + +Specify the alphabet used to produce labels for the items +matched in [quick select mode](../../../quickselect.md). + +The default alphabet is `"asdfqwerzxcvjklmiuopghtybn"` which +means that the first matching item from the bottom is labelled +with an `a`, the second with `s` and so forth; these are easily +accessible characters in a `qwerty` keyboard layout. + +|Keyboard Layout|Suggested Alphabet| +|---------------|------------------| +|`qwerty` |`"asdfqwerzxcvjklmiuopghtybn"` (this is the default)| +|`qwertz` |`"asdfqweryxcvjkluiopmghtzbn"`| +|`azerty` |`"qsdfazerwxcvjklmuiopghtybn"`| +|`dvorak` |`"aoeuqjkxpyhtnsgcrlmwvzfidb"`| +|`colemak` |`"arstqwfpzxcvneioluymdhgjbk"`| + + diff --git a/docs/config/lua/config/quick_select_patterns.md b/docs/config/lua/config/quick_select_patterns.md new file mode 100644 index 000000000..cc5b28b2a --- /dev/null +++ b/docs/config/lua/config/quick_select_patterns.md @@ -0,0 +1,17 @@ +# quick_select_patterns + +*Since: nightly builds only* + +Specify additional patterns to match when in [quick select mode](../../../quickselect.md). +This setting is a table listing out a set of regular expressions. + +```lua +return { + quick_select_patterns = { + -- match things that look like sha1 hashes + -- (this is actually one of the default patterns) + "[0-9a-f]{7,40}", + } +} +``` + diff --git a/docs/config/lua/keyassignment/QuickSelect.md b/docs/config/lua/keyassignment/QuickSelect.md new file mode 100644 index 000000000..39f332131 --- /dev/null +++ b/docs/config/lua/keyassignment/QuickSelect.md @@ -0,0 +1,15 @@ +# QuickSelect + +*Since: nightly builds only* + +Activates [Quick Select Mode](../../../quickselect.md). + +```lua +```lua +local wezterm = require 'wezterm'; +return { + keys = { + {key=" ", mods="SHIFT|CTRL", action="QuickSelect"}, + } +} +``` diff --git a/docs/copymode.md b/docs/copymode.md index 8580f5eb2..479787bed 100644 --- a/docs/copymode.md +++ b/docs/copymode.md @@ -3,7 +3,10 @@ *since: 20200607-144723-74889cd4* Copy mode allows you to make selections using the keyboard; no need to reach -for your mouse or trackpad. +for your mouse or trackpad. Copy mode is similar to [quick select + mode](quickselect.md) but is geared up for describing selections based on +keyboard control, whereas quick select mode is used to quickly select and +copy commonly used patterns. The `ActivateCopyMode` key assignment is used to enter copy mode; it is bound to `CTRL-SHIFT-X` by default. diff --git a/docs/quickselect.md b/docs/quickselect.md new file mode 100644 index 000000000..04e0232f3 --- /dev/null +++ b/docs/quickselect.md @@ -0,0 +1,26 @@ +## Quick Select Mode + +*Since: nightly builds only* + +Quick Select mode allows you to quickly highlight text that matches +commonly copied patterns, select a match by typing a one-or-two character +prefix and copy it to the clipboard. + +The `QuickSelect` key assignment is used to enter quick select mode; it is +bound to `CTRL-SHIFT-SPACE` by default. + +When quick select mode is activated, the terminal is searched for items that +match the patterns defined by the +[quick_select_patterns](config/lua/config/quick_select_patterns.md) +configuration combined with a default set of patterns that match things such as +URL and path fragments, git hashes, ip addresses and numbers. + +Matches are highlighted and shown with a one or two character prefix derived +from the [quick_select_alphabet](config/lua/config/quick_select_alphabet.md) +configuration. + +The bottom of the screen shows your input text along with a hint as to what to +do next; typing in a highlighted prefix will cause that text to be selected and +copied to the clipboard, and quick select mode will be cancelled. Pressing +`ESCAPE` will cancel quick select mode. + diff --git a/mux/src/localpane.rs b/mux/src/localpane.rs index 296885275..f2ca3542e 100644 --- a/mux/src/localpane.rs +++ b/mux/src/localpane.rs @@ -340,15 +340,25 @@ impl Pane for LocalPane { } Pattern::Regex(r) => { if let Ok(re) = regex::Regex::new(r) { - for m in re.find_iter(haystack) { - let (start_x, start_y) = haystack_idx_to_coord(m.start(), coords); - let (end_x, end_y) = haystack_idx_to_coord(m.end(), coords); - results.push(SearchResult { - start_x, - start_y, - end_x, - end_y, - }); + // Allow for the regex to contain captures + for c in re.captures_iter(haystack) { + // Look for the captures in reverse order, as index==0 is + // the whole matched string. We can't just call + // `c.iter().rev()` as the capture iterator isn't double-ended. + for idx in (0..c.len()).rev() { + if let Some(m) = c.get(idx) { + let (start_x, start_y) = + haystack_idx_to_coord(m.start(), coords); + let (end_x, end_y) = haystack_idx_to_coord(m.end(), coords); + results.push(SearchResult { + start_x, + start_y, + end_x, + end_y, + }); + break; + } + } } } } diff --git a/wezterm-gui/src/overlay/quickselect.rs b/wezterm-gui/src/overlay/quickselect.rs index 0bb2aa6b4..e78a51ac3 100644 --- a/wezterm-gui/src/overlay/quickselect.rs +++ b/wezterm-gui/src/overlay/quickselect.rs @@ -1,6 +1,7 @@ use crate::selection::{SelectionCoordinate, SelectionRange}; use crate::termwindow::TermWindow; use config::keyassignment::{ClipboardCopyDestination, ScrollbackEraseMode}; +use config::ConfigHandle; use mux::domain::DomainId; use mux::pane::{Pane, PaneId, Pattern, SearchResult}; use mux::renderable::*; @@ -18,7 +19,6 @@ use wezterm_term::color::ColorPalette; use wezterm_term::{Clipboard, KeyCode, KeyModifiers, Line, MouseEvent, StableRowIndex}; use window::WindowOps; -// TODO: make this configurable const PATTERNS: [&str; 14] = [ // markdown_url r"\[[^]]*\]\(([^)]+)\)", @@ -49,7 +49,6 @@ const PATTERNS: [&str; 14] = [ // number r"[0-9]{4,}", ]; -const QWERTY_ALPHABET: &str = "asdfqwerzxcvjklmiuopghtybn"; /// This function computes a set of labels for a given alphabet. /// It is derived from https://github.com/fcsonline/tmux-thumbs/blob/master/src/alphabets.rs @@ -168,6 +167,8 @@ struct QuickSelectRenderable { /// We use this to cancel ourselves later window: ::window::Window, + + config: ConfigHandle, } impl QuickSelectOverlay { @@ -175,7 +176,25 @@ impl QuickSelectOverlay { let viewport = term_window.get_viewport(pane.pane_id()); let dims = pane.get_dimensions(); - let pattern = format!("({})", PATTERNS.join("|")); + let config = term_window.config.clone(); + + let mut pattern = "(".to_string(); + if !config.disable_default_quick_select_patterns { + for p in &PATTERNS { + if pattern.len() > 1 { + pattern.push('|'); + } + pattern.push_str(p); + } + } + for p in &config.quick_select_patterns { + if pattern.len() > 1 { + pattern.push('|'); + } + pattern.push_str(p); + } + pattern.push(')'); + let pattern = Pattern::Regex(pattern); let window = term_window.window.clone().unwrap(); @@ -193,6 +212,7 @@ impl QuickSelectOverlay { result_pos: None, width: dims.cols, height: dims.viewport_rows, + config, }; let search_row = renderer.compute_search_row(); @@ -487,7 +507,7 @@ impl QuickSelectRenderable { fn recompute_results(&mut self) { let num_results = self.results.len(); - let labels = compute_labels_for_alphabet(QWERTY_ALPHABET, num_results); + let labels = compute_labels_for_alphabet(&self.config.quick_select_alphabet, num_results); self.by_label.clear(); for ((result_index, res), label) in self