Skip to content

Commit

Permalink
lineoperations: remove every n-th line
Browse files Browse the repository at this point in the history
Imlements a new feature which allows to remove every n-th line.
The user can enter the value of 'n' in a dialog. Implements #772.
  • Loading branch information
lpaulsen93 committed May 14, 2019
1 parent 3ecb15a commit 2833d96
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lineoperations/README
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Features
* Keep Unique Lines
* Remove Empty Lines
* Remove Whitespace Lines
* Remove Every Nth Line
* Sort Lines Ascending
* Sort Lines Descending

Expand Down Expand Up @@ -197,6 +198,30 @@ Removes all lines that have only whitespace characters.
Line 1\n
Line 2\n

Remove Every Nth Line
---------------------

The user can enter a number N. Every Nth line will be removed then.

Example: Suppose a file has the following lines and the user enters the
value 2 for N. (#comments added for clarity)

::

Line 1
Line 2 #removed
Line 3
Line 4 #removed
Line 5

The **Remove Every Nth Line** (N=2) will change the file into this:

::

Line 1
Line 3
Line 5

Sort Lines
----------

Expand Down
42 changes: 42 additions & 0 deletions lineoperations/src/linefunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,45 @@ sortlndesc(gchar **lines, gint num_lines, gchar *new_file)

return num_lines;
}


/* Remove Every Nth Line */
gint
rmnthln(ScintillaObject *sci, gint line_num, gint end_line_num)
{
gboolean ok;
gdouble n;
gint count;
gint changed = 0; /* number of lines removed */

ok = dialogs_show_input_numeric(_("Remove every Nth line"),
_("Value of N"), &n, 1, 1000, 1);
if (ok == FALSE)
{
return 0;
}

count = n;
while(line_num <= end_line_num) /* loop through lines */
{
count--;

/* check if this is the nth line. */
if(count == 0)
{
scintilla_send_message(sci,
SCI_DELETERANGE,
sci_get_position_from_line(sci, line_num),
sci_get_line_length(sci, line_num));

line_num--;
end_line_num--;
changed++;
count = n;
}
line_num++;
}

/* return the number of lines deleted */
return -changed;
}
5 changes: 5 additions & 0 deletions lineoperations/src/linefunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ sortlnsasc(gchar **lines, gint num_lines, gchar *new_file);
gint
sortlndesc(gchar **lines, gint num_lines, gchar *new_file);


/* Remove Every Nth Line */
gint
rmnthln(ScintillaObject *sci, gint line_num, gint end_line_num);

#endif
3 changes: 3 additions & 0 deletions lineoperations/src/lineoperations.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ lo_init(GeanyPlugin *plugin, gpointer gdata)
{ N_("Remove _Whitespace Lines"),
G_CALLBACK(action_sci_manip_item), (gpointer) rmwhspln },
{ NULL },
{ N_("Remove Every _Nth Line"),
G_CALLBACK(action_sci_manip_item), (gpointer) rmnthln },
{ NULL },
{ N_("Sort Lines _Ascending"),
G_CALLBACK(action_indir_manip_item), (gpointer) sortlnsasc },
{ N_("Sort Lines _Descending"),
Expand Down
1 change: 1 addition & 0 deletions po/POTFILES.in
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ keyrecord/src/keyrecord.c

# LineOperations
lineoperations/src/lineoperations.c
lineoperations/src/linefunctions.c

# lipsum
lipsum/src/lipsum.c
Expand Down

0 comments on commit 2833d96

Please sign in to comment.