Subject: Re: bug#65048: 29.1; cli argument "-x" fails with "unknown option" error on PGTK Emacs

From: Eli Zaretskii <[email-masked]>
Date: Fri, 04 Aug 2023 09:20:54 +0300

> From: John t <[email-masked]>
> Date: Thu, 3 Aug 2023 22:47:19 -0400
> 
> Scripts starting with "#!/usr/bin/emacs -x" do not function with PGTK Emacs.
> 
> Instead of evaluating the file contents, Emacs creates a new instance
> and opens a frame, echos a warning about "-x" being an unknown option in
> the minibuffer. This seems to suggest that the "-x" option is not being recognized at all.
> 
> The statement that parses this specific parameter seems to be inside
> of a conditionally compiled block of code that toggles based on the
> macro HAVE_X_WINDOWS (#ifdef HAVE_X_WINDOWS).
> 
> My current build of Emacs uses the PGTK, and non-PGTK builds appear to
> function correctly when used with the "-x" option.
> 
> This option doesn't seem to be related to x11 in any way as far as I can
> tell, so I have moved it to the location where the "--script" option is
> parsed, and then decrement skip_args by 2 instead of 1 which is also what
> "--script" does.
> 
> These changes cause the "-x" option to function correctly, but I am not
> experienced in C or with the C parts of Emacs, so this may not be the
> correct solution!

Thanks.  You are right: the -x switch should not be X-specific.

Please try the patch below instead (which removes some duplicate code,
but is in general the same idea as in your suggested patch).  Please
try this both in the PGTK and in non-PGTK builds, as I have only
limited access to such configurations.

diff --git a/src/emacs.c b/src/emacs.c
index e63b092..37c017d 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -2007,15 +2007,16 @@ main (int argc, char **argv)
     }
 #endif /* HAVE_NS */
 
-#ifdef HAVE_X_WINDOWS
   /* Stupid kludge to catch command-line display spec.  We can't
      handle this argument entirely in window system dependent code
      because we don't even know which window system dependent code
      to run until we've recognized this argument.  */
   {
-    char *displayname = 0;
     int count_before = skip_args;
 
+#ifdef HAVE_X_WINDOWS
+    char *displayname = 0;
+
     /* Skip any number of -d options, but only use the last one.  */
     while (!only_version)
       {
@@ -2045,12 +2046,15 @@ main (int argc, char **argv)
 	  }
 	argv[count_before + 1] = (char *) "-d";
       }
+#endif	/* !HAVE_X_WINDOWS */
 
     if (! no_site_lisp)
       {
-        if (argmatch (argv, argc, "-Q", "--quick", 3, NULL, &skip_args)
+
+	if (argmatch (argv, argc, "-Q", "--quick", 3, NULL, &skip_args)
             || argmatch (argv, argc, "-quick", 0, 2, NULL, &skip_args))
-          no_site_lisp = 1;
+	  no_site_lisp = 1;
+
       }
 
     if (argmatch (argv, argc, "-x", 0, 1, &junk, &skip_args))
@@ -2066,18 +2070,6 @@ main (int argc, char **argv)
     /* Don't actually discard this arg.  */
     skip_args = count_before;
   }
-#else  /* !HAVE_X_WINDOWS */
-  if (! no_site_lisp)
-  {
-    int count_before = skip_args;
-
-    if (argmatch (argv, argc, "-Q", "--quick", 3, NULL, &skip_args)
-        || argmatch (argv, argc, "-quick", 0, 2, NULL, &skip_args))
-      no_site_lisp = 1;
-
-    skip_args = count_before;
-  }
-#endif
 
   /* argmatch must not be used after here,
      except when building temacs