patch 9.1.0718: hard to know the users personal Vim Runtime Directory

Problem:  hard to guess the Vim Runtime Directory
Solution: Set the $MYVIMDIR environment variable to the users
          personal runtime directory (e.g. ~/.vim on Linux)

closes: #15576

Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/option.c b/src/option.c
index b26c606..185a928 100644
--- a/src/option.c
+++ b/src/option.c
@@ -8243,6 +8243,55 @@
 	    if (p != NULL)
 	    {
 		vim_setenv(envname, p);
+		if (vim_getenv((char_u *)"MYVIMDIR", &dofree) == NULL)
+		{
+		    size_t  usedlen = 0;
+		    int     len = 0;
+		    char_u  *fbuf = NULL;
+
+		    if (STRNCMP(gettail(fname), ".vimrc", 6) == 0)
+		    {
+			len = STRLEN(p) - 2;
+			p[len] = '/';
+		    }
+		    else if (STRNCMP(gettail(fname), ".gvimrc", 7) == 0)
+		    {
+			len = STRLEN(p);
+			char_u  *buf = alloc(len);
+			if (buf != NULL)
+			{
+			    mch_memmove(buf, fname, len - 7);
+			    mch_memmove(buf + len - 7, (char_u *)".vim/", 5);
+			    len -= 2;  // decrement len, so that we can set len+1 = NUL below
+			    vim_free(p);
+			    p = buf;
+			}
+		    }
+#ifdef MSWIN
+		    else if (STRNCMP(gettail(fname), "_vimrc", 6) == 0)
+		    {
+			len = STRLEN(p) + 4; // remove _vimrc, add vimfiles/
+			char_u  *buf = alloc(len);
+			if (buf != NULL)
+			{
+			    mch_memmove(buf, fname, len - 10);
+			    mch_memmove(buf + len - 10, (char_u *)"vimfiles\\", 9);
+			    len -= 2;  // decrement len, so that we can set len+1 = NUL below
+			    vim_free(p);
+			    p = buf;
+			}
+		    }
+#endif
+		    else
+			(void)modify_fname((char_u *)":h", FALSE, &usedlen, &p, &fbuf, &len);
+
+		    if (p != NULL)
+		    {
+			// keep the directory separator
+			p[len + 1] = NUL;
+			vim_setenv((char_u *)"MYVIMDIR", p);
+		    }
+		}
 		vim_free(p);
 	    }
 	}
diff --git a/src/testdir/test_xdg.vim b/src/testdir/test_xdg.vim
index b9ec3c7..d4c1d22 100644
--- a/src/testdir/test_xdg.vim
+++ b/src/testdir/test_xdg.vim
@@ -5,9 +5,10 @@
 
 func s:get_rcs()
   let rcs = {
-        \ 'file1': { 'path': '~/.vimrc' },
-        \ 'file2': { 'path': '~/.vim/vimrc' },
-        \ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config" },
+        \ 'file1': { 'path': '~/.vimrc', 'dir': expand('~/.vim/') },
+        \ 'file2': { 'path': '~/.vim/vimrc', 'dir': expand('~/.vim/') },
+        \ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config",
+                  \ 'dir': exists('$XDG_CONFIG_HOME') ? expand("$XDG_CONFIG_HOME/vim") : '~/.config/vim/'},
         \}
   for v in values(rcs)
     let v.exists = filereadable(expand(v.path))
@@ -20,18 +21,24 @@
   let rc = s:get_rcs()
   let before =<< trim CODE
     call writefile([expand('$MYVIMRC')], "XMY_VIMRC")
+    call writefile([expand('$MYVIMRCDIR')], "XMY_VIMDIR")
     quit!
   CODE
   call RunVim(before, [], "")
   let my_rc = readfile("XMY_VIMRC")
+  let my_rcdir = readfile("XMY_VIMDIR")
   if rc.file1.exists
     call assert_equal(rc.file1.path, my_rc)
