From a45c1c45a29cbf9416bb03f77c99b383db43e7a0 Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Mon, 18 Apr 2022 21:04:57 +0300 Subject: [PATCH] Minor improvements in 'restart-emacs' on MS-Windows * src/w32.c (w32_reexec_emacs): Explicitly request a new console for the restarted Emacs -nw, and specify its dimensions. Specify NULL instead of security attributes, per examples on the Internet. * src/w32console.c (initialize_w32_display): Check errors in call to GetConsoleCursorInfo. --- src/w32.c | 51 ++++++++++++++++++++++++++---------------------- src/w32console.c | 12 +++++++++--- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/w32.c b/src/w32.c index e4237579d84..74923ffece9 100644 --- a/src/w32.c +++ b/src/w32.c @@ -10628,41 +10628,46 @@ int w32_reexec_emacs (char *cmd_line, const char *wdir) { STARTUPINFO si; - SECURITY_ATTRIBUTES sec_attrs; BOOL status; PROCESS_INFORMATION proc_info; + DWORD dwCreationFlags = NORMAL_PRIORITY_CLASS; GetStartupInfo (&si); /* Use the same startup info as the caller. */ - sec_attrs.nLength = sizeof (sec_attrs); - sec_attrs.lpSecurityDescriptor = NULL; - sec_attrs.bInheritHandle = FALSE; - - /* Make sure we are in the original directory, in case the command - line specifies the program as a relative file name. */ - chdir (wdir); - - /* This is a kludge: it causes the restarted "emacs -nw" to have a - new console window created for it, and that new window might have - different (default) properties, not the ones of the parent - process's console window. But without this, restarting Emacs in - the -nw mode simply doesn't work. FIXME! */ if (inhibit_window_system) { - if (!FreeConsole ()) + HANDLE screen_handle; + CONSOLE_SCREEN_BUFFER_INFO screen_info; + + screen_handle = GetStdHandle (STD_OUTPUT_HANDLE); + if (screen_handle != INVALID_HANDLE_VALUE + && GetConsoleScreenBufferInfo (screen_handle, &screen_info)) { - errno = ENOEXEC; - return -1; + /* Make the restarted Emacs's console window the same + dimensions as ours. FIXME: for some reason this doesn't + seem to work! */ + si.dwFlags |= STARTF_USECOUNTCHARS; + si.dwXCountChars = screen_info.dwSize.X; + si.dwYCountChars = screen_info.dwSize.Y; } + /* This is a kludge: it causes the restarted "emacs -nw" to have + a new console window created for it, and that new window + might have different (default) properties, not the ones of + the parent process's console window. But without this, + restarting Emacs in the -nw mode simply doesn't work. + FIXME! */ + dwCreationFlags = CREATE_NEW_CONSOLE; } - status = CreateProcess (NULL, /* program */ + /* Make sure we are in the original directory, in case the command + line specifies the program as a relative file name. */ + chdir (wdir); + + status = CreateProcess (NULL, /* no program, take from command line */ cmd_line, /* command line */ - &sec_attrs, /* process attributes */ + NULL, NULL, /* thread attributes */ - TRUE, /* inherit handles? */ - inhibit_window_system - ? 0 /* inherit parent's console */ - : NORMAL_PRIORITY_CLASS, + FALSE, /* unherit handles? */ + dwCreationFlags, NULL, /* environment */ wdir, /* initial directory */ &si, /* startup info */ diff --git a/src/w32console.c b/src/w32console.c index 12e1f397894..09749126e03 100644 --- a/src/w32console.c +++ b/src/w32console.c @@ -716,10 +716,10 @@ initialize_w32_display (struct terminal *term, int *width, int *height) if (cur_screen == INVALID_HANDLE_VALUE) { - printf ("CreateConsoleScreenBuffer failed in ResetTerm\n"); + printf ("CreateConsoleScreenBuffer failed in initialize_w32_display\n"); printf ("LastError = 0x%lx\n", GetLastError ()); fflush (stdout); - exit (0); + exit (1); } #else cur_screen = prev_screen; @@ -760,7 +760,13 @@ initialize_w32_display (struct terminal *term, int *width, int *height) } } - GetConsoleScreenBufferInfo (cur_screen, &info); + if (!GetConsoleScreenBufferInfo (cur_screen, &info)) + { + printf ("GetConsoleScreenBufferInfo failed in initialize_w32_display\n"); + printf ("LastError = 0x%lx\n", GetLastError ()); + fflush (stdout); + exit (1); + } char_attr_normal = info.wAttributes; -- 2.39.2