* textmodes/fill.el (fill-region): Don't fill past the end.
[emacs.git] / nt / addpm.c
blob9bdfafc2a3e45b854a283f8b44d13bce5a595419
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)
24 * Usage:
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. */
40 #define COBJMACROS 1
42 #include <windows.h>
43 #include <shlobj.h>
44 #include <ddeml.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <malloc.h>
49 HDDEDATA CALLBACK
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"
66 static struct entry
68 char *name;
69 char *value;
71 env_vars[] =
73 {"emacs_dir", NULL},
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
79 is then ignored. */
80 /* {"INFOPATH", "%emacs_dir%/info"}, */
81 {"EMACSDOC", "%emacs_dir%/etc"},
82 {"TERM", "cmd"}
85 BOOL
86 add_registry (path)
87 char *path;
89 HKEY hrootkey = NULL;
90 int i;
91 BOOL ok = TRUE;
92 DWORD size;
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)
107 int len;
108 char *emacs_path;
109 HKEY gtk_key = NULL;
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, &gtk_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. */
132 char *dll_paths;
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)
159 return FALSE;
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,
167 0, REG_EXPAND_SZ,
168 value, lstrlen (value) + 1) != ERROR_SUCCESS)
169 ok = FALSE;
172 RegCloseKey (hrootkey);
174 return (ok);
178 main (argc, argv)
179 int argc;
180 char *argv[];
182 char start_folder[MAX_PATH + 1];
183 int shortcuts_created = 0;
184 int com_available = 1;
185 char modname[MAX_PATH];
186 char *prog_name;
187 char *emacs_path;
188 char *p;
189 int quiet = 0;
190 HRESULT result;
191 IShellLinkA *shortcut;
193 /* If no args specified, use our location to set emacs_path. */
195 if (argc > 1
196 && (argv[1][0] == '/' || argv[1][0] == '-')
197 && argv[1][1] == 'q')
199 quiet = 1;
200 --argc;
201 ++argv;
204 if (argc > 1)
205 emacs_path = argv[1];
206 else
208 if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
209 (p = strrchr (modname, '\\')) == NULL)
211 fprintf (stderr, "fatal error");
212 exit (1);
214 *p = 0;
216 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
217 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
219 *p = 0;
220 emacs_path = modname;
222 else
224 fprintf (stderr, "usage: addpm emacs_path\n");
225 exit (1);
228 /* Tell user what we are going to do. */
229 if (!quiet)
231 int result;
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);
237 if (result != IDOK)
239 fprintf (stderr, "Install cancelled\n");
240 exit (1);
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)))
255 com_available = 0;
258 if (com_available
259 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
261 if (strlen (start_folder) < (MAX_PATH - 20))
263 BOOL retval;
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];
270 IPersistFile *lnk;
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,
276 (void **) &lnk);
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))
296 BOOL retval;
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];
303 IPersistFile *lnk;
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,
309 (void **) &lnk);
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);
324 if (com_available)
325 IShellLinkA_Release (shortcut);
327 /* Need to call uninitialize, even if ComInitialize failed. */
328 CoUninitialize ();
330 /* Fallback on old DDE method if the above failed. */
331 if (!shortcuts_created)
333 DWORD dde = 0;
334 HCONV conversation;
335 HSZ progman;
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);
341 if (conversation)
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);
356 return 0;
359 /* arch-tag: f923609d-b781-4ef4-abce-ca0da29cbbf0
360 (do not change this comment) */