1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Removed static function arguments in inputwin.c

This commit is contained in:
James Booth 2015-01-16 00:36:42 +00:00
parent f0ffc31cd6
commit 7a5deca77c

View File

@ -80,13 +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 void _delete_previous_word(char *input); static void _delete_previous_word(void);
static int _get_display_length(void);
void void
create_input_window(void) create_input_window(void)
@ -138,11 +139,7 @@ inp_block(void)
char * char *
inp_read(int *key_type, wint_t *ch) inp_read(int *key_type, wint_t *ch)
{ {
int display_size = 0; int display_size = _get_display_length();
if (inp_size != 0) {
display_size = g_utf8_strlen(line, inp_size);
}
// echo off, and get some more input // echo off, and get some more input
noecho(); noecho();
@ -162,7 +159,7 @@ inp_read(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;
@ -260,14 +257,12 @@ inp_put_back(void)
void void
inp_replace_input(char *input, const char * const new_input, int *size) inp_replace_input(char *input, const char * const new_input, int *size)
{ {
int display_size;
strncpy(input, new_input, INP_WIN_MAX); strncpy(input, new_input, INP_WIN_MAX);
*size = strlen(input); *size = strlen(input);
display_size = g_utf8_strlen(input, *size);
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
@ -291,25 +286,20 @@ _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 = 0;
if (inp_size != 0) { int display_size = _get_display_length();
display_size = g_utf8_strlen(input, inp_size);
}
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;
@ -318,9 +308,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;
@ -339,7 +329,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);
} }
@ -356,15 +346,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;
@ -381,7 +371,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 {
@ -399,12 +389,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;
@ -416,7 +406,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();
@ -424,54 +414,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:
@ -487,7 +477,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:
@ -503,33 +493,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:
@ -539,31 +529,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;
@ -575,43 +565,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);
} }
@ -629,7 +621,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)
{ {
@ -671,7 +663,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;
@ -680,14 +672,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;
@ -696,9 +688,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;
@ -716,26 +708,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)
@ -750,8 +742,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;
@ -768,3 +761,13 @@ _printable(const wint_t ch)
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);
} }
static int
_get_display_length(void)
{
if (inp_size != 0) {
return g_utf8_strlen(line, inp_size);
} else {
return 0;
}
}