osc52.vim (2938B)
1 vim9script 2 3 export var allowed: bool = false 4 5 export def Available(): bool 6 if has('gui_running') 7 return false 8 endif 9 if get(g:, 'osc52_force_avail', 0) 10 return true 11 endif 12 return allowed 13 enddef 14 15 var sent_message: bool = false 16 17 def OSCMessage(id: number) 18 echo "Waiting for OSC52 response... Press CTRL-C to cancel" 19 sent_message = true 20 enddef 21 22 var loop_timerid: number = -1 23 24 export def Paste(reg: string): any 25 # Check if user has indicated that the terminal does not support OSC 52 paste 26 # (or has disabled it) 27 if get(g:, 'osc52_disable_paste', 0) 28 return ("c", []) 29 endif 30 31 if loop_timerid != -1 32 # This will result in the register being unchanged 33 return null 34 endif 35 36 # Some terminals like Kitty respect the selection type parameter on both X11 37 # and Wayland. If the terminal doesn't then the selection type parameter 38 # should be ignored (no-op) 39 if reg == "+" 40 echoraw("\<Esc>]52;c;?\<Esc>\\") 41 else 42 echoraw("\<Esc>]52;p;?\<Esc>\\") 43 endif 44 45 var timerid: number = timer_start(1000, OSCMessage) 46 var interrupt: bool = false 47 48 # Wait for response from terminal. If we got interrupted (Ctrl-C), then do a 49 # redraw if we already sent the message, and return an empty string. 50 try 51 while true 52 var key: string = getcharstr(-1, {cursor: "hide"}) 53 54 if key == "\<xOSC>" && match(v:termosc, '52;') != -1 55 break 56 elseif key == "\<C-c>" 57 interrupt = true 58 break 59 endif 60 endwhile 61 62 # This doesn't seem to catch Ctrl-C sent via term_sendkeys(), which is used in 63 # tests. So also check the result of getcharstr()/getchar(). 64 catch /^Vim:Interrupt$/ 65 interrupt = true 66 finally 67 timer_stop(timerid) 68 if sent_message 69 sent_message = false 70 :redraw! 71 endif 72 endtry 73 74 # A TextPutPost autocmd may cause this function to be called twice, which is 75 # technically intended behaviour, however it is not necessary for this plugin. 76 # To prevent this, return immediately if we have not returned back to the main 77 # loop since the last "paste" call. 78 loop_timerid = timer_start(0, (_) => { 79 loop_timerid = -1 80 }) 81 82 if interrupt 83 echo "Interrupted while waiting for OSC 52 response" 84 return ("c", [""]) 85 endif 86 87 # Extract the base64 stuff 88 var stuff: string = matchstr(v:termosc, '52;.*;\zs[A-Za-z0-9+/=]*') 89 90 if len(stuff) == 0 91 return ("c", []) 92 endif 93 94 var ret: list<string> 95 96 # "stuff" may be an invalid base64 string, so catch any errors 97 try 98 ret = blob2str(base64_decode(stuff)) 99 catch /E\(475\|1515\)/ 100 echo "Invalid OSC 52 response received" 101 return ("c", [""]) 102 endtry 103 104 return ("", ret) 105 enddef 106 107 export def Copy(reg: string, type: string, lines: list<string>): void 108 if reg == "+" 109 echoraw("\<Esc>]52;c;" .. base64_encode(str2blob(lines)) .. "\<Esc>\\") 110 else 111 echoraw("\<Esc>]52;p;" .. base64_encode(str2blob(lines)) .. "\<Esc>\\") 112 endif 113 enddef 114 115 # vim: set sw=2 sts=2 :