robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// ops.c: implementation of various operators: op_shift, op_delete, op_tilde, 2// op_change, op_yank, do_join 3 4#include <assert.h> 5#include <ctype.h> 6#include <inttypes.h> 7#include <limits.h> 8#include <stdbool.h> 9#include <stdio.h> 10#include <stdlib.h> 11#include <string.h> 12#include <uv.h> 13 14#include "nvim/api/private/defs.h" 15#include "nvim/api/private/helpers.h" 16#include "nvim/ascii_defs.h" 17#include "nvim/assert_defs.h" 18#include "nvim/autocmd.h" 19#include "nvim/autocmd_defs.h" 20#include "nvim/buffer.h" 21#include "nvim/buffer_defs.h" 22#include "nvim/buffer_updates.h" 23#include "nvim/change.h" 24#include "nvim/charset.h" 25#include "nvim/clipboard.h" 26#include "nvim/cursor.h" 27#include "nvim/drawscreen.h" 28#include "nvim/errors.h" 29#include "nvim/eval.h" 30#include "nvim/eval/typval.h" 31#include "nvim/eval/typval_defs.h" 32#include "nvim/ex_cmds2.h" 33#include "nvim/ex_cmds_defs.h" 34#include "nvim/ex_docmd.h" 35#include "nvim/ex_getln.h" 36#include "nvim/extmark.h" 37#include "nvim/file_search.h" 38#include "nvim/fold.h" 39#include "nvim/garray.h" 40#include "nvim/garray_defs.h" 41#include "nvim/gettext_defs.h" 42#include "nvim/globals.h" 43#include "nvim/highlight_defs.h" 44#include "nvim/indent.h" 45#include "nvim/indent_c.h" 46#include "nvim/input.h" 47#include "nvim/input_cmdatom.h" 48#include "nvim/input_defs.h" 49#include "nvim/insert.h" 50#include "nvim/keycodes.h" 51#include "nvim/macros_defs.h" 52#include "nvim/mark.h" 53#include "nvim/mark_defs.h" 54#include "nvim/math.h" 55#include "nvim/mbyte.h" 56#include "nvim/mbyte_defs.h" 57#include "nvim/mcursor.h" 58#include "nvim/memline.h" 59#include "nvim/memline_defs.h" 60#include "nvim/memory.h" 61#include "nvim/message.h" 62#include "nvim/mouse.h" 63#include "nvim/move.h" 64#include "nvim/normal.h" 65#include "nvim/ops.h" 66#include "nvim/option.h" 67#include "nvim/option_defs.h" 68#include "nvim/option_vars.h" 69#include "nvim/os/input.h" 70#include "nvim/os/time.h" 71#include "nvim/plines.h" 72#include "nvim/register.h" 73#include "nvim/search.h" 74#include "nvim/state.h" 75#include "nvim/state_defs.h" 76#include "nvim/strings.h" 77#include "nvim/terminal.h" 78#include "nvim/textformat.h" 79#include "nvim/types_defs.h" 80#include "nvim/ui.h" 81#include "nvim/ui_defs.h" 82#include "nvim/undo.h" 83#include "nvim/vim_defs.h" 84 85#include "ops.c.generated.h" 86 87// Flags for third item in "opchars". 88#define OPF_LINES 1 // operator always works on lines 89#define OPF_CHANGE 2 // operator changes text 90 91/// The names of operators. 92/// IMPORTANT: Index must correspond with defines in ops.h!!! 93/// The third field indicates whether the operator always works on lines. 94static const char opchars [][3 ]= { 95 {NUL ,NUL ,0 },// OP_NOP 96 {'d' ,NUL ,OPF_CHANGE },// OP_DELETE 97 {'y' ,NUL ,0 },// OP_YANK 98 {'c' ,NUL ,OPF_CHANGE },// OP_CHANGE 99 {'<' ,NUL ,OPF_LINES |OPF_CHANGE },// OP_LSHIFT 100 {'>' ,NUL ,OPF_LINES |OPF_CHANGE },// OP_RSHIFT 101 {'!' ,NUL ,OPF_LINES |OPF_CHANGE },// OP_FILTER 102 {'g' ,'~' ,OPF_CHANGE },// OP_TILDE 103 {'=' ,NUL ,OPF_LINES |OPF_CHANGE },// OP_INDENT 104 {'g' ,'q' ,OPF_LINES |OPF_CHANGE },// OP_FORMAT 105 {':' ,NUL ,OPF_LINES },// OP_COLON 106 {'g' ,'U' ,OPF_CHANGE },// OP_UPPER 107 {'g' ,'u' ,OPF_CHANGE },// OP_LOWER 108 {'J' ,NUL ,OPF_LINES |OPF_CHANGE },// DO_JOIN 109 {'g' ,'J' ,OPF_LINES |OPF_CHANGE },// DO_JOIN_NS 110 {'g' ,'?' ,OPF_CHANGE },// OP_ROT13 111 {'r' ,NUL ,OPF_CHANGE },// OP_REPLACE 112 {'I' ,NUL ,OPF_CHANGE },// OP_INSERT 113 {'A' ,NUL ,OPF_CHANGE },// OP_APPEND 114 {'z' ,'f' ,0 },// OP_FOLD 115 {'z' ,'o' ,OPF_LINES },// OP_FOLDOPEN 116 {'z' ,'O' ,OPF_LINES },// OP_FOLDOPENREC 117 {'z' ,'c' ,OPF_LINES },// OP_FOLDCLOSE 118 {'z' ,'C' ,OPF_LINES },// OP_FOLDCLOSEREC 119 {'z' ,'d' ,OPF_LINES },// OP_FOLDDEL 120 {'z' ,'D' ,OPF_LINES },// OP_FOLDDELREC 121 {'g' ,'w' ,OPF_LINES |OPF_CHANGE },// OP_FORMAT2 122 {'g' ,'@' ,OPF_CHANGE },// OP_FUNCTION 123 {Ctrl_A ,NUL ,OPF_CHANGE },// OP_NR_ADD 124 {Ctrl_X ,NUL ,OPF_CHANGE },// OP_NR_SUB 125 {'z' ,'q' ,0 },// OP_MCURSOR 126}; 127 128/// Translate a command name into an operator type. 129/// Must only be called with a valid operator name! 130int get_op_type (int char1 ,int char2 ) 131{ 132int i ; 133 134if (char1 == 'r' ) { 135// ignore second character 136return OP_REPLACE ; 137 } 138if (char1 == '~' ) { 139// when tilde is an operator 140return OP_TILDE ; 141 } 142if (char1 == 'g' && char2 == Ctrl_A ) { 143// add 144return OP_NR_ADD ; 145 } 146if (char1 == 'g' && char2 == Ctrl_X ) { 147// subtract 148return OP_NR_SUB ; 149 } 150if (char1 == 'z' && char2 == 'y' ) {// OP_YANK 151return OP_YANK ; 152 } 153for (i = 0 ;;i ++ ) { 154if (opchars [i ][0 ]== char1 && opchars [i ][1 ]== char2 ) { 155break ; 156 } 157if (i == (int )(ARRAY_SIZE (opchars )- 1 )) { 158internal_error ("get_op_type()" ); 159break ; 160 } 161 } 162return i ; 163} 164 165/// @return true if operator "op" always works on whole lines. 166int op_on_lines (int op ) 167{ 168return opchars [op ][2 ]& OPF_LINES ; 169} 170 171/// @return true if operator "op" changes text. 172int op_is_change (int op ) 173{ 174return opchars [op ][2 ]& OPF_CHANGE ; 175} 176 177/// Whether operator `op` builds a redo: text-changing operators do, ":" and the fold operators do 178/// not. Yank with the 'y' flag in 'cpoptions'. 179bool op_redoable (int op ,bool redo_yank ) 180{ 181return op == OP_YANK ?redo_yank :op_is_change (op ); 182} 183 184/// Get first operator command character. 185/// 186/// @return 'g' or 'z' if there is another command character. 187int get_op_char (int optype ) 188{ 189return opchars [optype ][0 ]; 190} 191 192/// Get second operator command character. 193int get_extra_op_char (int optype ) 194{ 195return opchars [optype ][1 ]; 196} 197 198/// handle a shift operation 199void op_shift (oparg_T * oap ,bool curs_top ,int amount ) 200{ 201int block_col = 0 ; 202 203if (u_save ((linenr_T )(oap -> start .lnum - 1 ), 204 (linenr_T )(oap -> end .lnum + 1 ))== FAIL ) { 205return ; 206 } 207 208if (oap -> motion_type == kMTBlockWise ) { 209block_col = curwin -> w_cursor .col ; 210 } 211 212for (int i = oap -> line_count - 1 ;i >=0 ;i -- ) { 213int first_char = (uint8_t )(* get_cursor_line_ptr ()); 214if (first_char == NUL ) {// empty line 215curwin -> w_cursor .col = 0 ; 216 }else if (oap -> motion_type == kMTBlockWise ) { 217shift_block (oap ,amount ); 218 }else if (first_char != '#' || !preprocs_left ()) { 219// Move the line right if it doesn't start with '#', 'smartindent' 220// isn't set or 'cindent' isn't set or '#' isn't in 'cino'. 221shift_line (oap -> op_type == OP_LSHIFT ,p_sr ,amount , false); 222 } 223curwin -> w_cursor .lnum ++ ; 224 } 225 226if (oap -> motion_type == kMTBlockWise ) { 227curwin -> w_cursor .lnum = oap -> start .lnum ; 228curwin -> w_cursor .col = block_col ; 229 }else if (curs_top ) {// put cursor on first line, for ">>" 230curwin -> w_cursor .lnum = oap -> start .lnum ; 231beginline (BL_SOL |BL_FIX );// shift_line() may have set cursor.col 232 }else { 233curwin -> w_cursor .lnum -- ;// put cursor on last line, for ":>" 234 } 235// The cursor line is not in a closed fold 236foldOpenCursor (); 237 238if (oap -> line_count > p_report ) { 239char * op = oap -> op_type == OP_RSHIFT ?">" :"<" ; 240 241char * msg_line_single = NGETTEXT ("%" PRId64 " line %sed %d time" , 242"%" PRId64 " line %sed %d times" ,amount ); 243char * msg_line_plural = NGETTEXT ("%" PRId64 " lines %sed %d time" , 244"%" PRId64 " lines %sed %d times" ,amount ); 245vim_snprintf (IObuff ,IOSIZE , 246NGETTEXT (msg_line_single ,msg_line_plural ,oap -> line_count ), 247 (int64_t )oap -> line_count ,op ,amount ); 248msg_keep (IObuff ,0 , true, false); 249 } 250 251if ((cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 252// Set "'[" and "']" marks. 253curbuf -> b_op_start = oap -> start ; 254curbuf -> b_op_end .lnum = oap -> end .lnum ; 255curbuf -> b_op_end .col = ml_get_len (oap -> end .lnum ); 256if (curbuf -> b_op_end .col > 0 ) { 257curbuf -> b_op_end .col -- ; 258 } 259 } 260 261changed_lines (curbuf ,oap -> start .lnum ,0 ,oap -> end .lnum + 1 ,0 , true); 262} 263 264/// Return the tabstop width at the index of the variable tabstop array. If an 265/// index greater than the length of the array is given, the last tabstop width 266/// in the array is returned. 267static int get_vts (const int * vts_array ,int index ) 268{ 269int ts ; 270 271if (index < 1 ) { 272ts = 0 ; 273 }else if (index <=vts_array [0 ]) { 274ts = vts_array [index ]; 275 }else { 276ts = vts_array [vts_array [0 ]]; 277 } 278 279return ts ; 280} 281 282/// Return the sum of all the tabstops through the index-th. 283static int get_vts_sum (const int * vts_array ,int index ) 284{ 285int sum = 0 ; 286int i ; 287 288// Perform the summation for indices within the actual array. 289for (i = 1 ;i <=index && i <=vts_array [0 ];i ++ ) { 290sum += vts_array [i ]; 291 } 292 293// Add tabstops whose indices exceed the actual array. 294if (i <=index ) { 295sum += vts_array [vts_array [0 ]]* (index - vts_array [0 ]); 296 } 297 298return sum ; 299} 300 301/// @param left true if shift is to the left 302/// @param count true if new indent is to be to a tabstop 303/// @param amount number of shifts 304static int64_t get_new_sw_indent (bool left ,bool round ,int64_t amount ,int64_t sw_val ) 305{ 306int64_t count = get_indent ();// get current indent 307 308if (round ) {// round off indent 309int64_t i = trim_to_int (count /sw_val );// number of 'shiftwidth' rounded down 310int64_t j = trim_to_int (count %sw_val );// extra spaces 311if (j && left ) {// first remove extra spaces 312amount -- ; 313 } 314if (left ) { 315i = MAX (i - amount ,0 ); 316 }else { 317i += amount ; 318 } 319count = i * sw_val ; 320 }else {// original vi indent 321if (left ) { 322count = MAX (count - sw_val * amount ,0 ); 323 }else { 324count += sw_val * amount ; 325 } 326 } 327 328return count ; 329} 330 331/// @param left true if shift is to the left 332/// @param count true if new indent is to be to a tabstop 333/// @param amount number of shifts 334static int64_t get_new_vts_indent (bool left ,bool round ,int amount ,int * vts_array ) 335{ 336int64_t indent = get_indent (); 337int vtsi = 0 ; 338int vts_indent = 0 ; 339int ts = 0 ;// Silence uninitialized variable warning. 340 341// Find the tabstop at or to the left of the current indent. 342while (vts_indent <=indent ) { 343vtsi ++ ; 344ts = get_vts (vts_array ,vtsi ); 345vts_indent += ts ; 346 } 347vts_indent -= ts ; 348vtsi -- ; 349 350// Extra indent spaces to the right of the tabstop 351int64_t offset = indent - vts_indent ; 352 353if (round ) { 354if (left ) { 355if (offset == 0 ) { 356indent = get_vts_sum (vts_array ,vtsi - amount ); 357 }else { 358indent = get_vts_sum (vts_array ,vtsi - (amount - 1 )); 359 } 360 }else { 361indent = get_vts_sum (vts_array ,vtsi + amount ); 362 } 363 }else { 364if (left ) { 365if (amount > vtsi ) { 366indent = 0 ; 367 }else { 368indent = get_vts_sum (vts_array ,vtsi - amount )+ offset ; 369 } 370 }else { 371indent = get_vts_sum (vts_array ,vtsi + amount )+ offset ; 372 } 373 } 374 375return indent ; 376} 377 378/// Shift the current line 'amount' shiftwidth(s) left (if 'left' is true) or 379/// right. 380/// 381/// The rules for choosing a shiftwidth are: If 'shiftwidth' is non-zero, use 382/// 'shiftwidth'; else if 'vartabstop' is not empty, use 'vartabstop'; else use 383/// 'tabstop'. The Vim documentation says nothing about 'softtabstop' or 384/// 'varsofttabstop' affecting the shiftwidth, and neither affects the 385/// shiftwidth in current versions of Vim, so they are not considered here. 386/// 387/// @param left true if shift is to the left 388/// @param count true if new indent is to be to a tabstop 389/// @param amount number of shifts 390/// @param call_changed_bytes call changed_bytes() 391void shift_line (bool left ,bool round ,int amount ,int call_changed_bytes ) 392{ 393int64_t count ; 394int64_t sw_val = curbuf -> b_p_sw ; 395int64_t ts_val = curbuf -> b_p_ts ; 396int * vts_array = curbuf -> b_p_vts_array ; 397 398if (sw_val != 0 ) { 399// 'shiftwidth' is not zero; use it as the shift size. 400count = get_new_sw_indent (left ,round ,amount ,sw_val ); 401 }else if ((vts_array == NULL )|| (vts_array [0 ]== 0 )) { 402// 'shiftwidth' is zero and 'vartabstop' is empty; use 'tabstop' as the 403// shift size. 404count = get_new_sw_indent (left ,round ,amount ,ts_val ); 405 }else { 406// 'shiftwidth' is zero and 'vartabstop' is defined; use 'vartabstop' 407// to determine the new indent. 408count = get_new_vts_indent (left ,round ,amount ,vts_array ); 409 } 410 411// Set new indent 412if (State & VREPLACE_FLAG ) { 413change_indent (INDENT_SET ,trim_to_int (count ), false,call_changed_bytes ); 414 }else { 415set_indent (trim_to_int (count ),call_changed_bytes ?SIN_CHANGED :0 ); 416 } 417} 418 419/// Shift one line of the current block one shiftwidth right or left. 420/// Leaves cursor on first character in block. 421static void shift_block (oparg_T * oap ,int amount ) 422{ 423const bool left = (oap -> op_type == OP_LSHIFT ); 424const int oldstate = State ; 425char * newp ; 426const int oldcol = curwin -> w_cursor .col ; 427const int sw_val = get_sw_value_indent (curbuf ,left ); 428const int ts_val = (int )curbuf -> b_p_ts ; 429struct block_def bd ; 430int incr ; 431const int old_p_ri = p_ri ; 432 433p_ri = 0 ;// don't want revins in indent 434 435State = MODE_INSERT ;// don't want MODE_REPLACE for State 436block_prep (oap ,& bd ,curwin -> w_cursor .lnum , true); 437if (bd .is_short ) { 438return ; 439 } 440 441// total is number of screen columns to be inserted/removed 442int total = (int )((unsigned )amount * (unsigned )sw_val ); 443if ((total /sw_val )!= amount ) { 444return ;// multiplication overflow 445 } 446 447char * const oldp = get_cursor_line_ptr (); 448const int old_line_len = get_cursor_line_len (); 449 450int startcol ,oldlen ,newlen ; 451 452if (!left ) { 453// 1. Get start vcol 454// 2. Total ws vcols 455// 3. Divvy into TABs & spp 456// 4. Construct new string 457total += bd .pre_whitesp ;// all virtual WS up to & incl a split TAB 458colnr_T ws_vcol = bd .start_vcol - bd .pre_whitesp ; 459char * old_textstart = bd .textstart ; 460if (bd .startspaces ) { 461if (utfc_ptr2len (bd .textstart )== 1 ) { 462bd .textstart ++ ; 463 }else { 464ws_vcol = 0 ; 465bd .startspaces = 0 ; 466 } 467 } 468 469// TODO(vim): is passing bd.textstart for start of the line OK? 470CharsizeArg csarg ; 471CSType cstype = init_charsize_arg (& csarg ,curwin ,curwin -> w_cursor .lnum ,bd .textstart ); 472StrCharInfo ci = utf_ptr2StrCharInfo (bd .textstart ); 473int vcol = bd .start_vcol ; 474while (ascii_iswhite (ci .chr .value )) { 475ClusterInfo cli = utf_ClusterInfo (ci ); 476incr = win_charsize (cstype ,vcol ,ci .ptr ,ci .chr .value ,& csarg ,cli .cells ).width ; 477ci = cli .next ; 478total += incr ; 479vcol += incr ; 480 } 481bd .textstart = ci .ptr ; 482bd .start_vcol = vcol ; 483 484int tabs = 0 ; 485int spaces = 0 ; 486// OK, now total=all the VWS reqd, and textstart points at the 1st 487// non-ws char in the block. 488if (!curbuf -> b_p_et ) { 489tabstop_fromto (ws_vcol ,ws_vcol + total , 490ts_val ,curbuf -> b_p_vts_array ,& tabs ,& spaces ); 491 }else { 492spaces = total ; 493 } 494 495// if we're splitting a TAB, allow for it 496const int col_pre = bd .pre_whitesp_c - (bd .startspaces != 0 ); 497bd .textcol -= col_pre ; 498 499const int new_line_len // the length of the line after the block shift 500= bd .textcol + tabs + spaces + (old_line_len - (int )(bd .textstart - oldp )); 501newp = xmalloc ((size_t )new_line_len + 1 ); 502memmove (newp ,oldp , (size_t )bd .textcol ); 503startcol = bd .textcol ; 504oldlen = (int )(bd .textstart - old_textstart )+ col_pre ; 505newlen = tabs + spaces ; 506memset (newp + bd .textcol ,TAB , (size_t )tabs ); 507memset (newp + bd .textcol + tabs ,' ' , (size_t )spaces ); 508STRCPY (newp + bd .textcol + tabs + spaces ,bd .textstart ); 509assert (newlen - oldlen == new_line_len - old_line_len ); 510 }else {// left 511char * verbatim_copy_end ;// end of the part of the line which is 512// copied verbatim 513colnr_T verbatim_copy_width ;// the (displayed) width of this part 514// of line 515char * non_white = bd .textstart ; 516 517// Firstly, let's find the first non-whitespace character that is 518// displayed after the block's start column and the character's column 519// number. Also, let's calculate the width of all the whitespace 520// characters that are displayed in the block and precede the searched 521// non-whitespace character. 522 523// If "bd.startspaces" is set, "bd.textstart" points to the character, 524// the part of which is displayed at the block's beginning. Let's start 525// searching from the next character. 526if (bd .startspaces ) { 527MB_PTR_ADV (non_white ); 528 } 529 530// The character's column is in "bd.start_vcol". 531colnr_T non_white_col = bd .start_vcol ; 532 533CharsizeArg csarg ; 534CSType cstype = init_charsize_arg (& csarg ,curwin ,curwin -> w_cursor .lnum ,bd .textstart ); 535while (ascii_iswhite (* non_white )) { 536incr = win_charsize (cstype ,non_white_col ,non_white , (uint8_t )(* non_white ),& csarg ,1 ).width ; 537non_white_col += incr ; 538non_white ++ ; 539 } 540 541const colnr_T block_space_width = non_white_col - oap -> start_vcol ; 542// We will shift by "total" or "block_space_width", whichever is less. 543const colnr_T shift_amount = MIN (block_space_width ,total ); 544// The column to which we will shift the text. 545const colnr_T destination_col = non_white_col - shift_amount ; 546 547// Now let's find out how much of the beginning of the line we can 548// reuse without modification. 549verbatim_copy_end = bd .textstart ; 550verbatim_copy_width = bd .start_vcol ; 551 552// If "bd.startspaces" is set, "bd.textstart" points to the character 553// preceding the block. We have to subtract its width to obtain its 554// column number. 555if (bd .startspaces ) { 556verbatim_copy_width -= bd .start_char_vcols ; 557 } 558cstype = init_charsize_arg (& csarg ,curwin ,0 ,bd .textstart ); 559StrCharInfo ci = utf_ptr2StrCharInfo (verbatim_copy_end ); 560while (verbatim_copy_width < destination_col ) { 561ClusterInfo cli = utf_ClusterInfo (ci ); 562incr = win_charsize (cstype ,verbatim_copy_width ,ci .ptr ,ci .chr .value ,& csarg , 563cli .cells ).width ; 564if (verbatim_copy_width + incr > destination_col ) { 565break ; 566 } 567verbatim_copy_width += incr ; 568ci = cli .next ; 569 } 570verbatim_copy_end = ci .ptr ; 571 572// If "destination_col" is different from the width of the initial 573// part of the line that will be copied, it means we encountered a tab 574// character, which we will have to partly replace with spaces. 575assert (destination_col - verbatim_copy_width >=0 ); 576const int fill // nr of spaces that replace a TAB 577= destination_col - verbatim_copy_width ; 578 579assert (verbatim_copy_end - oldp >=0 ); 580// length of string left of the shift position (ie the string not being shifted) 581const int fixedlen = (int )(verbatim_copy_end - oldp ); 582// The replacement line will consist of: 583// - the beginning of the original line up to "verbatim_copy_end", 584// - "fill" number of spaces, 585// - the rest of the line, pointed to by non_white. 586const int new_line_len // the length of the line after the block shift 587= fixedlen + fill + (old_line_len - (int )(non_white - oldp )); 588 589newp = xmalloc ((size_t )new_line_len + 1 ); 590startcol = fixedlen ; 591oldlen = bd .textcol + (int )(non_white - bd .textstart )- fixedlen ; 592newlen = fill ; 593memmove (newp ,oldp , (size_t )fixedlen ); 594memset (newp + fixedlen ,' ' , (size_t )fill ); 595STRCPY (newp + fixedlen + fill ,non_white ); 596assert (newlen - oldlen == new_line_len - old_line_len ); 597 } 598// replace the line 599ml_replace (curwin -> w_cursor .lnum ,newp , false); 600changed_bytes (curwin -> w_cursor .lnum ,bd .textcol ); 601extmark_splice_cols (curbuf , (int )curwin -> w_cursor .lnum - 1 ,startcol , 602oldlen ,newlen , 603kExtmarkUndo ); 604State = oldstate ; 605curwin -> w_cursor .col = oldcol ; 606p_ri = old_p_ri ; 607} 608 609/// Insert string "s" (b_insert ? before : after) block :AKelly 610/// Caller must prepare for undo. 611static void block_insert (oparg_T * oap ,const char * s ,size_t slen ,bool b_insert , 612struct block_def * bdp ) 613{ 614int ts_val ; 615int count = 0 ;// extra spaces to replace a cut TAB 616int spaces = 0 ;// non-zero if cutting a TAB 617colnr_T offset ;// pointer along new line 618char * newp ,* oldp ;// new, old lines 619int oldstate = State ; 620State = MODE_INSERT ;// don't want MODE_REPLACE for State 621 622for (linenr_T lnum = oap -> start .lnum + 1 ;lnum <=oap -> end .lnum ;lnum ++ ) { 623block_prep (oap ,bdp ,lnum , true); 624if (bdp -> is_short && b_insert ) { 625continue ;// OP_INSERT, line ends before block start 626 } 627 628oldp = ml_get (lnum ); 629 630if (b_insert ) { 631ts_val = bdp -> start_char_vcols ; 632spaces = bdp -> startspaces ; 633if (spaces != 0 ) { 634count = ts_val - 1 ;// we're cutting a TAB 635 } 636offset = bdp -> textcol ; 637 }else {// append 638ts_val = bdp -> end_char_vcols ; 639if (!bdp -> is_short ) {// spaces = padding after block 640spaces = (bdp -> endspaces ?ts_val - bdp -> endspaces :0 ); 641if (spaces != 0 ) { 642count = ts_val - 1 ;// we're cutting a TAB 643 } 644offset = bdp -> textcol + bdp -> textlen - (spaces != 0 ); 645 }else {// spaces = padding to block edge 646// if $ used, just append to EOL (ie spaces==0) 647if (!bdp -> is_MAX ) { 648spaces = (oap -> end_vcol - bdp -> end_vcol )+ 1 ; 649 } 650count = spaces ; 651offset = bdp -> textcol + bdp -> textlen ; 652 } 653 } 654 655if (spaces > 0 ) { 656// avoid copying part of a multi-byte character 657offset -= utf_head_off (oldp ,oldp + offset ); 658 } 659spaces = MAX (spaces ,0 );// can happen when the cursor was moved 660 661assert (count >=0 ); 662// Make sure the allocated size matches what is actually copied below. 663newp = xmalloc ((size_t )ml_get_len (lnum )+ (size_t )spaces + slen 664+ (spaces > 0 && !bdp -> is_short ? (size_t )(ts_val - spaces ) :0 ) 665+ (size_t )count + 1 ); 666 667// copy up to shifted part 668memmove (newp ,oldp , (size_t )offset ); 669oldp += offset ; 670int startcol = offset ; 671 672// insert pre-padding 673memset (newp + offset ,' ' , (size_t )spaces ); 674 675// copy the new text 676memmove (newp + offset + spaces ,s ,slen ); 677offset += (int )slen ; 678 679int skipped = 0 ; 680if (spaces > 0 && !bdp -> is_short ) { 681if (* oldp == TAB ) { 682// insert post-padding 683memset (newp + offset + spaces ,' ' , (size_t )(ts_val - spaces )); 684// We're splitting a TAB, don't copy it. 685oldp ++ ; 686// We allowed for that TAB, remember this now 687count ++ ; 688skipped = 1 ; 689 }else { 690// Not a TAB, no extra spaces 691count = spaces ; 692 } 693 } 694 695if (spaces > 0 ) { 696offset += count ; 697 } 698STRCPY (newp + offset ,oldp ); 699 700ml_replace (lnum ,newp , false); 701extmark_splice_cols (curbuf , (int )lnum - 1 ,startcol , 702skipped ,offset - startcol ,kExtmarkUndo ); 703 704if (lnum == oap -> end .lnum ) { 705// Set "']" mark to the end of the block instead of the end of 706// the insert in the first line. 707curbuf -> b_op_end .lnum = oap -> end .lnum ; 708curbuf -> b_op_end .col = offset ; 709if (curbuf -> b_visual .vi_end .coladd ) { 710curbuf -> b_visual .vi_end .col += curbuf -> b_visual .vi_end .coladd ; 711curbuf -> b_visual .vi_end .coladd = 0 ; 712 } 713 } 714 }// for all lnum 715 716State = oldstate ; 717 718// Only call changed_lines if we actually modified additional lines beyond the first 719// This matches the condition for the for loop above: start + 1 <= end 720if (oap -> start .lnum < oap -> end .lnum ) { 721changed_lines (curbuf ,oap -> start .lnum + 1 ,0 ,oap -> end .lnum + 1 ,0 , true); 722 } 723} 724 725/// Handle a delete operation. 726/// 727/// @return FAIL if undo failed, OK otherwise. 728int op_delete (oparg_T * oap ) 729{ 730linenr_T lnum ; 731struct block_def bd = {0 }; 732linenr_T old_lcount = curbuf -> b_ml .ml_line_count ; 733 734if (curbuf -> b_ml .ml_flags & ML_EMPTY ) {// nothing to do 735return OK ; 736 } 737 738// Nothing to delete, return here. Do prepare undo, for op_change(). 739if (oap -> empty ) { 740return u_save_cursor (); 741 } 742 743if (!MODIFIABLE (curbuf )) { 744emsg (_ (e_modifiable )); 745return FAIL ; 746 } 747 748if (Visual .select && oap -> is_VIsual ) { 749// Use the register given with CTRL_R, defaults to zero 750oap -> regname = Visual .select_reg ; 751 } 752 753mb_adjust_opend (oap ); 754 755// Imitate the strange Vi behaviour: If the delete spans more than one 756// line and motion_type == kMTCharWise and the result is a blank line, make the 757// delete linewise. Don't do this for the change command or Visual mode. 758if (oap -> motion_type == kMTCharWise 759&& !oap -> is_VIsual 760&& oap -> line_count > 1 761&& oap -> motion_force == NUL 762&& oap -> op_type == OP_DELETE ) { 763char * ptr = ml_get (oap -> end .lnum )+ oap -> end .col ; 764if (* ptr != NUL ) { 765ptr += oap -> inclusive ; 766 } 767ptr = skipwhite (ptr ); 768if (* ptr == NUL && inindent (0 )) { 769oap -> motion_type = kMTLineWise ; 770 } 771 } 772 773// Check for trying to delete (e.g. "D") in an empty line. 774// Note: For the change operator it is ok. 775if (oap -> motion_type != kMTLineWise 776&& oap -> line_count == 1 777&& oap -> op_type == OP_DELETE 778&& * ml_get (oap -> start .lnum )== NUL ) { 779// It's an error to operate on an empty region, when 'E' included in 780// 'cpoptions' (Vi compatible). 781if (virtual_op ) { 782// Virtual editing: Nothing gets deleted, but we set the '[ and '] 783// marks as if it happened. 784 gotosetmarks ; 785 } 786if (vim_strchr (p_cpo ,kCpoEmptyregion )!= NULL ) { 787beep_flush (); 788 } 789return OK ; 790 } 791 792// Do a yank of whatever we're about to delete. 793// If a yank register was specified, put the deleted text into that 794// register. For the black hole register '_' don't yank anything. 795if (oap -> regname != '_' ) { 796yankreg_T * reg = NULL ; 797bool did_yank = false; 798if (oap -> regname != 0 ) { 799// check for read-only register 800if (!valid_yank_reg (oap -> regname , true)) { 801beep_flush (); 802return OK ; 803 } 804reg = get_yank_register (oap -> regname ,YREG_YANK );// yank into specif'd reg 805op_yank_reg (oap , false,reg ,is_append_register (oap -> regname ));// yank without message 806did_yank = true; 807 } 808 809// Put deleted text into register 1 and shift number registers if the 810// delete contains a line break, or when using a specific operator (Vi 811// compatible) 812 813if (oap -> motion_type == kMTLineWise || oap -> line_count > 1 || oap -> use_reg_one ) { 814shift_delete_registers (is_append_register (oap -> regname )); 815reg = get_y_register (1 ); 816op_yank_reg (oap , false,reg , false); 817did_yank = true; 818 } 819 820// Yank into small delete register when no named register specified 821// and the delete is within one line. 822if (oap -> regname == 0 && oap -> motion_type != kMTLineWise 823&& oap -> line_count == 1 ) { 824reg = get_yank_register ('-' ,YREG_YANK ); 825op_yank_reg (oap , false,reg , false); 826did_yank = true; 827 } 828 829if (did_yank || oap -> regname == 0 ) { 830if (reg == NULL ) { 831abort (); 832 } 833set_clipboard (oap -> regname ,reg ); 834do_autocmd_textyankpost (oap ,reg ); 835 } 836 } 837 838// block mode delete 839if (oap -> motion_type == kMTBlockWise ) { 840if (u_save ((linenr_T )(oap -> start .lnum - 1 ), 841 (linenr_T )(oap -> end .lnum + 1 ))== FAIL ) { 842return FAIL ; 843 } 844 845for (lnum = curwin -> w_cursor .lnum ;lnum <=oap -> end .lnum ;lnum ++ ) { 846block_prep (oap ,& bd ,lnum , true); 847if (bd .textlen == 0 ) {// nothing to delete 848continue ; 849 } 850 851// Adjust cursor position for tab replaced by spaces and 'lbr'. 852if (lnum == curwin -> w_cursor .lnum ) { 853curwin -> w_cursor .col = bd .textcol + bd .startspaces ; 854curwin -> w_cursor .coladd = 0 ; 855 } 856 857// "n" == number of chars deleted 858// If we delete a TAB, it may be replaced by several characters. 859// Thus the number of characters may increase! 860int n = bd .textlen - bd .startspaces - bd .endspaces ; 861char * oldp = ml_get (lnum ); 862char * newp = xmalloc ((size_t )ml_get_len (lnum )- (size_t )n + 1 ); 863// copy up to deleted part 864memmove (newp ,oldp , (size_t )bd .textcol ); 865// insert spaces 866memset (newp + bd .textcol ,' ' , (size_t )bd .startspaces + 867 (size_t )bd .endspaces ); 868// copy the part after the deleted part 869STRCPY (newp + bd .textcol + bd .startspaces + bd .endspaces , 870oldp + bd .textcol + bd .textlen ); 871// replace the line 872ml_replace (lnum ,newp , false); 873 874extmark_splice_cols (curbuf , (int )lnum - 1 ,bd .textcol , 875bd .textlen ,bd .startspaces + bd .endspaces , 876kExtmarkUndo ); 877 } 878 879check_cursor_col (curwin ); 880changed_lines (curbuf ,curwin -> w_cursor .lnum ,curwin -> w_cursor .col , 881oap -> end .lnum + 1 ,0 , true); 882oap -> line_count = 0 ;// no lines deleted 883 }else if (oap -> motion_type == kMTLineWise ) { 884if (oap -> op_type == OP_CHANGE ) { 885// Delete the lines except the first one. Temporarily move the 886// cursor to the next line. Save the current line number, if the 887// last line is deleted it may be changed. 888 889if (oap -> line_count > 1 ) { 890lnum = curwin -> w_cursor .lnum ; 891curwin -> w_cursor .lnum ++ ; 892del_lines (oap -> line_count - 1 , true); 893curwin -> w_cursor .lnum = lnum ; 894 } 895if (u_save_cursor ()== FAIL ) { 896return FAIL ; 897 } 898if (curbuf -> b_p_ai ) {// don't delete indent 899beginline (BL_WHITE );// cursor on first non-white 900Ins .did_ai = true;// delete the indent when ESC hit 901Ins .ai_col = curwin -> w_cursor .col ; 902 }else { 903beginline (0 );// cursor in column 0 904 } 905truncate_line (false);// delete the rest of the line, 906// leaving cursor past last char in line 907if (oap -> line_count > 1 ) { 908u_clearline (curbuf );// "U" command not possible after "2cc" 909 } 910 }else { 911del_lines (oap -> line_count , true); 912beginline (BL_WHITE |BL_FIX ); 913u_clearline (curbuf );// "U" command not possible after "dd" 914 } 915 }else { 916if (virtual_op ) { 917// For virtualedit: break the tabs that are partly included. 918if (gchar_pos (& oap -> start )== '\t' ) { 919int endcol = 0 ; 920if (u_save_cursor ()== FAIL ) {// save first line for undo 921return FAIL ; 922 } 923if (oap -> line_count == 1 ) { 924endcol = getviscol2 (oap -> end .col ,oap -> end .coladd ); 925 } 926coladvance_force (getviscol2 (oap -> start .col ,oap -> start .coladd )); 927oap -> start = curwin -> w_cursor ; 928if (oap -> line_count == 1 ) { 929coladvance (curwin ,endcol ); 930oap -> end .col = curwin -> w_cursor .col ; 931oap -> end .coladd = curwin -> w_cursor .coladd ; 932curwin -> w_cursor = oap -> start ; 933 } 934 } 935 936// Break a tab only when it's included in the area. 937if (gchar_pos (& oap -> end )== '\t' 938&& oap -> end .coladd == 0 939&& oap -> inclusive ) { 940// save last line for undo 941if (u_save ((linenr_T )(oap -> end .lnum - 1 ), 942 (linenr_T )(oap -> end .lnum + 1 ))== FAIL ) { 943return FAIL ; 944 } 945curwin -> w_cursor = oap -> end ; 946coladvance_force (getviscol2 (oap -> end .col ,oap -> end .coladd )); 947oap -> end = curwin -> w_cursor ; 948curwin -> w_cursor = oap -> start ; 949 } 950mb_adjust_opend (oap ); 951 } 952 953if (oap -> line_count == 1 ) {// delete characters within one line 954if (u_save_cursor ()== FAIL ) {// save line for undo 955return FAIL ; 956 } 957 958// if 'cpoptions' contains '$', display '$' at end of change 959if (vim_strchr (p_cpo ,kCpoDollar )!= NULL 960&& oap -> op_type == OP_CHANGE 961&& oap -> end .lnum == curwin -> w_cursor .lnum 962&& !oap -> is_VIsual ) { 963display_dollar (oap -> end .col - !oap -> inclusive ); 964 } 965 966int n = oap -> end .col - oap -> start .col + 1 - !oap -> inclusive ; 967 968if (virtual_op ) { 969// fix up things for virtualedit-delete: 970// break the tabs which are going to get in our way 971int len = get_cursor_line_len (); 972 973if (oap -> end .coladd != 0 974&& (int )oap -> end .col >=len - 1 975&& !(oap -> start .coladd && (int )oap -> end .col >=len - 1 )) { 976n ++ ; 977 } 978// Delete at least one char (e.g, when on a control char). 979if (n == 0 && oap -> start .coladd != oap -> end .coladd ) { 980n = 1 ; 981 } 982 983// When deleted a char in the line, reset coladd. 984if (gchar_cursor ()!= NUL ) { 985curwin -> w_cursor .coladd = 0 ; 986 } 987 } 988 989del_bytes ((colnr_T )n , !virtual_op , 990oap -> op_type == OP_DELETE && !oap -> is_VIsual ); 991 }else { 992// delete characters between lines 993pos_T curpos ; 994 995// save deleted and changed lines for undo 996if (u_save (curwin -> w_cursor .lnum - 1 , 997curwin -> w_cursor .lnum + oap -> line_count )== FAIL ) { 998return FAIL ; 999 } 1000 1001curbuf_splice_pending ++ ; 1002pos_T startpos = curwin -> w_cursor ;// start position for delete 1003bcount_t deleted_bytes = get_region_bytecount (curbuf ,startpos .lnum ,oap -> end .lnum , 1004startpos .col , 1005oap -> end .col )+ oap -> inclusive ; 1006truncate_line (true);// delete from cursor to end of line 1007 1008curpos = curwin -> w_cursor ;// remember curwin->w_cursor 1009curwin -> w_cursor .lnum ++ ; 1010 1011del_lines (oap -> line_count - 2 , false); 1012 1013// delete from start of line until op_end 1014int n = (oap -> end .col + 1 - !oap -> inclusive ); 1015curwin -> w_cursor .col = 0 ; 1016del_bytes ((colnr_T )n , !virtual_op , 1017oap -> op_type == OP_DELETE && !oap -> is_VIsual ); 1018curwin -> w_cursor = curpos ;// restore curwin->w_cursor 1019do_join (2 , false, false, false, false); 1020curbuf_splice_pending -- ; 1021extmark_splice (curbuf , (int )startpos .lnum - 1 ,startpos .col , 1022 (int )oap -> line_count - 1 ,n ,deleted_bytes , 10230 ,0 ,0 ,kExtmarkUndo ); 1024 } 1025if (oap -> op_type == OP_DELETE ) { 1026auto_format (false, true); 1027 } 1028 } 1029 1030msgmore (curbuf -> b_ml .ml_line_count - old_lcount ); 1031 1032setmarks : 1033if ((cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 1034if (oap -> motion_type == kMTBlockWise ) { 1035curbuf -> b_op_end .lnum = oap -> end .lnum ; 1036curbuf -> b_op_end .col = oap -> start .col ; 1037 }else { 1038curbuf -> b_op_end = oap -> start ; 1039 } 1040curbuf -> b_op_start = oap -> start ; 1041 } 1042 1043return OK ; 1044} 1045 1046/// Adjust end of operating area for ending on a multi-byte character. 1047/// Used for deletion. 1048static void mb_adjust_opend (oparg_T * oap ) 1049{ 1050if (!oap -> inclusive ) { 1051return ; 1052 } 1053 1054const char * line = ml_get (oap -> end .lnum ); 1055const char * ptr = line + oap -> end .col ; 1056if (* ptr != NUL ) { 1057ptr -= utf_head_off (line ,ptr ); 1058ptr += utfc_ptr2len (ptr )- 1 ; 1059oap -> end .col = (colnr_T )(ptr - line ); 1060 } 1061} 1062 1063/// put byte 'c' at position 'lp', but 1064/// verify, that the position to place 1065/// is actually safe 1066static void pbyte (pos_T lp ,int c ) 1067{ 1068assert (c <=UCHAR_MAX ); 1069char * p = ml_get_buf_mut (curbuf ,lp .lnum ); 1070colnr_T len = curbuf -> b_ml .ml_line_textlen ; 1071 1072// safety check 1073if (lp .col >=len ) { 1074lp .col = (len > 1 ?len - 2 :0 ); 1075 } 1076* (p + lp .col )= (char )c ; 1077if (!curbuf_splice_pending ) { 1078extmark_splice_cols (curbuf , (int )lp .lnum - 1 ,lp .col ,1 ,1 ,kExtmarkUndo ); 1079 } 1080} 1081 1082/// Replace the character under the cursor with "c". 1083/// This takes care of multi-byte characters. 1084static void replace_character (int c ) 1085{ 1086const int n = State ; 1087 1088State = MODE_REPLACE ; 1089ins_char (c ); 1090State = n ; 1091// Backup to the replaced character. 1092dec_cursor (); 1093} 1094 1095/// Replace a whole area with one character. 1096static int op_replace (oparg_T * oap ,int c ) 1097{ 1098int n ; 1099struct block_def bd ; 1100char * after_p = NULL ; 1101bool had_ctrl_v_cr = false; 1102 1103if ((curbuf -> b_ml .ml_flags & ML_EMPTY )|| oap -> empty ) { 1104return OK ;// nothing to do 1105 } 1106if (c == REPLACE_CR_NCHAR ) { 1107had_ctrl_v_cr = true; 1108c = CAR ; 1109 }else if (c == REPLACE_NL_NCHAR ) { 1110had_ctrl_v_cr = true; 1111c = NL ; 1112 } 1113 1114mb_adjust_opend (oap ); 1115 1116if (u_save ((linenr_T )(oap -> start .lnum - 1 ), 1117 (linenr_T )(oap -> end .lnum + 1 ))== FAIL ) { 1118return FAIL ; 1119 } 1120 1121// block mode replace 1122if (oap -> motion_type == kMTBlockWise ) { 1123bd .is_MAX = (curwin -> w_curswant == MAXCOL ); 1124for (;curwin -> w_cursor .lnum <=oap -> end .lnum ;curwin -> w_cursor .lnum ++ ) { 1125curwin -> w_cursor .col = 0 ;// make sure cursor position is valid 1126block_prep (oap ,& bd ,curwin -> w_cursor .lnum , true); 1127if (bd .textlen == 0 && (!virtual_op || bd .is_MAX )) { 1128continue ;// nothing to replace 1129 } 1130 1131// n == number of extra chars required 1132// If we split a TAB, it may be replaced by several characters. 1133// Thus the number of characters may increase! 1134// If the range starts in virtual space, count the initial 1135// coladd offset as part of "startspaces" 1136if (virtual_op && bd .is_short && * bd .textstart == NUL ) { 1137pos_T vpos ; 1138 1139vpos .lnum = curwin -> w_cursor .lnum ; 1140getvpos (curwin ,& vpos ,oap -> start_vcol ); 1141bd .startspaces += vpos .coladd ; 1142n = bd .startspaces ; 1143 }else { 1144// allow for pre spaces 1145n = (bd .startspaces ?bd .start_char_vcols - 1 :0 ); 1146 } 1147 1148// allow for post spp 1149n += (bd .endspaces 1150&& !bd .is_oneChar 1151&& bd .end_char_vcols > 0 ) ?bd .end_char_vcols - 1 :0 ; 1152// Figure out how many characters to replace. 1153int numc = oap -> end_vcol - oap -> start_vcol + 1 ; 1154if (bd .is_short && (!virtual_op || bd .is_MAX )) { 1155numc -= (oap -> end_vcol - bd .end_vcol )+ 1 ; 1156 } 1157 1158// A double-wide character can be replaced only up to half the 1159// times. 1160if (utf_char2cells (c )> 1 ) { 1161if ((numc & 1 )&& !bd .is_short ) { 1162bd .endspaces ++ ; 1163n ++ ; 1164 } 1165numc = numc /2 ; 1166 } 1167 1168// Compute bytes needed, move character count to num_chars. 1169int num_chars = numc ; 1170numc *=utf_char2len (c ); 1171 1172char * oldp = get_cursor_line_ptr (); 1173colnr_T oldlen = get_cursor_line_len (); 1174 1175size_t newp_size = (size_t )bd .textcol + (size_t )bd .startspaces ; 1176if (had_ctrl_v_cr || (c != '\r' && c != '\n' )) { 1177newp_size += (size_t )numc ; 1178if (!bd .is_short ) { 1179newp_size += (size_t )(bd .endspaces + oldlen 1180- bd .textcol - bd .textlen ); 1181 } 1182 } 1183char * newp = xmallocz (newp_size ); 1184// copy up to deleted part 1185memmove (newp ,oldp , (size_t )bd .textcol ); 1186oldp += bd .textcol + bd .textlen ; 1187// insert pre-spaces 1188memset (newp + bd .textcol ,' ' , (size_t )bd .startspaces ); 1189// insert replacement chars CHECK FOR ALLOCATED SPACE 1190// REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR literally. 1191size_t after_p_len = 0 ; 1192int col = oldlen - bd .textcol - bd .textlen + 1 ; 1193assert (col >=0 ); 1194int newrows = 0 ; 1195int newcols = 0 ; 1196if (had_ctrl_v_cr || (c != '\r' && c != '\n' )) { 1197// strlen(newp) at this point 1198int newp_len = bd .textcol + bd .startspaces ; 1199while (-- num_chars >=0 ) { 1200newp_len += utf_char2bytes (c ,newp + newp_len ); 1201 } 1202if (!bd .is_short ) { 1203// insert post-spaces 1204memset (newp + newp_len ,' ' , (size_t )bd .endspaces ); 1205newp_len += bd .endspaces ; 1206// copy the part after the changed part 1207memmove (newp + newp_len ,oldp , (size_t )col ); 1208 } 1209newcols = newp_len - bd .textcol ; 1210 }else { 1211// Replacing with \r or \n means splitting the line. 1212after_p_len = (size_t )col ; 1213after_p = xmalloc (after_p_len ); 1214memmove (after_p ,oldp ,after_p_len ); 1215newrows = 1 ; 1216 } 1217// replace the line 1218ml_replace (curwin -> w_cursor .lnum ,newp , false); 1219curbuf_splice_pending ++ ; 1220linenr_T baselnum = curwin -> w_cursor .lnum ; 1221if (after_p != NULL ) { 1222ml_append (curwin -> w_cursor .lnum ++ ,after_p , (int )after_p_len , false); 1223appended_lines_mark (curwin -> w_cursor .lnum ,1 ); 1224oap -> end .lnum ++ ; 1225xfree (after_p ); 1226 } 1227curbuf_splice_pending -- ; 1228extmark_splice (curbuf , (int )baselnum - 1 ,bd .textcol , 12290 ,bd .textlen ,bd .textlen , 1230newrows ,newcols ,newrows + newcols ,kExtmarkUndo ); 1231 } 1232 }else { 1233// Characterwise or linewise motion replace. 1234if (oap -> motion_type == kMTLineWise ) { 1235oap -> start .col = 0 ; 1236curwin -> w_cursor .col = 0 ; 1237oap -> end .col = ml_get_len (oap -> end .lnum ); 1238if (oap -> end .col ) { 1239oap -> end .col -- ; 1240 } 1241 }else if (!oap -> inclusive ) { 1242dec (& (oap -> end )); 1243 } 1244 1245// TODO(bfredl): we could batch all the splicing 1246// done on the same line, at least 1247while (ltoreq (curwin -> w_cursor ,oap -> end )) { 1248bool done = false; 1249 1250n = gchar_cursor (); 1251if (n != NUL ) { 1252int new_byte_len = utf_char2len (c ); 1253int old_byte_len = utfc_ptr2len (get_cursor_pos_ptr ()); 1254 1255if (new_byte_len > 1 || old_byte_len > 1 ) { 1256// This is slow, but it handles replacing a single-byte 1257// with a multi-byte and the other way around. 1258if (curwin -> w_cursor .lnum == oap -> end .lnum ) { 1259oap -> end .col += new_byte_len - old_byte_len ; 1260 } 1261replace_character (c ); 1262done = true; 1263 }else { 1264if (n == TAB ) { 1265int end_vcol = 0 ; 1266 1267if (curwin -> w_cursor .lnum == oap -> end .lnum ) { 1268// oap->end has to be recalculated when 1269// the tab breaks 1270end_vcol = getviscol2 (oap -> end .col , 1271oap -> end .coladd ); 1272 } 1273coladvance_force (getviscol ()); 1274if (curwin -> w_cursor .lnum == oap -> end .lnum ) { 1275getvpos (curwin ,& oap -> end ,end_vcol ); 1276 } 1277 } 1278// with "coladd" set may move to just after a TAB 1279if (gchar_cursor ()!= NUL ) { 1280pbyte (curwin -> w_cursor ,c ); 1281done = true; 1282 } 1283 } 1284 } 1285if (!done && virtual_op && curwin -> w_cursor .lnum == oap -> end .lnum ) { 1286int virtcols = oap -> end .coladd ; 1287 1288if (curwin -> w_cursor .lnum == oap -> start .lnum 1289&& oap -> start .col == oap -> end .col && oap -> start .coladd ) { 1290virtcols -= oap -> start .coladd ; 1291 } 1292 1293// oap->end has been trimmed so it's effectively inclusive; 1294// as a result an extra +1 must be counted so we don't 1295// trample the NUL byte. 1296coladvance_force (getviscol2 (oap -> end .col ,oap -> end .coladd )+ 1 ); 1297curwin -> w_cursor .col -= (virtcols + 1 ); 1298for (;virtcols >=0 ;virtcols -- ) { 1299if (utf_char2len (c )> 1 ) { 1300replace_character (c ); 1301 }else { 1302pbyte (curwin -> w_cursor ,c ); 1303 } 1304if (inc (& curwin -> w_cursor )== -1 ) { 1305break ; 1306 } 1307 } 1308 } 1309 1310// Advance to next character, stop at the end of the file. 1311if (inc_cursor ()== -1 ) { 1312break ; 1313 } 1314 } 1315 } 1316 1317curwin -> w_cursor = oap -> start ; 1318check_cursor (curwin ); 1319changed_lines (curbuf ,oap -> start .lnum ,oap -> start .col ,oap -> end .lnum + 1 ,0 , true); 1320 1321if ((cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 1322// Set "'[" and "']" marks. 1323curbuf -> b_op_start = oap -> start ; 1324curbuf -> b_op_end = oap -> end ; 1325 } 1326 1327return OK ; 1328} 1329 1330/// Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?". 1331void op_tilde (oparg_T * oap ) 1332{ 1333struct block_def bd ; 1334bool did_change = false; 1335 1336if (u_save ((linenr_T )(oap -> start .lnum - 1 ), 1337 (linenr_T )(oap -> end .lnum + 1 ))== FAIL ) { 1338return ; 1339 } 1340 1341pos_T pos = oap -> start ; 1342if (oap -> motion_type == kMTBlockWise ) {// Visual block mode 1343for (;pos .lnum <=oap -> end .lnum ;pos .lnum ++ ) { 1344block_prep (oap ,& bd ,pos .lnum , false); 1345pos .col = bd .textcol ; 1346bool one_change = swapchars (oap -> op_type ,& pos ,bd .textlen ); 1347did_change |=one_change ; 1348 } 1349if (did_change ) { 1350changed_lines (curbuf ,oap -> start .lnum ,0 ,oap -> end .lnum + 1 ,0 , true); 1351 } 1352 }else {// not block mode 1353if (oap -> motion_type == kMTLineWise ) { 1354oap -> start .col = 0 ; 1355pos .col = 0 ; 1356oap -> end .col = ml_get_len (oap -> end .lnum ); 1357if (oap -> end .col ) { 1358oap -> end .col -- ; 1359 } 1360 }else if (!oap -> inclusive ) { 1361dec (& (oap -> end )); 1362 } 1363 1364if (pos .lnum == oap -> end .lnum ) { 1365did_change = swapchars (oap -> op_type ,& pos , 1366oap -> end .col - pos .col + 1 ); 1367 }else { 1368while (true) { 1369did_change |=swapchars (oap -> op_type ,& pos , 1370pos .lnum == oap -> end .lnum ?oap -> end .col + 1 1371 :ml_get_pos_len (& pos )); 1372if (ltoreq (oap -> end ,pos )|| inc (& pos )== -1 ) { 1373break ; 1374 } 1375 } 1376 } 1377if (did_change ) { 1378changed_lines (curbuf ,oap -> start .lnum ,oap -> start .col ,oap -> end .lnum + 1 , 13790 , true); 1380 } 1381 } 1382 1383if (!did_change && oap -> is_VIsual ) { 1384// No change: need to remove the Visual selection 1385redraw_curbuf_later (UPD_INVERTED ); 1386 } 1387 1388if ((cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 1389// Set '[ and '] marks. 1390curbuf -> b_op_start = oap -> start ; 1391curbuf -> b_op_end = oap -> end ; 1392 } 1393 1394if (oap -> line_count > p_report ) { 1395smsg (0 ,NGETTEXT ("%" PRId64 " line changed" ,"%" PRId64 " lines changed" ,oap -> line_count ), 1396 (int64_t )oap -> line_count ); 1397 } 1398} 1399 1400/// Invoke swapchar() on "length" bytes at position "pos". 1401/// 1402/// @param pos is advanced to just after the changed characters. 1403/// @param length is rounded up to include the whole last multi-byte character. 1404/// Also works correctly when the number of bytes changes. 1405/// 1406/// @return true if some character was changed. 1407static int swapchars (int op_type ,pos_T * pos ,int length ) 1408FUNC_ATTR_NONNULL_ALL 1409{ 1410int did_change = 0 ; 1411 1412for (int todo = length ;todo > 0 ;todo -- ) { 1413const int len = utfc_ptr2len (ml_get_pos (pos )); 1414 1415// we're counting bytes, not characters 1416if (len > 0 ) { 1417todo -= len - 1 ; 1418 } 1419did_change |=swapchar (op_type ,pos ); 1420if (inc (pos )== -1 ) {// at end of file 1421break ; 1422 } 1423 } 1424return did_change ; 1425} 1426 1427/// @param op_type 1428/// == OP_UPPER: make uppercase, 1429/// == OP_LOWER: make lowercase, 1430/// == OP_ROT13: do rot13 encoding, 1431/// else swap case of character at 'pos' 1432/// 1433/// @return true when something actually changed. 1434bool swapchar (int op_type ,pos_T * pos ) 1435FUNC_ATTR_NONNULL_ARG (2 ) 1436{ 1437const int c = gchar_pos (pos ); 1438 1439// Only do rot13 encoding for ASCII characters. 1440if (c >=0x80 && op_type == OP_ROT13 ) { 1441return false; 1442 } 1443 1444int nc = c ; 1445if (mb_islower (c )) { 1446if (op_type == OP_ROT13 ) { 1447nc = ROT13 (c ,'a' ); 1448 }else if (op_type != OP_LOWER ) { 1449nc = mb_toupper (c ); 1450 } 1451 }else if (mb_isupper (c )) { 1452if (op_type == OP_ROT13 ) { 1453nc = ROT13 (c ,'A' ); 1454 }else if (op_type != OP_UPPER ) { 1455nc = mb_tolower (c ); 1456 } 1457 } 1458if (nc != c ) { 1459if (c >=0x80 || nc >=0x80 ) { 1460pos_T sp = curwin -> w_cursor ; 1461 1462curwin -> w_cursor = * pos ; 1463// don't use del_char(), it also removes composing chars 1464del_bytes (utf_ptr2len (get_cursor_pos_ptr ()), false, false); 1465ins_char (nc ); 1466curwin -> w_cursor = sp ; 1467 }else { 1468pbyte (* pos ,nc ); 1469 } 1470return true; 1471 } 1472return false; 1473} 1474 1475/// Insert and append operators for Visual mode. 1476void op_insert (oparg_T * oap ,int count1 ) 1477{ 1478int pre_textlen = 0 ; 1479colnr_T ind_pre_col = 0 ; 1480int ind_pre_vcol = 0 ; 1481struct block_def bd ; 1482 1483// edit() changes this - record it for OP_APPEND 1484bd .is_MAX = (curwin -> w_curswant == MAXCOL ); 1485 1486// vis block is still marked. Get rid of it now. 1487curwin -> w_cursor .lnum = oap -> start .lnum ; 1488redraw_curbuf_later (UPD_INVERTED ); 1489update_screen (); 1490 1491if (oap -> motion_type == kMTBlockWise ) { 1492// When 'virtualedit' is used, need to insert the extra spaces before 1493// doing block_prep(). When only "block" is used, virtual edit is 1494// already disabled, but still need it when calling 1495// coladvance_force(). 1496// coladvance_force() uses get_ve_flags() to get the 'virtualedit' 1497// state for the current window. To override that state, we need to 1498// set the window-local value of ve_flags rather than the global value. 1499if (curwin -> w_cursor .coladd > 0 ) { 1500unsigned old_ve_flags = curwin -> w_ve_flags ; 1501 1502if (u_save_cursor ()== FAIL ) { 1503return ; 1504 } 1505curwin -> w_ve_flags = kOptVeFlagAll ; 1506coladvance_force (oap -> op_type == OP_APPEND 1507 ?oap -> end_vcol + 1 :getviscol ()); 1508if (oap -> op_type == OP_APPEND ) { 1509curwin -> w_cursor .col -- ; 1510 } 1511curwin -> w_ve_flags = old_ve_flags ; 1512 } 1513// Get the info about the block before entering the text 1514block_prep (oap ,& bd ,oap -> start .lnum , true); 1515// Get indent information 1516ind_pre_col = (colnr_T )getwhitecols_curline (); 1517ind_pre_vcol = get_indent (); 1518pre_textlen = ml_get_len (oap -> start .lnum )- bd .textcol ; 1519if (oap -> op_type == OP_APPEND ) { 1520pre_textlen -= bd .textlen ; 1521 } 1522 } 1523 1524if (oap -> op_type == OP_APPEND ) { 1525if (oap -> motion_type == kMTBlockWise 1526&& curwin -> w_cursor .coladd == 0 ) { 1527// Move the cursor to the character right of the block. 1528curwin -> w_set_curswant = true; 1529while (* get_cursor_pos_ptr ()!= NUL 1530&& (curwin -> w_cursor .col < bd .textcol + bd .textlen )) { 1531curwin -> w_cursor .col ++ ; 1532 } 1533if (bd .is_short && !bd .is_MAX ) { 1534// First line was too short, make it longer and adjust the 1535// values in "bd". 1536if (u_save_cursor ()== FAIL ) { 1537return ; 1538 } 1539for (int i = 0 ;i < bd .endspaces ;i ++ ) { 1540ins_char (' ' ); 1541 } 1542bd .textlen += bd .endspaces ; 1543 } 1544 }else { 1545curwin -> w_cursor = oap -> end ; 1546check_cursor_col (curwin ); 1547 1548// Works just like an 'i'nsert on the next character. 1549if (!LINEEMPTY (curwin -> w_cursor .lnum ) 1550&& oap -> start_vcol != oap -> end_vcol ) { 1551inc_cursor (); 1552 } 1553 } 1554 } 1555 1556pos_T t1 = oap -> start ; 1557const pos_T start_insert = curwin -> w_cursor ; 1558edit (NUL , false, (linenr_T )count1 ); 1559 1560// When a tab was inserted, and the characters in front of the tab 1561// have been converted to a tab as well, the column of the cursor 1562// might have actually been reduced, so need to adjust here. 1563if (t1 .lnum == curbuf -> b_op_start_orig .lnum 1564&& lt (curbuf -> b_op_start_orig ,t1 )) { 1565oap -> start = curbuf -> b_op_start_orig ; 1566 } 1567 1568// If user has moved off this line, we don't know what to do, so do 1569// nothing. 1570// Also don't repeat the insert when Insert mode ended with CTRL-C. 1571if (curwin -> w_cursor .lnum != oap -> start .lnum || got_int ) { 1572return ; 1573 } 1574 1575if (oap -> motion_type == kMTBlockWise ) { 1576int ind_post_vcol = 0 ; 1577struct block_def bd2 ; 1578bool did_indent = false; 1579 1580// if indent kicked in, the firstline might have changed 1581// but only do that, if the indent actually increased 1582colnr_T ind_post_col = (colnr_T )getwhitecols_curline (); 1583if (curbuf -> b_op_start .col > ind_pre_col && ind_post_col > ind_pre_col ) { 1584bd .textcol += ind_post_col - ind_pre_col ; 1585ind_post_vcol = get_indent (); 1586bd .start_vcol += ind_post_vcol - ind_pre_vcol ; 1587did_indent = true; 1588 } 1589 1590// The user may have moved the cursor before inserting something, try 1591// to adjust the block for that. But only do it, if the difference 1592// does not come from indent kicking in. 1593if (oap -> start .lnum == curbuf -> b_op_start_orig .lnum && !bd .is_MAX && !did_indent ) { 1594const int t = getviscol2 (curbuf -> b_op_start_orig .col ,curbuf -> b_op_start_orig .coladd ); 1595 1596if (oap -> op_type == OP_INSERT 1597&& oap -> start .col + oap -> start .coladd 1598!= curbuf -> b_op_start_orig .col + curbuf -> b_op_start_orig .coladd ) { 1599oap -> start .col = curbuf -> b_op_start_orig .col ; 1600pre_textlen -= t - oap -> start_vcol ; 1601oap -> start_vcol = t ; 1602 }else if (oap -> op_type == OP_APPEND 1603&& oap -> start .col + oap -> start .coladd 1604 >=curbuf -> b_op_start_orig .col + curbuf -> b_op_start_orig .coladd ) { 1605oap -> start .col = curbuf -> b_op_start_orig .col ; 1606// reset pre_textlen to the value of OP_INSERT 1607pre_textlen += bd .textlen ; 1608pre_textlen -= t - oap -> start_vcol ; 1609oap -> start_vcol = t ; 1610oap -> op_type = OP_INSERT ; 1611 } 1612 } 1613 1614// Spaces and tabs in the indent may have changed to other spaces and 1615// tabs. Get the starting column again and correct the length. 1616// Don't do this when "$" used, end-of-line will have changed. 1617// 1618// if indent was added and the inserted text was after the indent, 1619// correct the selection for the new indent. 1620if (did_indent && bd .textcol - ind_post_col > 0 ) { 1621oap -> start .col += ind_post_col - ind_pre_col ; 1622oap -> start_vcol += ind_post_vcol - ind_pre_vcol ; 1623oap -> end .col += ind_post_col - ind_pre_col ; 1624oap -> end_vcol += ind_post_vcol - ind_pre_vcol ; 1625 } 1626block_prep (oap ,& bd2 ,oap -> start .lnum , true); 1627if (did_indent && bd .textcol - ind_post_col > 0 ) { 1628// undo for where "oap" is used below 1629oap -> start .col -= ind_post_col - ind_pre_col ; 1630oap -> start_vcol -= ind_post_vcol - ind_pre_vcol ; 1631oap -> end .col -= ind_post_col - ind_pre_col ; 1632oap -> end_vcol -= ind_post_vcol - ind_pre_vcol ; 1633 } 1634if (!bd .is_MAX || bd2 .textlen < bd .textlen ) { 1635if (oap -> op_type == OP_APPEND ) { 1636pre_textlen += bd2 .textlen - bd .textlen ; 1637if (bd2 .endspaces ) { 1638bd2 .textlen -- ; 1639 } 1640 } 1641bd .textcol = bd2 .textcol ; 1642bd .textlen = bd2 .textlen ; 1643 } 1644 1645// Subsequent calls to ml_get() flush the firstline data - take a 1646// copy of the required string. 1647char * firstline = ml_get (oap -> start .lnum ); 1648colnr_T len = ml_get_len (oap -> start .lnum ); 1649colnr_T add = bd .textcol ; 1650colnr_T offset = 0 ;// offset when cursor was moved in insert mode 1651if (oap -> op_type == OP_APPEND ) { 1652add += bd .textlen ; 1653// account for pressing cursor in insert mode when '$' was used 1654if (bd .is_MAX && start_insert .lnum == Ins .start .lnum && start_insert .col > Ins .start .col ) { 1655offset = start_insert .col - Ins .start .col ; 1656add -= offset ; 1657if (oap -> end_vcol > offset ) { 1658oap -> end_vcol -= offset + 1 ; 1659 }else { 1660// moved outside of the visual block, what to do? 1661return ; 1662 } 1663 } 1664 } 1665add = MIN (add ,len );// short line, point to the NUL 1666firstline += add ; 1667len -= add ; 1668int ins_len = len - pre_textlen - offset ; 1669if (pre_textlen >=0 && ins_len > 0 ) { 1670char * ins_text = xmemdupz (firstline , (size_t )ins_len ); 1671// block handled here 1672if (u_save (oap -> start .lnum , (linenr_T )(oap -> end .lnum + 1 ))== OK ) { 1673block_insert (oap ,ins_text , (size_t )ins_len , (oap -> op_type == OP_INSERT ),& bd ); 1674 } 1675 1676curwin -> w_cursor .col = oap -> start .col ; 1677check_cursor (curwin ); 1678xfree (ins_text ); 1679 } 1680 } 1681} 1682 1683/// handle a change operation 1684/// 1685/// @return true if edit() returns because of a CTRL-O command 1686int op_change (oparg_T * oap ) 1687{ 1688int pre_textlen = 0 ; 1689int pre_indent = 0 ; 1690char * firstline ; 1691struct block_def bd ; 1692 1693colnr_T l = oap -> start .col ; 1694if (oap -> motion_type == kMTLineWise ) { 1695l = 0 ; 1696Ins .can_si = may_do_si ();// Like opening a new line, do smart indent 1697 } 1698 1699// First delete the text in the region. In an empty buffer only need to 1700// save for undo 1701if (curbuf -> b_ml .ml_flags & ML_EMPTY ) { 1702if (u_save_cursor ()== FAIL ) { 1703return false; 1704 } 1705 }else if (op_delete (oap )== FAIL ) { 1706return false; 1707 } 1708 1709if ((l > curwin -> w_cursor .col )&& !LINEEMPTY (curwin -> w_cursor .lnum ) 1710&& !virtual_op ) { 1711inc_cursor (); 1712 } 1713 1714// check for still on same line (<CR> in inserted text meaningless) 1715// skip blank lines too 1716if (oap -> motion_type == kMTBlockWise ) { 1717// Add spaces before getting the current line length. 1718if (virtual_op && (curwin -> w_cursor .coladd > 0 1719|| gchar_cursor ()== NUL )) { 1720coladvance_force (getviscol ()); 1721 } 1722firstline = ml_get (oap -> start .lnum ); 1723pre_textlen = ml_get_len (oap -> start .lnum ); 1724pre_indent = (int )getwhitecols (firstline ); 1725bd .textcol = curwin -> w_cursor .col ; 1726 } 1727 1728if (oap -> motion_type == kMTLineWise ) { 1729fix_indent (); 1730 } 1731 1732// Reset finish_op now, don't want it set inside edit(). 1733const bool save_finish_op = finish_op ; 1734finish_op = false; 1735 1736int retval = edit (NUL , false,1 ); 1737 1738finish_op = save_finish_op ; 1739 1740// In Visual block mode, handle copying the new text to all lines of the 1741// block. 1742// Don't repeat the insert when Insert mode ended with CTRL-C. 1743if (oap -> motion_type == kMTBlockWise 1744&& oap -> start .lnum != oap -> end .lnum && !got_int ) { 1745// Auto-indenting may have changed the indent. If the cursor was past 1746// the indent, exclude that indent change from the inserted text. 1747firstline = ml_get (oap -> start .lnum ); 1748if (bd .textcol > (colnr_T )pre_indent ) { 1749int new_indent = (int )getwhitecols (firstline ); 1750 1751pre_textlen += new_indent - pre_indent ; 1752bd .textcol += (colnr_T )(new_indent - pre_indent ); 1753 } 1754 1755int ins_len = ml_get_len (oap -> start .lnum )- pre_textlen ; 1756if (ins_len > 0 ) { 1757// Subsequent calls to ml_get() flush the firstline data - take a 1758// copy of the inserted text. 1759char * ins_text = xmalloc ((size_t )ins_len + 1 ); 1760xmemcpyz (ins_text ,firstline + bd .textcol , (size_t )ins_len ); 1761for (linenr_T linenr = oap -> start .lnum + 1 ;linenr <=oap -> end .lnum ; 1762linenr ++ ) { 1763block_prep (oap ,& bd ,linenr , true); 1764if (!bd .is_short || virtual_op ) { 1765pos_T vpos ; 1766 1767// If the block starts in virtual space, count the 1768// initial coladd offset as part of "startspaces" 1769if (bd .is_short ) { 1770vpos .lnum = linenr ; 1771getvpos (curwin ,& vpos ,oap -> start_vcol ); 1772 }else { 1773vpos .coladd = 0 ; 1774 } 1775char * oldp = ml_get (linenr ); 1776char * newp = xmalloc ((size_t )ml_get_len (linenr ) 1777+ (size_t )vpos .coladd + (size_t )ins_len + 1 ); 1778// copy up to block start 1779memmove (newp ,oldp , (size_t )bd .textcol ); 1780int newlen = bd .textcol ; 1781memset (newp + newlen ,' ' , (size_t )vpos .coladd ); 1782newlen += vpos .coladd ; 1783memmove (newp + newlen ,ins_text , (size_t )ins_len ); 1784newlen += ins_len ; 1785STRCPY (newp + newlen ,oldp + bd .textcol ); 1786ml_replace (linenr ,newp , false); 1787extmark_splice_cols (curbuf , (int )linenr - 1 ,bd .textcol , 17880 ,vpos .coladd + ins_len ,kExtmarkUndo ); 1789 } 1790 } 1791check_cursor (curwin ); 1792changed_lines (curbuf ,oap -> start .lnum + 1 ,0 ,oap -> end .lnum + 1 ,0 , true); 1793xfree (ins_text ); 1794 } 1795 } 1796auto_format (false, true); 1797 1798return retval ; 1799} 1800 1801/// When the cursor is on the NUL past the end of the line and it should not be 1802/// there move it left. 1803void adjust_cursor_eol (void ) 1804{ 1805unsigned cur_ve_flags = get_ve_flags (curwin ); 1806 1807const bool adj_cursor = (curwin -> w_cursor .col > 0 1808&& gchar_cursor ()== NUL 1809&& (cur_ve_flags & kOptVeFlagOnemore )== 0 1810&& (cur_ve_flags & kOptVeFlagAll )== 0 1811&& !(restart_edit || (State & MODE_INSERT ))); 1812if (!adj_cursor ) { 1813return ; 1814 } 1815 1816// Put the cursor on the last character in the line. 1817dec_cursor (); 1818 1819if (cur_ve_flags == kOptVeFlagAll ) { 1820colnr_T scol ,ecol ; 1821 1822// Coladd is set to the width of the last character. 1823getvcol (curwin ,& curwin -> w_cursor ,& scol ,NULL ,& ecol ,0 ); 1824curwin -> w_cursor .coladd = ecol - scol + 1 ; 1825 } 1826} 1827 1828/// If \p "process" is true and the line begins with a comment leader (possibly 1829/// after some white space), return a pointer to the text after it. 1830/// Put a boolean value indicating whether the line ends with an unclosed 1831/// comment in "is_comment". 1832/// 1833/// @param line - line to be processed 1834/// @param process - if false, will only check whether the line ends 1835/// with an unclosed comment, 1836/// @param include_space - whether to skip space following the comment leader 1837/// @param[out] is_comment - whether the current line ends with an unclosed 1838/// comment. 1839char * skip_comment (char * line ,bool process ,bool include_space ,bool * is_comment ) 1840{ 1841char * comment_flags = NULL ; 1842int leader_offset = get_last_leader_offset (line ,& comment_flags ); 1843 1844* is_comment = false; 1845if (leader_offset != -1 ) { 1846// Let's check whether the line ends with an unclosed comment. 1847// If the last comment leader has COM_END in flags, there's no comment. 1848while (* comment_flags ) { 1849if (* comment_flags == COM_END 1850|| * comment_flags == ':' ) { 1851break ; 1852 } 1853comment_flags ++ ; 1854 } 1855if (* comment_flags != COM_END ) { 1856* is_comment = true; 1857 } 1858 } 1859 1860if (process == false) { 1861return line ; 1862 } 1863 1864int lead_len = get_leader_len (line ,& comment_flags , false,include_space ); 1865 1866if (lead_len == 0 ) { 1867return line ; 1868 } 1869 1870// Find: 1871// - COM_END, 1872// - colon, 1873// whichever comes first. 1874while (* comment_flags ) { 1875if (* comment_flags == COM_END 1876|| * comment_flags == ':' ) { 1877break ; 1878 } 1879comment_flags ++ ; 1880 } 1881 1882// If we found a colon, it means that we are not processing a line 1883// starting with a closing part of a three-part comment. That's good, 1884// because we don't want to remove those as this would be annoying. 1885if (* comment_flags == ':' || * comment_flags == NUL ) { 1886line += lead_len ; 1887 } 1888 1889return line ; 1890} 1891 1892/// @param count number of lines (minimal 2) to join at the cursor position. 1893/// @param save_undo when true, save lines for undo first. 1894/// @param use_formatoptions set to false when e.g. processing backspace and comment 1895/// leaders should not be removed. 1896/// @param setmark when true, sets the '[ and '] mark, else, the caller is expected 1897/// to set those marks. 1898/// 1899/// @return FAIL for failure, OK otherwise 1900int do_join (size_t count ,bool insert_space ,bool save_undo ,bool use_formatoptions ,bool setmark ) 1901{ 1902char * curr = NULL ; 1903char * curr_start = NULL ; 1904char * cend ; 1905int endcurr1 = NUL ; 1906int endcurr2 = NUL ; 1907int currsize = 0 ;// size of the current line 1908int sumsize = 0 ;// size of the long new line 1909int ret = OK ; 1910int * comments = NULL ; 1911bool remove_comments = use_formatoptions && has_format_option (kFoRemoveComs ); 1912bool prev_was_comment = false; 1913assert (count >=1 ); 1914 1915if (save_undo && u_save (curwin -> w_cursor .lnum - 1 , 1916curwin -> w_cursor .lnum + (linenr_T )count )== FAIL ) { 1917return FAIL ; 1918 } 1919// Allocate an array to store the number of spaces inserted before each 1920// line. We will use it to pre-compute the length of the new line and the 1921// proper placement of each original line in the new one. 1922char * spaces = xcalloc (count ,1 );// number of spaces inserted before a line 1923if (remove_comments ) { 1924comments = xcalloc (count ,sizeof (* comments )); 1925 } 1926 1927// Don't move anything yet, just compute the final line length 1928// and setup the array of space strings lengths 1929// This loops forward over joined lines. 1930for (linenr_T t = 0 ;t < (linenr_T )count ;t ++ ) { 1931curr_start = ml_get (curwin -> w_cursor .lnum + t ); 1932curr = curr_start ; 1933if (t == 0 && setmark && (cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 1934// Set the '[ mark. 1935curwin -> w_buffer -> b_op_start .lnum = curwin -> w_cursor .lnum ; 1936curwin -> w_buffer -> b_op_start .col = (colnr_T )strlen (curr ); 1937 } 1938if (remove_comments ) { 1939// We don't want to remove the comment leader if the 1940// previous line is not a comment. 1941if (t > 0 && prev_was_comment ) { 1942char * new_curr = skip_comment (curr , true,insert_space ,& prev_was_comment ); 1943comments [t ]= (int )(new_curr - curr ); 1944curr = new_curr ; 1945 }else { 1946curr = skip_comment (curr , false,insert_space ,& prev_was_comment ); 1947 } 1948 } 1949 1950if (insert_space && t > 0 ) { 1951curr = skipwhite (curr ); 1952if (* curr != NUL 1953&& * curr != ')' 1954&& sumsize != 0 1955&& endcurr1 != TAB 1956&& (!has_format_option (kFoMbyteJoin ) 1957|| (utf_ptr2char (curr )< 0x100 && endcurr1 < 0x100 )) 1958&& (!has_format_option (kFoMbyteJoin2 ) 1959|| (utf_ptr2char (curr )< 0x100 && !utf_eat_space (endcurr1 )) 1960|| (endcurr1 < 0x100 1961&& !utf_eat_space (utf_ptr2char (curr ))))) { 1962// don't add a space if the line is ending in a space 1963if (endcurr1 == ' ' ) { 1964endcurr1 = endcurr2 ; 1965 }else { 1966spaces [t ]++ ; 1967 } 1968// Extra space when 'joinspaces' set and line ends in '.', '?', or '!'. 1969if (p_js && (endcurr1 == '.' || endcurr1 == '?' || endcurr1 == '!' )) { 1970spaces [t ]++ ; 1971 } 1972 } 1973 } 1974 1975if (t > 0 && curbuf_splice_pending == 0 ) { 1976colnr_T removed = (int )(curr - curr_start ); 1977extmark_splice (curbuf , (int )curwin -> w_cursor .lnum - 1 ,sumsize , 19781 ,removed ,removed + 1 , 19790 ,spaces [t ],spaces [t ], 1980kExtmarkUndo ); 1981 } 1982currsize = (int )strlen (curr ); 1983sumsize += currsize + spaces [t ]; 1984endcurr1 = endcurr2 = NUL ; 1985if (insert_space && currsize > 0 ) { 1986cend = curr + currsize ; 1987MB_PTR_BACK (curr ,cend ); 1988endcurr1 = utf_ptr2char (cend ); 1989if (cend > curr ) { 1990MB_PTR_BACK (curr ,cend ); 1991endcurr2 = utf_ptr2char (cend ); 1992 } 1993 } 1994line_breakcheck (); 1995if (got_int ) { 1996ret = FAIL ; 1997 gototheend ; 1998 } 1999 } 2000 2001// store the column position before last line 2002colnr_T col = sumsize - currsize - spaces [count - 1 ]; 2003 2004// allocate the space for the new line 2005size_t newp_len = (size_t )sumsize ; 2006char * newp = xmallocz (newp_len ); 2007cend = newp + sumsize ; 2008 2009// Move affected lines to the new long one. 2010// This loops backwards over the joined lines, including the original line. 2011// 2012// Move marks from each deleted line to the joined line, adjusting the 2013// column. This is not Vi compatible, but Vi deletes the marks, thus that 2014// should not really be a problem. 2015 2016curbuf_splice_pending ++ ; 2017 2018for (linenr_T t = (linenr_T )count - 1 ;;t -- ) { 2019cend -= currsize ; 2020memmove (cend ,curr , (size_t )currsize ); 2021 2022if (spaces [t ]> 0 ) { 2023cend -= spaces [t ]; 2024memset (cend ,' ' , (size_t )(spaces [t ])); 2025 } 2026 2027// If deleting more spaces than adding, the cursor moves no more than 2028// what is added if it is inside these spaces. 2029const int spaces_removed = (int )((curr - curr_start )- spaces [t ]); 2030linenr_T lnum = curwin -> w_cursor .lnum + t ; 2031colnr_T mincol = 0 ; 2032linenr_T lnum_amount = - t ; 2033colnr_T col_amount = (colnr_T )(cend - newp - spaces_removed ); 2034 2035mark_col_adjust (lnum ,mincol ,lnum_amount ,col_amount ,spaces_removed ); 2036 2037if (t == 0 ) { 2038break ; 2039 } 2040 2041curr_start = ml_get ((linenr_T )(curwin -> w_cursor .lnum + t - 1 )); 2042curr = curr_start ; 2043if (remove_comments ) { 2044curr += comments [t - 1 ]; 2045 } 2046if (insert_space && t > 1 ) { 2047curr = skipwhite (curr ); 2048 } 2049currsize = (int )strlen (curr ); 2050 } 2051 2052ml_replace_len (curwin -> w_cursor .lnum ,newp ,newp_len , false); 2053 2054if (setmark && (cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 2055// Set the '] mark. 2056curwin -> w_buffer -> b_op_end .lnum = curwin -> w_cursor .lnum ; 2057curwin -> w_buffer -> b_op_end .col = sumsize ; 2058 } 2059 2060// Only report the change in the first line here, del_lines() will report 2061// the deleted line. 2062changed_lines (curbuf ,curwin -> w_cursor .lnum ,currsize , 2063curwin -> w_cursor .lnum + 1 ,0 , true); 2064 2065// Delete following lines. To do this we move the cursor there 2066// briefly, and then move it back. After del_lines() the cursor may 2067// have moved up (last line deleted), so the current lnum is kept in t. 2068linenr_T t = curwin -> w_cursor .lnum ; 2069curwin -> w_cursor .lnum ++ ; 2070del_lines ((int )count - 1 , false); 2071curwin -> w_cursor .lnum = t ; 2072curbuf_splice_pending -- ; 2073curbuf -> deleted_bytes2 = 0 ; 2074 2075// Set the cursor column: 2076// Vi compatible: use the column of the first join 2077// vim: use the column of the last join 2078curwin -> w_cursor .col = 2079 (vim_strchr (p_cpo ,kCpoJoincol )!= NULL ?currsize :col ); 2080check_cursor_col (curwin ); 2081 2082curwin -> w_cursor .coladd = 0 ; 2083curwin -> w_set_curswant = true; 2084 2085theend : 2086xfree (spaces ); 2087if (remove_comments ) { 2088xfree (comments ); 2089 } 2090return ret ; 2091} 2092 2093/// TODO(zeertzjq): consider using CharSize.tail instead of temporarily 2094/// resetting 'linebreak'. 2095/// 2096/// Reset 'linebreak' and take care of side effects. 2097/// @return the previous value, to be passed to restore_lbr(). 2098bool reset_lbr (void ) 2099{ 2100if (!curwin -> w_p_lbr ) { 2101return false; 2102 } 2103// changing 'linebreak' may require w_virtcol to be updated 2104curwin -> w_p_lbr = false; 2105curwin -> w_valid &= ~(VALID_WROW |VALID_WCOL |VALID_VIRTCOL ); 2106return true; 2107} 2108 2109/// Restore 'linebreak' and take care of side effects. 2110void restore_lbr (bool lbr_saved ) 2111{ 2112if (curwin -> w_p_lbr || !lbr_saved ) { 2113return ; 2114 } 2115 2116// changing 'linebreak' may require w_virtcol to be updated 2117curwin -> w_p_lbr = true; 2118curwin -> w_valid &= ~(VALID_WROW |VALID_WCOL |VALID_VIRTCOL ); 2119} 2120 2121/// prepare a few things for block mode yank/delete/tilde 2122/// 2123/// for delete: 2124/// - textlen includes the first/last char to be (partly) deleted 2125/// - start/endspaces is the number of columns that are taken by the 2126/// first/last deleted char minus the number of columns that have to be 2127/// deleted. 2128/// for yank and tilde: 2129/// - textlen includes the first/last char to be wholly yanked 2130/// - start/endspaces is the number of columns of the first/last yanked char 2131/// that are to be yanked. 2132void block_prep (oparg_T * oap ,struct block_def * bdp ,linenr_T lnum ,bool is_del ) 2133{ 2134int incr = 0 ; 2135// Avoid a problem with unwanted linebreaks in block mode. 2136const bool lbr_saved = reset_lbr (); 2137 2138bdp -> startspaces = 0 ; 2139bdp -> endspaces = 0 ; 2140bdp -> textlen = 0 ; 2141bdp -> start_vcol = 0 ; 2142bdp -> end_vcol = 0 ; 2143bdp -> is_short = false; 2144bdp -> is_oneChar = false; 2145bdp -> pre_whitesp = 0 ; 2146bdp -> pre_whitesp_c = 0 ; 2147bdp -> end_char_vcols = 0 ; 2148bdp -> start_char_vcols = 0 ; 2149 2150char * line = ml_get (lnum ); 2151char * prev_pstart = line ; 2152 2153CharsizeArg csarg ; 2154CSType cstype = init_charsize_arg (& csarg ,curwin ,lnum ,line ); 2155StrCharInfo ci = utf_ptr2StrCharInfo (line ); 2156int vcol = bdp -> start_vcol ; 2157while (vcol < oap -> start_vcol && * ci .ptr != NUL ) { 2158ClusterInfo cli = utf_ClusterInfo (ci ); 2159incr = win_charsize (cstype ,vcol ,ci .ptr ,ci .chr .value ,& csarg ,cli .cells ).width ; 2160vcol += incr ; 2161if (ascii_iswhite (ci .chr .value )) { 2162bdp -> pre_whitesp += incr ; 2163bdp -> pre_whitesp_c ++ ; 2164 }else { 2165bdp -> pre_whitesp = 0 ; 2166bdp -> pre_whitesp_c = 0 ; 2167 } 2168prev_pstart = ci .ptr ; 2169ci = cli .next ; 2170 } 2171bdp -> start_vcol = vcol ; 2172char * pstart = ci .ptr ; 2173 2174bdp -> start_char_vcols = incr ; 2175if (bdp -> start_vcol < oap -> start_vcol ) {// line too short 2176bdp -> end_vcol = bdp -> start_vcol ; 2177bdp -> is_short = true; 2178if (!is_del || oap -> op_type == OP_APPEND ) { 2179bdp -> endspaces = oap -> end_vcol - oap -> start_vcol + 1 ; 2180 } 2181 }else { 2182// notice: this converts partly selected Multibyte characters to 2183// spaces, too. 2184bdp -> startspaces = bdp -> start_vcol - oap -> start_vcol ; 2185if (is_del && bdp -> startspaces ) { 2186bdp -> startspaces = bdp -> start_char_vcols - bdp -> startspaces ; 2187 } 2188char * pend = pstart ; 2189bdp -> end_vcol = bdp -> start_vcol ; 2190if (bdp -> end_vcol > oap -> end_vcol ) {// it's all in one character 2191bdp -> is_oneChar = true; 2192if (oap -> op_type == OP_INSERT ) { 2193bdp -> endspaces = bdp -> start_char_vcols - bdp -> startspaces ; 2194 }else if (oap -> op_type == OP_APPEND ) { 2195bdp -> startspaces += oap -> end_vcol - oap -> start_vcol + 1 ; 2196bdp -> endspaces = bdp -> start_char_vcols - bdp -> startspaces ; 2197 }else { 2198bdp -> startspaces = oap -> end_vcol - oap -> start_vcol + 1 ; 2199if (is_del && oap -> op_type != OP_LSHIFT ) { 2200// just putting the sum of those two into 2201// bdp->startspaces doesn't work for Visual replace, 2202// so we have to split the tab in two 2203bdp -> startspaces = bdp -> start_char_vcols 2204- (bdp -> start_vcol - oap -> start_vcol ); 2205bdp -> endspaces = bdp -> end_vcol - oap -> end_vcol - 1 ; 2206 } 2207 } 2208 }else { 2209cstype = init_charsize_arg (& csarg ,curwin ,lnum ,line ); 2210ci = utf_ptr2StrCharInfo (pend ); 2211vcol = bdp -> end_vcol ; 2212char * prev_pend = pend ; 2213while (vcol <=oap -> end_vcol && * ci .ptr != NUL ) { 2214prev_pend = ci .ptr ; 2215ClusterInfo cli = utf_ClusterInfo (ci ); 2216incr = win_charsize (cstype ,vcol ,ci .ptr ,ci .chr .value ,& csarg ,cli .cells ).width ; 2217vcol += incr ; 2218ci = cli .next ; 2219 } 2220bdp -> end_vcol = vcol ; 2221pend = ci .ptr ; 2222 2223if (bdp -> end_vcol <=oap -> end_vcol 2224&& (!is_del 2225|| oap -> op_type == OP_APPEND 2226|| oap -> op_type == OP_REPLACE )) {// line too short 2227bdp -> is_short = true; 2228// Alternative: include spaces to fill up the block. 2229// Disadvantage: can lead to trailing spaces when the line is 2230// short where the text is put 2231// if (!is_del || oap->op_type == OP_APPEND) 2232if (oap -> op_type == OP_APPEND || virtual_op ) { 2233bdp -> endspaces = oap -> end_vcol - bdp -> end_vcol 2234+ oap -> inclusive ; 2235 } 2236 }else if (bdp -> end_vcol > oap -> end_vcol ) { 2237bdp -> endspaces = bdp -> end_vcol - oap -> end_vcol - 1 ; 2238if (!is_del && bdp -> endspaces ) { 2239bdp -> endspaces = incr - bdp -> endspaces ; 2240if (pend != pstart ) { 2241pend = prev_pend ; 2242 } 2243 } 2244 } 2245 } 2246bdp -> end_char_vcols = incr ; 2247if (is_del && bdp -> startspaces ) { 2248pstart = prev_pstart ; 2249 } 2250bdp -> textlen = (int )(pend - pstart ); 2251 } 2252bdp -> textcol = (colnr_T )(pstart - line ); 2253bdp -> textstart = pstart ; 2254restore_lbr (lbr_saved ); 2255} 2256 2257/// Preps a region for per-line extraction (getregionpos_line()): orders `p1`/`p2`, applies 2258/// `exclusive` ('selection'), and moves `p2` to the last byte of its char. Blockwise fills `oap` 2259/// (`block_width` > 0 sets the width, else the corners decide it). Reads `virtual_op`. 2260/// 2261/// @param[out] inclusive Charwise: false if the region ends before `p2` (exclusive, or at NUL). 2262void getregionpos_prep (pos_T * p1 ,pos_T * p2 ,MotionType region_type ,bool exclusive , 2263int block_width ,bool * inclusive ,oparg_T * oap ) 2264{ 2265* inclusive = true; 2266if (!lt (* p1 ,* p2 )) { 2267pos_T p = * p1 ; 2268* p1 = * p2 ; 2269* p2 = p ; 2270 } 2271 2272if (region_type == kMTCharWise ) { 2273// Handle 'selection' == "exclusive". 2274if (exclusive && !equalpos (* p1 ,* p2 )) { 2275// When backing up to previous line, inclusive becomes false. 2276* inclusive = !unadjust_for_sel_inner (p2 ); 2277 } 2278// If p2 is on NUL (end of line), inclusive becomes false. 2279if (* inclusive && !virtual_op && * ml_get_pos (p2 )== NUL ) { 2280* inclusive = false; 2281 } 2282 }else if (region_type == kMTBlockWise ) { 2283colnr_T sc1 ,ec1 ,sc2 ,ec2 ; 2284const bool lbr_saved = reset_lbr (); 2285getvvcol (curwin ,p1 ,& sc1 ,NULL ,& ec1 ,0 ); 2286getvvcol (curwin ,p2 ,& sc2 ,NULL ,& ec2 ,0 ); 2287restore_lbr (lbr_saved ); 2288oap -> motion_type = kMTBlockWise ; 2289oap -> inclusive = true; 2290oap -> op_type = OP_NOP ; 2291oap -> start = * p1 ; 2292oap -> end = * p2 ; 2293oap -> start_vcol = MIN (sc1 ,sc2 ); 2294if (block_width > 0 ) { 2295oap -> end_vcol = oap -> start_vcol + block_width - 1 ; 2296 }else if (exclusive && ec1 < sc2 && 0 < sc2 && ec2 > ec1 ) { 2297oap -> end_vcol = sc2 - 1 ; 2298 }else { 2299oap -> end_vcol = MAX (ec1 ,ec2 ); 2300 } 2301 } 2302 2303// Include the trailing byte of a multi-byte char. 2304int l = utfc_ptr2len (ml_get_pos (p2 )); 2305if (l > 1 ) { 2306p2 -> col += l - 1 ; 2307 } 2308} 2309 2310/// Compute the positions of the region segment on line "lnum". 2311/// "ret_p1" is set to the start position of the segment and "ret_p2" to its 2312/// end position. 2313void getregionpos_line (linenr_T lnum ,pos_T p1 ,pos_T p2 ,bool inclusive ,MotionType region_type , 2314oparg_T * oap ,bool allow_eol ,pos_T * ret_p1 ,pos_T * ret_p2 ) 2315{ 2316char * line = ml_get (lnum ); 2317colnr_T line_len = ml_get_len (lnum ); 2318 2319if (region_type == kMTLineWise ) { 2320ret_p1 -> col = 1 ; 2321ret_p1 -> coladd = 0 ; 2322ret_p2 -> col = MAXCOL ; 2323ret_p2 -> coladd = 0 ; 2324 }else { 2325struct block_def bd ; 2326 2327if (region_type == kMTBlockWise ) { 2328block_prep (oap ,& bd ,lnum , false); 2329 }else { 2330charwise_block_prep (p1 ,p2 ,& bd ,lnum ,inclusive ); 2331 } 2332 2333if (bd .is_oneChar ) {// selection entirely inside one char 2334if (region_type == kMTBlockWise ) { 2335ret_p1 -> col = (colnr_T )(mb_prevptr (line ,bd .textstart )- line )+ 1 ; 2336ret_p1 -> coladd = bd .start_char_vcols - (bd .start_vcol - oap -> start_vcol ); 2337 }else { 2338ret_p1 -> col = p1 .col + 1 ; 2339ret_p1 -> coladd = p1 .coladd ; 2340 } 2341 }else if (region_type == kMTBlockWise && oap -> start_vcol > bd .start_vcol ) { 2342// blockwise selection entirely beyond end of line 2343ret_p1 -> col = MAXCOL ; 2344ret_p1 -> coladd = oap -> start_vcol - bd .start_vcol ; 2345bd .is_oneChar = true; 2346 }else if (bd .startspaces > 0 ) { 2347ret_p1 -> col = (colnr_T )(mb_prevptr (line ,bd .textstart )- line )+ 1 ; 2348ret_p1 -> coladd = bd .start_char_vcols - bd .startspaces ; 2349 }else { 2350ret_p1 -> col = bd .textcol + 1 ; 2351ret_p1 -> coladd = 0 ; 2352 } 2353 2354if (bd .is_oneChar ) {// selection entirely inside one char 2355ret_p2 -> col = ret_p1 -> col ; 2356ret_p2 -> coladd = ret_p1 -> coladd + bd .startspaces + bd .endspaces ; 2357 }else if (bd .endspaces > 0 ) { 2358ret_p2 -> col = bd .textcol + bd .textlen + 1 ; 2359ret_p2 -> coladd = bd .endspaces ; 2360 }else { 2361ret_p2 -> col = bd .textcol + bd .textlen ; 2362ret_p2 -> coladd = 0 ; 2363 } 2364 } 2365 2366if (!allow_eol && ret_p1 -> col > line_len ) { 2367ret_p1 -> col = 0 ; 2368ret_p1 -> coladd = 0 ; 2369 }else if (ret_p1 -> col > line_len + 1 ) { 2370ret_p1 -> col = line_len + 1 ; 2371 } 2372 2373if (!allow_eol && ret_p2 -> col > line_len ) { 2374ret_p2 -> col = ret_p1 -> col == 0 ?0 :line_len ; 2375ret_p2 -> coladd = 0 ; 2376 }else if (ret_p2 -> col > line_len + 1 ) { 2377ret_p2 -> col = line_len + 1 ; 2378 } 2379 2380ret_p1 -> lnum = lnum ; 2381ret_p2 -> lnum = lnum ; 2382} 2383 2384/// Get block text from "start" to "end" 2385void charwise_block_prep (pos_T start ,pos_T end ,struct block_def * bdp ,linenr_T lnum , 2386bool inclusive ) 2387{ 2388colnr_T startcol = 0 ; 2389colnr_T endcol = MAXCOL ; 2390colnr_T cs ,ce ; 2391char * p = ml_get (lnum ); 2392int plen = ml_get_len (lnum ); 2393 2394bdp -> startspaces = 0 ; 2395bdp -> endspaces = 0 ; 2396bdp -> is_oneChar = false; 2397bdp -> start_char_vcols = 0 ; 2398 2399if (lnum == start .lnum ) { 2400startcol = start .col ; 2401if (virtual_op ) { 2402getvcol (curwin ,& start ,& cs ,NULL ,& ce ,0 ); 2403if (ce != cs && start .coladd > 0 ) { 2404// Part of a tab selected -- but don't double-count it. 2405bdp -> start_char_vcols = ce - cs + 1 ; 2406bdp -> startspaces = MAX (bdp -> start_char_vcols - start .coladd ,0 ); 2407startcol ++ ; 2408 } 2409 } 2410 } 2411 2412if (lnum == end .lnum ) { 2413endcol = end .col ; 2414if (virtual_op ) { 2415getvcol (curwin ,& end ,& cs ,NULL ,& ce ,0 ); 2416if (p [endcol ]== NUL || (cs + end .coladd < ce 2417// Don't add space for double-wide 2418// char; endcol will be on last byte 2419// of multi-byte char. 2420&& utf_head_off (p ,p + endcol )== 0 )) { 2421if (start .lnum == end .lnum && start .col == end .col ) { 2422// Special case: inside a single char 2423bdp -> is_oneChar = true; 2424bdp -> startspaces = end .coladd - start .coladd + inclusive ; 2425endcol = startcol ; 2426 }else { 2427bdp -> endspaces = end .coladd + inclusive ; 2428endcol -= inclusive ; 2429 } 2430 } 2431 } 2432 } 2433if (endcol == MAXCOL ) { 2434endcol = ml_get_len (lnum ); 2435 } 2436if (startcol > endcol || bdp -> is_oneChar ) { 2437bdp -> textlen = 0 ; 2438 }else { 2439bdp -> textlen = endcol - startcol + inclusive ; 2440 } 2441bdp -> textcol = startcol ; 2442bdp -> textstart = startcol <=plen ?p + startcol :p ; 2443} 2444 2445/// Handle the add/subtract operator. 2446/// 2447/// @param[in] oap Arguments of operator. 2448/// @param[in] Prenum1 Amount of addition or subtraction. 2449/// @param[in] g_cmd Prefixed with `g`. 2450void op_addsub (oparg_T * oap ,linenr_T Prenum1 ,bool g_cmd ) 2451{ 2452struct block_def bd ; 2453ssize_t change_cnt = 0 ; 2454linenr_T amount = Prenum1 ; 2455 2456// do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the 2457// buffer is not completely updated yet. Postpone updating folds until before 2458// the call to changed_lines(). 2459disable_fold_update ++ ; 2460 2461if (!Visual .active ) { 2462pos_T pos = curwin -> w_cursor ; 2463if (u_save_cursor ()== FAIL ) { 2464disable_fold_update -- ; 2465return ; 2466 } 2467change_cnt = do_addsub (oap -> op_type ,& pos ,0 ,amount ); 2468disable_fold_update -- ; 2469if (change_cnt ) { 2470changed_lines (curbuf ,pos .lnum ,0 ,pos .lnum + 1 ,0 , true); 2471 } 2472 }else { 2473int length ; 2474pos_T startpos ; 2475 2476if (u_save ((linenr_T )(oap -> start .lnum - 1 ), 2477 (linenr_T )(oap -> end .lnum + 1 ))== FAIL ) { 2478disable_fold_update -- ; 2479return ; 2480 } 2481 2482pos_T pos = oap -> start ; 2483for (;pos .lnum <=oap -> end .lnum ;pos .lnum ++ ) { 2484if (oap -> motion_type == kMTBlockWise ) { 2485// Visual block mode 2486block_prep (oap ,& bd ,pos .lnum , false); 2487pos .col = bd .textcol ; 2488length = bd .textlen ; 2489 }else if (oap -> motion_type == kMTLineWise ) { 2490curwin -> w_cursor .col = 0 ; 2491pos .col = 0 ; 2492length = ml_get_len (pos .lnum ); 2493 }else { 2494// oap->motion_type == kMTCharWise 2495if (pos .lnum == oap -> start .lnum && !oap -> inclusive ) { 2496dec (& (oap -> end )); 2497 } 2498length = ml_get_len (pos .lnum ); 2499pos .col = 0 ; 2500if (pos .lnum == oap -> start .lnum ) { 2501pos .col += oap -> start .col ; 2502length -= oap -> start .col ; 2503 } 2504if (pos .lnum == oap -> end .lnum ) { 2505length = ml_get_len (oap -> end .lnum ); 2506oap -> end .col = MIN (oap -> end .col ,length - 1 ); 2507length = oap -> end .col - pos .col + 1 ; 2508 } 2509 } 2510bool one_change = do_addsub (oap -> op_type ,& pos ,length ,amount ); 2511if (one_change ) { 2512// Remember the start position of the first change. 2513if (change_cnt == 0 ) { 2514startpos = curbuf -> b_op_start ; 2515 } 2516change_cnt ++ ; 2517 } 2518 2519if (g_cmd && one_change ) { 2520amount += Prenum1 ; 2521 } 2522 } 2523 2524disable_fold_update -- ; 2525if (change_cnt ) { 2526changed_lines (curbuf ,oap -> start .lnum ,0 ,oap -> end .lnum + 1 ,0 , true); 2527 } 2528 2529if (!change_cnt && oap -> is_VIsual ) { 2530// No change: need to remove the Visual selection 2531redraw_curbuf_later (UPD_INVERTED ); 2532 } 2533 2534// Set '[ mark if something changed. Keep the last end 2535// position from do_addsub(). 2536if (change_cnt > 0 && (cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 2537curbuf -> b_op_start = startpos ; 2538 } 2539 2540if (change_cnt > p_report ) { 2541smsg (0 ,NGETTEXT ("%" PRId64 " lines changed" ,"%" PRId64 " lines changed" ,change_cnt ), 2542 (int64_t )change_cnt ); 2543 } 2544 } 2545} 2546 2547/// Add or subtract from a number in a line. 2548/// 2549/// @param op_type OP_NR_ADD or OP_NR_SUB. 2550/// @param pos Cursor position. 2551/// @param length Target number length. 2552/// @param Prenum1 Amount of addition or subtraction. 2553/// 2554/// @return true if some character was changed. 2555bool do_addsub (int op_type ,pos_T * pos ,int length ,linenr_T Prenum1 ) 2556{ 2557int pre ;// 'X' or 'x': hex; '0': octal; 'B' or 'b': bin 2558static bool hexupper = false;// 0xABC 2559uvarnumber_T n ; 2560bool blank_unsigned = false;// blank: treat as unsigned? 2561bool negative = false; 2562bool was_positive = true; 2563bool visual = Visual .active ; 2564bool did_change = false; 2565pos_T save_cursor = curwin -> w_cursor ; 2566int maxlen = 0 ; 2567pos_T startpos ; 2568pos_T endpos ; 2569colnr_T save_coladd = 0 ; 2570 2571const bool do_hex = vim_strchr (curbuf -> b_p_nf ,'x' )!= NULL ;// "heX" 2572const bool do_oct = vim_strchr (curbuf -> b_p_nf ,'o' )!= NULL ;// "Octal" 2573const bool do_bin = vim_strchr (curbuf -> b_p_nf ,'b' )!= NULL ;// "Bin" 2574const bool do_alpha = vim_strchr (curbuf -> b_p_nf ,'p' )!= NULL ;// "alPha" 2575const bool do_unsigned = vim_strchr (curbuf -> b_p_nf ,'u' )!= NULL ;// "Unsigned" 2576const bool do_blank = vim_strchr (curbuf -> b_p_nf ,'k' )!= NULL ;// "blanK" 2577 2578if (virtual_active (curwin )) { 2579save_coladd = pos -> coladd ; 2580pos -> coladd = 0 ; 2581 } 2582 2583curwin -> w_cursor = * pos ; 2584char * ptr = ml_get (pos -> lnum ); 2585int linelen = ml_get_len (pos -> lnum ); 2586int col = pos -> col ; 2587 2588if (col + !!save_coladd >=linelen ) { 2589 gototheend ; 2590 } 2591 2592// First check if we are on a hexadecimal number, after the "0x". 2593if (!Visual .active ) { 2594if (do_bin ) { 2595while (col > 0 && ascii_isbdigit (ptr [col ])) { 2596col -- ; 2597col -= utf_head_off (ptr ,ptr + col ); 2598 } 2599 } 2600 2601if (do_hex ) { 2602while (col > 0 && ascii_isxdigit (ptr [col ])) { 2603col -- ; 2604col -= utf_head_off (ptr ,ptr + col ); 2605 } 2606 } 2607if (do_bin 2608&& do_hex 2609&& !(col > 0 2610&& (ptr [col ]== 'X' || ptr [col ]== 'x' ) 2611&& ptr [col - 1 ]== '0' 2612&& !utf_head_off (ptr ,ptr + col - 1 ) 2613&& ascii_isxdigit (ptr [col + 1 ]))) { 2614// In case of binary/hexadecimal pattern overlap match, rescan 2615 2616col = curwin -> w_cursor .col ; 2617 2618while (col > 0 && ascii_isdigit (ptr [col ])) { 2619col -- ; 2620col -= utf_head_off (ptr ,ptr + col ); 2621 } 2622 } 2623 2624if ((do_hex 2625&& col > 0 2626&& (ptr [col ]== 'X' || ptr [col ]== 'x' ) 2627&& ptr [col - 1 ]== '0' 2628&& !utf_head_off (ptr ,ptr + col - 1 ) 2629&& ascii_isxdigit (ptr [col + 1 ])) 2630|| (do_bin 2631&& col > 0 2632&& (ptr [col ]== 'B' || ptr [col ]== 'b' ) 2633&& ptr [col - 1 ]== '0' 2634&& !utf_head_off (ptr ,ptr + col - 1 ) 2635&& ascii_isbdigit (ptr [col + 1 ]))) { 2636// Found hexadecimal or binary number, move to its start. 2637col -- ; 2638col -= utf_head_off (ptr ,ptr + col ); 2639 }else { 2640// Search forward and then backward to find the start of number. 2641col = pos -> col ; 2642 2643while (ptr [col ]!= NUL 2644&& !ascii_isdigit (ptr [col ]) 2645&& !(do_alpha && ASCII_ISALPHA (ptr [col ]))) { 2646col ++ ; 2647 } 2648 2649while (col > 0 2650&& ascii_isdigit (ptr [col - 1 ]) 2651&& !(do_alpha && ASCII_ISALPHA (ptr [col ]))) { 2652col -- ; 2653 } 2654 } 2655 } 2656 2657if (visual ) { 2658while (ptr [col ]!= NUL && length > 0 && !ascii_isdigit (ptr [col ]) 2659&& !(do_alpha && ASCII_ISALPHA (ptr [col ]))) { 2660int mb_len = utfc_ptr2len (ptr + col ); 2661 2662col += mb_len ; 2663length -= mb_len ; 2664 } 2665 2666if (length == 0 ) { 2667 gototheend ; 2668 } 2669 2670if (col > pos -> col && ptr [col - 1 ]== '-' 2671&& !utf_head_off (ptr ,ptr + col - 1 ) 2672&& !do_unsigned ) { 2673if (do_blank && col >=2 && !ascii_iswhite (ptr [col - 2 ])) { 2674blank_unsigned = true; 2675 }else { 2676negative = true; 2677was_positive = false; 2678 } 2679 } 2680 } 2681 2682// If a number was found, and saving for undo works, replace the number. 2683int firstdigit = (uint8_t )ptr [col ]; 2684if (!ascii_isdigit (firstdigit )&& !(do_alpha && ASCII_ISALPHA (firstdigit ))) { 2685beep_flush (); 2686 gototheend ; 2687 } 2688 2689if (do_alpha && ASCII_ISALPHA (firstdigit )) { 2690// decrement or increment alphabetic character 2691if (op_type == OP_NR_SUB ) { 2692if (CHAR_ORD (firstdigit )< Prenum1 ) { 2693firstdigit = isupper (firstdigit ) ?'A' :'a' ; 2694 }else { 2695firstdigit -= (int )Prenum1 ; 2696 } 2697 }else { 2698if (26 - CHAR_ORD (firstdigit )- 1 < Prenum1 ) { 2699firstdigit = isupper (firstdigit ) ?'Z' :'z' ; 2700 }else { 2701firstdigit += (int )Prenum1 ; 2702 } 2703 } 2704curwin -> w_cursor .col = col ; 2705startpos = curwin -> w_cursor ; 2706did_change = true; 2707del_char (false); 2708ins_char (firstdigit ); 2709endpos = curwin -> w_cursor ; 2710curwin -> w_cursor .col = col ; 2711 }else { 2712if (col > 0 && ptr [col - 1 ]== '-' 2713&& !utf_head_off (ptr ,ptr + col - 1 ) 2714&& !visual 2715&& !do_unsigned ) { 2716if (do_blank && col >=2 && !ascii_iswhite (ptr [col - 2 ])) { 2717blank_unsigned = true; 2718 }else { 2719// negative number 2720col -- ; 2721negative = true; 2722 } 2723 } 2724 2725// get the number value (unsigned) 2726if (visual && Visual .mode != 'V' ) { 2727maxlen = curbuf -> b_visual .vi_curswant == MAXCOL ?linelen - col :length ; 2728 } 2729 2730bool overflow = false; 2731vim_str2nr (ptr + col ,& pre ,& length , 27320 + (do_bin ?STR2NR_BIN :0 ) 2733+ (do_oct ?STR2NR_OCT :0 ) 2734+ (do_hex ?STR2NR_HEX :0 ), 2735NULL ,& n ,maxlen , false,& overflow ); 2736 2737// ignore leading '-' for hex, octal and bin numbers 2738if (pre && negative ) { 2739col ++ ; 2740length -- ; 2741negative = false; 2742 } 2743 2744// add or subtract 2745bool subtract = false; 2746if (op_type == OP_NR_SUB ) { 2747subtract ^= true; 2748 } 2749if (negative ) { 2750subtract ^= true; 2751 } 2752 2753uvarnumber_T oldn = n ; 2754 2755if (!overflow ) {// if number is too big don't add/subtract 2756n = subtract ?n - (uvarnumber_T )Prenum1 2757 :n + (uvarnumber_T )Prenum1 ; 2758 } 2759 2760// handle wraparound for decimal numbers 2761if (!pre ) { 2762if (subtract ) { 2763if (n > oldn ) { 2764n = 1 + (n ^ (uvarnumber_T )(-1 )); 2765negative ^= true; 2766 } 2767 }else { 2768// add 2769if (n < oldn ) { 2770n = (n ^ (uvarnumber_T )(-1 )); 2771negative ^= true; 2772 } 2773 } 2774if (n == 0 ) { 2775negative = false; 2776 } 2777 } 2778 2779if ((do_unsigned || blank_unsigned )&& negative ) { 2780if (subtract ) { 2781// sticking at zero. 2782n = 0 ; 2783 }else { 2784// sticking at 2^64 - 1. 2785n = (uvarnumber_T )(-1 ); 2786 } 2787negative = false; 2788 } 2789 2790if (visual && !was_positive && !negative && col > 0 ) { 2791// need to remove the '-' 2792col -- ; 2793length ++ ; 2794 } 2795 2796// Delete the old number. 2797curwin -> w_cursor .col = col ; 2798startpos = curwin -> w_cursor ; 2799did_change = true; 2800int todel = length ; 2801int c = gchar_cursor (); 2802 2803// Don't include the '-' in the length, only the length of the part 2804// after it is kept the same. 2805if (c == '-' ) { 2806length -- ; 2807 } 2808while (todel -- > 0 ) { 2809if (c < 0x100 && isalpha (c )) { 2810hexupper = isupper (c ); 2811 } 2812// del_char() will mark line needing displaying 2813del_char (false); 2814c = gchar_cursor (); 2815 } 2816 2817// Prepare the leading characters in buf1[]. 2818// When there are many leading zeros it could be very long. 2819// Allocate a bit too much. 2820char * buf1 = xmalloc ((size_t )length + NUMBUFLEN ); 2821ptr = buf1 ; 2822if (negative && (!visual || was_positive )) { 2823* ptr ++ = '-' ; 2824 } 2825if (pre ) { 2826* ptr ++ = '0' ; 2827length -- ; 2828 } 2829if (pre == 'b' || pre == 'B' || pre == 'x' || pre == 'X' ) { 2830* ptr ++ = (char )pre ; 2831length -- ; 2832 } 2833 2834// Put the number characters in buf2[]. 2835char buf2 [NUMBUFLEN ]; 2836int buf2len = 0 ; 2837if (pre == 'b' || pre == 'B' ) { 2838size_t bits = 0 ; 2839 2840// leading zeros 2841for (bits = 8 * sizeof (n );bits > 0 ;bits -- ) { 2842if ((n >> (bits - 1 ))& 0x1 ) { 2843break ; 2844 } 2845 } 2846 2847while (bits > 0 && buf2len < NUMBUFLEN - 1 ) { 2848buf2 [buf2len ++ ]= ((n >>-- bits )& 0x1 ) ?'1' :'0' ; 2849 } 2850 2851buf2 [buf2len ]= NUL ; 2852 }else if (pre == 0 ) { 2853buf2len = vim_snprintf (buf2 ,ARRAY_SIZE (buf2 ),"%" PRIu64 , (uint64_t )n ); 2854 }else if (pre == '0' ) { 2855buf2len = vim_snprintf (buf2 ,ARRAY_SIZE (buf2 ),"%" PRIo64 , (uint64_t )n ); 2856 }else if (hexupper ) { 2857buf2len = vim_snprintf (buf2 ,ARRAY_SIZE (buf2 ),"%" PRIX64 , (uint64_t )n ); 2858 }else { 2859buf2len = vim_snprintf (buf2 ,ARRAY_SIZE (buf2 ),"%" PRIx64 , (uint64_t )n ); 2860 } 2861length -= buf2len ; 2862 2863// Adjust number of zeros to the new number of digits, so the 2864// total length of the number remains the same. 2865// Don't do this when 2866// the result may look like an octal number. 2867if (firstdigit == '0' && !(do_oct && pre == 0 )) { 2868while (length -- > 0 ) { 2869* ptr ++ = '0' ; 2870 } 2871 } 2872* ptr = NUL ; 2873int buf1len = (int )(ptr - buf1 ); 2874 2875STRCPY (buf1 + buf1len ,buf2 ); 2876buf1len += buf2len ; 2877 2878ins_str (buf1 , (size_t )buf1len );// insert the new number 2879xfree (buf1 ); 2880 2881endpos = curwin -> w_cursor ; 2882if (curwin -> w_cursor .col ) { 2883curwin -> w_cursor .col -- ; 2884 } 2885 } 2886 2887if ((cmdmod .cmod_flags & CMOD_LOCKMARKS )== 0 ) { 2888// set the '[ and '] marks 2889curbuf -> b_op_start = startpos ; 2890curbuf -> b_op_end = endpos ; 2891if (curbuf -> b_op_end .col > 0 ) { 2892curbuf -> b_op_end .col -- ; 2893 } 2894 } 2895 2896theend : 2897if (visual ) { 2898curwin -> w_cursor = save_cursor ; 2899 }else if (did_change ) { 2900curwin -> w_set_curswant = true; 2901 }else if (virtual_active (curwin )) { 2902curwin -> w_cursor .coladd = save_coladd ; 2903 } 2904 2905return did_change ; 2906} 2907 2908void clear_oparg (oparg_T * oap ) 2909{ 2910CLEAR_POINTER (oap ); 2911} 2912 2913/// Count the number of bytes, characters and "words" in a line. 2914/// 2915/// "Words" are counted by looking for boundaries between non-space and 2916/// space characters. (it seems to produce results that match 'wc'.) 2917/// 2918/// Return value is byte count; word count for the line is added to "*wc". 2919/// Char count is added to "*cc". 2920/// 2921/// The function will only examine the first "limit" characters in the 2922/// line, stopping if it encounters an end-of-line (NUL byte). In that 2923/// case, eol_size will be added to the character count to account for 2924/// the size of the EOL character. 2925static varnumber_T line_count_info (char * line ,varnumber_T * wc ,varnumber_T * cc ,varnumber_T limit , 2926int eol_size ) 2927{ 2928varnumber_T i ; 2929varnumber_T words = 0 ; 2930varnumber_T chars = 0 ; 2931bool is_word = false; 2932 2933for (i = 0 ;i < limit && line [i ]!= NUL ;) { 2934if (is_word ) { 2935if (ascii_isspace (line [i ])) { 2936words ++ ; 2937is_word = false; 2938 } 2939 }else if (!ascii_isspace (line [i ])) { 2940is_word = true; 2941 } 2942chars ++ ; 2943i += utfc_ptr2len (line + i ); 2944 } 2945 2946if (is_word ) { 2947words ++ ; 2948 } 2949* wc += words ; 2950 2951// Add eol_size if the end of line was reached before hitting limit. 2952if (i < limit && line [i ]== NUL ) { 2953i += eol_size ; 2954chars += eol_size ; 2955 } 2956* cc += chars ; 2957return i ; 2958} 2959 2960/// Give some info about the position of the cursor (for "g CTRL-G"). 2961/// In Visual mode, give some info about the selected region. (In this case, 2962/// the *_count_cursor variables store running totals for the selection.) 2963/// 2964/// @param dict when not NULL, store the info there instead of showing it. 2965void cursor_pos_info (dict_T * dict ) 2966{ 2967char buf1 [50 ]; 2968char buf2 [40 ]; 2969varnumber_T byte_count = 0 ; 2970varnumber_T bom_count = 0 ; 2971varnumber_T byte_count_cursor = 0 ; 2972varnumber_T char_count = 0 ; 2973varnumber_T char_count_cursor = 0 ; 2974varnumber_T word_count = 0 ; 2975varnumber_T word_count_cursor = 0 ; 2976pos_T min_pos ,max_pos ; 2977oparg_T oparg ; 2978struct block_def bd ; 2979const int l_VIsual_active = Visual .active ; 2980const int l_VIsual_mode = Visual .mode ; 2981 2982// Compute the length of the file in characters. 2983if (curbuf -> b_ml .ml_flags & ML_EMPTY ) { 2984if (dict == NULL ) { 2985msg (_ (no_lines_msg ),0 ); 2986return ; 2987 } 2988 }else { 2989int eol_size ; 2990varnumber_T last_check = 100000 ; 2991int line_count_selected = 0 ; 2992if (get_fileformat (curbuf )== EOL_DOS ) { 2993eol_size = 2 ; 2994 }else { 2995eol_size = 1 ; 2996 } 2997 2998if (l_VIsual_active ) { 2999if (lt (Visual .start ,curwin -> w_cursor )) { 3000min_pos = Visual .start ; 3001max_pos = curwin -> w_cursor ; 3002 }else { 3003min_pos = curwin -> w_cursor ; 3004max_pos = Visual .start ; 3005 } 3006if (* p_sel == 'e' && max_pos .col > 0 ) { 3007max_pos .col -- ; 3008 } 3009 3010if (l_VIsual_mode == Ctrl_V ) { 3011char * const saved_sbr = p_sbr ; 3012char * const saved_w_sbr = curwin -> w_p_sbr ; 3013 3014// Make 'sbr' empty for a moment to get the correct size. 3015p_sbr = empty_string_option ; 3016curwin -> w_p_sbr = empty_string_option ; 3017oparg .is_VIsual = true; 3018oparg .motion_type = kMTBlockWise ; 3019oparg .op_type = OP_NOP ; 3020getvcols (curwin ,& min_pos ,& max_pos ,& oparg .start_vcol ,& oparg .end_vcol ,0 ); 3021p_sbr = saved_sbr ; 3022curwin -> w_p_sbr = saved_w_sbr ; 3023if (curwin -> w_curswant == MAXCOL ) { 3024oparg .end_vcol = MAXCOL ; 3025 } 3026// Swap the start, end vcol if needed 3027if (oparg .end_vcol < oparg .start_vcol ) { 3028oparg .end_vcol += oparg .start_vcol ; 3029oparg .start_vcol = oparg .end_vcol - oparg .start_vcol ; 3030oparg .end_vcol -= oparg .start_vcol ; 3031 } 3032 } 3033line_count_selected = max_pos .lnum - min_pos .lnum + 1 ; 3034 } 3035 3036for (linenr_T lnum = 1 ;lnum <=curbuf -> b_ml .ml_line_count ;lnum ++ ) { 3037// Check for a CTRL-C every 100000 characters. 3038if (byte_count > last_check ) { 3039os_breakcheck (); 3040if (got_int ) { 3041return ; 3042 } 3043last_check = byte_count + 100000 ; 3044 } 3045 3046// Do extra processing for Visual mode. 3047if (l_VIsual_active 3048&& lnum >=min_pos .lnum && lnum <=max_pos .lnum ) { 3049char * s = NULL ; 3050int len = 0 ; 3051 3052switch (l_VIsual_mode ) { 3053case Ctrl_V : 3054virtual_op = virtual_active (curwin ); 3055block_prep (& oparg ,& bd ,lnum , false); 3056virtual_op = kNone ; 3057s = bd .textstart ; 3058len = bd .textlen ; 3059break ; 3060case 'V' : 3061s = ml_get (lnum ); 3062len = MAXCOL ; 3063break ; 3064case 'v' : { 3065colnr_T start_col = (lnum == min_pos .lnum ) 3066 ?min_pos .col :0 ; 3067colnr_T end_col = (lnum == max_pos .lnum ) 3068 ?max_pos .col - start_col + 1 :MAXCOL ; 3069 3070s = ml_get (lnum )+ start_col ; 3071len = end_col ; 3072 } 3073break ; 3074 } 3075if (s != NULL ) { 3076byte_count_cursor += line_count_info (s ,& word_count_cursor , 3077& char_count_cursor ,len ,eol_size ); 3078if (lnum == curbuf -> b_ml .ml_line_count 3079&& !curbuf -> b_p_eol 3080&& (curbuf -> b_p_bin || !curbuf -> b_p_fixeol ) 3081&& (int )strlen (s )< len ) { 3082byte_count_cursor -= eol_size ; 3083 } 3084 } 3085 }else { 3086// In non-visual mode, check for the line the cursor is on 3087if (lnum == curwin -> w_cursor .lnum ) { 3088word_count_cursor += word_count ; 3089char_count_cursor += char_count ; 3090byte_count_cursor = byte_count 3091+ line_count_info (ml_get (lnum ),& word_count_cursor , 3092& char_count_cursor , 3093 (varnumber_T )curwin -> w_cursor .col + 1 , 3094eol_size ); 3095 } 3096 } 3097// Add to the running totals 3098byte_count += line_count_info (ml_get (lnum ),& word_count ,& char_count , 3099 (varnumber_T )MAXCOL ,eol_size ); 3100 } 3101 3102// Correction for when last line doesn't have an EOL. 3103if (!curbuf -> b_p_eol && (curbuf -> b_p_bin || !curbuf -> b_p_fixeol )) { 3104byte_count -= eol_size ; 3105 } 3106 3107if (dict == NULL ) { 3108if (l_VIsual_active ) { 3109if (l_VIsual_mode == Ctrl_V && curwin -> w_curswant < MAXCOL ) { 3110getvcols (curwin ,& min_pos ,& max_pos ,& min_pos .col ,& max_pos .col ,0 ); 3111int64_t cols ; 3112STRICT_SUB (oparg .end_vcol + 1 ,oparg .start_vcol ,& cols ,int64_t ); 3113vim_snprintf (buf1 ,sizeof (buf1 ),_ ("%" PRId64 " Cols; " ), 3114cols ); 3115 }else { 3116buf1 [0 ]= NUL ; 3117 } 3118 3119if (char_count_cursor == byte_count_cursor 3120&& char_count == byte_count ) { 3121vim_snprintf (IObuff ,IOSIZE , 3122_ ("Selected %s%" PRId64 " of %" PRId64 " Lines;" 3123" %" PRId64 " of %" PRId64 " Words;" 3124" %" PRId64 " of %" PRId64 " Bytes" ), 3125buf1 , (int64_t )line_count_selected , 3126 (int64_t )curbuf -> b_ml .ml_line_count , 3127 (int64_t )word_count_cursor , (int64_t )word_count , 3128 (int64_t )byte_count_cursor , (int64_t )byte_count ); 3129 }else { 3130vim_snprintf (IObuff ,IOSIZE , 3131_ ("Selected %s%" PRId64 " of %" PRId64 " Lines;" 3132" %" PRId64 " of %" PRId64 " Words;" 3133" %" PRId64 " of %" PRId64 " Chars;" 3134" %" PRId64 " of %" PRId64 " Bytes" ), 3135buf1 , (int64_t )line_count_selected , 3136 (int64_t )curbuf -> b_ml .ml_line_count , 3137 (int64_t )word_count_cursor , (int64_t )word_count , 3138 (int64_t )char_count_cursor , (int64_t )char_count , 3139 (int64_t )byte_count_cursor , (int64_t )byte_count ); 3140 } 3141 }else { 3142char * p = get_cursor_line_ptr (); 3143validate_virtcol (curwin ); 3144col_print (buf1 ,sizeof (buf1 ), (int )curwin -> w_cursor .col + 1 , 3145 (int )curwin -> w_virtcol + 1 ); 3146col_print (buf2 ,sizeof (buf2 ),get_cursor_line_len (),linetabsize_str (p )); 3147 3148if (char_count_cursor == byte_count_cursor 3149&& char_count == byte_count ) { 3150vim_snprintf (IObuff ,IOSIZE , 3151_ ("Col %s of %s; Line %" PRId64 " of %" PRId64 ";" 3152" Word %" PRId64 " of %" PRId64 ";" 3153" Byte %" PRId64 " of %" PRId64 "" ), 3154buf1 ,buf2 , 3155 (int64_t )curwin -> w_cursor .lnum , 3156 (int64_t )curbuf -> b_ml .ml_line_count , 3157 (int64_t )word_count_cursor , (int64_t )word_count , 3158 (int64_t )byte_count_cursor , (int64_t )byte_count ); 3159 }else { 3160vim_snprintf (IObuff ,IOSIZE , 3161_ ("Col %s of %s; Line %" PRId64 " of %" PRId64 ";" 3162" Word %" PRId64 " of %" PRId64 ";" 3163" Char %" PRId64 " of %" PRId64 ";" 3164" Byte %" PRId64 " of %" PRId64 "" ), 3165buf1 ,buf2 , 3166 (int64_t )curwin -> w_cursor .lnum , 3167 (int64_t )curbuf -> b_ml .ml_line_count , 3168 (int64_t )word_count_cursor , (int64_t )word_count , 3169 (int64_t )char_count_cursor , (int64_t )char_count , 3170 (int64_t )byte_count_cursor , (int64_t )byte_count ); 3171 } 3172 } 3173 } 3174 3175bom_count = bomb_size (); 3176if (dict == NULL && bom_count > 0 ) { 3177const size_t len = strlen (IObuff ); 3178vim_snprintf (IObuff + len ,IOSIZE - len , 3179_ ("(+%" PRId64 " for BOM)" ), (int64_t )bom_count ); 3180 } 3181if (dict == NULL ) { 3182// Don't shorten this message, the user asked for it. 3183char * p = p_shm ; 3184p_shm = "" ; 3185if (p_ch < 1 ) { 3186msg_start (); 3187msg_scroll = true; 3188 } 3189msg (IObuff ,0 ); 3190p_shm = p ; 3191 } 3192 } 3193 3194if (dict != NULL ) { 3195// Don't shorten this message, the user asked for it. 3196tv_dict_add_nr (dict ,S_LEN ("words" ),word_count ); 3197tv_dict_add_nr (dict ,S_LEN ("chars" ),char_count ); 3198tv_dict_add_nr (dict ,S_LEN ("bytes" ),byte_count + bom_count ); 3199 3200STATIC_ASSERT (sizeof ("visual" )== sizeof ("cursor" ), 3201"key_len argument in tv_dict_add_nr is wrong" ); 3202tv_dict_add_nr (dict ,l_VIsual_active ?"visual_bytes" :"cursor_bytes" , 3203sizeof ("visual_bytes" )- 1 ,byte_count_cursor ); 3204tv_dict_add_nr (dict ,l_VIsual_active ?"visual_chars" :"cursor_chars" , 3205sizeof ("visual_chars" )- 1 ,char_count_cursor ); 3206tv_dict_add_nr (dict ,l_VIsual_active ?"visual_words" :"cursor_words" , 3207sizeof ("visual_words" )- 1 ,word_count_cursor ); 3208 } 3209} 3210 3211/// Handle indent, format operators and visual mode ":". Stuff the ":[range]…" cmdline and run it 3212/// here. For OP_COLON/OP_FILTER the user types the rest after the stuffed part. 3213static void op_colon (oparg_T * oap ) 3214{ 3215if (oap -> is_VIsual ) { 3216stuffReadbuff ("'<,'>" ); 3217 }else { 3218// Make the range look nice, so it can be repeated. 3219if (oap -> start .lnum == curwin -> w_cursor .lnum ) { 3220stuffcharReadbuff ('.' ); 3221 }else { 3222stuffnumReadbuff (oap -> start .lnum ); 3223 } 3224 3225// When using !! on a closed fold the range ".!" works best to operate 3226// on, it will be made the whole closed fold later. 3227linenr_T endOfStartFold = oap -> start .lnum ; 3228hasFolding (curwin ,oap -> start .lnum ,NULL ,& endOfStartFold ); 3229if (oap -> end .lnum != oap -> start .lnum && oap -> end .lnum != endOfStartFold ) { 3230// Make it a range with the end line. 3231stuffcharReadbuff (',' ); 3232if (oap -> end .lnum == curwin -> w_cursor .lnum ) { 3233stuffcharReadbuff ('.' ); 3234 }else if (oap -> end .lnum == curbuf -> b_ml .ml_line_count ) { 3235stuffcharReadbuff ('$' ); 3236 }else if (oap -> start .lnum == curwin -> w_cursor .lnum 3237// do not use ".+number" for a closed fold, it would count 3238// folded lines twice 3239&& !hasFolding (curwin ,oap -> end .lnum ,NULL ,NULL )) { 3240stuffReadbuff (".+" ); 3241stuffnumReadbuff (oap -> line_count - 1 ); 3242 }else { 3243stuffnumReadbuff (oap -> end .lnum ); 3244 } 3245 } 3246 } 3247if (oap -> op_type != OP_COLON ) { 3248stuffReadbuff ("!" ); 3249 } 3250if (oap -> op_type == OP_INDENT ) { 3251stuffReadbuff (get_equalprg ()); 3252stuffReadbuff ("\n" ); 3253 }else if (oap -> op_type == OP_FORMAT ) { 3254if (* curbuf -> b_p_fp != NUL ) { 3255stuffReadbuff (curbuf -> b_p_fp ); 3256 }else if (* p_fp != NUL ) { 3257stuffReadbuff (p_fp ); 3258 }else { 3259stuffReadbuff ("fmt" ); 3260 } 3261stuffReadbuff ("\n']" ); 3262 } 3263 3264do_cmdline (NULL ,getexline ,NULL ,0 ); 3265// Keys stuffed past the cmdline ("']" for OP_FORMAT): internal cleanup, not an atom. 3266atom_suppress (true); 3267exec_stuffed (NULL ); 3268atom_suppress (false); 3269} 3270 3271#ifdef EXITFREE 3272void free_operatorfunc_option (void ) 3273{ 3274callback_free (& p_opfunc ); 3275} 3276#endif 3277 3278/// Mark the global 'operatorfunc' callback with "copyID" so that it is not 3279/// garbage collected. 3280bool set_ref_in_opfunc (int copyID ) 3281{ 3282return set_ref_in_callback (& p_opfunc ,copyID ,NULL ,NULL ); 3283} 3284 3285/// Handle the "g@" operator: call 'operatorfunc'. 3286static void op_function (const oparg_T * oap ) 3287FUNC_ATTR_NONNULL_ALL 3288{ 3289const pos_T orig_start = curbuf -> b_op_start ; 3290const pos_T orig_end = curbuf -> b_op_end ; 3291 3292if (p_opfunc .type == kCallbackNone ) { 3293emsg (_ ("E774: 'operatorfunc' is empty" )); 3294 }else { 3295// Set '[ and '] marks to text to be operated on. 3296curbuf -> b_op_start = oap -> start ; 3297curbuf -> b_op_end = oap -> end ; 3298if (oap -> motion_type != kMTLineWise && !oap -> inclusive ) { 3299// Exclude the end position. 3300decl (& curbuf -> b_op_end ); 3301 } 3302 3303typval_T argv [2 ]; 3304argv [0 ].v_type = VAR_STRING ; 3305argv [1 ].v_type = VAR_UNKNOWN ; 3306argv [0 ].vval .v_string = 3307 (char * )(((const char * const []) { 3308 [kMTBlockWise ]= "block" , 3309 [kMTLineWise ]= "line" , 3310 [kMTCharWise ]= "char" , 3311 })[oap -> motion_type ]); 3312 3313// Reset virtual_op so that 'virtualedit' can be changed in the 3314// function. 3315const TriState save_virtual_op = virtual_op ; 3316virtual_op = kNone ; 3317 3318// Reset finish_op so that mode() returns the right value. 3319const bool save_finish_op = finish_op ; 3320finish_op = false; 3321 3322// Preserve prepped "g@" redo from the callback's own commands. 3323// Like Vimscript call_user_func() does. 3324RedoState save_redo ; 3325save_redobuff (& save_redo ); 3326 3327typval_T rettv ; 3328if (callback_call (& p_opfunc ,1 ,argv ,& rettv )) { 3329tv_clear (& rettv ); 3330 } 3331restore_redobuff (& save_redo ); 3332 3333virtual_op = save_virtual_op ; 3334finish_op = save_finish_op ; 3335if (cmdmod .cmod_flags & CMOD_LOCKMARKS ) { 3336curbuf -> b_op_start = orig_start ; 3337curbuf -> b_op_end = orig_end ; 3338 } 3339 } 3340} 3341 3342/// Calculate start/end virtual columns for operating in block mode. 3343/// 3344/// @param initial when true: adjust position for 'selectmode' 3345static void get_op_vcol (oparg_T * oap ,bool initial ) 3346{ 3347colnr_T start ; 3348colnr_T end ; 3349 3350if (Visual .mode != Ctrl_V 3351|| (!initial && oap -> end .col < curwin -> w_view_width )) { 3352return ; 3353 } 3354 3355oap -> motion_type = kMTBlockWise ; 3356 3357// prevent from moving onto a trail byte 3358mark_mb_adjustpos (curwin -> w_buffer ,& oap -> end ); 3359 3360getvvcol (curwin ,& (oap -> start ),& oap -> start_vcol ,NULL ,& oap -> end_vcol ,0 ); 3361getvvcol (curwin ,& (oap -> end ),& start ,NULL ,& end ,0 ); 3362 3363oap -> start_vcol = MIN (oap -> start_vcol ,start ); 3364if (end > oap -> end_vcol ) { 3365if (initial && * p_sel == 'e' 3366&& start >=1 3367&& start - 1 >=oap -> end_vcol ) { 3368oap -> end_vcol = start - 1 ; 3369 }else { 3370oap -> end_vcol = end ; 3371 } 3372 } 3373 3374// if '$' was used, get oap->end_vcol from longest line 3375if (curwin -> w_curswant == MAXCOL ) { 3376curwin -> w_cursor .col = MAXCOL ; 3377oap -> end_vcol = 0 ; 3378for (curwin -> w_cursor .lnum = oap -> start .lnum ; 3379curwin -> w_cursor .lnum <=oap -> end .lnum ;curwin -> w_cursor .lnum ++ ) { 3380getvvcol (curwin ,& curwin -> w_cursor ,NULL ,NULL ,& end ,0 ); 3381oap -> end_vcol = MAX (oap -> end_vcol ,end ); 3382 } 3383 } 3384 3385// Correct oap->end.col and oap->start.col to be the 3386// upper-left and lower-right corner of the block area. 3387// 3388// (Actually, this does convert column positions into character 3389// positions) 3390curwin -> w_cursor .lnum = oap -> end .lnum ; 3391coladvance (curwin ,oap -> end_vcol ); 3392oap -> end = curwin -> w_cursor ; 3393 3394curwin -> w_cursor = oap -> start ; 3395coladvance (curwin ,oap -> start_vcol ); 3396oap -> start = curwin -> w_cursor ; 3397} 3398 3399static bool is_ex_cmdchar (cmdarg_T * cap ) 3400{ 3401return cap -> cmdchar == ':' || cap -> cmdchar == K_COMMAND ; 3402} 3403 3404/// True for an operator whose motion selects its own region (gn/gN/gv): the redo replays the 3405/// motion's own keys ("dgn" re-searches, "dgv" reselects). 3406bool op_self_select (const cmdarg_T * cap ) 3407{ 3408return cap -> cmdchar == 'g' && (cap -> nchar == 'n' || cap -> nchar == 'N' || cap -> nchar == 'v' ); 3409} 3410 3411/// How an Insert-entering operator (OP_CHANGE/OP_INSERT/OP_APPEND) was entered from Visual mode: 3412/// decides the session's redo/capture handling (atom_ins_start()). 3413static VisualIns op_ins_visual (oparg_T * oap ,cmdarg_T * cap ) 3414{ 3415if (!oap -> is_VIsual ) { 3416return kVInsNone ; 3417 } 3418if (is_ex_cmdchar (cap )|| cap -> cmdchar == K_LUA ) { 3419return kVInsMotion ;// An omap selected it (Lua, or ":norm"): redo replays the omap. 3420 } 3421if (op_self_select (cap )&& cap -> nchar != 'v' && oap -> motion_force == NUL ) { 3422return kVInsMotion ;// "gn" searches from the cursor: redo replays it per-cursor. 3423 } 3424if (oap -> motion_force != NUL || op_self_select (cap )) { 3425// A self-selecting motion ("gv") or a forced-motion operator: redo replays its keys. 3426return kVInsOther ; 3427 } 3428// Unreplayable (void/absent/"gv") selection: the redo falls back to an equal-size reselect ("1v"). 3429return atom_visual_redoable () ?kVInsKeys :kVInsOther ; 3430} 3431 3432/// Handle an operator after Visual mode or when the movement is finished. 3433/// "gui_yank" is true when yanking text for the clipboard. 3434void do_pending_operator (cmdarg_T * cap ,int old_col ,bool gui_yank ) 3435{ 3436oparg_T * oap = cap -> oap ; 3437int lbr_saved = curwin -> w_p_lbr ; 3438 3439pos_T old_cursor = curwin -> w_cursor ; 3440const pos_T op_origin = oap -> start ;// Where the operator started (before the motion). 3441 3442// If an operation is pending, handle it... 3443if ((finish_op 3444|| Visual .active ) 3445&& oap -> op_type != OP_NOP ) { 3446bool empty_region_error ; 3447int restart_edit_save ; 3448bool include_line_break = false; 3449// Yank can be redone when 'y' is in 'cpoptions', but not when yanking 3450// for the clipboard. 3451const bool redo_yank = vim_strchr (p_cpo ,kCpoYank )!= NULL && !gui_yank ; 3452 3453// Avoid a problem with unwanted linebreaks in block mode 3454reset_lbr (); 3455oap -> is_VIsual = Visual .active ; 3456if (oap -> motion_force == 'V' ) { 3457oap -> motion_type = kMTLineWise ; 3458 }else if (oap -> motion_force == 'v' ) { 3459// If the motion was linewise, "inclusive" will not have been set. 3460// Use "exclusive" to be consistent. Makes "dvj" work nice. 3461if (oap -> motion_type == kMTLineWise ) { 3462oap -> inclusive = false; 3463 }else if (oap -> motion_type == kMTCharWise ) { 3464// If the motion already was charwise, toggle "inclusive" 3465oap -> inclusive = !oap -> inclusive ; 3466 } 3467oap -> motion_type = kMTCharWise ; 3468 }else if (oap -> motion_force == Ctrl_V ) { 3469// Change line- or charwise motion into Visual block mode. 3470if (!Visual .active ) { 3471Visual .active = true; 3472Visual .start = oap -> start ; 3473 } 3474Visual .mode = Ctrl_V ; 3475Visual .select = false; 3476Visual .reselect = false; 3477 } 3478 3479atom_capture_op (oap ,cap ,redo_yank ); 3480 3481if (Visual .active ) { 3482if (!gui_yank 3483// Not if the operator target made the Visual area ("cgn", "d<C-v>j"). #40949 3484&& !finish_op ) { 3485// Save the current Visual area for '< and '> marks, and "gv" 3486curbuf -> b_visual = visualinfo (); 3487restore_visual_mode (); 3488curbuf -> b_visual_mode_eval = Visual .mode ; 3489 } 3490 3491// In Select mode, a linewise selection is operated upon like a 3492// charwise selection. 3493// Special case: gH<Del> deletes the last line. 3494if (Visual .select && Visual .mode == 'V' 3495&& cap -> oap -> op_type != OP_DELETE ) { 3496if (lt (Visual .start ,curwin -> w_cursor )) { 3497Visual .start .col = 0 ; 3498curwin -> w_cursor .col = ml_get_len (curwin -> w_cursor .lnum ); 3499 }else { 3500curwin -> w_cursor .col = 0 ; 3501Visual .start .col = ml_get_len (Visual .start .lnum ); 3502 } 3503Visual .mode = 'v' ; 3504 }else if (Visual .mode == 'v' ) { 3505// If 'selection' is "exclusive", backup one character for 3506// charwise selections. 3507include_line_break = unadjust_for_sel (); 3508 } 3509 3510oap -> start = Visual .start ; 3511if (Visual .mode == 'V' ) { 3512oap -> start .col = 0 ; 3513oap -> start .coladd = 0 ; 3514 } 3515 } 3516 3517// Set oap->start to the first position of the operated text, oap->end 3518// to the end of the operated text. w_cursor is equal to oap->start. 3519if (lt (oap -> start ,curwin -> w_cursor )) { 3520// Include folded lines completely. 3521if (!Visual .active ) { 3522if (hasFolding (curwin ,oap -> start .lnum ,& oap -> start .lnum ,NULL )) { 3523oap -> start .col = 0 ; 3524 } 3525if ((curwin -> w_cursor .col > 0 3526|| oap -> inclusive 3527|| oap -> motion_type == kMTLineWise ) 3528&& hasFolding (curwin ,curwin -> w_cursor .lnum ,NULL , 3529& curwin -> w_cursor .lnum )) { 3530curwin -> w_cursor .col = get_cursor_line_len (); 3531 } 3532 } 3533oap -> end = curwin -> w_cursor ; 3534curwin -> w_cursor = oap -> start ; 3535 3536// w_virtcol may have been updated; if the cursor goes back to its 3537// previous position w_virtcol becomes invalid and isn't updated 3538// automatically. 3539curwin -> w_valid &= ~VALID_VIRTCOL ; 3540 }else { 3541// Include folded lines completely. 3542if (!Visual .active && oap -> motion_type == kMTLineWise ) { 3543if (hasFolding (curwin ,curwin -> w_cursor .lnum ,& curwin -> w_cursor .lnum , 3544NULL )) { 3545curwin -> w_cursor .col = 0 ; 3546 } 3547if (hasFolding (curwin ,oap -> start .lnum ,NULL ,& oap -> start .lnum )) { 3548oap -> start .col = ml_get_len (oap -> start .lnum ); 3549 } 3550 } 3551oap -> end = oap -> start ; 3552oap -> start = curwin -> w_cursor ; 3553 } 3554 3555// Just in case lines were deleted that make the position invalid. 3556check_pos (curwin -> w_buffer ,& oap -> end ); 3557oap -> line_count = oap -> end .lnum - oap -> start .lnum + 1 ; 3558 3559// Set "virtual_op" before resetting Visual.active. 3560virtual_op = virtual_active (curwin ); 3561 3562if (Visual .active ) { 3563get_op_vcol (oap , true); 3564 3565if (!gui_yank ) { 3566// Remember Visual text size, for {count}v reselect (:h visual-repeat). 3567Visual .resel .mode = Visual .mode ; 3568if (curwin -> w_curswant == MAXCOL ) { 3569Visual .resel .vcol = MAXCOL ; 3570 }else { 3571if (Visual .mode != Ctrl_V ) { 3572getvvcol (curwin ,& (oap -> end ),NULL ,NULL ,& oap -> end_vcol ,0 ); 3573 } 3574if (Visual .mode == Ctrl_V || oap -> line_count <=1 ) { 3575if (Visual .mode != Ctrl_V ) { 3576getvvcol (curwin ,& (oap -> start ),& oap -> start_vcol ,NULL ,NULL ,0 ); 3577 } 3578Visual .resel .vcol = oap -> end_vcol - oap -> start_vcol + 1 ; 3579 }else { 3580Visual .resel .vcol = oap -> end_vcol ; 3581 } 3582 } 3583Visual .resel .line_count = oap -> line_count ; 3584 } 3585 3586// oap->inclusive defaults to true. 3587// If oap->end is on a NUL (empty line) oap->inclusive becomes 3588// false. This makes "d}P" and "v}dP" work the same. 3589if (oap -> motion_force == NUL || oap -> motion_type == kMTLineWise ) { 3590oap -> inclusive = true; 3591 } 3592if (Visual .mode == 'V' ) { 3593oap -> motion_type = kMTLineWise ; 3594 }else if (Visual .mode == 'v' ) { 3595oap -> motion_type = kMTCharWise ; 3596if (* ml_get_pos (& (oap -> end ))== NUL 3597&& (include_line_break || !virtual_op )) { 3598oap -> inclusive = false; 3599// Try to include the newline, unless it's an operator 3600// that works on lines only. 3601if (* p_sel != 'o' 3602&& !op_on_lines (oap -> op_type ) 3603&& oap -> end .lnum < curbuf -> b_ml .ml_line_count ) { 3604oap -> end .lnum ++ ; 3605oap -> end .col = 0 ; 3606oap -> end .coladd = 0 ; 3607oap -> line_count ++ ; 3608 } 3609 } 3610 } 3611 3612// Switch Visual off now, so screen updating does 3613// not show inverted text when the screen is redrawn. 3614// With OP_YANK and sometimes with OP_COLON and OP_FILTER there is 3615// no screen redraw, so it is done here to remove the inverted 3616// part. 3617if (!gui_yank ) { 3618Visual .active = false; 3619setmouse (); 3620mouse_dragging = 0 ; 3621may_clear_cmdline (); 3622if ((oap -> op_type == OP_YANK 3623|| oap -> op_type == OP_COLON 3624|| oap -> op_type == OP_FUNCTION 3625|| oap -> op_type == OP_FILTER ) 3626&& oap -> motion_force == NUL ) { 3627// Make sure redrawing is correct. 3628restore_lbr (lbr_saved ); 3629redraw_curbuf_later (UPD_INVERTED ); 3630 } 3631 } 3632 } 3633 3634// Include the trailing byte of a multi-byte char. 3635if (oap -> inclusive ) { 3636const int l = utfc_ptr2len (ml_get_pos (& oap -> end )); 3637if (l > 1 ) { 3638oap -> end .col += l - 1 ; 3639 } 3640 } 3641curwin -> w_set_curswant = true; 3642 3643// oap->empty is set when start and end are the same. The inclusive 3644// flag affects this too, unless yanking and the end is on a NUL. 3645oap -> empty = (oap -> motion_type != kMTLineWise 3646&& (!oap -> inclusive 3647|| (oap -> op_type == OP_YANK 3648&& gchar_pos (& oap -> end )== NUL )) 3649&& equalpos (oap -> start ,oap -> end ) 3650&& !(virtual_op && oap -> start .coladd != oap -> end .coladd )); 3651// For delete, change and yank, it's an error to operate on an 3652// empty region, when 'E' included in 'cpoptions' (Vi compatible). 3653empty_region_error = (oap -> empty 3654&& vim_strchr (p_cpo ,kCpoEmptyregion )!= NULL ); 3655 3656// Force a redraw when operating on an empty Visual region, when 3657// 'modifiable is off or creating a fold. 3658if (oap -> is_VIsual && (oap -> empty || !MODIFIABLE (curbuf ) 3659|| oap -> op_type == OP_FOLD )) { 3660restore_lbr (lbr_saved ); 3661redraw_curbuf_later (UPD_INVERTED ); 3662 } 3663 3664// If the end of an operator is in column one while oap->motion_type 3665// is kMTCharWise and oap->inclusive is false, we put op_end after the last 3666// character in the previous line. If op_start is on or before the 3667// first non-blank in the line, the operator becomes linewise 3668// (strange, but that's the way vi does it). 3669if (oap -> motion_type == kMTCharWise 3670&& oap -> inclusive == false 3671&& !(cap -> retval & CA_NO_ADJ_OP_END ) 3672&& oap -> end .col == 0 3673&& (!oap -> is_VIsual || * p_sel == 'o' ) 3674&& oap -> line_count > 1 ) { 3675oap -> end_adjusted = true;// remember that we did this 3676oap -> line_count -- ; 3677oap -> end .lnum -- ; 3678if (inindent (0 )) { 3679oap -> motion_type = kMTLineWise ; 3680 }else { 3681oap -> end .col = ml_get_len (oap -> end .lnum ); 3682if (oap -> end .col ) { 3683oap -> end .col -- ; 3684oap -> inclusive = true; 3685 } 3686 } 3687 }else { 3688oap -> end_adjusted = false; 3689 } 3690 3691switch (oap -> op_type ) { 3692case OP_LSHIFT : 3693case OP_RSHIFT : 3694op_shift (oap , true,oap -> is_VIsual ?cap -> count1 :1 ); 3695auto_format (false, true); 3696break ; 3697 3698case OP_JOIN_NS : 3699case OP_JOIN : 3700oap -> line_count = MAX (oap -> line_count ,2 ); 3701if (curwin -> w_cursor .lnum + oap -> line_count - 1 > 3702curbuf -> b_ml .ml_line_count ) { 3703beep_flush (); 3704 }else { 3705do_join ((size_t )oap -> line_count ,oap -> op_type == OP_JOIN , 3706 true, true, true); 3707auto_format (false, true); 3708 } 3709break ; 3710 3711case OP_DELETE : 3712Visual .reselect = false;// don't reselect now 3713if (empty_region_error ) { 3714vim_beep (kOptBoFlagOperator ); 3715redo_cancel (); 3716 }else { 3717op_delete (oap ); 3718// save cursor line for undo if it wasn't saved yet 3719if (oap -> motion_type == kMTLineWise 3720&& has_format_option (kFoAuto ) 3721&& u_save_cursor ()== OK ) { 3722auto_format (false, true); 3723 } 3724 } 3725break ; 3726 3727case OP_YANK : 3728if (empty_region_error ) { 3729if (!gui_yank ) { 3730vim_beep (kOptBoFlagOperator ); 3731redo_cancel (); 3732 } 3733 }else { 3734restore_lbr (lbr_saved ); 3735oap -> excl_tr_ws = cap -> cmdchar == 'z' ; 3736if (oap -> restore_cursor ) { 3737curwin -> w_cursor = oap -> cursor_start ; 3738 } 3739op_yank (oap , !gui_yank ); 3740 } 3741check_cursor_col (curwin ); 3742break ; 3743 3744case OP_CHANGE : 3745Visual .reselect = false;// don't reselect now 3746if (empty_region_error ) { 3747vim_beep (kOptBoFlagOperator ); 3748redo_cancel (); 3749 }else { 3750// This is a new edit command, not a restart. Need to 3751// remember it to make i_CTRL-O work with mappings for 3752// Visual mode. But do this only once and not when typed. 3753if (!KeyTyped ) { 3754restart_edit_save = restart_edit ; 3755 }else { 3756restart_edit_save = 0 ; 3757 } 3758restart_edit = 0 ; 3759 3760// Restore linebreak, so that when the user edits it looks as before. 3761restore_lbr (lbr_saved ); 3762 3763// trigger TextChangedI 3764curbuf -> b_last_changedtick_i = buf_get_changedtick (curbuf ); 3765 3766InsSession session = atom_ins_start ('c' ,cap -> count0 ,op_ins_visual (oap ,cap ), 3767oap -> is_VIsual && oap -> motion_type == kMTBlockWise ); 3768bool busy = op_change (oap );// will call edit() 3769if (busy ) { 3770cap -> retval |=CA_COMMAND_BUSY ; 3771 } 3772if (restart_edit == 0 ) { 3773restart_edit = restart_edit_save ; 3774 } 3775atom_ins_end (& session ,busy ); 3776 } 3777break ; 3778 3779case OP_FILTER : 3780if (vim_strchr (p_cpo ,kCpoFilter )!= NULL ) { 3781redo_append_str (S_LEN ("!\r" ));// Use any last used !cmd. 3782 }else { 3783bangredo = true;// do_bang() will put cmd in redo buffer. 3784 } 3785FALLTHROUGH ; 3786 3787case OP_INDENT : 3788case OP_COLON : 3789 3790// If 'equalprg' is empty, do the indenting internally. 3791if (oap -> op_type == OP_INDENT && * get_equalprg ()== NUL ) { 3792if (curbuf -> b_p_lisp ) { 3793if (use_indentexpr_for_lisp ()) { 3794op_reindent (oap ,get_expr_indent ); 3795 }else { 3796op_reindent (oap ,get_lisp_indent ); 3797 } 3798break ; 3799 } 3800op_reindent (oap ,curbuf -> b_p_inde .type != kCallbackNone ?get_expr_indent :get_c_indent ); 3801break ; 3802 } 3803 3804op_colon (oap ); 3805break ; 3806 3807case OP_TILDE : 3808case OP_UPPER : 3809case OP_LOWER : 3810case OP_ROT13 : 3811if (empty_region_error ) { 3812vim_beep (kOptBoFlagOperator ); 3813redo_cancel (); 3814 }else { 3815op_tilde (oap ); 3816 } 3817check_cursor_col (curwin ); 3818break ; 3819 3820case OP_FORMAT : 3821if (curbuf -> b_p_fex .type != kCallbackNone ) { 3822op_formatexpr (oap );// use expression 3823 }else { 3824if (* p_fp != NUL || * curbuf -> b_p_fp != NUL ) { 3825op_colon (oap );// use external command 3826 }else { 3827op_format (oap , false);// use internal function 3828 } 3829 } 3830break ; 3831 3832case OP_FORMAT2 : 3833op_format (oap , true);// use internal function 3834break ; 3835 3836case OP_FUNCTION : 3837// Restore linebreak, so that when the user edits it looks as before. 3838restore_lbr (lbr_saved ); 3839// call 'operatorfunc' 3840op_function (oap ); 3841break ; 3842 3843case OP_INSERT : 3844case OP_APPEND : 3845Visual .reselect = false;// don't reselect now 3846if (empty_region_error ) { 3847vim_beep (kOptBoFlagOperator ); 3848redo_cancel (); 3849 }else { 3850// This is a new edit command, not a restart. Need to 3851// remember it to make i_CTRL-O work with mappings for 3852// Visual mode. But do this only once. 3853restart_edit_save = restart_edit ; 3854restart_edit = 0 ; 3855 3856// Restore linebreak, so that when the user edits it looks as before. 3857restore_lbr (lbr_saved ); 3858 3859// trigger TextChangedI 3860curbuf -> b_last_changedtick_i = buf_get_changedtick (curbuf ); 3861 3862InsSession session = atom_ins_start ('I' ,cap -> count1 ,op_ins_visual (oap ,cap ), 3863oap -> motion_type == kMTBlockWise ); 3864op_insert (oap ,cap -> count1 ); 3865atom_ins_end (& session , false); 3866 3867// Reset linebreak, so that formatting works correctly. 3868reset_lbr (); 3869 3870// TODO(brammool): when inserting in several lines, should format all 3871// the lines. 3872auto_format (false, true); 3873 3874if (restart_edit == 0 ) { 3875restart_edit = restart_edit_save ; 3876 }else { 3877cap -> retval |=CA_COMMAND_BUSY ; 3878 } 3879 } 3880break ; 3881 3882case OP_REPLACE : 3883Visual .reselect = false;// don't reselect now 3884if (empty_region_error ) { 3885vim_beep (kOptBoFlagOperator ); 3886redo_cancel (); 3887 }else { 3888// Restore linebreak, so that when the user edits it looks as before. 3889restore_lbr (lbr_saved ); 3890 3891op_replace (oap ,cap -> nchar ); 3892 } 3893break ; 3894 3895case OP_FOLD : 3896Visual .reselect = false;// don't reselect now 3897foldCreate (curwin ,oap -> start ,oap -> end ); 3898break ; 3899 3900case OP_MCURSOR : 3901mc_zq (oap ,cap ,op_origin ); 3902break ; 3903 3904case OP_FOLDOPEN : 3905case OP_FOLDOPENREC : 3906case OP_FOLDCLOSE : 3907case OP_FOLDCLOSEREC : 3908Visual .reselect = false;// don't reselect now 3909opFoldRange (oap -> start ,oap -> end , 3910oap -> op_type == OP_FOLDOPEN 3911|| oap -> op_type == OP_FOLDOPENREC , 3912oap -> op_type == OP_FOLDOPENREC 3913|| oap -> op_type == OP_FOLDCLOSEREC , 3914oap -> is_VIsual ); 3915break ; 3916 3917case OP_FOLDDEL : 3918case OP_FOLDDELREC : 3919Visual .reselect = false;// don't reselect now 3920deleteFold (curwin ,oap -> start .lnum ,oap -> end .lnum , 3921oap -> op_type == OP_FOLDDELREC ,oap -> is_VIsual ); 3922break ; 3923 3924case OP_NR_ADD : 3925case OP_NR_SUB : 3926if (empty_region_error ) { 3927vim_beep (kOptBoFlagOperator ); 3928redo_cancel (); 3929 }else { 3930Visual .active = true; 3931restore_lbr (lbr_saved ); 3932op_addsub (oap , (linenr_T )cap -> count1 ,cap -> arg ); 3933Visual .active = false; 3934 } 3935check_cursor_col (curwin ); 3936break ; 3937default : 3938clearopbeep (oap ); 3939 } 3940virtual_op = kNone ; 3941if (!gui_yank ) { 3942// if 'sol' not set, go back to old column for some commands 3943if (!p_sol && oap -> motion_type == kMTLineWise && !oap -> end_adjusted 3944&& (oap -> op_type == OP_LSHIFT || oap -> op_type == OP_RSHIFT 3945|| oap -> op_type == OP_DELETE )) { 3946reset_lbr (); 3947coladvance (curwin ,curwin -> w_curswant = old_col ); 3948 } 3949 }else { 3950curwin -> w_cursor = old_cursor ; 3951 } 3952clearop (oap ); 3953motion_force = NUL ; 3954 } 3955restore_lbr (lbr_saved ); 3956} 3957 3958/// Get the byte count of buffer region. End-exclusive. 3959/// 3960/// @return number of bytes 3961bcount_t get_region_bytecount (buf_T * buf ,linenr_T start_lnum ,linenr_T end_lnum ,colnr_T start_col , 3962colnr_T end_col ) 3963{ 3964linenr_T max_lnum = buf -> b_ml .ml_line_count ; 3965if (start_lnum > max_lnum ) { 3966return 0 ; 3967 } 3968if (start_lnum == end_lnum ) { 3969return end_col - start_col ; 3970 } 3971bcount_t deleted_bytes = ml_get_buf_len (buf ,start_lnum )- start_col + 1 ; 3972 3973for (linenr_T i = 1 ;i <=end_lnum - start_lnum - 1 ;i ++ ) { 3974if (start_lnum + i > max_lnum ) { 3975return deleted_bytes ; 3976 } 3977deleted_bytes += ml_get_buf_len (buf ,start_lnum + i )+ 1 ; 3978 } 3979if (end_lnum > max_lnum ) { 3980return deleted_bytes ; 3981 } 3982return deleted_bytes + end_col ; 3983}