1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Merge branch 'master' into inp-utf8

Conflicts:
	src/ui/inputwin.c
This commit is contained in:
James Booth 2015-01-16 00:41:02 +00:00
commit 9fad03af74
3 changed files with 96 additions and 96 deletions

View File

@ -183,7 +183,7 @@ ui_readline(void)
int key_type; int key_type;
wint_t ch; wint_t ch;
char *line = inp_get_char(&key_type, &ch); char *line = inp_read(&key_type, &ch);
_win_handle_switch(ch); _win_handle_switch(ch);
ProfWin *current = wins_get_current(); ProfWin *current = wins_get_current();

View File

@ -80,14 +80,14 @@ static int rows, cols;
static char line[INP_WIN_MAX]; static char line[INP_WIN_MAX];
static int inp_size; static int inp_size;
static int _handle_edit(int result, const wint_t ch, char *input); static int _handle_edit(int key_type, const wint_t ch);
static int _handle_alt_key(char *input, int key); static int _handle_alt_key(int key);
static void _handle_backspace(int display_size, int inp_x, char *input); static void _handle_backspace(void);
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(void);
static int _get_display_length(char *input); static void _delete_previous_word(void);
static void _delete_previous_word(char *input); static int _get_display_length(void);
void void
create_input_window(void) create_input_window(void)
@ -137,9 +137,9 @@ inp_block(void)
} }
char * char *
inp_get_char(int *key_type, wint_t *ch) inp_read(int *key_type, wint_t *ch)
{ {
int display_size = _get_display_length(line); int display_size = _get_display_length();
// echo off, and get some more input // echo off, and get some more input
noecho(); noecho();
@ -159,7 +159,7 @@ inp_get_char(int *key_type, wint_t *ch)
} }
// if it wasn't an arrow key etc // if it wasn't an arrow key etc
if (!_handle_edit(*key_type, *ch, line)) { if (!_handle_edit(*key_type, *ch)) {
if (_printable(*ch) && *key_type != KEY_CODE_YES) { if (_printable(*ch) && *key_type != KEY_CODE_YES) {
if (inp_size >= INP_WIN_MAX) { if (inp_size >= INP_WIN_MAX) {
*ch = ERR; *ch = ERR;
@ -255,10 +255,10 @@ inp_put_back(void)
} }
static int static int
_get_display_length(char *input) _get_display_length(void)
{ {
int len = 0; int len = 0;
gchar *curr = g_utf8_offset_to_pointer(input, 0); gchar *curr = g_utf8_offset_to_pointer(line, 0);
while (*curr != '\0') { while (*curr != '\0') {
gunichar curru = g_utf8_get_char(curr); gunichar curru = g_utf8_get_char(curr);
if (g_unichar_iswide(curru)) { if (g_unichar_iswide(curru)) {
@ -277,11 +277,10 @@ inp_replace_input(char *input, const char * const new_input, int *size)
{ {
strncpy(input, new_input, INP_WIN_MAX); strncpy(input, new_input, INP_WIN_MAX);
*size = strlen(input); *size = strlen(input);
int display_size = _get_display_length(input);
inp_win_reset(); inp_win_reset();
input[*size] = '\0'; input[*size] = '\0';
waddstr(inp_win, input); waddstr(inp_win, input);
_go_to_end(display_size); _go_to_end();
} }
void void
@ -305,21 +304,19 @@ _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) _handle_edit(int key_type, const wint_t ch)
{ {
char *prev = NULL; char *prev = NULL;
char *next = NULL; char *next = NULL;
int inp_x = 0; int inp_x = getcurx(inp_win);
int next_ch; int next_ch;
int display_size = _get_display_length(input); int display_size = _get_display_length();
inp_x = getcurx(inp_win);
// CTRL-LEFT // CTRL-LEFT
if ((result == KEY_CODE_YES) && (ch == 547 || ch == 545 || ch == 544 || ch == 540 || ch == 539) && (inp_x > 0)) { if ((key_type == KEY_CODE_YES) && (ch == 547 || ch == 545 || ch == 544 || ch == 540 || ch == 539) && (inp_x > 0)) {
input[inp_size] = '\0'; line[inp_size] = '\0';
gchar *curr_ch = g_utf8_offset_to_pointer(input, inp_x); gchar *curr_ch = g_utf8_offset_to_pointer(line, inp_x);
curr_ch = g_utf8_find_prev_char(input, curr_ch); curr_ch = g_utf8_find_prev_char(line, curr_ch);
gchar *prev_ch; gchar *prev_ch;
gunichar curr_uni; gunichar curr_uni;
gunichar prev_uni; gunichar prev_uni;
@ -328,9 +325,9 @@ _handle_edit(int result, const wint_t ch, char *input)
curr_uni = g_utf8_get_char(curr_ch); curr_uni = g_utf8_get_char(curr_ch);
if (g_unichar_isspace(curr_uni)) { if (g_unichar_isspace(curr_uni)) {
curr_ch = g_utf8_find_prev_char(input, curr_ch); curr_ch = g_utf8_find_prev_char(line, curr_ch);
} else { } else {
prev_ch = g_utf8_find_prev_char(input, curr_ch); prev_ch = g_utf8_find_prev_char(line, curr_ch);
if (prev_ch == NULL) { if (prev_ch == NULL) {
curr_ch = NULL; curr_ch = NULL;
break; break;
@ -349,7 +346,7 @@ _handle_edit(int result, const wint_t ch, char *input)
inp_x = 0; inp_x = 0;
wmove(inp_win, 0, inp_x); wmove(inp_win, 0, inp_x);
} else { } else {
glong offset = g_utf8_pointer_to_offset(input, curr_ch); glong offset = g_utf8_pointer_to_offset(line, curr_ch);
inp_x = offset; inp_x = offset;
wmove(inp_win, 0, inp_x); wmove(inp_win, 0, inp_x);
} }
@ -366,15 +363,15 @@ _handle_edit(int result, const wint_t ch, char *input)
return 1; return 1;
// 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 ((key_type == KEY_CODE_YES) && (ch == 562 || ch == 560 || ch == 555 || ch == 559 || ch == 554) && (inp_x < display_size)) {
input[inp_size] = '\0'; line[inp_size] = '\0';
gchar *curr_ch = g_utf8_offset_to_pointer(input, inp_x); gchar *curr_ch = g_utf8_offset_to_pointer(line, 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;
gunichar next_uni; gunichar next_uni;
gboolean moved = FALSE; gboolean moved = FALSE;
while (g_utf8_pointer_to_offset(input, next_ch) < display_size) { while (g_utf8_pointer_to_offset(line, next_ch) < display_size) {
curr_uni = g_utf8_get_char(curr_ch); curr_uni = g_utf8_get_char(curr_ch);
next_uni = g_utf8_get_char(next_ch); next_uni = g_utf8_get_char(next_ch);
curr_ch = next_ch; curr_ch = next_ch;
@ -391,7 +388,7 @@ _handle_edit(int result, const wint_t ch, char *input)
inp_x = display_size; inp_x = display_size;
wmove(inp_win, 0, inp_x); wmove(inp_win, 0, inp_x);
} else { } else {
glong offset = g_utf8_pointer_to_offset(input, curr_ch); glong offset = g_utf8_pointer_to_offset(line, curr_ch);
if (offset == display_size - 1) { if (offset == display_size - 1) {
inp_x = offset + 1; inp_x = offset + 1;
} else { } else {
@ -409,12 +406,12 @@ _handle_edit(int result, const wint_t ch, char *input)
return 1; return 1;
// ALT-LEFT // ALT-LEFT
} else if ((result == KEY_CODE_YES) && (ch == 537 || ch == 542)) { } else if ((key_type == KEY_CODE_YES) && (ch == 537 || ch == 542)) {
ui_previous_win(); ui_previous_win();
return 1; return 1;
// ALT-RIGHT // ALT-RIGHT
} else if ((result == KEY_CODE_YES) && (ch == 552 || ch == 557)) { } else if ((key_type == KEY_CODE_YES) && (ch == 552 || ch == 557)) {
ui_next_win(); ui_next_win();
return 1; return 1;
@ -426,7 +423,7 @@ _handle_edit(int result, const wint_t ch, char *input)
// 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, next_ch); return _handle_alt_key(next_ch);
} else { } else {
inp_size = 0; inp_size = 0;
inp_win_reset(); inp_win_reset();
@ -434,54 +431,54 @@ _handle_edit(int result, const wint_t ch, char *input)
} }
case 127: case 127:
_handle_backspace(display_size, inp_x, input); _handle_backspace();
return 1; return 1;
case KEY_BACKSPACE: case KEY_BACKSPACE:
if (result != KEY_CODE_YES) { if (key_type != KEY_CODE_YES) {
return 0; return 0;
} }
_handle_backspace(display_size, inp_x, input); _handle_backspace();
return 1; return 1;
case KEY_DC: // DEL case KEY_DC: // DEL
if (result != KEY_CODE_YES) { if (key_type != KEY_CODE_YES) {
return 0; return 0;
} }
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(line, 0, inp_x);
for (inp_size = 0; inp_size < strlen(start); inp_size++) { for (inp_size = 0; inp_size < strlen(start); inp_size++) {
input[inp_size] = start[inp_size]; line[inp_size] = start[inp_size];
} }
input[inp_size] = '\0'; line[inp_size] = '\0';
g_free(start); g_free(start);
_clear_input(); _clear_input();
waddstr(inp_win, input); waddstr(inp_win, line);
} 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(line, 0, inp_x);
gchar *end = g_utf8_substring(input, inp_x+1, inp_size); gchar *end = g_utf8_substring(line, 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 (inp_size = 0; inp_size < strlen(new->str); inp_size++) { for (inp_size = 0; inp_size < strlen(new->str); inp_size++) {
input[inp_size] = new->str[inp_size]; line[inp_size] = new->str[inp_size];
} }
input[inp_size] = '\0'; line[inp_size] = '\0';
g_free(start); g_free(start);
g_free(end); g_free(end);
g_string_free(new, FALSE); g_string_free(new, FALSE);
_clear_input(); _clear_input();
waddstr(inp_win, input); waddstr(inp_win, line);
wmove(inp_win, 0, inp_x); wmove(inp_win, 0, inp_x);
} }
return 1; return 1;
case KEY_LEFT: case KEY_LEFT:
if (result != KEY_CODE_YES) { if (key_type != KEY_CODE_YES) {
return 0; return 0;
} }
case KEY_CTRL_B: case KEY_CTRL_B:
@ -497,7 +494,7 @@ _handle_edit(int result, const wint_t ch, char *input)
return 1; return 1;
case KEY_RIGHT: case KEY_RIGHT:
if (result != KEY_CODE_YES) { if (key_type != KEY_CODE_YES) {
return 0; return 0;
} }
case KEY_CTRL_F: case KEY_CTRL_F:
@ -513,33 +510,33 @@ _handle_edit(int result, const wint_t ch, char *input)
return 1; return 1;
case KEY_UP: case KEY_UP:
if (result != KEY_CODE_YES) { if (key_type != KEY_CODE_YES) {
return 0; return 0;
} }
case KEY_CTRL_P: case KEY_CTRL_P:
prev = cmd_history_previous(input, &inp_size); prev = cmd_history_previous(line, &inp_size);
if (prev) { if (prev) {
inp_replace_input(input, prev, &inp_size); inp_replace_input(line, prev, &inp_size);
} }
return 1; return 1;
case KEY_DOWN: case KEY_DOWN:
if (result != KEY_CODE_YES) { if (key_type != KEY_CODE_YES) {
return 0; return 0;
} }
case KEY_CTRL_N: case KEY_CTRL_N:
next = cmd_history_next(input, &inp_size); next = cmd_history_next(line, &inp_size);
if (next) { if (next) {
inp_replace_input(input, next, &inp_size); inp_replace_input(line, next, &inp_size);
} else if (inp_size != 0) { } else if (inp_size != 0) {
input[inp_size] = '\0'; line[inp_size] = '\0';
cmd_history_append(input); cmd_history_append(line);
inp_replace_input(input, "", &inp_size); inp_replace_input(line, "", &inp_size);
} }
return 1; return 1;
case KEY_HOME: case KEY_HOME:
if (result != KEY_CODE_YES) { if (key_type != KEY_CODE_YES) {
return 0; return 0;
} }
case KEY_CTRL_A: case KEY_CTRL_A:
@ -549,31 +546,31 @@ _handle_edit(int result, const wint_t ch, char *input)
return 1; return 1;
case KEY_END: case KEY_END:
if (result != KEY_CODE_YES) { if (key_type != KEY_CODE_YES) {
return 0; return 0;
} }
case KEY_CTRL_E: case KEY_CTRL_E:
_go_to_end(display_size); _go_to_end();
return 1; return 1;
case 9: // tab case 9: // tab
if (inp_size != 0) { if (inp_size != 0) {
if ((strncmp(input, "/", 1) != 0) && (ui_current_win_type() == WIN_MUC)) { if ((strncmp(line, "/", 1) != 0) && (ui_current_win_type() == WIN_MUC)) {
muc_autocomplete(input, &inp_size); muc_autocomplete(line, &inp_size);
} else if (strncmp(input, "/", 1) == 0) { } else if (strncmp(line, "/", 1) == 0) {
cmd_autocomplete(input, &inp_size); cmd_autocomplete(line, &inp_size);
} }
} }
return 1; return 1;
case KEY_CTRL_W: case KEY_CTRL_W:
_delete_previous_word(input); _delete_previous_word();
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); _delete_previous_word();
} }
return 1; return 1;
break; break;
@ -585,43 +582,45 @@ _handle_edit(int result, const wint_t ch, char *input)
} }
static void static void
_handle_backspace(int display_size, int inp_x, char *input) _handle_backspace(void)
{ {
int inp_x = getcurx(inp_win);
int display_size = _get_display_length();
roster_reset_search_attempts(); roster_reset_search_attempts();
if (display_size > 0) { if (display_size > 0) {
// 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(line, 0, inp_x-1);
for (inp_size = 0; inp_size < strlen(start); inp_size++) { for (inp_size = 0; inp_size < strlen(start); inp_size++) {
input[inp_size] = start[inp_size]; line[inp_size] = start[inp_size];
} }
input[inp_size] = '\0'; line[inp_size] = '\0';
g_free(start); g_free(start);
_clear_input(); _clear_input();
waddstr(inp_win, input); waddstr(inp_win, line);
wmove(inp_win, 0, inp_x -1); wmove(inp_win, 0, inp_x -1);
// 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(line, 0, inp_x - 1);
gchar *end = g_utf8_substring(input, inp_x, inp_size); gchar *end = g_utf8_substring(line, 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 (inp_size = 0; inp_size < strlen(new->str); inp_size++) { for (inp_size = 0; inp_size < strlen(new->str); inp_size++) {
input[inp_size] = new->str[inp_size]; line[inp_size] = new->str[inp_size];
} }
input[inp_size] = '\0'; line[inp_size] = '\0';
g_free(start); g_free(start);
g_free(end); g_free(end);
g_string_free(new, FALSE); g_string_free(new, FALSE);
_clear_input(); _clear_input();
waddstr(inp_win, input); waddstr(inp_win, line);
wmove(inp_win, 0, inp_x -1); wmove(inp_win, 0, inp_x -1);
} }
@ -639,7 +638,7 @@ _handle_backspace(int display_size, int inp_x, char *input)
} }
static int static int
_handle_alt_key(char *input, int key) _handle_alt_key(int key)
{ {
switch (key) switch (key)
{ {
@ -681,7 +680,7 @@ _handle_alt_key(char *input, int key)
break; break;
case 263: case 263:
case 127: case 127:
_delete_previous_word(input); _delete_previous_word();
break; break;
default: default:
break; break;
@ -690,14 +689,14 @@ _handle_alt_key(char *input, int key)
} }
static void static void
_delete_previous_word(char *input) _delete_previous_word(void)
{ {
int end_del = getcurx(inp_win); int end_del = getcurx(inp_win);
int start_del = end_del; int start_del = end_del;
input[inp_size] = '\0'; line[inp_size] = '\0';
gchar *curr_ch = g_utf8_offset_to_pointer(input, end_del); gchar *curr_ch = g_utf8_offset_to_pointer(line, end_del);
curr_ch = g_utf8_find_prev_char(input, curr_ch); curr_ch = g_utf8_find_prev_char(line, curr_ch);
gchar *prev_ch; gchar *prev_ch;
gunichar curr_uni; gunichar curr_uni;
gunichar prev_uni; gunichar prev_uni;
@ -706,9 +705,9 @@ _delete_previous_word(char *input)
curr_uni = g_utf8_get_char(curr_ch); curr_uni = g_utf8_get_char(curr_ch);
if (g_unichar_isspace(curr_uni)) { if (g_unichar_isspace(curr_uni)) {
curr_ch = g_utf8_find_prev_char(input, curr_ch); curr_ch = g_utf8_find_prev_char(line, curr_ch);
} else { } else {
prev_ch = g_utf8_find_prev_char(input, curr_ch); prev_ch = g_utf8_find_prev_char(line, curr_ch);
if (prev_ch == NULL) { if (prev_ch == NULL) {
curr_ch = NULL; curr_ch = NULL;
break; break;
@ -726,26 +725,26 @@ _delete_previous_word(char *input)
if (curr_ch == NULL) { if (curr_ch == NULL) {
start_del = 0; start_del = 0;
} else { } else {
start_del = g_utf8_pointer_to_offset(input, curr_ch); start_del = g_utf8_pointer_to_offset(line, curr_ch);
} }
gint len = g_utf8_strlen(input, -1); gint len = g_utf8_strlen(line, -1);
gchar *start_string = g_utf8_substring(input, 0, start_del); gchar *start_string = g_utf8_substring(line, 0, start_del);
gchar *end_string = g_utf8_substring(input, end_del, len); gchar *end_string = g_utf8_substring(line, end_del, len);
int i; int i;
for (i = 0; i < strlen(start_string); i++) { for (i = 0; i < strlen(start_string); i++) {
input[i] = start_string[i]; line[i] = start_string[i];
} }
for (i = 0; i < strlen(end_string); i++) { for (i = 0; i < strlen(end_string); i++) {
input[strlen(start_string)+i] = end_string[i]; line[strlen(start_string)+i] = end_string[i];
} }
inp_size = strlen(start_string)+i; inp_size = strlen(start_string)+i;
input[inp_size] = '\0'; line[inp_size] = '\0';
_clear_input(); _clear_input();
waddstr(inp_win, input); waddstr(inp_win, line);
wmove(inp_win, 0, start_del); wmove(inp_win, 0, start_del);
// if gone off screen to left, jump left (half a screen worth) // if gone off screen to left, jump left (half a screen worth)
@ -760,8 +759,9 @@ _delete_previous_word(char *input)
} }
static void static void
_go_to_end(int display_size) _go_to_end(void)
{ {
int display_size = _get_display_length();
wmove(inp_win, 0, display_size); wmove(inp_win, 0, display_size);
if (display_size > cols-2) { if (display_size > cols-2) {
pad_start = display_size - cols + 1; pad_start = display_size - cols + 1;
@ -777,4 +777,4 @@ _printable(const wint_t ch)
bytes[utf_len] = '\0'; bytes[utf_len] = '\0';
gunichar unichar = g_utf8_get_char(bytes); gunichar unichar = g_utf8_get_char(bytes);
return g_unichar_isprint(unichar) && (ch != KEY_MOUSE); return g_unichar_isprint(unichar) && (ch != KEY_MOUSE);
} }

View File

@ -36,7 +36,7 @@
#define UI_INPUTWIN_H #define UI_INPUTWIN_H
void create_input_window(void); void create_input_window(void);
char* inp_get_char(int *key_type, wint_t *ch); char* inp_read(int *key_type, wint_t *ch);
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);