rmux-core 0.10.0

Core session, pane, layout, format, hook, and buffer model for the RMUX terminal multiplexer.
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
use std::collections::BTreeMap;

use rmux_proto::{RmuxError, RotateWindowDirection, TerminalSize};

use super::target_error::{invalid_window_target, invalid_window_target_with_reason};
use super::{cyclic_previous_window_index, Session};
use crate::{Pane, PaneId, Window};

#[path = "window_ops/navigation.rs"]
mod navigation;

impl Session {
    /// Creates a new window at the lowest available index and returns its window index together with the initial pane ID.
    pub fn create_window(&mut self, size: TerminalSize) -> Result<(u32, PaneId), RmuxError> {
        self.create_window_at_or_above(size, 0)
    }

    /// Creates a new window at the lowest available index at or above `minimum_index`.
    pub fn create_window_at_or_above(
        &mut self,
        size: TerminalSize,
        minimum_index: u32,
    ) -> Result<(u32, PaneId), RmuxError> {
        let pane_id = self.allocate_pane_id();
        self.create_window_at_or_above_with_pane_id(size, minimum_index, pane_id)
    }

    /// Creates a new window at the lowest available index at or above `minimum_index`
    /// using the provided initial pane identity.
    pub fn create_window_at_or_above_with_pane_id(
        &mut self,
        size: TerminalSize,
        minimum_index: u32,
        pane_id: PaneId,
    ) -> Result<(u32, PaneId), RmuxError> {
        let window_index = self.lowest_available_window_index_at_or_above(minimum_index)?;
        let window_id = self.allocate_window_id();
        self.windows.insert(
            window_index,
            Window::new_with_initial_pane(size, pane_id, window_id),
        );
        self.winlink_alert_flags
            .insert(window_index, crate::AlertFlags::empty());
        Ok((window_index, pane_id))
    }

    /// Inserts a window with its initial pane at the provided window index.
    pub fn insert_window_with_initial_pane(
        &mut self,
        window_index: u32,
        size: TerminalSize,
    ) -> Result<(), RmuxError> {
        let pane_id = self.allocate_pane_id();
        self.insert_window_with_initial_pane_with_id(window_index, size, pane_id)
    }

    /// Inserts a window with its initial pane at the provided window index using the
    /// supplied pane identity.
    pub fn insert_window_with_initial_pane_with_id(
        &mut self,
        window_index: u32,
        size: TerminalSize,
        pane_id: PaneId,
    ) -> Result<(), RmuxError> {
        if self.windows.contains_key(&window_index) {
            return Err(invalid_window_target_with_reason(
                &self.name,
                window_index,
                "window index already exists in session",
            ));
        }

        let window_id = self.allocate_window_id();
        self.windows.insert(
            window_index,
            Window::new_with_initial_pane(size, pane_id, window_id),
        );
        self.winlink_alert_flags
            .insert(window_index, crate::AlertFlags::empty());
        Ok(())
    }

    /// Inserts an existing window at the provided window index without rewriting its identities.
    pub fn insert_existing_window(
        &mut self,
        window_index: u32,
        window: Window,
    ) -> Result<(), RmuxError> {
        if self.windows.contains_key(&window_index) {
            return Err(invalid_window_target_with_reason(
                &self.name,
                window_index,
                "window index already exists in session",
            ));
        }

        self.bump_allocators_for_window(&window);
        self.windows.insert(window_index, window);
        self.winlink_alert_flags
            .insert(window_index, crate::AlertFlags::empty());
        Ok(())
    }

    /// Opens a destination slot for insertion and returns every shifted old-to-new winlink index.
    pub fn make_room_for_window(
        &mut self,
        window_index: u32,
    ) -> Result<BTreeMap<u32, u32>, RmuxError> {
        let first_gap = self.lowest_available_window_index_at_or_above(window_index)?;
        if first_gap == window_index {
            return Ok(BTreeMap::new());
        }

        let mut index_map = BTreeMap::new();
        for source_index in (window_index..first_gap).rev() {
            let destination_index = source_index.checked_add(1).ok_or_else(|| {
                RmuxError::Server(format!(
                    "window index space exhausted for session {}",
                    self.name
                ))
            })?;
            let _ = self.move_window(source_index, destination_index, false, false)?;
            index_map.insert(source_index, destination_index);
        }

        Ok(index_map)
    }

