[geany/geany] c251dc: Merge pull request #394 from SiegeLord/selection_document_mods

Colomban Wendling git-noreply at xxxxx
Fri Aug 21 20:26:41 UTC 2015


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Fri, 21 Aug 2015 20:26:41 UTC
Commit:      c251dcb45f6333ea63fcc2275f7971e6d01ae5e1
             https://github.com/geany/geany/commit/c251dcb45f6333ea63fcc2275f7971e6d01ae5e1

Log Message:
-----------
Merge pull request #394 from SiegeLord/selection_document_mods

Make Document > Strip trailing spaces/Replace tabs/Replace spaces use
the current selection.


Modified Paths:
--------------
    doc/geany.txt
    src/callbacks.c
    src/document.c
    src/editor.c
    src/editor.h

Modified: doc/geany.txt
6 lines changed, 4 insertions(+), 2 deletions(-)
===================================================================
@@ -3774,9 +3774,11 @@ Action                               Default shortcut     Description
 ==================================== ==================== ==================================================
 Clone                                                     See `Cloning documents`_.
 
-Replace tabs with space                                   Replaces all tabs with the right amount of spaces.
+Replace tabs with space                                   Replaces all tabs with the right amount of spaces
+                                                          in the whole document, or the current selection.
 
-Replace spaces with tabs                                  Replaces leading spaces with tab characters.
+Replace spaces with tabs                                  Replaces leading spaces with tab characters in the
+                                                          whole document, or the current selection.
 
 Toggle current fold                                       Toggles the folding state of the current code block.
 


Modified: src/callbacks.c
6 lines changed, 3 insertions(+), 3 deletions(-)
===================================================================
@@ -536,7 +536,7 @@ void on_replace_tabs_activate(GtkMenuItem *menuitem, gpointer user_data)
 
 	g_return_if_fail(doc != NULL);
 
-	editor_replace_tabs(doc->editor);
+	editor_replace_tabs(doc->editor, FALSE);
 }
 
 
@@ -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);
 }
 
 
@@ -1652,7 +1652,7 @@ void on_replace_spaces_activate(GtkMenuItem *menuitem, gpointer user_data)
 
 	g_return_if_fail(doc != NULL);
 
-	editor_replace_spaces(doc->editor);
+	editor_replace_spaces(doc->editor, FALSE);
 }
 
 


Modified: src/document.c
4 lines changed, 2 insertions(+), 2 deletions(-)
===================================================================
@@ -2062,10 +2062,10 @@ gboolean document_save_file(GeanyDocument *doc, gboolean force)
 	fp = project_get_file_prefs();
 	/* replaces tabs with spaces but only if the current file is not a Makefile */
 	if (fp->replace_tabs && doc->file_type->id != GEANY_FILETYPES_MAKE)
-		editor_replace_tabs(doc->editor);
+		editor_replace_tabs(doc->editor, TRUE);
 	/* 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);


Modified: src/editor.c
73 lines changed, 63 insertions(+), 10 deletions(-)
===================================================================
@@ -4314,9 +4314,10 @@ void editor_fold_all(GeanyEditor *editor)
 }
 
 
-void editor_replace_tabs(GeanyEditor *editor)
+void editor_replace_tabs(GeanyEditor *editor, gboolean ignore_selection)
 {
 	gint search_pos, pos_in_line, current_tab_true_length;
+	gint anchor_pos, caret_pos;
 	gint tab_len;
 	gchar *tab_str;
 	struct Sci_TextToFind ttf;
@@ -4325,10 +4326,20 @@ void editor_replace_tabs(GeanyEditor *editor)
 
 	sci_start_undo_action(editor->sci);
 	tab_len = sci_get_tab_width(editor->sci);
-	ttf.chrg.cpMin = 0;
-	ttf.chrg.cpMax = sci_get_length(editor->sci);
+	if (sci_has_selection(editor->sci) && !ignore_selection)
+	{
+		ttf.chrg.cpMin = sci_get_selection_start(editor->sci);
+		ttf.chrg.cpMax = sci_get_selection_end(editor->sci);
+	}
+	else
+	{
+		ttf.chrg.cpMin = 0;
+		ttf.chrg.cpMax = sci_get_length(editor->sci);
+	}
 	ttf.lpstrText = (gchar*) "\t";
 
+	anchor_pos = SSM(editor->sci, SCI_GETANCHOR, 0, 0);
+	caret_pos = sci_get_current_position(editor->sci);
 	while (TRUE)
 	{
 		search_pos = sci_find_text(editor->sci, SCFIND_MATCHCASE, &ttf);
@@ -4346,15 +4357,23 @@ void editor_replace_tabs(GeanyEditor *editor)
 		/* update end of range now text has changed */
 		ttf.chrg.cpMax += current_tab_true_length - 1;
 		g_free(tab_str);
