Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

selection.h

1,062 lines40.4 KB

1/**
2 * @file selection.h
3 *
4 * Selection range type for specifying a region of terminal content.
5 */
6​
7#ifndef GHOSTTY_VT_SELECTION_H
8#define GHOSTTY_VT_SELECTION_H
9​
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13#include <ghostty/vt/allocator.h>
14#include <ghostty/vt/grid_ref.h>
15#include <ghostty/vt/point.h>
16#include <ghostty/vt/types.h>
17​
18#ifdef __cplusplus
19extern "C" {
20#endif
21​
22/** @addtogroup selection
23 *
24 * A selection is a range of terminal content between two cells. It is a
25 * snapshot, only valid until the next change to the terminal.
26 *
27 * You need a terminal, and usually a grid reference to start from.
28 *
29 * ## Example
30 *
31 * @code{.c}
32 * GhosttyTerminalSelectWordOptions word =
33 * GHOSTTY_INIT_SIZED(GhosttyTerminalSelectWordOptions);
34 * word.ref = ref;
35 *
36 * GhosttySelection selection = {0};
37 * if (ghostty_terminal_select_word(terminal, &word, &selection) ==
38 * GHOSTTY_SUCCESS) {
39 * ghostty_terminal_set(terminal, GHOSTTY_TERMINAL_OPT_SELECTION, &selection);
40 * }
41 * @endcode
42 *
43 * Full program: @ref c-vt-selection/src/main.c
44 *
45 * The @ref guide-selection "Selection guide" covers making one, turning pointer
46 * gestures into one, and copying it out.
47 *
48 * @{
49 */
50​
51/**
52 * Opaque handle to state for interpreting terminal selection gestures.
53 *
54 * The gesture owns only the state required to interpret pointer events. Calls
55 * that use a gesture are not concurrency-safe and must be serialized with
56 * terminal mutations.
57 *
58 * @ingroup selection
59 */
60typedef struct GhosttySelectionGestureImpl* GhosttySelectionGesture;
61​
62/**
63 * Opaque handle to reusable input data for selection gesture operations.
64 *
65 * Event options are set with ghostty_selection_gesture_event_set(). Individual
66 * gesture operations document which options are required or optional.
67 *
68 * @ingroup selection
69 */
70typedef struct GhosttySelectionGestureEventImpl* GhosttySelectionGestureEvent;
71​
72/**
73 * A range of terminal content between two cells.
74 *
75 * A selection is described by the two cells at its ends, `start` and `end`.
76 * Both ends are included in the selection. By default the selection is
77 * linear: it runs from `start` to `end` the way text flows, wrapping from
78 * the end of one row to the start of the next. Set `rectangle` to true to
79 * select a rectangular block instead, with `start` and `end` at opposite
80 * corners.
81 *
82 * The ends keep the direction the user selected in. If the user dragged
83 * up or to the left, `start` comes after `end` in the terminal. Don't
84 * assume `start` is the top-left end. Use
85 * ghostty_terminal_selection_ordered() when you need the ends in order.
86 *
87 * Both ends are untracked grid references, so a selection is only valid
88 * until the next call that changes the terminal. To keep a selection
89 * across changes, track its ends with GhosttyTrackedGridRef and build a new
90 * GhosttySelection from fresh snapshots when you need one.
91 *
92 * Start with a zero-initialized value. The example below selects the first
93 * ten cells of the second row and makes that the terminal's active
94 * selection.
95 *
96 * @code{.c}
97 * GhosttyPoint start_pt = {
98 * .tag = GHOSTTY_POINT_TAG_ACTIVE,
99 * .value = { .coordinate = { .x = 0, .y = 1 } },
100 * };
101 * GhosttyPoint end_pt = {
102 * .tag = GHOSTTY_POINT_TAG_ACTIVE,
103 * .value = { .coordinate = { .x = 9, .y = 1 } },
104 * };
105 *
106 * GhosttySelection selection = {0};
107 * ghostty_terminal_grid_ref(terminal, start_pt, &selection.start);
108 * ghostty_terminal_grid_ref(terminal, end_pt, &selection.end);
109 * ghostty_terminal_set(terminal, GHOSTTY_TERMINAL_OPT_SELECTION, &selection);
110 * @endcode
111 *
112 * The layout of this struct is frozen. It is 40 bytes on 64-bit targets
113 * and 20 bytes on 32-bit targets such as wasm32. If it ever needs to hold
114 * more, that will be a new type.
115 *
116 * @ingroup selection
117 */
118typedef struct {
119 /** The cell where the selection starts. It is included in the selection. */
120 GhosttyGridRef start;
121​
122 /** The cell where the selection ends. It is included in the selection. */
123 GhosttyGridRef end;
124​
125 /**
126 * Whether this is a rectangular selection. When true, `start` and `end`
127 * are opposite corners of a block of cells.
128 */
129 bool rectangle;
130} GhosttySelection;
131​
132/**
133 * A caller-provided buffer of selections.
134 *
135 * This follows the same conventions as GhosttyBuffer: ptr may be NULL with
136 * cap 0 to query the required capacity. APIs that fill this type set len to
137 * the number of entries written on GHOSTTY_SUCCESS, or to the required entry
138 * capacity on GHOSTTY_OUT_OF_SPACE.
139 *
140 * @ingroup selection
141 */
142typedef struct {
143 /** Destination buffer for selections. May be NULL when cap is 0 to query
144 * the required capacity. */
145 GhosttySelection* ptr;
146​
147 /** Capacity of ptr in entries. */
148 size_t cap;
149​
150 /** Entries written on success, or required entry capacity on
151 * GHOSTTY_OUT_OF_SPACE. */
152 size_t len;
153} GhosttySelectionBuffer;
154​
155/**
156 * Options for deriving a word selection from a terminal grid reference.
157 *
158 * This is a sized struct. Initialize it with GHOSTTY_INIT_SIZED(), then set
159 * the fields you need. See @ref sized-structs.
160 *
161 * If boundary_codepoints is NULL and boundary_codepoints_len is 0, Ghostty's
162 * default word-boundary codepoints are used. If boundary_codepoints_len is
163 * non-zero, boundary_codepoints must not be NULL.
164 *
165 * @ingroup selection
166 */
167typedef struct {
168 /** Size of this struct in bytes. Must be set to sizeof(GhosttyTerminalSelectWordOptions). */
169 size_t size;
170​
171 /** Grid reference under which to derive the word selection. */
172 GhosttyGridRef ref;
173​
174 /** Optional word-boundary codepoints as uint32_t scalar values. */
175 const uint32_t* boundary_codepoints;
176​
177 /** Number of entries in boundary_codepoints. */
178 size_t boundary_codepoints_len;
179} GhosttyTerminalSelectWordOptions;
180​
181/**
182 * Options for deriving the nearest word selection between two grid references.
183 *
184 * This is a sized struct. Initialize it with GHOSTTY_INIT_SIZED(), then set
185 * the fields you need. See @ref sized-structs.
186 *
187 * If boundary_codepoints is NULL and boundary_codepoints_len is 0, Ghostty's
188 * default word-boundary codepoints are used. If boundary_codepoints_len is
189 * non-zero, boundary_codepoints must not be NULL.
190 *
191 * @ingroup selection
192 */
193typedef struct {
194 /** Size of this struct in bytes. Must be set to sizeof(GhosttyTerminalSelectWordBetweenOptions). */
195 size_t size;
196​
197 /** Starting grid reference for the inclusive search range. */
198 GhosttyGridRef start;
199​
200 /** Ending grid reference for the inclusive search range. */
201 GhosttyGridRef end;
202​
203 /** Optional word-boundary codepoints as uint32_t scalar values. */
204 const uint32_t* boundary_codepoints;
205​
206 /** Number of entries in boundary_codepoints. */
207 size_t boundary_codepoints_len;
208} GhosttyTerminalSelectWordBetweenOptions;
209​
210/**
211 * Options for deriving a line selection from a terminal grid reference.
212 *
213 * This is a sized struct. Initialize it with GHOSTTY_INIT_SIZED(), then set
214 * the fields you need. See @ref sized-structs.
215 *
216 * If whitespace is NULL and whitespace_len is 0, Ghostty's default line-trim
217 * whitespace codepoints are used. If whitespace_len is non-zero, whitespace
218 * must not be NULL.
219 *
220 * @ingroup selection
221 */
222typedef struct {
223 /** Size of this struct in bytes. Must be set to sizeof(GhosttyTerminalSelectLineOptions). */
224 size_t size;
225​
226 /** Grid reference under which to derive the line selection. */
227 GhosttyGridRef ref;
228​
229 /** Optional codepoints to trim from the start and end of the line. */
230 const uint32_t* whitespace;
231​
232 /** Number of entries in whitespace. */
233 size_t whitespace_len;
234​
235 /** Whether semantic prompt state changes should bound the line selection. */
236 bool semantic_prompt_boundary;
237} GhosttyTerminalSelectLineOptions;
238​
239/**
240 * Ordering of a selection's endpoints in terminal coordinates.
241 *
242 * Mirrored orders are only produced by rectangular selections whose start
243 * and end endpoints are on opposite diagonal corners that are not simple
244 * top-left-to-bottom-right or bottom-right-to-top-left orderings.
245 *
246 * @ingroup selection
247 */
248typedef enum GHOSTTY_ENUM_TYPED {
249 /** Start is before end in top-left to bottom-right order. */
250 GHOSTTY_SELECTION_ORDER_FORWARD = 0,
251​
252 /** End is before start in top-left to bottom-right order. */
253 GHOSTTY_SELECTION_ORDER_REVERSE = 1,
254​
255 /** Rectangular selection from top-right to bottom-left. */
256 GHOSTTY_SELECTION_ORDER_MIRRORED_FORWARD = 2,
257​
258 /** Rectangular selection from bottom-left to top-right. */
259 GHOSTTY_SELECTION_ORDER_MIRRORED_REVERSE = 3,
260​
261 GHOSTTY_SELECTION_ORDER_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
262} GhosttySelectionOrder;
263​
264/**
265 * Operation used to adjust a selection endpoint.
266 *
267 * Adjustment mutates the selection's logical end endpoint, not whichever
268 * endpoint is visually bottom/right. This preserves keyboard and drag
269 * behavior for both forward and reversed selections.
270 *
271 * @ingroup selection
272 */
273typedef enum GHOSTTY_ENUM_TYPED {
274 /** Move left to the previous non-empty cell, wrapping upward. */
275 GHOSTTY_SELECTION_ADJUST_LEFT = 0,
276​
277 /** Move right to the next non-empty cell, wrapping downward. */
278 GHOSTTY_SELECTION_ADJUST_RIGHT = 1,
279​
280 /**
281 * Move up one row at the current column, or to the beginning of the
282 * line if already at the top.
283 */
284 GHOSTTY_SELECTION_ADJUST_UP = 2,
285​
286 /**
287 * Move down to the next non-blank row at the current column, or to the
288 * end of the line if none exists.
289 */
290 GHOSTTY_SELECTION_ADJUST_DOWN = 3,
291​
292 /** Move to the top-left cell of the screen. */
293 GHOSTTY_SELECTION_ADJUST_HOME = 4,
294​
295 /** Move to the right edge of the last non-blank row on the screen. */
296 GHOSTTY_SELECTION_ADJUST_END = 5,
297​
298 /**
299 * Move up by one terminal page height, or to home if that would move
300 * past the top.
301 */
302 GHOSTTY_SELECTION_ADJUST_PAGE_UP = 6,
303​
304 /**
305 * Move down by one terminal page height, or to end if that would move
306 * past the bottom.
307 */
308 GHOSTTY_SELECTION_ADJUST_PAGE_DOWN = 7,
309​
310 /** Move to the left edge of the current line. */
311 GHOSTTY_SELECTION_ADJUST_BEGINNING_OF_LINE = 8,
312​
313 /** Move to the right edge of the current line. */
314 GHOSTTY_SELECTION_ADJUST_END_OF_LINE = 9,
315​
316 GHOSTTY_SELECTION_ADJUST_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
317} GhosttySelectionAdjust;
318​
319/**
320 * Selection behavior chosen for a gesture's click sequence.
321 *
322 * @ingroup selection
323 */
324typedef enum GHOSTTY_ENUM_TYPED {
325 /** Cell-granular drag selection. */
326 GHOSTTY_SELECTION_GESTURE_BEHAVIOR_CELL = 0,
327​
328 /** Word selection on press and word-granular drag selection. */
329 GHOSTTY_SELECTION_GESTURE_BEHAVIOR_WORD = 1,
330​
331 /** Line selection on press and line-granular drag selection. */
332 GHOSTTY_SELECTION_GESTURE_BEHAVIOR_LINE = 2,
333​
334 /** Semantic command output selection on press and drag. */
335 GHOSTTY_SELECTION_GESTURE_BEHAVIOR_OUTPUT = 3,
336​
337 GHOSTTY_SELECTION_GESTURE_BEHAVIOR_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
338} GhosttySelectionGestureBehavior;
339​
340/**
341 * Selection behaviors for single-, double-, and triple-click gestures.
342 *
343 * @ingroup selection
344 */
345typedef struct {
346 /** Behavior for single-click selection gestures. */
347 GhosttySelectionGestureBehavior single_click;
348​
349 /** Behavior for double-click selection gestures. */
350 GhosttySelectionGestureBehavior double_click;
351​
352 /** Behavior for triple-click selection gestures. */
353 GhosttySelectionGestureBehavior triple_click;
354} GhosttySelectionGestureBehaviors;
355​
356/**
357 * Display geometry used to interpret selection gesture drag events.
358 *
359 * @ingroup selection
360 */
361typedef struct {
362 /** Number of columns in the rendered terminal grid. Must be non-zero. */
363 uint32_t columns;
364​
365 /** Width of one terminal cell in surface pixels. Must be non-zero. */
366 uint32_t cell_width;
367​
368 /** Left padding before the terminal grid begins in surface pixels. */
369 uint32_t padding_left;
370​
371 /** Height of the rendered terminal surface in surface pixels. Must be non-zero. */
372 uint32_t screen_height;
373} GhosttySelectionGestureGeometry;
374​
375/**
376 * Current autoscroll direction for an active selection drag gesture.
377 *
378 * @ingroup selection
379 */
380typedef enum GHOSTTY_ENUM_TYPED {
381 /** No selection autoscroll is requested. */
382 GHOSTTY_SELECTION_GESTURE_AUTOSCROLL_NONE = 0,
383​
384 /** Selection dragging should autoscroll the viewport upward. */
385 GHOSTTY_SELECTION_GESTURE_AUTOSCROLL_UP = 1,
386​
387 /** Selection dragging should autoscroll the viewport downward. */
388 GHOSTTY_SELECTION_GESTURE_AUTOSCROLL_DOWN = 2,
389​
390 GHOSTTY_SELECTION_GESTURE_AUTOSCROLL_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
391} GhosttySelectionGestureAutoscroll;
392​
393/**
394 * Data fields readable from a selection gesture with
395 * ghostty_selection_gesture_get().
396 *
397 * @ingroup selection
398 */
399typedef enum GHOSTTY_ENUM_TYPED {
400 /**
401 * Not a real data type. ghostty_selection_gesture_get() returns
402 * GHOSTTY_INVALID_VALUE for it. Its value is 0, so a zeroed or
403 * uninitialized variable is never mistaken for a real data type.
404 */
405 GHOSTTY_SELECTION_GESTURE_DATA_INVALID = 0,
406​
407 /** Current click count: uint8_t*. 0 means inactive. */
408 GHOSTTY_SELECTION_GESTURE_DATA_CLICK_COUNT = 1,
409​
410 /** Whether the current/last left-click gesture has dragged: bool*. */
411 GHOSTTY_SELECTION_GESTURE_DATA_DRAGGED = 2,
412​
413 /** Current autoscroll request: GhosttySelectionGestureAutoscroll*. */
414 GHOSTTY_SELECTION_GESTURE_DATA_AUTOSCROLL = 3,
415​
416 /** Current gesture behavior: GhosttySelectionGestureBehavior*. */
417 GHOSTTY_SELECTION_GESTURE_DATA_BEHAVIOR = 4,
418​
419 /**
420 * Current left-click anchor: GhosttyGridRef*.
421 *
422 * Returns GHOSTTY_NO_VALUE if there is no valid active anchor. On success,
423 * writes an untracked GhosttyGridRef snapshot with normal GhosttyGridRef
424 * lifetime rules.
425 */
426 GHOSTTY_SELECTION_GESTURE_DATA_ANCHOR = 5,
427​
428 GHOSTTY_SELECTION_GESTURE_DATA_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
429} GhosttySelectionGestureData;
430​
431/**
432 * Selection gesture event type.
433 *
434 * The event type is fixed when the event is created. Each event type documents
435 * which options are valid and which options are required by gesture operations.
436 *
437 * @ingroup selection
438 */
439typedef enum GHOSTTY_ENUM_TYPED {
440 /** Press event for ghostty_selection_gesture_event(). */
441 GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_PRESS = 0,
442​
443 /** Release event for ghostty_selection_gesture_event(). */
444 GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_RELEASE = 1,
445​
446 /** Drag event for ghostty_selection_gesture_event(). */
447 GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_DRAG = 2,
448​
449 /** Autoscroll tick event for ghostty_selection_gesture_event(). */
450 GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_AUTOSCROLL_TICK = 3,
451​
452 /** Deep press event for ghostty_selection_gesture_event(). */
453 GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_DEEP_PRESS = 4,
454​
455 GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
456} GhosttySelectionGestureEventType;
457​
458/**
459 * Options stored on a reusable selection gesture event.
460 *
461 * Passing NULL as the value to ghostty_selection_gesture_event_set() clears the
462 * corresponding option.
463 *
464 * @ingroup selection
465 */
466typedef enum GHOSTTY_ENUM_TYPED {
467 /**
468 * Not a real option. ghostty_selection_gesture_event_set() returns
469 * GHOSTTY_INVALID_VALUE for it. Its value is 0, so a zeroed or
470 * uninitialized variable is never mistaken for a real option.
471 */
472 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_INVALID = 0,
473​
474 /**
475 * Grid reference under the pointer: GhosttyGridRef*.
476 *
477 * Required for PRESS and DRAG events. Optional for RELEASE events; when unset
478 * or cleared, release records that the pointer did not map to a valid cell.
479 */
480 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_REF = 1,
481​
482 /**
483 * Surface-space pointer position: GhosttySurfacePosition*.
484 *
485 * Valid for PRESS, DRAG, and AUTOSCROLL_TICK.
486 */
487 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_POSITION = 2,
488​
489 /** Maximum repeat-click distance in pixels: double*. */
490 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_REPEAT_DISTANCE = 3,
491​
492 /**
493 * Optional monotonic event time in nanoseconds: uint64_t*.
494 *
495 * If unset, press treats the event as untimed and only single-click behavior
496 * is available.
497 */
498 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_TIME_NS = 4,
499​
500 /** Maximum interval between repeat clicks in nanoseconds: uint64_t*. */
501 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_REPEAT_INTERVAL_NS = 5,
502​
503 /**
504 * Word-boundary codepoints: GhosttyCodepoints*.
505 *
506 * The codepoints are copied into event-owned storage when set. If unset,
507 * operations that need word boundaries use Ghostty's defaults.
508 *
509 * Valid for PRESS, DRAG, AUTOSCROLL_TICK, and DEEP_PRESS.
510 */
511 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_WORD_BOUNDARY_CODEPOINTS = 6,
512​
513 /**
514 * Selection behavior table: GhosttySelectionGestureBehaviors*.
515 *
516 * If unset, press uses the default behavior table: cell, word, line.
517 */
518 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_BEHAVIORS = 7,
519​
520 /** Whether a drag or autoscroll tick should produce a rectangular selection: bool*. */
521 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_RECTANGLE = 8,
522​
523 /** Drag display geometry: GhosttySelectionGestureGeometry*. Required for DRAG and AUTOSCROLL_TICK. */
524 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_GEOMETRY = 9,
525​
526 /** Viewport coordinate for an autoscroll tick: GhosttyPointCoordinate*. Required for AUTOSCROLL_TICK. */
527 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_VIEWPORT = 10,
528​
529 GHOSTTY_SELECTION_GESTURE_EVENT_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
530} GhosttySelectionGestureEventOption;
531​
532/** @name Gesture Events
533 * @{
534 */
535​
536/**
537 * Create a reusable selection gesture event object.
538 *
539 * @param allocator Allocator, or NULL for the default allocator
540 * @param out_event Receives the created event handle
541 * @param type Event type. This is fixed for the lifetime of the event.
542 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if out_event is
543 * NULL or type is invalid, or GHOSTTY_OUT_OF_MEMORY if allocation fails
544 *
545 * @ingroup selection
546 */
547GHOSTTY_API GhosttyResult ghostty_selection_gesture_event_new(
548 const GhosttyAllocator* allocator,
549 GhosttySelectionGestureEvent* out_event,
550 GhosttySelectionGestureEventType type);
551​
552/**
553 * Free a selection gesture event object.
554 *
555 * Passing NULL is allowed and is a no-op.
556 *
557 * @param event Selection gesture event handle to free
558 *
559 * @ingroup selection
560 */
561GHOSTTY_API void ghostty_selection_gesture_event_free(
562 GhosttySelectionGestureEvent event);
563​
564/**
565 * Set or clear an option on a selection gesture event.
566 *
567 * The value type depends on option and is documented by
568 * GhosttySelectionGestureEventOption. Passing NULL for value clears the option.
569 *
570 * @param event Selection gesture event handle (NULL returns GHOSTTY_INVALID_VALUE)
571 * @param option Event option to set or clear
572 * @param value Pointer to the input value for option, or NULL to clear
573 * @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_MEMORY if copying
574 * event-owned data fails, GHOSTTY_INVALID_VALUE if `event` or `value`
575 * is invalid, or GHOSTTY_UNSUPPORTED if `option` is not known to this
576 * version of the library
577 *
578 * @ingroup selection
579 */
580GHOSTTY_API GhosttyResult ghostty_selection_gesture_event_set(
581 GhosttySelectionGestureEvent event,
582 GhosttySelectionGestureEventOption option,
583 const void* value);
584​
585/**
586 * Apply a selection gesture event and return the resulting selection snapshot.
587 *
588 * This dispatches to the gesture operation matching the event's fixed type.
589 * For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_PRESS, the event must have
590 * GHOSTTY_SELECTION_GESTURE_EVENT_OPT_REF set before calling this function.
591 * All other press options use their initialized defaults when unset or cleared.
592 *
593 * For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_RELEASE, only
594 * GHOSTTY_SELECTION_GESTURE_EVENT_OPT_REF is valid. It is optional; if unset or
595 * cleared, release records that the pointer did not map to a valid cell. Release
596 * events update gesture state but do not produce a selection, so this function
597 * returns GHOSTTY_NO_VALUE after applying them.
598 *
599 * For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_DRAG,
600 * GHOSTTY_SELECTION_GESTURE_EVENT_OPT_REF and
601 * GHOSTTY_SELECTION_GESTURE_EVENT_OPT_GEOMETRY are required. Position,
602 * rectangle, and word-boundary codepoints are optional and use initialized
603 * defaults when unset or cleared.
604 *
605 * For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_AUTOSCROLL_TICK,
606 * GHOSTTY_SELECTION_GESTURE_EVENT_OPT_VIEWPORT and
607 * GHOSTTY_SELECTION_GESTURE_EVENT_OPT_GEOMETRY are required. Position,
608 * rectangle, and word-boundary codepoints are optional and use initialized
609 * defaults when unset or cleared.
610 *
611 * An autoscroll tick scrolls the viewport exactly one row in the direction
612 * GHOSTTY_SELECTION_GESTURE_DATA_AUTOSCROLL reports, then continues the drag at
613 * the viewport coordinate. The gesture has no timer of its own. While that
614 * direction is up or down, send ticks from a timer you run. The interval is up
615 * to you, and ticking faster scrolls faster. A tick returns GHOSTTY_NO_VALUE
616 * and scrolls nothing if there's no direction to scroll in. If the cell of the
617 * original press is no longer on the active screen, it also resets the gesture
618 * and leaves any existing terminal selection alone, so stop your timer.
619 *
620 * For GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_DEEP_PRESS, only
621 * GHOSTTY_SELECTION_GESTURE_EVENT_OPT_WORD_BOUNDARY_CODEPOINTS is valid. It is
622 * optional and uses initialized defaults when unset or cleared.
623 *
624 * The returned selection is not installed as the terminal's current selection.
625 * It is a snapshot with the same lifetime rules as GhosttySelection.
626 *
627 * @param gesture Selection gesture handle (NULL returns GHOSTTY_INVALID_VALUE)
628 * @param terminal Terminal used to interpret and update gesture state
629 * @param event Selection gesture event handle (NULL returns GHOSTTY_INVALID_VALUE)
630 * @param[out] out_selection On success, receives the resulting selection. May
631 * be NULL to apply the event and discard the selection result.
632 * @return GHOSTTY_SUCCESS on success, GHOSTTY_NO_VALUE if the event does not
633 * currently produce a selection, GHOSTTY_OUT_OF_MEMORY if tracking
634 * gesture state fails, or GHOSTTY_INVALID_VALUE if gesture, terminal,
635 * event, or required event data is invalid
636 *
637 * @ingroup selection
638 */
639GHOSTTY_API GhosttyResult ghostty_selection_gesture_event(
640 GhosttySelectionGesture gesture,
641 GhosttyTerminal terminal,
642 GhosttySelectionGestureEvent event,
643 GhosttySelection* out_selection);
644​
645/** @} */
646​
647/** @name Gestures
648 * @{
649 */
650​
651/**
652 * Create a selection gesture object.
653 *
654 * The gesture stores mutable state for terminal text selection gestures. The
655 * gesture is not bound to a terminal at creation time; terminal-dependent APIs
656 * take the terminal explicitly.
657 *
658 * @param allocator Allocator, or NULL for the default allocator
659 * @param out_gesture Receives the created gesture handle
660 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if out_gesture is
661 * NULL, or GHOSTTY_OUT_OF_MEMORY if allocation fails
662 *
663 * @ingroup selection
664 */
665GHOSTTY_API GhosttyResult ghostty_selection_gesture_new(
666 const GhosttyAllocator* allocator,
667 GhosttySelectionGesture* out_gesture);
668​
669/**
670 * Free a selection gesture object.
671 *
672 * This releases any tracked terminal references owned by the gesture using the
673 * provided terminal, then frees the gesture object. Passing NULL for gesture is
674 * allowed and is a no-op.
675 *
676 * If the terminal is still alive, pass the terminal most recently used with the
677 * gesture so any tracked terminal references can be released correctly. If the
678 * terminal has already been freed, pass NULL for terminal; the terminal's page
679 * storage has already released the underlying tracked references, so the
680 * gesture wrapper can be safely discarded without touching the stale terminal
681 * state.
682 *
683 * @param gesture Selection gesture handle to free
684 * @param terminal Terminal used to release tracked gesture state, or NULL if
685 * the terminal has already been freed
686 *
687 * @ingroup selection
688 */
689GHOSTTY_API void ghostty_selection_gesture_free(
690 GhosttySelectionGesture gesture,
691 GhosttyTerminal terminal);
692​
693/**
694 * Reset any active selection gesture state.
695 *
696 * This cancels the active click sequence and releases any tracked terminal
697 * references owned by the gesture without freeing the gesture object.
698 * Passing NULL is allowed and is a no-op.
699 *
700 * @param gesture Selection gesture handle to reset
701 * @param terminal Terminal used to release tracked gesture state
702 *
703 * @ingroup selection
704 */
705GHOSTTY_API void ghostty_selection_gesture_reset(
706 GhosttySelectionGesture gesture,
707 GhosttyTerminal terminal);
708​
709/**
710 * Read data from a selection gesture.
711 *
712 * The type of value depends on data and is documented by
713 * GhosttySelectionGestureData. For GHOSTTY_SELECTION_GESTURE_DATA_ANCHOR,
714 * the returned GhosttyGridRef is an untracked snapshot with normal grid-ref
715 * lifetime rules.
716 *
717 * @param gesture Selection gesture handle (NULL returns GHOSTTY_INVALID_VALUE)
718 * @param terminal Terminal used to validate terminal-backed gesture state
719 * @param data Data field to read
720 * @param value Output pointer whose type depends on data
721 * @return GHOSTTY_SUCCESS on success, GHOSTTY_NO_VALUE if the requested data
722 * has no value, GHOSTTY_INVALID_VALUE if `gesture`, `terminal`, or
723 * `out` is invalid, or GHOSTTY_UNSUPPORTED if `data` is not known to
724 * this version of the library
725 *
726 * @ingroup selection
727 */
728GHOSTTY_API GhosttyResult ghostty_selection_gesture_get(
729 GhosttySelectionGesture gesture,
730 GhosttyTerminal terminal,
731 GhosttySelectionGestureData data,
732 void* value);
733​
734/**
735 * Read multiple data fields from a selection gesture in a single call.
736 *
737 * Use this when each call into the library is expensive, for example from
738 * WebAssembly or from another language through its foreign function
739 * interface. It reads every key in one call. From C in the same process it
740 * costs the same as calling ghostty_selection_gesture_get() once per key, so
741 * use whichever reads better.
742 *
743 * Each entry in values must point to storage of the type documented by
744 * the corresponding GhosttySelectionGestureData key.
745 *
746 * If any individual read fails, the function returns that error and writes the
747 * index of the failing key to out_written when out_written is non-NULL. On
748 * success, out_written receives count when non-NULL.
749 *
750 * @param gesture Selection gesture handle (NULL returns GHOSTTY_INVALID_VALUE)
751 * @param terminal Terminal used to validate terminal-backed gesture state
752 * @param count Number of data fields to read
753 * @param keys Data fields to read (NULL returns GHOSTTY_INVALID_VALUE)
754 * @param values Output pointers corresponding to keys (NULL returns GHOSTTY_INVALID_VALUE)
755 * @param out_written Optional number of fields read, or failing index on error
756 * @return GHOSTTY_SUCCESS if every key succeeds. Otherwise the result of the
757 * first key that failed, for example GHOSTTY_UNSUPPORTED for a key
758 * that is not known to this version of the library.
759 *
760 * @ingroup selection
761 */
762GHOSTTY_API GhosttyResult ghostty_selection_gesture_get_multi(
763 GhosttySelectionGesture gesture,
764 GhosttyTerminal terminal,
765 size_t count,
766 const GhosttySelectionGestureData* keys,
767 void** values,
768 size_t* out_written);
769​
770/** @} */
771​
772/** @name Selecting
773 * @{
774 */
775​
776/**
777 * Derive a word selection snapshot from a terminal grid reference.
778 *
779 * The returned selection is not installed as the terminal's current
780 * selection. It is a snapshot with the same lifetime rules as GhosttySelection.
781 *
782 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
783 * @param options Word-selection options
784 * @param[out] out_selection On success, receives the derived selection
785 * @return GHOSTTY_SUCCESS on success, GHOSTTY_NO_VALUE if the valid ref has
786 * no selectable word content, or GHOSTTY_INVALID_VALUE if the
787 * terminal, options, ref, codepoint pointer, or output pointer are
788 * invalid.
789 *
790 * @ingroup selection
791 */
792GHOSTTY_API GhosttyResult ghostty_terminal_select_word(
793 GhosttyTerminal terminal,
794 const GhosttyTerminalSelectWordOptions* options,
795 GhosttySelection* out_selection);
796​
797/**
798 * Derive the nearest word selection snapshot between two terminal grid refs.
799 *
800 * Starting at options->start, this searches toward options->end (inclusive)
801 * and returns the first selectable word found using Ghostty's word-selection
802 * rules.
803 *
804 * This is useful for implementing double-click-and-drag selection in a UI. If
805 * a user double-clicks one word and drags across spaces or punctuation toward
806 * another word, selecting only the word directly under the current pointer can
807 * flicker or collapse when the pointer is between words. Instead, ask for the
808 * nearest word between the original click and the drag point, ask again in the
809 * reverse direction, and combine the two word bounds into the drag selection.
810 *
811 * @snippet c-vt-selection/src/main.c selection-word-between
812 *
813 * Full program: @ref c-vt-selection/src/main.c
814 *
815 * The returned selection is not installed as the terminal's current
816 * selection. It is a snapshot with the same lifetime rules as GhosttySelection.
817 *
818 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
819 * @param options Word-between-selection options
820 * @param[out] out_selection On success, receives the derived selection
821 * @return GHOSTTY_SUCCESS on success, GHOSTTY_NO_VALUE if there is no
822 * selectable word content between the valid refs, or
823 * GHOSTTY_INVALID_VALUE if the terminal, options, refs, codepoint
824 * pointer, or output pointer are invalid.
825 *
826 * @ingroup selection
827 */
828GHOSTTY_API GhosttyResult ghostty_terminal_select_word_between(
829 GhosttyTerminal terminal,
830 const GhosttyTerminalSelectWordBetweenOptions* options,
831 GhosttySelection* out_selection);
832​
833/**
834 * Derive a line selection snapshot from a terminal grid reference.
835 *
836 * The returned selection is not installed as the terminal's current
837 * selection. It is a snapshot with the same lifetime rules as GhosttySelection.
838 *
839 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
840 * @param options Line-selection options
841 * @param[out] out_selection On success, receives the derived selection
842 * @return GHOSTTY_SUCCESS on success, GHOSTTY_NO_VALUE if the valid ref has
843 * no selectable line content, or GHOSTTY_INVALID_VALUE if the
844 * terminal, options, ref, codepoint pointer, or output pointer are
845 * invalid.
846 *
847 * @ingroup selection
848 */
849GHOSTTY_API GhosttyResult ghostty_terminal_select_line(
850 GhosttyTerminal terminal,
851 const GhosttyTerminalSelectLineOptions* options,
852 GhosttySelection* out_selection);
853​
854/**
855 * Derive a selection snapshot covering all selectable terminal content.
856 *
857 * The returned selection is not installed as the terminal's current
858 * selection. It is a snapshot with the same lifetime rules as GhosttySelection.
859 *
860 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
861 * @param[out] out_selection On success, receives the derived selection
862 * @return GHOSTTY_SUCCESS on success, GHOSTTY_NO_VALUE if there is no
863 * selectable content, or GHOSTTY_INVALID_VALUE if the terminal or
864 * output pointer is invalid.
865 *
866 * @ingroup selection
867 */
868GHOSTTY_API GhosttyResult ghostty_terminal_select_all(
869 GhosttyTerminal terminal,
870 GhosttySelection* out_selection);
871​
872/**
873 * Derive a command-output selection snapshot from a terminal grid reference.
874 *
875 * The returned selection is not installed as the terminal's current
876 * selection. It is a snapshot with the same lifetime rules as GhosttySelection.
877 *
878 * @code{.c}
879 * GhosttyGridRef ref = {0};
880 * ghostty_terminal_grid_ref(terminal, point, &ref);
881 *
882 * GhosttySelection selection = {0};
883 * if (ghostty_terminal_select_output(terminal, &ref, &selection) == GHOSTTY_SUCCESS) {
884 * ghostty_terminal_set(terminal, GHOSTTY_TERMINAL_OPT_SELECTION, &selection);
885 * }
886 * @endcode
887 *
888 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
889 * @param ref Grid reference within command output to select (NULL returns
890 * GHOSTTY_INVALID_VALUE)
891 * @param[out] out_selection On success, receives the derived selection
892 * @return GHOSTTY_SUCCESS on success, GHOSTTY_NO_VALUE if the valid ref is
893 * not selectable command output, or GHOSTTY_INVALID_VALUE if the
894 * terminal, ref, or output pointer is invalid.
895 *
896 * @ingroup selection
897 */
898GHOSTTY_API GhosttyResult ghostty_terminal_select_output(
899 GhosttyTerminal terminal,
900 const GhosttyGridRef* ref,
901 GhosttySelection* out_selection);
902​
903/** @} */
904​
905/** @name Working with Selections
906 * @{
907 */
908​
909/**
910 * Adjust a selection snapshot using terminal selection semantics.
911 *
912 * This mutates your GhosttySelection in place. The logical end
913 * endpoint is always moved, regardless of whether the selection is forward or
914 * reversed visually. The input selection remains a snapshot: after adjustment,
915 * call ghostty_terminal_set() with GHOSTTY_TERMINAL_OPT_SELECTION to install it
916 * as the terminal-owned selection if desired.
917 *
918 * The selection's start and end grid refs must both be valid untracked
919 * snapshots for the given terminal's currently active screen. In practice,
920 * they must come from that terminal and screen, and no mutating terminal call
921 * may have occurred since the refs were produced or reconstructed from
922 * tracked refs. Passing refs from another terminal, another screen, or stale
923 * refs violates this precondition.
924 *
925 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
926 * @param selection Selection snapshot to adjust in place
927 * @param adjustment The adjustment operation to apply
928 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the terminal,
929 * selection, or adjustment are invalid. Selection reference validity
930 * is a precondition and is not checked.
931 *
932 * @ingroup selection
933 */
934GHOSTTY_API GhosttyResult ghostty_terminal_selection_adjust(
935 GhosttyTerminal terminal,
936 GhosttySelection* selection,
937 GhosttySelectionAdjust adjustment);
938​
939/**
940 * Get the current endpoint ordering of a selection snapshot.
941 *
942 * The selection's start and end grid refs must both be valid untracked
943 * snapshots for the given terminal's currently active screen. In practice,
944 * they must come from that terminal and screen, and no mutating terminal call
945 * may have occurred since the refs were produced or reconstructed from
946 * tracked refs. Passing refs from another terminal, another screen, or stale
947 * refs violates this precondition.
948 *
949 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
950 * @param selection Selection snapshot to inspect
951 * @param[out] out_order On success, receives the selection order
952 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the terminal,
953 * selection, or output pointer are invalid. Selection reference
954 * validity is a precondition and is not checked.
955 *
956 * @ingroup selection
957 */
958GHOSTTY_API GhosttyResult ghostty_terminal_selection_order(
959 GhosttyTerminal terminal,
960 const GhosttySelection* selection,
961 GhosttySelectionOrder* out_order);
962​
963/**
964 * Return a selection snapshot with endpoints ordered as requested.
965 *
966 * Use GHOSTTY_SELECTION_ORDER_FORWARD to get top-left to bottom-right bounds,
967 * and GHOSTTY_SELECTION_ORDER_REVERSE to get bottom-right to top-left bounds.
968 * Mirrored desired orders are accepted but normalized the same as forward.
969 * The output selection is a fresh untracked snapshot and is not installed as
970 * the terminal's current selection.
971 *
972 * The selection's start and end grid refs must both be valid untracked
973 * snapshots for the given terminal's currently active screen. In practice,
974 * they must come from that terminal and screen, and no mutating terminal call
975 * may have occurred since the refs were produced or reconstructed from
976 * tracked refs. Passing refs from another terminal, another screen, or stale
977 * refs violates this precondition.
978 *
979 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
980 * @param selection Selection snapshot to order
981 * @param desired Desired endpoint order
982 * @param[out] out_selection On success, receives the ordered selection
983 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the terminal,
984 * selection, desired order, or output pointer are invalid. Selection
985 * reference validity is a precondition and is not checked.
986 *
987 * @ingroup selection
988 */
989GHOSTTY_API GhosttyResult ghostty_terminal_selection_ordered(
990 GhosttyTerminal terminal,
991 const GhosttySelection* selection,
992 GhosttySelectionOrder desired,
993 GhosttySelection* out_selection);
994​
995/**
996 * Test whether a terminal point is inside a selection snapshot.
997 *
998 * This uses the same selection semantics as the terminal, including
999 * rectangular/block selections and linear selections spanning multiple rows.
1001 * The selection's start and end grid refs must both be valid untracked
1002 * snapshots for the given terminal's currently active screen. In practice,
1003 * they must come from that terminal and screen, and no mutating terminal call
1004 * may have occurred since the refs were produced or reconstructed from
1005 * tracked refs. Passing refs from another terminal, another screen, or stale
1006 * refs violates this precondition.
1008 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
1009 * @param selection Selection snapshot to inspect
1010 * @param point Point to test for containment
1011 * @param[out] out_contains On success, receives whether point is inside selection
1012 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the terminal,
1013 * selection, point, or output pointer are invalid. Selection reference
1014 * validity is a precondition and is not checked.
1016 * @ingroup selection
1017 */
1018GHOSTTY_API GhosttyResult ghostty_terminal_selection_contains(
1019 GhosttyTerminal terminal,
1020 const GhosttySelection* selection,
1021 GhosttyPoint point,
1022 bool* out_contains);
1023​
1024/**
1025 * Test whether two selection snapshots are equal.
1027 * Equality uses the terminal's internal selection semantics: both endpoint
1028 * pins must match and both selections must have the same rectangular/block
1029 * state. This avoids requiring callers to compare raw GhosttyGridRef internals.
1031 * Both selections' start and end grid refs must be valid untracked snapshots
1032 * for the given terminal's currently active screen. In practice, they must
1033 * come from that terminal and screen, and no mutating terminal call may have
1034 * occurred since the refs were produced or reconstructed from tracked refs.
1035 * Passing refs from another terminal, another screen, or stale refs violates
1036 * this precondition.
1038 * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
1039 * @param a First selection snapshot to compare
1040 * @param b Second selection snapshot to compare
1041 * @param[out] out_equal On success, receives whether the selections are equal
1042 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the terminal,
1043 * selections, or output pointer are invalid. Selection reference
1044 * validity is a precondition and is not checked.
1046 * @ingroup selection
1047 */
1048GHOSTTY_API GhosttyResult ghostty_terminal_selection_equal(
1049 GhosttyTerminal terminal,
1050 const GhosttySelection* a,
1051 const GhosttySelection* b,
1052 bool* out_equal);
1053​
1054/** @} */
1055​
1056/** @} */
1057​
1058#ifdef __cplusplus
1060#endif
1061​
1062#endif /* GHOSTTY_VT_SELECTION_H */