C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
View Navigation

Navigation functions for the view application. More...

Functions

void destroy_line_table (View *view)
 Destroy Line Table.
void go_to_eof (View *view)
 Go to End of File.
int go_to_line (View *view, off_t line_idx)
 Go to Specific Line.
void go_to_mark (View *view, uint c)
 Go to Mark.
void go_to_percent (View *view, uint percent)
 Go to Percent of File.
void go_to_position (View *view, off_t pos)
 Go to Specific File Position.
void increment_ln (View *view)
 Increment Line Index and Update Line Table.
void initialize_line_table (View *view)
 Initialize Line Table.
off_t line_number (View *view, off_t position)
 Get Line Number for a Given File Position.
void page_next (View *view)
 Advance to Next Page.
void page_prev (View *view)
 display previous page
void scroll_next (View *view, uint n)
 Scroll Down by n Lines.
void scroll_prev (View *view, uint n)
 Move the view scope n lines toward the beginning of the file.
bool search (View *view, int search_cmd, char *regex_pattern)
 Search for Regular Expression Pattern.
void sync_ln (View *view, off_t ln_no)
 Synchronize Line Table with Current File Position.

Detailed Description

Navigation functions for the view application.

Function Documentation

◆ destroy_line_table()

void destroy_line_table ( View * view)

Destroy Line Table.

Parameters
viewdata structure

This function frees the memory allocated for the line table and resets the relevant fields in the View structure. It ensures that the line table is properly cleaned up when it is no longer needed, preventing memory leaks.

Definition at line 1987 of file view_engine.c.

1987 {
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}
View * view
Definition mem.c:38

References View::ln_no, View::ln_pos_max, View::ln_tbl, and View::ln_tbl_size.

Referenced by destroy_pick_view(), new_view_file(), and view_file().

Here is the caller graph for this function:

◆ go_to_eof()

void go_to_eof ( View * view)

Go to End of File.

Parameters
viewdata structure __go_to_eof

Definition at line 1870 of file view_engine.c.

1870 {
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}
void sync_pos(View *, off_t pos)
void page_prev(View *)
display previous page
void page_next(View *)
Advance to Next Page.

References View::cury, View::f_eod, View::file_size, View::ln_no, View::ln_no_max, page_next(), page_prev(), View::page_top_ln_no, View::scroll_lines, sync_pos(), and View::wrap.

Referenced by view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ go_to_line()

int go_to_line ( View * view,
off_t line_idx )

Go to Specific Line.

Parameters
viewdata structure
line_idxline number to go to (1-based index)
Returns
0 on success, EOF if line index is out of bounds or end of data is reached

Definition at line 1912 of file view_engine.c.

1912 {
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}
#define _(String)
Definition amort.c:23
void sync_ln(View *, off_t ln_no)
Synchronize Line Table with Current File Position.
int ui_perror(char *emsg_str)
Display a simple error message window or print to stderr.
Definition ui_common.c:1323

References View::file_pos, View::ln_tbl_size, View::page_bot_pos, page_next(), View::page_top_pos, sync_ln(), and ui_perror().

Referenced by view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ go_to_mark()

void go_to_mark ( View * view,
uint c )

Go to Mark.

Parameters
viewPointer to the View structure containing the state and parameters of the view application. This structure is used to access and modify the state of the application as needed.
cThe character representing the mark to go to. This character is typically a lowercase letter (a-z) corresponding to a mark that has been set previously in the view application. The function will attempt to navigate to the position associated with this mark, allowing the user to quickly jump to specific locations in the file based on the marks they have set. If the mark is not set, an error message will be displayed to the user.

Definition at line 1071 of file view_engine.c.

1071 {
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
1079 go_to_position(view, view->file_pos);
1080}
#define NMARKS
Definition view.h:33
#define NULL_POSITION
Definition view.h:36
void go_to_position(View *, off_t)
Go to Specific File Position.

References View::file_pos, go_to_position(), View::mark_tbl, and ui_perror().

Referenced by view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ go_to_percent()

void go_to_percent ( View * view,
uint percent )

Go to Percent of File.

Parameters
viewdata structure
percentof file

Definition at line 1896 of file view_engine.c.

1896 {
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}

References View::file_size, go_to_position(), and ui_perror().

Referenced by view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ go_to_position()

void go_to_position ( View * view,
off_t pos )

Go to Specific File Position.

Parameters
viewdata Structure
go_to_pos

Definition at line 1929 of file view_engine.c.

1929 {
1930 sync_pos(view, pos);
1931 page_next(view);
1932}

References page_next(), and sync_pos().

Here is the call graph for this function:

◆ increment_ln()

void increment_ln ( View * view)

Increment Line Index and Update Line Table.

Parameters
viewdata structure

This function is called when a line feed character is encountered while reading the file. It increments the line index (view->ln_no) and checks if the current file position exceeds the maximum position recorded in the line table. If it does, it updates the line table with the new file position. If the line index exceeds the current size of the line table, the table is resized by allocating more memory.

Definition at line 2006 of file view_engine.c.

