| 22 |
#include "str_process.h" |
#include "str_process.h" |
| 23 |
#include <stdlib.h> |
#include <stdlib.h> |
| 24 |
#include <sys/param.h> |
#include <sys/param.h> |
| 25 |
|
#include <strings.h> |
| 26 |
|
|
| 27 |
#define _POSIX_C_SOURCE 200809L |
#define _POSIX_C_SOURCE 200809L |
| 28 |
#include <string.h> |
#include <string.h> |
| 29 |
|
|
| 30 |
|
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
| 31 |
|
|
| 32 |
EDITOR_DATA *editor_data_load(const char *p_data) |
EDITOR_DATA *editor_data_load(const char *p_data) |
| 33 |
{ |
{ |
| 34 |
EDITOR_DATA *p_editor_data; |
EDITOR_DATA *p_editor_data; |
| 35 |
|
char *p_data_line = NULL; |
| 36 |
long line_offsets[MAX_EDITOR_DATA_LINES]; |
long line_offsets[MAX_EDITOR_DATA_LINES]; |
| 37 |
long current_data_line_length = 0; |
long current_data_line_length = 0; |
| 38 |
long i, j; |
long i, j; |
| 68 |
} |
} |
| 69 |
|
|
| 70 |
// Allocate new data line |
// Allocate new data line |
| 71 |
p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
| 72 |
if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL) |
if (p_data_line == NULL) |
| 73 |
{ |
{ |
| 74 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i); |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i); |
| 75 |
// Cleanup |
// Cleanup |
| 80 |
free(p_editor_data); |
free(p_editor_data); |
| 81 |
return NULL; |
return NULL; |
| 82 |
} |
} |
| 83 |
|
p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; |
|
p_editor_data->p_display_lines[i] = p_editor_data->p_data_lines[p_editor_data->data_line_total]; |
|
| 84 |
(p_editor_data->data_line_total)++; |
(p_editor_data->data_line_total)++; |
| 85 |
|
|
| 86 |
|
p_editor_data->p_display_lines[i] = p_data_line; |
| 87 |
current_data_line_length = 0; |
current_data_line_length = 0; |
| 88 |
} |
} |
| 89 |
else |
else |
| 93 |
|
|
| 94 |
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]); |
| 95 |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
| 96 |
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'; |
| 97 |
} |
} |
| 98 |
|
|
| 99 |
|
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); |
| 100 |
|
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); |
| 101 |
|
|
| 102 |
return p_editor_data; |
return p_editor_data; |
| 103 |
} |
} |
| 104 |
|
|
| 149 |
free(p_editor_data); |
free(p_editor_data); |
| 150 |
} |
} |
| 151 |
|
|
| 152 |
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, |
| 153 |
const char *str, int str_len, long *p_last_updated_line) |
const char *str, int str_len, long *p_last_updated_line) |
| 154 |
{ |
{ |
| 155 |
int len; |
long display_line = *p_display_line; |
| 156 |
int display_len; |
long offset = *p_offset; |
| 157 |
int eol; |
char *p_data_line = NULL; |
|
char *p_data_line; |
|
| 158 |
long len_data_line; |
long len_data_line; |
| 159 |
long offset_data_line; |
long offset_data_line; |
| 160 |
long last_display_line; // of data line |
long last_display_line; // of data line |
| 161 |
char buf_insert[MAX_EDITOR_DATA_LINE_LENGTH]; |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 162 |
long len_insert; |
long split_line_total; |
| 163 |
int display_len_insert; |
long i, j; |
|
char buf_catenate[MAX_EDITOR_DATA_LINE_LENGTH]; |
|
|
long len_catenate; |
|
|
long i; |
|
| 164 |
|
|
| 165 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 166 |
{ |
{ |
| 168 |
return -1; |
return -1; |
| 169 |
} |
} |
| 170 |
|
|
|
memcpy(buf_insert, str, (size_t)str_len); |
|
|
buf_insert[str_len] = '\0'; |
|
|
len_insert = str_len; |
|
|
|
|
| 171 |
// Get accurate offset of first character of CJK at offset position |
// Get accurate offset of first character of CJK at offset position |
| 172 |
for (i = 0; i < offset; i++) |
for (i = 0; i < offset; i++) |
| 173 |
{ |
{ |
| 174 |
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 |
| 175 |
{ |
{ |
| 176 |
i++; |
i++; |
| 177 |
} |
} |
| 210 |
} |
} |
| 211 |
|
|
| 212 |
// Split current data line if over-length |
// Split current data line if over-length |
| 213 |
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) |
| 214 |
{ |
{ |
| 215 |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES || p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES) |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES || p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES) |
| 216 |
{ |
{ |
| 220 |
} |
} |
| 221 |
|
|
| 222 |
// Allocate new data line |
// Allocate new data line |
| 223 |
p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
| 224 |
if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL) |
if (p_data_line == NULL) |
| 225 |
{ |
{ |
| 226 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); |
| 227 |
return -2; |
return -2; |
| 228 |
} |
} |
| 229 |
|
p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; |
| 230 |
|
(p_editor_data->data_line_total)++; |
| 231 |
|
|
| 232 |
// Copy rest part of current data line since next display line to new data line |
if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
|
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++) |
|
| 233 |
{ |
{ |
| 234 |
p_editor_data->p_display_lines[i] += |
if (str[0] == CR) |
| 235 |
(p_editor_data->p_data_lines[p_editor_data->data_line_total] - p_data_line); |
{ |
| 236 |
} |
str_len = 0; |
| 237 |
|
} |
| 238 |
|
|
| 239 |
// Copy rest part of current display line to buffer |
// Copy str to new data line |
| 240 |
if (offset_data_line >= MAX_EDITOR_DATA_LINE_LENGTH / 2) |
memcpy(p_data_line, str, (size_t)str_len); |
| 241 |
{ |
|
| 242 |
memcpy(buf_insert + len_insert, |
// Copy rest part of current data line to new data line |
| 243 |
p_editor_data->p_display_lines[display_line] + offset, |
memcpy(p_data_line + str_len, |
|
(size_t)(p_editor_data->display_line_lengths[display_line] - offset)); |
|
|
len_insert += (p_editor_data->display_line_lengths[display_line] - offset); |
|
|
} |
|
|
else |
|
|
{ |
|
|
memcpy(buf_insert, |
|
| 244 |
p_editor_data->p_display_lines[display_line] + offset, |
p_editor_data->p_display_lines[display_line] + offset, |
| 245 |
(size_t)(p_editor_data->display_line_lengths[display_line] - offset)); |
(size_t)(len_data_line - offset_data_line)); |
| 246 |
len_insert = (p_editor_data->display_line_lengths[display_line] - offset); |
|
| 247 |
} |
p_data_line[str_len + len_data_line - offset_data_line] = '\0'; |
|
buf_insert[len_insert] = '\0'; |
|
| 248 |
|
|
|
if (offset_data_line >= MAX_EDITOR_DATA_LINE_LENGTH / 2) |
|
|
{ |
|
| 249 |
// Add line ending to current display line (data line) |
// Add line ending to current display line (data line) |
| 250 |
p_editor_data->p_display_lines[display_line][offset] = '\n'; |
p_editor_data->p_display_lines[display_line][offset] = '\n'; |
| 251 |
p_editor_data->p_display_lines[display_line][offset + 1] = '\0'; |
p_editor_data->p_display_lines[display_line][offset + 1] = '\0'; |
| 252 |
p_editor_data->display_line_lengths[display_line] = offset + 1; |
p_editor_data->display_line_lengths[display_line] = offset + 1; |
| 253 |
|
|
| 254 |
|
*p_display_line = display_line + 1; |
| 255 |
|
*p_offset = str_len; |
| 256 |
} |
} |
| 257 |
else |
else |
| 258 |
{ |
{ |
| 259 |
|
// Copy rest part of current data line to new data line |
| 260 |
|
memcpy(p_data_line, |
| 261 |
|
p_editor_data->p_display_lines[display_line] + offset, |
| 262 |
|
(size_t)(len_data_line - offset_data_line)); |
| 263 |
|
|
| 264 |
|
p_data_line[len_data_line - offset_data_line] = '\0'; |
| 265 |
|
|
| 266 |
|
// Append str to current display line |
| 267 |
memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len); |
memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len); |
| 268 |
|
|
| 269 |
// Add line ending to current display line (data line) |
// Add line ending to current display line (data line) |
| 270 |
p_editor_data->p_display_lines[display_line][offset + str_len] = '\n'; |
p_editor_data->p_display_lines[display_line][offset + str_len] = '\n'; |
| 271 |
p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0'; |
p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0'; |
| 272 |
p_editor_data->display_line_lengths[display_line] = offset + str_len + 1; |
p_editor_data->display_line_lengths[display_line] = offset + str_len + 1; |
| 273 |
|
|
| 274 |
|
*p_display_line = display_line; |
| 275 |
|
*p_offset = offset + str_len; |
| 276 |
} |
} |
| 277 |
|
|
| 278 |
|
split_line_total = last_display_line - display_line + 3; |
| 279 |
|
|
| 280 |
|
// Set start display_line for spliting new data line |
| 281 |
display_line++; |
display_line++; |
|
offset = 0; |
|
| 282 |
|
|
| 283 |
*p_last_updated_line = p_editor_data->display_line_total; |
*p_last_updated_line = p_editor_data->display_line_total; |
| 284 |
|
} |
| 285 |
|
else // insert str into current data line at offset_data_line |
| 286 |
|
{ |
| 287 |
|
memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line)); |
| 288 |
|
memcpy(p_data_line + offset_data_line, str, (size_t)str_len); |
| 289 |
|
p_data_line[len_data_line + str_len] = '\0'; |
| 290 |
|
|
| 291 |
last_display_line++; |
// Set p_data_line to head of current display line |
| 292 |
(p_editor_data->display_line_total)++; |
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 293 |
(p_editor_data->data_line_total)++; |
split_line_total = last_display_line - display_line + 3; |
| 294 |
|
|
| 295 |
|
*p_display_line = display_line; |
| 296 |
|
*p_offset = offset + str_len; |
| 297 |
} |
} |
| 298 |
|
|
| 299 |
for (i = display_line; len_insert > 0 && i <= last_display_line; i++) |
// Split current data line since beginning of current display line |
| 300 |
|
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); |
| 301 |
|
|
| 302 |
|
for (i = 0; i < split_line_total; i++) |
| 303 |
{ |
{ |
| 304 |
len = split_line(buf_insert, SCREEN_COLS, &eol, &display_len_insert); |
if (display_line + i > last_display_line) |
|
if (len != len_insert) |
|
| 305 |
{ |
{ |
| 306 |
log_error("buf_insert is truncated at display_line(%ld): len(%d) != len_insert(%d), buf_insert: %s\n", |
// Insert blank display line after last_display_line |
| 307 |
i, len, len_insert, buf_insert); |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
| 308 |
return -3; |
{ |
| 309 |
|
log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
| 310 |
|
return -3; |
| 311 |
|
} |
| 312 |
|
for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) |
| 313 |
|
{ |
| 314 |
|
p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
| 315 |
|
p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; |
| 316 |
|
} |
| 317 |
|
last_display_line++; |
| 318 |
|
(p_editor_data->display_line_total)++; |
| 319 |
} |
} |
| 320 |
|
|
| 321 |
memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)p_editor_data->display_line_lengths[i]); |
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
| 322 |
buf_catenate[p_editor_data->display_line_lengths[i]] = '\0'; |
p_editor_data->p_display_lines[display_line + i] = |
| 323 |
|
(i == 0 |
| 324 |
|
? p_data_line |
| 325 |
|
: (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1])); |
| 326 |
|
|
| 327 |
len = split_line(buf_catenate, SCREEN_COLS - display_len_insert, &eol, &display_len); |
if (p_editor_data->display_line_lengths[display_line + i] > 0 && |
| 328 |
if (len < offset) // have no space to insert |
p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n') |
| 329 |
{ |
{ |
| 330 |
offset = 0; |
break; |
|
continue; // retry at next display line |
|
| 331 |
} |
} |
| 332 |
|
} |
| 333 |
|
|
| 334 |
|
*p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line); |
| 335 |
|
|
| 336 |
// move \n to next display line if current line is full |
if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] || |
| 337 |
if (len > 0 && buf_catenate[len - 1] == '\n' && display_len + display_len_insert >= SCREEN_COLS) |
(*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] && |
| 338 |
|
p_editor_data->p_display_lines[*p_display_line][*p_offset - 1] == '\n')) |
| 339 |
|
{ |
| 340 |
|
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
| 341 |
|
(*p_display_line)++; |
| 342 |
|
|
| 343 |
|
if (*p_display_line >= p_editor_data->display_line_total) |
| 344 |
{ |
{ |
| 345 |
len--; |
log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total); |
| 346 |
} |
} |
| 347 |
|
} |
| 348 |
|
|
| 349 |
memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)offset); |
return 0; |
| 350 |
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'; |
|
| 351 |
|
|
| 352 |
offset = 0; |
int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset, |
| 353 |
len_insert = p_editor_data->display_line_lengths[i] - len; |
long *p_last_updated_line) |
| 354 |
if (len_insert > 0) |
{ |
| 355 |
|
char *p_data_line = NULL; |
| 356 |
|
long len_data_line; |
| 357 |
|
long offset_data_line; |
| 358 |
|
long last_display_line; // of data line |
| 359 |
|
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 360 |
|
long split_line_total; |
| 361 |
|
long i, j; |
| 362 |
|
int str_len = 0; |
| 363 |
|
|
| 364 |
|
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 365 |
|
{ |
| 366 |
|
log_error("editor_data_op() error: NULL pointer\n"); |
| 367 |
|
return -1; |
| 368 |
|
} |
| 369 |
|
|
| 370 |
|
// Get accurate offset of first character of CJK at offset position |
| 371 |
|
for (i = 0; i < offset; i++) |
| 372 |
|
{ |
| 373 |
|
if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK |
| 374 |
{ |
{ |
| 375 |
memcpy(buf_insert, p_editor_data->p_display_lines[i] + len, (size_t)len_insert); |
i++; |
|
buf_insert[len_insert] = '\0'; |
|
| 376 |
} |
} |
| 377 |
|
} |
| 378 |
|
if (i > offset) // offset was skipped |
| 379 |
|
{ |
| 380 |
|
offset--; |
| 381 |
|
} |
| 382 |
|
|
| 383 |
memcpy(p_editor_data->p_display_lines[i], buf_catenate, (size_t)len_catenate); |
// Get length of current data line |
| 384 |
p_editor_data->display_line_lengths[i] = len_catenate; |
len_data_line = 0; |
| 385 |
|
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 386 |
|
for (i = display_line - 1; i >= 0; i--) |
| 387 |
|
{ |
| 388 |
|
if (p_editor_data->display_line_lengths[i] > 0 && |
| 389 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line |
| 390 |
|
{ |
| 391 |
|
break; |
| 392 |
|
} |
| 393 |
|
|
| 394 |
|
len_data_line += p_editor_data->display_line_lengths[i]; |
| 395 |
|
p_data_line = p_editor_data->p_display_lines[i]; |
| 396 |
} |
} |
| 397 |
|
offset_data_line = len_data_line + offset; |
| 398 |
|
last_display_line = p_editor_data->display_line_total - 1; |
| 399 |
|
for (i = display_line; i < p_editor_data->display_line_total; i++) |
| 400 |
|
{ |
| 401 |
|
len_data_line += p_editor_data->display_line_lengths[i]; |
| 402 |
|
|
| 403 |
*p_last_updated_line = MAX(i, *p_last_updated_line); |
if (p_editor_data->display_line_lengths[i] > 0 && |
| 404 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line |
| 405 |
|
{ |
| 406 |
|
last_display_line = i; |
| 407 |
|
break; |
| 408 |
|
} |
| 409 |
|
} |
| 410 |
|
|
| 411 |
if (len_insert > 0) |
// Check str to be deleted |
| 412 |
|
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
| 413 |
{ |
{ |
| 414 |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
str_len = 1; |
| 415 |
|
} |
| 416 |
|
else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK |
| 417 |
|
{ |
| 418 |
|
str_len = 2; |
| 419 |
|
} |
| 420 |
|
else |
| 421 |
|
{ |
| 422 |
|
log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n", |
| 423 |
|
display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1], |
| 424 |
|
p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]); |
| 425 |
|
str_len = 1; |
| 426 |
|
} |
| 427 |
|
|
| 428 |
|
// Current display line is (almost) empty |
| 429 |
|
if (offset_data_line + str_len > len_data_line || |
| 430 |
|
(offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n')) |
| 431 |
|
{ |
| 432 |
|
if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line) |
| 433 |
{ |
{ |
| 434 |
log_error("Append line error, display_line_total(%ld) reach limit(%d)\n", |
log_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total); |
| 435 |
p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
return 0; |
|
return -2; |
|
| 436 |
} |
} |
| 437 |
|
|
| 438 |
// Prepare one blank display line after last_display_line |
len_data_line = 0; // Next data line |
| 439 |
for (i = p_editor_data->display_line_total; i > last_display_line + 1; i--) |
last_display_line = p_editor_data->display_line_total - 1; |
| 440 |
|
for (i = display_line + 1; i < p_editor_data->display_line_total; i++) |
| 441 |
{ |
{ |
| 442 |
p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1]; |
len_data_line += p_editor_data->display_line_lengths[i]; |
| 443 |
p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1]; |
|
| 444 |
|
if (p_editor_data->display_line_lengths[i] > 0 && |
| 445 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line |
| 446 |
|
{ |
| 447 |
|
last_display_line = i; |
| 448 |
|
break; |
| 449 |
|
} |
| 450 |
} |
} |
|
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; |
|
| 451 |
|
|
| 452 |
(p_editor_data->display_line_total)++; |
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 |
| 453 |
last_display_line++; |
{ |
| 454 |
|
log_common("Debug: No enough buffer to merge with next data line: %ld > %ld\n", |
| 455 |
|
offset_data_line + len_data_line + 1, MAX_EDITOR_DATA_LINE_LENGTH); |
| 456 |
|
return 0; |
| 457 |
|
} |
| 458 |
|
|
| 459 |
// Fill data into blank display line |
// Append next data line to current one |
| 460 |
memcpy(p_editor_data->p_display_lines[last_display_line], buf_insert, (size_t)len_insert); |
memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line); |
| 461 |
p_editor_data->p_display_lines[last_display_line][len_insert] = '\0'; |
p_data_line[offset_data_line + len_data_line] = '\0'; |
|
p_editor_data->display_line_lengths[last_display_line] = len_insert; |
|
| 462 |
|
|
| 463 |
*p_last_updated_line = MAX(last_display_line, *p_last_updated_line); |
// Recycle next data line |
| 464 |
|
// TODO: free(p_editor_data->p_display_lines[display_line + 1]); |
| 465 |
|
} |
| 466 |
|
else |
| 467 |
|
{ |
| 468 |
|
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)); |
| 469 |
|
p_data_line[len_data_line - str_len] = '\0'; |
| 470 |
|
len_data_line -= str_len; |
| 471 |
} |
} |
| 472 |
|
|
| 473 |
return 0; |
// Set p_data_line to head of current display line |
| 474 |
} |
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 475 |
|
split_line_total = last_display_line - display_line + 2; |
| 476 |
|
|
| 477 |
int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset, |
// Split current data line since beginning of current display line |
| 478 |
long *p_last_updated_line) |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); |
| 479 |
{ |
|
| 480 |
return 0; |
for (i = 0; i < split_line_total; i++) |
| 481 |
|
{ |
| 482 |
|
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
| 483 |
|
p_editor_data->p_display_lines[display_line + i] = |
| 484 |
|
(i == 0 |
| 485 |
|
? p_data_line |
| 486 |
|
: (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1])); |
| 487 |
|
|
| 488 |
|
if (p_editor_data->display_line_lengths[display_line + i] > 0 && |
| 489 |
|
p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n') |
| 490 |
|
{ |
| 491 |
|
break; |
| 492 |
|
} |
| 493 |
|
} |
| 494 |
|
|
| 495 |
|
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
| 496 |
|
|
| 497 |
|
if (display_line + i < last_display_line) |
| 498 |
|
{ |
| 499 |
|
// Remove redundant display line after last_display_line |
| 500 |
|
for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++) |
| 501 |
|
{ |
| 502 |
|
p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j]; |
| 503 |
|
p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j]; |
| 504 |
|
} |
| 505 |
|
|
| 506 |
|
(p_editor_data->display_line_total) -= (last_display_line - (display_line + i)); |
| 507 |
|
last_display_line = display_line + i; |
| 508 |
|
|
| 509 |
|
*p_last_updated_line = p_editor_data->display_line_total - 1; |
| 510 |
|
} |
| 511 |
|
|
| 512 |
|
return str_len; |
| 513 |
} |
} |
| 514 |
|
|
| 515 |
static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx) |
static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx) |
| 531 |
char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; |
char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; |
| 532 |
EDITOR_CTX ctx; |
EDITOR_CTX ctx; |
| 533 |
int ch = 0; |
int ch = 0; |
| 534 |
char hz_ch[4]; |
char input_str[4]; |
| 535 |
int hz_len; |
int str_len = 0; |
| 536 |
int input_ok, screen_current_line; |
int input_ok; |
| 537 |
const int screen_begin_line = 1; |
int screen_current_row; |
| 538 |
int screen_end_line = SCREEN_ROWS - 1; |
const int screen_begin_row = 1; |
| 539 |
const int screen_line_total = screen_end_line - screen_begin_line + 1; |
int screen_end_row = SCREEN_ROWS - 1; |
| 540 |
|
const int screen_row_total = screen_end_row - screen_begin_row + 1; |
| 541 |
long int line_current = 0; |
long int line_current = 0; |
| 542 |
long int len; |
long int len; |
| 543 |
int loop; |
int loop; |
| 544 |
int eol, display_len; |
int eol, display_len; |
| 545 |
long row_pos = 1, col_pos = 1; |
long row_pos = 1, col_pos = 1; |
| 546 |
|
long display_line_in, offset_in; |
| 547 |
|
long display_line_out, offset_out; |
| 548 |
|
int scroll_rows; |
| 549 |
long last_updated_line = 0; |
long last_updated_line = 0; |
| 550 |
int insert = 1; |
int key_insert = 1; |
| 551 |
|
int i, j; |
| 552 |
|
char *p_str; |
| 553 |
|
|
| 554 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 555 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 556 |
|
|
| 557 |
// update msg_ext with extended key handler |
// update msg_ext with extended key handler |
| 558 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 563 |
loop = 1; |
loop = 1; |
| 564 |
while (!SYS_server_exit && loop) |
while (!SYS_server_exit && loop) |
| 565 |
{ |
{ |
| 566 |
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) |
| 567 |
{ |
{ |
| 568 |
ctx.line_cursor = line_current - screen_current_line + row_pos + 1; |
ctx.line_cursor = line_current - screen_current_row + row_pos + 1; |
| 569 |
|
|
| 570 |
snprintf(buffer, sizeof(buffer), |
snprintf(buffer, sizeof(buffer), |
| 571 |
"\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] " |
| 572 |
"µÚ\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] " |
| 573 |
"%s", |
"%s", |
| 574 |
row_pos, col_pos, |
row_pos, col_pos, |
| 575 |
ctx.line_cursor, p_editor_data->display_line_total, |
ctx.line_cursor, p_editor_data->display_line_total, |
| 576 |
|
key_insert ? "²åÈë" : "¸Äд", |
| 577 |
ctx.msg); |
ctx.msg); |
| 578 |
|
|
| 579 |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
| 591 |
iflush(); |
iflush(); |
| 592 |
|
|
| 593 |
input_ok = 0; |
input_ok = 0; |
| 594 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 595 |
while (!SYS_server_exit && !input_ok) |
while (!SYS_server_exit && !input_ok) |
| 596 |
{ |
{ |
|
ch = igetch_t(MAX_DELAY_TIME); |
|
|
input_ok = 1; |
|
|
|
|
| 597 |
// extended key handler |
// extended key handler |
| 598 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 599 |
{ |
{ |
| 600 |
goto cleanup; |
goto cleanup; |
| 601 |
} |
} |
| 602 |
|
|
| 603 |
if (ch >= 32 && ch < 127) // printable character |
if (ch > 127 && ch <= 255) // GBK |
| 604 |
{ |
{ |
| 605 |
last_updated_line = line_current; |
input_str[str_len] = (char)(ch - 256); |
| 606 |
|
str_len++; |
| 607 |
|
} |
| 608 |
|
else |
| 609 |
|
{ |
| 610 |
|
str_len = 0; |
| 611 |
|
} |
| 612 |
|
|
| 613 |
if (editor_data_insert(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1, |
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK |
| 614 |
(const char *)&ch, 1, &last_updated_line) < 0) |
ch == CR || ch == KEY_ESC) // Special character |
| 615 |
|
{ |
| 616 |
|
if (str_len == 0) |
| 617 |
{ |
{ |
| 618 |
log_error("editor_data_op(INSERT ch) error\n"); |
input_str[0] = (char)ch; |
| 619 |
|
str_len = 1; |
| 620 |
} |
} |
|
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; |
|
| 621 |
|
|
| 622 |
col_pos++; |
last_updated_line = line_current; |
| 623 |
if (col_pos <= p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]) |
display_line_in = line_current - screen_current_row + row_pos; |
| 624 |
|
offset_in = col_pos - 1; |
| 625 |
|
display_line_out = display_line_in; |
| 626 |
|
offset_out = offset_in; |
| 627 |
|
|
| 628 |
|
if (!key_insert) // overwrite |
| 629 |
|
{ |
| 630 |
|
if (editor_data_delete(p_editor_data, display_line_in, offset_in, |
| 631 |
|
&last_updated_line) < 0) |
| 632 |
{ |
{ |
| 633 |
continue; |
log_error("editor_data_delete() error\n"); |
| 634 |
} |
} |
|
col_pos = 1; |
|
|
ch = KEY_DOWN; |
|
| 635 |
} |
} |
|
} |
|
|
else if (ch > 127 && ch <= 255) // CJK character |
|
|
{ |
|
|
hz_ch[hz_len] = (char)(ch - 256); |
|
|
hz_len++; |
|
| 636 |
|
|
| 637 |
if (hz_len == 2) // GBK |
if (editor_data_insert(p_editor_data, &display_line_out, &offset_out, |
| 638 |
|
input_str, str_len, &last_updated_line) < 0) |
| 639 |
|
{ |
| 640 |
|
log_error("editor_data_insert(%s) error\n", input_str); |
| 641 |
|
str_len = 0; |
| 642 |
|
} |
| 643 |
|
else |
| 644 |
{ |
{ |
| 645 |
hz_len = 0; |
str_len = 0; |
|
last_updated_line = line_current; |
|
| 646 |
|
|
| 647 |
if (editor_data_insert(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1, |
screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); |
| 648 |
hz_ch, 2, &last_updated_line) < 0) |
line_current -= (screen_current_row - row_pos); |
| 649 |
{ |
screen_current_row = (int)row_pos; |
| 650 |
log_error("editor_data_op(INSERT hz) error\n"); |
|
| 651 |
} |
scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row)); |
| 652 |
else |
|
| 653 |
|
if (scroll_rows > 0) |
| 654 |
{ |
{ |
| 655 |
screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current)); |
moveto(SCREEN_ROWS, 0); |
| 656 |
line_current -= (screen_current_line - row_pos); |
clrtoeol(); |
| 657 |
screen_current_line = (int)row_pos; |
for (i = 0; i < scroll_rows; i++) |
| 658 |
|
{ |
| 659 |
|
prints("\033[S"); // Scroll up 1 line |
| 660 |
|
} |
| 661 |
|
|
| 662 |
col_pos += 2; |
screen_current_row -= scroll_rows; |
| 663 |
if (col_pos <= p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]) |
if (screen_current_row < screen_begin_row) |
| 664 |
{ |
{ |
| 665 |
continue; |
line_current += (screen_begin_row - screen_current_row); |
| 666 |
|
screen_current_row = screen_begin_row; |
| 667 |
} |
} |
| 668 |
col_pos = 1; |
row_pos = screen_end_row; |
| 669 |
ch = KEY_DOWN; |
} |
| 670 |
|
else // if (scroll_lines == 0) |
| 671 |
|
{ |
| 672 |
|
row_pos += (display_line_out - display_line_in); |
| 673 |
} |
} |
| 674 |
|
col_pos = offset_out + 1; |
| 675 |
|
} |
| 676 |
|
|
| 677 |
|
// Check whether there is additional input |
| 678 |
|
ch = igetch(0); |
| 679 |
|
if (ch == KEY_TIMEOUT) |
| 680 |
|
{ |
| 681 |
|
input_ok = 1; |
| 682 |
} |
} |
| 683 |
|
continue; |
| 684 |
} |
} |
| 685 |
else if (ch == KEY_DEL) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
| 686 |
{ |
{ |
| 687 |
last_updated_line = line_current; |
if (ch == BACKSPACE) |
| 688 |
|
{ |
| 689 |
|
col_pos--; |
| 690 |
|
if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0) |
| 691 |
|
{ |
| 692 |
|
row_pos--; |
| 693 |
|
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
| 694 |
|
} |
| 695 |
|
} |
| 696 |
|
|
| 697 |
if (editor_data_delete(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1, |
if ((str_len = editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1, |
| 698 |
&last_updated_line) < 0) |
&last_updated_line)) < 0) |
| 699 |
{ |
{ |
| 700 |
log_error("editor_data_op(DELETE) error\n"); |
log_error("editor_data_delete() error\n"); |
| 701 |
} |
} |
| 702 |
else |
else |
| 703 |
{ |
{ |
| 704 |
screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current)); |
if (ch == BACKSPACE) |
| 705 |
|
{ |
| 706 |
|
for (i = 1; i < str_len; i++) |
| 707 |
|
{ |
| 708 |
|
col_pos--; |
| 709 |
|
if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0) |
| 710 |
|
{ |
| 711 |
|
row_pos--; |
| 712 |
|
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
| 713 |
|
} |
| 714 |
|
} |
| 715 |
|
} |
| 716 |
|
|
| 717 |
|
screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); |
| 718 |
|
line_current -= (screen_current_row - row_pos); |
| 719 |
|
screen_current_row = (int)row_pos; |
| 720 |
|
|
| 721 |
|
if (screen_current_row < screen_begin_row) // row_pos <= 0 |
| 722 |
|
{ |
| 723 |
|
screen_current_row = screen_begin_row; |
| 724 |
|
row_pos = screen_begin_row; |
| 725 |
|
screen_end_row = SCREEN_ROWS - 1; |
| 726 |
|
} |
| 727 |
} |
} |
| 728 |
|
|
| 729 |
|
// Check whether there is additional input |
| 730 |
|
ch = igetch(0); |
| 731 |
|
if (ch == KEY_TIMEOUT) |
| 732 |
|
{ |
| 733 |
|
input_ok = 1; |
| 734 |
|
} |
| 735 |
continue; |
continue; |
| 736 |
} |
} |
| 737 |
|
|
| 738 |
hz_len = 0; |
input_ok = 1; |
|
|
|
| 739 |
switch (ch) |
switch (ch) |
| 740 |
{ |
{ |
| 741 |
case KEY_NULL: |
case KEY_NULL: |
| 744 |
case Ctrl('C'): |
case Ctrl('C'): |
| 745 |
loop = 0; |
loop = 0; |
| 746 |
break; |
break; |
| 747 |
|
case Ctrl('S'): // Start of line |
| 748 |
case KEY_CTRL_LEFT: |
case KEY_CTRL_LEFT: |
| 749 |
col_pos = 1; |
col_pos = 1; |
| 750 |
break; |
break; |
| 751 |
|
case Ctrl('E'): // End of line |
| 752 |
case KEY_CTRL_RIGHT: |
case KEY_CTRL_RIGHT: |
| 753 |
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]); |
| 754 |
break; |
break; |
| 755 |
|
case Ctrl('T'): // Top of screen |
| 756 |
case KEY_CTRL_UP: |
case KEY_CTRL_UP: |
| 757 |
row_pos = screen_begin_line; |
row_pos = screen_begin_row; |
| 758 |
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])); |
| 759 |
break; |
break; |
| 760 |
|
case Ctrl('B'): // Bottom of screen |
| 761 |
case KEY_CTRL_DOWN: |
case KEY_CTRL_DOWN: |
| 762 |
row_pos = SCREEN_ROWS - 1; |
row_pos = SCREEN_ROWS - 1; |
| 763 |
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])); |
| 764 |
break; |
break; |
| 765 |
case KEY_INS: |
case KEY_INS: |
| 766 |
insert = !insert; |
key_insert = !key_insert; |
| 767 |
break; |
break; |
| 768 |
case KEY_HOME: |
case KEY_HOME: |
| 769 |
row_pos = 1; |
row_pos = 1; |
| 770 |
col_pos = 1; |
col_pos = 1; |
| 771 |
if (line_current - screen_current_line < 0) // Reach begin |
if (line_current - screen_current_row < 0) // Reach begin |
| 772 |
{ |
{ |
| 773 |
break; |
break; |
| 774 |
} |
} |
| 775 |
line_current = 0; |
line_current = 0; |
| 776 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 777 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 778 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 779 |
break; |
break; |
| 780 |
case KEY_END: |
case KEY_END: |
| 781 |
if (p_editor_data->display_line_total < screen_line_total) |
if (p_editor_data->display_line_total < screen_row_total) |
| 782 |
{ |
{ |
| 783 |
row_pos = p_editor_data->display_line_total; |
row_pos = p_editor_data->display_line_total; |
| 784 |
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]); |
| 785 |
break; |
break; |
| 786 |
} |
} |
| 787 |
line_current = p_editor_data->display_line_total - screen_line_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 788 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 789 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 790 |
row_pos = screen_line_total; |
row_pos = screen_row_total; |
| 791 |
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]); |
| 792 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 793 |
break; |
break; |
| 794 |
case KEY_LEFT: |
case KEY_LEFT: |
| 795 |
if (col_pos > 1) |
if (col_pos > 1) |
| 799 |
} |
} |
| 800 |
col_pos = SCREEN_COLS; // continue to KEY_UP |
col_pos = SCREEN_COLS; // continue to KEY_UP |
| 801 |
case KEY_UP: |
case KEY_UP: |
| 802 |
if (row_pos > screen_begin_line) |
if (row_pos > screen_begin_row) |
| 803 |
{ |
{ |
| 804 |
row_pos--; |
row_pos--; |
| 805 |
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])); |
| 806 |
break; |
break; |
| 807 |
} |
} |
| 808 |
if (line_current - screen_current_line < 0) // Reach begin |
if (line_current - screen_current_row < 0) // Reach begin |
| 809 |
{ |
{ |
| 810 |
col_pos = 1; |
col_pos = 1; |
| 811 |
break; |
break; |
| 812 |
} |
} |
| 813 |
line_current -= screen_current_line; |
line_current -= screen_current_row; |
| 814 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 815 |
// screen_end_line = begin_line; |
// screen_end_line = begin_line; |
| 816 |
// prints("\033[T"); // Scroll down 1 line |
// prints("\033[T"); // Scroll down 1 line |
| 817 |
screen_end_line = SCREEN_ROWS - 1; // Legacy Fterm only works with this line |
screen_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line |
| 818 |
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 CR: |
|
| 819 |
break; |
break; |
| 820 |
case KEY_SPACE: |
case KEY_SPACE: |
| 821 |
break; |
break; |
| 822 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 823 |
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]) |
| 824 |
{ |
{ |
| 825 |
col_pos++; |
col_pos++; |
| 826 |
break; |
break; |
| 827 |
} |
} |
| 828 |
col_pos = 1; // continue to KEY_DOWN |
col_pos = 1; // continue to KEY_DOWN |
| 829 |
case KEY_DOWN: |
case KEY_DOWN: |
| 830 |
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)) |
| 831 |
{ |
{ |
| 832 |
row_pos++; |
row_pos++; |
| 833 |
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])); |
| 834 |
break; |
break; |
| 835 |
} |
} |
| 836 |
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 |
| 837 |
{ |
{ |
| 838 |
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]); |
| 839 |
break; |
break; |
| 840 |
} |
} |
| 841 |
screen_current_line--; |
line_current += (screen_row_total - (screen_current_row - screen_begin_row)); |
| 842 |
screen_end_line = SCREEN_ROWS - 1; |
screen_current_row = screen_row_total; |
| 843 |
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; |
| 844 |
|
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
| 845 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 846 |
clrtoeol(); |
clrtoeol(); |
| 847 |
prints("\033[S"); // Scroll up 1 line |
prints("\033[S"); // Scroll up 1 line |
| 848 |
break; |
break; |
| 849 |
case KEY_PGUP: |
case KEY_PGUP: |
| 850 |
if (line_current - screen_current_line < 0) // Reach begin |
if (line_current - screen_current_row < 0) // Reach begin |
| 851 |
{ |
{ |
| 852 |
break; |
break; |
| 853 |
} |
} |
| 854 |
line_current -= ((screen_line_total - 1) + (screen_current_line - screen_begin_line)); |
line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row)); |
| 855 |
if (line_current < 0) |
if (line_current < 0) |
| 856 |
{ |
{ |
| 857 |
line_current = 0; |
line_current = 0; |
| 858 |
} |
} |
| 859 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 860 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 861 |
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])); |
| 862 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 863 |
break; |
break; |
| 864 |
case KEY_PGDN: |
case KEY_PGDN: |
| 865 |
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 |
| 866 |
{ |
{ |
| 867 |
break; |
break; |
| 868 |
} |
} |
| 869 |
line_current += (screen_line_total - 1) - (screen_current_line - screen_begin_line); |
line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row); |
| 870 |
if (line_current + screen_line_total > p_editor_data->display_line_total) // No enough lines to display |
if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display |
| 871 |
{ |
{ |
| 872 |
line_current = p_editor_data->display_line_total - screen_line_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 873 |
} |
} |
| 874 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 875 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 876 |
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])); |
| 877 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
|
break; |
|
|
case KEY_ESC: |
|
| 878 |
break; |
break; |
| 879 |
case KEY_F1: |
case KEY_F1: |
| 880 |
if (!show_help) // Not reentrant |
if (!show_help) // Not reentrant |
| 887 |
show_help = 1; |
show_help = 1; |
| 888 |
case KEY_F5: |
case KEY_F5: |
| 889 |
// Refresh after display help information |
// Refresh after display help information |
| 890 |
line_current -= (screen_current_line - screen_begin_line); |
line_current -= (screen_current_row - screen_begin_row); |
| 891 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 892 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 893 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 894 |
break; |
break; |
| 895 |
case 0: // Refresh bottom line |
case 0: // Refresh bottom line |
| 896 |
break; |
break; |
| 900 |
} |
} |
| 901 |
|
|
| 902 |
BBS_last_access_tm = time(0); |
BBS_last_access_tm = time(0); |
| 903 |
|
if (!input_ok) |
| 904 |
|
{ |
| 905 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 906 |
|
} |
| 907 |
} |
} |
| 908 |
|
|
| 909 |
continue; |
continue; |
| 921 |
len = 0; |
len = 0; |
| 922 |
} |
} |
| 923 |
|
|
| 924 |
memcpy(buffer, (const char *)p_editor_data->p_display_lines[line_current], (size_t)len); |
// memcpy(buffer, p_editor_data->p_display_lines[line_current], (size_t)len); |
| 925 |
buffer[len] = '\0'; |
// Replace '\033' with '*' |
| 926 |
|
p_str = p_editor_data->p_display_lines[line_current]; |
| 927 |
|
for (i = 0, j = 0; i < len; i++) |
| 928 |
|
{ |
| 929 |
|
if (p_str[i] == '\033') |
| 930 |
|
{ |
| 931 |
|
memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1); |
| 932 |
|
j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1); |
| 933 |
|
} |
| 934 |
|
else |
| 935 |
|
{ |
| 936 |
|
buffer[j] = p_str[i]; |
| 937 |
|
j++; |
| 938 |
|
} |
| 939 |
|
} |
| 940 |
|
buffer[j] = '\0'; |
| 941 |
|
|
| 942 |
moveto(screen_current_line, 0); |
moveto(screen_current_row, 0); |
| 943 |
clrtoeol(); |
clrtoeol(); |
| 944 |
prints("%s", buffer); |
prints("%s", buffer); |
| 945 |
line_current++; |
line_current++; |
| 946 |
screen_current_line++; |
screen_current_row++; |
| 947 |
} |
} |
| 948 |
|
|
| 949 |
cleanup: |
cleanup: |