about summary refs log tree commit diff stats
path: root/src/ui
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-01-17 22:03:22 +0000
committerJames Booth <boothj5@gmail.com>2015-01-17 22:03:22 +0000
commit89260280d13b8ae7166913affa5f973f260c8b7c (patch)
treebf091a913aeadd23c883fe35cc819ab681816397 /src/ui
parentba89297382a40ea8f63f73d756dc1ad67a0b1aaa (diff)
downloadprofani-tty-89260280d13b8ae7166913affa5f973f260c8b7c.tar.gz
Fix backspace for utf8 wide chars
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/inputwin.c30
1 files changed, 11 insertions, 19 deletions
diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c
index 85ddc79a..5279de7c 100644
--- a/src/ui/inputwin.c
+++ b/src/ui/inputwin.c
@@ -588,29 +588,21 @@ _handle_edit(int key_type, const wint_t ch)
 static void
 _handle_backspace(void)
 {
-    int inp_x = getcurx(inp_win);
+    int col = getcurx(inp_win);
     int display_size = utf8_display_len(input);
+    int input_len_utf8 = g_utf8_strlen(input, -1);
     roster_reset_search_attempts();
-    if (display_size > 0) {
 
+    if (display_size > 0) {
         // if at end, delete last char
-        if (inp_x >= display_size) {
-            gchar *start = g_utf8_substring(input, 0, inp_x-1);
-            for (input_len_bytes = 0; input_len_bytes < strlen(start); input_len_bytes++) {
-                input[input_len_bytes] = start[input_len_bytes];
-            }
-            input[input_len_bytes] = '\0';
-
-            g_free(start);
-
-            _clear_input();
-            waddstr(inp_win, input);
-            wmove(inp_win, 0, inp_x -1);
+        if (col >= display_size) {
+            gchar *new_input = g_utf8_substring(input, 0, input_len_utf8-1);
+            inp_replace_input(new_input);
 
         // if in middle, delete and shift chars left
-        } else if (inp_x > 0 && inp_x < display_size) {
-            gchar *start = g_utf8_substring(input, 0, inp_x - 1);
-            gchar *end = g_utf8_substring(input, inp_x, input_len_bytes);
+        } else if (col > 0 && col < display_size) {
+            gchar *start = g_utf8_substring(input, 0, col - 1);
+            gchar *end = g_utf8_substring(input, col, input_len_bytes);
             GString *new = g_string_new(start);
             g_string_append(new, end);
 
@@ -625,11 +617,11 @@ _handle_backspace(void)
 
             _clear_input();
             waddstr(inp_win, input);
-            wmove(inp_win, 0, inp_x -1);
+            wmove(inp_win, 0, col -1);
         }
 
         // if gone off screen to left, jump left (half a screen worth)
-        if (inp_x <= pad_start) {
+        if (col <= pad_start) {
             pad_start = pad_start - (cols / 2);
             if (pad_start < 0) {
                 pad_start = 0;