/[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.3 by sysadm, Tue Jun 10 06:48:23 2025 UTC
# Line 54  EDITOR_DATA *editor_data_load(const char Line 54  EDITOR_DATA *editor_data_load(const char
54                  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];
55    
56                  if (i == 0 ||                  if (i == 0 ||
57                          (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 ||
58                          (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'))
59                  {                  {
60                            if (p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES)
61                            {
62                                    log_error("Append line error, data_line_total(%ld) reach limit(%d)\n", p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);
63                                    return NULL;
64                            }
65    
66                          // Allocate new data line                          // Allocate new data line
67                          p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                          p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH);
68                          if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)                          if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)
# Line 73  EDITOR_DATA *editor_data_load(const char Line 79  EDITOR_DATA *editor_data_load(const char
79    
80                          p_editor_data->p_display_lines[i] = p_editor_data->p_data_lines[p_editor_data->data_line_total];                          p_editor_data->p_display_lines[i] = p_editor_data->p_data_lines[p_editor_data->data_line_total];
81                          (p_editor_data->data_line_total)++;                          (p_editor_data->data_line_total)++;
82                            current_data_line_length = 0;
83                  }                  }
84                  else                  else
85                  {                  {
86                          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;  
87                  }                  }
88    
89                  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';  
   
90                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
91                    p_editor_data->p_data_lines[p_editor_data->data_line_total - 1][current_data_line_length] = '\0';
92          }          }
93    
94          return p_editor_data;          return p_editor_data;
# Line 130  void editor_data_cleanup(EDITOR_DATA *p_ Line 135  void editor_data_cleanup(EDITOR_DATA *p_
135          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)
136          {          {
137                  free(p_editor_data->p_data_lines[i]);                  free(p_editor_data->p_data_lines[i]);
138                    p_editor_data->p_data_lines[i] = NULL;
139          }          }
140    
141          free(p_editor_data);          free(p_editor_data);
142  }  }
143    
144  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,
145                                               const char *str, int str_len, long *p_last_updated_line)
146    {
147            long display_line = *p_display_line;
148            long offset = *p_offset;
149            int done = 0;
150            int len;
151            int display_len;
152            int eol;
153            char *p_data_line;
154            long len_data_line;
155            long offset_data_line;
156            long last_display_line; // of data line
157            char buf_insert[MAX_EDITOR_DATA_LINE_LENGTH];
158            long len_insert;
159            int display_len_insert;
160            char buf_catenate[MAX_EDITOR_DATA_LINE_LENGTH];
161            long len_catenate;
162            long i;
163    
164            if (p_editor_data == NULL || p_last_updated_line == NULL)
165            {
166                    log_error("editor_data_op() error: NULL pointer\n");
167                    return -1;
168            }
169    
170            memcpy(buf_insert, str, (size_t)str_len);
171            buf_insert[str_len] = '\0';
172            len_insert = str_len;
173    
174            // Get accurate offset of first character of CJK at offset position
175            for (i = 0; i < offset; i++)
176            {
177                    if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK
178                    {
179                            i++;
180                    }
181            }
182            if (i > offset) // offset was skipped
183            {
184                    offset--;
185            }
186    
187            // Get length of current data line
188            len_data_line = 0;
189            p_data_line = p_editor_data->p_display_lines[display_line];
190            for (i = display_line - 1; i >= 0; i--)
191            {
192                    if (p_editor_data->display_line_lengths[i] > 0 &&
193                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
194                    {
195                            break;
196                    }
197    
198                    len_data_line += p_editor_data->display_line_lengths[i];
199                    p_data_line = p_editor_data->p_display_lines[i];
200            }
201            offset_data_line = len_data_line + offset;
202            last_display_line = p_editor_data->display_line_total - 1;
203            for (i = display_line; i < p_editor_data->display_line_total; i++)
204            {
205                    len_data_line += p_editor_data->display_line_lengths[i];
206    
207                    if (p_editor_data->display_line_lengths[i] > 0 &&
208                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
209                    {
210                            last_display_line = i;
211                            break;
212                    }
213            }
214    
215            // Split current data line if over-length
216            if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH)
217            {
218                    if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES || p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES)
219                    {
220                            log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n",
221                                              p_editor_data->display_line_total, p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);
222                            return -2;
223                    }
224    
225                    // Allocate new data line
226                    p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH);
227                    if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)
228                    {
229                            log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");
230                            return -2;
231                    }
232    
233                    if (last_display_line > display_line)
234                    {
235                            // Copy rest part of current data line (since next display line) to new data line
236                            memcpy(p_editor_data->p_data_lines[p_editor_data->data_line_total],
237                                       p_editor_data->p_display_lines[display_line + 1],
238                                       (size_t)(len_data_line - (p_editor_data->p_display_lines[display_line + 1] - p_data_line)));
239                            p_editor_data->p_data_lines[p_editor_data->data_line_total]
240                                                                               [len_data_line - (p_editor_data->p_display_lines[display_line + 1] - p_data_line)] = '\0';
241    
242                            // Relocate rest display lines (since next one) of current data line
243                            p_data_line = p_editor_data->p_display_lines[display_line + 1];
244                            for (i = display_line + 1; i <= last_display_line; i++)
245                            {
246                                    p_editor_data->p_display_lines[i] =
247                                            p_editor_data->p_data_lines[p_editor_data->data_line_total] +
248                                            (p_editor_data->p_display_lines[i] - p_data_line);
249                            }
250                    }
251                    else // last_display_line == display_line
252                    {
253                            // Insert blank display line pointing to new data line
254                            for (i = p_editor_data->display_line_total; i > display_line + 1; i--)
255                            {
256                                    p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1];
257                                    p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1];
258                            }
259                            p_editor_data->p_display_lines[display_line + 1] = p_editor_data->p_data_lines[p_editor_data->data_line_total];
260                            p_editor_data->display_line_lengths[display_line + 1] = 0;
261    
262                            (p_editor_data->display_line_total)++;
263                            last_display_line++;
264                    }
265    
266                    *p_last_updated_line = p_editor_data->display_line_total;
267                    (p_editor_data->data_line_total)++;
268    
269                    if (offset_data_line + str_len + 2 < MAX_EDITOR_DATA_LINE_LENGTH)
270                    {
271                            // Copy rest part of current display line to insert buffer
272                            memcpy(buf_insert,
273                                       p_editor_data->p_display_lines[display_line] + offset,
274                                       (size_t)(p_editor_data->display_line_lengths[display_line] - offset));
275                            len_insert = (p_editor_data->display_line_lengths[display_line] - offset);
276                            buf_insert[len_insert] = '\0';
277    
278                            // Append str to current display line
279                            memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);
280    
281                            // Add line ending to current display line (data line)
282                            p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';
283                            p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';
284                            p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;
285    
286                            if (!done)
287                            {
288                                    *p_display_line = display_line;
289                                    *p_offset = offset + str_len;
290                                    done = 1;
291                            }
292                    }
293                    else
294                    {
295                            // Append rest part of current display line to insert buffer
296                            memcpy(buf_insert + len_insert,
297                                       p_editor_data->p_display_lines[display_line] + offset,
298                                       (size_t)(p_editor_data->display_line_lengths[display_line] - offset));
299                            len_insert += (p_editor_data->display_line_lengths[display_line] - offset);
300                            buf_insert[len_insert] = '\0';
301    
302                            // Add line ending to current display line (data line)
303                            p_editor_data->p_display_lines[display_line][offset] = '\n';
304                            p_editor_data->p_display_lines[display_line][offset + 1] = '\0';
305                            p_editor_data->display_line_lengths[display_line] = offset + 1;
306                    }
307    
308                    display_line++;
309                    offset = 0;
310            }
311    
312            for (i = display_line; len_insert > 0 && i <= last_display_line; i++)
313            {
314                    len = split_line(buf_insert, SCREEN_COLS, &eol, &display_len_insert);
315                    if (len != len_insert)
316                    {
317                            log_error("buf_insert is truncated at display_line(%ld): len(%d) != len_insert(%d), buf_insert: %s\n",
318                                              i, len, len_insert, buf_insert);
319                            return -3;
320                    }
321    
322                    memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)p_editor_data->display_line_lengths[i]);
323                    buf_catenate[p_editor_data->display_line_lengths[i]] = '\0';
324    
325                    len = split_line(buf_catenate, SCREEN_COLS - display_len_insert, &eol, &display_len);
326                    if (len < offset) // offset out of current display line
327                    {
328                            offset -= len;
329                            continue;
330                    }
331    
332                    // move \n to next display line if current line is full
333                    if (len > 0 && buf_catenate[len - 1] == '\n' && display_len + display_len_insert >= SCREEN_COLS)
334                    {
335                            len--;
336                    }
337    
338                    memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)offset);
339                    memcpy(buf_catenate + offset, buf_insert, (size_t)len_insert);
340                    memcpy(buf_catenate + offset + len_insert, p_editor_data->p_display_lines[i] + offset, (size_t)(len - offset));
341                    len_catenate = len_insert + len;
342                    buf_catenate[len_catenate] = '\0';
343    
344                    len_insert = p_editor_data->display_line_lengths[i] - len;
345                    if (len_insert > 0)
346                    {
347                            memcpy(buf_insert, p_editor_data->p_display_lines[i] + len, (size_t)len_insert);
348                            buf_insert[len_insert] = '\0';
349                    }
350    
351                    memcpy(p_editor_data->p_display_lines[i], buf_catenate, (size_t)len_catenate);
352                    p_editor_data->display_line_lengths[i] = len_catenate;
353    
354                    if (!done)
355                    {
356                            *p_display_line = i;
357                            *p_offset = offset + str_len;
358                            done = 1;
359                    }
360    
361                    offset = 0;
362            }
363    
364            *p_last_updated_line = MAX(i, *p_last_updated_line);
365    
366            if (len_insert > 0)
367            {
368                    if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
369                    {
370                            log_error("Append line error, display_line_total(%ld) reach limit(%d)\n",
371                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
372                            return -2;
373                    }
374    
375                    // Prepare one blank display line after last_display_line
376                    for (i = p_editor_data->display_line_total; i > last_display_line + 1; i--)
377                    {
378                            p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1];
379                            p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1];
380                    }
381                    p_editor_data->p_display_lines[last_display_line + 1] =
382                            p_editor_data->p_display_lines[last_display_line] + p_editor_data->display_line_lengths[last_display_line];
383                    p_editor_data->display_line_lengths[last_display_line + 1] = 0;
384    
385                    (p_editor_data->display_line_total)++;
386                    last_display_line++;
387    
388                    // Fill data into blank display line
389                    memcpy(p_editor_data->p_display_lines[last_display_line], buf_insert, (size_t)len_insert);
390                    p_editor_data->p_display_lines[last_display_line][len_insert] = '\0';
391                    p_editor_data->display_line_lengths[last_display_line] = len_insert;
392    
393                    if (!done)
394                    {
395                            *p_display_line = last_display_line;
396                            *p_offset = str_len;
397                            done = 1;
398                    }
399    
400                    *p_last_updated_line = MAX(last_display_line, *p_last_updated_line);
401            }
402    
403            if (done && *p_offset >= SCREEN_COLS)
404            {
405                    (*p_display_line)++;
406                    *p_offset = 0;
407    
408                    if (*p_display_line >= p_editor_data->display_line_total)
409                    {
410                            log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);
411                    }
412            }
413    
414            return done;
415    }
416    
417    int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,
418                                               long *p_last_updated_line)
419    {
420            return 0;
421    }
422    
423    static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)
424  {  {
425          switch (*p_key)          switch (*p_key)
426          {          {
427          case 0: // Set msg          case 0: // Set msg
428                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
429                                   "| ·µ»Ø[\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] |");  
430                  break;                  break;
431          }          }
432    
# Line 154  int editor_display(EDITOR_DATA *p_editor Line 437  int editor_display(EDITOR_DATA *p_editor
437  {  {
438          static int show_help = 1;          static int show_help = 1;
439          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
440          DISPLAY_CTX ctx;          EDITOR_CTX ctx;
441          int ch = 0;          int ch = 0;
442          int input_ok, screen_current_line;          char insert_str[4];
443          const int screen_begin_line = 1;          int str_len = 0;
444          int screen_end_line = SCREEN_ROWS - 1;          int input_ok;
445          const int screen_line_total = screen_end_line - screen_begin_line + 1;          int screen_current_row;
446            const int screen_begin_row = 1;
447            int screen_end_row = SCREEN_ROWS - 1;
448            const int screen_row_total = screen_end_row - screen_begin_row + 1;
449          long int line_current = 0;          long int line_current = 0;
450          long int len;          long int len;
         long int percentile;  
451          int loop;          int loop;
452          int eol, display_len;          int eol, display_len;
453          long row_pos = 1, col_pos = 1;          long row_pos = 1, col_pos = 1;
454            long display_line_in, offset_in;
455            long display_line_out, offset_out;
456            int scroll_rows;
457            long last_updated_line = 0;
458            int insert = 1;
459            int i;
460    
461          screen_current_line = screen_begin_line;          screen_current_row = screen_begin_row;
462          clrline(screen_begin_line, SCREEN_ROWS);          clrline(screen_begin_row, SCREEN_ROWS);
463    
464          // update msg_ext with extended key handler          // update msg_ext with extended key handler
465          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 470  int editor_display(EDITOR_DATA *p_editor
470          loop = 1;          loop = 1;
471          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
472          {          {
473                  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)
474                  {                  {
475                          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);  
476    
477                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
478                                           "\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] "
479                                           ctx.line_top,                                           "µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ "
480                                           ctx.line_bottom,                                           "%s",
481                                           percentile,                                           row_pos, col_pos,
482                                             ctx.line_cursor, p_editor_data->display_line_total,
483                                           ctx.msg);                                           ctx.msg);
484    
485                          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 508  int editor_display(EDITOR_DATA *p_editor
508                                          goto cleanup;                                          goto cleanup;
509                                  }                                  }
510    
511                                    if (ch > 127 && ch <= 255) // GBK
512                                    {
513                                            insert_str[str_len] = (char)(ch - 256);
514                                            str_len++;
515                                    }
516                                    else
517                                    {
518                                            str_len = 0;
519                                    }
520    
521                                    if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2)) // printable character or GBK
522                                    {
523                                            if (str_len == 0)
524                                            {
525                                                    insert_str[0] = (char)ch;
526                                                    str_len = 1;
527                                            }
528    
529                                            last_updated_line = line_current;
530                                            display_line_in = line_current - screen_current_row + row_pos;
531                                            offset_in = col_pos - 1;
532                                            display_line_out = display_line_in;
533                                            offset_out = offset_in;
534    
535                                            if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
536                                                                                       insert_str, str_len, &last_updated_line) < 0)
537                                            {
538                                                    log_error("editor_data_insert(%s) error\n", insert_str);
539                                                    str_len = 0;
540                                            }
541                                            else
542                                            {
543                                                    str_len = 0;
544    
545                                                    screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));
546                                                    line_current -= (screen_current_row - row_pos);
547                                                    screen_current_row = (int)row_pos;
548    
549                                                    scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row));
550    
551                                                    if (scroll_rows > 0)
552                                                    {
553                                                            moveto(SCREEN_ROWS, 0);
554                                                            clrtoeol();
555                                                            for (i = 0; i < scroll_rows; i++)
556                                                            {
557                                                                    prints("\033[S"); // Scroll up 1 line
558                                                            }
559    
560                                                            screen_current_row -= scroll_rows;
561                                                            if (screen_current_row < screen_begin_row)
562                                                            {
563                                                                    line_current += (screen_begin_row - screen_current_row);
564                                                                    screen_current_row = screen_begin_row;
565                                                            }
566                                                            row_pos = screen_end_row;
567                                                    }
568                                                    else // if (scroll_lines == 0)
569                                                    {
570                                                            row_pos += (display_line_out - display_line_in);
571                                                    }
572                                                    col_pos = offset_out + 1;
573    
574                                                    continue;
575                                            }
576                                    }
577                                    else if (ch == KEY_DEL) // Del
578                                    {
579                                            last_updated_line = line_current;
580    
581                                            if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,
582                                                                                       &last_updated_line) < 0)
583                                            {
584                                                    log_error("editor_data_delete() error\n");
585                                            }
586                                            else
587                                            {
588                                                    screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));
589                                            }
590    
591                                            continue;
592                                    }
593    
594                                  switch (ch)                                  switch (ch)
595                                  {                                  {
596                                  case KEY_NULL:                                  case KEY_NULL:
# Line 238  int editor_display(EDITOR_DATA *p_editor Line 599  int editor_display(EDITOR_DATA *p_editor
599                                  case Ctrl('C'):                                  case Ctrl('C'):
600                                          loop = 0;                                          loop = 0;
601                                          break;                                          break;
602                                  case Ctrl('H'):                                  case KEY_CTRL_LEFT:
603                                          col_pos = 1;                                          col_pos = 1;
604                                          break;                                          break;
605                                  case Ctrl('E'):                                  case KEY_CTRL_RIGHT:
606                                          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]);
607                                            break;
608                                    case KEY_CTRL_UP:
609                                            row_pos = screen_begin_row;
610                                            col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
611                                            break;
612                                    case KEY_CTRL_DOWN:
613                                            row_pos = SCREEN_ROWS - 1;
614                                            col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));
615                                            break;
616                                    case KEY_INS:
617                                            insert = !insert;
618                                          break;                                          break;
619                                  case KEY_HOME:                                  case KEY_HOME:
620                                          row_pos = 1;                                          row_pos = 1;
621                                          col_pos = 1;                                          col_pos = 1;
622                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - screen_current_row < 0) // Reach begin
623                                          {                                          {
624                                                  break;                                                  break;
625                                          }                                          }
626                                          line_current = 0;                                          line_current = 0;
627                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
628                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
629                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
630                                          break;                                          break;
631                                  case KEY_END:                                  case KEY_END:
632                                          if (p_editor_data->display_line_total < screen_line_total)                                          if (p_editor_data->display_line_total < screen_row_total)
633                                          {                                          {
634                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
635                                                  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]);
636                                                  break;                                                  break;
637                                          }                                          }
638                                          line_current = p_editor_data->display_line_total - screen_line_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
639                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
640                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
641                                          row_pos = screen_line_total;                                          row_pos = screen_row_total;
642                                          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]);
643                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
644                                          break;                                          break;
645                                  case KEY_LEFT:                                  case KEY_LEFT:
646                                          if (col_pos > 1)                                          if (col_pos > 1)
# Line 278  int editor_display(EDITOR_DATA *p_editor Line 650  int editor_display(EDITOR_DATA *p_editor
650                                          }                                          }
651                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
652                                  case KEY_UP:                                  case KEY_UP:
653                                          if (row_pos > screen_begin_line)                                          if (row_pos > screen_begin_row)
654                                          {                                          {
655                                                  row_pos--;                                                  row_pos--;
656                                                  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]));
657                                                  break;                                                  break;
658                                          }                                          }
659                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - screen_current_row < 0) // Reach begin
660                                          {                                          {
661                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  col_pos = 1;
662                                                  break;                                                  break;
663                                          }                                          }
664                                          line_current -= screen_current_line;                                          line_current -= screen_current_row;
665                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
666                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
667                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
668                                          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
669                                          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]));
670                                          break;                                          break;
671                                  case CR:                                  case CR:
672                                          break;                                          break;
673                                  case KEY_SPACE:                                  case KEY_SPACE:
674                                          break;                                          break;
675                                  case KEY_RIGHT:                                  case KEY_RIGHT:
676                                          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])
677                                          {                                          {
678                                                  col_pos++;                                                  col_pos++;
679                                                  break;                                                  break;
680                                          }                                          }
681                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
682                                  case KEY_DOWN:                                  case KEY_DOWN:
683                                          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))
684                                          {                                          {
685                                                  row_pos++;                                                  row_pos++;
686                                                  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]));
687                                                  break;                                                  break;
688                                          }                                          }
689                                          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
690                                          {                                          {
691                                                  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]);
692                                                  break;                                                  break;
693                                          }                                          }
694                                          line_current += (screen_line_total - (screen_current_line - screen_begin_line));                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));
695                                          screen_current_line = screen_line_total;                                          screen_current_row = screen_row_total;
696                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
697                                          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]));
698                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
699                                          clrtoeol();                                          clrtoeol();
700                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
701                                          break;                                          break;
702                                  case KEY_PGUP:                                  case KEY_PGUP:
703                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - screen_current_row < 0) // Reach begin
704                                          {                                          {
705                                                  break;                                                  break;
706                                          }                                          }
707                                          line_current -= ((screen_line_total - 1) + (screen_current_line - screen_begin_line));                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));
708                                          if (line_current < 0)                                          if (line_current < 0)
709                                          {                                          {
710                                                  line_current = 0;                                                  line_current = 0;
711                                          }                                          }
712                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
713                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
714                                          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]));
715                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
716                                          break;                                          break;
717                                  case KEY_PGDN:                                  case KEY_PGDN:
718                                          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
719                                          {                                          {
720                                                  break;                                                  break;
721                                          }                                          }
722                                          line_current += (screen_line_total - 1) - (screen_current_line - screen_begin_line);                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);
723                                          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
724                                          {                                          {
725                                                  line_current = p_editor_data->display_line_total - screen_line_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
726                                          }                                          }
727                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
728                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
729                                          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]));
730                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
731                                          break;                                          break;
732                                  case KEY_ESC:                                  case KEY_ESC:
733                                          break;                                          break;
# Line 370  int editor_display(EDITOR_DATA *p_editor Line 742  int editor_display(EDITOR_DATA *p_editor
742                                          show_help = 1;                                          show_help = 1;
743                                  case KEY_F5:                                  case KEY_F5:
744                                          // Refresh after display help information                                          // Refresh after display help information
745                                          line_current -= (screen_current_line - screen_begin_line);                                          line_current -= (screen_current_row - screen_begin_row);
746                                          screen_current_line = screen_begin_line;                                          screen_current_row = screen_begin_row;
747                                          screen_end_line = SCREEN_ROWS - 1;                                          screen_end_row = SCREEN_ROWS - 1;
748                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(screen_begin_row, SCREEN_ROWS);
749                                          break;                                          break;
750                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
751                                          break;                                          break;
# Line 403  int editor_display(EDITOR_DATA *p_editor Line 775  int editor_display(EDITOR_DATA *p_editor
775                  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);
776                  buffer[len] = '\0';                  buffer[len] = '\0';
777    
778                  moveto(screen_current_line, 0);                  moveto(screen_current_row, 0);
779                  clrtoeol();                  clrtoeol();
780                  prints("%s", buffer);                  prints("%s", buffer);
781                  line_current++;                  line_current++;
782                  screen_current_line++;                  screen_current_row++;
783          }          }
784    
785  cleanup:  cleanup:


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

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