2006 {
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) {
2013 view->ln_tbl_size += LINE_TBL_INCR;
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}
#define LINE_TBL_INCR
Definition view.h:46

References View::file_pos, View::ln_no, View::ln_no_max, View::ln_pos_max, View::ln_tbl, View::ln_tbl_size, and ui_perror().

Referenced by get_line().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialize_line_table()

void initialize_line_table ( View * view)

Initialize Line Table.

Parameters
viewdata structure

The line table is initialized with a specified increment size (LINE_TBL_INCR). Memory is allocated for the line table, and the first entry is set to 0, indicating the file position of the first line. The line index (view->ln_no) is initialized to 0.

Definition at line 1968 of file view_engine.c.

1968 {
1969 view->ln_tbl_size = LINE_TBL_INCR;
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}

References View::ln_no, View::ln_pos_max, View::ln_tbl, View::ln_tbl_size, and ui_perror().

Referenced by new_view_file(), and view_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ line_number()

off_t line_number ( View * view,
off_t position )

Get Line Number for a Given File Position.

Parameters
viewdata structure
targetfile position
Returns
line number corresponding to the given file position

This function performs a binary search on the line table (view->ln_tbl) to find the line number corresponding to the specified file position (target). It returns the index of the line in the line table that is closest to the target position without exceeding it. If the target position is not found, it returns the previous line number.

Definition at line 1944 of file view_engine.c.

1944 {
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}

References View::ln_tbl, and View::ln_tbl_size.

◆ page_next()

void page_next ( View * view)

Advance to Next Page.

Parameters
viewdata structure

Advances from view->page_bot_pos to view the next page of content. view->page_bot_pos must be set properly when calling this function. If the current bottom position of the page is at the end of the file, the function returns without making any changes. Otherwise, it resets the maximum column and current line position to the top of the page, updates the file position to the current bottom position of the page, and sets the top position and line number of the page accordingly. Finally, it calls the function to display the new page content. __page_next

Definition at line 1352 of file view_engine.c.

1352 {
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}
void view_display_page(View *)
Display Current Page.

References View::cury, View::file_pos, View::file_size, View::ln_no, View::ln_tbl, View::maxcol, View::page_top_ln_no, and view_display_page().

Referenced by go_to_eof(), go_to_line(), go_to_position(), new_view_file(), page_prev(), view_cmd_processor(), and view_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ page_prev()

void page_prev ( View * view)

display previous page

Parameters
viewdata structure

Displays the previous page starting at (view->page_top_ln_no - view->scroll_lines). __page_prev

Definition at line 1283 of file view_engine.c.

1283 {
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) {
1304 view->cur.sl_idx = view->page_top_sl_idx;
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++;
1323 view->page_top_sl_idx = view->cur.sl_idx;
1324 view->page_top_sl_cnt = view->cur.sl_cnt;
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}
#define min(x, y)
Definition lf.h:158
void get_line(View *, off_t)
int fmt_line(View *)
Format Line for Display.

References View::cur, View::cury, fmt_line(), get_line(), View::ln_no, View::ln_no_max, page_next(), View::page_top_ln_no, View::page_top_sl, View::page_top_sl_cnt, View::page_top_sl_idx, View::scroll_lines, SplitLine::sl_cnt, SplitLine::sl_idx, and View::wrap.

Referenced by go_to_eof(), and view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scroll_next()

void scroll_next ( View * view,
uint n )

Scroll Down by n Lines.

Parameters
viewPointer to the View structure containing the state and parameters of the view application. This structure is used to access and modify the state of the application as needed.
nadd n to page line pointers, moving the scope of the view window toward the end of data (eod). __scroll_next

Definition at line 1528 of file view_engine.c.

1528 {
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)
1553 view->cur.sl_idx = view->page_top_sl_idx;
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 }
1564 view->page_top_sl_idx = view->cur.sl_idx;
1565 view->page_top_sl_cnt = view->cur.sl_cnt;
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)
1585 view->cur.sl_idx = view->page_bot_sl_idx;
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;
1597 view->page_bot_sl_idx = view->cur.sl_idx;
1598 view->page_bot_sl_cnt = view->cur.sl_cnt;
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);
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);
1650 if (view->cury == view->scroll_lines)
1651 break;
1652 }
1653 view->page_bot_ln_no = view->ln_no;
1654 }
1655 return;
1656}
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
int ui_wclrtobot(UiSurface *s, ss_t w)
Set the style of a surface window.
void display_line(View *)
Display Line on Padparam View *view data structure.

References View::cur, View::cury, display_line(), View::f_bod, View::f_eod, View::f_ln, fmt_line(), get_line(), View::ln_no, View::ln_no_max, View::page_bot_ln_no, View::page_bot_sl, View::page_bot_sl_cnt, View::page_bot_sl_idx, View::page_top_ln_no, View::page_top_sl, View::page_top_sl_cnt, View::page_top_sl_idx, View::scroll_lines, View::sfc, SplitLine::sl_cnt, SplitLine::sl_idx, ui_cursor_move(), ui_wclrtobot(), ui_wscrl(), and View::wrap.

Referenced by view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scroll_prev()

void scroll_prev ( View * view,
uint n )

Move the view scope n lines toward the beginning of the file.

Parameters
viewPointer to the View structure containing the state and parameters of the view application. This structure is used to access and modify the state of the application as needed.
nsubtract n from page line pointers, moving the scope of the view window toward the beginning of data (bod). __scroll_prev

Definition at line 1665 of file view_engine.c.

1665 {
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) {
1707 view->cur.sl_idx = view->page_top_sl_idx;
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 }
1725 view->page_top_sl_idx = view->cur.sl_idx;
1726 view->page_top_sl_cnt = view->cur.sl_cnt;
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 }
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) {
1763 view->cur.sl_idx = view->page_bot_sl_idx;
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++;
1781 view->page_bot_sl_idx = view->cur.sl_idx;
1782 view->page_bot_sl_cnt = view->cur.sl_cnt;
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);
1799 scroll--;
1800 view->ln_no++;
1801 }
1802 view->ln_no--;
1803 }
1804 return;
1805}

References View::cur, View::cury, display_line(), View::f_bod, View::f_eod, View::f_ln, fmt_line(), get_line(), View::ln_no, View::ln_no_max, View::page_bot_ln_no, View::page_bot_sl, View::page_bot_sl_cnt, View::page_bot_sl_idx, View::page_top_ln_no, View::page_top_sl, View::page_top_sl_cnt, View::page_top_sl_idx, View::scroll_lines, View::sfc, SplitLine::sl_cnt, SplitLine::sl_idx, ui_cursor_move(), ui_wclrtobot(), ui_wscrl(), and View::wrap.

Referenced by view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ search()

bool search ( View * view,
int search_cmd,
char * regex_pattern )

Search for Regular Expression Pattern.

Parameters
viewPointer to View Structure
search_cmdSearch Command Character ('/' or '?')
regex_patternRegular Expression Pattern to Search For
Returns
true if a match is found and displayed, false if the search completes without finding a match or if an error occurs

The search performs extended regular expression matching, ignoring ANSI sequences and Unicode characters. Matches are highlighted on the screen, and the search continues until the page is full or the end of the file is reached. If the search wraps around the file, a message is displayed indicating that the search is complete. The search state is maintained in the view structure, allowing for repeat searches and tracking of the current search line. This function highlights all matches in the current ncurses pad, including those not displayed on the screen, and tracks the first and last match columns for prompt display. ANSI sequences and Unicode characters are stripped before matching, so matching corresponds to the visual display __search

initialize iteration

get line to scan

non-matching page filler

Display matching lines

Definition at line 1101 of file view_engine.c.

1101 {
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) {
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)
1142 view->srch_curr_ln_no = view->ln_no_max;
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 }
1160 sync_ln(view, view->srch_curr_ln_no);
1162 if (search_cmd == '/') {
1163 if (view->cury == view->scroll_lines)
1164 goto cleanup;
1165 prev_ln_no = view->srch_curr_ln_no;
1166 get_line(view, view->srch_curr_ln_no);
1167 view->page_bot_ln_no = view->srch_curr_ln_no;
1168 } else {
1169 if (view->cury == 0)
1170 goto cleanup;
1171 get_line(view, view->srch_curr_ln_no);
1172 prev_ln_no = view->srch_curr_ln_no;
1173 view->page_top_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) {
1181 if (search_cmd == '?')
1182 view->cury -= 2;
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;
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 }
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}
uint16_t attr_t
Definition ui_backend.h:458
ushort cp_nt_rev
Definition ui_common.c:168
UiCell cell_nt_rev
Definition ui_common.c:124
#define MAXLEN
Definition lf.h:38
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition lf.c:2299
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition lf.c:2330
uint64_t channels
Definition ui_backend.h:447

References View::cmplx_buf, cp_nt_rev, View::cury, display_line(), View::f_first_iter, View::f_ignore_case, View::f_search_complete, View::first_match_x, fmt_line(), get_line(), View::last_match_x, View::line_out_p, View::ln_no, View::ln_no_max, View::page_bot_ln_no, View::page_top_ln_no, View::scroll_lines, View::sfc, View::srch_beg_ln_no, View::srch_curr_ln_no, View::stripped_line_out, strnz__cat(), strnz__cpy(), sync_ln(), ui_cursor_move(), ui_perror(), and ui_wclrtobot().

Referenced by view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sync_ln()

void sync_ln ( View * view,
off_t ln_no )

Synchronize Line Table with Current File Position.

Parameters
viewdata Structure

The line table (view->ln_tbl) is an array that stores the file position of each line. The index (view->ln_no + 1) corresponds to the current line number.

If the line or position requested is not in the line table, this function reads forward to sycn. If the line or positione requested is behind the current line table index, the line index will be decremented until it matches the file position. __sync_ln

Definition at line 2038 of file view_engine.c.

2038 {
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}
#define get_next_char()
read the next characater from the virtual file
Definition view_engine.c:52

References View::f_eod, View::file_pos, View::ln_no, View::ln_no_max, and View::ln_tbl.

Referenced by go_to_line(), and search().

Here is the caller graph for this function: