--- lbbs/src/editor.c 2025/06/27 10:14:47 1.32 +++ lbbs/src/editor.c 2025/11/04 13:49:51 1.51 @@ -1,24 +1,17 @@ -/*************************************************************************** - editor.h - description - ------------------- - copyright : (C) 2004-2025 by Leaflet - email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * editor + * - user interactive full-screen text editor + * + * Copyright (C) 2004-2025 by Leaflet + */ #include "bbs.h" #include "common.h" #include "editor.h" #include "io.h" #include "log.h" +#include "login.h" #include "memory_pool.h" #include "str_process.h" #include @@ -48,7 +41,7 @@ int editor_memory_pool_init(void) } p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); - if (p_mp_data_line == NULL) + if (p_mp_editor_data == NULL) { log_error("Memory pool init error\n"); return -3; @@ -82,7 +75,7 @@ EDITOR_DATA *editor_data_load(const char if (p_data == NULL) { - log_error("editor_data_load() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return NULL; } @@ -93,7 +86,8 @@ EDITOR_DATA *editor_data_load(const char return NULL; } - p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, 0); + p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, + 0, p_editor_data->display_line_widths); for (i = 0; i < p_editor_data->display_line_total; i++) { @@ -130,6 +124,7 @@ EDITOR_DATA *editor_data_load(const char p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') { p_editor_data->display_line_lengths[i]--; + p_editor_data->display_line_widths[i]--; current_data_line_length--; } p_data_line[current_data_line_length] = '\0'; @@ -147,7 +142,7 @@ long editor_data_save(const EDITOR_DATA if (p_editor_data == NULL || p_data == NULL) { - log_error("editor_data_save() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } @@ -212,6 +207,7 @@ int editor_data_insert(EDITOR_DATA *p_ed long offset_data_line; long last_display_line; // of data line long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; + int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1]; long split_line_total; long i; int len; @@ -220,31 +216,10 @@ int editor_data_insert(EDITOR_DATA *p_ed if (p_editor_data == NULL || p_last_updated_line == NULL) { - log_error("editor_data_op() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } - // Validate str - if ((str_len == 1 && str[0] <= 0) || - (str_len == 2 && (str[0] >= 0 || str[1] >= 0))) - { - log_error("Invalid input str, len=%d\n", str_len); - return -2; - } - - // 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 - { - 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]; @@ -274,7 +249,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 || str[0] == CR) + if (len_data_line + str_len + 2 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) { if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) { @@ -294,7 +269,7 @@ int editor_data_insert(EDITOR_DATA *p_ed return -2; } - 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) { if (str[0] == CR) { @@ -340,6 +315,10 @@ int editor_data_insert(EDITOR_DATA *p_ed *p_offset = offset + str_len; } + // Update display width of current display line + len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0); + p_editor_data->display_line_widths[display_line] = display_len; + split_line_total = last_display_line - display_line + 3; // Set start display_line for spliting new data line @@ -362,7 +341,7 @@ 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, 0); + split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths); for (i = 0; i < split_line_total; i++) { @@ -382,6 +361,7 @@ int editor_data_insert(EDITOR_DATA *p_ed p_editor_data->p_display_lines[display_line + i - 1][len] = '\n'; p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0'; p_editor_data->display_line_lengths[display_line + i - 1] = len + 1; + p_editor_data->display_line_widths[display_line + i - 1] = display_len; } if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) { @@ -394,6 +374,7 @@ int editor_data_insert(EDITOR_DATA *p_ed // { // 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]; + // p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1]; // } memmove(p_editor_data->p_display_lines + last_display_line + 2, p_editor_data->p_display_lines + last_display_line + 1, @@ -403,6 +384,10 @@ int editor_data_insert(EDITOR_DATA *p_ed p_editor_data->display_line_lengths + last_display_line + 1, (size_t)(p_editor_data->display_line_total - last_display_line - 1) * sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); + memmove(p_editor_data->display_line_widths + last_display_line + 2, + p_editor_data->display_line_widths + last_display_line + 1, + (size_t)(p_editor_data->display_line_total - last_display_line - 1) * + sizeof(p_editor_data->display_line_widths[last_display_line + 1])); last_display_line++; *p_last_updated_line = p_editor_data->display_line_total; @@ -410,6 +395,7 @@ int editor_data_insert(EDITOR_DATA *p_ed } p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; + p_editor_data->display_line_widths[display_line + i] = line_widths[i]; p_editor_data->p_display_lines[display_line + i] = (i == 0 ? p_data_line @@ -439,6 +425,7 @@ int editor_data_insert(EDITOR_DATA *p_ed len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0); p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0'; p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len; + p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len; if (*p_display_line + 1 >= p_editor_data->display_line_total) { *p_offset = MIN(*p_offset, len); @@ -450,7 +437,7 @@ int editor_data_insert(EDITOR_DATA *p_ed } int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, - long *p_last_updated_line) + long *p_last_updated_line, int del_line) { long display_line = *p_display_line; long offset = *p_offset; @@ -459,29 +446,18 @@ int editor_data_delete(EDITOR_DATA *p_ed long offset_data_line; long last_display_line; // of data line long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; + int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1]; long split_line_total; long i, j; int str_len = 0; + char c; if (p_editor_data == NULL || p_last_updated_line == NULL) { - log_error("editor_data_op() error: NULL pointer\n"); + log_error("NULL pointer error\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) // 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]; @@ -516,13 +492,23 @@ int editor_data_delete(EDITOR_DATA *p_ed } // Check str to be deleted - if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) + if (del_line) + { + str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset); + } + else 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) // GBK + else if (p_data_line[offset_data_line] & 0x80) // head of multi-byte character { - str_len = 2; + str_len = 1; + c = (p_data_line[offset_data_line] & 0x70) << 1; + while (c & 0x80) + { + str_len++; + c = (c & 0x7f) << 1; + } } else { @@ -533,7 +519,8 @@ int editor_data_delete(EDITOR_DATA *p_ed // 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')) + (offset_data_line + str_len == len_data_line && + p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n')) { if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line) { @@ -578,11 +565,12 @@ int editor_data_delete(EDITOR_DATA *p_ed 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, 0); + split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths); 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->display_line_widths[display_line + i] = line_widths[i]; p_editor_data->p_display_lines[display_line + i] = (i == 0 ? p_data_line @@ -604,6 +592,7 @@ int editor_data_delete(EDITOR_DATA *p_ed // { // p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j]; // 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_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j]; // } memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1, p_editor_data->p_display_lines + last_display_line + 1, @@ -613,6 +602,10 @@ int editor_data_delete(EDITOR_DATA *p_ed p_editor_data->display_line_lengths + last_display_line + 1, (size_t)(p_editor_data->display_line_total - last_display_line - 1) * sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); + memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1, + p_editor_data->display_line_widths + last_display_line + 1, + (size_t)(p_editor_data->display_line_total - last_display_line - 1) * + sizeof(p_editor_data->display_line_widths[last_display_line + 1])); j = p_editor_data->display_line_total; (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line); @@ -631,7 +624,7 @@ static int editor_display_key_handler(in { case 0: // Set msg snprintf(p_ctx->msg, sizeof(p_ctx->msg), - "| ˳[\033[32mCtrl-W\033[33m] |"); + "| 退出[\033[32mCtrl-W\033[33m] |"); break; case KEY_CSI: *p_key = KEY_ESC; @@ -648,6 +641,7 @@ int editor_display(EDITOR_DATA *p_editor EDITOR_CTX ctx; int ch = 0; char input_str[4]; + char c; int str_len = 0; int input_ok; const int screen_begin_row = 1; @@ -666,6 +660,7 @@ int editor_display(EDITOR_DATA *p_editor int key_insert = 1; int i, j; char *p_str; + int del_line; clrline(output_current_row, SCREEN_ROWS); @@ -675,8 +670,7 @@ int editor_display(EDITOR_DATA *p_editor return ch; } - loop = 1; - while (!SYS_server_exit && loop) + for (loop = 1; !SYS_server_exit && loop;) { if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row) { @@ -684,11 +678,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%s\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 ? "" : "滻", + key_insert ? "插入" : "替换", ctx.msg); len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1); @@ -706,31 +700,56 @@ int editor_display(EDITOR_DATA *p_editor iflush(); str_len = 0; - input_ok = 0; ch = igetch_t(MAX_DELAY_TIME); - while (!SYS_server_exit && !input_ok) + while (!SYS_server_exit) { + if (ch != KEY_NULL && ch != KEY_TIMEOUT) + { + BBS_last_access_tm = time(NULL); + } + // extended key handler if (editor_display_key_handler(&ch, &ctx) != 0) { goto cleanup; } - if (ch > 127 && ch <= 255) // GBK + if (ch < 256 && (ch & 0x80)) // head of multi-byte character { - input_str[str_len] = (char)(ch - 256); - str_len++; - } - else if (str_len > 0) - { - log_error("Received %d character over 127 followed by character less than 127\n", str_len); str_len = 0; + c = (char)(ch & 0xf0); + while (c & 0x80) + { + input_str[str_len] = (char)(ch - 256); + str_len++; + c = (c & 0x7f) << 1; + + if ((c & 0x80) == 0) // Input completed + { + break; + } + + // Expect additional bytes of input + ch = igetch(100); // 0.1 second + if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input + { +#ifdef _DEBUG + log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len); +#endif + str_len = 0; + break; + } + } } - if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK - ch == CR || ch == KEY_ESC) // Special character + if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character + ch == CR || ch == KEY_ESC) // Special character { - BBS_last_access_tm = time(NULL); + // Refresh current action while user input + if (user_online_update(NULL) < 0) + { + log_error("user_online_update(NULL) error\n"); + } if (str_len == 0) // ch >= 32 && ch < 127 { @@ -739,7 +758,7 @@ int editor_display(EDITOR_DATA *p_editor } display_line_in = line_current - output_current_row + row_pos; - offset_in = col_pos - 1; + offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0); display_line_out = display_line_in; offset_out = offset_in; @@ -748,7 +767,7 @@ int editor_display(EDITOR_DATA *p_editor if (!key_insert) // overwrite { if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, - &last_updated_line) < 0) + &last_updated_line, 0) < 0) { log_error("editor_data_delete() error\n"); } @@ -773,7 +792,8 @@ int editor_display(EDITOR_DATA *p_editor clrtoeol(); for (i = 0; i < scroll_rows; i++) { - prints("\033[S"); // Scroll up 1 line + // prints("\033[S"); // Scroll up 1 line + prints("\n"); // Legacy Cterm only works with this line } output_current_row -= scroll_rows; @@ -788,7 +808,18 @@ int editor_display(EDITOR_DATA *p_editor { row_pos += (display_line_out - display_line_in); } - col_pos = offset_out + 1; // Set col_pos to accurate pos + + if (offset_out != offset_in) + { + if (display_line_out != display_line_in) + { + col_pos = 1; + } + if (offset_out > 0) + { + col_pos += (str_len == 1 ? 1 : 2); + } + } } if (display_line_out != display_line_in) // Output on line change @@ -805,45 +836,63 @@ int editor_display(EDITOR_DATA *p_editor str_len = 0; continue; } - else if (ch == KEY_DEL || ch == BACKSPACE) // Del + else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del { - BBS_last_access_tm = time(NULL); + // Refresh current action while user input + if (user_online_update(NULL) < 0) + { + log_error("user_online_update(NULL) error\n"); + } + + del_line = 0; if (ch == BACKSPACE) { if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden { - ch = igetch_t(MAX_DELAY_TIME); - continue; + break; // force output prior operation result if any } - col_pos--; - if (col_pos > 1 && - p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK + offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], + (int)col_pos - 1, &eol, &display_len, 0); + if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 { - col_pos--; + col_pos = display_len - 1; + } + else + { + col_pos = display_len; } 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]); + col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]); } } + else if (ch == Ctrl('K')) + { + del_line = 1; + } + else if (ch == Ctrl('Y')) + { + col_pos = 1; + del_line = 1; + } display_line_in = line_current - output_current_row + row_pos; - offset_in = col_pos - 1; + offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0); display_line_out = display_line_in; offset_out = offset_in; if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, - &last_updated_line)) < 0) + &last_updated_line, del_line)) < 0) { - log_error("editor_data_delete() error\n"); + log_error("editor_data_delete() error: %d\n", str_len); } else { - col_pos = offset_out + 1; // Set col_pos to accurate pos + col_pos = display_len + 1; // Set col_pos to accurate pos output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); line_current -= (output_current_row - row_pos); @@ -891,9 +940,13 @@ int editor_display(EDITOR_DATA *p_editor switch (ch) { case KEY_NULL: + log_error("KEY_NULL\n"); + goto cleanup; case KEY_TIMEOUT: + log_error("User input timeout\n"); goto cleanup; case Ctrl('W'): + case Ctrl('X'): loop = 0; break; case Ctrl('S'): // Start of line @@ -905,15 +958,15 @@ int editor_display(EDITOR_DATA *p_editor if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line { // last display line does NOT have \n in the end - col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; + col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; break; } - 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_widths[line_current - output_current_row + row_pos]); break; case Ctrl('T'): // Top of screen case KEY_CTRL_UP: row_pos = screen_begin_row; - 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_widths[line_current - output_current_row + row_pos])); break; case Ctrl('B'): // Bottom of screen case KEY_CTRL_DOWN: @@ -928,11 +981,11 @@ int editor_display(EDITOR_DATA *p_editor if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end { // last display line does NOT have \n in the end - col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1); + col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1); } else { - 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_widths[line_current - output_current_row + row_pos])); } break; case KEY_INS: @@ -954,26 +1007,29 @@ int editor_display(EDITOR_DATA *p_editor if (p_editor_data->display_line_total < screen_row_total) { row_pos = p_editor_data->display_line_total; - col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; + col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; break; } line_current = p_editor_data->display_line_total - screen_row_total; output_current_row = screen_begin_row; output_end_row = SCREEN_ROWS - 1; row_pos = SCREEN_ROWS - 1; - col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; + col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; clrline(output_current_row, SCREEN_ROWS); break; case KEY_LEFT: - if (col_pos > 1) + offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], + (int)col_pos - 1, &eol, &display_len, 0); + if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 + { + col_pos = display_len - 1; + } + else + { + col_pos = display_len; + } + if (col_pos >= 1) { - col_pos--; - if (col_pos > 1 && - p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && - p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK - { - col_pos--; - } break; } col_pos = SCREEN_COLS; // continue to KEY_UP @@ -981,7 +1037,7 @@ int editor_display(EDITOR_DATA *p_editor if (row_pos > screen_begin_row) { row_pos--; - 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_widths[line_current - output_current_row + row_pos])); break; } if (line_current - output_current_row < 0) // Reach begin @@ -994,19 +1050,22 @@ int editor_display(EDITOR_DATA *p_editor // screen_end_line = begin_line; // prints("\033[T"); // Scroll down 1 line output_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 - output_current_row + row_pos])); - break; - case KEY_SPACE: + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos])); break; case KEY_RIGHT: - if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]) + offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], + (int)col_pos - 1, &eol, &display_len, 0); + if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] && + p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8 + { + col_pos = display_len + 3; + } + else + { + col_pos = display_len + 2; + } + if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos]) { - if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && - p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK - { - col_pos++; - } - col_pos++; break; } col_pos = 1; // continue to KEY_DOWN @@ -1014,22 +1073,23 @@ int editor_display(EDITOR_DATA *p_editor 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 - output_current_row + row_pos])); + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos])); break; } if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line { // last display line does NOT have \n in the end - col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; + col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; break; } line_current += (screen_row_total - (output_current_row - screen_begin_row)); output_current_row = screen_row_total; output_end_row = SCREEN_ROWS - 1; - 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_widths[line_current - output_current_row + row_pos])); moveto(SCREEN_ROWS, 0); clrtoeol(); - prints("\033[S"); // Scroll up 1 line + // prints("\033[S"); // Scroll up 1 line + prints("\n"); // Legacy Cterm only works with this line break; case KEY_PGUP: if (line_current - output_current_row < 0) // Reach begin @@ -1043,7 +1103,7 @@ int editor_display(EDITOR_DATA *p_editor } output_current_row = screen_begin_row; output_end_row = SCREEN_ROWS - 1; - 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_widths[line_current - output_current_row + row_pos])); clrline(output_current_row, SCREEN_ROWS); break; case KEY_PGDN: @@ -1058,17 +1118,18 @@ int editor_display(EDITOR_DATA *p_editor } output_current_row = screen_begin_row; output_end_row = SCREEN_ROWS - 1; - 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_widths[line_current - output_current_row + row_pos])); clrline(output_current_row, SCREEN_ROWS); break; + case Ctrl('Q'): case KEY_F1: - if (!show_help) // Not reentrant + if (!show_help) // Not re-entrant { break; } // Display help information show_help = 0; - display_file(DATA_READ_HELP, 1); + display_file(DATA_EDITOR_HELP, 1); show_help = 1; case KEY_F5: // Refresh after display help information @@ -1084,7 +1145,11 @@ int editor_display(EDITOR_DATA *p_editor break; } - BBS_last_access_tm = time(NULL); + // Refresh current action while user input + if (user_online_update(NULL) < 0) + { + log_error("user_online_update(NULL) error\n"); + } if (input_ok) {