1 /* Add entries to the GNU Emacs Program Manager folder.
2 Copyright (C) 1995, 2001-2013 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 /****************************************************************************
21 * Program: addpm (adds emacs to the Windows program manager)
24 * argv[1] = install path for emacs
26 * argv[2] used to be an optional argument for setting the icon.
27 * But now Emacs has a professional looking icon of its own.
28 * If users really want to change it, they can go into the settings of
29 * the shortcut that is created and do it there.
32 /* Use parts of shell API that were introduced by the merge of IE4
33 into the desktop shell. If Windows 95 or NT4 users do not have IE4
34 installed, then the DDE fallback for creating icons the Windows 3.1
35 progman way will be used instead, but that is prone to lockups
36 caused by other applications not servicing their message queues. */
37 #define _WIN32_IE 0x400
38 /* Request C Object macros for COM interfaces. */
49 DdeCallback (UINT uType
, UINT uFmt
, HCONV hconv
,
50 HSZ hsz1
, HSZ hsz2
, HDDEDATA hdata
,
51 DWORD dwData1
, DWORD dwData2
)
53 return ((HDDEDATA
) NULL
);
56 #define DdeCommand(str) \
57 DdeClientTransaction (str, strlen (str)+1, conversation, (HSZ)NULL, \
58 CF_TEXT, XTYP_EXECUTE, 30000, NULL)
60 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
61 #define REG_GTK "SOFTWARE\\GTK\\2.0"
62 #define REG_APP_PATH \
63 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
64 #define REG_RUNEMACS_PATH \
65 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\runemacs.exe"
75 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"},
76 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
77 {"EMACSDATA", "%emacs_dir%/etc"},
78 {"EMACSPATH", "%emacs_dir%/bin"},
79 /* We no longer set INFOPATH because Info-default-directory-list
81 /* {"INFOPATH", "%emacs_dir%/info"}, */
82 {"EMACSDOC", "%emacs_dir%/etc"},
87 add_registry (const char *path
)
94 /* Record the location of Emacs to the App Paths key if we have
95 sufficient permissions to do so. This helps Windows find emacs quickly
96 if the user types emacs.exe in the "Run Program" dialog without having
97 emacs on their path. Some applications also use the same registry key
98 to discover the installation directory for programs they are looking for.
99 Multiple installations cannot be handled by this method, but it does not
100 affect the general operation of other installations of Emacs, and we
101 are blindly overwriting the Start Menu entries already.
103 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE
, REG_APP_PATH
, 0, "",
104 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
,
105 &hrootkey
, NULL
) == ERROR_SUCCESS
)
111 len
= strlen (path
) + 15; /* \bin\emacs.exe + terminator. */
112 emacs_path
= (char *) alloca (len
);
113 sprintf (emacs_path
, "%s\\bin\\emacs.exe", path
);
115 RegSetValueEx (hrootkey
, NULL
, 0, REG_EXPAND_SZ
, emacs_path
, len
);
117 /* Look for a GTK installation. If found, add it to the library search
118 path for Emacs so that the image libraries it provides are available
119 to Emacs regardless of whether it is in the path or not. */
120 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE
, REG_GTK
, REG_OPTION_NON_VOLATILE
,
121 KEY_READ
, >k_key
) == ERROR_SUCCESS
)
123 if (RegQueryValueEx (gtk_key
, "DllPath", NULL
, NULL
,
124 NULL
, &size
) == ERROR_SUCCESS
)
126 char *gtk_path
= (char *) alloca (size
);
127 if (RegQueryValueEx (gtk_key
, "DllPath", NULL
, NULL
,
128 gtk_path
, &size
) == ERROR_SUCCESS
)
130 /* Make sure the emacs bin directory continues to be searched
131 first by including it as well. */
133 HKEY runemacs_key
= NULL
;
134 len
= strlen (path
) + 5 + size
;
135 dll_paths
= (char *) alloca (size
+ strlen (path
) + 1);
136 sprintf (dll_paths
, "%s\\bin;%s", path
, gtk_path
);
137 RegSetValueEx (hrootkey
, "Path", 0, REG_EXPAND_SZ
,
140 /* Set the same path for runemacs.exe, as the Explorer shell
141 looks this up, so the above does not take effect when
142 emacs.exe is spawned from runemacs.exe. */
143 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE
, REG_RUNEMACS_PATH
,
144 0, "", REG_OPTION_NON_VOLATILE
,
145 KEY_WRITE
, NULL
, &runemacs_key
, NULL
)
148 RegSetValueEx (runemacs_key
, "Path", 0, REG_EXPAND_SZ
,
151 RegCloseKey (runemacs_key
);
155 RegCloseKey (gtk_key
);
157 RegCloseKey (hrootkey
);
160 /* Previous versions relied on registry settings, but we do not need
161 them any more. If registry settings are installed from a previous
162 version, replace them to ensure they are the current settings.
163 Otherwise, do nothing. */
165 /* Check both the current user and the local machine to see if we
166 have any resources. */
168 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE
, REG_ROOT
,
169 REG_OPTION_NON_VOLATILE
,
170 KEY_WRITE
, &hrootkey
) != ERROR_SUCCESS
171 && RegOpenKeyEx (HKEY_CURRENT_USER
, REG_ROOT
,
172 REG_OPTION_NON_VOLATILE
,
173 KEY_WRITE
, &hrootkey
) != ERROR_SUCCESS
)
178 for (i
= 0; i
< (sizeof (env_vars
) / sizeof (env_vars
[0])); i
++)
180 const char * value
= env_vars
[i
].value
? env_vars
[i
].value
: path
;
182 if (RegSetValueEx (hrootkey
, env_vars
[i
].name
,
184 value
, lstrlen (value
) + 1) != ERROR_SUCCESS
)
188 RegCloseKey (hrootkey
);
194 main (int argc
, char *argv
[])
196 char start_folder
[MAX_PATH
+ 1];
197 int shortcuts_created
= 0;
198 int com_available
= 1;
199 char modname
[MAX_PATH
];
200 const char *prog_name
;
201 const char *emacs_path
;
205 IShellLinkA
*shortcut
;
207 /* If no args specified, use our location to set emacs_path. */
210 && (argv
[1][0] == '/' || argv
[1][0] == '-')
211 && argv
[1][1] == 'q')
219 emacs_path
= argv
[1];
222 if (!GetModuleFileName (NULL
, modname
, MAX_PATH
) ||
223 (p
= strrchr (modname
, '\\')) == NULL
)
225 fprintf (stderr
, "fatal error");
230 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
231 if ((p
= strrchr (modname
, '\\')) && stricmp (p
, "\\bin") == 0)
234 emacs_path
= modname
;
238 fprintf (stderr
, "usage: addpm emacs_path\n");
242 /* Tell user what we are going to do. */
247 char msg
[ MAX_PATH
];
248 sprintf (msg
, "Install Emacs at %s?\n", emacs_path
);
249 result
= MessageBox (NULL
, msg
, "Install Emacs",
250 MB_OKCANCEL
| MB_ICONQUESTION
);
253 fprintf (stderr
, "Install canceled\n");
259 add_registry (emacs_path
);
260 prog_name
= "runemacs.exe";
262 /* Try to install globally. */
264 if (!SUCCEEDED (CoInitialize (NULL
))
265 || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink
, NULL
,
266 CLSCTX_INPROC_SERVER
, &IID_IShellLinkA
,
267 (void **) &shortcut
)))
273 && SHGetSpecialFolderPath (NULL
, start_folder
, CSIDL_COMMON_PROGRAMS
, 0))
275 if (strlen (start_folder
) < (MAX_PATH
- 20))
277 strcat (start_folder
, "\\Gnu Emacs");
278 if (CreateDirectory (start_folder
, NULL
)
279 || GetLastError () == ERROR_ALREADY_EXISTS
)
281 char full_emacs_path
[MAX_PATH
+ 1];
283 strcat (start_folder
, "\\Emacs.lnk");
284 sprintf (full_emacs_path
, "%s\\bin\\%s", emacs_path
, prog_name
);
285 IShellLinkA_SetPath (shortcut
, full_emacs_path
);
286 IShellLinkA_SetDescription (shortcut
, "GNU Emacs");
287 result
= IShellLinkA_QueryInterface (shortcut
, &IID_IPersistFile
,
289 if (SUCCEEDED (result
))
291 wchar_t unicode_path
[MAX_PATH
];
292 MultiByteToWideChar (CP_ACP
, 0, start_folder
, -1,
293 unicode_path
, MAX_PATH
);
294 if (SUCCEEDED (IPersistFile_Save (lnk
, unicode_path
, TRUE
)))
295 shortcuts_created
= 1;
296 IPersistFile_Release (lnk
);
302 if (!shortcuts_created
&& com_available
303 && SHGetSpecialFolderPath (NULL
, start_folder
, CSIDL_PROGRAMS
, 0))
305 /* Ensure there is enough room for "...\GNU Emacs\Emacs.lnk". */
306 if (strlen (start_folder
) < (MAX_PATH
- 20))
308 strcat (start_folder
, "\\Gnu Emacs");
309 if (CreateDirectory (start_folder
, NULL
)
310 || GetLastError () == ERROR_ALREADY_EXISTS
)
312 char full_emacs_path
[MAX_PATH
+ 1];
314 strcat (start_folder
, "\\Emacs.lnk");
315 sprintf (full_emacs_path
, "%s\\bin\\%s", emacs_path
, prog_name
);
316 IShellLinkA_SetPath (shortcut
, full_emacs_path
);
317 IShellLinkA_SetDescription (shortcut
, "GNU Emacs");
318 result
= IShellLinkA_QueryInterface (shortcut
, &IID_IPersistFile
,
320 if (SUCCEEDED (result
))
322 wchar_t unicode_path
[MAX_PATH
];
323 MultiByteToWideChar (CP_ACP
, 0, start_folder
, -1,
324 unicode_path
, MAX_PATH
);
325 if (SUCCEEDED (IPersistFile_Save (lnk
, unicode_path
, TRUE
)))
326 shortcuts_created
= 1;
327 IPersistFile_Release (lnk
);
335 IShellLinkA_Release (shortcut
);
337 /* Need to call uninitialize, even if ComInitialize failed. */
340 /* Fallback on old DDE method if the above failed. */
341 if (!shortcuts_created
)
346 char add_item
[MAX_PATH
*2 + 100];
348 DdeInitialize (&dde
, (PFNCALLBACK
) DdeCallback
, APPCMD_CLIENTONLY
, 0);
349 progman
= DdeCreateStringHandle (dde
, "PROGMAN", CP_WINANSI
);
350 conversation
= DdeConnect (dde
, progman
, progman
, NULL
);
353 DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
354 DdeCommand ("[ReplaceItem (Emacs)]");
355 sprintf (add_item
, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
356 emacs_path
, prog_name
);
357 DdeCommand (add_item
);
359 DdeDisconnect (conversation
);
362 DdeFreeStringHandle (dde
, progman
);
363 DdeUninitialize (dde
);