[gedit/wip/loader-saver] tab: some file loading post-processing
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/wip/loader-saver] tab: some file loading post-processing
- Date: Mon, 23 Jun 2014 13:32:12 +0000 (UTC)
commit 7221d3a0a2d9fc0cc0e5fcf1ea4faf9c21122d75
Author: Sébastien Wilmet <swilmet gnome org>
Date: Mon Jun 23 15:26:03 2014 +0200
tab: some file loading post-processing
Goto line and save encoding metadata.
gedit/gedit-tab.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 90 insertions(+), 0 deletions(-)
---
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index 4ca395f..265364c 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -23,6 +23,7 @@
#include <config.h>
#endif
+#include <stdlib.h>
#include <glib/gi18n.h>
#include "gedit-tab.h"
@@ -77,6 +78,7 @@ struct _GeditTabPrivate
/* tmp data for loading */
guint load_create : 1;
+ guint user_requested_encoding : 1;
};
G_DEFINE_TYPE_WITH_PRIVATE (GeditTab, gedit_tab, GTK_TYPE_BOX)
@@ -1964,10 +1966,60 @@ progress_cb (goffset size,
}
static void
+goto_line (GeditTab *tab)
+{
+ GeditDocument *doc = gedit_tab_get_document (tab);
+ GtkTextIter iter;
+
+ /* Move the cursor at the requested line if any. */
+ if (tab->priv->tmp_line_pos > 0)
+ {
+ gedit_document_goto_line_offset (doc,
+ tab->priv->tmp_line_pos - 1,
+ MAX (0, tab->priv->tmp_column_pos - 1));
+ return;
+ }
+
+ /* If enabled, move to the position stored in the metadata. */
+ if (g_settings_get_boolean (tab->priv->editor, GEDIT_SETTINGS_RESTORE_CURSOR_POSITION))
+ {
+ gchar *pos;
+ gint offset;
+
+ pos = gedit_document_get_metadata (doc, GEDIT_METADATA_ATTRIBUTE_POSITION);
+
+ offset = pos != NULL ? atoi (pos) : 0;
+ g_free (pos);
+
+ gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc),
+ &iter,
+ MAX (0, offset));
+
+ /* make sure it's a valid position, if the file
+ * changed we may have ended up in the middle of
+ * a utf8 character cluster */
+ if (!gtk_text_iter_is_cursor_position (&iter))
+ {
+ gtk_text_iter_set_line_offset (&iter, 0);
+ }
+ }
+
+ /* Otherwise to the top. */
+ else
+ {
+ gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (doc), &iter);
+ }
+
+ gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (doc), &iter);
+}
+
+static void
load_cb (GtkSourceFileLoader *loader,
GAsyncResult *result,
GeditTab *tab)
{
+ GeditDocument *doc = gedit_tab_get_document (tab);
+ GFile *location = gtk_source_file_loader_get_location (loader);
GError *error = NULL;
gtk_source_file_loader_load_finish (loader, result, &error);
@@ -1978,13 +2030,48 @@ load_cb (GtkSourceFileLoader *loader,
g_error_free (error);
}
+ /* Load was successful. */
+ if (error == NULL ||
+ (error->domain == GTK_SOURCE_FILE_LOADER_ERROR &&
+ error->code == GTK_SOURCE_FILE_LOADER_ERROR_CONVERSION_FALLBACK))
+ {
+ if (tab->priv->user_requested_encoding)
+ {
+ const GtkSourceEncoding *encoding = gtk_source_file_loader_get_encoding (loader);
+ const gchar *charset = gtk_source_encoding_get_charset (encoding);
+
+ gedit_document_set_metadata (doc,
+ GEDIT_METADATA_ATTRIBUTE_ENCODING, charset,
+ NULL);
+ }
+
+ goto_line (tab);
+ }
+
+ /* Special case creating a named new doc. */
+ else if (tab->priv->load_create &&
+ error->domain == G_IO_ERROR &&
+ error->code == G_IO_ERROR_NOT_FOUND &&
+ g_file_has_uri_scheme (location, "file"))
+ {
+ g_error_free (error);
+ error = NULL;
+ }
+
g_clear_object (&tab->priv->loader);
g_clear_object (&tab->priv->cancellable);
+ g_signal_emit_by_name (doc, "loaded", NULL);
+
gedit_tab_set_state (tab, GEDIT_TAB_STATE_NORMAL);
/* Async operation finished. */
g_object_unref (tab);
+
+ if (error != NULL)
+ {
+ g_error_free (error);
+ }
}
void
@@ -2013,12 +2100,15 @@ _gedit_tab_load (GeditTab *tab,
if (encoding != NULL)
{
+ tab->priv->user_requested_encoding = TRUE;
+
GSList *list = g_slist_append (NULL, (gpointer) encoding);
gtk_source_file_loader_set_candidate_encodings (tab->priv->loader, list);
g_slist_free (list);
}
else
{
+ tab->priv->user_requested_encoding = FALSE;
/* TODO */
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]