comparison src/register.c @ 19774:00a1b89256ea v8.2.0443

patch 8.2.0443: clipboard code is spread out Commit: https://github.com/vim/vim/commit/45fffdf10b7cb6e59794e76e9b8a2930fcb4b192 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Mar 24 21:42:01 2020 +0100 patch 8.2.0443: clipboard code is spread out Problem: Clipboard code is spread out. Solution: Move clipboard code to its own file. (Yegappan Lakshmanan, closes #5827)
author Bram Moolenaar <Bram@vim.org>
date Tue, 24 Mar 2020 21:45:04 +0100
parents 4a6a412e4565
children 06a1dd50463e
comparison
equal deleted inserted replaced
19773:c8242fe426a7 19774:00a1b89256ea
30 30
31 static int stuff_yank(int, char_u *); 31 static int stuff_yank(int, char_u *);
32 static void put_reedit_in_typebuf(int silent); 32 static void put_reedit_in_typebuf(int silent);
33 static int put_in_typebuf(char_u *s, int esc, int colon, 33 static int put_in_typebuf(char_u *s, int esc, int colon,
34 int silent); 34 int silent);
35 static void free_yank_all(void);
36 static int yank_copy_line(struct block_def *bd, long y_idx); 35 static int yank_copy_line(struct block_def *bd, long y_idx);
37 #ifdef FEAT_CLIPBOARD 36 #ifdef FEAT_CLIPBOARD
38 static void copy_yank_reg(yankreg_T *reg); 37 static void copy_yank_reg(yankreg_T *reg);
39 static void may_set_selection(void);
40 #endif 38 #endif
41 static void dis_msg(char_u *p, int skip_esc); 39 static void dis_msg(char_u *p, int skip_esc);
42 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
43 static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list);
44 #endif
45 40
46 yankreg_T * 41 yankreg_T *
47 get_y_regs(void) 42 get_y_regs(void)
48 { 43 {
49 return y_regs; 44 return y_regs;
50 } 45 }
51 46
52 yankreg_T * 47 yankreg_T *
48 get_y_register(int reg)
49 {
50 return &y_regs[reg];
51 }
52
53 yankreg_T *
53 get_y_current(void) 54 get_y_current(void)
54 { 55 {
55 return y_current; 56 return y_current;
56 } 57 }
57 58
58 yankreg_T * 59 yankreg_T *
59 get_y_previous(void) 60 get_y_previous(void)
60 { 61 {
61 return y_previous; 62 return y_previous;
63 }
64
65 void
66 set_y_current(yankreg_T *yreg)
67 {
68 y_current = yreg;
62 } 69 }
63 70
64 void 71 void
65 set_y_previous(yankreg_T *yreg) 72 set_y_previous(yankreg_T *yreg)
66 { 73 {
238 y_current = &(y_regs[i]); 245 y_current = &(y_regs[i]);
239 if (writing) // remember the register we write into for do_put() 246 if (writing) // remember the register we write into for do_put()
240 y_previous = y_current; 247 y_previous = y_current;
241 return ret; 248 return ret;
242 } 249 }
243
244 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
245 /*
246 * When "regname" is a clipboard register, obtain the selection. If it's not
247 * available return zero, otherwise return "regname".
248 */
249 int
250 may_get_selection(int regname)
251 {
252 if (regname == '*')
253 {
254 if (!clip_star.available)
255 regname = 0;
256 else
257 clip_get_selection(&clip_star);
258 }
259 else if (regname == '+')
260 {
261 if (!clip_plus.available)
262 regname = 0;
263 else
264 clip_get_selection(&clip_plus);
265 }
266 return regname;
267 }
268 #endif
269 250
270 /* 251 /*
271 * Obtain the contents of a "normal" register. The register is made empty. 252 * Obtain the contents of a "normal" register. The register is made empty.
272 * The returned pointer has allocated memory, use put_register() later. 253 * The returned pointer has allocated memory, use put_register() later.
273 */ 254 */
881 return FAIL; 862 return FAIL;
882 } 863 }
883 return OK; 864 return OK;
884 } 865 }
885 866
886 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
887 /*
888 * Adjust the register name pointed to with "rp" for the clipboard being
889 * used always and the clipboard being available.
890 */
891 void
892 adjust_clip_reg(int *rp)
893 {
894 // If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
895 // use '*' or '+' reg, respectively. "unnamedplus" prevails.
896 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
897 {
898 if (clip_unnamed != 0)
899 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
900 ? '+' : '*';
901 else
902 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS)
903 && clip_plus.available) ? '+' : '*';
904 }
905 if (!clip_star.available && *rp == '*')
906 *rp = 0;
907 if (!clip_plus.available && *rp == '+')
908 *rp = 0;
909 }
910 #endif
911
912 /* 867 /*
913 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc. 868 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
914 */ 869 */
915 void 870 void
916 shift_delete_registers() 871 shift_delete_registers()
1048 msg(""); 1003 msg("");
1049 #endif 1004 #endif
1050 } 1005 }
1051 } 1006 }
1052 1007
1053 static void 1008 void
1054 free_yank_all(void) 1009 free_yank_all(void)
1055 { 1010 {
1056 free_yank(y_current->y_size); 1011 free_yank(y_current->y_size);
1057 } 1012 }
1058 1013
2346 msg_outtrans_len(p++, 1); 2301 msg_outtrans_len(p++, 1);
2347 } 2302 }
2348 ui_breakcheck(); 2303 ui_breakcheck();
2349 } 2304 }
2350 2305
2351 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
2352 void
2353 clip_free_selection(Clipboard_T *cbd)
2354 {
2355 yankreg_T *y_ptr = y_current;
2356
2357 if (cbd == &clip_plus)
2358 y_current = &y_regs[PLUS_REGISTER];
2359 else
2360 y_current = &y_regs[STAR_REGISTER];
2361 free_yank_all();
2362 y_current->y_size = 0;
2363 y_current = y_ptr;
2364 }
2365
2366 /*
2367 * Get the selected text and put it in register '*' or '+'.
2368 */
2369 void
2370 clip_get_selection(Clipboard_T *cbd)
2371 {
2372 yankreg_T *old_y_previous, *old_y_current;
2373 pos_T old_cursor;
2374 pos_T old_visual;
2375 int old_visual_mode;
2376 colnr_T old_curswant;
2377 int old_set_curswant;
2378 pos_T old_op_start, old_op_end;
2379 oparg_T oa;
2380 cmdarg_T ca;
2381
2382 if (cbd->owned)
2383 {
2384 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
2385 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
2386 return;
2387
2388 // Get the text between clip_star.start & clip_star.end
2389 old_y_previous = y_previous;
2390 old_y_current = y_current;
2391 old_cursor = curwin->w_cursor;
2392 old_curswant = curwin->w_curswant;
2393 old_set_curswant = curwin->w_set_curswant;
2394 old_op_start = curbuf->b_op_start;
2395 old_op_end = curbuf->b_op_end;
2396 old_visual = VIsual;
2397 old_visual_mode = VIsual_mode;
2398 clear_oparg(&oa);
2399 oa.regname = (cbd == &clip_plus ? '+' : '*');
2400 oa.op_type = OP_YANK;
2401 vim_memset(&ca, 0, sizeof(ca));
2402 ca.oap = &oa;
2403 ca.cmdchar = 'y';
2404 ca.count1 = 1;
2405 ca.retval = CA_NO_ADJ_OP_END;
2406 do_pending_operator(&ca, 0, TRUE);
2407 y_previous = old_y_previous;
2408 y_current = old_y_current;
2409 curwin->w_cursor = old_cursor;
2410 changed_cline_bef_curs(); // need to update w_virtcol et al
2411 curwin->w_curswant = old_curswant;
2412 curwin->w_set_curswant = old_set_curswant;
2413 curbuf->b_op_start = old_op_start;
2414 curbuf->b_op_end = old_op_end;
2415 VIsual = old_visual;
2416 VIsual_mode = old_visual_mode;
2417 }
2418 else if (!is_clipboard_needs_update())
2419 {
2420 clip_free_selection(cbd);
2421
2422 // Try to get selected text from another window
2423 clip_gen_request_selection(cbd);
2424 }
2425 }
2426
2427 /*
2428 * Convert from the GUI selection string into the '*'/'+' register.
2429 */
2430 void
2431 clip_yank_selection(
2432 int type,
2433 char_u *str,
2434 long len,
2435 Clipboard_T *cbd)
2436 {
2437 yankreg_T *y_ptr;
2438
2439 if (cbd == &clip_plus)
2440 y_ptr = &y_regs[PLUS_REGISTER];
2441 else
2442 y_ptr = &y_regs[STAR_REGISTER];
2443
2444 clip_free_selection(cbd);
2445
2446 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
2447 }
2448
2449 /*
2450 * Convert the '*'/'+' register into a GUI selection string returned in *str
2451 * with length *len.
2452 * Returns the motion type, or -1 for failure.
2453 */
2454 int
2455 clip_convert_selection(char_u **str, long_u *len, Clipboard_T *cbd)
2456 {
2457 char_u *p;
2458 int lnum;
2459 int i, j;
2460 int_u eolsize;
2461 yankreg_T *y_ptr;
2462
2463 if (cbd == &clip_plus)
2464 y_ptr = &y_regs[PLUS_REGISTER];
2465 else
2466 y_ptr = &y_regs[STAR_REGISTER];
2467
2468 # ifdef USE_CRNL
2469 eolsize = 2;
2470 # else
2471 eolsize = 1;
2472 # endif
2473
2474 *str = NULL;
2475 *len = 0;
2476 if (y_ptr->y_array == NULL)
2477 return -1;
2478
2479 for (i = 0; i < y_ptr->y_size; i++)
2480 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
2481
2482 // Don't want newline character at end of last line if we're in MCHAR mode.
2483 if (y_ptr->y_type == MCHAR && *len >= eolsize)
2484 *len -= eolsize;
2485
2486 p = *str = alloc(*len + 1); // add one to avoid zero
2487 if (p == NULL)
2488 return -1;
2489 lnum = 0;
2490 for (i = 0, j = 0; i < (int)*len; i++, j++)
2491 {
2492 if (y_ptr->y_array[lnum][j] == '\n')
2493 p[i] = NUL;
2494 else if (y_ptr->y_array[lnum][j] == NUL)
2495 {
2496 # ifdef USE_CRNL
2497 p[i++] = '\r';
2498 # endif
2499 p[i] = '\n';
2500 lnum++;
2501 j = -1;
2502 }
2503 else
2504 p[i] = y_ptr->y_array[lnum][j];
2505 }
2506 return y_ptr->y_type;
2507 }
2508
2509
2510 /*
2511 * If we have written to a clipboard register, send the text to the clipboard.
2512 */
2513 static void
2514 may_set_selection(void)
2515 {
2516 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
2517 {
2518 clip_own_selection(&clip_star);
2519 clip_gen_set_selection(&clip_star);
2520 }
2521 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
2522 {
2523 clip_own_selection(&clip_plus);
2524 clip_gen_set_selection(&clip_plus);
2525 }
2526 }
2527
2528 #endif // FEAT_CLIPBOARD || PROTO
2529
2530
2531 #if defined(FEAT_DND) || defined(PROTO) 2306 #if defined(FEAT_DND) || defined(PROTO)
2532 /* 2307 /*
2533 * Replace the contents of the '~' register with str. 2308 * Replace the contents of the '~' register with str.
2534 */ 2309 */
2535 void 2310 void
2898 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) 2673 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
2899 /* 2674 /*
2900 * Put a string into a register. When the register is not empty, the string 2675 * Put a string into a register. When the register is not empty, the string
2901 * is appended. 2676 * is appended.
2902 */ 2677 */
2903 static void 2678 void
2904 str_to_reg( 2679 str_to_reg(
2905 yankreg_T *y_ptr, // pointer to yank register 2680 yankreg_T *y_ptr, // pointer to yank register
2906 int yank_type, // MCHAR, MLINE, MBLOCK, MAUTO 2681 int yank_type, // MCHAR, MLINE, MBLOCK, MAUTO
2907 char_u *str, // string to put in register 2682 char_u *str, // string to put in register
2908 long len, // length of string 2683 long len, // length of string