From 80bcde6d82c196c2e334eb4efbca51ec34afb18c Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Tue, 25 Sep 2018 16:31:30 +0200 Subject: [PATCH] ENH: Add ingo#cmdargs#file#FilterFileOptions() variant of ingo#cmdargs#file#FilterFileOptionsAndCommands() --- autoload/ingo/cmdargs/file.vim | 38 ++++++++++++++++--- .../t1010-FilterFileOptionsAndCommands.vim | 2 +- .../cmdargs/file/t1100-FilterFileOptions.vim | 16 ++++++++ .../cmdargs/file/t1111-FilterFileOptions.vim | 10 +++++ 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 tests/cmdargs/file/t1100-FilterFileOptions.vim create mode 100644 tests/cmdargs/file/t1111-FilterFileOptions.vim diff --git a/autoload/ingo/cmdargs/file.vim b/autoload/ingo/cmdargs/file.vim index 17f09ab..d766ab5 100644 --- a/autoload/ingo/cmdargs/file.vim +++ b/autoload/ingo/cmdargs/file.vim @@ -39,10 +39,36 @@ function! ingo#cmdargs#file#FilterEscapedFileOptionsAndCommands( arguments ) \)[1:2] endfunction +function! ingo#cmdargs#file#FilterFileOptions( fileglobs ) +"******************************************************************************* +"* PURPOSE: +" Strip off the optional ++opt file options that can be given to :write and +" :saveas. +" +"* ASSUMPTIONS / PRECONDITIONS: +" None. +"* EFFECTS / POSTCONDITIONS: +" (Potentially) removes options from a:fileglobs. +"* INPUTS: +" a:fileglobs Raw list of file patterns. To get this from a string, +" use ingo#cmdargs#file#SplitAndUnescape(). +"* RETURN VALUES: +" [a:fileglobs, fileOptions] First element is the passed list, with any file +" options removed. Second element is a List containing all removed file +" options. +" Note: If the file arguments were obtained through +" ingo#cmdargs#file#SplitAndUnescape(), these must be re-escaped for use +" in another Ex command: +" join(map(l:fileOptionsAndCommands, "escape(v:val, '\\ ')")) +"******************************************************************************* + return [a:fileglobs, ingo#list#split#RemoveFromStartWhilePredicate(a:fileglobs, 'v:val =~# ' . string('^' . s:fileOptionsExpr . '$'))] +endfunction + function! ingo#cmdargs#file#FilterFileOptionsAndCommands( fileglobs ) "******************************************************************************* "* PURPOSE: -" Strip off the optional ++opt +cmd file options and command. +" Strip off the optional ++opt +cmd file options and command that can be given +" to :edit, :split, etc. " " (In Vim 7.2,) options and commands can only appear at the beginning of the " file list; there can be multiple options, followed by only one command. They @@ -51,7 +77,7 @@ function! ingo#cmdargs#file#FilterFileOptionsAndCommands( fileglobs ) "* ASSUMPTIONS / PRECONDITIONS: " None. "* EFFECTS / POSTCONDITIONS: -" None. +" (Potentially) removes options and commands from a:fileglobs. "* INPUTS: " a:fileglobs Raw list of file patterns. To get this from a string, " use ingo#cmdargs#file#SplitAndUnescape(). Or alternatively @@ -65,13 +91,13 @@ function! ingo#cmdargs#file#FilterFileOptionsAndCommands( fileglobs ) " in another Ex command: " join(map(l:fileOptionsAndCommands, "escape(v:val, '\\ ')")) "******************************************************************************* - let l:fileOptionsAndCommands = ingo#list#split#RemoveFromStartWhilePredicate(a:fileglobs, 'v:val =~# ' . string('^' . s:fileOptionsExpr . '$')) + let [l:fileglobs, l:fileOptionsAndCommands] = ingo#cmdargs#file#FilterFileOptions(a:fileglobs) - if get(a:fileglobs, 0, '') =~# '^++\@!' - call add(l:fileOptionsAndCommands, remove(a:fileglobs, 0)) + if get(l:fileglobs, 0, '') =~# '^++\@!' + call add(l:fileOptionsAndCommands, remove(l:fileglobs, 0)) endif - return [a:fileglobs, l:fileOptionsAndCommands] + return [l:fileglobs, l:fileOptionsAndCommands] endfunction function! ingo#cmdargs#file#Unescape( fileArgument ) diff --git a/tests/cmdargs/file/t1010-FilterFileOptionsAndCommands.vim b/tests/cmdargs/file/t1010-FilterFileOptionsAndCommands.vim index 71e58b5..72304c4 100644 --- a/tests/cmdargs/file/t1010-FilterFileOptionsAndCommands.vim +++ b/tests/cmdargs/file/t1010-FilterFileOptionsAndCommands.vim @@ -1,4 +1,4 @@ -" Test modification of the passed-in fileglobs List. +" Test modification of the passed-in fileglobs List when stripping options and commands. call vimtest#StartTap() call vimtap#Plan(2) diff --git a/tests/cmdargs/file/t1100-FilterFileOptions.vim b/tests/cmdargs/file/t1100-FilterFileOptions.vim new file mode 100644 index 0000000..3d49742 --- /dev/null +++ b/tests/cmdargs/file/t1100-FilterFileOptions.vim @@ -0,0 +1,16 @@ +" Test stripping file options. + +call vimtest#StartTap() +call vimtap#Plan(8) + +call vimtap#Is(ingo#cmdargs#file#FilterFileOptions([]), [[], []], 'empty fileglobs') +call vimtap#Is(ingo#cmdargs#file#FilterFileOptions(['/tmp/foo']), [['/tmp/foo'], []], 'just a file') +call vimtap#Is(ingo#cmdargs#file#FilterFileOptions(['foo*', 'bar']), [['foo*', 'bar'], []], 'just two files') +call vimtap#Is(ingo#cmdargs#file#FilterFileOptions(['+setl et', 'foo*', 'bar']), [['+setl et', 'foo*', 'bar'], []], 'command is treated as file') +call vimtap#Is(ingo#cmdargs#file#FilterFileOptions(['foo*', '++ff=unix']), [['foo*', '++ff=unix'], []], 'option after fileglob is treated as fileglob') +call vimtap#Is(ingo#cmdargs#file#FilterFileOptions(['++ff=unix', 'foo*', 'bar']), [['foo*', 'bar'], ['++ff=unix']], 'option and two files') +call vimtap#Is(ingo#cmdargs#file#FilterFileOptions(['++ff=unix', '++enc=utf-8', 'foo*', 'bar']), [['foo*', 'bar'], ['++ff=unix', '++enc=utf-8']], 'two options and two files') + +call vimtap#Is(ingo#cmdargs#file#FilterFileOptions(['++whatis=that', 'foo*', 'bar']), [['++whatis=that', 'foo*', 'bar'], []], 'invalid option is treated as fileglob') + +call vimtest#Quit() diff --git a/tests/cmdargs/file/t1111-FilterFileOptions.vim b/tests/cmdargs/file/t1111-FilterFileOptions.vim new file mode 100644 index 0000000..8796193 --- /dev/null +++ b/tests/cmdargs/file/t1111-FilterFileOptions.vim @@ -0,0 +1,10 @@ +" Test modification of the passed-in fileglobs List when stripping options. + +call vimtest#StartTap() +call vimtap#Plan(2) + +let g:fileglobs = ['++ff=unix', '++enc=utf-8', 'foo*', 'bar'] +call vimtap#Is(ingo#cmdargs#file#FilterFileOptions(g:fileglobs), [['foo*', 'bar'], ['++ff=unix', '++enc=utf-8']], 'two options and two files') +call vimtap#Is(g:fileglobs, ['foo*', 'bar'], 'original argument list has been reduced') + +call vimtest#Quit()