/[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.29 by sysadm, Tue Jun 17 13:25:49 2025 UTC Revision 1.42 by sysadm, Tue Sep 30 06:05:48 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "editor.h"  
17  #include "bbs.h"  #include "bbs.h"
18    #include "common.h"
19    #include "editor.h"
20  #include "io.h"  #include "io.h"
21  #include "log.h"  #include "log.h"
 #include "common.h"  
 #include "str_process.h"  
22  #include "memory_pool.h"  #include "memory_pool.h"
23    #include "str_process.h"
24  #include <stdlib.h>  #include <stdlib.h>
 #include <sys/param.h>  
 #include <strings.h>  
25  #include <string.h>  #include <string.h>
26    #include <sys/param.h>
27    
28  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
29  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000
# Line 94  EDITOR_DATA *editor_data_load(const char Line 93  EDITOR_DATA *editor_data_load(const char
93                  return NULL;                  return NULL;
94          }          }
95    
96          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, 0);          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1,
97                                                                                                                     0, p_editor_data->display_line_widths);
98    
99          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
100          {          {
# Line 131  EDITOR_DATA *editor_data_load(const char Line 131  EDITOR_DATA *editor_data_load(const char
131                          p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')                          p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
132                  {                  {
133                          p_editor_data->display_line_lengths[i]--;                          p_editor_data->display_line_lengths[i]--;
134                            p_editor_data->display_line_widths[i]--;
135                          current_data_line_length--;                          current_data_line_length--;
136                  }                  }
137                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
# Line 213  int editor_data_insert(EDITOR_DATA *p_ed Line 214  int editor_data_insert(EDITOR_DATA *p_ed
214          long offset_data_line;          long offset_data_line;
215          long last_display_line; // of data line          long last_display_line; // of data line
216          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
217            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
218          long split_line_total;          long split_line_total;
219          long i, j;          long i;
220          int len;          int len;
221          int eol;          int eol;
222          int display_len;          int display_len;
# Line 225  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    
         // Validate str  
         if ((str_len == 1 && str[0] <= 0) ||  
                 (str_len == 2 && (str[0] >= 0 || str[1] >= 0)))  
         {  
                 log_error("Invalid input str, len=%d\n", str_len);  
                 return -2;  
         }  
   
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
230          // Get length of current data line          // Get length of current data line
231          len_data_line = 0;          len_data_line = 0;
232          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 275  int editor_data_insert(EDITOR_DATA *p_ed Line 256  int editor_data_insert(EDITOR_DATA *p_ed
256          }          }
257    
258          // Split current data line if over-length          // Split current data line if over-length
259          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)          if (len_data_line + str_len + 2 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
260          {          {
261                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
262                  {                  {
# Line 295  int editor_data_insert(EDITOR_DATA *p_ed Line 276  int editor_data_insert(EDITOR_DATA *p_ed
276                          return -2;                          return -2;
277                  }                  }
278    
279                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)                  if (offset_data_line + str_len + 2 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
280                  {                  {
281                          if (str[0] == CR)                          if (str[0] == CR)
282                          {                          {
# Line 341  int editor_data_insert(EDITOR_DATA *p_ed Line 322  int editor_data_insert(EDITOR_DATA *p_ed
322                          *p_offset = offset + str_len;                          *p_offset = offset + str_len;
323                  }                  }
324    
325                    // Update display width of current display line
326                    len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0);
327                    p_editor_data->display_line_widths[display_line] = display_len;
328    
329                  split_line_total = last_display_line - display_line + 3;                  split_line_total = last_display_line - display_line + 3;
330    
331                  // Set start display_line for spliting new data line                  // Set start display_line for spliting new data line
# Line 363  int editor_data_insert(EDITOR_DATA *p_ed Line 348  int editor_data_insert(EDITOR_DATA *p_ed
348          }          }
349    
350          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
351          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
352    
353          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
354          {          {
# Line 383  int editor_data_insert(EDITOR_DATA *p_ed Line 368  int editor_data_insert(EDITOR_DATA *p_ed
368                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
369                                          p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';                                          p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
370                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
371                                            p_editor_data->display_line_widths[display_line + i - 1] = display_len;
372                                  }                                  }
373                                  if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])                                  if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
374                                  {                                  {
# Line 390  int editor_data_insert(EDITOR_DATA *p_ed Line 376  int editor_data_insert(EDITOR_DATA *p_ed
376                                  }                                  }
377                                  break;                                  break;
378                          }                          }
379                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)  
380                          {                          // for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
381                                  p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];                          // {
382                                  p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];                          //      p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
383                          }                          //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
384                            //      p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1];
385                            // }
386                            memmove(p_editor_data->p_display_lines + last_display_line + 2,
387                                            p_editor_data->p_display_lines + last_display_line + 1,
388                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
389                                                    sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
390                            memmove(p_editor_data->display_line_lengths + last_display_line + 2,
391                                            p_editor_data->display_line_lengths + last_display_line + 1,
392                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
393                                                    sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
394                            memmove(p_editor_data->display_line_widths + last_display_line + 2,
395                                            p_editor_data->display_line_widths + last_display_line + 1,
396                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
397                                                    sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
398    
399                          last_display_line++;                          last_display_line++;
400                            *p_last_updated_line = p_editor_data->display_line_total;
401                          (p_editor_data->display_line_total)++;                          (p_editor_data->display_line_total)++;
402                  }                  }
403    
404                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
405                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
406                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
407                          (i == 0                          (i == 0
408                                   ? p_data_line                                   ? p_data_line
# Line 429  int editor_data_insert(EDITOR_DATA *p_ed Line 432  int editor_data_insert(EDITOR_DATA *p_ed
432                  len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);                  len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
433                  p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';                  p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
434                  p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;                  p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
435                    p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len;
436                  if (*p_display_line + 1 >= p_editor_data->display_line_total)                  if (*p_display_line + 1 >= p_editor_data->display_line_total)
437                  {                  {
438                          *p_offset = MIN(*p_offset, len);                          *p_offset = MIN(*p_offset, len);
# Line 449  int editor_data_delete(EDITOR_DATA *p_ed Line 453  int editor_data_delete(EDITOR_DATA *p_ed
453          long offset_data_line;          long offset_data_line;
454          long last_display_line; // of data line          long last_display_line; // of data line
455          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
456            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
457          long split_line_total;          long split_line_total;
458          long i, j;          long i, j;
459          int str_len = 0;          int str_len = 0;
460            char c;
461    
462          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
463          {          {
# Line 459  int editor_data_delete(EDITOR_DATA *p_ed Line 465  int editor_data_delete(EDITOR_DATA *p_ed
465                  return -1;                  return -1;
466          }          }
467    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
468          // Get length of current data line          // Get length of current data line
469          len_data_line = 0;          len_data_line = 0;
470          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 510  int editor_data_delete(EDITOR_DATA *p_ed Line 503  int editor_data_delete(EDITOR_DATA *p_ed
503          {          {
504                  str_len = 1;                  str_len = 1;
505          }          }
506          else if (p_data_line[offset_data_line + 1] < 0) // GBK          else if (p_data_line[offset_data_line] & 0b10000000) // head of multi-byte character
507          {          {
508                  str_len = 2;                  str_len = 1;
509                    c = (p_data_line[offset_data_line] & 0b01110000) << 1;
510                    while (c & 0b10000000)
511                    {
512                            str_len++;
513                            c = (c & 0b01111111) << 1;
514                    }
515          }          }
516          else          else
517          {          {
# Line 568  int editor_data_delete(EDITOR_DATA *p_ed Line 567  int editor_data_delete(EDITOR_DATA *p_ed
567          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
568    
569          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
570          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
571    
572          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
573          {          {
574                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
575                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
576                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
577                          (i == 0                          (i == 0
578                                   ? p_data_line                                   ? p_data_line
# Line 590  int editor_data_delete(EDITOR_DATA *p_ed Line 590  int editor_data_delete(EDITOR_DATA *p_ed
590          if (*p_last_updated_line < last_display_line)          if (*p_last_updated_line < last_display_line)
591          {          {
592                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
593                  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++)
594                  {                  // {
595                          p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = 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];
596                          p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = 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];
597                  }                  //      p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j];
598                    // }
599                    memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
600                                    p_editor_data->p_display_lines + last_display_line + 1,
601                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
602                                            sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
603                    memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
604                                    p_editor_data->display_line_lengths + last_display_line + 1,
605                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
606                                            sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
607                    memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1,
608                                    p_editor_data->display_line_widths + last_display_line + 1,
609                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
610                                            sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
611    
612                  j = p_editor_data->display_line_total;                  j = p_editor_data->display_line_total;
613                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
# Line 613  static int editor_display_key_handler(in Line 626  static int editor_display_key_handler(in
626          {          {
627          case 0: // Set msg          case 0: // Set msg
628                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
629                                   "| Í˳ö[\033[32mCtrl-W\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
630                  break;                  break;
631          case KEY_CSI:          case KEY_CSI:
632                  *p_key = KEY_ESC;                  *p_key = KEY_ESC;
# Line 630  int editor_display(EDITOR_DATA *p_editor Line 643  int editor_display(EDITOR_DATA *p_editor
643          EDITOR_CTX ctx;          EDITOR_CTX ctx;
644          int ch = 0;          int ch = 0;
645          char input_str[4];          char input_str[4];
646            char c;
647          int str_len = 0;          int str_len = 0;
648          int input_ok;          int input_ok;
649          const int screen_begin_row = 1;          const int screen_begin_row = 1;
# Line 657  int editor_display(EDITOR_DATA *p_editor Line 671  int editor_display(EDITOR_DATA *p_editor
671                  return ch;                  return ch;
672          }          }
673    
674          loop = 1;          for (loop = 1; !SYS_server_exit && loop;)
         while (!SYS_server_exit && loop)  
675          {          {
676                  if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row)                  if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row)
677                  {                  {
# Line 666  int editor_display(EDITOR_DATA *p_editor Line 679  int editor_display(EDITOR_DATA *p_editor
679    
680                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
681                                           "\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] "
682                                           "µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ [\033[32m%s\033[33m] "                                           "第\033[32m%ld\033[33m/\033[32m%ld\033[33m行 [\033[32m%s\033[33m] "
683                                           "%s",                                           "%s",
684                                           row_pos, col_pos,                                           row_pos, col_pos,
685                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
686                                           key_insert ? "²åÈë" : "Ìæ»»",                                           key_insert ? "æ’å…¥" : "替æ¢",
687                                           ctx.msg);                                           ctx.msg);
688    
689                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
# Line 688  int editor_display(EDITOR_DATA *p_editor Line 701  int editor_display(EDITOR_DATA *p_editor
701                          iflush();                          iflush();
702    
703                          str_len = 0;                          str_len = 0;
                         input_ok = 0;  
704                          ch = igetch_t(MAX_DELAY_TIME);                          ch = igetch_t(MAX_DELAY_TIME);
705                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit)
706                          {                          {
707                                  // extended key handler                                  // extended key handler
708                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 698  int editor_display(EDITOR_DATA *p_editor Line 710  int editor_display(EDITOR_DATA *p_editor
710                                          goto cleanup;                                          goto cleanup;
711                                  }                                  }
712    
713                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch < 256 && (ch & 0b10000000)) // head of multi-byte character
                                 {  
                                         input_str[str_len] = (char)(ch - 256);  
                                         str_len++;  
                                 }  
                                 else if (str_len > 0)  
714                                  {                                  {
                                         log_error("Received %d character over 127 followed by character less than 127\n", str_len);  
715                                          str_len = 0;                                          str_len = 0;
716                                            c = (char)(ch & 0b11110000);
717                                            while (c & 0b10000000)
718                                            {
719                                                    input_str[str_len] = (char)(ch - 256);
720                                                    str_len++;
721                                                    c = (c & 0b01111111) << 1;
722    
723                                                    if ((c & 0b10000000) == 0) // Input completed
724                                                    {
725                                                            break;
726                                                    }
727    
728                                                    // Expect additional bytes of input
729                                                    ch = igetch(100);                                                // 0.1 second
730                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
731                                                    {
732    #ifdef _DEBUG
733                                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
734    #endif
735                                                            str_len = 0;
736                                                            break;
737                                                    }
738                                            }
739                                  }                                  }
740    
741                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character
742                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                // Special character
743                                  {                                  {
744                                          BBS_last_access_tm = time(NULL);                                          BBS_last_access_tm = time(NULL);
745    
# Line 720  int editor_display(EDITOR_DATA *p_editor Line 749  int editor_display(EDITOR_DATA *p_editor
749                                                  str_len = 1;                                                  str_len = 1;
750                                          }                                          }
751    
                                         last_updated_line = line_current;  
752                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
753                                          offset_in = col_pos - 1;                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
754                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
755                                          offset_out = offset_in;                                          offset_out = offset_in;
756    
757                                            last_updated_line = display_line_in;
758    
759                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
760                                          {                                          {
761                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
# Line 754  int editor_display(EDITOR_DATA *p_editor Line 784  int editor_display(EDITOR_DATA *p_editor
784                                                          clrtoeol();                                                          clrtoeol();
785                                                          for (i = 0; i < scroll_rows; i++)                                                          for (i = 0; i < scroll_rows; i++)
786                                                          {                                                          {
787                                                                  prints("\033[S"); // Scroll up 1 line                                                                  // prints("\033[S"); // Scroll up 1 line
788                                                                    prints("\n"); // Legacy Cterm only works with this line
789                                                          }                                                          }
790    
791                                                          output_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
# Line 769  int editor_display(EDITOR_DATA *p_editor Line 800  int editor_display(EDITOR_DATA *p_editor
800                                                  {                                                  {
801                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
802                                                  }                                                  }
803                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos  
804                                                    if (offset_out != offset_in)
805                                                    {
806                                                            if (display_line_out != display_line_in)
807                                                            {
808                                                                    col_pos = 1;
809                                                            }
810                                                            if (offset_out > 0)
811                                                            {
812                                                                    col_pos += (str_len == 1 ? 1 : 2);
813                                                            }
814                                                    }
815                                          }                                          }
816    
817                                          if (display_line_out != display_line_in) // Output on line change                                          if (display_line_out != display_line_in) // Output on line change
# Line 794  int editor_display(EDITOR_DATA *p_editor Line 836  int editor_display(EDITOR_DATA *p_editor
836                                          {                                          {
837                                                  if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden                                                  if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
838                                                  {                                                  {
839                                                          ch = igetch_t(MAX_DELAY_TIME);                                                          break; // force output prior operation result if any
                                                         continue;  
840                                                  }                                                  }
841    
842                                                  col_pos--;                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
843                                                  if (col_pos > 1 &&                                                                                             (int)col_pos - 1, &eol, &display_len, 0);
844                                                          p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK                                                  if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
845                                                  {                                                  {
846                                                          col_pos--;                                                          col_pos = display_len - 1;
847                                                    }
848                                                    else
849                                                    {
850                                                            col_pos = display_len;
851                                                  }                                                  }
852    
853                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
854                                                  {                                                  {
855                                                          row_pos--;                                                          row_pos--;
856                                                          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_widths[line_current - output_current_row + row_pos]);
857                                                  }                                                  }
858                                          }                                          }
859    
860                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
861                                          offset_in = col_pos - 1;                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
862                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
863                                          offset_out = offset_in;                                          offset_out = offset_in;
864    
# Line 824  int editor_display(EDITOR_DATA *p_editor Line 869  int editor_display(EDITOR_DATA *p_editor
869                                          }                                          }
870                                          else                                          else
871                                          {                                          {
872                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos                                                  col_pos = display_len + 1; // Set col_pos to accurate pos
873    
874                                                  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));
875                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
# Line 875  int editor_display(EDITOR_DATA *p_editor Line 920  int editor_display(EDITOR_DATA *p_editor
920                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
921                                          goto cleanup;                                          goto cleanup;
922                                  case Ctrl('W'):                                  case Ctrl('W'):
923                                    case Ctrl('X'):
924                                          loop = 0;                                          loop = 0;
925                                          break;                                          break;
926                                  case Ctrl('S'): // Start of line                                  case Ctrl('S'): // Start of line
# Line 886  int editor_display(EDITOR_DATA *p_editor Line 932  int editor_display(EDITOR_DATA *p_editor
932                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
933                                          {                                          {
934                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
935                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
936                                                  break;                                                  break;
937                                          }                                          }
938                                          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_widths[line_current - output_current_row + row_pos]);
939                                          break;                                          break;
940                                  case Ctrl('T'): // Top of screen                                  case Ctrl('T'): // Top of screen
941                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
942                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
943                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
944                                          break;                                          break;
945                                  case Ctrl('B'): // Bottom of screen                                  case Ctrl('B'): // Bottom of screen
946                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
# Line 909  int editor_display(EDITOR_DATA *p_editor Line 955  int editor_display(EDITOR_DATA *p_editor
955                                          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
956                                          {                                          {
957                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
958                                                  col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);                                                  col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1);
959                                          }                                          }
960                                          else                                          else
961                                          {                                          {
962                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
963                                          }                                          }
964                                          break;                                          break;
965                                  case KEY_INS:                                  case KEY_INS:
# Line 935  int editor_display(EDITOR_DATA *p_editor Line 981  int editor_display(EDITOR_DATA *p_editor
981                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
982                                          {                                          {
983                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
984                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
985                                                  break;                                                  break;
986                                          }                                          }
987                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
988                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
989                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
990                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
991                                          col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                          col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
992                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
993                                          break;                                          break;
994                                  case KEY_LEFT:                                  case KEY_LEFT:
995                                          if (col_pos > 1)                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
996                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
997                                            if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
998                                            {
999                                                    col_pos = display_len - 1;
1000                                            }
1001                                            else
1002                                            {
1003                                                    col_pos = display_len;
1004                                            }
1005                                            if (col_pos >= 1)
1006                                          {                                          {
                                                 col_pos--;  
                                                 if (col_pos > 1 &&  
                                                         p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&  
                                                         p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK  
                                                 {  
                                                         col_pos--;  
                                                 }  
1007                                                  break;                                                  break;
1008                                          }                                          }
1009                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 962  int editor_display(EDITOR_DATA *p_editor Line 1011  int editor_display(EDITOR_DATA *p_editor
1011                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
1012                                          {                                          {
1013                                                  row_pos--;                                                  row_pos--;
1014                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1015                                                  break;                                                  break;
1016                                          }                                          }
1017                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 975  int editor_display(EDITOR_DATA *p_editor Line 1024  int editor_display(EDITOR_DATA *p_editor
1024                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
1025                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
1026                                          output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line                                          output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
1027                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1028                                          break;                                          break;
1029                                  case KEY_SPACE:                                  case KEY_SPACE:
1030                                          break;                                          break;
1031                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1032                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1033                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1034                                            if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&
1035                                                    p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8
1036                                            {
1037                                                    col_pos = display_len + 3;
1038                                            }
1039                                            else
1040                                            {
1041                                                    col_pos = display_len + 2;
1042                                            }
1043                                            if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1044                                          {                                          {
                                                 if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&  
                                                         p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK  
                                                 {  
                                                         col_pos++;  
                                                 }  
                                                 col_pos++;  
1045                                                  break;                                                  break;
1046                                          }                                          }
1047                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
# Line 995  int editor_display(EDITOR_DATA *p_editor Line 1049  int editor_display(EDITOR_DATA *p_editor
1049                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))
1050                                          {                                          {
1051                                                  row_pos++;                                                  row_pos++;
1052                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1053                                                  break;                                                  break;
1054                                          }                                          }
1055                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
1056                                          {                                          {
1057                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
1058                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1059                                                  break;                                                  break;
1060                                          }                                          }
1061                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
1062                                          output_current_row = screen_row_total;                                          output_current_row = screen_row_total;
1063                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1064                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1065                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1066                                          clrtoeol();                                          clrtoeol();
1067                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
1068                                            prints("\n"); // Legacy Cterm only works with this line
1069                                          break;                                          break;
1070                                  case KEY_PGUP:                                  case KEY_PGUP:
1071                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 1024  int editor_display(EDITOR_DATA *p_editor Line 1079  int editor_display(EDITOR_DATA *p_editor
1079                                          }                                          }
1080                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1081                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1082                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1083                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1084                                          break;                                          break;
1085                                  case KEY_PGDN:                                  case KEY_PGDN:
# Line 1039  int editor_display(EDITOR_DATA *p_editor Line 1094  int editor_display(EDITOR_DATA *p_editor
1094                                          }                                          }
1095                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1096                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1097                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1098                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1099                                          break;                                          break;
1100                                  case KEY_F1:                                  case KEY_F1:


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

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