From 0998ab3ade7caf2b163cd99a44b8a3f17be763d5 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Sun, 19 Feb 2023 19:56:51 +0800 Subject: [PATCH] Report both sides of the region to the input method upon setup * java/org/gnu/emacs/EmacsNative.java (getSelection): Return array of ints. * java/org/gnu/emacs/EmacsView.java (onCreateInputConnection): Adjust accordingly. * src/androidterm.c (struct android_get_selection_context): New field `mark'. (android_get_selection): Set the mark field as appropriate. (getSelection): Adjust accordingly. --- java/org/gnu/emacs/EmacsNative.java | 2 +- java/org/gnu/emacs/EmacsView.java | 9 ++++--- src/androidterm.c | 38 ++++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/java/org/gnu/emacs/EmacsNative.java b/java/org/gnu/emacs/EmacsNative.java index aaac9446510..ef1a0e75a5c 100644 --- a/java/org/gnu/emacs/EmacsNative.java +++ b/java/org/gnu/emacs/EmacsNative.java @@ -205,7 +205,7 @@ public class EmacsNative /* Return the current value of the selection, or -1 upon failure. */ - public static native int getSelection (short window); + public static native int[] getSelection (short window); static { diff --git a/java/org/gnu/emacs/EmacsView.java b/java/org/gnu/emacs/EmacsView.java index 5ea8b0dcc0e..89f526853b2 100644 --- a/java/org/gnu/emacs/EmacsView.java +++ b/java/org/gnu/emacs/EmacsView.java @@ -555,7 +555,8 @@ public class EmacsView extends ViewGroup public InputConnection onCreateInputConnection (EditorInfo info) { - int selection, mode; + int mode; + int[] selection; /* Figure out what kind of IME behavior Emacs wants. */ mode = getICMode (); @@ -575,7 +576,7 @@ public class EmacsView extends ViewGroup /* If this fails or ANDROID_IC_MODE_NULL was requested, then don't initialize the input connection. */ - if (selection == -1 || mode == EmacsService.IC_MODE_NULL) + if (mode == EmacsService.IC_MODE_NULL || selection == null) { info.inputType = InputType.TYPE_NULL; return null; @@ -585,8 +586,8 @@ public class EmacsView extends ViewGroup info.imeOptions |= EditorInfo.IME_ACTION_DONE; /* Set the initial selection fields. */ - info.initialSelStart = selection; - info.initialSelEnd = selection; + info.initialSelStart = selection[0]; + info.initialSelEnd = selection[1]; /* Create the input connection if necessary. */ diff --git a/src/androidterm.c b/src/androidterm.c index 62a8d5d12b3..0701ef44436 100644 --- a/src/androidterm.c +++ b/src/androidterm.c @@ -4831,8 +4831,8 @@ struct android_get_selection_context android_window window; /* The position of the window's point when it was last - redisplayed. */ - ptrdiff_t point; + redisplayed, and its last mark if active. */ + ptrdiff_t point, mark; }; /* Function run on the main thread by `getSelection'. @@ -4844,6 +4844,7 @@ android_get_selection (void *data) struct android_get_selection_context *context; struct frame *f; struct window *w; + struct buffer *b; context = data; @@ -4860,25 +4861,50 @@ android_get_selection (void *data) rather important to keep the input method consistent with the contents of the display. */ context->point = w->ephemeral_last_point; + + /* Default context->mark to w->last_point too. */ + context->mark = context->point; + + /* If the mark is active, then set it properly. */ + b = XBUFFER (w->contents); + if (!NILP (BVAR (b, mark_active)) && w->last_mark != -1) + context->mark = w->last_mark; } } -JNIEXPORT jint JNICALL +JNIEXPORT jintArray JNICALL NATIVE_NAME (getSelection) (JNIEnv *env, jobject object, jshort window) { struct android_get_selection_context context; + jintArray array; + jint contents[2]; context.window = window; android_sync_edit (); if (android_run_in_emacs_thread (android_get_selection, &context)) - return -1; + return NULL; if (context.point == -1) - return -1; + return NULL; + + /* Wraparound actually makes more sense than truncation; at least + editing will sort of work. */ + contents[0] = (unsigned int) min (context.point, + context.mark); + contents[1] = (unsigned int) max (context.point, + context.mark); + + /* Now create the array. */ + array = (*env)->NewIntArray (env, 2); + + if (!array) + return NULL; - return min (context.point, TYPE_MAXIMUM (jint)); + /* Set its contents. */ + (*env)->SetIntArrayRegion (env, array, 0, 2, contents); + return array; } JNIEXPORT void JNICALL -- 2.39.5