Merge branch '2417_edit_search_charset'

* 2417_edit_search_charset:
  Reimplemented search engine in editor.
  Ticket #2417: incorrect charset conversion in editor search engine.
This commit is contained in:
Andrew Borodin 2010-11-22 16:20:50 +03:00
commit 58ea06dadf
6 changed files with 252 additions and 269 deletions

View File

@ -224,7 +224,8 @@ void edit_mark_cmd (WEdit * edit, int unmark);
void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2); void edit_set_markers (WEdit * edit, long m1, long m2, int c1, int c2);
void edit_push_markers (WEdit * edit); void edit_push_markers (WEdit * edit);
void edit_replace_cmd (WEdit * edit, int again); void edit_replace_cmd (WEdit * edit, int again);
void edit_search_cmd (WEdit * edit, int again); void edit_search_cmd (WEdit * edit, gboolean again);
int edit_search_cmd_callback (const void *user_data, gsize char_offset);
void edit_complete_word_cmd (WEdit * edit); void edit_complete_word_cmd (WEdit * edit);
void edit_get_match_keyword_cmd (WEdit * edit); void edit_get_match_keyword_cmd (WEdit * edit);
int edit_save_block (WEdit * edit, const char *filename, long start, long finish); int edit_save_block (WEdit * edit, const char *filename, long start, long finish);
@ -335,6 +336,6 @@ extern int option_edit_top_extreme;
extern int option_edit_bottom_extreme; extern int option_edit_bottom_extreme;
extern const char *option_whole_chars_search; extern const char *option_whole_chars_search;
extern int search_create_bookmark; extern gboolean search_create_bookmark;
#endif /* MC_EDIT_IMPL_H */ #endif /* MC_EDIT_IMPL_H */

View File

@ -55,6 +55,7 @@ struct WEdit
mc_search_t *search; mc_search_t *search;
int replace_mode; int replace_mode;
char *last_search_string; /* String that have been searched */
long search_start; /* First character to start searching from */ long search_start; /* First character to start searching from */
int found_len; /* Length of found string or 0 if none was found */ int found_len; /* Length of found string or 0 if none was found */
long found_start; /* the found word from a search - start position */ long found_start; /* the found word from a search - start position */

View File

@ -931,7 +931,6 @@ edit_init (WEdit * edit, int lines, int columns, const char *filename, long line
} }
#endif /* ENABLE_NLS */ #endif /* ENABLE_NLS */
edit = g_malloc0 (sizeof (WEdit)); edit = g_malloc0 (sizeof (WEdit));
edit->search = NULL;
to_free = 1; to_free = 1;
} }
edit_purge_widget (edit); edit_purge_widget (edit);
@ -1025,6 +1024,8 @@ edit_clean (WEdit * edit)
mc_search_free (edit->search); mc_search_free (edit->search);
edit->search = NULL; edit->search = NULL;
g_free (edit->last_search_string);
edit->last_search_string = NULL;
if (edit->converter != str_cnv_from_term) if (edit->converter != str_cnv_from_term)
str_close_conv (edit->converter); str_close_conv (edit->converter);
@ -3539,10 +3540,10 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
break; break;
case CK_Find: case CK_Find:
edit_search_cmd (edit, 0); edit_search_cmd (edit, FALSE);
break; break;
case CK_Find_Again: case CK_Find_Again:
edit_search_cmd (edit, 1); edit_search_cmd (edit, TRUE);
break; break;
case CK_Replace: case CK_Replace:
edit_replace_cmd (edit, 0); edit_replace_cmd (edit, 0);

View File

