/[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.48 by sysadm, Sat Oct 18 12:06:10 2025 UTC Revision 1.56 by sysadm, Sat Nov 8 12:32:16 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                     editor.h  -  description  /*
3                                                           -------------------   * editor
4          copyright            : (C) 2004-2025 by Leaflet   *   - user interactive full-screen text editor
5          email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
# Line 24  Line 16 
16  #include "str_process.h"  #include "str_process.h"
17  #include <stdlib.h>  #include <stdlib.h>
18  #include <string.h>  #include <string.h>
19    #include <wchar.h>
20  #include <sys/param.h>  #include <sys/param.h>
21    
22  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  enum _editor_constant_t
23  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000  {
24  #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)          EDITOR_MEM_POOL_LINE_PER_CHUNK = 1000,
25            EDITOR_MEM_POOL_CHUNK_LIMIT = (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1),
26    };
27    
28    static const char EDITOR_ESC_DISPLAY_STR[] = "\033[32m*\033[m";
29    
30  static MEMORY_POOL *p_mp_data_line;  static MEMORY_POOL *p_mp_data_line;
31  static MEMORY_POOL *p_mp_editor_data;  static MEMORY_POOL *p_mp_editor_data;
# Line 445  int editor_data_insert(EDITOR_DATA *p_ed Line 442  int editor_data_insert(EDITOR_DATA *p_ed
442  }  }
443    
444  int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,  int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
445                                             long *p_last_updated_line)                                             long *p_last_updated_line, int del_line)
446  {  {
447          long display_line = *p_display_line;          long display_line = *p_display_line;
448          long offset = *p_offset;          long offset = *p_offset;
# Line 500  int editor_data_delete(EDITOR_DATA *p_ed Line 497  int editor_data_delete(EDITOR_DATA *p_ed
497          }          }
498    
499          // Check str to be deleted          // Check str to be deleted
500          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (del_line)
501            {
502                    str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
503            }
504            else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
505          {          {
506                  str_len = 1;                  str_len = 1;
507          }          }
# Line 523  int editor_data_delete(EDITOR_DATA *p_ed Line 524  int editor_data_delete(EDITOR_DATA *p_ed
524    
525          // Current display line is (almost) empty          // Current display line is (almost) empty
526          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
527                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line &&
528                     p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
529          {          {
530                  if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)                  if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
531                  {                  {
# Line 627  static int editor_display_key_handler(in Line 629  static int editor_display_key_handler(in
629          {          {
630          case 0: // Set msg          case 0: // Set msg
631                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
632                                   "| 退出[\033[32mCtrl-W\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]",
633                                     (UTF8_fixed_width ? "定宽" : "变宽"));
634                  break;                  break;
635          case KEY_CSI:          case KEY_CSI:
636                  *p_key = KEY_ESC;                  *p_key = KEY_ESC;
# Line 643  int editor_display(EDITOR_DATA *p_editor Line 646  int editor_display(EDITOR_DATA *p_editor
646          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
647          EDITOR_CTX ctx;          EDITOR_CTX ctx;
648          int ch = 0;          int ch = 0;
649          char input_str[4];          char input_str[5];
         char c;  
650          int str_len = 0;          int str_len = 0;
651            wchar_t wcs[2];
652            int wc_len;
653            char c;
654          int input_ok;          int input_ok;
655          const int screen_begin_row = 1;          const int screen_begin_row = 1;
656          const int screen_row_total = SCREEN_ROWS - screen_begin_row;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
# Line 663  int editor_display(EDITOR_DATA *p_editor Line 668  int editor_display(EDITOR_DATA *p_editor
668          int key_insert = 1;          int key_insert = 1;
669          int i, j;          int i, j;
670          char *p_str;          char *p_str;
671            int del_line;
672    
673          clrline(output_current_row, SCREEN_ROWS);          clrline(output_current_row, SCREEN_ROWS);
674    
# Line 702  int editor_display(EDITOR_DATA *p_editor Line 708  int editor_display(EDITOR_DATA *p_editor
708                          iflush();                          iflush();
709    
710                          str_len = 0;                          str_len = 0;
711                          ch = igetch_t(MAX_DELAY_TIME);                          ch = igetch_t(BBS_max_user_idle_time);
712                          while (!SYS_server_exit)                          while (!SYS_server_exit)
713                          {                          {
714                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
# Line 742  int editor_display(EDITOR_DATA *p_editor Line 748  int editor_display(EDITOR_DATA *p_editor
748                                                          break;                                                          break;
749                                                  }                                                  }
750                                          }                                          }
751                                            input_str[str_len] = '\0';
752                                  }                                  }
753    
754                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character
# Line 769  int editor_display(EDITOR_DATA *p_editor Line 776  int editor_display(EDITOR_DATA *p_editor
776                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
777                                          {                                          {
778                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
779                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line, 0) < 0)
780                                                  {                                                  {
781                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
782                                                  }                                                  }
# Line 819  int editor_display(EDITOR_DATA *p_editor Line 826  int editor_display(EDITOR_DATA *p_editor
826                                                          }                                                          }
827                                                          if (offset_out > 0)                                                          if (offset_out > 0)
828                                                          {                                                          {
829                                                                  col_pos += (str_len == 1 ? 1 : 2);                                                                  if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
830                                                                    {
831                                                                            log_error("mbstowcs() error\n");
832                                                                    }
833                                                                    col_pos += (str_len == 1 ? 1 : (UTF8_fixed_width ? 2 : wcwidth(wcs[0])));
834                                                          }                                                          }
835                                                  }                                                  }
836                                          }                                          }
# Line 838  int editor_display(EDITOR_DATA *p_editor Line 849  int editor_display(EDITOR_DATA *p_editor
849                                          str_len = 0;                                          str_len = 0;
850                                          continue;                                          continue;
851                                  }                                  }
852                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
853                                  {                                  {
854                                          // Refresh current action while user input                                          // Refresh current action while user input
855                                          if (user_online_update(NULL) < 0)                                          if (user_online_update(NULL) < 0)
# Line 846  int editor_display(EDITOR_DATA *p_editor Line 857  int editor_display(EDITOR_DATA *p_editor
857                                                  log_error("user_online_update(NULL) error\n");                                                  log_error("user_online_update(NULL) error\n");
858                                          }                                          }
859    
860                                            del_line = 0;
861    
862                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
863                                          {                                          {
864                                                  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
# Line 855  int editor_display(EDITOR_DATA *p_editor Line 868  int editor_display(EDITOR_DATA *p_editor
868    
869                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
870                                                                                             (int)col_pos - 1, &eol, &display_len, 0);                                                                                             (int)col_pos - 1, &eol, &display_len, 0);
871                                                  if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8                                                  col_pos = display_len;
                                                 {  
                                                         col_pos = display_len - 1;  
                                                 }  
                                                 else  
                                                 {  
                                                         col_pos = display_len;  
                                                 }  
   
872                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
873                                                  {                                                  {
874                                                          row_pos--;                                                          row_pos--;
875                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
876                                                  }                                                  }
877                                          }                                          }
878                                            else if (ch == Ctrl('K'))
879                                            {
880                                                    del_line = 1;
881                                            }
882                                            else if (ch == Ctrl('Y'))
883                                            {
884                                                    col_pos = 1;
885                                                    del_line = 1;
886                                            }
887    
888                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
889                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
# Line 877  int editor_display(EDITOR_DATA *p_editor Line 891  int editor_display(EDITOR_DATA *p_editor
891                                          offset_out = offset_in;                                          offset_out = offset_in;
892    
893                                          if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,                                          if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
894                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line, del_line)) < 0)
895                                          {                                          {
896                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error: %d\n", str_len);
897                                          }                                          }
898                                          else                                          else
899                                          {                                          {
# Line 1011  int editor_display(EDITOR_DATA *p_editor Line 1025  int editor_display(EDITOR_DATA *p_editor
1025                                  case KEY_LEFT:                                  case KEY_LEFT:
1026                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1027                                                                                     (int)col_pos - 1, &eol, &display_len, 0);                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1028                                          if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8                                          col_pos = display_len;
1029                                          {                                          if (offset_in > 0)
                                                 col_pos = display_len - 1;  
                                         }  
                                         else  
1030                                          {                                          {
1031                                                  col_pos = display_len;                                                  str_len = 1;
1032                                                    offset_in--;
1033                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0 ||
1034                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] > 127) // UTF8
1035                                                    {
1036                                                            while (offset_in > 0 &&
1037                                                                       (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xc0) != 0xc0)
1038                                                            {
1039                                                                    str_len++;
1040                                                                    offset_in--;
1041                                                            }
1042    
1043                                                            if (str_len > 4)
1044                                                            {
1045                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1046                                                            }
1047    
1048                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1049                                                                    (size_t)-1)
1050                                                            {
1051                                                                    log_error("mbstowcs() error\n");
1052                                                            }
1053                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1054    
1055                                                            if (wc_len == 2)
1056                                                            {
1057                                                                    col_pos--;
1058                                                            }
1059                                                    }
1060                                          }                                          }
1061                                          if (col_pos >= 1)                                          if (col_pos >= 1)
1062                                          {                                          {
# Line 1043  int editor_display(EDITOR_DATA *p_editor Line 1082  int editor_display(EDITOR_DATA *p_editor
1082                                          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
1083                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[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]));
1084                                          break;                                          break;
                                 case KEY_SPACE:  
                                         break;  
1085                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1086                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1087                                                                                     (int)col_pos - 1, &eol, &display_len, 0);                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1088                                          if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&                                          col_pos = display_len + 2;
1089                                                  p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8                                          if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
1090                                          {                                          {
1091                                                  col_pos = display_len + 3;                                                  str_len = 0;
1092                                          }                                                  if ((p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0x80) ==
1093                                          else                                                          0x80) // head of multi-byte character
1094                                          {                                                  {
1095                                                  col_pos = display_len + 2;                                                          c = (char)(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xf0);
1096                                                            while (c & 0x80)
1097                                                            {
1098                                                                    str_len++;
1099                                                                    c = (c & 0x7f) << 1;
1100                                                            }
1101    
1102                                                            if (str_len > 4)
1103                                                            {
1104                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1105                                                            }
1106    
1107                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1108                                                                    (size_t)-1)
1109                                                            {
1110                                                                    log_error("mbstowcs() error\n");
1111                                                            }
1112                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1113    
1114                                                            if (wc_len == 2)
1115                                                            {
1116                                                                    col_pos++;
1117                                                            }
1118                                                    }
1119                                                    else
1120                                                    {
1121                                                            str_len = 1;
1122                                                    }
1123                                                    offset_in += str_len;
1124                                          }                                          }
1125                                          if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])                                          if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1126                                          {                                          {
# Line 1116  int editor_display(EDITOR_DATA *p_editor Line 1181  int editor_display(EDITOR_DATA *p_editor
1181                                          break;                                          break;
1182                                  case Ctrl('Q'):                                  case Ctrl('Q'):
1183                                  case KEY_F1:                                  case KEY_F1:
1184                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not re-entrant
1185                                          {                                          {
1186                                                  break;                                                  break;
1187                                          }                                          }
1188                                          // Display help information                                          // Display help information
1189                                          show_help = 0;                                          show_help = 0;
1190                                          display_file(DATA_READ_HELP, 1);                                          display_file(DATA_EDITOR_HELP, 1);
1191                                          show_help = 1;                                          show_help = 1;
1192                                  case KEY_F5:                                  case KEY_F5:
1193                                          // Refresh after display help information                                          // Refresh after display help information
# Line 1149  int editor_display(EDITOR_DATA *p_editor Line 1214  int editor_display(EDITOR_DATA *p_editor
1214                                          break;                                          break;
1215                                  }                                  }
1216    
1217                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(BBS_max_user_idle_time);
1218                          }                          }
1219    
1220                          continue;                          continue;


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

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