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

eht16 at users.sourceforge.net eht16 at xxxxx
Tue Feb 17 13:58:58 UTC 2009


Revision: 483
          http://geany-plugins.svn.sourceforge.net/geany-plugins/?rev=483&view=rev
Author:   eht16
Date:     2009-02-17 13:58:58 +0000 (Tue, 17 Feb 2009)

Log Message:
-----------
Use the "editor-notify" signal to catch text modifications for checking while typing. Also remove the timeout which never worked as intended.
Don't add suggestions to the messages window when checking while typing.
These changes should increase the usability and accuracy of the 'check while typing' feature while not being much slower (if ever).

Modified Paths:
--------------
    trunk/spellcheck/ChangeLog
    trunk/spellcheck/src/gui.c
    trunk/spellcheck/src/gui.h
    trunk/spellcheck/src/scplugin.c
    trunk/spellcheck/src/scplugin.h
    trunk/spellcheck/src/speller.c
    trunk/spellcheck/src/speller.h

Modified: trunk/spellcheck/ChangeLog
===================================================================
--- trunk/spellcheck/ChangeLog	2009-02-15 22:55:55 UTC (rev 482)
+++ trunk/spellcheck/ChangeLog	2009-02-17 13:58:58 UTC (rev 483)
@@ -1,3 +1,16 @@
+2009-02-17  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>
+
+ * src/gui.c, src/gui.h, src/scplugin.c, src/scplugin.h, src/speller.c,
+   src/speller.h:
+   Use the "editor-notify" signal to catch text modifications for
+   checking while typing. Also remove the timeout which never worked
+   as intended.
+   Don't add suggestions to the messages window when checking while
+   typing.
+   These changes should increase the usability and accuracy of the
+   'check while typing' feature while not being much slower (if ever).
+
+
 2009-02-15  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>
 
  * src/sc_plugin.c:

Modified: trunk/spellcheck/src/gui.c
===================================================================
--- trunk/spellcheck/src/gui.c	2009-02-15 22:55:55 UTC (rev 482)
+++ trunk/spellcheck/src/gui.c	2009-02-17 13:58:58 UTC (rev 483)
@@ -129,20 +129,6 @@
 }
 
 
-static void clear_indicators_on_line(GeanyDocument *doc, gint line_number)
-{
-	gint start_pos, length;
-
-	g_return_if_fail(doc != NULL);
-
-	start_pos = sci_get_position_from_line(doc->editor->sci, line_number);
-	length = sci_get_line_length(doc->editor->sci, line_number);
-
-	sci_indicator_clear(doc->editor->sci, start_pos, length);
-}
-
-
-
 static void menu_suggestion_item_activate_cb(GtkMenuItem *menuitem, gpointer gdata)
 {
 	const gchar *sugg;
@@ -345,51 +331,42 @@
 }
 
 
-/* Checks only the last word before the current cursor position -> check as you type. */
-gboolean gui_key_release_cb(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
+static void indicator_clear_on_range(GeanyEditor *editor, gint start, gint len)
 {
-	gint line_number;
-	GString *str = g_string_sized_new(256);
-	gchar *line;
-	GeanyDocument *doc;
-	static time_t time_prev = 0;
-	time_t time_now = time(NULL);
-	GtkWidget *focusw;
+	sci_indicator_set(editor->sci, GEANY_INDICATOR_ERROR);
+	sci_indicator_clear(editor->sci, start, len);
+}
 
-	if (! sc->check_while_typing)
-		return FALSE;
-	/* check only once a second */
-	if (time_now == time_prev)
-		return FALSE;
-	/* set current time for the next key press */
-	time_prev = time_now;
 
-	doc = document_get_current();
-	/* bail out if we don't have a document or if we are not in the editor widget */
-	focusw = gtk_window_get_focus(GTK_WINDOW(geany->main_widgets->window));
-	if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
-		return FALSE;
+gboolean gui_editor_notify_cb(GObject *obj, GeanyEditor *editor, SCNotification *nt, gpointer data)
+{
+	gint start_pos, end_pos, pos;
+	gchar *word;
 
-	if (ev->keyval == '\r' &&
-		scintilla_send_message(doc->editor->sci, SCI_GETEOLMODE, 0, 0) == SC_EOL_CRLF)
-	{	/* prevent double line checking */
+	if (! sc->check_while_typing ||	nt->nmhdr.code != SCN_MODIFIED ||
+		! (nt->modificationType & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT)))
+	{
 		return FALSE;
 	}
 
-	line_number = sci_get_current_line(doc->editor->sci);
-	if (ev->keyval == '\n' || ev->keyval == '\r')
-		line_number--; /* check previous line if we start a new one */
-	line = sci_get_line(doc->editor->sci, line_number);
+	pos = nt->position;
 
-	clear_indicators_on_line(doc, line_number);
-	if (speller_process_line(doc, line_number, line) != 0)
+	/* retrieve the word at the position */
+	word = editor_get_word_at_pos(editor, pos, GEANY_WORDCHARS);
+	if (word == NULL)
+		return FALSE;
+
+	start_pos = scintilla_send_message(editor->sci, SCI_WORDSTARTPOSITION, pos, 0);
+	end_pos = start_pos + strlen(word);
+
+	indicator_clear_on_range(editor, start_pos, end_pos - start_pos);
+	if (speller_check_word(editor->document, -1, word, start_pos, end_pos) != 0)
 	{
 		if (sc->use_msgwin)
 			msgwin_switch_tab(MSG_MESSAGE, FALSE);
 	}
 
-	g_string_free(str, TRUE);
-	g_free(line);
+	g_free(word);
 
 	return FALSE;
 }

Modified: trunk/spellcheck/src/gui.h
===================================================================
--- trunk/spellcheck/src/gui.h	2009-02-15 22:55:55 UTC (rev 482)
+++ trunk/spellcheck/src/gui.h	2009-02-17 13:58:58 UTC (rev 483)
@@ -33,6 +33,8 @@
 
 gboolean gui_key_release_cb(GtkWidget *widget, GdkEventKey *ev, gpointer user_data);
 
+gboolean gui_editor_notify_cb(GObject *obj, GeanyEditor *editor, SCNotification *nt, gpointer data);
+
 void gui_kb_run_activate_cb(guint key_id);
 
 void gui_kb_toggle_typing_activate_cb(guint key_id);

Modified: trunk/spellcheck/src/scplugin.c
===================================================================
--- trunk/spellcheck/src/scplugin.c	2009-02-15 22:55:55 UTC (rev 482)
+++ trunk/spellcheck/src/scplugin.c	2009-02-17 13:58:58 UTC (rev 483)
@@ -75,10 +75,12 @@
 
 
 
+
 PluginCallback plugin_callbacks[] =
 {
-    { "update-editor-menu", (GCallback) &gui_update_editor_menu_cb, FALSE, NULL },
-    { NULL, NULL, FALSE, NULL }
+	{ "update-editor-menu", (GCallback) &gui_update_editor_menu_cb, FALSE, NULL },
+	{ "editor-notify", (GCallback) &gui_editor_notify_cb, TRUE, NULL },
+	{ NULL, NULL, FALSE, NULL }
 };
 
 
@@ -190,9 +192,6 @@
 	sp_item = gui_create_menu(sc->menu_item);
 	gtk_widget_show_all(sp_item);
 
-	sc->signal_id = g_signal_connect(geany->main_widgets->window,
-		"key-release-event", G_CALLBACK(gui_key_release_cb), NULL);
-
 	/* setup keybindings */
 	keybindings_set_item(plugin_key_group, KB_SPELL_CHECK, gui_kb_run_activate_cb,
 		0, 0, "spell_check", _("Run Spell Check"), NULL);
@@ -283,8 +282,6 @@
 	}
 	g_ptr_array_free(sc->dicts, TRUE);
 
-	g_signal_handler_disconnect(geany->main_widgets->window, sc->signal_id);
-
 	gtk_widget_destroy(sc->edit_menu);
 	gtk_widget_destroy(sc->edit_menu_sep);
 	if (sc->toolbar_button != NULL)

Modified: trunk/spellcheck/src/scplugin.h
===================================================================
--- trunk/spellcheck/src/scplugin.h	2009-02-15 22:55:55 UTC (rev 482)
+++ trunk/spellcheck/src/scplugin.h	2009-02-17 13:58:58 UTC (rev 483)
@@ -35,7 +35,6 @@
 	gboolean use_msgwin;
 	gboolean check_while_typing;
 	gboolean show_toolbar_item;
-	gulong signal_id;
 	GPtrArray *dicts;
 	GtkWidget *menu_item;
 	GtkWidget *edit_menu;

Modified: trunk/spellcheck/src/speller.c
===================================================================
--- trunk/spellcheck/src/speller.c	2009-02-15 22:55:55 UTC (rev 482)
+++ trunk/spellcheck/src/speller.c	2009-02-17 13:58:58 UTC (rev 483)
@@ -59,12 +59,15 @@
 }
 
 
-static gint speller_check_word(GeanyDocument *doc, gint line_number, const gchar *word,
+gint speller_check_word(GeanyDocument *doc, gint line_number, const gchar *word,
 					    gint start_pos, gint end_pos)
 {
 	gsize n_suggs = 0;
 
 	g_return_val_if_fail(speller_dict != NULL, 0);
+	g_return_val_if_fail(doc != NULL, 0);
+	g_return_val_if_fail(word != NULL, 0);
+	g_return_val_if_fail(start_pos >= 0 && end_pos >= 0, 0);
 
 	if (! NZV(word))
 		return 0;
@@ -81,12 +84,9 @@
 	if (enchant_dict_check(speller_dict, word, -1) == 0)
 		return 0;
 
-	if (start_pos == -1)
-		start_pos = end_pos - strlen(word);
-
 	editor_indicator_set_on_range(doc->editor, GEANY_INDICATOR_ERROR, start_pos, end_pos);
 
-	if (sc->use_msgwin)
+	if (sc->use_msgwin && line_number != -1)
 	{
 		gsize j;
 		gchar **suggs;
@@ -119,7 +119,7 @@
 }
 
 
-gint speller_process_line(GeanyDocument *doc, gint line_number, const gchar *line)
+static gint speller_process_line(GeanyDocument *doc, gint line_number, const gchar *line)
 {
 	gint pos_start, pos_end;
 	gint wstart, wend;
@@ -134,7 +134,6 @@
 	str = g_string_sized_new(256);
 
 	pos_start = sci_get_position_from_line(doc->editor->sci, line_number);
-	/* TODO use SCI_GETLINEENDPOSITION */
 	pos_end = sci_get_position_from_line(doc->editor->sci, line_number + 1);
 
 	while (pos_start < pos_end)

Modified: trunk/spellcheck/src/speller.h
===================================================================
--- trunk/spellcheck/src/speller.h	2009-02-15 22:55:55 UTC (rev 482)
+++ trunk/spellcheck/src/speller.h	2009-02-17 13:58:58 UTC (rev 483)
@@ -27,7 +27,8 @@
 #define SC_SPELLER_H 1
 
 
-gint speller_process_line(GeanyDocument *doc, gint line_number, const gchar *line);
+gint speller_check_word(GeanyDocument *doc, gint line_number, const gchar *word,
+					    gint start_pos, gint end_pos);
 
 void speller_check_document(GeanyDocument *doc);
 


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