Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Last active August 29, 2015 14:16
Show Gist options
  • Save codebrainz/95681f9d30937359f6f5 to your computer and use it in GitHub Desktop.
Save codebrainz/95681f9d30937359f6f5 to your computer and use it in GitHub Desktop.
Make "hunting for Scintilla" a little more robust
diff --git a/src/document.c b/src/document.c
index 8a57d93..edaed0d 100644
--- a/src/document.c
+++ b/src/document.c
@@ -226,16 +226,8 @@ GeanyDocument *document_find_by_filename(const gchar *utf8_filename)
/* returns the document which has sci, or NULL. */
GeanyDocument *document_find_by_sci(ScintillaObject *sci)
{
- guint i;
-
- g_return_val_if_fail(sci != NULL, NULL);
-
- for (i = 0; i < documents_array->len; i++)
- {
- if (documents[i]->is_valid && documents[i]->editor->sci == sci)
- return documents[i];
- }
- return NULL;
+ g_return_val_if_fail(IS_SCINTILLA(sci), NULL);
+ return g_object_get_data(G_OBJECT(sci), "geany-document");
}
@@ -305,6 +297,16 @@ gint document_get_notebook_page(GeanyDocument *doc)
}
+/* Check if the object is not only a Scintilla but also has our special
+ * "geany-document" tag set in editor_create_widget(). This prevents finding
+ * other Scintilla widgets that plugin's might have sneakily packed into the
+ * widget tree we search through to find our Scintilla. */
+static inline gboolean is_geany_scintilla(gpointer obj)
+{
+ return IS_SCINTILLA(obj) && (g_object_get_data(obj, "geany-document") != NULL);
+}
+
+
/*
* Recursively searches a containers children until it finds a
* Scintilla widget, or NULL if one was not found.
@@ -319,7 +321,7 @@ static ScintillaObject *locate_sci_in_container(GtkWidget *container)
children = gtk_container_get_children(GTK_CONTAINER(container));
for (iter = children; iter != NULL; iter = g_list_next(iter))
{
- if (IS_SCINTILLA(iter->data))
+ if (is_geany_scintilla(iter->data))
{
sci = SCINTILLA(iter->data);
break;
@@ -327,7 +329,7 @@ static ScintillaObject *locate_sci_in_container(GtkWidget *container)
else if (GTK_IS_CONTAINER(iter->data))
{
sci = locate_sci_in_container(iter->data);
- if (IS_SCINTILLA(sci))
+ if (is_geany_scintilla(sci))
break;
sci = NULL;
}
diff --git a/src/editor.c b/src/editor.c
index fc04ac6..a9c433e 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -4831,6 +4831,9 @@ ScintillaObject *editor_create_widget(GeanyEditor *editor)
editor->indent_width = old_indent_width;
editor->sci = old;
}
+
+ g_object_set_data(G_OBJECT(sci), "geany-document", editor->document);
+
return sci;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment