/[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.49 by sysadm, Sun Oct 19 07:08:29 2025 UTC Revision 1.59 by sysadm, Mon Nov 10 11:54:30 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 80  EDITOR_DATA *editor_data_load(const char Line 77  EDITOR_DATA *editor_data_load(const char
77          long line_offsets[MAX_EDITOR_DATA_LINES + 1];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
78          long current_data_line_length = 0;          long current_data_line_length = 0;
79          long i;          long i;
80            int j;
81    
82          if (p_data == NULL)          if (p_data == NULL)
83          {          {
# Line 126  EDITOR_DATA *editor_data_load(const char Line 124  EDITOR_DATA *editor_data_load(const char
124                  memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]);                  memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]);
125                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
126    
127                    // Convert \t to single space
128                    for (j = 0; j < p_editor_data->display_line_lengths[i]; j++)
129                    {
130                            if (p_editor_data->p_display_lines[i][j] == '\t')
131                            {
132                                    p_editor_data->p_display_lines[i][j] = ' ';
133                            }
134                    }
135    
136                  // Trim \n from last line                  // Trim \n from last line
137                  if (i + 1 == p_editor_data->display_line_total &&                  if (i + 1 == p_editor_data->display_line_total &&
138                          p_editor_data->display_line_lengths[i] > 0 &&                          p_editor_data->display_line_lengths[i] > 0 &&
# Line 632  static int editor_display_key_handler(in Line 639  static int editor_display_key_handler(in
639          {          {
640          case 0: // Set msg          case 0: // Set msg
641                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
642                                   "| 退出[\033[32mCtrl-W\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]",
643                                     (UTF8_fixed_width ? "定宽" : "变宽"));
644                  break;                  break;
645          case KEY_CSI:          case KEY_CSI:
646                  *p_key = KEY_ESC;                  *p_key = KEY_ESC;
# Line 648  int editor_display(EDITOR_DATA *p_editor Line 656  int editor_display(EDITOR_DATA *p_editor
656          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
657          EDITOR_CTX ctx;          EDITOR_CTX ctx;
658          int ch = 0;          int ch = 0;
659          char input_str[4];          char input_str[5];
         char c;  
660          int str_len = 0;          int str_len = 0;
661            wchar_t wcs[2];
662            int wc_len;
663            char c;
664          int input_ok;          int input_ok;
665          const int screen_begin_row = 1;          const int screen_begin_row = 1;
666          const int screen_row_total = SCREEN_ROWS - screen_begin_row;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
# Line 669  int editor_display(EDITOR_DATA *p_editor Line 679  int editor_display(EDITOR_DATA *p_editor
679          int i, j;          int i, j;
680          char *p_str;          char *p_str;
681          int del_line;          int del_line;
682            int tab_width = 0;
683    
684          clrline(output_current_row, SCREEN_ROWS);          clrline(output_current_row, SCREEN_ROWS);
685    
# Line 707  int editor_display(EDITOR_DATA *p_editor Line 718  int editor_display(EDITOR_DATA *p_editor
718                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
719                          iflush();                          iflush();
720    
721                            tab_width = 0;
722                          str_len = 0;                          str_len = 0;
723                          ch = igetch_t(MAX_DELAY_TIME);                          ch = igetch_t(BBS_max_user_idle_time);
724                          while (!SYS_server_exit)                          while (!SYS_server_exit)
725                          {                          {
726                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
# Line 722  int editor_display(EDITOR_DATA *p_editor Line 734  int editor_display(EDITOR_DATA *p_editor
734                                          goto cleanup;                                          goto cleanup;
735                                  }                                  }
736    
737                                    if (ch == '\t')
738                                    {
739                                            ch = ' ';
740                                            tab_width = TAB_SIZE - ((int)(col_pos - 1) % TAB_SIZE) - 1;
741                                    }
742    
743                                  if (ch < 256 && (ch & 0x80)) // head of multi-byte character                                  if (ch < 256 && (ch & 0x80)) // head of multi-byte character
744                                  {                                  {
745                                          str_len = 0;                                          str_len = 0;
# Line 748  int editor_display(EDITOR_DATA *p_editor Line 766  int editor_display(EDITOR_DATA *p_editor
766                                                          break;                                                          break;
767                                                  }                                                  }
768                                          }                                          }
769                                            input_str[str_len] = '\0';
770                                  }                                  }
771    
772                                  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 825  int editor_display(EDITOR_DATA *p_editor Line 844  int editor_display(EDITOR_DATA *p_editor
844                                                          }                                                          }
845                                                          if (offset_out > 0)                                                          if (offset_out > 0)
846                                                          {                                                          {
847                                                                  col_pos += (str_len == 1 ? 1 : 2);                                                                  if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
848                                                                    {
849                                                                            log_error("mbstowcs() error\n");
850                                                                    }
851                                                                    col_pos += (str_len == 1 ? 1 : (UTF8_fixed_width ? 2 : wcwidth(wcs[0])));
852                                                          }                                                          }
853                                                  }                                                  }
854                                          }                                          }
# Line 835  int editor_display(EDITOR_DATA *p_editor Line 858  int editor_display(EDITOR_DATA *p_editor
858                                                  break;                                                  break;
859                                          }                                          }
860    
861                                          ch = igetch(0);                                          if (ch == ' ' && tab_width > 0)
                                         if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input  
