1 /* Add entries to the GNU Emacs Program Manager folder.
2 Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009, 2010 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
10 (at 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/>. */
20 /****************************************************************************
22 * Program: addpm (adds emacs to the Windows program manager)
25 * argv[1] = install path for emacs
27 * argv[2] used to be an optional argument for setting the icon.
28 * But now Emacs has a professional looking icon of its own.
29 * If users really want to change it, they can go into the settings of
30 * the shortcut that is created and do it there.
33 /* Use parts of shell API that were introduced by the merge of IE4
34 into the desktop shell. If Windows 95 or NT4 users do not have IE4
35 installed, then the DDE fallback for creating icons the Windows 3.1
36 progman way will be used instead, but that is prone to lockups
37 caused by other applications not servicing their message queues. */
38 #define _WIN32_IE 0x400
39 /* Request C Object macros for COM interfaces. */
50 DdeCallback (UINT uType
, UINT uFmt
, HCONV hconv
,
51 HSZ hsz1
, HSZ hsz2
, HDDEDATA hdata
,
52 DWORD dwData1
, DWORD dwData2
)
54 return ((HDDEDATA
) NULL
);
57 #define DdeCommand(str) \
58 DdeClientTransaction (str, strlen (str)+1, conversation, (HSZ)NULL, \
59 CF_TEXT, XTYP_EXECUTE, 30000, NULL)
61 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
62 #define REG_GTK "SOFTWARE\\GTK\\2.0"
63 #define REG_APP_PATH \
64 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
74 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"},
75 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
76 {"EMACSDATA", "%emacs_dir%/etc"},
77 {"EMACSPATH", "%emacs_dir%/bin"},
78 /* We no longer set INFOPATH because Info-default-directory-list
80 /* {"INFOPATH", "%emacs_dir%/info"}, */
81 {"EMACSDOC", "%emacs_dir%/etc"},
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_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 len
= strlen (path
) + 5 + size
;
134 dll_paths
= (char *) alloca (size
+ strlen (path
) + 1);
135 sprintf (dll_paths
, "%s\\bin;%s", path
, gtk_path
);
136 RegSetValueEx (hrootkey
, "Path", 0, REG_SZ
, dll_paths
, len
);
139 RegCloseKey (gtk_key
);
141 RegCloseKey (hrootkey
);
144 /* Previous versions relied on registry settings, but we do not need
145 them any more. If registry settings are installed from a previous
146 version, replace them to ensure they are the current settings.
147 Otherwise, do nothing. */
149 /* Check both the current user and the local machine to see if we
150 have any resources. */
152 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE
, REG_ROOT
,
153 REG_OPTION_NON_VOLATILE
,
154 KEY_WRITE
, &hrootkey
) != ERROR_SUCCESS
155 && RegOpenKeyEx (HKEY_CURRENT_USER
, REG_ROOT
,
156 REG_OPTION_NON_VOLATILE
,
157 KEY_WRITE
, &hrootkey
) != ERROR_SUCCESS
)
162 for (i
= 0; i
< (sizeof (env_vars
) / sizeof (env_vars
[0])); i
++)
164 char * value
= env_vars
[i
].value
? env_vars
[i
].value
: path
;
166 if (RegSetValueEx (hrootkey
, env_vars
[i
].name
,
168 value
, lstrlen (value
) + 1) != ERROR_SUCCESS
)
172 RegCloseKey (hrootkey
);
182 char start_folder
[MAX_PATH
+ 1];
183 int shortcuts_created
= 0;
184 int com_available
= 1;
185 char modname
[MAX_PATH
];
191 IShellLinkA
*shortcut
;
193 /* If no args specified, use our location to set emacs_path. */
196 && (argv
[1][0] == '/' || argv
[1][0] == '-')
197 && argv
[1][1] == 'q')
205 emacs_path
= argv
[1];
208 if (!GetModuleFileName (NULL
, modname
, MAX_PATH
) ||
209 (p
= strrchr (modname
, '\\')) == NULL
)
211 fprintf (stderr
, "fatal error");
216 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
217 if ((p
= strrchr (modname
, '\\')) && stricmp (p
, "\\bin") == 0)
220 emacs_path
= modname
;
224 fprintf (stderr
, "usage: addpm emacs_path\n");
228 /* Tell user what we are going to do. */
233 char msg
[ MAX_PATH
];
234 sprintf (msg
, "Install Emacs at %s?\n", emacs_path
);
235 result
= MessageBox (NULL
, msg
, "Install Emacs",
236 MB_OKCANCEL
| MB_ICONQUESTION
);
239 fprintf (stderr
, "Install cancelled\n");
245 add_registry (emacs_path
);
246 prog_name
= "runemacs.exe";
248 /* Try to install globally. */
250 if (!SUCCEEDED (CoInitialize (NULL
))
251 || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink
, NULL
,
252 CLSCTX_INPROC_SERVER
, &IID_IShellLinkA
,
253 (void **) &shortcut
)))
259 && SHGetSpecialFolderPath (NULL
, start_folder
, CSIDL_COMMON_PROGRAMS
, 0))
261 if (strlen (start_folder
) < (MAX_PATH
- 20))
265 strcat (start_folder
, "\\Gnu Emacs");
266 if (CreateDirectory (start_folder
, NULL
)
267 || GetLastError () == ERROR_ALREADY_EXISTS
)
269 char full_emacs_path
[MAX_PATH
+ 1];
271 strcat (start_folder
, "\\Emacs.lnk");
272 sprintf (full_emacs_path
, "%s\\bin\\%s", emacs_path
, prog_name
);
273 IShellLinkA_SetPath (shortcut
, full_emacs_path
);
274 IShellLinkA_SetDescription (shortcut
, "GNU Emacs");
275 result
= IShellLinkA_QueryInterface (shortcut
, &IID_IPersistFile
,
277 if (SUCCEEDED (result
))
279 wchar_t unicode_path
[MAX_PATH
];
280 MultiByteToWideChar (CP_ACP
, 0, start_folder
, -1,
281 unicode_path
, MAX_PATH
);
282 if (SUCCEEDED (IPersistFile_Save (lnk
, unicode_path
, TRUE
)))
283 shortcuts_created
= 1;
284 IPersistFile_Release (lnk
);
290 if (!shortcuts_created
&& com_available
291 && SHGetSpecialFolderPath (NULL
, start_folder
, CSIDL_PROGRAMS
, 0))
293 /* Ensure there is enough room for "...\GNU Emacs\Emacs.lnk". */
294 if (strlen (start_folder
) < (MAX_PATH
- 20))
298 strcat (start_folder
, "\\Gnu Emacs");
299 if (CreateDirectory (start_folder
, NULL
)
300 || GetLastError () == ERROR_ALREADY_EXISTS
)
302 char full_emacs_path
[MAX_PATH
+ 1];
304 strcat (start_folder
, "\\Emacs.lnk");
305 sprintf (full_emacs_path
, "%s\\bin\\%s", emacs_path
, prog_name
);
306 IShellLinkA_SetPath (shortcut
, full_emacs_path
);
307 IShellLinkA_SetDescription (shortcut
, "GNU Emacs");
308 result
= IShellLinkA_QueryInterface (shortcut
, &IID_IPersistFile
,
310 if (SUCCEEDED (result
))
312 wchar_t unicode_path
[MAX_PATH
];
313 MultiByteToWideChar (CP_ACP
, 0, start_folder
, -1,
314 unicode_path
, MAX_PATH
);
315 if (SUCCEEDED (IPersistFile_Save (lnk
, unicode_path
, TRUE
)))
316 shortcuts_created
= 1;
317 IPersistFile_Release (lnk
);
325 IShellLinkA_Release (shortcut
);
327 /* Need to call uninitialize, even if ComInitialize failed. */
330 /* Fallback on old DDE method if the above failed. */
331 if (!shortcuts_created
)
336 char add_item
[MAX_PATH
*2 + 100];
338 DdeInitialize (&dde
, (PFNCALLBACK
) DdeCallback
, APPCMD_CLIENTONLY
, 0);
339 progman
= DdeCreateStringHandle (dde
, "PROGMAN", CP_WINANSI
);
340 conversation
= DdeConnect (dde
, progman
, progman
, NULL
);
343 DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
344 DdeCommand ("[ReplaceItem (Emacs)]");
345 sprintf (add_item
, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
346 emacs_path
, prog_name
);
347 DdeCommand (add_item
);
349 DdeDisconnect (conversation
);
352 DdeFreeStringHandle (dde
, progman
);
353 DdeUninitialize (dde
);
359 /* arch-tag: f923609d-b781-4ef4-abce-ca0da29cbbf0
360 (do not change this comment) */