aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorReuben Thomas2016-12-01 15:21:57 +0000
committerReuben Thomas2017-08-07 21:57:22 +0100
commit28f1fe97daa13e13714e6c43c9a6fbb0c0e99a26 (patch)
treec8e0b5bfe7d148378238a5a1b75ffe03a8f1c654
parent7136e6723d87b51ae3089f5ceef6b14621bfaf87 (diff)
downloademacs-28f1fe97daa13e13714e6c43c9a6fbb0c0e99a26.tar.gz
emacs-28f1fe97daa13e13714e6c43c9a6fbb0c0e99a26.zip
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. * test/lib-src/emacsclient-tests.el: Add a test.
-rw-r--r--etc/NEWS4
-rw-r--r--lib-src/emacsclient.c73
-rw-r--r--test/lib-src/emacsclient-tests.el33
3 files changed, 92 insertions, 18 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 58b08348b10..f18837adfc9 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -505,6 +505,10 @@ Linum mode and all similar packages are henceforth becoming obsolete.
505Users and developers are encouraged to switch to this new feature 505Users and developers are encouraged to switch to this new feature
506instead. 506instead.
507 507
508+++
509** emacsclient now accepts command-line options in ALTERNATE_EDITOR
510and --alternate-editor. For example, ALTERNATE_EDITOR="emacs -Q -nw".
511
508 512
509* Editing Changes in Emacs 26.1 513* Editing Changes in Emacs 26.1
510 514
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index f1d4e8976da..32b8c034ae7 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -110,6 +110,9 @@ char *w32_getenv (const char *);
110/* Name used to invoke this program. */ 110/* Name used to invoke this program. */
111const char *progname; 111const char *progname;
112 112
113/* The first argument to main. */
114int main_argc;
115
113/* The second argument to main. */ 116/* The second argument to main. */
114char **main_argv; 117char **main_argv;
115 118
@@ -201,6 +204,35 @@ xmalloc (size_t size)
201 return result; 204 return result;
202} 205}
203 206
207/* Like realloc but get fatal error if memory is exhausted. */
208
209static void *
210xrealloc (void *ptr, size_t size)
211{
212 void *result = realloc (ptr, size);
213 if (result == NULL)
214 {
215 perror ("realloc");
216 exit (EXIT_FAILURE);
217 }
218 return result;
219}
220
221/* Like strdup but get a fatal error if memory is exhausted. */
222char *xstrdup (const char *);
223
224char *
225xstrdup (const char *s)
226{
227 char *result = strdup (s);
228 if (result == NULL)
229 {
230 perror ("strdup");
231 exit (EXIT_FAILURE);
232 }
233 return result;
234}
235
204/* From sysdep.c */ 236/* From sysdep.c */
205#if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME) 237#if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
206 238
@@ -264,21 +296,6 @@ get_current_dir_name (void)
264 296
265#ifdef WINDOWSNT 297#ifdef WINDOWSNT
266 298
267/* Like strdup but get a fatal error if memory is exhausted. */
268char *xstrdup (const char *);
269
270char *
271xstrdup (const char *s)
272{
273 char *result = strdup (s);
274 if (result == NULL)
275 {
276 perror ("strdup");
277 exit (EXIT_FAILURE);
278 }
279 return result;
280}
281
282#define REG_ROOT "SOFTWARE\\GNU\\Emacs" 299#define REG_ROOT "SOFTWARE\\GNU\\Emacs"
283 300
284char *w32_get_resource (HKEY, const char *, LPDWORD); 301char *w32_get_resource (HKEY, const char *, LPDWORD);
@@ -673,7 +690,7 @@ Report bugs with M-x report-emacs-bug.\n");
673} 690}
674 691
675/* Try to run a different command, or --if no alternate editor is 692/* Try to run a different command, or --if no alternate editor is
676 defined-- exit with an errorcode. 693 defined-- exit with an decoderde.
677 Uses argv, but gets it from the global variable main_argv. */ 694 Uses argv, but gets it from the global variable main_argv. */
678 695
679static _Noreturn void 696static _Noreturn void
@@ -681,9 +698,27 @@ fail (void)
681{ 698{
682 if (alternate_editor) 699 if (alternate_editor)
683 { 700 {
684 int i = optind - 1; 701 size_t extra_args_size = (main_argc - optind + 1) * sizeof (char *);
702 size_t new_argv_size = extra_args_size;
703 char **new_argv = NULL;
704 /* Needed because strtok overwrites its input. */
705 char *s = xstrdup (alternate_editor);
706 unsigned toks = 0;
707 char *tok = strtok(s, " ");
708
709 /* Unpack alternate_editor's space-separated tokens into new_argv. */
710 do
711 {
712 toks++;
713 new_argv = xrealloc (new_argv, new_argv_size + toks * sizeof (char *));
714 new_argv[toks - 1] = tok;
715 }
716 while ((tok = strtok (NULL, " ")));
717
718 /* Append main_argv arguments to new_argv. */
719 memcpy (&new_argv[toks], main_argv + optind, extra_args_size);
685 720
686 execvp (alternate_editor, main_argv + i); 721 execvp (s, new_argv);
687 message (true, "%s: error executing alternate editor \"%s\"\n", 722 message (true, "%s: error executing alternate editor \"%s\"\n",
688 progname, alternate_editor); 723 progname, alternate_editor);
689 } 724 }
@@ -696,6 +731,7 @@ fail (void)
696int 731int
697main (int argc, char **argv) 732main (int argc, char **argv)
698{ 733{
734 main_argc = argc;
699 main_argv = argv; 735 main_argv = argv;
700 progname = argv[0]; 736 progname = argv[0];
701 message (true, "%s: Sorry, the Emacs server is supported only\n" 737 message (true, "%s: Sorry, the Emacs server is supported only\n"
@@ -1629,6 +1665,7 @@ main (int argc, char **argv)
1629 int start_daemon_if_needed; 1665 int start_daemon_if_needed;
1630 int exit_status = EXIT_SUCCESS; 1666 int exit_status = EXIT_SUCCESS;
1631 1667
1668 main_argc = argc;
1632 main_argv = argv; 1669 main_argv = argv;
1633 progname = argv[0]; 1670 progname = argv[0];
1634 1671
diff --git a/test/lib-src/emacsclient-tests.el b/test/lib-src/emacsclient-tests.el
new file mode 100644
index 00000000000..b06a6f57fee
--- /dev/null
+++ b/test/lib-src/emacsclient-tests.el
@@ -0,0 +1,33 @@
1;;; process-tests.el --- Test emacsclient
2
3;; Copyright (C) 2016 Free Software Foundation, Inc.
4
5;; This program is free software; you can redistribute it and/or modify
6;; it under the terms of the GNU General Public License as published by
7;; the Free Software Foundation, either version 3 of the License, or
8;; (at your option) any later version.
9
10;; This program is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;; GNU General Public License for more details.
14
15;; You should have received a copy of the GNU General Public License
16;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18;;; Commentary:
19
20;;
21
22;;; Code:
23
24(require 'ert)
25
26(ert-deftest emacsclient-test-alternate-editor-allows-arguments ()
27 (setenv "ALTERNATE_EDITOR" "emacs --batch")
28 (should
29 (= 0
30 (call-process "emacsclient" nil nil nil "foo"))))
31
32(provide 'emacsclient-tests)
33;; emacsclient-tests.el ends here.