0
0
mirror of https://github.com/vim/vim.git synced 2025-09-30 04:44:14 -04:00

patch 9.0.1007: there is no way to get a list of swap file names

Problem:    There is no way to get a list of swap file names.
Solution:   Add the swapfilelist() function.  Use it in the test script to
            clean up.  Remove deleting individual swap files.
This commit is contained in:
Bram Moolenaar
2022-12-05 13:50:55 +00:00
parent 65214053f6
commit c216a7a21a
14 changed files with 104 additions and 71 deletions

View File

@@ -608,6 +608,7 @@ submatch({nr} [, {list}]) String or List
specific match in ":s" or substitute() specific match in ":s" or substitute()
substitute({expr}, {pat}, {sub}, {flags}) substitute({expr}, {pat}, {sub}, {flags})
String all {pat} in {expr} replaced with {sub} String all {pat} in {expr} replaced with {sub}
swapfilelist() List swap files found in 'directory'
swapinfo({fname}) Dict information about swap file {fname} swapinfo({fname}) Dict information about swap file {fname}
swapname({buf}) String swap file of buffer {buf} swapname({buf}) String swap file of buffer {buf}
synID({lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col} synID({lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col}
@@ -9314,6 +9315,17 @@ substitute({string}, {pat}, {sub}, {flags}) *substitute()*
Can also be used as a |method|: > Can also be used as a |method|: >
GetString()->substitute(pat, sub, flags) GetString()->substitute(pat, sub, flags)
swapfilelist() *swapfilelist()*
Returns a list of swap file names, like what "vim -r" shows.
See the |-r| command argument. The 'directory' option is used
for the directories to inspect. If you only want to get a
list of swap files in the current directory then temporarily
set 'directory' to a dot: >
let save_dir = &directory
let &directory = '.'
let swapfiles = swapfilelist()
let &directory = save_dir
swapinfo({fname}) *swapinfo()* swapinfo({fname}) *swapinfo()*
The result is a dictionary, which holds information about the The result is a dictionary, which holds information about the
swapfile {fname}. The available fields are: swapfile {fname}. The available fields are:

View File

@@ -1031,6 +1031,7 @@ Buffers, windows and the argument list:
getwininfo() get a list with window information getwininfo() get a list with window information
getchangelist() get a list of change list entries getchangelist() get a list of change list entries
getjumplist() get a list of jump list entries getjumplist() get a list of jump list entries
swapfilelist() list of existing swap files in 'directory'
swapinfo() information about a swap file swapinfo() information about a swap file
swapname() get the swap file path of a buffer swapname() get the swap file path of a buffer

View File

@@ -168,6 +168,7 @@ static void f_split(typval_T *argvars, typval_T *rettv);
static void f_srand(typval_T *argvars, typval_T *rettv); static void f_srand(typval_T *argvars, typval_T *rettv);
static void f_submatch(typval_T *argvars, typval_T *rettv); static void f_submatch(typval_T *argvars, typval_T *rettv);
static void f_substitute(typval_T *argvars, typval_T *rettv); static void f_substitute(typval_T *argvars, typval_T *rettv);
static void f_swapfilelist(typval_T *argvars, typval_T *rettv);
static void f_swapinfo(typval_T *argvars, typval_T *rettv); static void f_swapinfo(typval_T *argvars, typval_T *rettv);
static void f_swapname(typval_T *argvars, typval_T *rettv); static void f_swapname(typval_T *argvars, typval_T *rettv);
static void f_synID(typval_T *argvars, typval_T *rettv); static void f_synID(typval_T *argvars, typval_T *rettv);
@@ -2579,6 +2580,8 @@ static funcentry_T global_functions[] =
ret_string, f_submatch}, ret_string, f_submatch},
{"substitute", 4, 4, FEARG_1, arg4_string_string_any_string, {"substitute", 4, 4, FEARG_1, arg4_string_string_any_string,
ret_string, f_substitute}, ret_string, f_substitute},
{"swapfilelist", 0, 0, 0, NULL,
ret_list_string, f_swapfilelist},
{"swapinfo", 1, 1, FEARG_1, arg1_string, {"swapinfo", 1, 1, FEARG_1, arg1_string,
ret_dict_any, f_swapinfo}, ret_dict_any, f_swapinfo},
{"swapname", 1, 1, FEARG_1, arg1_buffer, {"swapname", 1, 1, FEARG_1, arg1_buffer,
@@ -10170,6 +10173,17 @@ f_substitute(typval_T *argvars, typval_T *rettv)
rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg); rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
} }
/*
* "swapfilelist()" function
*/
static void
f_swapfilelist(typval_T *argvars UNUSED, typval_T *rettv)
{
if (rettv_list_alloc(rettv) == FAIL)
return;
recover_names(NULL, FALSE, rettv->vval.v_list, 0, NULL);
}
/* /*
* "swapinfo(swap_filename)" function * "swapinfo(swap_filename)" function
*/ */

