![]() |
libghostty
|
A snapshot selection range defined by two grid references that identifies a contiguous or rectangular region of terminal content.
The start and end values are GhosttyGridRef values. They are therefore untracked grid references and inherit the same lifetime rules: they are only safe to use until the next mutating operation on the terminal that produced them, including freeing the terminal. To keep a selection valid across terminal mutations, callers must maintain tracked grid references for the endpoints and reconstruct a GhosttySelection from fresh snapshots when needed.
Selection gestures provide a reusable state machine for turning UI pointer interactions into selection snapshots. A caller creates one GhosttySelectionGesture per active gesture stream, reuses typed GhosttySelectionGestureEvent objects for synthetic press, drag, release, autoscroll tick, and deep-press events, and applies each event with ghostty_selection_gesture_event(). The returned GhosttySelection is a snapshot; the embedder decides whether to render it, format/copy it, or install it as the terminal's active selection.
Typedefs | |
| typedef struct GhosttySelectionGestureImpl * | GhosttySelectionGesture |
| typedef struct GhosttySelectionGestureEventImpl * | GhosttySelectionGestureEvent |
Data Structures | |
| struct | GhosttySelection |
| struct | GhosttyTerminalSelectWordOptions |
| struct | GhosttyTerminalSelectWordBetweenOptions |
| struct | GhosttyTerminalSelectLineOptions |
| struct | GhosttyTerminalSelectionFormatOptions |
| struct | GhosttySelectionGestureBehaviors |
| struct | GhosttySelectionGestureGeometry |
| typedef struct GhosttySelectionGestureImpl* GhosttySelectionGesture |
Opaque handle to state for interpreting terminal selection gestures.
The gesture owns only the state required to interpret pointer events. Calls that use a gesture are not concurrency-safe and must be serialized with terminal mutations.
Definition at line 61 of file selection.h.
| typedef struct GhosttySelectionGestureEventImpl* GhosttySelectionGestureEvent |
Opaque handle to reusable input data for selection gesture operations.
Event options are set with ghostty_selection_gesture_event_set(). Individual gesture operations document which options are required or optional.
Definition at line 71 of file selection.h.
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.
Definition at line 271 of file selection.h.
Current autoscroll direction for an active selection drag gesture.
Definition at line 378 of file selection.h.
Selection behavior chosen for a gesture's click sequence.
Definition at line 322 of file selection.h.
Data fields readable from a selection gesture with ghostty_selection_gesture_get().
| Enumerator | |
|---|---|
| GHOSTTY_SELECTION_GESTURE_DATA_CLICK_COUNT | Current click count: uint8_t*. 0 means inactive. |
| GHOSTTY_SELECTION_GESTURE_DATA_DRAGGED | Whether the current/last left-click gesture has dragged: bool*. |
| GHOSTTY_SELECTION_GESTURE_DATA_AUTOSCROLL | Current autoscroll request: GhosttySelectionGestureAutoscroll*. |
| GHOSTTY_SELECTION_GESTURE_DATA_BEHAVIOR | Current gesture behavior: GhosttySelectionGestureBehavior*. |
| GHOSTTY_SELECTION_GESTURE_DATA_ANCHOR | Current left-click anchor: GhosttyGridRef*. Returns GHOSTTY_NO_VALUE if there is no valid active anchor. On success, writes an untracked GhosttyGridRef snapshot with normal GhosttyGridRef lifetime rules. |
Definition at line 397 of file selection.h.
Options stored on a reusable selection gesture event.
Passing NULL as the value to ghostty_selection_gesture_event_set() clears the corresponding option.
Definition at line 457 of file selection.h.
Selection gesture event type.
The event type is fixed when the event is created. Each event type documents which options are valid and which options are required by gesture operations.
| Enumerator | |
|---|---|
| GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_PRESS | Press event for ghostty_selection_gesture_event(). |
| GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_RELEASE | Release event for ghostty_selection_gesture_event(). |
| GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_DRAG | Drag event for ghostty_selection_gesture_event(). |
| GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_AUTOSCROLL_TICK | Autoscroll tick event for ghostty_selection_gesture_event(). |
| GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_DEEP_PRESS | Deep press event for ghostty_selection_gesture_event(). |
Definition at line 430 of file selection.h.
Ordering of a selection's endpoints in terminal coordinates.
Mirrored orders are only produced by rectangular selections whose start and end endpoints are on opposite diagonal corners that are not simple top-left-to-bottom-right or bottom-right-to-top-left orderings.
Definition at line 246 of file selection.h.
| GHOSTTY_API GhosttyResult ghostty_selection_gesture_event | ( | GhosttySelectionGesture | gesture, |
| GhosttyTerminal | terminal, | ||
| GhosttySelectionGestureEvent | event, | ||
| GhosttySelection * | out_selection ) |
Apply a selection gesture event and return the resulting selection snapshot.
This dispatches to the gesture operation matching the event's fixed type. For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_PRESS, the event must have GHOSTTY_SELECTION_GESTURE_EVENT_OPT_REF set before calling this function. All other press options use their initialized defaults when unset or cleared.
For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_RELEASE, only GHOSTTY_SELECTION_GESTURE_EVENT_OPT_REF is valid. It is optional; if unset or cleared, release records that the pointer did not map to a valid cell. Release events update gesture state but do not produce a selection, so this function returns GHOSTTY_NO_VALUE after applying them.
For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_DRAG, GHOSTTY_SELECTION_GESTURE_EVENT_OPT_REF and GHOSTTY_SELECTION_GESTURE_EVENT_OPT_GEOMETRY are required. Position, rectangle, and word-boundary codepoints are optional and use initialized defaults when unset or cleared.
For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_AUTOSCROLL_TICK, GHOSTTY_SELECTION_GESTURE_EVENT_OPT_VIEWPORT and GHOSTTY_SELECTION_GESTURE_EVENT_OPT_GEOMETRY are required. Position, rectangle, and word-boundary codepoints are optional and use initialized defaults when unset or cleared.
For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_DEEP_PRESS, only GHOSTTY_SELECTION_GESTURE_EVENT_OPT_WORD_BOUNDARY_CODEPOINTS is valid. It is optional and uses initialized defaults when unset or cleared.
The returned selection is not installed as the terminal's current selection. It is a snapshot with the same lifetime rules as GhosttySelection.
| gesture | Selection gesture handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| Terminal | Terminal used to interpret and update gesture state | |
| event | Selection gesture event handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| [out] | out_selection | On success, receives the resulting selection. May be NULL to apply the event and discard the selection result. |
| GHOSTTY_API void ghostty_selection_gesture_event_free | ( | GhosttySelectionGestureEvent | event | ) |
Free a selection gesture event object.
Passing NULL is allowed and is a no-op.
| event | Selection gesture event handle to free |
| GHOSTTY_API GhosttyResult ghostty_selection_gesture_event_new | ( | const GhosttyAllocator * | allocator, |
| GhosttySelectionGestureEvent * | out_event, | ||
| GhosttySelectionGestureEventType | type ) |
Create a reusable selection gesture event object.
| Memory Management | Allocator, or NULL for the default allocator |
| out_event | Receives the created event handle |
| type | Event type. This is fixed for the lifetime of the event. |
| GHOSTTY_API GhosttyResult ghostty_selection_gesture_event_set | ( | GhosttySelectionGestureEvent | event, |
| GhosttySelectionGestureEventOption | option, | ||
| const void * | value ) |
Set or clear an option on a selection gesture event.
The value type depends on option and is documented by GhosttySelectionGestureEventOption. Passing NULL for value clears the option.
| event | Selection gesture event handle (NULL returns GHOSTTY_INVALID_VALUE) |
| option | Event option to set or clear |
| value | Pointer to the input value for option, or NULL to clear |
| GHOSTTY_API void ghostty_selection_gesture_free | ( | GhosttySelectionGesture | gesture, |
| GhosttyTerminal | terminal ) |
Free a selection gesture object.
This releases any tracked terminal references owned by the gesture using the provided terminal, then frees the gesture object. Passing NULL for gesture is allowed and is a no-op.
If the terminal is still alive, pass the terminal most recently used with the gesture so any tracked terminal references can be released correctly. If the terminal has already been freed, pass NULL for terminal; the terminal's page storage has already released the underlying tracked references, so the gesture wrapper can be safely discarded without touching the stale terminal state.
| gesture | Selection gesture handle to free |
| Terminal | Terminal used to release tracked gesture state, or NULL if the terminal has already been freed |
| GHOSTTY_API GhosttyResult ghostty_selection_gesture_get | ( | GhosttySelectionGesture | gesture, |
| GhosttyTerminal | terminal, | ||
| GhosttySelectionGestureData | data, | ||
| void * | value ) |
Read data from a selection gesture.
The type of value depends on data and is documented by GhosttySelectionGestureData. For GHOSTTY_SELECTION_GESTURE_DATA_ANCHOR, the returned GhosttyGridRef is an untracked snapshot with normal grid-ref lifetime rules.
| gesture | Selection gesture handle (NULL returns GHOSTTY_INVALID_VALUE) |
| Terminal | Terminal used to validate terminal-backed gesture state |
| data | Data field to read |
| value | Output pointer whose type depends on data |
| GHOSTTY_API GhosttyResult ghostty_selection_gesture_get_multi | ( | GhosttySelectionGesture | gesture, |
| GhosttyTerminal | terminal, | ||
| size_t | count, | ||
| const GhosttySelectionGestureData * | keys, | ||
| void ** | values, | ||
| size_t * | out_written ) |
Read multiple data fields from a selection gesture in a single call.
This is an optimization over calling ghostty_selection_gesture_get() multiple times. Each entry in values must point to storage of the type documented by the corresponding GhosttySelectionGestureData key.
If any individual read fails, the function returns that error and writes the index of the failing key to out_written when out_written is non-NULL. On success, out_written receives count when non-NULL.
| gesture | Selection gesture handle (NULL returns GHOSTTY_INVALID_VALUE) |
| Terminal | Terminal used to validate terminal-backed gesture state |
| count | Number of data fields to read |
| keys | Data fields to read (must not be NULL) |
| values | Output pointers corresponding to keys (must not be NULL) |
| out_written | Optional number of fields read, or failing index on error |
| GHOSTTY_API GhosttyResult ghostty_selection_gesture_new | ( | const GhosttyAllocator * | allocator, |
| GhosttySelectionGesture * | out_gesture ) |
Create a selection gesture object.
The gesture stores mutable state for terminal text selection gestures. The gesture is not bound to a terminal at creation time; terminal-dependent APIs take the terminal explicitly.
| Memory Management | Allocator, or NULL for the default allocator |
| out_gesture | Receives the created gesture handle |
| GHOSTTY_API void ghostty_selection_gesture_reset | ( | GhosttySelectionGesture | gesture, |
| GhosttyTerminal | terminal ) |
Reset any active selection gesture state.
This cancels the active click sequence and releases any tracked terminal references owned by the gesture without freeing the gesture object. Passing NULL is allowed and is a no-op.
| gesture | Selection gesture handle to reset |
| Terminal | Terminal used to release tracked gesture state |
| GHOSTTY_API GhosttyResult ghostty_terminal_select_all | ( | GhosttyTerminal | terminal, |
| GhosttySelection * | out_selection ) |
Derive a selection snapshot covering all selectable terminal content.
The returned selection is not installed as the terminal's current selection. It is a snapshot with the same lifetime rules as GhosttySelection.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| [out] | out_selection | On success, receives the derived selection |
| GHOSTTY_API GhosttyResult ghostty_terminal_select_line | ( | GhosttyTerminal | terminal, |
| const GhosttyTerminalSelectLineOptions * | options, | ||
| GhosttySelection * | out_selection ) |
Derive a line selection snapshot from a terminal grid reference.
The returned selection is not installed as the terminal's current selection. It is a snapshot with the same lifetime rules as GhosttySelection.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| options | Line-selection options | |
| [out] | out_selection | On success, receives the derived selection |
| GHOSTTY_API GhosttyResult ghostty_terminal_select_output | ( | GhosttyTerminal | terminal, |
| GhosttyGridRef | ref, | ||
| GhosttySelection * | out_selection ) |
Derive a command-output selection snapshot from a terminal grid reference.
The returned selection is not installed as the terminal's current selection. It is a snapshot with the same lifetime rules as GhosttySelection.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| ref | Grid reference within command output to select | |
| [out] | out_selection | On success, receives the derived selection |
| GHOSTTY_API GhosttyResult ghostty_terminal_select_word | ( | GhosttyTerminal | terminal, |
| const GhosttyTerminalSelectWordOptions * | options, | ||
| GhosttySelection * | out_selection ) |
Derive a word selection snapshot from a terminal grid reference.
The returned selection is not installed as the terminal's current selection. It is a snapshot with the same lifetime rules as GhosttySelection.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| options | Word-selection options | |
| [out] | out_selection | On success, receives the derived selection |
| GHOSTTY_API GhosttyResult ghostty_terminal_select_word_between | ( | GhosttyTerminal | terminal, |
| const GhosttyTerminalSelectWordBetweenOptions * | options, | ||
| GhosttySelection * | out_selection ) |
Derive the nearest word selection snapshot between two terminal grid refs.
Starting at options->start, this searches toward options->end (inclusive) and returns the first selectable word found using Ghostty's word-selection rules.
This is useful for implementing double-click-and-drag selection in a UI. If a user double-clicks one word and drags across spaces or punctuation toward another word, selecting only the word directly under the current pointer can flicker or collapse when the pointer is between words. Instead, ask for the nearest word between the original click and the drag point, ask again in the reverse direction, and combine the two word bounds into the drag selection.
The returned selection is not installed as the terminal's current selection. It is a snapshot with the same lifetime rules as GhosttySelection.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| options | Word-between-selection options | |
| [out] | out_selection | On success, receives the derived selection |
| GHOSTTY_API GhosttyResult ghostty_terminal_selection_adjust | ( | GhosttyTerminal | terminal, |
| GhosttySelection * | selection, | ||
| GhosttySelectionAdjust | adjustment ) |
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.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) |
| Selection | Selection snapshot to adjust in place |
| adjustment | The adjustment operation to apply |
| GHOSTTY_API GhosttyResult ghostty_terminal_selection_contains | ( | GhosttyTerminal | terminal, |
| const GhosttySelection * | selection, | ||
| GhosttyPoint | point, | ||
| bool * | out_contains ) |
Test whether a terminal point is inside a selection snapshot.
This uses the same selection semantics as the terminal, including rectangular/block selections and linear selections spanning multiple rows.
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.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| Selection | Selection snapshot to inspect | |
| Point | Point to test for containment | |
| [out] | out_contains | On success, receives whether point is inside selection |
| GHOSTTY_API GhosttyResult ghostty_terminal_selection_equal | ( | GhosttyTerminal | terminal, |
| const GhosttySelection * | a, | ||
| const GhosttySelection * | b, | ||
| bool * | out_equal ) |
Test whether two selection snapshots are equal.
Equality uses the terminal's internal selection semantics: both endpoint pins must match and both selections must have the same rectangular/block state. This avoids requiring callers to compare raw GhosttyGridRef internals.
Both selections' start and end grid refs must 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.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| a | First selection snapshot to compare | |
| b | Second selection snapshot to compare | |
| [out] | out_equal | On success, receives whether the selections are equal |
| GHOSTTY_API GhosttyResult ghostty_terminal_selection_format_alloc | ( | GhosttyTerminal | terminal, |
| const GhosttyAllocator * | allocator, | ||
| GhosttyTerminalSelectionFormatOptions | options, | ||
| uint8_t ** | out_ptr, | ||
| size_t * | out_len ) |
Format a terminal selection into an allocated buffer.
This is a one-shot convenience API for formatting either the terminal's active selection or a caller-provided GhosttySelection without explicitly creating a GhosttyFormatter.
The returned buffer is allocated using allocator, or the default allocator if NULL is passed. The caller owns the returned buffer and must free it with ghostty_free(), passing the same allocator and returned length.
The returned bytes are not NUL-terminated. This supports plain text, VT, and HTML uniformly as byte output.
If options.selection is NULL and the terminal has no active selection, the function returns GHOSTTY_NO_VALUE and leaves out_ptr as NULL and out_len as 0.
| Terminal | The terminal to read from (must not be NULL) |
| Memory Management | Allocator used for the returned buffer, or NULL for the default allocator |
| options | Selection formatting options |
| out_ptr | Receives the allocated output buffer (must not be NULL) |
| out_len | Receives the output length in bytes (must not be NULL) |
| GHOSTTY_API GhosttyResult ghostty_terminal_selection_format_buf | ( | GhosttyTerminal | terminal, |
| GhosttyTerminalSelectionFormatOptions | options, | ||
| uint8_t * | buf, | ||
| size_t | buf_len, | ||
| size_t * | out_written ) |
Format a terminal selection into a caller-provided buffer.
This is a one-shot convenience API for formatting either the terminal's active selection or a caller-provided GhosttySelection without explicitly creating a GhosttyFormatter.
Pass NULL for buf to query the required output size. In that case, out_written receives the required size and the function returns GHOSTTY_OUT_OF_SPACE.
If buf is too small, the function returns GHOSTTY_OUT_OF_SPACE and writes the required size to out_written. The caller can then retry with a larger buffer.
If options.selection is NULL and the terminal has no active selection, the function returns GHOSTTY_NO_VALUE.
| Terminal | The terminal to read from (must not be NULL) |
| options | Selection formatting options |
| buf | Output buffer, or NULL to query required size |
| buf_len | Length of buf in bytes |
| out_written | Number of bytes written, or required size on GHOSTTY_OUT_OF_SPACE (must not be NULL) |
| GHOSTTY_API GhosttyResult ghostty_terminal_selection_order | ( | GhosttyTerminal | terminal, |
| const GhosttySelection * | selection, | ||
| GhosttySelectionOrder * | out_order ) |
Get the current endpoint ordering of a selection snapshot.
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.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| Selection | Selection snapshot to inspect | |
| [out] | out_order | On success, receives the selection order |
| GHOSTTY_API GhosttyResult ghostty_terminal_selection_ordered | ( | GhosttyTerminal | terminal, |
| const GhosttySelection * | selection, | ||
| GhosttySelectionOrder | desired, | ||
| GhosttySelection * | out_selection ) |
Return a selection snapshot with endpoints ordered as requested.
Use GHOSTTY_SELECTION_ORDER_FORWARD to get top-left to bottom-right bounds, and GHOSTTY_SELECTION_ORDER_REVERSE to get bottom-right to top-left bounds. Mirrored desired orders are accepted but normalized the same as forward. The output selection is a fresh untracked snapshot and is not installed as the terminal's current selection.
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.
| Terminal | The terminal handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| Selection | Selection snapshot to order | |
| desired | Desired endpoint order | |
| [out] | out_selection | On success, receives the ordered selection |