blob: d77ec961abae767edda7ff19b3bfd1b591f5cfcf [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;
229 if (curr == curbuf->b_ml.ml_line_count && what != '}')
230 {
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.
235 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
236 {
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 }
475 return OK;
476}
477
478/*
479 * end_word() - move to the end of the word
480 *
481 * There is an apparent bug in the 'e' motion of the real vi. At least on the
482 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
483 * motion crosses blank lines. When the real vi crosses a blank line in an
484 * 'e' motion, the cursor is placed on the FIRST character of the next
485 * non-blank line. The 'E' command, however, works correctly. Since this
486 * appears to be a bug, I have not duplicated it here.
487 *
488 * Returns FAIL if end of the file was reached.
489 *
490 * If stop is TRUE and we are already on the end of a word, move one less.
491 * If empty is TRUE stop on an empty line.
492 */
493 int
494end_word(
495 long count,
496 int bigword,
497 int stop,
498 int empty)
499{
500 int sclass; // starting class
501
502 curwin->w_cursor.coladd = 0;
503 cls_bigword = bigword;
504 while (--count >= 0)
505 {
506#ifdef FEAT_FOLDING
507 // When inside a range of folded lines, move to the last char of the
508 // last line.
509 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
510 coladvance((colnr_T)MAXCOL);
511#endif
512 sclass = cls();
513 if (inc_cursor() == -1)
514 return FAIL;
515
516 /*
517 * If we're in the middle of a word, we just have to move to the end
518 * of it.
519 */
520 if (cls() == sclass && sclass != 0)
521 {
522 /*
523 * Move forward to end of the current word
524 */
525 if (skip_chars(sclass, FORWARD))
526 return FAIL;
527 }
528 else if (!stop || sclass == 0)
529 {
530 /*
531 * We were at the end of a word. Go to the end of the next word.
532 * First skip white space, if 'empty' is TRUE, stop at empty line.
533 */
534 while (cls() == 0)
535 {
536 if (empty && curwin->w_cursor.col == 0
537 && LINEEMPTY(curwin->w_cursor.lnum))
538 goto finished;
539 if (inc_cursor() == -1) // hit end of file, stop here
540 return FAIL;
541 }
542
543 /*
544 * Move forward to the end of this word.
545 */
546 if (skip_chars(cls(), FORWARD))
547 return FAIL;
548 }
549 dec_cursor(); // overshot - one char backward
550finished:
551 stop = FALSE; // we move only one word less
552 }
553 return OK;
554}
555
556/*
557 * Move back to the end of the word.
558 *
559 * Returns FAIL if start of the file was reached.
560 */
561 int
562bckend_word(
563 long count,
564 int bigword, // TRUE for "B"
565 int eol) // TRUE: stop at end of line.
566{
567 int sclass; // starting class
568 int i;
569
570 curwin->w_cursor.coladd = 0;
571 cls_bigword = bigword;
572 while (--count >= 0)
573 {
574 sclass = cls();
575 if ((i = dec_cursor()) == -1)
576 return FAIL;
577 if (eol && i == 1)
578 return OK;
579
580 /*
581 * Move backward to before the start of this word.
582 */
583 if (sclass != 0)
584 {
585 while (cls() == sclass)
586 if ((i = dec_cursor()) == -1 || (eol && i == 1))
587 return OK;
588 }
589
590 /*
591 * Move backward to end of the previous word
592 */
593 while (cls() == 0)
594 {
595 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
596 break;
597 if ((i = dec_cursor()) == -1 || (eol && i == 1))
598 return OK;
599 }
600 }
601 return OK;
602}
603
604/*
605 * Skip a row of characters of the same class.
606 * Return TRUE when end-of-file reached, FALSE otherwise.
607 */
608 static int
609skip_chars(int cclass, int dir)
610{
611 while (cls() == cclass)
612 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
613 return TRUE;
614 return FALSE;
615}
616
Bram Moolenaared8ce052020-04-29 21:04:15 +0200617/*
618 * Go back to the start of the word or the start of white space
619 */
620 static void
621back_in_line(void)
622{
623 int sclass; // starting class
624
625 sclass = cls();
626 for (;;)
627 {
628 if (curwin->w_cursor.col == 0) // stop at start of line
629 break;
630 dec_cursor();
631 if (cls() != sclass) // stop at start of word
632 {
633 inc_cursor();
634 break;
635 }
636 }
637}
638
639 static void
640find_first_blank(pos_T *posp)
641{
642 int c;
643
644 while (decl(posp) != -1)
645 {
646 c = gchar_pos(posp);
647 if (!VIM_ISWHITE(c))
648 {
649 incl(posp);
650 break;
651 }
652 }
653}
654
655/*
656 * Skip count/2 sentences and count/2 separating white spaces.
657 */
658 static void
659findsent_forward(
660 long count,
661 int at_start_sent) // cursor is at start of sentence
662{
663 while (count--)
664 {
665 findsent(FORWARD, 1L);
666 if (at_start_sent)
667 find_first_blank(&curwin->w_cursor);
668 if (count == 0 || at_start_sent)
669 decl(&curwin->w_cursor);
670 at_start_sent = !at_start_sent;
671 }
672}
673
674/*
675 * Find word under cursor, cursor at end.
676 * Used while an operator is pending, and in Visual mode.
677 */
678 int
679current_word(
680 oparg_T *oap,
681 long count,
682 int include, // TRUE: include word and white space
683 int bigword) // FALSE == word, TRUE == WORD
684{
685 pos_T start_pos;
686 pos_T pos;
687 int inclusive = TRUE;
688 int include_white = FALSE;
689
690 cls_bigword = bigword;
691 CLEAR_POS(&start_pos);
692
693 // Correct cursor when 'selection' is exclusive
694 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
695 dec_cursor();
696
697 /*
698 * When Visual mode is not active, or when the VIsual area is only one
699 * character, select the word and/or white space under the cursor.
700 */
701 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
702 {
703 /*
704 * Go to start of current word or white space.
705 */
706 back_in_line();
707 start_pos = curwin->w_cursor;
708
709 /*
710 * If the start is on white space, and white space should be included
711 * (" word"), or start is not on white space, and white space should
712 * not be included ("word"), find end of word.
713 */
714 if ((cls() == 0) == include)
715 {
716 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
717 return FAIL;
718 }
719 else
720 {
721 /*
722 * If the start is not on white space, and white space should be
723 * included ("word "), or start is on white space and white
724 * space should not be included (" "), find start of word.
725 * If we end up in the first column of the next line (single char
726 * word) back up to end of the line.
727 */
728 fwd_word(1L, bigword, TRUE);
729 if (curwin->w_cursor.col == 0)
730 decl(&curwin->w_cursor);
731 else
732 oneleft();
733
734 if (include)
735 include_white = TRUE;
736 }
737
738 if (VIsual_active)
739 {
740 // should do something when inclusive == FALSE !
741 VIsual = start_pos;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100742 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaared8ce052020-04-29 21:04:15 +0200743 }
744 else
745 {
746 oap->start = start_pos;
747 oap->motion_type = MCHAR;
748 }
749 --count;
750 }
751
752 /*
753 * When count is still > 0, extend with more objects.
754 */
755 while (count > 0)
756 {
757 inclusive = TRUE;
758 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
759 {
760 /*
761 * In Visual mode, with cursor at start: move cursor back.
762 */
763 if (decl(&curwin->w_cursor) == -1)
764 return FAIL;
765 if (include != (cls() != 0))
766 {
767 if (bck_word(1L, bigword, TRUE) == FAIL)
768 return FAIL;
769 }
770 else
771 {
772 if (bckend_word(1L, bigword, TRUE) == FAIL)
773 return FAIL;
774 (void)incl(&curwin->w_cursor);
775 }
776 }
777 else
778 {
779 /*
780 * Move cursor forward one word and/or white area.
781 */
782 if (incl(&curwin->w_cursor) == -1)
783 return FAIL;
784 if (include != (cls() == 0))
785 {
786 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
787 return FAIL;
788 /*
789 * If end is just past a new-line, we don't want to include
790 * the first character on the line.
791 * Put cursor on last char of white.
792 */
793 if (oneleft() == FAIL)
794 inclusive = FALSE;
795 }
796 else
797 {
798 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
799 return FAIL;
800 }
801 }
802 --count;
803 }
804
805 if (include_white && (cls() != 0
806 || (curwin->w_cursor.col == 0 && !inclusive)))
807 {
808 /*
809 * If we don't include white space at the end, move the start
810 * to include some white space there. This makes "daw" work
811 * better on the last word in a sentence (and "2daw" on last-but-one
812 * word). Also when "2daw" deletes "word." at the end of the line
813 * (cursor is at start of next line).
814 * But don't delete white space at start of line (indent).
815 */
816 pos = curwin->w_cursor; // save cursor position
817 curwin->w_cursor = start_pos;
818 if (oneleft() == OK)
819 {
820 back_in_line();
821 if (cls() == 0 && curwin->w_cursor.col > 0)
822 {
823 if (VIsual_active)
824 VIsual = curwin->w_cursor;
825 else
826 oap->start = curwin->w_cursor;
827 }
828 }
829 curwin->w_cursor = pos; // put cursor back at end
830 }
831
832 if (VIsual_active)
833 {
834 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
835 inc_cursor();
836 if (VIsual_mode == 'V')
837 {
838 VIsual_mode = 'v';
839 redraw_cmdline = TRUE; // show mode later
840 }
841 }
842 else
843 oap->inclusive = inclusive;
844
845 return OK;
846}
847
848/*
849 * Find sentence(s) under the cursor, cursor at end.
850 * When Visual active, extend it by one or more sentences.
851 */
852 int
853current_sent(oparg_T *oap, long count, int include)
854{
855 pos_T start_pos;
856 pos_T pos;
857 int start_blank;
858 int c;
859 int at_start_sent;
860 long ncount;
861
862 start_pos = curwin->w_cursor;
863 pos = start_pos;
864 findsent(FORWARD, 1L); // Find start of next sentence.
865
866 /*
867 * When the Visual area is bigger than one character: Extend it.
868 */
869 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
870 {
871extend:
872 if (LT_POS(start_pos, VIsual))
873 {
874 /*
875 * Cursor at start of Visual area.
876 * Find out where we are:
877 * - in the white space before a sentence
878 * - in a sentence or just after it
879 * - at the start of a sentence
880 */
881 at_start_sent = TRUE;
882 decl(&pos);
883 while (LT_POS(pos, curwin->w_cursor))
884 {
885 c = gchar_pos(&pos);
886 if (!VIM_ISWHITE(c))
887 {
888 at_start_sent = FALSE;
889 break;
890 }
891 incl(&pos);
892 }
893 if (!at_start_sent)
894 {
895 findsent(BACKWARD, 1L);
896 if (EQUAL_POS(curwin->w_cursor, start_pos))
897 at_start_sent = TRUE; // exactly at start of sentence
898 else
899 // inside a sentence, go to its end (start of next)
900 findsent(FORWARD, 1L);
901 }
902 if (include) // "as" gets twice as much as "is"
903 count *= 2;
904 while (count--)
905 {
906 if (at_start_sent)
907 find_first_blank(&curwin->w_cursor);
908 c = gchar_cursor();
909 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
910 findsent(BACKWARD, 1L);
911 at_start_sent = !at_start_sent;
912 }
913 }
914 else
915 {
916 /*
917 * Cursor at end of Visual area.
918 * Find out where we are:
919 * - just before a sentence
920 * - just before or in the white space before a sentence
921 * - in a sentence
922 */
923 incl(&pos);
924 at_start_sent = TRUE;
925 // not just before a sentence
926 if (!EQUAL_POS(pos, curwin->w_cursor))
927 {
928 at_start_sent = FALSE;
929 while (LT_POS(pos, curwin->w_cursor))
930 {
931 c = gchar_pos(&pos);
932 if (!VIM_ISWHITE(c))
933 {
934 at_start_sent = TRUE;
935 break;
936 }
937 incl(&pos);
938 }
939 if (at_start_sent) // in the sentence
940 findsent(BACKWARD, 1L);
941 else // in/before white before a sentence
942 curwin->w_cursor = start_pos;
943 }
944
945 if (include) // "as" gets twice as much as "is"
946 count *= 2;
947 findsent_forward(count, at_start_sent);
948 if (*p_sel == 'e')
949 ++curwin->w_cursor.col;
950 }
951 return OK;
952 }
953
954 /*
955 * If the cursor started on a blank, check if it is just before the start
956 * of the next sentence.
957 */
958 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) // VIM_ISWHITE() is a macro
959 incl(&pos);
960 if (EQUAL_POS(pos, curwin->w_cursor))
961 {
962 start_blank = TRUE;
963 find_first_blank(&start_pos); // go back to first blank
964 }
965 else
966 {
967 start_blank = FALSE;
968 findsent(BACKWARD, 1L);
969 start_pos = curwin->w_cursor;
970 }
971 if (include)
972 ncount = count * 2;
973 else
974 {
975 ncount = count;
976 if (start_blank)
977 --ncount;
978 }
979 if (ncount > 0)
980 findsent_forward(ncount, TRUE);
981 else
982 decl(&curwin->w_cursor);
983
984 if (include)
985 {
986 /*
987 * If the blank in front of the sentence is included, exclude the
988 * blanks at the end of the sentence, go back to the first blank.
989 * If there are no trailing blanks, try to include leading blanks.
990 */
991 if (start_blank)
992 {
993 find_first_blank(&curwin->w_cursor);
994 c = gchar_pos(&curwin->w_cursor); // VIM_ISWHITE() is a macro
995 if (VIM_ISWHITE(c))
996 decl(&curwin->w_cursor);
997 }
998 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
999 find_first_blank(&start_pos);
1000 }
1001
1002 if (VIsual_active)
1003 {
1004 // Avoid getting stuck with "is" on a single space before a sentence.
1005 if (EQUAL_POS(start_pos, curwin->w_cursor))
1006 goto extend;
1007 if (*p_sel == 'e')
1008 ++curwin->w_cursor.col;
1009 VIsual = start_pos;
1010 VIsual_mode = 'v';
1011 redraw_cmdline = TRUE; // show mode later
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001012 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaared8ce052020-04-29 21:04:15 +02001013 }
1014 else
1015 {
1016 // include a newline after the sentence, if there is one
1017 if (incl(&curwin->w_cursor) == -1)
1018 oap->inclusive = TRUE;
1019 else
1020 oap->inclusive = FALSE;
1021 oap->start = start_pos;
1022 oap->motion_type = MCHAR;
1023 }
1024 return OK;
1025}
1026
1027/*
1028 * Find block under the cursor, cursor at end.
1029 * "what" and "other" are two matching parenthesis/brace/etc.
1030 */
1031 int
1032current_block(
1033 oparg_T *oap,
1034 long count,
1035 int include, // TRUE == include white space
1036 int what, // '(', '{', etc.
1037 int other) // ')', '}', etc.
1038{
1039 pos_T old_pos;
1040 pos_T *pos = NULL;
1041 pos_T start_pos;
1042 pos_T *end_pos;
1043 pos_T old_start, old_end;
1044 char_u *save_cpo;
1045 int sol = FALSE; // '{' at start of line
1046
1047 old_pos = curwin->w_cursor;
1048 old_end = curwin->w_cursor; // remember where we started
1049 old_start = old_end;
1050
1051 /*
1052 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
1053 */
1054 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
1055 {
1056 setpcmark();
1057 if (what == '{') // ignore indent
1058 while (inindent(1))
1059 if (inc_cursor() != 0)
1060 break;
1061 if (gchar_cursor() == what)
1062 // cursor on '(' or '{', move cursor just after it
1063 ++curwin->w_cursor.col;
1064 }
1065 else if (LT_POS(VIsual, curwin->w_cursor))
1066 {
1067 old_start = VIsual;
1068 curwin->w_cursor = VIsual; // cursor at low end of Visual
1069 }
1070 else
1071 old_end = VIsual;
1072
1073 /*
1074 * Search backwards for unclosed '(', '{', etc..
1075 * Put this position in start_pos.
1076 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
1077 * user wants.
1078 */
1079 save_cpo = p_cpo;
1080 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Connor Lane Smithb9115da2021-07-31 13:31:42 +02001081 if ((pos = findmatch(NULL, what)) != NULL)
Bram Moolenaared8ce052020-04-29 21:04:15 +02001082 {
Connor Lane Smithb9115da2021-07-31 13:31:42 +02001083 while (count-- > 0)
1084 {
1085 if ((pos = findmatch(NULL, what)) == NULL)
1086 break;
1087 curwin->w_cursor = *pos;
1088 start_pos = *pos; // the findmatch for end_pos will overwrite *pos
1089 }
1090 }
1091 else
1092 {
1093 while (count-- > 0)
1094 {
1095 if ((pos = findmatchlimit(NULL, what, FM_FORWARD, 0)) == NULL)
1096 break;
1097 curwin->w_cursor = *pos;
1098 start_pos = *pos; // the findmatch for end_pos will overwrite *pos
1099 }
Bram Moolenaared8ce052020-04-29 21:04:15 +02001100 }
1101 p_cpo = save_cpo;
1102
1103 /*
1104 * Search for matching ')', '}', etc.
1105 * Put this position in curwin->w_cursor.
1106 */
1107 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
1108 {
1109 curwin->w_cursor = old_pos;
1110 return FAIL;
1111 }
1112 curwin->w_cursor = *end_pos;
1113
1114 /*
1115 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
1116 * If the ending '}', ')' or ']' is only preceded by indent, skip that
1117 * indent. But only if the resulting area is not smaller than what we
1118 * started with.
1119 */
1120 while (!include)
1121 {
1122 incl(&start_pos);
1123 sol = (curwin->w_cursor.col == 0);
1124 decl(&curwin->w_cursor);
1125 while (inindent(1))
1126 {
1127 sol = TRUE;
1128 if (decl(&curwin->w_cursor) != 0)
1129 break;
1130 }
1131
1132 /*
1133 * In Visual mode, when the resulting area is not bigger than what we
1134 * started with, extend it to the next block, and then exclude again.
LemonBoy53737b52022-05-24 11:49:31 +01001135 * Don't try to expand the area if the area is empty.
Bram Moolenaared8ce052020-04-29 21:04:15 +02001136 */
1137 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
LemonBoy53737b52022-05-24 11:49:31 +01001138 && !EQUAL_POS(start_pos, curwin->w_cursor)
Bram Moolenaared8ce052020-04-29 21:04:15 +02001139 && VIsual_active)
1140 {
1141 curwin->w_cursor = old_start;
1142 decl(&curwin->w_cursor);
1143 if ((pos = findmatch(NULL, what)) == NULL)
1144 {
1145 curwin->w_cursor = old_pos;
1146 return FAIL;
1147 }
1148 start_pos = *pos;
1149 curwin->w_cursor = *pos;
1150 if ((end_pos = findmatch(NULL, other)) == NULL)
1151 {
1152 curwin->w_cursor = old_pos;
1153 return FAIL;
1154 }
1155 curwin->w_cursor = *end_pos;
1156 }
1157 else
1158 break;
1159 }
1160
1161 if (VIsual_active)
1162 {
1163 if (*p_sel == 'e')
1164 inc(&curwin->w_cursor);
1165 if (sol && gchar_cursor() != NUL)
1166 inc(&curwin->w_cursor); // include the line break
1167 VIsual = start_pos;
1168 VIsual_mode = 'v';
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001169 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaared8ce052020-04-29 21:04:15 +02001170 showmode();
1171 }
1172 else
1173 {
1174 oap->start = start_pos;
1175 oap->motion_type = MCHAR;
1176 oap->inclusive = FALSE;
1177 if (sol)
1178 incl(&curwin->w_cursor);
1179 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
1180 // Include the character under the cursor.
1181 oap->inclusive = TRUE;
1182 else
1183 // End is before the start (no text in between <>, [], etc.): don't
1184 // operate on any text.
1185 curwin->w_cursor = start_pos;
1186 }
1187
1188 return OK;
1189}
1190
Bram Moolenaar88774872022-08-16 20:24:29 +01001191#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared8ce052020-04-29 21:04:15 +02001192/*
1193 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
1194 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
1195 */
1196 static int
1197in_html_tag(
1198 int end_tag)
1199{
1200 char_u *line = ml_get_curline();
1201 char_u *p;
1202 int c;
1203 int lc = NUL;
1204 pos_T pos;
1205
1206 if (enc_dbcs)
1207 {
1208 char_u *lp = NULL;
1209
1210 // We search forward until the cursor, because searching backwards is
1211 // very slow for DBCS encodings.
1212 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
1213 if (*p == '>' || *p == '<')
1214 {
1215 lc = *p;
1216 lp = p;
1217 }
1218 if (*p != '<') // check for '<' under cursor
1219 {
1220 if (lc != '<')
1221 return FALSE;
1222 p = lp;
1223 }
1224 }
1225 else
1226 {
1227 for (p = line + curwin->w_cursor.col; p > line; )
1228 {
1229 if (*p == '<') // find '<' under/before cursor
1230 break;
1231 MB_PTR_BACK(line, p);
1232 if (*p == '>') // find '>' before cursor
1233 break;
1234 }
1235 if (*p != '<')
1236 return FALSE;
1237 }
1238
1239 pos.lnum = curwin->w_cursor.lnum;
1240 pos.col = (colnr_T)(p - line);
1241
1242 MB_PTR_ADV(p);
1243 if (end_tag)
1244 // check that there is a '/' after the '<'
1245 return *p == '/';
1246
1247 // check that there is no '/' after the '<'
1248 if (*p == '/')
1249 return FALSE;
1250
1251 // check that the matching '>' is not preceded by '/'
1252 for (;;)
1253 {
1254 if (inc(&pos) < 0)
1255 return FALSE;
1256 c = *ml_get_pos(&pos);
1257 if (c == '>')
1258 break;
1259 lc = c;
1260 }
1261 return lc != '/';
1262}
1263
1264/*
1265 * Find tag block under the cursor, cursor at end.
1266 */
1267 int
1268current_tagblock(
1269 oparg_T *oap,
1270 long count_arg,
1271 int include) // TRUE == include white space
1272{
1273 long count = count_arg;
1274 long n;
1275 pos_T old_pos;
1276 pos_T start_pos;
1277 pos_T end_pos;
1278 pos_T old_start, old_end;
1279 char_u *spat, *epat;
1280 char_u *p;
1281 char_u *cp;
1282 int len;
1283 int r;
1284 int do_include = include;
1285 int save_p_ws = p_ws;
1286 int retval = FAIL;
1287 int is_inclusive = TRUE;
1288
1289 p_ws = FALSE;
1290
1291 old_pos = curwin->w_cursor;
1292 old_end = curwin->w_cursor; // remember where we started
1293 old_start = old_end;
1294 if (!VIsual_active || *p_sel == 'e')
1295 decl(&old_end); // old_end is inclusive
1296
1297 /*
1298 * If we start on "<aaa>" select that block.
1299 */
1300 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
1301 {
1302 setpcmark();
1303
1304 // ignore indent
1305 while (inindent(1))
1306 if (inc_cursor() != 0)
1307 break;
1308
1309 if (in_html_tag(FALSE))
1310 {
1311 // cursor on start tag, move to its '>'
1312 while (*ml_get_cursor() != '>')
1313 if (inc_cursor() < 0)
1314 break;
1315 }
1316 else if (in_html_tag(TRUE))
1317 {
1318 // cursor on end tag, move to just before it
1319 while (*ml_get_cursor() != '<')
1320 if (dec_cursor() < 0)
1321 break;
1322 dec_cursor();
1323 old_end = curwin->w_cursor;
1324 }
1325 }
1326 else if (LT_POS(VIsual, curwin->w_cursor))
1327 {
1328 old_start = VIsual;
1329 curwin->w_cursor = VIsual; // cursor at low end of Visual
1330 }
1331 else
1332 old_end = VIsual;
1333
1334again:
1335 /*
1336 * Search backwards for unclosed "<aaa>".
1337 * Put this position in start_pos.
1338 */
1339 for (n = 0; n < count; ++n)
1340 {
1341 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
1342 (char_u *)"",
1343 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
1344 NULL, (linenr_T)0, 0L) <= 0)
1345 {
1346 curwin->w_cursor = old_pos;
1347 goto theend;
1348 }
1349 }
1350 start_pos = curwin->w_cursor;
1351
1352 /*
1353 * Search for matching "</aaa>". First isolate the "aaa".
1354 */
1355 inc_cursor();
1356 p = ml_get_cursor();
1357 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
1358 ;
1359 len = (int)(cp - p);
1360 if (len == 0)
1361 {
1362 curwin->w_cursor = old_pos;
1363 goto theend;
1364 }
Bram Moolenaara604ccc2020-10-15 21:23:28 +02001365 spat = alloc(len + 39);
Bram Moolenaared8ce052020-04-29 21:04:15 +02001366 epat = alloc(len + 9);
1367 if (spat == NULL || epat == NULL)
1368 {
1369 vim_free(spat);
1370 vim_free(epat);
1371 curwin->w_cursor = old_pos;
1372 goto theend;
1373 }
Bram Moolenaara604ccc2020-10-15 21:23:28 +02001374 sprintf((char *)spat, "<%.*s\\>\\%%(\\_s\\_[^>]\\{-}\\_[^/]>\\|\\_s\\?>\\)\\c", len, p);
Bram Moolenaared8ce052020-04-29 21:04:15 +02001375 sprintf((char *)epat, "</%.*s>\\c", len, p);
1376
1377 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
1378 0, NULL, (linenr_T)0, 0L);
1379
1380 vim_free(spat);
1381 vim_free(epat);
1382
1383 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
1384 {
1385 // Can't find other end or it's before the previous end. Could be a
1386 // HTML tag that doesn't have a matching end. Search backwards for
1387 // another starting tag.
1388 count = 1;
1389 curwin->w_cursor = start_pos;
1390 goto again;
1391 }
1392
1393 if (do_include)
1394 {
1395 // Include up to the '>'.
1396 while (*ml_get_cursor() != '>')
1397 if (inc_cursor() < 0)
1398 break;
1399 }
1400 else
1401 {
1402 char_u *c = ml_get_cursor();
1403
1404 // Exclude the '<' of the end tag.
1405 // If the closing tag is on new line, do not decrement cursor, but
1406 // make operation exclusive, so that the linefeed will be selected
1407 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
1408 // do not decrement cursor
1409 is_inclusive = FALSE;
1410 else if (*c == '<')
1411 dec_cursor();
1412 }
1413 end_pos = curwin->w_cursor;
1414
1415 if (!do_include)
1416 {
1417 // Exclude the start tag.
1418 curwin->w_cursor = start_pos;
1419 while (inc_cursor() >= 0)
1420 if (*ml_get_cursor() == '>')
1421 {
1422 inc_cursor();
1423 start_pos = curwin->w_cursor;
1424 break;
1425 }
1426 curwin->w_cursor = end_pos;
1427
1428 // If we are in Visual mode and now have the same text as before set
1429 // "do_include" and try again.
1430 if (VIsual_active && EQUAL_POS(start_pos, old_start)
1431 && EQUAL_POS(end_pos, old_end))
1432 {
1433 do_include = TRUE;
1434 curwin->w_cursor = old_start;
1435 count = count_arg;
1436 goto again;
1437 }
1438 }
1439
1440 if (VIsual_active)
1441 {
1442 // If the end is before the start there is no text between tags, select
1443 // the char under the cursor.
1444 if (LT_POS(end_pos, start_pos))
1445 curwin->w_cursor = start_pos;
1446 else if (*p_sel == 'e')
1447 inc_cursor();
1448 VIsual = start_pos;
1449 VIsual_mode = 'v';
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001450 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaared8ce052020-04-29 21:04:15 +02001451 showmode();
1452 }
1453 else
1454 {
1455 oap->start = start_pos;
1456 oap->motion_type = MCHAR;
1457 if (LT_POS(end_pos, start_pos))
1458 {
1459 // End is before the start: there is no text between tags; operate
1460 // on an empty area.
1461 curwin->w_cursor = start_pos;
1462 oap->inclusive = FALSE;
1463 }
1464 else
1465 oap->inclusive = is_inclusive;
1466 }
1467 retval = OK;
1468
1469theend:
1470 p_ws = save_p_ws;
1471 return retval;
1472}
Bram Moolenaar88774872022-08-16 20:24:29 +01001473#endif
Bram Moolenaared8ce052020-04-29 21:04:15 +02001474
1475 int
1476current_par(
1477 oparg_T *oap,
1478 long count,
1479 int include, // TRUE == include white space
1480 int type) // 'p' for paragraph, 'S' for section
1481{
1482 linenr_T start_lnum;
1483 linenr_T end_lnum;
1484 int white_in_front;
1485 int dir;
1486 int start_is_white;
1487 int prev_start_is_white;
1488 int retval = OK;
1489 int do_white = FALSE;
1490 int t;
1491 int i;
1492
1493 if (type == 'S') // not implemented yet
1494 return FAIL;
1495
1496 start_lnum = curwin->w_cursor.lnum;
1497
1498 /*
1499 * When visual area is more than one line: extend it.
1500 */
1501 if (VIsual_active && start_lnum != VIsual.lnum)
1502 {
1503extend:
1504 if (start_lnum < VIsual.lnum)
1505 dir = BACKWARD;
1506 else
1507 dir = FORWARD;
1508 for (i = count; --i >= 0; )
1509 {
1510 if (start_lnum ==
1511 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
1512 {
1513 retval = FAIL;
1514 break;
1515 }
1516
1517 prev_start_is_white = -1;
1518 for (t = 0; t < 2; ++t)
1519 {
1520 start_lnum += dir;
1521 start_is_white = linewhite(start_lnum);
1522 if (prev_start_is_white == start_is_white)
1523 {
1524 start_lnum -= dir;
1525 break;
1526 }
1527 for (;;)
1528 {
1529 if (start_lnum == (dir == BACKWARD
1530 ? 1 : curbuf->b_ml.ml_line_count))
1531 break;
1532 if (start_is_white != linewhite(start_lnum + dir)
1533 || (!start_is_white
1534 && startPS(start_lnum + (dir > 0
1535 ? 1 : 0), 0, 0)))
1536 break;
1537 start_lnum += dir;
1538 }
1539 if (!include)
1540 break;
1541 if (start_lnum == (dir == BACKWARD
1542 ? 1 : curbuf->b_ml.ml_line_count))
1543 break;
1544 prev_start_is_white = start_is_white;
1545 }
1546 }
1547 curwin->w_cursor.lnum = start_lnum;
1548 curwin->w_cursor.col = 0;
1549 return retval;
1550 }
1551
1552 /*
1553 * First move back to the start_lnum of the paragraph or white lines
1554 */
1555 white_in_front = linewhite(start_lnum);
1556 while (start_lnum > 1)
1557 {
1558 if (white_in_front) // stop at first white line
1559 {
1560 if (!linewhite(start_lnum - 1))
1561 break;
1562 }
1563 else // stop at first non-white line of start of paragraph
1564 {
1565 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
1566 break;
1567 }
1568 --start_lnum;
1569 }
1570
1571 /*
1572 * Move past the end of any white lines.
1573 */
1574 end_lnum = start_lnum;
1575 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
1576 ++end_lnum;
1577
1578 --end_lnum;
1579 i = count;
1580 if (!include && white_in_front)
1581 --i;
1582 while (i--)
1583 {
1584 if (end_lnum == curbuf->b_ml.ml_line_count)
1585 return FAIL;
1586
1587 if (!include)
1588 do_white = linewhite(end_lnum + 1);
1589
1590 if (include || !do_white)
1591 {
1592 ++end_lnum;
1593 /*
1594 * skip to end of paragraph
1595 */
1596 while (end_lnum < curbuf->b_ml.ml_line_count
1597 && !linewhite(end_lnum + 1)
1598 && !startPS(end_lnum + 1, 0, 0))
1599 ++end_lnum;
1600 }
1601
1602 if (i == 0 && white_in_front && include)
1603 break;
1604
1605 /*
1606 * skip to end of white lines after paragraph
1607 */
1608 if (include || do_white)
1609 while (end_lnum < curbuf->b_ml.ml_line_count
1610 && linewhite(end_lnum + 1))
1611 ++end_lnum;
1612 }
1613
1614 /*
1615 * If there are no empty lines at the end, try to find some empty lines at
1616 * the start (unless that has been done already).
1617 */
1618 if (!white_in_front && !linewhite(end_lnum) && include)
1619 while (start_lnum > 1 && linewhite(start_lnum - 1))
1620 --start_lnum;
1621
1622 if (VIsual_active)
1623 {
1624 // Problem: when doing "Vipipip" nothing happens in a single white
1625 // line, we get stuck there. Trap this here.
1626 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
1627 goto extend;
1628 if (VIsual.lnum != start_lnum)
1629 {
1630 VIsual.lnum = start_lnum;
1631 VIsual.col = 0;
1632 }
1633 VIsual_mode = 'V';
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001634 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaared8ce052020-04-29 21:04:15 +02001635 showmode();
1636 }
1637 else
1638 {
1639 oap->start.lnum = start_lnum;
1640 oap->start.col = 0;
1641 oap->motion_type = MLINE;
1642 }
1643 curwin->w_cursor.lnum = end_lnum;
1644 curwin->w_cursor.col = 0;
1645
1646 return OK;
1647}
1648
1649/*
1650 * Search quote char from string line[col].
1651 * Quote character escaped by one of the characters in "escape" is not counted
1652 * as a quote.
1653 * Returns column number of "quotechar" or -1 when not found.
1654 */
1655 static int
1656find_next_quote(
1657 char_u *line,
1658 int col,
1659 int quotechar,
1660 char_u *escape) // escape characters, can be NULL
1661{
1662 int c;
1663
1664 for (;;)
1665 {
1666 c = line[col];
1667 if (c == NUL)
1668 return -1;
1669 else if (escape != NULL && vim_strchr(escape, c))
Bram Moolenaar53a70282022-05-09 13:15:07 +01001670 {
Bram Moolenaared8ce052020-04-29 21:04:15 +02001671 ++col;
Bram Moolenaar53a70282022-05-09 13:15:07 +01001672 if (line[col] == NUL)
1673 return -1;
1674 }
Bram Moolenaared8ce052020-04-29 21:04:15 +02001675 else if (c == quotechar)
1676 break;
1677 if (has_mbyte)
1678 col += (*mb_ptr2len)(line + col);
1679 else
1680 ++col;
1681 }
1682 return col;
1683}
1684
1685/*
1686 * Search backwards in "line" from column "col_start" to find "quotechar".
1687 * Quote character escaped by one of the characters in "escape" is not counted
1688 * as a quote.
1689 * Return the found column or zero.
1690 */
1691 static int
1692find_prev_quote(
1693 char_u *line,
1694 int col_start,
1695 int quotechar,
1696 char_u *escape) // escape characters, can be NULL
1697{
1698 int n;
1699
1700 while (col_start > 0)
1701 {
1702 --col_start;
1703 col_start -= (*mb_head_off)(line, line + col_start);
1704 n = 0;
1705 if (escape != NULL)
1706 while (col_start - n > 0 && vim_strchr(escape,
1707 line[col_start - n - 1]) != NULL)
1708 ++n;
1709 if (n & 1)
1710 col_start -= n; // uneven number of escape chars, skip it
1711 else if (line[col_start] == quotechar)
1712 break;
1713 }
1714 return col_start;
1715}
1716
1717/*
1718 * Find quote under the cursor, cursor at end.
1719 * Returns TRUE if found, else FALSE.
1720 */
1721 int
1722current_quote(
1723 oparg_T *oap,
1724 long count,
1725 int include, // TRUE == include quote char
1726 int quotechar) // Quote character
1727{
1728 char_u *line = ml_get_curline();
1729 int col_end;
1730 int col_start = curwin->w_cursor.col;
1731 int inclusive = FALSE;
1732 int vis_empty = TRUE; // Visual selection <= 1 char
1733 int vis_bef_curs = FALSE; // Visual starts before cursor
1734 int did_exclusive_adj = FALSE; // adjusted pos for 'selection'
1735 int inside_quotes = FALSE; // Looks like "i'" done before
1736 int selected_quote = FALSE; // Has quote inside selection
1737 int i;
1738 int restore_vis_bef = FALSE; // restore VIsual on abort
1739
1740 // When 'selection' is "exclusive" move the cursor to where it would be
1741 // with 'selection' "inclusive", so that the logic is the same for both.
1742 // The cursor then is moved forward after adjusting the area.
1743 if (VIsual_active)
1744 {
1745 // this only works within one line
1746 if (VIsual.lnum != curwin->w_cursor.lnum)
1747 return FALSE;
1748
1749 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
1750 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
1751 if (*p_sel == 'e')
1752 {
1753 if (vis_bef_curs)
1754 {
1755 dec_cursor();
1756 did_exclusive_adj = TRUE;
1757 }
1758 else if (!vis_empty)
1759 {
1760 dec(&VIsual);
1761 did_exclusive_adj = TRUE;
1762 }
1763 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
1764 if (!vis_bef_curs && !vis_empty)
1765 {
1766 // VIsual needs to be the start of Visual selection.
1767 pos_T t = curwin->w_cursor;
1768
1769 curwin->w_cursor = VIsual;
1770 VIsual = t;
1771 vis_bef_curs = TRUE;
1772 restore_vis_bef = TRUE;
1773 }
1774 }
1775 }
1776
1777 if (!vis_empty)
1778 {
1779 // Check if the existing selection exactly spans the text inside
1780 // quotes.
1781 if (vis_bef_curs)
1782 {
1783 inside_quotes = VIsual.col > 0
1784 && line[VIsual.col - 1] == quotechar
1785 && line[curwin->w_cursor.col] != NUL
1786 && line[curwin->w_cursor.col + 1] == quotechar;
1787 i = VIsual.col;
1788 col_end = curwin->w_cursor.col;
1789 }
1790 else
1791 {
1792 inside_quotes = curwin->w_cursor.col > 0
1793 && line[curwin->w_cursor.col - 1] == quotechar
1794 && line[VIsual.col] != NUL
1795 && line[VIsual.col + 1] == quotechar;
1796 i = curwin->w_cursor.col;
1797 col_end = VIsual.col;
1798 }
1799
1800 // Find out if we have a quote in the selection.
1801 while (i <= col_end)
Bram Moolenaar2f074f42022-06-18 11:22:40 +01001802 {
1803 // check for going over the end of the line, which can happen if
1804 // the line was changed after the Visual area was selected.
1805 if (line[i] == NUL)
1806 break;
Bram Moolenaared8ce052020-04-29 21:04:15 +02001807 if (line[i++] == quotechar)
1808 {
1809 selected_quote = TRUE;
1810 break;
1811 }
Bram Moolenaar2f074f42022-06-18 11:22:40 +01001812 }
Bram Moolenaared8ce052020-04-29 21:04:15 +02001813 }
1814
1815 if (!vis_empty && line[col_start] == quotechar)
1816 {
1817 // Already selecting something and on a quote character. Find the
1818 // next quoted string.
1819 if (vis_bef_curs)
1820 {
1821 // Assume we are on a closing quote: move to after the next
1822 // opening quote.
1823 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
1824 if (col_start < 0)
1825 goto abort_search;
1826 col_end = find_next_quote(line, col_start + 1, quotechar,
1827 curbuf->b_p_qe);
1828 if (col_end < 0)
1829 {
1830 // We were on a starting quote perhaps?
1831 col_end = col_start;
1832 col_start = curwin->w_cursor.col;
1833 }
1834 }
1835 else
1836 {
1837 col_end = find_prev_quote(line, col_start, quotechar, NULL);
1838 if (line[col_end] != quotechar)
1839 goto abort_search;
1840 col_start = find_prev_quote(line, col_end, quotechar,
1841 curbuf->b_p_qe);
1842 if (line[col_start] != quotechar)
1843 {
1844 // We were on an ending quote perhaps?
1845 col_start = col_end;
1846 col_end = curwin->w_cursor.col;
1847 }
1848 }
1849 }
1850 else
1851
1852 if (line[col_start] == quotechar || !vis_empty)
1853 {
1854 int first_col = col_start;
1855
1856 if (!vis_empty)
1857 {
1858 if (vis_bef_curs)
1859 first_col = find_next_quote(line, col_start, quotechar, NULL);
1860 else
1861 first_col = find_prev_quote(line, col_start, quotechar, NULL);
1862 }
1863
1864 // The cursor is on a quote, we don't know if it's the opening or
1865 // closing quote. Search from the start of the line to find out.
1866 // Also do this when there is a Visual area, a' may leave the cursor
1867 // in between two strings.
1868 col_start = 0;
1869 for (;;)
1870 {
1871 // Find open quote character.
1872 col_start = find_next_quote(line, col_start, quotechar, NULL);
1873 if (col_start < 0 || col_start > first_col)
1874 goto abort_search;
1875 // Find close quote character.
1876 col_end = find_next_quote(line, col_start + 1, quotechar,
1877 curbuf->b_p_qe);
1878 if (col_end < 0)
1879 goto abort_search;
1880 // If is cursor between start and end quote character, it is
1881 // target text object.
1882 if (col_start <= first_col && first_col <= col_end)
1883 break;
1884 col_start = col_end + 1;
1885 }
1886 }
1887 else
1888 {
1889 // Search backward for a starting quote.
1890 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
1891 if (line[col_start] != quotechar)
1892 {
1893 // No quote before the cursor, look after the cursor.
1894 col_start = find_next_quote(line, col_start, quotechar, NULL);
1895 if (col_start < 0)
1896 goto abort_search;
1897 }
1898
1899 // Find close quote character.
1900 col_end = find_next_quote(line, col_start + 1, quotechar,
1901 curbuf->b_p_qe);
1902 if (col_end < 0)
1903 goto abort_search;
1904 }
1905
1906 // When "include" is TRUE, include spaces after closing quote or before
1907 // the starting quote.
1908 if (include)
1909 {
1910 if (VIM_ISWHITE(line[col_end + 1]))
1911 while (VIM_ISWHITE(line[col_end + 1]))
1912 ++col_end;
1913 else
1914 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
1915 --col_start;
1916 }
1917
1918 // Set start position. After vi" another i" must include the ".
1919 // For v2i" include the quotes.
1920 if (!include && count < 2 && (vis_empty || !inside_quotes))
1921 ++col_start;
1922 curwin->w_cursor.col = col_start;
1923 if (VIsual_active)
1924 {
1925 // Set the start of the Visual area when the Visual area was empty, we
1926 // were just inside quotes or the Visual area didn't start at a quote
1927 // and didn't include a quote.
1928 if (vis_empty
1929 || (vis_bef_curs
1930 && !selected_quote
1931 && (inside_quotes
1932 || (line[VIsual.col] != quotechar
1933 && (VIsual.col == 0
1934 || line[VIsual.col - 1] != quotechar)))))
1935 {
1936 VIsual = curwin->w_cursor;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001937 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaared8ce052020-04-29 21:04:15 +02001938 }
1939 }
1940 else
1941 {
1942 oap->start = curwin->w_cursor;
1943 oap->motion_type = MCHAR;
1944 }
1945
1946 // Set end position.
1947 curwin->w_cursor.col = col_end;
1948 if ((include || count > 1 // After vi" another i" must include the ".
1949 || (!vis_empty && inside_quotes)
1950 ) && inc_cursor() == 2)
1951 inclusive = TRUE;
1952 if (VIsual_active)
1953 {
1954 if (vis_empty || vis_bef_curs)
1955 {
1956 // decrement cursor when 'selection' is not exclusive
1957 if (*p_sel != 'e')
1958 dec_cursor();
1959 }
1960 else
1961 {
1962 // Cursor is at start of Visual area. Set the end of the Visual
1963 // area when it was just inside quotes or it didn't end at a
1964 // quote.
1965 if (inside_quotes
1966 || (!selected_quote
1967 && line[VIsual.col] != quotechar
1968 && (line[VIsual.col] == NUL
1969 || line[VIsual.col + 1] != quotechar)))
1970 {
1971 dec_cursor();
1972 VIsual = curwin->w_cursor;
1973 }
1974 curwin->w_cursor.col = col_start;
1975 }
1976 if (VIsual_mode == 'V')
1977 {
1978 VIsual_mode = 'v';
1979 redraw_cmdline = TRUE; // show mode later
1980 }
1981 }
1982 else
1983 {
1984 // Set inclusive and other oap's flags.
1985 oap->inclusive = inclusive;
1986 }
1987
1988 return OK;
1989
1990abort_search:
1991 if (VIsual_active && *p_sel == 'e')
1992 {
1993 if (did_exclusive_adj)
1994 inc_cursor();
1995 if (restore_vis_bef)
1996 {
1997 pos_T t = curwin->w_cursor;
1998
1999 curwin->w_cursor = VIsual;
2000 VIsual = t;
2001 }
2002 }
2003 return FALSE;
2004}