Correctly report start and end in extracted text

* src/androidterm.c (struct android_get_extracted_text_context):
New field `start_offset' and `end_offset'.  Delete `offset'.
(android_get_extracted_text, android_build_extracted_text):
Replace `offset' with new args `start_offset' and `end_offset'.
(NATIVE_NAME): Set `start_offset' and `end_offset'.
(android_update_selection): Likewise.
* src/textconv.c (get_extracted_text): Likewise.
* src/textconv.h: Update prototypes.
This commit is contained in:
Po Lu 2023-06-01 08:29:48 +08:00
parent a964116008
commit ce238de2b1
3 changed files with 38 additions and 21 deletions

View file

@ -5196,7 +5196,7 @@ struct android_get_extracted_text_context
ptrdiff_t length, bytes;
/* Offsets into that text. */
ptrdiff_t start, offset;
ptrdiff_t start, start_offset, end_offset;
/* The window. */
android_window window;
@ -5222,8 +5222,9 @@ android_get_extracted_text (void *data)
/* Now get the extracted text. */
request->text
= get_extracted_text (f, min (request->hint_max_chars, 600),
&request->start, &request->offset,
&request->length, &request->bytes);
&request->start, &request->start_offset,
&request->end_offset, &request->length,
&request->bytes);
/* See if request->flags & GET_EXTRACTED_TEXT_MONITOR. If so, then
the input method has asked to monitor changes to the extracted
@ -5268,8 +5269,9 @@ struct android_extracted_text_class text_class;
/* Return an ExtractedText object corresponding to the extracted text
TEXT. START is a character position describing the offset of the
first character in TEXT. OFFSET is the offset of point relative to
START.
first character in TEXT. START_OFFSET is the offset of the lesser
of point or mark relative to START, and END_OFFSET is that of the
greater of point or mark relative to START.
Assume that request_class and text_class have already been
initialized.
@ -5279,7 +5281,8 @@ struct android_extracted_text_class text_class;
static jobject
android_build_extracted_text (jstring text, ptrdiff_t start,
ptrdiff_t offset)
ptrdiff_t start_offset,
ptrdiff_t end_offset)
{
JNIEnv *env;
jobject object;
@ -5299,9 +5302,9 @@ android_build_extracted_text (jstring text, ptrdiff_t start,
(*env)->SetIntField (env, object, text_class.partial_start_offset, -1);
(*env)->SetIntField (env, object, text_class.partial_end_offset, -1);
(*env)->SetIntField (env, object, text_class.selection_start,
min (offset, TYPE_MAXIMUM (jint)));
min (start_offset, TYPE_MAXIMUM (jint)));
(*env)->SetIntField (env, object, text_class.selection_end,
min (offset, TYPE_MAXIMUM (jint)));
min (end_offset, TYPE_MAXIMUM (jint)));
/* Subtract 1 from start: point indices in Emacs start from 1, but
Android expects 0. */
@ -5404,9 +5407,9 @@ NATIVE_NAME (getExtractedText) (JNIEnv *env, jobject ignored_object,
(*env)->SetIntField (env, object, text_class.partial_start_offset, -1);
(*env)->SetIntField (env, object, text_class.partial_end_offset, -1);
(*env)->SetIntField (env, object, text_class.selection_start,
min (context.offset, TYPE_MAXIMUM (jint)));
min (context.start_offset, TYPE_MAXIMUM (jint)));
(*env)->SetIntField (env, object, text_class.selection_end,
min (context.offset, TYPE_MAXIMUM (jint)));
min (context.end_offset, TYPE_MAXIMUM (jint)));
/* Subtract 1 from start: point indices in Emacs start from 1, but
Android expects 0. */
@ -5507,7 +5510,8 @@ NATIVE_NAME (requestCursorUpdates) (JNIEnv *env, jobject object,
static void
android_update_selection (struct frame *f, struct window *w)
{
ptrdiff_t start, end, point, mark, offset, length, bytes;
ptrdiff_t start, end, point, mark, start_offset, end_offset;
ptrdiff_t length, bytes;
struct buffer *b;
int hint, token;
char *text;
@ -5560,7 +5564,8 @@ android_update_selection (struct frame *f, struct window *w)
hint = FRAME_ANDROID_OUTPUT (f)->extracted_text_hint;
token = FRAME_ANDROID_OUTPUT (f)->extracted_text_token;
text = get_extracted_text (f, min (hint, 600), &start,
&offset, &length, &bytes);
&start_offset, &end_offset,
&length, &bytes);
if (text)
{
@ -5572,7 +5577,8 @@ android_update_selection (struct frame *f, struct window *w)
/* Make extracted text out of that string. */
extracted = android_build_extracted_text (string, start,
offset);
start_offset,
end_offset);
android_exception_check_1 (string);
ANDROID_DELETE_LOCAL_REF (string);

View file

@ -1513,21 +1513,23 @@ request_point_update (struct frame *f, unsigned long counter)
that the mark is active.
Set *N to the actual number of characters returned, *START_RETURN
to the position of the first character returned, *OFFSET to the
offset of point within that text, *LENGTH to the actual number of
characters returned, and *BYTES to the actual number of bytes
returned.
to the position of the first character returned, *START_OFFSET to
the offset of the lesser of mark and point within that text,
*END_OFFSET to the greater of mark and point within that text, and
*LENGTH to the actual number of characters returned, and *BYTES to
the actual number of bytes returned.
Value is NULL upon failure, and a malloced string upon success. */
char *
get_extracted_text (struct frame *f, ptrdiff_t n,
ptrdiff_t *start_return,
ptrdiff_t *offset, ptrdiff_t *length,
ptrdiff_t *start_offset,
ptrdiff_t *end_offset, ptrdiff_t *length,
ptrdiff_t *bytes)
{
specpdl_ref count;
ptrdiff_t start, end, start_byte, end_byte;
ptrdiff_t start, end, start_byte, end_byte, mark;
char *buffer;
if (!WINDOW_LIVE_P (f->old_selected_window))
@ -1595,9 +1597,17 @@ get_extracted_text (struct frame *f, ptrdiff_t n,
copy_buffer (start, start_byte, end, end_byte,
buffer);
/* Get the mark. If it's not active, use PT. */
mark = get_mark ();
if (mark == -1)
mark = PT;
/* Return the offsets. */
*start_return = start;
*offset = PT - start;
*start_offset = min (mark - start, PT - start);
*end_offset = max (mark - start, PT - start);
*length = end - start;
*bytes = end_byte - start_byte;

View file

@ -139,7 +139,8 @@ extern void delete_surrounding_text (struct frame *, ptrdiff_t,
ptrdiff_t, unsigned long);
extern void request_point_update (struct frame *, unsigned long);
extern char *get_extracted_text (struct frame *, ptrdiff_t, ptrdiff_t *,
ptrdiff_t *, ptrdiff_t *, ptrdiff_t *);
ptrdiff_t *, ptrdiff_t *, ptrdiff_t *,
ptrdiff_t *);
extern bool conversion_disabled_p (void);
extern void register_textconv_interface (struct textconv_interface *);