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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
// Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@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.
use crate::*;
use crate::options_::options_get_number_;
use crate::options_::options_get_string_;

struct layout_sets_entry {
    name: SyncCharPtr,
    arrange: Option<unsafe fn(*mut window)>,
}
impl layout_sets_entry {
    const fn new(name: &'static CStr, arrange: unsafe fn(*mut window)) -> Self {
        Self {
            name: SyncCharPtr::new(name),
            arrange: Some(arrange),
        }
    }
}

const LAYOUT_SETS_LEN: usize = 7;
static LAYOUT_SETS: [layout_sets_entry; LAYOUT_SETS_LEN] = [
    layout_sets_entry::new(c"even-horizontal", layout_set_even_h),
    layout_sets_entry::new(c"even-vertical", layout_set_even_v),
    layout_sets_entry::new(c"main-horizontal", layout_set_main_h),
    layout_sets_entry::new(c"main-horizontal-mirrored", layout_set_main_h_mirrored),
    layout_sets_entry::new(c"main-vertical", layout_set_main_v),
    layout_sets_entry::new(c"main-vertical-mirrored", layout_set_main_v_mirrored),
    layout_sets_entry::new(c"tiled", layout_set_tiled),
];

/// C `vendor/tmux/layout-set.c:126`: `static struct window_pane *layout_set_first_tiled(struct window *w)`
///
/// The first pane that still owns a tiled (leaf, non-floating) layout cell —
/// used as the main pane after `layout_free(w, 1)` keeps the leaf cells intact.
unsafe fn layout_set_first_tiled(w: *mut window) -> *mut window_pane {
    unsafe {
        for wp in tailq_foreach::<_, discr_entry>(&raw mut (*w).panes).map(NonNull::as_ptr) {
            if !(*wp).layout_cell.is_null() && layout_cell_is_tiled((*wp).layout_cell) != 0 {
                return wp;
            }
        }
        null_mut()
    }
}

/// C `vendor/tmux/layout-set.c:53`: `int layout_set_lookup(const char *name)`
pub unsafe fn layout_set_lookup(name: *const u8) -> i32 {
    unsafe {
        let mut matched: i32 = -1;

        for (i, ls) in LAYOUT_SETS.iter().enumerate() {
            if libc::strcmp(ls.name.as_ptr(), name) == 0 {
                return i as i32;
            }
        }

        for (i, ls) in LAYOUT_SETS.iter().enumerate() {
            if libc::strncmp(ls.name.as_ptr(), name, strlen(name)) == 0 {
                if matched != -1 {
                    // ambiguous
                    return -1;
                }
                matched = i as i32;
            }
        }

        matched
    }
}

/// C `vendor/tmux/layout-set.c:74`: `u_int layout_set_select(struct window *w, u_int layout)`
pub unsafe fn layout_set_select(w: *mut window, mut layout: u32) -> u32 {
    unsafe {
        if layout > LAYOUT_SETS_LEN as u32 - 1 {
            layout = LAYOUT_SETS_LEN as u32 - 1;
        }

        if let Some(arrange) = LAYOUT_SETS[layout as usize].arrange {
            arrange(w);
        }

        (*w).lastlayout = layout as i32;
        layout
    }
}

/// C `vendor/tmux/layout-set.c:87`: `u_int layout_set_next(struct window *w)`
pub unsafe fn layout_set_next(w: *mut window) -> u32 {
    unsafe {
        let mut layout: u32;

        if (*w).lastlayout == -1 {
            layout = 0;
        } else {
            layout = ((*w).lastlayout + 1) as u32;
            if layout > LAYOUT_SETS_LEN as u32 - 1 {
                layout = 0;
            }
        }

        if let Some(arrange) = LAYOUT_SETS[layout as usize].arrange {
            arrange(w);
        }
        (*w).lastlayout = layout as i32;
        layout
    }
}

/// C `vendor/tmux/layout-set.c:106`: `u_int layout_set_previous(struct window *w)`
pub unsafe fn layout_set_previous(w: *mut window) -> u32 {
    unsafe {
        let mut layout: u32;

        if (*w).lastlayout == -1 {
            layout = (LAYOUT_SETS_LEN - 1) as u32;
        } else {
            layout = (*w).lastlayout as u32;
            if layout == 0 {
                layout = (LAYOUT_SETS_LEN - 1) as u32;
            } else {
                layout -= 1;
            }
        }

        if let Some(arrange) = LAYOUT_SETS[layout as usize].arrange {
            arrange(w);
        }
        (*w).lastlayout = layout as i32;
        layout
    }
}

/// C `vendor/tmux/layout-set.c:139`: `static void layout_set_even(struct window *w, enum layout_type type)`
pub unsafe fn layout_set_even(w: *mut window, type_: layout_type) {
    let __func__ = c!("layout_set_even");
    unsafe {
        let mut sx: u32;
        let mut sy: u32;

        layout_print_cell((*w).layout_root, __func__, 1);

        // Get number of panes.
        let n = window_count_panes(w);
        if n <= 1 {
            return;
        }

        // Free the old root and construct a new.
        layout_free(w, 0);
        let lc = layout_create_cell(null_mut());
        (*w).layout_root = lc;
        if type_ == layout_type::LAYOUT_LEFTRIGHT {
            sx = (n * (PANE_MINIMUM + 1)) - 1;
            if sx < (*w).sx {
                sx = (*w).sx;
            }
            sy = (*w).sy;
        } else {
            sy = (n * (PANE_MINIMUM + 1)) - 1;
            if sy < (*w).sy {
                sy = (*w).sy;
            }
            sx = (*w).sx;
        }
        layout_set_size(lc, sx, sy, 0, 0);
        layout_make_node(lc, type_);

        // Build new leaf cells.
        for wp in tailq_foreach::<_, discr_entry>(&raw mut (*w).panes).map(NonNull::as_ptr) {
            let lcnew = layout_create_cell(lc);
            layout_make_leaf(lcnew, wp);
            (*lcnew).sx = (*w).sx;
            (*lcnew).sy = (*w).sy;
            tailq_insert_tail(&raw mut (*lc).cells, lcnew);
        }

        // Spread out cells.
        layout_spread_cell(w, lc);

        // Fix cell offsets.
        layout_fix_offsets(w);
        layout_fix_panes(w, null_mut());

        layout_print_cell((*w).layout_root, __func__, 1);

        window_resize(w, (*lc).sx, (*lc).sy, -1, -1);
        notify_window(c"window-layout-changed", w);
        server_redraw_window(w);
    }
}

/// C `vendor/tmux/layout-set.c:191`: `static void layout_set_even_h(struct window *w)`
unsafe fn layout_set_even_h(w: *mut window) {
    unsafe {
        layout_set_even(w, layout_type::LAYOUT_LEFTRIGHT);
    }
}

/// C `vendor/tmux/layout-set.c:197`: `static void layout_set_even_v(struct window *w)`
unsafe fn layout_set_even_v(w: *mut window) {
    unsafe {
        layout_set_even(w, layout_type::LAYOUT_TOPBOTTOM);
    }
}

