summaryrefslogtreecommitdiffstats
path: root/src/filepath.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/filepath.c')
-rw-r--r--src/filepath.c2041
1 files changed, 1859 insertions, 182 deletions
diff --git a/src/filepath.c b/src/filepath.c
index 0ba1a439d3..21cd767c53 100644
--- a/src/filepath.c
+++ b/src/filepath.c
@@ -36,8 +36,8 @@ get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
if (l > len - 1)
{
- /* If that doesn't work (not enough space), then save the string
- * and try again with a new buffer big enough. */
+ // If that doesn't work (not enough space), then save the string
+ // and try again with a new buffer big enough.
newbuf = vim_strnsave(*fnamep, l);
if (newbuf == NULL)
return FAIL;
@@ -45,7 +45,7 @@ get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
vim_free(*bufp);
*fnamep = *bufp = newbuf;
- /* Really should always succeed, as the buffer is big enough. */
+ // Really should always succeed, as the buffer is big enough.
l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
}
@@ -81,13 +81,13 @@ shortpath_for_invalid_fname(
int new_len, sfx_len;
int retval = OK;
- /* Make a copy */
+ // Make a copy
old_len = *fnamelen;
save_fname = vim_strnsave(*fname, old_len);
pbuf_unused = NULL;
short_fname = NULL;
- endp = save_fname + old_len - 1; /* Find the end of the copy */
+ endp = save_fname + old_len - 1; // Find the end of the copy
save_endp = endp;
/*
@@ -97,11 +97,11 @@ shortpath_for_invalid_fname(
len = 0;
for (;;)
{
- /* go back one path-separator */
+ // go back one path-separator
while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
--endp;
if (endp <= save_fname)
- break; /* processed the complete path */
+ break; // processed the complete path
/*
* Replace the path separator with a NUL and try to shorten the
@@ -116,12 +116,12 @@ shortpath_for_invalid_fname(
retval = FAIL;
goto theend;
}
- *endp = ch; /* preserve the string */
+ *endp = ch; // preserve the string
if (len > 0)
- break; /* successfully shortened the path */
+ break; // successfully shortened the path
- /* failed to shorten the path. Skip the path separator */
+ // failed to shorten the path. Skip the path separator
--endp;
}
@@ -140,8 +140,8 @@ shortpath_for_invalid_fname(
vim_free(*bufp);
if (new_len > old_len)
{
- /* There is not enough space in the currently allocated string,
- * copy it to a buffer big enough. */
+ // There is not enough space in the currently allocated string,
+ // copy it to a buffer big enough.
*fname = *bufp = vim_strnsave(short_fname, new_len);
if (*fname == NULL)
{
@@ -151,15 +151,15 @@ shortpath_for_invalid_fname(
}
else
{
- /* Transfer short_fname to the main buffer (it's big enough),
- * unless get_short_pathname() did its work in-place. */
+ // Transfer short_fname to the main buffer (it's big enough),
+ // unless get_short_pathname() did its work in-place.
*fname = *bufp = save_fname;
if (short_fname != save_fname)
vim_strncpy(save_fname, short_fname, len);
save_fname = NULL;
}
- /* concat the not-shortened part of the path */
+ // concat the not-shortened part of the path
vim_strncpy(*fname + len, endp, sfx_len);
(*fname)[new_len] = NUL;
}
@@ -186,14 +186,14 @@ shortpath_for_partial(
char_u *pbuf, *tfname;
int hasTilde;
- /* Count up the path separators from the RHS.. so we know which part
- * of the path to return. */
+ // Count up the path separators from the RHS.. so we know which part
+ // of the path to return.
sepcount = 0;
for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
if (vim_ispathsep(*p))
++sepcount;
- /* Need full path first (use expand_env() to remove a "~/") */
+ // Need full path first (use expand_env() to remove a "~/")
hasTilde = (**fnamep == '~');
if (hasTilde)
pbuf = tfname = expand_env_save(*fnamep);
@@ -207,16 +207,15 @@ shortpath_for_partial(
if (len == 0)
{
- /* Don't have a valid filename, so shorten the rest of the
- * path if we can. This CAN give us invalid 8.3 filenames, but
- * there's not a lot of point in guessing what it might be.
- */
+ // Don't have a valid filename, so shorten the rest of the
+ // path if we can. This CAN give us invalid 8.3 filenames, but
+ // there's not a lot of point in guessing what it might be.
len = tflen;
if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
return FAIL;
}
- /* Count the paths backward to find the beginning of the desired string. */
+ // Count the paths backward to find the beginning of the desired string.
for (p = tfname + len - 1; p >= tfname; --p)
{
if (has_mbyte)
@@ -240,7 +239,7 @@ shortpath_for_partial(
else
++p;
- /* Copy in the string - p indexes into tfname - allocated at pbuf */
+ // Copy in the string - p indexes into tfname - allocated at pbuf
vim_free(*bufp);
*fnamelen = (int)STRLEN(p);
*bufp = pbuf;
@@ -278,7 +277,7 @@ modify_fname(
#endif
repeat:
- /* ":p" - full path/file_name */
+ // ":p" - full path/file_name
if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
{
has_fullname = 1;
@@ -286,7 +285,7 @@ repeat:
valid |= VALID_PATH;
*usedlen += 2;
- /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
+ // Expand "~/path" for all systems and "~user/path" for Unix and VMS
if ((*fnamep)[0] == '~'
#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
&& ((*fnamep)[1] == '/'
@@ -299,13 +298,13 @@ repeat:
)
{
*fnamep = expand_env_save(*fnamep);
- vim_free(*bufp); /* free any allocated file name */
+ vim_free(*bufp); // free any allocated file name
*bufp = *fnamep;
if (*fnamep == NULL)
return -1;
}
- /* When "/." or "/.." is used: force expansion to get rid of it. */
+ // When "/." or "/.." is used: force expansion to get rid of it.
for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
{
if (vim_ispathsep(*p)
@@ -317,11 +316,11 @@ repeat:
break;
}
- /* FullName_save() is slow, don't use it when not needed. */
+ // FullName_save() is slow, don't use it when not needed.
if (*p != NUL || !vim_isAbsName(*fnamep))
{
*fnamep = FullName_save(*fnamep, *p != NUL);
- vim_free(*bufp); /* free any allocated file name */
+ vim_free(*bufp); // free any allocated file name
*bufp = *fnamep;
if (*fnamep == NULL)
return -1;
@@ -354,12 +353,12 @@ repeat:
}
# endif
#endif
- /* Append a path separator to a directory. */
+ // Append a path separator to a directory.
if (mch_isdir(*fnamep))
{
- /* Make room for one or two extra characters. */
+ // Make room for one or two extra characters.
*fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
- vim_free(*bufp); /* free any allocated file name */
+ vim_free(*bufp); // free any allocated file name
*bufp = *fnamep;
if (*fnamep == NULL)
return -1;
@@ -367,9 +366,9 @@ repeat:
}
}
- /* ":." - path relative to the current directory */
- /* ":~" - path relative to the home directory */
- /* ":8" - shortname path - postponed till after */
+ // ":." - path relative to the current directory
+ // ":~" - path relative to the home directory
+ // ":8" - shortname path - postponed till after
while (src[*usedlen] == ':'
&& ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
{
@@ -377,12 +376,12 @@ repeat:
if (c == '8')
{
#ifdef MSWIN
- has_shortname = 1; /* Postpone this. */
+ has_shortname = 1; // Postpone this.
#endif
continue;
}
pbuf = NULL;
- /* Need full path first (use expand_env() to remove a "~/") */
+ // Need full path first (use expand_env() to remove a "~/")
if (!has_fullname)
{
if (c == '.' && **fnamep == '~')
@@ -406,7 +405,7 @@ repeat:
*fnamep = s;
if (pbuf != NULL)
{
- vim_free(*bufp); /* free any allocated file name */
+ vim_free(*bufp); // free any allocated file name
*bufp = pbuf;
pbuf = NULL;
}
@@ -415,7 +414,7 @@ repeat:
else
{
home_replace(NULL, p, dirname, MAXPATHL, TRUE);
- /* Only replace it when it starts with '~' */
+ // Only replace it when it starts with '~'
if (*dirname == '~')
{
s = vim_strsave(dirname);
@@ -434,8 +433,8 @@ repeat:
tail = gettail(*fnamep);
*fnamelen = (int)STRLEN(*fnamep);
- /* ":h" - head, remove "/file_name", can be repeated */
- /* Don't remove the first "/" or "c:\" */
+ // ":h" - head, remove "/file_name", can be repeated
+ // Don't remove the first "/" or "c:\"
while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
{
valid |= VALID_HEAD;
@@ -446,11 +445,11 @@ repeat:
*fnamelen = (int)(tail - *fnamep);
#ifdef VMS
if (*fnamelen > 0)
- *fnamelen += 1; /* the path separator is part of the path */
+ *fnamelen += 1; // the path separator is part of the path
#endif
if (*fnamelen == 0)
{
- /* Result is empty. Turn it into "." to make ":cd %:h" work. */
+ // Result is empty. Turn it into "." to make ":cd %:h" work.
p = vim_strsave((char_u *)".");
if (p == NULL)
return -1;
@@ -465,7 +464,7 @@ repeat:
}
}
- /* ":8" - shortname */
+ // ":8" - shortname
if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
{
*usedlen += 2;
@@ -480,9 +479,9 @@ repeat:
*/
if (has_shortname)
{
- /* Copy the string if it is shortened by :h and when it wasn't copied
- * yet, because we are going to change it in place. Avoids changing
- * the buffer name for "%:8". */
+ // Copy the string if it is shortened by :h and when it wasn't copied
+ // yet, because we are going to change it in place. Avoids changing
+ // the buffer name for "%:8".
if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
{
p = vim_strnsave(*fnamep, *fnamelen);
@@ -492,8 +491,8 @@ repeat:
*bufp = *fnamep = p;
}
- /* Split into two implementations - makes it easier. First is where
- * there isn't a full name already, second is where there is. */
+ // Split into two implementations - makes it easier. First is where
+ // there isn't a full name already, second is where there is.
if (!has_fullname && !vim_isAbsName(*fnamep))
{
if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
@@ -503,14 +502,14 @@ repeat:
{
int l = *fnamelen;
- /* Simple case, already have the full-name.
- * Nearly always shorter, so try first time. */
+ // Simple case, already have the full-name.
+ // Nearly always shorter, so try first time.
if (get_short_pathname(fnamep, bufp, &l) == FAIL)
return -1;
if (l == 0)
{
- /* Couldn't find the filename, search the paths. */
+ // Couldn't find the filename, search the paths.
l = *fnamelen;
if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
return -1;
@@ -520,7 +519,7 @@ repeat:
}
#endif // MSWIN
- /* ":t" - tail, just the basename */
+ // ":t" - tail, just the basename
if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
{
*usedlen += 2;
@@ -528,15 +527,14 @@ repeat:
*fnamep = tail;
}
- /* ":e" - extension, can be repeated */
- /* ":r" - root, without extension, can be repeated */
+ // ":e" - extension, can be repeated
+ // ":r" - root, without extension, can be repeated
while (src[*usedlen] == ':'
&& (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
{
- /* find a '.' in the tail:
- * - for second :e: before the current fname
- * - otherwise: The last '.'
- */
+ // find a '.' in the tail:
+ // - for second :e: before the current fname
+ // - otherwise: The last '.'
if (src[*usedlen + 1] == 'e' && *fnamep > tail)
s = *fnamep - 2;
else
@@ -544,14 +542,14 @@ repeat:
for ( ; s > tail; --s)
if (s[0] == '.')
break;
- if (src[*usedlen + 1] == 'e') /* :e */
+ if (src[*usedlen + 1] == 'e') // :e
{
if (s > tail)
{
*fnamelen += (int)(*fnamep - (s + 1));
*fnamep = s + 1;
#ifdef VMS
- /* cut version from the extension */
+ // cut version from the extension
s = *fnamep + *fnamelen - 1;
for ( ; s > *fnamep; --s)
if (s[0] == ';')
@@ -563,16 +561,16 @@ repeat:
else if (*fnamep <= tail)
*fnamelen = 0;
}
- else /* :r */
+ else // :r
{
- if (s > tail) /* remove one extension */
+ if (s > tail) // remove one extension
*fnamelen = (int)(s - *fnamep);
}
*usedlen += 2;
}
- /* ":s?pat?foo?" - substitute */
- /* ":gs?pat?foo?" - global substitute */
+ // ":s?pat?foo?" - substitute
+ // ":gs?pat?foo?" - global substitute
if (src[*usedlen] == ':'
&& (src[*usedlen + 1] == 's'
|| (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
@@ -595,7 +593,7 @@ repeat:
sep = *s++;
if (sep)
{
- /* find end of pattern */
+ // find end of pattern
p = vim_strchr(s, sep);
if (p != NULL)
{
@@ -603,7 +601,7 @@ repeat:
if (pat != NULL)
{
s = p + 1;
- /* find end of substitution */
+ // find end of substitution
p = vim_strchr(s, sep);
if (p != NULL)
{
@@ -628,7 +626,7 @@ repeat:
vim_free(pat);
}
}
- /* after using ":s", repeat all the modifiers */
+ // after using ":s", repeat all the modifiers
if (didit)
goto repeat;
}
@@ -636,7 +634,7 @@ repeat:
if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
{
- /* vim_strsave_shellescape() needs a NUL terminated string. */
+ // vim_strsave_shellescape() needs a NUL terminated string.
c = (*fnamep)[*fnamelen];
if (c != NUL)
(*fnamep)[*fnamelen] = NUL;
@@ -722,13 +720,13 @@ f_delete(typval_T *argvars, typval_T *rettv)
flags = (char_u *)"";
if (*flags == NUL)
- /* delete a file */
+ // delete a file
rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
else if (STRCMP(flags, "d") == 0)
- /* delete an empty directory */
+ // delete an empty directory
rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
else if (STRCMP(flags, "rf") == 0)
- /* delete a directory recursively */
+ // delete a directory recursively
rettv->vval.v_number = delete_recursive(name);
else
semsg(_(e_invexpr2), flags);
@@ -742,7 +740,7 @@ f_executable(typval_T *argvars, typval_T *rettv)
{
char_u *name = tv_get_string(&argvars[0]);
- /* Check in $PATH and also check directly if there is a directory name. */
+ // Check in $PATH and also check directly if there is a directory name.
rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
}
@@ -1022,7 +1020,7 @@ f_getfsize(typval_T *argvars, typval_T *rettv)
{
rettv->vval.v_number = (varnumber_T)st.st_size;
- /* non-perfect check for overflow */
+ // non-perfect check for overflow
if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
rettv->vval.v_number = -2;
}
@@ -1095,8 +1093,8 @@ f_glob(typval_T *argvars, typval_T *rettv)
expand_T xpc;
int error = FALSE;
- /* When the optional second argument is non-zero, don't remove matches
- * for 'wildignore' and don't put matches for 'suffixes' at the end. */
+ // When the optional second argument is non-zero, don't remove matches
+ // for 'wildignore' and don't put matches for 'suffixes' at the end.
rettv->v_type = VAR_STRING;
if (argvars[1].v_type != VAR_UNKNOWN)
{
@@ -1250,13 +1248,13 @@ mkdir_recurse(char_u *dir, int prot)
char_u *updir;
int r = FAIL;
- /* Get end of directory name in "dir".
- * We're done when it's "/" or "c:/". */
+ // Get end of directory name in "dir".
+ // We're done when it's "/" or "c:/".
p = gettail_sep(dir);
if (p <= get_past_head(dir))
return OK;
- /* If the directory exists we're done. Otherwise: create it.*/
+ // If the directory exists we're done. Otherwise: create it.
updir = vim_strnsave(dir, (int)(p - dir));
if (updir == NULL)
return FAIL;
@@ -1287,7 +1285,7 @@ f_mkdir(typval_T *argvars, typval_T *rettv)
return;
if (*gettail(dir) == NUL)
- /* remove trailing slashes */
+ // remove trailing slashes
*gettail_sep(dir) = NUL;
if (argvars[1].v_type != VAR_UNKNOWN)
@@ -1302,7 +1300,7 @@ f_mkdir(typval_T *argvars, typval_T *rettv)
{
if (mch_isdir(dir))
{
- /* With the "p" flag it's OK if the dir already exists. */
+ // With the "p" flag it's OK if the dir already exists.
rettv->vval.v_number = OK;
return;
}
@@ -1353,16 +1351,16 @@ f_readfile(typval_T *argvars, typval_T *rettv)
int failed = FALSE;
char_u *fname;
FILE *fd;
- char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
+ char_u buf[(IOSIZE/256)*256]; // rounded to avoid odd + 1
int io_size = sizeof(buf);
- int readlen; /* size of last fread() */
- char_u *prev = NULL; /* previously read bytes, if any */
- long prevlen = 0; /* length of data in prev */
- long prevsize = 0; /* size of prev buffer */
+ int readlen; // size of last fread()
+ char_u *prev = NULL; // previously read bytes, if any
+ long prevlen = 0; // length of data in prev
+ long prevsize = 0; // size of prev buffer
long maxline = MAXLNUM;
long cnt = 0;
- char_u *p; /* position in buf */
- char_u *start; /* start of current line */
+ char_u *p; // position in buf
+ char_u *start; // start of current line
if (argvars[1].v_type != VAR_UNKNOWN)
{
@@ -1386,8 +1384,8 @@ f_readfile(typval_T *argvars, typval_T *rettv)
return;
}
- /* Always open the file in binary mode, library functions have a mind of
- * their own about CR-LF conversion. */
+ // Always open the file in binary mode, library functions have a mind of
+ // their own about CR-LF conversion.
fname = tv_get_string(&argvars[0]);
if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
{
@@ -1410,11 +1408,11 @@ f_readfile(typval_T *argvars, typval_T *rettv)
{
readlen = (int)fread(buf, 1, io_size, fd);
- /* This for loop processes what was read, but is also entered at end
- * of file so that either:
- * - an incomplete line gets written
- * - a "binary" file gets an empty line at the end if it ends in a
- * newline. */
+ // This for loop processes what was read, but is also entered at end
+ // of file so that either:
+ // - an incomplete line gets written
+ // - a "binary" file gets an empty line at the end if it ends in a
+ // newline.
for (p = buf, start = buf;
p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
++p)
@@ -1425,12 +1423,12 @@ f_readfile(typval_T *argvars, typval_T *rettv)
char_u *s = NULL;
long_u len = p - start;
- /* Finished a line. Remove CRs before NL. */
+ // Finished a line. Remove CRs before NL.
if (readlen > 0 && !binary)
{
while (len > 0 && start[len - 1] == '\r')
--len;
- /* removal may cross back to the "prev" string */
+ // removal may cross back to the "prev" string
if (len == 0)
while (prevlen > 0 && prev[prevlen - 1] == '\r')
--prevlen;
@@ -1439,14 +1437,14 @@ f_readfile(typval_T *argvars, typval_T *rettv)
s = vim_strnsave(start, (int)len);
else
{
- /* Change "prev" buffer to be the right size. This way
- * the bytes are only copied once, and very long lines are
- * allocated only once. */
+ // Change "prev" buffer to be the right size. This way
+ // the bytes are only copied once, and very long lines are
+ // allocated only once.
if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
{
mch_memmove(s + prevlen, start, len);
s[prevlen + len] = NUL;
- prev = NULL; /* the list will own the string */
+ prev = NULL; // the list will own the string
prevlen = prevsize = 0;
}
}
@@ -1468,18 +1466,18 @@ f_readfile(typval_T *argvars, typval_T *rettv)
li->li_tv.vval.v_string = s;
list_append(rettv->vval.v_list, li);
- start = p + 1; /* step over newline */
+ start = p + 1; // step over newline
if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
break;
}
else if (*p == NUL)
*p = '\n';
- /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
- * when finding the BF and check the previous two bytes. */
+ // Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
+ // when finding the BF and check the previous two bytes.
else if (*p == 0xbf && enc_utf8 && !binary)
{
- /* Find the two bytes before the 0xbf. If p is at buf, or buf
- * + 1, these may be in the "prev" string. */
+ // Find the two bytes before the 0xbf. If p is at buf, or buf
+ // + 1, these may be in the "prev" string.
char_u back1 = p >= buf + 1 ? p[-1]
: prevlen >= 1 ? prev[prevlen - 1] : NUL;
char_u back2 = p >= buf + 2 ? p[-2]
@@ -1490,19 +1488,18 @@ f_readfile(typval_T *argvars, typval_T *rettv)
{
char_u *dest = p - 2;
- /* Usually a BOM is at the beginning of a file, and so at
- * the beginning of a line; then we can just step over it.
- */
+ // Usually a BOM is at the beginning of a file, and so at
+ // the beginning of a line; then we can just step over it.
if (start == dest)
start = p + 1;
else
{
- /* have to shuffle buf to close gap */
+ // have to shuffle buf to close gap
int adjust_prevlen = 0;
if (dest < buf)
{
- adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
+ adjust_prevlen = (int)(buf - dest); // must be 1 or 2
dest = buf;
}
if (readlen > p - buf + 1)
@@ -1513,22 +1510,22 @@ f_readfile(typval_T *argvars, typval_T *rettv)
}
}
}
- } /* for */
+ } // for
if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
break;
if (start < p)
{
- /* There's part of a line in buf, store it in "prev". */
+ // There's part of a line in buf, store it in "prev".
if (p - start + prevlen >= prevsize)
{
- /* need bigger "prev" buffer */
+ // need bigger "prev" buffer
char_u *newprev;
- /* A common use case is ordinary text files and "prev" gets a
- * fragment of a line, so the first allocation is made
- * small, to avoid repeatedly 'allocing' large and
- * 'reallocing' small. */
+ // A common use case is ordinary text files and "prev" gets a
+ // fragment of a line, so the first allocation is made
+ // small, to avoid repeatedly 'allocing' large and
+ // 'reallocing' small.
if (prevsize == 0)
prevsize = (long)(p - start);
else
@@ -1546,16 +1543,14 @@ f_readfile(typval_T *argvars, typval_T *rettv)
}
prev = newprev;
}
- /* Add the line part to end of "prev". */
+ // Add the line part to end of "prev".
mch_memmove(prev + prevlen, start, p - start);
prevlen += (long)(p - start);
}
- } /* while */
+ } // while
- /*
- * For a negative line count use only the lines at the end of the file,
- * free the rest.
- */
+ // For a negative line count use only the lines at the end of the file,
+ // free the rest.
if (!failed && maxline < 0)
while (cnt > -maxline)
{
@@ -1617,14 +1612,14 @@ f_resolve(typval_T *argvars, typval_T *rettv)
if (len > 0 && after_pathsep(p, p + len))
{
has_trailing_pathsep = TRUE;
- p[len - 1] = NUL; /* the trailing slash breaks readlink() */
+ p[len - 1] = NUL; // the trailing slash breaks readlink()
}
q = getnextcomp(p);
if (*q != NUL)
{
- /* Separate the first path component in "p", and keep the
- * remainder (beginning with the path separator). */
+ // Separate the first path component in "p", and keep the
+ // remainder (beginning with the path separator).
remain = vim_strsave(q - 1);
q[-1] = NUL;
}
@@ -1651,13 +1646,13 @@ f_resolve(typval_T *argvars, typval_T *rettv)
goto fail;
}
- /* Ensure that the result will have a trailing path separator
- * if the argument has one. */
+ // Ensure that the result will have a trailing path separator
+ // if the argument has one.
if (remain == NULL && has_trailing_pathsep)
add_pathsep(buf);
- /* Separate the first path component in the link value and
- * concatenate the remainders. */
+ // Separate the first path component in the link value and
+ // concatenate the remainders.
q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
if (*q != NUL)
{
@@ -1678,13 +1673,13 @@ f_resolve(typval_T *argvars, typval_T *rettv)
q = gettail(p);
if (q > p && *q == NUL)
{
- /* Ignore trailing path separator. */
+ // Ignore trailing path separator.
q[-1] = NUL;
q = gettail(p);
}
if (q > p && !mch_isFullName(buf))
{
- /* symlink is relative to directory of argument */
+ // symlink is relative to directory of argument
cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
if (cpy != NULL)
{
@@ -1704,7 +1699,7 @@ f_resolve(typval_T *argvars, typval_T *rettv)
if (remain == NULL)
break;
- /* Append the first path component of "remain" to "p". */
+ // Append the first path component of "remain" to "p".
q = getnextcomp(remain + 1);
len = q - remain - (*q != NUL);
cpy = vim_strnsave(p, STRLEN(p) + len);
@@ -1714,15 +1709,15 @@ f_resolve(typval_T *argvars, typval_T *rettv)
vim_free(p);
p = cpy;
}
- /* Shorten "remain". */
+ // Shorten "remain".
if (*q != NUL)
STRMOVE(remain, q - 1);
else
VIM_CLEAR(remain);
}
- /* If the result is a relative path name, make it explicitly relative to
- * the current directory if and only if the argument had this form. */
+ // If the result is a relative path name, make it explicitly relative to
+ // the current directory if and only if the argument had this form.
if (!vim_ispathsep(*p))
{
if (is_relative_to_current
@@ -1734,7 +1729,7 @@ f_resolve(typval_T *argvars, typval_T *rettv)
&& (p[2] == NUL
|| vim_ispathsep(p[2]))))))
{
- /* Prepend "./". */
+ // Prepend "./".
cpy = concat_str((char_u *)"./", p);
if (cpy != NULL)
{
@@ -1744,7 +1739,7 @@ f_resolve(typval_T *argvars, typval_T *rettv)
}
else if (!is_relative_to_current)
{
- /* Strip leading "./". */
+ // Strip leading "./".
q = p;
while (q[0] == '.' && vim_ispathsep(q[1]))
q += 2;
@@ -1753,8 +1748,8 @@ f_resolve(typval_T *argvars, typval_T *rettv)
}
}
- /* Ensure that the result will have no trailing path separator
- * if the argument had none. But keep "/" or "//". */
+ // Ensure that the result will have no trailing path separator
+ // if the argument had none. But keep "/" or "//".
if (!has_trailing_pathsep)
{
q = p + STRLEN(p);
@@ -1789,8 +1784,8 @@ f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
rettv->v_type = VAR_STRING;
rettv->vval.v_string = vim_tempname(x, FALSE);
- /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
- * names. Skip 'I' and 'O', they are used for shell redirection. */
+ // Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
+ // names. Skip 'I' and 'O', they are used for shell redirection.
do
{
if (x == 'Z')
@@ -1876,8 +1871,8 @@ f_writefile(typval_T *argvars, typval_T *rettv)
if (fname == NULL)
return;
- /* Always open the file in binary mode, library functions have a mind of
- * their own about CR-LF conversion. */
+ // Always open the file in binary mode, library functions have a mind of
+ // their own about CR-LF conversion.
if (*fname == NUL || (fd = mch_fopen((char *)fname,
append ? APPENDBIN : WRITEBIN)) == NULL)
{
@@ -1902,8 +1897,8 @@ f_writefile(typval_T *argvars, typval_T *rettv)
ret = -1;
#ifdef HAVE_FSYNC
else if (do_fsync)
- /* Ignore the error, the user wouldn't know what to do about it.
- * May happen for a device. */
+ // Ignore the error, the user wouldn't know what to do about it.
+ // May happen for a device.
vim_ignored = vim_fsync(fileno(fd));
#endif
fclose(fd);
@@ -1921,22 +1916,22 @@ f_writefile(typval_T *argvars, typval_T *rettv)
*/
char_u *
do_browse(
- int flags, /* BROWSE_SAVE and BROWSE_DIR */
- char_u *title, /* title for the window */
- char_u *dflt, /* default file name (may include directory) */
- char_u *ext, /* extension added */
- char_u *initdir, /* initial directory, NULL for current dir or
- when using path from "dflt" */
- char_u *filter, /* file name filter */
- buf_T *buf) /* buffer to read/write for */
+ int flags, // BROWSE_SAVE and BROWSE_DIR
+ char_u *title, // title for the window
+ char_u *dflt, // default file name (may include directory)
+ char_u *ext, // extension added
+ char_u *initdir, // initial directory, NULL for current dir or
+ // when using path from "dflt"
+ char_u *filter, // file name filter
+ buf_T *buf) // buffer to read/write for
{
char_u *fname;
- static char_u *last_dir = NULL; /* last used directory */
+ static char_u *last_dir = NULL; // last used directory
char_u *tofree = NULL;
int save_browse = cmdmod.browse;
- /* Must turn off browse to avoid that autocommands will get the
- * flag too! */
+ // Must turn off browse to avoid that autocommands will get the
+ // flag too!
cmdmod.browse = FALSE;
if (title == NULL || *title == NUL)
@@ -1949,16 +1944,16 @@ do_browse(
title = (char_u *)_("Open File dialog");
}
- /* When no directory specified, use default file name, default dir, buffer
- * dir, last dir or current dir */
+ // When no directory specified, use default file name, default dir, buffer
+ // dir, last dir or current dir
if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
{
- if (mch_isdir(dflt)) /* default file name is a directory */
+ if (mch_isdir(dflt)) // default file name is a directory
{
initdir = dflt;
dflt = NULL;
}
- else if (gettail(dflt) != dflt) /* default file name includes a path */
+ else if (gettail(dflt) != dflt) // default file name includes a path
{
tofree = vim_strsave(dflt);
if (tofree != NULL)
@@ -1972,13 +1967,13 @@ do_browse(
if (initdir == NULL || *initdir == NUL)
{
- /* When 'browsedir' is a directory, use it */
+ // When 'browsedir' is a directory, use it
if (STRCMP(p_bsdir, "last") != 0
&& STRCMP(p_bsdir, "buffer") != 0
&& STRCMP(p_bsdir, "current") != 0
&& mch_isdir(p_bsdir))
initdir = p_bsdir;
- /* When saving or 'browsedir' is "buffer", use buffer fname */
+ // When saving or 'browsedir' is "buffer", use buffer fname
else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
&& buf != NULL && buf->b_ffname != NULL)
{
@@ -1991,15 +1986,15 @@ do_browse(
*gettail(initdir) = NUL;
}
}
- /* When 'browsedir' is "last", use dir from last browse */
+ // When 'browsedir' is "last", use dir from last browse
else if (*p_bsdir == 'l')
initdir = last_dir;
- /* When 'browsedir is "current", use current directory. This is the
- * default already, leave initdir empty. */
+ // When 'browsedir is "current", use current directory. This is the
+ // default already, leave initdir empty.
}
# ifdef FEAT_GUI
- if (gui.in_use) /* when this changes, also adjust f_has()! */
+ if (gui.in_use) // when this changes, also adjust f_has()!
{
if (filter == NULL
# ifdef FEAT_EVAL
@@ -2011,23 +2006,23 @@ do_browse(
if (flags & BROWSE_DIR)
{
# if defined(FEAT_GUI_GTK) || defined(MSWIN)
- /* For systems that have a directory dialog. */
+ // For systems that have a directory dialog.
fname = gui_mch_browsedir(title, initdir);
# else
- /* Generic solution for selecting a directory: select a file and
- * remove the file name. */
+ // Generic solution for selecting a directory: select a file and
+ // remove the file name.
fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
# endif
# if !defined(FEAT_GUI_GTK)
- /* Win32 adds a dummy file name, others return an arbitrary file
- * name. GTK+ 2 returns only the directory, */
+ // Win32 adds a dummy file name, others return an arbitrary file
+ // name. GTK+ 2 returns only the directory,
if (fname != NULL && *fname != NUL && !mch_isdir(fname))
{
- /* Remove the file name. */
+ // Remove the file name.
char_u *tail = gettail_sep(fname);
if (tail == fname)
- *tail++ = '.'; /* use current dir */
+ *tail++ = '.'; // use current dir
*tail = NUL;
}
# endif
@@ -2036,21 +2031,21 @@ do_browse(
fname = gui_mch_browse(flags & BROWSE_SAVE,
title, dflt, ext, initdir, (char_u *)_(filter));
- /* We hang around in the dialog for a while, the user might do some
- * things to our files. The Win32 dialog allows deleting or renaming
- * a file, check timestamps. */
+ // We hang around in the dialog for a while, the user might do some
+ // things to our files. The Win32 dialog allows deleting or renaming
+ // a file, check timestamps.
need_check_timestamps = TRUE;
did_check_timestamps = FALSE;
}
else
# endif
{
- /* TODO: non-GUI file selector here */
+ // TODO: non-GUI file selector here
emsg(_("E338: Sorry, no file browser in console mode"));
fname = NULL;
}
- /* keep the directory for next time */
+ // keep the directory for next time
if (fname != NULL)
{
vim_free(last_dir);
@@ -2060,7 +2055,7 @@ do_browse(
*gettail(last_dir) = NUL;
if (*last_dir == NUL)
{
- /* filename only returned, must be in current dir */
+ // filename only returned, must be in current dir
vim_free(last_dir);
last_dir = alloc(MAXPATHL);
if (last_dir != NULL)
@@ -2136,3 +2131,1685 @@ f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
}
#endif // FEAT_EVAL
+
+/*
+ * Replace home directory by "~" in each space or comma separated file name in
+ * 'src'.
+ * If anything fails (except when out of space) dst equals src.
+ */
+ void
+home_replace(
+ buf_T *buf, // when not NULL, check for help files
+ char_u *src, // input file name
+ char_u *dst, // where to put the result
+ int dstlen, // maximum length of the result
+ int one) // if TRUE, only replace one file name, include
+ // spaces and commas in the file name.
+{
+ size_t dirlen = 0, envlen = 0;
+ size_t len;
+ char_u *homedir_env, *homedir_env_orig;
+ char_u *p;
+
+ if (src == NULL)
+ {
+ *dst = NUL;
+ return;
+ }
+
+ /*
+ * If the file is a help file, remove the path completely.
+ */
+ if (buf != NULL && buf->b_help)
+ {
+ vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
+ return;
+ }
+
+ /*
+ * We check both the value of the $HOME environment variable and the
+ * "real" home directory.
+ */
+ if (homedir != NULL)
+ dirlen = STRLEN(homedir);
+
+#ifdef VMS
+ homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
+#else
+ homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
+#endif
+#ifdef MSWIN
+ if (homedir_env == NULL)
+ homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
+#endif
+ // Empty is the same as not set.
+ if (homedir_env != NULL && *homedir_env == NUL)
+ homedir_env = NULL;
+
+ if (homedir_env != NULL && *homedir_env == '~')
+ {
+ int usedlen = 0;
+ int flen;
+ char_u *fbuf = NULL;
+
+ flen = (int)STRLEN(homedir_env);
+ (void)modify_fname((char_u *)":p", FALSE, &usedlen,
+ &homedir_env, &fbuf, &flen);
+ flen = (int)STRLEN(homedir_env);
+ if (flen >