GNU bug report logs - #74447
30.0.92; asm-comment-char cannot be set via dir-local variables

Previous Next

Package: emacs;

Reported by: Johann Klähn <johann <at> jklaehn.de>

Date: Wed, 20 Nov 2024 19:13:02 UTC

Severity: normal

Found in version 30.0.92

Done: Stefan Monnier <monnier <at> iro.umontreal.ca>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 74447 in the body.
You can then email your comments to 74447 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#74447; Package emacs. (Wed, 20 Nov 2024 19:13:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Johann Klähn <johann <at> jklaehn.de>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 20 Nov 2024 19:13:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Johann Klähn <johann <at> jklaehn.de>
To: bug-gnu-emacs <at> gnu.org
Subject: 30.0.92; asm-comment-char cannot be set via dir-local variables
Date: Wed, 20 Nov 2024 20:12:16 +0100
In `.dir-locals.el' setting `((asm-mode . ((asm-comment-char . 35))))'
does not have the desired effect.  The comment char is still `;' instead
of `#'.  The reason is that when the syntax table is adjusted according
to the comment char in the body of `asm-mode', the dir-local variables
haven't been loaded yet: `run-mode-hooks' (which would call
`hack-local-variables') is only invoked _after_ the `define-derived-mode'
body, i.e., too late.

I'm using the following workaround:

    (defun asm-mode-set-comment-char-from-dir-local-variables ()
      "Load `asm-comment-char' from dir-local variables.
    This is a HACK, since in `asm-mode', `run-mode-hooks' (which would call
    `hack-local-variables') is only invoked after the `define-derived-mode' body,
    i.e., too late."
      (let ((enable-local-variables :safe))
        (hack-dir-local-variables)
        (when-let* ((char (alist-get 'asm-comment-char dir-local-variables-alist)))
          (setq-local asm-comment-char char))))
    (add-hook 'asm-mode-set-comment-hook #'asm-mode-set-comment-char-from-dir-local-variables)

But maybe there is a more principled approach?

In GNU Emacs 30.0.92 (build 5, x86_64-pc-linux-gnu, GTK+ Version
 3.24.43, cairo version 1.18.0) of 2024-11-19 built on toolbx
Repository revision: 331610aef0572eacb2846f817e979aa5e29170b7
Repository branch: emacs-30
System Description: Fedora Linux 41 (Workstation Edition)

Configured using:
 'configure --with-dbus --with-gif --with-jpeg --with-png --with-rsvg
 --with-tiff --with-xpm --with-gpm=no --with-modules --with-harfbuzz
 --with-cairo --with-native-compilation --enable-link-time-optimization
 --with-pgtk 'CFLAGS=-DMAIL_USE_LOCKF -O2 -fexceptions -g
 -grecord-gcc-switches -pipe -Wp,-D_FORTIFY_SOURCE=2
 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic
 -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection'
 LDFLAGS=-Wl,-z,relro'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG
LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY
PDUMPER PGTK PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XIM GTK3 ZLIB

Important settings:
  value of $LC_MONETARY: en_AU.UTF-8
  value of $LC_NUMERIC: en_AU.UTF-8
  value of $LC_TIME: en_AU.UTF-8
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Assembler




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#74447; Package emacs. (Wed, 20 Nov 2024 19:22:02 GMT) Full text and rfc822 format available.

Message #8 received at 74447 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Johann Klähn <johann <at> jklaehn.de>,
 Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 74447 <at> debbugs.gnu.org
Subject: Re: bug#74447: 30.0.92;
 asm-comment-char cannot be set via dir-local variables
Date: Wed, 20 Nov 2024 21:21:28 +0200
> From: Johann Klähn <johann <at> jklaehn.de>
> Date: Wed, 20 Nov 2024 20:12:16 +0100
> 
> 
> In `.dir-locals.el' setting `((asm-mode . ((asm-comment-char . 35))))'
> does not have the desired effect.  The comment char is still `;' instead
> of `#'.  The reason is that when the syntax table is adjusted according
> to the comment char in the body of `asm-mode', the dir-local variables
> haven't been loaded yet: `run-mode-hooks' (which would call
> `hack-local-variables') is only invoked _after_ the `define-derived-mode'
> body, i.e., too late.
> 
> I'm using the following workaround:
> 
>     (defun asm-mode-set-comment-char-from-dir-local-variables ()
>       "Load `asm-comment-char' from dir-local variables.
>     This is a HACK, since in `asm-mode', `run-mode-hooks' (which would call
>     `hack-local-variables') is only invoked after the `define-derived-mode' body,
>     i.e., too late."
>       (let ((enable-local-variables :safe))
>         (hack-dir-local-variables)
>         (when-let* ((char (alist-get 'asm-comment-char dir-local-variables-alist)))
>           (setq-local asm-comment-char char))))
>     (add-hook 'asm-mode-set-comment-hook #'asm-mode-set-comment-char-from-dir-local-variables)
> 
> But maybe there is a more principled approach?

Stefan, any suggestions?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#74447; Package emacs. (Wed, 20 Nov 2024 19:37:02 GMT) Full text and rfc822 format available.

Message #11 received at 74447 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 74447 <at> debbugs.gnu.org,
 Johann Klähn <johann <at> jklaehn.de>
Subject: Re: bug#74447: 30.0.92; asm-comment-char cannot be set via
 dir-local variables
Date: Wed, 20 Nov 2024 14:36:42 -0500
[Message part 1 (text/plain, inline)]
>> In `.dir-locals.el' setting `((asm-mode . ((asm-comment-char . 35))))'
>> does not have the desired effect.  The comment char is still `;' instead
>> of `#'.  The reason is that when the syntax table is adjusted according
>> to the comment char in the body of `asm-mode', the dir-local variables
>> haven't been loaded yet: `run-mode-hooks' (which would call
>> `hack-local-variables') is only invoked _after_ the `define-derived-mode'
>> body, i.e., too late.
>> 
>> I'm using the following workaround:
>> 
>>     (defun asm-mode-set-comment-char-from-dir-local-variables ()
>>       "Load `asm-comment-char' from dir-local variables.
>>     This is a HACK, since in `asm-mode', `run-mode-hooks' (which would call
>>     `hack-local-variables') is only invoked after the `define-derived-mode' body,
>>     i.e., too late."
>>       (let ((enable-local-variables :safe))
>>         (hack-dir-local-variables)
>>         (when-let* ((char (alist-get 'asm-comment-char dir-local-variables-alist)))
>>           (setq-local asm-comment-char char))))
>>     (add-hook 'asm-mode-set-comment-hook #'asm-mode-set-comment-char-from-dir-local-variables)
>> 
>> But maybe there is a more principled approach?
>
> Stefan, any suggestions?

IIRC there are 2 "standard" solutions:

- in `asm-mode` use

      (add-hook 'hack-local-variables-hook #'asm--set-comment-syntax nil t)

  where `asm--set-comment-syntax` would be a new function that sets the
  syntax-table according to `asm-comment-char`.

- in `asm-mode` move the syntax-table setting code to an `:after-hook`
  section, as in the patch below.


        Stefan
[asm.patch (text/x-diff, inline)]
diff --git a/lisp/progmodes/asm-mode.el b/lisp/progmodes/asm-mode.el
index d47c525c5f9..76defbf6ac8 100644
--- a/lisp/progmodes/asm-mode.el
+++ b/lisp/progmodes/asm-mode.el
@@ -125,25 +125,29 @@ asm-mode
 
 Special commands:
 \\{asm-mode-map}"
+  :after-hook
+  (progn
+    (run-hooks 'asm-mode-set-comment-hook)
+    ;; Make our own local child of `asm-mode-map'
+    ;; so we can define our own comment character.
+    (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
+    (local-set-key (vector asm-comment-char) #'asm-comment)
+    (set-syntax-table (make-syntax-table asm-mode-syntax-table))
+    (modify-syntax-entry	asm-comment-char "< b")
+
+    (setq-local comment-start (string asm-comment-char)))
+
   (setq local-abbrev-table asm-mode-abbrev-table)
   (setq-local font-lock-defaults '(asm-font-lock-keywords))
   (setq-local indent-line-function #'asm-indent-line)
   ;; Stay closer to the old TAB behavior (was tab-to-tab-stop).
   (setq-local tab-always-indent nil)
 
-  (run-hooks 'asm-mode-set-comment-hook)
-  ;; Make our own local child of `asm-mode-map'
-  ;; so we can define our own comment character.
-  (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
-  (local-set-key (vector asm-comment-char) #'asm-comment)
-  (set-syntax-table (make-syntax-table asm-mode-syntax-table))
-  (modify-syntax-entry	asm-comment-char "< b")
-
-  (setq-local comment-start (string asm-comment-char))
   (setq-local comment-add 1)
   (setq-local comment-start-skip "\\(?:\\s<+\\|/[/*]+\\)[ \t]*")
   (setq-local comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)")
-  (setq-local comment-end ""))
+  (setq-local comment-end "")
+  ))
 
 (defun asm-indent-line ()
   "Auto-indent the current line."

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#74447; Package emacs. (Sat, 30 Nov 2024 10:05:02 GMT) Full text and rfc822 format available.

Message #14 received at 74447 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: johann <at> jklaehn.de, Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 74447 <at> debbugs.gnu.org
Subject: Re: bug#74447: 30.0.92; asm-comment-char cannot be set via
 dir-local variables
Date: Sat, 30 Nov 2024 12:04:01 +0200
Ping!  Johann, could you please try Stefan's patch below and see if it
solves the problem?

> From: Stefan Monnier <monnier <at> iro.umontreal.ca>
> Cc: Johann Klähn <johann <at> jklaehn.de>,
>   74447 <at> debbugs.gnu.org
> Date: Wed, 20 Nov 2024 14:36:42 -0500
> 
> >> In `.dir-locals.el' setting `((asm-mode . ((asm-comment-char . 35))))'
> >> does not have the desired effect.  The comment char is still `;' instead
> >> of `#'.  The reason is that when the syntax table is adjusted according
> >> to the comment char in the body of `asm-mode', the dir-local variables
> >> haven't been loaded yet: `run-mode-hooks' (which would call
> >> `hack-local-variables') is only invoked _after_ the `define-derived-mode'
> >> body, i.e., too late.
> >> 
> >> I'm using the following workaround:
> >> 
> >>     (defun asm-mode-set-comment-char-from-dir-local-variables ()
> >>       "Load `asm-comment-char' from dir-local variables.
> >>     This is a HACK, since in `asm-mode', `run-mode-hooks' (which would call
> >>     `hack-local-variables') is only invoked after the `define-derived-mode' body,
> >>     i.e., too late."
> >>       (let ((enable-local-variables :safe))
> >>         (hack-dir-local-variables)
> >>         (when-let* ((char (alist-get 'asm-comment-char dir-local-variables-alist)))
> >>           (setq-local asm-comment-char char))))
> >>     (add-hook 'asm-mode-set-comment-hook #'asm-mode-set-comment-char-from-dir-local-variables)
> >> 
> >> But maybe there is a more principled approach?
> >
> > Stefan, any suggestions?
> 
> IIRC there are 2 "standard" solutions:
> 
> - in `asm-mode` use
> 
>       (add-hook 'hack-local-variables-hook #'asm--set-comment-syntax nil t)
> 
>   where `asm--set-comment-syntax` would be a new function that sets the
>   syntax-table according to `asm-comment-char`.
> 
> - in `asm-mode` move the syntax-table setting code to an `:after-hook`
>   section, as in the patch below.
> 
> 
>         Stefan
> 
> diff --git a/lisp/progmodes/asm-mode.el b/lisp/progmodes/asm-mode.el
> index d47c525c5f9..76defbf6ac8 100644
> --- a/lisp/progmodes/asm-mode.el
> +++ b/lisp/progmodes/asm-mode.el
> @@ -125,25 +125,29 @@ asm-mode
>  
>  Special commands:
>  \\{asm-mode-map}"
> +  :after-hook
> +  (progn
> +    (run-hooks 'asm-mode-set-comment-hook)
> +    ;; Make our own local child of `asm-mode-map'
> +    ;; so we can define our own comment character.
> +    (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
> +    (local-set-key (vector asm-comment-char) #'asm-comment)
> +    (set-syntax-table (make-syntax-table asm-mode-syntax-table))
> +    (modify-syntax-entry	asm-comment-char "< b")
> +
> +    (setq-local comment-start (string asm-comment-char)))
> +
>    (setq local-abbrev-table asm-mode-abbrev-table)
>    (setq-local font-lock-defaults '(asm-font-lock-keywords))
>    (setq-local indent-line-function #'asm-indent-line)
>    ;; Stay closer to the old TAB behavior (was tab-to-tab-stop).
>    (setq-local tab-always-indent nil)
>  
> -  (run-hooks 'asm-mode-set-comment-hook)
> -  ;; Make our own local child of `asm-mode-map'
> -  ;; so we can define our own comment character.
> -  (use-local-map (nconc (make-sparse-keymap) asm-mode-map))
> -  (local-set-key (vector asm-comment-char) #'asm-comment)
> -  (set-syntax-table (make-syntax-table asm-mode-syntax-table))
> -  (modify-syntax-entry	asm-comment-char "< b")
> -
> -  (setq-local comment-start (string asm-comment-char))
>    (setq-local comment-add 1)
>    (setq-local comment-start-skip "\\(?:\\s<+\\|/[/*]+\\)[ \t]*")
>    (setq-local comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)")
> -  (setq-local comment-end ""))
> +  (setq-local comment-end "")
> +  ))
>  
>  (defun asm-indent-line ()
>    "Auto-indent the current line."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#74447; Package emacs. (Sat, 30 Nov 2024 12:19:01 GMT) Full text and rfc822 format available.

Message #17 received at 74447 <at> debbugs.gnu.org (full text, mbox):

From: Johann Klähn <johann <at> jklaehn.de>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 74447 <at> debbugs.gnu.org, Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#74447: 30.0.92; asm-comment-char cannot be set via
 dir-local variables
Date: Sat, 30 Nov 2024 13:18:04 +0100
Thanks for the ping, Eli.  After fixing up the parens added by mistake
in the second part of the patch it works great.  Thanks, Stefan!

>> From: Stefan Monnier <monnier <at> iro.umontreal.ca>
>> Date: Wed, 20 Nov 2024 14:36:42 -0500
>>    (setq-local comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)")
>> -  (setq-local comment-end ""))
>> +  (setq-local comment-end "")
>> +  ))
>>  

⬑ I reverted this part.




Reply sent to Stefan Monnier <monnier <at> iro.umontreal.ca>:
You have taken responsibility. (Mon, 02 Dec 2024 00:05:02 GMT) Full text and rfc822 format available.

Notification sent to Johann Klähn <johann <at> jklaehn.de>:
bug acknowledged by developer. (Mon, 02 Dec 2024 00:05:02 GMT) Full text and rfc822 format available.

Message #22 received at 74447-done <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Johann Klähn <johann <at> jklaehn.de>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 74447-done <at> debbugs.gnu.org
Subject: Re: bug#74447: 30.0.92; asm-comment-char cannot be set via
 dir-local variables
Date: Sun, 01 Dec 2024 19:04:50 -0500
> Thanks for the ping, Eli.  After fixing up the parens added by mistake
> in the second part of the patch it works great.  Thanks, Stefan!

Great, thank you, pushed to `master`.

>>>    (setq-local comment-end-skip "[ \t]*\\(\\s>\\|\\*+/\\)")
>>> -  (setq-local comment-end ""))
>>> +  (setq-local comment-end "")
>>> +  ))
>
> ⬑ I reverted this part.

Rightly so!
Closing,


        Stefan





bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Mon, 30 Dec 2024 12:24:11 GMT) Full text and rfc822 format available.

This bug report was last modified 264 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.