--- lbbs/src/editor.c 2025/06/09 15:41:09 1.2 +++ lbbs/src/editor.c 2025/06/11 04:57:19 1.5 @@ -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,10 +166,6 @@ 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++) { @@ -220,145 +218,129 @@ 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++) - { - p_editor_data->p_display_lines[i] += - (p_editor_data->p_data_lines[p_editor_data->data_line_total] - p_data_line); - } - - // Copy rest part of current display line to buffer - if (offset_data_line >= MAX_EDITOR_DATA_LINE_LENGTH / 2) + if (offset_data_line + str_len + 1 < MAX_EDITOR_DATA_LINE_LENGTH) { - memcpy(buf_insert + len_insert, + // 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)(p_editor_data->display_line_lengths[display_line] - offset)); - len_insert += (p_editor_data->display_line_lengths[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; } 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; - } - else - { - 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 + 1; + *p_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; - - last_display_line++; - (p_editor_data->display_line_total)++; - (p_editor_data->data_line_total)++; } - - for (i = display_line; len_insert > 0 && i <= last_display_line; i++) + else // insert str into current data line at offset_data_line { - len = split_line(buf_insert, SCREEN_COLS, &eol, &display_len_insert); - if (len != len_insert) - { - 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; - } + log_error("Insert %d chars into display_line = %d, offset = %d\n", str_len, display_line, offset); - 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'; + 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'; - len = split_line(buf_catenate, SCREEN_COLS - display_len_insert, &eol, &display_len); - if (len < offset) // have no space to insert - { - offset = 0; - continue; // retry at next display line - } + // 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; - // 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) + *p_display_line = display_line; + *p_offset = offset + str_len; + } + + // 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); + log_error("Debug: split data line, display_line = %ld, j = %ld\n", display_line, split_line_total); + + for (i = 0; i < split_line_total; i++) + { + if (display_line + i > last_display_line) { - len--; + // 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)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'; + 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])); - offset = 0; - len_insert = p_editor_data->display_line_lengths[i] - len; - if (len_insert > 0) + 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') { - memcpy(buf_insert, p_editor_data->p_display_lines[i] + len, (size_t)len_insert); - buf_insert[len_insert] = '\0'; + log_error("Debug: reach end of data line, i = %ld, j = %ld\n", i, split_line_total); + break; } - - memcpy(p_editor_data->p_display_lines[i], buf_catenate, (size_t)len_catenate); - p_editor_data->display_line_lengths[i] = len_catenate; } - *p_last_updated_line = MAX(i, *p_last_updated_line); + *p_last_updated_line = MAX(display_line + split_line_total - 1, *p_last_updated_line); - if (len_insert > 0) + 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')) { - if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) - { - 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; - } + *p_offset -= p_editor_data->display_line_lengths[*p_display_line]; + (*p_display_line)++; - // Prepare one blank display line after last_display_line - for (i = p_editor_data->display_line_total; i > last_display_line + 1; i--) + if (*p_display_line >= p_editor_data->display_line_total) { - 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]; + log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total); } - 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++; - - // 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; - - *p_last_updated_line = MAX(last_display_line, *p_last_updated_line); } return 0; @@ -389,22 +371,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 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,9 +402,9 @@ 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] " @@ -453,80 +440,89 @@ int editor_display(EDITOR_DATA *p_editor goto cleanup; } - if (ch >= 32 && ch < 127) // printable character + if (ch > 127 && ch <= 255) // GBK + { + input_str[str_len] = (char)(ch - 256); + str_len++; + } + else + { + str_len = 0; + } + + if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2)) // printable character or GBK { + if (str_len == 0) + { + input_str[0] = (char)ch; + str_len = 1; + } + 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 (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 (editor_data_insert(p_editor_data, &display_line_out, &offset_out, + input_str, str_len, &last_updated_line) < 0) { - log_error("editor_data_op(INSERT ch) error\n"); + log_error("editor_data_insert(%s) error\n", input_str); + str_len = 0; } 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; + str_len = 0; - col_pos++; - if (col_pos <= p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]) - { - continue; - } - col_pos = 1; - ch = KEY_DOWN; - } - } - else if (ch > 127 && ch <= 255) // CJK character - { - hz_ch[hz_len] = (char)(ch - 256); - hz_len++; + 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 (hz_len == 2) // GBK - { - hz_len = 0; - last_updated_line = line_current; + scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row)); - if (editor_data_insert(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1, - hz_ch, 2, &last_updated_line) < 0) + if (scroll_rows > 0) { - log_error("editor_data_op(INSERT hz) error\n"); - } - 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; + 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; + + continue; } } else if (ch == KEY_DEL) // Del { last_updated_line = line_current; - if (editor_data_delete(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1, + if (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)); + screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); } continue; } - hz_len = 0; - switch (ch) { case KEY_NULL: @@ -539,15 +535,15 @@ 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; @@ -555,28 +551,28 @@ int editor_display(EDITOR_DATA *p_editor 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 +582,84 @@ 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])); + 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 CR: 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 +674,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; @@ -710,11 +707,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: