ztmux 3.7.20

A Rust port of tmux — the full terminal multiplexer, server and client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright (c) 2012 George Nachman <tmux@georgester.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
// OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use crate::*;
use crate::options_::*;

#[repr(C)]
pub struct notify_entry {
    pub name: *mut u8,
    pub fs: cmd_find_state,
    pub formats: *mut format_tree,

    pub client: *mut client,
    pub session: *mut session,
    pub window: *mut window,
    pub pane: i32,
    pub pbname: *mut u8,
}

/// C `vendor/tmux/notify.c:39`: `static struct cmdq_item *notify_insert_one_hook(struct cmdq_item *item, struct notify_entry *ne, struct cmd_list *cmdlist, struct cmdq_state *state)`
pub unsafe fn notify_insert_one_hook(
    item: *mut cmdq_item,
    ne: *mut notify_entry,
    cmdlist: *mut cmd_list,
    state: *mut cmdq_state,
) -> *mut cmdq_item {
    unsafe {
        if cmdlist.is_null() {
            return item;
        }
        if log_get_level() != 0 {
            let s = cmd_list_print(&*cmdlist, 0);
            log_debug!(
                "{}: hook {}: {}",
                "notify_insert_one_hook",
                _s((*ne).name),
                _s(s)
            );
            free_(s);
        }
        let new_item = cmdq_get_command(cmdlist, state);
        cmdq_insert_after(item, new_item)
    }
}

/// C `vendor/tmux/notify.c:57`: `static void notify_insert_hook(struct cmdq_item *item, struct notify_entry *ne)`
pub unsafe fn notify_insert_hook(mut item: *mut cmdq_item, ne: *mut notify_entry) {
    let __func__ = "notify_insert_hook";
    unsafe {
        log_debug!("{}: inserting hook {}", __func__, _s((*ne).name));

        let mut fs: cmd_find_state = zeroed();

        cmd_find_clear_state(&raw mut fs, cmd_find_flags::empty());
        if cmd_find_empty_state(&raw mut (*ne).fs) != 0 || !cmd_find_valid_state(&raw mut (*ne).fs)
        {
            cmd_find_from_nothing(&raw mut fs, cmd_find_flags::empty());
        } else {
            cmd_find_copy_state(&raw mut fs, &raw mut (*ne).fs);
        }

        let mut oo = if fs.s.is_null() {
            GLOBAL_S_OPTIONS
        } else {
            (*fs.s).options
        };
        let mut o = options_get(&mut *oo, cstr_to_str((*ne).name));
        if o.is_null() && !fs.wp.is_null() {
            oo = (*fs.wp).options;
            o = options_get(&mut *oo, cstr_to_str((*ne).name));
        }
        if o.is_null() && !fs.wl.is_null() {
            oo = (*(*fs.wl).window).options;
            o = options_get(&mut *oo, cstr_to_str((*ne).name));
        }
        if o.is_null() {
            log_debug!("{}: hook {} not found", __func__, _s((*ne).name));
            return;
        }

        let state = cmdq_new_state(
            &raw mut fs,
            null_mut(),
            cmdq_state_flags::CMDQ_STATE_NOHOOKS,
        );
        cmdq_add_formats(state, (*ne).formats);

        if *(*ne).name == b'@' {
            let value = options_get_string(oo, cstr_to_str((*ne).name));
            match cmd_parse_from_string(cstr_to_str(value), None) {
                Err(error) => {
                    log_debug!(
                        "{}: can't parse hook {}: {}",
                        __func__,
                        _s((*ne).name),
                        _s(error)
                    );
                    free_(error);
                }
                Ok(cmdlist) => {
                    notify_insert_one_hook(item, ne, cmdlist, state);
                }
            }
        } else {
            let mut a = options_array_first(o);
            while !a.is_null() {
                let cmdlist = (*options_array_item_value(a)).cmdlist;
                item = notify_insert_one_hook(item, ne, cmdlist, state);
                a = options_array_next(a);
            }
        }

        cmdq_free_state(state);
    }
}

// notify_callback
// notify_add

