Merge branch '4587_mcview_search_bol'

* 4587_mcview_search_bol:
  (mcview_moveto_bottom): fix search start and stop positions.
  mcview: fix regex search.
  mcview: minor refactoring.
  Ticket #4587: mcviewer: fix BOL regex search.
This commit is contained in:
Andrew Borodin 2024-12-15 13:48:22 +03:00
commit 511d4d853b
8 changed files with 247 additions and 208 deletions

View File

@ -60,6 +60,18 @@ typedef enum
MC_SEARCH_T_GLOB
} mc_search_type_t;
/**
* enum to store search conditions check results.
* (whether the search condition has BOL (^) or EOL ($) regexp characters).
*/
typedef enum
{
MC_SEARCH_LINE_NONE = 0,
MC_SEARCH_LINE_BEGIN = 1 << 0,
MC_SEARCH_LINE_END = 1 << 1,
MC_SEARCH_LINE_ENTIRE = MC_SEARCH_LINE_BEGIN | MC_SEARCH_LINE_END
} mc_search_line_t;
enum mc_search_cbret_t
{
MC_SEARCH_CB_OK = 0,
@ -185,6 +197,8 @@ gchar **mc_search_get_types_strings_array (size_t *num);
gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str,
mc_search_type_t type);
mc_search_line_t mc_search_get_line_type (const mc_search_t *search);
int mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index);
int mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index);

View File

@ -1114,3 +1114,30 @@ mc_search_regex_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace
return ret;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Checks whether search condition has BOL (^) or EOL ($) regexp special characters.
*
* @param search search object
* @return check result
*/
mc_search_line_t
mc_search_get_line_type (const mc_search_t *search)
{
mc_search_line_t search_line_type = MC_SEARCH_LINE_NONE;
if (search->search_type == MC_SEARCH_T_REGEX)
{
if (search->original.str->str[0] == '^')
search_line_type |= MC_SEARCH_LINE_BEGIN;
if (search->original.str->str[search->original.str->len - 1] == '$')
search_line_type |= MC_SEARCH_LINE_END;
}
return search_line_type;
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -291,31 +291,6 @@ edit_search_get_current_end_line_char (const WEdit *edit)
}
}
/* --------------------------------------------------------------------------------------------- */
/**
* Checking if search condition have BOL(^) or EOL ($) regexp special characters.
*
* @param search search object
* @return result of checks.
*/
static edit_search_line_t
edit_get_search_line_type (const mc_search_t *search)
{
edit_search_line_t search_line_type = 0;
if (search->search_type == MC_SEARCH_T_REGEX)
{
if (search->original.str->str[0] == '^')
search_line_type |= AT_START_LINE;
if (search->original.str->str[search->original.str->len - 1] == '$')
search_line_type |= AT_END_LINE;
}
return search_line_type;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Calculating the start position of next line.
@ -460,14 +435,14 @@ 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 & AT_START_LINE) != 0
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0
&& (start_mark != 0
|| edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
start_mark =
edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
end_string_symbol);
if ((edit->search_line_type & AT_END_LINE) != 0
if ((edit->search_line_type & MC_SEARCH_LINE_END) != 0
&& (end_mark - 1 != edit->buffer.size
|| edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
end_mark =
@ -488,7 +463,7 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
/* backward search */
search_end = end_mark;
if ((edit->search_line_type & AT_START_LINE) != 0)
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
search_start =
edit_calculate_start_of_current_line (&edit->buffer, search_start,
end_string_symbol);
@ -511,7 +486,7 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
if (!ok && edit->search->error != MC_SEARCH_E_NOTFOUND)
return FALSE;
if ((edit->search_line_type & AT_START_LINE) != 0)
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
search_start =
edit_calculate_start_of_previous_line (&edit->buffer, search_start,
end_string_symbol);
@ -524,7 +499,7 @@ edit_find (edit_search_status_msg_t *esm, gsize *len)
}
/* forward search */
if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
if ((edit->search_line_type & MC_SEARCH_LINE_BEGIN) != 0 && search_start != start_mark)
search_start =
edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
end_string_symbol);
@ -697,7 +672,7 @@ edit_search_init (WEdit *edit, const char *str)
edit->search->search_fn = edit_search_cmd_callback;
edit->search->update_fn = edit_search_update_callback;
edit->search_line_type = edit_get_search_line_type (edit->search);
edit->search_line_type = mc_search_get_line_type (edit->search);
edit_search_fix_search_start_if_selection (edit);

View File

@ -19,16 +19,6 @@
/*** enums ***************************************************************************************/
/**
enum for store the search conditions check results.
(if search condition have BOL(^) or EOL ($) regexp checial characters).
*/
typedef enum
{
AT_START_LINE = (1 << 0),
AT_END_LINE = (1 << 1)
} edit_search_line_t;
/*** structures declarations (and typedefs of structures)*****************************************/
typedef struct edit_book_mark_t edit_book_mark_t;
@ -89,8 +79,8 @@ struct WEdit
/* search handler */
mc_search_t *search;
int replace_mode;
/* is search conditions should be started from BOL(^) or ended with EOL($) */
edit_search_line_t search_line_type;
/* whether search conditions should be started with BOL(^) or ended with EOL($) */
mc_search_line_t search_line_type;
char *last_search_string; /* String that have been searched */
off_t search_start; /* First character to start searching from */

View File

@ -97,40 +97,6 @@ mcview_remove_ext_script (WView *view)
/* --------------------------------------------------------------------------------------------- */
/* Both views */
static void
mcview_search (WView *view, gboolean start_search)
{
off_t want_search_start = view->search_start;
if (start_search)
{
if (mcview_dialog_search (view))
{
if (view->mode_flags.hex)
want_search_start = view->hex_cursor;
mcview_do_search (view, want_search_start);
}
}
else
{
if (view->mode_flags.hex)
{
if (!mcview_search_options.backwards)
want_search_start = view->hex_cursor + 1;
else if (view->hex_cursor > 0)
want_search_start = view->hex_cursor - 1;
else
want_search_start = 0;
}
mcview_do_search (view, want_search_start);
}
}
/* --------------------------------------------------------------------------------------------- */
static void
mcview_continue_search_cmd (WView *view)
{

View File

@ -170,6 +170,8 @@ struct WView
off_t search_start; /* First character to start searching from */
off_t search_end; /* Length of found string or 0 if none was found */
int search_numNeedSkipChar;
/* whether search conditions should be started with BOL(^) or ended with EOL($) */
mc_search_line_t search_line_type;
/* Markers */
int marker; /* mark to use */
@ -328,7 +330,7 @@ void mcview_search_deinit (WView * view);
mc_search_cbret_t mcview_search_cmd_callback (const void *user_data, gsize char_offset,
int *current_char);
mc_search_cbret_t mcview_search_update_cmd_callback (const void *user_data, gsize char_offset);
void mcview_do_search (WView * view, off_t want_search_start);
void mcview_search (WView * view, gboolean start_search);
/* --------------------------------------------------------------------------------------------- */
/*** inline functions ****************************************************************************/

View File

@ -263,6 +263,9 @@ mcview_moveto_bottom (WView *view)
view->dpy_paragraph_skip_lines = 0;
view->dpy_wrap_dirty = TRUE;
mcview_move_up (view, view->data_area.lines);
/* start backward search from EOF */
view->search_start = filesize;
view->search_end = view->search_start;
}
}

