From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Sebastian Miele Newsgroups: gmane.emacs.bugs Subject: bug#67321: 29.1.90; Different parsing rules for -*- lexical-binding:t; -*- in different circumstances Date: Wed, 22 Nov 2023 14:07:38 +0100 Message-ID: <87cyw1gb9c.fsf@whxvd.name> References: <8734wzts37.fsf@whxvd.name> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36272"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 67321@debbugs.gnu.org To: Gerd =?UTF-8?Q?M=C3=B6llmann?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Nov 22 14:38:16 2023 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 1r5nQl-0009Fj-G4 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 22 Nov 2023 14:38:15 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r5nQX-0004sK-Au; Wed, 22 Nov 2023 08:38:01 -0500 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 1r5nQU-0004sA-Om for bug-gnu-emacs@gnu.org; Wed, 22 Nov 2023 08:37:58 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r5nQU-0007dY-GX for bug-gnu-emacs@gnu.org; Wed, 22 Nov 2023 08:37:58 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r5nQX-0002YM-Nc for bug-gnu-emacs@gnu.org; Wed, 22 Nov 2023 08:38:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Sebastian Miele Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 22 Nov 2023 13:38:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67321 X-GNU-PR-Package: emacs Original-Received: via spool by 67321-submit@debbugs.gnu.org id=B67321.17006602399764 (code B ref 67321); Wed, 22 Nov 2023 13:38:01 +0000 Original-Received: (at 67321) by debbugs.gnu.org; 22 Nov 2023 13:37:19 +0000 Original-Received: from localhost ([127.0.0.1]:58266 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r5nPq-0002XP-Pp for submit@debbugs.gnu.org; Wed, 22 Nov 2023 08:37:19 -0500 Original-Received: from mout-p-202.mailbox.org ([80.241.56.172]:36498) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r5nPo-0002XB-77 for 67321@debbugs.gnu.org; Wed, 22 Nov 2023 08:37:17 -0500 Original-Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4Sb2NJ5X9Tz9tLh; Wed, 22 Nov 2023 14:37:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=whxvd.name; s=MBO0001; t=1700660224; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1jfHFvK7kZvnfvVwtvWYieAKTeYuf8ODVgUBa/VEDt0=; b=PY8v63cg7xh2z4MutWoNG61hfSdcnQiffFUP5dj2wY/U1/Yx3Wua3YPJl0uTVo3rlW0Ts3 TPXKn1pqo8TlQg17l47yDKP+OLZraQMc7eZFUNZGWUd4CWfU5a2hqvKZTJD8BhBKLSeHLK NtDg9ejYcIraRS2GyKwuxxtsUk30qrNM919XSLGofKgmEOymXLMukE5C6wrq9fKlMeryKx sDK+4V71mhLeiIGOa+DcMuSMo5VK+AlAKZ223yFsW3fxMI2SGFZJUFUolXG3m13sFJ5Sa6 e7RxUz7OfaqqT1T2KXmKuRBIrdQGTG5dKK9f0z+MqxQs+ltj42JtwcxFCermcA== In-reply-to: X-Rspamd-Queue-Id: 4Sb2NJ5X9Tz9tLh 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: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:274754 Archived-At: > From: Gerd M=C3=B6llmann > 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. What follows is the beginning of https://github.com/doomemacs/doomemacs/blob/master/bin/doom for those who do not visit GitHub: #!/usr/bin/env sh :; # -*- mode: emacs-lisp; lexical-binding: t -*- :; case "$EMACS" in *term*) EMACS=3Demacs ;; *) EMACS=3D"${EMACS:-emacs}" ;= ; esac :; emacs=3D"$EMACS ${DEBUG:+--debug-init} -q --no-site-file --batch" :; tmpdir=3D`$emacs --eval '(princ (temporary-file-directory))' 2>/dev/null` :; [ -z "$tmpdir" ] && { >&2 echo "Error: failed to run Emacs with command = '$EMACS'"; >&2 echo; >&2 echo "Are you sure Emacs is installed and in your = \$PATH?"; exit 1; } :; export __DOOMPID=3D"${__DOOMPID:-$$}" :; export __DOOMSTEP=3D"${__DOOMSTEP:-0}" :; export __DOOMGEOM=3D"${__DOOMGEOM:-`tput cols 2>/dev/null`x`tput lines 2= >/dev/null`}" :; export __DOOMGPIPE=3D${__DOOMGPIPE:-$__DOOMPIPE} :; export __DOOMPIPE=3D; [ -t 0 ] || __DOOMPIPE=3D"${__DOOMPIPE}0"; [ -t 1 = ] || __DOOMPIPE=3D"${__DOOMPIPE}1" :; $emacs --load "$0" -- "$@" || exit=3D$? :; [ "${exit:-0}" -eq 254 ] && { sh "${tmpdir}/doom.${__DOOMPID}.${__DOOMST= EP}.sh" "$0" "$@" && true; exit=3D"$?"; } :; exit $exit ;; This magical mess of a shebang is necessary for any script that relies on ;; Doom's CLI framework, because Emacs' tty libraries and capabilities are = too ;; immature (borderline non-existent) at the time of writing (28.1). This ;; shebang sets out to accomplish these three goals: ;; ;; 1. To produce a more helpful error if Emacs isn't installed or broken. It ;; must do so without assuming whether $EMACS is a shell command (e.g. '= snap ;; run emacs') or an absolute path (to an emacs executable). I've avoided ;; 'command -v $EMACS' for this reason. ;; ;; 2. To allow this Emacs session to "exit into" a child process (since Eli= sp ;; lacks an analogue for exec system calls) by calling an auto-generated= and ;; self-destructing "exit script" if the parent Emacs process exits with= code ;; 254. It takes care to prevent nested child instances from clobbering = the ;; exit script. ;; ;; 3. To expose some information about the terminal and session: ;; - $__DOOMGEOM holds the dimensions of the terminal (W . H). ;; - $__DOOMPIPE indicates whether the script has been piped (in and/or = out). ;; - $__DOOMGPIPE indicates whether one of this process' parent has been ;; piped to/from. ;; - $__DOOMPID is a unique identifier for the parent script, so ;; child processes can identify which persistent data files (like logs= ) it ;; has access to. ;; - $__DOOMSTEP counts how many levels deep we are in the dream (append= ing ;; this to the exit script's filename avoids child processes clobberin= g the ;; same exit script and causing read errors). ;; - $TMPDIR (or $TEMP and $TMP on Windows) aren't guaranteed to have va= lues, ;; and mktemp isn't available on all systems, but you know what is? Em= acs! ;; So I use it to print `temporary-file-directory'. And it seconds as a ;; quick sanity check for Emacs' existence (for goal #1). ;; ;; Other weird facts about this shebang line: ;; ;; - The :; hack exploits properties of : and ; in shell scripting and elis= p to ;; allow shell script and elisp to coexist in the same file without eithe= r's ;; interpreter throwing foreign syntax errors: ;; ;; - In elisp, ":" is a valid keyword symbol literal; it evaluates to its= elf ;; and has no side-effect. ;; - In the shell, ":" is a valid command that does nothing and ignores i= ts ;; arguments. ;; - In elisp, ";" begins a comment. I.e. the interpreter ignores everyth= ing ;; after it. ;; - In the shell, ";" is a command separator. ;; ;; Put together, plus a strategically placed exit call, the shell will re= ad ;; one part of this file and ignore the rest, while the elisp interpreter= will ;; do the opposite. ;; - I intentionally avoid loading site files, so lisp/doom-cli.el can load= them ;; by hand later. There, I can suppress and deal with unhelpful warnings = (e.g. ;; "package cl is deprecated"), "Loading X...DONE" spam, and any other ;; disasterous side-effects. ;; ;; But be careful not to use -Q! It implies --no-site-lisp, which omits t= he ;; site-lisp directory from `load-path'. ;; - POSIX-compliancy is paramount: there's no guarantee what /bin/sh will = be ;; symlinked to in the esoteric OSes/distros Emacs users use. ;; - The user may have a noexec flag set on /tmp, so pass the exit script to ;; /bin/sh rather than executing them directly.