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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Copyright (c) 2026 Dane Jensen <dhcjensen@gmail.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.
//
// Faithful port of vendor/tmux/sort.c. The C file caches its results in a
// `static` reallocated array and returns the count through a `u_int *n`
// out-parameter; the Rust collectors return an owned `Vec<*mut T>` instead
// (its `.len()` is the count), which is equivalent for every caller.
//
// The C comparators read the criteria from a file-static `sort_criteria`
// pointer so they can be passed to `qsort`. The Rust ports take the criteria
// by value and are driven through `Vec::sort_by`, so no shared static is
// needed.
use crate::*;

/// C `vendor/tmux/tmux.h:2469`: `enum sort_order`.
#[derive(Clone, Copy, PartialEq)]
pub enum sort_order {
    SORT_ACTIVITY,
    SORT_CREATION,
    SORT_INDEX,
    SORT_MODIFIER,
    SORT_NAME,
    SORT_ORDER,
    SORT_SIZE,
    SORT_Z,
    SORT_END,
}

/// C `vendor/tmux/tmux.h:2482`: `struct sort_criteria`.
#[derive(Clone, Copy)]
pub struct sort_criteria {
    pub order: sort_order,
    pub reversed: bool,
    /// C: `enum sort_order *order_seq` — the null(`SORT_END`)-terminated list of
    /// available orders cycled through by `sort_next_order`. `null` when the
    /// caller does not cycle (e.g. the format `#{S:} #{W:} #{P:} #{L:}` loops).
    pub order_seq: *mut sort_order,
}

/// C `vendor/tmux/sort.c:28`: `static void sort_qsort(void *l, u_int len, u_int size, int (*cmp)(const void *, const void *), struct sort_criteria *sort_crit)`
unsafe fn sort_qsort<T>(
    l: &mut [*mut T],
    cmp: unsafe fn(*mut T, *mut T, sort_criteria) -> i32,
    sc: sort_criteria,
) {
    match sc.order {
        sort_order::SORT_END => (),
        sort_order::SORT_ORDER => {
            if sc.reversed {
                l.reverse();
            }
        }
        _ => l.sort_by(|&a, &b| unsafe { cmp(a, b, sc) }.cmp(&0)),
    }
}

/// C `vendor/tmux/sort.c:53`: `static int sort_buffer_cmp(const void *a0, const void *b0)`
unsafe fn sort_buffer_cmp(a: *mut paste_buffer, b: *mut paste_buffer, sc: sort_criteria) -> i32 {
    unsafe {
        let na = (*a).name.as_ref();
        let nb = (*b).name.as_ref();
        let ncmp = na.cmp(nb) as i32;
        let mut result = match sc.order {
            sort_order::SORT_NAME => ncmp,
            sort_order::SORT_CREATION => (*b).order.cmp(&(*a).order) as i32,
            sort_order::SORT_SIZE => (*a).size.wrapping_sub((*b).size) as i32,
            _ => 0,
        };
        if result == 0 {
            result = ncmp;
        }
        if sc.reversed {
            result = -result;
        }
        result
    }
}

/// C `vendor/tmux/sort.c:95`: `static int sort_client_cmp(const void *a0, const void *b0)`
unsafe fn sort_client_cmp(a: *mut client, b: *mut client, sc: sort_criteria) -> i32 {
    unsafe {
        let ncmp = strcmp((*a).name, (*b).name);
        let mut result = match sc.order {
            sort_order::SORT_NAME => ncmp,
            sort_order::SORT_SIZE => {
                let mut r = (*a).tty.sx.wrapping_sub((*b).tty.sx) as i32;
                if r == 0 {
                    r = (*a).tty.sy.wrapping_sub((*b).tty.sy) as i32;
                }
                r
            }
            sort_order::SORT_CREATION => {
                let at = ((*a).creation_time.tv_sec, (*a).creation_time.tv_usec);
                let bt = ((*b).creation_time.tv_sec, (*b).creation_time.tv_usec);
                at.cmp(&bt) as i32
            }
            sort_order::SORT_ACTIVITY => {
                let at = ((*a).activity_time.tv_sec, (*a).activity_time.tv_usec);
                let bt = ((*b).activity_time.tv_sec, (*b).activity_time.tv_usec);
                bt.cmp(&at) as i32
            }
            _ => 0,
        };
        if result == 0 {
            result = ncmp;
        }
        if sc.reversed {
            result = -result;
        }
        result
    }
}

