From 4f395efa06d88832c376c2b1d4607677436228c0 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sun, 1 May 2022 20:54:11 +0200 Subject: [PATCH] Change string-lines semantics slightly * lisp/subr.el (string-lines): Change the semantics slightly -- don't return an empty string for a trailing newline. --- etc/NEWS | 5 +++++ lisp/subr.el | 7 +------ test/lisp/subr-tests.el | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 3380c266daa..882748d8c7f 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -135,6 +135,11 @@ of 'user-emacs-directory'. * Incompatible changes in Emacs 29.1 +--- +** 'string-lines' handles trailing newlines differently. +It no longer returns an empty final string if the string ends with a +newline. + --- ** 'TAB' and '' are now bound in 'button-map'. This means that if you're standing on a button, 'TAB' will take you to diff --git a/lisp/subr.el b/lisp/subr.el index d4f5d0d23bc..d6ea3092074 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -6762,12 +6762,7 @@ lines." (when (not (and keep-newlines omit-nulls (equal line "\n"))) (push line lines)))) - (setq start (1+ newline)) - ;; Include the final newline. - (when (and (= start (length string)) - (not omit-nulls) - (not keep-newlines)) - (push "" lines))) + (setq start (1+ newline))) ;; No newline in the remaining part. (if (zerop start) ;; Avoid a string copy if there are no newlines at all. diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index c431930c272..93e4475d6bc 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -1030,7 +1030,7 @@ final or penultimate step during initialization.")) (ert-deftest test-string-lines () (should (equal (string-lines "foo") '("foo"))) - (should (equal (string-lines "foo\n") '("foo" ""))) + (should (equal (string-lines "foo\n") '("foo"))) (should (equal (string-lines "foo\nbar") '("foo" "bar"))) (should (equal (string-lines "foo" t) '("foo"))) -- 2.39.2