Skip to content

Commit

Permalink
Markdown: Update using known GeanyDocument when available
Browse files Browse the repository at this point in the history
For whatever reason, in some cases `document_get_current()` doesn't
return a valid document when it seems like it should, so when updating
the markdown preview from signals where the related GeanyDocument
is available, use that instead of calling `document_get_current()`.

In other cases, continue to use `document_get_current()` as before.

Closes geany#1062
  • Loading branch information
codebrainz committed Feb 9, 2021
1 parent 7c540bf commit 9118a56
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions markdown/src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static GtkWidget *g_scrolled_win = NULL;
static GtkWidget *g_export_html = NULL;

/* Forward declarations */
static void update_markdown_viewer(MarkdownViewer *viewer);
static void update_markdown_viewer(MarkdownViewer *viewer, GeanyDocument *doc);
static gboolean on_editor_notify(GObject *obj, GeanyEditor *editor, SCNotification *notif, MarkdownViewer *viewer);
static void on_document_signal(GObject *obj, GeanyDocument *doc, MarkdownViewer *viewer);
static void on_document_filetype_set(GObject *obj, GeanyDocument *doc, GeanyFiletype *ft_old, MarkdownViewer *viewer);
Expand Down Expand Up @@ -120,7 +120,7 @@ void plugin_init(GeanyData *data)
* and when reloaded tries to re-register the GTypes. */
plugin_module_make_resident(geany_plugin);

update_markdown_viewer(MARKDOWN_VIEWER(viewer));
update_markdown_viewer(MARKDOWN_VIEWER(viewer), NULL);
}

/* Cleanup resources on plugin unload. */
Expand Down Expand Up @@ -162,9 +162,10 @@ void plugin_help(void)
* editor's text contents change and not on other editor events.
*/
static void
update_markdown_viewer(MarkdownViewer *viewer)
update_markdown_viewer(MarkdownViewer *viewer, GeanyDocument *doc)
{
GeanyDocument *doc = document_get_current();
if (!DOC_VALID(doc))
doc = document_get_current();

if (DOC_VALID(doc) && g_strcmp0(doc->file_type->name, "Markdown") == 0) {
gchar *text;
Expand Down Expand Up @@ -192,7 +193,7 @@ static gboolean on_editor_notify(GObject *obj, GeanyEditor *editor,
SCNotification *notif, MarkdownViewer *viewer)
{
if (IS_MOD_NOTIF(notif)) {
update_markdown_viewer(viewer);
update_markdown_viewer(viewer, NULL);
}
return FALSE; /* Allow others to handle this event too */
}
Expand All @@ -201,14 +202,14 @@ static gboolean on_editor_notify(GObject *obj, GeanyEditor *editor,
* activate, etc.) */
static void on_document_signal(GObject *obj, GeanyDocument *doc, MarkdownViewer *viewer)
{
update_markdown_viewer(viewer);
update_markdown_viewer(viewer, doc);
}

/* Queue update of the markdown preview when a document's filetype is set */
static void on_document_filetype_set(GObject *obj, GeanyDocument *doc, GeanyFiletype *ft_old,
MarkdownViewer *viewer)
{
update_markdown_viewer(viewer);
update_markdown_viewer(viewer, doc);
}

/* Move the MarkdownViewer to the correct notebook when the view position
Expand Down Expand Up @@ -248,7 +249,7 @@ on_view_pos_notify(GObject *obj, GParamSpec *pspec, MarkdownViewer *viewer)

g_object_unref(g_scrolled_win); /* The new notebook owns it now */

update_markdown_viewer(viewer);
update_markdown_viewer(viewer, NULL);
}

static gchar *replace_extension(const gchar *utf8_fn, const gchar *new_ext)
Expand Down

0 comments on commit 9118a56

Please sign in to comment.