/[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.4 by sysadm, Wed Jun 11 04:27:48 2025 UTC Revision 1.9 by sysadm, Wed Jun 11 10:44:33 2025 UTC
# Line 208  int editor_data_insert(EDITOR_DATA *p_ed Line 208  int editor_data_insert(EDITOR_DATA *p_ed
208          }          }
209    
210          // Split current data line if over-length          // Split current data line if over-length
211          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)
212          {          {
213                  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)
214                  {                  {
# Line 227  int editor_data_insert(EDITOR_DATA *p_ed Line 227  int editor_data_insert(EDITOR_DATA *p_ed
227                  p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;                  p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;
228                  (p_editor_data->data_line_total)++;                  (p_editor_data->data_line_total)++;
229    
230                  if (offset_data_line + str_len + 2 < MAX_EDITOR_DATA_LINE_LENGTH)                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
231                  {                  {
232                          // Copy rest part of current data line to new data line                          if (str[0] == CR)
233                          memcpy(p_data_line,                          {
234                                     p_editor_data->p_display_lines[display_line] + offset,                                  str_len = 0;
235                                     (size_t)(len_data_line - offset_data_line));                          }
   
                         p_data_line[len_data_line - offset_data_line] = '\0';  
   
                         // Append str to current display line  
                         memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);  
   
                         // Add line ending to current display line (data line)  
                         p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';  
                         p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';  
                         p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;  
236    
                         *p_display_line = display_line;  
                         *p_offset = offset + str_len;  
                 }  
                 else  
                 {  
237                          // Copy str to new data line                          // Copy str to new data line
238                          memcpy(p_data_line, str, (size_t)str_len);                          memcpy(p_data_line, str, (size_t)str_len);
239    
# Line 267  int editor_data_insert(EDITOR_DATA *p_ed Line 252  int editor_data_insert(EDITOR_DATA *p_ed
252                          *p_display_line = display_line + 1;                          *p_display_line = display_line + 1;
253                          *p_offset = str_len;                          *p_offset = str_len;
254                  }                  }
255                    else
256                    {
257                            // Copy rest part of current data line to new data line
258                            memcpy(p_data_line,
259                                       p_editor_data->p_display_lines[display_line] + offset,
260                                       (size_t)(len_data_line - offset_data_line));
261    
262                            p_data_line[len_data_line - offset_data_line] = '\0';
263    
264                            // Append str to current display line
265                            memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);
266    
267                            // Add line ending to current display line (data line)
268                            p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';
269                            p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';
270                            p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;
271    
272                            *p_display_line = display_line;
273                            *p_offset = offset + str_len;
274                    }
275    
276                  split_line_total = last_display_line - display_line + 3;                  split_line_total = last_display_line - display_line + 3;
277    
# Line 277  int editor_data_insert(EDITOR_DATA *p_ed Line 282  int editor_data_insert(EDITOR_DATA *p_ed
282          }          }
283          else // insert str into current data line at offset_data_line          else // insert str into current data line at offset_data_line
284          {          {
                 log_error("Insert %d chars into display_line = %d, offset = %d\n", str_len, display_line, offset);  
   
285                  memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line));                  memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line));
286                  memcpy(p_data_line + offset_data_line, str, (size_t)str_len);                  memcpy(p_data_line + offset_data_line, str, (size_t)str_len);
287                  p_data_line[len_data_line + str_len] = '\0';                  p_data_line[len_data_line + str_len] = '\0';
# Line 293  int editor_data_insert(EDITOR_DATA *p_ed Line 296  int editor_data_insert(EDITOR_DATA *p_ed
296    
297          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
298          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
         log_error("Debug: split data line, display_line = %ld, j = %ld\n", display_line, split_line_total);  
