C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
view_engine.c
Go to the documentation of this file.
1/** @file view_engine.c
2 @brief The working part of View
3 @author Bill Waller
4 Copyright (c) 2025
5 MIT License
6 billxwaller@gmail.com
7 @date 2026-02-09
8 */
9
10/**
11 @defgroup view_engine View Engine
12 @brief File mapping, user input, command processing, and display logic
13 */
14
15#ifdef NCURSES_UI
16#include "../ui/ui_ncurses_internal.h"
17#include "ui_backend.h"
18#include <ncursesw/ncurses.h>
19#include <ncursesw/panel.h>
20#endif
21#include <common.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <iso646.h>
25#include <regex.h>
26#include <stdbool.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/mman.h>
31#include <sys/param.h>
32#include <sys/stat.h>
33#include <termios.h>
34#include <time.h>
35#include <unistd.h>
36
37/** @brief read the next characater from the virtual file
38 @ingroup view_engine
39 @details Line numbers are tracked when reading forward and stored in a line
40 table for quick access when moving backwards.
41 When reading forward, the End of Data (EOD) flag is set when the
42 position is equal to the file size, and cleared when the position or reading
43 direction changes.
44 @note view_engine.c line numbering
45 The macro, get_next_char() reads lines delimited by '\n', advancing the
46 line number index at the end of each record. The line number index
47 is one record ahead for most operations, including line numbering as lines
48 are displayed. Nevertheless, when providing a line number index to
49 get_line(), you must use the zero origin index, which is one less than
50 the line number index
51 */
52#define get_next_char()
53 {
54 c = 0;
55 do {
56 if (view->file_pos == view->file_size) {
57 view->f_eod = true;
58 break;
59 } else
60 view->f_eod = false;
61 c = view->buf[view->file_pos++];
62 } while (c == 0x0d);
63 if (c == '\n')
64 increment_ln(view);
65 }
66/** @brief read the previous characater from the virtual file
67 @ingroup view_engine
68 There is no need to track line numbers when moving backwards as they
69 are stored in the line table and accessed as needed.
70 When reading in reverse, the Beginning of Data (BOD) flag is set when
71 the file position is zero, and cleared when the position or reading direction
72 changes.
73 Carriage-returns are ignored as they should be.
74 */
75#define get_prev_char()
76 {
77 c = 0;
78 do {
79 if (view->file_pos == 0) {
80 view->f_bod = true;
81 break;
82 } else
83 view->f_bod = false;
84 c = view->buf[--view->file_pos];
85 } while (c == 0x0d);
86 }
87
88#define confirm()
89 {
90 if ((c = get_cmd_char(view, &n_cmd)) == 'y' || c == 'Y')
91 ans = true;
92 else if (c == 'n' || c == 'N')
93 ans = false;
94 }
95
96#define _ui_perror(msg)
97 {
98 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 2);
99 strnz__cpy(em1, msg, MAXLEN - 1);
100 ui_display_error(em0, em1, nullptr, nullptr);
101 }
102
103#define _Refresh(view)
104 {
105 pad_refresh(view);
106 ui_render();
107 }
108
110FILE *dbgfp;
111int view_file(Init *);
112int view_cmd_processor(Init *);
113int get_cmd_char(View *, off_t *);
114int get_cmd_arg(View *, char *);
115void build_prompt(View *);
116void cat_file(View *);
117void lp(View *, char *);
118void go_to_mark(View *, uint);
119void go_to_eof(View *);
120int go_to_line(View *, off_t);
121void go_to_percent(View *, uint);
122void go_to_position(View *, off_t);
123bool search(View *, int, char *);
124void page_next(View *);
125void page_prev(View *);
126void scroll_next(View *view, uint n);
127void scroll_prev(View *view, uint n);
128void get_line(View *, off_t);
129int fmt_line(View *);
130void log_split_lines(View *);
131void log_cc_buf(View *);
132void log_stripped_line_out(View *);
133void log_strnz(char *, uint);
134void display_line(View *);
135void display_line_eod(View *);
136void display_split_line(View *);
137void view_display_page(View *);
138void parse_ansi_str(char *, attr_t *, ushort *);
139void view_display_help(Init *);
140int display_prompt(View *, char *);
141int write_view_buffer(Init *, bool);
142bool enter_file_spec(Init *, char *);
143int a_toi(char *, bool *);
144void increment_ln(View *);
145void initialize_line_table(View *);
146void destroy_line_table(View *);
147int pad_refresh(View *);
148void sync_ln(View *, off_t ln_no);
149void sync_pos(View *, off_t pos);
150off_t line_number(View *, off_t);
152
153/** @brief Start view
154 @ingroup view_engine
155 @param init Pointer to the Init structure containing initialization
156 parameters and state for the view application. This structure is used to pass
157 necessary information and maintain state across different functions within
158 the view application.
159 @return Returns 0 on successful completion of the view application, or a
160 non-zero value if an error occurs during initialization or execution.
161 */
162int view_file(Init *init) {
163 View *view = init->view;
164 if (view->argc < 1) {
165 view->curr_argc = -1;
166 view->argc = 0;
167 view->next_file_spec_ptr = "-";
168 } else
169 view->next_file_spec_ptr = view->argv[0];
170 while (view->curr_argc < view->argc) {
171 if (view->next_file_spec_ptr == nullptr ||
172 *view->next_file_spec_ptr == '\0') {
173 break;
174 }
176 view->next_file_spec_ptr = nullptr;
178 if (view_init_input(init, view->cur_file_str) == 0) {
179 if (view->buf) {
180 view->f_eod = 0;
181 view->f_bod = 0;
182 view->maxcol = 0;
183 view->page_top_pos = 0;
184 view->page_top_ln_no = 0;
185 view->page_bot_ln_no = 0;
186 view->ln_pos_max = 0;
187 view->page_bot_pos = 0;
188 view->file_pos = 0;
190 if (!view->f_full_screen)
193 page_next(view);
196 munmap(view->buf, view->file_size);
197 }
198 } else {
199 view->curr_argc++;
200 if (view->curr_argc < view->argc) {
202 }
203 }
204 }
206 return 0;
207}
208/** @brief Main Command Processing Loop for View
209 @ingroup view_engine
210 @param init Pointer to the Init structure containing initialization
211 parameters and state for the view application. This structure is used to pass
212 necessary information and maintain state across different functions within
213 the view application.
214*/
215int view_cmd_processor(Init *init) {
216 char tmp_str[MAXLEN];
217 int tfd;
218 int c;
219 uint shift = 0;
220 int search_cmd = 0;
221 int prev_search_cmd = 0;
222 int rc;
223 uint i;
224 bool ans = true;
225 ssize_t bytes_written;
226 char *e;
227 char shell_cmd_spec[MAXLEN];
228 off_t n_cmd = 0L;
229 off_t prev_file_pos;
230 View *view = init->view;
231 UiSurface *sfc = view->sfc;
232 view->cmd[0] = '\0';
233 while (1) {
234 if (view->f_redisplay_page) {
236 view->f_redisplay_page = false;
237 }
238 c = view->next_cmd_char;
239 view->next_cmd_char = 0;
240 if (!c) {
241 build_prompt(view);
243 c = get_cmd_char(view, &n_cmd);
244 if (c >= '0' && c <= '9') {
245 tmp_str[0] = (char)c;
246 tmp_str[1] = '\0';
247 c = get_cmd_arg(view, tmp_str);
248 }
249 }
250 switch (c) {
251
252 case 'd': /** 'd' Breakpoint for Debugging */
253 break;
254 case Ctrl('L'): /**< Ctrl('L') or UIKEY_RESIZE - Handle terminal resize */
255 case UIKEY_RESIZE:
256 ui_log(INFO, _("Resize key received"));
258 ui_log(INFO, _("Resize: lines=%d, cols=%d"), view->lines, view->cols);
259 if (view->f_full_screen)
261 else
263 view->f_redisplay_page = true;
264 continue;
265 case UIKEY_F05: /**< UIKEY_ALTHOME - horizontal scroll to the first
266 column */
267 view->pmincol = 0;
268 break;
269 case UIKEY_F06: /**< UIKEY_ALTEND horizontal scroll to the last
270 * column
271 */
272 if (view->maxcol > view->cols)
273 view->pmincol = view->maxcol - view->cols;
274 else
275 view->pmincol = 0;
276 break;
277 case 'h': /**< 'h', Ctrl('H'), UIKEY_LEFT, UIKEY_BACKSPACE - Horizontal
278 scroll left by two thirds of the page width */
279 case UIKEY_LEFT:
280 case UIKEY_BACKSPACE:
281 if (n_cmd > 0)
282 view->h_shift = (uint)n_cmd;
283 else if (n_cmd == 0) {
284 if (view->h_shift > 0)
285 n_cmd = view->h_shift;
286 else
287 n_cmd = 1;
288 }
289 shift = (uint)n_cmd;
290 if (view->pmincol - shift > 0)
291 view->pmincol -= shift;
292 else
293 view->pmincol = 0;
294 break;
295 case 'l': /**< 'l', UIKEY_RIGHT - Horizontal scroll right by two thirds of the page width */
296 case UIKEY_RIGHT:
297 if (n_cmd > 0)
298 view->h_shift = n_cmd;
299 else if (n_cmd == 0) {
300 if (view->h_shift > 0)
301 n_cmd = view->h_shift;
302 else
303 n_cmd = 1;
304 }
305 shift = (uint)n_cmd;
306 if ((view->pmincol + shift + view->cols) > view->maxcol)
307 view->pmincol = view->maxcol - view->cols + view->ln_win_cols;
308 else
309 view->pmincol += shift;
310 break;
311 case 'k': /** 'k', UIKEY_UP - Scroll up one line */
312 case UIKEY_SCROLL_UP:
313 case UIKEY_UP:
314 if (n_cmd <= 0)
315 n_cmd = 1;
316 scroll_prev(view, (uint)n_cmd);
317 break;
318 /** 'j', UIKEY_DOWN, UIKEY_ENTER - scroll down one line */
319 case 'j':
320 case '\n':
322 case UIKEY_DOWN:
323 case UIKEY_ENTER:
324 if (n_cmd <= 0)
325 n_cmd = 1;
326 scroll_next(view, (uint)n_cmd);
327 break;
328 /** Ctrl('B'), UIKEY_PPAGE - Previous Page */
329 case UIKEY_PPAGE:
330 case Ctrl('B'):
331 page_prev(view);
332 break;
333 /** Ctrl('F'), UIKEY_NPAGE Next Page */
334 case UIKEY_NPAGE:
335 case Ctrl('F'):
336 view->ln_no++;
337 page_next(view);
338 break;
339 /** '!', Execute Shell Command from within C-Menu View */
340 case '!':
341 if (view->f_displaying_help)
342 break;
343 if (get_cmd_arg(view, "!") == 0) {
344 if (!view->f_is_pipe) {
347 str_subc(shell_cmd_spec, view->cmd_arg, '%',
348 view->cur_file_str, MAXLEN - 1);
349 } else
350 strnz__cpy(shell_cmd_spec, view->cmd_arg, MAXLEN - 1);
351 full_screen_shell(shell_cmd_spec);
352 if (!view->f_is_pipe) {
354 return 0;
355 }
356 }
357 break;
358 /** '+', Set Startup Command */
359 case '+':
360 if (get_cmd_arg(view, _("Startup Command:")) == 0)
362 break;
363 /** '-', Change View Settings */
364 case '-':
365 display_prompt(view, "(i, n, s, t, w, or h)->");
366 c = get_cmd_char(view, &n_cmd);
367 c = S_TOLOWER(c);
368 switch (c) {
369 /** -i ignore_case in search */
370 /** -n line numbers */
371 /** -s squeeze multiple blank lines */
372 /** -t n set tab stop columns */
373 /** -w wrap long lines */
374 /** -h display help */
375 case 'i':
376 display_prompt(view, _("Ignore Case in search (Y or N)->"));
377 confirm();
378 if (ans)
379 view->f_ignore_case = true;
380 else if (c == 'n' || c == 'N')
381 view->f_ignore_case = false;
382 break;
383 /** -n line numbers */
384 case 'n':
385 display_prompt(view, _("Line Numbering (Y or N)->"));
386 confirm();
387 if (view->f_ln == ans)
388 break;
389 view->f_ln = ans;
390 view->f_redisplay_page = true;
392 break;
393 /** -s Squeeze Multiple Blank Lines */
394 case 's':
396 view, _("view->f_squeeze Multiple Blank lines (Y or N)->"));
397 confirm();
398 view->f_squeeze = ans;
399 break;
400 case 'w':
402 view, _("Line Wrapping (Y or N)->"));
403 if ((c = get_cmd_char(view, &n_cmd)) == 'y' || c == 'Y')
404 ans = true;
405 else if (c == 'n' || c == 'N')
406 ans = false;
407 if (view->wrap == ans)
408 break;
409 view->wrap = ans;
410 view->f_redisplay_page = true;
412 break;
413 /** -t n Set Tab Stop Columns */
414 case 't':
415 sprintf(tmp_str,
416 _("Tabstop Colums Currently %d:"), view->tab_stop);
417 i = 0;
418 if (get_cmd_arg(view, tmp_str) == 0)
419 i = atoi(view->cmd_arg);
420 if (i >= 1 && i <= 12) {
421 view->tab_stop = i;
422 view->f_redisplay_page = true;
423 } else
424 ui_perror(_("Tab stops not changed"));
425 break;
426 /** -h Display Help */
427 case UIKEY_F01:
428 case 'h':
429 if (!view->f_displaying_help) {
431 view = init->view;
432 }
433 view->next_cmd_char = '-';
434 break;
435 default:
436 break;
437 }
438 break;
439 /** 'n' - Repeat Previous Search */
440 case 'n':
441 if (view->f_search_complete) {
442 ui_perror(_("Search complete, no more matches"));
443 break;
444 }
445 if (prev_search_cmd == 0) {
446 ui_perror(_("No previouis search"));
447 break;
448 }
449 if (prev_search_cmd == '/') {
450 view->cury = 0;
452 } else {
453 view->cury = view->scroll_lines + 1;
455 }
456 rc = search(view, prev_search_cmd, prev_regex_pattern);
457 if (rc == false) {
458 ui_perror(_("No matches found"));
459 break;
460 }
461 break;
462 /** '/' or '?' - Search Forward fromk top of page */
463 case '/':
464 view->f_search_complete = false;
465 strnz__cpy(tmp_str, _(" Forward:"), MAXLEN - 1);
466 search_cmd = c;
467 c = get_cmd_arg(view, tmp_str);
468 if (c == UIKEY_F09 || c == '\033')
469 break;
470 if (c == UIKEY_ENTER) {
471 view->cury = 0;
472 view->f_first_iter = true;
475 rc = search(view, search_cmd, view->cmd_arg);
476 if (rc == false) {
477 ui_perror(_("No matches found"));
478 break;
479 }
480 prev_search_cmd = search_cmd;
482 }
483 break;
484 /** '?' - Search Backward */
485 case '?':
486 view->f_search_complete = false;
487 strnz__cpy(tmp_str, _(" Backward:"), MAXLEN - 1);
488 search_cmd = c;
489 c = get_cmd_arg(view, tmp_str);
490 if (c == UIKEY_F09 || c == '\033')
491 break;
492 if (c == '\n') {
493 view->cury = view->scroll_lines;
494 view->f_first_iter = true;
497 rc = search(view, search_cmd, view->cmd_arg);
498 if (rc == false) {
499 ui_perror(_("No matches found"));
500 break;
501 }
502 prev_search_cmd = search_cmd;
504 }
505 break;
506 /** 'o' - Open a File */
507 case 'o':
508 if (get_cmd_arg(view, _("File name:")) == 0) {
509 strtok(view->cmd_arg, " ");
510 view->next_file_spec_ptr = strdup(view->cmd_arg);
511 view->f_redisplay_page = true;
512 return 0;
513 }
514 break;
515 case 'G': /** 'G' - Go to the Beginning of the Document or line */
516 case UIKEY_HOME:
517 if (n_cmd > 0) {
518 n_cmd -= 1;
519 go_to_line(view, n_cmd);
520 } else
521 go_to_eof(view);
522 break;
523 /** 'H' or UIKEY_F01 - Display Help Information */
524 case 'H':
525 case UIKEY_F01:
526 if (!view->f_displaying_help) {
528 view = init->view;
529 }
530 break;
531 /** 'm' - Set a Mark at the Current Position */
532 case 'm':
533 display_prompt(view, _("Mark label (A-Z)->"));
534 c = get_cmd_char(view, &n_cmd);
535 if (c == '@' || c == UIKEY_F09 || c == '\033')
536 if (c >= 'A' && c <= 'Z')
537 c += ' ';
538 if (c < 'a' || c > 'z')
539 ui_perror(_("Not (a-z)"));
540 else
541 view->mark_tbl[c - 'a'] = view->page_top_pos;
542 break;
543 /** 'M' - Go to a Mark */
544 case 'M':
545 display_prompt(view, _("Goto mark (A-Z)->"));
546 c = get_cmd_char(view, &n_cmd);
547 if (c == '@' || c == UIKEY_F09 || c == '\033')
548 break;
549 if (c >= 'A' && c <= 'Z')
550 c += ' ';
551 if (c < 'a' || c > 'z')
552 ui_perror(_("Not (A-Z)"));
553 else
554 go_to_mark(view, c);
555 break;
556 /** 'N' - Close Current File and Open Next File */
557 case 'N':
558 if (n_cmd <= 0)
559 n_cmd = 1;
560 if (view->curr_argc + n_cmd >= view->argc) {
561 ui_perror(_("no more files"));
562 view->curr_argc = view->argc - 1;
563 } else {
564 view->curr_argc++;
565 if (view->curr_argc < view->argc)
567 return 0;
568 }
569 break;
570 /** 'p' or '%' - Go to a Percent of the File */
571 case 'p':
572 case '%':
573 if (n_cmd < 0)
574 go_to_line(view, 1);
575 if (n_cmd >= 100)
576 go_to_eof(view);
577 else
578 go_to_percent(view, n_cmd);
579 break;
580 /** Ctrl('Z') - Send File to Print Queue with Notation */
581 case Ctrl('Z'):
582 get_cmd_arg(view, _("Enter Notation:"));
583 strnz__cpy(tmp_str, "/tmp/view-XXXXXX", MAXLEN - 1);
584 tfd = mkstemp(tmp_str);
586 if (tfd == -1) {
587 ui_perror(_("Unable to create temporary file"));
588 break;
589 }
590 strnz__cpy(shell_cmd_spec, "echo ", MAXLEN - 5);
591 strnz__cat(shell_cmd_spec, view->cmd_arg, MAXLEN - 5);
592 strnz__cat(shell_cmd_spec, view->tmp_file_name_ptr, MAXLEN - 5);
593 shell(shell_cmd_spec);
594 strnz__cpy(shell_cmd_spec, "cat ", MAXLEN - 5);
595 strnz__cat(shell_cmd_spec, view->cmd_arg, MAXLEN - 5);
596 strnz__cat(shell_cmd_spec, ">>", MAXLEN - 5);
597 strnz__cat(shell_cmd_spec, view->tmp_file_name_ptr, MAXLEN - 5);
598 shell(shell_cmd_spec);
599 lp(view, view->cur_file_str);
600 shell(shell_cmd_spec);
601 ssnprintf(shell_cmd_spec, (size_t)(MAXLEN - 5), "rm %s",
603 strnz__cpy(shell_cmd_spec, "rm ", MAXLEN - 5);
604 strnz__cat(shell_cmd_spec, view->tmp_file_name_ptr, MAXLEN - 5);
605 shell(shell_cmd_spec);
607 view->f_redisplay_page = true;
608 unlink(tmp_str);
609 break;
610 /** 'P' Print Current File */
611 case Ctrl('P'):
612 lp(view, view->cur_file_str);
613 view->f_redisplay_page = true;
614 break;
615 /** 'P' or UIKEY_F09 or ESC - Close Current File and Open Next */
616 case 'P':
617 if (n_cmd <= 0)
618 n_cmd = 1;
619 if (view->curr_argc - n_cmd < 0) {
620 ui_perror(_("No previous file"));
621 view->curr_argc = 0;
622 } else {
623 view->curr_argc--;
624 if (view->curr_argc >= 0)
626 return 0;
627 }
628 break;
629 /** 'q' or 'Q' or UIKEY_F09 or ESC - Quit the Application */
630 case 'q':
631 case 'Q':
632 case UIKEY_F09:
634 view->curr_argc = view->argc;
635 view->next_file_spec_ptr = nullptr;
636 return 0;
637 /** 'v' - Open Current File in Editor */
638 case 'v':
639 if (init->editor[0] == 0) {
640 e = getenv("DEFAULTEDITOR");
641 if (e == nullptr || *e == '\0')
643 else
645 }
647 _("View doesn't support editing current buffer directly"),
648 MAXLEN - 1);
649 strnz__cpy(em1, _("Would you like to write the buffer to a file?"),
650 MAXLEN - 1);
651 strnz__cpy(em2, _("Enter Y for yes or any other key to cancel."),
652 MAXLEN - 1);
653 rc = ui_display_error(em0, em1, em2, nullptr);
654 if (rc != 'y' && rc != 'Y')
655 break;
656 if (!enter_file_spec(init, view->out_spec)) {
657 view->f_redisplay_page = true;
658 break;
659 }
660 prev_file_pos = view->page_top_pos;
661 bytes_written = write_view_buffer(init, view->f_strip_ansi);
662 if (bytes_written == 0) {
663 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__,
664 __LINE__ - 2);
665 strnz__cpy(em1, _("0 bytes written"), MAXLEN - 1);
666 strerror__r(errno, em1, MAXLEN - 1);
667 ui_display_error(em0, em1, nullptr, nullptr);
668 break;
669 }
671 strnz__cpy(shell_cmd_spec, init->editor, MAXLEN - 5);
672 strnz__cat(shell_cmd_spec, " ", MAXLEN - 5);
673 strnz__cat(shell_cmd_spec, view->in_spec, MAXLEN - 5);
674 full_screen_shell(shell_cmd_spec);
676 prev_file_pos;
677
679 view->f_redisplay_page = true;
680 return 0;
681 /** 'w' - Write the current buffer to file */
682 case 'w':
683 if (!enter_file_spec(init, view->out_spec)) {
684 view->f_redisplay_page = true;
685 break;
686 }
687 // prev_file_pos = view->page_top_pos;
688 bytes_written = write_view_buffer(init, view->f_strip_ansi);
689 if (bytes_written == 0) {
690 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__,
691 __LINE__ - 2);
692 strnz__cpy(em1, _("0 bytes written"), MAXLEN - 1);
693 strerror__r(errno, em1, MAXLEN - 1);
694 ui_display_error(em0, em1, nullptr, nullptr);
695 break;
696 }
697 display_prompt(view, tmp_str);
698 view->f_redisplay_page = true;
699 break;
700 case CT_VIEW:
701 break;
702 /** 'V' - Display Version Information */
703 case 'V':
704 ssnprintf(em0, MAXLEN - 1, _("C-Menu Version: %s"), CM_VERSION);
705 ui_display_error(em0, em1, nullptr, nullptr);
706 break;
707 default:
708 break;
709 }
710 view->cmd_arg[0] = '\0';
711 }
712}
713/** @brief Get Command Character from User Input
714 @ingroup view_engine
715 @param view Pointer to the View structure containing the state and
716 parameters of the view application. This structure is used to access and
717 modify the state of the application as needed.
718 @param n Pointer to an off_t variable where the numeric argument entered by
719 the user will be stored. If the user enters a numeric argument, it will be
720 converted to an off_t value and stored in this variable for use by the
721 calling function.
722 @return Returns the command character entered by the user, or a special
723 value if a mouse event is detected. The function handles user input,
724 including editing keys, and updates the command argument buffer accordingly.
725 If the user enters a numeric argument, it is validated based on the context
726 of the command being executed.
727 __get_cmd_char
728*/
729int get_cmd_char(View *view, off_t *n) {
730 int c = 0, i = 0;
731 char *d, *s;
732 uint prevx;
733 bool once = false;
734 char cmd_str[33];
735 cmd_str[0] = '\0';
736 UiSurface *sfc = view->sfc;
737 UiEvent event;
739 pad_refresh(view);
740 ui_mvwadd_wch(sfc, CMDLN, view->cmd_line, view->curx++, &cell_ran);
741 ui_top_surface(sfc, CMDLN);
742 ui_cursor_enable_yx(sfc, CMDLN, 0, view->curx, true);
743 while (1) {
744 if (i == 1 && !once) {
745 once = true;
746 view->curx = 0;
747 ui_cursor_move(sfc, CMDLN, view->cmd_line, view->curx);
748 ui_wclrtoeol(sfc, CMDLN);
749 ui_mvwadd_wch(sfc, CMDLN, view->cmd_line, view->curx++, &cell_ran);
750 ui_mvwaddch(sfc, CMDLN, view->cmd_line, view->curx++, c);
751 }
753 event.y = event.x = -1;
754 c = ui_get_event(sfc, CMDLN, NULL, &event, -1);
755 switch (c) {
756 case UIKEY_BUTTON1:
757 case UIKEY_SCROLL_UP:
759 break;
760 case UIKEY_RESIZE:
761 case '\n':
762 case '\r':
763 break;
764 case 'q':
765 case UIKEY_F09:
766 build_prompt(view);
768 c = UIKEY_F09;
769 return c;
770 case UIKEY_F12:
771 ssnprintf(view->prompt_str, MAXLEN - 1, _("set break at line: %d and restart gdb"), __LINE__);
773 break;
774 case '\b':
775 case UIKEY_BACKSPACE:
776 if (i > 0) {
777 s = &cmd_str[i];
778 d = &cmd_str[--i];
779 view->curx--;
780 prevx = view->curx;
781 while (*s != '\0') {
782 ui_mvwaddch(sfc, CMDLN, view->cmd_line, view->curx++, *s);
783 *d++ = *s++;
784 }
785 *d = '\0';
786 ui_cursor_move(sfc, CMDLN, view->cmd_line, view->curx);
787 ui_wclrtoeol(sfc, CMDLN);
788 view->curx = prevx;
789 ui_cursor_move(sfc, CMDLN, view->cmd_line, view->curx);
790 }
791 continue;
792 case UIKEY_DC:
793 if (i < 32 && cmd_str[i] != '\0') {
794 s = &cmd_str[i + 1];
795 d = &cmd_str[i];
796 while (*s != '\0') {
797 ui_mvwaddch(sfc, CMDLN, view->cmd_line, view->curx++, *s);
798 *d++ = *s++;
799 }
800 *d = '\0';
801 ui_cursor_move(sfc, CMDLN, view->cmd_line, view->curx);
802 ui_wclrtoeol(sfc, CMDLN);
803 ui_cursor_move(sfc, CMDLN, view->cmd_line, view->curx);
804 }
805 continue;
806 case UIKEY_RSHIFT:
807 if (i < 32 && cmd_str[i] != '\0') {
808 i++;
809 ui_getyx(sfc, CMDLN, &view->cury, &view->curx);
810 if (view->curx < view->cols - 1) {
811 view->curx++;
812 ui_cursor_move(sfc, CMDLN, view->cmd_line, view->curx);
813 }
814 }
815 continue;
816 case UIKEY_LSHIFT:
817 if (i > 0) {
818 i--;
819 ui_getyx(sfc, CMDLN, &view->cury, &view->curx);
820 if (view->curx > 0) {
821 view->curx--;
822 ui_cursor_move(sfc, CMDLN, view->cmd_line, view->curx);
823 }
824 }
825 continue;
826 default:
827 if (c < '0' || c > '9' || i == 32)
828 break;
829 cmd_str[i++] = (char)c;
830 cmd_str[i] = '\0';
831 ui_mvwaddch(sfc, CMDLN, view->cmd_line, view->curx, c);
832 view->curx++;
833 continue;
834 }
835 if (c < '0' || c > '9' || i == 32)
836 break;
837 }
838 if (cmd_str[0] == '\0') {
839 *n = 0;
840 view->cmd_arg[0] = '\0';
842 ui_wclrtoeol(sfc, CMDLN);
843 return (c);
844 }
845 *n = atol(cmd_str);
846 view->cmd_arg[0] = '\0';
847 ui_getyx(sfc, CMDLN, &view->cmd_line, &view->curx);
849 ui_wclrtoeol(sfc, CMDLN);
850 return (c);
851}
852/** @brief Get Command Argument from User Input
853 @ingroup view_engine
854 @param view Pointer to the View structure containing the state and
855 parameters of the view application. This structure is used to access and
856 modify the state of the application as needed.
857 @param prompt A string containing the prompt to be displayed to the user
858 when requesting input for the command argument. This prompt is shown on
859 the command line to guide the user in providing the necessary input for
860 the command being executed.
861 @return Returns the command character entered by the user, or a special
862 value if a mouse event is detected. The command argument entered by the
863 user is stored in the view->cmd_arg buffer for use by the calling
864 function. The function handles user input, including editing keys, and
865 updates the command argument buffer accordingly. If the user enters a
866 numeric argument, it is validated based on the context of the command
867 being executed.
868 __get_cmd_arg
869*/
870int get_cmd_arg(View *view, char *prompt) {
871 int c;
872 char prompt_s[MAXLEN];
873 UiSurface *sfc = view->sfc;
874 uint prompt_l = strnz__cpy(prompt_s, prompt, min(MAXLEN - 1, view->cols - 4));
875 if (view->cmd_arg[0] != '\0')
876 return 0;
877 ui_cursor_move(sfc, CMDLN, view->cmd_line, 0);
878 if (prompt_l > 1) {
879 ui_bkgdset(sfc, CMDLN, &cell_nt_rev);
880 ui_waddstr(sfc, CMDLN, prompt_s);
881 ui_bkgdset(sfc, CMDLN, &cell_nt);
882 }
883 view->curx = prompt_l;
884 ui_wclrtoeol(sfc, CMDLN);
885 ui_cursor_enable_yx(sfc, CMDLN, view->cmd_line, view->curx, true);
886 pad_refresh(view);
888 ui_cursor_move(sfc, CMDLN, view->cmd_line, view->curx + 1);
890 uint flin = view->cmd_line;
891 uint fcol = prompt_l + 1;
892 uint flen = view->cols - prompt_l;
893 c = cf_accept(sfc, CMDLN, view->cmd_arg, flin, fcol, flen);
894 return c;
895}
896/** @brief Build Prompt String
897 * @ingroup view_engine
898 @param view Pointer to the View structure containing the state and
899 parameters of the view application. This structure is used to access and
900 modify the state of the application as needed.
901 @details
902 This function constructs a prompt string that provides information about
903 the current state of the view application. The prompt includes details such
904 as the file name, current column, file number, file position, and whether
905 the end of the document has been reached. The constructed prompt string is
906 stored in the view->prompt_str buffer for display to the user.
907 @note The prompt string is built based on the current state of the view
908 application, and segments that are not relevant will be omitted. Less
909 relevant segments will be omitted if the length of the prompt string
910 exceeds more than half the available space. The length of the prompt
911 string has a hard limit of view->cols - 4.
912 */
913void build_prompt(View *view) {
914 char tmp_str[MAXLEN];
915 uint prompt_maxlen = min(MAXLEN - 1, view->cols - 4);
916 uint prompt_l = 0;
917 // ----------------< File Name >----------------
918 if (view->f_is_pipe)
919 strnz__cpy(view->prompt_str, "stdin", prompt_maxlen);
920 else
921 strnz__cpy(view->prompt_str, view->file_name, prompt_maxlen);
922 // ----------------< Columns >----------------
923 if (view->pmincol > 0) {
924 sprintf(tmp_str, _("Col %d of %d"), view->pmincol, view->maxcol);
925 if (view->prompt_str[0] != '\0')
926 strnz__cat(view->prompt_str, "|", prompt_maxlen);
927 strnz__cat(view->prompt_str, tmp_str, prompt_maxlen);
928 }
929 // ----------------< File Number >----------------
930 if (view->argc > 1) {
931 prompt_l = (uint)strlen(view->prompt_str);
932 if (prompt_l > (view->cols - 4) / 2)
933 return;
934 if (view->argc > 0) {
935 sprintf(tmp_str, _("File %d of %d"), view->curr_argc + 1, view->argc);
936 if (view->prompt_str[0] != '\0') {
937 strnz__cat(view->prompt_str, "|", prompt_maxlen);
938 strnz__cat(view->prompt_str, tmp_str, prompt_maxlen);
939 }
940 }
941 }
942 // ----------------< File Position >----------------
943 prompt_l = (uint)strlen(view->prompt_str);
944 if (prompt_l > (view->cols - 4) / 2)
945 return;
947 if (view->page_top_pos == NULL_POSITION)
948 view->page_top_pos = view->file_size;
949 view->page_bot_pos = view->ln_tbl[view->page_bot_ln_no + 1];
950 if (view->page_bot_pos == NULL_POSITION)
951 view->page_bot_pos = view->file_size;
952 sprintf(tmp_str, _("Pos %zd-%zd"), view->page_top_pos, view->page_bot_pos);
953 if (view->prompt_str[0] != '\0') {
954 strnz__cat(view->prompt_str, "|", prompt_maxlen);
955 strnz__cat(view->prompt_str, tmp_str, prompt_maxlen);
956 }
957 if (!view->f_is_pipe) {
958 if (view->file_size > 0) {
959 sprintf(tmp_str, _(" of %zd"), view->file_size);
960 strnz__cat(view->prompt_str, tmp_str, prompt_maxlen);
961 }
962 }
963 // ----------------< (End) >----------------
964 prompt_l = (uint)strlen(view->prompt_str);
965 if (prompt_l > (view->cols - 4) / 2)
966 return;
967 if (view->f_eod) {
968 if (view->prompt_str[0] != '\0')
969 strnz__cat(view->prompt_str, " ", prompt_maxlen);
970 strnz__cat(view->prompt_str, _("(End)"), prompt_maxlen);
971 if (view->curr_argc + 1 < view->argc) {
972 base_name(tmp_str, view->argv[view->curr_argc + 1]);
973 strnz__cpy(view->prompt_str, _(" Next File: "), prompt_maxlen);
974 strnz__cat(view->prompt_str, tmp_str, prompt_maxlen);
975 }
976 }
977}
978/** @brief Write buffer contents to files
979 @ingroup view_engine
980 @param init data structure
981 @param f_strip_ansi strip ANSI escape sequences
982 @details
983 */
984int write_view_buffer(Init *init, bool f_strip_ansi) {
985 ssize_t bytes_written = 0;
986 View *view = init->view;
987 int rc;
988 size_t l;
989 char tmp_line_s[PAD_COLS];
990 if (!f_strip_ansi) {
991 strnz__cpy(em0, _("Would you like to strip ansi escape sequences?"),
992 MAXLEN - 1);
993 strnz__cpy(em1, _("Enter Y for yes or any other key to cancel."),
994 MAXLEN - 1);
995 rc = ui_answer_yn(nullptr, em0, em1, nullptr);
996 if (rc == 'y' || rc == 'Y')
997 f_strip_ansi = true;
998 else
999 f_strip_ansi = false;
1000 }
1002 /** write the buffer */
1003 view->out_fd = open(view->out_spec, O_CREAT | O_TRUNC | O_WRONLY, 0644);
1004 if (view->out_fd == -1) {
1005 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 2);
1006 strnz__cpy(em1, _("fwrite "), MAXLEN - 1);
1008 strerror__r(errno, em2, MAXLEN - 1);
1010 return false;
1011 }
1012 bytes_written = 0;
1013 while (!view->f_eod) {
1014 off_t ln_no = 0;
1015 get_line(view, ln_no);
1016 if (f_strip_ansi)
1017 strip_ansi(tmp_line_s, view->line_in_s);
1018 else
1019 strnz__cpy(tmp_line_s, view->line_in_s, MAXLEN - 1);
1020 l = strnlf(tmp_line_s, PAD_COLS - 1);
1021 bytes_written += write(view->out_fd, tmp_line_s, l);
1022 if (ln_no >= view->ln_tbl_size - 1)
1023 break;
1024 }
1025 close(view->out_fd);
1027 return bytes_written;
1028}
1029/** @brief Concatenate File to Standard Output
1030 @ingroup view_engine
1031 */
1032void cat_file(View *view) {
1033 int c;
1034 while (1) {
1035 get_next_char();
1036 if (view->f_eod)
1037 break;
1038 putchar(c);
1039 }
1040}
1041/** @brief Send File to Print Queue
1042 @ingroup view_engine
1043 @param view Pointer to the View structure containing the state and
1044 @param PrintFile - file to print */
1045void lp(View *view, char *PrintFile) {
1046 char *print_cmd_ptr;
1047 char shell_cmd_spec[MAXLEN];
1048 print_cmd_ptr = getenv("PRINTCMD");
1049 if (print_cmd_ptr == nullptr || *print_cmd_ptr == '\0')
1050 print_cmd_ptr = PRINTCMD;
1051 ssnprintf(shell_cmd_spec, MAXLEN - 1, "%s %s", print_cmd_ptr, PrintFile);
1052 display_prompt(view, shell_cmd_spec);
1053 shell(shell_cmd_spec);
1054}
1055/** @defgroup view_navigation View Navigation
1056 @brief Navigation functions for the view application
1057 */
1058/** @brief Go to Mark
1059 @ingroup view_navigation
1060 @param view Pointer to the View structure containing the state and
1061 parameters of the view application. This structure is used to access and
1062 modify the state of the application as needed.
1063 @param c The character representing the mark to go to. This character is
1064 typically a lowercase letter (a-z) corresponding to a mark that has been
1065 set previously in the view application. The function will attempt to
1066 navigate to the position associated with this mark, allowing the user to
1067 quickly jump to specific locations in the file based on the marks they
1068 have set. If the mark is not set, an error message will be displayed to
1069 the user.
1070 */
1071void go_to_mark(View *view, uint c) {
1072 if (c == '\'')
1073 view->file_pos = view->mark_tbl[(NMARKS - 1)];
1074 else
1075 view->file_pos = view->mark_tbl[c - 'a'];
1076 if (view->file_pos == NULL_POSITION)
1077 ui_perror(_("Mark not set"));
1078 else
1080}
1081/** @brief Search for Regular Expression Pattern
1082 @ingroup view_navigation
1083 @param view Pointer to View Structure
1084 @param search_cmd Search Command Character ('/' or '?')
1085 @param regex_pattern Regular Expression Pattern to Search For
1086 @returns true if a match is found and displayed, false if the search
1087 completes without finding a match or if an error occurs
1088 @details The search performs extended regular expression matching, ignoring
1089 ANSI sequences and Unicode characters. Matches are highlighted on the
1090 screen, and the search continues until the page is full or the end of the
1091 file is reached. If the search wraps around the file, a message is
1092 displayed indicating that the search is complete.
1093 The search state is maintained in the view structure, allowing for
1094 repeat searches and tracking of the current search line. This
1095 function highlights all matches in the current ncurses pad, including
1096 those not displayed on the screen, and tracks the first and last match
1097 columns for prompt display.
1098 ANSI sequences and Unicode characters are stripped before
1099 matching, so matching corresponds to the visual display
1100 __search */
1101bool search(View *view, int search_cmd, char *regex_pattern) {
1102 char tmp_str[MAXLEN];
1103 int REG_FLAGS = 0;
1104 regmatch_t pmatch[1];
1105 regex_t compiled_regex;
1106 int reti;
1107 uint line_offset;
1108 uint line_len;
1109 uint match_idx;
1110 uint match_len;
1111 mbstate_t mbstate;
1112 memset(&mbstate, 0, sizeof(mbstate));
1113#ifdef UAL_UI
1114 wchar_t wstr[2] = {L'\0', L'\0'};
1115 attr_t attrs;
1116 short cpx;
1117#endif
1118 UiCell cc = {};
1119 off_t prev_ln_no;
1120 bool f_page = false;
1121 UiSurface *sfc = view->sfc;
1122 if (*regex_pattern == '\0')
1123 return false;
1124 if (view->f_ignore_case)
1125 REG_FLAGS = REG_ICASE | REG_EXTENDED;
1126 else
1127 REG_FLAGS = REG_EXTENDED;
1128 reti = regcomp(&compiled_regex, regex_pattern, REG_FLAGS);
1129 if (reti) {
1130 ui_perror(_("Invalid pattern"));
1131 return false;
1132 }
1133 bool rc = false;
1134 while (1) {
1135 /** initialize iteration */
1136 if (search_cmd == '/') {
1137 if (view->srch_curr_ln_no == view->ln_no_max)
1138 view->srch_curr_ln_no = 0;
1139 } else {
1140 if (view->srch_curr_ln_no == 0) {
1141 if (view->ln_no_max < view->scroll_lines)
1143 else
1144 view->srch_curr_ln_no = 0;
1145 }
1146 }
1147 if (view->srch_curr_ln_no == view->srch_beg_ln_no) {
1148 if (view->f_first_iter == true) {
1149 view->f_first_iter = false;
1150 view->f_search_complete = false;
1151 if (search_cmd == '/')
1152 view->cury = 0;
1153 else
1154 view->cury = view->scroll_lines + 1;
1155 } else {
1156 view->f_search_complete = true;
1157 goto cleanup;
1158 }
1159 }
1161 /** get line to scan */
1162 if (search_cmd == '/') {
1163 if (view->cury == view->scroll_lines)
1164 goto cleanup;
1165 prev_ln_no = view->srch_curr_ln_no;
1168 } else {
1169 if (view->cury == 0)
1170 goto cleanup;
1172 prev_ln_no = view->srch_curr_ln_no;
1174 }
1175 fmt_line(view);
1176 reti = regexec(&compiled_regex, view->stripped_line_out,
1177 compiled_regex.re_nsub + 1, pmatch, REG_FLAGS);
1178 if (reti == REG_NOMATCH) {
1179 if (f_page) {
1180 /** non-matching page filler */
1181 if (search_cmd == '?')
1182 view->cury -= 2;
1183 display_line(view);
1184 if ((search_cmd == '/' && view->cury == view->scroll_lines) ||
1185 (search_cmd == '?' && view->cury == 1)) {
1186 break;
1187 }
1188 }
1189 view->srch_curr_ln_no += (search_cmd == '/') ? 1 : -1;
1190 continue;
1191 }
1192 if (reti) {
1193 char err_str[MAXLEN];
1194 regerror(reti, &compiled_regex, err_str, sizeof(err_str));
1195 strnz__cpy(tmp_str, _("Regex match failed: "), MAXLEN - 1);
1196 strnz__cat(tmp_str, err_str, MAXLEN - 1);
1197 ui_perror(tmp_str);
1198 rc = false; /* Set status */
1199 goto cleanup;
1200 }
1201 rc = true;
1202 /** Display matching lines */
1203 if (!f_page) {
1204 if (search_cmd == '/') {
1205 view->page_top_ln_no = prev_ln_no;
1206 ui_cursor_move(sfc, PAD, view->cury, 0);
1207 } else {
1208 view->page_bot_ln_no = view->ln_no;
1209 ui_cursor_move(sfc, PAD, 0, 0);
1210 }
1211 ui_wclrtobot(sfc, CMDLN);
1212 f_page = true;
1213 }
1214 if (search_cmd == '?')
1215 view->cury -= 2;
1216 bool f_first_match = true;
1217 view->first_match_x = -1;
1218 view->last_match_x = 0;
1219 line_len = strlen(view->stripped_line_out);
1220 line_offset = 0;
1221 while (1) {
1222 match_idx = line_offset + pmatch[0].rm_so;
1223 match_len = pmatch[0].rm_eo - pmatch[0].rm_so;
1224 for (uint i = match_idx; i < match_idx + match_len; i++) {
1225 cc = view->cmplx_buf[i];
1226#ifdef UAL_UI
1227 ui_get_cell(view->sfc, PAD, &cc, wstr, &attrs, &cpx, nullptr);
1228 cpx = cp_nt_rev;
1229 ui_set_cell(view->sfc, PAD, &cc, wstr, attrs, cpx, nullptr);
1230#else
1231 cc.channels = cell_nt_rev.channels;
1232#endif
1233 view->cmplx_buf[i] = cc;
1234 }
1235 if (f_first_match) {
1236 f_first_match = false;
1237 view->first_match_x = pmatch[0].rm_so;
1238 }
1239 view->last_match_x = line_offset + pmatch[0].rm_eo;
1240 line_offset += pmatch[0].rm_eo;
1241 if (line_offset >= line_len)
1242 break;
1243 view->line_out_p = view->stripped_line_out + line_offset;
1244 reti = regexec(&compiled_regex, view->line_out_p,
1245 compiled_regex.re_nsub + 1, pmatch, REG_FLAGS);
1246 if (reti == REG_NOMATCH)
1247 break;
1248 if (reti) {
1249 char msgbuf[100];
1250 regerror(reti, &compiled_regex, msgbuf, sizeof(msgbuf));
1251 sprintf(tmp_str, _("Regex match failed: %s"), msgbuf);
1252 ui_perror(tmp_str);
1253 rc = false; /* Set status */
1254 goto cleanup;
1255 }
1256 if (search_cmd == '/') {
1257 if (view->cury == view->scroll_lines - 1) {
1258 break;
1259 }
1260 } else if (view->cury == 1) {
1261 break;
1262 }
1263 }
1264 display_line(view);
1265 view->srch_curr_ln_no += (search_cmd == '/') ? 1 : -1;
1266 }
1267cleanup:
1268 regfree(&compiled_regex);
1269 view->ln_no = view->srch_curr_ln_no;
1270 return rc;
1271}
1272
1273/* --------------------------------------------------------------
1274 Navigation
1275 --------------------------------------------------------------- */
1276/** @brief display previous page
1277 @ingroup view_navigation
1278 @param view data structure
1279 @details Displays the previous page starting at (view->page_top_ln_no -
1280 view->scroll_lines).
1281 __page_prev
1282 */
1283void page_prev(View *view) {
1284 off_t ln_no;
1285 if (view->page_top_ln_no == 0)
1286 return;
1287 view->cury = 0;
1288 view->ln_no = view->page_top_ln_no;
1289 uint scroll, avail, scroll_this_line;
1290 if (view->wrap) {
1291 scroll = view->scroll_lines;
1292 ln_no = view->page_top_ln_no;
1293 while (scroll > 0) {
1294 get_line(view, ln_no);
1295 fmt_line(view);
1296 view->page_top_sl = (view->cur.sl_cnt > 1);
1297 if (view->page_top_sl == false) {
1298 scroll--;
1299 if (ln_no > 0)
1300 ln_no--;
1301 continue;
1302 }
1303 if (ln_no == view->page_top_ln_no && ln_no != view->ln_no_max) {
1305 if (view->cur.sl_idx == 0) {
1306 if (ln_no > 0)
1307 ln_no--;
1308 continue;
1309 }
1310 avail = view->cur.sl_idx;
1311 } else
1312 avail = view->cur.sl_cnt;
1313 scroll_this_line = min(scroll, avail);
1314 scroll -= scroll_this_line;
1315 if (ln_no == view->page_top_ln_no && ln_no != view->ln_no_max)
1316 view->cur.sl_idx -= scroll_this_line;
1317 else
1318 view->cur.sl_idx = view->cur.sl_cnt - scroll_this_line;
1319 if (ln_no > 0)
1320 ln_no--;
1321 }
1322 ln_no++;
1325 view->page_top_ln_no = ln_no;
1326 view->ln_no = view->page_top_ln_no;
1327 view->page_top_sl = (view->page_top_sl_cnt > 1);
1328 } else {
1329 view->ln_no = view->page_top_ln_no;
1330 view->cury = 0;
1331 if (view->ln_no - view->scroll_lines >= 0)
1332 view->ln_no -= view->scroll_lines;
1333 else
1334 view->ln_no = 0;
1335 view->page_top_ln_no = view->ln_no;
1336 }
1337 page_next(view);
1338}
1339/** @brief Advance to Next Page
1340 @ingroup view_navigation
1341 @param view data structure
1342 @details Advances from view->page_bot_pos to view the next page of
1343 content. view->page_bot_pos must be set properly when calling this
1344 function. If the current bottom position of the page is at the end of the
1345 file, the function returns without making any changes. Otherwise, it
1346 resets the maximum column and current line position to the top of the
1347 page, updates the file position to the current bottom position of the
1348 page, and sets the top position and line number of the page accordingly.
1349 Finally, it calls the function to display the new page content.
1350 __page_next
1351*/
1352void page_next(View *view) {
1353
1354 view->file_pos = view->ln_tbl[view->ln_no];
1355 if (view->file_pos == view->file_size)
1356 return;
1357 view->maxcol = 0;
1358 view->cury = 0;
1359 view->page_top_ln_no = view->ln_no;
1361}
1362/** @brief Display Current Page
1363 @ingroup view_display
1364 @param view data structure
1365 __display_page
1366 */
1367void view_display_page(View *view) {
1368 view->cury = 0;
1369 view->page_top_sl = false;
1370 view->page_bot_sl = false;
1371 UiSurface *sfc = view->sfc;
1372 ui_cursor_move(sfc, PAD, 0, 0);
1373 if (view->f_ln)
1374 ui_cursor_move(sfc, LNNO, 0, 0);
1375 view->ln_no = view->page_top_ln_no;
1376 view->page_top_ln_no = 0;
1377 view->page_bot_ln_no = 0;
1378 while (view->cury < view->scroll_lines) {
1379 get_line(view, view->ln_no);
1380 if (view->f_eod) {
1381 view->ln_no_max = view->ln_no;
1382 break;
1383 }
1384 fmt_line(view);
1385 if (view->wrap && view->cury == 0)
1387 display_line(view);
1388#ifdef DEBUG_IMMED_UI
1389 ui_render();
1390 pad_refresh(view);
1391#endif
1392 view->ln_no++;
1393 }
1394 if (view->f_eod) {
1395 view->ln_no_max = view->ln_no;
1397 }
1398 view->ln_no--;
1399 view->page_bot_end_pos = view->file_pos - 1;
1400 view->page_bot_pos = view->file_pos;
1401 view->page_bot_ln_no = view->ln_no;
1402 // ui_render();
1403 // pad_refresh(view);
1404}
1405/** @brief Display Line on Pad
1406 @ingroup view_display
1407 param View *view data structure
1408 @details This function displays a single line of text on the ncurses
1409 pad.
1410 If line numbering is enabled (view->f_ln), it is formatted and
1411 displayed at the beginning of the line with the specified attributes and
1412 color pair.
1413 @details Because get_next_char calls increment_ln upon encountering a
1414 line feed and increment_ln advances view->ln_no after updating the line
1415 table, the line number displayed is one greater than the index to the
1416 line table. That means the line counter begins with 1, while the table
1417 origin is 0.
1418 __display_line
1419 */
1420void display_line(View *view) {
1421 char ln_s[16];
1422
1423 UiSurface *sfc = view->sfc;
1424 if (view->wrap && view->cur.sl_cnt > 0) {
1425 if (view->cur.sl_idx < view->cur.sl_cnt) {
1427 return;
1428 }
1429 }
1430 if (view->cury > view->scroll_lines - 1)
1431 view->cury = view->scroll_lines - 1;
1432 if (view->f_ln) {
1433 ssnprintf(ln_s, 8, "%7jd", view->ln_no);
1434 ui_cursor_move(sfc, LNNO, view->cury, 0);
1435 ui_wclrtoeol(sfc, LNNO);
1436 ui_mvwaddstr(sfc, LNNO, view->cury, 0, ln_s);
1437 }
1438 ui_cursor_move(sfc, PAD, view->cury, 0);
1439 ui_wclrtoeol(sfc, PAD);
1440#ifdef UAL_UI
1441 ui_mvwadd_wchstr(sfc, PAD, view->cury, 0, view->cmplx_buf);
1442#else
1443 ui_mvwadd_wchstr(sfc, PAD, view->cury, 0, (struct nccell *)view->cmplx_buf);
1444#endif
1445 if (view->cury == 0)
1446 view->page_top_ln_no = view->ln_no;
1447 if (view->cury == view->scroll_lines - 1)
1448 view->page_bot_ln_no = view->ln_no;
1449 view->cury++;
1450}
1451/** @brief Display Split Line on Pad
1452 @ingroup view_display
1453 @param view data structure
1454 @details This function is used to display a line that has been split into
1455 multiple segments due to wrapping. It iterates through the segments of the
1456 current line (stored in view->cur.sl_cc and view->cur.sl_cells) and
1457 displays each segment on the pad, along with the corresponding line number
1458 if line numbering is enabled. The function also updates the top and bottom
1459 line numbers of the page based on the current position in the split line.
1460 */
1461void display_split_line(View *view) {
1462 char ln_s[16];
1463 UiSurface *sfc = view->sfc;
1464 if (view->cury > view->scroll_lines - 1)
1465 view->cury = view->scroll_lines - 1;
1466
1467 for (uint i = view->cur.sl_idx; i < view->cur.sl_cnt; i++) {
1468 if (i == 0) {
1469 if (view->f_ln) {
1470 ssnprintf(ln_s, 8, "%7jd", view->ln_no);
1471 ui_cursor_move(sfc, LNNO, view->cury, 0);
1472 ui_wclrtoeol(sfc, LNNO);
1473 ui_mvwaddstr(sfc, LNNO, view->cury, 0, ln_s);
1474 }
1475 } else {
1476 if (view->f_ln) {
1477 ui_cursor_move(sfc, LNNO, view->cury, 0);
1478 ui_wclrtoeol(sfc, LNNO);
1479 }
1480 }
1481 ui_cursor_move(sfc, PAD, view->cury, 0);
1482 ui_wclrtoeol(sfc, PAD);
1483 ui_wadd_wchnstr(sfc, PAD, view->cur.sl_cc[i], view->cur.sl_cells[i]);
1484 if (view->cury == 0) {
1485 view->page_top_ln_no = view->ln_no;
1486 view->page_top_sl = true;
1487 view->page_top_sl_idx = i;
1489 }
1490 if (view->cury == view->scroll_lines - 1) {
1491 view->page_bot_ln_no = view->ln_no;
1492 view->page_bot_sl = true;
1493 view->page_bot_sl_idx = i;
1495 view->page_bot_ln_no = view->ln_no;
1496 }
1497 view->cury++;
1498 if (view->cury == view->scroll_lines)
1499 break;
1500 }
1501}
1502/** @brief Display End of Data
1503 @ingroup view_display
1504 @param view data structure
1505 @details This function is called when the end of the file is reached and
1506 there are no more lines to display. It clears the remaining lines in the
1507 pad and line number window (if line numbering is enabled) to ensure that
1508 no residual content from previous lines is displayed.
1509 */
1510void display_line_eod(View *view) {
1511 UiSurface *sfc = view->sfc;
1512 if (view->f_ln) {
1513 ui_cursor_move(sfc, LNNO, view->cury, 0);
1514 ui_wclrtobot(sfc, LNNO);
1515 }
1516 ui_cursor_move(sfc, PAD, view->cury, 0);
1517 ui_wclrtobot(sfc, PAD);
1518 view->page_bot_ln_no = view->ln_no;
1519 view->ln_no_max = view->ln_no;
1520}
1521/** @brief Scroll Down by n Lines
1522 @ingroup view_navigation
1523 @param view Pointer to the View structure containing the state and
1524 parameters of the view application. This structure is used to access and
1525 modify the state of the application as needed.
1526 @param n add n to page line pointers, moving the scope of the view window toward the end of data (eod).
1527 __scroll_next */
1528void scroll_next(View *view, uint n) {
1529 uint scroll, scroll_this_line, avail;
1530 off_t ln_no;
1531 UiSurface *sfc = view->sfc;
1532 view->f_bod = false;
1533 view->ln_no = view->page_bot_ln_no;
1534 if (view->wrap) {
1535 // Set Top Line State
1536 scroll = n;
1537 ln_no = view->page_top_ln_no;
1538 while (scroll > 0) {
1539 get_line(view, ln_no);
1540 if (view->f_eod) {
1541 view->ln_no = ln_no;
1542 view->ln_no_max = ln_no;
1543 break;
1544 }
1545 fmt_line(view);
1546 view->page_top_sl = (view->cur.sl_cnt > 1);
1547 if (view->page_top_sl == false) {
1548 ln_no++;
1549 scroll--;
1550 continue;
1551 }
1552 if (ln_no == view->page_top_ln_no)
1554 avail = view->cur.sl_cnt - 1 - view->cur.sl_idx;
1555 scroll_this_line = min(scroll, avail);
1556 scroll -= scroll_this_line;
1557 if (ln_no == view->page_top_ln_no)
1558 view->cur.sl_idx += scroll_this_line;
1559 else
1560 view->cur.sl_idx += scroll_this_line - 1;
1561 if (scroll != 0)
1562 ln_no++;
1563 }
1566 view->page_top_ln_no = ln_no;
1567 view->page_top_sl = (view->cur.sl_cnt > 1);
1568 // Set Bottom Line State
1569 scroll = n;
1570 ln_no = view->page_bot_ln_no;
1571 while (scroll > 0) {
1572 get_line(view, ln_no);
1573 if (view->f_eod) {
1574 view->ln_no_max = view->ln_no;
1575 break;
1576 }
1577 fmt_line(view);
1578 view->page_bot_sl = (view->cur.sl_cnt > 1);
1579 if (view->page_bot_sl == false) {
1580 ln_no++;
1581 scroll--;
1582 continue;
1583 }
1584 if (ln_no == view->page_bot_ln_no)
1586 avail = view->cur.sl_cnt - 1 - view->cur.sl_idx;
1587 if (avail == 0) {
1588 ln_no++;
1589 continue;
1590 }
1591 scroll_this_line = min(scroll, avail);
1592 scroll -= scroll_this_line;
1593 if (ln_no == view->page_bot_ln_no)
1594 view->cur.sl_idx += scroll_this_line;
1595 else
1596 view->cur.sl_idx += scroll_this_line - 1;
1599 view->page_bot_ln_no = ln_no;
1600 view->page_bot_sl = (view->cur.sl_cnt > 1);
1601 view->ln_no = view->page_bot_ln_no;
1602 if (view->f_ln)
1603 ui_wscrl(sfc, LNNO, 1);
1604 ui_wscrl(sfc, PAD, scroll_this_line);
1605 view->cury = view->scroll_lines - scroll_this_line;
1606 ui_cursor_move(sfc, PAD, view->cury, 0);
1607 display_line(view);
1608 if (scroll != 0)
1609 ln_no++;
1610 }
1611 } else {
1612 view->ln_no = view->page_bot_ln_no;
1613 if (view->ln_no_max > 0 && view->ln_no >= view->ln_no_max)
1614 return;
1615 if (view->f_eod) {
1616 view->ln_no_max = view->ln_no;
1617 return;
1618 }
1619 if (n > view->scroll_lines) {
1620 if (view->f_ln) {
1621 ui_cursor_move(sfc, LNNO, 0, 0);
1622 ui_wclrtobot(sfc, LNNO);
1623 }
1624 ui_cursor_move(sfc, PAD, 0, 0);
1625 ui_wclrtobot(sfc, PAD);
1626 } else {
1627 if (view->ln_no_max == 0 || view->ln_no < view->ln_no_max) {
1628 if (view->f_ln)
1629 ui_wscrl(sfc, LNNO, n);
1630 ui_wscrl(sfc, PAD, n);
1631 if (n < view->scroll_lines)
1632 view->cury = view->scroll_lines - n;
1633 }
1634 }
1635 view->page_top_ln_no += n;
1636 scroll = n;
1637 while (scroll > 0) {
1638 if (view->ln_no_max > 0 && view->ln_no >= view->ln_no_max)
1639 break;
1640 view->ln_no++;
1641 get_line(view, view->ln_no);
1642 if (view->f_eod) {
1643 view->ln_no--;
1644 view->ln_no_max = view->ln_no;
1645 break;
1646 }
1647 fmt_line(view);
1648 ui_cursor_move(sfc, PAD, view->cury, 0);
1649 display_line(view);
1650 if (view->cury == view->scroll_lines)
1651 break;
1652 }
1653 view->page_bot_ln_no = view->ln_no;
1654 }
1655 return;
1656}
1657/** @brief Move the view scope n lines toward the beginning of the file.
1658 * @ingroup view_navigation
1659 @param view Pointer to the View structure containing the state and
1660 parameters of the view application. This structure is used to access and
1661 modify the state of the application as needed.
1662 @param n subtract n from page line pointers, moving the scope of the view window toward the beginning of data (bod).
1663 __scroll_prev
1664 */
1665void scroll_prev(View *view, uint n) {
1666 uint scroll, avail, scroll_this_line;
1667 off_t ln_no;
1668 view->f_eod = false;
1669 if (view->page_top_ln_no == 0) {
1670 if (view->wrap) {
1671 if (view->page_top_sl == false || view->page_top_sl_idx == 0)
1672 return;
1673 } else
1674 return;
1675 }
1676 UiSurface *sfc = view->sfc;
1677 if (n > view->scroll_lines) {
1678 if (view->f_ln) {
1679 ui_cursor_move(sfc, LNNO, 0, 0);
1680 ui_wclrtobot(sfc, LNNO);
1681 }
1682 ui_cursor_move(sfc, PAD, 0, 0);
1683 ui_wclrtobot(sfc, PAD);
1684 } else {
1685 if (view->f_ln)
1686 ui_wscrl(sfc, LNNO, -n);
1687 ui_wscrl(sfc, PAD, -n);
1688 view->page_bot_ln_no -= n;
1689 }
1690 ui_cursor_move(sfc, PAD, 0, 0);
1691 if (view->wrap) {
1692 scroll = n;
1693 ln_no = view->page_top_ln_no;
1694 while (scroll > 0) {
1695 get_line(view, ln_no);
1696 if (view->f_bod)
1697 break;
1698 fmt_line(view);
1699 view->page_top_sl = (view->cur.sl_cnt > 1);
1700 if (view->page_top_sl == false) {
1701 scroll--;
1702 if (ln_no > 0)
1703 ln_no--;
1704 continue;
1705 }
1706 if (ln_no == view->page_top_ln_no) {
1708 if (view->cur.sl_idx == 0) {
1709 if (ln_no > 0)
1710 ln_no--;
1711 continue;
1712 }
1713 avail = view->cur.sl_idx;
1714 } else
1715 avail = view->cur.sl_cnt;
1716 scroll_this_line = min(scroll, avail);
1717 scroll -= scroll_this_line;
1718 if (ln_no == view->page_top_ln_no)
1719 view->cur.sl_idx -= scroll_this_line;
1720 else
1721 view->cur.sl_idx = view->cur.sl_cnt - scroll_this_line;
1722 if (scroll == 0)
1723 break;
1724 }
1727 view->page_top_ln_no = ln_no;
1728 view->page_top_sl = (view->page_top_sl_cnt > 1);
1729
1730 view->cury = 0;
1731 scroll = n;
1732 while (scroll > 0) {
1733 if (view->ln_no != view->page_top_ln_no) {
1734 get_line(view, view->ln_no);
1735 if (view->f_eod) {
1736 view->ln_no_max = view->ln_no;
1737 break;
1738 }
1739 fmt_line(view);
1740 }
1741 display_line(view);
1742 scroll--;
1743 view->ln_no++;
1744 }
1745 view->ln_no--;
1746
1747 // Set Bottom Line State
1748 scroll = n;
1749 ln_no = view->page_bot_ln_no;
1750 while (scroll > 0) {
1751 get_line(view, ln_no);
1752 if (view->f_bod)
1753 break;
1754 fmt_line(view);
1755 view->page_bot_sl = (view->cur.sl_cnt > 1);
1756 if (view->page_bot_sl == false) {
1757 scroll--;
1758 if (ln_no > 0)
1759 ln_no--;
1760 continue;
1761 }
1762 if (ln_no == view->page_bot_ln_no) {
1764 if (view->cur.sl_idx == 0) {
1765 ln_no--;
1766 continue;
1767 }
1768 avail = view->cur.sl_idx;
1769 } else
1770 avail = view->cur.sl_cnt;
1771 scroll_this_line = min(scroll, avail);
1772 scroll -= scroll_this_line;
1773 if (ln_no == view->page_bot_ln_no)
1774 view->cur.sl_idx -= scroll_this_line;
1775 else
1776 view->cur.sl_idx = view->cur.sl_cnt - scroll_this_line;
1777 if (ln_no > 0)
1778 ln_no--;
1779 }
1780 ln_no++;
1783 view->page_bot_ln_no = ln_no;
1784 view->page_bot_sl = (view->cur.sl_cnt > 1);
1785 } else {
1786 view->cury = 0;
1787 scroll = n;
1788 while (scroll > 0) {
1789 view->ln_no = view->page_top_ln_no - scroll;
1790 if (view->ln_no < 0)
1791 view->ln_no = 0;
1792 get_line(view, view->ln_no);
1793 if (view->f_eod) {
1794 view->ln_no_max = view->ln_no;
1795 break;
1796 }
1797 fmt_line(view);
1798 display_line(view);
1799 scroll--;
1800 view->ln_no++;
1801 }
1802 view->ln_no--;
1803 }
1804 return;
1805}
1806
1807/** __get_line
1808 */
1809void get_line(View *view, off_t line) {
1810 // if (view->f_eod)
1811 // return;
1812 view->ln_no = line;
1813 view->file_pos = view->ln_tbl[view->ln_no];
1814 char c;
1815 char *line_in_p;
1816
1817 view->f_eod = false;
1818 get_next_char();
1819 if (view->f_eod) {
1820 view->ln_no = line;
1821 view->ln_no_max = view->ln_no;
1822 return;
1823 }
1824 line_in_p = view->line_in_s;
1825 view->line_in_beg_p = view->line_in_s;
1826 view->line_in_end_p = view->line_in_s + PAD_COLS - 1;
1827 while (1) {
1828 if (c == '\n' || c == '\0')
1829 break;
1830 if (line_in_p > view->line_in_end_p) {
1831 *line_in_p = '\0';
1832 increment_ln(view);
1833 view->ln_no_max = view->ln_no;
1834 break;
1835 }
1836 *line_in_p++ = c;
1837 get_next_char();
1838 if (view->f_eod) {
1839 view->ln_no = line;
1840 view->ln_no_max = view->ln_no;
1841 return;
1842 }
1843 }
1844 *line_in_p = '\0';
1845 if (view->f_squeeze) {
1846 while (1) {
1847 get_next_char();
1848 if (c != '\n' && c != '\0')
1849 break;
1850 if (view->f_eod) {
1851 view->ln_no = line;
1852 view->ln_no_max = view->ln_no;
1853 break;
1854 }
1855 }
1856 if (!view->f_eod) {
1857 get_prev_char();
1858 return;
1859 }
1860 }
1861
1862 view->ln_no = line;
1863 return;
1864}
1865/** @brief Go to End of File
1866 @ingroup view_navigation
1867 @param view data structure
1868 __go_to_eof
1869 */
1870void go_to_eof(View *view) {
1871 sync_pos(view, view->file_size);
1872 view->ln_no--;
1873 view->ln_no_max = view->ln_no;
1874 if (view->wrap) {
1875 view->page_top_ln_no = view->ln_no;
1876 view->f_eod = true;
1877 page_prev(view);
1878 return;
1879 }
1880 if (view->ln_no > view->scroll_lines)
1881 view->ln_no -= view->scroll_lines - 1;
1882 // else
1883 // view->page_top_ln_no = 0;
1884 view->page_top_ln_no = view->ln_no;
1885 // view->page_top_pos = view->ln_tbl[view->ln_no];
1886 // view->page_bot_pos = view->page_top_pos;
1887 // view->file_pos = view->page_top_pos;
1888 view->cury = 0;
1889 page_next(view);
1890}
1891/** @brief Go to Percent of File
1892 @ingroup view_navigation
1893 @param view data structure
1894 @param percent of file
1895*/
1896void go_to_percent(View *view, uint percent) {
1897 off_t pos;
1898 if (view->file_size < 0) {
1899 ui_perror(_("Cannot determine file length"));
1900 return;
1901 }
1902 pos = (percent * view->file_size) / 100;
1903 go_to_position(view, pos);
1904}
1905/** @brief Go to Specific Line
1906 @ingroup view_navigation
1907 @param view data structure
1908 @param line_idx line number to go to (1-based index)
1909 @returns 0 on success, EOF if line index is out of bounds or end of data
1910 is reached
1911 */
1912int go_to_line(View *view, off_t line_idx) {
1913 if (line_idx < 0 || line_idx > view->ln_tbl_size - 1) {
1914 ui_perror(_("Line number out of bounds"));
1915 return EOF;
1916 }
1917 sync_ln(view, line_idx);
1918 view->page_top_pos = view->file_pos;
1919 view->page_bot_pos = view->file_pos;
1920 // view->file_pos = view->page_top_pos;
1921 page_next(view);
1922 return 0;
1923}
1924/** @brief Go to Specific File Position
1925 @ingroup view_navigation
1926 @param view data Structure
1927 @param go_to_pos
1928*/
1929void go_to_position(View *view, off_t pos) {
1930 sync_pos(view, pos);
1931 page_next(view);
1932}
1933/** @brief Get Line Number for a Given File Position
1934 @ingroup view_navigation
1935 @param view data structure
1936 @param target file position
1937 @returns line number corresponding to the given file position
1938 @details This function performs a binary search on the line table (view->ln_tbl)
1939 to find the line number corresponding to the specified file position (target).
1940 It returns the index of the line in the line table that is closest to the target
1941 position without exceeding it. If the target position is not found, it returns
1942 the previous line number.
1943 */
1944off_t line_number(View *view, off_t position) {
1945 off_t low = 0;
1946 off_t high = view->ln_tbl_size - 1;
1947 off_t prev_ln_no = 0;
1948 while (low <= high) {
1949 off_t guess = low + (high - low) / 2;
1950 if (view->ln_tbl[guess] == position)
1951 return guess;
1952 if (view->ln_tbl[guess] < position) {
1953 prev_ln_no = guess;
1954 low = guess + 1;
1955 } else
1956 high = guess - 1;
1957 }
1958 return prev_ln_no;
1959}
1960/** @brief Initialize Line Table
1961 @ingroup view_navigation
1962 @param view data structure
1963 @details The line table is initialized with a specified increment size
1964 (LINE_TBL_INCR). Memory is allocated for the line table, and the first
1965 entry is set to 0, indicating the file position of the first line. The
1966 line index (view->ln_no) is initialized to 0.
1967 */
1968void initialize_line_table(View *view) {
1970 view->ln_tbl = (off_t *)calloc(view->ln_tbl_size, sizeof(off_t));
1971 if (view->ln_tbl == nullptr) {
1972 ui_perror(_("Memory allocation failed"));
1973 exit(EXIT_FAILURE);
1974 }
1975 view->ln_pos_max = 0;
1976 view->ln_tbl[0] = 0;
1977 view->ln_no = 0;
1978}
1979/** @brief Destroy Line Table
1980 @ingroup view_navigation
1981 @param view data structure
1982 @details This function frees the memory allocated for the line table and
1983 resets the relevant fields in the View structure. It ensures that the line
1984 table is properly cleaned up when it is no longer needed, preventing memory
1985 leaks.
1986 */
1987void destroy_line_table(View *view) {
1988 if (view->ln_tbl == nullptr)
1989 return;
1990 free(view->ln_tbl);
1991 view->ln_tbl = nullptr;
1992 view->ln_tbl_size = 0;
1993 view->ln_pos_max = 0;
1994 view->ln_no = 0;
1995}
1996/** @brief Increment Line Index and Update Line Table
1997 @ingroup view_navigation
1998 @param view data structure
1999 @details This function is called when a line feed character is
2000 encountered while reading the file. It increments the line index
2001 (view->ln_no) and checks if the current file position exceeds the maximum
2002 position recorded in the line table. If it does, it updates the line
2003 table with the new file position. If the line index exceeds the current
2004 size of the line table, the table is resized by allocating more memory.
2005 */
2006void increment_ln(View *view) {
2007 // view->ln_tbl[0] is set to 0 in initialize_line_table
2008 // view->ln_tbl[1] is the second line
2009 view->ln_no++;
2010 if (view->file_pos <= view->ln_pos_max)
2011 return;
2012 if (view->ln_no > view->ln_tbl_size - 1) {
2014 view->ln_tbl =
2015 (off_t *)realloc(view->ln_tbl, view->ln_tbl_size * sizeof(off_t));
2016 if (view->ln_tbl == nullptr) {
2017 ui_perror(_("Memory allocation failed"));
2018 exit(EXIT_FAILURE);
2019 }
2020 }
2021 view->ln_no_max = view->ln_no;
2022 view->ln_pos_max = view->file_pos;
2023 view->ln_tbl[view->ln_no] = view->file_pos;
2024}
2025/** @brief Synchronize Line Table with Current File Position
2026 @ingroup view_navigation
2027 @param view data Structure
2028 @details The line table (view->ln_tbl) is an array that stores the file
2029 position of each line. The index (view->ln_no + 1) corresponds to the
2030 current line number.
2031 @details If the line or position requested is not in the line table, this
2032 function reads forward to sycn.
2033 If the line or positione requested is behind the current line
2034 table index, the line index will be decremented until it matches the file
2035 position.
2036 __sync_ln
2037 */
2038void sync_ln(View *view, off_t ln_no) {
2039 int c = 0;
2040 // Advance view->ln_no_max to view->ln_no if necessary
2041 if (ln_no < view->ln_no_max) {
2042 view->file_pos = view->ln_tbl[ln_no];
2043 view->ln_no = ln_no;
2044 return;
2045 }
2046 while (view->ln_no_max < ln_no) {
2047 get_next_char();
2048 if (view->f_eod)
2049 break;
2050 }
2051}
2052void sync_pos(View *view, off_t pos) {
2053 int c = 0;
2054 // Advance view->ln_tbl[view->ln_no] to view->file_pos if necessary
2055 while (view->ln_tbl[view->ln_no] < pos) {
2056 if (view->ln_no >= view->ln_no_max) {
2057 get_next_char();
2058 if (view->f_eod)
2059 break;
2060 } else
2061 view->ln_no++;
2062 }
2063 while (view->ln_tbl[view->ln_no] > pos) {
2064 if (view->ln_no == 0)
2065 break;
2066 view->ln_no--;
2067 }
2068 view->file_pos = view->ln_tbl[view->ln_no];
2069}
2070/*------------------------------------------------------------
2071 END NAVIGATION
2072 BEGIN DISPLAY
2073 ------------------------------------------------------------*/
2074/** @defgroup view_display Manage View Display
2075 @brief Manage the View Display
2076 */
2077/** @brief Refresh Pad and Line Number Window
2078 @ingroup view_display
2079 @param view data structure
2080 @returns OK on success, ERR on failure
2081 __pad_refresh
2082*/
2083int pad_refresh(View *view) {
2084 int rc = 0;
2085 // touchwin(view->sfc->mwin[WIN2]);
2086 // rc = pnoutrefresh(view->sfc->mwin[WIN2], view->pminrow, view->pmincol,
2087 // view->sminrow,
2088 // view->smincol, view->smaxrow, view->smaxcol);
2089 rc = ui_prefresh(view->sfc,
2090 PAD,
2091 view->pminrow,
2092 view->pmincol,
2093 view->sminrow,
2094 view->smincol,
2095 view->smaxrow,
2096 view->smaxcol);
2097 if (rc == ERR) {
2098 ui_log(ERROR, _("%s:%d prefresh(view->sfc->mwin[WIN2], pminrow=%d, pmincol=%d, smaxrow=%d, smaxcol=%d) returned %d\n"), __FILE__, __LINE__ - 1, view->pminrow, view->pmincol, view->smaxrow, view->smaxcol, rc);
2099 }
2100#ifdef IMMEDOK
2101 ui_render();
2102#endif
2103 return 0;
2104}
2105
2106/** @brief Format Line for Display
2107 @ingroup view_display
2108 @param view pointer to View structure containing line input and output
2109 buffers
2110 @return length of formatted line in characters
2111 @details This function processes the input line from view->line_in_s,
2112 handling ANSI escape sequences for text attributes and colors, as well as
2113 multi-byte characters. It converts the input line into a formatted line
2114 suitable for display in the terminal, storing the result in
2115 view->cmplx_buf and view->stripped_line_out. The function returns the
2116 length of the formatted line in characters, which may be used for
2117 tracking the maximum column width of the displayed content.
2118 __fmt_line
2119 */
2120int fmt_line(View *view) {
2121 char ansi_tok[MAXLEN];
2122 uint i = 0, j = 0, x = 0;
2123 uint len = 0;
2124 uint tab_spaces = 0;
2125 int char_width;
2126 attr_t attrs = WA_NORMAL;
2127 ushort cpx = cp_nt;
2128 UiCell cc = {};
2129 wchar_t wstr[2] = {L'\0', L'\0'};
2130 char *in_str = view->line_in_s;
2131 uint sl_cols = 0;
2132 uint sl_cells = 0;
2133 uint word_cells = 0;
2134 UiCell *cmplx_buf = view->cmplx_buf;
2135 view->cur.sl_idx = 0;
2136 view->cur.sl_cnt = 0;
2137 view->cur.sl_cols[0] = 0;
2138 view->cur.sl_cells[0] = 0;
2139 view->cur.sl_s[0] = nullptr;
2140 view->cur.sl_cc[0] = nullptr;
2141 char *sl_s = view->stripped_line_out;
2142 UiCell *sl_cc = view->cmplx_buf;
2144 mbstate_t mbstate;
2145 memset(&mbstate, 0, sizeof(mbstate));
2146 uint word_cols = 0;
2147 uint sl_maxlen = PAD_COLS - 1;
2148 if (view->f_eod) {
2149 view->ln_no_max = view->ln_no;
2150 return 0;
2151 }
2152 if (view->wrap)
2153 sl_maxlen = view->cols;
2154 if (view->f_ln)
2155 sl_maxlen -= view->ln_win_cols;
2156 memset(view->stripped_line_out, 0, sizeof(view->stripped_line_out));
2157 while (in_str[i] != '\0' && i <= PAD_COLS - 1) { // line
2158 while (1) { // ANSI SGR, Character, and Word
2159 if (in_str[i] == '\033') { // ANSI SGR
2160 if (in_str[i + 1] == '[') {
2161 len = strcspn(&in_str[i], "mK ") + 1;
2162 memcpy(ansi_tok, &in_str[i], len + 1);
2163 ansi_tok[len] = '\0';
2164 if (ansi_tok[0] == '\0') {
2165 if (i + 2 < MAXLEN)
2166 i += 2;
2167 continue;
2168 }
2169 if (len == 0 || in_str[i + len - 1] == ' ') {
2170 i += 2;
2171 continue;
2172 } else if (in_str[i + len - 1] == 'K') {
2173 i += len;
2174 continue;
2175 }
2176 parse_ansi_str(ansi_tok, &attrs, &cpx);
2177 i += len;
2178 } else {
2179 i++;
2180 continue;
2181 }
2182 } else { // Character and Word
2183 if (in_str[i] == ' ' || in_str[i] == '-') {
2184 if (view->wrap && (sl_cols + word_cols) > (sl_maxlen - 1))
2185 break;
2186 (in_str[i] == '-') ? (wstr[0] = L'-') : (wstr[0] = L' ');
2187 wstr[1] = L'\0';
2188 ui_set_cell(view->sfc, PAD, &cc, wstr, attrs, cpx, nullptr);
2189 view->stripped_line_out[x++] = in_str[i];
2190 cmplx_buf[j++] = cc;
2191 if (view->wrap) {
2192 sl_cols += word_cols + 1;
2193 word_cols = 0;
2194 sl_cells += word_cells + 1;
2195 word_cells = 0;
2196 }
2197 i++;
2198 continue;
2199 }
2200 if (in_str[i] == '\0') {
2201 if (view->wrap) {
2202 if (sl_cols + word_cols + 1 > sl_maxlen - 1)
2203 break;
2204 sl_cols += word_cols;
2205 word_cols = 0;
2206 sl_cells += word_cells;
2207 word_cells = 0;
2208 }
2209 break;
2210 }
2211 if (in_str[i] == '\t') {
2212 tab_spaces = view->tab_stop - ((sl_cols + word_cols) % view->tab_stop);
2213 if (view->wrap && sl_cols + word_cols + tab_spaces > sl_maxlen - 1)
2214 break;
2215 wstr[0] = L' ';
2216 wstr[1] = L'\0';
2217 ui_set_cell(view->sfc, PAD, &cc, wstr, attrs, cpx, nullptr);
2218 for (uint z = 0; z < tab_spaces; z++) {
2219 view->stripped_line_out[x++] = ' ';
2220 cmplx_buf[j++] = cc;
2221 }
2222 if (view->wrap) {
2223 sl_cols += word_cols + tab_spaces;
2224 word_cols = 0;
2225 sl_cells += word_cells + tab_spaces;
2226 word_cells = 0;
2227 }
2228 i++;
2229 continue;
2230 }
2231 wstr[1] = L'\0';
2232 len = mbrtowc(wstr, &in_str[i], MB_CUR_MAX, &mbstate);
2233 if (len <= 0) {
2234 wstr[0] = L'?';
2235 wstr[1] = L'\0';
2236 len = 1;
2237 }
2238 char_width = wcwidth(wstr[0]);
2239 if (char_width < 0) {
2240 char_width = 1;
2241 wstr[0] = L'?';
2242 wstr[1] = L'\0';
2243 len = 1;
2244 }
2245 if (x > PAD_COLS - 1) {
2246 view->stripped_line_out[x++] = '\0';
2247 break;
2248 }
2249 view->stripped_line_out[x++] = in_str[i];
2250 if (char_width > 1)
2251 for (int n = 1; n < char_width; n++)
2252 view->stripped_line_out[x++] = ' ';
2253 if (j > PAD_COLS - 1) {
2254 char_width = 1;
2255 wstr[0] = L'\0';
2256 wstr[1] = L'\0';
2257 len = 1;
2258 ui_set_cell(view->sfc, PAD, &cc, wstr, attrs, cpx, nullptr);
2259 break;
2260 }
2261 ui_set_cell(view->sfc, PAD, &cc, wstr, attrs, cpx, nullptr);
2262 cmplx_buf[j++] = cc;
2263 i += len;
2264 if (view->wrap) {
2265 word_cols += char_width;
2266 word_cells++;
2267 }
2268 } // END Character and Word
2269 } // END WHILE - ANSI SGR, Character, and Word
2270 if (view->wrap) { // handle line wrapping
2271 if (word_cols > 0) { // break line, add last word to next line
2272 if (sl_cols <= sl_maxlen) {
2273 view->cur.sl_s[view->cur.sl_idx] = sl_s;
2274 view->cur.sl_cc[view->cur.sl_idx] = sl_cc;
2275 view->cur.sl_cols[view->cur.sl_idx] = sl_cols;
2276 view->cur.sl_cells[view->cur.sl_idx] = sl_cells;
2277 view->cur.sl_idx++;
2278 sl_s = &sl_s[sl_cols];
2279 sl_cc = &sl_cc[sl_cells];
2280 sl_cols > view->maxcol ? view->maxcol = sl_cols : 0;
2281 sl_cols = word_cols;
2282 sl_cells = word_cells;
2283 word_cols = 0;
2284 word_cells = 0;
2285 }
2286 }
2287 while (sl_cols > sl_maxlen - 1) { // split long lines
2288 uint safe_cells = 0;
2289 uint safe_cols = 0;
2290 while (safe_cols < sl_maxlen && safe_cells < sl_cells) {
2291 wchar_t wstr_chk[CCHARW_MAX];
2292 attr_t attr_chk;
2293 short cpx_chk;
2294 ui_get_cell(view->sfc, PAD, &sl_cc[safe_cells], wstr_chk, &attr_chk, &cpx_chk, nullptr);
2295 uint cw = wcwidth(wstr_chk[0]);
2296 if (safe_cols + cw > sl_maxlen)
2297 break;
2298 safe_cols += cw;
2299 safe_cells++;
2300 }
2301 if (safe_cells == 0) {
2302 safe_cells = 1;
2303 safe_cols = sl_maxlen;
2304 }
2305 view->cur.sl_s[view->cur.sl_idx] = sl_s;
2306 view->cur.sl_cc[view->cur.sl_idx] = sl_cc;
2307 view->cur.sl_cols[view->cur.sl_idx] = safe_cols;
2308 view->cur.sl_cells[view->cur.sl_idx] = safe_cells;
2309 view->cur.sl_idx++;
2310 sl_s = &sl_s[safe_cells];
2311 sl_cc = &sl_cc[safe_cells];
2312 sl_cols > view->maxcol ? view->maxcol = sl_cols : 0;
2313 sl_cols -= safe_cols;
2314 sl_cells -= safe_cells;
2315 word_cols = 0;
2316 word_cells = 0;
2317 } // END WHILE - split long lines
2318 } // END IF - handle line wrapping
2319 } // END WHILE - line
2320 if (view->wrap) { // finish and commit wrap state
2321 if (view->cur.sl_idx > 0) {
2322 view->cur.sl_s[view->cur.sl_idx] = sl_s;
2323 view->cur.sl_cc[view->cur.sl_idx] = sl_cc;
2324 view->cur.sl_cols[view->cur.sl_idx] = sl_cols;
2325 view->cur.sl_cells[view->cur.sl_idx] = sl_cells;
2326 view->cur.sl_idx++;
2327 view->cur.sl_cc[view->cur.sl_idx] = nullptr;
2328 view->cur.sl_s[view->cur.sl_idx] = nullptr;
2329 view->cur.sl_cnt = view->cur.sl_idx;
2330 view->cur.sl_idx = 0;
2331 }
2332 view->cur.sl_ln_no = view->ln_no;
2333 } else {
2334 j > view->maxcol ? view->maxcol = j : 0;
2335 }
2336#ifdef DEBUG_WRAP
2337 // log_split_lines(view);
2338 log_cc_buf(view);
2339 // log_stripped_line_out(view);
2340#endif
2341 //-------------------------------------------------------------------------
2342 wstr[0] = '\0';
2343 wstr[1] = '\0';
2344 ui_set_cell(view->sfc, PAD, &cc, wstr, WA_NORMAL, cpx, nullptr);
2345 cmplx_buf[j] = cc;
2346 view->stripped_line_out[x] = '\0';
2347 return j;
2348}
2349/** @brief Log Stripped Line Output
2350 @ingroup view_display
2351 @param view pointer to View structure containing stripped line output
2352 buffer
2353 @details This function logs the contents of the stripped line output
2354 buffer (view->stripped_line_out) for debugging purposes. It iterates
2355 through the split lines (if any) and logs each line to the cmenu log.
2356 */
2357void log_stripped_line_out(View *view) {
2358 char tmp_str[PAD_COLS];
2359 ui_log(INFO, "%s:", view->stripped_line_out);
2360 for (uint k = 0; k < view->cur.sl_cnt; k++) {
2361 memset(tmp_str, 0, sizeof(tmp_str));
2362 for (uint c = 0; c < view->cur.sl_cols[k]; c++)
2363 tmp_str[c] = view->cur.sl_s[k][c];
2364 ui_log(INFO, "%s:", tmp_str);
2365 }
2366}
2367/** @brief Log Complex Character Buffer
2368 @ingroup view_display
2369 @param view pointer to View structure containing complex character buffer
2370 (view->cmplx_buf)
2371 @details This function logs the contents of the complex character buffer
2372 (view->cmplx_buf) for debugging purposes. It iterates through the split
2373 lines (if any) and logs each line to the cmenu log, converting wide
2374 characters to their corresponding single-byte representation for easier
2375 viewing.
2376 */
2377void log_cc_buf(View *view) {
2378 char tmp_str[PAD_COLS];
2379
2380 for (uint k = 0; k < view->cur.sl_cnt; k++) {
2381 memset(tmp_str, 0, sizeof(tmp_str));
2382 for (uint c = 0; c < view->cur.sl_cells[k]; c++) {
2383 wchar_t wstr[CCHARW_MAX];
2384 attr_t attrs;
2385 short cpx;
2386 ui_get_cell(view->sfc, PAD, &view->cur.sl_cc[k][c], wstr, &attrs, &cpx, nullptr);
2387 if (wstr[0] == L'\0')
2388 tmp_str[c] = ' ';
2389 else
2390 tmp_str[c] = (char)wstr[0];
2391 }
2392 ui_log(INFO, "%s:", tmp_str);
2393 }
2394}
2395/** @brief Log String with Specified Length
2396 @ingroup view_display
2397 @param str pointer to the string to log
2398 @param len length of the string to log
2399 @details This function logs a specified number of characters from a given
2400 string to the cmenu log. It copies the specified length of the string into
2401 a temporary buffer and then writes it to the log.
2402 */
2403void log_strnz(char *str, uint len) {
2404 char tmp_str[MAXLEN];
2405 strnz__cpy(tmp_str, str, len);
2406 ui_log(INFO, "%s:", tmp_str);
2407}
2408/** @brief Log Split Lines
2409 @ingroup view_display
2410 @param view pointer to View structure containing split line information
2411 @details This function logs the contents of the split lines stored in the
2412 view structure for debugging purposes. It iterates through the split lines
2413 and logs each line to the cmenu log, converting wide characters to their
2414 corresponding single-byte representation for easier viewing.
2415 */
2416void log_split_lines(View *view) {
2417 char tmp_str[MAXLEN];
2418
2419 for (uint k = 0; k < view->cur.sl_cnt; k++) {
2420 if (view->cur.sl_cols[k] > 0) {
2421 memset(tmp_str, 0, sizeof(tmp_str));
2422 char *s = view->cur.sl_s[k];
2423 char *d = tmp_str;
2424 char *e = view->cur.sl_s[k] + view->cur.sl_cols[k];
2425 while (s < e)
2426 *d++ = *s++;
2427 *d = '\0';
2428 ui_log(INFO, "%s:", tmp_str);
2429 }
2430 }
2431}
2432//----------------------------------------------------------------------------------
2433/** @brief Parse ANSI SGR Escape Sequence
2434 @ingroup view_display
2435 @param ansi_str is the ANSI escape sequence string to parse
2436 @param attrs is a pointer to an attr_t variable where the parsed
2437 attributes will be stored
2438 @param cpx is a pointer to an int variable where the parsed color pair
2439 index will be stored
2440 @details This function parses ANSI escape sequences, which it converts
2441 to color pairs and attributes used to construct complex character cells
2442 or extended grapheme clusters.
2443 @note ANSI Select Graphics Rendition (SGR), ECMA-48, (ISO/IEC 6429)
2444 @note Extends Color Pair and Color arrays as necessary
2445 @verbatim
2446
2447 SGR specification types:
2448
2449 RGB:
2450
2451 foreground \033[38;2;r;g;bm
2452 background \033[48;2;r;g;bm
2453
2454 Where r, g, b are the red, green, and blue color components (0-255)
2455
2456 XTERM 256-color:
2457
2458 foreground \033[38;5;xm
2459 background \033[48;5;xm
2460
2461 Where x is the 256-color index (0-255)
2462 uses ui_xterm256_idx_to_rgb() to convert the 256-color index to RGB
2463
2464 8-color:
2465
2466 foreground \033[3cm
2467 background \033[4cm
2468
2469 Where c is the color code (0 for black, 1 for red, 2 for green, 3
2470 for yellow, 4 for blue, 5 for magenta, 6 for cyan, 7 for white).
2471
2472 Attributes:
2473
2474 \033[am
2475
2476 Where a is the attribute code (1 for bold, 2 for dim, 3 for italic,
2477 4 for underline, 5 for blink, 7 for reverse, 8 for invis). The function
2478 also supports resetting attributes and colors to default using \033[0m.
2479
2480 see also: ui_xterm256_idx_to_rgb(), ui_color_from_rgb(), extended_pair_content(),
2481 ui_add_pair()
2482
2483 @endverbatim
2484 __parse_ansi_str
2485*/
2486void parse_ansi_str(char *ansi_str, attr_t *attrs, ushort *cpx) {
2487 char *tok;
2488 char t0, t1;
2489 char tstr[3];
2490 uint len, x_idx;
2491 uint fg, bg;
2492 uint fg_clr, bg_clr;
2493 char *ansi_p = ansi_str + 2;
2494 ui_pair_content(*cpx, &fg_clr, &bg_clr);
2495 bool f_rgb = false;
2496 fg = fg_clr;
2497 bg = bg_clr;
2498 RGB rgb;
2499 tok = strtok((char *)ansi_p, ";m");
2500 bool a_toi_error = false;
2501 while (1) {
2502 if (tok == nullptr || *tok == '\0')
2503 break;
2504 len = strlen(tok);
2505 if (len == 2) {
2506 t0 = tok[0];
2507 t1 = tok[1];
2508 if (t0 == '3' || t0 == '4') {
2509 if (t1 == '8') {
2510 tok = strtok(nullptr, ";m");
2511 if (tok != nullptr) {
2512 if (*tok == '5') {
2513 tok = strtok(nullptr, ";m");
2514 if (tok != nullptr) {
2515 x_idx = a_toi(tok, &a_toi_error);
2516 rgb = ui_xterm256_idx_to_rgb(x_idx);
2517 f_rgb = true;
2518 }
2519 } else if (*tok == '2') {
2520 tok = strtok(nullptr, ";m");
2521 if (tok != nullptr) {
2522 rgb.r = a_toi(tok, &a_toi_error);
2523 tok = strtok(nullptr, ";m");
2524 if (tok != nullptr) {
2525 rgb.g = a_toi(tok, &a_toi_error);
2526 tok = strtok(nullptr, ";m");
2527 if (tok != nullptr) {
2528 rgb.b = a_toi(tok, &a_toi_error);
2529 f_rgb = true;
2530 }
2531 }
2532 }
2533 }
2534 }
2535 if (f_rgb) {
2536 if (t0 == '3')
2537 fg_clr = ui_color_from_rgb(&rgb);
2538 else if (t0 == '4')
2539 bg_clr = ui_color_from_rgb(&rgb);
2540 }
2541 } else if (t1 == '9') {
2542 if (t0 == '3')
2543 fg_clr = CLR_NT_FG;
2544 else if (t0 == '4')
2545 bg_clr = CLR_NT_BG;
2546 } else if (t1 >= '0' && t1 <= '7') {
2547 if (t0 == '3') {
2548 tstr[0] = t1;
2549 tstr[1] = '\0';
2550 x_idx = a_toi(tstr, &a_toi_error);
2551 rgb = ui_xterm256_idx_to_rgb(x_idx);
2552 fg_clr = ui_color_from_rgb(&rgb);
2553 } else if (t0 == '4') {
2554 tstr[0] = t1;
2555 tstr[1] = '\0';
2556 x_idx = a_toi(tstr, &a_toi_error);
2557 rgb = ui_xterm256_idx_to_rgb(x_idx);
2558 bg_clr = ui_color_from_rgb(&rgb);
2559 }
2560 }
2561 } else if (t0 == '0') {
2562 *tok = t1;
2563 len = 1;
2564 }
2565 }
2566 if (len == 1) {
2567 if (*tok == '0') {
2568 *attrs = WA_NORMAL;
2569 fg_clr = CLR_NT_FG;
2570 bg_clr = CLR_NT_BG;
2571 } else {
2572 switch (a_toi(tok, &a_toi_error)) {
2573 case 1:
2574 *attrs |= WA_BOLD;
2575 break;
2576 case 2:
2577 *attrs |= WA_DIM;
2578 break;
2579 case 3:
2580 *attrs |= WA_ITALIC;
2581 break;
2582 case 4:
2583 *attrs |= WA_UNDERLINE;
2584 break;
2585 case 5:
2586 *attrs |= WA_BLINK;
2587 break;
2588 case 7:
2589 *attrs |= WA_REVERSE;
2590 break;
2591 case 8:
2592 *attrs |= WA_INVIS;
2593 break;
2594 default:
2595 break;
2596 }
2597 }
2598 } else if (len == 0) {
2599 *attrs = WA_NORMAL;
2600 fg_clr = CLR_NT_FG;
2601 bg_clr = CLR_NT_BG;
2602 }
2603 tok = strtok(nullptr, ";m");
2604 }
2605 if (!a_toi_error && (fg_clr != fg || bg_clr != bg))
2606 *cpx = ui_add_pair(fg_clr, bg_clr);
2607 return;
2608}
2609/** @brief Display Command Line Prompt
2610 @ingroup view_display
2611 @param view is the current view data structure
2612 @param s is the prompt string */
2613int display_prompt(View *view, char *s) {
2614 char message_str[PAD_COLS + 1];
2615 uint l;
2616 UiSurface *sfc = view->sfc;
2617 l = strnz__cpy(message_str, s, PAD_COLS);
2618 ui_cursor_move(sfc, CMDLN, view->cmd_line, 0);
2619 if (l != 0) {
2620 ui_wclrtoeol(sfc, CMDLN);
2621 ui_bkgdset(sfc, CMDLN, &cell_nt_rev);
2622 ui_mvwaddstr(sfc, CMDLN, view->cmd_line, 0, " ");
2623 ui_mvwaddstr(sfc, CMDLN, view->cmd_line, 1, message_str);
2624 ui_waddstr(sfc, CMDLN, " ");
2625 ui_bkgdset(sfc, CMDLN, &cell_nt);
2626 ui_getyx(sfc, CMDLN, &view->cmd_line, &view->curx);
2627 ui_cursor_move(sfc, CMDLN, view->cmd_line, view->curx);
2628 }
2629#ifdef DEBUG_IMMED_UI
2630 ui_render();
2631#endif
2632 return (view->curx);
2633}
2634/** @brief Display View Help File
2635 @ingroup view_display
2636 @param init is the current initialization data structure.
2637 @details The current View context is set aside by assigning the view
2638 structure to "view_save" while the help file is displayed using a new,
2639 separate view structure.
2640 The help file is specified by the VIEW_HELP_FILE macro can be set
2641 to a default help file path or overridden by the user through an
2642 environment variable.
2643 After the help file is closed, the original view is restored and
2644 the page is redisplayed.
2645 It may be necessary to reassign view after calling this function
2646 because the init->view pointer is temporarily set to nullptr during the
2647 help file display, and the original view is restored afterward.
2648 The default screen size for help can be set in the code below. If
2649 set to 0, popup_view will determine reasonable maximal size based on the
2650 terminal dimensions.
2651 The help file may contain Unicode characters and ANSI escape
2652 sequences for formatting, which will be properly handled and displayed by
2653 popup_view. */
2654void view_display_help(Init *init) {
2655 char tmp_str[MAXLEN];
2656 uint eargc = 0;
2657 char *eargv[MAXARGS];
2658 View *view = init->view;
2659 if (view->f_help_spec || view->help_spec[0] != '\0')
2660 strnz__cpy(tmp_str, view->help_spec, MAXLEN - 1);
2661 else {
2662 strnz__cpy(tmp_str, init->mapp_help, MAXLEN - 1);
2663 strnz__cat(tmp_str, "/", MAXLEN - 1);
2665 }
2666 eargv[eargc++] = strdup("view");
2667 eargv[eargc++] = strdup("-N");
2668 eargv[eargc++] = strdup(tmp_str);
2669 eargv[eargc] = nullptr;
2670 init->lines = 48;
2671 init->cols = 72;
2672 init->begy = 0;
2673 init->begx = 0;
2674 strnz__cpy(init->title, "View Help", MAXLEN - 1);
2675 popup_view(init, eargc, eargv, init->lines, init->cols, init->begy,
2676 init->begx);
2677 destroy_argv(eargc, eargv);
2678 init->view->f_redisplay_page = true;
2679}
2680/*------------------------------------------------------------
2681 END DISPLAY
2682 ------------------------------------------------------------*/
2683/** @brief use form to enter a file specification
2684 @ingroup view_engine
2685 @param init data structure
2686 @param file_spec - pointer to file specification
2687 the file_spec
2688 @returns true if successful
2689 @details the user must provide a character array large enough to
2690 hold file_spec without overflowing
2691 */
2692bool enter_file_spec(Init *init, char *file_spec) {
2693 char earg_str[MAXLEN];
2694 char *eargv[MAXARGS];
2695 uint eargc;
2696 char tmp_dir[MAXLEN];
2697 char tmp_str[MAXLEN];
2698 char tmp_spec[MAXLEN];
2699 int rc = 0;
2700 FILE *tmp_fp;
2701 View *view = init->view;
2702 strnz__cpy(tmp_dir, init->mapp_home, MAXLEN - 1);
2703 strnz__cat(tmp_dir, "/tmp", MAXLEN - 1);
2704 expand_tilde(tmp_dir, MAXLEN - 1);
2705 if (!mk_dir(tmp_dir)) {
2706 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 2);
2707 strnz__cpy(em1, _("Unable to "), MAXLEN - 1);
2708 strnz__cat(em1, "mkdir", MAXLEN - 1);
2709 strnz__cat(em1, tmp_dir, MAXLEN - 1);
2710 strerror__r(errno, em2, MAXLEN - 1);
2712 return false;
2713 }
2714 while (rc == false) {
2715 strnz__cpy(tmp_spec, tmp_dir, MAXLEN - 1);
2716 strnz__cat(tmp_spec, "/tmp_XXXXXX", MAXLEN - 1);
2717 view->in_fd = mkstemp(tmp_spec);
2718 if (view->in_fd == -1) {
2719 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 2);
2720 strnz__cpy(em1, _("unable to "), MAXLEN - 1);
2721 strnz__cat(em1, "mkstemp ", MAXLEN - 1);
2722 strnz__cat(em1, tmp_spec, MAXLEN - 1);
2723 strerror__r(errno, em2, MAXLEN - 1);
2724 ui_display_error(em0, em1, nullptr, nullptr);
2725 return false;
2726 }
2727 /* call form to get file_name
2728 write the name to a temporary file */
2729
2730 strnz__cpy(earg_str, _("form -d file_name.f -o "), MAXLEN - 1);
2731 strnz__cat(earg_str, tmp_spec, MAXLEN - 1);
2732 eargc = str_to_args(eargv, earg_str, MAX_ARGS);
2733 rc = popup_form(init, eargc, eargv, view->begy + view->lines - 7, 4);
2734 destroy_argv(eargc, eargv);
2736 if (rc == FA_CANCEL || rc == 'q' || rc == 'Q' || rc == UIKEY_F09)
2737 return false;
2738 close(view->in_fd);
2739 tmp_fp = fopen(tmp_spec, "r");
2740 if (tmp_fp == nullptr) {
2741 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 2);
2742 strnz__cpy(em1, _("unable to "), MAXLEN - 1);
2743 strnz__cat(em1, "fopen ", MAXLEN - 1);
2744 strnz__cat(em1, tmp_spec, MAXLEN - 1);
2745 strerror__r(errno, em2, MAXLEN - 1);
2747 return false;
2748 }
2749 fgets(tmp_str, MAXLEN - 1, tmp_fp);
2750 strnz(tmp_str, MAXLEN - 1);
2751 fclose(tmp_fp);
2752 unlink(tmp_spec);
2753 if (!verify_spec_arg(file_spec, tmp_str, "~/menuapp/tmp", ".",
2754 S_WCOK | S_QUIET)) {
2755 ssnprintf(em0, MAXLEN - 1, _("Unable to open %s for writing"),
2756 tmp_str);
2757 strnz__cpy(em1, _("Try again? y (yes) or n (no) "), MAXLEN - 1);
2758 rc = ui_display_error(em0, em1, nullptr, nullptr);
2759 if (rc == 'y' || rc == 'Y')
2760 continue;
2761
2762 } else
2763 return true;
2764 }
2765 return true;
2766}
@ FA_CANCEL
Definition form.h:33
#define PRINTCMD
Definition common.h:50
int popup_form(Init *, int, char **, uint, uint)
instantiate a form popup window
Definition popups.c:113
#define DEFAULTEDITOR
Definition common.h:40
int popup_view(Init *, int, char **, uint, uint, uint, uint)
instantiate a view popup window
Definition popups.c:145
#define VIEW_HELP_FILE
Definition common.h:38
size_t rtrim(char *)
Trims trailing spaces from string s in place.
Definition futil.c:307
#define MAXARGS
Definition cm.h:67
#define Ctrl(c)
Definition cm.h:70
#define CM_VERSION
Definition version.h:5
@ CT_VIEW
Definition menu.h:46
void ui_render()
Render the UI by updating panels and refreshing the display.
Definition ui_ncurses.c:931
void ui_getyx(UiSurface *s, ss_t w, uint *lines, uint *cols)
Get the current cursor position within a window of a UI surface.
Definition ui_ncurses.c:707
int ui_cursor_enable_yx(UiSurface *s, ss_t w, uint y, uint x, bool visible)
Enable or disable the cursor at a specific position within a UiSurface.
Definition ui_ncurses.c:896
int ui_wscrl(UiSurface *s, ss_t w, int rows)
Scroll the contents of a UiSurface by a specified number of lines.
Definition ui_ncurses.c:770
int ui_cursor_move(UiSurface *s, ss_t w, uint y, uint x)
Move the cursor to a specified position within a UiSurface.
Definition ui_ncurses.c:698
void ui_surface_destroy(UiSurface *s)
Destroy a UiSurface, cleaning up its windows and panels.
Definition ui_ncurses.c:499
#define ERR
Definition ui_backend.h:468
void ui_restore_wins()
Restore the state of all windows in the UI. This function refreshes the standard screen and all surfa...
int ui_prefresh(UiSurface *s, ss_t w, int pminrow, int pmincol, int sminrow, int smincol, int smaxrow, int smaxcol)
Definition ui_ncurses.c:923
#define CCHARW_MAX
Definition ui_backend.h:60
void ui_get_screen_size(uint *lines, uint *cols)
Get the current screen size (number of lines and columns).
Definition ui_ncurses.c:754
@ CLR_NT_FG
Definition ui_backend.h:583
@ CLR_NT_BG
Definition ui_backend.h:584
int ui_wclrtobot(UiSurface *s, ss_t w)
Set the style of a surface window.
int ui_top_surface(UiSurface *s, ss_t w)
Hide a UiSurface, making it invisible on the screen.
Definition ui_ncurses.c:655
int ui_wclrtoeol(UiSurface *s, ss_t w)
Clear to the end of the line in a surface window.
int ui_bkgdset(UiSurface *s, ss_t w, const UiCell *cell)
Set the background for new content to be written to a specific window within a UiSurface.
Definition ui_ncurses.c:988
#define LINE_TBL_INCR
Definition view.h:46
#define NMARKS
Definition view.h:33
char err_msg[MAXLEN]
void go_to_position(View *, long)
#define PAD_COLS
Definition view.h:39
#define NULL_POSITION
Definition view.h:36
#define UIKEY_DC
#define UIKEY_BACKSPACE
#define UIKEY_F12
#define UIKEY_LSHIFT
#define UIKEY_RESIZE
#define UIKEY_LEFT
#define UIKEY_F09
#define UIKEY_RSHIFT
#define UIKEY_PPAGE
#define UIKEY_F01
#define UIKEY_BUTTON1
#define UIKEY_RIGHT
#define UIKEY_UP
#define UIKEY_SCROLL_UP
#define UIKEY_HOME
#define UIKEY_F05
#define UIKEY_ENTER
#define UIKEY_SCROLL_DOWN
#define UIKEY_F06
#define UIKEY_DOWN
#define UIKEY_NPAGE
#define WA_NORMAL
#define WA_BLINK
#define WA_ITALIC
#define WA_DIM
#define WA_INVIS
#define WA_BOLD
#define WA_REVERSE
#define WA_UNDERLINE
int ui_get_event(UiSurface *s, ss_t w, UiChyron *chyron, UiEvent *ev, int timeout_ms)
Read input events.
UiCell cell_sp
Definition ui_common.c:149
ushort cp_nt
Definition ui_common.c:167
UiCell cell_nt
Definition ui_common.c:123
RGB ui_xterm256_idx_to_rgb(uint idx)
Convert XTerm 256 color index to RGB.
Definition ui_common.c:969
ushort cp_nt_rev
Definition ui_common.c:168
int ui_border_title(UiSurface *sfc, ss_t w, const char *title)
Draw a box with a title around the specified window.
Definition ui_common.c:621
UiCell cell_ran
Definition ui_common.c:132
void ui_log_write(const UiLogLevel level, const char *file, const char *func, const int line, const char *fmt,...)
Definition ui_common.c:1476
UiCell cell_nt_rev
Definition ui_common.c:124
int ui_pair_content(uint pair, uint *fg, uint *bg)
Get the foreground and background colors of a specified color pair.
uint ui_add_pair(uint fg, uint bg)
Add a new color pair to the UI, or retrieve the index of an existing pair.
int ui_color_from_rgb(RGB *rgb)
Convert RGB to curses color index, adding new color if necessary.
int ui_mvwadd_wchnstr(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cmplx_buf, uint n)
Draw a complex character string at a specific position in a surface window, with a maximum length.
int ui_waddstr(UiSurface *s, ss_t w, const char *text)
Draw a text string in a surface window.
int ui_mvwaddch(UiSurface *s, ss_t w, uint y, uint x, const char c)
Draw a character in a surface window.
int ui_mvwadd_wchstr(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cmplx_buf)
Draw a complex character string at a specific position in a surface window.
int ui_mvwaddstr(UiSurface *s, ss_t w, uint y, uint x, const char *text)
Draw a text string at a specific position in a surface window.
int ui_mvwadd_wch(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cell)
Draw a complex character at a specific position in a surface window.
int ui_wadd_wchnstr(UiSurface *s, ss_t w, const UiCell *cmplx_buf, uint n)
Draw a complex character string in a surface window, with a maximum length.
void view_full_screen_resize(Init *)
Resize the full screen view and its components.
Definition init_view.c:93
void view_boxwin_resize(Init *)
Resize the current window and its box.
Definition init_view.c:228
#define MAX_ARGS
Definition lf.h:37
#define S_TOLOWER(c)
Definition lf.h:183
#define min(x, y)
Definition lf.h:158
#define S_QUIET
Definition lf.h:282
#define S_WCOK
Definition lf.h:281
#define MAXLEN
Definition lf.h:38
char prev_regex_pattern[MAXLEN]
FILE * dbgfp
void get_line(View *, off_t)
void sync_pos(View *, off_t pos)
#define confirm()
Definition view_engine.c:88
int cf_accept(UiSurface *sfc, ss_t w, char *accept_s, uint flin, uint fcol, uint flen)
Accepts input for a field in a C-Menu window.
Definition cm_fields.c:41
char em1[MAXLEN]
Definition lf.c:43
char em2[MAXLEN]
Definition lf.c:44
char em0[MAXLEN]
Definition lf.c:42
#define _(String)
Definition amort.c:23
int shell(char *)
Execute a shell command.
Definition exec.c:74
int full_screen_shell(char *)
Execute a shell command in full screen mode.
Definition exec.c:58
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition lf.c:2299
size_t strnz(char *, size_t)
terminates string at New Line, Carriage Return, or max_len
Definition futil.c:621
size_t ssnprintf(char *, size_t, const char *,...)
Custom safe string formatting utility (Variadic version).
Definition futil.c:398
int str_to_args(char **, char *, uint)
Converts a string into an array of argument strings.
Definition futil.c:418
bool expand_tilde(char *, uint)
Expand a tilde (~) in a file path to the user's home directory.
Definition lf.c:2089
char size_t strip_ansi(char *, char *)
Strips ANSI SGR escape sequences (ending in 'm') from string s to d.
Definition futil.c:870
bool mk_dir(char *dir)
If directory doesn't exist, make it.
Definition futil.c:1351
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition lf.c:2330
size_t strnlf(char *, size_t)
terminates string with line feed
Definition futil.c:641
int a_toi(char *, bool *)
a safer alternative to atoi() for converting ASCII strings to integers.
Definition futil.c:806
bool str_subc(char *, char *, char, char *, uint)
Replaces "ReplaceChr" in "s" with "Withstr" in "d" won't copy more than "l" bytes to "d" Replaces all...
Definition futil.c:702
char * strerror__r(int errnum, char *buf, size_t buflen)
A thread-safe alternative to strerror() that writes the error message corresponding to errnum into th...
Definition futil.c:770
int destroy_argv(uint argc, char **argv)
Deallocates memory allocated for argument strings in argv.
Definition futil.c:474
bool base_name(char *, char *)
Returns the base name of a file specification.
Definition futil.c:1154
int view_init_input(Init *init, char *file_name)
Initialize the input for the C-Menu View.
Definition init_view.c:331
bool verify_spec_arg(char *, char *, char *, char *, uint)
Verify file specification argument.
Definition mem.c:361
int view_file(Init *)
Start view.
bool enter_file_spec(Init *, char *)
use form to enter a file specification
int write_view_buffer(Init *, bool)
Write buffer contents to files.
void lp(View *, char *)
Send File to Print Queue.
#define get_prev_char()
read the previous characater from the virtual fileThere is no need to track line numbers when moving ...
Definition view_engine.c:75
void cat_file(View *)
Concatenate File to Standard Output.
int get_cmd_arg(View *, char *)
Get Command Argument from User Input.
int view_cmd_processor(Init *)
Main Command Processing Loop for View.
int get_cmd_char(View *, off_t *)
Get Command Character from User Input.
void build_prompt(View *)
Build Prompt String.
#define get_next_char()
read the next characater from the virtual file
Definition view_engine.c:52
void go_to_percent(View *, uint)
Go to Percent of File.
off_t line_number(View *, off_t)
Get Line Number for a Given File Position.
void go_to_eof(View *)
Go to End of File.
void go_to_mark(View *, uint)
Go to Mark.
void page_prev(View *)
display previous page
void initialize_line_table(View *)
Initialize Line Table.
bool search(View *, int, char *)
Search for Regular Expression Pattern.
void destroy_line_table(View *)
Destroy Line Table.
int go_to_line(View *, off_t)
Go to Specific Line.
void page_next(View *)
Advance to Next Page.
void scroll_next(View *view, uint n)
Scroll Down by n Lines.
void increment_ln(View *)
Increment Line Index and Update Line Table.
void scroll_prev(View *view, uint n)
Move the view scope n lines toward the beginning of the file.
void sync_ln(View *, off_t ln_no)
Synchronize Line Table with Current File Position.
void log_cc_buf(View *)
Log Complex Character Buffer.
void log_split_lines(View *)
Log Split Lines.
void display_line(View *)
Display Line on Padparam View *view data structure.
int pad_refresh(View *)
Refresh Pad and Line Number Window.
void display_line_eod(View *)
Display End of Data.
int display_prompt(View *, char *)
Display Command Line Prompt.
void parse_ansi_str(char *, attr_t *, ushort *)
Parse ANSI SGR Escape Sequence.
void log_strnz(char *, uint)
Log String with Specified Length.
int fmt_line(View *)
Format Line for Display.
void display_split_line(View *)
Display Split Line on Pad.
void view_display_help(Init *)
Display View Help File.
void view_display_page(View *)
Display Current Page.
void log_stripped_line_out(View *)
Log Stripped Line Output.
int ui_perror(char *emsg_str)
Display a simple error message window or print to stderr.
Definition ui_common.c:1323
int ui_answer_yn(char *msg0, char *msg1, char *msg2, char *msg3)
Accept a single letter answer.
Definition ui_common.c:1033
int ui_display_error(char *msg0, char *msg1, char *msg2, char *msg3)
Display an error message window or print to stderr.
Definition ui_common.c:1261
char title[MAXLEN]
Definition common.h:132
char mapp_help[MAXLEN]
Definition common.h:152
int begx
Definition common.h:121
int cols
Definition common.h:119
char mapp_home[MAXLEN]
Definition common.h:150
int begy
Definition common.h:120
View * view
Definition common.h:193
int lines
Definition common.h:118
char editor[MAXLEN]
Definition common.h:175
off_t sl_ln_no
Definition view.h:50
uint sl_cnt
Definition view.h:56
uint sl_cols[MAXLEN]
Definition view.h:53
uint sl_idx
Definition view.h:55
UiCell * sl_cc[MAXLEN]
Definition view.h:52
uint sl_cells[MAXLEN]
Definition view.h:54
char * sl_s[MAXLEN]
Definition view.h:51
uint smaxrow
Definition view.h:133
uint sminrow
Definition view.h:129
char in_spec[MAXLEN]
Definition view.h:141
off_t page_top_ln_no
Definition view.h:158
char stripped_line_out[PAD_COLS+1]
Definition view.h:116
off_t srch_curr_ln_no
Definition view.h:160
int argc
Definition view.h:80
char ** argv
Definition view.h:81
bool f_search_complete
Definition view.h:107
int next_cmd_char
Definition view.h:97
uint page_bot_sl_idx
Definition view.h:195
char * buf
Definition view.h:173
uint maxcol
Definition view.h:126
bool wrap
Definition view.h:190
uint smincol
Definition view.h:131
char * line_out_p
Definition view.h:119
char * line_in_end_p
Definition view.h:121
int in_fd
Definition view.h:165
off_t srch_curr_pos
Definition view.h:156
off_t page_top_pos
Definition view.h:153
off_t file_pos
Definition view.h:151
bool page_bot_sl
Definition view.h:193
uint lines
Definition view.h:70
off_t ln_no
Definition view.h:184
char prompt_str[MAXLEN]
Definition view.h:78
off_t prev_file_pos
Definition view.h:152
bool f_bod
Definition view.h:99
bool f_first_iter
Definition view.h:106
char line_in_s[PAD_COLS+1]
Definition view.h:114
char help_spec[MAXLEN]
Definition view.h:143
bool f_redisplay_page
Definition view.h:104
char * file_spec_ptr
Definition view.h:147
off_t ln_pos_max
Definition view.h:189
char * next_file_spec_ptr
Definition view.h:148
bool f_is_pipe
Definition view.h:102
bool f_ignore_case
Definition view.h:83
bool f_help_spec
Definition view.h:146
char line_out_s[PAD_COLS+1]
Definition view.h:115
char cmd[MAXLEN]
Definition view.h:76
bool page_top_sl
Definition view.h:192
off_t srch_beg_ln_no
Definition view.h:161
off_t * ln_tbl
Definition view.h:186
bool f_full_screen
Definition view.h:108
bool f_ln
Definition view.h:183
bool f_strip_ansi
Definition view.h:86
bool f_eod
Definition view.h:100
off_t mark_tbl[NMARKS]
Definition view.h:162
off_t ln_no_max
Definition view.h:188
UiCell cmplx_buf[PAD_COLS+1]
Definition view.h:117
uint cmd_line
Definition view.h:125
uint page_top_sl_cnt
Definition view.h:196
off_t srch_beg_pos
Definition view.h:157
uint ln_win_cols
Definition view.h:182
uint first_match_x
Definition view.h:137
char * tmp_file_name_ptr
Definition view.h:149
uint curx
Definition view.h:123
off_t file_size
Definition view.h:150
uint scroll_lines
Definition view.h:124
bool f_squeeze
Definition view.h:85
char cur_file_str[MAXLEN]
Definition view.h:113
uint cury
Definition view.h:122
char out_spec[MAXLEN]
Definition view.h:142
int tab_stop
Definition view.h:95
uint page_bot_sl_cnt
Definition view.h:197
char title[MAXLEN]
Definition view.h:79
uint cols
Definition view.h:71
off_t page_bot_pos
Definition view.h:154
off_t page_bot_ln_no
Definition view.h:159
uint begy
Definition view.h:72
bool f_displaying_help
Definition view.h:105
int h_shift
Definition view.h:96
int curr_argc
Definition view.h:92
int out_fd
Definition view.h:167
uint pmincol
Definition view.h:128
uint page_top_sl_idx
Definition view.h:194
uint smaxcol
Definition view.h:135
off_t page_bot_end_pos
Definition view.h:155
UiSurface * sfc
Definition view.h:89
SplitLine cur
Definition view.h:191
char file_name[MAXLEN]
Definition view.h:103
uint last_match_x
Definition view.h:139
off_t ln_tbl_size
Definition view.h:187
char * line_in_beg_p
Definition view.h:120
uint pminrow
Definition view.h:127
char cmd_arg[MAXLEN]
Definition view.h:94