Skip to content

Commit 25a494c

Browse files
committed
patch 8.1.0534: MS-Windows installer uses different $HOME than Vim
Problem: MS-Windows installer uses different $HOME than Vim. Solution: Use the Vim logic also in the MS-Windows installer. (Ken Takata, closes #3564)
1 parent 447f6ce commit 25a494c

3 files changed

Lines changed: 111 additions & 13 deletions

File tree

src/dosinst.c

Lines changed: 107 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,13 @@ enum
115115
vimfiles_dir_vim,
116116
vimfiles_dir_home
117117
};
118-
static char *(vimfiles_dir_choices[]) =
118+
static char *(vimfiles_dir_choices[]) =
119119
{
120120
"\nCreate plugin directories:",
121121
"No",
122122
"In the VIM directory",
123123
"In your HOME directory",
124124
};
125-
static int vimfiles_dir_choice;
126125

127126
/* non-zero when selected to install the popup menu entry. */
128127
static int install_popup = 0;
@@ -741,7 +740,8 @@ add_dummy_choice(void)
741740
choices[choice_count].installfunc = NULL;
742741
choices[choice_count].active = 0;
743742
choices[choice_count].changefunc = NULL;
744-
choices[choice_count].installfunc = NULL;
743+
choices[choice_count].text = NULL;
744+
choices[choice_count].arg = 0;
745745
++choice_count;
746746
}
747747

@@ -2089,13 +2089,100 @@ dir_remove_last(const char *path, char to[BUFSIZE])
20892089
static void
20902090
set_directories_text(int idx)
20912091
{
2092+
int vimfiles_dir_choice = choices[idx].arg;
2093+
20922094
if (vimfiles_dir_choice == (int)vimfiles_dir_none)
20932095
alloc_text(idx, "Do NOT create plugin directories%s", "");
20942096
else
20952097
alloc_text(idx, "Create plugin directories: %s",
20962098
vimfiles_dir_choices[vimfiles_dir_choice]);
20972099
}
20982100

2101+
/*
2102+
* To get the "real" home directory:
2103+
* - get value of $HOME
2104+
* - if not found, get value of $HOMEDRIVE$HOMEPATH
2105+
* - if not found, get value of $USERPROFILE
2106+
*
2107+
* This code is based on init_homedir() in misc1.c, keep in sync!
2108+
*/
2109+
static char *homedir = NULL;
2110+
2111+
void
2112+
init_homedir(void)
2113+
{
2114+
char *var;
2115+
char buf[MAX_PATH];
2116+
2117+
if (homedir != NULL)
2118+
{
2119+
free(homedir);
2120+
homedir = NULL;
2121+
}
2122+
2123+
var = getenv("HOME");
2124+
2125+
/*
2126+
* Typically, $HOME is not defined on Windows, unless the user has
2127+
* specifically defined it for Vim's sake. However, on Windows NT
2128+
* platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
2129+
* each user. Try constructing $HOME from these.
2130+
*/
2131+
if (var == NULL || *var == NUL)
2132+
{
2133+
char *homedrive, *homepath;
2134+
2135+
homedrive = getenv("HOMEDRIVE");
2136+
homepath = getenv("HOMEPATH");
2137+
if (homepath == NULL || *homepath == NUL)
2138+
homepath = "\\";
2139+
if (homedrive != NULL
2140+
&& strlen(homedrive) + strlen(homepath) < MAX_PATH)
2141+
{
2142+
sprintf(buf, "%s%s", homedrive, homepath);
2143+
if (buf[0] != NUL)
2144+
var = buf;
2145+
}
2146+
}
2147+
2148+
if (var == NULL)
2149+
var = getenv("USERPROFILE");
2150+
2151+
/*
2152+
* Weird but true: $HOME may contain an indirect reference to another
2153+
* variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
2154+
* when $HOME is being set.
2155+
*/
2156+
if (var != NULL && *var == '%')
2157+
{
2158+
char *p;
2159+
char *exp;
2160+
2161+
p = strchr(var + 1, '%');
2162+
if (p != NULL)
2163+
{
2164+
strncpy(buf, var + 1, p - (var + 1));
2165+
buf[p - (var + 1)] = NUL;
2166+
exp = getenv(buf);
2167+
if (exp != NULL && *exp != NUL
2168+
&& strlen(exp) + strlen(p) < MAX_PATH)
2169+
{
2170+
_snprintf(buf, MAX_PATH, "%s%s", exp, p + 1);
2171+
buf[MAX_PATH - 1] = NUL;
2172+
var = buf;
2173+
}
2174+
}
2175+
}
2176+
2177+
if (var != NULL && *var == NUL) // empty is same as not set
2178+
var = NULL;
2179+
2180+
if (var == NULL)
2181+
homedir = NULL;
2182+
else
2183+
homedir = _strdup(var);
2184+
}
2185+
20992186
/*
21002187
* Change the directory that the vim plugin directories will be created in:
21012188
* $HOME, $VIM or nowhere.
@@ -2106,9 +2193,9 @@ change_directories_choice(int idx)
21062193
int choice_count = TABLE_SIZE(vimfiles_dir_choices);
21072194

21082195
/* Don't offer the $HOME choice if $HOME isn't set. */
2109-
if (getenv("HOME") == NULL)
2196+
if (homedir == NULL)
21102197
--choice_count;
2111-
vimfiles_dir_choice = get_choice(vimfiles_dir_choices, choice_count);
2198+
choices[idx].arg = get_choice(vimfiles_dir_choices, choice_count);
21122199
set_directories_text(idx);
21132200
}
21142201

