macro_rules! notification_plugin {
(|$arg:ident| $body:block) => { ... };
($name:expr, |$arg:ident| $body:block) => { ... };
}
Expand description
Defines a new notification callback.
This macro has two variants: If passed just a callback function, that function
is registered as a callback for all hook events. If passed a null-terminated hook name
and a callback function, that function will be registered as a callback for that specific
hook event. The callback function itself will in either case recive an argument of type
*mut
notify_entry
.
For example:
use tmux_plugin::notification_plugin;
use std::ffi::{CStr, CString};
// Enforce that window names are lower case.
notification_plugin!(b"window-renamed\0", |notify_entry| {
let window = unsafe { (*notify_entry).window };
let window_name = unsafe { CStr::from_ptr((*window).name) };
let lowercase_name = window_name
.to_string_lossy()
.into_owned()
.to_lowercase();
let c_string = CString::new(lowercase_name)
.expect("Does not contain null bytes, as the source was a valid C str");
unsafe {
// Free the old name, and duplicate the new name so
// that tmux can later free it safely.
libc::free((*window).name as *mut _);
(*window).name = libc::strdup(c_string.as_ptr())
}
});