    /// Inserts a linked copy of an existing window at the destination slot.
    pub fn link_window(
        &mut self,
        window_index: u32,
        window: Window,
        kill_destination: bool,
        select_destination: bool,
    ) -> Result<Option<Window>, RmuxError> {
        if self.windows.contains_key(&window_index) && !kill_destination {
            return Err(invalid_window_target_with_reason(
                &self.name,
                window_index,
                "window index already exists in session",
            ));
        }

        let removed = if kill_destination {
            let removed = self.replace_window(window_index, window)?;
            let _ = self.clear_all_winlink_alert_flags(window_index);
            removed
        } else {
            self.insert_existing_window(window_index, window)?;
            return if select_destination {
                self.select_window(window_index)?;
                Ok(None)
            } else {
                Ok(None)
            };
        };

        if select_destination {
            self.select_window(window_index)?;
        }

        Ok(Some(removed))
    }

    /// Replaces an existing window at the provided index and returns the removed window.
    pub fn replace_window(
        &mut self,
        window_index: u32,
        window: Window,
    ) -> Result<Window, RmuxError> {
        if !self.windows.contains_key(&window_index) {
            return Err(invalid_window_target(&self.name, window_index));
        }

        self.bump_allocators_for_window(&window);
        Ok(self
            .windows
            .insert(window_index, window)
            .expect("replaced window must exist at the addressed index"))
    }

    /// Removes the addressed window and returns it, using tmux's last-window then cyclic-previous fallback when needed.
    pub fn remove_window(&mut self, window_index: u32) -> Result<Window, RmuxError> {
        if !self.windows.contains_key(&window_index) {
            return Err(invalid_window_target(&self.name, window_index));
        }

        if self.windows.len() == 1 {
            return Err(RmuxError::Server(format!(
                "cannot kill the only window in session {}",
                self.name
            )));
        }

        self.remove_window_allowing_empty(window_index)
    }

    /// Removes a window even when it is the final window in the session.
    ///
    /// Callers must destroy the now-empty session in the same transaction.
    pub fn remove_window_allowing_empty(&mut self, window_index: u32) -> Result<Window, RmuxError> {
        if !self.windows.contains_key(&window_index) {
            return Err(invalid_window_target(&self.name, window_index));
        }

        let next_active = if self.active_window == window_index {
            (self.windows.len() > 1).then(|| self.next_active_window_after_removal(window_index))
        } else {
            None
        };

        let removed = self
            .windows
            .remove(&window_index)
            .expect("window existence was checked before removal");
        self.winlink_alert_flags.remove(&window_index);

        if let Some(next_active) = next_active {
            self.select_window(next_active)
                .expect("replacement window must exist after removal");
        }

        if self.last_window == Some(window_index) {
            self.last_window = None;
        }

        Ok(removed)
    }

    /// Renames the addressed window and disables automatic renaming for it.
    pub fn rename_window(&mut self, window_index: u32, name: String) -> Result<(), RmuxError> {
        self.resolve_window_target_mut(window_index)?.set_name(name);
        Ok(())
    }

    /// Updates the addressed window name while preserving automatic renaming.
    pub fn set_automatic_window_name(
        &mut self,
        window_index: u32,
        name: String,
    ) -> Result<(), RmuxError> {
        let window = self.resolve_window_target_mut(window_index)?;
        if window.automatic_rename() {
            window.set_automatic_name(name);
        }
        Ok(())
    }

    /// Reindexes sparse window slots into a contiguous `0..n` range without rewriting the windows themselves.
    pub fn reindex_windows(&mut self) -> Result<BTreeMap<u32, u32>, RmuxError> {
        self.reindex_windows_from(0)
    }

    /// Reindexes sparse window slots into a contiguous range starting at `first_index`.
    pub fn reindex_windows_from(
        &mut self,
        first_index: u32,
    ) -> Result<BTreeMap<u32, u32>, RmuxError> {
        if !self.windows.is_empty() {
            let last_offset = self.windows.len().saturating_sub(1) as u32;
            first_index.checked_add(last_offset).ok_or_else(|| {
                RmuxError::Server(format!(
                    "window index space exhausted for session {}",
                    self.name
                ))
            })?;
        }
        let previous_windows = std::mem::take(&mut self.windows);
        let mut reindexed = BTreeMap::new();
        let mut index_map = BTreeMap::new();
        let mut next_index = first_index;

        let window_count = previous_windows.len();
        for (position, (window_index, window)) in previous_windows.into_iter().enumerate() {
            index_map.insert(window_index, next_index);
            reindexed.insert(next_index, window);
            if position + 1 < window_count {
                next_index = next_index.checked_add(1).ok_or_else(|| {
                    RmuxError::Server(format!(
                        "window index space exhausted for session {}",
                        self.name
                    ))
                })?;
            }
        }

        self.windows = reindexed;
        self.winlink_alert_flags = self
            .winlink_alert_flags
            .iter()
            .filter_map(|(window_index, flags)| {
                index_map
                    .get(window_index)
                    .copied()
                    .map(|next_index| (next_index, *flags))
            })
            .collect();
        self.active_window = *index_map
            .get(&self.active_window)
            .expect("active window must survive reindexing");
        self.last_window = self
            .last_window
            .and_then(|window_index| index_map.get(&window_index).copied());

        Ok(index_map)
    }

    /// Moves one window to another slot within the same session, optionally removing an occupied destination.
    ///
    /// When `select_destination` is `true`, the destination slot becomes active
    /// using tmux's winlink-based selection semantics instead of following the
    /// moved window by identity.
    pub fn move_window(
        &mut self,
        source_index: u32,
        destination_index: u32,
        kill_destination: bool,
        select_destination: bool,
    ) -> Result<Option<Window>, RmuxError> {
        if !self.windows.contains_key(&source_index) {
            return Err(invalid_window_target(&self.name, source_index));
        }
        if source_index == destination_index {
            return Ok(None);
        }
        if self.windows.contains_key(&destination_index) && !kill_destination {
            return Err(invalid_window_target_with_reason(
                &self.name,
                destination_index,
                "window index already exists in session",
            ));
        }

        let previous_active = self.active_window;
        let previous_last = self.last_window;
        let moved_window = self.extract_window_for_move(source_index)?;
        let moved_alert_flags = self
            .winlink_alert_flags
            .remove(&source_index)
            .unwrap_or_else(crate::AlertFlags::empty);
        let removed_window = if kill_destination {
            let _ = self.winlink_alert_flags.remove(&destination_index);
            self.windows.remove(&destination_index)
        } else {
            None
        };

        self.windows.insert(destination_index, moved_window);
        self.winlink_alert_flags
            .insert(destination_index, moved_alert_flags);
        self.apply_move_tracking(
            source_index,
            destination_index,
            previous_active,
            previous_last,
            select_destination,
        );

        Ok(removed_window)
    }

    /// Swaps two window slots within the same session while preserving the underlying windows.
    pub fn swap_windows(
        &mut self,
        source_index: u32,
        destination_index: u32,
    ) -> Result<(), RmuxError> {
        if !self.windows.contains_key(&source_index) {
            return Err(invalid_window_target(&self.name, source_index));
        }
        if !self.windows.contains_key(&destination_index) {
            return Err(invalid_window_target(&self.name, destination_index));
        }
        if source_index == destination_index {
            return Ok(());
        }

        let source_window = self
            .windows
            .remove(&source_index)
            .expect("source window must exist for swap");
        let destination_window = self
            .windows
            .remove(&destination_index)
            .expect("destination window must exist for swap");
        let source_flags = self
            .winlink_alert_flags
            .remove(&source_index)
            .unwrap_or_else(crate::AlertFlags::empty);
        let destination_flags = self
            .winlink_alert_flags
            .remove(&destination_index)
            .unwrap_or_else(crate::AlertFlags::empty);

        self.windows.insert(source_index, destination_window);
        self.windows.insert(destination_index, source_window);
        self.winlink_alert_flags
            .insert(source_index, destination_flags);
        self.winlink_alert_flags
            .insert(destination_index, source_flags);
        Ok(())
    }

    /// Rotates pane positions in the addressed window without renumbering the panes themselves.
    pub fn rotate_window(
        &mut self,
        window_index: u32,
        direction: RotateWindowDirection,
    ) -> Result<(), RmuxError> {
        self.resolve_window_target_mut(window_index)?
            .rotate_panes(direction);
        Ok(())
    }

