summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/lispref/strings.texi4
-rw-r--r--etc/NEWS2
-rw-r--r--lisp/emacs-lisp/shortdoc.el2
-rw-r--r--lisp/emacs-lisp/subr-x.el4
-rw-r--r--test/lisp/emacs-lisp/subr-x-tests.el5
5 files changed, 16 insertions, 1 deletions
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index c65d839a028..17cc1a47124 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -428,6 +428,10 @@ string, and if it's negative, to the start of the string (using the
absolute value).
@end defun
+@defun string-chop-newline string
+Remove the final newline, if any, from @var{string}.
+@end defun
+
@node Modifying Strings
@section Modifying Strings
@cindex modifying strings
diff --git a/etc/NEWS b/etc/NEWS
index a6774be8f73..46b8435a14d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1443,7 +1443,7 @@ that makes it a valid button.
+++
*** A number of new string manipulation functions have been added.
'string-clean-whitespace', 'string-fill', 'string-limit',
-'string-lines', 'string-pad' and 'string-slice'.
+'string-lines', 'string-pad', 'string-chop-newline' and 'string-slice'.
+++
*** New variable 'current-minibuffer-command'.
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el
index df31b0aaf1f..9bd06636f4d 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -181,6 +181,8 @@ There can be any number of :example/:result elements."
(string-remove-prefix
:no-manual t
:eval (string-remove-prefix "foo" "foobar"))
+ (string-chop-newline
+ :eval (string-chop-newline "foo\n"))
(string-clean-whitespace
:eval (string-clean-whitespace " foo bar "))
(string-fill
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 78d0b054b35..80d4cb9b650 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -337,6 +337,10 @@ string."
(and (> length 0)
(make-string pad-length (or padding ?\s)))))))
+(defun string-chop-newline (string)
+ "Remove the final newline (if any) from STRING."
+ (replace-regexp-in-string "\n\\'" "" string))
+
(defun replace-region-contents (beg end replace-fn
&optional max-secs max-costs)
"Replace the region between BEG and END using REPLACE-FN.
diff --git a/test/lisp/emacs-lisp/subr-x-tests.el b/test/lisp/emacs-lisp/subr-x-tests.el
index c655fcf6ead..ab5a5bfa641 100644
--- a/test/lisp/emacs-lisp/subr-x-tests.el
+++ b/test/lisp/emacs-lisp/subr-x-tests.el
@@ -615,5 +615,10 @@
(should (equal (string-pad "foo" -5 ?-) "--foo"))
(should (equal (string-pad "foo" 2 ?-) "foo")))
+(ert-deftest subr-string-chop-newline ()
+ (should (equal (string-chop-newline "foo\n") "foo"))
+ (should (equal (string-chop-newline "foo\nbar\n") "foo\nbar"))
+ (should (equal (string-chop-newline "foo\nbar") "foo\nbar")))
+
(provide 'subr-x-tests)
;;; subr-x-tests.el ends here