summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaggerbot <[email protected]>2015-08-28 09:42:37 +0000
committerinfirit <[email protected]>2015-09-02 11:34:01 +0200
commit9f166a05aa5fb5ac8ce17f025aebf2a190b52672 (patch)
treec6891dd290f76882823cebbdc0113ddbf3415324
parentb24934e186456b50d7702192ec3a270926831791 (diff)
downloadpluma-9f166a05aa5fb5ac8ce17f025aebf2a190b52672.tar.bz2
pluma-9f166a05aa5fb5ac8ce17f025aebf2a190b52672.tar.xz
Add plugin to strip trailing spaces on save.
-rw-r--r--configure.ac1
-rwxr-xr-xplugins/Makefile.am6
-rw-r--r--plugins/trailsave/Makefile.am31
-rw-r--r--plugins/trailsave/pluma-trail-save-plugin.c214
-rw-r--r--plugins/trailsave/pluma-trail-save-plugin.h70
-rw-r--r--plugins/trailsave/trailsave.pluma-plugin.desktop.in9
6 files changed, 329 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac
index 4e27be17..91482659 100644
--- a/configure.ac
+++ b/configure.ac
@@ -400,6 +400,7 @@ plugins/sort/Makefile
plugins/spell/Makefile
plugins/taglist/Makefile
plugins/time/Makefile
+plugins/trailsave/Makefile
po/Makefile.in
tests/Makefile
])
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index c31e071d..f8df8e4d 100755
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -10,7 +10,8 @@ DIST_SUBDIRS = \
sort \
spell \
taglist \
- time
+ time \
+ trailsave
SUBDIRS = \
changecase \
@@ -20,7 +21,8 @@ SUBDIRS = \
modelines \
sort \
taglist \
- time
+ time \
+ trailsave
if ENABLE_PYTHON
SUBDIRS += pythonconsole snippets quickopen
diff --git a/plugins/trailsave/Makefile.am b/plugins/trailsave/Makefile.am
new file mode 100644
index 00000000..c8446959
--- /dev/null
+++ b/plugins/trailsave/Makefile.am
@@ -0,0 +1,31 @@
+# trailsave plugin
+plugindir = $(PLUMA_PLUGINS_LIBS_DIR)
+
+AM_CPPFLAGS = \
+ -I$(top_srcdir) \
+ $(PLUMA_CFLAGS) \
+ $(WARN_CFLAGS) \
+ $(DISABLE_DEPRECATED_CFLAGS)
+
+plugin_LTLIBRARIES = libtrailsave.la
+
+libtrailsave_la_SOURCES = \
+ pluma-trail-save-plugin.h \
+ pluma-trail-save-plugin.c
+
+libtrailsave_la_LDFLAGS = $(PLUGIN_LIBTOOL_FLAGS)
+libtrailsave_la_LIBADD = $(PLUMA_LIBS)
+
+plugin_in_files = trailsave.pluma-plugin.desktop.in
+
+%.pluma-plugin: %.pluma-plugin.desktop.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*po) ; $(INTLTOOL_MERGE) $(top_srcdir)/po $< $@ -d -u -c $(top_builddir)/po/.intltool-merge-cache
+
+plugin_DATA = $(plugin_in_files:.pluma-plugin.desktop.in=.pluma-plugin)
+
+EXTRA_DIST = $(plugin_in_files)
+
+CLEANFILES = $(plugin_DATA)
+DISTCLEANFILES = $(plugin_DATA)
+
+
+-include $(top_srcdir)/git.mk
diff --git a/plugins/trailsave/pluma-trail-save-plugin.c b/plugins/trailsave/pluma-trail-save-plugin.c
new file mode 100644
index 00000000..dbc1621b
--- /dev/null
+++ b/plugins/trailsave/pluma-trail-save-plugin.c
@@ -0,0 +1,214 @@
+/*
+ * pluma-trail-save-plugin.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $Id$
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "pluma-trail-save-plugin.h"
+
+PLUMA_PLUGIN_REGISTER_TYPE(PlumaTrailSavePlugin, pluma_trail_save_plugin)
+
+static void
+strip_trailing_spaces (GtkTextBuffer *text_buffer)
+{
+ gint line_count, line_num;
+ GtkTextIter line_start, line_end;
+ gchar *slice;
+ gchar byte;
+ gint byte_index;
+ gint strip_start_index, strip_end_index;
+ gboolean should_strip;
+ GtkTextIter strip_start, strip_end;
+
+ g_assert (text_buffer != NULL);
+
+ line_count = gtk_text_buffer_get_line_count (text_buffer);
+
+ for (line_num = 0; line_num < line_count; ++line_num)
+ {
+ /* Get line text */
+ gtk_text_buffer_get_iter_at_line (text_buffer, &line_start, line_num);
+
+ if (line_num == line_count - 1)
+ {
+ gtk_text_buffer_get_end_iter (text_buffer, &line_end);
+ }
+ else
+ {
+ gtk_text_buffer_get_iter_at_line (text_buffer, &line_end, line_num + 1);
+ }
+
+ slice = gtk_text_buffer_get_slice (text_buffer, &line_start, &line_end, TRUE);
+
+ if (slice == NULL)
+ {
+ continue;
+ }
+
+ /* Find indices of bytes that should be stripped */
+ should_strip = FALSE;
+
+ for (byte_index = 0; slice [byte_index] != 0; ++byte_index)
+ {
+ byte = slice [byte_index];
+
+ if ((byte == ' ') || (byte == '\t'))
+ {
+ if (!should_strip)
+ {
+ strip_start_index = byte_index;
+ should_strip = TRUE;
+ }
+
+ strip_end_index = byte_index + 1;
+ }
+ else if ((byte == '\r') || (byte == '\n'))
+ {
+ break;
+ }
+ else
+ {
+ should_strip = FALSE;
+ }
+ }
+
+ g_free (slice);
+
+ /* Strip trailing spaces */
+ if (should_strip)
+ {
+ gtk_text_buffer_get_iter_at_line_index (text_buffer, &strip_start, line_num, strip_start_index);
+ gtk_text_buffer_get_iter_at_line_index (text_buffer, &strip_end, line_num, strip_end_index);
+ gtk_text_buffer_delete (text_buffer, &strip_start, &strip_end);
+ }
+ }
+}
+
+static void
+on_save (PlumaDocument *document,
+ const gchar *uri,
+ PlumaEncoding *encoding,
+ PlumaDocumentSaveFlags save_flags,
+ PlumaPlugin *plugin)
+{
+ GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (document);
+
+ strip_trailing_spaces (text_buffer);
+}
+
+static void
+on_tab_added (PlumaWindow *window,
+ PlumaTab *tab,
+ PlumaPlugin *plugin)
+{
+ PlumaDocument *document;
+
+ document = pluma_tab_get_document (tab);
+ g_signal_connect (document, "save", G_CALLBACK (on_save), plugin);
+}
+
+static void
+on_tab_removed (PlumaWindow *window,
+ PlumaTab *tab,
+ PlumaPlugin *plugin)
+{
+ PlumaDocument *document;
+
+ document = pluma_tab_get_document (tab);
+ g_signal_handlers_disconnect_by_data (document, plugin);
+}
+
+static void
+impl_activate (PlumaPlugin *plugin,
+ PlumaWindow *window)
+{
+ GList *documents;
+ GList *documents_iter;
+ PlumaDocument *document;
+
+ pluma_debug (DEBUG_PLUGINS);
+
+ g_signal_connect (window, "tab_added", G_CALLBACK (on_tab_added), plugin);
+ g_signal_connect (window, "tab_removed", G_CALLBACK (on_tab_removed), plugin);
+
+ documents = pluma_window_get_documents (window);
+
+ for (documents_iter = documents;
+ documents_iter && documents_iter->data;
+ documents_iter = documents_iter->next)
+ {
+ document = (PlumaDocument *) documents_iter->data;
+ g_signal_connect (document, "save", G_CALLBACK (on_save), plugin);
+ }
+
+ g_list_free (documents);
+}
+
+static void
+impl_deactivate (PlumaPlugin *plugin,
+ PlumaWindow *window)
+{
+ GList *documents;
+ GList *documents_iter;
+ PlumaDocument *document;
+
+ pluma_debug (DEBUG_PLUGINS);
+
+ g_signal_handlers_disconnect_by_data (window, plugin);
+
+ documents = pluma_window_get_documents (window);
+
+ for (documents_iter = documents;
+ documents_iter && documents_iter->data;
+ documents_iter = documents_iter->next)
+ {
+ document = (PlumaDocument *) documents_iter->data;
+ g_signal_handlers_disconnect_by_data (document, plugin);
+ }
+
+ g_list_free (documents);
+}
+
+static void
+pluma_trail_save_plugin_init (PlumaTrailSavePlugin *plugin)
+{
+ pluma_debug_message (DEBUG_PLUGINS, "PlumaTrailSavePlugin initializing");
+}
+
+static void
+pluma_trail_save_plugin_finalize (GObject *object)
+{
+ pluma_debug_message (DEBUG_PLUGINS, "PlumaTrailSavePlugin finalizing");
+
+ G_OBJECT_CLASS (pluma_trail_save_plugin_parent_class)->finalize (object);
+}
+
+static void
+pluma_trail_save_plugin_class_init (PlumaTrailSavePluginClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ PlumaPluginClass *plugin_class = PLUMA_PLUGIN_CLASS (klass);
+
+ object_class->finalize = pluma_trail_save_plugin_finalize;
+
+ plugin_class->activate = impl_activate;
+ plugin_class->deactivate = impl_deactivate;
+}
diff --git a/plugins/trailsave/pluma-trail-save-plugin.h b/plugins/trailsave/pluma-trail-save-plugin.h
new file mode 100644
index 00000000..4d0e3306
--- /dev/null
+++ b/plugins/trailsave/pluma-trail-save-plugin.h
@@ -0,0 +1,70 @@
+/*
+ * pluma-trail-save-plugin.h
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $Id$
+ */
+
+#ifndef __PLUMA_TRAIL_SAVE_PLUGIN_H__
+#define __PLUMA_TRAIL_SAVE_PLUGIN_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <pluma/pluma-plugin.h>
+
+G_BEGIN_DECLS
+
+/*
+ * Type checking and casting macros
+ */
+#define PLUMA_TYPE_TRAIL_SAVE_PLUGIN (pluma_trail_save_plugin_get_type ())
+#define PLUMA_TRAIL_SAVE_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), PLUMA_TYPE_TRAIL_SAVE_PLUGIN, PlumaTrailSavePlugin))
+#define PLUMA_TRAIL_SAVE_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), PLUMA_TYPE_TRAIL_SAVE_PLUGIN, PlumaTrailSavePluginClass))
+#define PLUMA_IS_TRAIL_SAVE_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), PLUMA_TYPE_TRAIL_SAVE_PLUGIN))
+#define PLUMA_IS_TRAIL_SAVE_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), PLUMA_TYPE_TRAIL_SAVE_PLUGIN))
+#define PLUMA_TRAIL_SAVE_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), PLUMA_TYPE_TRAIL_SAVE_PLUGIN, PlumaTrailSavePluginClass))
+
+/*
+ * Main object structure
+ */
+typedef struct _PlumaTrailSavePlugin PlumaTrailSavePlugin;
+
+struct _PlumaTrailSavePlugin
+{
+ PlumaPlugin parent_instance;
+};
+
+/*
+ * Class definition
+ */
+typedef struct _PlumaTrailSavePluginClass PlumaTrailSavePluginClass;
+
+struct _PlumaTrailSavePluginClass
+{
+ PlumaPluginClass parent_class;
+};
+
+/*
+ * Public methods
+ */
+GType pluma_trail_save_plugin_get_type (void) G_GNUC_CONST;
+
+/* All the plugins must implement this function */
+G_MODULE_EXPORT GType register_pluma_plugin (GTypeModule *module);
+
+G_END_DECLS
+
+#endif /* __PLUMA_TRAIL_SAVE_PLUGIN_H__ */
diff --git a/plugins/trailsave/trailsave.pluma-plugin.desktop.in b/plugins/trailsave/trailsave.pluma-plugin.desktop.in
new file mode 100644
index 00000000..144fef9d
--- /dev/null
+++ b/plugins/trailsave/trailsave.pluma-plugin.desktop.in
@@ -0,0 +1,9 @@
+[Pluma Plugin]
+Module=trailsave
+IAge=2
+_Name=Save Without Trailing Spaces
+_Description=Removes trailing spaces from lines before saving.
+Icon=gtk-cut
+Authors=Marty Mills <[email protected]>
+Copyright=Copyright © 2015 Marty Mills
+Website=http://www.mate-desktop.org