diff --git a/config/src/lib.rs b/config/src/lib.rs index 4738ea935..5e0615c4a 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -700,6 +700,13 @@ impl ConfigHandle { pub fn generation(&self) -> usize { self.generation } + + pub fn default_config() -> Self { + Self { + config: Arc::new(Config::default_config()), + generation: 0, + } + } } impl std::ops::Deref for ConfigHandle { diff --git a/docs/changelog.md b/docs/changelog.md index 1d0f5a7b1..3d0669e37 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -11,6 +11,11 @@ usually the best available version. As features stabilize some brief notes about them will accumulate here. +#### New +* [wezterm.gui.default_key_tables](config/lua/wezterm.gui/default_key_tables.md) + and [wezterm.gui.default_keys](config/lua/wezterm.gui/default_keys.md) for + more conveniently copying and extending the default configuration. + #### Fixed * Wayland: key repeat gets stuck after pressing two keys in quick succession. Thanks to [@unrelentingtech](https://github.com/unrelentingtech)! diff --git a/docs/config/lua/wezterm.gui/default_key_tables.md b/docs/config/lua/wezterm.gui/default_key_tables.md new file mode 100644 index 000000000..55eb1a962 --- /dev/null +++ b/docs/config/lua/wezterm.gui/default_key_tables.md @@ -0,0 +1,32 @@ +# `wezterm.gui.default_key_tables()` + +*Since: nightly builds only* + +Returns a table holding the effective default set of `key_tables`. That is the +set of keys that is used as a base if there was no configuration file. + +This is useful in cases where you want to override a key table assignment +without replacing the entire set of key tables. + +This example shows how to add a key assignment for `Backspace` to `copy_mode`, +without having to manually specify the entire key table: + +```lua +local wezterm = require 'wezterm' +local act = wezterm.action + +local copy_mode = nil +if wezterm.gui then + copy_mode = wezterm.gui.default_key_tables().copy_mode + table.insert( + copy_mode, + { key = 'Backspace', mods = 'NONE', action = act.CopyMode 'MoveLeft' } + ) +end + +return { + key_tables = { + copy_mode = copy_mode, + }, +} +``` diff --git a/docs/config/lua/wezterm.gui/default_keys.md b/docs/config/lua/wezterm.gui/default_keys.md new file mode 100644 index 000000000..cb34b4054 --- /dev/null +++ b/docs/config/lua/wezterm.gui/default_keys.md @@ -0,0 +1,8 @@ +# `wezterm.gui.default_keys()` + +*Since: nightly builds only* + +Returns a table holding the effective default values for key assignments. That +is the set of keys that is used as a base if there was no configuration file. + + diff --git a/docs/copymode.md b/docs/copymode.md index 659e90269..715862c77 100644 --- a/docs/copymode.md +++ b/docs/copymode.md @@ -75,8 +75,14 @@ reassignable. The key assignments for copy mode are specified by the `copy_mode` [Key Table](config/key-tables.md). -You may provide your own definition of this key table if you wish to customize it. -There isn't a way to override portions of the key table, only to replace the entire table. +You may provide your own definition of this key table if you wish to customize +it. + +You may use +[wezterm.gui.default_key_tables](config/lua/wezterm.gui/default_key_tables.md) +to obtain the defaults and extend them. In earlier versions of wezterm there +wasn't a way to override portions of the key table, only to replace the entire +table. The default configuration at the time that these docs were built (which may be more recent than your version of wezterm) is shown below. diff --git a/docs/scrollback.md b/docs/scrollback.md index f7b968717..9e07d4bca 100644 --- a/docs/scrollback.md +++ b/docs/scrollback.md @@ -91,8 +91,11 @@ When the search overlay is active the behavior of wezterm changes: The key assignments for copy mode are specified by the `search_mode` [Key Table](config/key-tables.md). -You may provide your own definition of this key table if you wish to customize it. -There isn't a way to override portions of the key table, only to replace the entire table. +You may use +[wezterm.gui.default_key_tables](config/lua/wezterm.gui/default_key_tables.md) +to obtain the defaults and extend them. In earlier versions of wezterm there +wasn't a way to override portions of the key table, only to replace the entire +table. The default configuration at the time that these docs were built (which may be more recent than your version of wezterm) is shown below. diff --git a/wezterm-gui/src/inputmap.rs b/wezterm-gui/src/inputmap.rs index 0c22c8d57..ab06ef428 100644 --- a/wezterm-gui/src/inputmap.rs +++ b/wezterm-gui/src/inputmap.rs @@ -17,6 +17,11 @@ pub struct InputMap { } impl InputMap { + pub fn default_input_map() -> Self { + let config = ConfigHandle::default_config(); + Self::new(&config) + } + pub fn new(config: &ConfigHandle) -> Self { let mut mouse = config.mouse_bindings(); diff --git a/wezterm-gui/src/scripting/mod.rs b/wezterm-gui/src/scripting/mod.rs index 51e1341b1..29c7f21b0 100644 --- a/wezterm-gui/src/scripting/mod.rs +++ b/wezterm-gui/src/scripting/mod.rs @@ -1,7 +1,13 @@ use crate::frontend::try_front_end; +use crate::inputmap::InputMap; +use config::keyassignment::KeyTable; use config::lua::get_or_create_sub_module; use config::lua::mlua::{self, Lua}; +use config::{DeferredKeyCode, Key, KeyNoAction}; +use luahelper::dynamic_to_lua_value; use mux::window::WindowId as MuxWindowId; +use std::collections::HashMap; +use wezterm_dynamic::ToDynamic; pub mod guiwin; pub mod pane; @@ -28,5 +34,41 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> { })?, )?; + fn key_table_to_lua(table: &KeyTable) -> Vec { + let mut keys = vec![]; + for ((key, mods), entry) in table { + keys.push(Key { + key: KeyNoAction { + key: DeferredKeyCode::KeyCode(key.clone()), + mods: *mods, + }, + action: entry.action.clone(), + }); + } + keys + } + + window_mod.set( + "default_keys", + lua.create_function(|lua, _: ()| { + let map = InputMap::default_input_map(); + let keys = key_table_to_lua(&map.keys.default); + dynamic_to_lua_value(lua, keys.to_dynamic()) + })?, + )?; + + window_mod.set( + "default_key_tables", + lua.create_function(|lua, _: ()| { + let inputmap = InputMap::default_input_map(); + let mut tables: HashMap> = HashMap::new(); + for (k, table) in &inputmap.keys.by_name { + let keys = key_table_to_lua(table); + tables.insert(k.to_string(), keys); + } + dynamic_to_lua_value(lua, tables.to_dynamic()) + })?, + )?; + Ok(()) }