/[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.16 by sysadm, Thu Jun 12 10:34:21 2025 UTC Revision 1.22 by sysadm, Sun Jun 15 04:43:33 2025 UTC
# Line 79  EDITOR_DATA *editor_data_load(const char Line 79  EDITOR_DATA *editor_data_load(const char
79  {  {
80          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
81          char *p_data_line = NULL;          char *p_data_line = NULL;
82          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
83          long current_data_line_length = 0;          long current_data_line_length = 0;
84          long i;          long i;
85    
# Line 96  EDITOR_DATA *editor_data_load(const char Line 96  EDITOR_DATA *editor_data_load(const char
96                  return NULL;                  return NULL;
97          }          }
98    
99          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1);
100    
101          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
102          {          {
# Line 126  EDITOR_DATA *editor_data_load(const char Line 126  EDITOR_DATA *editor_data_load(const char
126    
127                  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]);
128                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
129    
130                    // Trim \n from last line
131                    if (i + 1 == p_editor_data->display_line_total &&
132                            p_editor_data->display_line_lengths[i] > 0 &&
133                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
134                    {
135                            p_editor_data->display_line_lengths[i]--;
136                            current_data_line_length--;
137                    }
138                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
139          }          }
140    
# Line 218  int editor_data_insert(EDITOR_DATA *p_ed Line 227  int editor_data_insert(EDITOR_DATA *p_ed
227                  return -1;                  return -1;
228          }          }
229    
230            // Validate str
231            if ((str_len == 1 && str[0] <= 0) ||
232                    (str_len == 2 && (str[0] >= 0 || str[1] >= 0)))
233            {
234                    log_error("Invalid input str, len=%d\n", str_len);
235                    return -2;
236            }
237    
238          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
239          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
240          {          {
# Line 395  int editor_data_insert(EDITOR_DATA *p_ed Line 412  int editor_data_insert(EDITOR_DATA *p_ed
412    
413          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
414          {          {
415                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                  if (*p_display_line + 1 < p_editor_data->display_line_total)
   
                 if (*p_display_line + 1 >= p_editor_data->display_line_total)  
416                  {                  {
417                          log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);                          *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
418                            (*p_display_line)++;
419                  }                  }
420                  else          }
421    
422            // Prevent the last display line from being over-length
423            if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
424            {
425                    len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len);
426                    p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
427                    p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
428                    if (*p_display_line + 1 >= p_editor_data->display_line_total)
429                  {                  {
430                          (*p_display_line)++;                          *p_offset = MIN(*p_offset, len);
431                            *p_display_line = p_editor_data->display_line_total - 1;
432                  }                  }
433          }          }
434    
# Line 469  int editor_data_delete(EDITOR_DATA *p_ed Line 494  int editor_data_delete(EDITOR_DATA *p_ed
494                  }                  }
495          }          }
496    
497            if (offset_data_line >= len_data_line) // end-of-line
498            {
499                    return 0;
500            }
501    
502          // Check str to be deleted          // Check str to be deleted
503          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
504          {          {
# Line 480  int editor_data_delete(EDITOR_DATA *p_ed Line 510  int editor_data_delete(EDITOR_DATA *p_ed
510          }          }
511          else          else
512          {          {
513                  log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n",                  log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
514                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1],                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
                                   p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]);  
515                  str_len = 1;                  str_len = 1;
516          }          }
517    
# Line 552  int editor_data_delete(EDITOR_DATA *p_ed Line 581  int editor_data_delete(EDITOR_DATA *p_ed
581    
582          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
583    
584          if (display_line + i < last_display_line)          if (*p_last_updated_line < last_display_line)
585          {          {
586                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
587                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
588                  {                  {
589                          p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j];                          p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j];
590                          p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j];                          p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];
591                  }                  }
592    
593                  (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));                  j = p_editor_data->display_line_total;
594                  last_display_line = display_line + i;                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
595                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
                 *p_last_updated_line = p_editor_data->display_line_total - 1;  
