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/runtime/doc/starting.txt b/runtime/doc/starting.txt
index cdc0f16..02885e6 100644
--- a/runtime/doc/starting.txt
+++ b/runtime/doc/starting.txt
@@ -1,4 +1,4 @@
-*starting.txt*  For Vim version 9.1.  Last change: 2024 Aug 03
+*starting.txt*  For Vim version 9.1.  Last change: 2024 Sep 05
 
 
 		  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -828,8 +828,8 @@
 	easy to copy it to another system.
 
 	If Vim was started with "-u filename", the file "filename" is used.
-	All following initializations until 4. are skipped. $MYVIMRC is not
-	set.
+	All following initializations until 4. are skipped. $MYVIMRC and
+	$MYVIMDIR are not set.
 	"vim -u NORC" can be used to skip these initializations without
 	reading a file.  "vim -u NONE" also skips loading plugins.  |-u|
 
@@ -847,11 +847,13 @@
 	'compatible' is only done later.  Add a ":set nocp" command if you
 	like.  For the Macintosh the $VIMRUNTIME/macmap.vim is read.
 
-	  *VIMINIT* *.vimrc* *_vimrc* *EXINIT* *.exrc* *_exrc* *$MYVIMRC*
+	  *VIMINIT* *.vimrc* *_vimrc* *EXINIT* *.exrc* *_exrc* *$MYVIMRC* *$MYVIMDIR*
      c. Five places are searched for initializations.  The first that exists
 	is used, the others are ignored.  The $MYVIMRC environment variable is
 	set to the file that was first found, unless $MYVIMRC was already set
-	and when using VIMINIT.
+	and when using VIMINIT.  The $MYVIMDIR environment variable is
+	set to the personal 'rtp' directory, however it is not verified
+	that the directory actually exists.
 	I   The environment variable VIMINIT (see also |compatible-default|) (*)
 	    The value of $VIMINIT is used as an Ex command line.
 	II  The user vimrc file(s):
@@ -971,7 +973,8 @@
 	The |VimEnter| autocommands are executed.
 
 The $MYVIMRC or $MYGVIMRC file will be set to the first found vimrc and/or
-gvimrc file.
+gvimrc file while $MYVIMDIR is set to the users personal runtime directory
+'rtp' (typically the first entry in 'runtimepath').
 
 
 Some hints on using initializations ~
diff --git a/runtime/doc/tags b/runtime/doc/tags
index 9cd6702..c5801af 100644
--- a/runtime/doc/tags
+++ b/runtime/doc/tags
@@ -7,6 +7,7 @@
 $HOME-use	version5.txt	/*$HOME-use*
 $HOME-windows	options.txt	/*$HOME-windows*
 $MYGVIMRC	gui.txt	/*$MYGVIMRC*
+$MYVIMDIR	starting.txt	/*$MYVIMDIR*
 $MYVIMRC	starting.txt	/*$MYVIMRC*
 $VIM	starting.txt	/*$VIM*
 $VIM-use	version5.txt	/*$VIM-use*
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,