| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#include "editor.h" |
|
| 17 |
#include "bbs.h" |
#include "bbs.h" |
| 18 |
|
#include "common.h" |
| 19 |
|
#include "editor.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
#include "log.h" |
#include "log.h" |
|
#include "common.h" |
|
|
#include "str_process.h" |
|
| 22 |
#include "memory_pool.h" |
#include "memory_pool.h" |
| 23 |
|
#include "str_process.h" |
| 24 |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <sys/param.h> |
|
|
#include <strings.h> |
|
| 25 |
#include <string.h> |
#include <string.h> |
| 26 |
|
#include <sys/param.h> |
| 27 |
|
|
| 28 |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
| 29 |
#define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000 |
#define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000 |
| 213 |
long last_display_line; // of data line |
long last_display_line; // of data line |
| 214 |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 215 |
long split_line_total; |
long split_line_total; |
| 216 |
long i, j; |
long i; |
| 217 |
int len; |
int len; |
| 218 |
int eol; |
int eol; |
| 219 |
int display_len; |
int display_len; |
| 274 |
} |
} |
| 275 |
|
|
| 276 |
// Split current data line if over-length |
// Split current data line if over-length |
| 277 |
if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
if (len_data_line + str_len + 2 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
| 278 |
{ |
{ |
| 279 |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
| 280 |
{ |
{ |
| 281 |
// log_error("Split line error, display_line_total(%ld) reach limit(%d)\n", |
#ifdef _DEBUG |
| 282 |
// p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
log_error("Split line error, display_line_total(%ld) reach limit(%d)\n", |
| 283 |
|
p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
| 284 |
|
#endif |
| 285 |
|
|
| 286 |
return -2; |
return -2; |
| 287 |
} |
} |
| 288 |
|
|
| 294 |
return -2; |
return -2; |
| 295 |
} |
} |
| 296 |
|
|
| 297 |
if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
if (offset_data_line + str_len + 2 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
| 298 |
{ |
{ |
| 299 |
if (str[0] == CR) |
if (str[0] == CR) |
| 300 |
{ |
{ |
| 371 |
// Insert blank display line after last_display_line |
// Insert blank display line after last_display_line |
| 372 |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
| 373 |
{ |
{ |
| 374 |
// log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
#ifdef _DEBUG |
| 375 |
|
log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
| 376 |
|
#endif |
| 377 |
|
|
| 378 |
// Terminate prior display line with \n, to avoid error on cleanup |
// Terminate prior display line with \n, to avoid error on cleanup |
| 379 |
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) |
| 380 |
{ |
{ |
| 389 |
} |
} |
| 390 |
break; |
break; |
| 391 |
} |
} |
| 392 |
for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) |
|
| 393 |
{ |
// for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) |
| 394 |
p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
// { |
| 395 |
p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; |
// p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
| 396 |
} |
// p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; |
| 397 |
|
// } |
| 398 |
|
memmove(p_editor_data->p_display_lines + last_display_line + 2, |
| 399 |
|
p_editor_data->p_display_lines + last_display_line + 1, |
| 400 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 401 |
|
sizeof(p_editor_data->p_display_lines[last_display_line + 1])); |
| 402 |
|
memmove(p_editor_data->display_line_lengths + last_display_line + 2, |
| 403 |
|
p_editor_data->display_line_lengths + last_display_line + 1, |
| 404 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 405 |
|
sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); |
| 406 |
|
|
| 407 |
last_display_line++; |
last_display_line++; |
| 408 |
|
*p_last_updated_line = p_editor_data->display_line_total; |
| 409 |
(p_editor_data->display_line_total)++; |
(p_editor_data->display_line_total)++; |
| 410 |
} |
} |
| 411 |
|
|
| 600 |
if (*p_last_updated_line < last_display_line) |
if (*p_last_updated_line < last_display_line) |
| 601 |
{ |
{ |
| 602 |
// Remove redundant display line after last_display_line |
// Remove redundant display line after last_display_line |
| 603 |
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++) |
| 604 |
{ |
// { |
| 605 |
p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = 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]; |
| 606 |
p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = 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]; |
| 607 |
} |
// } |
| 608 |
|
memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1, |
| 609 |
|
p_editor_data->p_display_lines + last_display_line + 1, |
| 610 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 611 |
|
sizeof(p_editor_data->p_display_lines[last_display_line + 1])); |
| 612 |
|
memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1, |
| 613 |
|
p_editor_data->display_line_lengths + last_display_line + 1, |
| 614 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 615 |
|
sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); |
| 616 |
|
|
| 617 |
j = p_editor_data->display_line_total; |
j = p_editor_data->display_line_total; |
| 618 |
(p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line); |
(p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line); |
| 675 |
return ch; |
return ch; |
| 676 |
} |
} |
| 677 |
|
|
| 678 |
loop = 1; |
for (loop = 1; !SYS_server_exit && loop;) |
|
while (!SYS_server_exit && loop) |
|
| 679 |
{ |
{ |
| 680 |
if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row) |
if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row) |
| 681 |
{ |
{ |
| 705 |
iflush(); |
iflush(); |
| 706 |
|
|
| 707 |
str_len = 0; |
str_len = 0; |
|
input_ok = 0; |
|
| 708 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(MAX_DELAY_TIME); |
| 709 |
while (!SYS_server_exit && !input_ok) |
while (!SYS_server_exit) |
| 710 |
{ |
{ |
| 711 |
// extended key handler |
// extended key handler |
| 712 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 736 |
str_len = 1; |
str_len = 1; |
| 737 |
} |
} |
| 738 |
|
|
|
last_updated_line = line_current; |
|
| 739 |
display_line_in = line_current - output_current_row + row_pos; |
display_line_in = line_current - output_current_row + row_pos; |
| 740 |
offset_in = col_pos - 1; |
offset_in = col_pos - 1; |
| 741 |
display_line_out = display_line_in; |
display_line_out = display_line_in; |
| 742 |
offset_out = offset_in; |
offset_out = offset_in; |
| 743 |
|
|
| 744 |
|
last_updated_line = display_line_in; |
| 745 |
|
|
| 746 |
if (!key_insert) // overwrite |
if (!key_insert) // overwrite |
| 747 |
{ |
{ |
| 748 |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 771 |
clrtoeol(); |
clrtoeol(); |
| 772 |
for (i = 0; i < scroll_rows; i++) |
for (i = 0; i < scroll_rows; i++) |
| 773 |
{ |
{ |
| 774 |
prints("\033[S"); // Scroll up 1 line |
// prints("\033[S"); // Scroll up 1 line |
| 775 |
|
prints("\n"); // Legacy Cterm only works with this line |
| 776 |
} |
} |
| 777 |
|
|
| 778 |
output_current_row -= scroll_rows; |
output_current_row -= scroll_rows; |
| 812 |
{ |
{ |
| 813 |
if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden |
if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden |
| 814 |
{ |
{ |
| 815 |
ch = igetch_t(MAX_DELAY_TIME); |
break; // force output prior operation result if any |
|
continue; |
|
| 816 |
} |
} |
| 817 |
|
|
| 818 |
col_pos--; |
col_pos--; |
| 1027 |
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])); |
| 1028 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 1029 |
clrtoeol(); |
clrtoeol(); |
| 1030 |
prints("\033[S"); // Scroll up 1 line |
// prints("\033[S"); // Scroll up 1 line |
| 1031 |
|
prints("\n"); // Legacy Cterm only works with this line |
| 1032 |
break; |
break; |
| 1033 |
case KEY_PGUP: |
case KEY_PGUP: |
| 1034 |
if (line_current - output_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |