From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matthew White Newsgroups: gmane.emacs.devel Subject: eval-print-last-sexp: "Selecting deleted buffer" error when the current buffer is killed evaluating the expression Date: Fri, 30 Jul 2021 00:55:51 +0200 Message-ID: <20210730005551.5c77f879@pineapple> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="Sig_/ZnWj9GBP0utpa7s3DXGfQYi"; protocol="application/pgp-signature"; micalg=pgp-sha256 Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40554"; 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 Fri Jul 30 00:59:09 2021 Return-path: 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 ) id 1m9Ezd-000AKN-4L for ged-emacs-devel@m.gmane-mx.org; Fri, 30 Jul 2021 00:59:09 +0200 Original-Received: from localhost ([::1]:53930 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m9Ezb-00074H-JY for ged-emacs-devel@m.gmane-mx.org; Thu, 29 Jul 2021 18:59:07 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46722) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m9Ex2-0005K9-T9 for emacs-devel@gnu.org; Thu, 29 Jul 2021 18:56:28 -0400 Original-Received: from confino.investici.org ([2a00:c38:11e:ffff::a020]:37997) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m9Ex0-0001gI-3p for emacs-devel@gnu.org; Thu, 29 Jul 2021 18:56:28 -0400 Original-Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 4GbQqw2H2dz10y6 for ; Thu, 29 Jul 2021 22:56:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inventati.org; s=stigmate; t=1627599372; bh=PDMhe0fTioasemWderyqROoSYT1q4XcPzHWQ7KkgEsM=; h=Date:From:To:Subject:From; b=HAq1DWclU5SunCWTOlYQhXDfs2BW42yNuN1jm8Qnp2E3BAFlHnGKkgWL2+dvwkI4s mKO7GMwQ/CHGIK61aDzo/xSupIm4pHv0YCMI5bqy9eZf+9ycKFL2fi0uqoRbRH0aES s99TeqXL87qaZ7R2EBDFMiyjfs7JtCknLzA5xvrQ= Original-Received: from [212.103.72.250] (mx1.investici.org [212.103.72.250]) (Authenticated sender: mehw.is.me@inventati.org) by localhost (Postfix) with ESMTPSA id 4GbQqv4hRxz10y2 for ; Thu, 29 Jul 2021 22:56:11 +0000 (UTC) X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Received-SPF: pass client-ip=2a00:c38:11e:ffff::a020; envelope-from=mehw.is.me@inventati.org; helo=confino.investici.org X-Spam_score_int: 9 X-Spam_score: 0.9 X-Spam_bar: / X-Spam_report: (0.9 / 5.0 requ) AC_FROM_MANY_DOTS=2.996, BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:271801 Archived-At: --Sig_/ZnWj9GBP0utpa7s3DXGfQYi Content-Type: multipart/mixed; boundary="MP_/VD/O6/wCiC7sEPpA9cRwUiT" --MP_/VD/O6/wCiC7sEPpA9cRwUiT Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hello, I tumbled on the error in the subject, "Selecting deleted buffer", evaluating the following line in the "*scratch*" buffer with C-j: (kill-buffer (current-buffer)) When the other buffer isn't read-only, suppose "*foo*" is selected after killing the current buffer "*scratch*", the error "Selecting deleted buffer" appears into the echo area and the return value of the evaluation is printed into the other buffer "*foo*". If the other buffer is "*Messages*", which is read-only, the error "Buffer is read-only: #" will appear instead. I'd like to discuss the possibility to enforce printing the output either to the initial current buffer when eval-print-last-sexp has been called, or to the echo area when the prefix '-' is given. If the initial current buffer (aka standard-output) is killed, during the evaluation of the expression, we fall back to the echo area as the output medium. I attach a patch as proof of concept. Below there's a version of the current eval-print-last-sexp, as of commit 7e8d1b08e3e23bc783cad10e620c2ebe6536965c, well commented: (defun eval-print-last-sexp (&optional eval-last-sexp-arg-internal) (interactive "P") (let ((standard-output (current-buffer))) ;; This prints to `standard-output', aka `current-buffer'. (terpri) ;; Since `eval-last-sexp-arg-internal' is always replaced with t ;; when nil, the expression's return value is printed to what is ;; the actual `current-buffer' after the evaluation is done. If ;; the initial buffer is killed, the current buffer is something ;; else after the evaluation. "Buffer is read-only" is given if ;; the `current-buffer' is read-only, otherwise the return value ;; is printed at whatever is the actual `current-buffer' cursor. (eval-last-sexp (or eval-last-sexp-arg-internal t)) ;; This prints to `standard-output', aka `current-buffer' before ;; the expression is evaluated. If the `standard-output' buffer ;; has been killed evaluating the expression, the PRINTPREPARE C ;; macro will trigger an error trying to `set-buffer' the killed ;; buffer: "Selecting deleted buffer". (terpri))) Thank you. Best regards, -Matthew --MP_/VD/O6/wCiC7sEPpA9cRwUiT Content-Type: text/x-patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=0001-eval-print-last-sexp-print-to-initial-buffer-or-to-e.patch =46rom d1a93240cd6150f67622b9f6f91b29b768ee5231 Mon Sep 17 00:00:00 2001 From: Matthew White Date: Thu, 29 Jul 2021 20:24:37 +0000 Subject: [PATCH] eval-print-last-sexp: print to initial buffer or to echo a= rea * lisp/progmodes/elisp-mode.el (eval-print-last-sexp): Update docstring about the possible use of the prefix argument. A '-' prefix prints to the echo area, otherwise prints to the initial current buffer (aka 'standard-output'), unless it's killed evaluating the expression. If 'standard-output' has been killed, print to the echo area. Do not print newlines to the echo area. This prevents an error triggered when calling 'terpri' with a PRINTCHARFUN or 'standard-output' set to a killed buffer. To be specific, PRINTPREPARE C macro may try to 'set-buffer' the initial current buffer (aka 'standard-output') which has been killed while evaluating the expression. Also, when the initial current buffer is killed, unless a '-' prefix argument is used, the output shouldn't be printed into a different buffer than the initial one, but to the echo area. The errors described above can be hit evaluating with C-j the following expression in the *scratch* buffer: (kill-buffer (current-buffer)) If the other buffer is not read-only, the evaluation's return value is printed to the other buffer and the error "Selecting deleted buffer" appears into the echo area. If the other buffer is read-only, like the *Messages* buffer, "Buffer is read-only: ..." appears into the echo area. --- lisp/progmodes/elisp-mode.el | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el index 7ed2d3d08c..20fcec6f85 100644 --- a/lisp/progmodes/elisp-mode.el +++ b/lisp/progmodes/elisp-mode.el @@ -1030,7 +1030,10 @@ Semicolons start comments. ;;; Globally accessible functionality =20 (defun eval-print-last-sexp (&optional eval-last-sexp-arg-internal) - "Evaluate sexp before point; print value into current buffer. + "Evaluate sexp before point. + +Interactively, with a non `-' prefix argument, print output into +current buffer, otherwise print in the echo area. =20 Normally, this function truncates long output according to the value of the variables `eval-expression-print-length' and @@ -1042,10 +1045,33 @@ also causes integers to be printed in several addit= ional formats If `eval-expression-debug-on-error' is non-nil, which is the default, this command arranges for all errors to enter the debugger." (interactive "P") - (let ((standard-output (current-buffer))) - (terpri) - (eval-last-sexp (or eval-last-sexp-arg-internal t)) - (terpri))) + (let* ((standard-output (current-buffer)) + ;; Unless the output is already redirected to the echo area, + ;; force it to the `standard-output' if it is a live buffer. + (force-standard-output `(lambda (args) + (let ((value (nth 0 args)) + (output (nth 1 args)) + (rest (nthcdr 2 args))) + (unless (eq output t) + (setq output (if (buffer-live-p ,st= andard-output) + ,standard-output + t))) + (append (list value output) rest))))) + (pcase-let* + ((`(,insert-value ,no-truncate ,char-print-limit) + (eval-expression-get-print-arguments (or eval-last-sexp-arg-inte= rnal t)))) + ;; Don't print newlines to the echo area. + (when insert-value + (terpri)) + ;; The expression may kill `standard-output'. + (unwind-protect + (progn + (advice-add #'elisp--eval-last-sexp-print-value :filter-args f= orce-standard-output) + (eval-last-sexp (list insert-value no-truncate char-print-limi= t))) + (advice-remove #'elisp--eval-last-sexp-print-value force-standard-= output)) + ;; Don't print to a killed `standard-output' buffer. + (when (and insert-value (buffer-live-p standard-output)) + (terpri))))) =20 =20 (defun last-sexp-setup-props (beg end value alt1 alt2) --=20 2.31.1 --MP_/VD/O6/wCiC7sEPpA9cRwUiT-- --Sig_/ZnWj9GBP0utpa7s3DXGfQYi Content-Type: application/pgp-signature Content-Description: OpenPGP digital signature -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEHleiF1SoBpqVdUvfw5wJ3TZNsgAFAmEDMfcACgkQw5wJ3TZN sgAqfwf+OlqZrhQM00TcjLIrcA6SbXKqSZlaGwGLlAv8WfgdF8pCbmTBwwAdQQU+ iU02owvzvp3nDBxqdz6WroEnJf6mCLc4Nz2wmfmcvXzVyF1h3qbY+R5/3S9TUvFl seg9WjICXvfu6HT+gSK0ZgqUF7rYZBDx/EP6JgH8qyMkZlraIQipMYbJdEP76DNU nFJIF1aeaN0lVw3gIwkTw1ou9vy5ND3nuSe8Xc7jyGP+OHWAl6qRfme2np9cgqBg JIZXXIJ2HE+O1vl4UhQT3CRp17k6j24+gTCvQLWc1puBt8VL9EDpxYARi/9Iu2s5 Im9LI/b1vQmf4H+pEsHiXdXIY/qb+w== =7MUD -----END PGP SIGNATURE----- --Sig_/ZnWj9GBP0utpa7s3DXGfQYi--