/[LeafOK_CVS]/lbbs/src/editor.c
ViewVC logotype

Diff of /lbbs/src/editor.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.6 by sysadm, Wed Jun 11 05:40:09 2025 UTC Revision 1.8 by sysadm, Wed Jun 11 08:00:51 2025 UTC
# Line 329  int editor_data_insert(EDITOR_DATA *p_ed Line 329  int editor_data_insert(EDITOR_DATA *p_ed
329                  }                  }
330          }          }
331    
332          *p_last_updated_line = MAX(display_line + split_line_total - 1, *p_last_updated_line);          *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
333    
334          if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] ||          if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] ||
335                  (*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] &&                  (*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] &&
# Line 350  int editor_data_insert(EDITOR_DATA *p_ed Line 350  int editor_data_insert(EDITOR_DATA *p_ed
350  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,
351                                             long *p_last_updated_line)                                             long *p_last_updated_line)
352  {  {
353            char *p_data_line = NULL;
354            long len_data_line;
355            long offset_data_line;
356            long last_display_line; // of data line
357            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
358            long split_line_total;
359            long i, j;
360            int str_len = 0;
361    
362            if (p_editor_data == NULL || p_last_updated_line == NULL)
363            {
364                    log_error("editor_data_op() error: NULL pointer\n");
365                    return -1;
366            }
367    
368            // Get accurate offset of first character of CJK at offset position
369            for (i = 0; i < offset; i++)
370            {
371                    if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK
372                    {
373                            i++;
374                    }
375            }
376            if (i > offset) // offset was skipped
377            {
378                    offset--;
379            }
380    
381            // Get length of current data line
382            len_data_line = 0;
383            p_data_line = p_editor_data->p_display_lines[display_line];
384            for (i = display_line - 1; i >= 0; i--)
385            {
386                    if (p_editor_data->display_line_lengths[i] > 0 &&
387                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
388                    {
389                            break;
390                    }
391    
392                    len_data_line += p_editor_data->display_line_lengths[i];
393                    p_data_line = p_editor_data->p_display_lines[i];
394            }
395            offset_data_line = len_data_line + offset;
396            last_display_line = p_editor_data->display_line_total - 1;
397            for (i = display_line; i < p_editor_data->display_line_total; i++)
398            {
399                    len_data_line += p_editor_data->display_line_lengths[i];
400    
401                    if (p_editor_data->display_line_lengths[i] > 0 &&
402                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
403                    {
404                            last_display_line = i;
405                            break;
406                    }
407            }
408    
409            // Check str to be deleted
410            if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
411            {
412                    str_len = 1;
413            }
414            else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK
415            {
416                    str_len = 2;
417            }
418            else
419            {
420                    log_error("Some strange character at display_line %ld, offset %ld\n", display_line, offset);
421                    return -2;
422            }
423    
424            // Current display line is (almost) empty
425            if (offset_data_line + str_len > len_data_line ||
426                    (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
427            {
428                    log_error("Nothing to be delete\n");
429                    return 0;
430            }
431    
432            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));
433            p_data_line[len_data_line - str_len] = '\0';
434            len_data_line -= str_len;
435    
436            // Set p_data_line to head of current display line
437            p_data_line = p_editor_data->p_display_lines[display_line];
438            split_line_total = last_display_line - display_line + 2;
439    
440            // Split current data line since beginning of current display line
441            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
442    
443            for (i = 0; i < split_line_total; i++)
444            {
445                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
446                    p_editor_data->p_display_lines[display_line + i] =
447                            (i == 0
448                                     ? p_data_line
449                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
450    
451                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
452                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
453                    {
454                            break;
455                    }
456            }
457    
458            *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
459    
460            if (display_line + i < last_display_line)
461            {
462                    // Remove redundant display line after last_display_line
463                    for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
464                    {
465                            p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j];
466                            p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j];
467                    }
468    
469                    (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));
470                    last_display_line = display_line + i;
471    
472                    *p_last_updated_line = p_editor_data->display_line_total - 1;
473            }
474    
475          return 0;          return 0;
476  }  }
477    
# Line 388  int editor_display(EDITOR_DATA *p_editor Line 510  int editor_display(EDITOR_DATA *p_editor
510          long display_line_out, offset_out;          long display_line_out, offset_out;
511          int scroll_rows;          int scroll_rows;
512          long last_updated_line = 0;          long last_updated_line = 0;
513          int insert = 1;          int key_insert = 1;
514          int i;          int i;
515    
516          screen_current_row = screen_begin_row;          screen_current_row = screen_begin_row;
# Line 409  int editor_display(EDITOR_DATA *p_editor Line 531  int editor_display(EDITOR_DATA *p_editor
531    
532                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
533                                           "\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] "
534                                           "第\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] "
535                                           "%s",                                           "%s",
536                                           row_pos, col_pos,                                           row_pos, col_pos,
537                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
538                                             key_insert ? "插入" : "改写",
539                                           ctx.msg);                                           ctx.msg);
540    
541                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);
# Line 465  int editor_display(EDITOR_DATA *p_editor Line 588  int editor_display(EDITOR_DATA *p_editor
588                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
589                                          offset_out = offset_in;                                          offset_out = offset_in;
590    
591                                            if (!key_insert) // overwrite
592                                            {
593                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
594                                                                                               &last_updated_line) < 0)
595                                                    {
596                                                            log_error("editor_data_delete() error\n");
597                                                    }
598                                            }
599    
600                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
601                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
602                                          {                                          {
# Line 519  int editor_display(EDITOR_DATA *p_editor Line 651  int editor_display(EDITOR_DATA *p_editor
651                                          else                                          else
652                                          {                                          {
653                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));
654                                                    line_current -= (screen_current_row - row_pos);
655                                                    screen_current_row = (int)row_pos;
656                                          }                                          }
657    
658                                          continue;                                          continue;
# Line 547  int editor_display(EDITOR_DATA *p_editor Line 681  int editor_display(EDITOR_DATA *p_editor
681                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
682                                          break;                                          break;
683                                  case KEY_INS:                                  case KEY_INS:
684                                          insert = !insert;                                          key_insert = !key_insert;
685                                          break;                                          break;
686                                  case KEY_HOME:                                  case KEY_HOME:
687                                          row_pos = 1;                                          row_pos = 1;


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1