/// C `vendor/tmux/layout-set.c:203`: `static void layout_set_main_h(struct window *w)`
pub unsafe fn layout_set_main_h(w: *mut window) {
    let __func__ = c!("layout_set_main_h");
    unsafe {
        // struct window_pane *wp;
        // struct layout_cell *lc, *lcmain, *lcother, *lcchild;
        // u_int n, mainh, otherh, sx, sy;
        // char *cause;
        // const char *s;
        let mut cause = null_mut();

        layout_print_cell((*w).layout_root, __func__, 1);

        // Get number of panes.
        let mut n = window_count_panes(w);
        if n <= 1 {
            return;
        }
        n -= 1; /* take off main pane */

        // Find available height - take off one line for the border.
        let sy = (*w).sy - 1;

        // Get the main pane height.
        let mut s = options_get_string_((*w).options, "main-pane-height");
        let mut mainh = args_string_percentage(s, 0, sy as i64, sy as i64, &raw mut cause) as u32;
        if !cause.is_null() {
            mainh = 24;
            free_(cause);
        }

        let mut otherh: u32;
        // Work out the other pane height.
        if mainh + PANE_MINIMUM >= sy {
            if sy <= PANE_MINIMUM + PANE_MINIMUM {
                mainh = PANE_MINIMUM;
            } else {
                mainh = sy - PANE_MINIMUM;
            }
            otherh = PANE_MINIMUM;
        } else {
            s = options_get_string_((*w).options, "other-pane-height");
            otherh = args_string_percentage(s, 0, sy as i64, sy as i64, &raw mut cause) as u32;
            if !cause.is_null() || otherh == 0 {
                otherh = sy - mainh;
                free_(cause);
            } else if otherh > sy || sy - otherh < mainh {
                otherh = sy - mainh;
            } else {
                mainh = sy - otherh;
            }
        }

        // Work out what width is needed.
        let mut sx = (n * (PANE_MINIMUM + 1)) - 1;
        if sx < (*w).sx {
            sx = (*w).sx;
        }

        // Free old tree and create a new root. Keep the leaf cells (only_nodes)
        // so each pane retains its current layout cell to reuse below.
        layout_free(w, 1);
        let lc = layout_create_cell(null_mut());
        (*w).layout_root = lc;
        layout_set_size(lc, sx, mainh + otherh + 1, 0, 0);
        layout_make_node(lc, layout_type::LAYOUT_TOPBOTTOM);

        // Use the first tiled pane as the main pane, reusing its cell.
        let wpmain = layout_set_first_tiled(w);
        let lcmain = (*wpmain).layout_cell;
        (*lcmain).parent = lc;
        layout_set_size(lcmain, sx, mainh, 0, 0);
        tailq_insert_tail(&raw mut (*lc).cells, lcmain);

        if n == 1 {
            // Reuse the single other pane's cell as-is (no resize): it keeps
            // its previous dimensions.
            let mut wp = tailq_next::<_, _, discr_entry>(wpmain);
            while !wp.is_null() && layout_cell_is_tiled((*wp).layout_cell) == 0 {
                wp = tailq_next::<_, _, discr_entry>(wp);
            }
            tailq_insert_tail(&raw mut (*lc).cells, (*wp).layout_cell);
            (*(*wp).layout_cell).parent = lc;
        } else {
            let lcother = layout_create_cell(lc);
            layout_set_size(lcother, sx, otherh, 0, 0);
            layout_make_node(lcother, layout_type::LAYOUT_LEFTRIGHT);
            tailq_insert_tail(&raw mut (*lc).cells, lcother);

            // Add the remaining panes as children, reusing their cells.
            for wp in tailq_foreach::<_, discr_entry>(&raw mut (*w).panes).map(NonNull::as_ptr) {
                if wp == wpmain {
                    continue;
                }
                let lcchild = (*wp).layout_cell;
                tailq_insert_tail(&raw mut (*lcother).cells, lcchild);
                (*lcchild).parent = lcother;
                if layout_cell_is_tiled(lcchild) != 0 {
                    layout_set_size(lcchild, PANE_MINIMUM, otherh, 0, 0);
                }
            }
            layout_spread_cell(w, lcother);
        }

        // Fix cell offsets.
        layout_fix_offsets(w);
        layout_fix_panes(w, null_mut());

        layout_print_cell((*w).layout_root, __func__, 1);

        window_resize(w, (*lc).sx, (*lc).sy, -1, -1);
        notify_window(c"window-layout-changed", w);
        server_redraw_window(w);
    }
}

