SF.net SVN: geany-plugins:[1120] trunk/geany-plugins

eht16 at users.sourceforge.net eht16 at xxxxx
Sun Jan 10 13:39:08 UTC 2010


Revision: 1120
          http://geany-plugins.svn.sourceforge.net/geany-plugins/?rev=1120&view=rev
Author:   eht16
Date:     2010-01-10 13:39:08 +0000 (Sun, 10 Jan 2010)

Log Message:
-----------
Add new addon 'Strip trailing blank lines' by Eugene Arshinov, thanks.

Modified Paths:
--------------
    trunk/geany-plugins/addons/AUTHORS
    trunk/geany-plugins/addons/ChangeLog
    trunk/geany-plugins/addons/README
    trunk/geany-plugins/addons/src/Makefile.am
    trunk/geany-plugins/addons/src/addons.c
    trunk/geany-plugins/po/POTFILES.in

Added Paths:
-----------
    trunk/geany-plugins/addons/src/ao_blanklines.c
    trunk/geany-plugins/addons/src/ao_blanklines.h

Modified: trunk/geany-plugins/addons/AUTHORS
===================================================================
--- trunk/geany-plugins/addons/AUTHORS	2010-01-05 23:39:22 UTC (rev 1119)
+++ trunk/geany-plugins/addons/AUTHORS	2010-01-10 13:39:08 UTC (rev 1120)
@@ -1,2 +1,3 @@
 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
 Bert Vermeulen <bert(at)biot(dot)com>
+Eugene Arshinov <earshinov(at)gmail(dot)com>

Modified: trunk/geany-plugins/addons/ChangeLog
===================================================================
--- trunk/geany-plugins/addons/ChangeLog	2010-01-05 23:39:22 UTC (rev 1119)
+++ trunk/geany-plugins/addons/ChangeLog	2010-01-10 13:39:08 UTC (rev 1120)
@@ -1,3 +1,11 @@
+2010-01-10  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>
+
+ * AUTHORS, README, src/ao_blanklines.c, src/ao_blanklines.h,
+   src/Makefile.am, src/addons.c:
+   Add new addon 'Strip trailing blank lines' by Eugene Arshinov,
+   thanks.
+
+
 2010-01-03  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>
 
  * src/ao_tasks.c:

Modified: trunk/geany-plugins/addons/README
===================================================================
--- trunk/geany-plugins/addons/README	2010-01-05 23:39:22 UTC (rev 1119)
+++ trunk/geany-plugins/addons/README	2010-01-10 13:39:08 UTC (rev 1120)
@@ -7,6 +7,7 @@
 This plugin adds various small addons to Geany which aren't worth an own
 plugin but might still useful for people.
 
+
 DocList
 ^^^^^^^
 This addon places a new item in the toolbar and when clicked offers a menu
@@ -44,7 +45,14 @@
 When double-clicking a word, all occurences of this word are searched
 and then highlighted (similar to Geany's 'Mark All' Find option).
 
+Strip trailing blank lines
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+This addon removes trailing blank lines from the document when it is
+saved. This is useful for filetypes like PHP but unfavorable for
+filetypes like C where compilers might warn about a missing trailing
+blank line.
 
+
 Downloads
 ---------
 

Modified: trunk/geany-plugins/addons/src/Makefile.am
===================================================================
--- trunk/geany-plugins/addons/src/Makefile.am	2010-01-05 23:39:22 UTC (rev 1119)
+++ trunk/geany-plugins/addons/src/Makefile.am	2010-01-10 13:39:08 UTC (rev 1120)
@@ -5,6 +5,7 @@
 
 addons_la_SOURCES = \
 	addons.h \
+	ao_blanklines.h \
 	ao_doclist.h \
 	ao_openuri.h \
 	ao_systray.h \
@@ -12,6 +13,7 @@
 	ao_markword.h \
 	ao_tasks.h \
 	addons.c \
+	ao_blanklines.c \
 	ao_doclist.c \
 	ao_openuri.c \
 	ao_systray.c \

Modified: trunk/geany-plugins/addons/src/addons.c
===================================================================
--- trunk/geany-plugins/addons/src/addons.c	2010-01-05 23:39:22 UTC (rev 1119)
+++ trunk/geany-plugins/addons/src/addons.c	2010-01-10 13:39:08 UTC (rev 1120)
@@ -24,6 +24,7 @@
 
 #include "geanyplugin.h"
 
+#include "ao_blanklines.h"
 #include "ao_doclist.h"
 #include "ao_openuri.h"
 #include "ao_systray.h"
@@ -40,7 +41,7 @@
 
 PLUGIN_VERSION_CHECK(165)
 PLUGIN_SET_INFO(_("Addons"), _("Various small addons for Geany."), VERSION,
-	"Enrico Tröger, Bert Vermeulen")
+	"Enrico Tröger, Bert Vermeulen, Eugene Arshinov")
 
 
 /* Keybinding(s) */
@@ -64,6 +65,7 @@
 	gboolean enable_systray;
 	gboolean enable_bookmarklist;
 	gboolean enable_markword;
+	gboolean strip_trailing_blank_lines;
 
 	gchar *tasks_token_list;
 	gboolean tasks_scan_all_documents;
@@ -85,6 +87,7 @@
 static void ao_document_activate_cb(GObject *obj, GeanyDocument *doc, gpointer data);
 static void ao_document_open_cb(GObject *obj, GeanyDocument *doc, gpointer data);
 static void ao_document_save_cb(GObject *obj, GeanyDocument *doc, gpointer data);
+static void ao_document_before_save_cb(GObject *obj, GeanyDocument *doc, gpointer data);
 static void ao_document_close_cb(GObject *obj, GeanyDocument *doc, gpointer data);
 gboolean ao_editor_notify_cb(GObject *object, GeanyEditor *editor,
 	SCNotification *nt, gpointer data);
@@ -92,13 +95,14 @@
 
 PluginCallback plugin_callbacks[] =
 {
-    { "update-editor-menu", (GCallback) &ao_update_editor_menu_cb, FALSE, NULL },
+	{ "update-editor-menu", (GCallback) &ao_update_editor_menu_cb, FALSE, NULL },
+	{ "editor-notify", (GCallback) &ao_editor_notify_cb, TRUE, NULL },
 
-	{ "editor-notify", (GCallback) &ao_editor_notify_cb, TRUE, NULL },
 	{ "document-open", (GCallback) &ao_document_open_cb, TRUE, NULL },
 	{ "document-save", (GCallback) &ao_document_save_cb, TRUE, NULL },
 	{ "document-close", (GCallback) &ao_document_close_cb, TRUE, NULL },
 	{ "document-activate", (GCallback) &ao_document_activate_cb, TRUE, NULL },
+	{ "document-before-save", (GCallback) &ao_document_before_save_cb, TRUE, NULL },
 
 	{ NULL, NULL, FALSE, NULL }
 };
@@ -174,6 +178,14 @@
 }
 
 
+static void ao_document_before_save_cb(GObject *obj, GeanyDocument *doc, gpointer data)
+{
+	g_return_if_fail(doc != NULL && doc->is_valid);
+
+	ao_blanklines_on_document_before_save(obj, doc, data);
+}
+
+
 GtkWidget *ao_image_menu_item_new(const gchar *stock_id, const gchar *label)
 {
 	GtkWidget *item = gtk_image_menu_item_new_with_label(label);
@@ -212,6 +224,8 @@
 		"addons", "enable_bookmarklist", FALSE);
 	ao_info->enable_markword = utils_get_setting_boolean(config,
 		"addons", "enable_markword", FALSE);
+	ao_info->strip_trailing_blank_lines = utils_get_setting_boolean(config,
+		"addons", "strip_trailing_blank_lines", FALSE);
 
 	main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
 	plugin_module_make_resident(geany_plugin);
@@ -224,6 +238,8 @@
 	ao_info->tasks = ao_tasks_new(ao_info->enable_tasks,
 						ao_info->tasks_token_list, ao_info->tasks_scan_all_documents);
 
+	ao_blanklines_set_enable(ao_info->strip_trailing_blank_lines);
+
 	/* setup keybindings */
 	key_group = plugin_set_key_group(geany_plugin, "addons", KB_COUNT, NULL);
 	keybindings_set_item(key_group, KB_FOCUS_BOOKMARK_LIST, kb_bmlist_activate,
@@ -269,6 +285,8 @@
 			g_object_get_data(G_OBJECT(dialog), "check_bookmarklist"))));
 		ao_info->enable_markword = (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
 			g_object_get_data(G_OBJECT(dialog), "check_markword"))));
