robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
7.7 KiB274 linesraw
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
37  yankreg_T *target = NULL;
38  bool explicit_cb_reg = (*name == '*' || *name == '+');
39  bool implicit_cb_reg = (*name == NUL) && (cb_flags & (kOptCbFlagUnnamed | kOptCbFlagUnnamedplus));
40  if (!explicit_cb_reg && !implicit_cb_reg) {
41    goto end;
42  }
43
44  if (!eval_has_provider("clipboard", false)) {
45    if (batch_change_count <= 1 && !quiet
46        && (!clipboard_didwarn || (explicit_cb_reg && !redirecting()))) {
47      clipboard_didwarn = true;
48      // Do NOT error (emsg()) here--if it interrupts :redir we get into
49      // a weird state, stuck in "redirect mode".
50      msg(MSG_NO_CLIP, 0);
51    }
52    // ... else, be silent (don't flood during :while, :redir, etc.).
53    goto end;
54  }
55
56  if (explicit_cb_reg) {
57    target = get_y_register(*name == '*' ? STAR_REGISTER : PLUS_REGISTER);
58    if (writing && (cb_flags & (*name == '*' ? kOptCbFlagUnnamed : kOptCbFlagUnnamedplus))) {
59      clipboard_needs_update = false;
60    }
61    goto end;
62  } else {  // unnamed register: "implicit" clipboard
63    if (writing && clipboard_delay_update) {
64      // For "set" (copy), defer the clipboard call.
65      clipboard_needs_update = true;
66      goto end;
67    } else if (!writing && clipboard_needs_update) {
68      // For "get" (paste), use the internal value.
69      goto end;
70    }
71
72    if (cb_flags & kOptCbFlagUnnamedplus) {
73      *name = (cb_flags & kOptCbFlagUnnamed && writing) ? '"' : '+';
74      target = get_y_register(PLUS_REGISTER);
75    } else {
76      *name = '*';
77      target = get_y_register(STAR_REGISTER);
78    }
79    goto end;
80  }
81
82end:
83  return target;
84}
85
86bool get_clipboard(int name, yankreg_T **target, bool quiet)
87{
88  const bool implicit = name == NUL;  // clipboard=unnamed[plus]
89  // show message on error
90  bool errmsg = true;
91
92  yankreg_T *reg = adjust_clipboard_name(&name, quiet, false);
93  if (reg == NULL) {
94    return false;
95  }
96  if (mc_replaying()) {  // Multicursor cascade: don't read the provider.
97    if (implicit) {
98      return false;  // clipboard=unnamed[plus] behaves as (cursor-local) unnamed register.
99    }
100    *target = reg;  // Explicit "+/"* gets the primary cursor's (cached) clipboard value.
101    return true;
102  }
103  free_register(reg);
104
105  list_T *const args = tv_list_alloc(1);
106  const char regname = (char)name;
107  tv_list_append_string(args, &regname, 1);
108
109  typval_T result = eval_call_provider("clipboard", "get", args, false);
110
111  if (result.v_type != VAR_LIST) {
112    if (result.v_type == VAR_NUMBER && result.vval.v_number == 0) {
113      // failure has already been indicated by provider
114      errmsg = false;
115    }
116    goto err;
117  }
118
119  list_T *res = result.vval.v_list;
120  list_T *lines = NULL;
121  if (tv_list_len(res) == 2
122      && TV_LIST_ITEM_TV(tv_list_first(res))->v_type == VAR_LIST) {
123    lines = TV_LIST_ITEM_TV(tv_list_first(res))->vval.v_list;
124    if (TV_LIST_ITEM_TV(tv_list_last(res))->v_type != VAR_STRING) {
125      goto err;
126    }
127    char *regtype = TV_LIST_ITEM_TV(tv_list_last(res))->vval.v_string;
128    if (regtype == NULL || strlen(regtype) > 1) {
129      goto err;
130    }
131    switch (regtype[0]) {
132    case 0:
133      reg->y_type = kMTUnknown;
134      break;
135    case 'v':
136    case 'c':
137      reg->y_type = kMTCharWise;
138      break;
139    case 'V':
140    case 'l':
141      reg->y_type = kMTLineWise;
142      break;
143    case 'b':
144    case Ctrl_V:
145      reg->y_type = kMTBlockWise;
146      break;
147    default:
148      goto err;
149    }
150  } else {
151    lines = res;
152    // provider did not specify regtype, calculate it below
153    reg->y_type = kMTUnknown;
154  }
155
156  reg->y_array = xcalloc((size_t)tv_list_len(lines), sizeof(String));
157  reg->y_size = (size_t)tv_list_len(lines);
158  reg->y_width = 0;  // Will be updated by update_yankreg_width() below.
159  reg->additional_data = NULL;
160  reg->timestamp = 0;
161  // Timestamp is not saved for clipboard registers because clipboard registers
162  // are not saved in the ShaDa file.
163
164  size_t tv_idx = 0;
165  TV_LIST_ITER_CONST(lines, li, {
166    if (TV_LIST_ITEM_TV(li)->v_type != VAR_STRING) {
167      goto err;
168    }
169    const char *s = TV_LIST_ITEM_TV(li)->vval.v_string;
170    reg->y_array[tv_idx++] = cstr_to_string(s != NULL ? s : "");
171  });
172
173  if (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
176    if (reg->y_type != kMTCharWise) {
177      xfree(reg->y_array[reg->y_size - 1].data);
178      reg->y_size--;
179      if (reg->y_type == kMTUnknown) {
180        reg->y_type = kMTLineWise;
181      }
182    }
183  } else {
184    if (reg->y_type == kMTUnknown) {
185      reg->y_type = kMTCharWise;
186    }
187  }
188
189  update_yankreg_width(reg);
190
191  *target = reg;
192  return true;
193
194err:
195  if (reg->y_array) {
196    for (size_t i = 0; i < reg->y_size; i++) {
197      xfree(reg->y_array[i].data);
198    }
199    xfree(reg->y_array);
200  }
201  reg->y_array = NULL;
202  reg->y_size = 0;
203  reg->additional_data = NULL;
204  reg->timestamp = 0;
205  if (errmsg) {
206    emsg("clipboard: provider returned invalid data");
207  }
208  *target = reg;
209  return false;
210}
211
212void set_clipboard(int name, yankreg_T *reg)
213{
214  if (mc_replaying()  // Multicursor cascade never writes the clipboard.
215      || !adjust_clipboard_name(&name, false, true)) {
216    return;
217  }
218
219  list_T *const lines = tv_list_alloc((ptrdiff_t)reg->y_size + (reg->y_type != kMTCharWise));
220
221  for (size_t i = 0; i < reg->y_size; i++) {
222    tv_list_append_string(lines, reg->y_array[i].data, (int)reg->y_array[i].size);
223  }
224
225  char regtype;
226  switch (reg->y_type) {
227  case kMTLineWise:
228    regtype = 'V';
229    tv_list_append_string(lines, NULL, 0);
230    break;
231  case kMTCharWise:
232    regtype = 'v';
233    break;
234  case kMTBlockWise:
235    regtype = 'b';
236    tv_list_append_string(lines, NULL, 0);
237    break;
238  case kMTUnknown:
239    abort();
240  }
241
242  list_T *args = tv_list_alloc(3);
243  tv_list_append_list(args, lines);
244  tv_list_append_string(args, &regtype, 1);
245  tv_list_append_string(args, ((char[]) { (char)name }), 1);
246
247  eval_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{
253  if (++batch_change_count > 1) {
254    return;
255  }
256  clipboard_delay_update = true;
257}
258
259/// Counterpart to start_batch_changes().
260void end_batch_changes(void)
261{
262  if (--batch_change_count > 0) {
263    // recursive
264    return;
265  }
266  clipboard_delay_update = false;
267  if (clipboard_needs_update) {
268    // must be before, as set_clipboard will invoke
269    // start/end_batch_changes recursively
270    clipboard_needs_update = false;
271    // unnamed ("implicit" clipboard)
272    set_clipboard(NUL, get_y_previous());
273  }
274}