emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] emacs lib-src/emacsclient.c lib-src/ChangeLog d...


From: Dan Nicolaescu
Subject: [Emacs-diffs] emacs lib-src/emacsclient.c lib-src/ChangeLog d...
Date: Wed, 10 Dec 2008 07:56:52 +0000

CVSROOT:        /cvsroot/emacs
Module name:    emacs
Changes by:     Dan Nicolaescu <dann>   08/12/10 07:56:52

Modified files:
        lib-src        : emacsclient.c ChangeLog 
        doc/man        : emacsclient.1 ChangeLog 
        doc/emacs      : misc.texi ChangeLog 

Log message:
        * misc.texi (emacsclient Options): Describe what an empty string
        argument does for --alternate-editor.
        
        * emacsclient.1: Describe what an empty string argument does for
        --alternate-editor.
        
        * emacsclient.c (print_help_and_exit): Describe what an empty
        string argument does for --alternate-editor.
        (set_socket): Make it possible to not exit in case of an error.
        (start_daemon_and_retry_set_socket): New function.
        (main): Use it.  Restore the NULL value for socket_name and
        server_file after the set_socket call.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/emacs/lib-src/emacsclient.c?cvsroot=emacs&r1=1.142&r2=1.143
http://cvs.savannah.gnu.org/viewcvs/emacs/lib-src/ChangeLog?cvsroot=emacs&r1=2.499&r2=2.500
http://cvs.savannah.gnu.org/viewcvs/emacs/doc/man/emacsclient.1?cvsroot=emacs&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/emacs/doc/man/ChangeLog?cvsroot=emacs&r1=1.12&r2=1.13
http://cvs.savannah.gnu.org/viewcvs/emacs/doc/emacs/misc.texi?cvsroot=emacs&r1=1.16&r2=1.17
http://cvs.savannah.gnu.org/viewcvs/emacs/doc/emacs/ChangeLog?cvsroot=emacs&r1=1.247&r2=1.248

Patches:
Index: lib-src/emacsclient.c
===================================================================
RCS file: /cvsroot/emacs/emacs/lib-src/emacsclient.c,v
retrieving revision 1.142
retrieving revision 1.143
diff -u -b -r1.142 -r1.143
--- lib-src/emacsclient.c       3 Dec 2008 04:33:44 -0000       1.142
+++ lib-src/emacsclient.c       10 Dec 2008 07:56:51 -0000      1.143
@@ -616,6 +616,8 @@
                        Set filename of the TCP authentication file\n\
 -a, --alternate-editor=EDITOR\n\
                        Editor to fallback to if the server is not running\n\
+                        If EDITOR is the empty string, start Emacs in daemon\n\
+                        mode and try connecting again
 \n\
 Report bugs to address@hidden", progname);
   exit (EXIT_SUCCESS);
@@ -1294,7 +1296,7 @@
 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
 
 HSOCKET
