From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#62419: 28.2; Elisp let-bound buffer-local variable and kill-local-variable Date: Sun, 26 Mar 2023 10:01:48 -0400 Message-ID: References: <19A857D6-D071-44DE-AF89-539A563FD782@gmail.com> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1106"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 62419@debbugs.gnu.org To: Matthew Malcomson Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Mar 26 16:02:19 2023 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 1pgQwt-000AZq-9r for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 26 Mar 2023 16:02:19 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pgQwj-0004by-Vy; Sun, 26 Mar 2023 10:02:10 -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 ) id 1pgQwd-0004bD-CC for bug-gnu-emacs@gnu.org; Sun, 26 Mar 2023 10:02:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pgQwc-0006b5-QR for bug-gnu-emacs@gnu.org; Sun, 26 Mar 2023 10:02:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pgQwc-0004Oy-Ae for bug-gnu-emacs@gnu.org; Sun, 26 Mar 2023 10:02:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 26 Mar 2023 14:02:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 62419 X-GNU-PR-Package: emacs Original-Received: via spool by 62419-submit@debbugs.gnu.org id=B62419.167983932116917 (code B ref 62419); Sun, 26 Mar 2023 14:02:02 +0000 Original-Received: (at 62419) by debbugs.gnu.org; 26 Mar 2023 14:02:01 +0000 Original-Received: from localhost ([127.0.0.1]:45608 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pgQwb-0004Ok-84 for submit@debbugs.gnu.org; Sun, 26 Mar 2023 10:02:01 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:25932) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pgQwY-0004OU-8o for 62419@debbugs.gnu.org; Sun, 26 Mar 2023 10:01:59 -0400 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 9C19C440B2C; Sun, 26 Mar 2023 10:01:52 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 6EA594403D5; Sun, 26 Mar 2023 10:01:50 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1679839310; bh=UTCqmxl/1IHrZsdIhuVTxMfk1Fsc5YulL8TPgiJLrvI=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=E+JcppWsdRCOgQpeWB6jo/Atgz0pTkDq6f0d6VBB0TqX7SqIhLYwg/R4VvLs+euY4 IBn5aIda6hChgm8vvn/B+/qk3AgveluNmyppFc54PqUPSsKLVHzkEXbEDuXuKsRt4t t2D22S6W8Hm8xZeluA9iHf7OBFPg5hkjz+KLqOA6ZXPGUsTxIgVpv2AtNa35xZlrYr Lxu7sJnBm/oN5u9Hf6iJxmWjFi5rpKhmgenz30E59hSFEKPxv4OVJp0L7HEjnlGyyh /mkhyX8CoHz6U8UixpwgPOnjXPJ3ETIcc1mzZbfOtVd4ne7PVaI+2wCcNp60L9PK4S olfoDfReYuTNw== Original-Received: from pastel (unknown [216.154.34.24]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 489C91231A8; Sun, 26 Mar 2023 10:01:50 -0400 (EDT) In-Reply-To: <19A857D6-D071-44DE-AF89-539A563FD782@gmail.com> (Matthew Malcomson's message of "Fri, 24 Mar 2023 13:37:57 +0000") 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-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:258683 Archived-At: > (setq auto-fill-function 'local-symbol) > (describe-variable 'auto-fill-function) > ;; `auto-fill-function' is let-bound in the buffer scope > (let ((auto-fill-function 'temp-symbol)) > ;; Now there is no buffer-local variable for `auto-fill-function', but the > ;; `let' unwrapping info is still there. > (kill-local-variable 'auto-fill-function) > ;; Since the check in the emacs source is > ;; a) Is there a buffer-local variable. > ;; b) Is there a let-binding shadowing the current variable. > ;; Then this `setq' sets the *global* variable. > (setq auto-fill-function 'other-symbol)) > ;; Exiting the `let' has special handling to avoid resetting a local variable > ;; when the local variable was `let' bound, which means that overall the `setq' > ;; set the global variable and the `let' has been lost. AFAIK the behavior is "as intended": the `let` only affects *one* binding, either the global one or the buffer-local one. > AIUI the minor mode is usually turned on outside of any let binding, but the > described situation happens due to an edge-case: > - `newline` let-binds `auto-fill-mode` > - Visited file has changed outside of Emacs, so user is queried whether to > `revert-buffer` while in `newline` > - `revert-buffer` calls `normal-mode`, which runs `kill-all-local-variables` > - Later, `yasnippet-mode` is turned on and the `setq` is evaluated. The "`newline` let-binds `auto-fill-mode`" seems like the source of the problem :-( As for yasnippet.el, I suspect that I patch like the one below would avoid the problem. Stefan diff --git a/yasnippet.el b/yasnippet.el index 78ef38ac39..98124c7a61 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -860,10 +828,9 @@ which decides on the snippet to expand.") "Hook run when `yas-minor-mode' is turned on.") (defun yas--auto-fill-wrapper () - (when (and auto-fill-function - (not (eq auto-fill-function #'yas--auto-fill))) - (setq yas--original-auto-fill-function auto-fill-function) - (setq auto-fill-function #'yas--auto-fill))) + (when auto-fill-function ;Turning the mode ON. + (cl-assert (local-variable-p 'auto-fill-function)) + (add-function :around (local 'auto-fill-function) #'yas--auto-fill))) ;;;###autoload (define-minor-mode yas-minor-mode @@ -906,8 +873,8 @@ Key bindings: ;; auto-fill handler. (remove-hook 'post-command-hook #'yas--post-command-handler t) (remove-hook 'auto-fill-mode-hook #'yas--auto-fill-wrapper) - (when (local-variable-p 'yas--original-auto-fill-function) - (setq auto-fill-function yas--original-auto-fill-function)) + (when (local-variable-p 'auto-fill-function) + (remove-function (local 'auto-fill-function) #'yas--auto-fill)) (setq emulation-mode-map-alists (remove 'yas--direct-keymaps emulation-mode-map-alists))))) @@ -3880,7 +3847,7 @@ field start. This hook does nothing if an undo is in progress." snippet (yas--snippet-field-mirrors snippet))) (setq yas--todo-snippet-indent nil)))) -(defun yas--auto-fill () +(defun yas--auto-fill (orig-fun &rest args) ;; Preserve snippet markers during auto-fill. (let* ((orig-point (point)) (end (progn (forward-paragraph) (point))) @@ -3897,44 +3864,7 @@ field start. This hook does nothing if an undo is in progress." reoverlays)) (goto-char orig-point) (let ((yas--inhibit-overlay-hooks t)) - (if yas--original-auto-fill-function - (funcall yas--original-auto-fill-function) - ;; Shouldn't happen, gather more info about it (see #873/919). - (let ((yas--fill-fun-values `((t ,(default-value 'yas--original-auto-fill-function)))) - (fill-fun-values `((t ,(default-value 'auto-fill-function)))) - ;; Listing 2 buffers with the same value is enough - (print-length 3)) - (save-current-buffer - (dolist (buf (let ((bufs (buffer-list))) - ;; List the current buffer first. - (setq bufs (cons (current-buffer) - (remq (current-buffer) bufs))))) - (set-buffer buf) - (let* ((yf-cell (assq yas--original-auto-fill-function - yas--fill-fun-values)) - (af-cell (assq auto-fill-function fill-fun-values))) - (when (local-variable-p 'yas--original-auto-fill-function) - (if yf-cell (setcdr yf-cell (cons buf (cdr yf-cell))) - (push (list yas--original-auto-fill-function buf) yas--fill-fun-values))) - (when (local-variable-p 'auto-fill-function) - (if af-cell (setcdr af-cell (cons buf (cdr af-cell))) - (push (list auto-fill-function buf) fill-fun-values)))))) - (lwarn '(yasnippet auto-fill bug) :error - "`yas--original-auto-fill-function' unexpectedly nil in %S! Disabling auto-fill. - %S - `auto-fill-function': %S\n%s" - (current-buffer) yas--fill-fun-values fill-fun-values - (if (fboundp 'backtrace--print-frame) - (with-output-to-string - (mapc (lambda (frame) - (apply #'backtrace--print-frame frame)) - yas--watch-auto-fill-backtrace)) - "")) - ;; Try to avoid repeated triggering of this bug. - (auto-fill-mode -1) - ;; Don't pop up more than once in a session (still log though). - (defvar warning-suppress-types) ; `warnings' is autoloaded by `lwarn'. - (add-to-list 'warning-suppress-types '(yasnippet auto-fill bug))))) + (apply orig-fun args)) (save-excursion (setq end (progn (forward-paragraph) (point))) (setq beg (progn (backward-paragraph) (point))))