@@ -171,6 +171,16 @@ std::vector<uint8_t> DrainResponses(Session* session) {
171171 return responses;
172172}
173173
174+ bool ViewportGridRef (Session* session, jint x, jint y, GhosttyGridRef* out) {
175+ *out = GhosttyGridRef{};
176+ out->size = sizeof (*out);
177+ GhosttyPoint point{};
178+ point.tag = GHOSTTY_POINT_TAG_VIEWPORT ;
179+ point.value .coordinate .x = static_cast <uint16_t >(std::max<jint>(x, 0 ));
180+ point.value .coordinate .y = static_cast <uint32_t >(std::max<jint>(y, 0 ));
181+ return ghostty_terminal_grid_ref (session->terminal , point, out) == GHOSTTY_SUCCESS ;
182+ }
183+
174184uint16_t StyleFlags (const GhosttyStyle& style, bool selected) {
175185 uint16_t flags = 0 ;
176186 if (style.bold ) flags |= kBold ;
@@ -272,6 +282,96 @@ Java_expo_modules_t3terminal_GhosttyBridge_nativeSetTheme(
272282 ApplyTheme (session, foreground, background, cursor, env, palette);
273283}
274284
285+ extern " C" JNIEXPORT jboolean JNICALL
286+ Java_expo_modules_t3terminal_GhosttyBridge_nativeSelectWordAt (JNIEnv*, jclass,
287+ jlong handle, jint x,
288+ jint y) {
289+ auto * session = FromHandle (handle);
290+ if (session == nullptr ) return JNI_FALSE ;
291+ std::lock_guard<std::mutex> lock (session->mutex );
292+ GhosttyTerminalSelectWordOptions options{};
293+ options.size = sizeof (options);
294+ if (!ViewportGridRef (session, x, y, &options.ref )) return JNI_FALSE ;
295+ GhosttySelection selection{};
296+ selection.size = sizeof (selection);
297+ if (ghostty_terminal_select_word (session->terminal , &options, &selection) !=
298+ GHOSTTY_SUCCESS ) {
299+ return JNI_FALSE ;
300+ }
301+ return ghostty_terminal_set (session->terminal , GHOSTTY_TERMINAL_OPT_SELECTION ,
302+ &selection) == GHOSTTY_SUCCESS
303+ ? JNI_TRUE
304+ : JNI_FALSE ;
305+ }
306+
307+ extern " C" JNIEXPORT void JNICALL
308+ Java_expo_modules_t3terminal_GhosttyBridge_nativeExtendSelection (
309+ JNIEnv*, jclass, jlong handle, jint anchor_x, jint anchor_y, jint x, jint y) {
310+ auto * session = FromHandle (handle);
311+ if (session == nullptr ) return ;
312+ std::lock_guard<std::mutex> lock (session->mutex );
313+ GhosttySelection selection{};
314+ selection.size = sizeof (selection);
315+ if (!ViewportGridRef (session, anchor_x, anchor_y, &selection.start )) return ;
316+ if (!ViewportGridRef (session, x, y, &selection.end )) return ;
317+ ghostty_terminal_set (session->terminal , GHOSTTY_TERMINAL_OPT_SELECTION , &selection);
318+ }
319+
320+ extern " C" JNIEXPORT jboolean JNICALL
321+ Java_expo_modules_t3terminal_GhosttyBridge_nativeSelectAll (JNIEnv*, jclass,
322+ jlong handle) {
323+ auto * session = FromHandle (handle);
324+ if (session == nullptr ) return JNI_FALSE ;
325+ std::lock_guard<std::mutex> lock (session->mutex );
326+ GhosttySelection selection{};
327+ selection.size = sizeof (selection);
328+ if (ghostty_terminal_select_all (session->terminal , &selection) != GHOSTTY_SUCCESS ) {
329+ return JNI_FALSE ;
330+ }
331+ return ghostty_terminal_set (session->terminal , GHOSTTY_TERMINAL_OPT_SELECTION ,
332+ &selection) == GHOSTTY_SUCCESS
333+ ? JNI_TRUE
334+ : JNI_FALSE ;
335+ }
336+
337+ extern " C" JNIEXPORT void JNICALL
338+ Java_expo_modules_t3terminal_GhosttyBridge_nativeClearSelection (JNIEnv*, jclass,
339+ jlong handle) {
340+ auto * session = FromHandle (handle);
341+ if (session == nullptr ) return ;
342+ std::lock_guard<std::mutex> lock (session->mutex );
343+ ghostty_terminal_set (session->terminal , GHOSTTY_TERMINAL_OPT_SELECTION , nullptr );
344+ }
345+
346+ // Returns the active selection as UTF-8 bytes (soft-wrapped lines unwrapped,
347+ // trailing whitespace trimmed), or null when there is no selection.
348+ extern " C" JNIEXPORT jbyteArray JNICALL
349+ Java_expo_modules_t3terminal_GhosttyBridge_nativeGetSelectionText (JNIEnv* env, jclass,
350+ jlong handle) {
351+ auto * session = FromHandle (handle);
352+ if (session == nullptr ) return nullptr ;
353+ std::lock_guard<std::mutex> lock (session->mutex );
354+ GhosttyTerminalSelectionFormatOptions options{};
355+ options.size = sizeof (options);
356+ options.emit = GHOSTTY_FORMATTER_FORMAT_PLAIN ;
357+ options.unwrap = true ;
358+ options.trim = true ;
359+ uint8_t * bytes = nullptr ;
360+ size_t len = 0 ;
361+ if (ghostty_terminal_selection_format_alloc (session->terminal , nullptr , options,
362+ &bytes, &len) != GHOSTTY_SUCCESS ||
363+ bytes == nullptr ) {
364+ return nullptr ;
365+ }
366+ auto result = env->NewByteArray (static_cast <jsize>(len));
367+ if (result != nullptr && len > 0 ) {
368+ env->SetByteArrayRegion (result, 0 , static_cast <jsize>(len),
369+ reinterpret_cast <const jbyte*>(bytes));
370+ }
371+ ghostty_free (nullptr , bytes, len);
372+ return result;
373+ }
374+
275375extern " C" JNIEXPORT jbyteArray JNICALL
276376Java_expo_modules_t3terminal_GhosttyBridge_nativeSnapshot (JNIEnv* env, jclass,
277377 jlong handle) {
0 commit comments