message-citation-line-format %Z is now tz name
[emacs.git] / nt / addpm.c
blobb034fffe29ccaa7de664cd18ab65bc06716a36a3
1 /* Add entries to the GNU Emacs Program Manager folder.
2 Copyright (C) 1995, 2001-2017 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 (at
9 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)
23 * Usage:
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 #include <stdlib.h>
38 #include <stdio.h>
39 #include <malloc.h>
41 /* MinGW64 barfs if _WIN32_IE is defined to anything below 0x500. */
42 #ifndef MINGW_W64
43 #define _WIN32_IE 0x400
44 #endif
45 /* Request C Object macros for COM interfaces. */
46 #define COBJMACROS 1
48 #include <windows.h>
49 #include <shlobj.h>
50 #include <ddeml.h>
52 #ifndef OLD_PATHS
53 #include "../src/epaths.h"
54 #endif
56 HDDEDATA CALLBACK DdeCallback (UINT, UINT, HCONV, HSZ, HSZ, HDDEDATA, DWORD,
57 DWORD);
59 HDDEDATA CALLBACK
60 DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
61 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
62 DWORD dwData1, DWORD dwData2)
64 return ((HDDEDATA) NULL);
67 #define DdeCommand(str) \
68 DdeClientTransaction ((LPBYTE)str, strlen (str)+1, conversation, (HSZ)NULL, \
69 CF_TEXT, XTYP_EXECUTE, 30000, NULL)
71 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
72 #define REG_APP_PATH \
73 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
75 static struct entry
77 const char *name;
78 const char *value;
80 env_vars[] =
82 #ifdef OLD_PATHS
83 {"emacs_dir", NULL},
84 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp"},
85 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
86 {"EMACSDATA", "%emacs_dir%/etc"},
87 {"EMACSPATH", "%emacs_dir%/bin"},
88 /* We no longer set INFOPATH because Info-default-directory-list
89 is then ignored. */
90 /* {"INFOPATH", "%emacs_dir%/info"}, */
91 {"EMACSDOC", "%emacs_dir%/etc"},
92 {"TERM", "cmd"}
93 #else /* !OLD_PATHS */
94 {"emacs_dir", NULL},
95 {"EMACSLOADPATH", PATH_SITELOADSEARCH ";" PATH_LOADSEARCH},
96 {"SHELL", PATH_EXEC "/cmdproxy.exe"},
97 {"EMACSDATA", PATH_DATA},
98 {"EMACSPATH", PATH_EXEC},
99 /* We no longer set INFOPATH because Info-default-directory-list
100 is then ignored. */
101 /* {"INFOPATH", "%emacs_dir%/info"}, */
102 {"EMACSDOC", PATH_DOC},
103 {"TERM", "cmd"}
104 #endif
107 static void
108 add_registry (const char *path)
110 HKEY hrootkey = NULL;
111 int i;
113 /* Record the location of Emacs to the App Paths key if we have
114 sufficient permissions to do so. This helps Windows find emacs quickly
115 if the user types emacs.exe in the "Run Program" dialog without having
116 emacs on their path. Some applications also use the same registry key
117 to discover the installation directory for programs they are looking for.
118 Multiple installations cannot be handled by this method, but it does not
119 affect the general operation of other installations of Emacs, and we
120 are blindly overwriting the Start Menu entries already.
122 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, NULL,
123 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
124 &hrootkey, NULL) == ERROR_SUCCESS)
126 int len;
127 char *emacs_path;
129 len = strlen (path) + 15; /* \bin\emacs.exe + terminator. */
130 emacs_path = (char *) alloca (len);
131 sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
133 RegSetValueEx (hrootkey, NULL, 0, REG_EXPAND_SZ, emacs_path, len);
134 RegCloseKey (hrootkey);
137 /* Previous versions relied on registry settings, but we do not need
138 them any more. If registry settings are installed from a previous
139 version, replace them to ensure they are the current settings.
140 Otherwise, do nothing. */
142 /* Check both the current user and the local machine to see if we
143 have any resources. */
145 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT, 0,
146 KEY_WRITE | KEY_QUERY_VALUE, &hrootkey) != ERROR_SUCCESS
147 && RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT, 0,
148 KEY_WRITE | KEY_QUERY_VALUE, &hrootkey) != ERROR_SUCCESS)
149 return;
151 for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
153 const char * value = env_vars[i].value ? env_vars[i].value : path;
155 /* Replace only those settings that already exist. */
156 if (RegQueryValueEx (hrootkey, env_vars[i].name, NULL,
157 NULL, NULL, NULL) == ERROR_SUCCESS)
158 RegSetValueEx (hrootkey, env_vars[i].name, 0, REG_EXPAND_SZ,
159 value, lstrlen (value) + 1);
162 RegCloseKey (hrootkey);
166 main (int argc, char *argv[])
168 char start_folder[MAX_PATH + 1];
169 int shortcuts_created = 0;
170 int com_available = 1;
171 char modname[MAX_PATH];
172 const char *prog_name;
173 const char *emacs_path;
174 char *p;
175 int quiet = 0;
176 HRESULT result;
177 IShellLinkA *shortcut;
179 /* If no args specified, use our location to set emacs_path. */
181 if (argc > 1
182 && (argv[1][0] == '/' || argv[1][0] == '-')
183 && argv[1][1] == 'q')
185 quiet = 1;
186 --argc;
187 ++argv;
190 if (argc > 1)
191 emacs_path = argv[1];
192 else
194 if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
195 (p = strrchr (modname, '\\')) == NULL)
197 fprintf (stderr, "fatal error");
198 exit (1);
200 *p = 0;
202 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
203 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
205 *p = 0;
206 emacs_path = modname;
208 else
210 fprintf (stderr, "usage: addpm emacs_path\n");
211 exit (1);
214 /* Tell user what we are going to do. */
215 if (!quiet)
217 int result;
219 char msg[ MAX_PATH ];
220 sprintf (msg, "Install Emacs at %s?\n", emacs_path);
221 result = MessageBox (NULL, msg, "Install Emacs",
222 MB_OKCANCEL | MB_ICONQUESTION);
223 if (result != IDOK)
225 fprintf (stderr, "Install canceled\n");
226 exit (1);
231 add_registry (emacs_path);
232 prog_name = "runemacs.exe";
234 /* Try to install globally. */
236 if (!SUCCEEDED (CoInitialize (NULL))
237 || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink, NULL,
238 CLSCTX_INPROC_SERVER, &IID_IShellLinkA,
239 (void **) &shortcut)))
241 com_available = 0;
244 if (com_available
245 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
247 if (strlen (start_folder) < (MAX_PATH - 20))
249 strcat (start_folder, "\\Gnu Emacs");
250 if (CreateDirectory (start_folder, NULL)
251 || GetLastError () == ERROR_ALREADY_EXISTS)
253 char full_emacs_path[MAX_PATH + 1];
254 IPersistFile *lnk;
255 strcat (start_folder, "\\Emacs.lnk");
256 sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
257 IShellLinkA_SetPath (shortcut, full_emacs_path);
258 IShellLinkA_SetDescription (shortcut, "GNU Emacs");
259 result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
260 (void **) &lnk);
261 if (SUCCEEDED (result))
263 wchar_t unicode_path[MAX_PATH];
264 MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
265 unicode_path, MAX_PATH);
266 if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
267 shortcuts_created = 1;
268 IPersistFile_Release (lnk);
274 if (!shortcuts_created && com_available
275 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_PROGRAMS, 0))
277 /* Ensure there is enough room for "...\GNU Emacs\Emacs.lnk". */
278 if (strlen (start_folder) < (MAX_PATH - 20))
280 strcat (start_folder, "\\Gnu Emacs");
281 if (CreateDirectory (start_folder, NULL)
282 || GetLastError () == ERROR_ALREADY_EXISTS)
284 char full_emacs_path[MAX_PATH + 1];
285 IPersistFile *lnk;
286 strcat (start_folder, "\\Emacs.lnk");
287 sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
288 IShellLinkA_SetPath (shortcut, full_emacs_path);
289 IShellLinkA_SetDescription (shortcut, "GNU Emacs");
290 result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
291 (void **) &lnk);
292 if (SUCCEEDED (result))
294 wchar_t unicode_path[MAX_PATH];
295 MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
296 unicode_path, MAX_PATH);
297 if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
298 shortcuts_created = 1;
299 IPersistFile_Release (lnk);
306 if (com_available)
307 IShellLinkA_Release (shortcut);
309 /* Need to call uninitialize, even if ComInitialize failed. */
310 CoUninitialize ();
312 /* Fallback on old DDE method if the above failed. */
313 if (!shortcuts_created)
315 DWORD dde = 0;
316 HCONV conversation;
317 HSZ progman;
318 char add_item[MAX_PATH*2 + 100];
320 DdeInitialize (&dde, (PFNCALLBACK) DdeCallback, APPCMD_CLIENTONLY, 0);
321 progman = DdeCreateStringHandle (dde, "PROGMAN", CP_WINANSI);
322 conversation = DdeConnect (dde, progman, progman, NULL);
323 if (conversation)
325 DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
326 DdeCommand ("[ReplaceItem (Emacs)]");
327 sprintf (add_item, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
328 emacs_path, prog_name);
329 DdeCommand (add_item);
331 DdeDisconnect (conversation);
334 DdeFreeStringHandle (dde, progman);
335 DdeUninitialize (dde);
338 return 0;