/// C `vendor/tmux/sort.c:142`: `static int sort_session_cmp(const void *a0, const void *b0)`
unsafe fn sort_session_cmp(a: *mut session, b: *mut session, sc: sort_criteria) -> i32 {
    unsafe {
        let na = (*a).name.as_ref();
        let nb = (*b).name.as_ref();
        let ncmp = na.cmp(nb) as i32;
        let mut result = match sc.order {
            sort_order::SORT_INDEX => (*a).id.wrapping_sub((*b).id) as i32,
            sort_order::SORT_CREATION => {
                let at = ((*a).creation_time.tv_sec, (*a).creation_time.tv_usec);
                let bt = ((*b).creation_time.tv_sec, (*b).creation_time.tv_usec);
                at.cmp(&bt) as i32
            }
            sort_order::SORT_ACTIVITY => {
                let at = ((*a).activity_time.tv_sec, (*a).activity_time.tv_usec);
                let bt = ((*b).activity_time.tv_sec, (*b).activity_time.tv_usec);
                bt.cmp(&at) as i32
            }
            sort_order::SORT_NAME => ncmp,
            _ => 0,
        };
        if result == 0 {
            result = ncmp;
        }
        if sc.reversed {
            result = -result;
        }
        result
    }
}

/// C `vendor/tmux/sort.c:195`: `static int sort_pane_cmp(const void *a0, const void *b0)`
unsafe fn sort_pane_cmp(a: *mut window_pane, b: *mut window_pane, sc: sort_criteria) -> i32 {
    unsafe {
        let title_cmp = || strcmp((*(*a).screen).title_ptr(), (*(*b).screen).title_ptr());
        let mut result = match sc.order {
            sort_order::SORT_ACTIVITY => (*a).active_point.wrapping_sub((*b).active_point) as i32,
            sort_order::SORT_CREATION => (*a).id.wrapping_sub((*b).id) as i32,
            sort_order::SORT_SIZE => {
                (*a).sx
                    .wrapping_mul((*a).sy)
                    .wrapping_sub((*b).sx.wrapping_mul((*b).sy)) as i32
            }
            sort_order::SORT_INDEX => {
                let mut ai = 0;
                let mut bi = 0;
                window_pane_index(a, &raw mut ai);
                window_pane_index(b, &raw mut bi);
                ai.wrapping_sub(bi) as i32
            }
            sort_order::SORT_NAME => title_cmp(),
            sort_order::SORT_Z => {
                let mut ai = 0;
                let mut bi = 0;
                window_pane_zindex(a, &raw mut ai);
                window_pane_zindex(b, &raw mut bi);
                ai.wrapping_sub(bi) as i32
            }
            _ => 0,
        };
        if result == 0 {
            result = title_cmp();
        }
        if sc.reversed {
            result = -result;
        }
        result
    }
}

/// C `vendor/tmux/sort.c:241`: `static int sort_winlink_cmp(const void *a0, const void *b0)`
unsafe fn sort_winlink_cmp(a: *mut winlink, b: *mut winlink, sc: sort_criteria) -> i32 {
    unsafe {
        let wa = (*a).window;
        let wb = (*b).window;
        let ncmp = strcmp((*wa).name_ptr(), (*wb).name_ptr());
        let mut result = match sc.order {
            sort_order::SORT_INDEX => (*a).idx.wrapping_sub((*b).idx),
            sort_order::SORT_CREATION => {
                let at = ((*wa).creation_time.tv_sec, (*wa).creation_time.tv_usec);
                let bt = ((*wb).creation_time.tv_sec, (*wb).creation_time.tv_usec);
                at.cmp(&bt) as i32
            }
            sort_order::SORT_ACTIVITY => {
                let at = ((*wa).activity_time.tv_sec, (*wa).activity_time.tv_usec);
                let bt = ((*wb).activity_time.tv_sec, (*wb).activity_time.tv_usec);
                bt.cmp(&at) as i32
            }
            sort_order::SORT_NAME => ncmp,
            sort_order::SORT_SIZE => (*wa)
                .sx
                .wrapping_mul((*wa).sy)
                .wrapping_sub((*wb).sx.wrapping_mul((*wb).sy))
                as i32,
            _ => 0,
        };
        if result == 0 {
            result = ncmp;
        }
        if sc.reversed {
            result = -result;
        }
        result
    }
}

