Skip to content

Commit

Permalink
Merge branch '4587_mcview_search_bol'
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
aborodin committed Dec 15, 2024
2 parents 7950ae8 + a83a6f4 commit 511d4d8
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 205 deletions.
14 changes: 14 additions & 0 deletions lib/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
27 changes: 27 additions & 0 deletions lib/search/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* --------------------------------------------------------------------------------------------- */
37 changes: 6 additions & 31 deletions src/editor/editsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 =
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 2 additions & 12 deletions src/editor/editwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
34 changes: 0 additions & 34 deletions src/viewer/actions_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 3 additions & 1 deletion src/viewer/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 ****************************************************************************/
Expand Down
3 changes: 3 additions & 0 deletions src/viewer/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Loading

0 comments on commit 511d4d8

Please sign in to comment.