/[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.19 by sysadm, Fri Jun 13 13:39:46 2025 UTC Revision 1.58 by sysadm, Mon Nov 10 07:23: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    
 #include "editor.h"  
9  #include "bbs.h"  #include "bbs.h"
10    #include "common.h"
11    #include "editor.h"
12  #include "io.h"  #include "io.h"
13  #include "log.h"  #include "log.h"
14  #include "common.h"  #include "login.h"
 #include "str_process.h"  
15  #include "memory_pool.h"  #include "memory_pool.h"
16    #include "str_process.h"
17  #include <stdlib.h>  #include <stdlib.h>
18    #include <string.h>
19    #include <wchar.h>
20  #include <sys/param.h>  #include <sys/param.h>
 #include <strings.h>  
21    
22  #define _POSIX_C_SOURCE 200809L  enum _editor_constant_t
23  #include <string.h>  {
24            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            TAB_SIZE = 4,
27    };
28    
29  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  static const char EDITOR_ESC_DISPLAY_STR[] = "\033[32m*\033[m";
 #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000  
 #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)  
30    
31  static MEMORY_POOL *p_mp_data_line;  static MEMORY_POOL *p_mp_data_line;
32  static MEMORY_POOL *p_mp_editor_data;  static MEMORY_POOL *p_mp_editor_data;
# Line 51  int editor_memory_pool_init(void) Line 47  int editor_memory_pool_init(void)
47          }          }
48    
49          p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);          p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
50          if (p_mp_data_line == NULL)          if (p_mp_editor_data == NULL)
51          {          {
52                  log_error("Memory pool init error\n");                  log_error("Memory pool init error\n");
53                  return -3;                  return -3;
# Line 79  EDITOR_DATA *editor_data_load(const char Line 75  EDITOR_DATA *editor_data_load(const char
75  {  {
76          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
77          char *p_data_line = NULL;          char *p_data_line = NULL;
78          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
79          long current_data_line_length = 0;          long current_data_line_length = 0;
80          long i;          long i;
81            int j;
82    
83          if (p_data == NULL)          if (p_data == NULL)
84          {          {
85                  log_error("editor_data_load() error: NULL pointer\n");                  log_error("NULL pointer error\n");
86                  return NULL;                  return NULL;
87          }          }
88    
# 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,
97                                                                                                                     0, p_editor_data->display_line_widths);
98    
99          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
100          {          {
# Line 127  EDITOR_DATA *editor_data_load(const char Line 125  EDITOR_DATA *editor_data_load(const char
125                  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]);
126                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
127    
128                    // Convert \t to single space
129                    for (j = 0; j < p_editor_data->display_line_lengths[i]; j++)
130                    {
131                            if (p_editor_data->p_display_lines[i][j] == '\t')
132                            {
133                                    p_editor_data->p_display_lines[i][j] = ' ';
134                            }
135                    }
136    
137                  // Trim \n from last line                  // Trim \n from last line
138                  if (i + 1 == p_editor_data->display_line_total &&                  if (i + 1 == p_editor_data->display_line_total &&
139                          p_editor_data->display_line_lengths[i] > 0 &&                          p_editor_data->display_line_lengths[i] > 0 &&
140                          p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')                          p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
141                  {                  {
142                          p_editor_data->display_line_lengths[i]--;                          p_editor_data->display_line_lengths[i]--;
143                            p_editor_data->display_line_widths[i]--;
144                          current_data_line_length--;                          current_data_line_length--;
145                  }                  }
146                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
147          }          }
148    
149          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);
150    
151          return p_editor_data;          return p_editor_data;
152  }  }
# Line 150  long editor_data_save(const EDITOR_DATA Line 158  long editor_data_save(const EDITOR_DATA
158    
159          if (p_editor_data == NULL || p_data == NULL)          if (p_editor_data == NULL || p_data == NULL)
160          {          {
161                  log_error("editor_data_save() error: NULL pointer\n");                  log_error("NULL pointer error\n");
162                  return -1;                  return -1;
163          }          }
164    
# Line 215  int editor_data_insert(EDITOR_DATA *p_ed Line 223  int editor_data_insert(EDITOR_DATA *p_ed
223          long offset_data_line;          long offset_data_line;
224          long last_display_line; // of data line          long last_display_line; // of data line
225          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
226            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
227          long split_line_total;          long split_line_total;
228          long i, j;          long i;
229          int len;          int len;
230          int eol;          int eol;
231          int display_len;          int display_len;
232    
233          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
234          {          {
235                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
236                  return -1;                  return -1;
237          }          }
238    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
239          // Get length of current data line          // Get length of current data line
240          len_data_line = 0;          len_data_line = 0;
241          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 269  int editor_data_insert(EDITOR_DATA *p_ed Line 265  int editor_data_insert(EDITOR_DATA *p_ed
265          }          }
266    
267          // Split current data line if over-length          // Split current data line if over-length
268          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)
269          {          {
270                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
271                  {                  {
272                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",  #ifdef _DEBUG
273                          //                p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                          log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
274                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
275    #endif
276    
277                          return -2;                          return -2;
278                  }                  }
279    
# Line 286  int editor_data_insert(EDITOR_DATA *p_ed Line 285  int editor_data_insert(EDITOR_DATA *p_ed
285                          return -2;                          return -2;
286                  }                  }
287    
288                  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)
289                  {                  {
290                          if (str[0] == CR)                          if (str[0] == CR)
291                          {                          {
# Line 332  int editor_data_insert(EDITOR_DATA *p_ed Line 331  int editor_data_insert(EDITOR_DATA *p_ed
331                          *p_offset = offset + str_len;                          *p_offset = offset + str_len;
332                  }                  }
333    
334                    // Update display width of current display line
335                    len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0);
336                    p_editor_data->display_line_widths[display_line] = display_len;
337    
338                  split_line_total = last_display_line - display_line + 3;                  split_line_total = last_display_line - display_line + 3;
339    
340                  // Set start display_line for spliting new data line                  // Set start display_line for spliting new data line
# Line 354  int editor_data_insert(EDITOR_DATA *p_ed Line 357  int editor_data_insert(EDITOR_DATA *p_ed
357          }          }
358    
359          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
360          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, line_widths);
361    
362          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
363          {          {
# Line 363  int editor_data_insert(EDITOR_DATA *p_ed Line 366  int editor_data_insert(EDITOR_DATA *p_ed
366                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
367                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
368                          {                          {
369                                  // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);  #ifdef _DEBUG
370                                    log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
371    #endif
372    
373                                  // Terminate prior display line with \n, to avoid error on cleanup                                  // Terminate prior display line with \n, to avoid error on cleanup
374                                  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)
375                                  {                                  {
376                                          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);
377                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
378                                          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';
379                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
380                                            p_editor_data->display_line_widths[display_line + i - 1] = display_len;
381                                  }                                  }
382                                  if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])                                  if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
383                                  {                                  {
# Line 378  int editor_data_insert(EDITOR_DATA *p_ed Line 385  int editor_data_insert(EDITOR_DATA *p_ed
385                                  }                                  }
386                                  break;                                  break;
387                          }                          }
388                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)  
389                          {                          // for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
390                                  p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];                          // {
391                                  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];
392                          }                          //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
393                            //      p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1];
394                            // }
395                            memmove(p_editor_data->p_display_lines + last_display_line + 2,
396                                            p_editor_data->p_display_lines + last_display_line + 1,
397                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
398                                                    sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
399                            memmove(p_editor_data->display_line_lengths + last_display_line + 2,
400                                            p_editor_data->display_line_lengths + last_display_line + 1,
401                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
402                                                    sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
403                            memmove(p_editor_data->display_line_widths + last_display_line + 2,
404                                            p_editor_data->display_line_widths + last_display_line + 1,
405                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
406                                                    sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
407    
408                          last_display_line++;                          last_display_line++;
409                            *p_last_updated_line = p_editor_data->display_line_total;
410                          (p_editor_data->display_line_total)++;                          (p_editor_data->display_line_total)++;
411                  }                  }
412    
413                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
414                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
415                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
416                          (i == 0                          (i == 0
417                                   ? p_data_line                                   ? p_data_line
# Line 411  int editor_data_insert(EDITOR_DATA *p_ed Line 435  int editor_data_insert(EDITOR_DATA *p_ed
435                  }                  }
436          }          }
437    
438            // Prevent the last display line from being over-length
439            if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
440            {
441                    len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
442                    p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
443                    p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
444                    p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len;
445                    if (*p_display_line + 1 >= p_editor_data->display_line_total)
446                    {
447                            *p_offset = MIN(*p_offset, len);
448                            *p_display_line = p_editor_data->display_line_total - 1;
449                    }
450            }
451    
452          return 0;          return 0;
453  }  }
454    
455  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,
456                                             long *p_last_updated_line)                                             long *p_last_updated_line, int del_line)
457  {  {
458            long display_line = *p_display_line;
459            long offset = *p_offset;
460          char *p_data_line = NULL;          char *p_data_line = NULL;
461          long len_data_line;          long len_data_line;
462          long offset_data_line;          long offset_data_line;
463          long last_display_line; // of data line          long last_display_line; // of data line
464          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
465            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
466          long split_line_total;          long split_line_total;
467          long i, j;          long i, j;
468          int str_len = 0;          int str_len = 0;
469            char c;
470    
471          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
472          {          {
473                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
474                  return -1;                  return -1;
475          }          }
476    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
477          // Get length of current data line          // Get length of current data line
478          len_data_line = 0;          len_data_line = 0;
479          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 479  int editor_data_delete(EDITOR_DATA *p_ed Line 508  int editor_data_delete(EDITOR_DATA *p_ed
508          }          }
509    
510          // Check str to be deleted          // Check str to be deleted
511          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (del_line)
512            {
513                    str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
514            }
515            else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
516          {          {
517                  str_len = 1;                  str_len = 1;
518          }          }
519          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] & 0x80) // head of multi-byte character
520          {          {
521                  str_len = 2;                  str_len = 1;
522                    c = (p_data_line[offset_data_line] & 0x70) << 1;
523                    while (c & 0x80)
524                    {
525                            str_len++;
526                            c = (c & 0x7f) << 1;
527                    }
528          }          }
529          else          else
530          {          {
# Line 496  int editor_data_delete(EDITOR_DATA *p_ed Line 535  int editor_data_delete(EDITOR_DATA *p_ed
535    
536          // Current display line is (almost) empty          // Current display line is (almost) empty
537          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
538                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line &&
539                     p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
540          {          {
541                  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)
542                  {                  {
# Line 541  int editor_data_delete(EDITOR_DATA *p_ed Line 581  int editor_data_delete(EDITOR_DATA *p_ed
581          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
582    
583          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
584          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, line_widths);
585    
586          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
587          {          {
588                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
589                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
590                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
591                          (i == 0                          (i == 0
592                                   ? p_data_line                                   ? p_data_line
# Line 563  int editor_data_delete(EDITOR_DATA *p_ed Line 604  int editor_data_delete(EDITOR_DATA *p_ed
604          if (*p_last_updated_line < last_display_line)          if (*p_last_updated_line < last_display_line)
605          {          {
606                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
607                  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++)
608                  {                  // {
609                          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];
610                          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];
611                  }                  //      p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j];
612                    // }
613                    memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
614                                    p_editor_data->p_display_lines + last_display_line + 1,
615                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
616                                            sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
617                    memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
618                                    p_editor_data->display_line_lengths + last_display_line + 1,
619                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
620                                            sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
621                    memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1,
622                                    p_editor_data->display_line_widths + last_display_line + 1,
623                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
624                                            sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
625    
626                  j = p_editor_data->display_line_total;                  j = p_editor_data->display_line_total;
627                  (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);
628                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
629          }          }
630    
631            // Return real offset
632            *p_offset = offset;
633    
634          return str_len;          return str_len;
635  }  }
636    
# Line 583  static int editor_display_key_handler(in Line 640  static int editor_display_key_handler(in
640          {          {
641          case 0: // Set msg          case 0: // Set msg
642                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
643                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]",
644                                     (UTF8_fixed_width ? "定宽" : "å˜å®½"));
645                    break;
646            case KEY_CSI:
647                    *p_key = KEY_ESC;
648                  break;                  break;
649          }          }
650    
# Line 596  int editor_display(EDITOR_DATA *p_editor Line 657  int editor_display(EDITOR_DATA *p_editor
657          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
658          EDITOR_CTX ctx;          EDITOR_CTX ctx;
659          int ch = 0;          int ch = 0;
660          char input_str[4];          char input_str[5];
661          int str_len = 0;          int str_len = 0;
662            wchar_t wcs[2];
663            int wc_len;
664            char c;
665          int input_ok;          int input_ok;
666          const int screen_begin_row = 1;          const int screen_begin_row = 1;
667          const int screen_row_total = SCREEN_ROWS - screen_begin_row;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
# Line 615  int editor_display(EDITOR_DATA *p_editor Line 679  int editor_display(EDITOR_DATA *p_editor
679          int key_insert = 1;          int key_insert = 1;
680          int i, j;          int i, j;
681          char *p_str;          char *p_str;
682            int del_line;
683            int tab_width = 0;
684    
685          clrline(output_current_row, SCREEN_ROWS);          clrline(output_current_row, SCREEN_ROWS);
686    
# Line 624  int editor_display(EDITOR_DATA *p_editor Line 690  int editor_display(EDITOR_DATA *p_editor
690                  return ch;                  return ch;
691          }          }
692    
693          loop = 1;          for (loop = 1; !SYS_server_exit && loop;)
         while (!SYS_server_exit && loop)  
