--- lbbs/src/editor.c 2025/06/11 04:27:48 1.4 +++ lbbs/src/editor.c 2025/06/11 11:01:07 1.10 @@ -169,7 +169,7 @@ int editor_data_insert(EDITOR_DATA *p_ed // 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++; } @@ -208,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) { @@ -227,28 +227,13 @@ int editor_data_insert(EDITOR_DATA *p_ed p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; (p_editor_data->data_line_total)++; - if (offset_data_line + str_len + 2 < MAX_EDITOR_DATA_LINE_LENGTH) + if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) { - // 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; + if (str[0] == CR) + { + str_len = 0; + } - *p_display_line = display_line; - *p_offset = offset + str_len; - } - else - { // Copy str to new data line memcpy(p_data_line, str, (size_t)str_len); @@ -267,6 +252,26 @@ int editor_data_insert(EDITOR_DATA *p_ed *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; @@ -277,8 +282,6 @@ int editor_data_insert(EDITOR_DATA *p_ed } else // insert str into current data line at offset_data_line { - log_error("Insert %d chars into display_line = %d, offset = %d\n", str_len, display_line, offset); - 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'; @@ -293,7 +296,6 @@ int editor_data_insert(EDITOR_DATA *p_ed // 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++) { @@ -323,12 +325,11 @@ int editor_data_insert(EDITOR_DATA *p_ed 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') { - log_error("Debug: reach end of data line, i = %ld, j = %ld\n", i, split_line_total); break; } } - *p_last_updated_line = MAX(display_line + split_line_total - 1, *p_last_updated_line); + *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line); 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] && @@ -349,7 +350,164 @@ int editor_data_insert(EDITOR_DATA *p_ed int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset, long *p_last_updated_line) { - return 0; + 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 + { + i++; + } + } + if (i > offset) // offset was skipped + { + offset--; + } + + // 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]; + + 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; + } + } + + // Check str to be deleted + if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) + { + 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_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total); + return 0; + } + + 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++) + { + 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; + } + } + + 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; + } + + // 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'; + + // 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; + } + + // 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; + + // 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) @@ -387,7 +545,7 @@ int editor_display(EDITOR_DATA *p_editor 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_row = screen_begin_row; @@ -408,10 +566,11 @@ int editor_display(EDITOR_DATA *p_editor 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); @@ -450,7 +609,7 @@ int editor_display(EDITOR_DATA *p_editor str_len = 0; } - 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) || ch == CR) // printable character or GBK { if (str_len == 0) { @@ -464,6 +623,15 @@ int editor_display(EDITOR_DATA *p_editor 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) + { + log_error("editor_data_delete() error\n"); + } + } + if (editor_data_insert(p_editor_data, &display_line_out, &offset_out, input_str, str_len, &last_updated_line) < 0) { @@ -506,18 +674,48 @@ int editor_display(EDITOR_DATA *p_editor 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_row + 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_delete() error\n"); } else { + 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; + } } continue; @@ -546,7 +744,7 @@ int editor_display(EDITOR_DATA *p_editor 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; @@ -600,8 +798,6 @@ int editor_display(EDITOR_DATA *p_editor 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: