robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// clipboard.c: Functions to handle the clipboard 2 3#include <assert.h> 4 5#include "nvim/api/private/helpers.h" 6#include "nvim/ascii_defs.h" 7#include "nvim/clipboard.h" 8#include "nvim/eval.h" 9#include "nvim/eval/typval.h" 10#include "nvim/mcursor.h" 11#include "nvim/option_vars.h" 12#include "nvim/register.h" 13 14#include "clipboard.c.generated.h" 15 16// for behavior between start_batch_changes() and end_batch_changes()) 17static int batch_change_count = 0 ;// inside a script 18static bool clipboard_delay_update = false;// delay clipboard update 19static bool clipboard_needs_update = false;// clipboard was updated 20static bool clipboard_didwarn = false; 21 22/// Determine if register `*name` should be used as a clipboard. 23/// In an unnamed operation, `*name` is `NUL` and will be adjusted to */+ if 24/// `clipboard=unnamed[plus]` is set. 25/// 26/// @param name The name of register, or `NUL` if unnamed. 27/// @param quiet Suppress error messages 28/// @param writing if we're setting the contents of the clipboard 29/// 30/// @return The yankreg that should be written-to/read-from, or `NULL` if the register isn't 31/// a clipboard or provider isn't available. 32yankreg_T * adjust_clipboard_name (int * name ,bool quiet ,bool writing ) 33{ 34#define MSG_NO_CLIP "clipboard: No provider. " \ 35 "Try \":checkhealth\" or \":h clipboard\"." 36 37yankreg_T * target = NULL ; 38bool explicit_cb_reg = (* name == '*' || * name == '+' ); 39bool implicit_cb_reg = (* name == NUL )&& (cb_flags & (kOptCbFlagUnnamed |kOptCbFlagUnnamedplus )); 40if (!explicit_cb_reg && !implicit_cb_reg ) { 41 gotoend ; 42 } 43 44if (!eval_has_provider ("clipboard" , false)) { 45if (batch_change_count <=1 && !quiet 46&& (!clipboard_didwarn || (explicit_cb_reg && !redirecting ()))) { 47clipboard_didwarn = true; 48// Do NOT error (emsg()) here--if it interrupts :redir we get into 49// a weird state, stuck in "redirect mode". 50msg (MSG_NO_CLIP ,0 ); 51 } 52// ... else, be silent (don't flood during :while, :redir, etc.). 53 gotoend ; 54 } 55 56if (explicit_cb_reg ) { 57target = get_y_register (* name == '*' ?STAR_REGISTER :PLUS_REGISTER ); 58if (writing && (cb_flags & (* name == '*' ?kOptCbFlagUnnamed :kOptCbFlagUnnamedplus ))) { 59clipboard_needs_update = false; 60 } 61 gotoend ; 62 }else {// unnamed register: "implicit" clipboard 63if (writing && clipboard_delay_update ) { 64// For "set" (copy), defer the clipboard call. 65clipboard_needs_update = true; 66 gotoend ; 67 }else if (!writing && clipboard_needs_update ) { 68// For "get" (paste), use the internal value. 69 gotoend ; 70 } 71 72if (cb_flags & kOptCbFlagUnnamedplus ) { 73* name = (cb_flags & kOptCbFlagUnnamed && writing ) ?'"' :'+' ; 74target = get_y_register (PLUS_REGISTER ); 75 }else { 76* name = '*' ; 77target = get_y_register (STAR_REGISTER ); 78 } 79 gotoend ; 80 } 81 82end : 83return target ; 84} 85 86bool get_clipboard (int name ,yankreg_T ** target ,bool quiet ) 87{ 88const bool implicit = name == NUL ;// clipboard=unnamed[plus] 89// show message on error 90bool errmsg = true; 91 92yankreg_T * reg = adjust_clipboard_name (& name ,quiet , false); 93if (reg == NULL ) { 94return false; 95 } 96if (mc_replaying ()) {// Multicursor cascade: don't read the provider. 97if (implicit ) { 98return false;// clipboard=unnamed[plus] behaves as (cursor-local) unnamed register. 99 } 100* target = reg ;// Explicit "+/"* gets the primary cursor's (cached) clipboard value. 101return true; 102 } 103free_register (reg ); 104 105list_T * const args = tv_list_alloc (1 ); 106const char regname = (char )name ; 107tv_list_append_string (args ,& regname ,1 ); 108 109typval_T result = eval_call_provider ("clipboard" ,"get" ,args , false); 110 111if (result .v_type != VAR_LIST ) { 112if (result .v_type == VAR_NUMBER && result .vval .v_number == 0 ) { 113// failure has already been indicated by provider 114errmsg = false; 115 } 116 gotoerr ; 117 } 118 119list_T * res = result .vval .v_list ; 120list_T * lines = NULL ; 121if (tv_list_len (res )== 2 122&& TV_LIST_ITEM_TV (tv_list_first (res ))-> v_type == VAR_LIST ) { 123lines = TV_LIST_ITEM_TV (tv_list_first (res ))-> vval .v_list ; 124if (TV_LIST_ITEM_TV (tv_list_last (res ))-> v_type != VAR_STRING ) { 125 gotoerr ; 126 } 127char * regtype = TV_LIST_ITEM_TV (tv_list_last (res ))-> vval .v_string ; 128if (regtype == NULL || strlen (regtype )> 1 ) { 129 gotoerr ; 130 } 131switch (regtype [0 ]) { 132case 0 : 133reg -> y_type = kMTUnknown ; 134break ; 135case 'v' : 136case 'c' : 137reg -> y_type = kMTCharWise ; 138break ; 139case 'V' : 140case 'l' : 141reg -> y_type = kMTLineWise ; 142break ; 143case 'b' : 144case Ctrl_V : 145reg -> y_type = kMTBlockWise ; 146break ; 147default : 148 gotoerr ; 149 } 150 }else { 151lines = res ; 152// provider did not specify regtype, calculate it below 153reg -> y_type = kMTUnknown ; 154 } 155 156reg -> y_array = xcalloc ((size_t )tv_list_len (lines ),sizeof (String )); 157reg -> y_size = (size_t )tv_list_len (lines ); 158reg -> y_width = 0 ;// Will be updated by update_yankreg_width() below. 159reg -> additional_data = NULL ; 160reg -> timestamp = 0 ; 161// Timestamp is not saved for clipboard registers because clipboard registers 162// are not saved in the ShaDa file. 163 164size_t tv_idx = 0 ; 165TV_LIST_ITER_CONST (lines ,li , { 166if (TV_LIST_ITEM_TV (li )-> v_type != VAR_STRING ) { 167 gotoerr ; 168 } 169const char * s = TV_LIST_ITEM_TV (li )-> vval .v_string ; 170reg -> y_array [tv_idx ++ ]= cstr_to_string (s != NULL ?s :"" ); 171 }); 172 173if (reg -> y_size > 0 && reg -> y_array [reg -> y_size - 1 ].size == 0 ) { 174// a known-to-be charwise yank might have a final linebreak 175// but otherwise there is no line after the final newline 176if (reg -> y_type != kMTCharWise ) { 177xfree (reg -> y_array [reg -> y_size - 1 ].data ); 178reg -> y_size -- ; 179if (reg -> y_type == kMTUnknown ) { 180reg -> y_type = kMTLineWise ; 181 } 182 } 183 }else { 184if (reg -> y_type == kMTUnknown ) { 185reg -> y_type = kMTCharWise ; 186 } 187 } 188 189update_yankreg_width (reg ); 190 191* target = reg ; 192return true; 193 194err : 195if (reg -> y_array ) { 196for (size_t i = 0 ;i < reg -> y_size ;i ++ ) { 197xfree (reg -> y_array [i ].data ); 198 } 199xfree (reg -> y_array ); 200 } 201reg -> y_array = NULL ; 202reg -> y_size = 0 ; 203reg -> additional_data = NULL ; 204reg -> timestamp = 0 ; 205if (errmsg ) { 206emsg ("clipboard: provider returned invalid data" ); 207 } 208* target = reg ; 209return false; 210} 211 212void set_clipboard (int name ,yankreg_T * reg ) 213{ 214if (mc_replaying ()// Multicursor cascade never writes the clipboard. 215|| !adjust_clipboard_name (& name , false, true)) { 216return ; 217 } 218 219list_T * const lines = tv_list_alloc ((ptrdiff_t )reg -> y_size + (reg -> y_type != kMTCharWise )); 220 221for (size_t i = 0 ;i < reg -> y_size ;i ++ ) { 222tv_list_append_string (lines ,reg -> y_array [i ].data , (int )reg -> y_array [i ].size ); 223 } 224 225char regtype ; 226switch (reg -> y_type ) { 227case kMTLineWise : 228regtype = 'V' ; 229tv_list_append_string (lines ,NULL ,0 ); 230break ; 231case kMTCharWise : 232regtype = 'v' ; 233break ; 234case kMTBlockWise : 235regtype = 'b' ; 236tv_list_append_string (lines ,NULL ,0 ); 237break ; 238case kMTUnknown : 239abort (); 240 } 241 242list_T * args = tv_list_alloc (3 ); 243tv_list_append_list (args ,lines ); 244tv_list_append_string (args ,& regtype ,1 ); 245tv_list_append_string (args , ((char []) { (char )name }),1 ); 246 247eval_call_provider ("clipboard" ,"set" ,args , true); 248} 249 250/// Avoid slow things (clipboard) during batch operations (while/for-loops). 251void start_batch_changes (void ) 252{ 253if (++ batch_change_count > 1 ) { 254return ; 255 } 256clipboard_delay_update = true; 257} 258 259/// Counterpart to start_batch_changes(). 260void end_batch_changes (void ) 261{ 262if (-- batch_change_count > 0 ) { 263// recursive 264return ; 265 } 266clipboard_delay_update = false; 267if (clipboard_needs_update ) { 268// must be before, as set_clipboard will invoke 269// start/end_batch_changes recursively 270clipboard_needs_update = false; 271// unnamed ("implicit" clipboard) 272set_clipboard (NUL ,get_y_previous ()); 273 } 274}