From ab6cda81aaa8e7ac17e21d47b28d228a99fb435b Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 10 Oct 2020 15:11:21 -0700 Subject: [PATCH] Add permute_any_mods and permute_any_or_no_mods functions refs: #288 --- config/src/keys.rs | 4 + config/src/lua.rs | 50 +++++ docs/config/lua/wezterm/permute_any_mods.md | 191 +++++++++++++++++ .../lua/wezterm/permute_any_or_no_mods.md | 200 ++++++++++++++++++ 4 files changed, 445 insertions(+) create mode 100644 docs/config/lua/wezterm/permute_any_mods.md create mode 100644 docs/config/lua/wezterm/permute_any_or_no_mods.md diff --git a/config/src/keys.rs b/config/src/keys.rs index ee61234ea..0b200644d 100644 --- a/config/src/keys.rs +++ b/config/src/keys.rs @@ -162,6 +162,10 @@ where let s = String::deserialize(deserializer)?; let mut mods = Modifiers::NONE; for ele in s.split('|') { + // Allow for whitespace; debug printing Modifiers includes spaces + // around the `|` so it is desirable to be able to reverse that + // encoding here. + let ele = ele.trim(); if ele == "SHIFT" { mods |= Modifiers::SHIFT; } else if ele == "ALT" || ele == "OPT" || ele == "META" { diff --git a/config/src/lua.rs b/config/src/lua.rs index b616a0e2d..12cc7515b 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -2,10 +2,12 @@ use crate::{FontAttributes, TextStyle}; use anyhow::anyhow; use bstr::BString; pub use luahelper::*; +use mlua::ToLua; use mlua::{Lua, Table, Value}; use serde::*; use smol::prelude::*; use std::path::Path; +use termwiz::input::Modifiers; /// Set up a lua context for executing some code. /// The path to the directory containing the configuration is @@ -95,6 +97,11 @@ pub fn make_lua_context(config_dir: &Path) -> anyhow::Result { )?; wezterm_mod.set("hostname", lua.create_function(hostname)?)?; wezterm_mod.set("action", lua.create_function(action)?)?; + wezterm_mod.set("permute_any_mods", lua.create_function(permute_any_mods)?)?; + wezterm_mod.set( + "permute_any_or_no_mods", + lua.create_function(permute_any_or_no_mods)?, + )?; wezterm_mod.set("read_dir", lua.create_async_function(read_dir)?)?; wezterm_mod.set("glob", lua.create_async_function(glob)?)?; @@ -406,6 +413,49 @@ async fn run_child_process<'lua>( )) } +fn permute_any_mods<'lua>( + lua: &'lua Lua, + item: mlua::Table, +) -> mlua::Result>> { + permute_mods(lua, item, false) +} + +fn permute_any_or_no_mods<'lua>( + lua: &'lua Lua, + item: mlua::Table, +) -> mlua::Result>> { + permute_mods(lua, item, true) +} + +fn permute_mods<'lua>( + lua: &'lua Lua, + item: mlua::Table, + allow_none: bool, +) -> mlua::Result>> { + let mut result = vec![]; + for ctrl in &[Modifiers::NONE, Modifiers::CTRL] { + for shift in &[Modifiers::NONE, Modifiers::SHIFT] { + for alt in &[Modifiers::NONE, Modifiers::ALT] { + for sup in &[Modifiers::NONE, Modifiers::SUPER] { + let flags = *ctrl | *shift | *alt | *sup; + if flags == Modifiers::NONE && !allow_none { + continue; + } + + let new_item = lua.create_table()?; + for pair in item.clone().pairs::() { + let (k, v) = pair?; + new_item.set(k, v)?; + } + new_item.set("mods", format!("{:?}", flags))?; + result.push(new_item.to_lua(lua)?); + } + } + } + } + Ok(result) +} + #[cfg(test)] mod test { use super::*; diff --git a/docs/config/lua/wezterm/permute_any_mods.md b/docs/config/lua/wezterm/permute_any_mods.md new file mode 100644 index 000000000..2c24fc87c --- /dev/null +++ b/docs/config/lua/wezterm/permute_any_mods.md @@ -0,0 +1,191 @@ +# `wezterm.permute_any_mods(table)` + +*Since: nightly builds only* + +This function is intended to help with generating key or mouse binding +entries that should apply regardless of the combination of modifier keys +pressed. + +For each combination of modifiers `CTRL`, `ALT`, `SHIFT` and `SUPER`, +the supplied table value is copied and has `mods = ` set into +the copy. + +An entry for `NONE` is *NOT* generated (this is the only +difference between `permute_any_mods` and `permute_any_or_no_mods`). + +An array holding all of those combinations is returned. + +The array can be unpacked into a lua table initializer and used like this: + +```lua +local wezterm = require 'wezterm'; + +return { + mouse_bindings = { + table.unpack(wezterm.permute_any_mods({ + event={Down={streak=1, button="Middle"}}, + action="PastePrimarySelection" + })) + } +} +``` + +This is equivalent to writing this out, but is much less verbose: + +```lua +return { + mouse_bindings = { + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "ALT", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "ALT | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | ALT", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | ALT | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "CTRL", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "CTRL | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "ALT | CTRL", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "ALT | CTRL | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | CTRL", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | CTRL | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | ALT | CTRL", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | ALT | CTRL | SUPER", + }, + } +} +``` + diff --git a/docs/config/lua/wezterm/permute_any_or_no_mods.md b/docs/config/lua/wezterm/permute_any_or_no_mods.md new file mode 100644 index 000000000..3bb9c1396 --- /dev/null +++ b/docs/config/lua/wezterm/permute_any_or_no_mods.md @@ -0,0 +1,200 @@ +# `wezterm.permute_any_or_no_mods(table)` + +*Since: nightly builds only* + +This function is intended to help with generating key or mouse binding +entries that should apply regardless of the combination of modifier keys +pressed. + +For each combination of modifiers `CTRL`, `ALT`, `SHIFT` and `SUPER`, +the supplied table value is copied and has `mods = ` set into +the copy. + +In addition, an entry for `NONE` *is* generated (this is the only +difference between `permute_any_mods` and `permute_any_or_no_mods`). + +An array holding all of those combinations is returned. + +The array can be unpacked into a lua table initializer and used like this: + +```lua +local wezterm = require 'wezterm'; + +return { + mouse_bindings = { + table.unpack(wezterm.permute_any_mods({ + event={Down={streak=1, button="Middle"}}, + action="PastePrimarySelection" + })) + } +} +``` + +This is equivalent to writing this out, but is much less verbose: + +```lua +return { + mouse_bindings = { + { + action= "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "NONE", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "ALT", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "ALT | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | ALT", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | ALT | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "CTRL", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "CTRL | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "ALT | CTRL", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "ALT | CTRL | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | CTRL", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | CTRL | SUPER", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | ALT | CTRL", + }, + { + action = "PastePrimarySelection", + event = { + Down = { + button = "Middle", + streak = 1, + }, + }, + mods = "SHIFT | ALT | CTRL | SUPER", + }, + } +} +```