Clean s:get() and s:set() up. Force s:set() to take a default value. Closes #287

This commit is contained in:
Israel Chauca F
2020-07-29 11:50:21 -05:00
parent 728b57a656
commit 16687aec24
2 changed files with 37 additions and 49 deletions

View File

@@ -12,32 +12,20 @@ if !exists('s:options')
let s:options = {} let s:options = {}
endif endif
function! s:set(name, value, ...) "{{{ function! s:set(name, value) "{{{
let scope = a:0 ? a:1 : 's'
let bufnr = bufnr('%') let bufnr = bufnr('%')
if !exists('s:options[bufnr]') if !has_key(s:options, bufnr)
let s:options[bufnr] = {} let s:options[bufnr] = {}
endif endif
if scope == 's' let s:options[bufnr][a:name] = a:value
let name = 's:options.' . bufnr . '.' . a:name
else
let name = scope . ':delimitMate_' . a:name
if exists('name')
exec 'unlet! ' . name
endif
endif
exec 'let ' . name . ' = a:value'
endfunction "}}} endfunction "}}}
function! s:get(name, ...) "{{{ function! s:get(...) "{{{
if a:0 == 2 let options = deepcopy(eval('s:options.' . bufnr('%')))
return deepcopy(get(a:2, 'delimitMate_' . a:name, a:1)) if a:0
elseif a:0 == 1 return options[a:1]
let bufoptions = get(s:options, bufnr('%'), {})
return deepcopy(get(bufoptions, a:name, a:1))
else
return deepcopy(eval('s:options.' . bufnr('%') . '.' . a:name))
endif endif
return options
endfunction "}}} endfunction "}}}
function! s:exists(name, ...) "{{{ function! s:exists(name, ...) "{{{

View File

@@ -29,15 +29,14 @@ let delimitMate_version = "2.8"
" Functions: {{{ " Functions: {{{
function! s:option_init(name, default) "{{{ function! s:option_init(name, default) "{{{
let b = exists("b:delimitMate_" . a:name) let opt_name = "delimitMate_" . a:name
let g = exists("g:delimitMate_" . a:name)
" Find value to use. " Find value to use.
if !b && !g if !has_key(b:, opt_name) && !has_key(g:, opt_name)
let value = a:default let value = a:default
elseif b elseif has_key(b:, opt_name)
exec "let value = b:delimitMate_" . a:name let value = b:[opt_name]
else else
exec "let value = g:delimitMate_" . a:name let value = g:[opt_name]
endif endif
call s:set(a:name, value) call s:set(a:name, value)
endfunction "}}} endfunction "}}}
@@ -48,8 +47,8 @@ function! s:init() "{{{
call s:option_init("autoclose", 1) call s:option_init("autoclose", 1)
" matchpairs " matchpairs
call s:option_init("matchpairs", string(&matchpairs)[1:-2]) call s:option_init("matchpairs", string(&matchpairs)[1:-2])
call s:option_init("matchpairs_list", map(split(s:get('matchpairs'), '.:.\zs,\ze.:.'), 'split(v:val, ''^.\zs:\ze.$'')')) call s:option_init("matchpairs_list", map(split(s:get('matchpairs', ''), '.:.\zs,\ze.:.'), 'split(v:val, ''^.\zs:\ze.$'')'))
let pairs = s:get('matchpairs_list') let pairs = s:get('matchpairs_list', [])
if len(filter(pairs, 'v:val[0] ==# v:val[1]')) if len(filter(pairs, 'v:val[0] ==# v:val[1]'))
echohl ErrorMsg echohl ErrorMsg
echom 'delimitMate: each member of a pair in delimitMate_matchpairs must be different from each other.' echom 'delimitMate: each member of a pair in delimitMate_matchpairs must be different from each other.'
@@ -57,17 +56,17 @@ function! s:init() "{{{
echohl Normal echohl Normal
return 0 return 0
endif endif
call s:option_init("left_delims", map(copy(s:get('matchpairs_list')), 'v:val[0]')) call s:option_init("left_delims", map(copy(s:get('matchpairs_list', [])), 'v:val[0]'))
call s:option_init("right_delims", map(copy(s:get('matchpairs_list')), 'v:val[1]')) call s:option_init("right_delims", map(copy(s:get('matchpairs_list', [])), 'v:val[1]'))
" quotes " quotes
call s:option_init("quotes", "\" ' `") call s:option_init("quotes", "\" ' `")
call s:option_init("quotes_list",split(s:get('quotes'), '\s\+')) call s:option_init("quotes_list",split(s:get('quotes', ''), '\s\+'))
" nesting_quotes " nesting_quotes
call s:option_init("nesting_quotes", []) call s:option_init("nesting_quotes", [])
" excluded_regions " excluded_regions
call s:option_init("excluded_regions", "Comment") call s:option_init("excluded_regions", "Comment")
call s:option_init("excluded_regions_list", split(s:get('excluded_regions'), ',\s*')) call s:option_init("excluded_regions_list", split(s:get('excluded_regions', ''), ',\s*'))
let enabled = len(s:get('excluded_regions_list')) > 0 let enabled = len(s:get('excluded_regions_list', [])) > 0
call s:option_init("excluded_regions_enabled", enabled) call s:option_init("excluded_regions_enabled", enabled)
" expand_space " expand_space
if exists("b:delimitMate_expand_space") && type(b:delimitMate_expand_space) == type("") if exists("b:delimitMate_expand_space") && type(b:delimitMate_expand_space) == type("")
@@ -110,7 +109,7 @@ function! s:init() "{{{
call s:option_init("smart_matchpairs", '^\%(\w\|\!\|[£$]\|[^[:punct:][:space:]]\)') call s:option_init("smart_matchpairs", '^\%(\w\|\!\|[£$]\|[^[:punct:][:space:]]\)')
" smart_quotes " smart_quotes
" XXX: backward compatibility. Ugly, should go the way of the dodo soon. " XXX: backward compatibility. Ugly, should go the way of the dodo soon.
let quotes = escape(join(s:get('quotes_list'), ''), '\-^[]') let quotes = escape(join(s:get('quotes_list', []), ''), '\-^[]')
let default_smart_quotes = '\%(\w\|[^[:punct:][:space:]' . quotes . ']\|\%(\\\\\)*\\\)\%#\|\%#\%(\w\|[^[:space:][:punct:]' . quotes . ']\)' let default_smart_quotes = '\%(\w\|[^[:punct:][:space:]' . quotes . ']\|\%(\\\\\)*\\\)\%#\|\%#\%(\w\|[^[:space:][:punct:]' . quotes . ']\)'
if exists('g:delimitMate_smart_quotes') && type(g:delimitMate_smart_quotes) == type(0) if exists('g:delimitMate_smart_quotes') && type(g:delimitMate_smart_quotes) == type(0)
if g:delimitMate_smart_quotes if g:delimitMate_smart_quotes
@@ -134,7 +133,7 @@ function! s:init() "{{{
call s:option_init("smart_quotes", default_smart_quotes) call s:option_init("smart_quotes", default_smart_quotes)
" apostrophes " apostrophes
call s:option_init("apostrophes", "") call s:option_init("apostrophes", "")
call s:option_init("apostrophes_list", split(s:get('apostrophes'), ":\s*")) call s:option_init("apostrophes_list", split(s:get('apostrophes', ''), ":\s*"))
" tab2exit " tab2exit
call s:option_init("tab2exit", 1) call s:option_init("tab2exit", 1)
" balance_matchpairs " balance_matchpairs
@@ -146,9 +145,10 @@ function! s:init() "{{{
return 1 return 1
endfunction "}}} Init() endfunction "}}} Init()
function! s:get(...) " {{{ function! s:get(name, default) "{{{
return call('delimitMate#Get', a:000) let bufoptions = delimitMate#Get()
endfunction " }}} return get(bufoptions, a:name, a:default)
endfunction "}}}
function! s:set(...) " {{{ function! s:set(...) " {{{
return call('delimitMate#Set', a:000) return call('delimitMate#Set', a:000)
@@ -164,7 +164,7 @@ function! s:Map() "{{{
set keymap= set keymap=
set cpo&vim set cpo&vim
silent! doautocmd <nomodeline> User delimitMate_map silent! doautocmd <nomodeline> User delimitMate_map
if s:get('autoclose') if s:get('autoclose', 1)
call s:AutoClose() call s:AutoClose()
else else
call s:NoAutoClose() call s:NoAutoClose()
@@ -270,7 +270,7 @@ endfunction "}}}
function! s:NoAutoClose() "{{{ function! s:NoAutoClose() "{{{
" inoremap <buffer> ) <C-R>=delimitMate#SkipDelim('\)')<CR> " inoremap <buffer> ) <C-R>=delimitMate#SkipDelim('\)')<CR>
for delim in s:get('right_delims') + s:get('quotes_list') for delim in s:get('right_delims', []) + s:get('quotes_list', [])
if delim == '|' if delim == '|'
let delim = '<Bar>' let delim = '<Bar>'
endif endif
@@ -283,9 +283,9 @@ function! s:AutoClose() "{{{
" Add matching pair and jump to the midle: " Add matching pair and jump to the midle:
" inoremap <silent> <buffer> ( ()<Left> " inoremap <silent> <buffer> ( ()<Left>
let i = 0 let i = 0
while i < len(s:get('matchpairs_list')) while i < len(s:get('matchpairs_list', []))
let ld = s:get('left_delims')[i] == '|' ? '<bar>' : s:get('left_delims')[i] let ld = s:get('left_delims', [])[i] == '|' ? '<bar>' : s:get('left_delims', [])[i]
let rd = s:get('right_delims')[i] == '|' ? '<bar>' : s:get('right_delims')[i] let rd = s:get('right_delims', [])[i] == '|' ? '<bar>' : s:get('right_delims', [])[i]
exec 'inoremap <expr><silent> <Plug>delimitMate' . ld exec 'inoremap <expr><silent> <Plug>delimitMate' . ld
\. ' <SID>TriggerAbb().delimitMate#ParenDelim("' . escape(rd, '|') . '")' \. ' <SID>TriggerAbb().delimitMate#ParenDelim("' . escape(rd, '|') . '")'
exec 'silent! imap <unique> <buffer> '.ld exec 'silent! imap <unique> <buffer> '.ld
@@ -294,7 +294,7 @@ function! s:AutoClose() "{{{
endwhile endwhile
" Exit from inside the matching pair: " Exit from inside the matching pair:
for delim in s:get('right_delims') for delim in s:get('right_delims', [])
let delim = delim == '|' ? '<bar>' : delim let delim = delim == '|' ? '<bar>' : delim
exec 'inoremap <expr><silent> <Plug>delimitMate' . delim exec 'inoremap <expr><silent> <Plug>delimitMate' . delim
\. ' <SID>TriggerAbb().delimitMate#JumpOut("\' . delim . '")' \. ' <SID>TriggerAbb().delimitMate#JumpOut("\' . delim . '")'
@@ -304,7 +304,7 @@ function! s:AutoClose() "{{{
" Add matching quote and jump to the midle, or exit if inside a pair of matching quotes: " Add matching quote and jump to the midle, or exit if inside a pair of matching quotes:
" inoremap <silent> <buffer> " <C-R>=delimitMate#QuoteDelim("\"")<CR> " inoremap <silent> <buffer> " <C-R>=delimitMate#QuoteDelim("\"")<CR>
for delim in s:get('quotes_list') for delim in s:get('quotes_list', [])
if delim == '|' if delim == '|'
let delim = '<Bar>' let delim = '<Bar>'
endif endif
@@ -316,7 +316,7 @@ function! s:AutoClose() "{{{
" Try to fix the use of apostrophes (kept for backward compatibility): " Try to fix the use of apostrophes (kept for backward compatibility):
" inoremap <silent> <buffer> n't n't " inoremap <silent> <buffer> n't n't
for map in s:get('apostrophes_list') for map in s:get('apostrophes_list', [])
exec "inoremap <silent> " . map . " " . map exec "inoremap <silent> " . map . " " . map
exec 'silent! imap <unique> <buffer> ' . map . ' <Plug>delimitMate' . map exec 'silent! imap <unique> <buffer> ' . map . ' <Plug>delimitMate' . map
endfor endfor
@@ -340,17 +340,17 @@ function! s:ExtraMappings() "{{{
endif endif
" Expand return if inside an empty pair: " Expand return if inside an empty pair:
inoremap <expr><silent> <Plug>delimitMateCR <SID>TriggerAbb()."\<C-R>=delimitMate#ExpandReturn()\<CR>" inoremap <expr><silent> <Plug>delimitMateCR <SID>TriggerAbb()."\<C-R>=delimitMate#ExpandReturn()\<CR>"
if s:get('expand_cr') && !hasmapto('<Plug>delimitMateCR', 'i') && maparg('<CR>', 'i') == '' if s:get('expand_cr', 0) && !hasmapto('<Plug>delimitMateCR', 'i') && maparg('<CR>', 'i') == ''
silent! imap <unique> <buffer> <CR> <Plug>delimitMateCR silent! imap <unique> <buffer> <CR> <Plug>delimitMateCR
endif endif
" Expand space if inside an empty pair: " Expand space if inside an empty pair:
inoremap <expr><silent> <Plug>delimitMateSpace <SID>TriggerAbb()."\<C-R>=delimitMate#ExpandSpace()\<CR>" inoremap <expr><silent> <Plug>delimitMateSpace <SID>TriggerAbb()."\<C-R>=delimitMate#ExpandSpace()\<CR>"
if s:get('expand_space') && !hasmapto('<Plug>delimitMateSpace', 'i') && maparg('<Space>', 'i') == '' if s:get('expand_space', 0) && !hasmapto('<Plug>delimitMateSpace', 'i') && maparg('<Space>', 'i') == ''
silent! imap <unique> <buffer> <Space> <Plug>delimitMateSpace silent! imap <unique> <buffer> <Space> <Plug>delimitMateSpace
endif endif
" Jump over any delimiter: " Jump over any delimiter:
inoremap <expr><silent> <Plug>delimitMateS-Tab <SID>TriggerAbb()."\<C-R>=delimitMate#JumpAny()\<CR>" inoremap <expr><silent> <Plug>delimitMateS-Tab <SID>TriggerAbb()."\<C-R>=delimitMate#JumpAny()\<CR>"
if s:get('tab2exit') && !hasmapto('<Plug>delimitMateS-Tab', 'i') && maparg('<S-Tab>', 'i') == '' if s:get('tab2exit', 0) && !hasmapto('<Plug>delimitMateS-Tab', 'i') && maparg('<S-Tab>', 'i') == ''
silent! imap <unique> <buffer> <S-Tab> <Plug>delimitMateS-Tab silent! imap <unique> <buffer> <S-Tab> <Plug>delimitMateS-Tab
endif endif
" Jump over next delimiters " Jump over next delimiters