| 16 |
#include "str_process.h" |
#include "str_process.h" |
| 17 |
#include <stdlib.h> |
#include <stdlib.h> |
| 18 |
#include <string.h> |
#include <string.h> |
| 19 |
|
#include <wchar.h> |
| 20 |
#include <sys/param.h> |
#include <sys/param.h> |
| 21 |
|
|
| 22 |
enum _editor_constant_t |
enum _editor_constant_t |
| 77 |
long line_offsets[MAX_EDITOR_DATA_LINES + 1]; |
long line_offsets[MAX_EDITOR_DATA_LINES + 1]; |
| 78 |
long current_data_line_length = 0; |
long current_data_line_length = 0; |
| 79 |
long i; |
long i; |
| 80 |
|
int j; |
| 81 |
|
|
| 82 |
if (p_data == NULL) |
if (p_data == NULL) |
| 83 |
{ |
{ |
| 124 |
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]); |
| 125 |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
| 126 |
|
|
| 127 |
|
// Convert \t to single space |
| 128 |
|
for (j = 0; j < p_editor_data->display_line_lengths[i]; j++) |
| 129 |
|
{ |
| 130 |
|
if (p_editor_data->p_display_lines[i][j] == '\t') |
| 131 |
|
{ |
| 132 |
|
p_editor_data->p_display_lines[i][j] = ' '; |
| 133 |
|
} |
| 134 |
|
} |
| 135 |
|
|
| 136 |
// Trim \n from last line |
// Trim \n from last line |
| 137 |
if (i + 1 == p_editor_data->display_line_total && |
if (i + 1 == p_editor_data->display_line_total && |
| 138 |
p_editor_data->display_line_lengths[i] > 0 && |
p_editor_data->display_line_lengths[i] > 0 && |
| 639 |
{ |
{ |
| 640 |
case 0: // Set msg |
case 0: // Set msg |
| 641 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 642 |
"| 退出[\033[32mCtrl-W\033[33m] |"); |
"| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]", |
| 643 |
|
(UTF8_fixed_width ? "定宽" : "变宽")); |
| 644 |
break; |
break; |
| 645 |
case KEY_CSI: |
case KEY_CSI: |
| 646 |
*p_key = KEY_ESC; |
*p_key = KEY_ESC; |
| 656 |
char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; |
char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; |
| 657 |
EDITOR_CTX ctx; |
EDITOR_CTX ctx; |
| 658 |
int ch = 0; |
int ch = 0; |
| 659 |
char input_str[4]; |
char input_str[5]; |
|
char c; |
|
| 660 |
int str_len = 0; |
int str_len = 0; |
| 661 |
|
wchar_t wcs[2]; |
| 662 |
|
int wc_len; |
| 663 |
|
char c; |
| 664 |
int input_ok; |
int input_ok; |
| 665 |
const int screen_begin_row = 1; |
const int screen_begin_row = 1; |
| 666 |
const int screen_row_total = SCREEN_ROWS - screen_begin_row; |
const int screen_row_total = SCREEN_ROWS - screen_begin_row; |
| 758 |
break; |
break; |
| 759 |
} |
} |
| 760 |
} |
} |
| 761 |
|
input_str[str_len] = '\0'; |
| 762 |
} |
} |
| 763 |
|
|
| 764 |
if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character |
if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character |
| 836 |
} |
} |
| 837 |
if (offset_out > 0) |
if (offset_out > 0) |
| 838 |
{ |
{ |
| 839 |
col_pos += (str_len == 1 ? 1 : 2); |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1) |
| 840 |
|
{ |
| 841 |
|
log_error("mbstowcs() error\n"); |
| 842 |
|
} |
| 843 |
|
col_pos += (str_len == 1 ? 1 : (UTF8_fixed_width ? 2 : wcwidth(wcs[0]))); |
| 844 |
} |
} |
| 845 |
} |
} |
| 846 |
} |
} |
| 878 |
|
|
| 879 |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
| 880 |
(int)col_pos - 1, &eol, &display_len, 0); |
(int)col_pos - 1, &eol, &display_len, 0); |
| 881 |
if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 |
col_pos = display_len; |
|
{ |
|
|
col_pos = display_len - 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
col_pos = display_len; |
|
|
} |
|
|
|
|
| 882 |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
| 883 |
{ |
{ |
| 884 |
row_pos--; |
row_pos--; |
| 1035 |
case KEY_LEFT: |
case KEY_LEFT: |
| 1036 |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
| 1037 |
(int)col_pos - 1, &eol, &display_len, 0); |
(int)col_pos - 1, &eol, &display_len, 0); |
| 1038 |
if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 |
col_pos = display_len; |
| 1039 |
{ |
if (offset_in > 0) |
|
col_pos = display_len - 1; |
|
|
} |
|
|
else |
|
| 1040 |
{ |
{ |
| 1041 |
col_pos = display_len; |
str_len = 1; |
| 1042 |
|
offset_in--; |
| 1043 |
|
if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0 || |
| 1044 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] > 127) // UTF8 |
| 1045 |
|
{ |
| 1046 |
|
while (offset_in > 0 && |
| 1047 |
|
(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xc0) != 0xc0) |
| 1048 |
|
{ |
| 1049 |
|
str_len++; |
| 1050 |
|
offset_in--; |
| 1051 |
|
} |
| 1052 |
|
|
| 1053 |
|
if (str_len > 4) |
| 1054 |
|
{ |
| 1055 |
|
log_error("Invalid UTF-8 data detected: str_len > 4\n"); |
| 1056 |
|
} |
| 1057 |
|
|
| 1058 |
|
if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) == |
| 1059 |
|
(size_t)-1) |
| 1060 |
|
{ |
| 1061 |
|
log_error("mbstowcs() error\n"); |
| 1062 |
|
} |
| 1063 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 1064 |
|
|
| 1065 |
|
if (wc_len == 2) |
| 1066 |
|
{ |
| 1067 |
|
col_pos--; |
| 1068 |
|
} |
| 1069 |
|
} |
| 1070 |
} |
} |
| 1071 |
if (col_pos >= 1) |
if (col_pos >= 1) |
| 1072 |
{ |
{ |
| 1095 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 1096 |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
| 1097 |
(int)col_pos - 1, &eol, &display_len, 0); |
(int)col_pos - 1, &eol, &display_len, 0); |
| 1098 |
if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] && |
col_pos = display_len + 2; |
| 1099 |
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8 |
if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]) |
| 1100 |
{ |
{ |
| 1101 |
col_pos = display_len + 3; |
str_len = 0; |
| 1102 |
} |
if ((p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0x80) == |
| 1103 |
else |
0x80) // head of multi-byte character |
| 1104 |
{ |
{ |
| 1105 |
col_pos = display_len + 2; |
c = (char)(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xf0); |
| 1106 |
|
while (c & 0x80) |
| 1107 |
|
{ |
| 1108 |
|
str_len++; |
| 1109 |
|
c = (c & 0x7f) << 1; |
| 1110 |
|
} |
| 1111 |
|
|
| 1112 |
|
if (str_len > 4) |
| 1113 |
|
{ |
| 1114 |
|
log_error("Invalid UTF-8 data detected: str_len > 4\n"); |
| 1115 |
|
} |
| 1116 |
|
|
| 1117 |
|
if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) == |
| 1118 |
|
(size_t)-1) |
| 1119 |
|
{ |
| 1120 |
|
log_error("mbstowcs() error\n"); |
| 1121 |
|
} |
| 1122 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 1123 |
|
|
| 1124 |
|
if (wc_len == 2) |
| 1125 |
|
{ |
| 1126 |
|
col_pos++; |
| 1127 |
|
} |
| 1128 |
|
} |
| 1129 |
|
else |
| 1130 |
|
{ |
| 1131 |
|
str_len = 1; |
| 1132 |
|
} |
| 1133 |
|
offset_in += str_len; |
| 1134 |
} |
} |
| 1135 |
if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos]) |
if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos]) |
| 1136 |
{ |
{ |