|
1 | | -*eval.txt* For Vim version 9.1. Last change: 2025 Oct 12 |
| 1 | +*eval.txt* For Vim version 9.1. Last change: 2025 Oct 14 |
2 | 2 |
|
3 | 3 |
|
4 | 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
@@ -38,6 +38,7 @@ a remark is given. |
38 | 38 | 12. The sandbox |eval-sandbox| |
39 | 39 | 13. Textlock |textlock| |
40 | 40 | 14. Vim script library |vim-script-library| |
| 41 | +15. Clipboard providers |clipboard-providers| |
41 | 42 |
|
42 | 43 | Testing support is documented in |testing.txt|. |
43 | 44 | Profiling is documented at |profiling|. |
@@ -2251,6 +2252,11 @@ v:clipmethod The current method of accessing the clipboard that is being |
2251 | 2252 | unavailable. |
2252 | 2253 | See 'clipmethod' for more details. |
2253 | 2254 |
|
| 2255 | + *v:clipproviders* |
| 2256 | +v:clipproviders |
| 2257 | + A dictionary containing clipboard providers, see |
| 2258 | + |clipboard-providers| for more information. |
| 2259 | + |
2254 | 2260 | *v:cmdarg* *cmdarg-variable* |
2255 | 2261 | v:cmdarg This variable is used for two purposes: |
2256 | 2262 | 1. The extra arguments given to a file read/write command. |
@@ -5263,5 +5269,133 @@ Usage: >vim |
5263 | 5269 | :call dist#vim9#Launch(<args>) |
5264 | 5270 | :Launch <app> <args>. |
5265 | 5271 | < |
| 5272 | +============================================================================== |
| 5273 | +15. Clipboard providers *clipboard-providers* |
| 5274 | + |
| 5275 | +When Vim is compiled with the |+clipboard_provider| feature, which requires |
| 5276 | +the |+eval| feature, creating custom clipboards is possible. These providers |
| 5277 | +handle the "+" and "*" registers. Note that on non-Unix or non-VMS systems, |
| 5278 | +only the "*" register will be available for use. |
| 5279 | + |
| 5280 | +To add a provider, add a new entry to the |v:clipproviders| dictionary, in the |
| 5281 | +format of: > |
| 5282 | + let v:clipproviders["name"] = { |
| 5283 | + \ "available": function("Available"), |
| 5284 | + \ "paste": { |
| 5285 | + \ '+': function("Paste"), " For the + register |
| 5286 | + \ '*': function("Paste"), " For the * register |
| 5287 | + \ }, |
| 5288 | + \ "copy": { |
| 5289 | + \ '+': function("Copy"), " Same thing as above |
| 5290 | + \ '*': function("Copy"), |
| 5291 | + \ }, |
| 5292 | + \ } |
| 5293 | +
|
| 5294 | +The key is the provider name, which should be used in 'clipmethod', for |
| 5295 | +example in the following example, you would add "name" to 'clipmethod' in |
| 5296 | +order to use it. > |
| 5297 | + set clipmethod=name,wayland,x11,gui |
| 5298 | +
|
| 5299 | +Each callback can either be a name of a function in a string, a |Funcref|, or |
| 5300 | +a |lambda| expression. |
| 5301 | + |
| 5302 | +Note that these dictionary entries are optional, for example, if you don't |
| 5303 | +care about the "copy" functions, then you can simply exclude them. When Vim |
| 5304 | +yanks/copies something, then simply nothing will be done. |
| 5305 | + |
| 5306 | + *clipboard-provider-available* |
| 5307 | +The "available" callback should return a string, which should contain which |
| 5308 | +clipboard registers are available. For example, if the "+" register is only |
| 5309 | +available, then the function would return "+", or if both "+" and "*" are |
| 5310 | +available, then return "+*". |
| 5311 | + |
| 5312 | + *clipboard-provider-paste* |
| 5313 | +The "paste" callback takes a first argument which is the register being put |
| 5314 | +(string), and a second argument which is the type of access (string). It |
| 5315 | +should return either a tuple/list or string. If a tuple/list is returned, it |
| 5316 | +should have two elements: |
| 5317 | + - The register type conforming to |setreg()|. |
| 5318 | + - A list of strings |
| 5319 | +If the register type is an empty string, then the type is automatically |
| 5320 | +chosen, see |setreg()|. If a string is returned, then it can either be "clear" |
| 5321 | +or "previous". "clear" makes Vim clear the register, and "previous" makes Vim |
| 5322 | +use the previous register contents (or current depending on how you view it). |
| 5323 | + |
| 5324 | +The second argument, the access type, can either be "explicit" or "implicit". |
| 5325 | +"explicit" means that the user is directly accessing the clipboard, such as |
| 5326 | +putting text, or calling |getreg()|; "implicit" means that the clipboard is |
| 5327 | +being accessed indirectly, such when |:registers| is called, or when an |
| 5328 | +unrelated operation causes Vim to access the clipboard. |
| 5329 | + |
| 5330 | +This is useful since some operations in Vim implicity access the clipboard |
| 5331 | +indirectly. For example, if when you want to create a provider for the OSC52 |
| 5332 | +command (accessing the clipboard via an escape code). Many terminals show a |
| 5333 | +confirmation if you want Vim to access the clipboard. This can be very |
| 5334 | +annoying with the terminal asking for permission everytime you do something |
| 5335 | +that accesses the clipboard behind the scenes. A good way to handle implicit |
| 5336 | +access is to return "previous", which leaves the register contents unchanged. |
| 5337 | + |
| 5338 | + *clipboard-provider-copy* |
| 5339 | +The "copy" callback returns nothing, and takes the given arguments in order: |
| 5340 | + - The register being operated on |
| 5341 | + - The register type, conforming to |getregtype()| |
| 5342 | + - A list of strings to copy |
| 5343 | + |
| 5344 | +The provider can do whatever it wants with the given information. This |
| 5345 | +function is called on every request to change the clipboard register(s). |
| 5346 | + |
| 5347 | + *clipboard-provider-textlock* |
| 5348 | +In both the "paste" and "copy" callbacks, it is not allowed to change the |
| 5349 | +buffer text, see |textlock|. |
| 5350 | + |
| 5351 | + *clipboard-provider-example* |
| 5352 | +Here is an example script that uses the clipboard provider feature through the |
| 5353 | +OSC52 command: >vim |
| 5354 | + |
| 5355 | + func Available() |
| 5356 | + return "+" |
| 5357 | + endfunc |
| 5358 | + |
| 5359 | + func Paste(reg, type) |
| 5360 | + " If implicit access, don't do anything |
| 5361 | + if a:type == "implicit" |
| 5362 | + return "previous" |
| 5363 | + endif |
| 5364 | + |
| 5365 | + augroup OSC |
| 5366 | + autocmd! |
| 5367 | + autocmd TermResponseAll osc ++once call feedkeys("\<F30>", '!') |
| 5368 | + augroup END |
| 5369 | + |
| 5370 | + " Send command |
| 5371 | + call echoraw("\<Esc>]52;c;?\<Esc>\\") |
| 5372 | + |
| 5373 | + " Wait until autocmd is triggered |
| 5374 | + while getchar(-1) != "\<F30>" |
| 5375 | + endwhile |
| 5376 | + |
| 5377 | + autocmd! OSC |
| 5378 | + |
| 5379 | + " Extract the base64 stuff |
| 5380 | + let l:stuff = matchstr(v:termosc, '52;c;\zs[A-Za-z0-9+/=]\+') |
| 5381 | + |
| 5382 | + return ("", blob2str(base64_decode(l:stuff))) |
| 5383 | + endfunc |
| 5384 | + |
| 5385 | + func Copy(reg, type, lines) |
| 5386 | + call echoraw("\<Esc>]52;c;" .. |
| 5387 | + \ base64_encode(str2blob(a:lines)) .. "\<Esc>\\") |
| 5388 | + endfunc |
| 5389 | + let v:clipproviders["myosc"] = { |
| 5390 | + \ "available": function("Available"), |
| 5391 | + \ "paste": { |
| 5392 | + \ '+': function("Paste"), |
| 5393 | + \ }, |
| 5394 | + \ "copy": { |
| 5395 | + \ '+': function("Copy"), |
| 5396 | + \ }, |
| 5397 | + \ } |
| 5398 | + set clipmethod=myosc |
5266 | 5399 |
|
| 5400 | +< |
5267 | 5401 | vim:tw=78:ts=8:noet:ft=help:norl: |
0 commit comments