| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#include "editor.h" |
|
| 17 |
#include "bbs.h" |
#include "bbs.h" |
| 18 |
|
#include "common.h" |
| 19 |
|
#include "editor.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
#include "log.h" |
#include "log.h" |
| 22 |
#include "common.h" |
#include "memory_pool.h" |
| 23 |
#include "str_process.h" |
#include "str_process.h" |
| 24 |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <sys/param.h> |
|
|
#include <strings.h> |
|
|
|
|
|
#define _POSIX_C_SOURCE 200809L |
|
| 25 |
#include <string.h> |
#include <string.h> |
| 26 |
|
#include <sys/param.h> |
| 27 |
|
|
| 28 |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
| 29 |
|
#define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000 |
| 30 |
|
#define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1) |
| 31 |
|
|
| 32 |
|
static MEMORY_POOL *p_mp_data_line; |
| 33 |
|
static MEMORY_POOL *p_mp_editor_data; |
| 34 |
|
|
| 35 |
|
int editor_memory_pool_init(void) |
| 36 |
|
{ |
| 37 |
|
if (p_mp_data_line != NULL || p_mp_editor_data != NULL) |
| 38 |
|
{ |
| 39 |
|
log_error("Editor mem pool already initialized\n"); |
| 40 |
|
return -1; |
| 41 |
|
} |
| 42 |
|
|
| 43 |
|
p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT); |
| 44 |
|
if (p_mp_data_line == NULL) |
| 45 |
|
{ |
| 46 |
|
log_error("Memory pool init error\n"); |
| 47 |
|
return -2; |
| 48 |
|
} |
| 49 |
|
|
| 50 |
|
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
| 51 |
|
if (p_mp_data_line == NULL) |
| 52 |
|
{ |
| 53 |
|
log_error("Memory pool init error\n"); |
| 54 |
|
return -3; |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
return 0; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
void editor_memory_pool_cleanup(void) |
| 61 |
|
{ |
| 62 |
|
if (p_mp_data_line != NULL) |
| 63 |
|
{ |
| 64 |
|
memory_pool_cleanup(p_mp_data_line); |
| 65 |
|
p_mp_data_line = NULL; |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
if (p_mp_editor_data != NULL) |
| 69 |
|
{ |
| 70 |
|
memory_pool_cleanup(p_mp_editor_data); |
| 71 |
|
p_mp_editor_data = NULL; |
| 72 |
|
} |
| 73 |
|
} |
| 74 |
|
|
| 75 |
EDITOR_DATA *editor_data_load(const char *p_data) |
EDITOR_DATA *editor_data_load(const char *p_data) |
| 76 |
{ |
{ |
| 77 |
EDITOR_DATA *p_editor_data; |
EDITOR_DATA *p_editor_data; |
| 78 |
char *p_data_line = NULL; |
char *p_data_line = NULL; |
| 79 |
long line_offsets[MAX_EDITOR_DATA_LINES]; |
long line_offsets[MAX_EDITOR_DATA_LINES + 1]; |
| 80 |
long current_data_line_length = 0; |
long current_data_line_length = 0; |
| 81 |
long i; |
long i; |
| 82 |
|
|
| 86 |
return NULL; |
return NULL; |
| 87 |
} |
} |
| 88 |
|
|
| 89 |
p_editor_data = malloc(sizeof(EDITOR_DATA)); |
p_editor_data = memory_pool_alloc(p_mp_editor_data); |
| 90 |
if (p_editor_data == NULL) |
if (p_editor_data == NULL) |
| 91 |
{ |
{ |
| 92 |
log_error("malloc(EDITOR_DATA) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 93 |
return NULL; |
return NULL; |
| 94 |
} |
} |
| 95 |
|
|
| 96 |
p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES); |
p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, |
| 97 |
|
0, p_editor_data->display_line_widths); |
| 98 |
|
|
| 99 |
for (i = 0; i < p_editor_data->display_line_total; i++) |
for (i = 0; i < p_editor_data->display_line_total; i++) |
| 100 |
{ |
{ |
| 105 |
(p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n')) |
(p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n')) |
| 106 |
{ |
{ |
| 107 |
// Allocate new data line |
// Allocate new data line |
| 108 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 109 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 110 |
{ |
{ |
| 111 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i); |
log_error("memory_pool_alloc() error: i = %d\n", i); |
| 112 |
// Cleanup |
// Cleanup |
| 113 |
editor_data_cleanup(p_editor_data); |
editor_data_cleanup(p_editor_data); |
| 114 |
return NULL; |
return NULL; |
| 124 |
|
|
| 125 |
memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); |
memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); |
| 126 |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
| 127 |
|
|
| 128 |
|
// Trim \n from last line |
| 129 |
|
if (i + 1 == p_editor_data->display_line_total && |
| 130 |
|
p_editor_data->display_line_lengths[i] > 0 && |
| 131 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
| 132 |
|
{ |
| 133 |
|
p_editor_data->display_line_lengths[i]--; |
| 134 |
|
p_editor_data->display_line_widths[i]--; |
| 135 |
|
current_data_line_length--; |
| 136 |
|
} |
| 137 |
p_data_line[current_data_line_length] = '\0'; |
p_data_line[current_data_line_length] = '\0'; |
| 138 |
} |
} |
| 139 |
|
|
| 140 |
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); |
memset(p_editor_data->p_display_lines + p_editor_data->display_line_total, 0, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total); |
| 141 |
|
|
| 142 |
return p_editor_data; |
return p_editor_data; |
| 143 |
} |
} |
| 191 |
if (p_editor_data->display_line_lengths[i] > 0 && |
if (p_editor_data->display_line_lengths[i] > 0 && |
| 192 |
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
| 193 |
{ |
{ |
| 194 |
free(p_data_line); |
memory_pool_free(p_mp_data_line, p_data_line); |
| 195 |
p_data_line = NULL; |
p_data_line = NULL; |
| 196 |
} |
} |
| 197 |
} |
} |
| 198 |
|
|
| 199 |
if (p_data_line != NULL) |
if (p_data_line != NULL) |
| 200 |
{ |
{ |
| 201 |
free(p_data_line); |
memory_pool_free(p_mp_data_line, p_data_line); |
| 202 |
} |
} |
| 203 |
|
|
| 204 |
free(p_editor_data); |
memory_pool_free(p_mp_editor_data, p_editor_data); |
| 205 |
} |
} |
| 206 |
|
|
| 207 |
int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
| 214 |
long offset_data_line; |
long offset_data_line; |
| 215 |
long last_display_line; // of data line |
long last_display_line; // of data line |
| 216 |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 217 |
|
int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 218 |
long split_line_total; |
long split_line_total; |
| 219 |
long i, j; |
long i; |
| 220 |
int len; |
int len; |
| 221 |
int eol; |
int eol; |
| 222 |
int display_len; |
int display_len; |
| 227 |
return -1; |
return -1; |
| 228 |
} |
} |
| 229 |
|
|
|
// 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--; |
|
|
} |
|
|
|
|
| 230 |
// Get length of current data line |
// Get length of current data line |
| 231 |
len_data_line = 0; |
len_data_line = 0; |
| 232 |
p_data_line = p_editor_data->p_display_lines[display_line]; |
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 256 |
} |
} |
| 257 |
|
|
| 258 |
// Split current data line if over-length |
// Split current data line if over-length |
| 259 |
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) |
| 260 |
{ |
{ |
| 261 |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
| 262 |
{ |
{ |
| 263 |
// log_error("Split line error, display_line_total(%ld) reach limit(%d)\n", |
#ifdef _DEBUG |
| 264 |
// p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
log_error("Split line error, display_line_total(%ld) reach limit(%d)\n", |
| 265 |
|
p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
| 266 |
|
#endif |
| 267 |
|
|
| 268 |
return -2; |
return -2; |
| 269 |
} |
} |
| 270 |
|
|
| 271 |
// Allocate new data line |
// Allocate new data line |
| 272 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 273 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 274 |
{ |
{ |
| 275 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 276 |
return -2; |
return -2; |
| 277 |
} |
} |
| 278 |
|
|
| 279 |
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) |
| 280 |
{ |
{ |
| 281 |
if (str[0] == CR) |
if (str[0] == CR) |
| 282 |
{ |
{ |
| 322 |
*p_offset = offset + str_len; |
*p_offset = offset + str_len; |
| 323 |
} |
} |
| 324 |
|
|
| 325 |
|
// Update display width of current display line |
| 326 |
|
len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0); |
| 327 |
|
p_editor_data->display_line_widths[display_line] = display_len; |
| 328 |
|
|
| 329 |
split_line_total = last_display_line - display_line + 3; |
split_line_total = last_display_line - display_line + 3; |
| 330 |
|
|
| 331 |
// Set start display_line for spliting new data line |
// Set start display_line for spliting new data line |
| 348 |
} |
} |
| 349 |
|
|
| 350 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 351 |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths); |
| 352 |
|
|
| 353 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 354 |
{ |
{ |
| 357 |
// Insert blank display line after last_display_line |
// Insert blank display line after last_display_line |
| 358 |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
| 359 |
{ |
{ |
| 360 |
// log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
#ifdef _DEBUG |
| 361 |
|
log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
| 362 |
|
#endif |
| 363 |
|
|
| 364 |
// Terminate prior display line with \n, to avoid error on cleanup |
// Terminate prior display line with \n, to avoid error on cleanup |
| 365 |
if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0) |
if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0) |
| 366 |
{ |
{ |
| 367 |
len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len); |
len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0); |
| 368 |
p_editor_data->p_display_lines[display_line + i - 1][len] = '\n'; |
p_editor_data->p_display_lines[display_line + i - 1][len] = '\n'; |
| 369 |
p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0'; |
p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0'; |
| 370 |
p_editor_data->display_line_lengths[display_line + i - 1] = len + 1; |
p_editor_data->display_line_lengths[display_line + i - 1] = len + 1; |
| 371 |
|
p_editor_data->display_line_widths[display_line + i - 1] = display_len; |
| 372 |
} |
} |
| 373 |
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
| 374 |
{ |
{ |
| 376 |
} |
} |
| 377 |
break; |
break; |
| 378 |
} |
} |
| 379 |
for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) |
|
| 380 |
{ |
// for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) |
| 381 |
p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
// { |
| 382 |
p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; |
// p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
| 383 |
} |
// p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; |
| 384 |
|
// p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1]; |
| 385 |
|
// } |
| 386 |
|
memmove(p_editor_data->p_display_lines + last_display_line + 2, |
| 387 |
|
p_editor_data->p_display_lines + last_display_line + 1, |
| 388 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 389 |
|
sizeof(p_editor_data->p_display_lines[last_display_line + 1])); |
| 390 |
|
memmove(p_editor_data->display_line_lengths + last_display_line + 2, |
| 391 |
|
p_editor_data->display_line_lengths + last_display_line + 1, |
| 392 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 393 |
|
sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); |
| 394 |
|
memmove(p_editor_data->display_line_widths + last_display_line + 2, |
| 395 |
|
p_editor_data->display_line_widths + last_display_line + 1, |
| 396 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 397 |
|
sizeof(p_editor_data->display_line_widths[last_display_line + 1])); |
| 398 |
|
|
| 399 |
last_display_line++; |
last_display_line++; |
| 400 |
|
*p_last_updated_line = p_editor_data->display_line_total; |
| 401 |
(p_editor_data->display_line_total)++; |
(p_editor_data->display_line_total)++; |
| 402 |
} |
} |
| 403 |
|
|
| 404 |
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
| 405 |
|
p_editor_data->display_line_widths[display_line + i] = line_widths[i]; |
| 406 |
p_editor_data->p_display_lines[display_line + i] = |
p_editor_data->p_display_lines[display_line + i] = |
| 407 |
(i == 0 |
(i == 0 |
| 408 |
? p_data_line |
? p_data_line |
| 419 |
|
|
| 420 |
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
| 421 |
{ |
{ |
| 422 |
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
if (*p_display_line + 1 < p_editor_data->display_line_total) |
|
|
|
|
if (*p_display_line + 1 >= p_editor_data->display_line_total) |
|
| 423 |
{ |
{ |
| 424 |
log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total); |
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
| 425 |
|
(*p_display_line)++; |
| 426 |
} |
} |
| 427 |
else |
} |
| 428 |
|
|
| 429 |
|
// Prevent the last display line from being over-length |
| 430 |
|
if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES) |
| 431 |
|
{ |
| 432 |
|
len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0); |
| 433 |
|
p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0'; |
| 434 |
|
p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len; |
| 435 |
|
p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len; |
| 436 |
|
if (*p_display_line + 1 >= p_editor_data->display_line_total) |
| 437 |
{ |
{ |
| 438 |
(*p_display_line)++; |
*p_offset = MIN(*p_offset, len); |
| 439 |
|
*p_display_line = p_editor_data->display_line_total - 1; |
| 440 |
} |
} |
| 441 |
} |
} |
| 442 |
|
|
| 443 |
return 0; |
return 0; |
| 444 |
} |
} |
| 445 |
|
|
| 446 |
int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset, |
int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
| 447 |
long *p_last_updated_line) |
long *p_last_updated_line) |
| 448 |
{ |
{ |
| 449 |
|
long display_line = *p_display_line; |
| 450 |
|
long offset = *p_offset; |
| 451 |
char *p_data_line = NULL; |
char *p_data_line = NULL; |
| 452 |
long len_data_line; |
long len_data_line; |
| 453 |
long offset_data_line; |
long offset_data_line; |
| 454 |
long last_display_line; // of data line |
long last_display_line; // of data line |
| 455 |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 456 |
|
int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 457 |
long split_line_total; |
long split_line_total; |
| 458 |
long i, j; |
long i, j; |
| 459 |
int str_len = 0; |
int str_len = 0; |
| 460 |
|
char c; |
| 461 |
|
|
| 462 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 463 |
{ |
{ |
| 465 |
return -1; |
return -1; |
| 466 |
} |
} |
| 467 |
|
|
|
// 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--; |
|
|
} |
|
|
|
|
| 468 |
// Get length of current data line |
// Get length of current data line |
| 469 |
len_data_line = 0; |
len_data_line = 0; |
| 470 |
p_data_line = p_editor_data->p_display_lines[display_line]; |
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 493 |
} |
} |
| 494 |
} |
} |
| 495 |
|
|
| 496 |
|
if (offset_data_line >= len_data_line) // end-of-line |
| 497 |
|
{ |
| 498 |
|
return 0; |
| 499 |
|
} |
| 500 |
|
|
| 501 |
// Check str to be deleted |
// Check str to be deleted |
| 502 |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
| 503 |
{ |
{ |
| 504 |
str_len = 1; |
str_len = 1; |
| 505 |
} |
} |
| 506 |
else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK |
else if (p_data_line[offset_data_line] & 0b10000000) // head of multi-byte character |
| 507 |
{ |
{ |
| 508 |
str_len = 2; |
str_len = 1; |
| 509 |
|
c = (p_data_line[offset_data_line] & 0b01110000) << 1; |
| 510 |
|
while (c & 0b10000000) |
| 511 |
|
{ |
| 512 |
|
str_len++; |
| 513 |
|
c = (c & 0b01111111) << 1; |
| 514 |
|
} |
| 515 |
} |
} |
| 516 |
else |
else |
| 517 |
{ |
{ |
| 518 |
log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n", |
log_error("Some strange character at display_line %ld, offset %ld: %d %d\n", |
| 519 |
display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1], |
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]); |
|
| 520 |
str_len = 1; |
str_len = 1; |
| 521 |
} |
} |
| 522 |
|
|
| 553 |
p_data_line[offset_data_line + len_data_line] = '\0'; |
p_data_line[offset_data_line + len_data_line] = '\0'; |
| 554 |
|
|
| 555 |
// Recycle next data line |
// Recycle next data line |
| 556 |
free(p_editor_data->p_display_lines[display_line + 1]); |
memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]); |
| 557 |
} |
} |
| 558 |
else |
else |
| 559 |
{ |
{ |
| 567 |
split_line_total = last_display_line - display_line + 2; |
split_line_total = last_display_line - display_line + 2; |
| 568 |
|
|
| 569 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 570 |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths); |
| 571 |
|
|
| 572 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 573 |
{ |
{ |
| 574 |
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
| 575 |
|
p_editor_data->display_line_widths[display_line + i] = line_widths[i]; |
| 576 |
p_editor_data->p_display_lines[display_line + i] = |
p_editor_data->p_display_lines[display_line + i] = |
| 577 |
(i == 0 |
(i == 0 |
| 578 |
? p_data_line |
? p_data_line |
| 587 |
|
|
| 588 |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
| 589 |
|
|
| 590 |
if (display_line + i < last_display_line) |
if (*p_last_updated_line < last_display_line) |
| 591 |
{ |
{ |
| 592 |
// Remove redundant display line after last_display_line |
// Remove redundant display line after last_display_line |
| 593 |
for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++) |
// for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++) |
| 594 |
{ |
// { |
| 595 |
p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j]; |
// p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j]; |
| 596 |
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_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j]; |
| 597 |
} |
// p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j]; |
| 598 |
|
// } |
| 599 |
(p_editor_data->display_line_total) -= (last_display_line - (display_line + i)); |
memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1, |
| 600 |
last_display_line = display_line + i; |
p_editor_data->p_display_lines + last_display_line + 1, |
| 601 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 602 |
*p_last_updated_line = p_editor_data->display_line_total - 1; |
sizeof(p_editor_data->p_display_lines[last_display_line + 1])); |
| 603 |
|
memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1, |
| 604 |
|
p_editor_data->display_line_lengths + last_display_line + 1, |
| 605 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 606 |
|
sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); |
| 607 |
|
memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1, |
| 608 |
|
p_editor_data->display_line_widths + last_display_line + 1, |
| 609 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 610 |
|
sizeof(p_editor_data->display_line_widths[last_display_line + 1])); |
| 611 |
|
|
| 612 |
|
j = p_editor_data->display_line_total; |
| 613 |
|
(p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line); |
| 614 |
|
*p_last_updated_line = MAX(j - 1, *p_last_updated_line); |
| 615 |
} |
} |
| 616 |
|
|
| 617 |
|
// Return real offset |
| 618 |
|
*p_offset = offset; |
| 619 |
|
|
| 620 |
return str_len; |
return str_len; |
| 621 |
} |
} |
| 622 |
|
|
| 626 |
{ |
{ |
| 627 |
case 0: // Set msg |
case 0: // Set msg |
| 628 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 629 |
"| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |"); |
"| 退出[\033[32mCtrl-W\033[33m] |"); |
| 630 |
|
break; |
| 631 |
|
case KEY_CSI: |
| 632 |
|
*p_key = KEY_ESC; |
| 633 |
break; |
break; |
| 634 |
} |
} |
| 635 |
|
|
| 643 |
EDITOR_CTX ctx; |
EDITOR_CTX ctx; |
| 644 |
int ch = 0; |
int ch = 0; |
| 645 |
char input_str[4]; |
char input_str[4]; |
| 646 |
|
char c; |
| 647 |
int str_len = 0; |
int str_len = 0; |
| 648 |
int input_ok; |
int input_ok; |
| 649 |
const int screen_begin_row = 1; |
const int screen_begin_row = 1; |
| 671 |
return ch; |
return ch; |
| 672 |
} |
} |
| 673 |
|
|
| 674 |
loop = 1; |
for (loop = 1; !SYS_server_exit && loop;) |
|
while (!SYS_server_exit && loop) |
|
| 675 |
{ |
{ |
| 676 |
if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row) |
if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row) |
| 677 |
{ |
{ |
| 679 |
|
|
| 680 |
snprintf(buffer, sizeof(buffer), |
snprintf(buffer, sizeof(buffer), |
| 681 |
"\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] " |
"\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] " |
| 682 |
"µÚ\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] " |
| 683 |
"%s", |
"%s", |
| 684 |
row_pos, col_pos, |
row_pos, col_pos, |
| 685 |
ctx.line_cursor, p_editor_data->display_line_total, |
ctx.line_cursor, p_editor_data->display_line_total, |
| 686 |
key_insert ? "²åÈë" : "¸Äд", |
key_insert ? "æ’å…¥" : "替æ¢", |
| 687 |
ctx.msg); |
ctx.msg); |
| 688 |
|
|
| 689 |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1); |
| 690 |
for (; display_len < SCREEN_COLS; display_len++) |
for (; display_len < SCREEN_COLS; display_len++) |
| 691 |
{ |
{ |
| 692 |
buffer[len++] = ' '; |
buffer[len++] = ' '; |
| 700 |
moveto((int)row_pos, (int)col_pos); |
moveto((int)row_pos, (int)col_pos); |
| 701 |
iflush(); |
iflush(); |
| 702 |
|
|
| 703 |
input_ok = 0; |
str_len = 0; |
| 704 |
while (!SYS_server_exit && !input_ok) |
ch = igetch_t(MAX_DELAY_TIME); |
| 705 |
|
while (!SYS_server_exit) |
| 706 |
{ |
{ |
|
ch = igetch_t(MAX_DELAY_TIME); |
|
|
input_ok = 1; |
|
|
|
|
| 707 |
// extended key handler |
// extended key handler |
| 708 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 709 |
{ |
{ |
| 710 |
goto cleanup; |
goto cleanup; |
| 711 |
} |
} |
| 712 |
|
|
| 713 |
if (ch > 127 && ch <= 255) // GBK |
if (ch < 256 && (ch & 0b10000000)) // head of multi-byte character |
|
{ |
|
|
input_str[str_len] = (char)(ch - 256); |
|
|
str_len++; |
|
|
} |
|
|
else |
|
| 714 |
{ |
{ |
| 715 |
str_len = 0; |
str_len = 0; |
| 716 |
|
c = (char)(ch & 0b11110000); |
| 717 |
|
while (c & 0b10000000) |
| 718 |
|
{ |
| 719 |
|
input_str[str_len] = (char)(ch - 256); |
| 720 |
|
str_len++; |
| 721 |
|
c = (c & 0b01111111) << 1; |
| 722 |
|
|
| 723 |
|
if ((c & 0b10000000) == 0) // Input completed |
| 724 |
|
{ |
| 725 |
|
break; |
| 726 |
|
} |
| 727 |
|
|
| 728 |
|
// Expect additional bytes of input |
| 729 |
|
ch = igetch(100); // 0.1 second |
| 730 |
|
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input |
| 731 |
|
{ |
| 732 |
|
#ifdef _DEBUG |
| 733 |
|
log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len); |
| 734 |
|
#endif |
| 735 |
|
str_len = 0; |
| 736 |
|
break; |
| 737 |
|
} |
| 738 |
|
} |
| 739 |
} |
} |
| 740 |
|
|
| 741 |
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK |
if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character |
| 742 |
ch == CR || ch == KEY_ESC) // Special character |
ch == CR || ch == KEY_ESC) // Special character |
| 743 |
{ |
{ |
| 744 |
if (str_len == 0) |
BBS_last_access_tm = time(NULL); |
| 745 |
|
|
| 746 |
|
if (str_len == 0) // ch >= 32 && ch < 127 |
| 747 |
{ |
{ |
| 748 |
input_str[0] = (char)ch; |
input_str[0] = (char)ch; |
| 749 |
str_len = 1; |
str_len = 1; |
| 750 |
} |
} |
| 751 |
|
|
|
last_updated_line = line_current; |
|
| 752 |
display_line_in = line_current - output_current_row + row_pos; |
display_line_in = line_current - output_current_row + row_pos; |
| 753 |
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); |
| 754 |
display_line_out = display_line_in; |
display_line_out = display_line_in; |
| 755 |
offset_out = offset_in; |
offset_out = offset_in; |
| 756 |
|
|
| 757 |
|
last_updated_line = display_line_in; |
| 758 |
|
|
| 759 |
if (!key_insert) // overwrite |
if (!key_insert) // overwrite |
| 760 |
{ |
{ |
| 761 |
if (editor_data_delete(p_editor_data, display_line_in, offset_in, |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 762 |
&last_updated_line) < 0) |
&last_updated_line) < 0) |
| 763 |
{ |
{ |
| 764 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 769 |
input_str, str_len, &last_updated_line) < 0) |
input_str, str_len, &last_updated_line) < 0) |
| 770 |
{ |
{ |
| 771 |
log_error("editor_data_insert(str_len=%d) error\n", str_len); |
log_error("editor_data_insert(str_len=%d) error\n", str_len); |
|
str_len = 0; |
|
| 772 |
} |
} |
| 773 |
else |
else |
| 774 |
{ |
{ |
|
str_len = 0; |
|
|
|
|
| 775 |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
| 776 |
line_current -= (output_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 777 |
output_current_row = (int)row_pos; |
output_current_row = (int)row_pos; |
| 784 |
clrtoeol(); |
clrtoeol(); |
| 785 |
for (i = 0; i < scroll_rows; i++) |
for (i = 0; i < scroll_rows; i++) |
| 786 |
{ |
{ |
| 787 |
prints("\033[S"); // Scroll up 1 line |
// prints("\033[S"); // Scroll up 1 line |
| 788 |
|
prints("\n"); // Legacy Cterm only works with this line |
| 789 |
} |
} |
| 790 |
|
|
| 791 |
output_current_row -= scroll_rows; |
output_current_row -= scroll_rows; |
| 800 |
{ |
{ |
| 801 |
row_pos += (display_line_out - display_line_in); |
row_pos += (display_line_out - display_line_in); |
| 802 |
} |
} |
| 803 |
col_pos = offset_out + 1; |
|
| 804 |
|
if (offset_out != offset_in) |
| 805 |
|
{ |
| 806 |
|
if (display_line_out != display_line_in) |
| 807 |
|
{ |
| 808 |
|
col_pos = 1; |
| 809 |
|
} |
| 810 |
|
if (ch != CR) |
| 811 |
|
{ |
| 812 |
|
col_pos += (str_len == 1 ? 1 : 2); |
| 813 |
|
} |
| 814 |
|
} |
| 815 |
|
} |
| 816 |
|
|
| 817 |
|
if (display_line_out != display_line_in) // Output on line change |
| 818 |
|
{ |
| 819 |
|
break; |
| 820 |
} |
} |
| 821 |
|
|
| 822 |
|
ch = igetch(0); |
| 823 |
|
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input |
| 824 |
|
{ |
| 825 |
|
break; |
| 826 |
|
} |
| 827 |
|
|
| 828 |
|
str_len = 0; |
| 829 |
continue; |
continue; |
| 830 |
} |
} |
| 831 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
| 832 |
{ |
{ |
| 833 |
|
BBS_last_access_tm = time(NULL); |
| 834 |
|
|
| 835 |
if (ch == BACKSPACE) |
if (ch == BACKSPACE) |
| 836 |
{ |
{ |
| 837 |
col_pos--; |
if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden |
| 838 |
|
{ |
| 839 |
|
break; // force output prior operation result if any |
| 840 |
|
} |
| 841 |
|
|
| 842 |
|
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
| 843 |
|
(int)col_pos - 1, &eol, &display_len, 0); |
| 844 |
|
if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 |
| 845 |
|
{ |
| 846 |
|
col_pos = display_len - 1; |
| 847 |
|
} |
| 848 |
|
else |
| 849 |
|
{ |
| 850 |
|
col_pos = display_len; |
| 851 |
|
} |
| 852 |
|
|
| 853 |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
| 854 |
{ |
{ |
| 855 |
row_pos--; |
row_pos--; |
| 856 |
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]); |
| 857 |
} |
} |
| 858 |
} |
} |
| 859 |
|
|
| 860 |
if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1, |
display_line_in = line_current - output_current_row + row_pos; |
| 861 |
|
offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0); |
| 862 |
|
display_line_out = display_line_in; |
| 863 |
|
offset_out = offset_in; |
| 864 |
|
|
| 865 |
|
if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 866 |
&last_updated_line)) < 0) |
&last_updated_line)) < 0) |
| 867 |
{ |
{ |
| 868 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 869 |
} |
} |
| 870 |
else |
else |
| 871 |
{ |
{ |
| 872 |
if (ch == BACKSPACE) |
col_pos = display_len + 1; // Set col_pos to accurate pos |
|
{ |
|
|
for (i = 1; i < str_len; i++) |
|
|
{ |
|
|
col_pos--; |
|
|
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]); |
|
|
} |
|
|
} |
|
|
} |
|
| 873 |
|
|
| 874 |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
| 875 |
line_current -= (output_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 893 |
row_pos += scroll_rows; |
row_pos += scroll_rows; |
| 894 |
output_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 895 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
|
clrline(output_current_row, SCREEN_ROWS); |
|
| 896 |
} |
} |
| 897 |
|
|
| 898 |
|
clrline(output_current_row, output_end_row); |
| 899 |
|
} |
| 900 |
|
|
| 901 |
|
if (display_line_out != display_line_in) // Output on line change |
| 902 |
|
{ |
| 903 |
|
break; |
| 904 |
} |
} |
| 905 |
|
|
| 906 |
|
ch = igetch(0); |
| 907 |
|
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input |
| 908 |
|
{ |
| 909 |
|
break; |
| 910 |
|
} |
| 911 |
|
|
| 912 |
|
str_len = 0; |
| 913 |
continue; |
continue; |
| 914 |
} |
} |
| 915 |
|
|
| 916 |
|
input_ok = 1; |
| 917 |
switch (ch) |
switch (ch) |
| 918 |
{ |
{ |
| 919 |
case KEY_NULL: |
case KEY_NULL: |
| 920 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 921 |
goto cleanup; |
goto cleanup; |
| 922 |
case Ctrl('C'): |
case Ctrl('W'): |
| 923 |
loop = 0; |
loop = 0; |
| 924 |
break; |
break; |
| 925 |
case Ctrl('S'): // Start of line |
case Ctrl('S'): // Start of line |
| 928 |
break; |
break; |
| 929 |
case Ctrl('E'): // End of line |
case Ctrl('E'): // End of line |
| 930 |
case KEY_CTRL_RIGHT: |
case KEY_CTRL_RIGHT: |
| 931 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line |
| 932 |
|
{ |
| 933 |
|
// last display line does NOT have \n in the end |
| 934 |
|
col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; |
| 935 |
|
break; |
| 936 |
|
} |
| 937 |
|
col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]); |
| 938 |
break; |
break; |
| 939 |
case Ctrl('T'): // Top of screen |
case Ctrl('T'): // Top of screen |
| 940 |
case KEY_CTRL_UP: |
case KEY_CTRL_UP: |
| 941 |
row_pos = screen_begin_row; |
row_pos = screen_begin_row; |
| 942 |
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])); |
| 943 |
break; |
break; |
| 944 |
case Ctrl('B'): // Bottom of screen |
case Ctrl('B'): // Bottom of screen |
| 945 |
case KEY_CTRL_DOWN: |
case KEY_CTRL_DOWN: |
| 946 |
row_pos = SCREEN_ROWS - 1; |
if (p_editor_data->display_line_total < screen_row_total) |
| 947 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
{ |
| 948 |
|
row_pos = p_editor_data->display_line_total; |
| 949 |
|
} |
| 950 |
|
else |
| 951 |
|
{ |
| 952 |
|
row_pos = SCREEN_ROWS - 1; |
| 953 |
|
} |
| 954 |
|
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
| 955 |
|
{ |
| 956 |
|
// last display line does NOT have \n in the end |
| 957 |
|
col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1); |
| 958 |
|
} |
| 959 |
|
else |
| 960 |
|
{ |
| 961 |
|
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos])); |
| 962 |
|
} |
| 963 |
break; |
break; |
| 964 |
case KEY_INS: |
case KEY_INS: |
| 965 |
key_insert = !key_insert; |
key_insert = !key_insert; |
| 980 |
if (p_editor_data->display_line_total < screen_row_total) |
if (p_editor_data->display_line_total < screen_row_total) |
| 981 |
{ |
{ |
| 982 |
row_pos = p_editor_data->display_line_total; |
row_pos = p_editor_data->display_line_total; |
| 983 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; |
| 984 |
break; |
break; |
| 985 |
} |
} |
| 986 |
line_current = p_editor_data->display_line_total - screen_row_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 987 |
output_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 988 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 989 |
row_pos = SCREEN_ROWS - 1; |
row_pos = SCREEN_ROWS - 1; |
| 990 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; |
| 991 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 992 |
break; |
break; |
| 993 |
case KEY_LEFT: |
case KEY_LEFT: |
| 994 |
if (col_pos > 1) |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
| 995 |
|
(int)col_pos - 1, &eol, &display_len, 0); |
| 996 |
|
if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 |
| 997 |
|
{ |
| 998 |
|
col_pos = display_len - 1; |
| 999 |
|
} |
| 1000 |
|
else |
| 1001 |
|
{ |
| 1002 |
|
col_pos = display_len; |
| 1003 |
|
} |
| 1004 |
|
if (col_pos >= 1) |
| 1005 |
{ |
{ |
|
col_pos--; |
|
| 1006 |
break; |
break; |
| 1007 |
} |
} |
| 1008 |
col_pos = SCREEN_COLS; // continue to KEY_UP |
col_pos = SCREEN_COLS; // continue to KEY_UP |
| 1010 |
if (row_pos > screen_begin_row) |
if (row_pos > screen_begin_row) |
| 1011 |
{ |
{ |
| 1012 |
row_pos--; |
row_pos--; |
| 1013 |
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])); |
| 1014 |
break; |
break; |
| 1015 |
} |
} |
| 1016 |
if (line_current - output_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |
| 1023 |
// screen_end_line = begin_line; |
// screen_end_line = begin_line; |
| 1024 |
// prints("\033[T"); // Scroll down 1 line |
// prints("\033[T"); // Scroll down 1 line |
| 1025 |
output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line |
output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line |
| 1026 |
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])); |
| 1027 |
break; |
break; |
| 1028 |
case KEY_SPACE: |
case KEY_SPACE: |
| 1029 |
break; |
break; |
| 1030 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 1031 |
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], |
| 1032 |
|
(int)col_pos - 1, &eol, &display_len, 0); |
| 1033 |
|
if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] && |
| 1034 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8 |
| 1035 |
|
{ |
| 1036 |
|
col_pos = display_len + 3; |
| 1037 |
|
} |
| 1038 |
|
else |
| 1039 |
|
{ |
| 1040 |
|
col_pos = display_len + 2; |
| 1041 |
|
} |
| 1042 |
|
if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos]) |
| 1043 |
{ |
{ |
|
col_pos++; |
|
| 1044 |
break; |
break; |
| 1045 |
} |
} |
| 1046 |
col_pos = 1; // continue to KEY_DOWN |
col_pos = 1; // continue to KEY_DOWN |
| 1048 |
if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total)) |
if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total)) |
| 1049 |
{ |
{ |
| 1050 |
row_pos++; |
row_pos++; |
| 1051 |
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])); |
| 1052 |
break; |
break; |
| 1053 |
} |
} |
| 1054 |
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line |
| 1055 |
{ |
{ |
| 1056 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
// last display line does NOT have \n in the end |
| 1057 |
|
col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; |
| 1058 |
break; |
break; |
| 1059 |
} |
} |
| 1060 |
line_current += (screen_row_total - (output_current_row - screen_begin_row)); |
line_current += (screen_row_total - (output_current_row - screen_begin_row)); |
| 1061 |
output_current_row = screen_row_total; |
output_current_row = screen_row_total; |
| 1062 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1063 |
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])); |
| 1064 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 1065 |
clrtoeol(); |
clrtoeol(); |
| 1066 |
prints("\033[S"); // Scroll up 1 line |
// prints("\033[S"); // Scroll up 1 line |
| 1067 |
|
prints("\n"); // Legacy Cterm only works with this line |
| 1068 |
break; |
break; |
| 1069 |
case KEY_PGUP: |
case KEY_PGUP: |
| 1070 |
if (line_current - output_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |
| 1078 |
} |
} |
| 1079 |
output_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1080 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1081 |
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])); |
| 1082 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 1083 |
break; |
break; |
| 1084 |
case KEY_PGDN: |
case KEY_PGDN: |
| 1093 |
} |
} |
| 1094 |
output_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1095 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1096 |
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])); |
| 1097 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 1098 |
break; |
break; |
| 1099 |
case KEY_F1: |
case KEY_F1: |
| 1119 |
break; |
break; |
| 1120 |
} |
} |
| 1121 |
|
|
| 1122 |
BBS_last_access_tm = time(0); |
BBS_last_access_tm = time(NULL); |
| 1123 |
|
|
| 1124 |
if (input_ok) |
if (input_ok) |
| 1125 |
{ |
{ |
| 1126 |
break; |
break; |
| 1127 |
} |
} |
| 1128 |
|
|
| 1129 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 1130 |
} |
} |
| 1131 |
|
|
| 1132 |
continue; |
continue; |