-set_socket ()
+set_socket (int no_exit_if_error)
 {
   HSOCKET s;
 
@@ -1305,7 +1307,7 @@
   if (socket_name)
     {
       s = set_local_socket ();
-      if ((s != INVALID_SOCKET) || alternate_editor)
+      if ((s != INVALID_SOCKET) || no_exit_if_error)
        return s;
       message (TRUE, "%s: error accessing socket \"%s\"\n",
               progname, socket_name);
@@ -1320,7 +1322,7 @@
   if (server_file)
     {
       s = set_tcp_socket ();
-      if ((s != INVALID_SOCKET) || alternate_editor)
+      if ((s != INVALID_SOCKET) || no_exit_if_error)
        return s;
 
       message (TRUE, "%s: error accessing server file \"%s\"\n",
@@ -1338,7 +1340,7 @@
   /* Implicit server file.  */
   server_file = "server";
   s = set_tcp_socket ();
-  if ((s != INVALID_SOCKET) || alternate_editor)
+  if ((s != INVALID_SOCKET) || no_exit_if_error)
     return s;
 
   /* No implicit or explicit socket, and no alternate editor.  */
@@ -1408,6 +1410,52 @@
 }
 #endif
 
+
+/* Start the emacs daemon and try to connect to it.  */
+
+void
+start_daemon_and_retry_set_socket (void)
+{
+  pid_t dpid;
+  int status;
+  pid_t p;
+
+  dpid = fork ();
+
+  if (dpid > 0)
+    {
+      p = waitpid (dpid, &status, WUNTRACED | WCONTINUED);
+
+      /* Try connecting again, the daemon should have started by
+        now.  */
+      message (TRUE, "daemon should have started, trying to connect again\n", 
dpid);
+      if ((emacs_socket = set_socket (1)) == INVALID_SOCKET)
+       message (TRUE, "Cannot connect even after starting the daemon\n");
+    }
+  else if (dpid < 0)
+    {
+      fprintf (stderr, "Cannot fork!\n");
+      exit (1);
+    }
+  else
+    {
+      char *d_argv[] = {"emacs", "--daemon", 0 };
+      if (socket_name != NULL)
+       {
+         /* Pass  --daemon=socket_name as argument.  */
+         char *deq = "--daemon=";
+         char *daemon_arg = alloca (strlen (deq)
+                                    + strlen (socket_name) + 1);
+         strcpy (daemon_arg, deq);
+         strcat (daemon_arg, socket_name);
+         d_argv[1] = daemon_arg;
+       }
+      execvp ("emacs", d_argv);
+      message (TRUE, "%s: error starting emacs daemon\n", progname);
+    }
+}
+
+
 int
 main (argc, argv)
      int argc;
@@ -1416,6 +1464,7 @@
   int i, rl, needlf = 0;
   char *cwd, *str;
   char string[BUFSIZ+1];
+  int null_socket_name, null_server_file, start_daemon_if_needed;
 
   main_argv = argv;
   progname = argv[0];
@@ -1431,9 +1480,34 @@
       exit (EXIT_FAILURE);
     }
 
-  if ((emacs_socket = set_socket ()) == INVALID_SOCKET)
-    fail ();
+  /* If alternate_editor is the empty string, start the emacs daemon
+     in case of failure to connect.  */
+  start_daemon_if_needed = (alternate_editor
+                           && (alternate_editor[0] == '\0'));
+  if (start_daemon_if_needed)
+    {
+      /* set_socket changes the values for socket_name and
+        server_file, we need to reset them, if they were NULL before
+        for the second call to set_socket.  */
+      null_socket_name = (socket_name == NULL);
+      null_server_file = (server_file == NULL);
+    }
+
+  if ((emacs_socket = set_socket (alternate_editor
+                                 || start_daemon_if_needed)) == INVALID_SOCKET)
+    if (start_daemon_if_needed)
+      {
+       /* Reset socket_name and server_file if they were NULL
+          before the set_socket call.  */
+       if (null_socket_name)
+         socket_name = NULL;
+       if (null_server_file)
+         server_file = NULL;
 
+       start_daemon_and_retry_set_socket ();
+      }
+    else
+      fail ();
 
   cwd = get_current_dir_name ();
   if (cwd == 0)

Index: lib-src/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/lib-src/ChangeLog,v
retrieving revision 2.499
retrieving revision 2.500
diff -u -b -r2.499 -r2.500
--- lib-src/ChangeLog   3 Dec 2008 04:37:24 -0000       2.499
+++ lib-src/ChangeLog   10 Dec 2008 07:56:51 -0000      2.500
@@ -1,3 +1,12 @@
+2008-12-10  Dan Nicolaescu  <address@hidden>
+
+       * emacsclient.c (print_help_and_exit): Describe what an empty
+       string argument does for --alternate-editor.
+       (set_socket): Make it possible to not exit in case of an error.
+       (start_daemon_and_retry_set_socket): New function.
+       (main): Use it.  Restore the NULL value for socket_name and
+       server_file after the set_socket call.
+
 2008-12-03  Dan Nicolaescu  <address@hidden>
 
        * emacsclient.c: Include <arpa/inet.h>.

Index: doc/man/emacsclient.1
===================================================================
RCS file: /cvsroot/emacs/emacs/doc/man/emacsclient.1,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- doc/man/emacsclient.1       27 Nov 2008 16:56:33 -0000      1.2
+++ doc/man/emacsclient.1       10 Dec 2008 07:56:51 -0000      1.3
@@ -72,6 +72,8 @@
 .B \-a, \-\-alternate-editor=EDITOR
 if the Emacs server is not running, run the specified editor instead.
 This can also be specified via the `ALTERNATE_EDITOR' environment variable.
+If the value of EDITOR is the empty string, then Emacs is started in
+daemon mode and emacsclient will try to connect to it.
 .TP
 .B \-d, \-\-display=DISPLAY
 tell the server to display the files on the given display.

Index: doc/man/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/doc/man/ChangeLog,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- doc/man/ChangeLog   2 Dec 2008 16:40:32 -0000       1.12
+++ doc/man/ChangeLog   10 Dec 2008 07:56:52 -0000      1.13
@@ -1,3 +1,8 @@
+2008-12-10  Dan Nicolaescu  <address@hidden>
+
+       * emacsclient.1: Describe what an empty string argument does for
+       --alternate-editor.
+
 2008-11-27  Dan Nicolaescu  <address@hidden>
 
        * emacsclient.1: Mention -nw and -c. Fix the character for --help.

Index: doc/emacs/misc.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/doc/emacs/misc.texi,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- doc/emacs/misc.texi 30 Nov 2008 22:57:43 -0000      1.16
+++ doc/emacs/misc.texi 10 Dec 2008 07:56:52 -0000      1.17
@@ -1574,10 +1574,11 @@
 @item -a @var{command}
 @itemx address@hidden
 Specify a command to run if @code{emacsclient} fails to contact Emacs.
-This is useful when running @code{emacsclient} in a script.  For
-example, the following setting for the @env{EDITOR} environment
-variable will always give you an editor, even if no Emacs server is
-running:
+This is useful when running @code{emacsclient} in a script.  If
address@hidden is the empty string, then start Emacs in daemon mode and
+try connecting again.  For example, the following setting for the
address@hidden environment variable will always give you an editor, even
+if no Emacs server is running:
 
 @example
 EDITOR="emacsclient --alternate-editor emacs +%d %s"

Index: doc/emacs/ChangeLog
===================================================================
RCS file: /cvsroot/emacs/emacs/doc/emacs/ChangeLog,v
retrieving revision 1.247
retrieving revision 1.248
diff -u -b -r1.247 -r1.248
--- doc/emacs/ChangeLog 9 Dec 2008 14:32:02 -0000       1.247
+++ doc/emacs/ChangeLog 10 Dec 2008 07:56:52 -0000      1.248
@@ -1,3 +1,8 @@
+2008-12-10  Dan Nicolaescu  <address@hidden>
+
+       * misc.texi (emacsclient Options): Describe what an empty string
+       argument does for --alternate-editor.
+
 2008-12-09  Frank Schmitt  <address@hidden>
 
        * cmdargs.texi (Font X): Distinguish between client-side and




reply via email to

[Prev in Thread] Current Thread [Next in Thread]