+		ao_info->strip_trailing_blank_lines = (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
+			g_object_get_data(G_OBJECT(dialog), "check_blanklines"))));
 
 		g_key_file_load_from_file(config, ao_info->config_file, G_KEY_FILE_NONE, NULL);
 		g_key_file_set_boolean(config, "addons",
@@ -282,6 +300,8 @@
 		g_key_file_set_boolean(config, "addons", "enable_bookmarklist",
 			ao_info->enable_bookmarklist);
 		g_key_file_set_boolean(config, "addons", "enable_markword", ao_info->enable_markword);
+		g_key_file_set_boolean(config, "addons", "strip_trailing_blank_lines",
+		  ao_info->strip_trailing_blank_lines);
 
 		g_object_set(ao_info->doclist, "enable-doclist", ao_info->show_toolbar_doclist_item, NULL);
 		g_object_set(ao_info->openuri, "enable-openuri", ao_info->enable_openuri, NULL);
@@ -294,6 +314,7 @@
 			"scan-all-documents", ao_info->tasks_scan_all_documents,
 			"tokens", ao_info->tasks_token_list,
 			NULL);
+		ao_blanklines_set_enable(ao_info->strip_trailing_blank_lines);
 
 		if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
 		{
@@ -318,6 +339,7 @@
 	GtkWidget *vbox, *check_doclist, *check_openuri, *check_tasks, *check_systray;
 	GtkWidget *check_bookmarklist, *check_markword, *frame_tasks, *vbox_tasks;
 	GtkWidget *check_tasks_scan_mode, *entry_tasks_tokens, *label_tasks_tokens, *tokens_hbox;
+	GtkWidget *check_blanklines;
 
 	vbox = gtk_vbox_new(FALSE, 6);
 
@@ -388,6 +410,12 @@
 		ao_info->enable_markword);
 	gtk_box_pack_start(GTK_BOX(vbox), check_markword, FALSE, FALSE, 3);
 
+	check_blanklines = gtk_check_button_new_with_label(
+		_("Strip trailing blank lines"));
+	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_blanklines),
+		ao_info->strip_trailing_blank_lines);
+	gtk_box_pack_start(GTK_BOX(vbox), check_blanklines, FALSE, FALSE, 3);
+
 	g_object_set_data(G_OBJECT(dialog), "check_doclist", check_doclist);
 	g_object_set_data(G_OBJECT(dialog), "check_openuri", check_openuri);
 	g_object_set_data(G_OBJECT(dialog), "check_tasks", check_tasks);
@@ -396,6 +424,7 @@
 	g_object_set_data(G_OBJECT(dialog), "check_systray", check_systray);
 	g_object_set_data(G_OBJECT(dialog), "check_bookmarklist", check_bookmarklist);
 	g_object_set_data(G_OBJECT(dialog), "check_markword", check_markword);
+	g_object_set_data(G_OBJECT(dialog), "check_blanklines", check_blanklines);
 	g_signal_connect(dialog, "response", G_CALLBACK(ao_configure_response_cb), NULL);
 
 	ao_configure_tasks_toggled_cb(GTK_TOGGLE_BUTTON(check_tasks), dialog);
@@ -420,6 +449,8 @@
 	g_object_unref(ao_info->tasks);
 	g_free(ao_info->tasks_token_list);
 
+	ao_blanklines_set_enable(FALSE);
+
 	g_free(ao_info->config_file);
 	g_free(ao_info);
 }

