Merge changes made in Gnus trunk.
[bpt/emacs.git] / nt / runemacs.c
CommitLineData
73b0cd50 1/* Copyright (C) 2001-2011
62eda0e2 2 Free Software Foundation, Inc.
b65d8176
TTN
3
4This file is part of GNU Emacs.
5
eef0be9e 6GNU Emacs is free software: you can redistribute it and/or modify
b65d8176 7it under the terms of the GNU General Public License as published by
eef0be9e
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
b65d8176
TTN
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
eef0be9e 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
b65d8176
TTN
18
19
c911543b
GV
20/*
21 Simple program to start Emacs with its console window hidden.
22
23 This program is provided purely for convenience, since most users will
24 use Emacs in windowing (GUI) mode, and will not want to have an extra
25 console window lying around. */
26
662463d9
RS
27/*
28 You may want to define this if you want to be able to install updated
29 emacs binaries even when other users are using the current version.
30 The problem with some file servers (notably Novell) is that an open
31 file cannot be overwritten, deleted, or even renamed. So if someone
32 is running emacs.exe already, you cannot install a newer version.
33 By defining CHOOSE_NEWEST_EXE, you can name your new emacs.exe
34 something else which matches "emacs*.exe", and runemacs will
4da0d3f7 35 automatically select the newest emacs executable in the bin directory.
662463d9
RS
36 (So you'll probably be able to delete the old version some hours/days
37 later).
38*/
39
40/* #define CHOOSE_NEWEST_EXE */
41
c911543b
GV
42#include <windows.h>
43#include <string.h>
44#include <malloc.h>
45
361358ea 46static void set_user_model_id (void);
ff90fbde 47
c911543b
GV
48int WINAPI
49WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
50{
51 STARTUPINFO start;
52 SECURITY_ATTRIBUTES sec_attrs;
c911543b
GV
53 PROCESS_INFORMATION child;
54 int wait_for_child = FALSE;
05c4be3c 55 DWORD priority_class = NORMAL_PRIORITY_CLASS;
c911543b 56 DWORD ret_code = 0;
662463d9
RS
57 char *new_cmdline;
58 char *p;
c911543b
GV
59 char modname[MAX_PATH];
60
ff90fbde
JR
61 set_user_model_id ();
62
c911543b
GV
63 if (!GetModuleFileName (NULL, modname, MAX_PATH))
64 goto error;
65 if ((p = strrchr (modname, '\\')) == NULL)
66 goto error;
67 *p = 0;
68
0ac7bf6c
JR
69 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 3);
70 /* Quote executable name in case of spaces in the path. */
71 *new_cmdline = '"';
72 strcpy (new_cmdline + 1, modname);
c911543b 73
662463d9 74#ifdef CHOOSE_NEWEST_EXE
c911543b 75 {
662463d9
RS
76 /* Silly hack to allow new versions to be installed on
77 server even when current version is in use. */
78
79 char * best_name = alloca (MAX_PATH + 1);
80 FILETIME best_time = {0,0};
81 WIN32_FIND_DATA wfd;
82 HANDLE fh;
83 p = new_cmdline + strlen (new_cmdline);
0ac7bf6c 84 strcpy (p, "\\emacs*.exe\" ");
662463d9
RS
85 fh = FindFirstFile (new_cmdline, &wfd);
86 if (fh == INVALID_HANDLE_VALUE)
87 goto error;
88 do
89 {
4da0d3f7
JB
90 if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
91 || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
92 && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
93 {
94 best_time = wfd.ftLastWriteTime;
95 strcpy (best_name, wfd.cFileName);
96 }
662463d9
RS
97 }
98 while (FindNextFile (fh, &wfd));
99 FindClose (fh);
100 *p++ = '\\';
101 strcpy (p, best_name);
102 strcat (p, " ");
c911543b 103 }
662463d9 104#else
0ac7bf6c 105 strcat (new_cmdline, "\\emacs.exe\" ");
662463d9
RS
106#endif
107
05c4be3c
GV
108 /* Append original arguments if any; first look for arguments we
109 recognise (-wait, -high, and -low), and apply them ourselves. */
110 while (cmdline[0] == '-' || cmdline[0] == '/')
662463d9 111 {
05c4be3c
GV
112 if (strncmp (cmdline+1, "wait", 4) == 0)
113 {
4da0d3f7
JB
114 wait_for_child = TRUE;
115 cmdline += 5;
116 }
05c4be3c
GV
117 else if (strncmp (cmdline+1, "high", 4) == 0)
118 {
119 priority_class = HIGH_PRIORITY_CLASS;
120 cmdline += 5;
121 }
122 else if (strncmp (cmdline+1, "low", 3) == 0)
123 {
124 priority_class = IDLE_PRIORITY_CLASS;
125 cmdline += 4;
126 }
127 else
128 break;
4da0d3f7
JB
129 /* Look for next argument. */
130 while (*++cmdline == ' ');
05c4be3c 131 }
4da0d3f7 132
c911543b
GV
133 strcat (new_cmdline, cmdline);
134
662463d9 135 /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
c911543b 136 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
662463d9
RS
137 {
138 *p = 0;
139 for (p = modname; *p; p++)
140 if (*p == '\\') *p = '/';
141 SetEnvironmentVariable ("emacs_dir", modname);
142 }
c911543b
GV
143
144 memset (&start, 0, sizeof (start));
145 start.cb = sizeof (start);
3bb69bbd 146 start.dwFlags = STARTF_USESHOWWINDOW | STARTF_USECOUNTCHARS;
c911543b 147 start.wShowWindow = SW_HIDE;
3bb69bbd
JR
148 /* Ensure that we don't waste memory if the user has specified a huge
149 default screen buffer for command windows. */
150 start.dwXCountChars = 80;
151 start.dwYCountChars = 25;
c911543b
GV
152
153 sec_attrs.nLength = sizeof (sec_attrs);
154 sec_attrs.lpSecurityDescriptor = NULL;
155 sec_attrs.bInheritHandle = FALSE;
156
05c4be3c 157 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, priority_class,
1e3c9713 158 NULL, NULL, &start, &child))
c911543b
GV
159 {
160 if (wait_for_child)
161 {
162 WaitForSingleObject (child.hProcess, INFINITE);
163 GetExitCodeProcess (child.hProcess, &ret_code);
164 }
165 CloseHandle (child.hThread);
166 CloseHandle (child.hProcess);
167 }
168 else
169 goto error;
170 return (int) ret_code;
171
172error:
173 MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
174 return 1;
175}
ab5796a9 176
7c3320d8
JB
177void
178set_user_model_id (void)
ff90fbde
JR
179{
180 HMODULE shell;
0a3472c7 181 HRESULT (WINAPI * set_user_model) (wchar_t * id);
ff90fbde
JR
182
183 /* On Windows 7 and later, we need to set the user model ID
184 to associate emacsclient launched files with Emacs frames
185 in the UI. */
186 shell = LoadLibrary ("shell32.dll");
187 if (shell)
188 {
189 set_user_model
190 = (void *) GetProcAddress (shell,
191 "SetCurrentProcessExplicitAppUserModelID");
192
193 /* If the function is defined, then we are running on Windows 7
194 or newer, and the UI uses this to group related windows
195 together. Since emacs, runemacs, emacsclient are related, we
196 want them grouped even though the executables are different,
197 so we need to set a consistent ID between them. */
198 if (set_user_model)
199 set_user_model (L"GNU.Emacs");
200
201 FreeLibrary (shell);
202 }
203}
204