forked from bfredl/bfredl.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloaty.lua
More file actions
151 lines (142 loc) · 3.25 KB
/
floaty.lua
File metadata and controls
151 lines (142 loc) · 3.25 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
local h = _G._bfredl_floaty or {}
local colors = require'bfredl.colors'
local u = require'bfredl.util'
local a, win, buf = u.a, u.win, u.buf
_G._bfredl_floaty = h
h.toclose = h.toclose or {}
function h.f(args)
local b,w, oc
if args.update and a.win_is_valid(args.update) then
w = args.update
b = win.get_buf(w)
oc = win.get_config(w)
end
if args.buf and buf.is_valid(args.buf) then
b = args.buf
else
b = a.create_buf(false, true)
end
local firstline = nil
local text
if args.text then
if type(args.text) == "string" then
text = vim.split(args.text, '\n', true)
else
text = args.text
end
firstline = text[1] or ""
buf.set_lines(b, 0, -1, true, text)
end
local p_rows, p_cols = vim.o.lines-1, vim.o.columns
if args.win then
p_rows = win.get_height(args.win)
p_cols = win.get_width(args.win)
end
local width=args.w or (oc and oc.width)
if not width then
if firstline then
width = a.strwidth(firstline)
else
width = 10
end
end
if width < 1 then width = 1 end
local height=args.h or (oc and oc.height)
if not height then
if text then
height = #text
else
height = 1
end
end
if args.center == true or args.center == "r" then
args.r = (p_rows - height) / 2
end
if args.center == true or args.center == "c" then
args.c = (p_cols - width) / 2
end
local relative = args.relative
if not relative then
if args.win then
relative = 'win'
else
relative = 'editor'
end
end
local config = {
relative=relative;
win=args.win;
width=width;
height=height;
row=args.r or 2;
col=args.c or 5;
style=args.style or "minimal";
focusable=args.focusable;
border=args.border;
zindex=args.zindex;
}
if w then
win.set_config(w, config)
if args.enter then
a.set_current_win(w)
end
else
w = a.open_win(b, args.enter, config)
end
if args.blend then
win.set_option(w, 'winblend', args.blend)
end
if args.bg then
local bg
if string.sub(args.bg, 1, 1) == "#" then
-- TODO(bfredl):be smart and reuse hl ids.
bg = "XXTMP"..u.id()
colors.def_hi(bg, {bg=args.bg, fg=args.fg})
else
bg = args.bg
end
oldhl = win.get_option(w, 'winhl')
win.set_option(w, 'winhl', oldhl..(#oldhl > 0 and ',' or '')..'Normal:'..bg)
end
if args.chold then
h.toclose[w] = true
end
if args.replace and win.is_valid(args.replace) then
win.close(args.replace, false)
end
local ret
return buf.call(b, function()
local ret
if args.cat then
if args.term then error('FY!') end
args.term = {'cat', vim.fn.expand(args.cat, ':p')}
end
if args.term then
vim.fn.termopen(args.term)
end
if args.ft then
v ([[set ft=]]..args.ft)
end
-- already curwin/curbuf but be nice
if args.fn then
ret = args.fn(b,w)
end
return ret or w
end)
end
u.exec [[
augroup bfred_floaty
au CursorHold * lua _G._bfredl_floaty.cursorhold()
augroup END
]]
function h.cursorhold()
for w, k in pairs(h.toclose) do
if not a.win_is_valid(w) then
h.toclose[w] = nil
elseif k and a.get_current_win() ~= w then
a.win_close(w, false)
h.toclose[w] = nil
end
end
end
return h