/// C `vendor/tmux/layout-set.c:300`: `static void layout_set_main_h_mirrored(struct window *w)`
pub unsafe fn layout_set_main_h_mirrored(w: *mut window) {
    let __func__ = c!("layout_set_main_h_mirrored");
    unsafe {
        let mut otherh: u32;
        let mut cause: *mut u8 = null_mut();

        layout_print_cell((*w).layout_root, __func__, 1);

        // Get number of panes.
        let mut n = window_count_panes(w);
        if n <= 1 {
            return;
        }
        n -= 1; // take off main pane

        // Find available height - take off one line for the border.
        let sy = (*w).sy - 1;

        // Get the main pane height.
        let s = options_get_string_((*w).options, "main-pane-height");
        let mut mainh = args_string_percentage(s, 0, sy as i64, sy as i64, &raw mut cause) as u32;
        if !cause.is_null() {
            mainh = 24;
            free_(cause);
        }

        // Work out the other pane height.
        if mainh + PANE_MINIMUM >= sy {
            if sy <= PANE_MINIMUM + PANE_MINIMUM {
                mainh = PANE_MINIMUM;
            } else {
                mainh = sy - PANE_MINIMUM;
            }
            otherh = PANE_MINIMUM;
        } else {
            let s = options_get_string_((*w).options, "other-pane-height");
            otherh = args_string_percentage(s, 0, sy as i64, sy as i64, &raw mut cause) as u32;
            if !cause.is_null() || otherh == 0 {
                otherh = sy - mainh;
                free_(cause);
            } else if otherh > sy || sy - otherh < mainh {
                otherh = sy - mainh;
            } else {
                mainh = sy - otherh;
            }
        }

        // Work out what width is needed.
        let mut sx = (n * (PANE_MINIMUM + 1)) - 1;
        if sx < (*w).sx {
            sx = (*w).sx;
        }

        // Free old tree and create a new root. Keep the leaf cells (only_nodes)
        // so each pane retains its current layout cell to reuse below.
        layout_free(w, 1);
        let lc = layout_create_cell(null_mut());
        (*w).layout_root = lc;
        layout_set_size(lc, sx, mainh + otherh + 1, 0, 0);
        layout_make_node(lc, layout_type::LAYOUT_TOPBOTTOM);

        // Use the first tiled pane as the main pane, reusing its cell.
        let wpmain = layout_set_first_tiled(w);
        let lcmain = (*wpmain).layout_cell;
        (*lcmain).parent = lc;
        layout_set_size(lcmain, sx, mainh, 0, 0);
        tailq_insert_tail(&raw mut (*lc).cells, lcmain);

        // Mirrored: the other panes go at the head so they sit above the main.
        if n == 1 {
            let mut wp = tailq_next::<_, _, discr_entry>(wpmain);
            while !wp.is_null() && layout_cell_is_tiled((*wp).layout_cell) == 0 {
                wp = tailq_next::<_, _, discr_entry>(wp);
            }
            tailq_insert_head(&raw mut (*lc).cells, (*wp).layout_cell);
            (*(*wp).layout_cell).parent = lc;
        } else {
            let lcother = layout_create_cell(lc);
            layout_set_size(lcother, sx, otherh, 0, 0);
            layout_make_node(lcother, layout_type::LAYOUT_LEFTRIGHT);
            tailq_insert_head(&raw mut (*lc).cells, lcother);

            // Add the remaining panes as children, reusing their cells.
            for wp in tailq_foreach::<_, discr_entry>(&raw mut (*w).panes).map(NonNull::as_ptr) {
                if wp == wpmain {
                    continue;
                }
                let lcchild = (*wp).layout_cell;
                tailq_insert_tail(&raw mut (*lcother).cells, lcchild);
                (*lcchild).parent = lcother;
                if layout_cell_is_tiled(lcchild) != 0 {
                    layout_set_size(lcchild, PANE_MINIMUM, otherh, 0, 0);
                }
            }
            layout_spread_cell(w, lcother);
        }

        // Fix cell offsets.
        layout_fix_offsets(w);
        layout_fix_panes(w, null_mut());

        layout_print_cell((*w).layout_root, __func__, 1);

        window_resize(w, (*lc).sx, (*lc).sy, -1, -1);
        notify_window(c"window-layout-changed", w);
        server_redraw_window(w);
    }
}

