@@ -142,6 +142,8 @@ function GuiName()
142
142
return get (info.client, ' name' , ' ' )
143
143
endfunction
144
144
145
+ let s: ui_clipboard_enabled = 0
146
+
145
147
function s: ui_has_clipboard (idx, ui_info)
146
148
if has_key (a: ui_info , ' chan' ) == 0
147
149
return 0
@@ -158,45 +160,99 @@ function s:ui_has_clipboard(idx, ui_info)
158
160
endif
159
161
endfunction
160
162
161
- " Enable a GUI provided clipboard
162
- function GuiClipboard ()
163
+ function s: reload_clipboard_provider ()
164
+ " We need to reload the neovim clipboard provider here so it picks up on
165
+ " g:clipboard. In older versions of neovim (<=0.3.8) the provider would
166
+ " short circuit if a working clipboard was not available. After 0.3.8
167
+ " the provider should not short circuit, and unsetting
168
+ " g:loaded_clipboard_provider will enable a full reload of the provider.
169
+ "
170
+ " TLDR; source this to reinitialize the clipboard provider, this may not
171
+ " work
172
+ unlet ! g: loaded_clipboard_provider
173
+ runtime autoload/provider/ clipboard .vim
174
+ endfunction
175
+
176
+ function s: disable_custom_clipboard ()
177
+ if exists (" g:clipboard" )
178
+ unlet g: clipboard
179
+ endif
180
+ call s: reload_clipboard_provider ()
181
+ endfunction
182
+
183
+ " Enable a GUI clipboard
184
+ function s: SetupGuiClipboard (silent )
163
185
if ! has (" nvim-0.3.2" )
164
- echoerr " UI clipboard requires nvim >=0.3.2"
186
+ if a: silent == 0
187
+ echoerr " UI clipboard requires nvim >=0.3.2"
188
+ endif
165
189
return
166
190
endif
167
191
168
192
let uis = nvim_list_uis ()
169
193
call filter (uis, funcref (' s:ui_has_clipboard' ))
170
194
if len (uis) == 0
171
- echoerr " No UIs with clipboard support are attached"
195
+ if a: silent == 0
196
+ echoerr " No UIs with clipboard support are attached"
197
+ end
198
+ call s: disable_custom_clipboard ()
172
199
return
173
200
endif
174
201
let ui_chan = uis[-1 ].chan
175
202
176
- let g: clipboard = {
177
- \ ' name' : ' custom' ,
178
- \ ' copy' : {
179
- \ ' +' : {lines , regtype - > rpcnotify (ui_chan, ' Gui' , ' SetClipboard' , lines , regtype, ' +' )},
180
- \ ' *' : {lines , regtype - > rpcnotify (ui_chan, ' Gui' , ' SetClipboard' , lines , regtype, ' *' )},
181
- \ },
182
- \ ' paste' : {
183
- \ ' +' : {- > rpcrequest (ui_chan, ' Gui' , ' GetClipboard' , ' +' )},
184
- \ ' *' : {- > rpcrequest (ui_chan, ' Gui' , ' GetClipboard' , ' *' )},
185
- \ },
186
- \ }
203
+ let g: clipboard = {
204
+ \ ' name' : ' custom' ,
205
+ \ ' copy' : {
206
+ \ ' +' : {lines , regtype - > rpcnotify (ui_chan, ' Gui' , ' SetClipboard' , lines , regtype, ' +' )},
207
+ \ ' *' : {lines , regtype - > rpcnotify (ui_chan, ' Gui' , ' SetClipboard' , lines , regtype, ' *' )},
208
+ \ },
209
+ \ ' paste' : {
210
+ \ ' +' : {- > rpcrequest (ui_chan, ' Gui' , ' GetClipboard' , ' +' )},
211
+ \ ' *' : {- > rpcrequest (ui_chan, ' Gui' , ' GetClipboard' , ' *' )},
212
+ \ },
213
+ \ }
214
+
215
+ call s: reload_clipboard_provider ()
216
+ endfunction
187
217
188
- " We need to reload the neovim clipboard provider here so it picks up on
189
- " g:clipboard. In older versions of neovim (<=0.3.8) the provider would
190
- " short circuit if a working clipboard was not available. After 0.3.8
191
- " the provider should not short circuit, andunsetting
192
- " g:loaded_clipboard_provider will enable a full reload of the provider.
193
- "
194
- " TLDR; source this to reinitialize the clipboard provider, this may not
195
- " work
196
- unlet ! g: loaded_clipboard_provider
197
- runtime autoload/provider/ clipboard .vim
218
+ " For compatibility with an earlier version
219
+ function GuiClipboard ()
220
+ call s: SetupGuiClipboard (0 )
221
+ endfunction
222
+
223
+ " Enable/Disable the GUI clipboard
224
+ function s: GuiClipboardSet (enable )
225
+ if a: enable == 0
226
+ let s: ui_clipboard_enabled = 0
227
+ call s: disable_custom_clipboard ()
228
+ elseif s: ui_clipboard_enabled == 1
229
+ " clipboard already enabled
230
+ else
231
+ if exists (" g:clipboard" )
232
+ echoerr " A custom g:clipboard is already configured"
233
+ endif
234
+
235
+ call s: SetupGuiClipboard (0 )
236
+ let s: ui_clipboard_enabled = 1
237
+ endif
238
+ endfunction
239
+ command ! -nargs =1 GuiClipboard call s: GuiClipboardSet (<args> )
240
+
241
+ " If enabled reconfigure the GUI clipboard
242
+ function s: UpdateGuiClipboard ()
243
+ if s: ui_clipboard_enabled == 1
244
+ call s: SetupGuiClipboard (1 )
245
+ endif
198
246
endfunction
199
247
248
+ " When a UI attaches/detaches try to reconfigure the GUI
249
+ " clipboard
250
+ augroup GuiClipboard
251
+ autocmd !
252
+ autocmd UIEnter * :call s: UpdateGuiClipboard ()
253
+ autocmd UILeave * :call s: UpdateGuiClipboard ()
254
+ augroup END
255
+
200
256
" Directory autocommands for Treeview
201
257
augroup guiDirEvents
202
258
autocmd !
0 commit comments