-
Notifications
You must be signed in to change notification settings - Fork 171
/
nvim_gui_shim.vim
275 lines (232 loc) · 8.17 KB
/
nvim_gui_shim.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
" A Neovim plugin that implements GUI helper commands
if !has('nvim') || exists('g:GuiLoaded')
finish
endif
let g:GuiLoaded = 1
" Get the channel to the last UI in the uis_list
function! s:get_last_ui_chan()
let uis = nvim_list_uis()
if len(uis) == 0
echoerr "No UIs are attached"
return -1
endif
" Use the last UI in the list
return uis[-1].chan
endfunction
" Close the GUI
function! GuiClose() abort
call rpcnotify(0, 'Gui', 'Close', v:exiting)
endfunction
" Open new window
function! GuiNewWindow(args) abort
call rpcnotify(0, 'Gui', 'NewWindow', a:args)
endfunction
" Notify the GUI when exiting Neovim
augroup nvim_gui_shim
autocmd!
autocmd VimLeave * call GuiClose()
augroup END
" A replacement for foreground()
function! GuiForeground() abort
call rpcnotify(0, 'Gui', 'Foreground')
endfunction
" Set maximized state for GUI window (1 is enabled, 0 disabled)
function! GuiWindowMaximized(enabled) abort
call rpcnotify(0, 'Gui', 'WindowMaximized', a:enabled)
endfunction
" Set fullscreen state for GUI window (1 is enabled, 0 disabled)
function! GuiWindowFullScreen(enabled) abort
call rpcnotify(0, 'Gui', 'WindowFullScreen', a:enabled)
endfunction
" Set GUI font
function! GuiFont(fname, ...) abort
let force = get(a:000, 0, 0)
call rpcnotify(0, 'Gui', 'Font', a:fname, force)
endfunction
" Set additional linespace
function! GuiLinespace(height) abort
call rpcnotify(0, 'Gui', 'Linespace', a:height)
endfunction
" Configure mouse hide behaviour (1 is enabled, 0 disabled)
function! GuiMousehide(enabled) abort
call rpcnotify(0, 'Gui', 'Mousehide', a:enabled)
endfunction
" The GuiFont command. For compatibility there is also Guifont
function s:GuiFontCommand(fname, bang) abort
if a:fname ==# ''
if exists('g:GuiFont')
echo g:GuiFont
else
echo 'No GuiFont is set'
endif
else
call GuiFont(a:fname, a:bang ==# '!')
endif
endfunction
command! -nargs=? -bang Guifont call s:GuiFontCommand("<args>", "<bang>")
command! -nargs=? -bang GuiFont call s:GuiFontCommand("<args>", "<bang>")
function s:GuiLinespaceCommand(height) abort
if a:height ==# ''
if exists('g:GuiLinespace')
echo g:GuiLinespace
else
echo 'No GuiLinespace is set'
endif
else
call GuiLinespace(a:height)
endif
endfunction
command! -nargs=? GuiLinespace call s:GuiLinespaceCommand("<args>")
function! s:GuiTabline(enable) abort
call rpcnotify(0, 'Gui', 'Option', 'Tabline', a:enable)
endfunction
command! -nargs=1 GuiTabline call s:GuiTabline(<args>)
function! s:GuiPopupmenu(enable) abort
call rpcnotify(0, 'Gui', 'Option', 'Popupmenu', a:enable)
endfunction
command! -nargs=1 GuiPopupmenu call s:GuiPopupmenu(<args>)
" GuiDrop('file1', 'file2', ...) is similar to :drop file1 file2 ...
" but it calls fnameescape() over all arguments
function GuiDrop(...)
" Check for user defined handler function
if exists('*GuiDropCustomHandler')
return call("GuiDropCustomHandler", a:000)
endif
let l:fnames = deepcopy(a:000)
let l:args = map(l:fnames, 'fnameescape(v:val)')
exec 'drop '.join(l:args, ' ')
if !has('nvim-0.2')
doautocmd BufEnter
endif
endfunction
function GuiName()
if !has('nvim-0.3')
return ''
endif
" Use the last UI in the list
let ui_chan = s:get_last_ui_chan()
if (ui_chan == -1)
return
endif
let info = nvim_get_chan_info(ui_chan)
return get(info.client, 'name', '')
endfunction
function s:ui_has_clipboard(idx, ui_info)
if has_key(a:ui_info, 'chan') == 0
return 0
endif
let info = nvim_get_chan_info(a:ui_info.chan)
let client_info = get(info, 'client', {})
if get(client_info, 'type', 0) == 'ui'
let attrs = get(client_info, 'attributes', {})
return has_key(attrs, 'gui-clipboard')
else
return 0
endif
endfunction
"Enable a GUI provided clipboard
function GuiClipboard()
if !has("nvim-0.3.2")
echoerr "UI clipboard requires nvim >=0.3.2"
return
endif
let uis = nvim_list_uis()
call filter(uis, funcref('s:ui_has_clipboard'))
if len(uis) == 0
echoerr "No UIs with clipboard support are attached"
return
endif
let ui_chan = uis[-1].chan
let g:clipboard = {
\ 'name': 'custom',
\ 'copy': {
\ '+': {lines, regtype -> rpcnotify(ui_chan, 'Gui', 'SetClipboard', lines, regtype, '+')},
\ '*': {lines, regtype -> rpcnotify(ui_chan, 'Gui', 'SetClipboard', lines, regtype, '*')},
\ },
\ 'paste': {
\ '+': {-> rpcrequest(ui_chan, 'Gui', 'GetClipboard', '+')},
\ '*': {-> rpcrequest(ui_chan, 'Gui', 'GetClipboard', '*')},
\ },
\ }
" We need to reload the neovim clipboard provider here so it picks up on
" g:clipboard. In older versions of neovim (<=0.3.8) the provider would
" short circuit if a working clipboard was not available. After 0.3.8
" the provider should not short circuit, andunsetting
" g:loaded_clipboard_provider will enable a full reload of the provider.
"
" TLDR; source this to reinitialize the clipboard provider, this may not
" work
unlet! g:loaded_clipboard_provider
runtime autoload/provider/clipboard.vim
endfunction
" Directory autocommands for Treeview
augroup guiDirEvents
autocmd!
autocmd DirChanged * call rpcnotify(0, 'Dir', getcwd())
autocmd WinEnter * call rpcnotify(0, 'Dir', getcwd())
autocmd VimEnter * call rpcnotify(0, 'Dir', getcwd())
augroup END
" Notifies the TreeView widget of a Show or Hide event
function! s:TreeViewShowHide(show)
call rpcnotify(0, 'Gui', 'TreeView', 'ShowHide', a:show)
endfunction
command! GuiTreeviewShow call <SID>TreeViewShowHide(1)
command! GuiTreeviewHide call <SID>TreeViewShowHide(0)
noremap <script> <Plug>GuiTreeviewShow :call <SID>TreeViewShowHide(1)
noremap <script> <Plug>GuiTreeviewHide :call <SID>TreeViewShowHide(0)
anoremenu <script> Gui.Treeview.Show :call <SID>TreeViewShowHide(1)
anoremenu <script> Gui.Treeview.Hide :call <SID>TreeViewShowHide(0)
" Notifies the TreeView widget of a Toggle event
function! s:TreeViewToggle()
call rpcnotify(0, 'Gui', 'TreeView', 'Toggle')
endfunction
command! GuiTreeviewToggle call <SID>TreeViewToggle()
noremap <script> <Plug>GuiTreeviewToggle :call <SID>TreeViewToggle()
anoremenu <script> Gui.Treeview.Toggle :call <SID>TreeViewShowToggle()
" Show Right-Click ContextMenu
function GuiShowContextMenu() range
call rpcnotify(0, 'Gui', 'ShowContextMenu')
endfunction
" Notify Gui of any cursor movement, used by GuiScrollBar
function s:GuiCursorMoved()
let l:minLineVisible = line('w0')
let l:bufferSize = line('$')
let l:windowHeight = winheight(winnr())
call rpcnotify(0, 'Gui', 'CursorMoved', l:minLineVisible, l:bufferSize, l:windowHeight)
endfunction
autocmd CursorMoved,CursorMovedI,VimResized * call <SID>GuiCursorMoved()
" Show/Hide the Gui ScrollBar
function! s:GuiScrollBar(enable) abort
call rpcnotify(0, 'Gui', 'SetScrollBarVisible', a:enable)
endfunction
command! -nargs=1 GuiScrollBar call s:GuiScrollBar(<args>)
" Use Neovim theming for Qt Widgets
function! s:GuiAdaptiveColor(enable) abort
call rpcnotify(0, 'Gui', 'AdaptiveColor', a:enable)
endfunction
command! -nargs=1 GuiAdaptiveColor call s:GuiAdaptiveColor(<args>)
" Use Neovim font for Qt Widgets
function! s:GuiAdaptiveFont(enable) abort
call rpcnotify(0, 'Gui', 'AdaptiveFont', a:enable)
endfunction
command! -nargs=1 GuiAdaptiveFont call s:GuiAdaptiveFont(<args>)
" Override default Qt Style using QStyleFactory
function! s:GuiAdaptiveStyle(styleName, ...) abort
call rpcnotify(0, 'Gui', 'AdaptiveStyle', a:styleName)
endfunction
command! -nargs=? GuiAdaptiveStyle call s:GuiAdaptiveStyle("<args>")
" Print a list of available Qt Styles
function! s:GuiAdaptiveStyleList() abort
call rpcnotify(0, 'Gui', 'AdaptiveStyleList')
endfunction
command! -nargs=0 GuiAdaptiveStyleList call s:GuiAdaptiveStyleList()
" Change rendering logic to use ligature-compatible rendering scheme
function! s:GuiRenderLigatures(enable) abort
call rpcnotify(0, 'Gui', 'Option', 'RenderLigatures', a:enable)
endfunction
command! -nargs=1 GuiRenderLigatures call s:GuiRenderLigatures(<args>)
" Set window transparency, forwards to Qt setWindowOpacity
function! s:GuiWindowOpacityCommand(value) abort
call rpcnotify(0, 'Gui', 'WindowOpacity', a:value)
endfunction
command! -nargs=1 GuiWindowOpacity call s:GuiWindowOpacityCommand("<args>")