![]() |
libghostty
|
A grid reference is a reference to a specific cell position in the terminal. Obtain a grid reference from ghostty_terminal_grid_ref for untracked or ghostty_terminal_grid_ref_track for tracked. Untracked vs tracked is explained next.
Important: The grid reference APIs are not meant to be used as the core of a render loop. They are not built to sustain the framerates needed for rendering large screens. Use the render state API for that.
An untracked grid reference is a value type that snapshots a specific cell. It is only valid until the next update to the terminal instance. There is no guarantee that it will remain valid after any operation, even if a seemingly unrelated part of the grid is changed. These are meant to be read and have their values cached immediately after obtaining it.
An untracked grid reference has a performance cost in its initial lookup, but doesn't affect the ongoing performance of the terminal in any way, since it is a one-time snapshot.
A tracked grid reference follows its cell across normal screen operations. For example scrolling, scrollback pruning, resize/reflow, and other terminal mutations update the tracked reference automatically.
A tracked reference can still lose its original semantic location. This can happen when the underlying grid is reset, pruned, or otherwise discarded in a way that cannot be mapped to a meaningful new cell. In that state, ghostty_tracked_grid_ref_has_value() returns false and ghostty_tracked_grid_ref_snapshot() / ghostty_tracked_grid_ref_point() return GHOSTTY_NO_VALUE. The handle remains valid, and callers may move it to a new point with ghostty_tracked_grid_ref_set().
To read cell data from a tracked reference, first snapshot it with ghostty_tracked_grid_ref_snapshot(). The returned GhosttyGridRef is again an untracked reference and follows the same short lifetime rules as any other untracked grid reference.
A tracked reference belongs to the terminal screen/page-list that was active when it was created or last set. Converting it to a point uses that owning screen/page-list, even if the terminal has since switched between primary and alternate screens. Calling ghostty_tracked_grid_ref_set() resolves the new point against the terminal's currently active screen/page-list and may move the tracked reference between screens.
Tracked references are owned by the caller and must be freed with ghostty_tracked_grid_ref_free(). If the terminal that created a tracked reference is freed first, the handle remains valid only for tracked-grid-ref APIs: it reports no value and can still be freed.
Each tracked reference adds bookkeeping to terminal mutations. Use them sparingly for long-lived anchors such as selections, search state, marks, or application-side bookmarks.
An untracked reference is a snapshot. It doesn't need to be freed. The safety of accessing the value is documented explicitly above: it is only safe to access any data until the next terminal mutating operation (including free).
A tracked reference is allocated and must be freed when it is no longer needed. A tracked reference may outlive the terminal that created it; after terminal free, it reports no value and can still be freed.
Typedefs | |
| typedef struct GhosttyTrackedGridRefImpl * | GhosttyTrackedGridRef |
Data Structures | |
| struct | GhosttyGridRef |
| typedef struct GhosttyTrackedGridRefImpl* GhosttyTrackedGridRef |
Opaque handle to a tracked grid reference.
A tracked grid reference is owned by the caller and must be freed with ghostty_tracked_grid_ref_free(). If the terminal that created it is freed first, the handle remains valid only for tracked-grid-ref APIs: it reports no value and can still be freed.
| GHOSTTY_API GhosttyResult ghostty_grid_ref_cell | ( | const GhosttyGridRef * | ref, |
| GhosttyCell * | out_cell ) |
Get the cell from a grid reference.
| ref | Pointer to the grid reference | |
| [out] | out_cell | On success, set to the cell at the ref's position (may be NULL) |
| GHOSTTY_API GhosttyResult ghostty_grid_ref_graphemes | ( | const GhosttyGridRef * | ref, |
| uint32_t * | buf, | ||
| size_t | buf_len, | ||
| size_t * | out_len ) |
Get the grapheme cluster codepoints for the cell at the grid reference's position.
Writes the full grapheme cluster (the cell's primary codepoint followed by any combining codepoints) into the provided buffer. If the cell has no text, out_len is set to 0 and GHOSTTY_SUCCESS is returned.
If the buffer is too small (or NULL), the function returns GHOSTTY_OUT_OF_SPACE and writes the required number of codepoints to out_len. The caller can then retry with a sufficiently sized buffer.
| ref | Pointer to the grid reference | |
| buf | Output buffer of uint32_t codepoints (may be NULL) | |
| buf_len | Number of uint32_t elements in the buffer | |
| [out] | out_len | On success, the number of codepoints written. On GHOSTTY_OUT_OF_SPACE, the required buffer size in codepoints. |
| GHOSTTY_API GhosttyResult ghostty_grid_ref_hyperlink_uri | ( | const GhosttyGridRef * | ref, |
| uint8_t * | buf, | ||
| size_t | buf_len, | ||
| size_t * | out_len ) |
Get the hyperlink URI for the cell at the grid reference's position.
Writes the URI bytes into the provided buffer. If the cell has no hyperlink, out_len is set to 0 and GHOSTTY_SUCCESS is returned.
If the buffer is too small (or NULL), the function returns GHOSTTY_OUT_OF_SPACE and writes the required number of bytes to out_len. The caller can then retry with a sufficiently sized buffer.
| ref | Pointer to the grid reference | |
| buf | Output buffer for the URI bytes (may be NULL) | |
| buf_len | Size of the output buffer in bytes | |
| [out] | out_len | On success, the number of bytes written. On GHOSTTY_OUT_OF_SPACE, the required buffer size in bytes. |
| GHOSTTY_API GhosttyResult ghostty_grid_ref_row | ( | const GhosttyGridRef * | ref, |
| GhosttyRow * | out_row ) |
Get the row from a grid reference.
| ref | Pointer to the grid reference | |
| [out] | out_row | On success, set to the row at the ref's position (may be NULL) |
| GHOSTTY_API GhosttyResult ghostty_grid_ref_style | ( | const GhosttyGridRef * | ref, |
| GhosttyStyle * | out_style ) |
Get the style of the cell at the grid reference's position.
| ref | Pointer to the grid reference | |
| [out] | out_style | On success, set to the cell's style (may be NULL) |
| GHOSTTY_API void ghostty_tracked_grid_ref_free | ( | GhosttyTrackedGridRef | ref | ) |
Tracked grid references are owned grid references that move with the terminal. See Grid Reference for the full overview of tracked and untracked grid reference behavior. Free a tracked grid reference.
Passing NULL is allowed and has no effect. A tracked reference may be freed after the terminal that created it is freed.
| ref | Tracked grid reference to free. |
| GHOSTTY_API bool ghostty_tracked_grid_ref_has_value | ( | GhosttyTrackedGridRef | ref | ) |
Return whether a tracked grid reference currently has a meaningful value.
If the terminal that created the tracked reference has been freed, this returns false.
| ref | Tracked grid reference. |
| GHOSTTY_API GhosttyResult ghostty_tracked_grid_ref_point | ( | GhosttyTrackedGridRef | ref, |
| GhosttyPointTag | tag, | ||
| GhosttyPointCoordinate * | out_point ) |
Convert a tracked grid reference to a point in the requested coordinate space.
This is the tracked equivalent of ghostty_terminal_point_from_grid_ref(). Unlike snapshotting, this does not expose an intermediate untracked GhosttyGridRef.
A tracked reference is resolved against the terminal screen/page-list that currently owns the reference. If the terminal has switched between primary and alternate screens since the reference was created or last set, this may be different from the terminal's currently active screen.
If the tracked reference no longer has a meaningful value, this returns GHOSTTY_NO_VALUE. GHOSTTY_NO_VALUE is also returned when the reference cannot be represented in the requested coordinate space, including after the terminal that created the tracked reference has been freed.
| ref | Tracked grid reference. | |
| tag | Coordinate space to convert into. | |
| [out] | out_point | On success, receives the coordinate. May be NULL. |
| GHOSTTY_API GhosttyResult ghostty_tracked_grid_ref_set | ( | GhosttyTrackedGridRef | ref, |
| GhosttyTerminal | terminal, | ||
| GhosttyPoint | point ) |
Move an existing tracked grid reference to a new terminal point.
On success, the tracked reference begins tracking the new point and any prior "no value" state is cleared. On GHOSTTY_OUT_OF_MEMORY, the original tracked reference is left unchanged.
The terminal must be the same terminal that created the tracked reference. The point is resolved against the terminal screen/page-list that is active at the time this function is called. If the terminal has switched between primary and alternate screens, this may move the tracked reference from one screen/page-list to the other.
| ref | Tracked grid reference. |
| Terminal | Terminal instance that owns the reference. |
| Point | New point to track. |
| GHOSTTY_API GhosttyResult ghostty_tracked_grid_ref_snapshot | ( | GhosttyTrackedGridRef | ref, |
| GhosttyGridRef * | out_ref ) |
Snapshot a tracked grid reference into a regular GhosttyGridRef.
The returned GhosttyGridRef is an untracked snapshot and has the same lifetime rules as ghostty_terminal_grid_ref(): it is only valid until the next terminal update. Snapshot immediately before calling ghostty_grid_ref_cell(), ghostty_grid_ref_row(), ghostty_grid_ref_graphemes(), ghostty_grid_ref_hyperlink_uri(), or ghostty_grid_ref_style().
If the tracked reference no longer has a meaningful value, this returns GHOSTTY_NO_VALUE. This includes references whose owning terminal has been freed.
| ref | Tracked grid reference. | |
| [out] | out_ref | On success, receives an untracked snapshot. May be NULL. |