Skip to content

Commit 3e2affc

Browse files
committed
patch 9.1.1159: $MYVIMDIR may not always be set
Problem: $MYVIMDIR may not always be set (after 9.1.0718) (sandwm) Solution: always set $MYVIMDIR to first item in runtimepath (except when using --clean), update it when changing &rtp fixes: #16609 closes: #16709 Signed-off-by: Christian Brabandt <[email protected]>
1 parent 5e8b226 commit 3e2affc

5 files changed

Lines changed: 69 additions & 61 deletions

File tree

runtime/doc/starting.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*starting.txt* For Vim version 9.1. Last change: 2024 Dec 19
1+
*starting.txt* For Vim version 9.1. Last change: 2025 Feb 27
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -828,8 +828,9 @@ accordingly. Vim proceeds in this order:
828828
easy to copy it to another system.
829829

830830
If Vim was started with "-u filename", the file "filename" is used.
831-
All following initializations until 4. are skipped. $MYVIMRC and
832-
$MYVIMDIR are not set.
831+
All following initializations until 4. are skipped. `$MYVIMRC` and
832+
`$MYVIMDIR` are not set (but `$MYVIMDIR` will be set, if 'rtp' is
833+
updated).
833834
"vim -u NORC" can be used to skip these initializations without
834835
reading a file. "vim -u NONE" also skips loading plugins. |-u|
835836

@@ -850,9 +851,9 @@ accordingly. Vim proceeds in this order:
850851
*VIMINIT* *.vimrc* *_vimrc* *EXINIT* *.exrc* *_exrc*
851852
*$MYVIMRC* *$MYVIMDIR*
852853
c. Five places are searched for initializations. The first that exists
853-
is used, the others are ignored. The $MYVIMRC environment variable is
854-
set to the file that was first found, unless $MYVIMRC was already set
855-
and when using VIMINIT. The $MYVIMDIR environment variable is
854+
is used, the others are ignored. The `$MYVIMRC` environment variable is
855+
set to the file that was first found, unless `$MYVIMRC` was already set
856+
when using VIMINIT. The `$MYVIMDIR` environment variable is
856857
set to the personal 'rtp' directory, however it is not verified
857858
that the directory actually exists.
858859
I The environment variable VIMINIT (see also |compatible-default|) (*)
@@ -973,9 +974,10 @@ accordingly. Vim proceeds in this order:
973974
The |v:vim_did_enter| variable is set to 1.
974975
The |VimEnter| autocommands are executed.
975976

976-
The $MYVIMRC or $MYGVIMRC environment variable will be set to the first found
977-
vimrc and/or gvimrc file while $MYVIMDIR is set to the users personal runtime
978-
directory 'rtp' (typically the first entry in 'runtimepath').
977+
The `$MYVIMRC` or `$MYGVIMRC` environment variable will be set to the first found
978+
vimrc and/or gvimrc file while `$MYVIMDIR` is set to the users personal runtime
979+
directory 'rtp' (typically the first entry in 'runtimepath'). If 'rtp'
980+
changes, `$MYVIMDIR` will be updated.
979981
Note: These environment variables resolve symbolic links, but 'rtp' does not.
980982

981983

src/option.c

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8418,55 +8418,6 @@ vimrc_found(char_u *fname, char_u *envname)
84188418
if (p != NULL)
84198419
{
84208420
vim_setenv(envname, p);
8421-
if (vim_getenv((char_u *)"MYVIMDIR", &dofree) == NULL)
8422-
{
8423-
size_t usedlen = 0;
8424-
size_t len = 0;
8425-
char_u *fbuf = NULL;
8426-
8427-
if (STRNCMP(gettail(fname), ".vimrc", 6) == 0)
8428-
{
8429-
len = STRLEN(p) - 2;
8430-
p[len] = '/';
8431-
}
8432-
else if (STRNCMP(gettail(fname), ".gvimrc", 7) == 0)
8433-
{
8434-
len = STRLEN(p);
8435-
char_u *buf = alloc(len);
8436-
if (buf != NULL)
8437-
{
8438-
mch_memmove(buf, fname, len - 7);
8439-
mch_memmove(buf + len - 7, (char_u *)".vim/", 5);
8440-
len -= 2; // decrement len, so that we can set len+1 = NUL below
8441-
vim_free(p);
8442-
p = buf;
8443-
}
8444-
}
8445-
#ifdef MSWIN
8446-
else if (STRNCMP(gettail(fname), "_vimrc", 6) == 0)
8447-
{
8448-
len = STRLEN(p) + 4; // remove _vimrc, add vimfiles/
8449-
char_u *buf = alloc(len);
8450-
if (buf != NULL)
8451-
{
8452-
mch_memmove(buf, fname, len - 10);
8453-
mch_memmove(buf + len - 10, (char_u *)"vimfiles\\", 9);
8454-
len -= 2; // decrement len, so that we can set len+1 = NUL below
8455-
vim_free(p);
8456-
p = buf;
8457-
}
8458-
}
8459-
#endif
8460-
else
8461-
(void)modify_fname((char_u *)":h", FALSE, &usedlen, &p, &fbuf, &len);
8462-
8463-
if (p != NULL)
8464-
{
8465-
// keep the directory separator
8466-
p[len + 1] = NUL;
8467-
vim_setenv((char_u *)"MYVIMDIR", p);
8468-
}
8469-
}
84708421
vim_free(p);
84718422
}
84728423
}

