aboutsummaryrefslogtreecommitdiff
path: root/plugin/supert.vim
blob: f4a8e85e38df38703b1ceeb859950178138ff417 (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
116
117
118
119
120
121
" 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 = []

let s:last_char = ''
function! s:getchar()
  echom "Is not repeating? " . string(! exists('v:repeating') || ! v:repeating)
  if ! exists('v:repeating') || ! v:repeating
    let s:last_char = nr2char(getchar())
  endif
  return s:last_char
endfunction

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, s: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

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

nnoremap <Plug>(supert-replace-t) <cmd>call <SID>do_search('t', '')<cr>
nnoremap <Plug>(supert-replace-T) <cmd>call <SID>do_search('T', '')<cr>
nnoremap <Plug>(supert-replace-f) <cmd>call <SID>do_search('f', '')<cr>
nnoremap <Plug>(supert-replace-F) <cmd>call <SID>do_search('F', '')<cr>

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

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

onoremap <Plug>(supert-replace-,) <cmd>call <SID>do_search(',', 'o')<cr>
onoremap <Plug>(supert-replace-;) <cmd>call <SID>do_search(';', 'o')<cr>

vnoremap <Plug>(supert-replace-,) <cmd>call <SID>do_search(',', 'v')<cr>
vnoremap <Plug>(supert-replace-;) <cmd>call <SID>do_search(';', 'v')<cr>

nnoremap <Plug>(supert-replace-,) <cmd>call <SID>do_search(',', '')<cr>
nnoremap <Plug>(supert-replace-;) <cmd>call <SID>do_search(';', '')<cr>


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

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

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

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

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

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