From 6d007712bd5cecc6a28db1428aed64426a1680b3 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 22 May 2020 08:05:59 -0700 Subject: [PATCH] wezterm: enable mouse binding assignments This commit caps hooks up the mouse event plumbing to the config system and enables mouse button event bindings. closes: https://github.com/wez/wezterm/issues/119 --- docs/SUMMARY.md | 2 +- docs/config/keys.markdown | 106 +++++++++++++++++++++++++++++++++++++- src/config/keys.rs | 11 +++- src/config/mod.rs | 17 +++++- src/keyassignment.rs | 7 ++- 5 files changed, 136 insertions(+), 7 deletions(-) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 2b636a4d5..2414dc20e 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -7,7 +7,7 @@ - [Fonts](config/fonts.markdown) - [Font Shaping](config/font-shaping.markdown) - [Misc](config/misc.markdown) - - [Key Binding](config/keys.markdown) + - [Key & Mouse Binding](config/keys.markdown) - [Colors & Appearance](config/appearance.markdown) - [Lua Reference](config/lua.markdown) - [Hyperlinks](hyperlinks.markdown) diff --git a/docs/config/keys.markdown b/docs/config/keys.markdown index 2b06cc9a4..1a99a25f0 100644 --- a/docs/config/keys.markdown +++ b/docs/config/keys.markdown @@ -1,4 +1,14 @@ -### Shortcut / Key Binding Assignments +Both the keyboard and (starting with the most recently *nightly* builds) the +mouse bindings are configurable. + +The assignments are based around a triggering event, such as a keypress or +mouse button click, which is combined with a set of modifier keys to produce +an action. + +A full list of possible keys, mouse events and actions are included below, +after these tables describing the default assignments. + +### Default Shortcut / Key Binding Assignments The default key bindings are: @@ -48,6 +58,98 @@ The default key bindings are: | `CTRL+SHIFT` | `PAGEDOWN` | `MoveTabRelative=1` | | `SHIFT` | `PAGEUP` | `ScrollByPage=-1` | | `SHIFT` | `PAGEDOWN` | `ScrollByPage=1` | +| `ALT` | `9` | `ShowTabNavigator` | +| `SUPER` | `r` | `ReloadConfiguration` | +| `CTRL+SHIFT` | `R` | `ReloadConfiguration` | +| `SUPER` | `h` | `HideApplication` (macOS only) | + +## Default Mouse Assignments + +In the table below, `Triple Left Down` means that the left mouse button is +being triple clicked and that the event matches the downstroke of the third +quick consecutive press. `Triple Left Up` matches the subsequent release event +of that triple click, so for a triple click both +`SelectTextAtMouseCursor="Line"` and `CompleteSelection` will be triggered in +that order. + +| Event | Modifiers | Action | +| --------- | --- | ------ | +| Triple Left Down | `NONE` | `SelectTextAtMouseCursor="Line"` | +| Double Left Down | `NONE` | `SelectTextAtMouseCursor="Word"` | +| Single Left Down | `NONE` | `SelectTextAtMouseCursor="Cell"` | +| Single Left Down | `SHIFT` | `ExtendSelectionToMouseCursor=nil` | +| Single Left Up | `NONE` | `CompleteSelectionOrOpenLinkAtMouseCursor` | +| Double Left Up | `NONE` | `CompleteSelection` | +| Triple Left Up | `NONE` | `CompleteSelection` | +| Single Left Drag | `NONE` | `ExtendSelectionToMouseCursor="Cell"` | +| Double Left Drag | `NONE` | `ExtendSelectionToMouseCursor="Word"` | +| Triple Left Drag | `NONE` | `ExtendSelectionToMouseCursor="Line"` | +| Single Middle Down | `NONE` | `Paste` | + +## Configuring Mouse Assignments + +*available in the most recent nightly* + +You can define mouse actions using the `mouse_bindings` configuration section: + +```lua +local wezterm = require 'wezterm'; + +return { + mouse_bindings = { + -- Right click sends "woot" to the terminal + { + event={Down={streak=1, button="Right"}}, + mods="NONE", + action=wezterm.action{SendString="woot"} + }, + + -- Change the default click behavior so that it only selects + -- text and doesn't open hyperlinks + { + event={Up={streak=1, button="Left"}}, + mods="NONE", + action="CompleteSelection", + }, + + -- and make CTRL-Click open hyperlinks + { + event={Up={streak=1, button="Left"}}, + mods="CTRL", + action="OpenLinkAtMouseCursor", + }, + }, +} +``` + +The `action` and `mods` portions are described in more detail in the key assignment +information below. + +The `event` portion has three components; + +* Whether it is a `Down`, `Up` or `Drag` event +* The number of consecutive clicks within the click threshold (the *click streak*) +* The mouse button; `Left`, `Right`, or `Middle`. + +A double click is a `down-up-down` sequence where either the second button down +is held for long enough or is released and no subsequent down event occurs +within the click threshold. When recognized, it emits a `Down` event with +`streak=2`. If the mouse is moved while the button is held, a `Drag` event +with `streak=2` is generated. When the mouse button is released an `Up` event +with `streak=2` is generated. + +The mouse event recognizer supports an arbitrary click streak, so if +you wanted quadruple-click bindings you can specify `streak=4`. + +| Event | Lua Representation | +| ----------------- | ------------------- | +| Triple Left Down | `event={Down={streak=3, button="Left"}}` | +| Double Left Up | `event={Up={streak=2, button="Left"}}` | +| Single Left Drag | `event={Drag={streak=1, button="Left"}}` | + + +## Configuring Key Assignments + These can be overridden using the `keys` section in your `~/.wezterm.lua` config file. For example, you can disable a default assignment like this: @@ -495,7 +597,7 @@ a hyperlink, this action causes that link to be opened. ## CompleteSelection Completes an active text selection process; the selection range is -marked closed and then the selected text is copied as thought the +marked closed and then the selected text is copied as though the `Copy` action was executed. ## CompleteSelectionOrOpenLinkAtMouseCursor diff --git a/src/config/keys.rs b/src/config/keys.rs index 9c5e8b73f..4daf54fb0 100644 --- a/src/config/keys.rs +++ b/src/config/keys.rs @@ -1,4 +1,4 @@ -use crate::keyassignment::KeyAssignment; +use crate::keyassignment::{KeyAssignment, MouseEventTrigger}; use serde::{Deserialize, Deserializer, Serialize}; use termwiz::input::{KeyCode, Modifiers}; @@ -12,6 +12,15 @@ pub struct Key { } impl_lua_conversion!(Key); +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct Mouse { + pub event: MouseEventTrigger, + #[serde(deserialize_with = "de_modifiers", default)] + pub mods: Modifiers, + pub action: KeyAssignment, +} +impl_lua_conversion!(Mouse); + fn de_keycode<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, diff --git a/src/config/mod.rs b/src/config/mod.rs index 61aa71bec..8e4b5e542 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -5,7 +5,7 @@ use crate::font::locator::FontLocatorSelection; use crate::font::rasterizer::FontRasterizerSelection; use crate::font::shaper::FontShaperSelection; use crate::frontend::FrontEndSelection; -use crate::keyassignment::{KeyAssignment, SpawnCommand}; +use crate::keyassignment::{KeyAssignment, MouseEventTrigger, SpawnCommand}; use anyhow::{anyhow, bail, Context, Error}; use lazy_static::lazy_static; use portable_pty::{CommandBuilder, PtySize}; @@ -417,6 +417,9 @@ pub struct Config { #[serde(default)] pub keys: Vec, + #[serde(default)] + pub mouse_bindings: Vec, + #[serde(default)] pub daemon_options: DaemonOptions, @@ -657,6 +660,18 @@ impl Config { Ok(map) } + pub fn mouse_bindings( + &self, + ) -> anyhow::Result> { + let mut map = HashMap::new(); + + for m in &self.mouse_bindings { + map.insert((m.event.clone(), m.mods), m.action.clone()); + } + + Ok(map) + } + /// In some cases we need to compute expanded values based /// on those provided by the user. This is where we do that. fn compute_extra_defaults(&self, config_path: Option<&Path>) -> Self { diff --git a/src/keyassignment.rs b/src/keyassignment.rs index 7ffe91130..3e96d8176 100644 --- a/src/keyassignment.rs +++ b/src/keyassignment.rs @@ -113,9 +113,12 @@ pub struct InputMap { impl InputMap { pub fn new() -> Self { - let mut mouse = HashMap::new(); + let config = configuration(); + let mut mouse = config + .mouse_bindings() + .expect("mouse_bindings section of the config to be valid"); - let mut keys = configuration() + let mut keys = config .key_bindings() .expect("keys section of config to be valid");