" ingo/regexp/collection.vim: Functions around handling collections in regular expressions. " " DEPENDENCIES: " " Copyright: (C) 2016-2020 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat let s:save_cpo = &cpo set cpo&vim function! ingo#regexp#collection#Expr( ... ) "****************************************************************************** "* PURPOSE: " Returns a regular expression that matches any collection atom. "* ASSUMPTIONS / PRECONDITIONS: " None. "* EFFECTS / POSTCONDITIONS: " None. "* INPUTS: " The exact pattern can be influenced by the following options: " a:option.isBarePattern Flag whether to return a bare pattern that " does not make any assertions on what's " before the [. This overrides the following " options. Default false. " a:option.isIncludeEolVariant Flag whether to include the /\_[]/ variant as " well. Default true. " a:option.isMagic Flag whether 'magic' is set, and [] is used " instead of \[]. Default true. " a:option.isCapture Flag whether to capture the stuff inside the " collection. Default false. "* RETURN VALUES: " Regular expression. "****************************************************************************** let l:options = (a:0 ? a:1 : {}) let l:isBarePattern = get(l:options, 'isBarePattern', 0) let l:isIncludeEolVariant = get(l:options, 'isIncludeEolVariant', 1) let l:isMagic = get(l:options, 'isMagic', 1) let l:isCapture = get(l:options, 'isCapture', 0) let [l:capturePrefix, l:captureSuffix] = (l:isCapture ? ['\(', '\)'] : ['', '']) let l:prefixExpr = (l:isBarePattern ? \ '' : \ '\%(\%(^\|[^\\]\)\%(\\\\\)*\\%\?\)\@= 256) call add(l:collections, printf('[\u%04x-\u%04x]', l:codePoint, l:codePoint + 255)) let l:codePoint += 256 endwhile if l:codePoint < a:endCodePoint call add(l:collections, printf('[\u%04x-\u%04x]', l:codePoint, a:endCodePoint)) endif return '\%(' . join(l:collections, '\|') . '\)' endfunction let &cpo = s:save_cpo unlet s:save_cpo " vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax :