[Xfce4-commits] [apps/mousepad] 01/01: Append a newline char at EOF when saving (Bug #4824)

noreply at xfce.org noreply at xfce.org
Sat Apr 27 10:39:30 CEST 2019


This is an automated email from the git hooks/post-receive script.

l   a   n   d   r   y       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository apps/mousepad.

commit 7e7a4214ce2d54cd9ee4be1c211c4250b4d4cade
Author: Aaron B <aaron_bru at hotmail.com>
Date:   Sat Apr 27 10:31:34 2019 +0200

    Append a newline char at EOF when saving (Bug #4824)
    
    Per POSIX, a regular file must end by a newline char,
    so add it if it isnt present.
    
    Don't add it if we're using the DOS-style newline
    ending setting (No such requirement on Windows).
---
 mousepad/mousepad-file.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/mousepad/mousepad-file.c b/mousepad/mousepad-file.c
index 980d5ce..88aacba 100644
--- a/mousepad/mousepad-file.c
+++ b/mousepad/mousepad-file.c
@@ -604,6 +604,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);
 
@@ -761,8 +766,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);
@@ -776,6 +781,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))
             {

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list