aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2015-11-23 23:48:08 -0500
committerTeddy Wing2015-11-23 23:53:37 -0500
commit24f334fa25c736ccfd6bbea337434fba759eded4 (patch)
tree9239d4d2f1921084339152aed450e7d272c14d38
parent29b34c5e0b48428b81240bf6b49c2845885ef32a (diff)
downloadauditory.vim-24f334fa25c736ccfd6bbea337434fba759eded4.tar.bz2
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.
-rw-r--r--autoload/auditory.vim136
1 files 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['<c-r>'] = {
\ }
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 = ':<c-u>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 = ':<c-u>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 . "'<cr>"
- else
- let l:map_to_with_count = l:map_to
- endif
-
- " If this an `execute` mapping, add a pipe.
- " Otherwise <cr> to exit command mode.
- if l:audio !=# ''
- let l:pipe = match(l:map_to_with_count , 'exec') !=# -1 ? ' \| ' : '<cr>'
- 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 . "'<cr>"
+ else
+ let l:map_to_with_count = l:map_to
+ endif
+
+ " If this an `execute` mapping, add a pipe.
+ " Otherwise <cr> to exit command mode.
+ if l:audio !=# ''
+ let l:pipe = match(l:map_to_with_count , 'exec') !=# -1 ? ' \| ' : '<cr>'
+ else
+ let l:pipe = ''
+ endif
+
+ " Default to <silent> unless the mapping explicitly calls for a value
+ let l:silence = '<silent>'
+ 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 <silent> unless the mapping explicitly calls for a value
- let l:silence = '<silent>'
- 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