/// C `vendor/tmux/layout-set.c:397`: `static void layout_set_main_v(struct window *w)`
pub unsafe fn layout_set_main_v(w: *mut window) {
    let __func__ = c!("layout_set_main_v");
    let mut cause = null_mut();

    unsafe {
        layout_print_cell((*w).layout_root, __func__, 1);

        // Get number of panes.
        let mut n = window_count_panes(w);
        if n <= 1 {
            return;
        }
        n -= 1; // take off main pane

        // Find available width - take off one line for the border.
        let sx = (*w).sx - 1;

        // Get the main pane width.
        let s = options_get_string_((*w).options, "main-pane-width");
        let mut mainw: u32 =
            args_string_percentage(s, 0, sx as i64, sx as i64, &raw mut cause) as u32;
        // C: default only when the option failed to parse (cause != NULL). The
        // check was inverted, so a valid main-pane-width was overwritten with 80.
        if !cause.is_null() {
            mainw = 80;
            free_(cause);
        }

        // Work out the other pane width.
        let mut otherw;
        if mainw + PANE_MINIMUM >= sx {
            if sx <= PANE_MINIMUM + PANE_MINIMUM {
                mainw = PANE_MINIMUM;
            } else {
                mainw = sx - PANE_MINIMUM;
            }
            otherw = PANE_MINIMUM;
        } else {
            let s = options_get_string_((*w).options, "other-pane-width");
            otherw = args_string_percentage(s, 0, sx as i64, sx as i64, &raw mut cause) as u32;
            if !cause.is_null() || otherw == 0 {
                otherw = sx - mainw;
                free_(cause);
            } else if otherw > sx || sx - otherw < mainw {
                otherw = sx - mainw;
            } else {
                mainw = sx - otherw;
            }
        }

        // Work out what height is needed.
        let mut sy = (n * (PANE_MINIMUM + 1)) - 1;
        if sy < (*w).sy {
            sy = (*w).sy;
        }

        // Free old tree and create a new root. Keep the leaf cells (only_nodes)
        // so each pane retains its current layout cell to reuse below.
        layout_free(w, 1);
        let lc = layout_create_cell(null_mut());
        (*w).layout_root = lc;
        layout_set_size(lc, mainw + otherw + 1, sy, 0, 0);
        layout_make_node(lc, layout_type::LAYOUT_LEFTRIGHT);

        // Use the first tiled pane as the main pane, reusing its cell.
        let wpmain = layout_set_first_tiled(w);
        let lcmain = (*wpmain).layout_cell;
        (*lcmain).parent = lc;
        layout_set_size(lcmain, mainw, sy, 0, 0);
        tailq_insert_tail(&raw mut (*lc).cells, lcmain);

        if n == 1 {
            // Reuse the single other pane's cell as-is (no resize): it keeps
            // its previous dimensions.
            let mut wp = tailq_next::<_, _, discr_entry>(wpmain);
            while !wp.is_null() && layout_cell_is_tiled((*wp).layout_cell) == 0 {
                wp = tailq_next::<_, _, discr_entry>(wp);
            }
            tailq_insert_tail(&raw mut (*lc).cells, (*wp).layout_cell);
            (*(*wp).layout_cell).parent = lc;
        } else {
            let lcother = layout_create_cell(lc);
            layout_set_size(lcother, otherw, sy, 0, 0);
            layout_make_node(lcother, layout_type::LAYOUT_TOPBOTTOM);
            tailq_insert_tail(&raw mut (*lc).cells, lcother);

            // Add the remaining panes as children, reusing their cells.
            for wp in tailq_foreach::<_, discr_entry>(&raw mut (*w).panes).map(NonNull::as_ptr) {
                if wp == wpmain {
                    continue;
                }
                let lcchild = (*wp).layout_cell;
                tailq_insert_tail(&raw mut (*lcother).cells, lcchild);
                (*lcchild).parent = lcother;
                if layout_cell_is_tiled(lcchild) != 0 {
                    layout_set_size(lcchild, otherw, PANE_MINIMUM, 0, 0);
                }
            }
            layout_spread_cell(w, lcother);
        }

        // Fix cell offsets.
        layout_fix_offsets(w);
        layout_fix_panes(w, null_mut());

        layout_print_cell((*w).layout_root, __func__, 1);

        window_resize(w, (*lc).sx, (*lc).sy, -1, -1);
        notify_window(c"window-layout-changed", w);
        server_redraw_window(w);
    }
}