/// C `vendor/tmux/notify.c:123`: `static enum cmd_retval notify_callback(struct cmdq_item *item, void *data)`
pub unsafe fn notify_callback(item: *mut cmdq_item, data: *mut c_void) -> cmd_retval {
    let __func__ = c!("notify_callback");
    unsafe {
        let ne = data as *mut notify_entry;

        log_debug!("{}: {}", _s(__func__), _s((*ne).name));

        if streq_((*ne).name, "pane-mode-changed") {
            control_notify_pane_mode_changed((*ne).pane);
        }
        if streq_((*ne).name, "window-layout-changed") {
            control_notify_window_layout_changed((*ne).window);
        }
        if streq_((*ne).name, "window-pane-changed") {
            control_notify_window_pane_changed((*ne).window);
        }
        if streq_((*ne).name, "window-unlinked") {
            control_notify_window_unlinked((*ne).session, (*ne).window);
        }
        if streq_((*ne).name, "window-linked") {
            control_notify_window_linked((*ne).session, (*ne).window);
        }
        if streq_((*ne).name, "window-renamed") {
            control_notify_window_renamed((*ne).window);
        }
        if streq_((*ne).name, "client-session-changed") {
            control_notify_client_session_changed((*ne).client);
        }
        if streq_((*ne).name, "client-detached") {
            control_notify_client_detached((*ne).client);
        }
        if streq_((*ne).name, "session-renamed") {
            control_notify_session_renamed((*ne).session);
        }
        if streq_((*ne).name, "session-created") {
            control_notify_session_created((*ne).session);
        }
        if streq_((*ne).name, "session-closed") {
            control_notify_session_closed((*ne).session);
        }
        if streq_((*ne).name, "session-window-changed") {
            control_notify_session_window_changed((*ne).session);
        }
        if streq_((*ne).name, "paste-buffer-changed") {
            control_notify_paste_buffer_changed((*ne).pbname);
        }
        if streq_((*ne).name, "paste-buffer-deleted") {
            control_notify_paste_buffer_deleted((*ne).pbname);
        }

        notify_insert_hook(item, ne);

        if !(*ne).client.is_null() {
            server_client_unref((*ne).client);
        }
        if !(*ne).session.is_null() {
            session_remove_ref((*ne).session, __func__);
        }
        if !(*ne).window.is_null() {
            window_remove_ref((*ne).window, __func__);
        }

        if !(*ne).fs.s.is_null() {
            session_remove_ref((*ne).fs.s, __func__);
        }

        format_free((*ne).formats);
        free_((*ne).name);
        free_((*ne).pbname);
        free_(ne);
    }

    cmd_retval::CMD_RETURN_NORMAL
}

/// C `vendor/tmux/notify.c:179`: `static void notify_add(const char *name, struct cmd_find_state *fs, struct client *c, struct session *s, struct window *w, struct window_pane *wp, const char *pbname)`
pub unsafe fn notify_add(
    name: &'static CStr,
    fs: *mut cmd_find_state,
    c: *mut client,
    s: *mut session,
    w: *mut window,
    wp: *mut window_pane,
    pbname: Option<&str>,
) {
    let __func__ = c!("notify_add");
    unsafe {
        let item = cmdq_running(null_mut());
        if !item.is_null() && cmdq_get_flags(item).intersects(cmdq_state_flags::CMDQ_STATE_NOHOOKS)
        {
            return;
        }

        let ne = xcalloc1::<notify_entry>() as *mut notify_entry;
        (*ne).name = xstrdup(name.as_ptr().cast()).as_ptr();

        (*ne).client = c;
        (*ne).session = s;
        (*ne).window = w;
        (*ne).pane = if !wp.is_null() { (*wp).id as i32 } else { -1 };
        (*ne).pbname = if let Some(pbname) = pbname {
            xstrdup__(pbname)
        } else {
            null_mut()
        };

        (*ne).formats = format_create(null_mut(), null_mut(), 0, format_flags::FORMAT_NOJOBS);
        format_add!((*ne).formats, "hook", "{}", _s(name.as_ptr()));
        if !c.is_null() {
            format_add!((*ne).formats, "hook_client", "{}", _s((*c).name));
        }
        if !s.is_null() {
            format_add!((*ne).formats, "hook_session", "${}", (*s).id);
            format_add!((*ne).formats, "hook_session_name", "{}", (*s).name);
        }
        if !w.is_null() {
            format_add!((*ne).formats, "hook_window", "@{}", (*w).id,);
            format_add!((*ne).formats, "hook_window_name", "{}", _s((*w).name_ptr()));
        }
        if !wp.is_null() {
            format_add!((*ne).formats, "hook_pane", "%%{}", (*wp).id);
        }
        format_log_debug((*ne).formats, __func__);

        if !c.is_null() {
            (*c).references += 1;
        }
        if !s.is_null() {
            session_add_ref(s, __func__);
        }
        if !w.is_null() {
            window_add_ref(w, __func__);
        }

        cmd_find_copy_state(&raw mut (*ne).fs, fs);
        if !(*ne).fs.s.is_null() {
            session_add_ref((*ne).fs.s, __func__);
        } /* cmd_find_valid_state needs session */

        cmdq_append(
            null_mut(),
            cmdq_get_callback!(notify_callback, ne.cast()).as_ptr(),
        );
    }
}