Added: trunk/geany-plugins/addons/src/ao_blanklines.c
===================================================================
--- trunk/geany-plugins/addons/src/ao_blanklines.c	                        (rev 0)
+++ trunk/geany-plugins/addons/src/ao_blanklines.c	2010-01-10 13:39:08 UTC (rev 1120)
@@ -0,0 +1,92 @@
+/*
+ *			ao_blanklines.c - this file is part of Addons, a Geany plugin
+ *
+ *			Copyright 2009 Eugene Arshinov <earshinov at gmail.com>
+ *
+ *			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 of the License, 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 Street, Fifth Floor, Boston,
+ *			MA 02110-1301, USA.
+ */
+
+#include "geanyplugin.h"
+
+#include "addons.h"
+#include "ao_blanklines.h"
+
+static gboolean enabled = FALSE;
+
+static void editor_strip_trailing_newlines(GeanyEditor *editor)
+{
+	const gint maxline = sci_get_line_count(editor->sci) - 1;
+	const gint maxpos = sci_get_line_end_position(editor->sci, maxline);
+
+	gint line, start, end, pos;
+	gchar ch;
+
+	/*
+	 * Store index of the last non-empty line in `line' and position of the first
+	 * of its trailing spaces in `pos'. If all lines are empty, `line' will
+	 * contain -1, and `pos' will be undefined. If `line' does not contain
+	 * trailing spaces, `pos' will be its end position.
+	 */
+	for (line = maxline; line >= 0; line--)
+	{
+		start = sci_get_position_from_line(editor->sci, line);
+		end = sci_get_line_end_position(editor->sci, line);
+
+		/*
+		 * We can't be sure that `geany_data->file_prefs->strip_trailing_spaces'
+		 * setting is set, so we should check for trailing spaces manually.
+		 * Performance overhead of the for loop below is very small anyway,
+		 * so it does not worth checking the setting manually and writing
+		 * additional code.
+		 */
+
+		for (pos = end-1; pos >= start; pos--)
+		{
+			ch = sci_get_char_at(editor->sci, pos);
+			if (ch != ' ' && ch != '\t')
+				break;
+		}
+		++pos;
+		if (pos > start)
+			break;
+	}
+
+	if (line == -1 || geany_data->file_prefs->final_new_line)
+	{
+		/* leave one newline */
+		line++;
+		pos = sci_get_position_from_line(editor->sci, line);
+	}
+
+	if (pos < maxpos)
+	{
+		/* there are some lines to be removed */
+		sci_set_target_start(editor->sci, pos);
+		sci_set_target_end(editor->sci, maxpos);
+		sci_replace_target(editor->sci, "", FALSE);
+	}
+}
+
+void ao_blanklines_set_enable(gboolean enabled_)
+{
+	enabled = enabled_;
+}
+
+void ao_blanklines_on_document_before_save(GObject *object, GeanyDocument *doc, gpointer data)
+{
+	if (enabled)
+		editor_strip_trailing_newlines(doc->editor);
+}
\ No newline at end of file


Property changes on: trunk/geany-plugins/addons/src/ao_blanklines.c
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Added: trunk/geany-plugins/addons/src/ao_blanklines.h
===================================================================
--- trunk/geany-plugins/addons/src/ao_blanklines.h	                        (rev 0)
+++ trunk/geany-plugins/addons/src/ao_blanklines.h	2010-01-10 13:39:08 UTC (rev 1120)
@@ -0,0 +1,30 @@
+/*
+ *			ao_blanklines.h - this file is part of Addons, a Geany plugin
+ *
+ *			Copyright 2009 Eugene Arshinov <earshinov at gmail.com>
+ *
+ *			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 of the License, 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 Street, Fifth Floor, Boston,
+ *			MA 02110-1301, USA.
+ */
+
+#ifndef __AO_BLANKLINES_H__
+#define __AO_BLANKLINES_H__
+
+
+void ao_blanklines_set_enable(gboolean enabled_);
+
+void ao_blanklines_on_document_before_save(GObject *object, GeanyDocument *doc, gpointer data);
+
+#endif


Property changes on: trunk/geany-plugins/addons/src/ao_blanklines.h
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Modified: trunk/geany-plugins/po/POTFILES.in
===================================================================
--- trunk/geany-plugins/po/POTFILES.in	2010-01-05 23:39:22 UTC (rev 1119)
+++ trunk/geany-plugins/po/POTFILES.in	2010-01-10 13:39:08 UTC (rev 1120)
@@ -8,6 +8,7 @@
 addons/src/ao_doclist.c
 addons/src/ao_bookmarklist.c
 addons/src/ao_markword.c
+addons/src/ao_blanklines.c
 
 #codenav
 codenav/src/codenavigation.c


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Plugins-Commits mailing list