[geany/geany-plugins] 867ffb: lineoperations: added some ui changes

Sylvan Mostert git-noreply at xxxxx
Tue Jan 3 21:46:08 UTC 2017


Branch:      refs/heads/master
Author:      Sylvan Mostert <smostert.dev at gmail.com>
Committer:   Sylvan Mostert <smostert.dev at gmail.com>
Date:        Fri, 09 Sep 2016 05:47:54 UTC
Commit:      867ffbd11e63975a89ace2fdfa8c93a03b79de2b
             https://github.com/geany/geany-plugins/commit/867ffbd11e63975a89ace2fdfa8c93a03b79de2b

Log Message:
-----------
lineoperations: added some ui changes

Added a message to statusbar to display number of lines changed.

Changed some variable names for clarity, added some spacing.


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

Modified: lineoperations/src/linefunctions.c
36 lines changed, 19 insertions(+), 17 deletions(-)
===================================================================
@@ -41,7 +41,7 @@ compare_desc(const void * a, const void * b)
 
 /* Remove Duplicate Lines, sorted */
 void
-rmdupst(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file)
+rmdupst(gchar **lines, gint num_lines, gchar *new_file)
 {
 	gchar *nf_end  = new_file;    /* points to last char of new_file */
 	gchar *lineptr = (gchar *)""; /* temporary line pointer */
@@ -64,7 +64,7 @@ rmdupst(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file)
 
 /* Remove Duplicate Lines, ordered */
 void
-rmdupln(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file)
+rmdupln(gchar **lines, gint num_lines, gchar *new_file)
 {
 	gchar *nf_end  = new_file;  /* points to last char of new_file */
 	gint  i        = 0;         /* iterator */
@@ -105,7 +105,7 @@ rmdupln(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file)
 
 /* Remove Unique Lines */
 void
-rmunqln(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file)
+rmunqln(gchar **lines, gint num_lines, gchar *new_file)
 {
 	gchar *nf_end = new_file;   /* points to last char of new_file */
 	gint i        = 0;          /* iterator */
@@ -142,7 +142,7 @@ rmunqln(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file)
 
 /* Remove Empty Lines */
 void
-rmemtyln(GeanyDocument *doc, gint line_num, gint end_line_num)
+rmemtyln(ScintillaObject *sci, gint line_num, gint end_line_num)
 {
 
 	while(line_num <= end_line_num)    /* loop through lines */
@@ -151,10 +151,11 @@ rmemtyln(GeanyDocument *doc, gint line_num, gint end_line_num)
 		if(sci_get_position_from_line(sci, line_num) ==
 		   sci_get_line_end_position (sci, line_num))
 		{
-			scintilla_send_message(doc->editor->sci,
+			scintilla_send_message(sci,
 					   SCI_DELETERANGE,
-					   sci_get_position_from_line(doc->editor->sci, line_num),
-					   sci_get_line_length(doc->editor->sci, line_num));
+					   sci_get_position_from_line(sci, line_num),
+					   sci_get_line_length(sci, line_num));
+
 			line_num--;
 			end_line_num--;
 		}
@@ -165,26 +166,27 @@ rmemtyln(GeanyDocument *doc, gint line_num, gint end_line_num)
 
 /* Remove Whitespace Lines */
 void
-rmwhspln(GeanyDocument *doc, gint line_num, gint end_line_num)
+rmwhspln(ScintillaObject *sci, gint line_num, gint end_line_num)
 {
 	gint indent;                       /* indent position */
 
 	while(line_num <= end_line_num)    /* loop through lines */
 	{
-		indent = scintilla_send_message(doc->editor->sci,
+		indent = scintilla_send_message(sci,
 									SCI_GETLINEINDENTPOSITION,
 									line_num, 0);
 
 		/* check if the posn of indentation is also the end of line posn */
 		if(indent -
-		   sci_get_position_from_line(doc->editor->sci, line_num) ==
-		   sci_get_line_end_position (doc->editor->sci, line_num)-
-		   sci_get_position_from_line(doc->editor->sci, line_num))
+		   sci_get_position_from_line(sci, line_num) ==
+		   sci_get_line_end_position (sci, line_num) -
+		   sci_get_position_from_line(sci, line_num))
 		{
-			scintilla_send_message(doc->editor->sci,
+			scintilla_send_message(sci,
 					   SCI_DELETERANGE,
-					   sci_get_position_from_line(doc->editor->sci, line_num),
-					   sci_get_line_length(doc->editor->sci, line_num));
+					   sci_get_position_from_line(sci, line_num),
+					   sci_get_line_length(sci, line_num));
+
 			line_num--;
 			end_line_num--;
 		}
@@ -196,7 +198,7 @@ rmwhspln(GeanyDocument *doc, gint line_num, gint end_line_num)
 
 /* Sort Lines Ascending */
 void
-sortlnsasc(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file)
+sortlnsasc(gchar **lines, gint num_lines, gchar *new_file)
 {
 	gchar *nf_end = new_file;          /* points to last char of new_file */
 	gint i;
@@ -211,7 +213,7 @@ sortlnsasc(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file)
 
 /* Sort Lines Descending */
 void
-sortlndesc(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file)
+sortlndesc(gchar **lines, gint num_lines, gchar *new_file)
 {
 	gchar *nf_end = new_file;          /* points to last char of new_file */
 	gint i;


Modified: lineoperations/src/linefunctions.h
14 lines changed, 7 insertions(+), 7 deletions(-)
===================================================================
@@ -31,37 +31,37 @@
 
 
 /* Remove Duplicate Lines, sorted */
-void
+gint
 rmdupst(gchar **lines, gint num_lines, gchar *new_file);
 
 
 /* Remove Duplicate Lines, ordered */
-void
+gint
 rmdupln(gchar **lines, gint num_lines, gchar *new_file);
 
 
 /* Remove Unique Lines */
-void
+gint
 rmunqln(gchar **lines, gint num_lines, gchar *new_file);
 
 
 /* Remove Empty Lines */
-void
+gint
 rmemtyln(ScintillaObject *sci, gint line_num, gint end_line_num);
 
 
 /* Remove Whitespace Lines */
-void
+gint
 rmwhspln(ScintillaObject *sci, gint line_num, gint end_line_num);
 
 
 /* Sort Lines Ascending */
-void
+gint
 sortlnsasc(gchar **lines, gint num_lines, gchar *new_file);
 
 
 /* Sort Lines Descending */
-void
+gint
 sortlndesc(gchar **lines, gint num_lines, gchar *new_file);
 
 #endif


Modified: lineoperations/src/lineoperations.c
117 lines changed, 81 insertions(+), 36 deletions(-)
===================================================================
@@ -31,25 +31,25 @@ static GtkWidget *main_menu_item = NULL;
 
 
 
-/* represents lines that will have Operation applied to */
+/* represents a selection of lines that will have Operation applied to */
 struct lo_lines {
 	gboolean is_selection;
-	gint start_posn_line;
-	gint end_posn_line;
+	gint     start_line;
+	gint     end_line;
 };
 
 
-/* selects lines in document (based of lo_lines struct parameter) */
+/* selects lines in document (based off of lo_lines struct parameter) */
 static void
 select_lines(GeanyEditor *editor, struct lo_lines sel)
 {
 	/* set the selection to beginning of first line */
 	sci_set_selection_start(editor->sci,
-			sci_get_position_from_line(editor->sci, sel.start_posn_line));
+			sci_get_position_from_line(editor->sci, sel.start_line));
 
 	/* set the selection to end of last line */
 	sci_set_selection_end(editor->sci,
-			sci_get_line_end_position(editor->sci, sel.end_posn_line) +
+			sci_get_line_end_position(editor->sci, sel.end_line) +
 									 editor_get_eol_char_len(editor));
 }
 
@@ -70,11 +70,11 @@ get_current_sel_lines(ScintillaObject *sci)
 		end_posn   = sci_get_selection_end  (sci);
 
 		/* get the line number of those positions */
-		sel.start_posn_line = scintilla_send_message(sci,
+		sel.start_line = scintilla_send_message(sci,
 									SCI_LINEFROMPOSITION,
 									start_posn, 0);
 
-		sel.end_posn_line   = scintilla_send_message(sci,
+		sel.end_line   = scintilla_send_message(sci,
 									SCI_LINEFROMPOSITION,
 									end_posn, 0);
 
@@ -83,9 +83,9 @@ get_current_sel_lines(ScintillaObject *sci)
 	else
 	{
 		/* if there is no selection, start at first line */
-		sel.start_posn_line = 0;
+		sel.start_line = 0;
 		/* and end at last one */
-		sel.end_posn_line   = (sci_get_line_count(sci) - 1);
+		sel.end_line   = (sci_get_line_count(sci) - 1);
 
 		sel.is_selection = FALSE;
 	}
@@ -96,44 +96,89 @@ get_current_sel_lines(ScintillaObject *sci)
 
 /* altered from geany/src/editor.c, ensure new line at file end */
 static void
-ensure_final_newline(GeanyEditor *editor, gint max_lines)
+ensure_final_newline(GeanyEditor *editor, gint *num_lines, struct lo_lines *sel)
 {
-	gint end_document      = sci_get_position_from_line(editor->sci, max_lines);
+	gint end_document   = sci_get_position_from_line(editor->sci, (*num_lines));
 	gboolean append_newline = end_document >
-					sci_get_position_from_line(editor->sci, max_lines - 1);
+					sci_get_position_from_line(editor->sci, ((*num_lines) - 1));
 
 	if (append_newline)
 	{
 		const gchar *eol = editor_get_eol_char(editor);
 		sci_insert_text(editor->sci, end_document, eol);
+		(*num_lines)++;
+		(*sel).end_line++;
+	}
+}
+
+
+/* set statusbar with message and select altered lines */
+static void
+user_indicate(GeanyEditor *editor, gint lines_affected, struct lo_lines sel)
+{
+	if(lines_affected < 0)
+	{
+		ui_set_statusbar(FALSE, _("Operation successful! %d lines removed."),
+					-lines_affected);
+
+		/* select lines to indicate to user what lines were altered */
+		sel.end_line   += lines_affected;
+		
+		if(sel.is_selection)
+			select_lines(editor, sel);	
+	}
+	else if(lines_affected == 0)
+	{
+		ui_set_statusbar(FALSE, _("Operation successful! No lines removed."));
+
+		/* select lines to indicate to user what lines were altered */
+		if(sel.is_selection)
+			select_lines(editor, sel);
+	}
+	else
+	{
+		ui_set_statusbar(FALSE, _("Operation successful! %d lines affected."),
+					lines_affected);
+
+		/* select lines to indicate to user what lines were altered */
+		if(sel.is_selection)
+			select_lines(editor, sel);	
 	}
 }
 
 
 /*
  * Menu action for functions with indirect scintilla manipulation
- * e.g. functions requiring **lines array, *new_file...
+ * e.g. functions requiring **lines array, num_lines, *new_file
 */
 static void
 action_indir_manip_item(GtkMenuItem *menuitem, gpointer gdata)
 {
-	/* function pointer to gdata -- function to be used */
-	void (*func)(gchar **lines, gint num_lines, gchar *new_file) = gdata;
+	/* function pointer to function to be used */
+	gint (*func)(gchar **lines, gint num_lines, gchar *new_file) = gdata;
+	GeanyDocument *doc    = document_get_current();
 
-	GeanyDocument *doc  = document_get_current();
-	struct lo_lines sel = get_current_sel_lines(doc->editor->sci);
-	gint  num_chars     = 0;
-	gint  num_lines     = (sel.end_posn_line - sel.start_posn_line) + 1;
-	gchar **lines       = g_malloc(sizeof(gchar *) * num_lines);
-	gint i              = 0;
+	struct lo_lines sel   = get_current_sel_lines(doc->editor->sci);
+	gint   num_lines      = (sel.end_line - sel.start_line) + 1;
+
+	gint   num_chars      = 0;
+	gint   i              = 0;
+	gint   lines_affected = 0;
+
+
+	/* if last line within selection ensure that the file ends with newline */
+	if((sel.end_line + 1) == sci_get_line_count(doc->editor->sci))
+		ensure_final_newline(doc->editor, &num_lines, &sel);
 
 	/* get num_chars and **lines */
+	gchar **lines        = g_malloc(sizeof(gchar *) * num_lines);
 	for(i = 0; i < num_lines; i++)
 	{
 		num_chars += (sci_get_line_length(doc->editor->sci,
-										(i + sel.start_posn_line)));
+										(i + sel.start_line)));
+
 		lines[i]   = sci_get_line(doc->editor->sci,
-										(i + sel.start_posn_line));
+										(i + sel.start_line));
 	}
 
 	gchar *new_file  = g_malloc(sizeof(gchar) * (num_chars + 1));
@@ -144,21 +189,19 @@ action_indir_manip_item(GtkMenuItem *menuitem, gpointer gdata)
 
 	sci_start_undo_action(doc->editor->sci);
 
-	/* if file is not empty, ensure that the file ends with newline */
-	if((!sel.is_selection && num_lines != 1) || (sel.is_selection &&
-		sel.end_posn_line == (sci_get_line_count(doc->editor->sci) - 1)))
-	{
-		ensure_final_newline(doc->editor, num_lines);
-	}
 
-	if(doc) func(lines, num_lines, new_file);
+	if(doc)
+		lines_affected = func(lines, num_lines, new_file);
+
 
 	/* set new document */
 	if(sci_has_selection(doc->editor->sci))
 		sci_replace_sel(doc->editor->sci, new_file);
 	else
 		sci_set_text(doc->editor->sci, new_file);
 
+	user_indicate(doc->editor, lines_affected, sel);
+
 	sci_end_undo_action(doc->editor->sci);
 
 	/* free used memory */
@@ -178,17 +221,19 @@ static void
 action_sci_manip_item(GtkMenuItem *menuitem, gpointer gdata)
 {
 	/* function pointer to gdata -- function to be used */
-	void (*func)(ScintillaObject *, gint, gint) = gdata;
+	gint (*func)(ScintillaObject *, gint, gint) = gdata;
 	GeanyDocument *doc  = document_get_current();
 	struct lo_lines sel = get_current_sel_lines(doc->editor->sci);
+	gint lines_affected = 0;
 
-	/* select lines to indicate to user what lines were altered */
-	if(sel.is_selection)
-		select_lines(doc->editor, sel);
 
 	sci_start_undo_action(doc->editor->sci);
 
-	if(doc) func(doc->editor->sci, sel.start_posn_line, sel.end_posn_line);
+	if(doc)
+		lines_affected = func(doc->editor->sci, sel.start_line, sel.end_line);
+
+	/* put message in ui_statusbar, and highlight lines that were affected */
+	user_indicate(doc->editor, lines_affected, sel);
 
 	sci_end_undo_action(doc->editor->sci);
 }



--------------
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