robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e533ec5160
master
1// textobject.c: functions for text objects 2 3#include <stdbool.h> 4#include <stdint.h> 5#include <stdio.h> 6#include <string.h> 7 8#include "nvim/ascii_defs.h" 9#include "nvim/buffer_defs.h" 10#include "nvim/charset.h" 11#include "nvim/cursor.h" 12#include "nvim/drawscreen.h" 13#include "nvim/eval/funcs.h" 14#include "nvim/fold.h" 15#include "nvim/globals.h" 16#include "nvim/indent.h" 17#include "nvim/insert.h" 18#include "nvim/mark.h" 19#include "nvim/mark_defs.h" 20#include "nvim/mbyte.h" 21#include "nvim/memline.h" 22#include "nvim/memory.h" 23#include "nvim/move.h" 24#include "nvim/normal.h" 25#include "nvim/option_vars.h" 26#include "nvim/pos_defs.h" 27#include "nvim/search.h" 28#include "nvim/strings.h" 29#include "nvim/textobject.h" 30#include "nvim/vim_defs.h" 31 32#include "textobject.c.generated.h" 33 34/// Find the start of the next sentence, searching in the direction specified 35/// by the "dir" argument. The cursor is positioned on the start of the next 36/// sentence when found. If the next sentence is found, return OK. Return FAIL 37/// otherwise. See ":h sentence" for the precise definition of a "sentence" 38/// text object. 39int findsent (Direction dir ,int count ) 40{ 41int c ; 42int (* func )(pos_T * ); 43bool noskip = false;// do not skip blanks 44 45pos_T pos = curwin -> w_cursor ; 46if (dir == FORWARD ) { 47func = incl ; 48 }else { 49func = decl ; 50 } 51 52while (count -- ) { 53const pos_T prev_pos = pos ; 54 55// if on an empty line, skip up to a non-empty line 56if (gchar_pos (& pos )== NUL ) { 57do { 58if ((* func )(& pos )== -1 ) { 59break ; 60 } 61 }while (gchar_pos (& pos )== NUL ); 62if (dir == FORWARD ) { 63 gotofound ; 64 } 65// if on the start of a paragraph or a section and searching forward, 66// go to the next line 67 }else if (dir == FORWARD && pos .col == 0 68&& startPS (pos .lnum ,NUL , false)) { 69if (pos .lnum == curbuf -> b_ml .ml_line_count ) { 70return FAIL ; 71 } 72pos .lnum ++ ; 73 gotofound ; 74 }else if (dir == BACKWARD ) { 75decl (& pos ); 76 } 77 78// go back to the previous non-white non-punctuation character 79bool found_dot = false; 80while (c = gchar_pos (& pos ),ascii_iswhite (c ) 81|| vim_strchr (".!?)]\"'" ,c )!= NULL ) { 82pos_T tpos = pos ; 83if (decl (& tpos )== -1 || (LINEEMPTY (tpos .lnum )&& dir == FORWARD )) { 84break ; 85 } 86if (found_dot ) { 87break ; 88 } 89if (vim_strchr (".!?" ,c )!= NULL ) { 90found_dot = true; 91 } 92if (vim_strchr (")]\"'" ,c )!= NULL 93&& vim_strchr (".!?)]\"'" ,gchar_pos (& tpos ))== NULL ) { 94break ; 95 } 96decl (& pos ); 97 } 98 99// remember the line where the search started 100const int startlnum = pos .lnum ; 101const bool cpo_J = vim_strchr (p_cpo ,kCpoEndofsent )!= NULL ; 102 103while (true) {// find end of sentence 104c = gchar_pos (& pos ); 105if (c == NUL || (pos .col == 0 && startPS (pos .lnum ,NUL , false))) { 106if (dir == BACKWARD && pos .lnum != startlnum ) { 107pos .lnum ++ ; 108 } 109break ; 110 } 111if (c == '.' || c == '!' || c == '?' ) { 112pos_T tpos = pos ; 113do { 114if ((c = inc (& tpos ))== -1 ) { 115break ; 116 } 117 }while (vim_strchr (")]\"'" ,c = gchar_pos (& tpos )) 118!= NULL ); 119if (c == -1 || (!cpo_J && (c == ' ' || c == '\t' ))|| c == NUL 120|| (cpo_J && (c == ' ' && inc (& tpos ) >=0 121&& gchar_pos (& tpos )== ' ' ))) { 122pos = tpos ; 123if (gchar_pos (& pos )== NUL ) {// skip NUL at EOL 124inc (& pos ); 125 } 126break ; 127 } 128 } 129if ((* func )(& pos )== -1 ) { 130if (count ) { 131return FAIL ; 132 } 133noskip = true; 134break ; 135 } 136 } 137found : 138// skip white space 139while (!noskip && ((c = gchar_pos (& pos ))== ' ' || c == '\t' )) { 140if (incl (& pos )== -1 ) { 141break ; 142 } 143 } 144 145if (equalpos (prev_pos ,pos )) { 146// didn't actually move, advance one character and try again 147if ((* func )(& pos )== -1 ) { 148if (count ) { 149return FAIL ; 150 } 151break ; 152 } 153count ++ ; 154 } 155 } 156 157setpcmark (); 158curwin -> w_cursor = pos ; 159return OK ; 160} 161 162/// Find the next paragraph or section in direction 'dir'. 163/// Paragraphs are currently supposed to be separated by empty lines. 164/// If 'what' is NUL we go to the next paragraph. 165/// If 'what' is '{' or '}' we go to the next section. 166/// If 'both' is true also stop at '}'. 167/// 168/// @param pincl Return: true if last char is to be included 169/// 170/// @return true if the next paragraph or section was found. 171bool findpar (bool * pincl ,int dir ,int count ,int what ,bool both ) 172{ 173bool first ;// true on first line 174linenr_T fold_first ;// first line of a closed fold 175linenr_T fold_last ;// last line of a closed fold 176bool fold_skipped ;// true if a closed fold was skipped this 177// iteration 178 179linenr_T curr = curwin -> w_cursor .lnum ; 180 181while (count -- ) { 182bool did_skip = false;// true after separating lines have been skipped 183for (first = true;;first = false) { 184if (* ml_get (curr )!= NUL ) { 185did_skip = true; 186 } 187 188// skip folded lines 189fold_skipped = false; 190if (first && hasFolding (curwin ,curr ,& fold_first ,& fold_last )) { 191curr = ((dir > 0 ) ?fold_last :fold_first )+ dir ; 192fold_skipped = true; 193 } 194 195if (!first && did_skip && startPS (curr ,what ,both )) { 196break ; 197 } 198 199if (fold_skipped ) { 200curr -= dir ; 201 } 202if ((curr += dir )< 1 || curr > curbuf -> b_ml .ml_line_count ) { 203if (count ) { 204return false; 205 } 206curr -= dir ; 207break ; 208 } 209 } 210 } 211setpcmark (); 212if (both && * ml_get (curr )== '}' ) {// include line with '}' 213curr ++ ; 214 } 215curwin -> w_cursor .lnum = curr ; 216if (curr == curbuf -> b_ml .ml_line_count && what != '}' && dir == FORWARD ) { 217char * line = ml_get (curr ); 218 219// Put the cursor on the last character in the last line and make the 220// motion inclusive. 221if ((curwin -> w_cursor .col = ml_get_len (curr ))!= 0 ) { 222curwin -> w_cursor .col -- ; 223curwin -> w_cursor .col -= utf_head_off (line ,line + curwin -> w_cursor .col ); 224* pincl = true; 225 } 226 }else { 227curwin -> w_cursor .col = 0 ; 228 } 229return true; 230} 231 232/// check if the string 's' is a nroff macro that is in option 'opt' 233static bool inmacro (char * opt ,const char * s ) 234{ 235char * macro ; 236 237for (macro = opt ;macro [0 ];macro ++ ) { 238// Accept two characters in the option being equal to two characters 239// in the line. A space in the option matches with a space in the 240// line or the line having ended. 241if ((macro [0 ]== s [0 ] 242|| (macro [0 ]== ' ' 243&& (s [0 ]== NUL || s [0 ]== ' ' ))) 244&& (macro [1 ]== s [1 ] 245|| ((macro [1 ]== NUL || macro [1 ]== ' ' ) 246&& (s [0 ]== NUL || s [1 ]== NUL || s [1 ]== ' ' )))) { 247break ; 248 } 249macro ++ ; 250if (macro [0 ]== NUL ) { 251break ; 252 } 253 } 254return macro [0 ]!= NUL ; 255} 256 257/// startPS: return true if line 'lnum' is the start of a section or paragraph. 258/// If 'para' is '{' or '}' only check for sections. 259/// If 'both' is true also stop at '}' 260bool startPS (linenr_T lnum ,int para ,bool both ) 261{ 262char * s = ml_get (lnum ); 263if ((uint8_t )(* s )== para || * s == '\f' || (both && * s == '}' )) { 264return true; 265 } 266if (* s == '.' && (inmacro (p_sections ,s + 1 ) 267|| (!para && inmacro (p_para ,s + 1 )))) { 268return true; 269 } 270return false; 271} 272 273// The following routines do the word searches performed by the 'w', 'W', 274// 'b', 'B', 'e', and 'E' commands. 275 276// To perform these searches, characters are placed into one of three 277// classes, and transitions between classes determine word boundaries. 278// 279// The classes are: 280// 281// 0 - white space 282// 1 - punctuation 283// 2 or higher - keyword characters (letters, digits and underscore) 284 285static bool cls_bigword ;///< true for "W", "B" or "E" 286 287/// cls() - returns the class of character at curwin->w_cursor 288/// 289/// If a 'W', 'B', or 'E' motion is being done (cls_bigword == true), chars 290/// from class 2 and higher are reported as class 1 since only white space 291/// boundaries are of interest. 292static int cls (void ) 293{ 294int c = gchar_cursor (); 295if (c == ' ' || c == '\t' || c == NUL ) { 296return 0 ; 297 } 298 299c = utf_class (c ); 300 301// If cls_bigword is true, report all non-blanks as class 1. 302if (c != 0 && cls_bigword ) { 303return 1 ; 304 } 305return c ; 306} 307 308/// fwd_word(count, type, eol) - move forward one word 309/// 310/// @return FAIL if the cursor was already at the end of the file. 311/// If eol is true, last word stops at end of line (for operators). 312/// 313/// @param bigword "W", "E" or "B" 314int fwd_word (int count ,bool bigword ,bool eol ) 315{ 316curwin -> w_cursor .coladd = 0 ; 317cls_bigword = bigword ; 318while (-- count >=0 ) { 319// When inside a range of folded lines, move to the last char of the 320// last line. 321if (hasFolding (curwin ,curwin -> w_cursor .lnum ,NULL ,& curwin -> w_cursor .lnum )) { 322coladvance (curwin ,MAXCOL ); 323 } 324int sclass = cls ();// starting class 325 326// We always move at least one character, unless on the last 327// character in the buffer. 328int last_line = (curwin -> w_cursor .lnum == curbuf -> b_ml .ml_line_count ); 329int i = inc_cursor (); 330if (i == -1 || (i >=1 && last_line )) {// started at last char in file 331return FAIL ; 332 } 333if (i >=1 && eol && count == 0 ) {// started at last char in line 334return OK ; 335 } 336 337// Go one char past end of current word (if any) 338if (sclass != 0 ) { 339while (cls ()== sclass ) { 340i = inc_cursor (); 341if (i == -1 || (i >=1 && eol && count == 0 )) { 342return OK ; 343 } 344 } 345 } 346 347// go to next non-white 348while (cls ()== 0 ) { 349// We'll stop if we land on a blank line 350if (curwin -> w_cursor .col == 0 && * get_cursor_line_ptr ()== NUL ) { 351break ; 352 } 353 354i = inc_cursor (); 355if (i == -1 || (i >=1 && eol && count == 0 )) { 356return OK ; 357 } 358 } 359 } 360return OK ; 361} 362 363/// bck_word() - move backward 'count' words 364/// 365/// If stop is true and we are already on the start of a word, move one less. 366/// 367/// Returns FAIL if top of the file was reached. 368int bck_word (int count ,bool bigword ,bool stop ) 369{ 370int sclass ;// starting class 371 372curwin -> w_cursor .coladd = 0 ; 373cls_bigword = bigword ; 374while (-- count >=0 ) { 375// When inside a range of folded lines, move to the first char of the 376// first line. 377if (hasFolding (curwin ,curwin -> w_cursor .lnum ,& curwin -> w_cursor .lnum ,NULL )) { 378curwin -> w_cursor .col = 0 ; 379 } 380sclass = cls (); 381if (dec_cursor ()== -1 ) {// started at start of file 382return FAIL ; 383 } 384 385if (!stop || sclass == cls ()|| sclass == 0 ) { 386// Skip white space before the word. 387// Stop on an empty line. 388while (cls ()== 0 ) { 389if (curwin -> w_cursor .col == 0 390&& LINEEMPTY (curwin -> w_cursor .lnum )) { 391 gotofinished ; 392 } 393if (dec_cursor ()== -1 ) {// hit start of file, stop here 394return OK ; 395 } 396 } 397 398// Move backward to start of this word. 399if (skip_chars (cls (),BACKWARD )) { 400return OK ; 401 } 402 } 403 404inc_cursor ();// overshot - forward one 405finished : 406stop = false; 407 } 408adjust_skipcol (); 409return OK ; 410} 411 412/// end_word() - move to the end of the word 413/// 414/// There is an apparent bug in the 'e' motion of the real vi. At least on the 415/// System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e' 416/// motion crosses blank lines. When the real vi crosses a blank line in an 417/// 'e' motion, the cursor is placed on the FIRST character of the next 418/// non-blank line. The 'E' command, however, works correctly. Since this 419/// appears to be a bug, I have not duplicated it here. 420/// 421/// Returns FAIL if end of the file was reached. 422/// 423/// If stop is true and we are already on the end of a word, move one less. 424/// If empty is true stop on an empty line. 425int end_word (int count ,bool bigword ,bool stop ,bool empty ) 426{ 427int sclass ;// starting class 428 429curwin -> w_cursor .coladd = 0 ; 430cls_bigword = bigword ; 431 432// If adjusted cursor position previously, unadjust it. 433if (* p_sel == 'e' && Visual .active && Visual .mode == 'v' 434&& Visual .select_exclu_adj ) { 435unadjust_for_sel (); 436 } 437 438while (-- count >=0 ) { 439// When inside a range of folded lines, move to the last char of the 440// last line. 441if (hasFolding (curwin ,curwin -> w_cursor .lnum ,NULL ,& curwin -> w_cursor .lnum )) { 442coladvance (curwin ,MAXCOL ); 443 } 444sclass = cls (); 445if (inc_cursor ()== -1 ) { 446return FAIL ; 447 } 448 449// If we're in the middle of a word, we just have to move to the end 450// of it. 451if (cls ()== sclass && sclass != 0 ) { 452// Move forward to end of the current word 453if (skip_chars (sclass ,FORWARD )) { 454return FAIL ; 455 } 456 }else if (!stop || sclass == 0 ) { 457// We were at the end of a word. Go to the end of the next word. 458// First skip white space, if 'empty' is true, stop at empty line. 459while (cls ()== 0 ) { 460if (empty && curwin -> w_cursor .col == 0 461&& LINEEMPTY (curwin -> w_cursor .lnum )) { 462 gotofinished ; 463 } 464if (inc_cursor ()== -1 ) {// hit end of file, stop here 465return FAIL ; 466 } 467 } 468 469// Move forward to the end of this word. 470if (skip_chars (cls (),FORWARD )) { 471return FAIL ; 472 } 473 } 474dec_cursor ();// overshot - one char backward 475finished : 476stop = false;// we move only one word less 477 } 478return OK ; 479} 480 481/// Move back to the end of the word. 482/// 483/// @param bigword true for "B" 484/// @param eol if true, then stop at end of line. 485/// 486/// @return FAIL if start of the file was reached. 487int bckend_word (int count ,bool bigword ,bool eol ) 488{ 489curwin -> w_cursor .coladd = 0 ; 490cls_bigword = bigword ; 491while (-- count >=0 ) { 492int i ; 493int sclass = cls ();// starting class 494if ((i = dec_cursor ())== -1 ) { 495return FAIL ; 496 } 497if (eol && i == 1 ) { 498return OK ; 499 } 500 501// Move backward to before the start of this word. 502if (sclass != 0 ) { 503while (cls ()== sclass ) { 504if ((i = dec_cursor ())== -1 || (eol && i == 1 )) { 505return OK ; 506 } 507 } 508 } 509 510// Move backward to end of the previous word 511while (cls ()== 0 ) { 512if (curwin -> w_cursor .col == 0 && LINEEMPTY (curwin -> w_cursor .lnum )) { 513break ; 514 } 515if ((i = dec_cursor ())== -1 || (eol && i == 1 )) { 516return OK ; 517 } 518 } 519 } 520adjust_skipcol (); 521return OK ; 522} 523 524/// Skip a row of characters of the same class. 525/// 526/// @return true when end-of-file reached, false otherwise. 527static bool skip_chars (int cclass ,int dir ) 528{ 529while (cls ()== cclass ) { 530if ((dir == FORWARD ?inc_cursor () :dec_cursor ())== -1 ) { 531return true; 532 } 533 } 534return false; 535} 536 537/// Go back to the start of the word or the start of white space 538static void back_in_line (void ) 539{ 540int sclass = cls ();// starting class 541while (true) { 542if (curwin -> w_cursor .col == 0 ) {// stop at start of line 543break ; 544 } 545dec_cursor (); 546if (cls ()!= sclass ) {// stop at start of word 547inc_cursor (); 548break ; 549 } 550 } 551} 552 553static void find_first_blank (pos_T * posp ) 554{ 555while (decl (posp )!= -1 ) { 556int c = gchar_pos (posp ); 557if (!ascii_iswhite (c )) { 558incl (posp ); 559break ; 560 } 561 } 562} 563 564/// Skip count/2 sentences and count/2 separating white spaces. 565/// 566/// @param at_start_sent cursor is at start of sentence 567static void findsent_forward (int count ,bool at_start_sent ) 568{ 569while (count -- ) { 570findsent (FORWARD ,1 ); 571if (at_start_sent ) { 572find_first_blank (& curwin -> w_cursor ); 573 } 574if (count == 0 || at_start_sent ) { 575decl (& curwin -> w_cursor ); 576 } 577at_start_sent = !at_start_sent ; 578 } 579} 580 581/// Find word under cursor, cursor at end. 582/// Used while an operator is pending, and in Visual mode. 583/// 584/// @param include true: include word and white space 585/// @param bigword false == word, true == WORD 586int current_word (oparg_T * oap ,int count ,bool include ,bool bigword ) 587{ 588pos_T start_pos ; 589bool inclusive = true; 590bool include_white = false; 591 592cls_bigword = bigword ; 593clearpos (& start_pos ); 594 595// Correct cursor when 'selection' is exclusive 596if (Visual .active && * p_sel == 'e' && lt (Visual .start ,curwin -> w_cursor )) { 597dec_cursor (); 598 } 599 600// When Visual mode is not active, or when the Visual area is only one 601// character, select the word and/or white space under the cursor. 602if (!Visual .active || equalpos (curwin -> w_cursor ,Visual .start )) { 603// Go to start of current word or white space. 604back_in_line (); 605start_pos = curwin -> w_cursor ; 606 607// If the start is on white space, and white space should be included 608// (" word"), or start is not on white space, and white space should 609// not be included ("word"), find end of word. 610if ((cls ()== 0 )== include ) { 611if (end_word (1 ,bigword , true, true)== FAIL ) { 612return FAIL ; 613 } 614 }else { 615// If the start is not on white space, and white space should be 616// included ("word "), or start is on white space and white 617// space should not be included (" "), find start of word. 618// If we end up in the first column of the next line (single char 619// word) back up to end of the line. 620fwd_word (1 ,bigword , true); 621if (curwin -> w_cursor .col == 0 ) { 622decl (& curwin -> w_cursor ); 623 }else { 624oneleft (); 625 } 626 627if (include ) { 628include_white = true; 629 } 630 } 631 632if (Visual .active ) { 633// should do something when inclusive == false ! 634Visual .start = start_pos ; 635redraw_curbuf_later (UPD_INVERTED );// update the inversion 636 }else { 637oap -> start = start_pos ; 638oap -> motion_type = kMTCharWise ; 639 } 640count -- ; 641 } 642 643// When count is still > 0, extend with more objects. 644while (count > 0 ) { 645inclusive = true; 646if (Visual .active && lt (curwin -> w_cursor ,Visual .start )) { 647// In Visual mode, with cursor at start: move cursor back. 648if (decl (& curwin -> w_cursor )== -1 ) { 649return FAIL ; 650 } 651if (include != (cls ()!= 0 )) { 652if (bck_word (1 ,bigword , true)== FAIL ) { 653return FAIL ; 654 } 655 }else { 656if (bckend_word (1 ,bigword , true)== FAIL ) { 657return FAIL ; 658 } 659 (void )incl (& curwin -> w_cursor ); 660 } 661 }else { 662// Move cursor forward one word and/or white area. 663if (incl (& curwin -> w_cursor )== -1 ) { 664return FAIL ; 665 } 666if (include != (cls ()== 0 )) { 667if (fwd_word (1 ,bigword , true)== FAIL && count > 1 ) { 668return FAIL ; 669 } 670// If end is just past a new-line, we don't want to include 671// the first character on the line. 672// Put cursor on last char of white. 673if (oneleft ()== FAIL ) { 674inclusive = false; 675 } 676 }else { 677if (end_word (1 ,bigword , true, true)== FAIL ) { 678return FAIL ; 679 } 680 } 681 } 682count -- ; 683 } 684 685if (include_white && (cls ()!= 0 686|| (curwin -> w_cursor .col == 0 && !inclusive ))) { 687// If we don't include white space at the end, move the start 688// to include some white space there. This makes "daw" work 689// better on the last word in a sentence (and "2daw" on last-but-one 690// word). Also when "2daw" deletes "word." at the end of the line 691// (cursor is at start of next line). 692// But don't delete white space at start of line (indent). 693pos_T pos = curwin -> w_cursor ;// save cursor position 694curwin -> w_cursor = start_pos ; 695if (oneleft ()== OK ) { 696back_in_line (); 697if (cls ()== 0 && curwin -> w_cursor .col > 0 ) { 698if (Visual .active ) { 699Visual .start = curwin -> w_cursor ; 700 }else { 701oap -> start = curwin -> w_cursor ; 702 } 703 } 704 } 705curwin -> w_cursor = pos ;// put cursor back at end 706 } 707 708if (Visual .active ) { 709if (* p_sel == 'e' && inclusive && ltoreq (Visual .start ,curwin -> w_cursor )) { 710inc_cursor (); 711 } 712if (Visual .mode == 'V' ) { 713Visual .mode = 'v' ; 714redraw_cmdline = true;// show mode later 715 } 716 }else { 717oap -> inclusive = inclusive ; 718 } 719 720return OK ; 721} 722 723/// Find the current line ("il") or all lines ("al"). 724/// 725/// @param include true: all lines, false: current line without surrounding 726/// white space 727int current_line (oparg_T * oap ,bool include ) 728{ 729if (include ) { 730if (Visual .active ) { 731Visual .start .lnum = 1 ; 732Visual .start .col = 0 ; 733Visual .start .coladd = 0 ; 734Visual .mode = 'V' ; 735redraw_curbuf_later (UPD_INVERTED ); 736showmode (); 737 }else { 738oap -> cursor_start = curwin -> w_cursor ; 739oap -> restore_cursor = true; 740oap -> start .lnum = 1 ; 741oap -> start .col = 0 ; 742oap -> start .coladd = 0 ; 743oap -> motion_type = kMTLineWise ; 744 } 745curwin -> w_cursor .lnum = curbuf -> b_ml .ml_line_count ; 746curwin -> w_cursor .col = 0 ; 747curwin -> w_cursor .coladd = 0 ; 748return OK ; 749 } 750 751char * line = ml_get (curwin -> w_cursor .lnum ); 752char * start = skipwhite (line ); 753char * end = line + strlen (line ); 754while (end > start ) { 755char * prev = mb_prevptr (line ,end ); 756if (!ascii_iswhite ((uint8_t )(* prev ))) { 757break ; 758 } 759end = prev ; 760 } 761if (start == end ) { 762return FAIL ; 763 } 764 765pos_T start_pos = curwin -> w_cursor ; 766start_pos .col = (colnr_T )(start - line ); 767start_pos .coladd = 0 ; 768 769pos_T end_pos = curwin -> w_cursor ; 770end_pos .col = (colnr_T )(mb_prevptr (line ,end )- line ); 771end_pos .coladd = 0 ; 772 773if (Visual .active ) { 774Visual .start = start_pos ; 775curwin -> w_cursor = end_pos ; 776if (* p_sel == 'e' && ltoreq (Visual .start ,curwin -> w_cursor )) { 777inc_cursor (); 778 } 779Visual .mode = 'v' ; 780redraw_curbuf_later (UPD_INVERTED ); 781showmode (); 782 }else { 783oap -> start = start_pos ; 784oap -> motion_type = kMTCharWise ; 785oap -> inclusive = true; 786curwin -> w_cursor = end_pos ; 787 } 788 789return OK ; 790} 791 792/// Find sentence(s) under the cursor, cursor at end. 793/// When Visual active, extend it by one or more sentences. 794int current_sent (oparg_T * oap ,int count ,bool include ) 795{ 796bool start_blank ; 797int c ; 798bool at_start_sent ; 799int ncount ; 800 801pos_T start_pos = curwin -> w_cursor ; 802pos_T pos = start_pos ; 803findsent (FORWARD ,1 );// Find start of next sentence. 804 805// When the Visual area is bigger than one character: Extend it. 806if (Visual .active && !equalpos (start_pos ,Visual .start )) { 807extend : 808if (lt (start_pos ,Visual .start )) { 809// Cursor at start of Visual area. 810// Find out where we are: 811// - in the white space before a sentence 812// - in a sentence or just after it 813// - at the start of a sentence 814at_start_sent = true; 815decl (& pos ); 816while (lt (pos ,curwin -> w_cursor )) { 817c = gchar_pos (& pos ); 818if (!ascii_iswhite (c )) { 819at_start_sent = false; 820break ; 821 } 822incl (& pos ); 823 } 824if (!at_start_sent ) { 825findsent (BACKWARD ,1 ); 826if (equalpos (curwin -> w_cursor ,start_pos )) { 827at_start_sent = true;// exactly at start of sentence 828 }else { 829// inside a sentence, go to its end (start of next) 830findsent (FORWARD ,1 ); 831 } 832 } 833if (include ) {// "as" gets twice as much as "is" 834count *=2 ; 835 } 836while (count -- ) { 837if (at_start_sent ) { 838find_first_blank (& curwin -> w_cursor ); 839 } 840c = gchar_cursor (); 841if (!at_start_sent || (!include && !ascii_iswhite (c ))) { 842findsent (BACKWARD ,1 ); 843 } 844at_start_sent = !at_start_sent ; 845 } 846 }else { 847// Cursor at end of Visual area. 848// Find out where we are: 849// - just before a sentence 850// - just before or in the white space before a sentence 851// - in a sentence 852incl (& pos ); 853at_start_sent = true; 854if (!equalpos (pos ,curwin -> w_cursor )) {// not just before a sentence 855at_start_sent = false; 856while (lt (pos ,curwin -> w_cursor )) { 857c = gchar_pos (& pos ); 858if (!ascii_iswhite (c )) { 859at_start_sent = true; 860break ; 861 } 862incl (& pos ); 863 } 864if (at_start_sent ) {// in the sentence 865findsent (BACKWARD ,1 ); 866 }else {// in/before white before a sentence 867curwin -> w_cursor = start_pos ; 868 } 869 } 870 871if (include ) {// "as" gets twice as much as "is" 872count *=2 ; 873 } 874findsent_forward (count ,at_start_sent ); 875if (* p_sel == 'e' ) { 876curwin -> w_cursor .col ++ ; 877 } 878 } 879return OK ; 880 } 881 882// If the cursor started on a blank, check if it is just before the start 883// of the next sentence. 884while (c = gchar_pos (& pos ),ascii_iswhite (c )) { 885incl (& pos ); 886 } 887if (equalpos (pos ,curwin -> w_cursor )) { 888start_blank = true; 889find_first_blank (& start_pos );// go back to first blank 890 }else { 891start_blank = false; 892findsent (BACKWARD ,1 ); 893start_pos = curwin -> w_cursor ; 894 } 895if (include ) { 896ncount = count * 2 ; 897 }else { 898ncount = count ; 899if (start_blank ) { 900ncount -- ; 901 } 902 } 903if (ncount > 0 ) { 904findsent_forward (ncount , true); 905 }else { 906decl (& curwin -> w_cursor ); 907 } 908 909if (include ) { 910// If the blank in front of the sentence is included, exclude the 911// blanks at the end of the sentence, go back to the first blank. 912// If there are no trailing blanks, try to include leading blanks. 913if (start_blank ) { 914find_first_blank (& curwin -> w_cursor ); 915c = gchar_pos (& curwin -> w_cursor ); 916if (ascii_iswhite (c )) { 917decl (& curwin -> w_cursor ); 918 } 919 }else if (c = gchar_cursor (), !ascii_iswhite (c )) { 920find_first_blank (& start_pos ); 921 } 922 } 923 924if (Visual .active ) { 925// Avoid getting stuck with "is" on a single space before a sentence. 926if (equalpos (start_pos ,curwin -> w_cursor )) { 927 gotoextend ; 928 } 929if (* p_sel == 'e' ) { 930curwin -> w_cursor .col ++ ; 931 } 932Visual .start = start_pos ; 933Visual .mode = 'v' ; 934redraw_cmdline = true;// show mode later 935redraw_curbuf_later (UPD_INVERTED );// update the inversion 936 }else { 937// include a newline after the sentence, if there is one 938if (incl (& curwin -> w_cursor )== -1 ) { 939oap -> inclusive = true; 940 }else { 941oap -> inclusive = false; 942 } 943oap -> start = start_pos ; 944oap -> motion_type = kMTCharWise ; 945 } 946return OK ; 947} 948 949/// Find block under the cursor, cursor at end. 950/// "what" and "other" are two matching parenthesis/brace/etc. 951/// 952/// @param include true == include white space 953/// @param what '(', '{', etc. 954/// @param other ')', '}', etc. 955int current_block (oparg_T * oap ,int count ,bool include ,int what ,int other ) 956{ 957pos_T * pos = NULL ; 958pos_T start_pos ; 959pos_T * end_pos ; 960bool sol = false;// '{' at start of line 961 962pos_T old_pos = curwin -> w_cursor ; 963pos_T old_end = curwin -> w_cursor ;// remember where we started 964pos_T old_start = old_end ; 965 966// If we start on '(', '{', ')', '}', etc., use the whole block inclusive. 967if (!Visual .active || equalpos (Visual .start ,curwin -> w_cursor )) { 968setpcmark (); 969if (what == '{' ) {// ignore indent 970while (inindent (1 )) { 971if (inc_cursor ()!= 0 ) { 972break ; 973 } 974 } 975 } 976if (gchar_cursor ()== what ) { 977// cursor on '(' or '{', move cursor just after it 978curwin -> w_cursor .col ++ ; 979 } 980 }else if (lt (Visual .start ,curwin -> w_cursor )) { 981old_start = Visual .start ; 982curwin -> w_cursor = Visual .start ;// cursor at low end of Visual 983 }else { 984old_end = Visual .start ; 985 } 986 987// Search backwards for unclosed '(', '{', etc.. 988// Put this position in start_pos. 989// Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the 990// user wants. 991char * save_cpo = p_cpo ; 992p_cpo = vim_strchr (p_cpo ,kCpoMatchbsl )!= NULL ?"%M" :"%" ; 993if ((pos = findmatch (NULL ,what ))!= NULL ) { 994while (count -- > 0 ) { 995if ((pos = findmatch (NULL ,what ))== NULL ) { 996break ; 997 } 998curwin -> w_cursor = * pos ; 999start_pos = * pos ;// the findmatch for end_pos will overwrite *pos 1000 } 1001 }else { 1002while (count -- > 0 ) { 1003if ((pos = findmatchlimit (NULL ,what ,FM_FORWARD ,0 ))== NULL ) { 1004break ; 1005 } 1006curwin -> w_cursor = * pos ; 1007start_pos = * pos ;// the findmatch for end_pos will overwrite *pos 1008 } 1009 } 1010p_cpo = save_cpo ; 1011 1012// Search for matching ')', '}', etc. 1013// Put this position in curwin->w_cursor. 1014if (pos == NULL || (end_pos = findmatch (NULL ,other ))== NULL ) { 1015curwin -> w_cursor = old_pos ; 1016return FAIL ; 1017 } 1018curwin -> w_cursor = * end_pos ; 1019 1020// Try to exclude the '(', '{', ')', '}', etc. when "include" is false. 1021// If the ending '}', ')' or ']' is only preceded by indent, skip that 1022// indent. But only if the resulting area is not smaller than what we 1023// started with. 1024while (!include ) { 1025incl (& start_pos ); 1026sol = (curwin -> w_cursor .col == 0 ); 1027decl (& curwin -> w_cursor ); 1028while (inindent (1 )) { 1029sol = true; 1030if (decl (& curwin -> w_cursor )!= 0 ) { 1031break ; 1032 } 1033 } 1034 1035// In Visual mode, when resulting area is empty 1036// i.e. there is no inner block to select, abort. 1037if (equalpos (start_pos ,* end_pos )&& Visual .active ) { 1038curwin -> w_cursor = old_pos ; 1039return FAIL ; 1040 } 1041 1042// In Visual mode, when the resulting area is not bigger than what we 1043// started with, extend it to the next block, and then exclude again. 1044// Don't try to expand the area if the area is empty. 1045if (!lt (start_pos ,old_start )&& !lt (old_end ,curwin -> w_cursor ) 1046&& !equalpos (start_pos ,curwin -> w_cursor ) 1047&& Visual .active ) { 1048curwin -> w_cursor = old_start ; 1049decl (& curwin -> w_cursor ); 1050if ((pos = findmatch (NULL ,what ))== NULL ) { 1051curwin -> w_cursor = old_pos ; 1052return FAIL ; 1053 } 1054start_pos = * pos ; 1055curwin -> w_cursor = * pos ; 1056if ((end_pos = findmatch (NULL ,other ))== NULL ) { 1057curwin -> w_cursor = old_pos ; 1058return FAIL ; 1059 } 1060curwin -> w_cursor = * end_pos ; 1061 }else { 1062break ; 1063 } 1064 } 1065 1066if (Visual .active ) { 1067if (* p_sel == 'e' ) { 1068inc (& curwin -> w_cursor ); 1069 } 1070if (sol && gchar_cursor ()!= NUL ) { 1071inc (& curwin -> w_cursor );// include the line break 1072 } 1073Visual .start = start_pos ; 1074Visual .mode = 'v' ; 1075redraw_curbuf_later (UPD_INVERTED );// update the inversion 1076showmode (); 1077 }else { 1078oap -> start = start_pos ; 1079oap -> motion_type = kMTCharWise ; 1080oap -> inclusive = false; 1081if (sol ) { 1082incl (& curwin -> w_cursor ); 1083 }else if (ltoreq (start_pos ,curwin -> w_cursor )) { 1084// Include the character under the cursor. 1085oap -> inclusive = true; 1086 }else { 1087// End is before the start (no text in between <>, [], etc.): don't 1088// operate on any text. 1089curwin -> w_cursor = start_pos ; 1090 } 1091 } 1092 1093return OK ; 1094} 1095 1096/// @param end_tag when true, return true if the cursor is on "</aaa>". 1097/// 1098/// @return true if the cursor is on a "<aaa>" tag. Ignore "<aaa/>". 1099static bool in_html_tag (bool end_tag ) 1100{ 1101char * line = get_cursor_line_ptr (); 1102char * p ; 1103int lc = NUL ; 1104pos_T pos ; 1105 1106for (p = line + curwin -> w_cursor .col ;p > line ;) { 1107if (* p == '<' ) {// find '<' under/before cursor 1108break ; 1109 } 1110MB_PTR_BACK (line ,p ); 1111if (* p == '>' ) {// find '>' before cursor 1112break ; 1113 } 1114 } 1115if (* p != '<' ) { 1116return false; 1117 } 1118 1119pos .lnum = curwin -> w_cursor .lnum ; 1120pos .col = (colnr_T )(p - line ); 1121 1122MB_PTR_ADV (p ); 1123if (end_tag ) { 1124// check that there is a '/' after the '<' 1125return * p == '/' ; 1126 } 1127 1128// check that there is no '/' after the '<' 1129if (* p == '/' ) { 1130return false; 1131 } 1132 1133// check that the matching '>' is not preceded by '/' 1134while (true) { 1135if (inc (& pos )< 0 ) { 1136return false; 1137 } 1138int c = (uint8_t )(* ml_get_pos (& pos )); 1139if (c == '>' ) { 1140break ; 1141 } 1142lc = c ; 1143 } 1144return lc != '/' ; 1145} 1146 1147/// Find tag block under the cursor, cursor at end. 1148/// 1149/// @param include true == include white space 1150int current_tagblock (oparg_T * oap ,int count_arg ,bool include ) 1151{ 1152int count = count_arg ; 1153char * cp ; 1154bool do_include = include ; 1155bool save_p_ws = p_ws ; 1156int retval = FAIL ; 1157bool is_inclusive = true; 1158 1159p_ws = false; 1160 1161pos_T old_pos = curwin -> w_cursor ; 1162pos_T old_end = curwin -> w_cursor ;// remember where we started 1163pos_T old_start = old_end ; 1164if (!Visual .active || * p_sel == 'e' ) { 1165decl (& old_end );// old_end is inclusive 1166 } 1167 1168// If we start on "<aaa>" select that block. 1169if (!Visual .active || equalpos (Visual .start ,curwin -> w_cursor )) { 1170setpcmark (); 1171 1172// ignore indent 1173while (inindent (1 )) { 1174if (inc_cursor ()!= 0 ) { 1175break ; 1176 } 1177 } 1178 1179if (in_html_tag (false)) { 1180// cursor on start tag, move to its '>' 1181while (* get_cursor_pos_ptr ()!= '>' ) { 1182if (inc_cursor ()< 0 ) { 1183break ; 1184 } 1185 } 1186 }else if (in_html_tag (true)) { 1187// cursor on end tag, move to just before it 1188while (* get_cursor_pos_ptr ()!= '<' ) { 1189if (dec_cursor ()< 0 ) { 1190break ; 1191 } 1192 } 1193dec_cursor (); 1194old_end = curwin -> w_cursor ; 1195 } 1196 }else if (lt (Visual .start ,curwin -> w_cursor )) { 1197old_start = Visual .start ; 1198curwin -> w_cursor = Visual .start ;// cursor at low end of Visual 1199 }else { 1200old_end = Visual .start ; 1201 } 1202 1203again : 1204// Search backwards for unclosed "<aaa>". 1205// Put this position in start_pos. 1206for (int n = 0 ;n < count ;n ++ ) { 1207if (do_searchpair ("<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)" , 1208"" , 1209"</[^>]*>" ,BACKWARD ,NULL ,0 , 1210NULL ,0 ,0 ) <=0 ) { 1211curwin -> w_cursor = old_pos ; 1212 gototheend ; 1213 } 1214 } 1215pos_T start_pos = curwin -> w_cursor ; 1216 1217// Search for matching "</aaa>". First isolate the "aaa". 1218inc_cursor (); 1219char * p = get_cursor_pos_ptr (); 1220for (cp = p ; 1221* cp != NUL && * cp != '>' && !ascii_iswhite (* cp ); 1222MB_PTR_ADV (cp )) {} 1223int len = (int )(cp - p ); 1224if (len == 0 ) { 1225curwin -> w_cursor = old_pos ; 1226 gototheend ; 1227 } 1228const size_t spat_len = (size_t )len + 39 ; 1229char * const spat = xmalloc (spat_len ); 1230const size_t epat_len = (size_t )len + 9 ; 1231char * const epat = xmalloc (epat_len ); 1232snprintf (spat ,spat_len , 1233"<%.*s\\>\\%%(\\_s\\_[^>]\\{-}\\_[^/]>\\|\\_s\\?>\\)\\c" ,len ,p ); 1234snprintf (epat ,epat_len ,"</%.*s>\\c" ,len ,p ); 1235 1236const int r = do_searchpair (spat ,"" ,epat ,FORWARD ,NULL ,0 ,NULL ,0 ,0 ); 1237 1238xfree (spat ); 1239xfree (epat ); 1240 1241if (r < 1 || lt (curwin -> w_cursor ,old_end )) { 1242// Can't find other end or it's before the previous end. Could be a 1243// HTML tag that doesn't have a matching end. Search backwards for 1244// another starting tag. 1245count = 1 ; 1246curwin -> w_cursor = start_pos ; 1247 gotoagain ; 1248 } 1249 1250if (do_include ) { 1251// Include up to the '>'. 1252while (* get_cursor_pos_ptr ()!= '>' ) { 1253if (inc_cursor ()< 0 ) { 1254break ; 1255 } 1256 } 1257 }else { 1258char * c = get_cursor_pos_ptr (); 1259// Exclude the '<' of the end tag. 1260// If the closing tag is on new line, do not decrement cursor, but make 1261// operation exclusive, so that the linefeed will be selected 1262if (* c == '<' && !Visual .active && curwin -> w_cursor .col == 0 ) { 1263// do not decrement cursor 1264is_inclusive = false; 1265 }else if (* c == '<' ) { 1266dec_cursor (); 1267 } 1268 } 1269pos_T end_pos = curwin -> w_cursor ; 1270 1271if (!do_include ) { 1272// Exclude the start tag, 1273// but skip over '>' if it appears in quotes 1274bool in_quotes = false; 1275curwin -> w_cursor = start_pos ; 1276while (inc_cursor () >=0 ) { 1277p = get_cursor_pos_ptr (); 1278if (* p == '>' && !in_quotes ) { 1279inc_cursor (); 1280start_pos = curwin -> w_cursor ; 1281break ; 1282 }else if (* p == '"' || * p == '\'' ) { 1283in_quotes = !in_quotes ; 1284 } 1285 } 1286curwin -> w_cursor = end_pos ; 1287 1288// If we are in Visual mode and now have the same text as before set 1289// "do_include" and try again. 1290if (Visual .active 1291&& equalpos (start_pos ,old_start ) 1292&& equalpos (end_pos ,old_end )) { 1293do_include = true; 1294curwin -> w_cursor = old_start ; 1295count = count_arg ; 1296 gotoagain ; 1297 } 1298 } 1299 1300if (Visual .active ) { 1301// If the end is before the start there is no text between tags, select 1302// the char under the cursor. 1303if (lt (end_pos ,start_pos )) { 1304curwin -> w_cursor = start_pos ; 1305 }else if (* p_sel == 'e' ) { 1306inc_cursor (); 1307 } 1308Visual .start = start_pos ; 1309Visual .mode = 'v' ; 1310redraw_curbuf_later (UPD_INVERTED );// update the inversion 1311showmode (); 1312 }else { 1313oap -> start = start_pos ; 1314oap -> motion_type = kMTCharWise ; 1315if (lt (end_pos ,start_pos )) { 1316// End is before the start: there is no text between tags; operate 1317// on an empty area. 1318curwin -> w_cursor = start_pos ; 1319oap -> inclusive = false; 1320 }else { 1321oap -> inclusive = is_inclusive ; 1322 } 1323 } 1324retval = OK ; 1325 1326theend : 1327p_ws = save_p_ws ; 1328return retval ; 1329} 1330 1331/// @param include true == include white space 1332/// @param type 'p' for paragraph, 'S' for section 1333int current_par (oparg_T * oap ,int count ,bool include ,int type ) 1334{ 1335int dir ; 1336int retval = OK ; 1337int do_white = false; 1338 1339if (type == 'S' ) {// not implemented yet 1340return FAIL ; 1341 } 1342 1343linenr_T start_lnum = curwin -> w_cursor .lnum ; 1344 1345// When visual area is more than one line: extend it. 1346if (Visual .active && start_lnum != Visual .start .lnum ) { 1347extend : 1348dir = start_lnum < Visual .start .lnum ?BACKWARD :FORWARD ; 1349for (int i = count ;-- i >=0 ;) { 1350if (start_lnum == 1351 (dir == BACKWARD ?1 :curbuf -> b_ml .ml_line_count )) { 1352retval = FAIL ; 1353break ; 1354 } 1355 1356int prev_start_is_white = -1 ; 1357for (int t = 0 ;t < 2 ;t ++ ) { 1358start_lnum += dir ; 1359int start_is_white = linewhite (start_lnum ); 1360if (prev_start_is_white == start_is_white ) { 1361start_lnum -= dir ; 1362break ; 1363 } 1364while (true) { 1365if (start_lnum == (dir == BACKWARD 1366 ?1 :curbuf -> b_ml .ml_line_count )) { 1367break ; 1368 } 1369if (start_is_white != linewhite (start_lnum + dir ) 1370|| (!start_is_white 1371&& startPS (start_lnum + (dir > 0 1372 ?1 :0 ),0 ,0 ))) { 1373break ; 1374 } 1375start_lnum += dir ; 1376 } 1377if (!include ) { 1378break ; 1379 } 1380if (start_lnum == (dir == BACKWARD 1381 ?1 :curbuf -> b_ml .ml_line_count )) { 1382break ; 1383 } 1384prev_start_is_white = start_is_white ; 1385 } 1386 } 1387curwin -> w_cursor .lnum = start_lnum ; 1388curwin -> w_cursor .col = 0 ; 1389return retval ; 1390 } 1391 1392// First move back to the start_lnum of the paragraph or white lines 1393bool white_in_front = linewhite (start_lnum ); 1394while (start_lnum > 1 ) { 1395if (white_in_front ) {// stop at first white line 1396if (!linewhite (start_lnum - 1 )) { 1397break ; 1398 } 1399 }else {// stop at first non-white line of start of paragraph 1400if (linewhite (start_lnum - 1 )|| startPS (start_lnum ,0 ,0 )) { 1401break ; 1402 } 1403 } 1404start_lnum -- ; 1405 } 1406 1407// Move past the end of any white lines. 1408linenr_T end_lnum = start_lnum ; 1409while (end_lnum <=curbuf -> b_ml .ml_line_count && linewhite (end_lnum )) { 1410end_lnum ++ ; 1411 } 1412 1413end_lnum -- ; 1414int i = count ; 1415if (!include && white_in_front ) { 1416i -- ; 1417 } 1418while (i -- ) { 1419if (end_lnum == curbuf -> b_ml .ml_line_count ) { 1420return FAIL ; 1421 } 1422 1423if (!include ) { 1424do_white = linewhite (end_lnum + 1 ); 1425 } 1426 1427if (include || !do_white ) { 1428end_lnum ++ ; 1429// skip to end of paragraph 1430while (end_lnum < curbuf -> b_ml .ml_line_count 1431&& !linewhite (end_lnum + 1 ) 1432&& !startPS (end_lnum + 1 ,0 ,0 )) { 1433end_lnum ++ ; 1434 } 1435 } 1436 1437if (i == 0 && white_in_front && include ) { 1438break ; 1439 } 1440 1441// skip to end of white lines after paragraph 1442if (include || do_white ) { 1443while (end_lnum < curbuf -> b_ml .ml_line_count 1444&& linewhite (end_lnum + 1 )) { 1445end_lnum ++ ; 1446 } 1447 } 1448 } 1449 1450// If there are no empty lines at the end, try to find some empty lines at 1451// the start (unless that has been done already). 1452if (!white_in_front && !linewhite (end_lnum )&& include ) { 1453while (start_lnum > 1 && linewhite (start_lnum - 1 )) { 1454start_lnum -- ; 1455 } 1456 } 1457 1458if (Visual .active ) { 1459// Problem: when doing "Vipipip" nothing happens in a single white 1460// line, we get stuck there. Trap this here. 1461if (Visual .mode == 'V' && start_lnum == curwin -> w_cursor .lnum ) { 1462 gotoextend ; 1463 } 1464if (Visual .start .lnum != start_lnum ) { 1465Visual .start .lnum = start_lnum ; 1466Visual .start .col = 0 ; 1467 } 1468Visual .mode = 'V' ; 1469redraw_curbuf_later (UPD_INVERTED );// update the inversion 1470showmode (); 1471 }else { 1472oap -> start .lnum = start_lnum ; 1473oap -> start .col = 0 ; 1474oap -> motion_type = kMTLineWise ; 1475 } 1476curwin -> w_cursor .lnum = end_lnum ; 1477curwin -> w_cursor .col = 0 ; 1478 1479return OK ; 1480} 1481 1482/// Search quote char from string line[col]. 1483/// Quote character escaped by one of the characters in "escape" is not counted 1484/// as a quote. 1485/// 1486/// @param escape escape characters, can be NULL 1487/// 1488/// @return column number of "quotechar" or -1 when not found. 1489static int find_next_quote (char * line ,int col ,int quotechar ,char * escape ) 1490{ 1491while (true) { 1492int c = (uint8_t )line [col ]; 1493if (c == NUL ) { 1494return -1 ; 1495 }else if (escape != NULL && vim_strchr (escape ,c )) { 1496col ++ ; 1497if (line [col ]== NUL ) { 1498return -1 ; 1499 } 1500 }else if (c == quotechar ) { 1501break ; 1502 } 1503col += utfc_ptr2len (line + col ); 1504 } 1505return col ; 1506} 1507 1508/// Search backwards in "line" from column "col_start" to find "quotechar". 1509/// Quote character escaped by one of the characters in "escape" is not counted 1510/// as a quote. 1511/// 1512/// @param escape escape characters, can be NULL 1513/// 1514/// @return the found column or zero. 1515static int find_prev_quote (char * line ,int col_start ,int quotechar ,char * escape ) 1516{ 1517while (col_start > 0 ) { 1518col_start -- ; 1519col_start -= utf_head_off (line ,line + col_start ); 1520int n = 0 ; 1521if (escape != NULL ) { 1522while (col_start - n > 0 && vim_strchr (escape , 1523 (uint8_t )line [col_start - n - 1 ])!= NULL ) { 1524n ++ ; 1525 } 1526 } 1527if (n & 1 ) { 1528col_start -= n ;// uneven number of escape chars, skip it 1529 }else if ((uint8_t )line [col_start ]== quotechar ) { 1530break ; 1531 } 1532 } 1533return col_start ; 1534} 1535 1536/// Find quote under the cursor, cursor at end. 1537/// 1538/// @param include true == include quote char 1539/// @param quotechar Quote character 1540/// 1541/// @return true if found, else false. 1542bool current_quote (oparg_T * oap ,int count ,bool include ,int quotechar ) 1543FUNC_ATTR_NONNULL_ALL 1544{ 1545char * line = get_cursor_line_ptr (); 1546int col_end ; 1547int col_start = curwin -> w_cursor .col ; 1548bool inclusive = false; 1549bool vis_empty = true;// Visual selection <= 1 char 1550bool vis_bef_curs = false;// Visual starts before cursor 1551bool did_exclusive_adj = false;// adjusted pos for 'selection' 1552bool inside_quotes = false;// Looks like "i'" done before 1553bool selected_quote = false;// Has quote inside selection 1554int i ; 1555bool restore_vis_bef = false;// restore Visual.start on abort 1556 1557// When 'selection' is "exclusive" move the cursor to where it would be 1558// with 'selection' "inclusive", so that the logic is the same for both. 1559// The cursor then is moved forward after adjusting the area. 1560if (Visual .active ) { 1561// this only works within one line 1562if (Visual .start .lnum != curwin -> w_cursor .lnum ) { 1563return false; 1564 } 1565 1566vis_bef_curs = lt (Visual .start ,curwin -> w_cursor ); 1567vis_empty = equalpos (Visual .start ,curwin -> w_cursor ); 1568if (* p_sel == 'e' ) { 1569if (vis_bef_curs ) { 1570dec_cursor (); 1571did_exclusive_adj = true; 1572 }else if (!vis_empty ) { 1573dec (& Visual .start ); 1574did_exclusive_adj = true; 1575 } 1576vis_empty = equalpos (Visual .start ,curwin -> w_cursor ); 1577if (!vis_bef_curs && !vis_empty ) { 1578// Visual.start needs to be start of Visual selection. 1579pos_T t = curwin -> w_cursor ; 1580 1581curwin -> w_cursor = Visual .start ; 1582Visual .start = t ; 1583vis_bef_curs = true; 1584restore_vis_bef = true; 1585 } 1586 } 1587 } 1588 1589if (!vis_empty ) { 1590// Check if the existing selection exactly spans the text inside 1591// quotes. 1592if (vis_bef_curs ) { 1593inside_quotes = Visual .start .col > 0 1594&& (uint8_t )line [Visual .start .col - 1 ]== quotechar 1595&& line [curwin -> w_cursor .col ]!= NUL 1596&& (uint8_t )line [curwin -> w_cursor .col + 1 ]== quotechar ; 1597i = Visual .start .col ; 1598col_end = curwin -> w_cursor .col ; 1599 }else { 1600inside_quotes = curwin -> w_cursor .col > 0 1601&& (uint8_t )line [curwin -> w_cursor .col - 1 ]== quotechar 1602&& line [Visual .start .col ]!= NUL 1603&& (uint8_t )line [Visual .start .col + 1 ]== quotechar ; 1604i = curwin -> w_cursor .col ; 1605col_end = Visual .start .col ; 1606 } 1607 1608// Find out if we have a quote in the selection. 1609while (i <=col_end ) { 1610// check for going over the end of the line, which can happen if 1611// the line was changed after the Visual area was selected. 1612if (line [i ]== NUL ) { 1613break ; 1614 } 1615if ((uint8_t )line [i ++ ]== quotechar ) { 1616selected_quote = true; 1617break ; 1618 } 1619 } 1620 } 1621 1622if (!vis_empty && (uint8_t )line [col_start ]== quotechar ) { 1623// Already selecting something and on a quote character. Find the 1624// next quoted string. 1625if (vis_bef_curs ) { 1626// Assume we are on a closing quote: move to after the next 1627// opening quote. 1628col_start = find_next_quote (line ,col_start + 1 ,quotechar ,NULL ); 1629if (col_start < 0 ) { 1630 gotoabort_search ; 1631 } 1632col_end = find_next_quote (line ,col_start + 1 ,quotechar ,curbuf -> b_p_qe ); 1633if (col_end < 0 ) { 1634// We were on a starting quote perhaps? 1635col_end = col_start ; 1636col_start = curwin -> w_cursor .col ; 1637 } 1638 }else { 1639col_end = find_prev_quote (line ,col_start ,quotechar ,NULL ); 1640if ((uint8_t )line [col_end ]!= quotechar ) { 1641 gotoabort_search ; 1642 } 1643col_start = find_prev_quote (line ,col_end ,quotechar ,curbuf -> b_p_qe ); 1644if ((uint8_t )line [col_start ]!= quotechar ) { 1645// We were on an ending quote perhaps? 1646col_start = col_end ; 1647col_end = curwin -> w_cursor .col ; 1648 } 1649 } 1650 }else if ((uint8_t )line [col_start ]== quotechar || !vis_empty ) { 1651int first_col = col_start ; 1652 1653if (!vis_empty ) { 1654if (vis_bef_curs ) { 1655first_col = find_next_quote (line ,col_start ,quotechar ,NULL ); 1656 }else { 1657first_col = find_prev_quote (line ,col_start ,quotechar ,NULL ); 1658 } 1659 } 1660// The cursor is on a quote, we don't know if it's the opening or 1661// closing quote. Search from the start of the line to find out. 1662// Also do this when there is a Visual area, a' may leave the cursor 1663// in between two strings. 1664col_start = 0 ; 1665while (true) { 1666// Find open quote character. 1667col_start = find_next_quote (line ,col_start ,quotechar ,NULL ); 1668if (col_start < 0 || col_start > first_col ) { 1669 gotoabort_search ; 1670 } 1671// Find close quote character. 1672col_end = find_next_quote (line ,col_start + 1 ,quotechar ,curbuf -> b_p_qe ); 1673if (col_end < 0 ) { 1674 gotoabort_search ; 1675 } 1676// If is cursor between start and end quote character, it is 1677// target text object. 1678if (col_start <=first_col && first_col <=col_end ) { 1679break ; 1680 } 1681col_start = col_end + 1 ; 1682 } 1683 }else { 1684// Search backward for a starting quote. 1685col_start = find_prev_quote (line ,col_start ,quotechar ,curbuf -> b_p_qe ); 1686if ((uint8_t )line [col_start ]!= quotechar ) { 1687// No quote before the cursor, look after the cursor. 1688col_start = find_next_quote (line ,col_start ,quotechar ,NULL ); 1689if (col_start < 0 ) { 1690 gotoabort_search ; 1691 } 1692 } 1693 1694// Find close quote character. 1695col_end = find_next_quote (line ,col_start + 1 ,quotechar ,curbuf -> b_p_qe ); 1696if (col_end < 0 ) { 1697 gotoabort_search ; 1698 } 1699 } 1700 1701// When "include" is true, include spaces after closing quote or before 1702// the starting quote. 1703if (include ) { 1704if (ascii_iswhite (line [col_end + 1 ])) { 1705while (ascii_iswhite (line [col_end + 1 ])) { 1706col_end ++ ; 1707 } 1708 }else { 1709while (col_start > 0 && ascii_iswhite (line [col_start - 1 ])) { 1710col_start -- ; 1711 } 1712 } 1713 } 1714 1715// Set start position. After vi" another i" must include the ". 1716// For v2i" include the quotes. 1717if (!include && count < 2 && (vis_empty || !inside_quotes )) { 1718col_start ++ ; 1719 } 1720curwin -> w_cursor .col = col_start ; 1721if (Visual .active ) { 1722// Set the start of the Visual area when the Visual area was empty, we 1723// were just inside quotes or the Visual area didn't start at a quote 1724// and didn't include a quote. 1725if (vis_empty 1726|| (vis_bef_curs 1727&& !selected_quote 1728&& (inside_quotes 1729|| ((uint8_t )line [Visual .start .col ]!= quotechar 1730&& (Visual .start .col == 0 1731|| (uint8_t )line [Visual .start .col - 1 ]!= quotechar ))))) { 1732Visual .start = curwin -> w_cursor ; 1733redraw_curbuf_later (UPD_INVERTED ); 1734 } 1735 }else { 1736oap -> start = curwin -> w_cursor ; 1737oap -> motion_type = kMTCharWise ; 1738 } 1739 1740// Set end position. 1741curwin -> w_cursor .col = col_end ; 1742if ((include || count > 1 1743// After vi" another i" must include the ". 1744|| (!vis_empty && inside_quotes ))&& inc_cursor ()== 2 ) { 1745inclusive = true; 1746 } 1747if (Visual .active ) { 1748if (vis_empty || vis_bef_curs ) { 1749// decrement cursor when 'selection' is not exclusive 1750if (* p_sel != 'e' ) { 1751dec_cursor (); 1752 } 1753 }else { 1754// Cursor is at start of Visual area. Set the end of the Visual 1755// area when it was just inside quotes or it didn't end at a 1756// quote. 1757if (inside_quotes 1758|| (!selected_quote 1759&& (uint8_t )line [Visual .start .col ]!= quotechar 1760&& (line [Visual .start .col ]== NUL 1761|| (uint8_t )line [Visual .start .col + 1 ]!= quotechar ))) { 1762dec_cursor (); 1763Visual .start = curwin -> w_cursor ; 1764 } 1765curwin -> w_cursor .col = col_start ; 1766 } 1767if (Visual .mode == 'V' ) { 1768Visual .mode = 'v' ; 1769redraw_cmdline = true;// show mode later 1770 } 1771 }else { 1772// Set inclusive and other oap's flags. 1773oap -> inclusive = inclusive ; 1774 } 1775 1776return true; 1777 1778abort_search : 1779if (Visual .active && * p_sel == 'e' ) { 1780if (did_exclusive_adj ) { 1781inc_cursor (); 1782 } 1783if (restore_vis_bef ) { 1784pos_T t = curwin -> w_cursor ; 1785 1786curwin -> w_cursor = Visual .start ; 1787Visual .start = t ; 1788 } 1789 } 1790return false; 1791}