diff --git a/config/src/lua.rs b/config/src/lua.rs index 9ba4f3ab2..9c73f5114 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -735,6 +735,14 @@ pub fn register_event<'lua>( } } +const IS_EVENT: &str = "wezterm-is-event-emission"; + +/// Returns true if the current lua context is being called as part +/// of an emit_event call. +pub fn is_event_emission<'lua>(lua: &'lua Lua) -> mlua::Result { + lua.named_registry_value(IS_EVENT) +} + /// This implements `wezterm.emit`. /// The first parameter to emit is the name of a signal that may or may not /// have previously been registered via `wezterm.on`. @@ -751,6 +759,8 @@ pub async fn emit_event<'lua>( lua: &'lua Lua, (name, args): (String, mlua::MultiValue<'lua>), ) -> mlua::Result { + lua.set_named_registry_value(IS_EVENT, true)?; + let decorated_name = format!("wezterm-event-{}", name); let tbl: mlua::Value = lua.named_registry_value(&decorated_name)?; match tbl { diff --git a/docs/changelog.md b/docs/changelog.md index a3e7f28cc..ad9c8b76b 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -147,6 +147,9 @@ As features stabilize some brief notes about them will accumulate here. [#3263](https://github.com/wez/wezterm/issues/3263) * Improved compatiblity with the Kitty Image Protocol [#2716](https://github.com/wez/wezterm/issues/2716) +* [wezterm.time.call_after](config/lua/wezterm.time/call_after.md) would not + work when used in an event callback. + [#3026](https://github.com/wez/wezterm/issues/3026) #### Changed * `CTRL-SHIFT-P` now activates the new [command diff --git a/lua-api-crates/time-funcs/src/lib.rs b/lua-api-crates/time-funcs/src/lib.rs index 90011a8f7..deda28089 100644 --- a/lua-api-crates/time-funcs/src/lib.rs +++ b/lua-api-crates/time-funcs/src/lib.rs @@ -1,6 +1,8 @@ use chrono::prelude::*; use config::lua::mlua::{self, Lua, MetaMethod, UserData, UserDataMethods}; -use config::lua::{emit_event, get_or_create_module, get_or_create_sub_module, wrap_callback}; +use config::lua::{ + emit_event, get_or_create_module, get_or_create_sub_module, is_event_emission, wrap_callback, +}; use config::ConfigSubscription; use std::rc::Rc; use std::sync::Mutex; @@ -16,6 +18,7 @@ lazy_static::lazy_static! { fn schedule_all(lua: Option>) -> mlua::Result<()> { if let Some(lua) = lua { let scheduled_events: Vec = lua.named_registry_value(SCHEDULED_EVENTS)?; + lua.set_named_registry_value(SCHEDULED_EVENTS, Vec::::new())?; let generation = config::configuration().generation(); for event in scheduled_events { event.schedule(generation); @@ -147,13 +150,21 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> { "call_after", lua.create_function(|lua, (interval_seconds, func): (u64, mlua::Function)| { let user_event_id = wrap_callback(lua, func)?; - let mut scheduled_events: Vec = - lua.named_registry_value(SCHEDULED_EVENTS)?; - scheduled_events.push(ScheduledEvent { + + let event = ScheduledEvent { user_event_id, interval_seconds, - }); - lua.set_named_registry_value(SCHEDULED_EVENTS, scheduled_events)?; + }; + + if is_event_emission(lua)? { + let generation = config::configuration().generation(); + event.schedule(generation); + } else { + let mut scheduled_events: Vec = + lua.named_registry_value(SCHEDULED_EVENTS)?; + scheduled_events.push(event); + lua.set_named_registry_value(SCHEDULED_EVENTS, scheduled_events)?; + } Ok(()) })?, )?;