Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
Created April 12, 2018 20:31
Show Gist options
  • Save PeterRincker/89c51610e0d45358f360679d0a2af5a4 to your computer and use it in GitHub Desktop.
Save PeterRincker/89c51610e0d45358f360679d0a2af5a4 to your computer and use it in GitHub Desktop.
Voggle - toggling pairs
" Interpratation of Voggle:
" https://github.com/JasperSmienk/Voggle
"
" Example mapping:
" nmap <cr> <Plug>(voggle)
"
" Customize by creating g:voggle_pairs or b:voggle_pairs Dictionaries
let s:voggle_pairs = {
\ 'true': 'false',
\ 'false': 'true',
\ 'True': 'False',
\ 'False': 'True',
\ 'yes': 'no',
\ 'no': 'yes',
\ 'Yes': 'No',
\ 'No': 'Yes',
\ 'yep': 'nope',
\ 'nope': 'yep',
\ 'Yep': 'Nope',
\ 'Nope': 'Yep',
\ 'on': 'off',
\ 'off': 'on',
\ 'On': 'Off',
\ 'Off': 'On',
\ ">": "<",
\ "<": ">",
\ "==": "!=",
\ "!=": "==",
\ "&&": "||",
\ "||": "&&",
\ ".": "->",
\ "->": ".",
\ "1": "0",
\ "0": "1",
\ "+": "-",
\ "-": "+"
\ }
nnoremap <script> <silent> <Plug>(voggle) :<c-u>call <SID>voggle()<cr>
function! s:voggle()
let words = get(b:, 'voggle_pairs', get(g:, 'voggle_pairs', s:toggle_pairs))
if len(keys(words)) == 0
return
endif
let view = winsaveview()
normal! "_yiw
let keys = sort(copy(keys(words)), {a, b -> len(a) == len(b) ? 0 : (len(a) < len(b) ? 1 : -1)})
let pat = '\V\C\(' . join(map(keys, 'escape(v:val, "\\")'), '\|') . '\)'
call search(pat, 'c', line('.'))
let line = strpart(getline('.'), col('.') - 1)
let current = matchstr(line, pat)
if !has_key(words, current)
call winrestview(view)
return
endif
execute "normal! " . strlen(current) . "s\<c-r>=" . string(words[current]) . "\<cr>\<esc>`["
silent! call repeat#set("\<Plug>(voggle)")
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment