" ingo/regexp/fromwildcard.vim: Functions for converting a shell-like wildcard to a regular expression. " " DEPENDENCIES: " " Copyright: (C) 2013-2022 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat " " REVISION DATE REMARKS " 1.023.003 30-Jan-2015 Add " ingo#regexp#fromwildcard#AnchoredToPathBoundaries(). " 1.021.002 23-Jun-2014 ENH: Allow to pass path separator to " ingo#regexp#fromwildcard#Convert() and " ingo#regexp#fromwildcard#IsWildcardPathPattern(). " 1.014.001 26-Oct-2013 file creation from " autoload/EditSimilar/Substitute.vim if exists('+shellslash') && ! &shellslash let s:pathSeparator = '\' let s:notPathSeparatorPattern = '\\[^/\\\\]' else let s:pathSeparator = '/' let s:notPathSeparatorPattern = '\\[^/]' endif function! s:AdaptCollection() " Special processing for the submatch inside the [...] collection. " Earlier, simpler regexp that didn't handle \] inside [...]: "let l:expr = substitute(l:expr, '\[\(\%(\^\?\]\)\?.\{-}\)\]', '\\%(\\%(\\[\1]\\\&' . s:notPathSeparatorPattern . '\\)\\|[\1]\\)', 'g') " Handle \] inside by including \] in the inner pattern, then undoing the " backslash escaping done first in this function (i.e. recreate \] from the " initial \\]). " Vim doesn't seem to support other escaped characters like [\x6f\d122] in a " file pattern. let l:result = substitute(submatch(1), '\\\\]', '\\]', 'g') " Escape ? and *; the later wildcard expansions will trample over them. let l:result = substitute(l:result, '[?*]', '\\\\\0', 'g') return l:result endfunction function! s:CanonicalizeWildcard( expr, pathSeparator ) let l:expr = escape(a:expr, '\') if a:pathSeparator ==# '\' " On Windows, when the 'shellslash' option isn't set (i.e. backslashes " are used as path separators), still allow using forward slashes as " path separators, like Vim does. let l:expr = substitute(l:expr, '/', '\\\\', 'g') endif return l:expr endfunction function! s:Convert( wildcardExpr, ... ) let l:pathSeparator = (a:0 > 1 ? a:2 : s:pathSeparator) let l:expr = s:CanonicalizeWildcard(a:wildcardExpr, l:pathSeparator) " [...] wildcards let l:expr = substitute(l:expr, '\[\(\%(\^\?\]\)\?\(\\\\\]\|[^]]\)*\)\]', '\="\\%(\\%(\\[". s:AdaptCollection() . "]\\\&' . s:notPathSeparatorPattern . '\\)\\|[". s:AdaptCollection() . "]\\)"', 'g') " ? wildcards let l:expr = substitute(l:expr, '\\\@