From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
From: Alan Mackenzie <acm@muc.de>
Newsgroups: gmane.emacs.devel
Subject: buffer-string considered as a Bad Thing.
Date: Tue, 4 Jun 2024 19:05:22 +0000
Message-ID: <Zl9lcuxkwBDw3GvZ@ACM>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214";
	logging-data="17589"; mail-complaints-to="usenet@ciao.gmane.io"
To: emacs-devel@gnu.org
Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Jun 04 21:06:31 2024
Return-path: <emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org>
Envelope-to: ged-emacs-devel@m.gmane-mx.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.92)
	(envelope-from <emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org>)
	id 1sEZUM-0004OE-HK
	for ged-emacs-devel@m.gmane-mx.org; Tue, 04 Jun 2024 21:06:30 +0200
Original-Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <emacs-devel-bounces@gnu.org>)
	id 1sEZTZ-0004dk-Fs; Tue, 04 Jun 2024 15:05:41 -0400
Original-Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <acm@muc.de>) id 1sEZTX-0004dG-TP
 for emacs-devel@gnu.org; Tue, 04 Jun 2024 15:05:39 -0400
Original-Received: from mail.muc.de ([193.149.48.3])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <acm@muc.de>) id 1sEZTU-0000VD-Ru
 for emacs-devel@gnu.org; Tue, 04 Jun 2024 15:05:39 -0400
Original-Received: (qmail 62409 invoked by uid 3782); 4 Jun 2024 21:05:23 +0200
Original-Received: from muc.de (pd953a1a0.dip0.t-ipconnect.de [217.83.161.160]) (using
 STARTTLS) by colin.muc.de (tmda-ofmipd) with ESMTP;
 Tue, 04 Jun 2024 21:05:22 +0200
Original-Received: (qmail 11199 invoked by uid 1000); 4 Jun 2024 19:05:22 -0000
Content-Disposition: inline
X-Submission-Agent: TMDA/1.3.x (Ph3nix)
X-Primary-Address: acm@muc.de
Received-SPF: pass client-ip=193.149.48.3; envelope-from=acm@muc.de;
 helo=mail.muc.de
X-Spam_score_int: -18
X-Spam_score: -1.9
X-Spam_bar: -
X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001,
 SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: emacs-devel@gnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: "Emacs development discussions." <emacs-devel.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/emacs-devel>,
 <mailto:emacs-devel-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/emacs-devel>
List-Post: <mailto:emacs-devel@gnu.org>
List-Help: <mailto:emacs-devel-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/emacs-devel>,
 <mailto:emacs-devel-request@gnu.org?subject=subscribe>
Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org
Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org
Xref: news.gmane.io gmane.emacs.devel:319825
Archived-At: <http://permalink.gmane.org/gmane.emacs.devel/319825>

Hello, Emacs.

In master (or any other non-prehistoric version of Emacs), buffer-string
gets misused.  In particular, function attempting to "print to a string"
first print to a temporary buffer, then call buffer-string to get that
printed object into the string.

As an example, in pp, the function pp-to-string does this.  To see the
effect, try

    M-: (pp "foo \"bar\" baz")

..  This gets printed correctly to the temporary buffer  *temp* as:

    "foo \"bar\" baz" ;; (a 13 character string)

, but buffer-string wrongly converts those literal characters to

    "foo \\\"bar\\\" baz" ;; (a 15 character string)

..

Printing to a temporary buffer then converting that to a string (without
reading it) appears to be a workaround for the Emacs core failing to
allow a string as a possibility for PRINTCHARFUN in functions like
prin1 and princ.

An ideal solution seems to be somehow allowing a string to be
PRINTCHARFUN for these functions.  However print1 and friends don't
return values; instead they append to buffers or output to output
streams like stdout.  Allowing a string output would be somewhat
awkward.

If this awkwardness rules out output to strings, we need better
machinery than buffer-string to get a correct string from a buffer.  We
need to expose some more functionality from the reader to Lisp.  With
that we could write a function for filter-buffer-substring-function and
thus correctly convert the temp buffer back to a string.  Or something
like that.

Before I raise a bug report, what do people think about this?

-- 
Alan Mackenzie (Nuremberg, Germany).