blob: 54944a55198d09d9115d534705f656c4cbc171b2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * dosinst.c: Install program for Vim on MS-DOS and MS-Windows
12 *
13 * Compile with Make_mvc.mak, Make_bc3.mak, Make_bc5.mak or Make_djg.mak.
14 */
15
16/*
17 * Include common code for dosinst.c and uninstal.c.
18 */
19#define DOSINST
20#include "dosinst.h"
21
22/* Macro to do an error check I was typing over and over */
23#define CHECK_REG_ERROR(code) if (code != ERROR_SUCCESS) { printf("%ld error number: %ld\n", (long)__LINE__, (long)code); return 1; }
24
25int has_vim = 0; /* installable vim.exe exists */
26int has_gvim = 0; /* installable gvim.exe exists */
27
28char oldvimrc[BUFSIZE]; /* name of existing vimrc file */
29char vimrc[BUFSIZE]; /* name of vimrc file to create */
30
31char *default_bat_dir = NULL; /* when not NULL, use this as the default
32 directory to write .bat files in */
33char *default_vim_dir = NULL; /* when not NULL, use this as the default
34 install dir for NSIS */
35#if 0
36char homedir[BUFSIZE]; /* home directory or "" */
37#endif
38
39/*
40 * Structure used for each choice the user can make.
41 */
42struct choice
43{
44 int active; /* non-zero when choice is active */
45 char *text; /* text displayed for this choice */
46 void (*changefunc)(int idx); /* function to change this choice */
47 int arg; /* argument for function */
48 void (*installfunc)(int idx); /* function to install this choice */
49};
50
51struct choice choices[30]; /* choices the user can make */
52int choice_count = 0; /* number of choices available */
53
54#define TABLE_SIZE(s) (int)(sizeof(s) / sizeof(*s))
55
56enum
57{
58 compat_vi = 1,
59 compat_some_enhancements,
60 compat_all_enhancements
61};
62char *(compat_choices[]) =
63{
64 "\nChoose the default way to run Vim:",
65 "Vi compatible",
Bram Moolenaarfff2bee2010-05-15 13:56:02 +020066 "with some Vim enhancements",
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 "with syntax highlighting and other features switched on",
68};
69int compat_choice = (int)compat_all_enhancements;
70char *compat_text = "- run Vim %s";
71
72enum
73{
74 remap_no = 1,
75 remap_win
76};
77char *(remap_choices[]) =
78{
79 "\nChoose:",
80 "Do not remap keys for Windows behavior",
81 "Remap a few keys for Windows behavior (<C-V>, <C-C>, etc)",
82};
83int remap_choice = (int)remap_win;
84char *remap_text = "- %s";
85
86enum
87{
88 mouse_xterm = 1,
89 mouse_mswin
90};
91char *(mouse_choices[]) =
92{
93 "\nChoose the way how Vim uses the mouse:",
94 "right button extends selection (the Unix way)",
95 "right button has a popup menu (the Windows way)",
96};
97int mouse_choice = (int)mouse_mswin;
98char *mouse_text = "- The mouse %s";
99
100enum
101{
102 vimfiles_dir_none = 1,
103 vimfiles_dir_vim,
104 vimfiles_dir_home
105};
106static char *(vimfiles_dir_choices[]) =
107{
108 "\nCreate plugin directories:",
109 "No",
110 "In the VIM directory",
111 "In your HOME directory",
112};
113static int vimfiles_dir_choice;
114
115/* non-zero when selected to install the popup menu entry. */
116static int install_popup = 0;
117
118/* non-zero when selected to install the "Open with" entry. */
119static int install_openwith = 0;
120
121/* non-zero when need to add an uninstall entry in the registry */
122static int need_uninstall_entry = 0;
123
124/*
125 * Definitions of the directory name (under $VIM) of the vimfiles directory
126 * and its subdirectories:
127 */
128static char *(vimfiles_subdirs[]) =
129{
130 "colors",
131 "compiler",
132 "doc",
133 "ftdetect",
134 "ftplugin",
135 "indent",
136 "keymap",
137 "plugin",
138 "syntax",
139};
140
141/*
142 * Copy a directory name from "dir" to "buf", doubling backslashes.
143 * Also make sure it ends in a double backslash.
144 */
145 static void
146double_bs(char *dir, char *buf)
147{
148 char *d = buf;
149 char *s;
150
151 for (s = dir; *s; ++s)
152 {
153 if (*s == '\\')
154 *d++ = '\\';
155 *d++ = *s;
156 }
157 /* when dir is not empty, it must end in a double backslash */
158 if (d > buf && d[-1] != '\\')
159 {
160 *d++ = '\\';
161 *d++ = '\\';
162 }
163 *d = NUL;
164}
165
166/*
167 * Obtain a choice from a table.
168 * First entry is a question, others are choices.
169 */
170 static int
171get_choice(char **table, int entries)
172{
173 int answer;
174 int idx;
175 char dummy[100];
176
177 do
178 {
179 for (idx = 0; idx < entries; ++idx)
180 {
181 if (idx)
182 printf("%2d ", idx);
183 printf(table[idx]);
184 printf("\n");
185 }
186 printf("Choice: ");
187 if (scanf("%d", &answer) != 1)
188 {
189 scanf("%99s", dummy);
190 answer = 0;
191 }
192 }
193 while (answer < 1 || answer >= entries);
194
195 return answer;
196}
197
198/*
199 * Check if the user unpacked the archives properly.
200 * Sets "runtimeidx".
201 */
202 static void
203check_unpack(void)
204{
205 char buf[BUFSIZE];
206 FILE *fd;
207 struct stat st;
208
209 /* check for presence of the correct version number in installdir[] */
210 runtimeidx = strlen(installdir) - strlen(VIM_VERSION_NODOT);
211 if (runtimeidx <= 0
212 || stricmp(installdir + runtimeidx, VIM_VERSION_NODOT) != 0
213 || (installdir[runtimeidx - 1] != '/'
214 && installdir[runtimeidx - 1] != '\\'))
215 {
216 printf("ERROR: Install program not in directory \"%s\"\n",
217 VIM_VERSION_NODOT);
218 printf("This program can only work when it is located in its original directory\n");
219 myexit(1);
220 }
221
222 /* check if filetype.vim is present, which means the runtime archive has
223 * been unpacked */
224 sprintf(buf, "%s\\filetype.vim", installdir);
225 if (stat(buf, &st) < 0)
226 {
227 printf("ERROR: Cannot find filetype.vim in \"%s\"\n", installdir);
228 printf("It looks like you did not unpack the runtime archive.\n");
229 printf("You must unpack the runtime archive \"vim%srt.zip\" before installing.\n",
230 VIM_VERSION_NODOT + 3);
231 myexit(1);
232 }
233
234 /* Check if vim.exe or gvim.exe is in the current directory. */
235 if ((fd = fopen("gvim.exe", "r")) != NULL)
236 {
237 fclose(fd);
238 has_gvim = 1;
239 }
240 if ((fd = fopen("vim.exe", "r")) != NULL)
241 {
242 fclose(fd);
243 has_vim = 1;
244 }
245 if (!has_gvim && !has_vim)
246 {
247 printf("ERROR: Cannot find any Vim executables in \"%s\"\n\n",
248 installdir);
249 myexit(1);
250 }
251}
252
253/*
254 * Compare paths "p[plen]" to "q[qlen]". Return 0 if they match.
255 * Ignores case and differences between '/' and '\'.
256 * "plen" and "qlen" can be negative, strlen() is used then.
257 */
258 static int
259pathcmp(char *p, int plen, char *q, int qlen)
260{
261 int i;
262
263 if (plen < 0)
264 plen = strlen(p);
265 if (qlen < 0)
266 qlen = strlen(q);
267 for (i = 0; ; ++i)
268 {
269 /* End of "p": check if "q" also ends or just has a slash. */
270 if (i == plen)
271 {
272 if (i == qlen) /* match */
273 return 0;
274 if (i == qlen - 1 && (q[i] == '\\' || q[i] == '/'))
275 return 0; /* match with trailing slash */
276 return 1; /* no match */
277 }
278
279 /* End of "q": check if "p" also ends or just has a slash. */
280 if (i == qlen)
281 {
282 if (i == plen) /* match */
283 return 0;
284 if (i == plen - 1 && (p[i] == '\\' || p[i] == '/'))
285 return 0; /* match with trailing slash */
286 return 1; /* no match */
287 }
288
289 if (!(mytoupper(p[i]) == mytoupper(q[i])
290 || ((p[i] == '/' || p[i] == '\\')
291 && (q[i] == '/' || q[i] == '\\'))))
292 return 1; /* no match */
293 }
294 /*NOTREACHED*/
295}
296
297/*
298 * If the executable "**destination" is in the install directory, find another
299 * one in $PATH.
300 * On input "**destination" is the path of an executable in allocated memory
301 * (or NULL).
302 * "*destination" is set to NULL or the location of the file.
303 */
304 static void
305findoldfile(char **destination)
306{
307 char *bp = *destination;
308 size_t indir_l = strlen(installdir);
309 char *cp = bp + indir_l;
310 char *tmpname;
311 char *farname;
312
313 /*
314 * No action needed if exe not found or not in this directory.
315 */
316 if (bp == NULL
317 || strnicmp(bp, installdir, indir_l) != 0
318 || strchr("/\\", *cp++) == NULL
319 || strchr(cp, '\\') != NULL
320 || strchr(cp, '/') != NULL)
321 return;
322
323 tmpname = alloc((int)strlen(cp) + 1);
324 strcpy(tmpname, cp);
325 tmpname[strlen(tmpname) - 1] = 'x'; /* .exe -> .exx */
326
327 if (access(tmpname, 0) == 0)
328 {
329 printf("\nERROR: %s and %s clash. Remove or rename %s.\n",
330 tmpname, cp, tmpname);
331 myexit(1);
332 }
333
334 if (rename(cp, tmpname) != 0)
335 {
336 printf("\nERROR: failed to rename %s to %s: %s\n",
337 cp, tmpname, strerror(0));
338 myexit(1);
339 }
340
341 farname = searchpath_save(cp);
342
343 if (rename(tmpname, cp) != 0)
344 {
345 printf("\nERROR: failed to rename %s back to %s: %s\n",
346 tmpname, cp, strerror(0));
347 myexit(1);
348 }
349
350 free(*destination);
351 free(tmpname);
352 *destination = farname;
353}
354
355/*
356 * Check if there is a vim.[exe|bat|, gvim.[exe|bat|, etc. in the path.
357 * When "check_bat_only" is TRUE, only find "default_bat_dir".
358 */
359 static void
360find_bat_exe(int check_bat_only)
361{
362 int i;
363
Bram Moolenaar42bbef42006-03-25 22:02:07 +0000364 /* avoid looking in the "installdir" by chdir to system root */
365 mch_chdir(sysdrive);
366 mch_chdir("\\");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367
368 for (i = 1; i < TARGET_COUNT; ++i)
369 {
370 targets[i].oldbat = searchpath_save(targets[i].batname);
371 if (!check_bat_only)
372 targets[i].oldexe = searchpath_save(targets[i].exename);
373
374 if (default_bat_dir == NULL && targets[i].oldbat != NULL)
375 {
376 default_bat_dir = alloc(strlen(targets[i].oldbat) + 1);
377 strcpy(default_bat_dir, targets[i].oldbat);
378 remove_tail(default_bat_dir);
379 }
380 if (check_bat_only && targets[i].oldbat != NULL)
Bram Moolenaar42bbef42006-03-25 22:02:07 +0000381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 free(targets[i].oldbat);
Bram Moolenaar42bbef42006-03-25 22:02:07 +0000383 targets[i].oldbat = NULL;
384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 }
386
387 mch_chdir(installdir);
388}
389
390#ifdef WIN3264
391/*
392 * Get the value of $VIMRUNTIME or $VIM and write it in $TEMP/vimini.ini, so
393 * that NSIS can read it.
394 * When not set, use the directory of a previously installed Vim.
395 */
396 static void
397get_vim_env(void)
398{
399 char *vim;
400 char buf[BUFSIZE];
401 FILE *fd;
402 char fname[BUFSIZE];
403
404 /* First get $VIMRUNTIME. If it's set, remove the tail. */
405 vim = getenv("VIMRUNTIME");
406 if (vim != NULL && *vim != 0)
407 {
408 strcpy(buf, vim);
409 remove_tail(buf);
410 vim = buf;
411 }
412 else
413 {
414 vim = getenv("VIM");
415 if (vim == NULL || *vim == 0)
416 {
417 /* Use the directory from an old uninstall entry. */
418 if (default_vim_dir != NULL)
419 vim = default_vim_dir;
420 else
421 /* Let NSIS know there is no default, it should use
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000422 * $PROGRAMFILES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 vim = "";
424 }
425 }
426
427 /* NSIS also uses GetTempPath(), thus we should get the same directory
428 * name as where NSIS will look for vimini.ini. */
429 GetTempPath(BUFSIZE, fname);
430 add_pathsep(fname);
431 strcat(fname, "vimini.ini");
432
433 fd = fopen(fname, "w");
434 if (fd != NULL)
435 {
436 /* Make it look like an .ini file, so that NSIS can read it with a
437 * ReadINIStr command. */
438 fprintf(fd, "[vimini]\n");
439 fprintf(fd, "dir=\"%s\"\n", vim);
440 fclose(fd);
441 }
442 else
443 {
444 printf("Failed to open %s\n", fname);
445 Sleep(2000);
446 }
447}
448
Bram Moolenaarb230bd52010-05-25 21:02:00 +0200449static int num_windows;
450
451/*
452 * Callback used for EnumWindows():
453 * Count the window if the title looks like it is for the uninstaller.
454 */
455/*ARGSUSED*/
456 static BOOL CALLBACK
457window_cb(HWND hwnd, LPARAM lparam)
458{
459 char title[256];
460
461 title[0] = 0;
462 GetWindowText(hwnd, title, 256);
463 if (strstr(title, "Vim ") != NULL && strstr(title, "Uninstall:") != NULL)
464 ++num_windows;
465 return TRUE;
466}
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468/*
469 * Check for already installed Vims.
470 * Return non-zero when found one.
471 */
472 static int
Bram Moolenaar442b4222010-05-24 21:34:22 +0200473uninstall_check(int skip_question)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
475 HKEY key_handle;
476 HKEY uninstall_key_handle;
477 char *uninstall_key = "software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
478 char subkey_name_buff[BUFSIZE];
479 char temp_string_buffer[BUFSIZE];
480 DWORD local_bufsize = BUFSIZE;
481 FILETIME temp_pfiletime;
482 DWORD key_index;
483 char input;
484 long code;
485 DWORD value_type;
486 DWORD orig_num_keys;
487 DWORD new_num_keys;
488 int foundone = 0;
489
490 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, uninstall_key, 0, KEY_READ,
491 &key_handle);
492 CHECK_REG_ERROR(code);
493
494 for (key_index = 0;
495 RegEnumKeyEx(key_handle, key_index, subkey_name_buff, &local_bufsize,
496 NULL, NULL, NULL, &temp_pfiletime) != ERROR_NO_MORE_ITEMS;
497 key_index++)
498 {
499 local_bufsize = BUFSIZE;
500 if (strncmp("Vim", subkey_name_buff, 3) == 0)
501 {
502 /* Open the key named Vim* */
503 code = RegOpenKeyEx(key_handle, subkey_name_buff, 0, KEY_READ,
504 &uninstall_key_handle);
505 CHECK_REG_ERROR(code);
506
507 /* get the DisplayName out of it to show the user */
508 code = RegQueryValueEx(uninstall_key_handle, "displayname", 0,
509 &value_type, (LPBYTE)temp_string_buffer,
510 &local_bufsize);
511 local_bufsize = BUFSIZE;
512 CHECK_REG_ERROR(code);
513
514 foundone = 1;
515 printf("\n*********************************************************\n");
516 printf("Vim Install found what looks like an existing Vim version.\n");
517 printf("The name of the entry is:\n");
518 printf("\n \"%s\"\n\n", temp_string_buffer);
519
520 printf("Installing the new version will disable part of the existing version.\n");
521 printf("(The batch files used in a console and the \"Edit with Vim\" entry in\n");
522 printf("the popup menu will use the new version)\n");
523
Bram Moolenaar442b4222010-05-24 21:34:22 +0200524 if (skip_question)
525 printf("\nRunning uninstall program for \"%s\"\n", temp_string_buffer);
526 else
527 printf("\nDo you want to uninstall \"%s\" now?\n(y)es/(n)o) ", temp_string_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 fflush(stdout);
529
530 /* get the UninstallString */
531 code = RegQueryValueEx(uninstall_key_handle, "uninstallstring", 0,
532 &value_type, (LPBYTE)temp_string_buffer, &local_bufsize);
533 local_bufsize = BUFSIZE;
534 CHECK_REG_ERROR(code);
535
536 /* Remember the directory, it is used as the default for NSIS. */
537 default_vim_dir = alloc(strlen(temp_string_buffer) + 1);
538 strcpy(default_vim_dir, temp_string_buffer);
539 remove_tail(default_vim_dir);
540 remove_tail(default_vim_dir);
541
542 input = 'n';
543 do
544 {
545 if (input != 'n')
546 printf("%c is an invalid reply. Please enter either 'y' or 'n'\n", input);
547
Bram Moolenaar442b4222010-05-24 21:34:22 +0200548 if (skip_question)
549 input = 'y';
550 else
551 {
552 rewind(stdin);
553 scanf("%c", &input);
554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 switch (input)
556 {
557 case 'y':
558 case 'Y':
559 /* save the number of uninstall keys so we can know if
560 * it changed */
561 RegQueryInfoKey(key_handle, NULL, NULL, NULL,
562 &orig_num_keys, NULL, NULL, NULL,
563 NULL, NULL, NULL, NULL);
564
Bram Moolenaar442b4222010-05-24 21:34:22 +0200565 /* Find existing .bat files before deleting them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 find_bat_exe(TRUE);
567
568 /* Execute the uninstall program. Put it in double
569 * quotes if there is an embedded space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
571 char buf[BUFSIZE];
572
Bram Moolenaarb230bd52010-05-25 21:02:00 +0200573 if (strchr(temp_string_buffer, ' ') != NULL)
574 sprintf(buf, "\"%s\"", temp_string_buffer);
575 else
576 strcpy(buf, temp_string_buffer);
577 run_command(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
Bram Moolenaarb230bd52010-05-25 21:02:00 +0200579
580 /* Count the number of windows with a title that match
581 * the installer, so that we can check when it's done.
582 * The uninstaller copies itself, executes the copy
583 * and exits, thus we can't wait for the process to
584 * finish. */
585 Sleep(1000); /* wait for uninstaller to start up */
586 num_windows = 0;
587 EnumWindows(window_cb, 0);
588 Sleep(1000); /* wait for windows to be counted */
589 if (num_windows == 0)
590 {
591 /* Did not find the uninstaller, ask user to press
592 * Enter when done. Just in case. */
593 printf("Press Enter when the uninstaller is finished\n");
594 rewind(stdin);
595 (void)getchar();
596 }
597 else
598 {
599 printf("Waiting for the uninstaller to finish (press CTRL-C to abort).");
600 do
601 {
602 printf(".");
603 fflush(stdout);
604 num_windows = 0;
605 EnumWindows(window_cb, 0);
606 Sleep(1000); /* wait for windows to be counted */
607 } while (num_windows > 0);
608 }
609 printf("\nDone!\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200611 /* Check if an uninstall reg key was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 * if it was, we want to decrement key_index.
613 * if we don't do this, we will skip the key
614 * immediately after any key that we delete. */
615 RegQueryInfoKey(key_handle, NULL, NULL, NULL,
616 &new_num_keys, NULL, NULL, NULL,
617 NULL, NULL, NULL, NULL);
618 if (new_num_keys < orig_num_keys)
619 key_index--;
620
621 input = 'y';
622 break;
623
624 case 'n':
625 case 'N':
626 /* Do not uninstall */
627 input = 'n';
628 break;
629
630 default: /* just drop through and redo the loop */
631 break;
632 }
633
634 } while (input != 'n' && input != 'y');
635
636 RegCloseKey(uninstall_key_handle);
637 }
638 }
639 RegCloseKey(key_handle);
640
641 return foundone;
642}
643#endif
644
645/*
646 * Find out information about the system.
647 */
648 static void
649inspect_system(void)
650{
651 char *p;
652 char buf[BUFSIZE];
653 FILE *fd;
654 int i;
655 int foundone;
656
657 /* This may take a little while, let the user know what we're doing. */
658 printf("Inspecting system...\n");
659
660 /*
661 * If $VIM is set, check that it's pointing to our directory.
662 */
663 p = getenv("VIM");
664 if (p != NULL && pathcmp(p, -1, installdir, runtimeidx - 1) != 0)
665 {
666 printf("------------------------------------------------------\n");
667 printf("$VIM is set to \"%s\".\n", p);
668 printf("This is different from where this version of Vim is:\n");
669 strcpy(buf, installdir);
670 *(buf + runtimeidx - 1) = NUL;
671 printf("\"%s\"\n", buf);
672 printf("You must adjust or remove the setting of $VIM,\n");
673 if (interactive)
674 {
675 printf("to be able to use this install program.\n");
676 myexit(1);
677 }
678 printf("otherwise Vim WILL NOT WORK properly!\n");
679 printf("------------------------------------------------------\n");
680 }
681
682 /*
683 * If $VIMRUNTIME is set, check that it's pointing to our runtime directory.
684 */
685 p = getenv("VIMRUNTIME");
686 if (p != NULL && pathcmp(p, -1, installdir, -1) != 0)
687 {
688 printf("------------------------------------------------------\n");
689 printf("$VIMRUNTIME is set to \"%s\".\n", p);
690 printf("This is different from where this version of Vim is:\n");
691 printf("\"%s\"\n", installdir);
692 printf("You must adjust or remove the setting of $VIMRUNTIME,\n");
693 if (interactive)
694 {
695 printf("to be able to use this install program.\n");
696 myexit(1);
697 }
698 printf("otherwise Vim WILL NOT WORK properly!\n");
699 printf("------------------------------------------------------\n");
700 }
701
702 /*
703 * Check if there is a vim.[exe|bat|, gvim.[exe|bat|, etc. in the path.
704 */
705 find_bat_exe(FALSE);
706
707 /*
708 * A .exe in the install directory may be found anyway on Windows 2000.
709 * Check for this situation and find another executable if necessary.
710 * w.briscoe@ponl.com 2001-01-20
711 */
712 foundone = 0;
713 for (i = 1; i < TARGET_COUNT; ++i)
714 {
715 findoldfile(&(targets[i].oldexe));
716 if (targets[i].oldexe != NULL)
717 foundone = 1;
718 }
719
720 if (foundone)
721 {
722 printf("Warning: Found Vim executable(s) in your $PATH:\n");
723 for (i = 1; i < TARGET_COUNT; ++i)
724 if (targets[i].oldexe != NULL)
725 printf("%s\n", targets[i].oldexe);
726 printf("It will be used instead of the version you are installing.\n");
727 printf("Please delete or rename it, or adjust your $PATH setting.\n");
728 }
729
730 /*
731 * Check if there is an existing ../_vimrc or ../.vimrc file.
732 */
733 strcpy(oldvimrc, installdir);
734 strcpy(oldvimrc + runtimeidx, "_vimrc");
735 if ((fd = fopen(oldvimrc, "r")) == NULL)
736 {
737 strcpy(oldvimrc + runtimeidx, "vimrc~1"); /* short version of .vimrc */
738 if ((fd = fopen(oldvimrc, "r")) == NULL)
739 {
740 strcpy(oldvimrc + runtimeidx, ".vimrc");
741 fd = fopen(oldvimrc, "r");
742 }
743 }
744 if (fd != NULL)
745 fclose(fd);
746 else
747 *oldvimrc = NUL;
748
749#if 0 /* currently not used */
750 /*
751 * Get default home directory.
752 * Prefer $HOME if it's set. For Win NT use $HOMEDRIVE and $HOMEPATH.
753 * Otherwise, if there is a "c:" drive use that.
754 */
755 p = getenv("HOME");
756 if (p != NULL && *p != NUL && strlen(p) < BUFSIZE)
757 strcpy(homedir, p);
758 else
759 {
760 p = getenv("HOMEDRIVE");
761 if (p != NULL && *p != NUL && strlen(p) + 2 < BUFSIZE)
762 {
763 strcpy(homedir, p);
764 p = getenv("HOMEPATH");
765 if (p != NULL && *p != NUL && strlen(homedir) + strlen(p) < BUFSIZE)
766 strcat(homedir, p);
767 else
768 strcat(homedir, "\\");
769 }
770 else
771 {
772 struct stat st;
773
774 if (stat("c:\\", &st) == 0)
775 strcpy(homedir, "c:\\");
776 else
777 *homedir = NUL;
778 }
779 }
780#endif
781}
782
783/*
784 * Add a dummy choice to avoid that the numbering changes depending on items
785 * in the environment. The user may type a number he remembered without
786 * looking.
787 */
788 static void
789add_dummy_choice(void)
790{
791 choices[choice_count].installfunc = NULL;
792 choices[choice_count].active = 0;
793 choices[choice_count].changefunc = NULL;
794 choices[choice_count].installfunc = NULL;
795 ++choice_count;
796}
797
798/***********************************************
799 * stuff for creating the batch files.
800 */
801
802/*
803 * Install the vim.bat, gvim.bat, etc. files.
804 */
805 static void
806install_bat_choice(int idx)
807{
808 char *batpath = targets[choices[idx].arg].batpath;
809 char *oldname = targets[choices[idx].arg].oldbat;
810 char *exename = targets[choices[idx].arg].exenamearg;
811 char *vimarg = targets[choices[idx].arg].exearg;
812 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 if (*batpath != NUL)
815 {
816 fd = fopen(batpath, "w");
817 if (fd == NULL)
818 printf("\nERROR: Cannot open \"%s\" for writing.\n", batpath);
819 else
820 {
821 need_uninstall_entry = 1;
822
823 fprintf(fd, "@echo off\n");
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000824 fprintf(fd, "rem -- Run Vim --\n");
825 fprintf(fd, "\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000827 /* Don't use double quotes for the "set" argument, also when it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 * contains a space. The quotes would be included in the value
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000829 * for MSDOS and NT.
830 * The order of preference is:
831 * 1. $VIMRUNTIME/vim.exe (user preference)
832 * 2. $VIM/vim70/vim.exe (hard coded version)
833 * 3. installdir/vim.exe (hard coded install directory)
834 */
835 fprintf(fd, "set VIM_EXE_DIR=%s\n", installdir);
836 fprintf(fd, "if exist \"%%VIM%%\\%s\\%s\" set VIM_EXE_DIR=%%VIM%%\\%s\n",
837 VIM_VERSION_NODOT, exename, VIM_VERSION_NODOT);
838 fprintf(fd, "if exist \"%%VIMRUNTIME%%\\%s\" set VIM_EXE_DIR=%%VIMRUNTIME%%\n", exename);
839 fprintf(fd, "\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
841 /* Give an error message when the executable could not be found. */
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000842 fprintf(fd, "if exist \"%%VIM_EXE_DIR%%\\%s\" goto havevim\n",
843 exename);
844 fprintf(fd, "echo \"%%VIM_EXE_DIR%%\\%s\" not found\n", exename);
845 fprintf(fd, "goto eof\n");
846 fprintf(fd, "\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 fprintf(fd, ":havevim\n");
848
849 fprintf(fd, "rem collect the arguments in VIMARGS for Win95\n");
850 fprintf(fd, "set VIMARGS=\n");
851 if (*exename == 'g')
852 fprintf(fd, "set VIMNOFORK=\n");
853 fprintf(fd, ":loopstart\n");
854 fprintf(fd, "if .%%1==. goto loopend\n");
855 if (*exename == 'g')
856 {
857 fprintf(fd, "if NOT .%%1==.-f goto noforkarg\n");
858 fprintf(fd, "set VIMNOFORK=1\n");
859 fprintf(fd, ":noforkarg\n");
860 }
861 fprintf(fd, "set VIMARGS=%%VIMARGS%% %%1\n");
862 fprintf(fd, "shift\n");
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000863 fprintf(fd, "goto loopstart\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 fprintf(fd, ":loopend\n");
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000865 fprintf(fd, "\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000867 fprintf(fd, "if .%%OS%%==.Windows_NT goto ntaction\n");
868 fprintf(fd, "\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 /* For gvim.exe use "start" to avoid that the console window stays
871 * open. */
872 if (*exename == 'g')
873 {
874 fprintf(fd, "if .%%VIMNOFORK%%==.1 goto nofork\n");
875 fprintf(fd, "start ");
876 }
877
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000878 /* Always use quotes, $VIM or $VIMRUNTIME might have a space. */
879 fprintf(fd, "\"%%VIM_EXE_DIR%%\\%s\" %s %%VIMARGS%%\n",
880 exename, vimarg);
881 fprintf(fd, "goto eof\n");
882 fprintf(fd, "\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884 if (*exename == 'g')
885 {
886 fprintf(fd, ":nofork\n");
887 fprintf(fd, "start /w ");
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000888 /* Always use quotes, $VIM or $VIMRUNTIME might have a space. */
889 fprintf(fd, "\"%%VIM_EXE_DIR%%\\%s\" %s %%VIMARGS%%\n",
890 exename, vimarg);
891 fprintf(fd, "goto eof\n");
892 fprintf(fd, "\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 }
894
895 fprintf(fd, ":ntaction\n");
896 fprintf(fd, "rem for WinNT we can use %%*\n");
897
898 /* For gvim.exe use "start /b" to avoid that the console window
899 * stays open. */
900 if (*exename == 'g')
901 {
902 fprintf(fd, "if .%%VIMNOFORK%%==.1 goto noforknt\n");
903 fprintf(fd, "start \"dummy\" /b ");
904 }
905
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000906 /* Always use quotes, $VIM or $VIMRUNTIME might have a space. */
907 fprintf(fd, "\"%%VIM_EXE_DIR%%\\%s\" %s %%*\n", exename, vimarg);
908 fprintf(fd, "goto eof\n");
909 fprintf(fd, "\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
911 if (*exename == 'g')
912 {
913 fprintf(fd, ":noforknt\n");
914 fprintf(fd, "start \"dummy\" /b /wait ");
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000915 /* Always use quotes, $VIM or $VIMRUNTIME might have a space. */
916 fprintf(fd, "\"%%VIM_EXE_DIR%%\\%s\" %s %%*\n",
917 exename, vimarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919
920 fprintf(fd, "\n:eof\n");
921 fprintf(fd, "set VIMARGS=\n");
922 if (*exename == 'g')
923 fprintf(fd, "set VIMNOFORK=\n");
924
925 fclose(fd);
926 printf("%s has been %s\n", batpath,
927 oldname == NULL ? "created" : "overwritten");
928 }
929 }
930}
931
932/*
933 * Make the text string for choice "idx".
934 * The format "fmt" is must have one %s item, which "arg" is used for.
935 */
936 static void
937alloc_text(int idx, char *fmt, char *arg)
938{
939 if (choices[idx].text != NULL)
940 free(choices[idx].text);
941
942 choices[idx].text = alloc((int)(strlen(fmt) + strlen(arg)) - 1);
943 sprintf(choices[idx].text, fmt, arg);
944}
945
946/*
947 * Toggle the "Overwrite .../vim.bat" to "Don't overwrite".
948 */
949 static void
950toggle_bat_choice(int idx)
951{
952 char *batname = targets[choices[idx].arg].batpath;
953 char *oldname = targets[choices[idx].arg].oldbat;
954
955 if (*batname == NUL)
956 {
957 alloc_text(idx, " Overwrite %s", oldname);
958 strcpy(batname, oldname);
959 }
960 else
961 {
962 alloc_text(idx, " Do NOT overwrite %s", oldname);
963 *batname = NUL;
964 }
965}
966
967/*
968 * Do some work for a batch file entry: Append the batch file name to the path
969 * and set the text for the choice.
970 */
971 static void
972set_bat_text(int idx, char *batpath, char *name)
973{
974 strcat(batpath, name);
975
976 alloc_text(idx, " Create %s", batpath);
977}
978
979/*
980 * Select a directory to write the batch file line.
981 */
982 static void
983change_bat_choice(int idx)
984{
985 char *path;
986 char *batpath;
987 char *name;
988 int n;
989 char *s;
990 char *p;
991 int count;
992 char **names = NULL;
993 int i;
994 int target = choices[idx].arg;
995
996 name = targets[target].batname;
997 batpath = targets[target].batpath;
998
999 path = getenv("PATH");
1000 if (path == NULL)
1001 {
1002 printf("\nERROR: The variable $PATH is not set\n");
1003 return;
1004 }
1005
1006 /*
1007 * first round: count number of names in path;
1008 * second round: save names to names[].
1009 */
1010 for (;;)
1011 {
1012 count = 1;
1013 for (p = path; *p; )
1014 {
1015 s = strchr(p, ';');
1016 if (s == NULL)
1017 s = p + strlen(p);
1018 if (names != NULL)
1019 {
1020 names[count] = alloc((int)(s - p) + 1);
1021 strncpy(names[count], p, s - p);
1022 names[count][s - p] = NUL;
1023 }
1024 ++count;
1025 p = s;
1026 if (*p != NUL)
1027 ++p;
1028 }
1029 if (names != NULL)
1030 break;
1031 names = alloc((int)(count + 1) * sizeof(char *));
1032 }
1033 names[0] = alloc(50);
1034 sprintf(names[0], "Select directory to create %s in:", name);
1035 names[count] = alloc(50);
1036 if (choices[idx].arg == 0)
1037 sprintf(names[count], "Do not create any .bat file.");
1038 else
1039 sprintf(names[count], "Do not create a %s file.", name);
1040 n = get_choice(names, count + 1);
1041
1042 if (n == count)
1043 {
1044 /* Selected last item, don't create bat file. */
1045 *batpath = NUL;
1046 if (choices[idx].arg != 0)
1047 alloc_text(idx, " Do NOT create %s", name);
1048 }
1049 else
1050 {
1051 /* Selected one of the paths. For the first item only keep the path,
1052 * for the others append the batch file name. */
1053 strcpy(batpath, names[n]);
1054 add_pathsep(batpath);
1055 if (choices[idx].arg != 0)
1056 set_bat_text(idx, batpath, name);
1057 }
1058
1059 for (i = 0; i <= count; ++i)
1060 free(names[i]);
1061 free(names);
1062}
1063
1064char *bat_text_yes = "Install .bat files to use Vim at the command line:";
1065char *bat_text_no = "do NOT install .bat files to use Vim at the command line";
1066
1067 static void
1068change_main_bat_choice(int idx)
1069{
1070 int i;
1071
1072 /* let the user select a default directory or NONE */
1073 change_bat_choice(idx);
1074
1075 if (targets[0].batpath[0] != NUL)
1076 choices[idx].text = bat_text_yes;
1077 else
1078 choices[idx].text = bat_text_no;
1079
1080 /* update the individual batch file selections */
1081 for (i = 1; i < TARGET_COUNT; ++i)
1082 {
1083 /* Only make it active when the first item has a path and the vim.exe
1084 * or gvim.exe exists (there is a changefunc then). */
1085 if (targets[0].batpath[0] != NUL
1086 && choices[idx + i].changefunc != NULL)
1087 {
1088 choices[idx + i].active = 1;
1089 if (choices[idx + i].changefunc == change_bat_choice
1090 && targets[i].batpath[0] != NUL)
1091 {
1092 strcpy(targets[i].batpath, targets[0].batpath);
1093 set_bat_text(idx + i, targets[i].batpath, targets[i].batname);
1094 }
1095 }
1096 else
1097 choices[idx + i].active = 0;
1098 }
1099}
1100
1101/*
1102 * Initialize a choice for creating a batch file.
1103 */
1104 static void
1105init_bat_choice(int target)
1106{
1107 char *batpath = targets[target].batpath;
1108 char *oldbat = targets[target].oldbat;
1109 char *p;
1110 int i;
1111
1112 choices[choice_count].arg = target;
1113 choices[choice_count].installfunc = install_bat_choice;
1114 choices[choice_count].active = 1;
1115 choices[choice_count].text = NULL; /* will be set below */
1116 if (oldbat != NULL)
1117 {
1118 /* A [g]vim.bat exists: Only choice is to overwrite it or not. */
1119 choices[choice_count].changefunc = toggle_bat_choice;
1120 *batpath = NUL;
1121 toggle_bat_choice(choice_count);
1122 }
1123 else
1124 {
1125 if (default_bat_dir != NULL)
1126 /* Prefer using the same path as an existing .bat file. */
1127 strcpy(batpath, default_bat_dir);
1128 else
1129 {
1130 /* No [g]vim.bat exists: Write it to a directory in $PATH. Use
1131 * $WINDIR by default, if it's empty the first item in $PATH. */
1132 p = getenv("WINDIR");
1133 if (p != NULL && *p != NUL)
1134 strcpy(batpath, p);
1135 else
1136 {
1137 p = getenv("PATH");
1138 if (p == NULL || *p == NUL) /* "cannot happen" */
1139 strcpy(batpath, "C:/Windows");
1140 else
1141 {
1142 i = 0;
1143 while (*p != NUL && *p != ';')
1144 batpath[i++] = *p++;
1145 batpath[i] = NUL;
1146 }
1147 }
1148 }
1149 add_pathsep(batpath);
1150 set_bat_text(choice_count, batpath, targets[target].batname);
1151
1152 choices[choice_count].changefunc = change_bat_choice;
1153 }
1154 ++choice_count;
1155}
1156
1157/*
1158 * Set up the choices for installing .bat files.
1159 * For these items "arg" is the index in targets[].
1160 */
1161 static void
1162init_bat_choices(void)
1163{
1164 int i;
1165
1166 /* The first item is used to switch installing batch files on/off and
1167 * setting the default path. */
1168 choices[choice_count].text = bat_text_yes;
1169 choices[choice_count].changefunc = change_main_bat_choice;
1170 choices[choice_count].installfunc = NULL;
1171 choices[choice_count].active = 1;
1172 choices[choice_count].arg = 0;
1173 ++choice_count;
1174
1175 /* Add items for each batch file target. Only used when not disabled by
1176 * the first item. When a .exe exists, don't offer to create a .bat. */
1177 for (i = 1; i < TARGET_COUNT; ++i)
1178 if (targets[i].oldexe == NULL
1179 && (targets[i].exenamearg[0] == 'g' ? has_gvim : has_vim))
1180 init_bat_choice(i);
1181 else
1182 add_dummy_choice();
1183}
1184
1185/*
1186 * Install the vimrc file.
1187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 static void
1189install_vimrc(int idx)
1190{
1191 FILE *fd, *tfd;
1192 char *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
1194 /* If an old vimrc file exists, overwrite it.
1195 * Otherwise create a new one. */
1196 if (*oldvimrc != NUL)
1197 fname = oldvimrc;
1198 else
1199 fname = vimrc;
1200
1201 fd = fopen(fname, "w");
1202 if (fd == NULL)
1203 {
1204 printf("\nERROR: Cannot open \"%s\" for writing.\n", fname);
1205 return;
1206 }
1207 switch (compat_choice)
1208 {
1209 case compat_vi:
1210 fprintf(fd, "set compatible\n");
1211 break;
1212 case compat_some_enhancements:
1213 fprintf(fd, "set nocompatible\n");
1214 break;
1215 case compat_all_enhancements:
1216 fprintf(fd, "set nocompatible\n");
1217 fprintf(fd, "source $VIMRUNTIME/vimrc_example.vim\n");
1218 break;
1219 }
1220 switch (remap_choice)
1221 {
1222 case remap_no:
1223 break;
1224 case remap_win:
1225 fprintf(fd, "source $VIMRUNTIME/mswin.vim\n");
1226 break;
1227 }
1228 switch (mouse_choice)
1229 {
1230 case mouse_xterm:
1231 fprintf(fd, "behave xterm\n");
1232 break;
1233 case mouse_mswin:
1234 fprintf(fd, "behave mswin\n");
1235 break;
1236 }
1237 if ((tfd = fopen("diff.exe", "r")) != NULL)
1238 {
1239 /* Use the diff.exe that comes with the self-extracting gvim.exe. */
1240 fclose(tfd);
1241 fprintf(fd, "\n");
1242 fprintf(fd, "set diffexpr=MyDiff()\n");
1243 fprintf(fd, "function MyDiff()\n");
1244 fprintf(fd, " let opt = '-a --binary '\n");
1245 fprintf(fd, " if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif\n");
1246 fprintf(fd, " if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif\n");
1247 /* Use quotes only when needed, they may cause trouble. */
1248 fprintf(fd, " let arg1 = v:fname_in\n");
1249 fprintf(fd, " if arg1 =~ ' ' | let arg1 = '\"' . arg1 . '\"' | endif\n");
1250 fprintf(fd, " let arg2 = v:fname_new\n");
1251 fprintf(fd, " if arg2 =~ ' ' | let arg2 = '\"' . arg2 . '\"' | endif\n");
1252 fprintf(fd, " let arg3 = v:fname_out\n");
1253 fprintf(fd, " if arg3 =~ ' ' | let arg3 = '\"' . arg3 . '\"' | endif\n");
Bram Moolenaar33aec762006-01-22 23:30:12 +00001254
1255 /* If the path has a space: When using cmd.exe (Win NT/2000/XP) put
1256 * quotes around the whole command and around the diff command.
1257 * Otherwise put a double quote just before the space and at the
1258 * end of the command. Putting quotes around the whole thing
1259 * doesn't work on Win 95/98/ME. This is mostly guessed! */
1260 fprintf(fd, " let eq = ''\n");
1261 fprintf(fd, " if $VIMRUNTIME =~ ' '\n");
1262 fprintf(fd, " if &sh =~ '\\<cmd'\n");
1263 fprintf(fd, " let cmd = '\"\"' . $VIMRUNTIME . '\\diff\"'\n");
1264 fprintf(fd, " let eq = '\"'\n");
1265 fprintf(fd, " else\n");
1266 fprintf(fd, " let cmd = substitute($VIMRUNTIME, ' ', '\" ', '') . '\\diff\"'\n");
1267 fprintf(fd, " endif\n");
1268 fprintf(fd, " else\n");
1269 fprintf(fd, " let cmd = $VIMRUNTIME . '\\diff'\n");
1270 fprintf(fd, " endif\n");
1271 fprintf(fd, " silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq\n");
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 fprintf(fd, "endfunction\n");
1274 fprintf(fd, "\n");
1275 }
1276 fclose(fd);
1277 printf("%s has been written\n", fname);
1278}
1279
1280 static void
1281change_vimrc_choice(int idx)
1282{
1283 if (choices[idx].installfunc != NULL)
1284 {
1285 /* Switch to NOT change or create a vimrc file. */
1286 if (*oldvimrc != NUL)
1287 alloc_text(idx, "Do NOT change startup file %s", oldvimrc);
1288 else
1289 alloc_text(idx, "Do NOT create startup file %s", vimrc);
1290 choices[idx].installfunc = NULL;
1291 choices[idx + 1].active = 0;
1292 choices[idx + 2].active = 0;
1293 choices[idx + 3].active = 0;
1294 }
1295 else
1296 {
1297 /* Switch to change or create a vimrc file. */
1298 if (*oldvimrc != NUL)
1299 alloc_text(idx, "Overwrite startup file %s with:", oldvimrc);
1300 else
1301 alloc_text(idx, "Create startup file %s with:", vimrc);
1302 choices[idx].installfunc = install_vimrc;
1303 choices[idx + 1].active = 1;
1304 choices[idx + 2].active = 1;
1305 choices[idx + 3].active = 1;
1306 }
1307}
1308
1309/*
1310 * Change the choice how to run Vim.
1311 */
1312 static void
1313change_run_choice(int idx)
1314{
1315 compat_choice = get_choice(compat_choices, TABLE_SIZE(compat_choices));
1316 alloc_text(idx, compat_text, compat_choices[compat_choice]);
1317}
1318
1319/*
1320 * Change the choice if keys are to be remapped.
1321 */
1322 static void
1323change_remap_choice(int idx)
1324{
1325 remap_choice = get_choice(remap_choices, TABLE_SIZE(remap_choices));
1326 alloc_text(idx, remap_text, remap_choices[remap_choice]);
1327}
1328
1329/*
1330 * Change the choice how to select text.
1331 */
1332 static void
1333change_mouse_choice(int idx)
1334{
1335 mouse_choice = get_choice(mouse_choices, TABLE_SIZE(mouse_choices));
1336 alloc_text(idx, mouse_text, mouse_choices[mouse_choice]);
1337}
1338
1339 static void
1340init_vimrc_choices(void)
1341{
1342 /* set path for a new _vimrc file (also when not used) */
1343 strcpy(vimrc, installdir);
1344 strcpy(vimrc + runtimeidx, "_vimrc");
1345
1346 /* Set opposite value and then toggle it by calling change_vimrc_choice() */
1347 if (*oldvimrc == NUL)
1348 choices[choice_count].installfunc = NULL;
1349 else
1350 choices[choice_count].installfunc = install_vimrc;
1351 choices[choice_count].text = NULL;
1352 change_vimrc_choice(choice_count);
1353 choices[choice_count].changefunc = change_vimrc_choice;
1354 choices[choice_count].active = 1;
1355 ++choice_count;
1356
1357 /* default way to run Vim */
1358 alloc_text(choice_count, compat_text, compat_choices[compat_choice]);
1359 choices[choice_count].changefunc = change_run_choice;
1360 choices[choice_count].installfunc = NULL;
1361 choices[choice_count].active = (*oldvimrc == NUL);
1362 ++choice_count;
1363
1364 /* Whether to remap keys */
1365 alloc_text(choice_count, remap_text , remap_choices[remap_choice]);
1366 choices[choice_count].changefunc = change_remap_choice;
1367 choices[choice_count].installfunc = NULL;;
1368 choices[choice_count].active = (*oldvimrc == NUL);
1369 ++choice_count;
1370
1371 /* default way to use the mouse */
1372 alloc_text(choice_count, mouse_text, mouse_choices[mouse_choice]);
1373 choices[choice_count].changefunc = change_mouse_choice;
1374 choices[choice_count].installfunc = NULL;;
1375 choices[choice_count].active = (*oldvimrc == NUL);
1376 ++choice_count;
1377}
1378
1379/*
1380 * Add some entries to the registry:
1381 * - to add "Edit with Vim" to the context * menu
1382 * - to add Vim to the "Open with..." list
1383 * - to uninstall Vim
1384 */
1385/*ARGSUSED*/
1386 static void
1387install_registry(void)
1388{
1389#if defined(DJGPP) || defined(WIN3264) || defined(UNIX_LINT)
1390 FILE *fd;
1391 const char *vim_ext_ThreadingModel = "Apartment";
1392 const char *vim_ext_name = "Vim Shell Extension";
1393 const char *vim_ext_clsid = "{51EEE242-AD87-11d3-9C1E-0090278BBD99}";
1394 char buf[BUFSIZE];
1395
1396 fd = fopen("vim.reg", "w");
1397 if (fd == NULL)
1398 printf("ERROR: Could not open vim.reg for writing\n");
1399 else
1400 {
1401 double_bs(installdir, buf); /* double the backslashes */
1402
1403 /*
1404 * Write the registry entries for the "Edit with Vim" menu.
1405 */
1406 fprintf(fd, "REGEDIT4\n");
1407 fprintf(fd, "\n");
1408 if (install_popup)
1409 {
1410 char bufg[BUFSIZE];
1411 struct stat st;
1412
1413 if (stat("gvimext.dll", &st) >= 0)
1414 strcpy(bufg, buf);
1415 else
1416 /* gvimext.dll is in gvimext subdir */
1417 sprintf(bufg, "%sgvimext\\\\", buf);
1418
1419 printf("Creating \"Edit with Vim\" popup menu entry\n");
1420
Bram Moolenaar0fde2902008-03-16 13:54:13 +00001421 fprintf(fd, "[HKEY_CLASSES_ROOT\\CLSID\\%s]\n", vim_ext_clsid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 fprintf(fd, "@=\"%s\"\n", vim_ext_name);
1423 fprintf(fd, "[HKEY_CLASSES_ROOT\\CLSID\\%s\\InProcServer32]\n",
1424 vim_ext_clsid);
1425 fprintf(fd, "@=\"%sgvimext.dll\"\n", bufg);
1426 fprintf(fd, "\"ThreadingModel\"=\"%s\"\n", vim_ext_ThreadingModel);
1427 fprintf(fd, "\n");
1428 fprintf(fd, "[HKEY_CLASSES_ROOT\\*\\shellex\\ContextMenuHandlers\\gvim]\n");
1429 fprintf(fd, "@=\"%s\"\n", vim_ext_clsid);
1430 fprintf(fd, "\n");
1431 fprintf(fd, "[HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved]\n");
1432 fprintf(fd, "\"%s\"=\"%s\"\n", vim_ext_clsid, vim_ext_name);
1433 fprintf(fd, "\n");
1434 fprintf(fd, "[HKEY_LOCAL_MACHINE\\Software\\Vim\\Gvim]\n");
1435 fprintf(fd, "\"path\"=\"%sgvim.exe\"\n", buf);
1436 fprintf(fd, "\n");
1437 }
1438
1439 if (install_openwith)
1440 {
1441 printf("Creating \"Open with ...\" list entry\n");
1442
1443 fprintf(fd, "[HKEY_CLASSES_ROOT\\Applications\\gvim.exe]\n\n");
1444 fprintf(fd, "[HKEY_CLASSES_ROOT\\Applications\\gvim.exe\\shell]\n\n");
1445 fprintf(fd, "[HKEY_CLASSES_ROOT\\Applications\\gvim.exe\\shell\\edit]\n\n");
1446 fprintf(fd, "[HKEY_CLASSES_ROOT\\Applications\\gvim.exe\\shell\\edit\\command]\n");
1447 fprintf(fd, "@=\"%sgvim.exe \\\"%%1\\\"\"\n\n", buf);
1448 fprintf(fd, "[HKEY_CLASSES_ROOT\\.htm\\OpenWithList\\gvim.exe]\n\n");
1449 fprintf(fd, "[HKEY_CLASSES_ROOT\\.vim\\OpenWithList\\gvim.exe]\n\n");
1450 fprintf(fd, "[HKEY_CLASSES_ROOT\\*\\OpenWithList\\gvim.exe]\n\n");
1451 }
1452
1453 printf("Creating an uninstall entry\n");
1454
1455 /* The registry entries for uninstalling the menu */
1456 fprintf(fd, "[HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Vim " VIM_VERSION_SHORT "]\n");
1457
1458 /* For the NSIS installer use the generated uninstaller. */
1459 if (interactive)
1460 {
1461 fprintf(fd, "\"DisplayName\"=\"Vim " VIM_VERSION_SHORT "\"\n");
1462 fprintf(fd, "\"UninstallString\"=\"%suninstal.exe\"\n", buf);
1463 }
1464 else
1465 {
1466 fprintf(fd, "\"DisplayName\"=\"Vim " VIM_VERSION_SHORT " (self-installing)\"\n");
1467 fprintf(fd, "\"UninstallString\"=\"%suninstall-gui.exe\"\n", buf);
1468 }
1469
1470 fclose(fd);
1471
1472 run_command("regedit /s vim.reg");
1473
1474 remove("vim.reg");
1475 }
1476#endif /* if defined(DJGPP) || defined(WIN3264) */
1477}
1478
1479 static void
1480change_popup_choice(int idx)
1481{
1482 if (install_popup == 0)
1483 {
1484 choices[idx].text = "Install an entry for Vim in the popup menu for the right\n mouse button so that you can edit any file with Vim";
1485 install_popup = 1;
1486 }
1487 else
1488 {
1489 choices[idx].text = "Do NOT install an entry for Vim in the popup menu for the\n right mouse button to edit any file with Vim";
1490 install_popup = 0;
1491 }
1492}
1493
1494/*
1495 * Only add the choice for the popup menu entry when gvim.exe was found and
1496 * both gvimext.dll and regedit.exe exist.
1497 */
1498 static void
1499init_popup_choice(void)
1500{
1501 struct stat st;
1502
1503 if (has_gvim
1504 && (stat("gvimext.dll", &st) >= 0
1505 || stat("gvimext/gvimext.dll", &st) >= 0)
1506#ifndef WIN3264
1507 && searchpath("regedit.exe") != NULL
1508#endif
1509 )
1510 {
1511 choices[choice_count].changefunc = change_popup_choice;
1512 choices[choice_count].installfunc = NULL;
1513 choices[choice_count].active = 1;
1514 change_popup_choice(choice_count); /* set the text */
1515 ++choice_count;
1516 }
1517 else
1518 add_dummy_choice();
1519}
1520
1521 static void
1522change_openwith_choice(int idx)
1523{
1524 if (install_openwith == 0)
1525 {
1526 choices[idx].text = "Add Vim to the \"Open With...\" list in the popup menu for the right\n mouse button so that you can edit any file with Vim";
1527 install_openwith = 1;
1528 }
1529 else
1530 {
1531 choices[idx].text = "Do NOT add Vim to the \"Open With...\" list in the popup menu for the\n right mouse button to edit any file with Vim";
1532 install_openwith = 0;
1533 }
1534}
1535
1536/*
1537 * Only add the choice for the open-with menu entry when gvim.exe was found
1538 * and and regedit.exe exist.
1539 */
1540 static void
1541init_openwith_choice(void)
1542{
1543 if (has_gvim
1544#ifndef WIN3264
1545 && searchpath("regedit.exe") != NULL
1546#endif
1547 )
1548 {
1549 choices[choice_count].changefunc = change_openwith_choice;
1550 choices[choice_count].installfunc = NULL;
1551 choices[choice_count].active = 1;
1552 change_openwith_choice(choice_count); /* set the text */
1553 ++choice_count;
1554 }
1555 else
1556 add_dummy_choice();
1557}
1558
1559#ifdef WIN3264
1560/* create_shortcut
1561 *
1562 * Create a shell link.
1563 *
1564 * returns 0 on failure, non-zero on successful completion.
1565 *
1566 * NOTE: Currently untested with mingw.
1567 */
1568 int
1569create_shortcut(
1570 const char *shortcut_name,
1571 const char *iconfile_path,
1572 int iconindex,
1573 const char *shortcut_target,
1574 const char *shortcut_args,
1575 const char *workingdir
1576 )
1577{
1578 IShellLink *shelllink_ptr;
1579 HRESULT hres;
1580 IPersistFile *persistfile_ptr;
1581
1582 /* Initialize COM library */
1583 hres = CoInitialize(NULL);
1584 if (!SUCCEEDED(hres))
1585 {
1586 printf("Error: Could not open the COM library. Not creating shortcut.\n");
1587 return FAIL;
1588 }
1589
1590 /* Instantiate a COM object for the ShellLink, store a pointer to it
1591 * in shelllink_ptr. */
1592 hres = CoCreateInstance(&CLSID_ShellLink,
1593 NULL,
1594 CLSCTX_INPROC_SERVER,
1595 &IID_IShellLink,
1596 (void **) &shelllink_ptr);
1597
1598 if (SUCCEEDED(hres)) /* If the instantiation was successful... */
1599 {
1600 /* ...Then build a PersistFile interface for the ShellLink so we can
1601 * save it as a file after we build it. */
1602 hres = shelllink_ptr->lpVtbl->QueryInterface(shelllink_ptr,
1603 &IID_IPersistFile, (void **) &persistfile_ptr);
1604
1605 if (SUCCEEDED(hres))
1606 {
1607 wchar_t wsz[BUFSIZE];
1608
1609 /* translate the (possibly) multibyte shortcut filename to windows
1610 * Unicode so it can be used as a file name.
1611 */
1612 MultiByteToWideChar(CP_ACP, 0, shortcut_name, -1, wsz, BUFSIZE);
1613
1614 /* set the attributes */
1615 shelllink_ptr->lpVtbl->SetPath(shelllink_ptr, shortcut_target);
1616 shelllink_ptr->lpVtbl->SetWorkingDirectory(shelllink_ptr,
1617 workingdir);
1618 shelllink_ptr->lpVtbl->SetIconLocation(shelllink_ptr,
1619 iconfile_path, iconindex);
1620 shelllink_ptr->lpVtbl->SetArguments(shelllink_ptr, shortcut_args);
1621
1622 /* save the shortcut to a file and return the PersistFile object*/
1623 persistfile_ptr->lpVtbl->Save(persistfile_ptr, wsz, 1);
1624 persistfile_ptr->lpVtbl->Release(persistfile_ptr);
1625 }
1626 else
1627 {
1628 printf("QueryInterface Error\n");
1629 return FAIL;
1630 }
1631
1632 /* Return the ShellLink object */
1633 shelllink_ptr->lpVtbl->Release(shelllink_ptr);
1634 }
1635 else
1636 {
1637 printf("CoCreateInstance Error - hres = %08x\n", (int)hres);
1638 return FAIL;
1639 }
1640
1641 return OK;
1642}
1643
1644/*
1645 * Build a path to where we will put a specified link.
1646 *
1647 * Return 0 on error, non-zero on success
1648 */
1649 int
1650build_link_name(
1651 char *link_path,
1652 const char *link_name,
1653 const char *shell_folder_name)
1654{
1655 char shell_folder_path[BUFSIZE];
1656
1657 if (get_shell_folder_path(shell_folder_path, shell_folder_name) == FAIL)
1658 {
1659 printf("An error occurred while attempting to find the path to %s.\n",
1660 shell_folder_name);
1661 return FAIL;
1662 }
1663
1664 /* Make sure the directory exists (create Start Menu\Programs\Vim).
1665 * Ignore errors if it already exists. */
1666 vim_mkdir(shell_folder_path, 0755);
1667
1668 /* build the path to the shortcut and the path to gvim.exe */
1669 sprintf(link_path, "%s\\%s.lnk", shell_folder_path, link_name);
1670
1671 return OK;
1672}
1673
1674 static int
1675build_shortcut(
1676 const char *name, /* Name of the shortcut */
1677 const char *exename, /* Name of the executable (e.g., vim.exe) */
1678 const char *args,
1679 const char *shell_folder,
1680 const char *workingdir)
1681{
1682 char executable_path[BUFSIZE];
1683 char link_name[BUFSIZE];
1684
1685 sprintf(executable_path, "%s\\%s", installdir, exename);
1686
1687 if (build_link_name(link_name, name, shell_folder) == FAIL)
1688 {
1689 printf("An error has occurred. A shortcut to %s will not be created %s.\n",
1690 name,
1691 *shell_folder == 'd' ? "on the desktop" : "in the Start menu");
1692 return FAIL;
1693 }
1694
1695 /* Create the shortcut: */
1696 return create_shortcut(link_name, executable_path, 0,
1697 executable_path, args, workingdir);
1698}
1699
1700/*
1701 * We used to use "homedir" as the working directory, but that is a bad choice
1702 * on multi-user systems. Not specifying a directory appears to work best.
1703 */
1704#define WORKDIR ""
1705
1706/*
1707 * Create shortcut(s) in the Start Menu\Programs\Vim folder.
1708 */
1709 static void
1710install_start_menu(int idx)
1711{
1712 need_uninstall_entry = 1;
1713 printf("Creating start menu\n");
1714 if (has_vim)
1715 {
1716 if (build_shortcut("Vim", "vim.exe", "",
1717 VIM_STARTMENU, WORKDIR) == FAIL)
1718 return;
1719 if (build_shortcut("Vim Read-only", "vim.exe", "-R",
1720 VIM_STARTMENU, WORKDIR) == FAIL)
1721 return;
1722 if (build_shortcut("Vim Diff", "vim.exe", "-d",
1723 VIM_STARTMENU, WORKDIR) == FAIL)
1724 return;
1725 }
1726 if (has_gvim)
1727 {
1728 if (build_shortcut("gVim", "gvim.exe", "",
1729 VIM_STARTMENU, WORKDIR) == FAIL)
1730 return;
1731 if (build_shortcut("gVim Easy", "gvim.exe", "-y",
1732 VIM_STARTMENU, WORKDIR) == FAIL)
1733 return;
1734 if (build_shortcut("gVim Read-only", "gvim.exe", "-R",
1735 VIM_STARTMENU, WORKDIR) == FAIL)
1736 return;
1737 if (build_shortcut("gVim Diff", "gvim.exe", "-d",
1738 VIM_STARTMENU, WORKDIR) == FAIL)
1739 return;
1740 }
1741 if (build_shortcut("Uninstall",
1742 interactive ? "uninstal.exe" : "uninstall-gui.exe", "",
1743 VIM_STARTMENU, installdir) == FAIL)
1744 return;
1745 /* For Windows NT the working dir of the vimtutor.bat must be right,
1746 * otherwise gvim.exe won't be found and using gvimbat doesn't work. */
1747 if (build_shortcut("Vim tutor", "vimtutor.bat", "",
1748 VIM_STARTMENU, installdir) == FAIL)
1749 return;
1750 if (build_shortcut("Help", has_gvim ? "gvim.exe" : "vim.exe", "-c h",
1751 VIM_STARTMENU, WORKDIR) == FAIL)
1752 return;
1753 {
1754 char shell_folder_path[BUFSIZE];
1755
1756 /* Creating the URL shortcut works a bit differently... */
1757 if (get_shell_folder_path(shell_folder_path, VIM_STARTMENU) == FAIL)
1758 {
1759 printf("Finding the path of the Start menu failed\n");
1760 return ;
1761 }
1762 add_pathsep(shell_folder_path);
1763 strcat(shell_folder_path, "Vim Online.url");
1764 if (!WritePrivateProfileString("InternetShortcut", "URL",
1765 "http://vim.sf.net/", shell_folder_path))
1766 {
1767 printf("Creating the Vim online URL failed\n");
1768 return;
1769 }
1770 }
1771}
1772
1773 static void
1774toggle_startmenu_choice(int idx)
1775{
1776 if (choices[idx].installfunc == NULL)
1777 {
1778 choices[idx].installfunc = install_start_menu;
1779 choices[idx].text = "Add Vim to the Start menu";
1780 }
1781 else
1782 {
1783 choices[idx].installfunc = NULL;
1784 choices[idx].text = "Do NOT add Vim to the Start menu";
1785 }
1786}
1787
1788/*
1789 * Function to actually create the shortcuts
1790 *
1791 * Currently I am supplying no working directory to the shortcut. This
1792 * means that the initial working dir will be:
1793 * - the location of the shortcut if no file is supplied
1794 * - the location of the file being edited if a file is supplied (ie via
1795 * drag and drop onto the shortcut).
1796 */
1797 void
1798install_shortcut_gvim(int idx)
1799{
1800 /* Create shortcut(s) on the desktop */
1801 if (choices[idx].arg)
1802 {
1803 (void)build_shortcut(icon_names[0], "gvim.exe",
1804 "", "desktop", WORKDIR);
1805 need_uninstall_entry = 1;
1806 }
1807}
1808
1809 void
1810install_shortcut_evim(int idx)
1811{
1812 if (choices[idx].arg)
1813 {
1814 (void)build_shortcut(icon_names[1], "gvim.exe",
1815 "-y", "desktop", WORKDIR);
1816 need_uninstall_entry = 1;
1817 }
1818}
1819
1820 void
1821install_shortcut_gview(int idx)
1822{
1823 if (choices[idx].arg)
1824 {
1825 (void)build_shortcut(icon_names[2], "gvim.exe",
1826 "-R", "desktop", WORKDIR);
1827 need_uninstall_entry = 1;
1828 }
1829}
1830
1831 void
1832toggle_shortcut_choice(int idx)
1833{
1834 char *arg;
1835
1836 if (choices[idx].installfunc == install_shortcut_gvim)
1837 arg = "gVim";
1838 else if (choices[idx].installfunc == install_shortcut_evim)
1839 arg = "gVim Easy";
1840 else
1841 arg = "gVim Read-only";
1842 if (choices[idx].arg)
1843 {
1844 choices[idx].arg = 0;
1845 alloc_text(idx, "Do NOT create a desktop icon for %s", arg);
1846 }
1847 else
1848 {
1849 choices[idx].arg = 1;
1850 alloc_text(idx, "Create a desktop icon for %s", arg);
1851 }
1852}
1853#endif /* WIN3264 */
1854
1855 static void
1856init_startmenu_choice(void)
1857{
1858#ifdef WIN3264
1859 /* Start menu */
1860 choices[choice_count].changefunc = toggle_startmenu_choice;
1861 choices[choice_count].installfunc = NULL;
1862 choices[choice_count].active = 1;
1863 toggle_startmenu_choice(choice_count); /* set the text */
1864 ++choice_count;
1865#else
1866 add_dummy_choice();
1867#endif
1868}
1869
1870/*
1871 * Add the choice for the desktop shortcuts.
1872 */
1873 static void
1874init_shortcut_choices(void)
1875{
1876#ifdef WIN3264
1877 /* Shortcut to gvim */
1878 choices[choice_count].text = NULL;
1879 choices[choice_count].arg = 0;
1880 choices[choice_count].active = has_gvim;
1881 choices[choice_count].changefunc = toggle_shortcut_choice;
1882 choices[choice_count].installfunc = install_shortcut_gvim;
1883 toggle_shortcut_choice(choice_count);
1884 ++choice_count;
1885
1886 /* Shortcut to evim */
1887 choices[choice_count].text = NULL;
1888 choices[choice_count].arg = 0;
1889 choices[choice_count].active = has_gvim;
1890 choices[choice_count].changefunc = toggle_shortcut_choice;
1891 choices[choice_count].installfunc = install_shortcut_evim;
1892 toggle_shortcut_choice(choice_count);
1893 ++choice_count;
1894
1895 /* Shortcut to gview */
1896 choices[choice_count].text = NULL;
1897 choices[choice_count].arg = 0;
1898 choices[choice_count].active = has_gvim;
1899 choices[choice_count].changefunc = toggle_shortcut_choice;
1900 choices[choice_count].installfunc = install_shortcut_gview;
1901 toggle_shortcut_choice(choice_count);
1902 ++choice_count;
1903#else
1904 add_dummy_choice();
1905 add_dummy_choice();
1906 add_dummy_choice();
1907#endif
1908}
1909
1910#ifdef WIN3264
1911/*
1912 * Attempt to register OLE for Vim.
1913 */
1914 static void
1915install_OLE_register(void)
1916{
1917 char register_command_string[BUFSIZE + 30];
1918
1919 printf("\n--- Attempting to register Vim with OLE ---\n");
1920 printf("(There is no message whether this works or not.)\n");
1921
1922#ifndef __CYGWIN__
1923 sprintf(register_command_string, "\"%s\\gvim.exe\" -silent -register", installdir);
1924#else
1925 /* handle this differently for Cygwin which sometimes has trouble with
1926 * Windows-style pathnames here. */
1927 sprintf(register_command_string, "./gvim.exe -silent -register");
1928#endif
1929 system(register_command_string);
1930}
1931#endif /* WIN3264 */
1932
1933/*
1934 * Remove the last part of directory "path[]" to get its parent, and put the
1935 * result in "to[]".
1936 */
1937 static void
1938dir_remove_last(const char *path, char to[BUFSIZE])
1939{
1940 char c;
1941 long last_char_to_copy;
1942 long path_length = strlen(path);
1943
1944 /* skip the last character just in case it is a '\\' */
1945 last_char_to_copy = path_length - 2;
1946 c = path[last_char_to_copy];
1947
1948 while (c != '\\')
1949 {
1950 last_char_to_copy--;
1951 c = path[last_char_to_copy];
1952 }
1953
1954 strncpy(to, path, (size_t)last_char_to_copy);
1955 to[last_char_to_copy] = NUL;
1956}
1957
1958 static void
1959set_directories_text(int idx)
1960{
1961 if (vimfiles_dir_choice == (int)vimfiles_dir_none)
1962 alloc_text(idx, "Do NOT create plugin directories%s", "");
1963 else
1964 alloc_text(idx, "Create plugin directories: %s",
1965 vimfiles_dir_choices[vimfiles_dir_choice]);
1966}
1967
1968/*
1969 * Change the directory that the vim plugin directories will be created in:
1970 * $HOME, $VIM or nowhere.
1971 */
1972 static void
1973change_directories_choice(int idx)
1974{
1975 int choice_count = TABLE_SIZE(vimfiles_dir_choices);
1976
1977 /* Don't offer the $HOME choice if $HOME isn't set. */
1978 if (getenv("HOME") == NULL)
1979 --choice_count;
1980 vimfiles_dir_choice = get_choice(vimfiles_dir_choices, choice_count);
1981 set_directories_text(idx);
1982}
1983
1984/*
1985 * Create the plugin directories...
1986 */
1987/*ARGSUSED*/
1988 static void
1989install_vimfilesdir(int idx)
1990{
1991 int i;
1992 char *p;
1993 char vimdir_path[BUFSIZE];
1994 char vimfiles_path[BUFSIZE];
1995 char tmp_dirname[BUFSIZE];
1996
1997 /* switch on the location that the user wants the plugin directories
1998 * built in */
1999 switch (vimfiles_dir_choice)
2000 {
2001 case vimfiles_dir_vim:
2002 {
2003 /* Go to the %VIM% directory - check env first, then go one dir
2004 * below installdir if there is no %VIM% environment variable.
2005 * The accuracy of $VIM is checked in inspect_system(), so we
2006 * can be sure it is ok to use here. */
2007 p = getenv("VIM");
2008 if (p == NULL) /* No $VIM in path */
2009 dir_remove_last(installdir, vimdir_path);
2010 else
2011 strcpy(vimdir_path, p);
2012 break;
2013 }
2014 case vimfiles_dir_home:
2015 {
2016 /* Find the $HOME directory. Its existence was already checked. */
2017 p = getenv("HOME");
2018 if (p == NULL)
2019 {
2020 printf("Internal error: $HOME is NULL\n");
2021 p = "c:\\";
2022 }
2023 strcpy(vimdir_path, p);
2024 break;
2025 }
2026 case vimfiles_dir_none:
2027 {
2028 /* Do not create vim plugin directory */
2029 return;
2030 }
2031 }
2032
2033 /* Now, just create the directory. If it already exists, it will fail
2034 * silently. */
2035 sprintf(vimfiles_path, "%s\\vimfiles", vimdir_path);
2036 vim_mkdir(vimfiles_path, 0755);
2037
2038 printf("Creating the following directories in \"%s\":\n", vimfiles_path);
2039 for (i = 0; i < TABLE_SIZE(vimfiles_subdirs); i++)
2040 {
2041 sprintf(tmp_dirname, "%s\\%s", vimfiles_path, vimfiles_subdirs[i]);
2042 printf(" %s", vimfiles_subdirs[i]);
2043 vim_mkdir(tmp_dirname, 0755);
2044 }
2045 printf("\n");
2046}
2047
2048/*
2049 * Add the creation of runtime files to the setup sequence.
2050 */
2051 static void
2052init_directories_choice(void)
2053{
2054 struct stat st;
2055 char tmp_dirname[BUFSIZE];
2056 char *p;
2057
2058 choices[choice_count].text = alloc(150);
2059 choices[choice_count].changefunc = change_directories_choice;
2060 choices[choice_count].installfunc = install_vimfilesdir;
2061 choices[choice_count].active = 1;
2062
2063 /* Check if the "compiler" directory already exists. That's a good
2064 * indication that the plugin directories were already created. */
2065 if (getenv("HOME") != NULL)
2066 {
2067 vimfiles_dir_choice = (int)vimfiles_dir_home;
2068 sprintf(tmp_dirname, "%s\\vimfiles\\compiler", getenv("HOME"));
2069 if (stat(tmp_dirname, &st) == 0)
2070 vimfiles_dir_choice = (int)vimfiles_dir_none;
2071 }
2072 else
2073 {
2074 vimfiles_dir_choice = (int)vimfiles_dir_vim;
2075 p = getenv("VIM");
2076 if (p == NULL) /* No $VIM in path, use the install dir */
2077 dir_remove_last(installdir, tmp_dirname);
2078 else
2079 strcpy(tmp_dirname, p);
2080 strcat(tmp_dirname, "\\vimfiles\\compiler");
2081 if (stat(tmp_dirname, &st) == 0)
2082 vimfiles_dir_choice = (int)vimfiles_dir_none;
2083 }
2084
2085 set_directories_text(choice_count);
2086 ++choice_count;
2087}
2088
2089/*
2090 * Setup the choices and the default values.
2091 */
2092 static void
2093setup_choices(void)
2094{
2095 /* install the batch files */
2096 init_bat_choices();
2097
2098 /* (over) write _vimrc file */
2099 init_vimrc_choices();
2100
2101 /* Whether to add Vim to the popup menu */
2102 init_popup_choice();
2103
2104 /* Whether to add Vim to the "Open With..." menu */
2105 init_openwith_choice();
2106
2107 /* Whether to add Vim to the Start Menu. */
2108 init_startmenu_choice();
2109
2110 /* Whether to add shortcuts to the Desktop. */
2111 init_shortcut_choices();
2112
2113 /* Whether to create the runtime directories. */
2114 init_directories_choice();
2115}
2116
2117 static void
2118print_cmd_line_help(void)
2119{
2120 printf("Vim installer non-interactive command line arguments:\n");
2121 printf("\n");
2122 printf("-create-batfiles [vim gvim evim view gview vimdiff gvimdiff]\n");
2123 printf(" Create .bat files for Vim variants in the Windows directory.\n");
2124 printf("-create-vimrc\n");
2125 printf(" Create a default _vimrc file if one does not already exist.\n");
2126 printf("-install-popup\n");
2127 printf(" Install the Edit-with-Vim context menu entry\n");
2128 printf("-install-openwith\n");
2129 printf(" Add Vim to the \"Open With...\" context menu list\n");
2130#ifdef WIN3264
2131 printf("-add-start-menu");
2132 printf(" Add Vim to the start menu\n");
2133 printf("-install-icons");
2134 printf(" Create icons for gVim executables on the desktop\n");
2135#endif
2136 printf("-create-directories [vim|home]\n");
2137 printf(" Create runtime directories to drop plugins into; in the $VIM\n");
2138 printf(" or $HOME directory\n");
2139#ifdef WIN3264
2140 printf("-register-OLE");
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002141 printf(" Ignored\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#endif
2143 printf("\n");
2144}
2145
2146/*
2147 * Setup installation choices based on command line switches
2148 */
2149 static void
2150command_line_setup_choices(int argc, char **argv)
2151{
2152 int i, j;
2153
2154 for (i = 1; i < argc; i++)
2155 {
2156 if (strcmp(argv[i], "-create-batfiles") == 0)
2157 {
2158 if (i + 1 == argc)
2159 continue;
2160 while (argv[i + 1][0] != '-' && i < argc)
2161 {
2162 i++;
2163 for (j = 1; j < TARGET_COUNT; ++j)
2164 if ((targets[j].exenamearg[0] == 'g' ? has_gvim : has_vim)
2165 && strcmp(argv[i], targets[j].name) == 0)
2166 {
2167 init_bat_choice(j);
2168 break;
2169 }
2170 if (j == TARGET_COUNT)
2171 printf("%s is not a valid choice for -create-batfiles\n",
2172 argv[i]);
2173
2174 if (i + 1 == argc)
2175 break;
2176 }
2177 }
2178 else if (strcmp(argv[i], "-create-vimrc") == 0)
2179 {
2180 /* Setup default vimrc choices. If there is already a _vimrc file,
2181 * it will NOT be overwritten.
2182 */
2183 init_vimrc_choices();
2184 }
2185 else if (strcmp(argv[i], "-install-popup") == 0)
2186 {
2187 init_popup_choice();
2188 }
2189 else if (strcmp(argv[i], "-install-openwith") == 0)
2190 {
2191 init_openwith_choice();
2192 }
2193 else if (strcmp(argv[i], "-add-start-menu") == 0)
2194 {
2195 init_startmenu_choice();
2196 }
2197 else if (strcmp(argv[i], "-install-icons") == 0)
2198 {
2199 init_shortcut_choices();
2200 }
2201 else if (strcmp(argv[i], "-create-directories") == 0)
2202 {
2203 init_directories_choice();
2204 if (argv[i + 1][0] != '-')
2205 {
2206 i++;
2207 if (strcmp(argv[i], "vim") == 0)
2208 vimfiles_dir_choice = (int)vimfiles_dir_vim;
2209 else if (strcmp(argv[i], "home") == 0)
2210 {
2211 if (getenv("HOME") == NULL) /* No $HOME in environment */
2212 vimfiles_dir_choice = (int)vimfiles_dir_vim;
2213 else
2214 vimfiles_dir_choice = (int)vimfiles_dir_home;
2215 }
2216 else
2217 {
2218 printf("Unknown argument for -create-directories: %s\n",
2219 argv[i]);
2220 print_cmd_line_help();
2221 }
2222 }
2223 else /* No choice specified, default to vim directory */
2224 vimfiles_dir_choice = (int)vimfiles_dir_vim;
2225 }
2226#ifdef WIN3264
2227 else if (strcmp(argv[i], "-register-OLE") == 0)
2228 {
2229 /* This is always done when gvim is found */
2230 }
2231#endif
2232 else /* Unknown switch */
2233 {
2234 printf("Got unknown argument argv[%d] = %s\n", i, argv[i]);
2235 print_cmd_line_help();
2236 }
2237 }
2238}
2239
2240
2241/*
2242 * Show a few screens full of helpful information.
2243 */
2244 static void
2245show_help(void)
2246{
2247 static char *(items[]) =
2248 {
2249"Installing .bat files\n"
2250"---------------------\n"
2251"The vim.bat file is written in one of the directories in $PATH.\n"
2252"This makes it possible to start Vim from the command line.\n"
2253"If vim.exe can be found in $PATH, the choice for vim.bat will not be\n"
2254"present. It is assumed you will use the existing vim.exe.\n"
2255"If vim.bat can already be found in $PATH this is probably for an old\n"
2256"version of Vim (but this is not checked!). You can overwrite it.\n"
2257"If no vim.bat already exists, you can select one of the directories in\n"
2258"$PATH for creating the batch file, or disable creating a vim.bat file.\n"
2259"\n"
2260"If you choose not to create the vim.bat file, Vim can still be executed\n"
2261"in other ways, but not from the command line.\n"
2262"\n"
2263"The same applies to choices for gvim, evim, (g)view, and (g)vimdiff.\n"
2264"The first item can be used to change the path for all of them.\n"
2265,
2266"Creating a _vimrc file\n"
2267"----------------------\n"
2268"The _vimrc file is used to set options for how Vim behaves.\n"
2269"The install program can create a _vimrc file with a few basic choices.\n"
2270"You can edit this file later to tune your preferences.\n"
2271"If you already have a _vimrc or .vimrc file it can be overwritten.\n"
2272"Don't do that if you have made changes to it.\n"
2273,
2274"Vim features\n"
2275"------------\n"
2276"(this choice is only available when creating a _vimrc file)\n"
2277"1. Vim can run in Vi-compatible mode. Many nice Vim features are then\n"
2278" disabled. In the not-Vi-compatible mode Vim is still mostly Vi\n"
2279" compatible, but adds nice features like multi-level undo. Only\n"
2280" choose Vi-compatible if you really need full Vi compatibility.\n"
2281"2. Running Vim with some enhancements is useful when you want some of\n"
2282" the nice Vim features, but have a slow computer and want to keep it\n"
2283" really fast.\n"
2284"3. Syntax highlighting shows many files in color. Not only does this look\n"
2285" nice, it also makes it easier to spot errors and you can work faster.\n"
2286" The other features include editing compressed files.\n"
2287,
2288"Windows key mapping\n"
2289"-------------------\n"
2290"(this choice is only available when creating a _vimrc file)\n"
2291"Under MS-Windows the CTRL-C key copies text to the clipboard and CTRL-V\n"
2292"pastes text from the clipboard. There are a few more keys like these.\n"
2293"Unfortunately, in Vim these keys normally have another meaning.\n"
2294"1. Choose to have the keys like they normally are in Vim (useful if you\n"
2295" also use Vim on other systems).\n"
2296"2. Choose to have the keys work like they are used on MS-Windows (useful\n"
2297" if you mostly work on MS-Windows).\n"
2298,
2299"Mouse use\n"
2300"---------\n"
2301"(this choice is only available when creating a _vimrc file)\n"
2302"The right mouse button can be used in two ways:\n"
2303"1. The Unix way is to extend an existing selection. The popup menu is\n"
2304" not available.\n"
2305"2. The MS-Windows way is to show a popup menu, which allows you to\n"
2306" copy/paste text, undo/redo, etc. Extending the selection can still be\n"
2307" done by keeping SHIFT pressed while using the left mouse button\n"
2308,
2309"Edit-with-Vim context menu entry\n"
2310"--------------------------------\n"
2311"(this choice is only available when gvim.exe and gvimext.dll are present)\n"
2312"You can associate different file types with Vim, so that you can (double)\n"
2313"click on a file to edit it with Vim. This means you have to individually\n"
2314"select each file type.\n"
2315"An alternative is the option offered here: Install an \"Edit with Vim\"\n"
2316"entry in the popup menu for the right mouse button. This means you can\n"
2317"edit any file with Vim.\n"
2318,
2319"\"Open With...\" context menu entry\n"
2320"--------------------------------\n"
2321"(this choice is only available when gvim.exe is present)\n"
2322"This option adds Vim to the \"Open With...\" entry in the popup menu for\n"
2323"the right mouse button. This also makes it possible to edit HTML files\n"
2324"directly from Internet Explorer.\n"
2325,
2326"Add Vim to the Start menu\n"
2327"-------------------------\n"
2328"In Windows 95 and later, Vim can be added to the Start menu. This will\n"
2329"create a submenu with an entry for vim, gvim, evim, vimdiff, etc..\n"
2330,
2331"Icons on the desktop\n"
2332"--------------------\n"
2333"(these choices are only available when installing gvim)\n"
2334"In Windows 95 and later, shortcuts (icons) can be created on the Desktop.\n"
2335,
2336"Create plugin directories\n"
2337"-------------------------\n"
2338"Plugin directories allow extending Vim by dropping a file into a directory.\n"
2339"This choice allows creating them in $HOME (if you have a home directory) or\n"
2340"$VIM (used for everybody on the system).\n"
2341,
2342NULL
2343 };
2344 int i;
2345 int c;
2346
2347 rewind(stdin);
2348 printf("\n");
2349 for (i = 0; items[i] != NULL; ++i)
2350 {
2351 printf(items[i]);
2352 printf("\n");
2353 printf("Hit Enter to continue, b (back) or q (quit help): ");
2354 c = getchar();
2355 rewind(stdin);
2356 if (c == 'b' || c == 'B')
2357 {
2358 if (i == 0)
2359 --i;
2360 else
2361 i -= 2;
2362 }
2363 if (c == 'q' || c == 'Q')
2364 break;
2365 printf("\n");
2366 }
2367}
2368
2369/*
2370 * Install the choices.
2371 */
2372 static void
2373install(void)
2374{
2375 int i;
2376
2377 /* Install the selected choices. */
2378 for (i = 0; i < choice_count; ++i)
2379 if (choices[i].installfunc != NULL && choices[i].active)
2380 (choices[i].installfunc)(i);
2381
2382 /* Add some entries to the registry, if needed. */
2383 if (install_popup
2384 || install_openwith
2385 || (need_uninstall_entry && interactive)
2386 || !interactive)
2387 install_registry();
2388
2389#ifdef WIN3264
2390 /* Register gvim with OLE. */
2391 if (has_gvim)
2392 install_OLE_register();
2393#endif
2394}
2395
2396/*
2397 * request_choice
2398 */
2399 static void
2400request_choice(void)
2401{
2402 int i;
2403
2404 printf("\n\nInstall will do for you:\n");
2405 for (i = 0; i < choice_count; ++i)
2406 if (choices[i].active)
2407 printf("%2d %s\n", i + 1, choices[i].text);
2408 printf("To change an item, enter its number\n\n");
2409 printf("Enter item number, h (help), d (do it) or q (quit): ");
2410}
2411
2412 int
2413main(int argc, char **argv)
2414{
2415 int i;
2416 char buf[BUFSIZE];
2417
2418 /*
2419 * Run interactively if there are no command line arguments.
2420 */
2421 if (argc > 1)
2422 interactive = 0;
2423 else
2424 interactive = 1;
2425
2426 /* Initialize this program. */
2427 do_inits(argv);
2428
2429#ifdef WIN3264
2430 if (argc > 1 && strcmp(argv[1], "-uninstall-check") == 0)
2431 {
2432 /* Only check for already installed Vims. Used by NSIS installer. */
Bram Moolenaar442b4222010-05-24 21:34:22 +02002433 i = uninstall_check(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 /* Find the value of $VIM, because NSIS isn't able to do this by
2436 * itself. */
2437 get_vim_env();
2438
2439 /* When nothing found exit quietly. If something found wait for
Bram Moolenaarb230bd52010-05-25 21:02:00 +02002440 * a little while, so that the user can read the messages. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (i)
Bram Moolenaarb230bd52010-05-25 21:02:00 +02002442 Sleep(3000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 exit(0);
2444 }
2445#endif
2446
2447 printf("This program sets up the installation of Vim "
2448 VIM_VERSION_MEDIUM "\n\n");
2449
2450 /* Check if the user unpacked the archives properly. */
2451 check_unpack();
2452
2453#ifdef WIN3264
2454 /* Check for already installed Vims. */
2455 if (interactive)
Bram Moolenaar442b4222010-05-24 21:34:22 +02002456 uninstall_check(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457#endif
2458
2459 /* Find out information about the system. */
2460 inspect_system();
2461
2462 if (interactive)
2463 {
2464 /* Setup all the choices. */
2465 setup_choices();
2466
2467 /* Let the user change choices and finally install (or quit). */
2468 for (;;)
2469 {
2470 request_choice();
2471 rewind(stdin);
2472 if (scanf("%99s", buf) == 1)
2473 {
2474 if (isdigit(buf[0]))
2475 {
2476 /* Change a choice. */
2477 i = atoi(buf);
2478 if (i > 0 && i <= choice_count && choices[i - 1].active)
2479 (choices[i - 1].changefunc)(i - 1);
2480 else
2481 printf("\nIllegal choice\n");
2482 }
2483 else if (buf[0] == 'h' || buf[0] == 'H')
2484 {
2485 /* Help */
2486 show_help();
2487 }
2488 else if (buf[0] == 'd' || buf[0] == 'D')
2489 {
2490 /* Install! */
2491 install();
2492 printf("\nThat finishes the installation. Happy Vimming!\n");
2493 break;
2494 }
2495 else if (buf[0] == 'q' || buf[0] == 'Q')
2496 {
2497 /* Quit */
2498 printf("\nExiting without anything done\n");
2499 break;
2500 }
2501 else
2502 printf("\nIllegal choice\n");
2503 }
2504 }
2505 printf("\n");
Bram Moolenaar442b4222010-05-24 21:34:22 +02002506 myexit(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 }
2508 else
2509 {
2510 /*
2511 * Run non-interactive - setup according to the command line switches
2512 */
2513 command_line_setup_choices(argc, argv);
2514 install();
Bram Moolenaar442b4222010-05-24 21:34:22 +02002515
2516 /* Avoid that the user has to hit Enter, just wait a little bit to
2517 * allow reading the messages. */
2518 Sleep(2000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 return 0;
2522}