Merge branch 'master' into inp-utf8

Conflicts:
	src/ui/inputwin.c
This commit is contained in:
James Booth
2015-01-15 22:53:50 +00:00
3 changed files with 61 additions and 62 deletions

View File

@@ -182,14 +182,7 @@ char*
ui_readline(void) ui_readline(void)
{ {
int result = 0; int result = 0;
gboolean return_line = FALSE; wint_t ch = inp_get_char(input, &result);
wint_t ch = inp_get_char(input, &inp_size, &result);
if (ch == '\n') {
input[inp_size++] = '\0';
inp_size = 0;
return_line = TRUE;
}
_win_handle_switch(ch); _win_handle_switch(ch);
@@ -207,7 +200,7 @@ ui_readline(void)
ui_input_nonblocking(FALSE); ui_input_nonblocking(FALSE);
} }
if (return_line) { if (ch == '\n') {
return input; return input;
} else { } else {
return NULL; return NULL;

View File

@@ -75,15 +75,16 @@
static WINDOW *inp_win; static WINDOW *inp_win;
static int pad_start = 0; static int pad_start = 0;
static int rows, cols; static int rows, cols;
static int inp_size;
static int _handle_edit(int result, const wint_t ch, char *input, int *size); static int _handle_edit(int result, const wint_t ch, char *input);
static int _handle_alt_key(char *input, int *size, int key); static int _handle_alt_key(char *input, int key);
static void _handle_backspace(int display_size, int inp_x, int *size, char *input); static void _handle_backspace(int display_size, int inp_x, char *input);
static int _printable(const wint_t ch); static int _printable(const wint_t ch);
static void _clear_input(void); static void _clear_input(void);
static void _go_to_end(int display_size); static void _go_to_end(int display_size);
static int _get_display_length(char *input); static int _get_display_length(char *input);
static void _delete_previous_word(char *input, int *size); static void _delete_previous_word(char *input);
void void
create_input_window(void) create_input_window(void)
@@ -133,7 +134,7 @@ inp_block(void)
} }
wint_t wint_t
inp_get_char(char *input, int *size, int *result) inp_get_char(char *input, int *result)
{ {
wint_t ch; wint_t ch;
int display_size = _get_display_length(input); int display_size = _get_display_length(input);
@@ -156,9 +157,9 @@ inp_get_char(char *input, int *size, int *result)
} }
// if it wasn't an arrow key etc // if it wasn't an arrow key etc
if (!_handle_edit(*result, ch, input, size)) { if (!_handle_edit(*result, ch, input)) {
if (_printable(ch) && *result != KEY_CODE_YES) { if (_printable(ch) && *result != KEY_CODE_YES) {
if (*size >= INP_WIN_MAX) { if (inp_size >= INP_WIN_MAX) {
return ERR; return ERR;
} }
@@ -171,7 +172,7 @@ inp_get_char(char *input, int *size, int *result)
char *next_ch = g_utf8_offset_to_pointer(input, inp_x); char *next_ch = g_utf8_offset_to_pointer(input, inp_x);
char *offset; char *offset;
for (offset = &input[*size - 1]; offset >= next_ch; offset--) { for (offset = &input[inp_size - 1]; offset >= next_ch; offset--) {
*(offset + utf_len) = *offset; *(offset + utf_len) = *offset;
} }
int i; int i;
@@ -179,8 +180,8 @@ inp_get_char(char *input, int *size, int *result)
*(next_ch + i) = bytes[i]; *(next_ch + i) = bytes[i];
} }
*size += utf_len; inp_size += utf_len;
input[*size] = '\0'; input[inp_size] = '\0';
waddstr(inp_win, next_ch); waddstr(inp_win, next_ch);
wmove(inp_win, 0, inp_x + 1); wmove(inp_win, 0, inp_x + 1);
@@ -198,9 +199,9 @@ inp_get_char(char *input, int *size, int *result)
if (utf_len < MB_CUR_MAX) { if (utf_len < MB_CUR_MAX) {
int i; int i;
for (i = 0 ; i < utf_len; i++) { for (i = 0 ; i < utf_len; i++) {
input[(*size)++] = bytes[i]; input[inp_size++] = bytes[i];
} }
input[*size] = '\0'; input[inp_size] = '\0';
bytes[utf_len] = '\0'; bytes[utf_len] = '\0';
waddstr(inp_win, bytes); waddstr(inp_win, bytes);
@@ -222,6 +223,11 @@ inp_get_char(char *input, int *size, int *result)
echo(); echo();
if (ch == '\n') {
input[inp_size++] = '\0';
inp_size = 0;
}
return ch; return ch;
} }
@@ -295,7 +301,7 @@ _clear_input(void)
* return 0 if it wasn't * return 0 if it wasn't
*/ */
static int static int
_handle_edit(int result, const wint_t ch, char *input, int *size) _handle_edit(int result, const wint_t ch, char *input)
{ {
char *prev = NULL; char *prev = NULL;
char *next = NULL; char *next = NULL;
@@ -307,7 +313,7 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
// CTRL-LEFT // CTRL-LEFT
if ((result == KEY_CODE_YES) && (ch == 547 || ch == 545 || ch == 544 || ch == 540 || ch == 539) && (inp_x > 0)) { if ((result == KEY_CODE_YES) && (ch == 547 || ch == 545 || ch == 544 || ch == 540 || ch == 539) && (inp_x > 0)) {
input[*size] = '\0'; input[inp_size] = '\0';
gchar *curr_ch = g_utf8_offset_to_pointer(input, inp_x); gchar *curr_ch = g_utf8_offset_to_pointer(input, inp_x);
curr_ch = g_utf8_find_prev_char(input, curr_ch); curr_ch = g_utf8_find_prev_char(input, curr_ch);
gchar *prev_ch; gchar *prev_ch;
@@ -357,7 +363,7 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
// CTRL-RIGHT // CTRL-RIGHT
} else if ((result == KEY_CODE_YES) && (ch == 562 || ch == 560 || ch == 555 || ch == 559 || ch == 554) && (inp_x < display_size)) { } else if ((result == KEY_CODE_YES) && (ch == 562 || ch == 560 || ch == 555 || ch == 559 || ch == 554) && (inp_x < display_size)) {
input[*size] = '\0'; input[inp_size] = '\0';
gchar *curr_ch = g_utf8_offset_to_pointer(input, inp_x); gchar *curr_ch = g_utf8_offset_to_pointer(input, inp_x);
gchar *next_ch = g_utf8_find_next_char(curr_ch, NULL); gchar *next_ch = g_utf8_find_next_char(curr_ch, NULL);
gunichar curr_uni; gunichar curr_uni;
@@ -416,21 +422,21 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
// check for ALT-key // check for ALT-key
next_ch = wgetch(inp_win); next_ch = wgetch(inp_win);
if (next_ch != ERR) { if (next_ch != ERR) {
return _handle_alt_key(input, size, next_ch); return _handle_alt_key(input, next_ch);
} else { } else {
*size = 0; inp_size = 0;
inp_win_reset(); inp_win_reset();
return 1; return 1;
} }
case 127: case 127:
_handle_backspace(display_size, inp_x, size, input); _handle_backspace(display_size, inp_x, input);
return 1; return 1;
case KEY_BACKSPACE: case KEY_BACKSPACE:
if (result != KEY_CODE_YES) { if (result != KEY_CODE_YES) {
return 0; return 0;
} }
_handle_backspace(display_size, inp_x, size, input); _handle_backspace(display_size, inp_x, input);
return 1; return 1;
case KEY_DC: // DEL case KEY_DC: // DEL
@@ -440,10 +446,10 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
case KEY_CTRL_D: case KEY_CTRL_D:
if (inp_x == display_size-1) { if (inp_x == display_size-1) {
gchar *start = g_utf8_substring(input, 0, inp_x); gchar *start = g_utf8_substring(input, 0, inp_x);
for (*size = 0; *size < strlen(start); (*size)++) { for (inp_size = 0; inp_size < strlen(start); inp_size++) {
input[*size] = start[*size]; input[inp_size] = start[inp_size];
} }
input[*size] = '\0'; input[inp_size] = '\0';
g_free(start); g_free(start);
@@ -451,14 +457,14 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
waddstr(inp_win, input); waddstr(inp_win, input);
} else if (inp_x < display_size-1) { } else if (inp_x < display_size-1) {
gchar *start = g_utf8_substring(input, 0, inp_x); gchar *start = g_utf8_substring(input, 0, inp_x);
gchar *end = g_utf8_substring(input, inp_x+1, *size); gchar *end = g_utf8_substring(input, inp_x+1, inp_size);
GString *new = g_string_new(start); GString *new = g_string_new(start);
g_string_append(new, end); g_string_append(new, end);
for (*size = 0; *size < strlen(new->str); (*size)++) { for (inp_size = 0; inp_size < strlen(new->str); inp_size++) {
input[*size] = new->str[*size]; input[inp_size] = new->str[inp_size];
} }
input[*size] = '\0'; input[inp_size] = '\0';
g_free(start); g_free(start);
g_free(end); g_free(end);
@@ -507,9 +513,9 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
return 0; return 0;
} }
case KEY_CTRL_P: case KEY_CTRL_P:
prev = cmd_history_previous(input, size); prev = cmd_history_previous(input, &inp_size);
if (prev) { if (prev) {
inp_replace_input(input, prev, size); inp_replace_input(input, prev, &inp_size);
} }
return 1; return 1;
@@ -518,13 +524,13 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
return 0; return 0;
} }
case KEY_CTRL_N: case KEY_CTRL_N:
next = cmd_history_next(input, size); next = cmd_history_next(input, &inp_size);
if (next) { if (next) {
inp_replace_input(input, next, size); inp_replace_input(input, next, &inp_size);
} else if (*size != 0) { } else if (inp_size != 0) {
input[*size] = '\0'; input[inp_size] = '\0';
cmd_history_append(input); cmd_history_append(input);
inp_replace_input(input, "", size); inp_replace_input(input, "", &inp_size);
} }
return 1; return 1;
@@ -547,23 +553,23 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
return 1; return 1;
case 9: // tab case 9: // tab
if (*size != 0) { if (inp_size != 0) {
if ((strncmp(input, "/", 1) != 0) && (ui_current_win_type() == WIN_MUC)) { if ((strncmp(input, "/", 1) != 0) && (ui_current_win_type() == WIN_MUC)) {
muc_autocomplete(input, size); muc_autocomplete(input, &inp_size);
} else if (strncmp(input, "/", 1) == 0) { } else if (strncmp(input, "/", 1) == 0) {
cmd_autocomplete(input, size); cmd_autocomplete(input, &inp_size);
} }
} }
return 1; return 1;
case KEY_CTRL_W: case KEY_CTRL_W:
_delete_previous_word(input, size); _delete_previous_word(input);
return 1; return 1;
break; break;
case KEY_CTRL_U: case KEY_CTRL_U:
while (getcurx(inp_win) > 0) { while (getcurx(inp_win) > 0) {
_delete_previous_word(input, size); _delete_previous_word(input);
} }
return 1; return 1;
break; break;
@@ -575,7 +581,7 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
} }
static void static void
_handle_backspace(int display_size, int inp_x, int *size, char *input) _handle_backspace(int display_size, int inp_x, char *input)
{ {
roster_reset_search_attempts(); roster_reset_search_attempts();
if (display_size > 0) { if (display_size > 0) {
@@ -583,10 +589,10 @@ _handle_backspace(int display_size, int inp_x, int *size, char *input)
// if at end, delete last char // if at end, delete last char
if (inp_x >= display_size) { if (inp_x >= display_size) {
gchar *start = g_utf8_substring(input, 0, inp_x-1); gchar *start = g_utf8_substring(input, 0, inp_x-1);
for (*size = 0; *size < strlen(start); (*size)++) { for (inp_size = 0; inp_size < strlen(start); inp_size++) {
input[*size] = start[*size]; input[inp_size] = start[inp_size];
} }
input[*size] = '\0'; input[inp_size] = '\0';
g_free(start); g_free(start);
@@ -597,14 +603,14 @@ _handle_backspace(int display_size, int inp_x, int *size, char *input)
// if in middle, delete and shift chars left // if in middle, delete and shift chars left
} else if (inp_x > 0 && inp_x < display_size) { } else if (inp_x > 0 && inp_x < display_size) {
gchar *start = g_utf8_substring(input, 0, inp_x - 1); gchar *start = g_utf8_substring(input, 0, inp_x - 1);
gchar *end = g_utf8_substring(input, inp_x, *size); gchar *end = g_utf8_substring(input, inp_x, inp_size);
GString *new = g_string_new(start); GString *new = g_string_new(start);
g_string_append(new, end); g_string_append(new, end);
for (*size = 0; *size < strlen(new->str); (*size)++) { for (inp_size = 0; inp_size < strlen(new->str); inp_size++) {
input[*size] = new->str[*size]; input[inp_size] = new->str[inp_size];
} }
input[*size] = '\0'; input[inp_size] = '\0';
g_free(start); g_free(start);
g_free(end); g_free(end);
@@ -629,7 +635,7 @@ _handle_backspace(int display_size, int inp_x, int *size, char *input)
} }
static int static int
_handle_alt_key(char *input, int *size, int key) _handle_alt_key(char *input, int key)
{ {
switch (key) switch (key)
{ {
@@ -671,7 +677,7 @@ _handle_alt_key(char *input, int *size, int key)
break; break;
case 263: case 263:
case 127: case 127:
_delete_previous_word(input, size); _delete_previous_word(input);
break; break;
default: default:
break; break;
@@ -680,12 +686,12 @@ _handle_alt_key(char *input, int *size, int key)
} }
static void static void
_delete_previous_word(char *input, int *size) _delete_previous_word(char *input)
{ {
int end_del = getcurx(inp_win); int end_del = getcurx(inp_win);
int start_del = end_del; int start_del = end_del;
input[*size] = '\0'; input[inp_size] = '\0';
gchar *curr_ch = g_utf8_offset_to_pointer(input, end_del); gchar *curr_ch = g_utf8_offset_to_pointer(input, end_del);
curr_ch = g_utf8_find_prev_char(input, curr_ch); curr_ch = g_utf8_find_prev_char(input, curr_ch);
gchar *prev_ch; gchar *prev_ch;
@@ -731,8 +737,8 @@ _delete_previous_word(char *input, int *size)
input[strlen(start_string)+i] = end_string[i]; input[strlen(start_string)+i] = end_string[i];
} }
*size = strlen(start_string)+i; inp_size = strlen(start_string)+i;
input[*size] = '\0'; input[inp_size] = '\0';
_clear_input(); _clear_input();
waddstr(inp_win, input); waddstr(inp_win, input);

View File

@@ -36,7 +36,7 @@
#define UI_INPUTWIN_H #define UI_INPUTWIN_H
void create_input_window(void); void create_input_window(void);
wint_t inp_get_char(char *input, int *size, int *result); wint_t inp_get_char(char *input, int *result);
void inp_win_reset(void); void inp_win_reset(void);
void inp_win_resize(void); void inp_win_resize(void);
void inp_put_back(void); void inp_put_back(void);