1 /* runemacs --- Simple program to start Emacs with its console window hidden.
3 Copyright (C) 2001-2017 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 Simple program to start Emacs with its console window hidden.
24 This program is provided purely for convenience, since most users will
25 use Emacs in windowing (GUI) mode, and will not want to have an extra
26 console window lying around. */
29 You may want to define this if you want to be able to install updated
30 emacs binaries even when other users are using the current version.
31 The problem with some file servers (notably Novell) is that an open
32 file cannot be overwritten, deleted, or even renamed. So if someone
33 is running emacs.exe already, you cannot install a newer version.
34 By defining CHOOSE_NEWEST_EXE, you can name your new emacs.exe
35 something else which matches "emacs*.exe", and runemacs will
36 automatically select the newest emacs executable in the bin directory.
37 (So you'll probably be able to delete the old version some hours/days
41 /* #define CHOOSE_NEWEST_EXE */
47 static void set_user_model_id (void);
48 static int ensure_unicows_dll (void);
51 WinMain (HINSTANCE hSelf
, HINSTANCE hPrev
, LPSTR cmdline
, int nShow
)
54 SECURITY_ATTRIBUTES sec_attrs
;
55 PROCESS_INFORMATION child
;
56 int wait_for_child
= FALSE
;
57 DWORD priority_class
= NORMAL_PRIORITY_CLASS
;
61 char modname
[MAX_PATH
];
62 static const char iconic_opt
[] = "--iconic ", maximized_opt
[] = "--maximized ";
64 if (!ensure_unicows_dll ())
69 if (!GetModuleFileName (NULL
, modname
, MAX_PATH
))
71 if ((p
= strrchr (modname
, '\\')) == NULL
)
75 new_cmdline
= alloca (MAX_PATH
77 + ((nShow
== SW_SHOWMINNOACTIVE
78 || nShow
== SW_SHOWMAXIMIZED
)
79 ? max (sizeof (iconic_opt
), sizeof (maximized_opt
))
82 /* Quote executable name in case of spaces in the path. */
84 strcpy (new_cmdline
+ 1, modname
);
85 /* Detect and handle un-installed runemacs.exe in nt/ subdirectory,
86 while emacs.exe is in src/. */
87 if ((p
= strrchr (new_cmdline
, '\\')) != NULL
88 && stricmp (p
, "\\nt") == 0)
91 #ifdef CHOOSE_NEWEST_EXE
93 /* Silly hack to allow new versions to be installed on
94 server even when current version is in use. */
96 char * best_name
= alloca (MAX_PATH
+ 1);
97 FILETIME best_time
= {0,0};
100 p
= new_cmdline
+ strlen (new_cmdline
);
101 strcpy (p
, "\\emacs*.exe\" ");
102 fh
= FindFirstFile (new_cmdline
, &wfd
);
103 if (fh
== INVALID_HANDLE_VALUE
)
107 if (wfd
.ftLastWriteTime
.dwHighDateTime
> best_time
.dwHighDateTime
108 || (wfd
.ftLastWriteTime
.dwHighDateTime
== best_time
.dwHighDateTime
109 && wfd
.ftLastWriteTime
.dwLowDateTime
> best_time
.dwLowDateTime
))
111 best_time
= wfd
.ftLastWriteTime
;
112 strcpy (best_name
, wfd
.cFileName
);
115 while (FindNextFile (fh
, &wfd
));
118 strcpy (p
, best_name
);
122 strcat (new_cmdline
, "\\emacs.exe\" ");
125 /* Append original arguments if any; first look for arguments we
126 recognize (-wait, -high, and -low), and apply them ourselves. */
127 while (cmdline
[0] == '-' || cmdline
[0] == '/')
129 if (strncmp (cmdline
+1, "wait", 4) == 0)
131 wait_for_child
= TRUE
;
134 else if (strncmp (cmdline
+1, "high", 4) == 0)
136 priority_class
= HIGH_PRIORITY_CLASS
;
139 else if (strncmp (cmdline
+1, "low", 3) == 0)
141 priority_class
= IDLE_PRIORITY_CLASS
;
146 /* Look for next argument. */
147 while (*++cmdline
== ' ');
150 /* If the desktop shortcut properties tell to invoke runemacs
151 minimized, or if they invoked runemacs via "start /min", pass
152 '--iconic' to Emacs, as that's what users will expect. Likewise
153 with invoking runemacs maximized: pass '--maximized' to Emacs. */
154 if (nShow
== SW_SHOWMINNOACTIVE
)
155 strcat (new_cmdline
, iconic_opt
);
156 else if (nShow
== SW_SHOWMAXIMIZED
)
157 strcat (new_cmdline
, maximized_opt
);
158 strcat (new_cmdline
, cmdline
);
160 /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
161 if ((p
= strrchr (modname
, '\\')) && stricmp (p
, "\\bin") == 0)
164 for (p
= modname
; *p
; p
++)
165 if (*p
== '\\') *p
= '/';
166 SetEnvironmentVariable ("emacs_dir", modname
);
169 memset (&start
, 0, sizeof (start
));
170 start
.cb
= sizeof (start
);
171 start
.dwFlags
= STARTF_USESHOWWINDOW
| STARTF_USECOUNTCHARS
;
172 start
.wShowWindow
= SW_HIDE
;
173 /* Ensure that we don't waste memory if the user has specified a huge
174 default screen buffer for command windows. */
175 start
.dwXCountChars
= 80;
176 start
.dwYCountChars
= 25;
178 sec_attrs
.nLength
= sizeof (sec_attrs
);
179 sec_attrs
.lpSecurityDescriptor
= NULL
;
180 sec_attrs
.bInheritHandle
= FALSE
;
182 if (CreateProcess (NULL
, new_cmdline
, &sec_attrs
, NULL
, TRUE
, priority_class
,
183 NULL
, NULL
, &start
, &child
))
187 WaitForSingleObject (child
.hProcess
, INFINITE
);
188 GetExitCodeProcess (child
.hProcess
, &ret_code
);
190 CloseHandle (child
.hThread
);
191 CloseHandle (child
.hProcess
);
195 return (int) ret_code
;
198 MessageBox (NULL
, "Could not start Emacs.", "Error", MB_ICONSTOP
);
203 set_user_model_id (void)
206 HRESULT (WINAPI
* set_user_model
) (wchar_t * id
);
208 /* On Windows 7 and later, we need to set the user model ID
209 to associate emacsclient launched files with Emacs frames
211 shell
= LoadLibrary ("shell32.dll");
215 = (void *) GetProcAddress (shell
,
216 "SetCurrentProcessExplicitAppUserModelID");
218 /* If the function is defined, then we are running on Windows 7
219 or newer, and the UI uses this to group related windows
220 together. Since emacs, runemacs, emacsclient are related, we
221 want them grouped even though the executables are different,
222 so we need to set a consistent ID between them. */
224 set_user_model (L
"GNU.Emacs");
231 ensure_unicows_dll (void)
233 OSVERSIONINFO os_ver
;
236 ZeroMemory (&os_ver
, sizeof (OSVERSIONINFO
));
237 os_ver
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
238 if (!GetVersionEx (&os_ver
))
241 if (os_ver
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
)
243 h
= LoadLibrary ("Unicows.dll");
248 button
= MessageBox (NULL
,
249 "Emacs cannot load the UNICOWS.DLL library.\n"
250 "This library is essential for using Emacs\n"
251 "on this system. You need to install it.\n\n"
252 "Emacs will exit when you click OK.",
253 "Emacs cannot load UNICOWS.DLL",
254 MB_ICONERROR
| MB_TASKMODAL
255 | MB_SETFOREGROUND
| MB_OK
);