From fefeccc4e22b64e4e2ddcfb2bf9e696c988d2699 Mon Sep 17 00:00:00 2001 From: Sylvan Mostert Date: Wed, 11 Jan 2017 23:06:45 -0500 Subject: [PATCH 1/3] lineoperations: fixed -Werror=aggregate-return Functions no longer work with structure, instead pointer to structure. --- lineoperations/src/lineoperations.c | 72 +++++++++++++++++------------ 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/lineoperations/src/lineoperations.c b/lineoperations/src/lineoperations.c index 370a4640e..a72e72a3b 100644 --- a/lineoperations/src/lineoperations.c +++ b/lineoperations/src/lineoperations.c @@ -41,24 +41,23 @@ struct lo_lines { /* selects lines in document (based off of lo_lines struct parameter) */ static void -select_lines(GeanyEditor *editor, struct lo_lines sel) +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_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_line) + + sci_get_line_end_position(editor->sci, sel->end_line) + editor_get_eol_char_len(editor)); } -/* get lo_lines struct based off of document */ -static struct lo_lines -get_current_sel_lines(ScintillaObject *sci) +/* get lo_lines struct 'sel' from document */ +static void +get_current_sel_lines(ScintillaObject *sci, struct lo_lines *sel) { - struct lo_lines sel = { FALSE, 0, 0 }; gint start_posn = 0; /* position of selection start */ gint end_posn = 0; /* position of selection end */ @@ -70,27 +69,25 @@ get_current_sel_lines(ScintillaObject *sci) end_posn = sci_get_selection_end (sci); /* get the *line number* of those positions */ - sel.start_line = scintilla_send_message(sci, + sel->start_line = scintilla_send_message(sci, SCI_LINEFROMPOSITION, start_posn, 0); - sel.end_line = scintilla_send_message(sci, + sel->end_line = scintilla_send_message(sci, SCI_LINEFROMPOSITION, end_posn, 0); - sel.is_selection = TRUE; + sel->is_selection = TRUE; } else { /* if there is no selection, start at first line */ - sel.start_line = 0; + sel->start_line = 0; /* and end at last one */ - sel.end_line = (sci_get_line_count(sci) - 1); + sel->end_line = (sci_get_line_count(sci) - 1); - sel.is_selection = FALSE; + sel->is_selection = FALSE; } - - return sel; } @@ -109,14 +106,14 @@ ensure_final_newline(GeanyEditor *editor, gint *num_lines, struct lo_lines *sel) /* re-adjust the selection */ (*num_lines)++; - (*sel).end_line++; + sel->end_line++; } } /* set statusbar with message and select altered lines */ static void -user_indicate(GeanyEditor *editor, gint lines_affected, struct lo_lines sel) +user_indicate(GeanyEditor *editor, gint lines_affected, struct lo_lines *sel) { if(lines_affected < 0) { @@ -124,9 +121,9 @@ user_indicate(GeanyEditor *editor, gint lines_affected, struct lo_lines sel) -lines_affected); /* select lines to indicate to user what lines were altered */ - sel.end_line += lines_affected; + sel->end_line += lines_affected; - if(sel.is_selection) + if(sel->is_selection) select_lines(editor, sel); } else if(lines_affected == 0) @@ -134,7 +131,7 @@ user_indicate(GeanyEditor *editor, gint lines_affected, struct lo_lines sel) ui_set_statusbar(FALSE, _("Operation successful! No lines removed.")); /* select lines to indicate to user what lines were altered */ - if(sel.is_selection) + if(sel->is_selection) select_lines(editor, sel); } else @@ -143,7 +140,7 @@ user_indicate(GeanyEditor *editor, gint lines_affected, struct lo_lines sel) lines_affected); /* select lines to indicate to user what lines were altered */ - if(sel.is_selection) + if(sel->is_selection) select_lines(editor, sel); } } @@ -162,27 +159,32 @@ action_indir_manip_item(GtkMenuItem *menuitem, gpointer gdata) g_return_if_fail(doc != NULL); - 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; + struct lo_lines *sel = g_malloc(sizeof(struct lo_lines)); + sel->is_selection = FALSE; + sel->start_line = 0; + sel->end_line = 0; + + get_current_sel_lines(doc->editor->sci, sel); + gint num_lines = (sel->end_line - sel->start_line) + 1; + /* 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); + 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_line))); + (i + sel->start_line))); lines[i] = sci_get_line(doc->editor->sci, - (i + sel.start_line)); + (i + sel->start_line)); } gchar *new_file = g_malloc(sizeof(gchar) * (num_chars + 1)); @@ -201,6 +203,8 @@ action_indir_manip_item(GtkMenuItem *menuitem, gpointer gdata) /* select affected lines and set statusbar message */ user_indicate(doc->editor, lines_affected, sel); + g_free(sel); + sci_end_undo_action(doc->editor->sci); /* free used memory */ @@ -225,16 +229,24 @@ action_sci_manip_item(GtkMenuItem *menuitem, gpointer gdata) g_return_if_fail(doc != NULL); - struct lo_lines sel = get_current_sel_lines(doc->editor->sci); + struct lo_lines *sel = g_malloc(sizeof(struct lo_lines)); + sel->is_selection = FALSE; + sel->start_line = 0; + sel->end_line = 0; + + get_current_sel_lines(doc->editor->sci, sel); gint lines_affected = 0; sci_start_undo_action(doc->editor->sci); - lines_affected = func(doc->editor->sci, sel.start_line, sel.end_line); + 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); + g_free(sel); + sci_end_undo_action(doc->editor->sci); } From 42e701b2c84b822d435d4d3d26c6a6c092f8ff17 Mon Sep 17 00:00:00 2001 From: Sylvan Mostert Date: Wed, 11 Jan 2017 23:46:36 -0500 Subject: [PATCH 2/3] lineoperations: removed check for doc Items are added with ui_add_document_sensitive so doc is never null. --- lineoperations/src/lineoperations.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lineoperations/src/lineoperations.c b/lineoperations/src/lineoperations.c index a72e72a3b..26f639434 100644 --- a/lineoperations/src/lineoperations.c +++ b/lineoperations/src/lineoperations.c @@ -197,6 +197,7 @@ action_indir_manip_item(GtkMenuItem *menuitem, gpointer gdata) lines_affected = func(lines, num_lines, new_file); + /* set new document */ sci_replace_sel(doc->editor->sci, new_file); From c4de25225a192aed99b247dd39fa43cdac68147b Mon Sep 17 00:00:00 2001 From: Sylvan Mostert Date: Tue, 7 Mar 2017 22:34:06 -0500 Subject: [PATCH 3/3] lineoperations: unnecessary heap allocations --- lineoperations/src/lineoperations.c | 40 ++++++++++------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/lineoperations/src/lineoperations.c b/lineoperations/src/lineoperations.c index 26f639434..7dc6aa03f 100644 --- a/lineoperations/src/lineoperations.c +++ b/lineoperations/src/lineoperations.c @@ -156,42 +156,37 @@ action_indir_manip_item(GtkMenuItem *menuitem, gpointer gdata) /* function pointer to function to be used */ gint (*func)(gchar **lines, gint num_lines, gchar *new_file) = gdata; GeanyDocument *doc = document_get_current(); - g_return_if_fail(doc != NULL); + struct lo_lines sel; gint num_chars = 0; gint i = 0; gint lines_affected = 0; - struct lo_lines *sel = g_malloc(sizeof(struct lo_lines)); - sel->is_selection = FALSE; - sel->start_line = 0; - sel->end_line = 0; - - get_current_sel_lines(doc->editor->sci, sel); - gint num_lines = (sel->end_line - sel->start_line) + 1; + get_current_sel_lines(doc->editor->sci, &sel); + gint num_lines = (sel.end_line - sel.start_line) + 1; /* 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); + 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_line))); + (i + sel.start_line))); lines[i] = sci_get_line(doc->editor->sci, - (i + sel->start_line)); + (i + sel.start_line)); } gchar *new_file = g_malloc(sizeof(gchar) * (num_chars + 1)); new_file[0] = '\0'; /* select lines that will be replaced with array */ - select_lines(doc->editor, sel); + select_lines(doc->editor, &sel); sci_start_undo_action(doc->editor->sci); @@ -202,9 +197,7 @@ action_indir_manip_item(GtkMenuItem *menuitem, gpointer gdata) sci_replace_sel(doc->editor->sci, new_file); /* select affected lines and set statusbar message */ - user_indicate(doc->editor, lines_affected, sel); - - g_free(sel); + user_indicate(doc->editor, lines_affected, &sel); sci_end_undo_action(doc->editor->sci); @@ -227,26 +220,19 @@ action_sci_manip_item(GtkMenuItem *menuitem, gpointer gdata) /* function pointer to gdata -- function to be used */ gint (*func)(ScintillaObject *, gint, gint) = gdata; GeanyDocument *doc = document_get_current(); - g_return_if_fail(doc != NULL); - struct lo_lines *sel = g_malloc(sizeof(struct lo_lines)); - sel->is_selection = FALSE; - sel->start_line = 0; - sel->end_line = 0; - - get_current_sel_lines(doc->editor->sci, sel); + struct lo_lines sel; + get_current_sel_lines(doc->editor->sci, &sel); gint lines_affected = 0; sci_start_undo_action(doc->editor->sci); - lines_affected = func(doc->editor->sci, sel->start_line, sel->end_line); + 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); - - g_free(sel); + user_indicate(doc->editor, lines_affected, &sel); sci_end_undo_action(doc->editor->sci); }