From 381d74b1a72d5feef85fef56b25bb15d5403f955 Mon Sep 17 00:00:00 2001 From: Pavel Sountsov Date: Thu, 25 Dec 2014 11:55:02 -0800 Subject: [PATCH] Use current selection when stripping trailing whitespace from the menu. When the lines are stripped due to the file being saved, ignore the selection. --- src/callbacks.c | 2 +- src/document.c | 2 +- src/editor.c | 18 +++++++++++++++--- src/editor.h | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/callbacks.c b/src/callbacks.c index 0848c7f0..9e005e81 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -1608,7 +1608,7 @@ static void on_strip_trailing_spaces1_activate(GtkMenuItem *menuitem, gpointer u doc = document_get_current(); g_return_if_fail(doc != NULL); - editor_strip_trailing_spaces(doc->editor); + editor_strip_trailing_spaces(doc->editor, FALSE); } diff --git a/src/document.c b/src/document.c index ff3f5e85..769a9385 100644 --- a/src/document.c +++ b/src/document.c @@ -2065,7 +2065,7 @@ gboolean document_save_file(GeanyDocument *doc, gboolean force) editor_replace_tabs(doc->editor); /* strip trailing spaces */ if (fp->strip_trailing_spaces) - editor_strip_trailing_spaces(doc->editor); + editor_strip_trailing_spaces(doc->editor, TRUE); /* ensure the file has a newline at the end */ if (fp->final_new_line) editor_ensure_final_newline(doc->editor); diff --git a/src/editor.c b/src/editor.c index 6f11514d..e0acf033 100644 --- a/src/editor.c +++ b/src/editor.c @@ -4429,14 +4429,26 @@ void editor_strip_line_trailing_spaces(GeanyEditor *editor, gint line) } -void editor_strip_trailing_spaces(GeanyEditor *editor) +void editor_strip_trailing_spaces(GeanyEditor *editor, gboolean ignore_selection) { - gint max_lines = sci_get_line_count(editor->sci); + gint start_line; + gint end_line; gint line; + if (sci_has_selection(editor->sci) && !ignore_selection) + { + start_line = sci_get_line_from_position(editor->sci, sci_get_selection_start(editor->sci)); + end_line = sci_get_line_from_position(editor->sci, sci_get_selection_end(editor->sci)) + 1; + } + else + { + start_line = 0; + end_line = sci_get_line_count(editor->sci); + } + sci_start_undo_action(editor->sci); - for (line = 0; line < max_lines; line++) + for (line = start_line; line < end_line; line++) { editor_strip_line_trailing_spaces(editor, line); } diff --git a/src/editor.h b/src/editor.h index 21926295..3999707c 100644 --- a/src/editor.h +++ b/src/editor.h @@ -301,7 +301,7 @@ void editor_replace_spaces(GeanyEditor *editor); void editor_strip_line_trailing_spaces(GeanyEditor *editor, gint line); -void editor_strip_trailing_spaces(GeanyEditor *editor); +void editor_strip_trailing_spaces(GeanyEditor *editor, gboolean ignore_selection); void editor_ensure_final_newline(GeanyEditor *editor);