From: Lars Ingebrigtsen Date: Sat, 22 Jan 2022 14:11:17 +0000 (+0100) Subject: Add new function 'readablep' X-Git-Tag: emacs-29.0.90~2859 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=f047d3c5137e75ea22713e1e7a6f715e6544299a;p=emacs.git Add new function 'readablep' * doc/lispref/streams.texi (Input Functions): Document it. * lisp/subr.el (readablep): New function (bug#52566). --- diff --git a/doc/lispref/streams.texi b/doc/lispref/streams.texi index 5ab6cf5777b..b93a7610fa0 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 02e7a462a18..95e53852cea 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 81c02338531..29b9b6dfcf5 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 512b6545355..e027c68d0b2 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