src/optionstr.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ static char *(p_sws_values[]) = {"fsync", "sync", NULL};
135135

136136
static int check_opt_strings(char_u *val, char **values, int list);
137137
static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
138+
static void export_myvimdir(void);
138139

139140
/*
140141
* After setting various option values: recompute variables that depend on
@@ -4652,10 +4653,13 @@ did_set_string_option(
46524653
setmouse(); // in case 'mouse' changed
46534654
}
46544655

4655-
#if defined(FEAT_LUA) || defined(PROTO)
46564656
if (varp == &p_rtp)
4657+
{
4658+
export_myvimdir();
4659+
#if defined(FEAT_LUA) || defined(PROTO)
46574660
update_package_paths_in_lua();
46584661
#endif
4662+
}
46594663

46604664
#if defined(FEAT_LINEBREAK)
46614665
// Changing Formatlistpattern when briopt includes the list setting:
@@ -4809,3 +4813,37 @@ restore_shm_value(void)
48094813
vim_memset(shm_buf, 0, SHM_LEN);
48104814
}
48114815
}
4816+
4817+
/*
4818+
* Export the environment variable $MYVIMDIR to the first item in runtimepath
4819+
*/
4820+
static void
4821+
export_myvimdir()
4822+
{
4823+
int dofree = FALSE;
4824+
char_u *p;
4825+
char_u *q = p_rtp;
4826+
char_u *buf = alloc(MAXPATHL);
4827+
4828+
if (buf == NULL)
4829+
return;
4830+
4831+
(void)copy_option_part(&q, buf, MAXPATHL, ",");
4832+
4833+
p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
4834+
4835+
if (p == NULL || STRCMP(p, buf) != 0)
4836+
{
4837+
add_pathsep(buf);
4838+
#ifdef MSWIN
4839+
// normalize path separators
4840+
for (q = buf; *q != NUL; q++)
4841+
if (*q == '/')
4842+
*q = '\\';
4843+
#endif
4844+
vim_setenv((char_u *)"MYVIMDIR", buf);
4845+
}
4846+
if (dofree)
4847+
vim_free(p);
4848+
vim_free(buf);
4849+
}

src/testdir/test_xdg.vim

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ func Test_xdg_runtime_files()
150150
call system($'{vimcmd} -S Xscript')
151151
call assert_equal([], readfile('Xresult'))
152152

153+
" Test for $MYVIMDIR changes when updating runtimepath
154+
let lines =<< trim END
155+
let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}" rtp-prepend'
156+
set rtp^=/non-existing
157+
call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
158+
call assert_match('/non-existing', $MYVIMDIR, msg)
159+
call writefile(v:errors, 'Xresult')
160+
quit
161+
END
162+
call writefile(lines, 'Xscript', 'D')
163+
call system($'{vimcmd} -S Xscript')
164+
call assert_equal([], readfile('Xresult'))
165+
153166
call delete(rc4)
154167
unlet $XDG_CONFIG_HOME
155168
endfunc
@@ -267,13 +280,14 @@ func Test_zzz_xdg_runtime_files()
267280
call delete(rc2)
268281

269282
" Test for ~/.config/vim/gvimrc
283+
" MYVIMDIR is only set to ~/config/.vim if ~/.config/vim/vimrc exists!
270284
let lines =<< trim END
271285
" Ignore the "failed to create input context" error.
272286
call test_ignore_error('E285')
273287
gui -f
274288
let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
275289
call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
276-
call assert_match('Xhome/\.config/vim/', $MYVIMDIR, msg)
290+
call assert_match('Xhome/\.vim/', $MYVIMDIR, msg)
277291
call filter(g:, {idx, _ -> idx =~ '^rc'})
278292
call assert_equal(#{rc_three: 'three', rc: '.config/vim/gvimrc'}, g:)
279293
call writefile(v:errors, 'Xresult')
@@ -286,14 +300,15 @@ func Test_zzz_xdg_runtime_files()
286300
call delete(rc3)
287301

288302
" Test for ~/xdg/vim/gvimrc
303+
" MYVIMDIR is only set to ~/xdg/vim if ~/xdg/vim/vimrc exists!
289304
let $XDG_CONFIG_HOME=expand('~/xdg/')
290305
let lines =<< trim END
291306
" Ignore the "failed to create input context" error.
292307
call test_ignore_error('E285')
293308
gui -f
294309
let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
295310
call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
296-
call assert_match('Xhome/xdg/vim/', $MYVIMDIR, msg)
311+
call assert_match('Xhome/\.vim/', $MYVIMDIR, msg)
297312
call filter(g:, {idx, _ -> idx =~ '^rc'})
298313
call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
299314
call writefile(v:errors, 'Xresult')

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1159,
707709
/**/
708710
1158,
709711
/**/

0 commit comments

Comments
 (0)