[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master f047d3c513 2/3: Add new function 'readablep'
From: |
Lars Ingebrigtsen |
Subject: |
master f047d3c513 2/3: Add new function 'readablep' |
Date: |
Sat, 22 Jan 2022 09:13:36 -0500 (EST) |
branch: master
commit f047d3c5137e75ea22713e1e7a6f715e6544299a
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>
Add new function 'readablep'
* doc/lispref/streams.texi (Input Functions): Document it.
* lisp/subr.el (readablep): New function (bug#52566).
---
doc/lispref/streams.texi | 7 +++++++
etc/NEWS | 4 ++++
lisp/subr.el | 13 +++++++++++++
test/lisp/subr-tests.el | 4 ++++
4 files changed, 28 insertions(+)
diff --git a/doc/lispref/streams.texi b/doc/lispref/streams.texi
index 5ab6cf5777..b93a7610fa 100644
--- a/doc/lispref/streams.texi
+++ b/doc/lispref/streams.texi
@@ -358,6 +358,13 @@ mode for @var{stream}. On POSIX hosts, it always returns a
non-@code{nil} value and does nothing except flushing pending output.
@end defun
+@defun readablep object
+This predicate says whether @var{object} can be written out and then
+read back by the Emacs Lisp reader. If it can't, this function
+returns @code{nil}, and if it can, a printed representation (via
+@code{prin1}) of @var{object} is returned.
+@end defun
+
@node Output Streams
@section Output Streams
@cindex stream (for printing)
diff --git a/etc/NEWS b/etc/NEWS
index 02e7a462a1..95e53852ce 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -977,6 +977,10 @@ functions.
* Lisp Changes in Emacs 29.1
+** New function 'readablep'.
+This function says whether an object can be written out and then
+read back by the Emacs Lisp reader.
+
+++
** New variable 'print-unreadable-function'.
This variable allows changing how Emacs prints unreadable objects.
diff --git a/lisp/subr.el b/lisp/subr.el
index 81c0233853..29b9b6dfcf 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -6561,4 +6561,17 @@ signalled. If NOERROR, the non-loop parts of the chain
is returned."
(push func chain))
chain))))
+(defun readablep (object)
+ "Say whether OBJECT has a readable syntax.
+This means that OBJECT can be printed out and then read back
+again by the Lisp reader. This function returns nil if OBJECT is
+unreadable, and the printed representation (from `prin1') of
+OBJECT if it is readable."
+ (declare (side-effect-free t))
+ (catch 'unreadable
+ (let ((print-unreadable-function
+ (lambda (_object _escape)
+ (throw 'unreadable nil))))
+ (prin1-to-string object))))
+
;;; subr.el ends here
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index 512b654535..e027c68d0b 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -1024,5 +1024,9 @@ final or penultimate step during initialization."))
(should (equal (function-alias-p 'subr-tests--d t)
'(subr-tests--e))))
+(ert-deftest test-readablep ()
+ (should (readablep "foo"))
+ (should-not (readablep (list (make-marker)))))
+
(provide 'subr-tests)
;;; subr-tests.el ends here