-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhlyanked.vim
102 lines (82 loc) · 2.26 KB
/
hlyanked.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
95
96
97
98
99
100
101
102
vim9script noclear
# Highlight yanked text.
# Maintainer: Ubaldo Tiberi
# License: BSD3-Clause
# GetLatestVimScripts: 6075 1 :AutoInstall: hlyanked.vim
if !has('vim9script') || v:version < 900
finish
endif
if exists('g:hlyanked_loaded')
finish
endif
g:hlyanked_loaded = 1
if !exists('g:hlyanked_hlgroup')
g:hlyanked_hlgroup = 'Visual'
endif
if !exists('g:hlyanked_timeout')
g:hlyanked_timeout = 400
endif
if !exists('g:hlyanked_save_yanks')
g:hlyanked_save_yanks = true
endif
# ----------------------------------------------------
# The real deal follows
var timer_id = -1
var match_id = -1
def HighlightYanked()
# Remove leftover timers and highlights
KillHighlight()
# Get extremes of yanking: start = (l0, c0), end = (l1, c1)
var l0 = line("'[")
var c0 = col("'[")
var l1 = line("']")
var delta = 0
if l0 == l1
delta = len(v:event.regcontents[-1]) - (col("']") - c0)
endif
var c1 = col("']") + delta
# For understanding the following regex read :h \% and mind that \_.* are
# all characters including new lines.
# The regex reads:
# 'Take all characters, including newlines, from (l0,c0) to (l1,c1)'
var match_pattern = $'\%{l0}l\%{c0}c\_.*\%{l1}l\%{c1}c'
match_id = matchadd(g:hlyanked_hlgroup, match_pattern)
timer_id = timer_start(g:hlyanked_timeout, 'RemoveHighlight')
enddef
def RemoveHighlight(timer: number)
if match_id != -1
matchdelete(match_id)
match_id = -1
endif
enddef
def StopTimer(timer: number)
if timer_id != -1
timer_stop(timer_id)
timer_id = -1
endif
enddef
def KillHighlight()
StopTimer(timer_id)
RemoveHighlight(match_id)
enddef
augroup HighlightYanked
autocmd!
autocmd TextYankPost * if !v:event.visual && v:event.operator == 'y' && !empty(v:event.regtype)
| HighlightYanked()
| endif
augroup END
augroup KillHighlight
autocmd!
autocmd WinLeave * KillHighlight()
augroup END
def ShiftRegisters()
for ii in [8, 7, 6, 5, 4, 3, 2, 1, 0]
setreg(string(ii + 1), getreg(string(ii)))
endfor
enddef
if g:hlyanked_save_yanks
augroup YankShiftRegisters
autocmd!
autocmd TextYankPost * if v:event.operator == 'y' | ShiftRegisters() | endif
augroup END
endif