From 24f334fa25c736ccfd6bbea337434fba759eded4 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 23 Nov 2015 23:48:08 -0500 Subject: autoload/auditory.vim: Prevent double `AuditoryOn` & `AuditoryOff` Previously executing `:AuditoryOn` and `:AuditoryOff` would always do the thing they're supposed to do regardless of whether Auditory was already on or off. This caused errors because things got messed up when trying to double assign the mappings and double unmap the mappings. Now check for the status of the `g:auditory_on` flag before turning Auditory on or off. --- autoload/auditory.vim | 136 ++++++++++++++++++++++++++------------------------ 1 file changed, 70 insertions(+), 66 deletions(-) diff --git a/autoload/auditory.vim b/autoload/auditory.vim index bae93e5..56dc06d 100644 --- a/autoload/auditory.vim +++ b/autoload/auditory.vim @@ -421,63 +421,65 @@ let g:auditory_mappings[''] = { \ } function! auditory#AssignMappings() - for [key, value] in items(g:auditory_mappings) - call auditory#StoreUserMapping(key) - - " Default to nnoremap - let l:cmd = has_key(value, 'map_command') ? value.map_command : 'nnoremap' - - " If `map_from` is specified, we can't rely on `key` to provide it - let l:map_from = has_key(value, 'map_from') ? value.map_from : key - - if has_key(value, 'audio') - let l:audio = ':call auditory#Play("' . value.audio . '")' - else - let l:audio = '' - endif - - " Map to key unless a `map_to` or user mapping is defined - if has_key(value, 'map_to') - let l:map_to = value.map_to - else - if has_key(value, 'user_mapping') - let l:map_to = value.user_mapping + if !g:auditory_on + for [key, value] in items(g:auditory_mappings) + call auditory#StoreUserMapping(key) + + " Default to nnoremap + let l:cmd = has_key(value, 'map_command') ? value.map_command : 'nnoremap' + + " If `map_from` is specified, we can't rely on `key` to provide it + let l:map_from = has_key(value, 'map_from') ? value.map_from : key + + if has_key(value, 'audio') + let l:audio = ':call auditory#Play("' . value.audio . '")' else - let l:map_to = key + let l:audio = '' endif - endif - - if has_key(value, 'count') - let vcount = value.count ==# 1 ? 'v:count1' : 'v:count' - let l:map_to_with_count = "execute 'normal!' " . vcount . " . '" . l:map_to . "'" - else - let l:map_to_with_count = l:map_to - endif - - " If this an `execute` mapping, add a pipe. - " Otherwise to exit command mode. - if l:audio !=# '' - let l:pipe = match(l:map_to_with_count , 'exec') !=# -1 ? ' \| ' : '' - else - let l:pipe = '' - endif + + " Map to key unless a `map_to` or user mapping is defined + if has_key(value, 'map_to') + let l:map_to = value.map_to + else + if has_key(value, 'user_mapping') + let l:map_to = value.user_mapping + else + let l:map_to = key + endif + endif + + if has_key(value, 'count') + let vcount = value.count ==# 1 ? 'v:count1' : 'v:count' + let l:map_to_with_count = "execute 'normal!' " . vcount . " . '" . l:map_to . "'" + else + let l:map_to_with_count = l:map_to + endif + + " If this an `execute` mapping, add a pipe. + " Otherwise to exit command mode. + if l:audio !=# '' + let l:pipe = match(l:map_to_with_count , 'exec') !=# -1 ? ' \| ' : '' + else + let l:pipe = '' + endif + + " Default to unless the mapping explicitly calls for a value + let l:silence = '' + if has_key(value, 'silent') + let l:silence = value.silent ? l:silence : '' + endif + + execute l:cmd . ' ' . l:silence . ' ' . l:map_from . + \ ' ' . + \ l:audio . + \ l:pipe . + \ l:map_to_with_count + endfor - " Default to unless the mapping explicitly calls for a value - let l:silence = '' - if has_key(value, 'silent') - let l:silence = value.silent ? l:silence : '' - endif + call auditory#AssignInsertMappings() - execute l:cmd . ' ' . l:silence . ' ' . l:map_from . - \ ' ' . - \ l:audio . - \ l:pipe . - \ l:map_to_with_count - endfor - - call auditory#AssignInsertMappings() - - let g:auditory_on = 1 + let g:auditory_on = 1 + endif endfunction @@ -500,21 +502,23 @@ endfunction function! auditory#Unmap() - for [key, value] in items(g:auditory_mappings) - let l:cmd = has_key(value, 'map_command') ? value.map_command : 'nnoremap' - let l:key = has_key(value, 'map_from') ? value.map_from : key - let l:user_mapping = get(value, 'user_mapping', '') + if g:auditory_on + for [key, value] in items(g:auditory_mappings) + let l:cmd = has_key(value, 'map_command') ? value.map_command : 'nnoremap' + let l:key = has_key(value, 'map_from') ? value.map_from : key + let l:user_mapping = get(value, 'user_mapping', '') + + execute l:cmd[0] . 'unmap ' . l:key + + if l:user_mapping !=# '' + execute l:cmd . ' ' . get(value, 'map_from', key) . ' ' . value.user_mapping + endif + endfor - execute l:cmd[0] . 'unmap ' . l:key + call auditory#UnmapInsert() - if l:user_mapping !=# '' - execute l:cmd . ' ' . get(value, 'map_from', key) . ' ' . value.user_mapping - endif - endfor - - call auditory#UnmapInsert() - - let g:auditory_on = 0 + let g:auditory_on = 0 + endif endfunction -- cgit v1.2.3