| 20 |
#include "log.h" |
#include "log.h" |
| 21 |
#include "common.h" |
#include "common.h" |
| 22 |
#include "str_process.h" |
#include "str_process.h" |
| 23 |
|
#include "memory_pool.h" |
| 24 |
#include <stdlib.h> |
#include <stdlib.h> |
| 25 |
#include <sys/param.h> |
#include <sys/param.h> |
| 26 |
#include <strings.h> |
#include <strings.h> |
| 29 |
#include <string.h> |
#include <string.h> |
| 30 |
|
|
| 31 |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
| 32 |
|
#define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000 |
| 33 |
|
#define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1) |
| 34 |
|
|
| 35 |
|
static MEMORY_POOL *p_mp_data_line; |
| 36 |
|
static MEMORY_POOL *p_mp_editor_data; |
| 37 |
|
|
| 38 |
|
int editor_memory_pool_init(void) |
| 39 |
|
{ |
| 40 |
|
if (p_mp_data_line != NULL || p_mp_editor_data != NULL) |
| 41 |
|
{ |
| 42 |
|
log_error("Editor mem pool already initialized\n"); |
| 43 |
|
return -1; |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT); |
| 47 |
|
if (p_mp_data_line == NULL) |
| 48 |
|
{ |
| 49 |
|
log_error("Memory pool init error\n"); |
| 50 |
|
return -2; |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
| 54 |
|
if (p_mp_data_line == NULL) |
| 55 |
|
{ |
| 56 |
|
log_error("Memory pool init error\n"); |
| 57 |
|
return -3; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
return 0; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
void editor_memory_pool_cleanup(void) |
| 64 |
|
{ |
| 65 |
|
if (p_mp_data_line != NULL) |
| 66 |
|
{ |
| 67 |
|
memory_pool_cleanup(p_mp_data_line); |
| 68 |
|
p_mp_data_line = NULL; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
if (p_mp_editor_data != NULL) |
| 72 |
|
{ |
| 73 |
|
memory_pool_cleanup(p_mp_editor_data); |
| 74 |
|
p_mp_editor_data = NULL; |
| 75 |
|
} |
| 76 |
|
} |
| 77 |
|
|
| 78 |
EDITOR_DATA *editor_data_load(const char *p_data) |
EDITOR_DATA *editor_data_load(const char *p_data) |
| 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 |
|
|
| 89 |
return NULL; |
return NULL; |
| 90 |
} |
} |
| 91 |
|
|
| 92 |
p_editor_data = malloc(sizeof(EDITOR_DATA)); |
p_editor_data = memory_pool_alloc(p_mp_editor_data); |
| 93 |
if (p_editor_data == NULL) |
if (p_editor_data == NULL) |
| 94 |
{ |
{ |
| 95 |
log_error("malloc(EDITOR_DATA) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 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 |
{ |
{ |
| 107 |
(p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n')) |
(p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n')) |
| 108 |
{ |
{ |
| 109 |
// Allocate new data line |
// Allocate new data line |
| 110 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 111 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 112 |
{ |
{ |
| 113 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i); |
log_error("memory_pool_alloc() error: i = %d\n", i); |
| 114 |
// Cleanup |
// Cleanup |
| 115 |
editor_data_cleanup(p_editor_data); |
editor_data_cleanup(p_editor_data); |
| 116 |
return NULL; |
return NULL; |
| 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 |
|
|
| 192 |
if (p_editor_data->display_line_lengths[i] > 0 && |
if (p_editor_data->display_line_lengths[i] > 0 && |
| 193 |
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') |
| 194 |
{ |
{ |
| 195 |
free(p_data_line); |
memory_pool_free(p_mp_data_line, p_data_line); |
| 196 |
p_data_line = NULL; |
p_data_line = NULL; |
| 197 |
} |
} |
| 198 |
} |
} |
| 199 |
|
|
| 200 |
if (p_data_line != NULL) |
if (p_data_line != NULL) |
| 201 |
{ |
{ |
| 202 |
free(p_data_line); |
memory_pool_free(p_mp_data_line, p_data_line); |
| 203 |
} |
} |
| 204 |
|
|
| 205 |
free(p_editor_data); |
memory_pool_free(p_mp_editor_data, p_editor_data); |
| 206 |
} |
} |
| 207 |
|
|
| 208 |
int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
| 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 |
} |
} |
| 287 |
} |
} |
| 288 |
|
|
| 289 |
// Allocate new data line |
// Allocate new data line |
| 290 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 291 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 292 |
{ |
{ |
| 293 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 294 |
return -2; |
return -2; |
| 295 |
} |
} |
| 296 |
|
|
| 412 |
|
|
| 413 |
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
| 414 |
{ |
{ |
| 415 |
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
if (*p_display_line + 1 < p_editor_data->display_line_total) |
|
|
|
|
if (*p_display_line + 1 >= p_editor_data->display_line_total) |
|
| 416 |
{ |
{ |
| 417 |
log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total); |
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
| 418 |
|
(*p_display_line)++; |
| 419 |
} |
} |
| 420 |
else |
} |
| 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_display_line)++; |
*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 |
} |
} |
| 496 |
} |
} |
| 497 |
} |
} |
| 498 |
|
|
| 499 |
|
if (offset_data_line >= len_data_line) // end-of-line |
| 500 |
|
{ |
| 501 |
|
return 0; |
| 502 |
|
} |
| 503 |
|
|
| 504 |
// Check str to be deleted |
// Check str to be deleted |
| 505 |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
| 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 |
} |
} |
| 513 |
else |
else |
| 514 |
{ |
{ |
| 515 |
log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n", |
log_error("Some strange character at display_line %ld, offset %ld: %d %d\n", |
| 516 |
display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1], |
display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]); |
|
p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]); |
|
| 517 |
str_len = 1; |
str_len = 1; |
| 518 |
} |
} |
| 519 |
|
|
| 550 |
p_data_line[offset_data_line + len_data_line] = '\0'; |
p_data_line[offset_data_line + len_data_line] = '\0'; |
| 551 |
|
|
| 552 |
// Recycle next data line |
// Recycle next data line |
| 553 |
free(p_editor_data->p_display_lines[display_line + 1]); |
memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]); |
| 554 |
} |
} |
| 555 |
else |
else |
| 556 |
{ |
{ |
| 583 |
|
|
| 584 |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
| 585 |
|
|
| 586 |
if (display_line + i < last_display_line) |
if (*p_last_updated_line < last_display_line) |
| 587 |
{ |
{ |
| 588 |
// Remove redundant display line after last_display_line |
// Remove redundant display line after last_display_line |
| 589 |
for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++) |
for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++) |
| 590 |
{ |
{ |
| 591 |
p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j]; |
p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j]; |
| 592 |
p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j]; |
p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j]; |
| 593 |
} |
} |
| 594 |
|
|
| 595 |
(p_editor_data->display_line_total) -= (last_display_line - (display_line + i)); |
j = p_editor_data->display_line_total; |
| 596 |
last_display_line = display_line + i; |
(p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line); |
| 597 |
|
*p_last_updated_line = MAX(j - 1, *p_last_updated_line); |
|
*p_last_updated_line = p_editor_data->display_line_total - 1; |
|
| 598 |
} |
} |
| 599 |
|
|
| 600 |
|
// Return real offset |
| 601 |
|
*p_offset = offset; |
| 602 |
|
|
| 603 |
return str_len; |
return str_len; |
| 604 |
} |
} |
| 605 |
|
|
| 609 |
{ |
{ |
| 610 |
case 0: // Set msg |
case 0: // Set msg |
| 611 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 612 |
"| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |"); |
"| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |"); |
| 613 |
break; |
break; |
| 614 |
} |
} |
| 615 |
|
|
| 697 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 698 |
str_len++; |
str_len++; |
| 699 |
} |
} |
| 700 |
else |
else if (str_len > 0) |
| 701 |
{ |
{ |
| 702 |
|
log_error("Received %d character over 127 followed by character less than 127\n", str_len); |
| 703 |
str_len = 0; |
str_len = 0; |
| 704 |
} |
} |
| 705 |
|
|
| 706 |
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 |
| 707 |
ch == CR || ch == KEY_ESC) // Special character |
ch == CR || ch == KEY_ESC) // Special character |
| 708 |
{ |
{ |
| 709 |
if (str_len == 0) |
if (str_len == 0) // ch >= 32 && ch < 127 |
| 710 |
{ |
{ |
| 711 |
input_str[0] = (char)ch; |
input_str[0] = (char)ch; |
| 712 |
str_len = 1; |
str_len = 1; |
| 720 |
|
|
| 721 |
if (!key_insert) // overwrite |
if (!key_insert) // overwrite |
| 722 |
{ |
{ |
| 723 |
if (editor_data_delete(p_editor_data, display_line_in, offset_in, |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 724 |
&last_updated_line) < 0) |
&last_updated_line) < 0) |
| 725 |
{ |
{ |
| 726 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 731 |
input_str, str_len, &last_updated_line) < 0) |
input_str, str_len, &last_updated_line) < 0) |
| 732 |
{ |
{ |
| 733 |
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; |
|
| 734 |
} |
} |
| 735 |
else |
else |
| 736 |
{ |
{ |
|
str_len = 0; |
|
|
|
|
| 737 |
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)); |
| 738 |
line_current -= (output_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 739 |
output_current_row = (int)row_pos; |
output_current_row = (int)row_pos; |
| 761 |
{ |
{ |
| 762 |
row_pos += (display_line_out - display_line_in); |
row_pos += (display_line_out - display_line_in); |
| 763 |
} |
} |
| 764 |
col_pos = offset_out + 1; |
col_pos = offset_out + 1; // Set col_pos to accurate pos |
| 765 |
} |
} |
| 766 |
|
|
| 767 |
|
str_len = 0; |
| 768 |
continue; |
continue; |
| 769 |
} |
} |
| 770 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
| 771 |
{ |
{ |
| 772 |
if (ch == BACKSPACE) |
if (ch == BACKSPACE) |
| 773 |
{ |
{ |
| 774 |
|
if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden |
| 775 |
|
{ |
| 776 |
|
input_ok = 0; |
| 777 |
|
continue; |
| 778 |
|
} |
| 779 |
|
|
| 780 |
col_pos--; |
col_pos--; |
| 781 |
|
if (col_pos > 1 && |
| 782 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK |
| 783 |
|
{ |
| 784 |
|
col_pos--; |
| 785 |
|
} |
| 786 |
|
|
| 787 |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
| 788 |
{ |
{ |
| 789 |
row_pos--; |
row_pos--; |
| 791 |
} |
} |
| 792 |
} |
} |
| 793 |
|
|
| 794 |
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; |
| 795 |
|
offset_in = col_pos - 1; |
| 796 |
|
display_line_out = display_line_in; |
| 797 |
|
offset_out = offset_in; |
| 798 |
|
|
| 799 |
|
if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 800 |
&last_updated_line)) < 0) |
&last_updated_line)) < 0) |
| 801 |
{ |
{ |
| 802 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 803 |
} |
} |
| 804 |
else |
else |
| 805 |
{ |
{ |
| 806 |
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]); |
|
|
} |
|
|
} |
|
|
} |
|
| 807 |
|
|
| 808 |
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)); |
| 809 |
line_current -= (output_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 827 |
row_pos += scroll_rows; |
row_pos += scroll_rows; |
| 828 |
output_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 829 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
|
clrline(output_current_row, SCREEN_ROWS); |
|
| 830 |
} |
} |
| 831 |
|
|
| 832 |
|
clrline(output_current_row, output_end_row); |
| 833 |
} |
} |
| 834 |
|
|
| 835 |
|
str_len = 0; |
| 836 |
continue; |
continue; |
| 837 |
} |
} |
| 838 |
|
|
| 841 |
case KEY_NULL: |
case KEY_NULL: |
| 842 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 843 |
goto cleanup; |
goto cleanup; |
| 844 |
case Ctrl('C'): |
case Ctrl('W'): |
| 845 |
loop = 0; |
loop = 0; |
| 846 |
break; |
break; |
| 847 |
case Ctrl('S'): // Start of line |
case Ctrl('S'): // Start of line |
| 850 |
break; |
break; |
| 851 |
case Ctrl('E'): // End of line |
case Ctrl('E'): // End of line |
| 852 |
case KEY_CTRL_RIGHT: |
case KEY_CTRL_RIGHT: |
| 853 |
|
if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line |
| 854 |
|
{ |
| 855 |
|
// last display line does NOT have \n in the end |
| 856 |
|
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 857 |
|
break; |
| 858 |
|
} |
| 859 |
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]); |
| 860 |
break; |
break; |
| 861 |
case Ctrl('T'): // Top of screen |
case Ctrl('T'): // Top of screen |
| 865 |
break; |
break; |
| 866 |
case Ctrl('B'): // Bottom of screen |
case Ctrl('B'): // Bottom of screen |
| 867 |
case KEY_CTRL_DOWN: |
case KEY_CTRL_DOWN: |
| 868 |
row_pos = SCREEN_ROWS - 1; |
if (p_editor_data->display_line_total < screen_row_total) |
| 869 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
{ |
| 870 |
|
row_pos = p_editor_data->display_line_total; |
| 871 |
|
} |
| 872 |
|
else |
| 873 |
|
{ |
| 874 |
|
row_pos = SCREEN_ROWS - 1; |
| 875 |
|
} |
| 876 |
|
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
| 877 |
|
{ |
| 878 |
|
// last display line does NOT have \n in the end |
| 879 |
|
col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1); |
| 880 |
|
} |
| 881 |
|
else |
| 882 |
|
{ |
| 883 |
|
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
| 884 |
|
} |
| 885 |
break; |
break; |
| 886 |
case KEY_INS: |
case KEY_INS: |
| 887 |
key_insert = !key_insert; |
key_insert = !key_insert; |
| 902 |
if (p_editor_data->display_line_total < screen_row_total) |
if (p_editor_data->display_line_total < screen_row_total) |
| 903 |
{ |
{ |
| 904 |
row_pos = p_editor_data->display_line_total; |
row_pos = p_editor_data->display_line_total; |
| 905 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 906 |
break; |
break; |
| 907 |
} |
} |
| 908 |
line_current = p_editor_data->display_line_total - screen_row_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 909 |
output_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 910 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 911 |
row_pos = SCREEN_ROWS - 1; |
row_pos = SCREEN_ROWS - 1; |
| 912 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 913 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 914 |
break; |
break; |
| 915 |
case KEY_LEFT: |
case KEY_LEFT: |
| 916 |
if (col_pos > 1) |
if (col_pos > 1) |
| 917 |
{ |
{ |
| 918 |
col_pos--; |
col_pos--; |
| 919 |
|
if (col_pos > 1 && |
| 920 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && |
| 921 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK |
| 922 |
|
{ |
| 923 |
|
col_pos--; |
| 924 |
|
} |
| 925 |
break; |
break; |
| 926 |
} |
} |
| 927 |
col_pos = SCREEN_COLS; // continue to KEY_UP |
col_pos = SCREEN_COLS; // continue to KEY_UP |
| 949 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 950 |
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]) |
| 951 |
{ |
{ |
| 952 |
|
if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && |
| 953 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK |
| 954 |
|
{ |
| 955 |
|
col_pos++; |
| 956 |
|
} |
| 957 |
col_pos++; |
col_pos++; |
| 958 |
break; |
break; |
| 959 |
} |
} |
| 965 |
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])); |
| 966 |
break; |
break; |
| 967 |
} |
} |
| 968 |
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 |
| 969 |
{ |
{ |
| 970 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
// last display line does NOT have \n in the end |
| 971 |
|
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 972 |
break; |
break; |
| 973 |
} |
} |
| 974 |
line_current += (screen_row_total - (output_current_row - screen_begin_row)); |
line_current += (screen_row_total - (output_current_row - screen_begin_row)); |