[gedit] Add gsettings option to ensure trailing newline
- From: Jesse van den Kieboom <jessevdk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit] Add gsettings option to ensure trailing newline
- Date: Mon, 30 Jan 2012 15:32:57 +0000 (UTC)
commit fe62ab8175747298a5315aaeed53ef0c56e60817
Author: Jesse van den Kieboom <jessevdk gnome org>
Date: Mon Jan 30 16:17:53 2012 +0100
Add gsettings option to ensure trailing newline
data/org.gnome.gedit.gschema.xml.in.in | 5 ++++
gedit/gedit-document-input-stream.c | 35 +++++++++++++++++++++++++--
gedit/gedit-document-input-stream.h | 3 +-
gedit/gedit-document-loader.c | 15 +++++++++++-
gedit/gedit-document-output-stream.c | 41 ++++++++++++++++++++++++++++---
gedit/gedit-document-output-stream.h | 3 +-
gedit/gedit-document-saver.c | 8 +++++-
7 files changed, 99 insertions(+), 11 deletions(-)
---
diff --git a/data/org.gnome.gedit.gschema.xml.in.in b/data/org.gnome.gedit.gschema.xml.in.in
index 49b8d18..2acdbc5 100644
--- a/data/org.gnome.gedit.gschema.xml.in.in
+++ b/data/org.gnome.gedit.gschema.xml.in.in
@@ -141,6 +141,11 @@
<_summary>Enable Search Highlighting</_summary>
<_description>Whether gedit should highlight all the occurrences of the searched text.</_description>
</key>
+ <key name="ensure-trailing-newline" type="b">
+ <default>true</default>
+ <_summary>Ensure Trailing Newline</_summary>
+ <_description>Whether gedit will ensure that documents always end with a trailing newline.</_description>
+ </key>
</schema>
<schema gettext-domain="@GETTEXT_PACKAGE@" id="org.gnome.gedit.preferences.ui" path="/org/gnome/gedit/preferences/ui/">
<key name="toolbar-visible" type="b">
diff --git a/gedit/gedit-document-input-stream.c b/gedit/gedit-document-input-stream.c
index 365ffb2..2b28804 100644
--- a/gedit/gedit-document-input-stream.c
+++ b/gedit/gedit-document-input-stream.c
@@ -47,13 +47,15 @@ struct _GeditDocumentInputStreamPrivate
guint newline_added : 1;
guint is_initialized : 1;
+ guint ensure_trailing_newline : 1;
};
enum
{
PROP_0,
PROP_BUFFER,
- PROP_NEWLINE_TYPE
+ PROP_NEWLINE_TYPE,
+ PROP_ENSURE_TRAILING_NEWLINE
};
static gssize gedit_document_input_stream_read (GInputStream *stream,
@@ -83,6 +85,10 @@ gedit_document_input_stream_set_property (GObject *object,
stream->priv->newline_type = g_value_get_enum (value);
break;
+ case PROP_ENSURE_TRAILING_NEWLINE:
+ stream->priv->ensure_trailing_newline = g_value_get_boolean (value);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -107,6 +113,10 @@ gedit_document_input_stream_get_property (GObject *object,
g_value_set_enum (value, stream->priv->newline_type);
break;
+ case PROP_ENSURE_TRAILING_NEWLINE:
+ g_value_set_boolean (value, stream->priv->ensure_trailing_newline);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -153,6 +163,22 @@ gedit_document_input_stream_class_init (GeditDocumentInputStreamClass *klass)
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_CONSTRUCT_ONLY));
+
+ /**
+ *
+ * The :ensure-trailing-newline property specifies whether or not to
+ * ensure (enforce) the document ends with a trailing newline.
+ */
+ g_object_class_install_property (gobject_class,
+ PROP_ENSURE_TRAILING_NEWLINE,
+ g_param_spec_boolean ("ensure-trailing-newline",
+ "Ensure Trailing Newline",
+ "Ensure the document ends with a trailing newline",
+ TRUE,
+ G_PARAM_READWRITE |
+ G_PARAM_STATIC_NAME |
+ G_PARAM_STATIC_BLURB |
+ G_PARAM_CONSTRUCT_ONLY));
}
static void
@@ -198,7 +224,8 @@ get_new_line_size (GeditDocumentInputStream *stream)
*/
GInputStream *
gedit_document_input_stream_new (GtkTextBuffer *buffer,
- GeditDocumentNewlineType type)
+ GeditDocumentNewlineType type,
+ gboolean ensure_trailing_newline)
{
GeditDocumentInputStream *stream;
@@ -207,6 +234,7 @@ gedit_document_input_stream_new (GtkTextBuffer *buffer,
stream = g_object_new (GEDIT_TYPE_DOCUMENT_INPUT_STREAM,
"buffer", buffer,
"newline-type", type,
+ "ensure-trailing-newline", ensure_trailing_newline,
NULL);
return G_INPUT_STREAM (stream);
@@ -438,7 +466,8 @@ gedit_document_input_stream_read (GInputStream *stream,
dstream->priv->pos);
if (gtk_text_iter_is_end (&iter) &&
- !gtk_text_iter_is_start (&iter))
+ !gtk_text_iter_is_start (&iter) &&
+ dstream->priv->ensure_trailing_newline)
{
gssize newline_size;
diff --git a/gedit/gedit-document-input-stream.h b/gedit/gedit-document-input-stream.h
index 8652347..d487670 100644
--- a/gedit/gedit-document-input-stream.h
+++ b/gedit/gedit-document-input-stream.h
@@ -57,7 +57,8 @@ struct _GeditDocumentInputStreamClass
GType gedit_document_input_stream_get_type (void) G_GNUC_CONST;
GInputStream *gedit_document_input_stream_new (GtkTextBuffer *buffer,
- GeditDocumentNewlineType type);
+ GeditDocumentNewlineType type,
+ gboolean ensure_trailing_newline);
gsize gedit_document_input_stream_get_total_size (GeditDocumentInputStream *stream);
diff --git a/gedit/gedit-document-loader.c b/gedit/gedit-document-loader.c
index 3d367f0..7d7ad8c 100644
--- a/gedit/gedit-document-loader.c
+++ b/gedit/gedit-document-loader.c
@@ -99,6 +99,7 @@ static void open_async_read (AsyncData *async);
struct _GeditDocumentLoaderPrivate
{
GSettings *enc_settings;
+ GSettings *editor_settings;
GeditDocument *document;
gboolean used;
@@ -218,6 +219,12 @@ gedit_document_loader_dispose (GObject *object)
g_clear_object (&priv->location);
g_clear_object (&priv->enc_settings);
+ if (priv->editor_settings != NULL)
+ {
+ g_object_unref (priv->editor_settings);
+ priv->editor_settings = NULL;
+ }
+
G_OBJECT_CLASS (gedit_document_loader_parent_class)->dispose (object);
}
@@ -311,6 +318,7 @@ gedit_document_loader_init (GeditDocumentLoader *loader)
loader->priv = GEDIT_DOCUMENT_LOADER_GET_PRIVATE (loader);
loader->priv->enc_settings = g_settings_new ("org.gnome.gedit.preferences.encodings");
+ loader->priv->editor_settings = g_settings_new ("org.gnome.gedit.preferences.editor");
}
GeditDocumentLoader *
@@ -677,6 +685,7 @@ start_stream_read (AsyncData *async)
GeditDocumentLoader *loader;
GInputStream *base_stream = NULL;
GFileInfo *info;
+ gboolean ensure_trailing_newline;
loader = async->loader;
info = loader->priv->info;
@@ -715,9 +724,13 @@ start_stream_read (AsyncData *async)
candidate_encodings = g_slist_prepend (NULL, (gpointer)loader->priv->encoding);
}
+ ensure_trailing_newline = g_settings_get_boolean (loader->priv->editor_settings,
+ "ensure-trailing-newline");
+
/* Output stream */
loader->priv->output = gedit_document_output_stream_new (loader->priv->document,
- candidate_encodings);
+ candidate_encodings,
+ ensure_trailing_newline);
g_slist_free (candidate_encodings);
diff --git a/gedit/gedit-document-output-stream.c b/gedit/gedit-document-output-stream.c
index 0e4c984..0143e83 100644
--- a/gedit/gedit-document-output-stream.c
+++ b/gedit/gedit-document-output-stream.c
@@ -75,12 +75,15 @@ struct _GeditDocumentOutputStreamPrivate
guint is_initialized : 1;
guint is_closed : 1;
+
+ guint ensure_trailing_newline : 1;
};
enum
{
PROP_0,
- PROP_DOCUMENT
+ PROP_DOCUMENT,
+ PROP_ENSURE_TRAILING_NEWLINE
};
G_DEFINE_TYPE (GeditDocumentOutputStream, gedit_document_output_stream, G_TYPE_OUTPUT_STREAM)
@@ -113,6 +116,10 @@ gedit_document_output_stream_set_property (GObject *object,
stream->priv->doc = GEDIT_DOCUMENT (g_value_get_object (value));
break;
+ case PROP_ENSURE_TRAILING_NEWLINE:
+ stream->priv->ensure_trailing_newline = g_value_get_boolean (value);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -133,6 +140,10 @@ gedit_document_output_stream_get_property (GObject *object,
g_value_set_object (value, stream->priv->doc);
break;
+ case PROP_ENSURE_TRAILING_NEWLINE:
+ g_value_set_boolean (value, stream->priv->ensure_trailing_newline);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -210,6 +221,22 @@ gedit_document_output_stream_class_init (GeditDocumentOutputStreamClass *klass)
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
+ /**
+ *
+ * The :ensure-trailing-newline property specifies whether or not to
+ * ensure (enforce) the document ends with a trailing newline.
+ */
+ g_object_class_install_property (object_class,
+ PROP_ENSURE_TRAILING_NEWLINE,
+ g_param_spec_boolean ("ensure-trailing-newline",
+ "Ensure Trailing Newline",
+ "Ensure the document ends with a trailing newline",
+ TRUE,
+ G_PARAM_READWRITE |
+ G_PARAM_STATIC_NAME |
+ G_PARAM_STATIC_BLURB |
+ G_PARAM_CONSTRUCT_ONLY));
+
g_type_class_add_private (object_class, sizeof (GeditDocumentOutputStreamPrivate));
}
@@ -450,12 +477,15 @@ get_newline_type (GtkTextIter *end)
GOutputStream *
gedit_document_output_stream_new (GeditDocument *doc,
- GSList *candidate_encodings)
+ GSList *candidate_encodings,
+ gboolean ensure_trailing_newline)
{
GeditDocumentOutputStream *stream;
stream = g_object_new (GEDIT_TYPE_DOCUMENT_OUTPUT_STREAM,
- "document", doc, NULL);
+ "document", doc,
+ "ensure-trailing-newline", ensure_trailing_newline,
+ NULL);
stream->priv->encodings = g_slist_copy (candidate_encodings);
@@ -668,7 +698,10 @@ remove_ending_newline (GeditDocumentOutputStream *stream)
static void
end_append_text_to_document (GeditDocumentOutputStream *stream)
{
- remove_ending_newline (stream);
+ if (stream->priv->ensure_trailing_newline)
+ {
+ remove_ending_newline (stream);
+ }
gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (stream->priv->doc),
FALSE);
diff --git a/gedit/gedit-document-output-stream.h b/gedit/gedit-document-output-stream.h
index eadd4c3..6f32531 100644
--- a/gedit/gedit-document-output-stream.h
+++ b/gedit/gedit-document-output-stream.h
@@ -57,7 +57,8 @@ struct _GeditDocumentOutputStreamClass
GType gedit_document_output_stream_get_type (void) G_GNUC_CONST;
GOutputStream *gedit_document_output_stream_new (GeditDocument *doc,
- GSList *candidate_encodings);
+ GSList *candidate_encodings,
+ gboolean ensure_trailing_newline);
GeditDocumentNewlineType gedit_document_output_stream_detect_newline_type (GeditDocumentOutputStream *stream);
diff --git a/gedit/gedit-document-saver.c b/gedit/gedit-document-saver.c
index d97b985..20c3298 100644
--- a/gedit/gedit-document-saver.c
+++ b/gedit/gedit-document-saver.c
@@ -691,6 +691,7 @@ async_replace_ready_callback (GFile *source,
GOutputStream *base_stream;
gchar *content_type;
GError *error = NULL;
+ gboolean ensure_trailing_newline;
gedit_debug (DEBUG_SAVER);
@@ -757,8 +758,13 @@ async_replace_ready_callback (GFile *source,
saver->priv->stream = G_OUTPUT_STREAM (base_stream);
}
+ ensure_trailing_newline = g_settings_get_boolean (saver->priv->editor_settings,
+ "ensure-trailing-newline");
+
+
saver->priv->input = gedit_document_input_stream_new (GTK_TEXT_BUFFER (saver->priv->document),
- saver->priv->newline_type);
+ saver->priv->newline_type,
+ ensure_trailing_newline);
saver->priv->size = gedit_document_input_stream_get_total_size (GEDIT_DOCUMENT_INPUT_STREAM (saver->priv->input));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]