unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#25082: [PATCH] Add support to emacsclient for command-lline options in ALTERNATE_EDITOR/--alternate-editor
@ 2016-12-01 15:31 Reuben Thomas
  2016-12-02 10:20 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Reuben Thomas @ 2016-12-01 15:31 UTC (permalink / raw)
  To: 25082


[-- Attachment #1.1: Type: text/plain, Size: 540 bytes --]

The attached patch adds support to emacsclient for command-line options
when specifying the alternate editor, so that for example one can now say:

ALTERNATE_EDITOR="emacs -Q -nw"

I have added documentation to NEWS. It did not seem necessary to change the
man page, as nowhere does it imply that you can't do this, and I
implemented the feature when I found that it didn't work as I expected;
other similar environment variables with which users may be familiar, such
as EDITOR and VISUAL, already work like this.

-- 
http://rrt.sc3d.org

[-- Attachment #1.2: Type: text/html, Size: 1005 bytes --]

[-- Attachment #2: 0005-Add-support-for-arguments-in-ALTERNATE_EDITOR-to-ema.patch --]
[-- Type: text/x-patch, Size: 4237 bytes --]

From 20f8b12cb667d99bd62dda338558c797f29f6861 Mon Sep 17 00:00:00 2001
From: Reuben Thomas <rrt@sc3d.org>
Date: Thu, 1 Dec 2016 15:21:57 +0000
Subject: [PATCH 5/5] Add support for arguments in ALTERNATE_EDITOR to
 emacsclient

* lib-src/emacsclient.c (fail): Parse ALTERNATE_EDITOR, or
corresponding command-line argument, into space-separated tokens.
* etc/NEWS: Document.
---
 etc/NEWS              |  4 +++
 lib-src/emacsclient.c | 73 ++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 59 insertions(+), 18 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 33b8a42..c4f4569 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -268,6 +268,10 @@ variable of this kind to swap modifiers in Emacs.
 ---
 ** New input methods: 'cyrillic-tuvan', 'polish-prefix'.
 
+++
+** emacsclient now accepts command-line options in ALTERNATE_EDITOR
+and --alternate-editor. For example, ALTERNATE_EDITOR="emacs -Q -nw".
+
 \f
 * Editing Changes in Emacs 26.1
 
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 2909d63..b561db6 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -109,6 +109,9 @@ char *w32_getenv (const char *);
 /* Name used to invoke this program.  */
 const char *progname;
 
+/* The first argument to main.  */
+int main_argc;
+
 /* The second argument to main.  */
 char **main_argv;
 
@@ -192,6 +195,35 @@ xmalloc (size_t size)
   return result;
 }
 
+/* Like realloc but get fatal error if memory is exhausted.  */
+
+static void *
+xrealloc (void *ptr, size_t size)
+{
+  void *result = realloc (ptr, size);
+  if (result == NULL)
+    {
+      perror ("realloc");
+      exit (EXIT_FAILURE);
+    }
+  return result;
+}
+
+/* Like strdup but get a fatal error if memory is exhausted. */
+char *xstrdup (const char *);
+
+char *
+xstrdup (const char *s)
+{
+  char *result = strdup (s);
+  if (result == NULL)
+    {
+      perror ("strdup");
+      exit (EXIT_FAILURE);
+    }
+  return result;
+}
+
 /* From sysdep.c */
 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
 
@@ -255,21 +287,6 @@ get_current_dir_name (void)
 
 #ifdef WINDOWSNT
 
-/* Like strdup but get a fatal error if memory is exhausted. */
-char *xstrdup (const char *);
-
-char *
-xstrdup (const char *s)
-{
-  char *result = strdup (s);
-  if (result == NULL)
-    {
-      perror ("strdup");
-      exit (EXIT_FAILURE);
-    }
-  return result;
-}
-
 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
 
 char *w32_get_resource (HKEY, const char *, LPDWORD);
@@ -651,7 +668,7 @@ Report bugs with M-x report-emacs-bug.\n");
 }
 
 /* Try to run a different command, or --if no alternate editor is
-   defined-- exit with an errorcode.
+   defined-- exit with an decoderde.
    Uses argv, but gets it from the global variable main_argv.  */
 
 static _Noreturn void
@@ -659,9 +676,27 @@ fail (void)
 {
   if (alternate_editor)
     {
-      int i = optind - 1;
+      size_t extra_args_size = (main_argc - optind + 1) * sizeof (char *);
+      size_t new_argv_size = extra_args_size;
+      char **new_argv = NULL;
+      /* Needed because strtok overwrites its input.  */
+      char *s = xstrdup (alternate_editor);
+      unsigned toks = 0;
+      char *tok = strtok(s, " ");
+
+      /* Unpack alternate_editor's space-separated tokens into new_argv.  */
+      do
+        {
+          toks++;
+          new_argv = xrealloc (new_argv, new_argv_size + toks * sizeof (char *));
+          new_argv[toks - 1] = tok;
+        }
+      while ((tok = strtok (NULL, " ")));
+
+      /* Append main_argv arguments to new_argv.  */
+      memcpy (&new_argv[toks], main_argv + optind, extra_args_size);
 
-      execvp (alternate_editor, main_argv + i);
+      execvp (s, new_argv);
       message (true, "%s: error executing alternate editor \"%s\"\n",
 	       progname, alternate_editor);
     }
@@ -674,6 +709,7 @@ fail (void)
 int
 main (int argc, char **argv)
 {
+  main_argc = argc;
   main_argv = argv;
   progname = argv[0];
   message (true, "%s: Sorry, the Emacs server is supported only\n"
@@ -1607,6 +1643,7 @@ main (int argc, char **argv)
   int start_daemon_if_needed;
   int exit_status = EXIT_SUCCESS;
 
+  main_argc = argc;
   main_argv = argv;
   progname = argv[0];
 
-- 
2.7.4


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

end of thread, other threads:[~2017-08-30 21:02 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-01 15:31 bug#25082: [PATCH] Add support to emacsclient for command-lline options in ALTERNATE_EDITOR/--alternate-editor Reuben Thomas
2016-12-02 10:20 ` Eli Zaretskii
2016-12-02 15:31   ` Reuben Thomas
2016-12-08 23:10 ` Glenn Morris
2016-12-09 13:44   ` Reuben Thomas
2017-08-20 21:08     ` Reuben Thomas
2017-08-22  0:16       ` Glenn Morris
2017-08-22  0:23         ` Reuben Thomas
2017-08-22  0:52           ` Reuben Thomas
2017-08-23 22:59   ` Reuben Thomas
2017-08-27 14:55     ` Eli Zaretskii
2017-08-28 10:15       ` Reuben Thomas
2017-08-29 15:43         ` Eli Zaretskii
2017-08-29 15:49           ` Reuben Thomas
2017-08-29 16:49             ` Eli Zaretskii
2017-08-29 22:29               ` Reuben Thomas
2017-08-30 16:48                 ` Eli Zaretskii
2017-08-30 21:01                   ` Reuben Thomas
2017-08-30 21:02 ` bug#25082: Patch installed Reuben Thomas

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