+    call assert_equal(rc.file1.dir, my_rcdir)
   elseif !rc.file1.exists && rc.file2.exists
     call assert_equal(rc.file2.path, my_rc)
+    call assert_equal(rc.file2.dir, my_rcdir)
   elseif !rc.file1.exists && !rc.file2.exists && rc.xdg.exists
     call assert_equal(rc.xdg.path, my_rc)
+    call assert_equal(rc.xdg.dir, my_rcdir)
   endif
   call delete("XMY_VIMRC")
+  call delete("XMY_VIMDIR")
 endfunc
 
 func Test_xdg_runtime_files()
@@ -78,6 +85,7 @@
   " Test for ~/.vimrc
   let lines =<< trim END
     call assert_match('XfakeHOME/\.vimrc', $MYVIMRC)
+    call assert_match('XfakeHOME/.vim/', $MYVIMDIR)
     call filter(g:, {idx, _ -> idx =~ '^rc'})
     call assert_equal(#{rc_one: 'one', rc: '.vimrc'}, g:)
     call assert_match('XfakeHOME/\.vim/view', &viewdir)
@@ -93,6 +101,7 @@
   " Test for ~/.vim/vimrc
   let lines =<< trim END
     call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC)
+    call assert_match('XfakeHOME/\.vim/', $MYVIMDIR)
     call filter(g:, {idx, _ -> idx =~ '^rc'})
     call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc'}, g:)
     call assert_match('XfakeHOME/\.vim/view', &viewdir)
@@ -112,6 +121,7 @@
   let lines =<< trim END
     let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
     call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg)
+    call assert_match('XfakeHOME/\.config/vim/', $MYVIMDIR, msg)
     call filter(g:, {idx, _ -> idx =~ '^rc'})
     call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc'}, g:)
     call assert_match('XfakeHOME/\.config/vim/view', &viewdir)
@@ -129,6 +139,7 @@
   let lines =<< trim END
     let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
     call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
+    call assert_match('XfakeHOME/xdg/vim/', $MYVIMDIR, msg)
     call filter(g:, {idx, _ -> idx =~ '^rc'})
     call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc'}, g:)
     call assert_match('XfakeHOME/xdg/vim/view, &viewdir)
@@ -225,6 +236,7 @@
     call test_ignore_error('E285')
     gui -f
     call assert_match('Xhome/\.gvimrc', $MYGVIMRC)
+    call assert_match('Xhome/\.vim/', $MYVIMDIR)
     call filter(g:, {idx, _ -> idx =~ '^rc'})
     call assert_equal(#{rc_one: 'one', rc: '.gvimrc'}, g:)
     call writefile(v:errors, 'Xresult')
@@ -242,6 +254,7 @@
     call test_ignore_error('E285')
     gui -f
     call assert_match('Xhome/\.vim/gvimrc', $MYGVIMRC)
+    call assert_match('Xhome/\.vim/', $MYVIMDIR)
     call filter(g:, {idx, _ -> idx =~ '^rc'})
     call assert_equal(#{rc_two: 'two', rc: '.vim/gvimrc'}, g:)
     call writefile(v:errors, 'Xresult')
@@ -260,6 +273,7 @@
     gui -f
     let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
     call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
+    call assert_match('Xhome/\.config/vim/', $MYVIMDIR, msg)
     call filter(g:, {idx, _ -> idx =~ '^rc'})
     call assert_equal(#{rc_three: 'three', rc: '.config/vim/gvimrc'}, g:)
     call writefile(v:errors, 'Xresult')
@@ -279,6 +293,7 @@
     gui -f
     let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
     call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
+    call assert_match('Xhome/xdg/vim/', $MYVIMDIR, msg)
     call filter(g:, {idx, _ -> idx =~ '^rc'})
     call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
     call writefile(v:errors, 'Xresult')
diff --git a/src/version.c b/src/version.c
index 145e083..d7f012c 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    718,
+/**/
     717,
 /**/
     716,