static cb_ret_t insert_char (WInput * in, int c_code) { int res; if (in->highlight) { long m1, m2; if (input_eval_marks (in, &m1, &m2)) delete_region (in, m1, m2); } if (c_code == -1) return MSG_NOT_HANDLED; if (in->charpoint >= MB_LEN_MAX) return MSG_HANDLED; in->charbuf[in->charpoint] = c_code; in->charpoint++; res = str_is_valid_char (in->charbuf, in->charpoint); if (res < 0) { if (res != -2) in->charpoint = 0; /* broken multibyte char, skip */ return MSG_HANDLED; } in->need_push = TRUE; if (strlen (in->buffer) + 1 + in->charpoint >= in->current_max_size) { /* Expand the buffer */ size_t new_length; char *narea; new_length = in->current_max_size + WIDGET (in)->cols + in->charpoint; narea = g_try_renew (char, in->buffer, new_length); if (narea != NULL) { in->buffer = narea; in->current_max_size = new_length; } }
static gboolean edit_translate_key (WEdit * edit, long x_key, int *cmd, int *ch) { long command = CK_InsertChar; int char_for_insertion = -1; /* an ordinary insertable character */ if (!edit->extmod && x_key < 256) { #ifndef HAVE_CHARSET if (is_printable (x_key)) { char_for_insertion = x_key; goto fin; } #else int c; if (edit->charpoint >= 4) { edit->charpoint = 0; edit->charbuf[edit->charpoint] = '\0'; } if (edit->charpoint < 4) { edit->charbuf[edit->charpoint++] = x_key; edit->charbuf[edit->charpoint] = '\0'; } /* input from 8-bit locale */ if (!mc_global.utf8_display) { /* source in 8-bit codeset */ c = convert_from_input_c (x_key); if (is_printable (c)) { if (!edit->utf8) char_for_insertion = c; else char_for_insertion = convert_from_8bit_to_utf_c2 ((char) x_key); goto fin; } } else { /* UTF-8 locale */ int res; res = str_is_valid_char (edit->charbuf, edit->charpoint); if (res < 0 && res != -2) { edit->charpoint = 0; /* broken multibyte char, skip */ goto fin; } if (edit->utf8) { /* source in UTF-8 codeset */ if (res < 0) { char_for_insertion = x_key; goto fin; } edit->charbuf[edit->charpoint] = '\0'; edit->charpoint = 0; if (g_unichar_isprint (g_utf8_get_char (edit->charbuf))) { char_for_insertion = x_key; goto fin; } } else { /* 8-bit source */ if (res < 0) { /* not finised multibyte input (in meddle multibyte utf-8 char) */ goto fin; } if (g_unichar_isprint (g_utf8_get_char (edit->charbuf))) { c = convert_from_utf_to_current (edit->charbuf); edit->charbuf[0] = '\0'; edit->charpoint = 0; char_for_insertion = c; goto fin; } /* unprinteble utf input, skip it */ edit->charbuf[0] = '\0'; edit->charpoint = 0; } } #endif /* HAVE_CHARSET */ } /* Commands specific to the key emulation */ if (edit->extmod) { edit->extmod = FALSE; command = keybind_lookup_keymap_command (editor_x_map, x_key); } else command = keybind_lookup_keymap_command (editor_map, x_key); if (command == CK_IgnoreKey) command = CK_InsertChar; fin: *cmd = (int) command; /* FIXME */ *ch = char_for_insertion; return !(command == CK_InsertChar && char_for_insertion == -1); }
static cb_ret_t query_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data) { static char buff[MB_LEN_MAX] = ""; static int bl = 0; switch (msg) { case DLG_KEY: switch (parm) { case KEY_LEFT: case KEY_RIGHT: bl = 0; h->ret_value = 0; dlg_stop (h); return MSG_HANDLED; case KEY_BACKSPACE: bl = 0; /* exit from completion list if input line is empty */ if (end == 0) { h->ret_value = 0; dlg_stop (h); } /* Refill the list box and start again */ else if (end == min_end) { end = str_get_prev_char (&input->buffer[end]) - input->buffer; input_handle_char (input, parm); h->ret_value = B_USER; dlg_stop (h); return MSG_HANDLED; } else { int new_end; int i; GList *e; new_end = str_get_prev_char (&input->buffer[end]) - input->buffer; for (i = 0, e = ((WListbox *) h->current->data)->list; e != NULL; i++, e = g_list_next (e)) { WLEntry *le = (WLEntry *) e->data; if (strncmp (input->buffer + start, le->text, new_end - start) == 0) { listbox_select_entry ((WListbox *) h->current->data, i); end = new_end; input_handle_char (input, parm); send_message ((Widget *) h->current->data, WIDGET_DRAW, 0); break; } } } return MSG_HANDLED; default: if (parm < 32 || parm > 255) { bl = 0; if (input_key_is_in_map (input, parm) != 2) return MSG_NOT_HANDLED; if (end == min_end) return MSG_HANDLED; /* This means we want to refill the list box and start again */ h->ret_value = B_USER; dlg_stop (h); return MSG_HANDLED; } else { GList *e; int i; int need_redraw = 0; int low = 4096; char *last_text = NULL; buff[bl++] = (char) parm; buff[bl] = '\0'; switch (str_is_valid_char (buff, bl)) { case -1: bl = 0; /* fallthrough */ case -2: return MSG_HANDLED; } for (i = 0, e = ((WListbox *) h->current->data)->list; e != NULL; i++, e = g_list_next (e)) { WLEntry *le = (WLEntry *) e->data; if (strncmp (input->buffer + start, le->text, end - start) == 0 && strncmp (&le->text[end - start], buff, bl) == 0) { if (need_redraw == 0) { need_redraw = 1; listbox_select_entry ((WListbox *) h->current->data, i); last_text = le->text; } else { char *si, *sl; int si_num = 0; int sl_num = 0; /* count symbols between start and end */ for (si = le->text + start; si < le->text + end; str_next_char (&si), si_num++) ; for (sl = last_text + start; sl < last_text + end; str_next_char (&sl), sl_num++) ; /* pointers to next symbols */ si = &le->text[str_offset_to_pos (le->text, ++si_num)]; sl = &last_text[str_offset_to_pos (last_text, ++sl_num)]; while (si[0] != '\0' && sl[0] != '\0') { char *nexti, *nextl; nexti = str_get_next_char (si); nextl = str_get_next_char (sl); if (nexti - si != nextl - sl || strncmp (si, sl, nexti - si) != 0) break; si = nexti; sl = nextl; si_num++; } last_text = le->text; si = &last_text[str_offset_to_pos (last_text, si_num)]; if (low > si - last_text) low = si - last_text; need_redraw = 2; } } } if (need_redraw == 2) { insert_text (input, last_text, low); send_message ((Widget *) h->current->data, WIDGET_DRAW, 0); } else if (need_redraw == 1) { h->ret_value = B_ENTER; dlg_stop (h); } bl = 0; } return MSG_HANDLED; } break; default: return default_dlg_callback (h, sender, msg, parm, data); } }