diff --git a/mousepad/mousepad-file.c b/mousepad/mousepad-file.c index b44acec..37247d7 100644 --- a/mousepad/mousepad-file.c +++ b/mousepad/mousepad-file.c @@ -603,6 +603,11 @@ mousepad_file_open (MousepadFile *file, } } + /* text view doesn't expect a line ending at end of last line, but Unix and Mac files do */ + if ((n = g_utf8_find_prev_char(contents, end)) && (*n == '\r' || + (*n == '\n' && (!(n = g_utf8_find_prev_char(contents, n)) || *n != '\r')))) + end--; + /* get the iter at the beginning of the document */ gtk_text_buffer_get_start_iter (file->buffer, &start_iter); @@ -757,8 +762,8 @@ mousepad_file_save (MousepadFile *file, /* add and utf-8 bom at the start of the contents if needed */ if (file->write_bom && mousepad_encoding_is_unicode (file->encoding)) { - /* realloc the contents string */ - contents = g_realloc (contents, length + 4); + /* realloc the contents string. +1 in case we append extra line ending */ + contents = g_realloc (contents, length + 5); /* move the existing contents 3 bytes */ g_memmove (contents + 3, contents, length + 1); @@ -772,6 +777,18 @@ mousepad_file_save (MousepadFile *file, length += 3; } + /* text view doesn't expect a line ending at end of last line, but Unix and Mac files do */ + if (file->line_ending != MOUSEPAD_EOL_DOS && 0 < length) + { + /* realloc contents. does nothing if realloc above already resized */ + contents = g_realloc (contents, length + 2); + + contents[length] = file->line_ending == MOUSEPAD_EOL_MAC ? '\r' : '\n'; + contents[length + 1] = '\0'; + + length++; + } + /* convert to the encoding if set */ if (G_UNLIKELY (file->encoding != MOUSEPAD_ENCODING_UTF_8)) {