aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2022-04-30 12:46:40 +0200
committerLars Ingebrigtsen2022-04-30 12:57:20 +0200
commitaab5d7b3f3bb6fb82924aaabdfdd6e2a79ad3141 (patch)
tree2bf60263ec953c17dee664ea762d70b8c827effb
parent57447f5ce0a723f698d1515485860ca17ce93960 (diff)
downloademacs-aab5d7b3f3bb6fb82924aaabdfdd6e2a79ad3141.tar.gz
emacs-aab5d7b3f3bb6fb82924aaabdfdd6e2a79ad3141.zip
Add a KEEP-NEWLINES argument to string-lines
* doc/lispref/strings.texi (Creating Strings): Document it. * lisp/subr.el (string-lines): Add a KEEP-NEWLINES argument.
-rw-r--r--doc/lispref/strings.texi6
-rw-r--r--lisp/subr.el32
-rw-r--r--test/lisp/subr-tests.el22
3 files changed, 55 insertions, 5 deletions
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index d31807ad2aa..6f620c9d769 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -434,9 +434,11 @@ display purposes; use @code{truncate-string-to-width} or
434(@pxref{Size of Displayed Text}). 434(@pxref{Size of Displayed Text}).
435@end defun 435@end defun
436 436
437@defun string-lines string &optional omit-nulls 437@defun string-lines string &optional omit-nulls keep-newlines
438Split @var{string} into a list of strings on newline boundaries. If 438Split @var{string} into a list of strings on newline boundaries. If
439@var{omit-nulls}, remove empty lines from the results. 439@var{omit-nulls}, remove empty lines from the results. if
440@var{keep-newlines}, don't remove the trailing newlines from the
441result strings.
440@end defun 442@end defun
441 443
442@defun string-pad string length &optional padding start 444@defun string-pad string length &optional padding start
diff --git a/lisp/subr.el b/lisp/subr.el
index 9623ea63b55..14cab04d429 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -6646,10 +6646,36 @@ is inserted before adjusting the number of empty lines."
6646 ((< (- (point) start) lines) 6646 ((< (- (point) start) lines)
6647 (insert (make-string (- lines (- (point) start)) ?\n)))))) 6647 (insert (make-string (- lines (- (point) start)) ?\n))))))
6648 6648
6649(defun string-lines (string &optional omit-nulls) 6649(defun string-lines (string &optional omit-nulls keep-newlines)
6650 "Split STRING into a list of lines. 6650 "Split STRING into a list of lines.
6651If OMIT-NULLS, empty lines will be removed from the results." 6651If OMIT-NULLS, empty lines will be removed from the results.
6652 (split-string string "\n" omit-nulls)) 6652If KEEP-NEWLINES, don't strip trailing newlines from the result
6653lines."
6654 (let ((lines nil)
6655 (start 0))
6656 (while (< start (length string))
6657 (if-let ((newline (string-search "\n" string start)))
6658 (progn
6659 (when (or (not omit-nulls)
6660 (not (= start newline)))
6661 (let ((line (substring string start
6662 (if keep-newlines
6663 (1+ newline)
6664 newline))))
6665 (when (not (and keep-newlines omit-nulls
6666 (equal line "\n")))
6667 (push line lines))))
6668 (setq start (1+ newline))
6669 ;; Include the final newline.
6670 (when (and (= start (length string))
6671 (not omit-nulls)
6672 (not keep-newlines))
6673 (push "" lines)))
6674 (if (zerop start)
6675 (push string lines)
6676 (push (substring string start) lines))
6677 (setq start (length string))))
6678 (nreverse lines)))
6653 6679
6654(defun buffer-match-p (condition buffer-or-name &optional arg) 6680(defun buffer-match-p (condition buffer-or-name &optional arg)
6655 "Return non-nil if BUFFER-OR-NAME matches CONDITION. 6681 "Return non-nil if BUFFER-OR-NAME matches CONDITION.
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index e027c68d0b2..c431930c272 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -1028,5 +1028,27 @@ final or penultimate step during initialization."))
1028 (should (readablep "foo")) 1028 (should (readablep "foo"))
1029 (should-not (readablep (list (make-marker))))) 1029 (should-not (readablep (list (make-marker)))))
1030 1030
1031(ert-deftest test-string-lines ()
1032 (should (equal (string-lines "foo") '("foo")))
1033 (should (equal (string-lines "foo\n") '("foo" "")))
1034 (should (equal (string-lines "foo\nbar") '("foo" "bar")))
1035
1036 (should (equal (string-lines "foo" t) '("foo")))
1037 (should (equal (string-lines "foo\n" t) '("foo")))
1038 (should (equal (string-lines "foo\nbar" t) '("foo" "bar")))
1039 (should (equal (string-lines "foo\n\n\nbar" t) '("foo" "bar")))
1040
1041 (should (equal (string-lines "foo" nil t) '("foo")))
1042 (should (equal (string-lines "foo\n" nil t) '("foo\n")))
1043 (should (equal (string-lines "foo\nbar" nil t) '("foo\n" "bar")))
1044 (should (equal (string-lines "foo\n\n\nbar" nil t)
1045 '("foo\n" "\n" "\n" "bar")))
1046
1047 (should (equal (string-lines "foo" t t) '("foo")))
1048 (should (equal (string-lines "foo\n" t t) '("foo\n")))
1049 (should (equal (string-lines "foo\nbar" t t) '("foo\n" "bar")))
1050 (should (equal (string-lines "foo\n\n\nbar" t t)
1051 '("foo\n" "bar"))))
1052
1031(provide 'subr-tests) 1053(provide 'subr-tests)
1032;;; subr-tests.el ends here 1054;;; subr-tests.el ends here