[geany/geany-plugins] 6159a7: lineoperations: fixed last newline, empty doc problems

Sylvan Mostert git-noreply at xxxxx
Sat Jan 16 00:46:11 UTC 2016


Branch:      refs/heads/master
Author:      Sylvan Mostert <smostert.dev at gmail.com>
Committer:   Sylvan Mostert <smostert.dev at gmail.com>
Date:        Sat, 16 Jan 2016 00:46:11 UTC
Commit:      6159a7362a5eca9f3511b8435a01d5d9cb145f2e
             https://github.com/geany/geany-plugins/commit/6159a7362a5eca9f3511b8435a01d5d9cb145f2e

Log Message:
-----------
lineoperations: fixed last newline, empty doc problems

Added `ensure_final_newline()` to add a newline if it does not exist
before operation. If the final line does not have an newline, and it
shifts position, the resulting line will be concatenation of two lines.

Added null terminating character to new_file string in case of empty
file.


Modified Paths:
--------------
    lineoperations/src/linefunctions.c

Modified: lineoperations/src/linefunctions.c
35 lines changed, 35 insertions(+), 0 deletions(-)
===================================================================
@@ -22,6 +22,20 @@
 #include "linefunctions.h"
 
 
+/* altered from geany/src/editor.c, ensure new line at file end */
+static void ensure_final_newline(GeanyEditor *editor, gint max_lines)
+{
+	gint end_document       = sci_get_position_from_line(editor->sci, max_lines);
+	gboolean append_newline = end_document >
+					sci_get_position_from_line(editor->sci, max_lines - 1);
+
+	if (append_newline)
+	{
+		const gchar *eol = editor_get_eol_char(editor);
+		sci_insert_text(editor->sci, end_document, eol);
+	}
+}
+
 /* comparison function to be used in qsort */
 static gint compare_asc(const void * a, const void * b)
 {
@@ -50,9 +64,15 @@ void rmdupst(GeanyDocument *doc) {
 	total_num_lines = sci_get_line_count(doc->editor->sci);
 	lines           = g_malloc(sizeof(gchar *) * total_num_lines);
 	new_file        = g_malloc(sizeof(gchar)   * (total_num_chars+1));
+	new_file[0]     = '\0';
 	nf_end          = new_file;
 	lineptr         = (gchar *)"";
 
+
+	/* if file is not empty, ensure that the file ends with newline */
+	if(total_num_lines != 1)
+		ensure_final_newline(doc->editor, total_num_lines);
+
 	/* copy *all* lines into **lines array */
 	for(i = 0; i < total_num_lines; i++)
 		lines[i] = sci_get_line(doc->editor->sci, i);
@@ -94,9 +114,13 @@ void rmdupln(GeanyDocument *doc) {
 	total_num_lines = sci_get_line_count(doc->editor->sci);
 	lines           = g_malloc(sizeof(gchar *) * total_num_lines);
 	new_file        = g_malloc(sizeof(gchar) * (total_num_chars+1));
+	new_file[0]     = '\0';
 	nf_end          = new_file;
 
 
+	/* if file is not empty, ensure that the file ends with newline */
+	if(total_num_lines != 1)
+		ensure_final_newline(doc->editor, total_num_lines);
 
 	/* copy *all* lines into **lines array */
 	for(i = 0; i < total_num_lines; i++)
@@ -149,8 +173,14 @@ void rmunqln(GeanyDocument *doc) {
 	total_num_lines = sci_get_line_count(doc->editor->sci);
 	lines           = g_malloc(sizeof(gchar *) * total_num_lines);
 	new_file        = g_malloc(sizeof(gchar) * (total_num_chars+1));
+	new_file[0]     = '\0';
 	nf_end          = new_file;
 
+
+	/* if file is not empty, ensure that the file ends with newline */
+	if(total_num_lines != 1)
+		ensure_final_newline(doc->editor, total_num_lines);
+
 	/* copy *all* lines into **lines array */
 	for(i = 0; i < total_num_lines; i++)
 		lines[i] = sci_get_line(doc->editor->sci, i);
@@ -261,6 +291,11 @@ void sortlines(GeanyDocument *doc, gboolean asc) {
 	total_num_lines = sci_get_line_count(doc->editor->sci);
 	lines           = g_malloc(sizeof(gchar *) * total_num_lines+1);
 	new_file        = g_malloc(sizeof(gchar) * (total_num_chars+1));
+	new_file[0]     = '\0';
+
+	/* if file is not empty, ensure that the file ends with newline */
+	if(total_num_lines != 1)
+		ensure_final_newline(doc->editor, total_num_lines);
 
 	/* copy *all* lines into **lines array */
 	for(i = 0; i < total_num_lines; i++)



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


More information about the Plugins-Commits mailing list