+
+		if (anchor_pos > search_pos)
+			anchor_pos += current_tab_true_length - 1;
+		if (caret_pos > search_pos)
+			caret_pos += current_tab_true_length - 1;
 	}
+	sci_set_selection(editor->sci, anchor_pos, caret_pos);
 	sci_end_undo_action(editor->sci);
 }
 
 
-/* Replaces all occurrences all spaces of the length of a given tab_width. */
-void editor_replace_spaces(GeanyEditor *editor)
+/* Replaces all occurrences all spaces of the length of a given tab_width,
+ * optionally restricting the search to the current selection. */
+void editor_replace_spaces(GeanyEditor *editor, gboolean ignore_selection)
 {
 	gint search_pos;
+	gint anchor_pos, caret_pos;
 	static gdouble tab_len_f = -1.0; /* keep the last used value */
 	gint tab_len;
 	gchar *text;
@@ -4376,10 +4395,20 @@ void editor_replace_spaces(GeanyEditor *editor)
 	text = g_strnfill(tab_len, ' ');
 
 	sci_start_undo_action(editor->sci);
-	ttf.chrg.cpMin = 0;
-	ttf.chrg.cpMax = sci_get_length(editor->sci);
+	if (sci_has_selection(editor->sci) && !ignore_selection)
+	{
+		ttf.chrg.cpMin = sci_get_selection_start(editor->sci);
+		ttf.chrg.cpMax = sci_get_selection_end(editor->sci);
+	}
+	else
+	{
+		ttf.chrg.cpMin = 0;
+		ttf.chrg.cpMax = sci_get_length(editor->sci);
+	}
 	ttf.lpstrText = text;
 
+	anchor_pos = SSM(editor->sci, SCI_GETANCHOR, 0, 0);
+	caret_pos = sci_get_current_position(editor->sci);
 	while (TRUE)
 	{
 		search_pos = sci_find_text(editor->sci, SCFIND_MATCHCASE, &ttf);
@@ -4398,7 +4427,13 @@ void editor_replace_spaces(GeanyEditor *editor)
 		ttf.chrg.cpMin = search_pos;
 		/* update end of range now text has changed */
 		ttf.chrg.cpMax -= tab_len - 1;
+
+		if (anchor_pos > search_pos)
+			anchor_pos -= tab_len - 1;
+		if (caret_pos > search_pos)
+			caret_pos -= tab_len - 1;
 	}
+	sci_set_selection(editor->sci, anchor_pos, caret_pos);
 	sci_end_undo_action(editor->sci);
 	g_free(text);
 }
@@ -4429,14 +4464,32 @@ 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)
+	{
+		gint selection_start = sci_get_selection_start(editor->sci);
+		gint selection_end = sci_get_selection_end(editor->sci);
+
+		start_line = sci_get_line_from_position(editor->sci, selection_start);
+		end_line = sci_get_line_from_position(editor->sci, selection_end);
+
+		if (sci_get_col_from_position(editor->sci, selection_end) > 0)
+			end_line++;
+	}
+	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);
 	}


Modified: src/editor.h
6 lines changed, 3 insertions(+), 3 deletions(-)
===================================================================
@@ -295,13 +295,13 @@ void editor_fold_all(GeanyEditor *editor);
 
 void editor_unfold_all(GeanyEditor *editor);
 
-void editor_replace_tabs(GeanyEditor *editor);
+void editor_replace_tabs(GeanyEditor *editor, gboolean ignore_selection);
 
-void editor_replace_spaces(GeanyEditor *editor);
+void editor_replace_spaces(GeanyEditor *editor, gboolean ignore_selection);
 
 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);
 



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list