694          {          {
695                  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)
696                  {                  {
# Line 633  int editor_display(EDITOR_DATA *p_editor Line 698  int editor_display(EDITOR_DATA *p_editor
698    
699                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
700                                           "\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] "                                           "\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] "
701                                           "µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ [\033[32m%s\033[33m] "                                           "第\033[32m%ld\033[33m/\033[32m%ld\033[33m行 [\033[32m%s\033[33m] "
702                                           "%s",                                           "%s",
703                                           row_pos, col_pos,                                           row_pos, col_pos,
704                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
705                                           key_insert ? "²åÈë" : "¸Äд",                                           key_insert ? "æ’å…¥" : "替æ¢",
706                                           ctx.msg);                                           ctx.msg);
707    
708                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
709                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
710                          {                          {
711                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 654  int editor_display(EDITOR_DATA *p_editor Line 719  int editor_display(EDITOR_DATA *p_editor
719                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
720                          iflush();                          iflush();
721    
722                          input_ok = 0;                          tab_width = 0;
723                          while (!SYS_server_exit && !input_ok)                          str_len = 0;
724                            ch = igetch_t(BBS_max_user_idle_time);
725                            while (!SYS_server_exit)
726                          {                          {
727                                  ch = igetch_t(MAX_DELAY_TIME);                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
728                                  input_ok = 1;                                  {
729                                            BBS_last_access_tm = time(NULL);
730                                    }
731    
732                                  // extended key handler                                  // extended key handler
733                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 666  int editor_display(EDITOR_DATA *p_editor Line 735  int editor_display(EDITOR_DATA *p_editor
735                                          goto cleanup;                                          goto cleanup;
736                                  }                                  }
737    
738                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch == '\t')
739                                  {                                  {
740                                          input_str[str_len] = (char)(ch - 256);                                          ch = ' ';
741                                          str_len++;                                          tab_width = TAB_SIZE - ((int)(col_pos - 1) % TAB_SIZE) - 1;
742                                  }                                  }
743                                  else  
744                                    if (ch < 256 && (ch & 0x80)) // head of multi-byte character
745                                  {                                  {
746                                          str_len = 0;                                          str_len = 0;
747                                            c = (char)(ch & 0xf0);
748                                            while (c & 0x80)
749                                            {
750                                                    input_str[str_len] = (char)(ch - 256);
751                                                    str_len++;
752                                                    c = (c & 0x7f) << 1;
753    
754                                                    if ((c & 0x80) == 0) // Input completed
755                                                    {
756                                                            break;
757                                                    }
758    
759                                                    // Expect additional bytes of input
760                                                    ch = igetch(100);                                                // 0.1 second
761                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
762                                                    {
763    #ifdef _DEBUG
764                                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
765    #endif
766                                                            str_len = 0;
767                                                            break;
768                                                    }
769                                            }
770                                            input_str[str_len] = '\0';
771                                  }                                  }
772    
773                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character
774                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                // Special character
775                                  {                                  {
776                                          if (str_len == 0)                                          // Refresh current action while user input
777                                            if (user_online_update(NULL) < 0)
778                                            {
779                                                    log_error("user_online_update(NULL) error\n");
780                                            }
781    
782                                            if (str_len == 0) // ch >= 32 && ch < 127
783                                          {                                          {
784                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
785                                                  str_len = 1;                                                  str_len = 1;
786                                          }                                          }
787    
                                         last_updated_line = line_current;  
788                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
789                                          offset_in = col_pos - 1;                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
790                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
791                                          offset_out = offset_in;                                          offset_out = offset_in;
792    
793                                            last_updated_line = display_line_in;
794    
795                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
796                                          {                                          {
797                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
798                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line, 0) < 0)
799                                                  {                                                  {
800                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
801                                                  }                                                  }
# Line 704  int editor_display(EDITOR_DATA *p_editor Line 805  int editor_display(EDITOR_DATA *p_editor
805                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
806                                          {                                          {
807                                                  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;  
808                                          }                                          }
809                                          else                                          else
810                                          {                                          {
                                                 str_len = 0;  
   
811                                                  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));
812                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
813                                                  output_current_row = (int)row_pos;                                                  output_current_row = (int)row_pos;
# Line 722  int editor_display(EDITOR_DATA *p_editor Line 820  int editor_display(EDITOR_DATA *p_editor
820                                                          clrtoeol();                                                          clrtoeol();
821                                                          for (i = 0; i < scroll_rows; i++)                                                          for (i = 0; i < scroll_rows; i++)
822                                                          {                                                          {
823                                                                  prints("\033[S"); // Scroll up 1 line                                                                  // prints("\033[S"); // Scroll up 1 line
824                                                                    prints("\n"); // Legacy Cterm only works with this line
825                                                          }                                                          }
826    
827                                                          output_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
# Line 737  int editor_display(EDITOR_DATA *p_editor Line 836  int editor_display(EDITOR_DATA *p_editor
836                                                  {                                                  {
837                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
838                                                  }                                                  }
839                                                  col_pos = offset_out + 1;  
840                                                    if (offset_out != offset_in)
841                                                    {
842                                                            if (display_line_out != display_line_in)
843                                                            {
844                                                                    col_pos = 1;
845                                                            }
846                                                            if (offset_out > 0)
847                                                            {
848                                                                    if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
849                                                                    {
850                                                                            log_error("mbstowcs() error\n");
851                                                                    }
852                                                                    col_pos += (str_len == 1 ? 1 : (UTF8_fixed_width ? 2 : wcwidth(wcs[0])));
853                                                            }
854                                                    }
855                                            }
856    
857                                            if (display_line_out != display_line_in) // Output on line change
858                                            {
859                                                    break;
860                                            }
861    
862                                            if (ch == ' ' && tab_width > 0)
863                                            {
864                                                    tab_width--;
865                                            }
866                                            else
867                                            {
868                                                    ch = igetch(0);
869                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
870                                                    {
871                                                            break;
872                                                    }
873                                          }                                          }
874    
875                                            str_len = 0;
876                                          continue;                                          continue;
877                                  }                                  }
878                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
879                                  {                                  {
880                                            // Refresh current action while user input
881                                            if (user_online_update(NULL) < 0)
882                                            {
883                                                    log_error("user_online_update(NULL) error\n");
884                                            }
885    
886                                            del_line = 0;
887    
888                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
889                                          {                                          {
890                                                  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
891                                                  {                                                  {
892                                                          input_ok = 0;                                                          break; // force output prior operation result if any
                                                         continue;  
893                                                  }                                                  }
894    
895                                                  col_pos--;                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
896                                                                                               (int)col_pos - 1, &eol, &display_len, 0);
897                                                    col_pos = display_len;
898                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
899                                                  {                                                  {
900                                                          row_pos--;                                                          row_pos--;
901                                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
902                                                  }                                                  }
903                                          }                                          }
904                                            else if (ch == Ctrl('K'))
905                                            {
906                                                    del_line = 1;
907                                            }
908                                            else if (ch == Ctrl('Y'))
909                                            {
910                                                    col_pos = 1;
911                                                    del_line = 1;
912                                            }
913    
914                                          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;
915                                                                                                            &last_updated_line)) < 0)                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
916                                            display_line_out = display_line_in;
917                                            offset_out = offset_in;
918    
919                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
920                                                                                                              &last_updated_line, del_line)) < 0)
921                                          {                                          {
922                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error: %d\n", str_len);
923                                          }                                          }
924                                          else                                          else
925                                          {                                          {
926                                                  if (ch == BACKSPACE)                                                  col_pos = display_len + 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]);  
                                                                 }  
                                                         }  
                                                 }  
927    
928                                                  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));
929                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
# Line 807  int editor_display(EDITOR_DATA *p_editor Line 952  int editor_display(EDITOR_DATA *p_editor
952                                                  clrline(output_current_row, output_end_row);                                                  clrline(output_current_row, output_end_row);
953                                          }                                          }
954    
955                                            if (display_line_out != display_line_in) // Output on line change
956                                            {
957                                                    break;
958                                            }
959    
960                                            ch = igetch(0);
961                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
962                                            {
963                                                    break;
964                                            }
965    
966                                            str_len = 0;
967                                          continue;                                          continue;
968                                  }                                  }
969    
970                                    input_ok = 1;
971                                  switch (ch)                                  switch (ch)
972                                  {                                  {
973                                  case KEY_NULL:                                  case KEY_NULL:
974                                            log_error("KEY_NULL\n");
975                                            goto cleanup;
976                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
977                                            log_error("User input timeout\n");
978                                          goto cleanup;                                          goto cleanup;
979                                  case Ctrl('W'):                                  case Ctrl('W'):
980                                    case Ctrl('X'):
981                                          loop = 0;                                          loop = 0;
982                                          break;                                          break;
983                                  case Ctrl('S'): // Start of line                                  case Ctrl('S'): // Start of line
# Line 824  int editor_display(EDITOR_DATA *p_editor Line 986  int editor_display(EDITOR_DATA *p_editor
986                                          break;                                          break;
987                                  case Ctrl('E'): // End of line                                  case Ctrl('E'): // End of line
988                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
989                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
990                                            {
991                                                    // last display line does NOT have \n in the end
992                                                    col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
993                                                    break;
994                                            }
995                                            col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
996                                          break;                                          break;
997                                  case Ctrl('T'): // Top of screen                                  case Ctrl('T'): // Top of screen
998                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
999                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
1000                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1001                                          break;                                          break;
1002                                  case Ctrl('B'): // Bottom of screen                                  case Ctrl('B'): // Bottom of screen
1003                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
# Line 844  int editor_display(EDITOR_DATA *p_editor Line 1012  int editor_display(EDITOR_DATA *p_editor
1012                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
1013                                          {                                          {
1014                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
1015                                                  col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);                                                  col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1);
1016                                          }                                          }
1017                                          else                                          else
1018                                          {                                          {
1019                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1020                                          }                                          }
1021                                          break;                                          break;
1022                                  case KEY_INS:                                  case KEY_INS:
# Line 870  int editor_display(EDITOR_DATA *p_editor Line 1038  int editor_display(EDITOR_DATA *p_editor
1038                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
1039                                          {                                          {
1040                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
1041                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1042                                                  break;                                                  break;
1043                                          }                                          }
1044                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
1045                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1046                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1047                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
1048                                          col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                          col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1049                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1050                                          break;                                          break;
1051                                  case KEY_LEFT:                                  case KEY_LEFT:
1052                                          if (col_pos > 1)                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1053                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1054                                            col_pos = display_len;
1055                                            if (offset_in > 0)
1056                                            {
1057                                                    str_len = 1;
1058                                                    offset_in--;
1059                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0 ||
1060                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] > 127) // UTF8
1061                                                    {
1062                                                            while (offset_in > 0 &&
1063                                                                       (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xc0) != 0xc0)
1064                                                            {
1065                                                                    str_len++;
1066                                                                    offset_in--;
1067                                                            }
1068    
1069                                                            if (str_len > 4)
1070                                                            {
1071                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1072                                                            }
1073    
1074                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1075                                                                    (size_t)-1)
1076                                                            {
1077                                                                    log_error("mbstowcs() error\n");
1078                                                            }
1079                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1080    
1081                                                            if (wc_len == 2)
1082                                                            {
1083                                                                    col_pos--;
1084                                                            }
1085                                                    }
1086                                            }
1087                                            if (col_pos >= 1)
1088                                          {                                          {
                                                 col_pos--;  
1089                                                  break;                                                  break;
1090                                          }                                          }
1091                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 891  int editor_display(EDITOR_DATA *p_editor Line 1093  int editor_display(EDITOR_DATA *p_editor
1093                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
1094                                          {                                          {
1095                                                  row_pos--;                                                  row_pos--;
1096                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1097                                                  break;                                                  break;
1098                                          }                                          }
1099                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 904  int editor_display(EDITOR_DATA *p_editor Line 1106  int editor_display(EDITOR_DATA *p_editor
1106                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
1107                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
1108                                          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
1109                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
                                         break;  
                                 case KEY_SPACE:  
1110                                          break;                                          break;
1111                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1112                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1113                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1114                                            col_pos = display_len + 2;
1115                                            if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
1116                                            {
1117                                                    str_len = 0;
1118                                                    if ((p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0x80) ==
1119                                                            0x80) // head of multi-byte character
1120                                                    {
1121                                                            c = (char)(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xf0);
1122                                                            while (c & 0x80)
1123                                                            {
1124                                                                    str_len++;
1125                                                                    c = (c & 0x7f) << 1;
1126                                                            }
1127    
1128                                                            if (str_len > 4)
1129                                                            {
1130                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1131                                                            }
1132    
1133                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1134                                                                    (size_t)-1)
1135                                                            {
1136                                                                    log_error("mbstowcs() error\n");
1137                                                            }
1138                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1139    
1140                                                            if (wc_len == 2)
1141                                                            {
1142                                                                    col_pos++;
1143                                                            }
1144                                                    }
1145                                                    else
1146                                                    {
1147                                                            str_len = 1;
1148                                                    }
1149                                                    offset_in += str_len;
1150                                            }
1151                                            if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1152                                          {                                          {
                                                 col_pos++;  
1153                                                  break;                                                  break;
1154                                          }                                          }
1155                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
# Line 919  int editor_display(EDITOR_DATA *p_editor Line 1157  int editor_display(EDITOR_DATA *p_editor
1157                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))
1158                                          {                                          {
1159                                                  row_pos++;                                                  row_pos++;
1160                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1161                                                  break;                                                  break;
1162                                          }                                          }
1163                                          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
1164                                          {                                          {
1165                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
1166                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1167                                                  break;                                                  break;
1168                                          }                                          }
1169                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
1170                                          output_current_row = screen_row_total;                                          output_current_row = screen_row_total;
1171                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1172                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1173                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1174                                          clrtoeol();                                          clrtoeol();
1175                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
1176                                            prints("\n"); // Legacy Cterm only works with this line
1177                                          break;                                          break;
1178                                  case KEY_PGUP:                                  case KEY_PGUP:
1179                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 948  int editor_display(EDITOR_DATA *p_editor Line 1187  int editor_display(EDITOR_DATA *p_editor
1187                                          }                                          }
1188                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1189                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1190                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1191                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1192                                          break;                                          break;
1193                                  case KEY_PGDN:                                  case KEY_PGDN:
# Line 963  int editor_display(EDITOR_DATA *p_editor Line 1202  int editor_display(EDITOR_DATA *p_editor
1202                                          }                                          }
1203                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1204                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1205                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1206                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1207                                          break;                                          break;
1208                                    case Ctrl('Q'):
1209                                  case KEY_F1:                                  case KEY_F1:
1210                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not re-entrant
1211                                          {                                          {
1212                                                  break;                                                  break;
1213                                          }                                          }
1214                                          // Display help information                                          // Display help information
1215                                          show_help = 0;                                          show_help = 0;
1216                                          display_file(DATA_READ_HELP, 1);                                          display_file(DATA_EDITOR_HELP, 1);
1217                                          show_help = 1;                                          show_help = 1;
1218                                  case KEY_F5:                                  case KEY_F5:
1219                                          // Refresh after display help information                                          // Refresh after display help information
# Line 989  int editor_display(EDITOR_DATA *p_editor Line 1229  int editor_display(EDITOR_DATA *p_editor
1229                                          break;                                          break;
1230                                  }                                  }
1231    
1232                                  BBS_last_access_tm = time(0);                                  // Refresh current action while user input
1233                                    if (user_online_update(NULL) < 0)
1234                                    {
1235                                            log_error("user_online_update(NULL) error\n");
1236                                    }
1237    
1238                                  if (input_ok)                                  if (input_ok)
1239                                  {                                  {
1240                                          break;                                          break;
1241                                  }                                  }
1242    
1243                                    ch = igetch_t(BBS_max_user_idle_time);
1244                          }                          }
1245    
1246                          continue;                          continue;


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

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