| 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, 0); |
| 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 |
{ |
{ |
| 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 |
// Trim \n from last line |
| 131 |
if (i + 1 == p_editor_data->display_line_total && |
if (i + 1 == p_editor_data->display_line_total && |
| 132 |
p_editor_data->display_line_lengths[i] > 0 && |
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') |
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]--; |
p_editor_data->display_line_lengths[i]--; |
| 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 |
{ |
{ |
| 241 |
if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK |
if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK |
| 242 |
{ |
{ |
| 243 |
i++; |
i++; |
| 244 |
} |
} |
| 362 |
} |
} |
| 363 |
|
|
| 364 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 365 |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0); |
| 366 |
|
|
| 367 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 368 |
{ |
{ |
| 375 |
// Terminate prior display line with \n, to avoid error on cleanup |
// Terminate prior display line with \n, to avoid error on cleanup |
| 376 |
if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0) |
if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0) |
| 377 |
{ |
{ |
| 378 |
len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len); |
len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0); |
| 379 |
p_editor_data->p_display_lines[display_line + i - 1][len] = '\n'; |
p_editor_data->p_display_lines[display_line + i - 1][len] = '\n'; |
| 380 |
p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0'; |
p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0'; |
| 381 |
p_editor_data->display_line_lengths[display_line + i - 1] = len + 1; |
p_editor_data->display_line_lengths[display_line + i - 1] = len + 1; |
| 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, 0); |
| 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 |
|
|
| 438 |
int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset, |
int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
| 439 |
long *p_last_updated_line) |
long *p_last_updated_line) |
| 440 |
{ |
{ |
| 441 |
|
long display_line = *p_display_line; |
| 442 |
|
long offset = *p_offset; |
| 443 |
char *p_data_line = NULL; |
char *p_data_line = NULL; |
| 444 |
long len_data_line; |
long len_data_line; |
| 445 |
long offset_data_line; |
long offset_data_line; |
| 458 |
// Get accurate offset of first character of CJK at offset position |
// Get accurate offset of first character of CJK at offset position |
| 459 |
for (i = 0; i < offset; i++) |
for (i = 0; i < offset; i++) |
| 460 |
{ |
{ |
| 461 |
if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK |
if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK |
| 462 |
{ |
{ |
| 463 |
i++; |
i++; |
| 464 |
} |
} |
| 506 |
{ |
{ |
| 507 |
str_len = 1; |
str_len = 1; |
| 508 |
} |
} |
| 509 |
else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK |
else if (p_data_line[offset_data_line + 1] < 0) // GBK |
| 510 |
{ |
{ |
| 511 |
str_len = 2; |
str_len = 2; |
| 512 |
} |
} |
| 564 |
split_line_total = last_display_line - display_line + 2; |
split_line_total = last_display_line - display_line + 2; |
| 565 |
|
|
| 566 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 567 |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0); |
| 568 |
|
|
| 569 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 570 |
{ |
{ |
| 597 |
*p_last_updated_line = MAX(j - 1, *p_last_updated_line); |
*p_last_updated_line = MAX(j - 1, *p_last_updated_line); |
| 598 |
} |
} |
| 599 |
|
|
| 600 |
|
// Return real offset |
| 601 |
|
*p_offset = offset; |
| 602 |
|
|
| 603 |
return str_len; |
return str_len; |
| 604 |
} |
} |
| 605 |
|
|
| 611 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 612 |
"| 退出[\033[32mCtrl-W\033[33m] | 帮助[\033[32mh\033[33m] |"); |
"| 退出[\033[32mCtrl-W\033[33m] | 帮助[\033[32mh\033[33m] |"); |
| 613 |
break; |
break; |
| 614 |
|
case KEY_CSI: |
| 615 |
|
*p_key = KEY_ESC; |
| 616 |
|
break; |
| 617 |
} |
} |
| 618 |
|
|
| 619 |
return 0; |
return 0; |
| 666 |
"%s", |
"%s", |
| 667 |
row_pos, col_pos, |
row_pos, col_pos, |
| 668 |
ctx.line_cursor, p_editor_data->display_line_total, |
ctx.line_cursor, p_editor_data->display_line_total, |
| 669 |
key_insert ? "插入" : "改写", |
key_insert ? "插入" : "替换", |
| 670 |
ctx.msg); |
ctx.msg); |
| 671 |
|
|
| 672 |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1); |
| 673 |
for (; display_len < SCREEN_COLS; display_len++) |
for (; display_len < SCREEN_COLS; display_len++) |
| 674 |
{ |
{ |
| 675 |
buffer[len++] = ' '; |
buffer[len++] = ' '; |
| 700 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 701 |
str_len++; |
str_len++; |
| 702 |
} |
} |
| 703 |
else |
else if (str_len > 0) |
| 704 |
{ |
{ |
| 705 |
|
log_error("Received %d character over 127 followed by character less than 127\n", str_len); |
| 706 |
str_len = 0; |
str_len = 0; |
| 707 |
} |
} |
| 708 |
|
|
| 709 |
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 |
| 710 |
ch == CR || ch == KEY_ESC) // Special character |
ch == CR || ch == KEY_ESC) // Special character |
| 711 |
{ |
{ |
| 712 |
if (str_len == 0) |
if (str_len == 0) // ch >= 32 && ch < 127 |
| 713 |
{ |
{ |
| 714 |
input_str[0] = (char)ch; |
input_str[0] = (char)ch; |
| 715 |
str_len = 1; |
str_len = 1; |
| 723 |
|
|
| 724 |
if (!key_insert) // overwrite |
if (!key_insert) // overwrite |
| 725 |
{ |
{ |
| 726 |
if (editor_data_delete(p_editor_data, display_line_in, offset_in, |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 727 |
&last_updated_line) < 0) |
&last_updated_line) < 0) |
| 728 |
{ |
{ |
| 729 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 734 |
input_str, str_len, &last_updated_line) < 0) |
input_str, str_len, &last_updated_line) < 0) |
| 735 |
{ |
{ |
| 736 |
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; |
|
| 737 |
} |
} |
| 738 |
else |
else |
| 739 |
{ |
{ |
|
str_len = 0; |
|
|
|
|
| 740 |
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)); |
| 741 |
line_current -= (output_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 742 |
output_current_row = (int)row_pos; |
output_current_row = (int)row_pos; |
| 764 |
{ |
{ |
| 765 |
row_pos += (display_line_out - display_line_in); |
row_pos += (display_line_out - display_line_in); |
| 766 |
} |
} |
| 767 |
col_pos = offset_out + 1; |
col_pos = offset_out + 1; // Set col_pos to accurate pos |
| 768 |
} |
} |
| 769 |
|
|
| 770 |
|
str_len = 0; |
| 771 |
continue; |
continue; |
| 772 |
} |
} |
| 773 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
| 781 |
} |
} |
| 782 |
|
|
| 783 |
col_pos--; |
col_pos--; |
| 784 |
|
if (col_pos > 1 && |
| 785 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK |
| 786 |
|
{ |
| 787 |
|
col_pos--; |
| 788 |
|
} |
| 789 |
|
|
| 790 |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
| 791 |
{ |
{ |
| 792 |
row_pos--; |
row_pos--; |
| 794 |
} |
} |
| 795 |
} |
} |
| 796 |
|
|
| 797 |
if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1, |
display_line_in = line_current - output_current_row + row_pos; |
| 798 |
|
offset_in = col_pos - 1; |
| 799 |
|
display_line_out = display_line_in; |
| 800 |
|
offset_out = offset_in; |
| 801 |
|
|
| 802 |
|
if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 803 |
&last_updated_line)) < 0) |
&last_updated_line)) < 0) |
| 804 |
{ |
{ |
| 805 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 806 |
} |
} |
| 807 |
else |
else |
| 808 |
{ |
{ |
| 809 |
if (ch == BACKSPACE) |
col_pos = offset_out + 1; // Set col_pos to accurate pos |
|
{ |
|
|
for (i = 1; i < str_len; i++) |
|
|
{ |
|
|
col_pos--; |
|
|
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
|
|
{ |
|
|
row_pos--; |
|
|
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
|
|
} |
|
|
} |
|
|
} |
|
| 810 |
|
|
| 811 |
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)); |
| 812 |
line_current -= (output_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 835 |
clrline(output_current_row, output_end_row); |
clrline(output_current_row, output_end_row); |
| 836 |
} |
} |
| 837 |
|
|
| 838 |
|
str_len = 0; |
| 839 |
continue; |
continue; |
| 840 |
} |
} |
| 841 |
|
|
| 853 |
break; |
break; |
| 854 |
case Ctrl('E'): // End of line |
case Ctrl('E'): // End of line |
| 855 |
case KEY_CTRL_RIGHT: |
case KEY_CTRL_RIGHT: |
| 856 |
|
if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line |
| 857 |
|
{ |
| 858 |
|
// last display line does NOT have \n in the end |
| 859 |
|
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 860 |
|
break; |
| 861 |
|
} |
| 862 |
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]); |
| 863 |
break; |
break; |
| 864 |
case Ctrl('T'): // Top of screen |
case Ctrl('T'): // Top of screen |
| 919 |
if (col_pos > 1) |
if (col_pos > 1) |
| 920 |
{ |
{ |
| 921 |
col_pos--; |
col_pos--; |
| 922 |
|
if (col_pos > 1 && |
| 923 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && |
| 924 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK |
| 925 |
|
{ |
| 926 |
|
col_pos--; |
| 927 |
|
} |
| 928 |
break; |
break; |
| 929 |
} |
} |
| 930 |
col_pos = SCREEN_COLS; // continue to KEY_UP |
col_pos = SCREEN_COLS; // continue to KEY_UP |
| 952 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 953 |
if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]) |
if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]) |
| 954 |
{ |
{ |
| 955 |
|
if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && |
| 956 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK |
| 957 |
|
{ |
| 958 |
|
col_pos++; |
| 959 |
|
} |
| 960 |
col_pos++; |
col_pos++; |
| 961 |
break; |
break; |
| 962 |
} |
} |
| 968 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
| 969 |
break; |
break; |
| 970 |
} |
} |
| 971 |
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line |
| 972 |
{ |
{ |
| 973 |
// last display line does NOT have \n in the end |
// last display line does NOT have \n in the end |
| 974 |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |