" ingo/cmdargs/glob.vim: Functions for expanding file glob arguments. " " DEPENDENCIES: " - ingo/cmdargs/file.vim autoload script " - ingo/compat.vim autoload script " - ingo/os.vim autoload script " " Copyright: (C) 2012-2016 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat " " REVISION DATE REMARKS " 1.025.004 08-Jul-2016 ENH: Add second optional flag " a:isKeepDirectories to " ingo#cmdargs#glob#Expand() / " ingo#cmdargs#glob#ExpandSingle(). " 1.022.003 22-Sep-2014 Use ingo#compat#glob(). " 1.013.002 13-Sep-2013 Use operating system detection functions from " ingo/os.vim. " 1.007.001 01-Jun-2013 file creation from ingofileargs.vim function! ingo#cmdargs#glob#ExpandSingle( fileglob, ... ) "****************************************************************************** "* PURPOSE: " Expand any file wildcards in a:fileglob to a list of normal filespecs. "* ASSUMPTIONS / PRECONDITIONS: " None. "* EFFECTS / POSTCONDITIONS: " None. "* INPUTS: " a:fileglob File glob (already processed by " ingo#cmdargs#file#Unescape()). " a:isKeepNoMatch Optional flag that lets globs that have no matches be kept " and returned as-is, instead of being removed. Set this when " you want to support creating new files. " a:isKeepDirectories Optional flag that keeps directories in the list. "* RETURN VALUES: " List of normal filespecs; globs have been expanded. To consume this in " another Vim command, use: " join(map(l:filespecs, 'fnameescape(v:val))) "****************************************************************************** " XXX: Special Vim variables are expanded by -complete=file, but (in Vim " 7.3), escaped special names are _not_ correctly re-escaped, and a " following glob() or expand() will mistakenly expand them. Because of the " auto-expansion, any unescaped special Vim variable that gets here is in " fact a literal special filename. We don't even need to re-escape and " glob() it, just return it verbatim. if a:fileglob =~# '^\%(%\|#\d\?\)\%(:\a\)*$\|^<\%(cfile\|cword\|cWORD\)>\%(:\a\)*$' return [a:fileglob] else " Filter out directories; we're usually only interested in files. let l:specs = (a:0 && a:1 ? split(expand(a:fileglob), '\n') : ingo#compat#glob(a:fileglob, 0, 1)) return (a:0 >= 2 && a:2 ? l:specs : filter(l:specs, '! isdirectory(v:val)')) endif endfunction function! ingo#cmdargs#glob#Expand( fileglobs, ... ) "****************************************************************************** "* PURPOSE: " Expand any file wildcards in a:fileglobs to a list of normal filespecs. "* ASSUMPTIONS / PRECONDITIONS: " None. "* EFFECTS / POSTCONDITIONS: " None. "* INPUTS: " a:fileglobs Either space-separated arguments string (from a :command " -complete=file ... custom command), or a list of " fileglobs (already processed by " ingo#cmdargs#file#Unescape()). " a:isKeepNoMatch Optional flag that lets globs that have no matches be kept " and returned as-is, instead of being removed. Set this when " you want to support creating new files. " a:isKeepDirectories Optional flag that keeps directories in the list. "* RETURN VALUES: " List of filespecs; globs have been expanded. To consume this in another Vim " command, use: " join(map(l:filespecs, 'fnameescape(v:val))) "****************************************************************************** let l:fileglobs = (type(a:fileglobs) == type([]) ? a:fileglobs : ingo#cmdargs#file#SplitAndUnescape(a:fileglobs)) let l:filespecs = [] for l:fileglob in l:fileglobs call extend(l:filespecs, call('ingo#cmdargs#glob#ExpandSingle', [l:fileglob] + a:000)) endfor return l:filespecs endfunction function! s:ContainsNoWildcards( fileglob ) " Note: This is only an empirical approximation; it is not perfect. if ingo#os#IsWinOrDos() return a:fileglob !~ '[*?]' else return a:fileglob !~ '\\\@