Skip to content

Commit

Permalink
Fix anchored regex line matching inconsistencies.
Browse files Browse the repository at this point in the history
  * (edit_find): fix start and end search boundaries in case of regex
    search and search direction.
  * (edit_replace_cmd): clarify cursor position.
  • Loading branch information
aborodin committed Feb 9, 2025
1 parent 56845e7 commit 3922b5f
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions src/editor/editsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
off_t search_end;
off_t start_mark = 0;
off_t end_mark = buf->size;
gboolean start_from_next_line = FALSE;
char end_string_symbol;

end_string_symbol = edit_search_get_current_end_line_char (edit);
Expand All @@ -459,9 +460,19 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
}

// fix the start and the end of search block positions
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0 && start_mark != 0)
start_mark = edit_calculate_start_of_next_line (buf, start_mark, buf->size,
end_string_symbol);
if (!edit_search_options.backwards && (edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0
&& search_start != 0)
{
const off_t bol =
edit_calculate_start_of_current_line (buf, search_start, end_string_symbol);

if (search_start != bol)
{
start_mark = edit_calculate_start_of_next_line (buf, start_mark, buf->size,
end_string_symbol);
start_from_next_line = TRUE;
}
}

if ((edit->search_line_type & MC_SEARCH_LINE_END) != 0
&& (end_mark - 1 != buf->size
Expand All @@ -476,6 +487,14 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
}
else if (edit_search_options.backwards)
end_mark = MAX (1, buf->curs1) - 1;
else if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
{
// regex forward search
const off_t bol =
edit_calculate_start_of_current_line (buf, search_start, end_string_symbol);

start_from_next_line = search_start != bol;
}

// search
if (edit_search_options.backwards)
Expand All @@ -484,8 +503,8 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
search_end = end_mark;

if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
search_start = edit_calculate_start_of_current_line (buf, search_start,
end_string_symbol);
search_start =
edit_calculate_start_of_current_line (buf, search_start, end_string_symbol);

while (search_start >= start_mark)
{
Expand All @@ -506,8 +525,8 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
return FALSE;

if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
search_start = edit_calculate_start_of_previous_line (buf, search_start,
end_string_symbol);
search_start =
edit_calculate_start_of_previous_line (buf, search_start, end_string_symbol);
else
search_start--;
}
Expand All @@ -517,9 +536,14 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
}

// forward search
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0 && search_start != start_mark)
search_start = edit_calculate_start_of_next_line (buf, search_start, end_mark,
end_string_symbol);

// correct end_mark if cursor is in column 0: move end_mark to the end of previous line
if (end_mark == edit_calculate_start_of_current_line (buf, end_mark, end_string_symbol))
end_mark = edit_calculate_end_of_previous_line (buf, end_mark, end_string_symbol);

if (start_from_next_line)
search_start =
edit_calculate_start_of_next_line (buf, search_start, end_mark, end_string_symbol);

return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
}
Expand Down Expand Up @@ -923,7 +947,7 @@ edit_replace_cmd (WEdit *edit, gboolean again)
edit->found_start = edit->search_start;
edit->found_len = len;

edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
edit_cursor_move (edit, edit->found_start - edit->buffer.curs1);
edit_scroll_screen_over_cursor (edit);

if (edit->replace_mode == 0)
Expand Down

0 comments on commit 3922b5f

Please sign in to comment.