mirror of
https://github.com/neovim/neovim.git
synced 2025-09-14 15:28:17 +00:00
vim-patch:7.4.1107
Problem: Vim can create a directory but not delete it.
Solution: Add an argument to delete() to make it possible to delete a
directory, also recursively.
da440d21a6
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
*eval.txt* For Vim version 7.4. Last change: 2015 Sep 19
|
||||
*eval.txt* For Vim version 7.4. Last change: 2016 Jan 16
|
||||
|
||||
|
||||
VIM REFERENCE MANUAL by Bram Moolenaar
|
||||
@@ -1830,7 +1830,7 @@ cursor( {lnum}, {col} [, {off}])
|
||||
Number move cursor to {lnum}, {col}, {off}
|
||||
cursor( {list}) Number move cursor to position in {list}
|
||||
deepcopy( {expr} [, {noref}]) any make a full copy of {expr}
|
||||
delete( {fname}) Number delete file {fname}
|
||||
delete( {fname} [, {flags}]) Number delete the file or directory {fname}
|
||||
dictwatcheradd( {dict}, {pattern}, {callback})
|
||||
Start watching a dictionary
|
||||
dictwatcherdel( {dict}, {pattern}, {callback})
|
||||
@@ -2770,13 +2770,18 @@ deepcopy({expr}[, {noref}]) *deepcopy()* *E698*
|
||||
{noref} set to 1 will fail.
|
||||
Also see |copy()|.
|
||||
|
||||
delete({fname}) *delete()*
|
||||
Deletes the file by the name {fname}. The result is a Number,
|
||||
which is 0 if the file was deleted successfully, and non-zero
|
||||
when the deletion failed.
|
||||
Use |remove()| to delete an item from a |List|.
|
||||
To delete a line from the buffer use |:delete|. Use |:exe|
|
||||
when the line number is in a variable.
|
||||
delete({fname} [, {flags}]) *delete()*
|
||||
Without {flags} or with {flags} empty: Deletes the file by the
|
||||
name {fname}.
|
||||
|
||||
When {flags} is "d": Deletes the directory by the name
|
||||
{fname}. This fails when {fname} is not empty.
|
||||
|
||||
When {flags} is "rf": Deletes the directory by the name
|
||||
{fname} and everything in it, recursively. Be careful!
|
||||
|
||||
The result is a Number, which is 0 if the delete operation was
|
||||
successful and -1 when the deletion failed or partly failed.
|
||||
|
||||
dictwatcheradd({dict}, {pattern}, {callback}) *dictwatcheradd()*
|
||||
Adds a watcher to a dictionary. A dictionary watcher is
|
||||
|
@@ -6702,7 +6702,7 @@ static struct fst {
|
||||
{ "cscope_connection", 0, 3, f_cscope_connection },
|
||||
{ "cursor", 1, 3, f_cursor },
|
||||
{ "deepcopy", 1, 2, f_deepcopy },
|
||||
{ "delete", 1, 1, f_delete },
|
||||
{ "delete", 1, 2, f_delete },
|
||||
{ "dictwatcheradd", 3, 3, f_dictwatcheradd },
|
||||
{ "dictwatcherdel", 3, 3, f_dictwatcherdel },
|
||||
{ "did_filetype", 0, 0, f_did_filetype },
|
||||
@@ -8374,15 +8374,42 @@ static void f_deepcopy(typval_T *argvars, typval_T *rettv)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* "delete()" function
|
||||
*/
|
||||
// "delete()" function
|
||||
static void f_delete(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
if (check_restricted() || check_secure())
|
||||
rettv->vval.v_number = -1;
|
||||
else
|
||||
rettv->vval.v_number = os_remove((char *)get_tv_string(&argvars[0]));
|
||||
char_u nbuf[NUMBUFLEN];
|
||||
char_u *name;
|
||||
char_u *flags;
|
||||
|
||||
rettv->vval.v_number = -1;
|
||||
if (check_restricted() || check_secure()) {
|
||||
return;
|
||||
}
|
||||
|
||||
name = get_tv_string(&argvars[0]);
|
||||
if (name == NULL || *name == NUL) {
|
||||
EMSG(_(e_invarg));
|
||||
return;
|
||||
}
|
||||
|
||||
if (argvars[1].v_type != VAR_UNKNOWN) {
|
||||
flags = get_tv_string_buf(&argvars[1], nbuf);
|
||||
} else {
|
||||
flags = (char_u *)"";
|
||||
}
|
||||
|
||||
if (*flags == NUL) {
|
||||
// delete a file
|
||||
rettv->vval.v_number = os_remove((char *)name) == 0 ? 0 : -1;
|
||||
} else if (STRCMP(flags, "d") == 0) {
|
||||
// delete an empty directory
|
||||
rettv->vval.v_number = os_rmdir((char *)name) == 0 ? 0 : -1;
|
||||
} else if (STRCMP(flags, "rf") == 0) {
|
||||
// delete a directory recursively
|
||||
rettv->vval.v_number = delete_recursive(name);
|
||||
} else {
|
||||
EMSG2(_(e_invexpr2), flags);
|
||||
}
|
||||
}
|
||||
|
||||
// dictwatcheradd(dict, key, funcref) function
|
||||
|
@@ -56,28 +56,47 @@ static void vim_maketempdir(void)
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete "name" and everything in it, recursively.
|
||||
/// @param name The path which should be deleted.
|
||||
/// @return 0 for success, -1 if some file was not deleted.
|
||||
int delete_recursive(char_u *name)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (os_isdir(name)) {
|
||||
snprintf((char *)NameBuff, MAXPATHL, "%s/*", name);
|
||||
|
||||
char_u **files;
|
||||
int file_count;
|
||||
char_u *exp = vim_strsave(NameBuff);
|
||||
if (gen_expand_wildcards(1, &exp, &file_count, &files,
|
||||
EW_DIR | EW_FILE | EW_SILENT) == OK) {
|
||||
for (int i = 0; i < file_count; i++) {
|
||||
if (delete_recursive(files[i]) != 0) {
|
||||
result = -1;
|
||||
}
|
||||
}
|
||||
FreeWild(file_count, files);
|
||||
} else {
|
||||
result = -1;
|
||||
}
|
||||
|
||||
xfree(exp);
|
||||
os_rmdir((char *)name);
|
||||
} else {
|
||||
result = os_remove((char *)name) == 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Delete the temp directory and all files it contains.
|
||||
void vim_deltempdir(void)
|
||||
{
|
||||
if (vim_tempdir != NULL) {
|
||||
snprintf((char *)NameBuff, MAXPATHL, "%s*", vim_tempdir);
|
||||
|
||||
char_u **files;
|
||||
int file_count;
|
||||
|
||||
// Note: We cannot just do `&NameBuff` because it is a statically
|
||||
// sized array so `NameBuff == &NameBuff` according to C semantics.
|
||||
char_u *buff_list[1] = {NameBuff};
|
||||
if (gen_expand_wildcards(1, buff_list, &file_count, &files,
|
||||
EW_DIR|EW_FILE|EW_SILENT) == OK) {
|
||||
for (int i = 0; i < file_count; ++i) {
|
||||
os_remove((char *)files[i]);
|
||||
}
|
||||
FreeWild(file_count, files);
|
||||
}
|
||||
path_tail(NameBuff)[-1] = NUL;
|
||||
os_rmdir((char *)NameBuff);
|
||||
|
||||
// remove the trailing path separator
|
||||
path_tail(vim_tempdir)[-1] = NUL;
|
||||
delete_recursive(vim_tempdir);
|
||||
xfree(vim_tempdir);
|
||||
vim_tempdir = NULL;
|
||||
}
|
||||
|
@@ -257,7 +257,7 @@ static int included_patches[] = {
|
||||
// 1110,
|
||||
// 1109 NA
|
||||
// 1108,
|
||||
// 1107,
|
||||
1107,
|
||||
// 1106 NA
|
||||
1105,
|
||||
// 1104 NA
|
||||
|
41
test/functional/legacy/delete_spec.lua
Normal file
41
test/functional/legacy/delete_spec.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
local helpers = require('test.functional.helpers')
|
||||
local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert
|
||||
local eq, eval, execute, expect = helpers.eq, helpers.eval, helpers.execute, helpers.expect
|
||||
|
||||
describe('Test for delete()', function()
|
||||
before_each(clear)
|
||||
|
||||
it('file delete', function()
|
||||
execute('split Xfile')
|
||||
execute("call setline(1, ['a', 'b'])")
|
||||
execute('wq')
|
||||
eq(eval("['a', 'b']"), eval("readfile('Xfile')"))
|
||||
eq(0, eval("delete('Xfile')"))
|
||||
eq(-1, eval("delete('Xfile')"))
|
||||
end)
|
||||
|
||||
it('directory delete', function()
|
||||
execute("call mkdir('Xdir1')")
|
||||
eq(1, eval("isdirectory('Xdir1')"))
|
||||
eq(0, eval("delete('Xdir1', 'd')"))
|
||||
eq(0, eval("isdirectory('Xdir1')"))
|
||||
eq(-1, eval("delete('Xdir1', 'd')"))
|
||||
end)
|
||||
it('recursive delete', function()
|
||||
execute("call mkdir('Xdir1')")
|
||||
execute("call mkdir('Xdir1/subdir')")
|
||||
execute('split Xdir1/Xfile')
|
||||
execute("call setline(1, ['a', 'b'])")
|
||||
execute('w')
|
||||
execute('w Xdir1/subdir/Xfile')
|
||||
execute('close')
|
||||
|
||||
eq(1, eval("isdirectory('Xdir1')"))
|
||||
eq(eval("['a', 'b']"), eval("readfile('Xdir1/Xfile')"))
|
||||
eq(1, eval("isdirectory('Xdir1/subdir')"))
|
||||
eq(eval("['a', 'b']"), eval("readfile('Xdir1/subdir/Xfile')"))
|
||||
eq(0, eval("delete('Xdir1', 'rf')"))
|
||||
eq(0, eval("isdirectory('Xdir1')"))
|
||||
eq(-1, eval("delete('Xdir1', 'd')"))
|
||||
end)
|
||||
end)
|
Reference in New Issue
Block a user