| 79 |
{ |
{ |
| 80 |
EDITOR_DATA *p_editor_data; |
EDITOR_DATA *p_editor_data; |
| 81 |
char *p_data_line = NULL; |
char *p_data_line = NULL; |
| 82 |
long line_offsets[MAX_EDITOR_DATA_LINES]; |
long line_offsets[MAX_EDITOR_DATA_LINES + 1]; |
| 83 |
long current_data_line_length = 0; |
long current_data_line_length = 0; |
| 84 |
long i; |
long i; |
| 85 |
|
|
| 96 |
return NULL; |
return NULL; |
| 97 |
} |
} |
| 98 |
|
|
| 99 |
p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES); |
p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1); |
| 100 |
|
|
| 101 |
for (i = 0; i < p_editor_data->display_line_total; i++) |
for (i = 0; i < p_editor_data->display_line_total; i++) |
| 102 |
{ |
{ |
| 126 |
|
|
| 127 |
memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); |
memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); |
| 128 |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
| 129 |
|
|
| 130 |
|
// Trim \n from last line |
| 131 |
|
if (i + 1 == p_editor_data->display_line_total && |
| 132 |
|
p_editor_data->display_line_lengths[i] > 0 && |
| 133 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
| 134 |
|
{ |
| 135 |
|
p_editor_data->display_line_lengths[i]--; |
| 136 |
|
current_data_line_length--; |
| 137 |
|
} |
| 138 |
p_data_line[current_data_line_length] = '\0'; |
p_data_line[current_data_line_length] = '\0'; |
| 139 |
} |
} |
| 140 |
|
|
| 227 |
return -1; |
return -1; |
| 228 |
} |
} |
| 229 |
|
|
| 230 |
|
// Validate str |
| 231 |
|
if ((str_len == 1 && str[0] <= 0) || |
| 232 |
|
(str_len == 2 && (str[0] >= 0 || str[1] >= 0))) |
| 233 |
|
{ |
| 234 |
|
log_error("Invalid input str, len=%d\n", str_len); |
| 235 |
|
return -2; |
| 236 |
|
} |
| 237 |
|
|
| 238 |
// Get accurate offset of first character of CJK at offset position |
// Get accurate offset of first character of CJK at offset position |
| 239 |
for (i = 0; i < offset; i++) |
for (i = 0; i < offset; i++) |
| 240 |
{ |
{ |
| 419 |
} |
} |
| 420 |
} |
} |
| 421 |
|
|
| 422 |
|
// Prevent the last display line from being over-length |
| 423 |
|
if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES) |
| 424 |
|
{ |
| 425 |
|
len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len); |
| 426 |
|
p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0'; |
| 427 |
|
p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len; |
| 428 |
|
if (*p_display_line + 1 >= p_editor_data->display_line_total) |
| 429 |
|
{ |
| 430 |
|
*p_offset = MIN(*p_offset, len); |
| 431 |
|
*p_display_line = p_editor_data->display_line_total - 1; |
| 432 |
|
} |
| 433 |
|
} |
| 434 |
|
|
| 435 |
return 0; |
return 0; |
| 436 |
} |
} |
| 437 |
|
|
| 692 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 693 |
str_len++; |
str_len++; |
| 694 |
} |
} |
| 695 |
else |
else if (str_len > 0) |
| 696 |
{ |
{ |
| 697 |
|
log_error("Received %d character over 127 followed by character less than 127\n", str_len); |
| 698 |
str_len = 0; |
str_len = 0; |
| 699 |
} |
} |
| 700 |
|
|
| 701 |
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK |
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK |
| 702 |
ch == CR || ch == KEY_ESC) // Special character |
ch == CR || ch == KEY_ESC) // Special character |
| 703 |
{ |
{ |
| 704 |
if (str_len == 0) |
if (str_len == 0) // ch >= 32 && ch < 127 |
| 705 |
{ |
{ |
| 706 |
input_str[0] = (char)ch; |
input_str[0] = (char)ch; |
| 707 |
str_len = 1; |
str_len = 1; |
| 726 |
input_str, str_len, &last_updated_line) < 0) |
input_str, str_len, &last_updated_line) < 0) |
| 727 |
{ |
{ |
| 728 |
log_error("editor_data_insert(str_len=%d) error\n", str_len); |
log_error("editor_data_insert(str_len=%d) error\n", str_len); |
|
str_len = 0; |
|
| 729 |
} |
} |
| 730 |
else |
else |
| 731 |
{ |
{ |
|
str_len = 0; |
|
|
|
|
| 732 |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
| 733 |
line_current -= (output_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 734 |
output_current_row = (int)row_pos; |
output_current_row = (int)row_pos; |
| 759 |
col_pos = offset_out + 1; |
col_pos = offset_out + 1; |
| 760 |
} |
} |
| 761 |
|
|
| 762 |
|
str_len = 0; |
| 763 |
continue; |
continue; |
| 764 |
} |
} |
| 765 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
| 827 |
clrline(output_current_row, output_end_row); |
clrline(output_current_row, output_end_row); |
| 828 |
} |
} |
| 829 |
|
|
| 830 |
|
str_len = 0; |
| 831 |
continue; |
continue; |
| 832 |
} |
} |
| 833 |
|
|
| 845 |
break; |
break; |
| 846 |
case Ctrl('E'): // End of line |
case Ctrl('E'): // End of line |
| 847 |
case KEY_CTRL_RIGHT: |
case KEY_CTRL_RIGHT: |
| 848 |
|
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
| 849 |
|
{ |
| 850 |
|
// last display line does NOT have \n in the end |
| 851 |
|
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 852 |
|
break; |
| 853 |
|
} |
| 854 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
| 855 |
break; |
break; |
| 856 |
case Ctrl('T'): // Top of screen |
case Ctrl('T'): // Top of screen |