/// C `vendor/tmux/layout-set.c:494`: `static void layout_set_main_v_mirrored(struct window *w)`
pub unsafe fn layout_set_main_v_mirrored(w: *mut window) {
    let __func__ = c!("layout_set_main_v_mirrored");
    unsafe {
        let mut cause: *mut u8 = null_mut();

        layout_print_cell((*w).layout_root, __func__, 1);

        // Get number of panes.
        let mut n = window_count_panes(w);
        if n <= 1 {
            return;
        }
        n -= 1; // take off main pane

        // Find available width - take off one line for the border.
        let sx = (*w).sx - 1;

        // Get the main pane width.
        let s = options_get_string_((*w).options, "main-pane-width");
        let mut mainw = args_string_percentage(s, 0, sx as i64, sx as i64, &raw mut cause) as u32;
        if !cause.is_null() {
            mainw = 80;
            free_(cause);
        }

        // Work out the other pane width.
        let mut otherw: u32;
        if mainw + PANE_MINIMUM >= sx {
            if sx <= PANE_MINIMUM + PANE_MINIMUM {
                mainw = PANE_MINIMUM;
            } else {
                mainw = sx - PANE_MINIMUM;
            }
            otherw = PANE_MINIMUM;
        } else {
            let s = options_get_string_((*w).options, "other-pane-width");
            otherw = args_string_percentage(s, 0, sx as i64, sx as i64, &raw mut cause) as u32;
            if !cause.is_null() || otherw == 0 {
                otherw = sx - mainw;
                free_(cause);
            } else if otherw > sx || sx - otherw < mainw {
                otherw = sx - mainw;
            } else {
                mainw = sx - otherw;
            }
        }

        // Work out what height is needed.
        let mut sy = (n * (PANE_MINIMUM + 1)) - 1;
        if sy < (*w).sy {
            sy = (*w).sy;
        }

        // Free old tree and create a new root. Keep the leaf cells (only_nodes)
        // so each pane retains its current layout cell to reuse below.
        layout_free(w, 1);
        let lc = layout_create_cell(null_mut());
        (*w).layout_root = lc;
        layout_set_size(lc, mainw + otherw + 1, sy, 0, 0);
        layout_make_node(lc, layout_type::LAYOUT_LEFTRIGHT);

        // Use the first tiled pane as the main pane, reusing its cell.
        let wpmain = layout_set_first_tiled(w);
        let lcmain = (*wpmain).layout_cell;
        (*lcmain).parent = lc;
        layout_set_size(lcmain, mainw, sy, 0, 0);
        tailq_insert_tail(&raw mut (*lc).cells, lcmain);

        // Mirrored: the other panes go at the head so they sit left of the main.
        if n == 1 {
            let mut wp = tailq_next::<_, _, discr_entry>(wpmain);
            while !wp.is_null() && layout_cell_is_tiled((*wp).layout_cell) == 0 {
                wp = tailq_next::<_, _, discr_entry>(wp);
            }
            tailq_insert_head(&raw mut (*lc).cells, (*wp).layout_cell);
            (*(*wp).layout_cell).parent = lc;
        } else {
            let lcother = layout_create_cell(lc);
            layout_make_node(lcother, layout_type::LAYOUT_TOPBOTTOM);
            layout_set_size(lcother, otherw, sy, 0, 0);
            tailq_insert_head(&raw mut (*lc).cells, lcother);

            // Add the remaining panes as children, reusing their cells.
            for wp in tailq_foreach::<_, discr_entry>(&raw mut (*w).panes).map(NonNull::as_ptr) {
                if wp == wpmain {
                    continue;
                }
                let lcchild = (*wp).layout_cell;
                tailq_insert_tail(&raw mut (*lcother).cells, lcchild);
                (*lcchild).parent = lcother;
                if layout_cell_is_tiled(lcchild) != 0 {
                    layout_set_size(lcchild, otherw, PANE_MINIMUM, 0, 0);
                }
            }
            layout_spread_cell(w, lcother);
        }

        // Fix cell offsets.
        layout_fix_offsets(w);
        layout_fix_panes(w, null_mut());

        layout_print_cell((*w).layout_root, __func__, 1);

        window_resize(w, (*lc).sx, (*lc).sy, -1, -1);
        notify_window(c"window-layout-changed", w);
        server_redraw_window(w);
    }
}

