-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathpeekaboo.vim
More file actions
247 lines (219 loc) · 6.94 KB
/
Copy pathpeekaboo.vim
File metadata and controls
247 lines (219 loc) · 6.94 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
" The MIT License (MIT)
"
" Copyright (c) 2017 Junegunn Choi
"
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to deal
" in the Software without restriction, including without limitation the rights
" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
" copies of the Software, and to permit persons to whom the Software is
" furnished to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included in
" all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
" THE SOFTWARE.
let s:cpo_save = &cpo
set cpo&vim
" Default options
let s:default_delay = 0
let s:default_window = 'vertical botright 30new'
let s:default_compact = 0
let s:QUOTE = '"'
let s:REPLAY = '@'
let s:CTRL_R = "\<c-r>"
let s:buf_peekaboo = 0
" Returns true if timed out
function! s:wait_with_timeout(timeout)
let timeout = a:timeout
while timeout >= 0
if getchar(1)
return 0
endif
if timeout > 0
sleep 20m
endif
let timeout -= 20
endwhile
return 1
endfunction
" Checks if Peekaboo buffer is open
function! s:is_open()
return s:buf_peekaboo
endfunction
" Closes peekaboo buffer
function! s:close()
silent! execute 'bd' s:buf_peekaboo
let s:buf_peekaboo = 0
execute s:winrestcmd
endfunction
" Appends macro list for the specified group to Peekaboo window
function! s:append_group(title, regs)
let compact = get(g:, 'peekaboo_compact', s:default_compact)
if !compact | call append(line('$'), a:title.':') | endif
for r in a:regs
try
if r == '%' | let val = s:buf_current
elseif r == '#' | let val = s:buf_alternate
else | let val = eval('@'.r)[:&columns]
endif
if empty(val)
continue
endif
let s:regs[printf('%s', r)] = line('$')
call append(line('$'), printf(' %s: %s', r, val))
catch
endtry
endfor
if !compact | call append(line('$'), '') | endif
endfunction
" Opens peekaboo window
function! s:open(mode)
let [s:buf_current, s:buf_alternate, s:winrestcmd] = [@%, @#, winrestcmd()]
execute get(g:, 'peekaboo_window', s:default_window)
let s:buf_peekaboo = bufnr('')
setlocal nonumber buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
\ modifiable statusline=>\ Registers nocursorline nofoldenable
if exists('&relativenumber')
setlocal norelativenumber
endif
setfiletype peekaboo
augroup peekaboo
autocmd!
autocmd CursorMoved <buffer> bd
augroup END
let s:regs = {}
call s:append_group('Special', ['"', '*', '+', '-'])
call s:append_group('Read-only', a:mode ==# s:REPLAY ? ['.', ':'] : ['.', '%', '#', '/', ':'])
call s:append_group('Numbered', map(range(0, 9), 'string(v:val)'))
call s:append_group('Named', map(range(97, 97 + 25), 'nr2char(v:val)'))
normal! "_dd
endfunction
" Checks if the buffer for the position is visible on screen
function! s:is_visible(pos)
return a:pos.tab == tabpagenr() && bufwinnr(a:pos.buf) != -1
endfunction
" Triggers gv to keep visual highlight on
function! s:gv(visualmode, visible)
if a:visualmode && a:visible
let wnr = bufwinnr('')
wincmd p
normal! gv
redraw
execute wnr 'wincmd w'
else
redraw
endif
endfunction
" Feeds the final key sequence
function! s:feed(count, mode, reg, rest)
call feedkeys(a:count > 1 ? a:count : '', 'n')
if a:mode ==# s:QUOTE
call feedkeys('"'.a:reg, 'n')
call feedkeys(a:rest)
elseif a:mode ==# s:CTRL_R
call feedkeys("\<c-r>".a:reg, 'n')
else
call feedkeys('@'.a:reg, 'n')
endif
endfunction
let s:scroll = {
\ "\<up>": "\<c-y>", "\<down>": "\<c-e>",
\ "\<c-y>": "\<c-y>", "\<c-e>": "\<c-e>",
\ "\<c-u>": "\<c-u>", "\<c-d>": "\<c-d>",
\ "\<c-b>": "\<c-b>", "\<c-f>": "\<c-f>",
\ "\<pageup>": "\<c-b>", "\<pagedown>": "\<c-f>"
\ }
" Returns the position of the current buffer as a dictionary
function! s:getpos()
return {'tab': tabpagenr(), 'buf': bufnr(''), 'win': winnr(), 'cnt': winnr('$')}
endfunction
function! peekaboo#peek(count, mode, visualmode)
" First check if we should start peekaboo, if not just return the mode key
let timeout = get(g:, 'peekaboo_delay', s:default_delay)
if !s:wait_with_timeout(timeout)
return a:mode
endif
let s:args = [a:count, a:mode, a:visualmode]
return "\<Plug>(peekaboo)"
endfunction
function! peekaboo#aboo()
let [cnt, mode, visualmode] = s:args
if s:is_open()
call s:close()
endif
let positions = { 'current': s:getpos() }
call s:open(mode)
let positions.peekaboo = s:getpos()
let inplace = positions.current.tab == positions.peekaboo.tab &&
\ positions.current.win == positions.peekaboo.win &&
\ positions.current.cnt == positions.peekaboo.cnt
let visible = !inplace && s:is_visible(positions.current)
call s:gv(visualmode, visible)
let [stl, lst] = [&showtabline, &laststatus]
let zoom = 0
try
while 1
let ch = getchar()
let reg = nr2char(ch)
let key = get(s:scroll, ch, get(s:scroll, reg, ''))
if !empty(key)
execute 'normal!' key
call s:gv(visualmode, visible)
continue
endif
if zoom
tab close
let [&showtabline, &laststatus] = [stl, lst]
call s:gv(visualmode, visible)
endif
if reg != ' '
break
endif
if !zoom
tab split
set showtabline=0 laststatus=0
endif
let zoom = !zoom
redraw
endwhile
let rest = ''
if mode ==# s:QUOTE && has_key(s:regs, tolower(reg))
let line = s:regs[tolower(reg)]
execute line
execute 'syntax region peekabooSelected start=/\%'.line.'l\%5c/ end=/$/'
setlocal cursorline
call setline(line('.'), substitute(getline('.'), ' .', ' '.reg, ''))
call s:gv(visualmode, visible)
let rest = nr2char(getchar())
endif
" - Make sure that we're back to the original tab/window/buffer
" - e.g. g:peekaboo_window = 'tabnew' / 'enew'
if inplace
noautocmd execute positions.current.win.'wincmd w'
noautocmd execute 'buf' positions.current.buf
else
noautocmd execute 'tabnext' positions.current.tab
call s:close()
noautocmd execute positions.current.win.'wincmd w'
endif
if visualmode
normal! gv
endif
call s:feed(cnt, mode, reg, rest)
catch /^Vim:Interrupt$/
return
finally
let [&showtabline, &laststatus] = [stl, lst]
call s:close()
redraw
endtry
endfunction
let &cpo = s:cpo_save
unlet s:cpo_save