Add new function 'readablep'

* doc/lispref/streams.texi (Input Functions): Document it.
* lisp/subr.el (readablep): New function (bug#52566).
This commit is contained in:
Lars Ingebrigtsen 2022-01-22 15:11:17 +01:00
parent e4d2a7894b
commit f047d3c513
4 changed files with 28 additions and 0 deletions

View file

@ -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)

View file

@ -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.

View file

@ -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

View file

@ -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