@@ -2120,6 +2207,7 @@ change_directories_choice(int idx)
21202207
install_vimfilesdir(int idx)
21212208
{
21222209
int i;
2210+
int vimfiles_dir_choice = choices[idx].arg;
21232211
char *p;
21242212
char vimdir_path[BUFSIZE];
21252213
char vimfiles_path[BUFSIZE];
@@ -2144,8 +2232,8 @@ install_vimfilesdir(int idx)
21442232
}
21452233
case vimfiles_dir_home:
21462234
{
2147-
/* Find the $HOME directory. Its existence was already checked. */
2148-
p = getenv("HOME");
2235+
// Find the $HOME directory. Its existence was already checked.
2236+
p = homedir;
21492237
if (p == NULL)
21502238
{
21512239
printf("Internal error: $HOME is NULL\n");
@@ -2156,7 +2244,7 @@ install_vimfilesdir(int idx)
21562244
}
21572245
case vimfiles_dir_none:
21582246
{
2159-
/* Do not create vim plugin directory */
2247+
// Do not create vim plugin directory.
21602248
return;
21612249
}
21622250
}
@@ -2185,14 +2273,15 @@ init_directories_choice(void)
21852273
struct stat st;
21862274
char tmp_dirname[BUFSIZE];
21872275
char *p;
2276+
int vimfiles_dir_choice;
21882277

21892278
choices[choice_count].text = alloc(150);
21902279
choices[choice_count].changefunc = change_directories_choice;
21912280
choices[choice_count].installfunc = install_vimfilesdir;
21922281
choices[choice_count].active = 1;
21932282

2194-
/* Check if the "compiler" directory already exists. That's a good
2195-
* indication that the plugin directories were already created. */
2283+
// Check if the "compiler" directory already exists. That's a good
2284+
// indication that the plugin directories were already created.
21962285
if (getenv("HOME") != NULL)
21972286
{
21982287
vimfiles_dir_choice = (int)vimfiles_dir_home;
@@ -2204,7 +2293,7 @@ init_directories_choice(void)
22042293
{
22052294
vimfiles_dir_choice = (int)vimfiles_dir_vim;
22062295
p = getenv("VIM");
2207-
if (p == NULL) /* No $VIM in path, use the install dir */
2296+
if (p == NULL) // No $VIM in path, use the install dir.
22082297
dir_remove_last(installdir, tmp_dirname);
22092298
else
22102299
strcpy(tmp_dirname, p);
@@ -2213,6 +2302,7 @@ init_directories_choice(void)
22132302
vimfiles_dir_choice = (int)vimfiles_dir_none;
22142303
}
22152304

2305+
choices[choice_count].arg = vimfiles_dir_choice;
22162306
set_directories_text(choice_count);
22172307
++choice_count;
22182308
}
@@ -2369,6 +2459,8 @@ command_line_setup_choices(int argc, char **argv)
23692459
}
23702460
else if (strcmp(argv[i], "-create-directories") == 0)
23712461
{
2462+
int vimfiles_dir_choice;
2463+
23722464
init_directories_choice();
23732465
if (argv[i + 1][0] != '-')
23742466
{
@@ -2377,8 +2469,8 @@ command_line_setup_choices(int argc, char **argv)
23772469
vimfiles_dir_choice = (int)vimfiles_dir_vim;
23782470
else if (strcmp(argv[i], "home") == 0)
23792471
{
2380-
if (getenv("HOME") == NULL) /* No $HOME in environment */
2381-
vimfiles_dir_choice = (int)vimfiles_dir_vim;
2472+
if (homedir == NULL) // No $HOME in environment
2473+
vimfiles_dir_choice = (int)vimfiles_dir_none;
23822474
else
23832475
vimfiles_dir_choice = (int)vimfiles_dir_home;
23842476
}
@@ -2391,6 +2483,7 @@ command_line_setup_choices(int argc, char **argv)
23912483
}
23922484
else /* No choice specified, default to vim directory */
23932485
vimfiles_dir_choice = (int)vimfiles_dir_vim;
2486+
choices[choice_count - 1].arg = vimfiles_dir_choice;
23942487
}
23952488
else if (strcmp(argv[i], "-register-OLE") == 0)
23962489
{
@@ -2589,6 +2682,7 @@ main(int argc, char **argv)
25892682

25902683
/* Initialize this program. */
25912684
do_inits(argv);
2685+
init_homedir();
25922686

25932687
if (argc > 1 && strcmp(argv[1], "-uninstall-check") == 0)
25942688
{

src/misc1.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3905,6 +3905,8 @@ vim_beep(
39053905
* - do mch_dirname() to get the real name of that directory.
39063906
* This also works with mounts and links.
39073907
* Don't do this for MS-DOS, it will change the "current dir" for a drive.
3908+
* For Windows:
3909+
* This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
39083910
*/
39093911
static char_u *homedir = NULL;
39103912

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@ static char *(features[]) =
792792

793793
static int included_patches[] =
794794
{ /* Add new patch number below this line */
795+
/**/
796+
534,
795797
/**/
796798
533,
797799
/**/

0 commit comments

Comments
 (0)