#[expect(
    dead_code,
    reason = "unified sort path (cmd-list-*/mode-tree), not yet wired"
)]
/// C `vendor/tmux/sort.c:334`: `void sort_next_order(struct sort_criteria *sort_crit)`
pub unsafe fn sort_next_order(sc: *mut sort_criteria) {
    unsafe {
        let seq = (*sc).order_seq;
        if seq.is_null() {
            return;
        }
        let mut i = 0usize;
        while *seq.add(i) != sort_order::SORT_END {
            if (*sc).order == *seq.add(i) {
                break;
            }
            i += 1;
        }

        if *seq.add(i) == sort_order::SORT_END {
            i = 0;
        } else {
            i += 1;
            if *seq.add(i) == sort_order::SORT_END {
                i = 0;
            }
        }
        (*sc).order = *seq.add(i);
    }
}

/// C `vendor/tmux/sort.c:356`: `enum sort_order sort_order_from_string(const char* order)`
pub unsafe fn sort_order_from_string(order: *const u8) -> sort_order {
    unsafe {
        if !order.is_null() {
            if strcaseeq_(order, "activity") {
                return sort_order::SORT_ACTIVITY;
            }
            if strcaseeq_(order, "creation") {
                return sort_order::SORT_CREATION;
            }
            if strcaseeq_(order, "index") || strcaseeq_(order, "key") {
                return sort_order::SORT_INDEX;
            }
            if strcaseeq_(order, "modifier") {
                return sort_order::SORT_MODIFIER;
            }
            if strcaseeq_(order, "name") || strcaseeq_(order, "title") {
                return sort_order::SORT_NAME;
            }
            if strcaseeq_(order, "order") {
                return sort_order::SORT_ORDER;
            }
            if strcaseeq_(order, "size") {
                return sort_order::SORT_SIZE;
            }
            if strcaseeq_(order, "z") {
                return sort_order::SORT_Z;
            }
        }
        sort_order::SORT_END
    }
}

#[expect(
    dead_code,
    reason = "unified sort path (cmd-list-*/mode-tree), not yet wired"
)]
/// C `vendor/tmux/sort.c:382`: `const char *sort_order_to_string(enum sort_order order)`
pub fn sort_order_to_string(order: sort_order) -> *const u8 {
    match order {
        sort_order::SORT_ACTIVITY => c!("activity"),
        sort_order::SORT_CREATION => c!("creation"),
        sort_order::SORT_INDEX => c!("index"),
        sort_order::SORT_MODIFIER => c!("modifier"),
        sort_order::SORT_NAME => c!("name"),
        sort_order::SORT_ORDER => c!("order"),
        sort_order::SORT_SIZE => c!("size"),
        sort_order::SORT_Z => c!("z"),
        sort_order::SORT_END => null(),
    }
}

#[expect(
    dead_code,
    reason = "unified sort path (cmd-list-*/mode-tree), not yet wired"
)]
/// C `vendor/tmux/sort.c:404`: `int sort_would_window_tree_swap(struct sort_criteria *sort_crit, struct winlink *wla, struct winlink *wlb)`
pub unsafe fn sort_would_window_tree_swap(
    sc: sort_criteria,
    wla: *mut winlink,
    wlb: *mut winlink,
) -> i32 {
    unsafe {
        if sc.order == sort_order::SORT_INDEX {
            return 0;
        }
        i32::from(sort_winlink_cmp(wla, wlb, sc) != 0)
    }
}

