![]() |
libghostty
|
Pasting into a terminal, plus the terminal-free utilities for validating and encoding paste data.
What a paste writes to the pty depends on the terminal's state, so the recommended way to paste is ghostty_terminal_paste(). The embedder hands over the MIME types the clipboard holds (just text/plain for an ordinary paste), a GhosttyMimeReader that produces the data of any one of them, and where the paste came from, and the terminal decides how its current modes apply:
The data is pulled through GhosttyPaste::reader only when a representation is actually pasted (so a clipboard holding a large image next to some text costs nothing), and the encoded bytes stream to the write_pty callback (GHOSTTY_TERMINAL_OPT_WRITE_PTY) in chunks as they are produced, never in one piece. The callback may be invoked several times for a single paste; the pieces must be written to the pty in order.
Text that could inject commands (a newline when unbracketed, or the bracketed paste terminator when bracketed) is refused with GHOSTTY_REJECTED and nothing written unless GhosttyPaste::allow_unsafe is set. The usual flow is to call once, confirm with the user on GHOSTTY_REJECTED, and call again with allow_unsafe set. Each call reads the text at most once and buffers it whole while the rule is applied, so the source needs no stability across reads (the confirmed retry simply pastes whatever the source holds then) and a refused or failed paste writes nothing at all.
For embedders that encode without a terminal, ghostty_paste_is_safe() checks if paste data contains potentially dangerous sequences (conservatively, regardless of terminal state) and ghostty_paste_encode() encodes paste data for writing to the pty, including bracketed paste wrapping and unsafe byte stripping.
Enumerations | |
| enum | GhosttyPasteSource { GHOSTTY_PASTE_SOURCE_CLIPBOARD = 0 , GHOSTTY_PASTE_SOURCE_TEXT = 1 , GHOSTTY_PASTE_SOURCE_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE } |
Functions | |
| GHOSTTY_API GhosttyResult | ghostty_terminal_paste (GhosttyTerminal terminal, const GhosttyPaste *paste, bool *out_written) |
| GHOSTTY_API bool | ghostty_paste_is_safe (const char *data, size_t len) |
| GHOSTTY_API GhosttyResult | ghostty_paste_encode (char *data, size_t data_len, bool bracketed, char *buf, size_t buf_len, size_t *out_written) |
Data Structures | |
| struct | GhosttyPaste |
| enum GhosttyPasteSource |
Why a paste happened.
| GHOSTTY_API GhosttyResult ghostty_paste_encode | ( | char * | data, |
| size_t | data_len, | ||
| bool | bracketed, | ||
| char * | buf, | ||
| size_t | buf_len, | ||
| size_t * | out_written ) |
Encode paste data for writing to the terminal pty.
This function prepares paste data for terminal input by:
bracketed is truebracketed is falseThe input data buffer is modified in place during encoding. The encoded result (potentially with bracketed paste prefix/suffix) is written to the output buffer.
If the output buffer is too small, the function returns GHOSTTY_OUT_OF_SPACE and sets the required size in out_written. The caller can then retry with a sufficiently sized buffer.
This is the encoder ghostty_terminal_paste() uses for a text paste; use it directly when there is no terminal to paste into.
| data | The paste data to encode (modified in place, may be NULL) | |
| data_len | The length of the input data in bytes | |
| bracketed | Whether bracketed paste mode is active | |
| buf | Output buffer to write the encoded result into (may be NULL) | |
| buf_len | Size of the output buffer in bytes | |
| [out] | out_written | On success, the number of bytes written. On GHOSTTY_OUT_OF_SPACE, the required buffer size. |
References ghostty_paste_encode().
Referenced by ghostty_paste_encode().
| GHOSTTY_API bool ghostty_paste_is_safe | ( | const char * | data, |
| size_t | len ) |
Check if paste data is safe to paste into the terminal.
Data is considered unsafe if it contains:
This check is conservative and considers data unsafe regardless of current terminal state. ghostty_terminal_paste() applies the terminal-state-aware rule itself (newlines are safe inside a bracketed paste); use this to apply the stricter rule on top.
| data | The paste data to check (must not be NULL) |
| len | The length of the data in bytes |
References ghostty_paste_is_safe().
Referenced by ghostty_paste_is_safe().
| GHOSTTY_API GhosttyResult ghostty_terminal_paste | ( | GhosttyTerminal | terminal, |
| const GhosttyPaste * | paste, | ||
| bool * | out_written ) |
Paste into the terminal according to its current state: a Kitty clipboard protocol paste event if mode 5522 is enabled and a clipboard_read callback is installed, otherwise the text framed per mode 2004. See the group documentation for the full behavior. Output streams through the write_pty callback in chunks. The viewport is not scrolled; that is up to the embedder, as for key input.
A paste event records a session grant for its one-time password only once the event is written; a failed call never leaves a grant for an event that was never sent.
| terminal | The terminal handle | |
| paste | The paste request, borrowed for the duration of the call | |
| [out] | out_written | On success, whether anything was written to the pty (the encoded text or a paste event). False means there was nothing to paste: no non-empty text representation. May be NULL. |
out_written); GHOSTTY_REJECTED if the text could inject commands and GhosttyPaste::allow_unsafe is false (nothing was written); GHOSTTY_INVALID_VALUE for a NULL terminal or paste, MIME types without a reader, or when no write_pty callback is installed; GHOSTTY_OUT_OF_MEMORY; GHOSTTY_IO_ERROR if the reader failed or there is no secure entropy source to mint a paste event password (wasm32-freestanding without GHOSTTY_SYS_OPT_RANDOM_SECURE set). Errors write nothing. References ghostty_terminal_paste().
Referenced by ghostty_terminal_paste().