SF.net SVN: geany:[5390] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Wed Nov 10 13:13:29 UTC 2010


Revision: 5390
          http://geany.svn.sourceforge.net/geany/?rev=5390&view=rev
Author:   ntrel
Date:     2010-11-10 13:13:28 +0000 (Wed, 10 Nov 2010)

Log Message:
-----------
Use LF line endings for templates internally instead of default
pref because the default can change. This fixes missing line
endings sometimes when changing default setting.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/document.c
    trunk/src/templates.c

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2010-11-09 18:17:57 UTC (rev 5389)
+++ trunk/ChangeLog	2010-11-10 13:13:28 UTC (rev 5390)
@@ -1,3 +1,11 @@
+2010-11-10  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
+
+ * src/templates.c, src/document.c:
+   Use LF line endings for templates internally instead of default
+   pref because the default can change. This fixes missing line
+   endings sometimes when changing default setting.
+
+
 2010-11-09  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
 
  * src/templates.c, src/filetypes.c:

Modified: trunk/src/document.c
===================================================================
--- trunk/src/document.c	2010-11-09 18:17:57 UTC (rev 5389)
+++ trunk/src/document.c	2010-11-10 13:13:28 UTC (rev 5390)
@@ -719,6 +719,7 @@
 
 /**
  *  Creates a new document.
+ *  Line endings in @a text will be converted to the default setting.
  *  Afterwards, the @c "document-new" signal is emitted for plugins.
  *
  *  @param utf8_filename The file name in UTF-8 encoding, or @c NULL to open a file as "untitled".

Modified: trunk/src/templates.c
===================================================================
--- trunk/src/templates.c	2010-11-09 18:17:57 UTC (rev 5389)
+++ trunk/src/templates.c	2010-11-10 13:13:28 UTC (rev 5390)
@@ -69,10 +69,24 @@
 	g_strconcat(app->configdir, \
 		G_DIR_SEPARATOR_S GEANY_TEMPLATES_SUBDIR G_DIR_SEPARATOR_S, shortname, NULL)
 
-#define TEMPLATES_READ_FILE(fname, contents_ptr) \
-	g_file_get_contents(fname, contents_ptr, NULL, NULL);
 
+static gchar *read_file(const gchar *locale_fname)
+{
+	gchar *contents;
+	GString *str;
 
+	if (!g_file_get_contents(locale_fname, &contents, NULL, NULL))
+		return NULL;
+
+	str = g_string_new(contents);
+	g_free(contents);
+
+	/* convert to LF endings for consistency in mixing templates */
+	utils_ensure_same_eol_characters(str, SC_EOL_LF);
+	return g_string_free(str, FALSE);
+}
+
+
 static void read_template(const gchar *name, gint id)
 {
 	gchar *fname = TEMPLATES_GET_FILENAME(name);
@@ -82,17 +96,7 @@
 		setptr(fname, g_strconcat(app->datadir,
 			G_DIR_SEPARATOR_S GEANY_TEMPLATES_SUBDIR G_DIR_SEPARATOR_S, name, NULL));
 
-	TEMPLATES_READ_FILE(fname, &templates[id]);
-
-	{
-		GString *tmp = g_string_new(templates[id]);
-
-		/* Convert to default line endings e.g. for file header use in a file template.
-		 * When inserting separately we replace line endings with the document setting. */
-		utils_ensure_same_eol_characters(tmp, file_prefs.default_eol_character);
-		setptr(templates[id], tmp->str);
-		g_string_free(tmp, FALSE);
-	}
+	templates[id] = read_file(fname);
 	g_free(fname);
 }
 
@@ -118,6 +122,7 @@
 }
 
 
+/* called when inserting templates into an existing document */
 static void convert_eol_characters(GString *template, GeanyDocument *doc)
 {
 	gint doc_eol_mode;
@@ -160,7 +165,7 @@
 		gchar *shortname = g_strconcat("filetype.", ext, NULL);
 		gchar *fname = TEMPLATES_GET_FILENAME(shortname);
 
-		TEMPLATES_READ_FILE(fname, &ft_templates[ft_id]);
+		ft_templates[ft_id] = read_file(fname);
 		ft_templates[ft_id] = replace_all(ft_templates[ft_id], year, date, datetime);
 
 		g_free(fname);
@@ -253,14 +258,13 @@
 	gchar *content;
 	GString *template = NULL;
 
-	g_file_get_contents(locale_fname, &content, NULL, NULL);
+	content = read_file(locale_fname);
 
 	if (content != NULL)
 	{
 		gchar *file_header;
 
 		template = g_string_new(content);
-		utils_ensure_same_eol_characters(template, file_prefs.default_eol_character);
 
 		file_header = get_template_fileheader(ft);
 		templates_replace_valist(template,
@@ -301,7 +305,10 @@
 		template = get_template_from_file(path, new_filename, ft);
 	}
 	if (template)
+	{
+		/* line endings will be converted */
 		document_new_file(new_filename, ft, template);
+	}
 	else
 	{
 		setptr(fname, utils_get_utf8_from_locale(fname));
@@ -577,6 +584,7 @@
 }
 
 
+/* old filetype templates - use file templates instead */
 gchar *templates_get_template_new_file(GeanyFiletype *ft)
 {
 	GString *ft_template;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list