libghostty: add selection adjustment api

This commit is contained in:
Mitchell Hashimoto
2026-05-23 15:08:38 -07:00
parent 545a5aef66
commit 15d8963681
6 changed files with 168 additions and 12 deletions

View File

@@ -77,6 +77,61 @@ typedef struct {
bool rectangle;
} GhosttySelection;
/**
* Operation used to adjust a selection endpoint.
*
* Adjustment mutates the selection's logical end endpoint, not whichever
* endpoint is visually bottom/right. This preserves keyboard and drag
* behavior for both forward and reversed selections.
*
* @ingroup selection
*/
typedef enum GHOSTTY_ENUM_TYPED {
/** Move left to the previous non-empty cell, wrapping upward. */
GHOSTTY_SELECTION_ADJUST_LEFT = 0,
/** Move right to the next non-empty cell, wrapping downward. */
GHOSTTY_SELECTION_ADJUST_RIGHT = 1,
/**
* Move up one row at the current column, or to the beginning of the
* line if already at the top.
*/
GHOSTTY_SELECTION_ADJUST_UP = 2,
/**
* Move down to the next non-blank row at the current column, or to the
* end of the line if none exists.
*/
GHOSTTY_SELECTION_ADJUST_DOWN = 3,
/** Move to the top-left cell of the screen. */
GHOSTTY_SELECTION_ADJUST_HOME = 4,
/** Move to the right edge of the last non-blank row on the screen. */
GHOSTTY_SELECTION_ADJUST_END = 5,
/**
* Move up by one terminal page height, or to home if that would move
* past the top.
*/
GHOSTTY_SELECTION_ADJUST_PAGE_UP = 6,
/**
* Move down by one terminal page height, or to end if that would move
* past the bottom.
*/
GHOSTTY_SELECTION_ADJUST_PAGE_DOWN = 7,
/** Move to the left edge of the current line. */
GHOSTTY_SELECTION_ADJUST_BEGINNING_OF_LINE = 8,
/** Move to the right edge of the current line. */
GHOSTTY_SELECTION_ADJUST_END_OF_LINE = 9,
GHOSTTY_SELECTION_ADJUST_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
} GhosttySelectionAdjust;
/** @} */
#ifdef __cplusplus

View File

@@ -1123,6 +1123,35 @@ GHOSTTY_API GhosttyResult ghostty_terminal_get_multi(GhosttyTerminal terminal,
void** values,
size_t* out_written);
/**
* Adjust a selection snapshot using terminal selection semantics.
*
* This mutates the caller-provided GhosttySelection in place. The logical end
* endpoint is always moved, regardless of whether the selection is forward or
* reversed visually. The input selection remains a snapshot: after adjustment,
* call ghostty_terminal_set() with GHOSTTY_TERMINAL_OPT_SELECTION to install it
* as the terminal-owned selection if desired.
*
* The selection's start and end grid refs must both be valid untracked
* snapshots for the given terminal's currently active screen. In practice,
* they must come from that terminal and screen, and no mutating terminal call
* may have occurred since the refs were produced or reconstructed from
* tracked refs. Passing refs from another terminal, another screen, or stale
* refs violates this precondition.
*
* @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
* @param selection Selection snapshot to adjust in place
* @param adjustment The adjustment operation to apply
* @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the terminal,
* selection, selection references, or adjustment are invalid
*
* @ingroup terminal
*/
GHOSTTY_API GhosttyResult ghostty_terminal_selection_adjust(
GhosttyTerminal terminal,
GhosttySelection* selection,
GhosttySelectionAdjust adjustment);
/**
* Resolve a point in the terminal grid to a grid reference.
*