Hello,

This patch makes the three commands: mkvimrc, mksession and mkexrc commands could have a directory name as its argument. If the argument is a directory, Then the file, which is the .vimrc, Session.vim or .exrc, is generated under the directory and is named as the default file name (which is .vimrc, Session.vim, .exrc respectively).

Regards,
Hong Xu
05/07/2011

--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
diff -r 60890b26cb17 src/ex_docmd.c
--- a/src/ex_docmd.c    Thu May 05 18:31:59 2011 +0200
+++ b/src/ex_docmd.c    Sat May 07 17:58:41 2011 +0800
@@ -8814,6 +8814,12 @@
     FILE       *fd;
     int                failed = FALSE;
     char_u     *fname;
+    /* if *eap->arg is a directory, set this to the directory name */
+    char_u     *dir_name = NULL;
+    /* default file name according to eap->cmdidx */
+    char_u     *default_file_name = NULL;
+    /* set to 1 if fname needed to be freed */
+    int                fname_free_needed = 0;
 #ifdef FEAT_BROWSE
     char_u     *browseFile = NULL;
 #endif
@@ -8834,6 +8840,24 @@
 #endif
     }
 
+    /* set default_file_name to the default file name */
+    switch (eap->cmdidx)
+    {
+    case CMD_mkvimrc:
+       default_file_name = (char_u *)VIMRC_FILE;
+       break;
+    case CMD_mkexrc:
+       default_file_name = (char_u *)EXRC_FILE;
+       break;
+#ifdef FEAT_SESSION
+    case CMD_mksession:
+       default_file_name = (char_u *)SESSION_FILE;
+       break;
+#endif
+    default:
+       break;
+    }
+
 #ifdef FEAT_SESSION
     /* Use the short file name until ":lcd" is used.  We also don't use the
      * short file name when 'acd' is set, that is checked later. */
@@ -8853,16 +8877,53 @@
     }
     else
 #endif
-       if (*eap->arg != NUL)
-       fname = eap->arg;
-    else if (eap->cmdidx == CMD_mkvimrc)
-       fname = (char_u *)VIMRC_FILE;
-#ifdef FEAT_SESSION
-    else if (eap->cmdidx == CMD_mksession)
-       fname = (char_u *)SESSION_FILE;
-#endif
-    else
-       fname = (char_u *)EXRC_FILE;
+    {
+       /* set dir_name to the directory path if eap->arg is a directory */
+       if (*eap->arg != NUL && mch_isdir(eap->arg))
+       {
+           int arg_len = 0;
+           int end_with_pathsep = 0;
+
+           arg_len = STRLEN(eap->arg);
+           end_with_pathsep = ((eap->arg)[arg_len - 1] == (char_u)'/'
+#ifdef WIN32
+                   || (eap->arg)[arg_len - 1] == (char_u)'\\'
+#endif
+                   );
+           dir_name = (char_u *)alloc(arg_len + (end_with_pathsep ? 0 : 1) + 
1);
+
+           STRCPY(dir_name, eap->arg);
+
+           if (!end_with_pathsep)
+               STRCAT(dir_name,
+#ifdef WIN32
+                       "\\"
+#else
+                       "/"
+#endif
+                     );
+       }
+
+       /*
+        * If eap->arg is not a directory, then set fname to eap->arg directly; 
+        * otherwise we need to set fname to the full path name using dir_name 
+        * as its directory and default_file_name as its file name.
+        */
+       if (*eap->arg != NUL && !dir_name)
+           fname = eap->arg;
+       else
+       {
+           if (dir_name)
+           {
+               fname = alloc(STRLEN(dir_name) + STRLEN(default_file_name) + 1);
+               STRCPY(fname, dir_name);
+               STRCAT(fname, (char_u *)default_file_name);
+               fname_free_needed = 1;
+           }
+           else
+               fname = default_file_name;
+       }
+    }
 
 #ifdef FEAT_BROWSE
     if (cmdmod.browse)
@@ -9029,6 +9090,12 @@
 #ifdef FEAT_SESSION
     vim_free(viewFile);
 #endif
+
+    if (fname_free_needed)
+       vim_free(fname);
+
+    if (dir_name)
+       vim_free(dir_name);
 }
 
 #if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \

Reply via email to