/
lens.vim
194 lines (160 loc) · 5.05 KB
/
lens.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
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
" Copyright (c) 2020 Spiers, Cam <camspiers@gmail.com>
" Licensed under the terms of the MIT license.
""
" ██╗ ███████╗███╗ ██╗███████╗ ██╗ ██╗██╗███╗ ███╗
" ██║ ██╔════╝████╗ ██║██╔════╝ ██║ ██║██║████╗ ████║
" ██║ █████╗ ██╔██╗ ██║███████╗ ██║ ██║██║██╔████╔██║
" ██║ ██╔══╝ ██║╚██╗██║╚════██║ ╚██╗ ██╔╝██║██║╚██╔╝██║
" ███████╗███████╗██║ ╚████║███████║██╗╚████╔╝ ██║██║ ╚═╝ ██║
" ╚══════╝╚══════╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═══╝ ╚═╝╚═╝ ╚═╝
if exists('g:lens#loaded')
finish
endif
let g:lens#loaded = 1
if ! exists('g:lens#disabled')
" Global disable
let g:lens#disabled = 0
endif
if ! exists('g:lens#animate')
" Enable animation when available
let g:lens#animate = 1
endif
if ! exists("g:lens#resize_floating")
" Enable resizing of neovim's floating windows
let g:lens#resize_floating = 0
endif
if ! exists('g:lens#height_resize_max')
" When resizing don't go beyond the following height
let g:lens#height_resize_max = 20
endif
if ! exists('g:lens#height_resize_min')
" When resizing don't go below the following height
let g:lens#height_resize_min = 5
endif
if ! exists('g:lens#width_resize_max')
" When resizing don't go beyond the following width
let g:lens#width_resize_max = 80
endif
if ! exists('g:lens#width_resize_min')
" When resizing don't go below the following width
let g:lens#width_resize_min = 20
endif
if ! exists('g:lens#disabled_filetypes')
" Disable for the following filetypes
let g:lens#disabled_filetypes = []
endif
if ! exists('g:lens#disabled_buftypes')
" Disable for the following buftypes
let g:lens#disabled_buftypes = []
endif
if ! exists('g:lens#disabled_filenames')
" Disable for the following filenames
let g:lens#disabled_filenames = []
endif
if ! exists('g:lens#disable_for_diff')
" Disable for the following filenames
let g:lens#disable_for_diff = 0
endif
""
" Toggles the plugin on and off
function! lens#toggle() abort
let g:lens#disabled = !g:lens#disabled
endfunction
""
" Returns a width or height respecting the passed configuration
function! lens#get_size(current, target, resize_min, resize_max) abort
if a:current > a:target
return a:current
endif
return max([
\ a:current,
\ min([
\ max([a:target, a:resize_min]),
\ a:resize_max,
\ ])
\ ])
endfunction
""
" Gets the rows of the current window
function! lens#get_rows() abort
return line('$')
endfunction
""
" Gets the target height
function! lens#get_target_height() abort
return lens#get_rows() + (&laststatus != 0 ? 1 : 0)
endfunction
""
" Gets the cols of the current window
function! lens#get_cols() abort
return max(map(getline(line("w0"),line("w$")), {k,v->len(v)}))
endfunction
""
" Gets the target width
function! lens#get_target_width() abort
return lens#get_cols() + (wincol() - virtcol('.'))
endfunction
""
" Resizes the window to respect minimal lens configuration
function! lens#run() abort
let width = lens#get_size(
\ winwidth(0),
\ lens#get_target_width(),
\ g:lens#width_resize_min,
\ g:lens#width_resize_max
\)
let height = lens#get_size(
\ winheight(0),
\ lens#get_target_height(),
\ g:lens#height_resize_min,
\ g:lens#height_resize_max
\)
if g:lens#animate && exists('g:animate#loaded') && g:animate#loaded
if ! animate#window_is_animating(winnr())
call animate#window_absolute(width, height)
endif
else
execute 'vertical resize ' . width
execute 'resize ' . height
endif
endfunction
function! lens#win_enter() abort
" Don't resize if the window is floating
if exists('*nvim_win_get_config')
if ! g:lens#resize_floating && nvim_win_get_config(0)['relative'] != ''
return
endif
endif
if g:lens#disabled || g:lens#enter_disabled
return
endif
if index(g:lens#disabled_filetypes, &filetype) != -1
return
endif
if index(g:lens#disabled_buftypes, &buftype) != -1
return
endif
if len(g:lens#disabled_filenames) > 0
let l:filename = expand('%:p')
for l:pattern in g:lens#disabled_filenames
if match(l:filename, l:pattern) > -1
return
endif
endfor
endif
if g:lens#disable_for_diff == 1
if &diff == 1
return
endif
endif
call lens#run()
endfunction
""
" By default set up running resize on window enter except for new windows
augroup lens
let g:lens#enter_disabled = 0
autocmd! WinNew * let g:lens#enter_disabled = 1
autocmd! WinEnter * call lens#win_enter()
autocmd! WinNew * let g:lens#enter_disabled = 0
augroup END
" vim:fdm=marker