/// C `vendor/tmux/layout-set.c:592`: `static void layout_set_tiled(struct window *w)`
pub unsafe fn layout_set_tiled(w: *mut window) {
    let __func__ = c!("layout_set_tiled");

    unsafe {
        layout_print_cell((*w).layout_root, __func__, 1);

        // Get number of panes.
        let n = window_count_panes(w);
        if n <= 1 {
            return;
        }

        // Get maximum columns from window option.
        let max_columns = options_get_number_((*w).options, "tiled-layout-max-columns") as u32;

        // How many rows and columns are wanted?
        let mut rows = 1;
        let mut columns = 1;
        while rows * columns < n {
            rows += 1;
            if rows * columns < n && (max_columns == 0 || columns < max_columns) {
                columns += 1;
            }
        }

        // What width and height should they be?
        let mut width = ((*w).sx - (columns - 1)) / columns;
        if width < PANE_MINIMUM {
            width = PANE_MINIMUM;
        }
        let mut height = ((*w).sy - (rows - 1)) / rows;
        if height < PANE_MINIMUM {
            height = PANE_MINIMUM;
        }

        // Free old tree and create a new root.
        layout_free(w, 0);
        let lc = layout_create_cell(null_mut());
        (*w).layout_root = lc;
        let mut sx = ((width + 1) * columns) - 1;
        if sx < (*w).sx {
            sx = (*w).sx;
        }
        let mut sy = ((height + 1) * rows) - 1;
        if sy < (*w).sy {
            sy = (*w).sy;
        }
        layout_set_size(lc, sx, sy, 0, 0);
        layout_make_node(lc, layout_type::LAYOUT_TOPBOTTOM);

        // Create a grid of the cells.
        let mut wp = tailq_first(&raw mut (*w).panes);
        for j in 0..rows {
            // If this is the last cell, all done.
            if wp.is_null() {
                break;
            }

            // Create the new row.
            let lcrow = layout_create_cell(lc);
            layout_set_size(lcrow, (*w).sx, height, 0, 0);
            tailq_insert_tail(&raw mut (*lc).cells, lcrow);

            // If only one column, just use the row directly.
            if n - (j * columns) == 1 || columns == 1 {
                layout_make_leaf(lcrow, wp);
                wp = tailq_next::<_, _, discr_entry>(wp);
                continue;
            }

            // Add in the columns.
            layout_make_node(lcrow, layout_type::LAYOUT_LEFTRIGHT);
            let mut i = 0;
            for i_ in 0..columns {
                i = i_;
                // Create and add a pane cell.
                let lcchild = layout_create_cell(lcrow);
                layout_set_size(lcchild, width, height, 0, 0);
                layout_make_leaf(lcchild, wp);
                tailq_insert_tail(&raw mut (*lcrow).cells, lcchild);

                // Move to the next cell.
                wp = tailq_next::<_, _, discr_entry>(wp);
                if wp.is_null() {
                    break;
                }
                i += 1;
            }

            // Adjust the row and columns to fit the full width if necessary.
            if i == columns {
                i -= 1;
            }
            let used = ((i + 1) * (width + 1)) - 1;
            if (*w).sx <= used {
                continue;
            }
            let lcchild = tailq_last(&raw mut (*lcrow).cells);
            layout_resize_adjust(
                w,
                lcchild,
                layout_type::LAYOUT_LEFTRIGHT,
                ((*w).sx - used) as i32,
            );
        }

        // Adjust the last row height to fit if necessary.
        let used = (rows * height) + rows - 1;
        if (*w).sy > used {
            let lcrow = tailq_last(&raw mut (*lc).cells);
            layout_resize_adjust(
                w,
                lcrow,
                layout_type::LAYOUT_TOPBOTTOM,
                ((*w).sy - used) as i32,
            );
        }

        // Fix cell offsets.
        layout_fix_offsets(w);
        layout_fix_panes(w, null_mut());

        layout_print_cell((*w).layout_root, __func__, 1);

        window_resize(w, (*lc).sx, (*lc).sy, -1, -1);
        notify_window(c"window-layout-changed", w);
        server_redraw_window(w);
    }
}

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

    // LAYOUT_SETS order (vendor/tmux/layout-set.c):
    //   0 even-horizontal          4 main-vertical
    //   1 even-vertical            5 main-vertical-mirrored
    //   2 main-horizontal          6 tiled
    //   3 main-horizontal-mirrored

    #[test]
    fn lookup_exact_names() {
        unsafe {
            assert_eq!(layout_set_lookup(crate::c!("even-horizontal")), 0);
            assert_eq!(layout_set_lookup(crate::c!("even-vertical")), 1);
            assert_eq!(layout_set_lookup(crate::c!("tiled")), 6);
            // An exact match wins even when the name is a prefix of another
            // entry (main-horizontal vs main-horizontal-mirrored).
            assert_eq!(layout_set_lookup(crate::c!("main-horizontal")), 2);
            assert_eq!(layout_set_lookup(crate::c!("main-vertical")), 4);
        }
    }

    #[test]
    fn lookup_unique_prefix() {
        unsafe {
            // "ti" is a unique prefix of tiled only.
            assert_eq!(layout_set_lookup(crate::c!("ti")), 6);
            // "even-h" only matches even-horizontal (even-vertical differs at
            // the sixth character).
            assert_eq!(layout_set_lookup(crate::c!("even-h")), 0);
        }
    }

    #[test]
    fn lookup_ambiguous_and_unknown_return_minus_one() {
        unsafe {
            // "even" matches both even-horizontal and even-vertical.
            assert_eq!(layout_set_lookup(crate::c!("even")), -1);
            // "main-h" matches main-horizontal and main-horizontal-mirrored.
            assert_eq!(layout_set_lookup(crate::c!("main-h")), -1);
            // No layout has this name.
            assert_eq!(layout_set_lookup(crate::c!("does-not-exist")), -1);
        }
    }

    // Every full name resolves to its own index via the exact-match pass
    // (vendor/tmux/layout-set.c:53). This guards the LAYOUT_SETS ordering the
    // rest of the module (select/next/previous) depends on.
    #[test]
    fn lookup_every_name_roundtrips() {
        unsafe {
            for (i, ls) in LAYOUT_SETS.iter().enumerate() {
                assert_eq!(
                    layout_set_lookup(ls.name.as_ptr()),
                    i as i32,
                    "name at index {i} did not resolve to itself"
                );
            }
        }
    }

    // main-vertical vs main-vertical-mirrored: the bare name is an exact hit,
    // "main-v" is ambiguous, and the longer unique prefix reaches the mirror.
    #[test]
    fn lookup_main_vertical_variants() {
        unsafe {
            assert_eq!(layout_set_lookup(crate::c!("main-vertical")), 4);
            assert_eq!(layout_set_lookup(crate::c!("main-vertical-mirrored")), 5);
            // "main-v" prefixes both main-vertical and main-vertical-mirrored.
            assert_eq!(layout_set_lookup(crate::c!("main-v")), -1);
            // First character past "main-vertical" disambiguates to the mirror.
            assert_eq!(layout_set_lookup(crate::c!("main-vertical-m")), 5);
        }
    }

    // The empty string is a prefix of every entry (strncmp with length 0 returns
    // 0), so it is ambiguous across all seven and returns -1.
    #[test]
    fn lookup_empty_string_ambiguous() {
        unsafe {
            assert_eq!(layout_set_lookup(crate::c!("")), -1);
        }
    }

    // "tiled" has no siblings, so each of its proper prefixes resolves uniquely.
    #[test]
    fn lookup_tiled_prefixes_all_resolve() {
        unsafe {
            for p in ["t", "ti", "til", "tile", "tiled"] {
                let mut s = p.to_string();
                s.push('\0');
                assert_eq!(layout_set_lookup(s.as_bytes().as_ptr()), 6, "prefix {p:?}");
            }
        }
    }

    // Matching is case-sensitive (strcmp/strncmp): an upper-case name misses
    // entirely, and "main" alone is ambiguous across all four main-* layouts.
    #[test]
    fn lookup_case_sensitive_and_main_ambiguous() {
        unsafe {
            assert_eq!(layout_set_lookup(crate::c!("MAIN-VERTICAL")), -1);
            assert_eq!(layout_set_lookup(crate::c!("main")), -1);
            assert_eq!(layout_set_lookup(crate::c!("main-horizontal-mirrored")), 3);
        }
    }
}