Hi all.

Everybody knows that Geany has menu item "Document > Strip trailing spaces" and option to do it automatically when a document is saved. My proposal is another command, something like "Strip trailing blank lines", to remove blank lines in the end of file (as they are absolutely useless there).

I wrote new `editor_strip_trailing_newlines' function (see the attached patch). Currently I call it directly from `document_save_file' if `strip_trailing_spaces' setting is set. Of course, it's not how it should be. Maybe separate menu item and option should be added, like for trailing spaces. Alternative way is to combine those, i.e., rename "Strip trailing spaces" to "Remove excessive whitespace" :-) These all are usability issues, with which I don't like to deal. If any of the developers has an opinion upon those, I'll consider it and update the patch.

P.S. My code leaves one trailing newline if `final_new_line' setting is set. Otherwise, it removes all trailing newlines. I believe it is right.
diff --git a/src/document.c b/src/document.c
index 48a37a7..d7bdef9 100644
--- a/src/document.c
+++ b/src/document.c
@@ -1722,7 +1722,10 @@ gboolean document_save_file(GeanyDocument *doc, gboolean force)
 		editor_replace_tabs(doc->editor);
 	/* strip trailing spaces */
 	if (file_prefs.strip_trailing_spaces)
+	{
 		editor_strip_trailing_spaces(doc->editor);
+		editor_strip_trailing_newlines(doc->editor);
+	}
 	/* ensure the file has a newline at the end */
 	if (file_prefs.final_new_line)
 		editor_ensure_final_newline(doc->editor);
@@ -2957,5 +2960,3 @@ gboolean document_check_disk_status(GeanyDocument *doc, gboolean force)
 
 	return ret;
 }
-
-
diff --git a/src/editor.c b/src/editor.c
index b109fff..d5b9656 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -4352,6 +4352,42 @@ void editor_strip_trailing_spaces(GeanyEditor *editor)
 }
 
 
+void editor_strip_trailing_newlines(GeanyEditor *editor)
+{
+	gint max_lines = sci_get_line_count(editor->sci);
+	gint line, pos;
+
+	/*
+	 * Store index of last non-empty line in `line'
+	 * and its end position in `pos'. If all lines
+	 * are empty, `line' will contain -1.
+	 */
+	line = max_lines-1;
+	pos = sci_get_line_end_position(editor->sci, line);
+	while (line >= 0 && sci_get_position_from_line(editor->sci, line) == pos)
+	{
+		--line;
+		--pos;
+	}
+
+	if (line == -1 || file_prefs.final_new_line)
+	{
+		/* leave one newline */
+		++line;
+		++pos;
+	}
+
+	if ((max_lines-1) - line > 0)
+	{
+		/* there are some lines to be removed */
+		sci_set_target_start(editor->sci, pos);
+		sci_set_target_end(editor->sci,
+			sci_get_line_end_position(editor->sci, max_lines-1));
+		sci_replace_target(editor->sci, "", FALSE);
+	}
+}
+
+
 void editor_ensure_final_newline(GeanyEditor *editor)
 {
 	gint max_lines = sci_get_line_count(editor->sci);
@@ -4903,5 +4939,3 @@ void editor_indent(GeanyEditor *editor, gboolean increase)
 		sci_set_current_line(sci, lstart);
 	}
 }
-
-
diff --git a/src/editor.h b/src/editor.h
index 2d21738..77ed526 100644
--- a/src/editor.h
+++ b/src/editor.h
@@ -255,6 +255,8 @@ void editor_strip_line_trailing_spaces(GeanyEditor *editor, gint line);
 
 void editor_strip_trailing_spaces(GeanyEditor *editor);
 
+void editor_strip_trailing_newlines(GeanyEditor *editor);
+
 void editor_ensure_final_newline(GeanyEditor *editor);
 
 void editor_insert_color(GeanyEditor *editor, const gchar *colour);
_______________________________________________
Geany-devel mailing list
Geany-devel@uvena.de
http://lists.uvena.de/cgi-bin/mailman/listinfo/geany-devel

Reply via email to