299    
300          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
301          {          {
# Line 323  int editor_data_insert(EDITOR_DATA *p_ed Line 325  int editor_data_insert(EDITOR_DATA *p_ed
325                  if (p_editor_data->display_line_lengths[display_line + i] > 0 &&                  if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
326                          p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')                          p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
327                  {                  {
                         log_error("Debug: reach end of data line, i = %ld, j = %ld\n", i, split_line_total);  
328                          break;                          break;
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 349  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          return 0;          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                    if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
429                    {
430                            log_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total);
431                            return 0;
432                    }
433    
434                    len_data_line = 0; // Next data line
435                    last_display_line = p_editor_data->display_line_total - 1;
436                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
437                    {
438                            len_data_line += p_editor_data->display_line_lengths[i];
439    
440                            if (p_editor_data->display_line_lengths[i] > 0 &&
441                                    p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
442                            {
443                                    last_display_line = i;
444                                    break;
445                            }
446                    }
447    
448                    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
449                    {
450                            log_common("Debug: No enough buffer to merge with next data line: %ld > %ld\n",
451                                               offset_data_line + len_data_line + 1, MAX_EDITOR_DATA_LINE_LENGTH);
452                            return 0;
453                    }
454    
455                    // Append next data line to current one
456                    memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
457                    p_data_line[offset_data_line + len_data_line] = '\0';
458    
459                    // Recycle next data line
460                    // TODO: free(p_editor_data->p_display_lines[display_line + 1]);
461            }
462            else
463            {
464                    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));
465                    p_data_line[len_data_line - str_len] = '\0';
466                    len_data_line -= str_len;
467            }
468    
469            // Set p_data_line to head of current display line
470            p_data_line = p_editor_data->p_display_lines[display_line];
471            split_line_total = last_display_line - display_line + 2;
472    
473            // Split current data line since beginning of current display line
474            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
475    
476            for (i = 0; i < split_line_total; i++)
477            {
478                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
479                    p_editor_data->p_display_lines[display_line + i] =
480                            (i == 0
481                                     ? p_data_line
482                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
483    
484                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
485                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
486                    {
487                            break;
488                    }
489            }
490    
491            *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
492    
493            if (display_line + i < last_display_line)
494            {
495                    // Remove redundant display line after last_display_line
496                    for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
497                    {
498                            p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j];
499                            p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j];
500                    }
501    
502                    (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));
503                    last_display_line = display_line + i;
504    
505                    *p_last_updated_line = p_editor_data->display_line_total - 1;
506            }
507    
508            return str_len;
509  }  }
510    
511  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 387  int editor_display(EDITOR_DATA *p_editor Line 543  int editor_display(EDITOR_DATA *p_editor
543          long display_line_out, offset_out;          long display_line_out, offset_out;
544          int scroll_rows;          int scroll_rows;
545          long last_updated_line = 0;          long last_updated_line = 0;
546          int insert = 1;          int key_insert = 1;
547          int i;          int i;
548    
549          screen_current_row = screen_begin_row;          screen_current_row = screen_begin_row;
# Line 408  int editor_display(EDITOR_DATA *p_editor Line 564  int editor_display(EDITOR_DATA *p_editor
564    
565                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
566                                           "\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] "
567                                           "第\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] "
568                                           "%s",                                           "%s",
569                                           row_pos, col_pos,                                           row_pos, col_pos,
570                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
571                                             key_insert ? "插入" : "改写",
572                                           ctx.msg);                                           ctx.msg);
573    
574                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);
# Line 450  int editor_display(EDITOR_DATA *p_editor Line 607  int editor_display(EDITOR_DATA *p_editor
607                                          str_len = 0;                                          str_len = 0;
608                                  }                                  }
609    
610                                  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) || ch == CR) // printable character or GBK
611                                  {                                  {
612                                          if (str_len == 0)                                          if (str_len == 0)
613                                          {                                          {
# Line 464  int editor_display(EDITOR_DATA *p_editor Line 621  int editor_display(EDITOR_DATA *p_editor
621                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
622                                          offset_out = offset_in;                                          offset_out = offset_in;
623    
624                                            if (!key_insert) // overwrite
625                                            {
626                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
627                                                                                               &last_updated_line) < 0)
628                                                    {
629                                                            log_error("editor_data_delete() error\n");
630                                                    }
631                                            }
632    
633                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
634                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
635                                          {                                          {
# Line 506  int editor_display(EDITOR_DATA *p_editor Line 672  int editor_display(EDITOR_DATA *p_editor
672                                                  continue;                                                  continue;
673                                          }                                          }
674                                  }                                  }
675                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
676                                  {                                  {
677                                          last_updated_line = line_current;                                          if (ch == BACKSPACE)
678                                            {
679                                                    col_pos--;
680                                                    if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0)
681                                                    {
682                                                            row_pos--;
683                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);
684                                                    }
685                                            }
686    
687                                          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,
688                                                                                     &last_updated_line) < 0)                                                                                                            &last_updated_line)) < 0)
689                                          {                                          {
690                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
691                                          }                                          }
692                                          else                                          else
693                                          {                                          {
694                                                    if (ch == BACKSPACE)
695                                                    {
696                                                            for (i = 1; i < str_len; i++)
697                                                            {
698                                                                    col_pos--;
699                                                                    if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0)
700                                                                    {
701                                                                            row_pos--;
702                                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);
703                                                                    }
704                                                            }
705                                                    }
706    
707                                                  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));
708                                                    line_current -= (screen_current_row - row_pos);
709                                                    screen_current_row = (int)row_pos;
710    
711                                                    if (screen_current_row < screen_begin_row) // row_pos <= 0
712                                                    {
713                                                            screen_current_row = screen_begin_row;
714                                                            row_pos = screen_begin_row;
715                                                            screen_end_row = SCREEN_ROWS - 1;
716                                                    }
717                                          }                                          }
718    
719                                          continue;                                          continue;
# Line 546  int editor_display(EDITOR_DATA *p_editor Line 742  int editor_display(EDITOR_DATA *p_editor
742                                          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]));
743                                          break;                                          break;
744                                  case KEY_INS:                                  case KEY_INS:
745                                          insert = !insert;                                          key_insert = !key_insert;
746                                          break;                                          break;
747                                  case KEY_HOME:                                  case KEY_HOME:
748                                          row_pos = 1;                                          row_pos = 1;
# Line 600  int editor_display(EDITOR_DATA *p_editor Line 796  int editor_display(EDITOR_DATA *p_editor
796                                          screen_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line                                          screen_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
797                                          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]));
798                                          break;                                          break;
                                 case CR:  
                                         break;  
799                                  case KEY_SPACE:                                  case KEY_SPACE:
800                                          break;                                          break;
801                                  case KEY_RIGHT:                                  case KEY_RIGHT:


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

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