View File

@ -116,6 +116,20 @@ mcview_search_status_update_cb (status_msg_t *sm)
/* --------------------------------------------------------------------------------------------- */
static inline off_t
mcview_calculate_start_of_previous_line (WView *view, const off_t current_pos)
{
const off_t bol = mcview_bol (view, current_pos, 0);
/* Are we in the 1st line? */
if (bol == 0)
return (-1);
return mcview_bol (view, bol - 1, 0);
}
/* --------------------------------------------------------------------------------------------- */
static void
mcview_search_update_steps (WView *view)
{
@ -150,6 +164,10 @@ mcview_find (mcview_search_status_msg_t *ssm, off_t search_start, off_t search_e
if (mcview_search_options.backwards)
{
search_end = mcview_get_filesize (view);
if ((view->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
search_start = mcview_bol (view, search_start, 0);
while (search_start >= 0)
{
gboolean ok;
@ -174,12 +192,19 @@ mcview_find (mcview_search_status_msg_t *ssm, off_t search_start, off_t search_e
if (!ok && view->search->error != MC_SEARCH_E_NOTFOUND)
return FALSE;
search_start--;
if ((view->search_line_type & MC_SEARCH_LINE_BEGIN) != 0)
search_start = mcview_calculate_start_of_previous_line (view, search_start);
else
search_start--;
}
mc_search_set_error (view->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
return FALSE;
}
if ((view->search_line_type & MC_SEARCH_LINE_BEGIN) != 0 && search_start != 0)
search_start = mcview_eol (view, search_start);
view->search_nroff_seq->index = search_start;
mcview_nroff_seq_info (view->search_nroff_seq);
@ -210,6 +235,150 @@ mcview_search_show_result (WView *view, size_t match_len)
mcview_moveto_match (view);
}
/* --------------------------------------------------------------------------------------------- */
static void
mcview_do_search (WView *view, off_t want_search_start)
{
mcview_search_status_msg_t vsm;
off_t search_start = 0;
off_t orig_search_start = view->search_start;
gboolean found = FALSE;
size_t match_len;
view->search_start = want_search_start;
/* to avoid infinite search loop we need to increase or decrease start offset of search */
if (view->search_start != 0)
{
if (!view->mode_flags.nroff)
search_start = view->search_start + (mcview_search_options.backwards ? -2 : 0);
else
{
if (mcview_search_options.backwards)
{
mcview_nroff_t *nroff;
nroff = mcview_nroff_seq_new_num (view, view->search_start);
if (mcview_nroff_seq_prev (nroff) != -1)
search_start =
-(mcview__get_nroff_real_len (view, nroff->index - 1, 2) +
nroff->char_length + 1);
else
search_start = -2;
mcview_nroff_seq_free (&nroff);
}
else
{
search_start = mcview__get_nroff_real_len (view, view->search_start + 1, 2);
}
search_start += view->search_start;
}
}
if (mcview_search_options.backwards && search_start < 0)
search_start = 0;
/* Compute the percent steps */
mcview_search_update_steps (view);
view->update_activate = search_start;
vsm.first = TRUE;
vsm.view = view;
vsm.offset = search_start;
status_msg_init (STATUS_MSG (&vsm), _("Search"), 1.0, simple_status_msg_init_cb,
mcview_search_status_update_cb, NULL);
do
{
off_t growbufsize;
if (view->growbuf_in_use)
growbufsize = mcview_growbuf_filesize (view);
else
growbufsize = view->search->original.str->len;
if (mcview_find (&vsm, search_start, mcview_get_filesize (view), &match_len))
{
mcview_search_show_result (view, match_len);
found = TRUE;
break;
}
/* Search error is here.
* MC_SEARCH_E_NOTFOUND: continue search
* others: stop
*/
if (view->search->error != MC_SEARCH_E_NOTFOUND)
break;
search_start = growbufsize - view->search->original.str->len;
}
while (search_start > 0 && mcview_may_still_grow (view));
/* After mcview_may_still_grow (view) == FALSE, last chunk remains. Search there. */
if (view->growbuf_in_use && !found && view->search->error == MC_SEARCH_E_NOTFOUND
&& !mcview_search_options.backwards
&& mcview_find (&vsm, search_start, mcview_get_filesize (view), &match_len))
{
mcview_search_show_result (view, match_len);
found = TRUE;
}
status_msg_deinit (STATUS_MSG (&vsm));
if (orig_search_start != 0 && (!found && view->search->error == MC_SEARCH_E_NOTFOUND)
&& !mcview_search_options.backwards)
{
view->search_start = orig_search_start;
mcview_update (view);
if (query_dialog
(_("Search done"), _("Continue from beginning?"), D_NORMAL, 2, _("&Yes"),
_("&No")) != 0)
found = TRUE;
else
{
/* continue search from beginning */
view->update_activate = 0;
vsm.first = TRUE;
vsm.view = view;
vsm.offset = 0;
status_msg_init (STATUS_MSG (&vsm), _("Search"), 1.0, simple_status_msg_init_cb,
mcview_search_status_update_cb, NULL);
/* search from file begin up to initial search start position */
if (mcview_find (&vsm, 0, orig_search_start, &match_len))
{
mcview_search_show_result (view, match_len);
found = TRUE;
}
status_msg_deinit (STATUS_MSG (&vsm));
}
}
if (!found)
{
view->search_start = orig_search_start;
mcview_update (view);
if (view->search->error == MC_SEARCH_E_NOTFOUND)
query_dialog (_("Search"), _(STR_E_NOTFOUND), D_NORMAL, 1, _("&Dismiss"));
else if (view->search->error_str != NULL)
query_dialog (_("Search"), view->search->error_str, D_NORMAL, 1, _("&Dismiss"));
}
view->dirty++;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
@ -237,6 +406,8 @@ mcview_search_init (WView *view)
view->search->search_fn = mcview_search_cmd_callback;
view->search->update_fn = mcview_search_update_cmd_callback;
view->search_line_type = mc_search_get_line_type (view->search);
return TRUE;
}
@ -347,145 +518,36 @@ mcview_search_update_cmd_callback (const void *user_data, gsize char_offset)
/* --------------------------------------------------------------------------------------------- */
/* Both views */
void
mcview_do_search (WView *view, off_t want_search_start)
mcview_search (WView *view, gboolean start_search)
{
mcview_search_status_msg_t vsm;
off_t want_search_start = view->search_start;
off_t search_start = 0;
off_t orig_search_start = view->search_start;
gboolean found = FALSE;
size_t match_len;
view->search_start = want_search_start;
/* for avoid infinite search loop we need to increase or decrease start offset of search */
if (view->search_start != 0)
if (start_search)
{
if (!view->mode_flags.nroff)
search_start = view->search_start + (mcview_search_options.backwards ? -2 : 0);
else
if (mcview_dialog_search (view))
{
if (mcview_search_options.backwards)
{
mcview_nroff_t *nroff;
if (view->mode_flags.hex)
want_search_start = view->hex_cursor;
nroff = mcview_nroff_seq_new_num (view, view->search_start);
if (mcview_nroff_seq_prev (nroff) != -1)
search_start =
-(mcview__get_nroff_real_len (view, nroff->index - 1, 2) +
nroff->char_length + 1);
else
search_start = -2;
mcview_nroff_seq_free (&nroff);
}
mcview_do_search (view, want_search_start);
}
}
else
{
if (view->mode_flags.hex)
{
if (!mcview_search_options.backwards)
want_search_start = view->hex_cursor + 1;
else if (view->hex_cursor > 0)
want_search_start = view->hex_cursor - 1;
else
{
search_start = mcview__get_nroff_real_len (view, view->search_start + 1, 2);
}
search_start += view->search_start;
}
}
if (mcview_search_options.backwards && search_start < 0)
search_start = 0;
/* Compute the percent steps */
mcview_search_update_steps (view);
view->update_activate = search_start;
vsm.first = TRUE;
vsm.view = view;
vsm.offset = search_start;
status_msg_init (STATUS_MSG (&vsm), _("Search"), 1.0, simple_status_msg_init_cb,
mcview_search_status_update_cb, NULL);
do
{
off_t growbufsize;
if (view->growbuf_in_use)
growbufsize = mcview_growbuf_filesize (view);
else
growbufsize = view->search->original.str->len;
if (mcview_find (&vsm, search_start, mcview_get_filesize (view), &match_len))
{
mcview_search_show_result (view, match_len);
found = TRUE;
break;
want_search_start = 0;
}
/* Search error is here.
* MC_SEARCH_E_NOTFOUND: continue search
* others: stop
*/
if (view->search->error != MC_SEARCH_E_NOTFOUND)
break;
search_start = growbufsize - view->search->original.str->len;
mcview_do_search (view, want_search_start);
}
while (search_start > 0 && mcview_may_still_grow (view));
/* After mcview_may_still_grow (view) == FALSE we have remained last chunk. Search there. */
if (view->growbuf_in_use && !found && view->search->error == MC_SEARCH_E_NOTFOUND
&& !mcview_search_options.backwards
&& mcview_find (&vsm, search_start, mcview_get_filesize (view), &match_len))
{
mcview_search_show_result (view, match_len);
found = TRUE;
}
status_msg_deinit (STATUS_MSG (&vsm));
if (orig_search_start != 0 && (!found && view->search->error == MC_SEARCH_E_NOTFOUND)
&& !mcview_search_options.backwards)
{
view->search_start = orig_search_start;
mcview_update (view);
if (query_dialog
(_("Search done"), _("Continue from beginning?"), D_NORMAL, 2, _("&Yes"),
_("&No")) != 0)
found = TRUE;
else
{
/* continue search from beginning */
view->update_activate = 0;
vsm.first = TRUE;
vsm.view = view;
vsm.offset = 0;
status_msg_init (STATUS_MSG (&vsm), _("Search"), 1.0, simple_status_msg_init_cb,
mcview_search_status_update_cb, NULL);
/* search from file begin up to initial search start position */
if (mcview_find (&vsm, 0, orig_search_start, &match_len))
{
mcview_search_show_result (view, match_len);
found = TRUE;
}
status_msg_deinit (STATUS_MSG (&vsm));
}
}
if (!found)
{
view->search_start = orig_search_start;
mcview_update (view);
if (view->search->error == MC_SEARCH_E_NOTFOUND)
query_dialog (_("Search"), _(STR_E_NOTFOUND), D_NORMAL, 1, _("&Dismiss"));
else if (view->search->error_str != NULL)
query_dialog (_("Search"), view->search->error_str, D_NORMAL, 1, _("&Dismiss"));
}
view->dirty++;
}
/* --------------------------------------------------------------------------------------------- */