@ -75,8 +75,7 @@
/* globals: */ /* globals: */
/* search and replace: */ /* search and replace: */
int search_create_bookmark = 0; int search_create_bookmark = FALSE;
/* static int search_in_all_charsets = 0; */
/* queries on a save */ /* queries on a save */
int edit_confirm_save = 1; int edit_confirm_save = 1;
@ -84,46 +83,7 @@ int edit_confirm_save = 1;
static int edit_save_cmd (WEdit * edit); static int edit_save_cmd (WEdit * edit);
static unsigned char *edit_get_block (WEdit * edit, long start, long finish, int *l); static unsigned char *edit_get_block (WEdit * edit, long start, long finish, int *l);
static void int
edit_search_cmd_search_create_bookmark (WEdit * edit)
{
int found = 0, books = 0;
long l = 0, l_last = -1;
long q = 0;
gsize len = 0;
search_create_bookmark = 0;
book_mark_flush (edit, -1);
for (;;)
{
if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
break;
if (found == 0)
edit->search_start = edit->search->normal_offset;
found++;
l += edit_count_lines (edit, q, edit->search->normal_offset);
if (l != l_last)
{
book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
books++;
}
l_last = l;
q = edit->search->normal_offset + 1;
}
if (found == 0)
{
edit_error_dialog (_("Search"), _("Search string not found"));
}
else
{
edit_cursor_move (edit, edit->search_start - edit->curs1);
edit_scroll_screen_over_cursor (edit);
}
}
static int
edit_search_cmd_callback (const void *user_data, gsize char_offset) edit_search_cmd_callback (const void *user_data, gsize char_offset)
{ {
return edit_get_byte ((WEdit *) user_data, (long) char_offset); return edit_get_byte ((WEdit *) user_data, (long) char_offset);
@ -1902,121 +1862,54 @@ edit_replace_cmd (WEdit * edit, int again)
g_free (input2); g_free (input2);
} }
static void
void edit_do_search (WEdit * edit)
edit_search_cmd (WEdit * edit, int again)
{ {
char *search_string = NULL, *search_string_dup = NULL;
gsize len = 0; gsize len = 0;
if (!edit) if (edit->search == NULL)
return;
if (edit->search != NULL)
{
search_string = g_strndup (edit->search->original, edit->search->original_len);
search_string_dup = search_string;
}
else
{
GList *history;
history = history_get (MC_HISTORY_SHARED_SEARCH);
if (history != NULL && history->data != NULL)
{
search_string_dup = search_string = (char *) g_strdup (history->data);
history = g_list_first (history);
g_list_foreach (history, (GFunc) g_free, NULL);
g_list_free (history);
}
edit->search_start = edit->curs1; edit->search_start = edit->curs1;
}
if (!again)
{
#ifdef HAVE_CHARSET
GString *tmp;
if (search_string && *search_string)
{
tmp = str_convert_to_display (search_string);
if (tmp != NULL)
{
if (tmp->len == 0)
g_string_free (tmp, TRUE);
else
{
g_free (search_string);
search_string = search_string_dup = g_string_free (tmp, FALSE);
}
}
}
#endif /* HAVE_CHARSET */
editcmd_dialog_search_show (edit, &search_string);
g_free (search_string_dup);
search_string_dup = NULL;
#ifdef HAVE_CHARSET
if (search_string && *search_string)
{
tmp = str_convert_to_input (search_string);
if (tmp != NULL)
{
if (tmp->len == 0)
g_string_free (tmp, TRUE);
else
{
g_free (search_string);
search_string = g_string_free (tmp, FALSE);
}
}
}
#endif /* HAVE_CHARSET */
edit_push_action (edit, KEY_PRESS + edit->start_display); edit_push_action (edit, KEY_PRESS + edit->start_display);
if (search_string == NULL)
{
edit->force |= REDRAW_COMPLETELY;
edit_scroll_screen_over_cursor (edit);
return;
}
if (edit->search)
{
mc_search_free (edit->search);
edit->search = NULL;
}
}
if (!edit->search)
{
edit->search = mc_search_new (search_string, -1);
if (edit->search == NULL)
{
edit->search_start = edit->curs1;
g_free (search_string);
return;
}
edit->search->search_type = edit_search_options.type;
edit->search->is_all_charsets = edit_search_options.all_codepages;
edit->search->is_case_sensitive = edit_search_options.case_sens;
edit->search->whole_words = edit_search_options.whole_words;
edit->search->search_fn = edit_search_cmd_callback;
}
g_free (search_string);
if (search_create_bookmark) if (search_create_bookmark)
{ {
edit_search_cmd_search_create_bookmark (edit); int found = 0, books = 0;
long l = 0, l_last = -1;
long q = 0;
search_create_bookmark = FALSE;
book_mark_flush (edit, -1);
while (TRUE)
{
if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
break;
if (found == 0)
edit->search_start = edit->search->normal_offset;
found++;
l += edit_count_lines (edit, q, edit->search->normal_offset);
if (l != l_last)
{
book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
books++;
}
l_last = l;
q = edit->search->normal_offset + 1;
}
if (found == 0)
edit_error_dialog (_("Search"), _("Search string not found"));
else
edit_cursor_move (edit, edit->search_start - edit->curs1);
} }
else else
{ {
if (edit->found_len && edit->search_start == edit->found_start + 1 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
&& edit_search_options.backwards) && edit_search_options.backwards)
edit->search_start--; edit->search_start--;
if (edit->found_len && edit->search_start == edit->found_start - 1 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
&& !edit_search_options.backwards) && !edit_search_options.backwards)
edit->search_start++; edit->search_start++;
@ -2035,7 +1928,7 @@ edit_search_cmd (WEdit * edit, int again)
else else
{ {
edit->search_start = edit->curs1; edit->search_start = edit->curs1;
if (edit->search->error_str) if (edit->search->error_str != NULL)
edit_error_dialog (_("Search"), edit->search->error_str); edit_error_dialog (_("Search"), edit->search->error_str);
} }
} }
@ -2044,6 +1937,65 @@ edit_search_cmd (WEdit * edit, int again)
edit_scroll_screen_over_cursor (edit); edit_scroll_screen_over_cursor (edit);
} }
static void
edit_search (WEdit *edit)
{
if (editcmd_dialog_search_show (edit))
edit_do_search (edit);
}
void
edit_search_cmd (WEdit * edit, gboolean again)
{
if (edit == NULL)
return;
if (!again)
edit_search (edit);
else if (edit->last_search_string != NULL)
edit_do_search (edit);
else
{
/* find last search string in history */
GList *history;
history = history_get (MC_HISTORY_SHARED_SEARCH);
if (history != NULL && history->data != NULL)
{
edit->last_search_string = (char *) history->data;
history->data = NULL;
history = g_list_first (history);
g_list_foreach (history, (GFunc) g_free, NULL);
g_list_free (history);
edit->search = mc_search_new (edit->last_search_string, -1);
if (edit->search == NULL)
{
/* if not... then ask for an expression */
g_free (edit->last_search_string);
edit->last_search_string = NULL;
edit_search (edit);
}
else
{
edit->search->search_type = edit_search_options.type;
edit->search->is_all_charsets = edit_search_options.all_codepages;
edit->search->is_case_sensitive = edit_search_options.case_sens;
edit->search->whole_words = edit_search_options.whole_words;
edit->search->search_fn = edit_search_cmd_callback;
edit_do_search (edit);
}
}
else
{
/* if not... then ask for an expression */
g_free (edit->last_search_string);
edit->last_search_string = NULL;
edit_search (edit);
}
}
}
/* /*
* Check if it's OK to close the editor. If there are unsaved changes, * Check if it's OK to close the editor. If there are unsaved changes,

View File

@ -157,15 +157,11 @@ editcmd_dialog_replace_show (WEdit * edit, const char *search_default, const cha
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
void gboolean
editcmd_dialog_search_show (WEdit * edit, char **search_text) editcmd_dialog_search_show (WEdit * edit)
{ {
(void) edit; char *search_text;
if (*search_text == NULL)
*search_text = INPUT_LAST_TEXT;
{
size_t num_of_types; size_t num_of_types;
gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types); gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + num_of_types - SEARCH_DLG_HEIGHT_SUPPLY; int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + num_of_types - SEARCH_DLG_HEIGHT_SUPPLY;
@ -203,8 +199,8 @@ editcmd_dialog_search_show (WEdit * edit, char **search_text)
(int *) &edit_search_options.type), (int *) &edit_search_options.type),
/* 9 */ /* 9 */
QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT, QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT,
*search_text, SEARCH_DLG_WIDTH - 6, 0, INPUT_LAST_TEXT, SEARCH_DLG_WIDTH - 6, 0,
MC_HISTORY_SHARED_SEARCH, search_text), MC_HISTORY_SHARED_SEARCH, &search_text),
/* 10 */ /* 10 */
QUICK_LABEL (3, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_("Enter search string:")), QUICK_LABEL (3, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_("Enter search string:")),
QUICK_END QUICK_END
@ -294,11 +290,43 @@ editcmd_dialog_search_show (WEdit * edit, char **search_text)
g_strfreev (list_of_types); g_strfreev (list_of_types);
if (dialog_result == B_CANCEL) if ((dialog_result == B_CANCEL) || (search_text == NULL) || (search_text[0] == '\0'))
*search_text = NULL; {
else if (dialog_result == B_USER) g_free (search_text);
search_create_bookmark = 1; return FALSE;
} }
if (dialog_result == B_USER)
search_create_bookmark = TRUE;
#ifdef HAVE_CHARSET
{
GString *tmp;
tmp = str_convert_to_input (search_text);
if (tmp != NULL)
{
g_free (search_text);
search_text = g_string_free (tmp, FALSE);
}
}
#endif
g_free (edit->last_search_string);
edit->last_search_string = search_text;
mc_search_free (edit->search);
edit->search = mc_search_new (edit->last_search_string, -1);
if (edit->search != NULL)
{
edit->search->search_type = edit_search_options.type;
edit->search->is_all_charsets = edit_search_options.all_codepages;
edit->search->is_case_sensitive = edit_search_options.case_sens;
edit->search->whole_words = edit_search_options.whole_words;
edit->search->search_fn = edit_search_cmd_callback;
}
return (edit->search != NULL);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */

View File

@ -26,7 +26,7 @@ struct selection {
void editcmd_dialog_replace_show (WEdit *, const char *, const char *, char **, char **); void editcmd_dialog_replace_show (WEdit *, const char *, const char *, char **, char **);
void editcmd_dialog_search_show (WEdit *, char **); gboolean editcmd_dialog_search_show (WEdit *edit);
int editcmd_dialog_raw_key_query (const char *, const char *, int); int editcmd_dialog_raw_key_query (const char *, const char *, int);