#[expect(
    dead_code,
    reason = "unified sort path (cmd-list-*/mode-tree), not yet wired"
)]
/// C `vendor/tmux/sort.c:414`: `struct paste_buffer **sort_get_buffers(u_int *n, struct sort_criteria *sort_crit)`
pub unsafe fn sort_get_buffers(sc: sort_criteria) -> Vec<*mut paste_buffer> {
    unsafe {
        let mut l: Vec<*mut paste_buffer> = Vec::new();
        let mut pb: *mut paste_buffer = null_mut();
        loop {
            pb = paste_walk(pb);
            if pb.is_null() {
                break;
            }
            l.push(pb);
        }
        sort_qsort(&mut l, sort_buffer_cmp, sc);
        l
    }
}

/// C `vendor/tmux/sort.c:437`: `struct client **sort_get_clients(u_int *n, struct sort_criteria *sort_crit)`
pub unsafe fn sort_get_clients(sc: sort_criteria) -> Vec<*mut client> {
    unsafe {
        let mut l: Vec<*mut client> = tailq_foreach(&raw mut CLIENTS)
            .map(NonNull::as_ptr)
            .filter(|&c| {
                !(*c).flags.intersects(CLIENT_UNATTACHEDFLAGS)
                    && (*c).flags.intersects(client_flag::ATTACHED)
            })
            .collect();
        sort_qsort(&mut l, sort_client_cmp, sc);
        l
    }
}

/// C `vendor/tmux/sort.c:464`: `struct session **sort_get_sessions(u_int *n, struct sort_criteria *sort_crit)`
pub unsafe fn sort_get_sessions(sc: sort_criteria) -> Vec<*mut session> {
    unsafe {
        let mut l: Vec<*mut session> = rb_foreach(&raw mut SESSIONS).map(NonNull::as_ptr).collect();
        sort_qsort(&mut l, sort_session_cmp, sc);
        l
    }
}

#[expect(
    dead_code,
    reason = "unified sort path (cmd-list-*/mode-tree), not yet wired"
)]
/// C `vendor/tmux/sort.c:487`: `struct window_pane **sort_get_panes(u_int *n, struct sort_criteria *sort_crit)`
pub unsafe fn sort_get_panes(sc: sort_criteria) -> Vec<*mut window_pane> {
    unsafe {
        let mut l: Vec<*mut window_pane> = Vec::new();
        for s in rb_foreach(&raw mut SESSIONS).map(NonNull::as_ptr) {
            for wl in rb_foreach(&raw mut (*s).windows).map(NonNull::as_ptr) {
                let w = (*wl).window;
                for wp in tailq_foreach::<_, discr_entry>(&raw mut (*w).panes).map(NonNull::as_ptr)
                {
                    l.push(wp);
                }
            }
        }
        sort_qsort(&mut l, sort_pane_cmp, sc);
        l
    }
}

#[expect(
    dead_code,
    reason = "unified sort path (cmd-list-*/mode-tree), not yet wired"
)]
/// C `vendor/tmux/sort.c:518`: `struct window_pane **sort_get_panes_session(struct session *s, u_int *n, struct sort_criteria *sort_crit)`
pub unsafe fn sort_get_panes_session(s: *mut session, sc: sort_criteria) -> Vec<*mut window_pane> {
    unsafe {
        let mut l: Vec<*mut window_pane> = Vec::new();
        for wl in rb_foreach(&raw mut (*s).windows).map(NonNull::as_ptr) {
            let w = (*wl).window;
            for wp in tailq_foreach::<_, discr_entry>(&raw mut (*w).panes).map(NonNull::as_ptr) {
                l.push(wp);
            }
        }
        sort_qsort(&mut l, sort_pane_cmp, sc);
        l
    }
}

/// C `vendor/tmux/sort.c:547`: `struct window_pane **sort_get_panes_window(struct window *w, u_int *n, struct sort_criteria *sort_crit)`
pub unsafe fn sort_get_panes_window(w: *mut window, sc: sort_criteria) -> Vec<*mut window_pane> {
    unsafe {
        let mut l: Vec<*mut window_pane> = tailq_foreach::<_, discr_entry>(&raw mut (*w).panes)
            .map(NonNull::as_ptr)
            .collect();
        sort_qsort(&mut l, sort_pane_cmp, sc);
        l
    }
}

