Skip to content

Instantly share code, notes, and snippets.

@tpope
Created October 16, 2011 05:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpope/1290527 to your computer and use it in GitHub Desktop.
Save tpope/1290527 to your computer and use it in GitHub Desktop.
Increment keywords with speeddating.vim
" Place in ~/.vim/after/plugin/speeddating.vim
" In Vim, -4 % 3 == -1. Let's return 2 instead.
function! s:mod(a,b)
if (a:a < 0 && a:b > 0 || a:a > 0 && a:b < 0) && a:a % a:b != 0
return (a:a % a:b) + a:b
else
return a:a % a:b
endif
endfunction
let s:cycles = [
\ ['true', 'false'],
\ ['TRUE', 'FALSE'],
\ ['True', 'False'],
\ ['on', 'off'],
\ ['ON', 'OFF'],
\ ['On', 'Off'],
\ ['yes', 'no'],
\ ['YES', 'NO'],
\ ['Yes', 'No']]
function! KeywordIncrement(word, offset, increment)
for set in s:cycles
let index = index(set, a:word)
if index >= 0
let index = s:mod(index + a:increment, len(set))
return [set[index], -1]
endif
endfor
endfunction
let s:handler = {'regexp': '\<\%('.join(map(copy(s:cycles),'join(v:val,"\\|")'),'\|').'\)\>', 'increment': function("KeywordIncrement")}
let g:speeddating_handlers += [s:handler]
@jceb
Copy link

jceb commented Oct 16, 2011

Thanks a lot. That's a really nice addon to the speeddating plugin.

@tpope
Copy link
Author

tpope commented Oct 16, 2011

@bronson
Copy link

bronson commented Jun 16, 2014

Love this. Really hoping it gets into speeddating one day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment