From 01b4d568b99fc48fdab0ec628a550dfbeaa7b021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Courr=C3=A8ges-Anglas?= Date: Mon, 15 Apr 2013 01:21:04 +0200 Subject: Support UTF-8 in the input bar * introduce RP_IS_UTF8_{CHAR,CONT,START} macros. Those yield non-zero only if the locale is UTF-8. * use those macros in editor.c to properly handle UTF-8 multibyte characters. * use them also in input.c:update_input_window, to draw the cursor Reviewing and comments are welcome. Patches for generic support of multibyte encodings are welcome too. UTF-8 was chosen because of its processing simplicity and its wide use, not because of any political opinion or religious belief. ;) --- src/input.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/input.c') diff --git a/src/input.c b/src/input.c index 86866f7..5757bf7 100644 --- a/src/input.c +++ b/src/input.c @@ -428,16 +428,23 @@ read_key (KeySym *keysym, unsigned int *modifiers, char *keysym_name, int len) static void update_input_window (rp_screen *s, rp_input_line *line) { - int prompt_width = rp_text_width (s, line->prompt, -1); - int input_width = rp_text_width (s, line->buffer, line->length); - int total_width; + int prompt_width, input_width, total_width; + int char_len = 0, height; GC lgc; XGCValues gcv; - int height; + prompt_width = rp_text_width (s, line->prompt, -1); + input_width = rp_text_width (s, line->buffer, line->length); total_width = defaults.bar_x_padding * 2 + prompt_width + input_width + MAX_FONT_WIDTH (defaults.font); height = (FONT_HEIGHT (s) + defaults.bar_y_padding * 2); + if (RP_IS_UTF8_START (line->buffer[line->position])) + do + char_len++; + while (RP_IS_UTF8_CONT (line->buffer[line->position + char_len])); + else + char_len = 1; + if (total_width < defaults.input_window_size + prompt_width) { total_width = defaults.input_window_size + prompt_width; @@ -466,12 +473,12 @@ update_input_window (rp_screen *s, rp_input_line *line) gcv.foreground = s->fg_color ^ s->bg_color; lgc = XCreateGC (dpy, s->input_window, GCFunction | GCForeground, &gcv); - /* Draw a cheap-o cursor - MkII */ + /* Draw a cheap-o cursor - MkIII */ XFillRectangle (dpy, s->input_window, lgc, defaults.bar_x_padding + prompt_width + rp_text_width (s, line->buffer, line->position), defaults.bar_y_padding, - rp_text_width (s, &line->buffer[line->position], 1), + rp_text_width (s, &line->buffer[line->position], char_len), FONT_HEIGHT (s)); XFlush (dpy); -- cgit v1.2.3