]> code.delx.au - gnu-emacs/blobdiff - lib-src/emacsclient.c
(turkish-case-conversion-enable)
[gnu-emacs] / lib-src / emacsclient.c
index aeb221dfed12882bf4112c8302cbf3bda6b9cba4..7cf703d40d12e45cae53c23221cc4e136cf37724 100644 (file)
@@ -41,6 +41,8 @@ Boston, MA 02110-1301, USA.  */
 
 #else /* !WINDOWSNT */
 
+# include <sys/types.h>
+
 # ifdef HAVE_INET_SOCKETS
 #  include <netinet/in.h>
 # endif
@@ -432,7 +434,7 @@ initialize_sockets ()
 \f
 /*
  * Read the information needed to set up a TCP comm channel with
- * the Emacs server: host, port and authentication string.
+ * the Emacs server: host, port, pid and authentication string.
 */
 int
 get_server_config (server, authentication)
@@ -441,6 +443,7 @@ get_server_config (server, authentication)
 {
   char dotted[32];
   char *port;
+  char *pid;
   FILE *config = NULL;
 
   if (file_name_absolute_p (server_file))
@@ -448,25 +451,32 @@ get_server_config (server, authentication)
   else
     {
       char *home = getenv ("HOME");
-#ifdef WINDOWSNT
-      if (! home)
-          home = getenv ("APPDATA");
-#endif
+
       if (home)
         {
           char *path = alloca (32 + strlen (home) + strlen (server_file));
           sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
           config = fopen (path, "rb");
         }
+#ifdef WINDOWSNT
+      if (!config && (home = getenv ("APPDATA")))
+        {
+          char *path = alloca (32 + strlen (home) + strlen (server_file));
+          sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
+          config = fopen (path, "rb");
+        }
+#endif
     }
 
   if (! config)
     return FALSE;
 
   if (fgets (dotted, sizeof dotted, config)
-      && (port = strchr (dotted, ':')))
+      && (port = strchr (dotted, ':'))
+      && (pid = strchr (port, ' ')))
     {
       *port++ = '\0';
+      *pid++  = '\0';
     }
   else
     {
@@ -486,6 +496,30 @@ get_server_config (server, authentication)
 
   fclose (config);
 
+#ifdef WINDOWSNT
+  /*
+    Modern Windows restrict which processes can set the foreground window.
+    So, for emacsclient to be able to force Emacs into the foreground, we
+    have to call AllowSetForegroundWindow().  Unfortunately, older Windows
+    (W95, W98 and NT) don't have this function, so we have to check first.
+
+    We're doing this here because it has to be done before sending info
+    to Emacs, and otherwise we'll need a global variable just to pass around
+    the pid, which is also inelegant.
+   */
+  {
+    HMODULE hUser32;
+
+    if (hUser32 = LoadLibrary ("user32.dll"))
+      {
+        FARPROC set_fg;
+        if (set_fg = GetProcAddress (hUser32, "AllowSetForegroundWindow"))
+          set_fg (atoi (pid));
+        FreeLibrary (hUser32);
+      }
+  }
+#endif
+
   return TRUE;
 }