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#58888: 28.1.90; font-lock-defaults not respected when hack-local-variables unsafe variable dialogue is displayed before setting the defaults Date: Sun, 30 Oct 2022 22:15:40 -0400 Message-ID: References: <878rkxajcs.fsf@localhost> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25728"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 58888@debbugs.gnu.org To: Ihor Radchenko Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Oct 31 03:18:45 2022 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 1opKNw-0006QM-LH for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 31 Oct 2022 03:18:45 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1opKMe-0002C7-35; Sun, 30 Oct 2022 22:17:26 -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 1opKMJ-00027X-F3 for bug-gnu-emacs@gnu.org; Sun, 30 Oct 2022 22:17:06 -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 1opKMJ-0003CA-28 for bug-gnu-emacs@gnu.org; Sun, 30 Oct 2022 22:17:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1opKMI-0007Q6-HO for bug-gnu-emacs@gnu.org; Sun, 30 Oct 2022 22:17: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: Mon, 31 Oct 2022 02:17:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58888 X-GNU-PR-Package: emacs Original-Received: via spool by 58888-submit@debbugs.gnu.org id=B58888.166718256228414 (code B ref 58888); Mon, 31 Oct 2022 02:17:02 +0000 Original-Received: (at 58888) by debbugs.gnu.org; 31 Oct 2022 02:16:02 +0000 Original-Received: from localhost ([127.0.0.1]:39300 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1opKLJ-0007Ns-M0 for submit@debbugs.gnu.org; Sun, 30 Oct 2022 22:16:02 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:27694) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1opKLG-0007NZ-W3 for 58888@debbugs.gnu.org; Sun, 30 Oct 2022 22:15:59 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 03A2C100189; Sun, 30 Oct 2022 22:15:53 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id C117E10000D; Sun, 30 Oct 2022 22:15:50 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1667182550; bh=V26k14DRnVJS8sRupiWNCb6wTuByWyz6CLFz82tHJdo=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=mM1IFdeO9xvf/PV9w4rM/3ER8bwZxJNjIoiXUB9V8yOacOLvkYfixr8eXtOV5SoIE 9si0ri6bLmHAfFxn7rJkkr/U8/CbKITA1+eWGznJyA2B0tFNUXy1FJDI9uBrhTJfJQ 7Go5pDhddcBC+X4juNv7Mqg6sDx65afEvw905fBpRyfRPhmpDXs4fp/2nQScStOJZn Jd9QhsA4+5xoXb+fWGDw/0JNJgT3IwDseeqtE395pH99huMt52W8Fp5kZ3aMWYxuBT dfH4NSdmEKFWokchchGOwlBCS2TpKRUkc7RkYAYvdrv128I008aJ4zqHXLB+20HElk OW/wnFp30D8uw== Original-Received: from milanesa (65-110-220-202.cpe.pppoe.ca [65.110.220.202]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 51AA2120172; Sun, 30 Oct 2022 22:15:50 -0400 (EDT) In-Reply-To: <878rkxajcs.fsf@localhost> (Ihor Radchenko's message of "Sun, 30 Oct 2022 06:58:11 +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: , Original-Sender: "bug-gnu-emacs" Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:246662 Archived-At: --=-=-= Content-Type: text/plain > ------------ > # -*- mode:my/test -*- > This is test with keyword to be fontified. > > # Local Variables: > # eval: (setq unsafe-variable t) > # End: > ------------- > > Then, consider the following major mode: > > (define-derived-mode my/test-mode text-mode "Test" > "" > (add-hook 'hack-local-variables-hook > (lambda () > (setq-local my/test-mode-keywords '(("keyword" . font-lock-keyword-face))) > (setq font-lock-defaults '(my/test-mode-keywords))) > nil 'local)) > > 1. emacs -Q > 2. Evaluate the major mode definition > 3. Open the file > 4. Answer "y" in the unsafe variable prompt > 5. Observe "keyword" not being fontified. > 6. Expected: "keyword" fontified using font-lock-keyword-face. Hmm... AFAICT the problem here is that the implementation of `global-font-lock-mode` ends up trying to enable `font-lock-mode` in that file's buffer during execution of the `after-major-mode-change-hook` of *another* buffer while querying whether to obey those file-local settings, and then fails to try again when the hook is run in the desired buffer. I suspect the patch below might help (requires recompiling `font-core.el` and re-dumping Emacs), but as the comment in there explains it might not always be sufficient either. Hmm... Stefan --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=easy-mmode.patch diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el index 7d54a84687b..4d2174fbe6a 100644 --- a/lisp/emacs-lisp/easy-mmode.el +++ b/lisp/emacs-lisp/easy-mmode.el @@ -485,6 +485,8 @@ define-globalized-minor-mode (extra-keywords nil) (MODE-variable mode) (MODE-buffers (intern (concat global-mode-name "-buffers"))) + (MODE-enable-in-buffer + (intern (concat global-mode-name "-enable-in-buffer"))) (MODE-enable-in-buffers (intern (concat global-mode-name "-enable-in-buffers"))) (MODE-check-buffers @@ -551,10 +553,10 @@ define-globalized-minor-mode (if ,global-mode (progn (add-hook 'after-change-major-mode-hook - #',MODE-enable-in-buffers) + #',MODE-enable-in-buffer) (add-hook 'find-file-hook #',MODE-check-buffers) (add-hook 'change-major-mode-hook #',MODE-cmhh)) - (remove-hook 'after-change-major-mode-hook #',MODE-enable-in-buffers) + (remove-hook 'after-change-major-mode-hook #',MODE-enable-in-buffer) (remove-hook 'find-file-hook #',MODE-check-buffers) (remove-hook 'change-major-mode-hook #',MODE-cmhh)) @@ -601,7 +603,32 @@ define-globalized-minor-mode ;; List of buffers left to process. (defvar ,MODE-buffers nil) - ;; The function that calls TURN-ON in each buffer. + ;; The function that calls TURN-ON in the current buffer. + (defun ,MODE-enable-in-buffer () + ;; Remove ourselves from the list of pending buffers. + (setq ,MODE-buffers (delq (current-buffer) ,MODE-buffers)) + (unless ,MODE-set-explicitly + (unless (eq ,MODE-major-mode major-mode) + (if ,MODE-variable + (progn + (,mode -1) + (funcall ,turn-on-function)) + (funcall ,turn-on-function)))) + (setq ,MODE-major-mode major-mode)) + (put ',MODE-enable-in-buffer 'definition-name ',global-mode) + + ;; In the normal case, major modes run `after-change-major-mode-hook' + ;; which will have called `MODE-enable-in-buffer' for us. But some + ;; major modes don't use `define-derived-mode' and thus fail to run + ;; `after-change-major-mode-hook', so have a combination of ugly hacks + ;; to try and catch those corner cases by listening to + ;; `change-major-mode-hook' to discover potential candidates and then + ;; checking in `post-command-hook' and `find-file-hook' if some of those + ;; still haven't run `after-change-major-mode-hook'. + ;; FIXME: We should try a get rid of this ugly hack and rely purely + ;; on `after-change-major-mode-hook' because they can (and do) end up + ;; running `MODE-enable-in-buffer' too early (when the major isn't yet + ;; fully setup) in some cases (see bug#58888). (defun ,MODE-enable-in-buffers () (let ((buffers ,MODE-buffers)) ;; Clear MODE-buffers to avoid scanning the same list of @@ -611,14 +638,7 @@ define-globalized-minor-mode (dolist (buf buffers) (when (buffer-live-p buf) (with-current-buffer buf - (unless ,MODE-set-explicitly - (unless (eq ,MODE-major-mode major-mode) - (if ,MODE-variable - (progn - (,mode -1) - (funcall ,turn-on-function)) - (funcall ,turn-on-function)))) - (setq ,MODE-major-mode major-mode)))))) + (,MODE-enable-in-buffer)))))) (put ',MODE-enable-in-buffers 'definition-name ',global-mode) (defun ,MODE-check-buffers () --=-=-=--