Branch: refs/heads/master Author: Sylvan Mostert smostert.dev@gmail.com Committer: Sylvan Mostert smostert.dev@gmail.com Date: Fri, 09 Sep 2016 05:43:53 UTC Commit: be20ded4e8adbf85833cc8cc6368351189ff5516 https://github.com/geany/geany-plugins/commit/be20ded4e8adbf85833cc8cc636835...
Log Message: ----------- lineoperations: combined similar code
Repeated variable declarations in each function (in linefunctions.c) are now passed in.
Repeated code is now in `lineoperations.c` in `action_sci_manip_item()` and `action_indir_manip_item()`.
The `sortlines()` function is now split into ascending and descending -- to remove extra gboolean flag parameter.
Added undo action to include `ensure_final_newline()`.
Modified Paths: -------------- lineoperations/src/linefunctions.c lineoperations/src/linefunctions.h lineoperations/src/lineoperations.c
Modified: lineoperations/src/linefunctions.c 47 lines changed, 17 insertions(+), 30 deletions(-) =================================================================== @@ -284,40 +284,27 @@ void rmwhspln(GeanyDocument *doc) { }
-/* Sort Lines Ascending and Descending */ -void sortlines(GeanyDocument *doc, gboolean asc) { - gint total_num_lines; /* number of lines in the document */ - gchar **lines; /* array to hold all lines in the document */ - gchar *new_file; /* *final* string to replace current document */ - gint i; /* iterator */ - - total_num_lines = sci_get_line_count(doc->editor->sci); - lines = g_malloc(sizeof(gchar *) * (total_num_lines+1)); +/* Sort Lines Ascending */ +void sortlinesasc(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file) { + gchar *nf_end = new_file; /* points to last char of new_file */ + gint i;
- /* 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); + qsort(lines, num_lines, sizeof(gchar *), compare_asc);
- /* copy *all* lines into **lines array */ - for(i = 0; i < total_num_lines; i++) - lines[i] = sci_get_line(doc->editor->sci, i); + /* join **lines into one string (new_file) */ + for(i = 0; i < num_lines; i++) + nf_end = g_stpcpy(nf_end, lines[i]); +}
- /* sort **lines array */ - if(asc) - qsort(lines, total_num_lines, sizeof(gchar *), compare_asc); - else - qsort(lines, total_num_lines, sizeof(gchar *), compare_desc);
- /* join **lines into one string (new_file) */ - lines[total_num_lines] = NULL; - new_file = g_strjoinv("", lines); +/* Sort Lines Descending */ +void sortlinesdesc(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file) { + gchar *nf_end = new_file; /* points to last char of new_file */ + gint i;
- /* set new document */ - sci_set_text(doc->editor->sci, new_file); + qsort(lines, num_lines, sizeof(gchar *), compare_desc);
- /* free used memory */ - for(i = 0; i < total_num_lines; i++) - g_free(lines[i]); - g_free(lines); - g_free(new_file); + /* join **lines into one string (new_file) */ + for(i = 0; i < num_lines; i++) + nf_end = g_stpcpy(nf_end, lines[i]); }
Modified: lineoperations/src/linefunctions.h 17 lines changed, 10 insertions(+), 7 deletions(-) =================================================================== @@ -30,26 +30,29 @@
/* Remove Duplicate Lines, sorted */ -void rmdupst(GeanyDocument *doc); +void rmdupst(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file);
/* Remove Duplicate Lines, ordered */ -void rmdupln(GeanyDocument *doc); +void rmdupln(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file);
/* Remove Unique Lines */ -void rmunqln(GeanyDocument *doc); +void rmunqln(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file);
/* Remove Empty Lines */ -void rmemtyln(GeanyDocument *doc); +void rmemtyln(GeanyDocument *doc, gint num_lines);
/* Remove Whitespace Lines */ -void rmwhspln(GeanyDocument *doc); +void rmwhspln(GeanyDocument *doc, gint num_lines);
-/* Sort Lines Ascending and Descending */ -void sortlines(GeanyDocument *doc, gboolean asc); +/* Sort Lines Ascending */ +void sortlinesasc(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file); + +/* Sort Lines Descending */ +void sortlinesdesc(GeanyDocument *doc, gchar **lines, gint num_lines, gchar *new_file);
#endif
Modified: lineoperations/src/lineoperations.c 94 lines changed, 76 insertions(+), 18 deletions(-) =================================================================== @@ -29,26 +29,84 @@ static GtkWidget *main_menu_item = NULL;
-/* call custom func */ + + +/* 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); + } +} + + +/* + * functions with indirect scintilla manipulation + * e.g. requires **lines array, *new_file... +*/ static void -action_func_item(GtkMenuItem *menuitem, gpointer gdata) +action_indir_manip_item(GtkMenuItem *menuitem, gpointer gdata) { - void (*func)(GeanyDocument *) = gdata; - GeanyDocument *doc = NULL; - doc = document_get_current(); + void (*func)(GeanyDocument *, gchar **, gint, gchar *) = gdata; + GeanyDocument *doc = document_get_current();
- if(doc) func(doc); + gint num_chars = sci_get_length(doc->editor->sci); + gint num_lines = sci_get_line_count(doc->editor->sci); + gchar **lines = g_malloc(sizeof(gchar *) * num_lines); + gchar *new_file = g_malloc(sizeof(gchar) * (num_chars+1)); + gint i = 0; + new_file[0] = '\0'; + + + /* copy *all* lines into **lines array */ + for(i = 0; i < num_lines; i++) + lines[i] = sci_get_line(doc->editor->sci, i); + + sci_start_undo_action(doc->editor->sci); + + /* if file is not empty, ensure that the file ends with newline */ + if(num_lines != 1) + ensure_final_newline(doc->editor, num_lines); + + if(doc) func(doc, lines, num_lines, new_file); + + /* set new document */ + sci_set_text(doc->editor->sci, new_file); + + sci_end_undo_action(doc->editor->sci); + + /* free used memory */ + for(i = 0; i < num_lines; i++) + g_free(lines[i]); + g_free(lines); + g_free(new_file); }
-/* Sort Lines */ +/* + * functions with direct scintilla manipulation + * e.g. no need for **lines array, *new_file... +*/ static void -action_sort_item(GtkMenuItem *menuitem, gpointer gdata) +action_sci_manip_item(GtkMenuItem *menuitem, gpointer gdata) { - GeanyDocument *doc = NULL; - doc = document_get_current(); + void (*func)(GeanyDocument *, gint) = gdata; + GeanyDocument *doc = document_get_current(); + + gint num_lines = sci_get_line_count(doc->editor->sci); + + sci_start_undo_action(doc->editor->sci); + + if(doc) func(doc, num_lines);
- if(doc) sortlines(doc, GPOINTER_TO_INT(gdata)); + sci_end_undo_action(doc->editor->sci); }
@@ -65,21 +123,21 @@ lo_init(GeanyPlugin *plugin, gpointer gdata) gpointer cb_data; } menu_items[] = { { N_("Remove Duplicate Lines, _Sorted"), - G_CALLBACK(action_func_item), (gpointer) rmdupst }, + G_CALLBACK(action_indir_manip_item), (gpointer) rmdupst }, { N_("Remove Duplicate Lines, _Ordered"), - G_CALLBACK(action_func_item), (gpointer) rmdupln }, + G_CALLBACK(action_indir_manip_item), (gpointer) rmdupln }, { N_("Remove _Unique Lines"), - G_CALLBACK(action_func_item), (gpointer) rmunqln }, + G_CALLBACK(action_indir_manip_item), (gpointer) rmunqln }, { NULL }, { N_("Remove _Empty Lines"), - G_CALLBACK(action_func_item), (gpointer) rmemtyln }, + G_CALLBACK(action_sci_manip_item), (gpointer) rmemtyln }, { N_("Remove _Whitespace Lines"), - G_CALLBACK(action_func_item), (gpointer) rmwhspln }, + G_CALLBACK(action_sci_manip_item), (gpointer) rmwhspln }, { NULL }, { N_("Sort Lines _Ascending"), - G_CALLBACK(action_sort_item), GINT_TO_POINTER(1) }, + G_CALLBACK(action_indir_manip_item), (gpointer) sortlinesasc }, { N_("Sort Lines _Descending"), - G_CALLBACK(action_sort_item), GINT_TO_POINTER(0) } + G_CALLBACK(action_indir_manip_item), (gpointer) sortlinesdesc } };
main_menu_item = gtk_menu_item_new_with_mnemonic(_("_Line Operations"));
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).
plugins-commits@lists.geany.org