862                                          {                                          {
863                                                  break;                                                  tab_width--;
864                                            }
865                                            else
866                                            {
867                                                    ch = igetch(0);
868                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
869                                                    {
870                                                            break;
871                                                    }
872                                          }                                          }
873    
874                                          str_len = 0;                                          str_len = 0;
# Line 863  int editor_display(EDITOR_DATA *p_editor Line 893  int editor_display(EDITOR_DATA *p_editor
893    
894                                                  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],
895                                                                                             (int)col_pos - 1, &eol, &display_len, 0);                                                                                             (int)col_pos - 1, &eol, &display_len, 0);
896                                                  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;  
                                                 }  
   
897                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
898                                                  {                                                  {
899                                                          row_pos--;                                                          row_pos--;
# Line 1028  int editor_display(EDITOR_DATA *p_editor Line 1050  int editor_display(EDITOR_DATA *p_editor
1050                                  case KEY_LEFT:                                  case KEY_LEFT:
1051                                          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],
1052                                                                                     (int)col_pos - 1, &eol, &display_len, 0);                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1053                                          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;
1054                                          {                                          if (offset_in > 0)
                                                 col_pos = display_len - 1;  
                                         }  
                                         else  
1055                                          {                                          {
1056                                                  col_pos = display_len;                                                  str_len = 1;
1057                                                    offset_in--;
1058                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0 ||
1059                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] > 127) // UTF8
1060                                                    {
1061                                                            while (offset_in > 0 &&
1062                                                                       (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xc0) != 0xc0)
1063                                                            {
1064                                                                    str_len++;
1065                                                                    offset_in--;
1066                                                            }
1067    
1068                                                            if (str_len > 4)
1069                                                            {
1070                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1071                                                            }
1072    
1073                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1074                                                                    (size_t)-1)
1075                                                            {
1076                                                                    log_error("mbstowcs() error\n");
1077                                                            }
1078                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1079    
1080                                                            if (wc_len == 2)
1081                                                            {
1082                                                                    col_pos--;
1083                                                            }
1084                                                    }
1085                                          }                                          }
1086                                          if (col_pos >= 1)                                          if (col_pos >= 1)
1087                                          {                                          {
# Line 1060  int editor_display(EDITOR_DATA *p_editor Line 1107  int editor_display(EDITOR_DATA *p_editor
1107                                          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
1108                                          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]));
1109                                          break;                                          break;
                                 case KEY_SPACE:  
                                         break;  
1110                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1111                                          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],
1112                                                                                     (int)col_pos - 1, &eol, &display_len, 0);                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1113                                          if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&                                          col_pos = display_len + 2;
1114                                                  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])
                                         {  
                                                 col_pos = display_len + 3;  
                                         }  
                                         else  
1115                                          {                                          {
1116                                                  col_pos = display_len + 2;                                                  str_len = 0;
1117                                                    if ((p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0x80) ==
1118                                                            0x80) // head of multi-byte character
1119                                                    {
1120                                                            c = (char)(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xf0);
1121                                                            while (c & 0x80)
1122                                                            {
1123                                                                    str_len++;
1124                                                                    c = (c & 0x7f) << 1;
1125                                                            }
1126    
1127                                                            if (str_len > 4)
1128                                                            {
1129                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1130                                                            }
1131    
1132                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1133                                                                    (size_t)-1)
1134                                                            {
1135                                                                    log_error("mbstowcs() error\n");
1136                                                            }
1137                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1138    
1139                                                            if (wc_len == 2)
1140                                                            {
1141                                                                    col_pos++;
1142                                                            }
1143                                                    }
1144                                                    else
1145                                                    {
1146                                                            str_len = 1;
1147                                                    }
1148                                                    offset_in += str_len;
1149                                          }                                          }
1150                                          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])
1151                                          {                                          {
# Line 1133  int editor_display(EDITOR_DATA *p_editor Line 1206  int editor_display(EDITOR_DATA *p_editor
1206                                          break;                                          break;
1207                                  case Ctrl('Q'):                                  case Ctrl('Q'):
1208                                  case KEY_F1:                                  case KEY_F1:
1209                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not re-entrant
1210                                          {                                          {
1211                                                  break;                                                  break;
1212                                          }                                          }
1213                                          // Display help information                                          // Display help information
1214                                          show_help = 0;                                          show_help = 0;
1215                                          display_file(DATA_READ_HELP, 1);                                          display_file(DATA_EDITOR_HELP, 1);
1216                                          show_help = 1;                                          show_help = 1;
1217                                  case KEY_F5:                                  case KEY_F5:
1218                                          // Refresh after display help information                                          // Refresh after display help information
# Line 1166  int editor_display(EDITOR_DATA *p_editor Line 1239  int editor_display(EDITOR_DATA *p_editor
1239                                          break;                                          break;
1240                                  }                                  }
1241    
1242                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(BBS_max_user_idle_time);
1243                          }                          }
1244    
1245                          continue;                          continue;


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

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