Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Start Emacs with a minimal bash and emacs lisp mix script for testing.

25 views
Skip to first unread message

hongy...@gmail.com

unread,
Aug 3, 2021, 3:16:30 AM8/3/21
to
On Ubuntu 20.04, I'm using the following minimal bash and emacs lisp mix script to test/debug specific Emacs configurations:
----------- begin --------------
#!/usr/bin/env bash
:; set -e # -*- mode: emacs-lisp; lexical-binding: t -*-
:; # https://github.com/abo-abo/swiper/issues/2899#issuecomment-889926472
:; # https://github.com/hlissner/doom-emacs/blob/develop/bin/doom
:; emacs -Q --load "$(realpath -e $0)" -- "$@"


;;Bootstrap straight
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(straight-use-package 'use-package)
(setq straight-use-package-by-default t)
[...]
----------- end --------------

I find the above script can start Emacs successfully with the following calling method:

$ bash the-bash-elisp-mix-script

But when quit Emacs with `C-x C-c', the script always complains the following info:

init.the-bash-elisp-mix-script: line 7: syntax error near unexpected token `;;'


OTOH, if I change the Emacs command line in the above script into the following:

:; emacs -Q --script "$(realpath -e $0)" -- "$@"

The script will refuse to start, and the following info will be triggered immediately:

$ bash init.the-bash-elisp-mix-script
init.the-bash-elisp-mix-script: line 7: syntax error near unexpected token `;;'
$

The above idea is based on code example given on <https://github.com/hlissner/doom-emacs/blob/develop/bin/doom>.

Any hints for fixing these problems?

Regards,
HY

Helmut Waitzmann

unread,
Aug 3, 2021, 1:21:32 PM8/3/21
to
"hongy...@gmail.com" <hongy...@gmail.com>:

[An 123 characters long line deleted]


Please make use of your return key to avoid overly long lines
(maximum of 78 characters) when posting via the google web.

>----------- begin --------------
>#!/usr/bin/env bash
>:; set -e # -*- mode: emacs-lisp; lexical-binding: t -*-
>:; # https://github.com/abo-abo/swiper/issues/2899#issuecomment-889926472
>:; # https://github.com/hlissner/doom-emacs/blob/develop/bin/doom
>:; emacs -Q --load "$(realpath -e $0)" -- "$@"
>
>
>;;Bootstrap straight
>(defvar bootstrap-version)

[…]

>----------- end --------------

[Another overly long line deleted]

>
>$ bash the-bash-elisp-mix-script
>
[Another overly long line deleted]


>init.the-bash-elisp-mix-script: line 7: syntax error near
>unexpected token `;;'

[…]

>The above idea is based on code example given on
><https://github.com/hlissner/doom-emacs/blob/develop/bin/doom>.
>
>Any hints for fixing these problems?
>

Yes.  Answer the following question:  What complains about the token
';;' in line 7 and why does it complain?

Your question reveals, that you don't have any clue about the
mechanism of the script, so the real answer should be:  If you don't
understand it, don't run it.


You might try


:; exec emacs -Q --load "$(realpath -e $0)" -- "$@"

rather than


:; emacs -Q --load "$(realpath -e $0)" -- "$@"

in that script and see whether that works for you.

hongy...@gmail.com

unread,
Aug 3, 2021, 8:53:34 PM8/3/21
to
On Wednesday, August 4, 2021 at 1:21:32 AM UTC+8, Helmut Waitzmann wrote:
> "hongy...@gmail.com" <hongy...@gmail.com>:
>
> [An 123 characters long line deleted]
>
>
> Please make use of your return key to avoid overly long lines
> (maximum of 78 characters) when posting via the google web.

What about the long URL? Does this break the URL auto recognition by web browser?

> >----------- begin --------------
> >#!/usr/bin/env bash
> >:; set -e # -*- mode: emacs-lisp; lexical-binding: t -*-
> >:; # https://github.com/abo-abo/swiper/issues/2899#issuecomment-889926472
> >:; # https://github.com/hlissner/doom-emacs/blob/develop/bin/doom
> >:; emacs -Q --load "$(realpath -e $0)" -- "$@"
> >
> >
> >;;Bootstrap straight
> >(defvar bootstrap-version)
> […]
>
> >----------- end --------------
>
> [Another overly long line deleted]
>
> >
> >$ bash the-bash-elisp-mix-script
> >
> [Another overly long line deleted]
> >init.the-bash-elisp-mix-script: line 7: syntax error near
> >unexpected token `;;'
> […]
> >The above idea is based on code example given on
> ><https://github.com/hlissner/doom-emacs/blob/develop/bin/doom>.
> >
> >Any hints for fixing these problems?
> >
> Yes. Answer the following question: What complains about the token
> ';;' in line 7 and why does it complain?

Got it. They are not valid bash command sequences. See the following testings:

$ ;;
bash: syntax error near unexpected token `;;'
$ ;
bash: syntax error near unexpected token `;'
$ :;
$

> Your question reveals, that you don't have any clue about the
> mechanism of the script, so the real answer should be: If you don't
> understand it, don't run it.
>
>
> You might try
>
>
> :; exec emacs -Q --load "$(realpath -e $0)" -- "$@"

Yes, it does the trick, as well as the following one:

:; emacs -Q --load "$(realpath -e $0)" -- "$@"; exit

Another question: how to do the same job with the `--script' option of
Emacs? I've tried with the following command, but failed to start
Emacs:

:; exec emacs -Q --script $(realpath -e $0) -- "$@"

Javier

unread,
Aug 4, 2021, 7:24:38 AM8/4/21
to
hongy...@gmail.com <hongy...@gmail.com> wrote:
>> Please make use of your return key to avoid overly long lines
>> (maximum of 78 characters) when posting via the google web.
>
> What about the long URL? Does this break the URL auto recognition by
> web browser?

The long URLS you don't need to break, but please break the text
pararagraphs at 72 chars (or at maximum 78).

> Another question: how to do the same job with the `--script' option of
> Emacs? I've tried with the following command, but failed to start
> Emacs:
>
> :; exec emacs -Q --script $(realpath -e $0) -- "$@"
>
>> rather than
>> :; emacs -Q --load "$(realpath -e $0)" -- "$@"
>> in that script and see whether that works for you.

You just use --script it in the shebang line with no bash code

#!/usr/bin/emacs --script
; elisp code here
; ...

But with --script emacs is executed in batch mode and does not enter
interactive mode as your previous example, so it might not be what you
are looking for.

hongy...@gmail.com

unread,
Aug 4, 2021, 10:11:57 AM8/4/21
to
On Wednesday, August 4, 2021 at 7:24:38 PM UTC+8, Javier wrote:
> hongy...@gmail.com <hongy...@gmail.com> wrote:
> >> Please make use of your return key to avoid overly long lines
> >> (maximum of 78 characters) when posting via the google web.
> >
> > What about the long URL? Does this break the URL auto recognition by
> > web browser?
> The long URLS you don't need to break, but please break the text
> pararagraphs at 72 chars (or at maximum 78).
> > Another question: how to do the same job with the `--script' option of
> > Emacs? I've tried with the following command, but failed to start
> > Emacs:
> >
> > :; exec emacs -Q --script $(realpath -e $0) -- "$@"
> >
> >> rather than
> >> :; emacs -Q --load "$(realpath -e $0)" -- "$@"
> >> in that script and see whether that works for you.
> You just use --script it in the shebang line with no bash code
>
> #!/usr/bin/emacs --script

Based on my tries, the only shebang line can't start Emacs, while the following can

#!/usr/local/bin/emacs -Q --script
or
#!/usr/local/bin/emacs -q --script

But they all will load the already existing configurations.

> ; elisp code here
> ; ...

OTOH, I also tried the following method:

------------------- begin ---------------------------
#!/usr/bin/env bash
emacs -Q --load <(tail -n +4 "$0")
exit

;;Bootstrap straight
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(straight-use-package 'use-package)
(setq straight-use-package-by-default t)
------------------- end ---------------------------

Though the above method can start Emacs, but I'll see the following info in the *Messages* buffer:

For information about GNU Emacs and the GNU system, type C-h C-a.
eval-buffer: Symbol’s function definition is void: straight-use-package


> But with --script emacs is executed in batch mode and does not enter
> interactive mode as your previous example, so it might not be what you
> are looking for.

Thank you for your explanation.

Javier

unread,
Aug 4, 2021, 12:03:15 PM8/4/21
to
hongy...@gmail.com <hongy...@gmail.com> wrote:
> On Wednesday, August 4, 2021 at 7:24:38 PM UTC+8, Javier wrote:
>> You just use --script it in the shebang line with no bash code
>>
>> #!/usr/bin/emacs --script
>
> Based on my tries, the only shebang line can't start Emacs

As I told you before '#!/usr/bin/emacs --script' will start emacs in
batch mode. This means that emacs will exectute the elisp code and
exit without asking for anything interactive. You will not see an
emacs window popping up with --script.

> while the following can
>
> #!/usr/local/bin/emacs -Q --script
>
> But they all will load the already existing configurations.

You see an emacs window that loads the existing (~/.emacs I guess)
configuration because neither of the -Q nor --script configurations

You will most likely see a error message in emacs at startup

Unknown option ‘-Q --script’

That's because the shebang line can only send 1 command-line option to
emacs. The shebang line is unable to send two options to emacs (or
any other program for the matter).

hongy...@gmail.com

unread,
Aug 4, 2021, 7:42:40 PM8/4/21
to
On Thursday, August 5, 2021 at 12:03:15 AM UTC+8, Javier wrote:
> hongy...@gmail.com <hongy...@gmail.com> wrote:
> > On Wednesday, August 4, 2021 at 7:24:38 PM UTC+8, Javier wrote:
> >> You just use --script it in the shebang line with no bash code
> >>
> >> #!/usr/bin/emacs --script
> >
> > Based on my tries, the only shebang line can't start Emacs
> As I told you before '#!/usr/bin/emacs --script' will start emacs in
> batch mode. This means that emacs will exectute the elisp code and
> exit without asking for anything interactive. You will not see an
> emacs window popping up with --script.
> > while the following can
> >
> > #!/usr/local/bin/emacs -Q --script
> >
> > But they all will load the already existing configurations.
> You see an emacs window that loads the existing (~/.emacs I guess)
> configuration because neither of the -Q nor --script configurations
>
> You will most likely see a error message in emacs at startup
>
> Unknown option ‘-Q --script’

I confirm that the following info is shown in *Messages* buffer:

command-line-1: Unknown option ‘-Q --script’

> That's because the shebang line can only send 1 command-line option to
> emacs. The shebang line is unable to send two options to emacs (or
> any other program for the matter).

Then, what about the following trick, as I've posted:


#!/usr/bin/env bash
emacs -Q --load <(tail -n +4 "$0")
exit

[elisp code comes here]

HY
0 new messages