596          }          }
597    
598          return str_len;          return str_len;
# Line 576  static int editor_display_key_handler(in Line 604  static int editor_display_key_handler(in
604          {          {
605          case 0: // Set msg          case 0: // Set msg
606                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
607                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");
608                  break;                  break;
609          }          }
610    
# Line 664  int editor_display(EDITOR_DATA *p_editor Line 692  int editor_display(EDITOR_DATA *p_editor
692                                          input_str[str_len] = (char)(ch - 256);                                          input_str[str_len] = (char)(ch - 256);
693                                          str_len++;                                          str_len++;
694                                  }                                  }
695                                  else                                  else if (str_len > 0)
696                                  {                                  {
697                                            log_error("Received %d character over 127 followed by character less than 127\n", str_len);
698                                          str_len = 0;                                          str_len = 0;
699                                  }                                  }
700    
701                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK
702                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
703                                  {                                  {
704                                          if (str_len == 0)                                          if (str_len == 0) // ch >= 32 && ch < 127
705                                          {                                          {
706                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
707                                                  str_len = 1;                                                  str_len = 1;
# Line 697  int editor_display(EDITOR_DATA *p_editor Line 726  int editor_display(EDITOR_DATA *p_editor
726                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
727                                          {                                          {
728                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);
                                                 str_len = 0;  
729                                          }                                          }
730                                          else                                          else
731                                          {                                          {
                                                 str_len = 0;  
   
732                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
733                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
734                                                  output_current_row = (int)row_pos;                                                  output_current_row = (int)row_pos;
# Line 733  int editor_display(EDITOR_DATA *p_editor Line 759  int editor_display(EDITOR_DATA *p_editor
759                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1;
760                                          }                                          }
761    
762                                            str_len = 0;
763                                          continue;                                          continue;
764                                  }                                  }
765                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
766                                  {                                  {
767                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
768                                          {                                          {
769                                                    if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
770                                                    {
771                                                            input_ok = 0;
772                                                            continue;
773                                                    }
774    
775                                                  col_pos--;                                                  col_pos--;
776                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
777                                                  {                                                  {
# Line 789  int editor_display(EDITOR_DATA *p_editor Line 822  int editor_display(EDITOR_DATA *p_editor
822                                                          row_pos += scroll_rows;                                                          row_pos += scroll_rows;
823                                                          output_current_row = screen_begin_row;                                                          output_current_row = screen_begin_row;
824                                                          output_end_row = SCREEN_ROWS - 1;                                                          output_end_row = SCREEN_ROWS - 1;
                                                         clrline(output_current_row, SCREEN_ROWS);  
825                                                  }                                                  }
826    
827                                                    clrline(output_current_row, output_end_row);
828                                          }                                          }
829    
830                                            str_len = 0;
831                                          continue;                                          continue;
832                                  }                                  }
833    
# Line 801  int editor_display(EDITOR_DATA *p_editor Line 836  int editor_display(EDITOR_DATA *p_editor
836                                  case KEY_NULL:                                  case KEY_NULL:
837                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
838                                          goto cleanup;                                          goto cleanup;
839                                  case Ctrl('C'):                                  case Ctrl('W'):
840                                          loop = 0;                                          loop = 0;
841                                          break;                                          break;
842                                  case Ctrl('S'): // Start of line                                  case Ctrl('S'): // Start of line
# Line 810  int editor_display(EDITOR_DATA *p_editor Line 845  int editor_display(EDITOR_DATA *p_editor
845                                          break;                                          break;
846                                  case Ctrl('E'): // End of line                                  case Ctrl('E'): // End of line
847                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
848                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
849                                            {
850                                                    // last display line does NOT have \n in the end
851                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
852                                                    break;
853                                            }
854                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
855                                          break;                                          break;
856                                  case Ctrl('T'): // Top of screen                                  case Ctrl('T'): // Top of screen
# Line 819  int editor_display(EDITOR_DATA *p_editor Line 860  int editor_display(EDITOR_DATA *p_editor
860                                          break;                                          break;
861                                  case Ctrl('B'): // Bottom of screen                                  case Ctrl('B'): // Bottom of screen
862                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
863                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
864                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          {
865                                                    row_pos = p_editor_data->display_line_total;
866                                            }
867                                            else
868                                            {
869                                                    row_pos = SCREEN_ROWS - 1;
870                                            }
871                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
872                                            {
873                                                    // last display line does NOT have \n in the end
874                                                    col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);
875                                            }
876                                            else
877                                            {
878                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
879                                            }
880                                          break;                                          break;
881                                  case KEY_INS:                                  case KEY_INS:
882                                          key_insert = !key_insert;                                          key_insert = !key_insert;
# Line 841  int editor_display(EDITOR_DATA *p_editor Line 897  int editor_display(EDITOR_DATA *p_editor
897                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
898                                          {                                          {
899                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
900                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
901                                                  break;                                                  break;
902                                          }                                          }
903                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
904                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
905                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
906                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
907                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                          col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
908                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
909                                          break;                                          break;
910                                  case KEY_LEFT:                                  case KEY_LEFT:
# Line 895  int editor_display(EDITOR_DATA *p_editor Line 951  int editor_display(EDITOR_DATA *p_editor
951                                          }                                          }
952                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
953                                          {                                          {
954                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                                  // last display line does NOT have \n in the end
955                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
956                                                  break;                                                  break;
957                                          }                                          }
958                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));


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

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