From 14562b45bd81334064b19ed91f02e11cd46aaf56 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Sat, 19 Feb 2022 20:59:12 +0800 Subject: [PATCH] Avoid consing extra string when processing GTK native input * src/gtkutil.c (xg_im_context_commit): Use `decode_string_utf8' to decode input text. * src/keyboard.c (kbd_buffer_get_event_1): If coding system is Qt, simply return the string without decoding it. * src/termhooks.h (enum event_kind): Document meaning of Qt as coding system in a multibyte keystroke event's string argument. --- src/gtkutil.c | 16 +++++++++++++--- src/keyboard.c | 3 +++ src/termhooks.h | 4 +++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/gtkutil.c b/src/gtkutil.c index 27aa28b8902..158c29272f5 100644 --- a/src/gtkutil.c +++ b/src/gtkutil.c @@ -6097,11 +6097,21 @@ xg_im_context_commit (GtkIMContext *imc, gchar *str, struct input_event ie; EVENT_INIT (ie); + /* This used to use g_utf8_to_ucs4_fast, which led to bad results + when STR wasn't actually a UTF-8 string, which some input method + modules commit. */ + ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT; - ie.arg = build_unibyte_string (str); + ie.arg = decode_string_utf_8 (Qnil, str, strlen (str), + Qnil, false, Qnil, Qnil); + + /* STR is invalid and not really encoded in UTF-8. */ + if (NILP (ie.arg)) + ie.arg = build_unibyte_string (str); - Fput_text_property (make_fixnum (0), make_fixnum (strlen (str)), - Qcoding, Qutf_8_unix, ie.arg); + Fput_text_property (make_fixnum (0), + make_fixnum (SCHARS (ie.arg)), + Qcoding, Qt, ie.arg); XSETFRAME (ie.frame_or_window, f); ie.modifiers = 0; diff --git a/src/keyboard.c b/src/keyboard.c index 0747ab48205..2aff0f1011d 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -3841,6 +3841,9 @@ kbd_buffer_get_event_1 (Lisp_Object arg) Lisp_Object coding_system = Fget_text_property (make_fixnum (0), Qcoding, arg); + if (EQ (coding_system, Qt)) + return arg; + return code_convert_string (arg, (!NILP (coding_system) ? coding_system : Vlocale_coding_system), diff --git a/src/termhooks.h b/src/termhooks.h index 0a9ab61afab..b7696fed4f8 100644 --- a/src/termhooks.h +++ b/src/termhooks.h @@ -100,7 +100,9 @@ enum event_kind If it is nil, then the locale coding system will - be used. */ + be used. If it is t, then + no decoding will take + place. */ NON_ASCII_KEYSTROKE_EVENT, /* .code is a number identifying the function key. A code N represents a key whose name is -- 2.39.5