Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new "Lineoperations" plugin #324

Merged
merged 26 commits into from
Jan 17, 2016
Merged

Conversation

smostertdev
Copy link
Contributor

LineOperations is a plugin that gives a bunch of simple functions that can be applied to a document.
(remove duplicate lines, empty lines, lines with only whitespace, sort lines...)

It is intended to be a convenient way to use these functions for non-technical users.
I am thinking of adding some more useful functions (e.g. similar to TextFX in Notepad++).

Take a look and let me know what you think, I'm open to suggestions for improvements.

@frlan frlan added this to the 1.27 milestone Jan 8, 2016
@smostertdev
Copy link
Contributor Author

Alright, this should now compile with autotools. (Thanks for your help @frlan )
The README file was also re-written in reStructuredText. bd2293f

@frlan
Copy link
Member

frlan commented Jan 9, 2016

Ca you please rebase your pr against current master and repush it?

nfposn = 0;
k = 0;

if(newfile && lines) // verify memory allocation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

g_malloc() aborts on failure. If you really mean to support allocation failure you need to use g_try_malloc() instead (no need unless you allocate really large data, as on very low memory situations something in GLib/GTK/Geany will most likely abort anyway).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is now updated. I decided to keep g_malloc and remove the null check since it is unnecessary as "GLib/GTK/Geany will most likely abort anyways.

Line 1\n #NOT removed (contains non whitespace chars)
Line 2\n

The **Remove Unique Lines** will change the file into this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too, reference should probably be "Remove Whitespace Lines"

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.
@smostertdev smostertdev force-pushed the lineoperations branch 2 times, most recently from d0f4fd2 to 8ff44b7 Compare January 16, 2016 04:42


void
lo_cleanup(void)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be static, and signature should be static void lo_cleanup(GeanyPlugin *plugin, gpointer pdata)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched `sci_get_line_indentation()` to `SCI_GETLINEINDENTPOSITION`.
Added 'static' keyword for `lo_cleanup`.

Added `main_locale_init(LOCALEDIR, GETTEXT_PACKAGE)` for the translation
information.
g_signal_connect(rmdupln_item, "activate", G_CALLBACK(action_rmdupln_item), NULL);
g_signal_connect(rmunqln_item, "activate", G_CALLBACK(action_rmunqln_item), NULL);
g_signal_connect(rmemtyln_item, "activate", G_CALLBACK(action_rmemtyln_item), NULL);
g_signal_connect(rmwhspln_item, "activate", G_CALLBACK(action_rmwhspln_item), NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could lower duplication of the wrapper callbacks by having a single callback that calls the appropriate function (either passed as parameter [1] or referenced as an ID or something), as they all have they all behave the same (but the sort ones).
You may or may not want this, it's merely a suggestion. And it's even a bad one if you expect to alter the various functions to receive different parameters in the future.

[1] technically in plain C you're not supposed to convert a function pointer to a data pointer. However, GTK depends on this and it effectively works on all supported platforms (if not all platforms in general).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b4n@f05a05b if you're interested. The patch however depends on the simplified menu building, so you might want to apply it afterward, or just use this as inspiration (or not at all).


total_num_chars = sci_get_length(doc->editor->sci);
total_num_lines = sci_get_line_count(doc->editor->sci);
lines = g_malloc(sizeof(gchar *) * total_num_lines+1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a subtle bug here: the +1 is not applied correctly as * has priority over +. Wrap in parentheses. (thanks to Valgrind for finding this)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix available at b4n@ee35465

An overlooked operator priority lead to underallocating
`sizeof(gchar*)-1` bytes, later leading to an invalid memory access.
@b4n b4n added the new-plugin label Jan 17, 2016
Removed trailing spaces, and changed all indents to tabs.
plugin->info->name = _("Line Operations");
plugin->info->description = _("Line Operations provides a handful of functions that can be applied to a document such as, removing duplicate lines, removing empty lines, removing lines with only whitespace, and sorting lines.");
plugin->info->version = "0.1";
plugin->info->author = _("Sylvan Mostert <[email protected]>");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there probably is no need to translate your name :)

@b4n b4n removed the pending fixes label Jan 17, 2016
@b4n
Copy link
Member

b4n commented Jan 17, 2016

LGTM

frlan added a commit that referenced this pull request Jan 17, 2016
@frlan frlan merged commit 7c1d98d into geany:master Jan 17, 2016
@smostertdev smostertdev deleted the lineoperations branch January 19, 2016 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants