diff --git a/moo/medit-app/data/test-lua/testlineends.lua b/moo/medit-app/data/test-lua/testlineends.lua new file mode 100644 index 00000000..218d2589 --- /dev/null +++ b/moo/medit-app/data/test-lua/testlineends.lua @@ -0,0 +1,120 @@ +require("munit") +require("medit") +require("gtk") +require("moo.os") + +editor = medit.Editor.instance() + +tassert(medit.LE_NATIVE ~= nil) +tassert(medit.LE_UNIX ~= nil) +tassert(medit.LE_WIN32 ~= nil) +tassert(moo.os.name == 'nt' or medit.LE_NATIVE ~= medit.LE_WIN32) +tassert(moo.os.name ~= 'nt' or medit.LE_NATIVE == medit.LE_WIN32) + +text_unix = 'line1\nline2\nline3\n' +text_win32 = 'line1\r\nline2\r\nline3\r\n' +text_mix = 'line1\nline2\r\nline3\r\n' +if moo.os.name == 'nt' then + text_native = text_win32 +else + text_native = text_unix +end + +function read_file(filename) + local f = assert(io.open(filename, 'rb')) + local t = f:read("*all") + f:close() + return t +end + +function save_file(filename, content) + local f = assert(io.open(filename, 'wb')) + f:write(content) + f:close() +end + +function test_default() + doc = editor.new_doc() + filename = medit.tempnam() + tassert(doc.get_line_end_type() == medit.LE_NATIVE) + doc.set_text(text_unix) + tassert(doc.save_as(medit.SaveInfo.new_path(filename))) + tassert(read_file(filename) == text_native) + tassert(doc.get_line_end_type() == medit.LE_NATIVE) + doc.set_text(text_win32) + tassert(doc.save_as(medit.SaveInfo.new_path(filename))) + tassert(read_file(filename) == text_native) + tassert(doc.get_line_end_type() == medit.LE_NATIVE) + doc.set_modified(false) + editor.close_doc(doc) +end + +function test_set() + doc = editor.new_doc() + filename = medit.tempnam() + + tassert(doc.get_line_end_type() == medit.LE_NATIVE) + doc.set_text(text_unix) + tassert(doc.save_as(medit.SaveInfo.new_path(filename))) + tassert(read_file(filename) == text_native) + tassert(doc.get_line_end_type() == medit.LE_NATIVE) + + doc.set_line_end_type(medit.LE_UNIX) + tassert(doc.get_line_end_type() == medit.LE_UNIX) + tassert(doc.save_as(medit.SaveInfo.new_path(filename))) + tassert(read_file(filename) == text_unix) + tassert(doc.get_line_end_type() == medit.LE_UNIX) + + doc.set_line_end_type(medit.LE_WIN32) + tassert(doc.get_line_end_type() == medit.LE_WIN32) + tassert(doc.save_as(medit.SaveInfo.new_path(filename))) + tassert(read_file(filename) == text_win32) + tassert(doc.get_line_end_type() == medit.LE_WIN32) + + doc.set_modified(false) + editor.close_doc(doc) +end + +function test_load() + filename = medit.tempnam() + + save_file(filename, text_unix) + doc = editor.open_file(medit.OpenInfo.new_path(filename)) + tassert(doc.get_filename() == filename) + tassert(doc.get_line_end_type() == medit.LE_UNIX) + doc.close() + + save_file(filename, text_win32) + doc = editor.open_file(medit.OpenInfo.new_path(filename)) + tassert(doc.get_filename() == filename) + tassert(doc.get_line_end_type() == medit.LE_WIN32) + doc.close() +end + +function test_mix() + filename = medit.tempnam() + + save_file(filename, text_mix) + doc = editor.open_file(medit.OpenInfo.new_path(filename)) + tassert(doc.get_filename() == filename) + tassert(doc.get_line_end_type() == medit.LE_NATIVE) + + tassert(doc.save()) + tassert(doc.get_line_end_type() == medit.LE_NATIVE) + tassert(read_file(filename) == text_unix) + + save_file(filename, text_mix) + tassert(doc.reload()) + tassert(doc.get_line_end_type() == medit.LE_NATIVE) + doc.set_line_end_type(medit.LE_WIN32) + tassert(doc.save()) + tassert(doc.get_line_end_type() == medit.LE_WIN32) + tassert(read_file(filename) == text_win32) + + doc.close() +end + +-- test_default() +-- test_set() +-- test_load() +test_mix() diff --git a/moo/mooedit/mooedit-enums.h b/moo/mooedit/mooedit-enums.h index 1db3ffb4..1d231af7 100644 --- a/moo/mooedit/mooedit-enums.h +++ b/moo/mooedit/mooedit-enums.h @@ -41,27 +41,26 @@ typedef enum { MOO_EDIT_STATUS_CLEAN = 1 << 4 } MooEditStatus; +#ifdef __WIN32__ +#define MOO_LE_NATIVE_VALUE MOO_LE_WIN32 +#else +#define MOO_LE_NATIVE_VALUE MOO_LE_UNIX +#endif + /** * enum:MooLineEndType * - * @MOO_LE_NONE: * @MOO_LE_UNIX: * @MOO_LE_WIN32: * @MOO_LE_MAC: - * @MOO_LE_MIX: - * @MOO_LE_DEFAULT: + * @MOO_LE_NATIVE: **/ typedef enum { MOO_LE_NONE, MOO_LE_UNIX, MOO_LE_WIN32, MOO_LE_MAC, - MOO_LE_MIX, -#ifdef __WIN32__ - MOO_LE_DEFAULT = MOO_LE_WIN32 -#else - MOO_LE_DEFAULT = MOO_LE_UNIX -#endif + MOO_LE_NATIVE = MOO_LE_NATIVE_VALUE } MooLineEndType; typedef enum { diff --git a/moo/mooedit/mooedit-fileops.c b/moo/mooedit/mooedit-fileops.c index 1d2e2441..2cb892ce 100644 --- a/moo/mooedit/mooedit-fileops.c +++ b/moo/mooedit/mooedit-fileops.c @@ -222,9 +222,9 @@ set_encoding_error (GError **error) MooLineEndType moo_edit_get_line_end_type (MooEdit *edit) { - g_return_val_if_fail (MOO_IS_EDIT (edit), MOO_LE_DEFAULT); + g_return_val_if_fail (MOO_IS_EDIT (edit), MOO_LE_NATIVE); if (edit->priv->line_end_type == MOO_LE_NONE) - return MOO_LE_DEFAULT; + return MOO_LE_NATIVE; else return edit->priv->line_end_type; } @@ -503,6 +503,7 @@ do_load (MooEdit *edit, GIOStatus status; GtkTextBuffer *buffer; MooLineEndType le = MOO_LE_NONE; + gboolean mixed_le = FALSE; GString *text = NULL; char *line = NULL; LoadResult result = ERROR_FILE; @@ -589,8 +590,8 @@ do_load (MooEdit *edit, if (le_here) { - if (le && le != le_here) - le = MOO_LE_MIX; + if (mixed_le || (le && le != le_here)) + mixed_le = TRUE; else le = le_here; } @@ -623,15 +624,11 @@ do_load (MooEdit *edit, if (text->len) gtk_text_buffer_insert_at_cursor (buffer, text->str, text->len); - switch (edit->priv->line_end_type) - { - case MOO_LE_MIX: - break; - default: - if (le != MOO_LE_NONE) - moo_edit_set_line_end_type_full (edit, le, TRUE); - break; - } + if (mixed_le) + le = MOO_LE_NATIVE; + + if (le != MOO_LE_NONE) + moo_edit_set_line_end_type_full (edit, le, TRUE); g_string_free (text, TRUE); g_clear_error (error); @@ -823,56 +820,30 @@ get_contents_with_fixed_line_end (GtkTextBuffer *buffer, const char *le, guint l static char * get_contents (MooEdit *edit) { - gboolean normalize_le = FALSE; const char *le = "\n"; gsize le_len = 1; - MooLineEndType line_end_type; GtkTextBuffer *buffer; - char *contents; - line_end_type = edit->priv->line_end_type; - if (!line_end_type) - line_end_type = MOO_LE_DEFAULT; - - switch (line_end_type) + switch (moo_edit_get_line_end_type (edit)) { case MOO_LE_UNIX: - normalize_le = TRUE; le = "\n"; le_len = 1; break; case MOO_LE_WIN32: - normalize_le = TRUE; le = "\r\n"; le_len = 2; break; case MOO_LE_MAC: - normalize_le = TRUE; le = "\r"; le_len = 1; break; - - case MOO_LE_MIX: - break; - default: moo_assert_not_reached (); } buffer = moo_edit_get_buffer (edit); - - if (normalize_le) - { - contents = get_contents_with_fixed_line_end (buffer, le, le_len); - } - else - { - GtkTextIter start, end; - gtk_text_buffer_get_bounds (buffer, &start, &end); - contents = gtk_text_buffer_get_text (buffer, &start, &end, TRUE); - } - - return contents; + return get_contents_with_fixed_line_end (buffer, le, le_len); } static gboolean diff --git a/moo/mooedit/mooeditwindow.c b/moo/mooedit/mooeditwindow.c index 1f9ffe6a..031e33fa 100644 --- a/moo/mooedit/mooeditwindow.c +++ b/moo/mooedit/mooeditwindow.c @@ -1389,7 +1389,6 @@ static const char *line_end_menu_items[] = { "LineEndUnix", /* MOO_LE_UNIX */ "LineEndWin32", /* MOO_LE_WIN32 */ "LineEndMac", /* MOO_LE_MAC */ - "LineEndMix" /* MOO_LE_MIX */ }; static void @@ -1451,10 +1450,6 @@ create_doc_line_end_action (MooEditWindow *window) line_end_menu_items[MOO_LE_MAC], _("_Mac (CR)"), NULL, MOO_MENU_ITEM_RADIO, GINT_TO_POINTER (MOO_LE_MAC), NULL); - moo_menu_mgr_append (mgr, NULL, - line_end_menu_items[MOO_LE_MIX], - _("Mi_xed"), NULL, MOO_MENU_ITEM_RADIO, - GINT_TO_POINTER (MOO_LE_MIX), NULL); moo_bind_bool_property (action, "sensitive", window, "has-open-document", FALSE);