Revision: 5114 http://geany.svn.sourceforge.net/geany/?rev=5114&view=rev Author: eht16 Date: 2010-08-01 17:20:50 +0000 (Sun, 01 Aug 2010)
Log Message: ----------- Ensure inserted templates always have proper line ending characters according to the current document's preference. This is also fixes problems with templates on Windows which had always Unix line ending characters but now since they are read from files, these have Windows line ending characters and had been converted twice.
Modified Paths: -------------- trunk/ChangeLog trunk/plugins/geanyfunctions.h trunk/src/document.c trunk/src/editor.c trunk/src/editor.h trunk/src/plugindata.h trunk/src/plugins.c trunk/src/templates.c trunk/src/utils.c trunk/src/utils.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/ChangeLog 2010-08-01 17:20:50 UTC (rev 5114) @@ -2,6 +2,15 @@
* src/utils.c, src/utils.h: Add utils_get_eol_char(). + * plugins/geanyfunctions.h, src/document.c, src/editor.c, src/editor.h, + src/plugindata.h, src/plugins.c, src/templates.c, src/utils.c, + src/utils.h: + Ensure inserted templates always have proper line ending characters + according to the current document's preference. + This is also fixes problems with templates on Windows which had + always Unix line ending characters but now since they are read + from files, these have Windows line ending characters and had been + converted twice.
2010-07-31 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
Modified: trunk/plugins/geanyfunctions.h =================================================================== --- trunk/plugins/geanyfunctions.h 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/plugins/geanyfunctions.h 2010-08-01 17:20:50 UTC (rev 5114) @@ -88,6 +88,8 @@ geany_functions->p_editor->editor_get_eol_char #define editor_insert_text_block \ geany_functions->p_editor->editor_insert_text_block +#define editor_get_eol_char_mode \ + geany_functions->p_editor->editor_get_eol_char_mode #define scintilla_send_message \ geany_functions->p_scintilla->scintilla_send_message #define scintilla_new \
Modified: trunk/src/document.c =================================================================== --- trunk/src/document.c 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/src/document.c 2010-08-01 17:20:50 UTC (rev 5114) @@ -710,8 +710,7 @@ * * @return The new document. **/ -GeanyDocument *document_new_file(const gchar *utf8_filename, GeanyFiletype *ft, - const gchar *text) +GeanyDocument *document_new_file(const gchar *utf8_filename, GeanyFiletype *ft, const gchar *text) { GeanyDocument *doc;
@@ -728,15 +727,17 @@
sci_set_undo_collection(doc->editor->sci, FALSE); /* avoid creation of an undo action */ if (text) - sci_set_text(doc->editor->sci, text); + { + GString *template = g_string_new(text); + utils_ensure_same_eol_characters(template, file_prefs.default_eol_character); + + sci_set_text(doc->editor->sci, template->str); + g_string_free(template, TRUE); + } else sci_clear_all(doc->editor->sci);
sci_set_eol_mode(doc->editor->sci, file_prefs.default_eol_character); - /* convert the eol chars in the template text in case they are different from - * from file_prefs.default_eol */ - if (text != NULL) - sci_convert_eols(doc->editor->sci, file_prefs.default_eol_character);
sci_set_undo_collection(doc->editor->sci, TRUE); sci_empty_undo_buffer(doc->editor->sci);
Modified: trunk/src/editor.c =================================================================== --- trunk/src/editor.c 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/src/editor.c 2010-08-01 17:20:50 UTC (rev 5114) @@ -4273,6 +4273,26 @@
/** + * Retrieves the end of line characters mode (LF, CR/LF, CR) in the given editor. + * If @a editor is @c NULL, the default end of line characters are used. + * + * @param editor The editor to operate on, or @c NULL to query the default value. + * @return The used end of line characters mode. + * + * @since 0.20 + */ +gint editor_get_eol_char_mode(GeanyEditor *editor) +{ + gint mode = file_prefs.default_eol_character; + + if (editor != NULL) + mode = sci_get_eol_mode(editor->sci); + + return mode; +} + + +/** * Retrieves the localized name (for displaying) of the used end of line characters * (LF, CR/LF, CR) in the given editor. * If @a editor is @c NULL, the default end of line characters are used. @@ -4335,12 +4355,7 @@ if (editor != NULL) mode = sci_get_eol_mode(editor->sci);
- switch (mode) - { - case SC_EOL_CRLF: return "\r\n"; break; - case SC_EOL_CR: return "\r"; break; - default: return "\n"; break; - } + return utils_get_eol_char(mode); }
Modified: trunk/src/editor.h =================================================================== --- trunk/src/editor.h 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/src/editor.h 2010-08-01 17:20:50 UTC (rev 5114) @@ -258,6 +258,8 @@
void editor_indicator_clear(GeanyEditor *editor, gint indic);
+gint editor_get_eol_char_mode(GeanyEditor *editor); + const gchar *editor_get_eol_char_name(GeanyEditor *editor);
gint editor_get_eol_char_len(GeanyEditor *editor);
Modified: trunk/src/plugindata.h =================================================================== --- trunk/src/plugindata.h 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/src/plugindata.h 2010-08-01 17:20:50 UTC (rev 5114) @@ -50,7 +50,7 @@ enum { /** The Application Programming Interface (API) version, incremented * whenever any plugin data types are modified or appended to. */ - GEANY_API_VERSION = 190, + GEANY_API_VERSION = 191,
/** The Application Binary Interface (ABI) version, incremented whenever * existing fields in the plugin data types have to be changed or reordered. */ @@ -597,6 +597,8 @@ void (*editor_insert_text_block) (struct GeanyEditor *editor, const gchar *text, gint insert_pos, gint cursor_index, gint newline_indent_size, gboolean replace_newlines); + + gint (*editor_get_eol_char_mode) (struct GeanyEditor *editor); } EditorFuncs;
Modified: trunk/src/plugins.c =================================================================== --- trunk/src/plugins.c 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/src/plugins.c 2010-08-01 17:20:50 UTC (rev 5114) @@ -122,7 +122,8 @@ &editor_get_eol_char_name, &editor_get_eol_char_len, &editor_get_eol_char, - &editor_insert_text_block + &editor_insert_text_block, + &editor_get_eol_char_mode };
static ScintillaFuncs scintilla_funcs = {
Modified: trunk/src/templates.c =================================================================== --- trunk/src/templates.c 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/src/templates.c 2010-08-01 17:20:50 UTC (rev 5114) @@ -35,6 +35,7 @@ #include "support.h" #include "utils.h" #include "document.h" +#include "editor.h" #include "filetypes.h" #include "ui_utils.h" #include "toolbar.h" @@ -120,6 +121,20 @@ }
+static void convert_eol_characters(GString *template, GeanyDocument *doc) +{ + gint doc_eol_mode; + + if (doc == NULL) + doc = document_get_current(); + + g_return_if_fail(doc != NULL); + + doc_eol_mode = editor_get_eol_char_mode(doc->editor); + utils_ensure_same_eol_characters(template, doc_eol_mode); +} + + static void init_general_templates(const gchar *year, const gchar *date, const gchar *datetime) { guint id; @@ -526,6 +541,7 @@ templates_replace_command(template, DOC_FILENAME(doc), doc->file_type->name, NULL);
make_comment_block(template, FILETYPE_ID(doc->file_type), 8); + convert_eol_characters(template, doc);
return g_string_free(template, FALSE); } @@ -557,6 +573,7 @@
g_free(str); templates_replace_common(template, fname, ft, NULL); + convert_eol_characters(template, NULL); return g_string_free(template, FALSE); }
@@ -580,6 +597,7 @@ templates_replace_valist(ft_template, "{fileheader}", file_header, NULL); } templates_replace_common(ft_template, NULL, ft, NULL); + convert_eol_characters(ft_template, NULL);
g_free(file_header); return g_string_free(ft_template, FALSE); @@ -598,6 +616,7 @@ templates_replace_command(text, DOC_FILENAME(doc), doc->file_type->name, func_name);
make_comment_block(text, doc->file_type->id, 3); + convert_eol_characters(text, doc);
return g_string_free(text, FALSE); } @@ -611,6 +630,7 @@ replace_static_values(result); templates_replace_default_dates(result); templates_replace_command(result, DOC_FILENAME(doc), file_type_name, NULL); + convert_eol_characters(result, doc);
return g_string_free(result, FALSE); }
Modified: trunk/src/utils.c =================================================================== --- trunk/src/utils.c 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/src/utils.c 2010-08-01 17:20:50 UTC (rev 5114) @@ -356,6 +356,21 @@ }
+void utils_ensure_same_eol_characters(GString *template, gint target_eol_mode) +{ + gint template_eol_mode; + + template_eol_mode = utils_get_line_endings(template->str, template->len); + + if (target_eol_mode != template_eol_mode) + { + const gchar *target_eol_char = utils_get_eol_char(target_eol_mode); + const gchar *template_eol_char = utils_get_eol_char(template_eol_mode); + utils_string_replace_all(template, template_eol_char, target_eol_char); + } +} + + gboolean utils_atob(const gchar *str) { if (G_UNLIKELY(str == NULL))
Modified: trunk/src/utils.h =================================================================== --- trunk/src/utils.h 2010-08-01 10:23:19 UTC (rev 5113) +++ trunk/src/utils.h 2010-08-01 17:20:50 UTC (rev 5114) @@ -127,6 +127,8 @@
gchar *utils_find_open_xml_tag(const gchar sel[], gint size, gboolean check_tag);
+void utils_ensure_same_eol_characters(GString *template, gint target_eol_mode); + const gchar *utils_get_eol_char(gint eol_mode);
const gchar *utils_get_eol_name(gint eol_mode);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.