emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: `unreadablep'


From: Steingold
Subject: Re: `unreadablep'
Date: Wed, 15 Dec 2021 11:09:58 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (darwin)

> * Lars Ingebrigtsen <ynefv@tahf.bet> [2021-12-15 08:49:58 +0100]:
>
> Do we have any primitive that can be used to check whether an object is
> printable or not?  Code like this (from savehist.el) makes me believe
> "not":
>
>               ;; Print elements of VALUE one by one, carefully.
>               (dolist (elt value)
>                 (let ((start (point)))
>                   (insert " ")
>                   ;; Try to print and then to read an element.
>                   (condition-case nil
>                       (progn
>                         (prin1 elt (current-buffer))
>                         (save-excursion
>                           (goto-char start)
>                           (read (current-buffer))))
>                     (error
>                      ;; If writing or reading gave an error, comment it out.
>                      (goto-char start)
>                      (insert "\n")
>                      (while (not (eobp))
>                        (insert ";;; ")
>                        (forward-line 1))
>                      (insert "\n")))
>                   (goto-char (point-max))))

I think the above code would have been perfect, if

1. prin1 were replaced with something like `(write ... :readably t)`
   (http://clhs.lisp.se/Body/f_wr_pr.htm), _and_

2. there were no `read` after `prin1`

(BTW, the return value of `read` is ignored, to be "honest", it should
be compared to `elt` using `equal`)

> It would be nice to have such a function (i.e., that says whether it can
> be read back after printing it).  The problem is, of course, complex
> structures that require recursing (and then checking for loops), etc, so
> you basically have to implement it the way printing is done if you want
> it to be fast, I think?
>
> Does anybody have any thoughts on this issue?

Why would one want to know in advance if the object is printable readably?
Only to decide how to print it, right?
Then the optimal approach is to _try_ to print readably and then take an
alternative/remedial action on failure.
This will usually save a structure traversal.

Thus I would advocate for a `print-readably' variable that would emulate
the CL behavior <http://clhs.lisp.se/Body/v_pr_rda.htm>.

> I wonder whether this could be efficiently implemented by, say, having
> some kind of special value for PRINTCHARFUN for prin1, but I haven't
> looked at the code yet.

Yeah, we could create a "/dev/null"-type object and pass it to prin1.
However, this would still make the above approach better in most cases.

(My original reply was, apparently, sent just to Lars, and was incorrect)

-- 
Sam Steingold (http://sds.podval.org/) on darwin Ns 10.3.2022
http://childpsy.net http://calmchildstories.com http://steingoldpsychology.com
https://www.memritv.org https://www.dhimmitude.org http://think-israel.org
When you are arguing with an idiot, your opponent is doing the same.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]