>From 57a0992b735e3846a8d1a0317c329e793797354b Mon Sep 17 00:00:00 2001 From: Mickael Albertus Date: Sat, 27 Apr 2013 08:56:31 +0200 Subject: [PATCH] Repair search&replace functions --- src/gtr-actions-go.c | 14 ++--- src/gtr-actions-search.c | 153 ++++++++++++++++++----------------------------- src/gtr-tab.c | 68 ++++++++++++--------- src/gtr-tab.h | 16 ++--- src/gtr-view.c | 105 ++++++++++++++++---------------- 5 files changed, 167 insertions(+), 189 deletions(-) diff --git a/src/gtr-actions-go.c b/src/gtr-actions-go.c index 4d10242..37bfefa 100644 --- a/src/gtr-actions-go.c +++ b/src/gtr-actions-go.c @@ -1,16 +1,16 @@ /* * Copyright (C) 2007 Ignacio Casal Quinteiro - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * @@ -39,7 +39,7 @@ gtr_message_go_to_first (GtkAction * action, GtrWindow * window) current = gtr_window_get_active_tab (window); po = gtr_tab_get_po (current); - gtr_tab_go_to_first (current); + gtr_tab_go_to_first (current, FALSE); _gtr_window_set_sensitive_according_to_message (window, po); } @@ -51,7 +51,7 @@ gtr_message_go_to_previous (GtkAction * action, GtrWindow * window) current = gtr_window_get_active_tab (window); po = gtr_tab_get_po (current); - gtr_tab_go_to_prev (current); + gtr_tab_go_to_prev (current, FALSE); _gtr_window_set_sensitive_according_to_message (window, po); } @@ -63,7 +63,7 @@ gtr_message_go_to_next (GtkAction * action, GtrWindow * window) current = gtr_window_get_active_tab (window); po = gtr_tab_get_po (current); - gtr_tab_go_to_next (current); + gtr_tab_go_to_next (current, FALSE); _gtr_window_set_sensitive_according_to_message (window, po); } @@ -75,7 +75,7 @@ gtr_message_go_to_last (GtkAction * action, GtrWindow * window) current = gtr_window_get_active_tab (window); po = gtr_tab_get_po (current); - gtr_tab_go_to_last (current); + gtr_tab_go_to_last (current, FALSE); _gtr_window_set_sensitive_according_to_message (window, po); } diff --git a/src/gtr-actions-search.c b/src/gtr-actions-search.c index 8872726..bd3fb46 100644 --- a/src/gtr-actions-search.c +++ b/src/gtr-actions-search.c @@ -193,7 +193,7 @@ phrase_not_found (GtrWindow * window) } static gboolean -run_search (GtrView * view, gboolean follow) +run_search (GtrView * view) { GtkSourceBuffer *doc; GtkTextIter start_iter; @@ -205,121 +205,86 @@ run_search (GtrView * view, gboolean follow) doc = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view))); - if (!follow) + if(!gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (doc), + NULL, &start_iter)) gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (doc), &start_iter); - else - gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (doc), - NULL, &start_iter); - found = gtr_view_search_forward (view, - &start_iter, + found = gtr_view_search_forward (view, &start_iter, NULL, &match_start, &match_end); if (found) - { - gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (doc), &match_start); - - gtk_text_buffer_move_mark_by_name (GTK_TEXT_BUFFER (doc), - "selection_bound", &match_end); - - } + gtk_text_buffer_select_range(GTK_TEXT_BUFFER(doc), &match_start, &match_end); else - { - gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (doc), &start_iter); - } + gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (doc), &start_iter); return found; } static gboolean find_in_list (GtrWindow * window, - GList * views, + gboolean original_text, gboolean translated_text, gboolean fuzzy, gboolean wrap_around, gboolean search_backwards) { GtrTab *tab = gtr_window_get_active_tab (window); GtrPo *po = gtr_tab_get_po (tab); GList *l = gtr_po_get_current_message (po); - GList *current; - static GList *viewsaux = NULL; - - current = l; - - if (viewsaux == NULL) - viewsaux = views; - - /* - * Variable used to know when start search in from the beggining of the view - */ - static gboolean found = FALSE; + GList *current = l; do { - if (gtr_msg_is_fuzzy (GTR_MSG (l->data)) && !fuzzy) + if (!gtr_msg_is_fuzzy (GTR_MSG (l->data)) || fuzzy) { - if (!search_backwards) + GtrView *translated_view = gtr_tab_get_active_view(tab); + GtrView *original_view = gtr_tab_get_active_original_view(tab); + + if (original_text) + { + if (run_search(original_view)) + return TRUE; + else if (translated_text) { - if (l->next == NULL) - { - if (!wrap_around) - return FALSE; - l = g_list_first (l); - } - else - l = l->next; + if (run_search(translated_view)) + return TRUE; } - else - { - if (l->prev == NULL) - { - if (!wrap_around) - return FALSE; - l = g_list_last (l); - } - else - l = l->prev; - } - gtr_tab_message_go_to (tab, l->data, TRUE, GTR_TAB_MOVE_NONE); + } + else if (translated_text) + { + if (run_search(translated_view)) + return TRUE; + } } - else - { - while (viewsaux != NULL) - { - gboolean aux = found; - - found = run_search (GTR_VIEW (viewsaux->data), found); - if (found) - { - gtr_tab_message_go_to (tab, l->data, FALSE, GTR_TAB_MOVE_NONE); - run_search (GTR_VIEW (viewsaux->data), aux); - return TRUE; - } - viewsaux = viewsaux->next; - } - if (!search_backwards) + + if (!search_backwards) + { + if (l->next == NULL) + { + if (!wrap_around) + return FALSE; + + l = g_list_first (l); + gtr_tab_go_to_first(tab, TRUE); + } + else { - if (l->next == NULL) - { - if (!wrap_around) - return FALSE; - l = g_list_first (l); - } - else - l = l->next; + l = l->next; + gtr_tab_go_to_next(tab, TRUE); } + } else + { + if (l->prev == NULL) + { + if (!wrap_around) + return FALSE; + l = g_list_last (l); + gtr_tab_go_to_last(tab, TRUE); + } + else { - if (l->prev == NULL) - { - if (!wrap_around) - return FALSE; - l = g_list_last (l); - } - else - l = l->prev; + l = l->prev; + gtr_tab_go_to_prev(tab, TRUE); } - gtr_tab_message_go_to (tab, l->data, TRUE, GTR_TAB_MOVE_NONE); - viewsaux = views; - } + } } while (l != current); @@ -330,7 +295,7 @@ static void do_find (GtrSearchDialog * dialog, GtrWindow * window) { GtrTab *tab; - GList *views, *list; + GList *views; gchar *search_text; const gchar *entry_text; gboolean original_text; @@ -368,27 +333,25 @@ do_find (GtrSearchDialog * dialog, GtrWindow * window) g_return_if_fail (views != NULL); - list = views; - GTR_SEARCH_SET_CASE_SENSITIVE (flags, match_case); GTR_SEARCH_SET_ENTIRE_WORD (flags, entire_word); - while (list != NULL) + while (views != NULL) { search_text = - gtr_view_get_search_text (GTR_VIEW (list->data), &old_flags); + gtr_view_get_search_text (GTR_VIEW (views->data), &old_flags); if ((search_text == NULL) || (strcmp (search_text, entry_text) != 0) || (flags != old_flags)) { - gtr_view_set_search_text (GTR_VIEW (list->data), entry_text, flags); + gtr_view_set_search_text (GTR_VIEW (views->data), entry_text, flags); } g_free (search_text); - list = list->next; + views = views->next; } - found = find_in_list (window, views, fuzzy, wrap_around, search_backwards); + found = find_in_list (window, original_text, translated_text, fuzzy, wrap_around, search_backwards); if (found) phrase_found (window, 0); diff --git a/src/gtr-tab.c b/src/gtr-tab.c index 211c7ff..879c251 100644 --- a/src/gtr-tab.c +++ b/src/gtr-tab.c @@ -10,12 +10,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * @@ -474,7 +474,7 @@ gtr_message_plural_forms (GtrTab * tab, GtrMsg * msg) * gtr_tab_show_message: * @tab: a #GtranslationTab * @msg: a #GtrMsg - * + * * Shows the @msg in the @tab TextViews * */ @@ -1075,9 +1075,9 @@ gtr_tab_class_init (GtrTabClass * klass) /** * gtr_tab_new: * @po: a #GtrPo - * + * * Creates a new #GtrTab. - * + * * Return value: a new #GtrTab object **/ GtrTab * @@ -1126,7 +1126,7 @@ gtr_tab_get_po (GtrTab * tab) /** * gtr_tab_get_active_trans_tab: * @tab: a #GtranslationTab - * + * * Return value: the number of the active translation notebook. **/ gint @@ -1165,6 +1165,18 @@ gtr_tab_get_active_view (GtrTab * tab) } /** + * gtr_tab_get_active_original_view: + * @tab: a #GtranslationTab + * + * Return value: (transfer none): the active page of the original active text. +**/ +GtrView * +gtr_tab_get_active_original_view (GtrTab * tab) +{ + return GTR_VIEW (tab->priv->text_msgid); +} + +/** * gtr_tab_get_all_views: * @tab: the #GtranslationTab * @original: TRUE if you want original TextViews. @@ -1231,10 +1243,10 @@ gtr_tab_message_go_to (GtrTab * tab, * we have to change to the next/prev plural tab in case is not * the last * To implement that: - * if the tabs are showed then we check if we want prev or - * next and then if we need to change the tab we change it + * if the tabs are showed then we check if we want prev or + * next and then if we need to change the tab we change it * in other case we show the message - * + * * I don't like too much this implementation so if anybody can * rewrite this is a better way would be great. */ @@ -1290,8 +1302,8 @@ gtr_tab_message_go_to (GtrTab * tab, /** * _gtr_tab_get_name: - * @tab: a #GtrTab - * + * @tab: a #GtrTab + * * Return value: a new allocated string with the name of the @tab. */ gchar * @@ -1371,9 +1383,9 @@ gtr_tab_get_from_document (GtrPo * po) /** * gtr_tab_get_autosave_enabled: * @tab: a #GtrTab - * + * * Gets the current state for the autosave feature - * + * * Return value: TRUE if the autosave is enabled, else FALSE **/ gboolean @@ -1388,7 +1400,7 @@ gtr_tab_get_autosave_enabled (GtrTab * tab) * gtr_tab_set_autosave_enabled: * @tab: a #GtrTab * @enable: enable (TRUE) or disable (FALSE) auto save - * + * * Enables or disables the autosave feature. It does not install an * autosave timeout if the document is new or is read-only **/ @@ -1422,9 +1434,9 @@ gtr_tab_set_autosave_enabled (GtrTab * tab, gboolean enable) /** * gtr_tab_get_autosave_interval: * @tab: a #GtrTab - * + * * Gets the current interval for the autosaves - * + * * Return value: the value of the autosave **/ gint @@ -1439,7 +1451,7 @@ gtr_tab_get_autosave_interval (GtrTab * tab) * gtr_tab_set_autosave_interval: * @tab: a #GtrTab * @interval: the new interval - * + * * Sets the interval for the autosave feature. It does nothing if the * interval is the same as the one already present. It removes the old * interval timeout and adds a new one with the autosave passed as @@ -1542,7 +1554,7 @@ gtr_tab_show_widget (GtrTab *tab, /** * gtr_tab_clear_msgstr_views: * @tab: a #GtrTab - * + * * Clears all text from msgstr text views. */ void @@ -1616,7 +1628,7 @@ gtr_tab_block_movement (GtrTab * tab) /** * gtr_tab_unblock_movement: * @tab: a #GtrTab - * + * * Unblocks the movement to the next/prev message. */ void @@ -1648,7 +1660,7 @@ _gtr_tab_finish_edition (GtrTab * tab) * Moves to the next message or plural tab in case the message has plurals. */ void -gtr_tab_go_to_next (GtrTab * tab) +gtr_tab_go_to_next (GtrTab * tab, gboolean searching) { GtrMsg *msg; @@ -1658,7 +1670,7 @@ gtr_tab_go_to_next (GtrTab * tab) GTR_NAVIGATE_NEXT, NULL); if (msg) gtr_tab_message_go_to (tab, msg, - FALSE, GTR_TAB_MOVE_NEXT); + searching, GTR_TAB_MOVE_NEXT); } } @@ -1669,7 +1681,7 @@ gtr_tab_go_to_next (GtrTab * tab) * Moves to the previous message or plural tab in case the message has plurals. */ void -gtr_tab_go_to_prev (GtrTab * tab) +gtr_tab_go_to_prev (GtrTab * tab, gboolean searching) { GtrMsg *msg; @@ -1679,7 +1691,7 @@ gtr_tab_go_to_prev (GtrTab * tab) GTR_NAVIGATE_PREV, NULL); if (msg) gtr_tab_message_go_to (tab, msg, - FALSE, GTR_TAB_MOVE_PREV); + searching, GTR_TAB_MOVE_PREV); } } @@ -1690,7 +1702,7 @@ gtr_tab_go_to_prev (GtrTab * tab) * Jumps to the first message. */ void -gtr_tab_go_to_first (GtrTab * tab) +gtr_tab_go_to_first (GtrTab * tab, gboolean searching) { GtrMsg *msg; @@ -1700,18 +1712,18 @@ gtr_tab_go_to_first (GtrTab * tab) GTR_NAVIGATE_FIRST, NULL); if (msg) gtr_tab_message_go_to (tab, msg, - FALSE, GTR_TAB_MOVE_NONE); + searching, GTR_TAB_MOVE_NONE); } } /** * gtr_tab_go_to_last: - * @tab: a #GtrTab + * @tab: a #GtrTab * * Jumps to the last message. */ void -gtr_tab_go_to_last (GtrTab * tab) +gtr_tab_go_to_last (GtrTab * tab, gboolean searching) { GtrMsg *msg; @@ -1721,7 +1733,7 @@ gtr_tab_go_to_last (GtrTab * tab) GTR_NAVIGATE_LAST, NULL); if (msg) gtr_tab_message_go_to (tab, msg, - FALSE, GTR_TAB_MOVE_NONE); + searching, GTR_TAB_MOVE_NONE); } } diff --git a/src/gtr-tab.h b/src/gtr-tab.h index 98b90fa..0d1ac04 100644 --- a/src/gtr-tab.h +++ b/src/gtr-tab.h @@ -1,17 +1,17 @@ /* * Copyright (C) 2007 Ignacio Casal Quinteiro - * 2008 Igalia + * 2008 Igalia * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * @@ -106,6 +106,8 @@ GtrContextPanel *gtr_tab_get_context_panel (GtrTab * tab); GtrView *gtr_tab_get_active_view (GtrTab * tab); +GtrView *gtr_tab_get_active_original_view (GtrTab * tab); + GList *gtr_tab_get_all_views (GtrTab * tab, gboolean original, gboolean translated); @@ -143,13 +145,13 @@ void gtr_tab_block_movement (GtrTab * tab); void gtr_tab_unblock_movement (GtrTab * tab); -void gtr_tab_go_to_next (GtrTab * tab); +void gtr_tab_go_to_next (GtrTab * tab, gboolean searching); -void gtr_tab_go_to_prev (GtrTab * tab); +void gtr_tab_go_to_prev (GtrTab * tab, gboolean searching); -void gtr_tab_go_to_first (GtrTab * tab); +void gtr_tab_go_to_first (GtrTab * tab, gboolean searching); -void gtr_tab_go_to_last (GtrTab * tab); +void gtr_tab_go_to_last (GtrTab * tab, gboolean searching); gboolean gtr_tab_go_to_next_fuzzy (GtrTab * tab); diff --git a/src/gtr-view.c b/src/gtr-view.c index b9c4f6a..2ea57de 100644 --- a/src/gtr-view.c +++ b/src/gtr-view.c @@ -1,21 +1,21 @@ /* * Copyright (C) 2007 Ignacio Casal Quinteiro * 1998, 1999 Alex Roberts, Evan Lawrence - * 2000 2002 Chema Celorio, Paolo Maggi - * 2003 2005 Paolo Maggi - * + * 2000 2002 Chema Celorio, Paolo Maggi + * 2003 2005 Paolo Maggi + * * Some funcs based in gedit-view.c file. - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * @@ -195,7 +195,7 @@ gtr_view_class_init (GtrViewClass * klass) * gtr_view_new: * * Creates a new #GtrView. An empty default buffer will be created for you. - * + * * Returns: a new #GtrView */ GtkWidget * @@ -250,7 +250,7 @@ gtr_view_get_selected_text (GtrView * view, * gtr_view_enable_spellcheck: * @view: a #GtrView * @enable: TRUE if you want enable the spellcheck - * + * * Enables the spellcheck **/ void @@ -386,7 +386,7 @@ gtr_view_paste_clipboard (GtrView * view) * @view: a #GtrView * @def: TRUE if you want to use the default font * @font_name: The name of the font you want to use in the #GtrView - * + * * Sets the #GtrView font. **/ void @@ -485,10 +485,10 @@ gtr_view_set_search_text (GtrView * view, const gchar * text, guint flags) * gtr_view_get_search_text: * @view: a #GtrView * @flags: the #GtrSearchFlags of the stored text. - * + * * Returns the text to search for it and the #GtrSearchFlags of that * text. - * + * * Returns: the text to search for it. */ gchar * @@ -505,7 +505,7 @@ gtr_view_get_search_text (GtrView * view, guint * flags) /** * gtr_view_get_can_search_again: * @view: a #GtrView - * + * * Returns: TRUE if it can search again */ gboolean @@ -520,17 +520,17 @@ gtr_view_get_can_search_again (GtrView * view) /** * gtr_view_search_forward: * @view: a #GtrView - * @start: start of search + * @start: start of search * @end: bound for the search, or %NULL for the end of the buffer * @match_start: return location for start of match, or %NULL * @match_end: return location for end of match, or %NULL - * + * * Searches forward for str. Any match is returned by setting match_start to the * first character of the match and match_end to the first character after the match. * The search will not continue past limit. * Note that a search is a linear or O(n) operation, so you may wish to use limit - * to avoid locking up your UI on large buffers. - * + * to avoid locking up your UI on large buffers. + * * Returns: whether a match was found */ gboolean @@ -608,17 +608,17 @@ gtr_view_search_forward (GtrView * view, /** * gtr_view_search_backward: * @view: a #GtrView - * @start: start of search + * @start: start of search * @end: bound for the search, or %NULL for the end of the buffer * @match_start: return location for start of match, or %NULL * @match_end: return location for end of match, or %NULL - * + * * Searches backward for str. Any match is returned by setting match_start to the * first character of the match and match_end to the first character after the match. * The search will not continue past limit. * Note that a search is a linear or O(n) operation, so you may wish to use limit - * to avoid locking up your UI on large buffers. - * + * to avoid locking up your UI on large buffers. + * * Returns: whether a match was found */ gboolean @@ -699,10 +699,10 @@ gtr_view_search_backward (GtrView * view, * @find: the text to find * @replace: the text to replace @find * @flags: a #GtrSearchFlags - * - * Replaces all matches of @find with @replace and returns the number of + * + * Replaces all matches of @find with @replace and returns the number of * replacements. - * + * * Returns: the number of replacements made it. */ gint @@ -752,47 +752,48 @@ gtr_view_replace_all (GtrView * view, */ //view->priv->stop_cursor_moved_emission = TRUE; - gtk_text_buffer_begin_user_action (buffer); - - do - { - found = gtk_text_iter_forward_search (&iter, + found = gtk_text_iter_forward_search (&iter, search_text, search_flags, &m_start, &m_end, NULL); + if (found) + { + gtk_text_buffer_begin_user_action (buffer); - if (found && GTR_SEARCH_IS_ENTIRE_WORD (flags)) - { - gboolean word; + while (found) + { + if (GTR_SEARCH_IS_ENTIRE_WORD (flags)) + { + gboolean word; - word = gtk_text_iter_starts_word (&m_start) && - gtk_text_iter_ends_word (&m_end); + word = gtk_text_iter_starts_word (&m_start) && + gtk_text_iter_ends_word (&m_end); - if (!word) - { - iter = m_end; - continue; - } - } + if (!word) + { + iter = m_end; + continue; + } + } - if (found) - { - ++cont; + ++cont; + gtk_text_buffer_delete (buffer, &m_start, &m_end); + gtk_text_buffer_insert (buffer, &m_start, replace_text, replace_text_len); + iter = m_start; - gtk_text_buffer_delete (buffer, &m_start, &m_end); - gtk_text_buffer_insert (buffer, - &m_start, replace_text, replace_text_len); + found = gtk_text_iter_forward_search (&iter, + search_text, + search_flags, + &m_start, &m_end, NULL); + } - iter = m_start; - } - } - while (found); + gtk_text_buffer_end_user_action (buffer); + } - gtk_text_buffer_end_user_action (buffer); /* re-enable cursor_moved emission and notify - * the current position + * the current position */ //view->priv->stop_cursor_moved_emission = FALSE; //emit_cursor_moved (GTK_SOURCE_BUFFER(buffer)); @@ -807,7 +808,7 @@ gtr_view_replace_all (GtrView * view, * gtr_view_reload_scheme_color: * @view: a #GtrView * - * Reloads the gtksourceview scheme color. Neccessary when the scheme color + * Reloads the gtksourceview scheme color. Neccessary when the scheme color * changes. */ void -- 1.8.1.4