aboutsummaryrefslogtreecommitdiff
path: root/plugin/supert.vim
blob: bd8f3129a8284ebef876923c834ad766308ee959 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
" Vim plugin to provide multiline analogs to t & f

if !exists('g:supert_provide_bindings')
  let g:supert_provide_bindings = 1
endif

let s:last = []

function! s:do_search(type, vis) abort
  if a:type == ';'
    call s:do_last(0, a:vis)
  elseif a:type == ','
    call s:do_last(1, a:vis)
  else
    call s:do_search_ch(a:type, a:vis, nr2char(getchar()))
  endif
endfunction

function! s:do_last(inv, vis) abort
  if len(s:last) < 2
    return
  endif

  let type = s:last[0]
  let ch = s:last[1]

  if a:inv
    if type =~ '[a-z]'
      let type = toupper(type)
    else
      let type = tolower(type)
    endif
  endif

  return s:do_search_ch(type, a:vis, ch)
endfunction

function! s:do_search_ch(type, vis, ch)
  let s:last = [a:type, a:ch]

  let flags = 'W'
  let pattern = ''

  if a:type =~ '[A-Z]'
    let flags .= 'b'
  endif

  let pattern = a:ch

  if a:type == 't'
    let pattern = '\zs\_.\ze' . pattern
  elseif a:type == 'T'
    let pattern = pattern . '\zs\_.\ze'
  endif

  if a:vis == 'v'
    exec "norm gv"
  elseif a:vis == 'o'
    exec "norm v"
  endif

  let i = 0
  while i < v:count1
    call search(pattern, flags)
    let i += 1
  endwhile
endfunction

nnoremap <silent> <Plug>(supert-replace-t) :<c-u>silent call <SID>do_search('t', '')<cr>
nnoremap <silent> <Plug>(supert-replace-T) :<c-u>silent call <SID>do_search('T', '')<cr>
nnoremap <silent> <Plug>(supert-replace-f) :<c-u>silent call <SID>do_search('f', '')<cr>
nnoremap <silent> <Plug>(supert-replace-F) :<c-u>silent call <SID>do_search('F', '')<cr>

vnoremap <silent> <Plug>(supert-replace-t) :<c-u>silent call <SID>do_search('t', 'v')<cr>
vnoremap <silent> <Plug>(supert-replace-T) :<c-u>silent call <SID>do_search('T', 'v')<cr>
vnoremap <silent> <Plug>(supert-replace-f) :<c-u>silent call <SID>do_search('f', 'v')<cr>
vnoremap <silent> <Plug>(supert-replace-F) :<c-u>silent call <SID>do_search('F', 'v')<cr>

onoremap <silent> <Plug>(supert-replace-t) :<c-u>silent call <SID>do_search('t', 'o')<cr>
onoremap <silent> <Plug>(supert-replace-T) :<c-u>silent call <SID>do_search('T', 'o')<cr>
onoremap <silent> <Plug>(supert-replace-f) :<c-u>silent call <SID>do_search('f', 'o')<cr>
onoremap <silent> <Plug>(supert-replace-F) :<c-u>silent call <SID>do_search('F', 'o')<cr>

onoremap <silent> <Plug>(supert-replace-,) :<c-u>silent call <SID>do_search(',', 'o')<cr>
onoremap <silent> <Plug>(supert-replace-;) :<c-u>silent call <SID>do_search(';', 'o')<cr>

vnoremap <silent> <Plug>(supert-replace-,) :<c-u>silent call <SID>do_search(',', 'v')<cr>
vnoremap <silent> <Plug>(supert-replace-;) :<c-u>silent call <SID>do_search(';', 'v')<cr>

nnoremap <silent> <Plug>(supert-replace-,) :<c-u>silent call <SID>do_search(',', '')<cr>
nnoremap <silent> <Plug>(supert-replace-;) :<c-u>silent call <SID>do_search(';', '')<cr>


if g:supert_provide_bindings
  nnoremap <silent> <M-t> <Plug>(supert-replace-t)
  nnoremap <silent> <M-T> <Plug>(supert-replace-T)
  nnoremap <silent> <M-f> <Plug>(supert-replace-f)
  nnoremap <silent> <M-F> <Plug>(supert-replace-F)

  vnoremap <silent> <M-t> <Plug>(supert-replace-t)
  vnoremap <silent> <M-T> <Plug>(supert-replace-T)
  vnoremap <silent> <M-f> <Plug>(supert-replace-f)
  vnoremap <silent> <M-F> <Plug>(supert-replace-F)

  vnoremap <silent> <M-t> <Plug>(supert-replace-t)
  vnoremap <silent> <M-T> <Plug>(supert-replace-T)
  vnoremap <silent> <M-f> <Plug>(supert-replace-f)
  vnoremap <silent> <M-F> <Plug>(supert-replace-F)

  vnoremap <silent> <M-;> <Plug>(supert-replace-;)
  vnoremap <silent> <M-,> <Plug>(supert-replace-,)

  nnoremap <silent> <M-;> <Plug>(supert-replace-;)
  nnoremap <silent> <M-,> <Plug>(supert-replace-,)
endif