diff --git a/ChangeLog b/ChangeLog index 77d5cc8f5..e5f26d98c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,10 @@ * tagmanager/options.c: Use extern for c_tags_ignore declaration to avoid allocating a second copy of it (patch by Daniel Richard G., thanks). + * src/callbacks.c, src/search.c, src/document.c, src/editor.c, + src/editor.h: + Don't scroll the editor view if it is unnecessary when using Find + Next/Previous, Find Selected and when searching from the search bar. 2008-02-22 Enrico Tröger diff --git a/src/callbacks.c b/src/callbacks.c index 84c4d53d9..6681a6a39 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -1125,39 +1125,42 @@ on_find1_activate (GtkMenuItem *menuitem, } -void -on_find_next1_activate (GtkMenuItem *menuitem, - gpointer user_data) +static void find_again(gboolean change_direction) { gint idx = document_get_cur_idx(); + g_return_if_fail(DOC_IDX_VALID(idx)); + if (search_data.text) { + gboolean forward = ! search_data.backwards; gint result = document_find_text(idx, search_data.text, search_data.flags, - search_data.backwards, TRUE, NULL); + change_direction ? forward : !forward, FALSE, NULL); + + if (result > -1) + editor_display_current_line(idx, 0.3F); set_search_bar_background((result > -1) ? TRUE : FALSE); } } +void +on_find_next1_activate (GtkMenuItem *menuitem, + gpointer user_data) +{ + find_again(FALSE); +} + + void on_find_previous1_activate (GtkMenuItem *menuitem, gpointer user_data) { - gint idx = document_get_cur_idx(); - - if (search_data.text == NULL) return; - if (search_data.flags & SCFIND_REGEXP) utils_beep(); // Can't reverse search order for a regex (find next ignores search backwards) else - { - gint result = document_find_text(idx, search_data.text, search_data.flags, - !search_data.backwards, TRUE, NULL); - - set_search_bar_background((result > -1) ? TRUE : FALSE); - } + find_again(TRUE); } diff --git a/src/document.c b/src/document.c index af25da34a..48efd5ef5 100644 --- a/src/document.c +++ b/src/document.c @@ -1475,16 +1475,20 @@ gboolean document_search_bar_find(gint idx, const gchar *text, gint flags, gbool if (search_pos != -1) { + gint line = sci_get_line_from_position(doc_list[idx].sci, ttf.chrgText.cpMin); + // unfold maybe folded results - sci_ensure_line_is_visible(doc_list[idx].sci, - sci_get_line_from_position(doc_list[idx].sci, ttf.chrgText.cpMin)); + sci_ensure_line_is_visible(doc_list[idx].sci, line); sci_set_selection_start(doc_list[idx].sci, ttf.chrgText.cpMin); sci_set_selection_end(doc_list[idx].sci, ttf.chrgText.cpMax); - // we need to force scrolling in case the cursor is outside of the current visible area - // doc_list[].scroll_percent doesn't work because sci isn't always updated while searching - editor_scroll_to_line(doc_list[idx].sci, -1, 0.3F); + if (! editor_line_in_view(doc_list[idx].sci, line)) + { + // we need to force scrolling in case the cursor is outside of the current visible area + // doc_list[].scroll_percent doesn't work because sci isn't always updated while searching + editor_scroll_to_line(doc_list[idx].sci, -1, 0.3F); + } return TRUE; } else diff --git a/src/editor.c b/src/editor.c index e61f961da..b8998d5a7 100644 --- a/src/editor.c +++ b/src/editor.c @@ -2643,3 +2643,36 @@ gchar *editor_get_default_selection(gint idx, const gchar *wordchars) } return s; } + + +/* Note: Usually the line should be made visible (not folded) before calling this. + * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is + * outside the view. */ +gboolean editor_line_in_view(ScintillaObject *sci, gint line) +{ + gint vis1, los; + + line = SSM(sci, SCI_VISIBLEFROMDOCLINE, line, 0); // convert to visible line number + vis1 = SSM(sci, SCI_GETFIRSTVISIBLELINE, 0, 0); + los = SSM(sci, SCI_LINESONSCREEN, 0, 0); + + return (line >= vis1 && line < vis1 + los); +} + + +/* If the current line is outside the current view window, scroll the line + * so it appears at percent_of_view. */ +void editor_display_current_line(gint idx, gfloat percent_of_view) +{ + ScintillaObject *sci = doc_list[idx].sci; + gint line = sci_get_current_line(sci); + + // unfold maybe folded results + sci_ensure_line_is_visible(doc_list[idx].sci, line); + + // scroll the line if it's off screen + if (! editor_line_in_view(sci, line)) + doc_list[idx].scroll_percent = percent_of_view; +} + + diff --git a/src/editor.h b/src/editor.h index 8bc528782..58fd8bbd0 100644 --- a/src/editor.h +++ b/src/editor.h @@ -131,8 +131,12 @@ void editor_auto_line_indentation(gint idx, gint pos); void editor_indentation_by_one_space(gint idx, gint pos, gboolean decrease); +gboolean editor_line_in_view(ScintillaObject *sci, gint line); + void editor_scroll_to_line(ScintillaObject *sci, gint line, gfloat percent_of_view); +void editor_display_current_line(gint idx, gfloat percent_of_view); + void editor_finalize(void); diff --git a/src/search.c b/src/search.c index 7415031af..8222a2133 100644 --- a/src/search.c +++ b/src/search.c @@ -251,7 +251,9 @@ void search_find_selection(gint idx, gboolean search_backwards) if (s) { setup_find_next(s); // allow find next/prev - document_find_text(idx, s, 0, search_backwards, TRUE, NULL); + + if (document_find_text(idx, s, 0, search_backwards, FALSE, NULL) > -1) + editor_display_current_line(idx, 0.3F); g_free(s); } }