unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#74447: 30.0.92; asm-comment-char cannot be set via dir-local variables
@ 2024-11-20 19:12 Johann Klähn
  2024-11-20 19:21 ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: Johann Klähn @ 2024-11-20 19:12 UTC (permalink / raw)
  To: 74447


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





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#74447: 30.0.92; asm-comment-char cannot be set via dir-local variables
  2024-11-20 19:12 bug#74447: 30.0.92; asm-comment-char cannot be set via dir-local variables Johann Klähn
@ 2024-11-20 19:21 ` Eli Zaretskii
  2024-11-20 19:36   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2024-11-20 19:21 UTC (permalink / raw)
  To: Johann Klähn, Stefan Monnier; +Cc: 74447

> From: Johann Klähn <johann@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?





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#74447: 30.0.92; asm-comment-char cannot be set via dir-local variables
  2024-11-20 19:21 ` Eli Zaretskii
@ 2024-11-20 19:36   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-11-30 10:04     ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-11-20 19:36 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 74447, Johann Klähn

[-- Attachment #1: Type: text/plain, Size: 1613 bytes --]

>> 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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: asm.patch --]
[-- Type: text/x-diff, Size: 1711 bytes --]

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."

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* bug#74447: 30.0.92; asm-comment-char cannot be set via dir-local variables
  2024-11-20 19:36   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-11-30 10:04     ` Eli Zaretskii
  2024-11-30 12:18       ` Johann Klähn
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2024-11-30 10:04 UTC (permalink / raw)
  To: johann, Stefan Monnier; +Cc: 74447

Ping!  Johann, could you please try Stefan's patch below and see if it
solves the problem?

> From: Stefan Monnier <monnier@iro.umontreal.ca>
> Cc: Johann Klähn <johann@jklaehn.de>,
>   74447@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."





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#74447: 30.0.92; asm-comment-char cannot be set via dir-local variables
  2024-11-30 10:04     ` Eli Zaretskii
@ 2024-11-30 12:18       ` Johann Klähn
  2024-12-02  0:04         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 6+ messages in thread
From: Johann Klähn @ 2024-11-30 12:18 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 74447, Stefan Monnier

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@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.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#74447: 30.0.92; asm-comment-char cannot be set via dir-local variables
  2024-11-30 12:18       ` Johann Klähn
@ 2024-12-02  0:04         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-12-02  0:04 UTC (permalink / raw)
  To: Johann Klähn; +Cc: Eli Zaretskii, 74447-done

> 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






^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-12-02  0:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-20 19:12 bug#74447: 30.0.92; asm-comment-char cannot be set via dir-local variables Johann Klähn
2024-11-20 19:21 ` Eli Zaretskii
2024-11-20 19:36   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-11-30 10:04     ` Eli Zaretskii
2024-11-30 12:18       ` Johann Klähn
2024-12-02  0:04         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).