/[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.7 by sysadm, Wed Jun 11 07:21:59 2025 UTC Revision 1.10 by sysadm, Wed Jun 11 11:01:07 2025 UTC
# Line 169  int editor_data_insert(EDITOR_DATA *p_ed Line 169  int editor_data_insert(EDITOR_DATA *p_ed
169          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
170          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
171          {          {
172                  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
173                  {                  {
174                          i++;                          i++;
175                  }                  }
# Line 368  int editor_data_delete(EDITOR_DATA *p_ed Line 368  int editor_data_delete(EDITOR_DATA *p_ed
368          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
369          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
370          {          {
371                  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
372                  {                  {
373                          i++;                          i++;
374                  }                  }
# Line 417  int editor_data_delete(EDITOR_DATA *p_ed Line 417  int editor_data_delete(EDITOR_DATA *p_ed
417          }          }
418          else          else
419          {          {
420                  log_error("Some strange character at display_line %ld, offset %ld\n", display_line, offset);                  log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n",
421                  return -2;                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1],
422                                      p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]);
423                    str_len = 1;
424          }          }
425    
426          // Current display line is (almost) empty          // Current display line is (almost) empty
427          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
428                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
429          {          {
430                  log_error("Nothing to be delete\n");                  if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
431                  return 0;                  {
432          }                          log_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total);
433                            return 0;
434                    }
435    
436                    len_data_line = 0; // Next data line
437                    last_display_line = p_editor_data->display_line_total - 1;
438                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
439                    {
440                            len_data_line += p_editor_data->display_line_lengths[i];
441    
442          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));                          if (p_editor_data->display_line_lengths[i] > 0 &&
443          p_data_line[len_data_line - str_len] = '\0';                                  p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
444          len_data_line -= str_len;                          {
445                                    last_display_line = i;
446                                    break;
447                            }
448                    }
449    
450                    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
451                    {
452                            log_common("Debug: No enough buffer to merge with next data line: %ld > %ld\n",
453                                               offset_data_line + len_data_line + 1, MAX_EDITOR_DATA_LINE_LENGTH);
454                            return 0;
455                    }
456    
457                    // Append next data line to current one
458                    memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
459                    p_data_line[offset_data_line + len_data_line] = '\0';
460    
461                    // Recycle next data line
462                    // TODO: free(p_editor_data->p_display_lines[display_line + 1]);
463            }
464            else
465            {
466                    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));
467                    p_data_line[len_data_line - str_len] = '\0';
468                    len_data_line -= str_len;
469            }
470    
471          // Set p_data_line to head of current display line          // Set p_data_line to head of current display line
472          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 472  int editor_data_delete(EDITOR_DATA *p_ed Line 507  int editor_data_delete(EDITOR_DATA *p_ed
507                  *p_last_updated_line = p_editor_data->display_line_total - 1;                  *p_last_updated_line = p_editor_data->display_line_total - 1;
508          }          }
509    
510          return 0;          return str_len;
511  }  }
512    
513  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)
# Line 510  int editor_display(EDITOR_DATA *p_editor Line 545  int editor_display(EDITOR_DATA *p_editor
545          long display_line_out, offset_out;          long display_line_out, offset_out;
546          int scroll_rows;          int scroll_rows;
547          long last_updated_line = 0;          long last_updated_line = 0;
548          int insert = 1;          int key_insert = 1;
549          int i;          int i;
550    
551          screen_current_row = screen_begin_row;          screen_current_row = screen_begin_row;
# Line 531  int editor_display(EDITOR_DATA *p_editor Line 566  int editor_display(EDITOR_DATA *p_editor
566    
567                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
568                                           "\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] "
569                                           "第\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] "
570                                           "%s",                                           "%s",
571                                           row_pos, col_pos,                                           row_pos, col_pos,
572                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
573                                             key_insert ? "插入" : "改写",
574                                           ctx.msg);                                           ctx.msg);
575    
576                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);
# Line 587  int editor_display(EDITOR_DATA *p_editor Line 623  int editor_display(EDITOR_DATA *p_editor
623                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
624                                          offset_out = offset_in;                                          offset_out = offset_in;
625    
626                                            if (!key_insert) // overwrite
627                                            {
628                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
629                                                                                               &last_updated_line) < 0)
630                                                    {
631                                                            log_error("editor_data_delete() error\n");
632                                                    }
633                                            }
634    
635                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
636                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
637                                          {                                          {
# Line 629  int editor_display(EDITOR_DATA *p_editor Line 674  int editor_display(EDITOR_DATA *p_editor
674                                                  continue;                                                  continue;
675                                          }                                          }
676                                  }                                  }
677                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
678                                  {                                  {
679                                          last_updated_line = line_current;                                          if (ch == BACKSPACE)
680                                            {
681                                                    col_pos--;
682                                                    if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0)
683                                                    {
684                                                            row_pos--;
685                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);
686                                                    }
687                                            }
688    
689                                          if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,                                          if ((str_len = editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,
690                                                                                     &last_updated_line) < 0)                                                                                                            &last_updated_line)) < 0)
691                                          {                                          {
692                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
693                                          }                                          }
694                                          else                                          else
695                                          {                                          {
696                                                    if (ch == BACKSPACE)
697                                                    {
698                                                            for (i = 1; i < str_len; i++)
699                                                            {
700                                                                    col_pos--;
701                                                                    if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0)
702                                                                    {
703                                                                            row_pos--;
704                                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);
705                                                                    }
706                                                            }
707                                                    }
708    
709                                                  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));
710                                                  line_current -= (screen_current_row - row_pos);                                                  line_current -= (screen_current_row - row_pos);
711                                                  screen_current_row = (int)row_pos;                                                  screen_current_row = (int)row_pos;
712    
713                                                    if (screen_current_row < screen_begin_row) // row_pos <= 0
714                                                    {
715                                                            screen_current_row = screen_begin_row;
716                                                            row_pos = screen_begin_row;
717                                                            screen_end_row = SCREEN_ROWS - 1;
718                                                    }
719                                          }                                          }
720    
721                                          continue;                                          continue;
# Line 671  int editor_display(EDITOR_DATA *p_editor Line 744  int editor_display(EDITOR_DATA *p_editor
744                                          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]));
745                                          break;                                          break;
746                                  case KEY_INS:                                  case KEY_INS:
747                                          insert = !insert;                                          key_insert = !key_insert;
748                                          break;                                          break;
749                                  case KEY_HOME:                                  case KEY_HOME:
750                                          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