    /// Rotates pane positions with zoom save/restore via `-Z`.
    pub fn rotate_window_with_zoom(
        &mut self,
        window_index: u32,
        direction: RotateWindowDirection,
        restore_zoom: bool,
    ) -> Result<(), RmuxError> {
        self.resolve_window_target_mut(window_index)?
            .rotate_panes_with_zoom(direction, restore_zoom);
        Ok(())
    }

    /// Sets the explicit size of the addressed window.
    pub fn resize_window(
        &mut self,
        window_index: u32,
        size: TerminalSize,
    ) -> Result<(), RmuxError> {
        self.resolve_window_target_mut(window_index)?.set_size(size);
        Ok(())
    }

    /// Respawns the addressed window, replacing it with a single pane.
    /// Returns the pane ID retained for the respawned pane.
    pub fn respawn_window(&mut self, window_index: u32) -> Result<PaneId, RmuxError> {
        let pane_id = self
            .window_at(window_index)
            .ok_or_else(|| invalid_window_target(&self.name, window_index))?
            .panes()
            .first()
            .map(Pane::id)
            .ok_or_else(|| RmuxError::Server("window has no panes".to_owned()))?;
        self.respawn_window_with_pane_id(window_index, pane_id)
    }

    /// Respawns the addressed window with an explicitly provided pane identity.
    pub fn respawn_window_with_pane_id(
        &mut self,
        window_index: u32,
        pane_id: PaneId,
    ) -> Result<PaneId, RmuxError> {
        let window = self.resolve_window_target_mut(window_index)?;
        window.respawn(pane_id);
        Ok(pane_id)
    }

    fn extract_window_for_move(&mut self, window_index: u32) -> Result<Window, RmuxError> {
        self.windows
            .remove(&window_index)
            .ok_or_else(|| invalid_window_target(&self.name, window_index))
    }

    fn apply_move_tracking(
        &mut self,
        source_index: u32,
        destination_index: u32,
        previous_active: u32,
        previous_last: Option<u32>,
        select_destination: bool,
    ) {
        let source_was_active = previous_active == source_index;
        let destination_was_active = previous_active == destination_index;

        self.active_window = if source_was_active {
            if select_destination {
                destination_index
            } else {
                self.next_active_window_after_detach(source_index, previous_last)
            }
        } else if select_destination {
            destination_index
        } else {
            previous_active
        };

        self.last_window = if source_was_active {
            if select_destination {
                self.preserved_last_after_selecting_destination(
                    source_index,
                    destination_index,
                    previous_last,
                )
            } else {
                None
            }
        } else if select_destination {
            if destination_was_active {
                self.preserved_last_after_selecting_destination(
                    source_index,
                    destination_index,
                    previous_last,
                )
            } else {
                Some(previous_active)
            }
        } else if previous_last == Some(source_index) {
            None
        } else {
            previous_last.filter(|window_index| self.windows.contains_key(window_index))
        };

        if self.last_window == Some(self.active_window) {
            self.last_window = None;
        }
    }

    fn next_active_window_after_detach(
        &self,
        removed_index: u32,
        previous_last: Option<u32>,
    ) -> u32 {
        if let Some(last_window) = previous_last {
            if last_window != removed_index && self.windows.contains_key(&last_window) {
                return last_window;
            }
        }

        cyclic_previous_window_index(&self.windows, removed_index)
            .expect("a non-empty session must have a replacement window")
    }

    fn preserved_last_after_selecting_destination(
        &self,
        source_index: u32,
        destination_index: u32,
        previous_last: Option<u32>,
    ) -> Option<u32> {
        previous_last.filter(|window_index| {
            *window_index != source_index
                && *window_index != destination_index
                && self.windows.contains_key(window_index)
        })
    }

    fn bump_allocators_for_window(&mut self, window: &Window) {
        self.next_window_id
            .bump_to(window.id().as_u32().saturating_add(1));
        self.next_pane_id = self.next_pane_id.max(
            window
                .panes()
                .iter()
                .map(|pane| pane.id().as_u32().saturating_add(1))
                .max()
                .unwrap_or(self.next_pane_id),
        );
    }
}