From 500b0c080e53c40dc46bbc05e94575b32fa72499 Mon Sep 17 00:00:00 2001 From: Lars Henriksen Date: Wed, 14 Feb 2018 20:28:01 +0100 Subject: Support UTF-8 encoded characters in user choices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Translations (in po-files) of texts that are used for alternative choices (e.g. [dwmy]), may use UTF-8 encoded Unicode characters (e.g. [éãüå]). Signed-off-by: Lukas Fleischer --- src/utils.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/utils.c b/src/utils.c index 495b8fc..bb90e36 100644 --- a/src/utils.c +++ b/src/utils.c @@ -192,7 +192,7 @@ void status_mesg(const char *msg1, const char *msg2) /* * Prompts the user to make a choice between named alternatives. * - * The available choices are described by a string of the form + * The available choices are described (in po-files) by a string of the form * "[ynp]". The first and last char are ignored (they are only here to * make the translators' life easier), and every other char indicates * a key the user is allowed to press. @@ -203,25 +203,36 @@ void status_mesg(const char *msg1, const char *msg2) int status_ask_choice(const char *message, const char choice[], int nb_choice) { - /* "[4/2/f/t/w/.../Z] " */ - char avail_choice[2 * nb_choice + 3]; - int i, ch; + /* Turn "[42w...Z]" into * "[4/2/w/.../Z]". */ + char avail_choice[nb_choice * UTF8_MAXLEN + nb_choice + 1]; + int ichoice[nb_choice]; + int i, j, k, n, ch; avail_choice[0] = '['; - for (i = 1; i <= nb_choice; i++) { - avail_choice[i * 2 - 1] = choice[i]; - avail_choice[i * 2] = '/'; + for (n = 0, i = 1, j = 1; n < nb_choice; n++, i += k) { + for (k = 0; k < UTF8_LENGTH(choice[i]); k++) { + avail_choice[j] = choice[i + k]; + j++; + } + avail_choice[j] = '/'; + j++; } - avail_choice[nb_choice * 2] = ']'; - avail_choice[nb_choice * 2 + 1] = '\0'; + avail_choice[j - 1] = ']'; + avail_choice[j] = '\0'; status_mesg(message, avail_choice); + /* Convert the character choices to internal integer codes. */ + for (n = 0, i = 1; n < nb_choice; n++, i += j) { + j = UTF8_LENGTH(choice[i]); + ichoice[n] = utf8_decode(choice + i) + (j > 1 ? KEY_MAX : 0); + } + for (;;) { ch = keys_wgetch(win[KEY].p); - for (i = 1; i <= nb_choice; i++) - if (ch == choice[i]) - return i; + for (i = 0; i < nb_choice; i++) + if (ch == ichoice[i]) + return i + 1; if (ch == ESCAPE) return (-1); if (resize) { -- cgit v1.2.3-70-g09d2