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