summaryrefslogtreecommitdiff
path: root/.vim/plugin/toggle_words.vim
blob: 2d79df5769cd7a56152a98e173d67690e9a3f4eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
" toggle_words.vim
" Author: Vincent Wang (linsong dot qizi at gmail dot com)
" Created:  Fri Oct 13 07:51:16 CST 2006
" Requires: Vim Ver7.0+ 
" Version:  1.0
" TODO: 
"
" Documentation: 
"   The purpose of this plugin is very simple, it can toggle words among
"   ['true', 'false'], ['on', 'off'], ['yes', 'no'], ['if', 'elseif', 'else',
"   'endif'] etc . It will search the candicates words to toggle based on
"   current filetype, for example, you can put the following configuration
"   into your .vimrc to define some words for python: 
"      let g:toggle_words_dict = {'python': [['if', 'elif', 'else'], ['True',
"      'False']]}
" 
"   There are some default words for toggling predefined in the
"   script(g:_toogle_words_dict) that will work for all filetypes.
"   Any comment, suggestion, bug report are welcomed. 

if v:version < 700
    "TODO: maybe I should make this script works under vim7.0
    echo "This script required vim7.0 or above version." 
    finish 
endif

if exists("g:load_toggle_words")
   finish
endif

let s:keepcpo= &cpo
set cpo&vim

let g:load_toggle_words = "1.0"

let g:_toggle_words_dict = {'*': [['true', 'false'], ['on', 'off'], ['yes', 'no'], ['+', '-'], ['define', 'undef'], ['if', 'elseif', 'else', 'endif'], ['>', '<'], ['{', '}'], ['(', ')'], ['[', ']'] ], }

if exists('g:toggle_words_dict')
    :call extend(g:_toggle_words_dict, g:toggle_words_dict)
endif

function! s:ToggleWord()
    let cur_filetype = &filetype
    if ! has_key(g:_toggle_words_dict, cur_filetype)
        let words_candicates_array = g:_toggle_words_dict['*']
    else
        let words_candicates_array = g:_toggle_words_dict[cur_filetype] + g:_toggle_words_dict['*']
    endif
    let cur_word = expand("<cword>")
    for words_candicates in words_candicates_array
        let index = index(words_candicates, cur_word)
        if index != -1
            let new_word_index = (index+1)%len(words_candicates)
            let new_word = words_candicates[new_word_index]
            " use the new word to replace the old word
            exec "norm ciw" . new_word . ""
            break
        endif
    endfor
endfunction

command! ToggleWord :call <SID>ToggleWord() <CR>
nmap ,t :call <SID>ToggleWord()<CR>
vmap ,t <ESC>:call <SID>ToggleWord()<CR>

let &cpo= s:keepcpo
unlet s:keepcpo