Subject: bug#67321: 29.1.90; Different parsing rules for -*- lexical-binding:t; -*- in different circumstances

From: Gerd Möllmann <[email-masked]>
Date: Wed, 22 Nov 2023 15:59:15 +0100

Sebastian Miele <[email-masked]> writes:

>> From: Gerd Möllmann <[email-masked]>
>> Date: Tue, 2023-11-21 11:46 +0100
>>
>>>   #!/bin/sh
>>>   : ; exec emacs --script "$0" -- "$@" #; -*- lexical-binding: t; mode: emacs-lisp; -*-
>>>
>>>   (defmacro lexical-binding-p ()
>>>     '(let* ((x t)
>>>             (f (lambda () x))
>>>             (x nil))
>>>        (funcall f)))
>>>
>>>   (message "%s %s" lexical-binding (lexical-binding-p))
>>
>> Can I ask why that idiom is used?
>
> The idiom allows arbitrary shell processing before, and possibly even
> after, actually running Emacs in the script.
>
> See https://github.com/doomemacs/doomemacs/blob/master/bin/doom for an
> example including both pre- and postprocessing, and lots of comments
> about what is done there, and why.
>
> Another case that recently popped up on emacs-devel is that it allows to
> insert the "--" for cleanly separating the script commandline options
> from options that Emacs may interpret, see
> https://lists.gnu.org/archive/html/emacs-devel/2023-11/msg00896.html.
>
> I do not know anymore why I considered the trick in the past.  I do not
> want to spend the time to look into my scripts to find that bits that
> would become cumbersome without such tricks.  Because of the problem
> with lexical-binding not being picked up, and because I, for now, have
> the luxury of only using Linux, I can use another trick.  But being able
> to mangle the command line of Emacs before running Emacs definitely is a
> useful thing.  The portable alternative would be to have one separate
> (shell) wrapper around every Emacs script that needs such mangling.

Thanks, Sebastian. I got the trick, though it took me a minute. Clever,
clever :-).

TBH, I don't see a good reason why that should be used instead of the
much simpler

cat >somefile <<EOF
; -*- lexical-binding: t -*-
(message "%s" lexical-binding)
EOF
emacs --script somefile

So, I personally would not like to spend the time to change Emacs,
sorry.