| 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> |
| 30 |
EDITOR_DATA *editor_data_load(const char *p_data) |
EDITOR_DATA *editor_data_load(const char *p_data) |
| 31 |
{ |
{ |
| 32 |
EDITOR_DATA *p_editor_data; |
EDITOR_DATA *p_editor_data; |
| 33 |
|
char *p_data_line = NULL; |
| 34 |
long line_offsets[MAX_EDITOR_DATA_LINES]; |
long line_offsets[MAX_EDITOR_DATA_LINES]; |
| 35 |
long current_data_line_length = 0; |
long current_data_line_length = 0; |
| 36 |
long i, j; |
long i, j; |
| 56 |
p_editor_data->display_line_lengths[i] = line_offsets[i + 1] - line_offsets[i]; |
p_editor_data->display_line_lengths[i] = line_offsets[i + 1] - line_offsets[i]; |
| 57 |
|
|
| 58 |
if (i == 0 || |
if (i == 0 || |
| 59 |
(current_data_line_length + p_editor_data->display_line_lengths[i] + 1) >= MAX_EDITOR_DATA_LINE_LENGTH || |
current_data_line_length + p_editor_data->display_line_lengths[i] + 1 > MAX_EDITOR_DATA_LINE_LENGTH || |
| 60 |
(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')) |
| 61 |
{ |
{ |
| 62 |
|
if (p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES) |
| 63 |
|
{ |
| 64 |
|
log_error("Append line error, data_line_total(%ld) reach limit(%d)\n", p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES); |
| 65 |
|
return NULL; |
| 66 |
|
} |
| 67 |
|
|
| 68 |
// Allocate new data line |
// Allocate new data line |
| 69 |
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); |
| 70 |
if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL) |
if (p_data_line == NULL) |
| 71 |
{ |
{ |
| 72 |
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); |
| 73 |
// Cleanup |
// Cleanup |
| 78 |
free(p_editor_data); |
free(p_editor_data); |
| 79 |
return NULL; |
return NULL; |
| 80 |
} |
} |
| 81 |
|
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]; |
|
| 82 |
(p_editor_data->data_line_total)++; |
(p_editor_data->data_line_total)++; |
| 83 |
|
|
| 84 |
|
p_editor_data->p_display_lines[i] = p_data_line; |
| 85 |
|
current_data_line_length = 0; |
| 86 |
} |
} |
| 87 |
else |
else |
| 88 |
{ |
{ |
| 89 |
p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1] + p_editor_data->display_line_lengths[i - 1]; |
p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1] + p_editor_data->display_line_lengths[i - 1]; |
|
current_data_line_length = 0; |
|
| 90 |
} |
} |
| 91 |
|
|
| 92 |
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]); |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i]] = '\0'; |
|
|
|
|
| 93 |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
| 94 |
|
p_data_line[current_data_line_length] = '\0'; |
| 95 |
} |
} |
| 96 |
|
|
| 97 |
|
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); |
| 98 |
|
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); |
| 99 |
|
|
| 100 |
return p_editor_data; |
return p_editor_data; |
| 101 |
} |
} |
| 102 |
|
|
| 141 |
for (i = p_editor_data->data_line_total - 1; i >= 0; i--) |
for (i = p_editor_data->data_line_total - 1; i >= 0; i--) |
| 142 |
{ |
{ |
| 143 |
free(p_editor_data->p_data_lines[i]); |
free(p_editor_data->p_data_lines[i]); |
| 144 |
|
p_editor_data->p_data_lines[i] = NULL; |
| 145 |
} |
} |
| 146 |
|
|
| 147 |
free(p_editor_data); |
free(p_editor_data); |
| 148 |
} |
} |
| 149 |
|
|
| 150 |
static int editor_display_key_handler(int *p_key, DISPLAY_CTX *p_ctx) |
int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
| 151 |
|
const char *str, int str_len, long *p_last_updated_line) |
| 152 |
|
{ |
| 153 |
|
long display_line = *p_display_line; |
| 154 |
|
long offset = *p_offset; |
| 155 |
|
char *p_data_line = NULL; |
| 156 |
|
long len_data_line; |
| 157 |
|
long offset_data_line; |
| 158 |
|
long last_display_line; // of data line |
| 159 |
|
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 160 |
|
long split_line_total; |
| 161 |
|
long i, j; |
| 162 |
|
|
| 163 |
|
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 164 |
|
{ |
| 165 |
|
log_error("editor_data_op() error: NULL pointer\n"); |
| 166 |
|
return -1; |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
// Get accurate offset of first character of CJK at offset position |
| 170 |
|
for (i = 0; i < offset; i++) |
| 171 |
|
{ |
| 172 |
|
if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK |
| 173 |
|
{ |
| 174 |
|
i++; |
| 175 |
|
} |
| 176 |
|
} |
| 177 |
|
if (i > offset) // offset was skipped |
| 178 |
|
{ |
| 179 |
|
offset--; |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
// Get length of current data line |
| 183 |
|
len_data_line = 0; |
| 184 |
|
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 185 |
|
for (i = display_line - 1; i >= 0; i--) |
| 186 |
|
{ |
| 187 |
|
if (p_editor_data->display_line_lengths[i] > 0 && |
| 188 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line |
| 189 |
|
{ |
| 190 |
|
break; |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
len_data_line += p_editor_data->display_line_lengths[i]; |
| 194 |
|
p_data_line = p_editor_data->p_display_lines[i]; |
| 195 |
|
} |
| 196 |
|
offset_data_line = len_data_line + offset; |
| 197 |
|
last_display_line = p_editor_data->display_line_total - 1; |
| 198 |
|
for (i = display_line; i < p_editor_data->display_line_total; i++) |
| 199 |
|
{ |
| 200 |
|
len_data_line += p_editor_data->display_line_lengths[i]; |
| 201 |
|
|
| 202 |
|
if (p_editor_data->display_line_lengths[i] > 0 && |
| 203 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line |
| 204 |
|
{ |
| 205 |
|
last_display_line = i; |
| 206 |
|
break; |
| 207 |
|
} |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
// Split current data line if over-length |
| 211 |
|
if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH) |
| 212 |
|
{ |
| 213 |
|
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES || p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES) |
| 214 |
|
{ |
| 215 |
|
log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n", |
| 216 |
|
p_editor_data->display_line_total, p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES); |
| 217 |
|
return -2; |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
// Allocate new data line |
| 221 |
|
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
| 222 |
|
if (p_data_line == NULL) |
| 223 |
|
{ |
| 224 |
|
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); |
| 225 |
|
return -2; |
| 226 |
|
} |
| 227 |
|
p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; |
| 228 |
|
(p_editor_data->data_line_total)++; |
| 229 |
|
|
| 230 |
|
if (offset_data_line + str_len + 1 < MAX_EDITOR_DATA_LINE_LENGTH) |
| 231 |
|
{ |
| 232 |
|
// Copy rest part of current data line to new data line |
| 233 |
|
memcpy(p_data_line, |
| 234 |
|
p_editor_data->p_display_lines[display_line] + offset, |
| 235 |
|
(size_t)(len_data_line - offset_data_line)); |
| 236 |
|
|
| 237 |
|
p_data_line[len_data_line - offset_data_line] = '\0'; |
| 238 |
|
|
| 239 |
|
// Append str to current display line |
| 240 |
|
memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len); |
| 241 |
|
|
| 242 |
|
// Add line ending to current display line (data line) |
| 243 |
|
p_editor_data->p_display_lines[display_line][offset + str_len] = '\n'; |
| 244 |
|
p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0'; |
| 245 |
|
p_editor_data->display_line_lengths[display_line] = offset + str_len + 1; |
| 246 |
|
|
| 247 |
|
*p_display_line = display_line; |
| 248 |
|
*p_offset = offset + str_len; |
| 249 |
|
} |
| 250 |
|
else |
| 251 |
|
{ |
| 252 |
|
// Copy str to new data line |
| 253 |
|
memcpy(p_data_line, str, (size_t)str_len); |
| 254 |
|
|
| 255 |
|
// Copy rest part of current data line to new data line |
| 256 |
|
memcpy(p_data_line + str_len, |
| 257 |
|
p_editor_data->p_display_lines[display_line] + offset, |
| 258 |
|
(size_t)(len_data_line - offset_data_line)); |
| 259 |
|
|
| 260 |
|
p_data_line[str_len + len_data_line - offset_data_line] = '\0'; |
| 261 |
|
|
| 262 |
|
// Add line ending to current display line (data line) |
| 263 |
|
p_editor_data->p_display_lines[display_line][offset] = '\n'; |
| 264 |
|
p_editor_data->p_display_lines[display_line][offset + 1] = '\0'; |
| 265 |
|
p_editor_data->display_line_lengths[display_line] = offset + 1; |
| 266 |
|
|
| 267 |
|
*p_display_line = display_line + 1; |
| 268 |
|
*p_offset = str_len; |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
split_line_total = last_display_line - display_line + 3; |
| 272 |
|
|
| 273 |
|
// Set start display_line for spliting new data line |
| 274 |
|
display_line++; |
| 275 |
|
|
| 276 |
|
*p_last_updated_line = p_editor_data->display_line_total; |
| 277 |
|
} |
| 278 |
|
else // insert str into current data line at offset_data_line |
| 279 |
|
{ |
| 280 |
|
log_error("Insert %d chars into display_line = %d, offset = %d\n", str_len, display_line, offset); |
| 281 |
|
|
| 282 |
|
memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line)); |
| 283 |
|
memcpy(p_data_line + offset_data_line, str, (size_t)str_len); |
| 284 |
|
p_data_line[len_data_line + str_len] = '\0'; |
| 285 |
|
|
| 286 |
|
// Set p_data_line to head of current display line |
| 287 |
|
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 288 |
|
split_line_total = last_display_line - display_line + 3; |
| 289 |
|
|
| 290 |
|
*p_display_line = display_line; |
| 291 |
|
*p_offset = offset + str_len; |
| 292 |
|
} |
| 293 |
|
|
| 294 |
|
// Split current data line since beginning of current display line |
| 295 |
|
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); |
| 296 |
|
log_error("Debug: split data line, display_line = %ld, j = %ld\n", display_line, split_line_total); |
| 297 |
|
|
| 298 |
|
for (i = 0; i < split_line_total; i++) |
| 299 |
|
{ |
| 300 |
|
if (display_line + i > last_display_line) |
| 301 |
|
{ |
| 302 |
|
// Insert blank display line after last_display_line |
| 303 |
|
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
| 304 |
|
{ |
| 305 |
|
log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
| 306 |
|
return -3; |
| 307 |
|
} |
| 308 |
|
for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) |
| 309 |
|
{ |
| 310 |
|
p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
| 311 |
|
p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; |
| 312 |
|
} |
| 313 |
|
last_display_line++; |
| 314 |
|
(p_editor_data->display_line_total)++; |
| 315 |
|
} |
| 316 |
|
|
| 317 |
|
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
| 318 |
|
p_editor_data->p_display_lines[display_line + i] = |
| 319 |
|
(i == 0 |
| 320 |
|
? p_data_line |
| 321 |
|
: (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1])); |
| 322 |
|
|
| 323 |
|
if (p_editor_data->display_line_lengths[display_line + i] > 0 && |
| 324 |
|
p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n') |
| 325 |
|
{ |
| 326 |
|
log_error("Debug: reach end of data line, i = %ld, j = %ld\n", i, split_line_total); |
| 327 |
|
break; |
| 328 |
|
} |
| 329 |
|
} |
| 330 |
|
|
| 331 |
|
*p_last_updated_line = MAX(display_line + split_line_total - 1, *p_last_updated_line); |
| 332 |
|
|
| 333 |
|
if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] || |
| 334 |
|
(*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] && |
| 335 |
|
p_editor_data->p_display_lines[*p_display_line][*p_offset - 1] == '\n')) |
| 336 |
|
{ |
| 337 |
|
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
| 338 |
|
(*p_display_line)++; |
| 339 |
|
|
| 340 |
|
if (*p_display_line >= p_editor_data->display_line_total) |
| 341 |
|
{ |
| 342 |
|
log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total); |
| 343 |
|
} |
| 344 |
|
} |
| 345 |
|
|
| 346 |
|
return 0; |
| 347 |
|
} |
| 348 |
|
|
| 349 |
|
int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset, |
| 350 |
|
long *p_last_updated_line) |
| 351 |
|
{ |
| 352 |
|
return 0; |
| 353 |
|
} |
| 354 |
|
|
| 355 |
|
static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx) |
| 356 |
{ |
{ |
| 357 |
switch (*p_key) |
switch (*p_key) |
| 358 |
{ |
{ |
| 359 |
case 0: // Set msg |
case 0: // Set msg |
| 360 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 361 |
"| ·µ»Ø[\033[32m¡û\033[33m,\033[32mESC\033[33m] | " |
"| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |"); |
|
"ÒÆ¶¯[\033[32m¡ü\033[33m/\033[32m¡ý\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | " |
|
|
"°ïÖú[\033[32mh\033[33m] |"); |
|
| 362 |
break; |
break; |
| 363 |
} |
} |
| 364 |
|
|
| 369 |
{ |
{ |
| 370 |
static int show_help = 1; |
static int show_help = 1; |
| 371 |
char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; |
char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; |
| 372 |
DISPLAY_CTX ctx; |
EDITOR_CTX ctx; |
| 373 |
int ch = 0; |
int ch = 0; |
| 374 |
int input_ok, screen_current_line; |
char input_str[4]; |
| 375 |
const int screen_begin_line = 1; |
int str_len = 0; |
| 376 |
int screen_end_line = SCREEN_ROWS - 1; |
int input_ok; |
| 377 |
const int screen_line_total = screen_end_line - screen_begin_line + 1; |
int screen_current_row; |
| 378 |
|
const int screen_begin_row = 1; |
| 379 |
|
int screen_end_row = SCREEN_ROWS - 1; |
| 380 |
|
const int screen_row_total = screen_end_row - screen_begin_row + 1; |
| 381 |
long int line_current = 0; |
long int line_current = 0; |
| 382 |
long int len; |
long int len; |
|
long int percentile; |
|
| 383 |
int loop; |
int loop; |
| 384 |
int eol, display_len; |
int eol, display_len; |
| 385 |
long row_pos = 1, col_pos = 1; |
long row_pos = 1, col_pos = 1; |
| 386 |
|
long display_line_in, offset_in; |
| 387 |
|
long display_line_out, offset_out; |
| 388 |
|
int scroll_rows; |
| 389 |
|
long last_updated_line = 0; |
| 390 |
|
int insert = 1; |
| 391 |
|
int i; |
| 392 |
|
|
| 393 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 394 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 395 |
|
|
| 396 |
// update msg_ext with extended key handler |
// update msg_ext with extended key handler |
| 397 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 402 |
loop = 1; |
loop = 1; |
| 403 |
while (!SYS_server_exit && loop) |
while (!SYS_server_exit && loop) |
| 404 |
{ |
{ |
| 405 |
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) |
| 406 |
{ |
{ |
| 407 |
ctx.reach_begin = (line_current < screen_current_line ? 1 : 0); |
ctx.line_cursor = line_current - screen_current_row + row_pos + 1; |
|
|
|
|
if (line_current - (screen_current_line - screen_begin_line) + screen_line_total < p_editor_data->display_line_total) |
|
|
{ |
|
|
percentile = (line_current - (screen_current_line - screen_begin_line) + screen_line_total) * 100 / p_editor_data->display_line_total; |
|
|
ctx.reach_end = 0; |
|
|
} |
|
|
else |
|
|
{ |
|
|
percentile = 100; |
|
|
ctx.reach_end = 1; |
|
|
} |
|
|
|
|
|
ctx.line_top = line_current - (screen_current_line - screen_begin_line) + 1; |
|
|
ctx.line_bottom = MIN(line_current - (screen_current_line - screen_begin_line) + screen_line_total, p_editor_data->display_line_total); |
|
| 408 |
|
|
| 409 |
snprintf(buffer, sizeof(buffer), |
snprintf(buffer, sizeof(buffer), |
| 410 |
"\033[1;44;33mµÚ\033[32m%ld\033[33m-\033[32m%ld\033[33mÐÐ (\033[32m%ld%%\033[33m) %s", |
"\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] " |
| 411 |
ctx.line_top, |
"µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ " |
| 412 |
ctx.line_bottom, |
"%s", |
| 413 |
percentile, |
row_pos, col_pos, |
| 414 |
|
ctx.line_cursor, p_editor_data->display_line_total, |
| 415 |
ctx.msg); |
ctx.msg); |
| 416 |
|
|
| 417 |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
| 440 |
goto cleanup; |
goto cleanup; |
| 441 |
} |
} |
| 442 |
|
|
| 443 |
|
if (ch > 127 && ch <= 255) // GBK |
| 444 |
|
{ |
| 445 |
|
input_str[str_len] = (char)(ch - 256); |
| 446 |
|
str_len++; |
| 447 |
|
} |
| 448 |
|
else |
| 449 |
|
{ |
| 450 |
|
str_len = 0; |
| 451 |
|
} |
| 452 |
|
|
| 453 |
|
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2)) // printable character or GBK |
| 454 |
|
{ |
| 455 |
|
if (str_len == 0) |
| 456 |
|
{ |
| 457 |
|
input_str[0] = (char)ch; |
| 458 |
|
str_len = 1; |
| 459 |
|
} |
| 460 |
|
|
| 461 |
|
last_updated_line = line_current; |
| 462 |
|
display_line_in = line_current - screen_current_row + row_pos; |
| 463 |
|
offset_in = col_pos - 1; |
| 464 |
|
display_line_out = display_line_in; |
| 465 |
|
offset_out = offset_in; |
| 466 |
|
|
| 467 |
|
if (editor_data_insert(p_editor_data, &display_line_out, &offset_out, |
| 468 |
|
input_str, str_len, &last_updated_line) < 0) |
| 469 |
|
{ |
| 470 |
|
log_error("editor_data_insert(%s) error\n", input_str); |
| 471 |
|
str_len = 0; |
| 472 |
|
} |
| 473 |
|
else |
| 474 |
|
{ |
| 475 |
|
str_len = 0; |
| 476 |
|
|
| 477 |
|
screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); |
| 478 |
|
line_current -= (screen_current_row - row_pos); |
| 479 |
|
screen_current_row = (int)row_pos; |
| 480 |
|
|
| 481 |
|
scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row)); |
| 482 |
|
|
| 483 |
|
if (scroll_rows > 0) |
| 484 |
|
{ |
| 485 |
|
moveto(SCREEN_ROWS, 0); |
| 486 |
|
clrtoeol(); |
| 487 |
|
for (i = 0; i < scroll_rows; i++) |
| 488 |
|
{ |
| 489 |
|
prints("\033[S"); // Scroll up 1 line |
| 490 |
|
} |
| 491 |
|
|
| 492 |
|
screen_current_row -= scroll_rows; |
| 493 |
|
if (screen_current_row < screen_begin_row) |
| 494 |
|
{ |
| 495 |
|
line_current += (screen_begin_row - screen_current_row); |
| 496 |
|
screen_current_row = screen_begin_row; |
| 497 |
|
} |
| 498 |
|
row_pos = screen_end_row; |
| 499 |
|
} |
| 500 |
|
else // if (scroll_lines == 0) |
| 501 |
|
{ |
| 502 |
|
row_pos += (display_line_out - display_line_in); |
| 503 |
|
} |
| 504 |
|
col_pos = offset_out + 1; |
| 505 |
|
|
| 506 |
|
continue; |
| 507 |
|
} |
| 508 |
|
} |
| 509 |
|
else if (ch == KEY_DEL) // Del |
| 510 |
|
{ |
| 511 |
|
last_updated_line = line_current; |
| 512 |
|
|
| 513 |
|
if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1, |
| 514 |
|
&last_updated_line) < 0) |
| 515 |
|
{ |
| 516 |
|
log_error("editor_data_delete() error\n"); |
| 517 |
|
} |
| 518 |
|
else |
| 519 |
|
{ |
| 520 |
|
screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); |
| 521 |
|
} |
| 522 |
|
|
| 523 |
|
continue; |
| 524 |
|
} |
| 525 |
|
|
| 526 |
switch (ch) |
switch (ch) |
| 527 |
{ |
{ |
| 528 |
case KEY_NULL: |
case KEY_NULL: |
| 531 |
case Ctrl('C'): |
case Ctrl('C'): |
| 532 |
loop = 0; |
loop = 0; |
| 533 |
break; |
break; |
| 534 |
case Ctrl('H'): |
case KEY_CTRL_LEFT: |
| 535 |
col_pos = 1; |
col_pos = 1; |
| 536 |
break; |
break; |
| 537 |
case Ctrl('E'): |
case KEY_CTRL_RIGHT: |
| 538 |
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]); |
| 539 |
|
break; |
| 540 |
|
case KEY_CTRL_UP: |
| 541 |
|
row_pos = screen_begin_row; |
| 542 |
|
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
| 543 |
|
break; |
| 544 |
|
case KEY_CTRL_DOWN: |
| 545 |
|
row_pos = SCREEN_ROWS - 1; |
| 546 |
|
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
| 547 |
|
break; |
| 548 |
|
case KEY_INS: |
| 549 |
|
insert = !insert; |
| 550 |
break; |
break; |
| 551 |
case KEY_HOME: |
case KEY_HOME: |
| 552 |
row_pos = 1; |
row_pos = 1; |
| 553 |
col_pos = 1; |
col_pos = 1; |
| 554 |
if (line_current - screen_current_line < 0) // Reach begin |
if (line_current - screen_current_row < 0) // Reach begin |
| 555 |
{ |
{ |
| 556 |
break; |
break; |
| 557 |
} |
} |
| 558 |
line_current = 0; |
line_current = 0; |
| 559 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 560 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 561 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 562 |
break; |
break; |
| 563 |
case KEY_END: |
case KEY_END: |
| 564 |
if (p_editor_data->display_line_total < screen_line_total) |
if (p_editor_data->display_line_total < screen_row_total) |
| 565 |
{ |
{ |
| 566 |
row_pos = p_editor_data->display_line_total; |
row_pos = p_editor_data->display_line_total; |
| 567 |
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]); |
| 568 |
break; |
break; |
| 569 |
} |
} |
| 570 |
line_current = p_editor_data->display_line_total - screen_line_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 571 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 572 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 573 |
row_pos = screen_line_total; |
row_pos = screen_row_total; |
| 574 |
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]); |
| 575 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 576 |
break; |
break; |
| 577 |
case KEY_LEFT: |
case KEY_LEFT: |
| 578 |
if (col_pos > 1) |
if (col_pos > 1) |
| 582 |
} |
} |
| 583 |
col_pos = SCREEN_COLS; // continue to KEY_UP |
col_pos = SCREEN_COLS; // continue to KEY_UP |
| 584 |
case KEY_UP: |
case KEY_UP: |
| 585 |
if (row_pos > screen_begin_line) |
if (row_pos > screen_begin_row) |
| 586 |
{ |
{ |
| 587 |
row_pos--; |
row_pos--; |
| 588 |
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])); |
| 589 |
break; |
break; |
| 590 |
} |
} |
| 591 |
if (line_current - screen_current_line < 0) // Reach begin |
if (line_current - screen_current_row < 0) // Reach begin |
| 592 |
{ |
{ |
| 593 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]); |
col_pos = 1; |
| 594 |
break; |
break; |
| 595 |
} |
} |
| 596 |
line_current -= screen_current_line; |
line_current -= screen_current_row; |
| 597 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 598 |
// screen_end_line = begin_line; |
// screen_end_line = begin_line; |
| 599 |
// prints("\033[T"); // Scroll down 1 line |
// prints("\033[T"); // Scroll down 1 line |
| 600 |
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 |
| 601 |
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])); |
| 602 |
break; |
break; |
| 603 |
case CR: |
case CR: |
| 604 |
break; |
break; |
| 605 |
case KEY_SPACE: |
case KEY_SPACE: |
| 606 |
break; |
break; |
| 607 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 608 |
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]) |
| 609 |
{ |
{ |
| 610 |
col_pos++; |
col_pos++; |
| 611 |
break; |
break; |
| 612 |
} |
} |
| 613 |
col_pos = 1; // continue to KEY_DOWN |
col_pos = 1; // continue to KEY_DOWN |
| 614 |
case KEY_DOWN: |
case KEY_DOWN: |
| 615 |
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)) |
| 616 |
{ |
{ |
| 617 |
row_pos++; |
row_pos++; |
| 618 |
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])); |
| 619 |
break; |
break; |
| 620 |
} |
} |
| 621 |
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 |
| 622 |
{ |
{ |
| 623 |
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]); |
| 624 |
break; |
break; |
| 625 |
} |
} |
| 626 |
line_current += (screen_line_total - (screen_current_line - screen_begin_line)); |
line_current += (screen_row_total - (screen_current_row - screen_begin_row)); |
| 627 |
screen_current_line = screen_line_total; |
screen_current_row = screen_row_total; |
| 628 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 629 |
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])); |
| 630 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 631 |
clrtoeol(); |
clrtoeol(); |
| 632 |
prints("\033[S"); // Scroll up 1 line |
prints("\033[S"); // Scroll up 1 line |
| 633 |
break; |
break; |
| 634 |
case KEY_PGUP: |
case KEY_PGUP: |
| 635 |
if (line_current - screen_current_line < 0) // Reach begin |
if (line_current - screen_current_row < 0) // Reach begin |
| 636 |
{ |
{ |
| 637 |
break; |
break; |
| 638 |
} |
} |
| 639 |
line_current -= ((screen_line_total - 1) + (screen_current_line - screen_begin_line)); |
line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row)); |
| 640 |
if (line_current < 0) |
if (line_current < 0) |
| 641 |
{ |
{ |
| 642 |
line_current = 0; |
line_current = 0; |
| 643 |
} |
} |
| 644 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 645 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 646 |
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])); |
| 647 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 648 |
break; |
break; |
| 649 |
case KEY_PGDN: |
case KEY_PGDN: |
| 650 |
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 |
| 651 |
{ |
{ |
| 652 |
break; |
break; |
| 653 |
} |
} |
| 654 |
line_current += (screen_line_total - 1) - (screen_current_line - screen_begin_line); |
line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row); |
| 655 |
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 |
| 656 |
{ |
{ |
| 657 |
line_current = p_editor_data->display_line_total - screen_line_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 658 |
} |
} |
| 659 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 660 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 661 |
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])); |
| 662 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 663 |
break; |
break; |
| 664 |
case KEY_ESC: |
case KEY_ESC: |
| 665 |
break; |
break; |
| 674 |
show_help = 1; |
show_help = 1; |
| 675 |
case KEY_F5: |
case KEY_F5: |
| 676 |
// Refresh after display help information |
// Refresh after display help information |
| 677 |
line_current -= (screen_current_line - screen_begin_line); |
line_current -= (screen_current_row - screen_begin_row); |
| 678 |
screen_current_line = screen_begin_line; |
screen_current_row = screen_begin_row; |
| 679 |
screen_end_line = SCREEN_ROWS - 1; |
screen_end_row = SCREEN_ROWS - 1; |
| 680 |
clrline(screen_begin_line, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 681 |
break; |
break; |
| 682 |
case 0: // Refresh bottom line |
case 0: // Refresh bottom line |
| 683 |
break; |
break; |
| 707 |
memcpy(buffer, (const char *)p_editor_data->p_display_lines[line_current], (size_t)len); |
memcpy(buffer, (const char *)p_editor_data->p_display_lines[line_current], (size_t)len); |
| 708 |
buffer[len] = '\0'; |
buffer[len] = '\0'; |
| 709 |
|
|
| 710 |
moveto(screen_current_line, 0); |
moveto(screen_current_row, 0); |
| 711 |
clrtoeol(); |
clrtoeol(); |
| 712 |
prints("%s", buffer); |
prints("%s", buffer); |
| 713 |
line_current++; |
line_current++; |
| 714 |
screen_current_line++; |
screen_current_row++; |
| 715 |
} |
} |
| 716 |
|
|
| 717 |
cleanup: |
cleanup: |