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