View File

@@ -507,7 +507,7 @@ vim_main2(void)
*/ */
if (recoverymode && params.fname == NULL) if (recoverymode && params.fname == NULL)
{ {
recover_names(NULL, TRUE, 0, NULL); recover_names(NULL, TRUE, NULL, 0, NULL);
mch_exit(0); mch_exit(0);
} }

View File

@@ -1208,7 +1208,7 @@ ml_recover(int checkext)
directly = FALSE; directly = FALSE;
// count the number of matching swap files // count the number of matching swap files
len = recover_names(fname, FALSE, 0, NULL); len = recover_names(fname, FALSE, NULL, 0, NULL);
if (len == 0) // no swap files found if (len == 0) // no swap files found
{ {
semsg(_(e_no_swap_file_found_for_str), fname); semsg(_(e_no_swap_file_found_for_str), fname);
@@ -1219,7 +1219,7 @@ ml_recover(int checkext)
else // several swap files found, choose else // several swap files found, choose
{ {
// list the names of the swap files // list the names of the swap files
(void)recover_names(fname, TRUE, 0, NULL); (void)recover_names(fname, TRUE, NULL, 0, NULL);
msg_putchar('\n'); msg_putchar('\n');
msg_puts(_("Enter number of swap file to use (0 to quit): ")); msg_puts(_("Enter number of swap file to use (0 to quit): "));
i = get_number(FALSE, NULL); i = get_number(FALSE, NULL);
@@ -1227,7 +1227,7 @@ ml_recover(int checkext)
goto theend; goto theend;
} }
// get the swap file name that will be used // get the swap file name that will be used
(void)recover_names(fname, FALSE, i, &fname_used); (void)recover_names(fname, FALSE, NULL, i, &fname_used);
} }
if (fname_used == NULL) if (fname_used == NULL)
goto theend; // out of memory goto theend; // out of memory
@@ -1801,12 +1801,14 @@ theend:
* - list the swap files for "vim -r" * - list the swap files for "vim -r"
* - count the number of swap files when recovering * - count the number of swap files when recovering
* - list the swap files when recovering * - list the swap files when recovering
* - list the swap files for swapfilelist()
* - find the name of the n'th swap file when recovering * - find the name of the n'th swap file when recovering
*/ */
int int
recover_names( recover_names(
char_u *fname, // base for swap file name char_u *fname, // base for swap file name
int list, // when TRUE, list the swap file names int do_list, // when TRUE, list the swap file names
list_T *ret_list UNUSED, // when not NULL add file names to it
int nr, // when non-zero, return nr'th swap file name int nr, // when non-zero, return nr'th swap file name
char_u **fname_out) // result when "nr" > 0 char_u **fname_out) // result when "nr" > 0
{ {
@@ -1817,7 +1819,6 @@ recover_names(
int num_files; int num_files;
int file_count = 0; int file_count = 0;
char_u **files; char_u **files;
int i;
char_u *dirp; char_u *dirp;
char_u *dir_name; char_u *dir_name;
char_u *fname_res = NULL; char_u *fname_res = NULL;
@@ -1837,7 +1838,7 @@ recover_names(
fname_res = fname; fname_res = fname;
} }
if (list) if (do_list)
{ {
// use msg() to start the scrolling properly // use msg() to start the scrolling properly
msg(_("Swap files found:")); msg(_("Swap files found:"));
@@ -1938,7 +1939,7 @@ recover_names(
} }
// check for out-of-memory // check for out-of-memory
for (i = 0; i < num_names; ++i) for (int i = 0; i < num_names; ++i)
{ {
if (names[i] == NULL) if (names[i] == NULL)
{ {
@@ -1987,12 +1988,14 @@ recover_names(
} }
/* /*
* remove swapfile name of the current buffer, it must be ignored * Remove swapfile name of the current buffer, it must be ignored.
* But keep it for swapfilelist().
*/ */
if (curbuf->b_ml.ml_mfp != NULL if (curbuf->b_ml.ml_mfp != NULL
&& (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL) && (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL
&& ret_list == NULL)
{ {
for (i = 0; i < num_files; ++i) for (int i = 0; i < num_files; ++i)
// Do not expand wildcards, on windows would try to expand // Do not expand wildcards, on windows would try to expand
// "%tmp%" in "%tmp%file". // "%tmp%" in "%tmp%file".
if (fullpathcmp(p, files[i], TRUE, FALSE) & FPC_SAME) if (fullpathcmp(p, files[i], TRUE, FALSE) & FPC_SAME)
@@ -2018,7 +2021,7 @@ recover_names(
dirp = (char_u *)""; // stop searching dirp = (char_u *)""; // stop searching
} }
} }
else if (list) else if (do_list)
{ {
if (dir_name[0] == '.' && dir_name[1] == NUL) if (dir_name[0] == '.' && dir_name[1] == NUL)
{ {
@@ -2036,7 +2039,7 @@ recover_names(
if (num_files) if (num_files)
{ {
for (i = 0; i < num_files; ++i) for (int i = 0; i < num_files; ++i)
{ {
// print the swap file name // print the swap file name
msg_outnum((long)++file_count); msg_outnum((long)++file_count);
@@ -2050,10 +2053,24 @@ recover_names(
msg_puts(_(" -- none --\n")); msg_puts(_(" -- none --\n"));
out_flush(); out_flush();
} }
#ifdef FEAT_EVAL
else if (ret_list != NULL)
{
for (int i = 0; i < num_files; ++i)
{
char_u *name = concat_fnames(dir_name, files[i], TRUE);
if (name != NULL)
{
list_append_string(ret_list, name, -1);
vim_free(name);
}
}
}
#endif
else else
file_count += num_files; file_count += num_files;
for (i = 0; i < num_names; ++i) for (int i = 0; i < num_names; ++i)
vim_free(names[i]); vim_free(names[i]);
if (num_files > 0) if (num_files > 0)
FreeWild(num_files, files); FreeWild(num_files, files);

View File

@@ -10,7 +10,7 @@ void ml_close_all(int del_file);
void ml_close_notmod(void); void ml_close_notmod(void);
void ml_timestamp(buf_T *buf); void ml_timestamp(buf_T *buf);
void ml_recover(int checkext); void ml_recover(int checkext);
int recover_names(char_u *fname, int list, int nr, char_u **fname_out); int recover_names(char_u *fname, int do_list, list_T *ret_list, int nr, char_u **fname_out);
char_u *make_percent_swname(char_u *dir, char_u *name); char_u *make_percent_swname(char_u *dir, char_u *name);
void get_b0_dict(char_u *fname, dict_T *d); void get_b0_dict(char_u *fname, dict_T *d);
void ml_sync_all(int check_file, int check_char); void ml_sync_all(int check_file, int check_char);

View File

@@ -160,6 +160,14 @@ if has('mac')
let $BASH_SILENCE_DEPRECATION_WARNING = 1 let $BASH_SILENCE_DEPRECATION_WARNING = 1
endif endif
" A previous (failed) test run may have left swap files behind. Delete them
" before running tests again, they might interfere.
for name in s:GetSwapFileList()
call delete(name)
endfor
" Prepare for calling test_garbagecollect_now(). " Prepare for calling test_garbagecollect_now().
let v:testing = 1 let v:testing = 1
@@ -186,6 +194,22 @@ if has('reltime')
let g:func_start = reltime() let g:func_start = reltime()
endif endif
" Get the list of swap files in the current directory.
func s:GetSwapFileList()
let save_dir = &directory
let &directory = '.'
let files = swapfilelist()
let &directory = save_dir
" remove a match with runtest.vim
let idx = indexof(files, 'v:val =~ "runtest.vim."')
if idx >= 0
call remove(files, idx)
endif
return files
endfunc
" Invoked when a test takes too much time. " Invoked when a test takes too much time.
func TestTimeout(id) func TestTimeout(id)
split test.log split test.log
@@ -339,6 +363,16 @@ func RunTheTest(test)
endif endif
call add(s:messages, message) call add(s:messages, message)
let s:done += 1 let s:done += 1
" Check if the test has left any swap files behind. Delete them before
" running tests again, they might interfere.
let swapfiles = s:GetSwapFileList()
if len(swapfiles) > 0
call add(s:messages, "Found swap files: " .. string(swapfiles))
for name in swapfiles
call delete(name)
endfor
endif
endfunc endfunc
func AfterTheTest(func_name) func AfterTheTest(func_name)

View File

@@ -28,9 +28,6 @@ func Test_suspend()
sleep 150m sleep 150m
endif endif
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
let buf = term_start('/bin/sh') let buf = term_start('/bin/sh')
" Wait for shell prompt. " Wait for shell prompt.
call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))}) call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
@@ -69,7 +66,6 @@ func Test_suspend()
exe buf . 'bwipe!' exe buf . 'bwipe!'
call delete('Xfoo') call delete('Xfoo')
call delete('.Xfoo.swp')
endfunc endfunc
func Test_suspend_autocmd() func Test_suspend_autocmd()
@@ -82,9 +78,6 @@ func Test_suspend_autocmd()
sleep 150m sleep 150m
endif endif
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
let buf = term_start('/bin/sh', #{term_rows: 6}) let buf = term_start('/bin/sh', #{term_rows: 6})
" Wait for shell prompt. " Wait for shell prompt.
call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))}) call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
@@ -123,7 +116,6 @@ func Test_suspend_autocmd()
exe buf . 'bwipe!' exe buf . 'bwipe!'
call delete('Xfoo') call delete('Xfoo')
call delete('.Xfoo.swp')
endfunc endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab

View File

@@ -110,6 +110,15 @@ func Test_swapinfo()
w w
let fname = s:swapname() let fname = s:swapname()
call assert_match('Xswapinfo', fname) call assert_match('Xswapinfo', fname)
let nr = 0
for name in swapfilelist()
if name =~ '[\\/]' .. fname .. '$'
let nr += 1
endif
endfor
call assert_equal(1, nr)
let info = fname->swapinfo() let info = fname->swapinfo()
let ver = printf('VIM %d.%d', v:version / 100, v:version % 100) let ver = printf('VIM %d.%d', v:version / 100, v:version % 100)

View File

@@ -733,9 +733,6 @@ endfunc
" Tests for guessing the tag location " Tests for guessing the tag location
func Test_tag_guess() func Test_tag_guess()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
\ "func1\tXfoo\t/^int func1(int x)/", \ "func1\tXfoo\t/^int func1(int x)/",
\ "func2\tXfoo\t/^int func2(int y)/", \ "func2\tXfoo\t/^int func2(int y)/",
@@ -772,9 +769,6 @@ endfunc
" Test for an unsorted tags file " Test for an unsorted tags file
func Test_tag_sort() func Test_tag_sort()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
let l = [ let l = [
\ "first\tXfoo\t1", \ "first\tXfoo\t1",
\ "ten\tXfoo\t3", \ "ten\tXfoo\t3",
@@ -802,9 +796,6 @@ endfunc
" Test for an unsorted tags file " Test for an unsorted tags file
func Test_tag_fold() func Test_tag_fold()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
call writefile([ call writefile([
\ "!_TAG_FILE_ENCODING\tutf-8\t//", \ "!_TAG_FILE_ENCODING\tutf-8\t//",
\ "!_TAG_FILE_SORTED\t2\t/0=unsorted, 1=sorted, 2=foldcase/", \ "!_TAG_FILE_SORTED\t2\t/0=unsorted, 1=sorted, 2=foldcase/",
@@ -831,9 +822,6 @@ endfunc
" Test for the :ltag command " Test for the :ltag command
func Test_ltag() func Test_ltag()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
call writefile([ call writefile([
\ "!_TAG_FILE_ENCODING\tutf-8\t//", \ "!_TAG_FILE_ENCODING\tutf-8\t//",
\ "first\tXfoo\t1", \ "first\tXfoo\t1",
@@ -871,9 +859,6 @@ endfunc
" Test for setting the last search pattern to the tag search pattern " Test for setting the last search pattern to the tag search pattern
" when cpoptions has 't' " when cpoptions has 't'
func Test_tag_last_search_pat() func Test_tag_last_search_pat()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
call writefile([ call writefile([
\ "!_TAG_FILE_ENCODING\tutf-8\t//", \ "!_TAG_FILE_ENCODING\tutf-8\t//",
\ "first\tXfoo\t/^int first() {}/", \ "first\tXfoo\t/^int first() {}/",
@@ -902,9 +887,6 @@ endfunc
" Tag stack tests " Tag stack tests
func Test_tag_stack() func Test_tag_stack()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
let l = [] let l = []
for i in range(10, 31) for i in range(10, 31)
let l += ["var" .. i .. "\tXfoo\t/^int var" .. i .. ";$/"] let l += ["var" .. i .. "\tXfoo\t/^int var" .. i .. ";$/"]
@@ -967,9 +949,6 @@ endfunc
" Test for browsing multiple matching tags " Test for browsing multiple matching tags
func Test_tag_multimatch() func Test_tag_multimatch()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
call writefile([ call writefile([
\ "!_TAG_FILE_ENCODING\tutf-8\t//", \ "!_TAG_FILE_ENCODING\tutf-8\t//",
\ "first\tXfoo\t1", \ "first\tXfoo\t1",
@@ -1015,9 +994,6 @@ endfunc
" Test for previewing multiple matching tags " Test for previewing multiple matching tags
func Test_preview_tag_multimatch() func Test_preview_tag_multimatch()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
call writefile([ call writefile([
\ "!_TAG_FILE_ENCODING\tutf-8\t//", \ "!_TAG_FILE_ENCODING\tutf-8\t//",
\ "first\tXfoo\t1", \ "first\tXfoo\t1",
@@ -1066,9 +1042,6 @@ endfunc
" Test for jumping to multiple matching tags across multiple :tags commands " Test for jumping to multiple matching tags across multiple :tags commands
func Test_tnext_multimatch() func Test_tnext_multimatch()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
call writefile([ call writefile([
\ "!_TAG_FILE_ENCODING\tutf-8\t//", \ "!_TAG_FILE_ENCODING\tutf-8\t//",
\ "first\tXfoo1\t1", \ "first\tXfoo1\t1",
@@ -1096,9 +1069,6 @@ endfunc
" Test for jumping to multiple matching tags in non-existing files " Test for jumping to multiple matching tags in non-existing files
func Test_multimatch_non_existing_files() func Test_multimatch_non_existing_files()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
call writefile([ call writefile([
\ "!_TAG_FILE_ENCODING\tutf-8\t//", \ "!_TAG_FILE_ENCODING\tutf-8\t//",
\ "first\tXfoo1\t1", \ "first\tXfoo1\t1",
@@ -1116,9 +1086,6 @@ func Test_multimatch_non_existing_files()
endfunc endfunc
func Test_tselect_listing() func Test_tselect_listing()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
call writefile([ call writefile([
\ "!_TAG_FILE_ENCODING\tutf-8\t//", \ "!_TAG_FILE_ENCODING\tutf-8\t//",
\ "first\tXfoo\t1" .. ';"' .. "\tv\ttyperef:typename:int\tfile:", \ "first\tXfoo\t1" .. ';"' .. "\tv\ttyperef:typename:int\tfile:",
@@ -1500,9 +1467,6 @@ endfunc
" Test for 'tagbsearch' (binary search) " Test for 'tagbsearch' (binary search)
func Test_tagbsearch() func Test_tagbsearch()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
" If a tags file header says the tags are sorted, but the tags are actually " If a tags file header says the tags are sorted, but the tags are actually
" unsorted, then binary search should fail and linear search should work. " unsorted, then binary search should fail and linear search should work.
call writefile([ call writefile([

View File

@@ -638,9 +638,6 @@ endfunc
" Test $buf delcmd {cmd} (command executed when buffer is deleted) " Test $buf delcmd {cmd} (command executed when buffer is deleted)
func Test_buffer_delcmd() func Test_buffer_delcmd()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
new Xfoo new Xfoo
split split
tcl $::vim::current(buffer) delcmd [list set msg "buffer deleted"] tcl $::vim::current(buffer) delcmd [list set msg "buffer deleted"]

View File

@@ -665,9 +665,6 @@ func Test_term_mouse_click_tab()
set mouse=a term=xterm set mouse=a term=xterm
call WaitForResponses() call WaitForResponses()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
let row = 1 let row = 1
for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm

View File

@@ -137,9 +137,6 @@ endfunc
" Test the ":wincmd ^" and "<C-W>^" commands. " Test the ":wincmd ^" and "<C-W>^" commands.
func Test_window_split_edit_alternate() func Test_window_split_edit_alternate()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
" Test for failure when the alternate buffer/file no longer exists. " Test for failure when the alternate buffer/file no longer exists.
edit Xfoo | %bw edit Xfoo | %bw
call assert_fails(':wincmd ^', 'E23:') call assert_fails(':wincmd ^', 'E23:')
@@ -172,9 +169,6 @@ endfunc
" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands. " Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
func Test_window_split_edit_bufnr() func Test_window_split_edit_bufnr()
" in case a previous failure left a swap file behind
call delete('.Xfoo.swp')
%bwipeout %bwipeout
let l:nr = bufnr('%') + 1 let l:nr = bufnr('%') + 1
call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:') call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')

View File

@@ -695,6 +695,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1007,
/**/ /**/
1006, 1006,
/**/ /**/