/[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.20 by sysadm, Fri Jun 13 15:50:21 2025 UTC Revision 1.37 by sysadm, Tue Jul 1 08:31:11 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>  
   
 #define _POSIX_C_SOURCE 200809L  
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 79  EDITOR_DATA *editor_data_load(const char Line 76  EDITOR_DATA *editor_data_load(const char
76  {  {
77          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
78          char *p_data_line = NULL;          char *p_data_line = NULL;
79          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
80          long current_data_line_length = 0;          long current_data_line_length = 0;
81          long i;          long i;
82    
# Line 96  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);          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, 0);
97    
98          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
99          {          {
# Line 138  EDITOR_DATA *editor_data_load(const char Line 135  EDITOR_DATA *editor_data_load(const char
135                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
136          }          }
137    
138          bzero(p_editor_data->p_display_lines + p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);          memset(p_editor_data->p_display_lines + p_editor_data->display_line_total, 0, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);
139    
140          return p_editor_data;          return p_editor_data;
141  }  }
# Line 216  int editor_data_insert(EDITOR_DATA *p_ed Line 213  int editor_data_insert(EDITOR_DATA *p_ed
213          long last_display_line; // of data line          long last_display_line; // of data line
214          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
215          long split_line_total;          long split_line_total;
216          long i, j;          long i;
217          int len;          int len;
218          int eol;          int eol;
219          int display_len;          int display_len;
# Line 227  int editor_data_insert(EDITOR_DATA *p_ed Line 224  int editor_data_insert(EDITOR_DATA *p_ed
224                  return -1;                  return -1;
225          }          }
226    
227            // Validate str
228            if ((str_len == 1 && str[0] <= 0) ||
229                    (str_len == 2 && (str[0] >= 0 || str[1] >= 0)))
230            {
231                    log_error("Invalid input str, len=%d\n", str_len);
232                    return -2;
233            }
234    
235          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
236          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
237          {          {
238                  if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK                  if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK
239                  {                  {
240                          i++;                          i++;
241                  }                  }
# Line 269  int editor_data_insert(EDITOR_DATA *p_ed Line 274  int editor_data_insert(EDITOR_DATA *p_ed
274          }          }
275    
276          // Split current data line if over-length          // Split current data line if over-length
277          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)
278          {          {
279                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
280                  {                  {
281                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",  #ifdef _DEBUG
282                          //                p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                          log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
283                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
284    #endif
285    
286                          return -2;                          return -2;
287                  }                  }
288    
# Line 286  int editor_data_insert(EDITOR_DATA *p_ed Line 294  int editor_data_insert(EDITOR_DATA *p_ed
294                          return -2;                          return -2;
295                  }                  }
296    
297                  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)
298                  {                  {
299                          if (str[0] == CR)                          if (str[0] == CR)
300                          {                          {
# Line 354  int editor_data_insert(EDITOR_DATA *p_ed Line 362  int editor_data_insert(EDITOR_DATA *p_ed
362          }          }
363    
364          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
365          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, 0);
366    
367          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
368          {          {
# Line 363  int editor_data_insert(EDITOR_DATA *p_ed Line 371  int editor_data_insert(EDITOR_DATA *p_ed
371                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
372                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
373                          {                          {
374                                  // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);  #ifdef _DEBUG
375                                    log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
376    #endif
377    
378                                  // Terminate prior display line with \n, to avoid error on cleanup                                  // Terminate prior display line with \n, to avoid error on cleanup
379                                  if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)                                  if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
380                                  {                                  {
381                                          len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len);                                          len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
382                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
383                                          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';
384                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
# Line 378  int editor_data_insert(EDITOR_DATA *p_ed Line 389  int editor_data_insert(EDITOR_DATA *p_ed
389                                  }                                  }
390                                  break;                                  break;
391                          }                          }
392                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)  
393                          {                          // for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
394                                  p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];                          // {
395                                  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];
396                          }                          //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
397                            // }
398                            memmove(p_editor_data->p_display_lines + last_display_line + 2,
399                                            p_editor_data->p_display_lines + last_display_line + 1,
400                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
401                                                    sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
402                            memmove(p_editor_data->display_line_lengths + last_display_line + 2,
403                                            p_editor_data->display_line_lengths + last_display_line + 1,
404                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
405                                                    sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
406    
407                          last_display_line++;                          last_display_line++;
408                            *p_last_updated_line = p_editor_data->display_line_total;
409                          (p_editor_data->display_line_total)++;                          (p_editor_data->display_line_total)++;
410                  }                  }
411    
# Line 409  int editor_data_insert(EDITOR_DATA *p_ed Line 431  int editor_data_insert(EDITOR_DATA *p_ed
431                          *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                          *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
432                          (*p_display_line)++;                          (*p_display_line)++;
433                  }                  }
434                  else if (*p_display_line + 1 >= MAX_EDITOR_DATA_LINES)          }
435    
436            // Prevent the last display line from being over-length
437            if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
438            {
439                    len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
440                    p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
441                    p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
442                    if (*p_display_line + 1 >= p_editor_data->display_line_total)
443                  {                  {
444                          len = split_line(p_editor_data->p_display_lines[*p_display_line], SCREEN_COLS - 1, &eol, &display_len);                          *p_offset = MIN(*p_offset, len);
445                          p_editor_data->p_display_lines[*p_display_line][len] = '\0';                          *p_display_line = p_editor_data->display_line_total - 1;
                         p_editor_data->display_line_lengths[*p_display_line] = len;  
                         *p_offset = len;  
446                  }                  }
447          }          }
448    
449          return 0;          return 0;
450  }  }
451    
452  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,  int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
453                                             long *p_last_updated_line)                                             long *p_last_updated_line)
454  {  {
455            long display_line = *p_display_line;
456            long offset = *p_offset;
457          char *p_data_line = NULL;          char *p_data_line = NULL;
458          long len_data_line;          long len_data_line;
459          long offset_data_line;          long offset_data_line;
# Line 442  int editor_data_delete(EDITOR_DATA *p_ed Line 472  int editor_data_delete(EDITOR_DATA *p_ed
472          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
473          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
474          {          {
475                  if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK                  if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK
476                  {                  {
477                          i++;                          i++;
478                  }                  }
# Line 490  int editor_data_delete(EDITOR_DATA *p_ed Line 520  int editor_data_delete(EDITOR_DATA *p_ed
520          {          {
521                  str_len = 1;                  str_len = 1;
522          }          }
523          else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK          else if (p_data_line[offset_data_line + 1] < 0) // GBK
524          {          {
525                  str_len = 2;                  str_len = 2;
526          }          }
# Line 548  int editor_data_delete(EDITOR_DATA *p_ed Line 578  int editor_data_delete(EDITOR_DATA *p_ed
578          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
579    
580          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
581          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, 0);
582    
583          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
584          {          {
# Line 570  int editor_data_delete(EDITOR_DATA *p_ed Line 600  int editor_data_delete(EDITOR_DATA *p_ed
600          if (*p_last_updated_line < last_display_line)          if (*p_last_updated_line < last_display_line)
601          {          {
602                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
603                  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++)
604                  {                  // {
605                          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];
606                          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];
607                  }                  // }
608                    memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
609                                    p_editor_data->p_display_lines + last_display_line + 1,
610                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
611                                            sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
612                    memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
613                                    p_editor_data->display_line_lengths + last_display_line + 1,
614                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
615                                            sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
616    
617                  j = p_editor_data->display_line_total;                  j = p_editor_data->display_line_total;
618                  (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);
619                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
620          }          }
621    
622            // Return real offset
623            *p_offset = offset;
624    
625          return str_len;          return str_len;
626  }  }
627    
# Line 590  static int editor_display_key_handler(in Line 631  static int editor_display_key_handler(in
631          {          {
632          case 0: // Set msg          case 0: // Set msg
633                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
634                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] |");
635                    break;
636            case KEY_CSI:
637                    *p_key = KEY_ESC;
638                  break;                  break;
639          }          }
640    
# Line 631  int editor_display(EDITOR_DATA *p_editor Line 675  int editor_display(EDITOR_DATA *p_editor
675                  return ch;                  return ch;
676          }          }
677    
678          loop = 1;          for (loop = 1; !SYS_server_exit && loop;)
         while (!SYS_server_exit && loop)  
