--- lbbs/src/editor.c 2025/06/09 15:41:09 1.2 +++ lbbs/src/editor.c 2025/06/11 11:55:50 1.11 @@ -22,6 +22,7 @@ #include "str_process.h" #include #include +#include #define _POSIX_C_SOURCE 200809L #include @@ -29,6 +30,7 @@ EDITOR_DATA *editor_data_load(const char *p_data) { EDITOR_DATA *p_editor_data; + char *p_data_line = NULL; long line_offsets[MAX_EDITOR_DATA_LINES]; long current_data_line_length = 0; long i, j; @@ -64,8 +66,8 @@ EDITOR_DATA *editor_data_load(const char } // Allocate new data line - p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH); - if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL) + p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); + if (p_data_line == NULL) { log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i); // Cleanup @@ -76,9 +78,10 @@ EDITOR_DATA *editor_data_load(const char free(p_editor_data); return NULL; } - - p_editor_data->p_display_lines[i] = p_editor_data->p_data_lines[p_editor_data->data_line_total]; + p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; (p_editor_data->data_line_total)++; + + p_editor_data->p_display_lines[i] = p_data_line; current_data_line_length = 0; } else @@ -88,9 +91,12 @@ EDITOR_DATA *editor_data_load(const char memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); current_data_line_length += p_editor_data->display_line_lengths[i]; - p_editor_data->p_data_lines[p_editor_data->data_line_total - 1][current_data_line_length] = '\0'; + p_data_line[current_data_line_length] = '\0'; } + bzero(p_editor_data->p_data_lines + p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->data_line_total); + bzero(p_editor_data->p_display_lines + p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total); + return p_editor_data; } @@ -141,22 +147,18 @@ void editor_data_cleanup(EDITOR_DATA *p_ free(p_editor_data); } -int editor_data_insert(EDITOR_DATA *p_editor_data, long display_line, long offset, +int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, const char *str, int str_len, long *p_last_updated_line) { - int len; - int display_len; - int eol; - char *p_data_line; + long display_line = *p_display_line; + long offset = *p_offset; + char *p_data_line = NULL; long len_data_line; long offset_data_line; long last_display_line; // of data line - char buf_insert[MAX_EDITOR_DATA_LINE_LENGTH]; - long len_insert; - int display_len_insert; - char buf_catenate[MAX_EDITOR_DATA_LINE_LENGTH]; - long len_catenate; - long i; + long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; + long split_line_total; + long i, j; if (p_editor_data == NULL || p_last_updated_line == NULL) { @@ -164,14 +166,10 @@ int editor_data_insert(EDITOR_DATA *p_ed return -1; } - memcpy(buf_insert, str, (size_t)str_len); - buf_insert[str_len] = '\0'; - len_insert = str_len; - // Get accurate offset of first character of CJK at offset position for (i = 0; i < offset; i++) { - if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK + if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK { i++; } @@ -210,7 +208,7 @@ int editor_data_insert(EDITOR_DATA *p_ed } // Split current data line if over-length - if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH) + if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) { if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES || p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES) { @@ -220,154 +218,296 @@ int editor_data_insert(EDITOR_DATA *p_ed } // Allocate new data line - p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH); - if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL) + p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); + if (p_data_line == NULL) { log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); return -2; } + p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; + (p_editor_data->data_line_total)++; - // Copy rest part of current data line since next display line to new data line - memcpy(p_editor_data->p_data_lines[p_editor_data->data_line_total], - p_editor_data->p_display_lines[display_line + 1], - (size_t)(len_data_line - (p_editor_data->p_display_lines[display_line + 1] - p_data_line))); - p_editor_data->p_data_lines[p_editor_data->data_line_total] - [len_data_line - (p_editor_data->p_display_lines[display_line + 1] - p_data_line)] = '\0'; - - p_data_line = p_editor_data->p_display_lines[display_line + 1]; - for (i = display_line + 1; i <= last_display_line; i++) + if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) { - p_editor_data->p_display_lines[i] += - (p_editor_data->p_data_lines[p_editor_data->data_line_total] - p_data_line); - } + if (str[0] == CR) + { + str_len = 0; + } - // Copy rest part of current display line to buffer - if (offset_data_line >= MAX_EDITOR_DATA_LINE_LENGTH / 2) - { - memcpy(buf_insert + len_insert, - p_editor_data->p_display_lines[display_line] + offset, - (size_t)(p_editor_data->display_line_lengths[display_line] - offset)); - len_insert += (p_editor_data->display_line_lengths[display_line] - offset); - } - else - { - memcpy(buf_insert, + // Copy str to new data line + memcpy(p_data_line, str, (size_t)str_len); + + // Copy rest part of current data line to new data line + memcpy(p_data_line + str_len, p_editor_data->p_display_lines[display_line] + offset, - (size_t)(p_editor_data->display_line_lengths[display_line] - offset)); - len_insert = (p_editor_data->display_line_lengths[display_line] - offset); - } - buf_insert[len_insert] = '\0'; + (size_t)(len_data_line - offset_data_line)); + + p_data_line[str_len + len_data_line - offset_data_line] = '\0'; - if (offset_data_line >= MAX_EDITOR_DATA_LINE_LENGTH / 2) - { // Add line ending to current display line (data line) p_editor_data->p_display_lines[display_line][offset] = '\n'; p_editor_data->p_display_lines[display_line][offset + 1] = '\0'; p_editor_data->display_line_lengths[display_line] = offset + 1; + + *p_display_line = display_line + 1; + *p_offset = str_len; } else { + // Copy rest part of current data line to new data line + memcpy(p_data_line, + p_editor_data->p_display_lines[display_line] + offset, + (size_t)(len_data_line - offset_data_line)); + + p_data_line[len_data_line - offset_data_line] = '\0'; + + // Append str to current display line memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len); // Add line ending to current display line (data line) p_editor_data->p_display_lines[display_line][offset + str_len] = '\n'; p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0'; p_editor_data->display_line_lengths[display_line] = offset + str_len + 1; + + *p_display_line = display_line; + *p_offset = offset + str_len; } + split_line_total = last_display_line - display_line + 3; + + // Set start display_line for spliting new data line display_line++; - offset = 0; *p_last_updated_line = p_editor_data->display_line_total; + } + else // insert str into current data line at offset_data_line + { + memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line)); + memcpy(p_data_line + offset_data_line, str, (size_t)str_len); + p_data_line[len_data_line + str_len] = '\0'; - last_display_line++; - (p_editor_data->display_line_total)++; - (p_editor_data->data_line_total)++; + // Set p_data_line to head of current display line + p_data_line = p_editor_data->p_display_lines[display_line]; + split_line_total = last_display_line - display_line + 3; + + *p_display_line = display_line; + *p_offset = offset + str_len; } - for (i = display_line; len_insert > 0 && i <= last_display_line; i++) + // Split current data line since beginning of current display line + split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); + + for (i = 0; i < split_line_total; i++) { - len = split_line(buf_insert, SCREEN_COLS, &eol, &display_len_insert); - if (len != len_insert) + if (display_line + i > last_display_line) { - log_error("buf_insert is truncated at display_line(%ld): len(%d) != len_insert(%d), buf_insert: %s\n", - i, len, len_insert, buf_insert); - return -3; + // Insert blank display line after last_display_line + if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) + { + log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); + return -3; + } + for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) + { + p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; + p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; + } + last_display_line++; + (p_editor_data->display_line_total)++; } - memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)p_editor_data->display_line_lengths[i]); - buf_catenate[p_editor_data->display_line_lengths[i]] = '\0'; + p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; + p_editor_data->p_display_lines[display_line + i] = + (i == 0 + ? p_data_line + : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1])); - len = split_line(buf_catenate, SCREEN_COLS - display_len_insert, &eol, &display_len); - if (len < offset) // have no space to insert + if (p_editor_data->display_line_lengths[display_line + i] > 0 && + p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n') { - offset = 0; - continue; // retry at next display line + break; } + } + + *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line); - // move \n to next display line if current line is full - if (len > 0 && buf_catenate[len - 1] == '\n' && display_len + display_len_insert >= SCREEN_COLS) + if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] || + (*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] && + p_editor_data->p_display_lines[*p_display_line][*p_offset - 1] == '\n')) + { + *p_offset -= p_editor_data->display_line_lengths[*p_display_line]; + (*p_display_line)++; + + if (*p_display_line >= p_editor_data->display_line_total) { - len--; + log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total); } + } - memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)offset); - memcpy(buf_catenate + offset, buf_insert, (size_t)len_insert); - memcpy(buf_catenate + offset + len_insert, p_editor_data->p_display_lines[i] + offset, (size_t)(len - offset)); - len_catenate = len_insert + len; - buf_catenate[len_catenate] = '\0'; + return 0; +} - offset = 0; - len_insert = p_editor_data->display_line_lengths[i] - len; - if (len_insert > 0) +int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset, + long *p_last_updated_line) +{ + char *p_data_line = NULL; + long len_data_line; + long offset_data_line; + long last_display_line; // of data line + long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; + long split_line_total; + long i, j; + int str_len = 0; + + if (p_editor_data == NULL || p_last_updated_line == NULL) + { + log_error("editor_data_op() error: NULL pointer\n"); + return -1; + } + + // Get accurate offset of first character of CJK at offset position + for (i = 0; i < offset; i++) + { + if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK { - memcpy(buf_insert, p_editor_data->p_display_lines[i] + len, (size_t)len_insert); - buf_insert[len_insert] = '\0'; + i++; } + } + if (i > offset) // offset was skipped + { + offset--; + } - memcpy(p_editor_data->p_display_lines[i], buf_catenate, (size_t)len_catenate); - p_editor_data->display_line_lengths[i] = len_catenate; + // Get length of current data line + len_data_line = 0; + p_data_line = p_editor_data->p_display_lines[display_line]; + for (i = display_line - 1; i >= 0; i--) + { + if (p_editor_data->display_line_lengths[i] > 0 && + p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line + { + break; + } + + len_data_line += p_editor_data->display_line_lengths[i]; + p_data_line = p_editor_data->p_display_lines[i]; } + offset_data_line = len_data_line + offset; + last_display_line = p_editor_data->display_line_total - 1; + for (i = display_line; i < p_editor_data->display_line_total; i++) + { + len_data_line += p_editor_data->display_line_lengths[i]; - *p_last_updated_line = MAX(i, *p_last_updated_line); + if (p_editor_data->display_line_lengths[i] > 0 && + p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line + { + last_display_line = i; + break; + } + } - if (len_insert > 0) + // Check str to be deleted + if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) { - if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) + str_len = 1; + } + else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK + { + str_len = 2; + } + else + { + log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n", + 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]); + str_len = 1; + } + + // Current display line is (almost) empty + if (offset_data_line + str_len > len_data_line || + (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n')) + { + if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line) { - log_error("Append line error, display_line_total(%ld) reach limit(%d)\n", - p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); - return -2; + log_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total); + return 0; } - // Prepare one blank display line after last_display_line - for (i = p_editor_data->display_line_total; i > last_display_line + 1; i--) + len_data_line = 0; // Next data line + last_display_line = p_editor_data->display_line_total - 1; + for (i = display_line + 1; i < p_editor_data->display_line_total; i++) { - p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1]; - p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1]; + len_data_line += p_editor_data->display_line_lengths[i]; + + if (p_editor_data->display_line_lengths[i] > 0 && + p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line + { + last_display_line = i; + break; + } } - p_editor_data->p_display_lines[last_display_line + 1] = - p_editor_data->p_display_lines[last_display_line] + p_editor_data->display_line_lengths[last_display_line]; - p_editor_data->display_line_lengths[last_display_line + 1] = 0; - (p_editor_data->display_line_total)++; - last_display_line++; + if (offset_data_line + len_data_line + 1 > MAX_EDITOR_DATA_LINE_LENGTH) // No enough buffer to merge current data line with next data line + { + log_common("Debug: No enough buffer to merge with next data line: %ld > %ld\n", + offset_data_line + len_data_line + 1, MAX_EDITOR_DATA_LINE_LENGTH); + return 0; + } - // Fill data into blank display line - memcpy(p_editor_data->p_display_lines[last_display_line], buf_insert, (size_t)len_insert); - p_editor_data->p_display_lines[last_display_line][len_insert] = '\0'; - p_editor_data->display_line_lengths[last_display_line] = len_insert; + // Append next data line to current one + memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line); + p_data_line[offset_data_line + len_data_line] = '\0'; - *p_last_updated_line = MAX(last_display_line, *p_last_updated_line); + // Recycle next data line + // TODO: free(p_editor_data->p_display_lines[display_line + 1]); + } + else + { + memmove(p_data_line + offset_data_line, p_data_line + offset_data_line + str_len, (size_t)(len_data_line - offset_data_line - str_len)); + p_data_line[len_data_line - str_len] = '\0'; + len_data_line -= str_len; } - return 0; -} + // Set p_data_line to head of current display line + p_data_line = p_editor_data->p_display_lines[display_line]; + split_line_total = last_display_line - display_line + 2; -int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset, - long *p_last_updated_line) -{ - return 0; + // Split current data line since beginning of current display line + split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); + + for (i = 0; i < split_line_total; i++) + { + p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; + p_editor_data->p_display_lines[display_line + i] = + (i == 0 + ? p_data_line + : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1])); + + if (p_editor_data->display_line_lengths[display_line + i] > 0 && + p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n') + { + break; + } + } + + *p_last_updated_line = display_line + MIN(i, split_line_total - 1); + + if (display_line + i < last_display_line) + { + // Remove redundant display line after last_display_line + for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++) + { + p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j]; + 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_total) -= (last_display_line - (display_line + i)); + last_display_line = display_line + i; + + *p_last_updated_line = p_editor_data->display_line_total - 1; + } + + return str_len; } static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx) @@ -389,22 +529,27 @@ int editor_display(EDITOR_DATA *p_editor char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; EDITOR_CTX ctx; int ch = 0; - char hz_ch[4]; - int hz_len; - int input_ok, screen_current_line; - const int screen_begin_line = 1; - int screen_end_line = SCREEN_ROWS - 1; - const int screen_line_total = screen_end_line - screen_begin_line + 1; + char input_str[4]; + int str_len = 0; + int input_ok; + int screen_current_row; + const int screen_begin_row = 1; + int screen_end_row = SCREEN_ROWS - 1; + const int screen_row_total = screen_end_row - screen_begin_row + 1; long int line_current = 0; long int len; int loop; int eol, display_len; long row_pos = 1, col_pos = 1; + long display_line_in, offset_in; + long display_line_out, offset_out; + int scroll_rows; long last_updated_line = 0; - int insert = 1; + int key_insert = 1; + int i; - screen_current_line = screen_begin_line; - clrline(screen_begin_line, SCREEN_ROWS); + screen_current_row = screen_begin_row; + clrline(screen_begin_row, SCREEN_ROWS); // update msg_ext with extended key handler if (editor_display_key_handler(&ch, &ctx) != 0) @@ -415,16 +560,17 @@ int editor_display(EDITOR_DATA *p_editor loop = 1; while (!SYS_server_exit && loop) { - if (line_current >= p_editor_data->display_line_total || screen_current_line > screen_end_line) + if (line_current >= p_editor_data->display_line_total || screen_current_row > screen_end_row) { - ctx.line_cursor = line_current - screen_current_line + row_pos + 1; + ctx.line_cursor = line_current - screen_current_row + row_pos + 1; snprintf(buffer, sizeof(buffer), "\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] " - "第\033[32m%ld\033[33m/\033[32m%ld\033[33m行 " + "第\033[32m%ld\033[33m/\033[32m%ld\033[33m行 [\033[32m%s\033[33m] " "%s", row_pos, col_pos, ctx.line_cursor, p_editor_data->display_line_total, + key_insert ? "插入" : "改写", ctx.msg); len = split_line(buffer, SCREEN_COLS, &eol, &display_len); @@ -442,91 +588,150 @@ int editor_display(EDITOR_DATA *p_editor iflush(); input_ok = 0; + ch = igetch_t(MAX_DELAY_TIME); while (!SYS_server_exit && !input_ok) { - ch = igetch_t(MAX_DELAY_TIME); - input_ok = 1; - // extended key handler if (editor_display_key_handler(&ch, &ctx) != 0) { goto cleanup; } - if (ch >= 32 && ch < 127) // printable character + if (ch > 127 && ch <= 255) // GBK { - last_updated_line = line_current; + input_str[str_len] = (char)(ch - 256); + str_len++; + } + else + { + str_len = 0; + } - if (editor_data_insert(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1, - (const char *)&ch, 1, &last_updated_line) < 0) + if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || ch == CR) // printable character or GBK + { + if (str_len == 0) { - log_error("editor_data_op(INSERT ch) error\n"); + input_str[0] = (char)ch; + str_len = 1; } - else - { - screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current)); - line_current -= (screen_current_line - row_pos); - screen_current_line = (int)row_pos; - col_pos++; - if (col_pos <= p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]) + last_updated_line = line_current; + display_line_in = line_current - screen_current_row + row_pos; + offset_in = col_pos - 1; + display_line_out = display_line_in; + offset_out = offset_in; + + if (!key_insert) // overwrite + { + if (editor_data_delete(p_editor_data, display_line_in, offset_in, + &last_updated_line) < 0) { - continue; + log_error("editor_data_delete() error\n"); } - col_pos = 1; - ch = KEY_DOWN; } - } - else if (ch > 127 && ch <= 255) // CJK character - { - hz_ch[hz_len] = (char)(ch - 256); - hz_len++; - if (hz_len == 2) // GBK + if (editor_data_insert(p_editor_data, &display_line_out, &offset_out, + input_str, str_len, &last_updated_line) < 0) + { + log_error("editor_data_insert(%s) error\n", input_str); + str_len = 0; + } + else { - hz_len = 0; - last_updated_line = line_current; + str_len = 0; - if (editor_data_insert(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1, - hz_ch, 2, &last_updated_line) < 0) - { - log_error("editor_data_op(INSERT hz) error\n"); - } - else + screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); + line_current -= (screen_current_row - row_pos); + screen_current_row = (int)row_pos; + + scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row)); + + if (scroll_rows > 0) { - screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current)); - line_current -= (screen_current_line - row_pos); - screen_current_line = (int)row_pos; + moveto(SCREEN_ROWS, 0); + clrtoeol(); + for (i = 0; i < scroll_rows; i++) + { + prints("\033[S"); // Scroll up 1 line + } - col_pos += 2; - if (col_pos <= p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]) + screen_current_row -= scroll_rows; + if (screen_current_row < screen_begin_row) { - continue; + line_current += (screen_begin_row - screen_current_row); + screen_current_row = screen_begin_row; } - col_pos = 1; - ch = KEY_DOWN; + row_pos = screen_end_row; } + else // if (scroll_lines == 0) + { + row_pos += (display_line_out - display_line_in); + } + col_pos = offset_out + 1; + } + + // Check whether there is additional input + ch = igetch(0); + if (ch == KEY_TIMEOUT) + { + input_ok = 1; } + continue; } - else if (ch == KEY_DEL) // Del + else if (ch == KEY_DEL || ch == BACKSPACE) // Del { - last_updated_line = line_current; + if (ch == BACKSPACE) + { + col_pos--; + if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0) + { + row_pos--; + col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); + } + } - if (editor_data_delete(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1, - &last_updated_line) < 0) + if ((str_len = editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1, + &last_updated_line)) < 0) { - log_error("editor_data_op(DELETE) error\n"); + log_error("editor_data_delete() error\n"); } else { - screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current)); + if (ch == BACKSPACE) + { + for (i = 1; i < str_len; i++) + { + col_pos--; + if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0) + { + row_pos--; + col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); + } + } + } + + screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); + line_current -= (screen_current_row - row_pos); + screen_current_row = (int)row_pos; + + if (screen_current_row < screen_begin_row) // row_pos <= 0 + { + screen_current_row = screen_begin_row; + row_pos = screen_begin_row; + screen_end_row = SCREEN_ROWS - 1; + } } + // Check whether there is additional input + ch = igetch(0); + if (ch == KEY_TIMEOUT) + { + input_ok = 1; + } continue; } - hz_len = 0; - + input_ok = 1; switch (ch) { case KEY_NULL: @@ -539,44 +744,44 @@ int editor_display(EDITOR_DATA *p_editor col_pos = 1; break; case KEY_CTRL_RIGHT: - col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]); + col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); break; case KEY_CTRL_UP: - row_pos = screen_begin_line; - col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])); + row_pos = screen_begin_row; + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); break; case KEY_CTRL_DOWN: row_pos = SCREEN_ROWS - 1; - col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])); + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); break; case KEY_INS: - insert = !insert; + key_insert = !key_insert; break; case KEY_HOME: row_pos = 1; col_pos = 1; - if (line_current - screen_current_line < 0) // Reach begin + if (line_current - screen_current_row < 0) // Reach begin { break; } line_current = 0; - screen_current_line = screen_begin_line; - screen_end_line = SCREEN_ROWS - 1; - clrline(screen_begin_line, SCREEN_ROWS); + screen_current_row = screen_begin_row; + screen_end_row = SCREEN_ROWS - 1; + clrline(screen_begin_row, SCREEN_ROWS); break; case KEY_END: - if (p_editor_data->display_line_total < screen_line_total) + if (p_editor_data->display_line_total < screen_row_total) { row_pos = p_editor_data->display_line_total; - col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]); + col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); break; } - line_current = p_editor_data->display_line_total - screen_line_total; - screen_current_line = screen_begin_line; - screen_end_line = SCREEN_ROWS - 1; - row_pos = screen_line_total; - col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]); - clrline(screen_begin_line, SCREEN_ROWS); + line_current = p_editor_data->display_line_total - screen_row_total; + screen_current_row = screen_begin_row; + screen_end_row = SCREEN_ROWS - 1; + row_pos = screen_row_total; + col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); + clrline(screen_begin_row, SCREEN_ROWS); break; case KEY_LEFT: if (col_pos > 1) @@ -586,83 +791,82 @@ int editor_display(EDITOR_DATA *p_editor } col_pos = SCREEN_COLS; // continue to KEY_UP case KEY_UP: - if (row_pos > screen_begin_line) + if (row_pos > screen_begin_row) { row_pos--; - col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])); + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); break; } - if (line_current - screen_current_line < 0) // Reach begin + if (line_current - screen_current_row < 0) // Reach begin { col_pos = 1; break; } - line_current -= screen_current_line; - screen_current_line = screen_begin_line; + line_current -= screen_current_row; + screen_current_row = screen_begin_row; // screen_end_line = begin_line; // prints("\033[T"); // Scroll down 1 line - screen_end_line = SCREEN_ROWS - 1; // Legacy Fterm only works with this line - col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])); - break; - case CR: + screen_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); break; case KEY_SPACE: break; case KEY_RIGHT: - if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]) + if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]) { col_pos++; break; } col_pos = 1; // continue to KEY_DOWN case KEY_DOWN: - if (row_pos < MIN(screen_line_total, p_editor_data->display_line_total)) + if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total)) { row_pos++; - col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])); + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); break; } - if (line_current + (screen_line_total - (screen_current_line - screen_begin_line)) >= p_editor_data->display_line_total) // Reach end + if (line_current + (screen_row_total - (screen_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end { - col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]); + col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); break; } - screen_current_line--; - screen_end_line = SCREEN_ROWS - 1; - col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])); + line_current += (screen_row_total - (screen_current_row - screen_begin_row)); + screen_current_row = screen_row_total; + screen_end_row = SCREEN_ROWS - 1; + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); moveto(SCREEN_ROWS, 0); clrtoeol(); prints("\033[S"); // Scroll up 1 line break; case KEY_PGUP: - if (line_current - screen_current_line < 0) // Reach begin + if (line_current - screen_current_row < 0) // Reach begin { break; } - line_current -= ((screen_line_total - 1) + (screen_current_line - screen_begin_line)); + line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row)); if (line_current < 0) { line_current = 0; } - screen_current_line = screen_begin_line; - screen_end_line = SCREEN_ROWS - 1; - col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])); - clrline(screen_begin_line, SCREEN_ROWS); + screen_current_row = screen_begin_row; + screen_end_row = SCREEN_ROWS - 1; + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); + clrline(screen_begin_row, SCREEN_ROWS); break; case KEY_PGDN: - if (line_current + screen_line_total - (screen_current_line - screen_begin_line) >= p_editor_data->display_line_total) // Reach end + if (line_current + screen_row_total - (screen_current_row - screen_begin_row) >= p_editor_data->display_line_total) // Reach end { break; } - line_current += (screen_line_total - 1) - (screen_current_line - screen_begin_line); - if (line_current + screen_line_total > p_editor_data->display_line_total) // No enough lines to display + line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row); + if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display { - line_current = p_editor_data->display_line_total - screen_line_total; + line_current = p_editor_data->display_line_total - screen_row_total; } - screen_current_line = screen_begin_line; - screen_end_line = SCREEN_ROWS - 1; - col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])); - clrline(screen_begin_line, SCREEN_ROWS); + screen_current_row = screen_begin_row; + screen_end_row = SCREEN_ROWS - 1; + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); + clrline(screen_begin_row, SCREEN_ROWS); break; case KEY_ESC: break; @@ -677,10 +881,10 @@ int editor_display(EDITOR_DATA *p_editor show_help = 1; case KEY_F5: // Refresh after display help information - line_current -= (screen_current_line - screen_begin_line); - screen_current_line = screen_begin_line; - screen_end_line = SCREEN_ROWS - 1; - clrline(screen_begin_line, SCREEN_ROWS); + line_current -= (screen_current_row - screen_begin_row); + screen_current_row = screen_begin_row; + screen_end_row = SCREEN_ROWS - 1; + clrline(screen_begin_row, SCREEN_ROWS); break; case 0: // Refresh bottom line break; @@ -690,6 +894,10 @@ int editor_display(EDITOR_DATA *p_editor } BBS_last_access_tm = time(0); + if (!input_ok) + { + ch = igetch_t(MAX_DELAY_TIME); + } } continue; @@ -710,11 +918,11 @@ int editor_display(EDITOR_DATA *p_editor memcpy(buffer, (const char *)p_editor_data->p_display_lines[line_current], (size_t)len); buffer[len] = '\0'; - moveto(screen_current_line, 0); + moveto(screen_current_row, 0); clrtoeol(); prints("%s", buffer); line_current++; - screen_current_line++; + screen_current_row++; } cleanup: