/[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.1 by sysadm, Sun Jun 8 09:25:53 2025 UTC Revision 1.8 by sysadm, Wed Jun 11 08:00:51 2025 UTC
# Line 22  Line 22 
22  #include "str_process.h"  #include "str_process.h"
23  #include <stdlib.h>  #include <stdlib.h>
24  #include <sys/param.h>  #include <sys/param.h>
25    #include <strings.h>
26    
27  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
28  #include <string.h>  #include <string.h>
# Line 29  Line 30 
30  EDITOR_DATA *editor_data_load(const char *p_data)  EDITOR_DATA *editor_data_load(const char *p_data)
31  {  {
32          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
33            char *p_data_line = NULL;
34          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES];
35          long current_data_line_length = 0;          long current_data_line_length = 0;
36          long i, j;          long i, j;
# Line 54  EDITOR_DATA *editor_data_load(const char Line 56  EDITOR_DATA *editor_data_load(const char
56                  p_editor_data->display_line_lengths[i] = line_offsets[i + 1] - line_offsets[i];                  p_editor_data->display_line_lengths[i] = line_offsets[i + 1] - line_offsets[i];
57    
58                  if (i == 0 ||                  if (i == 0 ||
59                          (current_data_line_length + p_editor_data->display_line_lengths[i] + 1) >= MAX_EDITOR_DATA_LINE_LENGTH ||                          current_data_line_length + p_editor_data->display_line_lengths[i] + 1 > MAX_EDITOR_DATA_LINE_LENGTH ||
60                          (p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n'))                          (p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n'))
61                  {                  {
62                            if (p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES)
63                            {
64                                    log_error("Append line error, data_line_total(%ld) reach limit(%d)\n", p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);
65                                    return NULL;
66                            }
67    
68                          // Allocate new data line                          // Allocate new data line
69                          p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                          p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);
70                          if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)                          if (p_data_line == NULL)
71                          {                          {
72                                  log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);                                  log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);
73                                  // Cleanup                                  // Cleanup
# Line 70  EDITOR_DATA *editor_data_load(const char Line 78  EDITOR_DATA *editor_data_load(const char
78                                  free(p_editor_data);                                  free(p_editor_data);
79                                  return NULL;                                  return NULL;
80                          }                          }
81                            p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;
                         p_editor_data->p_display_lines[i] = p_editor_data->p_data_lines[p_editor_data->data_line_total];  
82                          (p_editor_data->data_line_total)++;                          (p_editor_data->data_line_total)++;
83    
84                            p_editor_data->p_display_lines[i] = p_data_line;
85                            current_data_line_length = 0;
86                  }                  }
87                  else                  else
88                  {                  {
89                          p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1] + p_editor_data->display_line_lengths[i - 1];                          p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1] + p_editor_data->display_line_lengths[i - 1];
                         current_data_line_length = 0;  
90                  }                  }
91    
92                  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]);
                 p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i]] = '\0';  
   
93                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
94                    p_data_line[current_data_line_length] = '\0';
95          }          }
96    
97            bzero(p_editor_data->p_data_lines + p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->data_line_total);
98            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);
99    
100          return p_editor_data;          return p_editor_data;
101  }  }
102    
# Line 130  void editor_data_cleanup(EDITOR_DATA *p_ Line 141  void editor_data_cleanup(EDITOR_DATA *p_
141          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)
142          {          {
143                  free(p_editor_data->p_data_lines[i]);                  free(p_editor_data->p_data_lines[i]);
144                    p_editor_data->p_data_lines[i] = NULL;
145          }          }
146    
147          free(p_editor_data);          free(p_editor_data);
148  }  }
149    
150  static int editor_display_key_handler(int *p_key, DISPLAY_CTX *p_ctx)  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
151                                               const char *str, int str_len, long *p_last_updated_line)
152    {
153            long display_line = *p_display_line;
154            long offset = *p_offset;
155            char *p_data_line = NULL;
156            long len_data_line;
157            long offset_data_line;
158            long last_display_line; // of data line
159            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
160            long split_line_total;
161            long i, j;
162    
163            if (p_editor_data == NULL || p_last_updated_line == NULL)
164            {
165                    log_error("editor_data_op() error: NULL pointer\n");
166                    return -1;
167            }
168    
169            // Get accurate offset of first character of CJK at offset position
170            for (i = 0; i < offset; i++)
171            {
172                    if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK
173                    {
174                            i++;
175                    }
176            }
177            if (i > offset) // offset was skipped
178            {
179                    offset--;
180            }
181    
182            // Get length of current data line
183            len_data_line = 0;
184            p_data_line = p_editor_data->p_display_lines[display_line];
185            for (i = display_line - 1; i >= 0; i--)
186            {
187                    if (p_editor_data->display_line_lengths[i] > 0 &&
188                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
189                    {
190                            break;
191                    }
192    
193                    len_data_line += p_editor_data->display_line_lengths[i];
194                    p_data_line = p_editor_data->p_display_lines[i];
195            }
196            offset_data_line = len_data_line + offset;
197            last_display_line = p_editor_data->display_line_total - 1;
198            for (i = display_line; i < p_editor_data->display_line_total; i++)
199            {
200                    len_data_line += p_editor_data->display_line_lengths[i];
201    
202                    if (p_editor_data->display_line_lengths[i] > 0 &&
203                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
204                    {
205                            last_display_line = i;
206                            break;
207                    }
208            }
209    
210            // Split current data line if over-length
211            if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
212            {
213                    if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES || p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES)
214                    {
215                            log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n",
216                                              p_editor_data->display_line_total, p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);
217                            return -2;
218                    }
219    
220                    // Allocate new data line
221                    p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);
222                    if (p_data_line == NULL)
223                    {
224                            log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");
225                            return -2;
226                    }
227                    p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;
228                    (p_editor_data->data_line_total)++;
229    
230                    if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
231                    {
232                            if (str[0] == CR)
233                            {
234                                    str_len = 0;
235                            }
236    
237                            // Copy str to new data line
238                            memcpy(p_data_line, str, (size_t)str_len);
239    
240                            // Copy rest part of current data line to new data line
241                            memcpy(p_data_line + str_len,
242                                       p_editor_data->p_display_lines[display_line] + offset,
243                                       (size_t)(len_data_line - offset_data_line));
244    
245                            p_data_line[str_len + len_data_line - offset_data_line] = '\0';
246    
247                            // Add line ending to current display line (data line)
248                            p_editor_data->p_display_lines[display_line][offset] = '\n';
249                            p_editor_data->p_display_lines[display_line][offset + 1] = '\0';
250                            p_editor_data->display_line_lengths[display_line] = offset + 1;
251    
252                            *p_display_line = display_line + 1;
253                            *p_offset = str_len;
254                    }
255                    else
256                    {
257                            // Copy rest part of current data line to new data line
258                            memcpy(p_data_line,
259                                       p_editor_data->p_display_lines[display_line] + offset,
260                                       (size_t)(len_data_line - offset_data_line));
261    
262                            p_data_line[len_data_line - offset_data_line] = '\0';
263    
264                            // Append str to current display line
265                            memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);
266    
267                            // Add line ending to current display line (data line)
268                            p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';
269                            p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';
270                            p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;
271    
272                            *p_display_line = display_line;
273                            *p_offset = offset + str_len;
274                    }
275    
276                    split_line_total = last_display_line - display_line + 3;
277    
278                    // Set start display_line for spliting new data line
279                    display_line++;
280    
281                    *p_last_updated_line = p_editor_data->display_line_total;
282            }
283            else // insert str into current data line at offset_data_line
284            {
285                    memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line));
286                    memcpy(p_data_line + offset_data_line, str, (size_t)str_len);
287                    p_data_line[len_data_line + str_len] = '\0';
288    
289                    // Set p_data_line to head of current display line
290                    p_data_line = p_editor_data->p_display_lines[display_line];
291                    split_line_total = last_display_line - display_line + 3;
292    
293                    *p_display_line = display_line;
294                    *p_offset = offset + str_len;
295            }
296    
297            // Split current data line since beginning of current display line
298            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
299    
300            for (i = 0; i < split_line_total; i++)
301            {
302                    if (display_line + i > last_display_line)
303                    {
304                            // Insert blank display line after last_display_line
305                            if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
306                            {
307                                    log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
308                                    return -3;
309                            }
310                            for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
311                            {
312                                    p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
313                                    p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
314                            }
315                            last_display_line++;
316                            (p_editor_data->display_line_total)++;
317                    }
318    
319                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
320                    p_editor_data->p_display_lines[display_line + i] =
321                            (i == 0
322                                     ? p_data_line
323                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
324    
325                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
326                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
327                    {
328                            break;
329                    }
330            }
331    
332            *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
333    
334            if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] ||
335                    (*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] &&
336                     p_editor_data->p_display_lines[*p_display_line][*p_offset - 1] == '\n'))
337            {
338                    *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
339                    (*p_display_line)++;
340    
341                    if (*p_display_line >= p_editor_data->display_line_total)
342                    {
343                            log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);
344                    }
345            }
346    
347            return 0;
348    }
349    
350    int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,
351                                               long *p_last_updated_line)
352    {
353            char *p_data_line = NULL;
354            long len_data_line;
355            long offset_data_line;
356            long last_display_line; // of data line
357            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
358            long split_line_total;
359            long i, j;
360            int str_len = 0;
361    
362            if (p_editor_data == NULL || p_last_updated_line == NULL)
363            {
364                    log_error("editor_data_op() error: NULL pointer\n");
365                    return -1;
366            }
367    
368            // Get accurate offset of first character of CJK at offset position
369            for (i = 0; i < offset; i++)
370            {
371                    if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK
372                    {
373                            i++;
374                    }
375            }
376            if (i > offset) // offset was skipped
377            {
378                    offset--;
379            }
380    
381            // Get length of current data line
382            len_data_line = 0;
383            p_data_line = p_editor_data->p_display_lines[display_line];
384            for (i = display_line - 1; i >= 0; i--)
385            {
386                    if (p_editor_data->display_line_lengths[i] > 0 &&
387                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
388                    {
389                            break;
390                    }
391    
392                    len_data_line += p_editor_data->display_line_lengths[i];
393                    p_data_line = p_editor_data->p_display_lines[i];
394            }
395            offset_data_line = len_data_line + offset;
396            last_display_line = p_editor_data->display_line_total - 1;
397            for (i = display_line; i < p_editor_data->display_line_total; i++)
398            {
399                    len_data_line += p_editor_data->display_line_lengths[i];
400    
401                    if (p_editor_data->display_line_lengths[i] > 0 &&
402                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
403                    {
404                            last_display_line = i;
405                            break;
406                    }
407            }
408    
409            // Check str to be deleted
410            if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
411            {
412                    str_len = 1;
413            }
414            else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK
415            {
416                    str_len = 2;
417            }
418            else
419            {
420                    log_error("Some strange character at display_line %ld, offset %ld\n", display_line, offset);
421                    return -2;
422            }
423    
424            // Current display line is (almost) empty
425            if (offset_data_line + str_len > len_data_line ||
426                    (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
427            {
428                    log_error("Nothing to be delete\n");
429                    return 0;
430            }
431    
432            memmove(p_data_line + offset_data_line, p_data_line + offset_data_line + str_len, (size_t)(len_data_line - offset_data_line - str_len));
433            p_data_line[len_data_line - str_len] = '\0';
434            len_data_line -= str_len;
435    
436            // Set p_data_line to head of current display line
437            p_data_line = p_editor_data->p_display_lines[display_line];
438            split_line_total = last_display_line - display_line + 2;
439    
440            // Split current data line since beginning of current display line
441            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
442    
443            for (i = 0; i < split_line_total; i++)
444            {
445                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
446                    p_editor_data->p_display_lines[display_line + i] =
447                            (i == 0
448                                     ? p_data_line
449                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
450    
451                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
452                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
453                    {
454                            break;
455                    }
456            }
457    
458            *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
459    
460            if (display_line + i < last_display_line)
461            {
462                    // Remove redundant display line after last_display_line
463                    for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
464                    {
465                            p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j];
466                            p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j];
467                    }
468    
469                    (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));
470                    last_display_line = display_line + i;
471    
472                    *p_last_updated_line = p_editor_data->display_line_total - 1;
473            }
474    
475            return 0;
476    }
477    
478    static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)
479  {  {
480          switch (*p_key)          switch (*p_key)
481          {          {
482          case 0: // Set msg          case 0: // Set msg
483                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
484                                   "| ·µ»Ø[\033[32m¡û\033[33m,\033[32mESC\033[33m] | "                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");
                                  "ÒÆ¶¯[\033[32m¡ü\033[33m/\033[32m¡ý\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "  
                                  "°ïÖú[\033[32mh\033[33m] |");  
485                  break;                  break;
486          }          }
487    
# Line 154  int editor_display(EDITOR_DATA *p_editor Line 492  int editor_display(EDITOR_DATA *p_editor
492  {  {
493          static int show_help = 1;          static int show_help = 1;
494          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
495          DISPLAY_CTX ctx;          EDITOR_CTX ctx;
496          int ch = 0;          int ch = 0;
497          int input_ok, screen_current_line;          char input_str[4];
498          const int screen_begin_line = 1;          int str_len = 0;
499          int screen_end_line = SCREEN_ROWS - 1;          int input_ok;
500          const int screen_line_total = screen_end_line - screen_begin_line + 1;          int screen_current_row;
501            const int screen_begin_row = 1;
502            int screen_end_row = SCREEN_ROWS - 1;
503            const int screen_row_total = screen_end_row - screen_begin_row + 1;
504          long int line_current = 0;          long int line_current = 0;
505          long int len;          long int len;
         long int percentile;  
506          int loop;          int loop;
507          int eol, display_len;          int eol, display_len;
508          long row_pos = 1, col_pos = 1;          long row_pos = 1, col_pos = 1;
509            long display_line_in, offset_in;
510            long display_line_out, offset_out;
511            int scroll_rows;
512            long last_updated_line = 0;
513            int key_insert = 1;
514            int i;
515    
516          screen_current_line = screen_begin_line;          screen_current_row = screen_begin_row;
517          clrline(screen_begin_line, SCREEN_ROWS);          clrline(screen_begin_row, SCREEN_ROWS);
518    
519          // update msg_ext with extended key handler          // update msg_ext with extended key handler
520          if (editor_display_key_handler(&ch, &ctx) != 0)          if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 179  int editor_display(EDITOR_DATA *p_editor Line 525  int editor_display(EDITOR_DATA *p_editor
525          loop = 1;          loop = 1;
526          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
527          {          {
528                  if (line_current >= p_editor_data->display_line_total || screen_current_line > screen_end_line)                  if (line_current >= p_editor_data->display_line_total || screen_current_row > screen_end_row)
529                  {                  {
530                          ctx.reach_begin = (line_current < screen_current_line ? 1 : 0);                          ctx.line_cursor = line_current - screen_current_row + row_pos + 1;
   
                         if (line_current - (screen_current_line - screen_begin_line) + screen_line_total < p_editor_data->display_line_total)  
                         {  
                                 percentile = (line_current - (screen_current_line - screen_begin_line) + screen_line_total) * 100 / p_editor_data->display_line_total;  
                                 ctx.reach_end = 0;  
                         }  
                         else  
                         {  
                                 percentile = 100;  
                                 ctx.reach_end = 1;  
                         }  
   
                         ctx.line_top = line_current - (screen_current_line - screen_begin_line) + 1;  
                         ctx.line_bottom = MIN(line_current - (screen_current_line - screen_begin_line) + screen_line_total, p_editor_data->display_line_total);  
531    
532                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
533                                           "\033[1;44;33mµÚ\033[32m%ld\033[33m-\033[32m%ld\033[33mÐÐ (\033[32m%ld%%\033[33m) %s",                                           "\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] "
534                                           ctx.line_top,                                           "µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ [\033[32m%s\033[33m] "
535                                           ctx.line_bottom,                                           "%s",
536                                           percentile,                                           row_pos, col_pos,
537                                             ctx.line_cursor, p_editor_data->display_line_total,
538                                             key_insert ? "²åÈë" : "¸Äд",
539                                           ctx.msg);                                           ctx.msg);
540    
541                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);
# Line 230  int editor_display(EDITOR_DATA *p_editor Line 564  int editor_display(EDITOR_DATA *p_editor
564                                          goto cleanup;                                          goto cleanup;
565                                  }                                  }
566    
567                                    if (ch > 127 && ch <= 255) // GBK
568                                    {
569                                            input_str[str_len] = (char)(ch - 256);
570                                            str_len++;
571                                    }
572                                    else
573                                    {
574                                            str_len = 0;
575                                    }
576    
577                                    if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || ch == CR) // printable character or GBK
578                                    {
579                                            if (str_len == 0)
580                                            {
581                                                    input_str[0] = (char)ch;
582                                                    str_len = 1;
583                                            }
584    
585                                            last_updated_line = line_current;
586                                            display_line_in = line_current - screen_current_row + row_pos;
587                                            offset_in = col_pos - 1;
588                                            display_line_out = display_line_in;
589                                            offset_out = offset_in;
590    
591                                            if (!key_insert) // overwrite
592                                            {
593                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
594                                                                                               &last_updated_line) < 0)
595                                                    {
596                                                            log_error("editor_data_delete() error\n");
597                                                    }
598                                            }
599    
600                                            if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
601                                                                                       input_str, str_len, &last_updated_line) < 0)
602                                            {
603                                                    log_error("editor_data_insert(%s) error\n", input_str);
604                                                    str_len = 0;
605                                            }
606                                            else
607                                            {
608                                                    str_len = 0;
609    
610                                                    screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));
611                                                    line_current -= (screen_current_row - row_pos);
612                                                    screen_current_row = (int)row_pos;
613    
614                                                    scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row));
615    
616                                                    if (scroll_rows > 0)
617                                                    {
618                                                            moveto(SCREEN_ROWS, 0);
619                                                            clrtoeol();
620                                                            for (i = 0; i < scroll_rows; i++)
621                                                            {
622                                                                    prints("\033[S"); // Scroll up 1 line
623                                                            }
624    
625                                                            screen_current_row -= scroll_rows;
626                                                            if (screen_current_row < screen_begin_row)
627                                                            {
628                                                                    line_current += (screen_begin_row - screen_current_row);
629                                                                    screen_current_row = screen_begin_row;
630                                                            }
631                                                            row_pos = screen_end_row;
632                                                    }
633                                                    else // if (scroll_lines == 0)
634                                                    {
635                                                            row_pos += (display_line_out - display_line_in);
636                                                    }
637                                                    col_pos = offset_out + 1;
638    
639                                                    continue;
640                                            }
641                                    }
642                                    else if (ch == KEY_DEL) // Del
643                                    {
644                                            last_updated_line = line_current;
645    
646                                            if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,
647                                                                                       &last_updated_line) < 0)
648                                            {
649                                                    log_error("editor_data_delete() error\n");
650                                            }
651                                            else
652                                            {
653                                                    screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));
654                                                    line_current -= (screen_current_row - row_pos);
655                                                    screen_current_row = (int)row_pos;
656                                            }
657    
658                                            continue;
659                                    }
660    
661                                  switch (ch)                                  switch (ch)
662                                  {                                  {
663                                  case KEY_NULL:                                  case KEY_NULL:
# Line 238  int editor_display(EDITOR_DATA *p_editor Line 666  int editor_display(EDITOR_DATA *p_editor
666                                  case Ctrl('C'):                                  case Ctrl('C'):
667                                          loop = 0;                                          loop = 0;
668                                          break;                                          break;
669                                  case Ctrl('H'):                                  case KEY_CTRL_LEFT:
670                                          col_pos = 1;                                          col_pos = 1;
671                                          break;                                          break;
672                                  case Ctrl('E'):                                  case KEY_CTRL_RIGHT:
673                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);
674                                            break;
675                                    case KEY_CTRL_UP:
676                                            row_pos = screen_begin_row;
677                                            col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
678                                            break;
679                                    case KEY_CTRL_DOWN:
680                                            row_pos = SCREEN_ROWS - 1;
681                                            col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
682                                            break;
683                                    case KEY_INS:
684                                            key_insert = !key_insert;
685                                          break;                                          break;
686                                  case KEY_HOME:                                  case KEY_HOME:
687                                          row_pos = 1;                                          row_pos = 1;
688                                          col_pos = 1;                                          col_pos = 1;
689                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - screen_current_row < 0) // Reach begin
690                                          {                                          {
691                                                  break;                                                  break;
692                                          }                                          }
693                                          line_current = 0;                                          line_current = 0;
694                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
695                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
696                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
697                                          break;                                          break;
698                                  case KEY_END:                                  case KEY_END:
699                                          if (p_editor_data->display_line_total < screen_line_total)                                          if (p_editor_data->display_line_total < screen_row_total)
700                                          {                                          {
701                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
702                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);
703                                                  break;                                                  break;
704                                          }                                          }
705                                          line_current = p_editor_data->display_line_total - screen_line_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
706                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
707                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
708                                          row_pos = screen_line_total;                                          row_pos = screen_row_total;
709                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);
710                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
711                                          break;                                          break;
712                                  case KEY_LEFT:                                  case KEY_LEFT:
713                                          if (col_pos > 1)                                          if (col_pos > 1)
# Line 278  int editor_display(EDITOR_DATA *p_editor Line 717  int editor_display(EDITOR_DATA *p_editor
717                                          }                                          }
718                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
719                                  case KEY_UP:                                  case KEY_UP:
720                                          if (row_pos > screen_begin_line)                                          if (row_pos > screen_begin_row)
721                                          {                                          {
722                                                  row_pos--;                                                  row_pos--;
723                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
724                                                  break;                                                  break;
725                                          }                                          }
726                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - screen_current_row < 0) // Reach begin
727                                          {                                          {
728                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  col_pos = 1;
729                                                  break;                                                  break;
730                                          }                                          }
731                                          line_current -= screen_current_line;                                          line_current -= screen_current_row;
732                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
733                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
734                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
735                                          screen_end_line = SCREEN_ROWS - 1; // Legacy Fterm only works with this line                                          screen_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
736                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
                                         break;  
                                 case CR:  
737                                          break;                                          break;
738                                  case KEY_SPACE:                                  case KEY_SPACE:
739                                          break;                                          break;
740                                  case KEY_RIGHT:                                  case KEY_RIGHT:
741                                          if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])                                          if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])
742                                          {                                          {
743                                                  col_pos++;                                                  col_pos++;
744                                                  break;                                                  break;
745                                          }                                          }
746                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
747                                  case KEY_DOWN:                                  case KEY_DOWN:
748                                          if (row_pos < MIN(screen_line_total, p_editor_data->display_line_total))                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))
749                                          {                                          {
750                                                  row_pos++;                                                  row_pos++;
751                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
752                                                  break;                                                  break;
753                                          }                                          }
754                                          if (line_current + (screen_line_total - (screen_current_line - screen_begin_line)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current + (screen_row_total - (screen_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
755                                          {                                          {
756                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);
757                                                  break;                                                  break;
758                                          }                                          }
759                                          line_current += (screen_line_total - (screen_current_line - screen_begin_line));                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));
760                                          screen_current_line = screen_line_total;                                          screen_current_row = screen_row_total;
761                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
762                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
763                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
764                                          clrtoeol();                                          clrtoeol();
765                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
766                                          break;                                          break;
767                                  case KEY_PGUP:                                  case KEY_PGUP:
768                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - screen_current_row < 0) // Reach begin
769                                          {                                          {
770                                                  break;                                                  break;
771                                          }                                          }
772                                          line_current -= ((screen_line_total - 1) + (screen_current_line - screen_begin_line));                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));
773                                          if (line_current < 0)                                          if (line_current < 0)
774                                          {                                          {
775                                                  line_current = 0;                                                  line_current = 0;
776                                          }                                          }
777                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
778                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
779                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
780                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
781                                          break;                                          break;
782                                  case KEY_PGDN:                                  case KEY_PGDN:
783                                          if (line_current + screen_line_total - (screen_current_line - screen_begin_line) >= p_editor_data->display_line_total) // Reach end                                          if (line_current + screen_row_total - (screen_current_row - screen_begin_row) >= p_editor_data->display_line_total) // Reach end
784                                          {                                          {
785                                                  break;                                                  break;
786                                          }                                          }
787                                          line_current += (screen_line_total - 1) - (screen_current_line - screen_begin_line);                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);
788                                          if (line_current + screen_line_total > p_editor_data->display_line_total) // No enough lines to display                                          if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display
789                                          {                                          {
790                                                  line_current = p_editor_data->display_line_total - screen_line_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
791                                          }                                          }
792                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
793                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
794                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
795                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
796                                          break;                                          break;
797                                  case KEY_ESC:                                  case KEY_ESC:
798                                          break;                                          break;
# Line 370  int editor_display(EDITOR_DATA *p_editor Line 807  int editor_display(EDITOR_DATA *p_editor
807                                          show_help = 1;                                          show_help = 1;
808                                  case KEY_F5:                                  case KEY_F5:
809                                          // Refresh after display help information                                          // Refresh after display help information
810                                          line_current -= (screen_current_line - screen_begin_line);                                          line_current -= (screen_current_row - screen_begin_row);
811                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
812                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
813                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
814                                          break;                                          break;
815                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
816                                          break;                                          break;
# Line 403  int editor_display(EDITOR_DATA *p_editor Line 840  int editor_display(EDITOR_DATA *p_editor
840                  memcpy(buffer, (const char *)p_editor_data->p_display_lines[line_current], (size_t)len);                  memcpy(buffer, (const char *)p_editor_data->p_display_lines[line_current], (size_t)len);
841                  buffer[len] = '\0';                  buffer[len] = '\0';
842    
843                  moveto(screen_current_line, 0);                  moveto(screen_current_row, 0);
844                  clrtoeol();                  clrtoeol();
845                  prints("%s", buffer);                  prints("%s", buffer);
846                  line_current++;                  line_current++;
847                  screen_current_line++;                  screen_current_row++;
848          }          }
849    
850  cleanup:  cleanup:


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

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