forked from liuchengxu/vim-clap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyanks.vim
94 lines (77 loc) · 2.67 KB
/
yanks.vim
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
" Author: Ratheesh S <[email protected]>
" Description: List the recently yanked/deleted lines
" Based on : https://github.com/sgur/ctrlp-extensions.vim
let s:save_cpo = &cpoptions
set cpoptions&vim
let s:min_len = get(g:, 'clap_provider_yanks_min_len', 1)
let s:max_yanks = get(g:, 'clap_provider_yanks_max_entries', 20)
let s:yank_info_map = {}
if exists('g:clap_provider_yanks_history') && filereadable(expand(g:clap_provider_yanks_history))
let s:yank_history = readfile(expand(g:clap_provider_yanks_history))
else
let s:yank_history = []
endif
let s:yanks = {}
function! clap#provider#yanks#collect() abort
let last_yanked = getreg('"')
if len(last_yanked) < s:min_len
return
endif
if !empty(s:yank_history) && last_yanked == s:yank_history[0]
return
endif
call filter(s:yank_history, 'v:val != last_yanked')
call insert(s:yank_history, last_yanked)
let s:yank_info_map[last_yanked] = { 'syntax': getbufvar('', '&syntax'), 'bufname': bufname('%') }
" Trim yank entries(purge old ones)
if len(s:yank_history) > s:max_yanks
let oldest_yanked = remove(s:yank_history, -1)
if type(oldest_yanked) == v:t_string && has_key(s:yank_info_map, oldest_yanked)
call remove(s:yank_info_map, oldest_yanked)
endif
endif
endfunction
function! s:save_history() abort
if !empty(s:yank_history)
call writefile(s:yank_history, expand(g:clap_provider_yanks_history))
endif
endfunction
function! clap#provider#yanks#init() abort
augroup ClapYanksCollect
autocmd!
autocmd TextYankPost * call clap#provider#yanks#collect()
if exists('g:clap_provider_yanks_history')
\ && filewritable(expand(g:clap_provider_yanks_history))
autocmd VimLeavePre * call s:save_history()
endif
augroup END
" collect the data from default register
call clap#provider#yanks#collect()
endfunction
function! s:yanks.source() abort
return s:yank_history
endfunction
function! s:yanks.on_move() abort
let curline = g:clap.display.getcurline()
let lines = split(curline, "\n")[:10]
if !empty(lines)
call g:clap.preview.show(lines)
if has_key(s:yank_info_map, curline)
call g:clap.preview.setbufvar('&syntax', s:yank_info_map[curline].syntax)
endif
endif
endfunction
function! s:yanks.sink(selected) abort
call setreg('"', a:selected)
normal! ""p
endfunction
function! s:yanks.on_enter() abort
if !get(g:, 'clap_enable_yanks_provider', 1)
call clap#helper#echo_error('Clap yanks provider is disabled, set g:clap_enable_yanks_provider to 1 to enable.')
call clap#handler#exit()
call feedkeys("\<Esc>", 'n')
endif
endfunction
let g:clap#provider#yanks# = s:yanks
let &cpoptions = s:save_cpo
unlet s:save_cpo