Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit bfe71d6

Browse files
committed
Fix anchored regex line matching inconsistencies.
* (edit_find): fix start and end search boundaries in case of regex search and search direction. * (edit_replace_cmd): clarify cursor position.
1 parent 6a079ab commit bfe71d6

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

src/editor/editsearch.c

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
445445
off_t search_end;
446446
off_t start_mark = 0;
447447
off_t end_mark = buf->size;
448+
gboolean start_from_next_line = FALSE;
448449
char end_string_symbol;
449450

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

461462
// fix the start and the end of search block positions
462-
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0 && start_mark != 0)
463-
start_mark = edit_calculate_start_of_next_line (buf, start_mark, buf->size,
464-
end_string_symbol);
463+
if (!edit_search_options.backwards && (edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0
464+
&& search_start != 0)
465+
{
466+
const off_t bol =
467+
edit_calculate_start_of_current_line (buf, search_start, end_string_symbol);
468+
469+
if (search_start != bol)
470+
{
471+
start_mark = edit_calculate_start_of_next_line (buf, start_mark, buf->size,
472+
end_string_symbol);
473+
start_from_next_line = TRUE;
474+
}
475+
}
465476

466477
if ((edit->search_line_type & MC_SEARCH_LINE_END) != 0
467478
&& (end_mark - 1 != buf->size
@@ -476,6 +487,14 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
476487
}
477488
else if (edit_search_options.backwards)
478489
end_mark = MAX (1, buf->curs1) - 1;
490+
else if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
491+
{
492+
// regex forward search
493+
const off_t bol =
494+
edit_calculate_start_of_current_line (buf, search_start, end_string_symbol);
495+
496+
start_from_next_line = search_start != bol;
497+
}
479498

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

486505
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
487-
search_start = edit_calculate_start_of_current_line (buf, search_start,
488-
end_string_symbol);
506+
search_start =
507+
edit_calculate_start_of_current_line (buf, search_start, end_string_symbol);
489508

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

508527
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
509-
search_start = edit_calculate_start_of_previous_line (buf, search_start,
510-
end_string_symbol);
528+
search_start =
529+
edit_calculate_start_of_previous_line (buf, search_start, end_string_symbol);
511530
else
512531
search_start--;
513532
}
@@ -517,9 +536,14 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
517536
}
518537

519538
// forward search
520-
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0 && search_start != start_mark)
521-
search_start = edit_calculate_start_of_next_line (buf, search_start, end_mark,
522-
end_string_symbol);
539+
540+
// correct end_mark if cursor is in column 0: move end_mark to the end of previous line
541+
if (end_mark == edit_calculate_start_of_current_line (buf, end_mark, end_string_symbol))
542+
end_mark = edit_calculate_end_of_previous_line (buf, end_mark, end_string_symbol);
543+
544+
if (start_from_next_line)
545+
search_start =
546+
edit_calculate_start_of_next_line (buf, search_start, end_mark, end_string_symbol);
523547

524548
return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
525549
}
@@ -923,7 +947,7 @@ edit_replace_cmd (WEdit *edit, gboolean again)
923947
edit->found_start = edit->search_start;
924948
edit->found_len = len;
925949

926-
edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
950+
edit_cursor_move (edit, edit->found_start - edit->buffer.curs1);
927951
edit_scroll_screen_over_cursor (edit);
928952

929953
if (edit->replace_mode == 0)

0 commit comments

Comments
 (0)