From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Newsgroups: gmane.emacs.bugs Subject: bug#50946: insert-file-contents can corrupt buffers. [Was: bug#50946: Emacs-28: Inadequate coding in hack-elisp-shorthands] Date: Sun, 03 Oct 2021 16:42:48 +0100 Message-ID: <87tuhyt8qv.fsf@gmail.com> References: <831r54einq.fsf@gnu.org> <871r54xnds.fsf@gmail.com> <87ee933bcj.fsf@gmail.com> <83pmsnbnci.fsf@gnu.org> <83k0ivbjbu.fsf@gnu.org> <83czonbhex.fsf@gnu.org> <83lf3a8eo7.fsf@gnu.org> <87y27at950.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18644"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.60 (gnu/linux) Cc: 50946@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Oct 03 17:43:15 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1mX3dz-0004l2-6U for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 03 Oct 2021 17:43:15 +0200 Original-Received: from localhost ([::1]:51448 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mX3dv-00087R-U7 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 03 Oct 2021 11:43:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37132) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mX3dm-0007yj-9K for bug-gnu-emacs@gnu.org; Sun, 03 Oct 2021 11:43:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:51460) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mX3dm-0002wy-0C for bug-gnu-emacs@gnu.org; Sun, 03 Oct 2021 11:43:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mX3dl-0002nt-UK for bug-gnu-emacs@gnu.org; Sun, 03 Oct 2021 11:43:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 03 Oct 2021 15:43:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 50946 X-GNU-PR-Package: emacs Original-Received: via spool by 50946-submit@debbugs.gnu.org id=B50946.163327578010768 (code B ref 50946); Sun, 03 Oct 2021 15:43:01 +0000 Original-Received: (at 50946) by debbugs.gnu.org; 3 Oct 2021 15:43:00 +0000 Original-Received: from localhost ([127.0.0.1]:34773 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mX3dj-0002nb-QS for submit@debbugs.gnu.org; Sun, 03 Oct 2021 11:43:00 -0400 Original-Received: from mail-wr1-f41.google.com ([209.85.221.41]:38540) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mX3de-0002nH-CV for 50946@debbugs.gnu.org; Sun, 03 Oct 2021 11:42:58 -0400 Original-Received: by mail-wr1-f41.google.com with SMTP id u18so25649818wrg.5 for <50946@debbugs.gnu.org>; Sun, 03 Oct 2021 08:42:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-transfer-encoding; bh=dpuGXQ00U4MpXuZLaokmaatH/c5fB+DoA82GSqG2bm4=; b=bHT61T/ZxLaqpOvuSgROcxKG6zAknfgF4vOVy2q9Yu3ixtB83ABefQwM5xn1wFr/3k tKEUIpY1V9zCD/pK0mEEGzGiz4dg18GqSCf0n+IGAIrBApU+2qQFEy8Bdf39QUoz9qva ts5iyZMWXTr7ZtOedRk1Xxnr0Xk/0Wb8zGQilHdO9KtoTAg5b9LlXj+jcK792Cb5AScg 6MbsNgtZsEZHOXEihEAeMrt7GKM2wS0fwNxxXjMvuLxvVps9k6FaQZv+HsvYD1rVw7RV atKkI2MRVZ/CeAjqpCq3xb8O+SpjG9aMfejW+i4ml9XC5456u+bu7qwxT6NSWwMWw62r GVCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=dpuGXQ00U4MpXuZLaokmaatH/c5fB+DoA82GSqG2bm4=; b=Rqm/5+sOz/+EvsQKfAy48eJ0OiIn/lxyIDybiCvO7Vmi6UQ3jhqmTgoktfO9PdBQ0j CQcmqddQZQTnDsqBzfQIfOYAciQ3FoEfmNWXij75ytqF5oNyCfqn7OFPYQzEZ31V7Hzb e0nuNKPEMWaylRKN1O3JuhYibia5o6Yk5EYKPErYQf1v3CF+dTCVpmQF4isWWMps52Mn P/COUCJg77xzNEhEjJs245i8Aad9/cK303uZlGlTCoWejOITOliKQ9QKFTcinh2b1OGk Yvr8mTlj9IsF3zxccirqt41vm8UmkUfYMH27vSUY7UE8SpDzOSPAJAPYmrGsQkJXRxvW oK9Q== X-Gm-Message-State: AOAM533GZ7kj5vNWlv36c56i50NMXZlSA466AmDkWZ9qZ0QKtWAYT0wW zRF2BtWXZCNeyoNd2ezw7QqcB8QD4aM= X-Google-Smtp-Source: ABdhPJyugMilmCqSoqbKfMjx68RpoQkxgTN5jfsVEM4BBbefsvLBGQuw3pJwT836pt55qGW3tO6GiA== X-Received: by 2002:a05:6000:18a2:: with SMTP id b2mr9127276wri.342.1633275767847; Sun, 03 Oct 2021 08:42:47 -0700 (PDT) Original-Received: from krug ([87.196.156.235]) by smtp.gmail.com with ESMTPSA id g2sm11743677wrq.62.2021.10.03.08.42.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 08:42:47 -0700 (PDT) In-Reply-To: <87y27at950.fsf@gmail.com> ("=?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?="'s message of "Sun, 03 Oct 2021 16:34:19 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:216270 Archived-At: Jo=C3=A3o T=C3=A1vora writes: > I tried with a hook but it doesn't work: it breaks a shorthands test, > a most basic one. Maybe you can understand where the problem is? As usual, digging a bit after posting the broken patch brought me to an apparent solution. The following patch seems to work, slightly complexifies load-with-code-conversion, but simplifies shorthands.el very much (and should solve the problems). But please have a look. Jo=C3=A3o commit 9bc76f971597d42ba0c8b4655501223d03181b3b Author: Jo=C3=A3o T=C3=A1vora Date: Sun Oct 3 16:05:40 2021 +0100 Simplify hack-read-symbol-shorthands again (bug#50946) =20=20=20=20 * lisp/loadup.el (load-source-file-function): Don't set twice. =20=20=20=20 * lisp/shorthands.el (hack-read-symbol-shorthands): Simplify. (load-with-shorthands-and-code-conversion): Remove. =20=20=20=20 * lisp/international/mule.el (load-with-code-conversion): Call load-with-code-coversion-hook. Set up shorthands. diff --git a/lisp/international/mule.el b/lisp/international/mule.el index 2a855b5673..b612ea5c77 100644 --- a/lisp/international/mule.el +++ b/lisp/international/mule.el @@ -294,6 +294,9 @@ define-charset =20 (apply 'define-charset-internal name (mapcar 'cdr attrs)))) =20 +(defvar load-with-code-conversion-hook nil + "Hook run in `load-with-code-conversion'.") + (defun load-with-code-conversion (fullname file &optional noerror nomessag= e) "Execute a file of Lisp code named FILE whose absolute name is FULLNAME. The file contents are decoded before evaluation if necessary. @@ -319,7 +322,8 @@ load-with-code-conversion (let ((load-true-file-name fullname) (load-file-name fullname) (set-auto-coding-for-load t) - (inhibit-file-name-operation nil)) + (inhibit-file-name-operation nil) + shorthands) (with-current-buffer buffer ;; So that we don't get completely screwed if the ;; file is encoded in some complicated character set, @@ -328,6 +332,8 @@ load-with-code-conversion ;; Don't let deactivate-mark remain set. (let (deactivate-mark) (insert-file-contents fullname)) + (run-hooks 'load-with-code-conversion-hook) + (setq shorthands read-symbol-shorthands) ;; If the loaded file was inserted with no-conversion or ;; raw-text coding system, make the buffer unibyte. ;; Otherwise, eval-buffer might try to interpret random @@ -338,11 +344,13 @@ load-with-code-conversion (set-buffer-multibyte nil)) ;; Make `kill-buffer' quiet. (set-buffer-modified-p nil)) - ;; Have the original buffer current while we eval. - (eval-buffer buffer nil - ;; This is compatible with what `load' does. - (if dump-mode file fullname) - nil t)) + ;; Have the original buffer current while we eval, + ;; but consider shorthands of the eval'ed one. + (let ((read-symbol-shorthands shorthands)) + (eval-buffer buffer nil + ;; This is compatible with what `load' does. + (if dump-mode file fullname) + nil t))) (let (kill-buffer-hook kill-buffer-query-functions) (kill-buffer buffer))) (do-after-load-evaluation fullname) diff --git a/lisp/loadup.el b/lisp/loadup.el index 3fb6b81328..3a55d2c805 100644 --- a/lisp/loadup.el +++ b/lisp/loadup.el @@ -355,7 +355,6 @@ (load "paren") =20 (load "shorthands") -(setq load-source-file-function #'load-with-shorthands-and-code-conversion) =20 (load "emacs-lisp/eldoc") (load "cus-start") ;Late to reduce customize-rogue (needs loaddefs.el anyw= ay) diff --git a/lisp/shorthands.el b/lisp/shorthands.el index c31ef3d216..ecf04ac587 100644 --- a/lisp/shorthands.el +++ b/lisp/shorthands.el @@ -28,35 +28,17 @@ (require 'files) (eval-when-compile (require 'cl-lib)) =20 -(defun hack-read-symbol-shorthands (fullname) - "Return value of `read-symbol-shorthands' file-local variable in FULLNAM= E. -FULLNAME is the absolute file name of an Elisp .el file which -potentially specifies a file-local value for -`read-symbol-shorthands'. The Elisp code in FULLNAME isn't read -or evaluated in any way, except for extraction of the -buffer-local value of `read-symbol-shorthands'." - (let* ((size (nth 7 (file-attributes fullname))) - (from (max 0 (- size 3000))) - (to size)) - (with-temp-buffer - (while (and (< (buffer-size) 3000) (>=3D from 0)) - (insert-file-contents fullname nil from to) - (setq to from - from (cond - ((=3D from 0) -1) - (t (max 0 (- from 100)))))) - ;; FIXME: relies on the `hack-local-variables--find-variables' - ;; detail of files.el. That function should be exported, - ;; possibly be refactored into two parts, since we're only - ;; interested in basic "Local Variables" parsing. - (alist-get 'read-symbol-shorthands (hack-local-variables--find-varia= bles))))) - -(defun load-with-shorthands-and-code-conversion (fullname file noerror nom= essage) - "Like `load-with-code-conversion', but also consider Elisp shorthands. -This function uses shorthands defined in the file FULLNAME's local -value of `read-symbol-shorthands', when it processes that file's Elisp cod= e." - (let ((read-symbol-shorthands (hack-read-symbol-shorthands fullname))) - (load-with-code-conversion fullname file noerror nomessage))) +(add-hook 'load-with-code-conversion-hook #'hack-read-symbol-shorthands) + +(defun hack-read-symbol-shorthands () + "Set `read-symbol-shorthands' from Local Variables section." + ;; FIXME: relies on the `hack-local-variables--find-variables' + ;; detail of files.el. That function should be exported, + ;; possibly be refactored into two parts, since we're only + ;; interested in basic "Local Variables" parsing. + (setq-local read-symbol-shorthands + (alist-get 'read-symbol-shorthands + (hack-local-variables--find-variables)))) =20 ;; FIXME: move this all to progmodes/elisp-mode.el? OTOH it'd make