/// C `vendor/tmux/sort.c:571`: `struct winlink **sort_get_winlinks(u_int *n, struct sort_criteria *sort_crit)`
pub unsafe fn sort_get_winlinks(sc: sort_criteria) -> Vec<*mut winlink> {
    unsafe {
        let mut l: Vec<*mut winlink> = Vec::new();
        for s in rb_foreach(&raw mut SESSIONS).map(NonNull::as_ptr) {
            for wl in rb_foreach(&raw mut (*s).windows).map(NonNull::as_ptr) {
                l.push(wl);
            }
        }
        sort_qsort(&mut l, sort_winlink_cmp, sc);
        l
    }
}

/// C `vendor/tmux/sort.c:597`: `struct winlink **sort_get_winlinks_session(struct session *s, u_int *n, struct sort_criteria *sort_crit)`
pub unsafe fn sort_get_winlinks_session(s: *mut session, sc: sort_criteria) -> Vec<*mut winlink> {
    unsafe {
        let mut l: Vec<*mut winlink> = rb_foreach(&raw mut (*s).windows)
            .map(NonNull::as_ptr)
            .collect();
        sort_qsort(&mut l, sort_winlink_cmp, sc);
        l
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // A plain-old-data stand-in for the tmux structs the real comparators walk,
    // so `sort_qsort`'s dispatch can be exercised without a live server.
    struct Item {
        key: i32,
    }

    unsafe fn item_cmp(a: *mut Item, b: *mut Item, sc: sort_criteria) -> i32 {
        unsafe {
            let mut result = match sc.order {
                sort_order::SORT_INDEX => (*a).key - (*b).key,
                _ => 0,
            };
            if sc.reversed {
                result = -result;
            }
            result
        }
    }

    fn crit(order: sort_order, reversed: bool) -> sort_criteria {
        sort_criteria {
            order,
            reversed,
            order_seq: core::ptr::null_mut(),
        }
    }

    fn keys(l: &[*mut Item]) -> Vec<i32> {
        unsafe { l.iter().map(|&p| (*p).key).collect() }
    }

    // `sort_qsort` faithfully mirrors sort.c:28 — SORT_END is a no-op, SORT_ORDER
    // keeps insertion order (reversing it only when `reversed`), and any other
    // order runs the comparator. These are the exact branches that decide the
    // element order of every `#{S:} #{W:} #{P:} #{L:}` format loop.
    #[test]
    fn sort_qsort_dispatch() {
        unsafe {
            let mut items = [Item { key: 3 }, Item { key: 1 }, Item { key: 2 }];
            let (p0, p1, p2) = (&raw mut items[0], &raw mut items[1], &raw mut items[2]);

            // SORT_INDEX runs the comparator: ascending by key.
            let mut l = vec![p0, p1, p2];
            sort_qsort(&mut l, item_cmp, crit(sort_order::SORT_INDEX, false));
            assert_eq!(keys(&l), [1, 2, 3]);

            // Reversed comparator: descending by key.
            let mut l = vec![p0, p1, p2];
            sort_qsort(&mut l, item_cmp, crit(sort_order::SORT_INDEX, true));
            assert_eq!(keys(&l), [3, 2, 1]);

            // SORT_ORDER keeps insertion order untouched.
            let mut l = vec![p0, p1, p2];
            sort_qsort(&mut l, item_cmp, crit(sort_order::SORT_ORDER, false));
            assert_eq!(keys(&l), [3, 1, 2]);

            // SORT_ORDER reversed flips insertion order in place (no comparator).
            let mut l = vec![p0, p1, p2];
            sort_qsort(&mut l, item_cmp, crit(sort_order::SORT_ORDER, true));
            assert_eq!(keys(&l), [2, 1, 3]);

            // SORT_END is a no-op even when reversed.
            let mut l = vec![p0, p1, p2];
            sort_qsort(&mut l, item_cmp, crit(sort_order::SORT_END, true));
            assert_eq!(keys(&l), [3, 1, 2]);
        }
    }
}