Removed 'mixed' line end type, it was broken

This commit is contained in:
Yevgen Muntyan 2011-01-22 21:55:40 -08:00
parent d896acf220
commit d80f9b90a9
4 changed files with 140 additions and 55 deletions

View File

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

View File

@ -41,27 +41,26 @@ typedef enum {
MOO_EDIT_STATUS_CLEAN = 1 << 4 MOO_EDIT_STATUS_CLEAN = 1 << 4
} MooEditStatus; } MooEditStatus;
#ifdef __WIN32__
#define MOO_LE_NATIVE_VALUE MOO_LE_WIN32
#else
#define MOO_LE_NATIVE_VALUE MOO_LE_UNIX
#endif
/** /**
* enum:MooLineEndType * enum:MooLineEndType
* *
* @MOO_LE_NONE:
* @MOO_LE_UNIX: * @MOO_LE_UNIX:
* @MOO_LE_WIN32: * @MOO_LE_WIN32:
* @MOO_LE_MAC: * @MOO_LE_MAC:
* @MOO_LE_MIX: * @MOO_LE_NATIVE:
* @MOO_LE_DEFAULT:
**/ **/
typedef enum { typedef enum {
MOO_LE_NONE, MOO_LE_NONE,
MOO_LE_UNIX, MOO_LE_UNIX,
MOO_LE_WIN32, MOO_LE_WIN32,
MOO_LE_MAC, MOO_LE_MAC,
MOO_LE_MIX, MOO_LE_NATIVE = MOO_LE_NATIVE_VALUE
#ifdef __WIN32__
MOO_LE_DEFAULT = MOO_LE_WIN32
#else
MOO_LE_DEFAULT = MOO_LE_UNIX
#endif
} MooLineEndType; } MooLineEndType;
typedef enum { typedef enum {

View File

@ -222,9 +222,9 @@ set_encoding_error (GError **error)
MooLineEndType MooLineEndType
moo_edit_get_line_end_type (MooEdit *edit) 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) if (edit->priv->line_end_type == MOO_LE_NONE)
return MOO_LE_DEFAULT; return MOO_LE_NATIVE;
else else
return edit->priv->line_end_type; return edit->priv->line_end_type;
} }
@ -503,6 +503,7 @@ do_load (MooEdit *edit,
GIOStatus status; GIOStatus status;
GtkTextBuffer *buffer; GtkTextBuffer *buffer;
MooLineEndType le = MOO_LE_NONE; MooLineEndType le = MOO_LE_NONE;
gboolean mixed_le = FALSE;
GString *text = NULL; GString *text = NULL;
char *line = NULL; char *line = NULL;
LoadResult result = ERROR_FILE; LoadResult result = ERROR_FILE;
@ -589,8 +590,8 @@ do_load (MooEdit *edit,
if (le_here) if (le_here)
{ {
if (le && le != le_here) if (mixed_le || (le && le != le_here))
le = MOO_LE_MIX; mixed_le = TRUE;
else else
le = le_here; le = le_here;
} }
@ -623,15 +624,11 @@ do_load (MooEdit *edit,
if (text->len) if (text->len)
gtk_text_buffer_insert_at_cursor (buffer, text->str, text->len); gtk_text_buffer_insert_at_cursor (buffer, text->str, text->len);
switch (edit->priv->line_end_type) if (mixed_le)
{ le = MOO_LE_NATIVE;
case MOO_LE_MIX:
break; if (le != MOO_LE_NONE)
default: moo_edit_set_line_end_type_full (edit, le, TRUE);
if (le != MOO_LE_NONE)
moo_edit_set_line_end_type_full (edit, le, TRUE);
break;
}
g_string_free (text, TRUE); g_string_free (text, TRUE);
g_clear_error (error); g_clear_error (error);
@ -823,56 +820,30 @@ get_contents_with_fixed_line_end (GtkTextBuffer *buffer, const char *le, guint l
static char * static char *
get_contents (MooEdit *edit) get_contents (MooEdit *edit)
{ {
gboolean normalize_le = FALSE;
const char *le = "\n"; const char *le = "\n";
gsize le_len = 1; gsize le_len = 1;
MooLineEndType line_end_type;
GtkTextBuffer *buffer; GtkTextBuffer *buffer;
char *contents;
line_end_type = edit->priv->line_end_type; switch (moo_edit_get_line_end_type (edit))
if (!line_end_type)
line_end_type = MOO_LE_DEFAULT;
switch (line_end_type)
{ {
case MOO_LE_UNIX: case MOO_LE_UNIX:
normalize_le = TRUE;
le = "\n"; le = "\n";
le_len = 1; le_len = 1;
break; break;
case MOO_LE_WIN32: case MOO_LE_WIN32:
normalize_le = TRUE;
le = "\r\n"; le = "\r\n";
le_len = 2; le_len = 2;
break; break;
case MOO_LE_MAC: case MOO_LE_MAC:
normalize_le = TRUE;
le = "\r"; le = "\r";
le_len = 1; le_len = 1;
break; break;
case MOO_LE_MIX:
break;
default: default:
moo_assert_not_reached (); moo_assert_not_reached ();
} }
buffer = moo_edit_get_buffer (edit); buffer = moo_edit_get_buffer (edit);
return get_contents_with_fixed_line_end (buffer, le, le_len);
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;
} }
static gboolean static gboolean

View File

@ -1389,7 +1389,6 @@ static const char *line_end_menu_items[] = {
"LineEndUnix", /* MOO_LE_UNIX */ "LineEndUnix", /* MOO_LE_UNIX */
"LineEndWin32", /* MOO_LE_WIN32 */ "LineEndWin32", /* MOO_LE_WIN32 */
"LineEndMac", /* MOO_LE_MAC */ "LineEndMac", /* MOO_LE_MAC */
"LineEndMix" /* MOO_LE_MIX */
}; };
static void static void
@ -1451,10 +1450,6 @@ create_doc_line_end_action (MooEditWindow *window)
line_end_menu_items[MOO_LE_MAC], line_end_menu_items[MOO_LE_MAC],
_("_Mac (CR)"), NULL, MOO_MENU_ITEM_RADIO, _("_Mac (CR)"), NULL, MOO_MENU_ITEM_RADIO,
GINT_TO_POINTER (MOO_LE_MAC), NULL); 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", moo_bind_bool_property (action, "sensitive",
window, "has-open-document", FALSE); window, "has-open-document", FALSE);