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.

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@5114 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Enrico Tröger 2010-08-01 17:20:50 +00:00
parent a18f00ae62
commit befcb63e59
10 changed files with 84 additions and 15 deletions

View File

@ -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>

View File

@ -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 \

View File

@ -710,8 +710,7 @@ GeanyDocument *document_new_file_if_non_open(void)
*
* @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 @@ GeanyDocument *document_new_file(const gchar *utf8_filename, GeanyFiletype *ft,
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);

View File

@ -4272,6 +4272,26 @@ void editor_insert_color(GeanyEditor *editor, const gchar *colour)
}
/**
* 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.
@ -4335,12 +4355,7 @@ const gchar *editor_get_eol_char(GeanyEditor *editor)
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);
}

View File

@ -258,6 +258,8 @@ void editor_indicator_set_on_range(GeanyEditor *editor, gint indic, gint start,
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);

View File

@ -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 @@ typedef struct EditorFuncs
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;

View File

@ -122,7 +122,8 @@ static EditorFuncs editor_funcs = {
&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 = {

View File

@ -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 gchar *replace_all(gchar *text, const gchar *year, const gchar *date, con
}
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 @@ gchar *templates_get_template_licence(GeanyDocument *doc, gint licence_type)
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 @@ gchar *templates_get_template_fileheader(gint filetype_idx, const gchar *fname)
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 @@ gchar *templates_get_template_new_file(GeanyFiletype *ft)
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 @@ gchar *templates_get_template_function(GeanyDocument *doc, const gchar *func_nam
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 @@ gchar *templates_get_template_changelog(GeanyDocument *doc)
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);
}

View File

@ -356,6 +356,21 @@ const gchar *utils_get_eol_char(gint eol_mode)
}
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))

View File

@ -127,6 +127,8 @@ gint utils_write_file(const gchar *filename, const gchar *text);
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);