679          {          {
680                  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)
681                  {                  {
# Line 644  int editor_display(EDITOR_DATA *p_editor Line 687  int editor_display(EDITOR_DATA *p_editor
687                                           "%s",                                           "%s",
688                                           row_pos, col_pos,                                           row_pos, col_pos,
689                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
690                                           key_insert ? "²åÈë" : "¸Äд",                                           key_insert ? "²åÈë" : "Ìæ»»",
691                                           ctx.msg);                                           ctx.msg);
692    
693                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
694                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
695                          {                          {
696                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 661  int editor_display(EDITOR_DATA *p_editor Line 704  int editor_display(EDITOR_DATA *p_editor
704                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
705                          iflush();                          iflush();
706    
707                          input_ok = 0;                          str_len = 0;
708                          while (!SYS_server_exit && !input_ok)                          ch = igetch_t(MAX_DELAY_TIME);
709                            while (!SYS_server_exit)
710                          {                          {
                                 ch = igetch_t(MAX_DELAY_TIME);  
                                 input_ok = 1;  
   
711                                  // extended key handler                                  // extended key handler
712                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
713                                  {                                  {
# Line 678  int editor_display(EDITOR_DATA *p_editor Line 719  int editor_display(EDITOR_DATA *p_editor
719                                          input_str[str_len] = (char)(ch - 256);                                          input_str[str_len] = (char)(ch - 256);
720                                          str_len++;                                          str_len++;
721                                  }                                  }
722                                  else                                  else if (str_len > 0)
723                                  {                                  {
724                                            log_error("Received %d character over 127 followed by character less than 127\n", str_len);
725                                          str_len = 0;                                          str_len = 0;
726                                  }                                  }
727    
728                                  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
729                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
730                                  {                                  {
731                                          if (str_len == 0)                                          BBS_last_access_tm = time(NULL);
732    
733                                            if (str_len == 0) // ch >= 32 && ch < 127
734                                          {                                          {
735                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
736                                                  str_len = 1;                                                  str_len = 1;
737                                          }                                          }
738    
                                         last_updated_line = line_current;  
739                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
740                                          offset_in = col_pos - 1;                                          offset_in = col_pos - 1;
741                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
742                                          offset_out = offset_in;                                          offset_out = offset_in;
743    
744                                            last_updated_line = display_line_in;
745    
746                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
747                                          {                                          {
748                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
749                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line) < 0)
750                                                  {                                                  {
751                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
# Line 711  int editor_display(EDITOR_DATA *p_editor Line 756  int editor_display(EDITOR_DATA *p_editor
756                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
757                                          {                                          {
758                                                  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;  
759                                          }                                          }
760                                          else                                          else
761                                          {                                          {
                                                 str_len = 0;  
   
762                                                  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));
763                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
764                                                  output_current_row = (int)row_pos;                                                  output_current_row = (int)row_pos;
# Line 729  int editor_display(EDITOR_DATA *p_editor Line 771  int editor_display(EDITOR_DATA *p_editor
771                                                          clrtoeol();                                                          clrtoeol();
772                                                          for (i = 0; i < scroll_rows; i++)                                                          for (i = 0; i < scroll_rows; i++)
773                                                          {                                                          {
774                                                                  prints("\033[S"); // Scroll up 1 line                                                                  // prints("\033[S"); // Scroll up 1 line
775                                                                    prints("\n"); // Legacy Cterm only works with this line
776                                                          }                                                          }
777    
778                                                          output_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
# Line 744  int editor_display(EDITOR_DATA *p_editor Line 787  int editor_display(EDITOR_DATA *p_editor
787                                                  {                                                  {
788                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
789                                                  }                                                  }
790                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
791                                            }
792    
793                                            if (display_line_out != display_line_in) // Output on line change
794                                            {
795                                                    break;
796                                          }                                          }
797    
798                                            ch = igetch(0);
799                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
800                                            {
801                                                    break;
802                                            }
803    
804                                            str_len = 0;
805                                          continue;                                          continue;
806                                  }                                  }
807                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
808                                  {                                  {
809                                            BBS_last_access_tm = time(NULL);
810    
811                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
812                                          {                                          {
813                                                  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
814                                                  {                                                  {
815                                                          input_ok = 0;                                                          break; // force output prior operation result if any
                                                         continue;  
816                                                  }                                                  }
817    
818                                                  col_pos--;                                                  col_pos--;
819                                                    if (col_pos > 1 &&
820                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK
821                                                    {
822                                                            col_pos--;
823                                                    }
824    
825                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
826                                                  {                                                  {
827                                                          row_pos--;                                                          row_pos--;
# Line 767  int editor_display(EDITOR_DATA *p_editor Line 829  int editor_display(EDITOR_DATA *p_editor
829                                                  }                                                  }
830                                          }                                          }
831    
832                                          if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1,                                          display_line_in = line_current - output_current_row + row_pos;
833                                            offset_in = col_pos - 1;
834                                            display_line_out = display_line_in;
835                                            offset_out = offset_in;
836    
837                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
838                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line)) < 0)
839                                          {                                          {
840                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
841                                          }                                          }
842                                          else                                          else
843                                          {                                          {
844                                                  if (ch == BACKSPACE)                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
                                                 {  
                                                         for (i = 1; i < str_len; i++)  
                                                         {  
                                                                 col_pos--;  
                                                                 if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)  
                                                                 {  
                                                                         row_pos--;  
                                                                         col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);  
                                                                 }  
                                                         }  
                                                 }  
845    
846                                                  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));
847                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
# Line 814  int editor_display(EDITOR_DATA *p_editor Line 870  int editor_display(EDITOR_DATA *p_editor
870                                                  clrline(output_current_row, output_end_row);                                                  clrline(output_current_row, output_end_row);
871                                          }                                          }
872    
873                                            if (display_line_out != display_line_in) // Output on line change
874                                            {
875                                                    break;
876                                            }
877    
878                                            ch = igetch(0);
879                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
880                                            {
881                                                    break;
882                                            }
883    
884                                            str_len = 0;
885                                          continue;                                          continue;
886                                  }                                  }
887    
888                                    input_ok = 1;
889                                  switch (ch)                                  switch (ch)
890                                  {                                  {
891                                  case KEY_NULL:                                  case KEY_NULL:
# Line 831  int editor_display(EDITOR_DATA *p_editor Line 900  int editor_display(EDITOR_DATA *p_editor
900                                          break;                                          break;
901                                  case Ctrl('E'): // End of line                                  case Ctrl('E'): // End of line
902                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
903                                            if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
904                                            {
905                                                    // last display line does NOT have \n in the end
906                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
907                                                    break;
908                                            }
909                                          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]);
910                                          break;                                          break;
911                                  case Ctrl('T'): // Top of screen                                  case Ctrl('T'): // Top of screen
# Line 891  int editor_display(EDITOR_DATA *p_editor Line 966  int editor_display(EDITOR_DATA *p_editor
966                                          if (col_pos > 1)                                          if (col_pos > 1)
967                                          {                                          {
968                                                  col_pos--;                                                  col_pos--;
969                                                    if (col_pos > 1 &&
970                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
971                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK
972                                                    {
973                                                            col_pos--;
974                                                    }
975                                                  break;                                                  break;
976                                          }                                          }
977                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 918  int editor_display(EDITOR_DATA *p_editor Line 999  int editor_display(EDITOR_DATA *p_editor
999                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1000                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
1001                                          {                                          {
1002                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
1003                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK
1004                                                    {
1005                                                            col_pos++;
1006                                                    }
1007                                                  col_pos++;                                                  col_pos++;
1008                                                  break;                                                  break;
1009                                          }                                          }
# Line 929  int editor_display(EDITOR_DATA *p_editor Line 1015  int editor_display(EDITOR_DATA *p_editor
1015                                                  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_lengths[line_current - output_current_row + row_pos]));
1016                                                  break;                                                  break;
1017                                          }                                          }
1018                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
1019                                          {                                          {
1020                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
1021                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
# Line 941  int editor_display(EDITOR_DATA *p_editor Line 1027  int editor_display(EDITOR_DATA *p_editor
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_lengths[line_current - output_current_row + row_pos]));
1028                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1029                                          clrtoeol();                                          clrtoeol();
1030                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
1031                                            prints("\n"); // Legacy Cterm only works with this line
1032                                          break;                                          break;
1033                                  case KEY_PGUP:                                  case KEY_PGUP:
1034                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 996  int editor_display(EDITOR_DATA *p_editor Line 1083  int editor_display(EDITOR_DATA *p_editor
1083                                          break;                                          break;
1084                                  }                                  }
1085    
1086                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(NULL);
1087    
1088                                  if (input_ok)                                  if (input_ok)
1089                                  {                                  {
1090                                          break;                                          break;
1091                                  }                                  }
1092    
1093                                    ch = igetch_t(MAX_DELAY_TIME);
1094                          }                          }
1095    
1096                          continue;                          continue;


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

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