/// C `vendor/tmux/notify.c:234`: `void notify_hook(struct cmdq_item *item, const char *name)`
pub unsafe fn notify_hook(item: *mut cmdq_item, name: *mut u8) {
    let __func__ = c!("notify_hook");
    unsafe {
        let target = cmdq_get_target(item);
        let mut ne: notify_entry = zeroed();

        ne.name = name;
        cmd_find_copy_state(&raw mut ne.fs, target);

        ne.client = cmdq_get_client(item);
        ne.session = (*target).s;
        ne.window = (*target).w;
        ne.pane = if !(*target).wp.is_null() {
            (*(*target).wp).id as i32
        } else {
            -1
        };

        ne.formats = format_create(null_mut(), null_mut(), 0, format_flags::FORMAT_NOJOBS);
        format_add!(ne.formats, "hook", "{}", _s(name));
        format_log_debug(ne.formats, __func__);

        notify_insert_hook(item, &raw mut ne);
        format_free(ne.formats);
    }
}

/// C `vendor/tmux/notify.c:258`: `void notify_client(const char *name, struct client *c)`
pub unsafe fn notify_client(name: &'static CStr, c: *mut client) {
    unsafe {
        let mut fs: cmd_find_state = zeroed(); // TODO use uninit

        cmd_find_from_client(&raw mut fs, c, cmd_find_flags::empty());
        notify_add(
            name,
            &raw mut fs,
            c,
            null_mut(),
            null_mut(),
            null_mut(),
            None,
        );
    }
}

/// C `vendor/tmux/notify.c:267`: `void notify_session(const char *name, struct session *s)`
pub unsafe fn notify_session(name: &'static CStr, s: *mut session) {
    unsafe {
        let mut fs = zeroed(); // TODO use uninit

        if session_alive(s) {
            cmd_find_from_session(&raw mut fs, s, cmd_find_flags::empty());
        } else {
            cmd_find_from_nothing(&raw mut fs, cmd_find_flags::empty());
        }
        notify_add(
            name,
            &raw mut fs,
            null_mut(),
            s,
            null_mut(),
            null_mut(),
            None,
        );
    }
}

/// C `vendor/tmux/notify.c:279`: `void notify_winlink(const char *name, struct winlink *wl)`
pub unsafe fn notify_winlink(name: &'static CStr, wl: *mut winlink) {
    unsafe {
        let mut fs: cmd_find_state = zeroed();

        cmd_find_from_winlink(&raw mut fs, wl, cmd_find_flags::empty());
        notify_add(
            name,
            &raw mut fs,
            null_mut(),
            (*wl).session,
            (*wl).window,
            null_mut(),
            None,
        );
    }
}

/// C `vendor/tmux/notify.c:288`: `void notify_session_window(const char *name, struct session *s, struct window *w)`
pub unsafe fn notify_session_window(name: &'static CStr, s: *mut session, w: *mut window) {
    unsafe {
        let mut fs: cmd_find_state = zeroed();

        cmd_find_from_session_window(&raw mut fs, s, w, cmd_find_flags::empty());
        notify_add(name, &raw mut fs, null_mut(), s, w, null_mut(), None);
    }
}

/// C `vendor/tmux/notify.c:297`: `void notify_window(const char *name, struct window *w)`
pub unsafe fn notify_window(name: &'static CStr, w: *mut window) {
    unsafe {
        let mut fs: cmd_find_state = zeroed();

        cmd_find_from_window(&raw mut fs, w, cmd_find_flags::empty());
        notify_add(
            name,
            &raw mut fs,
            null_mut(),
            null_mut(),
            w,
            null_mut(),
            None,
        );
    }
}

/// C `vendor/tmux/notify.c:306`: `void notify_pane(const char *name, struct window_pane *wp)`
pub unsafe fn notify_pane(name: &'static CStr, wp: *mut window_pane) {
    unsafe {
        let mut fs: cmd_find_state = zeroed();

        cmd_find_from_pane(&raw mut fs, wp, cmd_find_flags::empty());
        notify_add(
            name,
            &raw mut fs,
            null_mut(),
            null_mut(),
            null_mut(),
            wp,
            None,
        );
    }
}

/// C `vendor/tmux/notify.c:315`: `void notify_paste_buffer(const char *pbname, int deleted)`
pub unsafe fn notify_paste_buffer(pbname: &str, deleted: bool) {
    unsafe {
        let mut fs: cmd_find_state = zeroed();

        cmd_find_clear_state(&raw mut fs, cmd_find_flags::empty());
        if deleted {
            notify_add(
                c"paste-buffer-deleted",
                &raw mut fs,
                null_mut(),
                null_mut(),
                null_mut(),
                null_mut(),
                Some(pbname),
            );
        } else {
            notify_add(
                c"paste-buffer-changed",
                &raw mut fs,
                null_mut(),
                null_mut(),
                null_mut(),
                null_mut(),
                Some(pbname),
            );
        }
    }
}