Skip to content

Commit 8134039

Browse files
committed
updated for version 7.3.541
Problem: When joining lines comment leaders need to be removed manually. Solution: Add the 'j' flag to 'formatoptions'. (Lech Lorens)
1 parent bc256d9 commit 8134039

File tree

13 files changed

+482
-27
lines changed

13 files changed

+482
-27
lines changed

runtime/doc/change.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,12 @@ B When joining lines, don't insert a space between two multi-byte
15221522
characters. Overruled by the 'M' flag.
15231523
1 Don't break a line after a one-letter word. It's broken before it
15241524
instead (if possible).
1525+
j Where it makes sense, remove a comment leader when joining lines. For
1526+
example, joining:
1527+
int i; // the index ~
1528+
// in the list ~
1529+
Becomes:
1530+
int i; // the index in the list ~
15251531

15261532

15271533
With 't' and 'c' you can specify when Vim performs auto-wrapping:

src/edit.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5847,7 +5847,7 @@ insertchar(c, flags, second_indent)
58475847
* Need to remove existing (middle) comment leader and insert end
58485848
* comment leader. First, check what comment leader we can find.
58495849
*/
5850-
i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5850+
i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
58515851
if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
58525852
{
58535853
/* Skip middle-comment string */
@@ -6085,7 +6085,7 @@ internal_format(textwidth, second_indent, flags, format_only, c)
60856085

60866086
/* Don't break until after the comment leader */
60876087
if (do_comments)
6088-
leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
6088+
leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
60896089
else
60906090
leader_len = 0;
60916091

@@ -6411,7 +6411,7 @@ auto_format(trailblank, prev_line)
64116411
/* With the 'c' flag in 'formatoptions' and 't' missing: only format
64126412
* comments. */
64136413
if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6414-
&& get_leader_len(old, NULL, FALSE) == 0)
6414+
&& get_leader_len(old, NULL, FALSE, TRUE) == 0)
64156415
return;
64166416
#endif
64176417

@@ -8565,7 +8565,7 @@ ins_del()
85658565
{
85668566
temp = curwin->w_cursor.col;
85678567
if (!can_bs(BS_EOL) /* only if "eol" included */
8568-
|| do_join(2, FALSE, TRUE) == FAIL)
8568+
|| do_join(2, FALSE, TRUE, FALSE) == FAIL)
85698569
vim_beep();
85708570
else
85718571
curwin->w_cursor.col = temp;
@@ -8746,7 +8746,7 @@ ins_bs(c, mode, inserted_space_p)
87468746
ptr[len - 1] = NUL;
87478747
}
87488748

8749-
(void)do_join(2, FALSE, FALSE);
8749+
(void)do_join(2, FALSE, FALSE, FALSE);
87508750
if (temp == NUL && gchar_cursor() != NUL)
87518751
inc_cursor();
87528752
}

src/ex_docmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8545,7 +8545,7 @@ ex_join(eap)
85458545
}
85468546
++eap->line2;
85478547
}
8548-
(void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
8548+
(void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
85498549
beginline(BL_WHITE | BL_FIX);
85508550
ex_may_print(eap);
85518551
}

src/misc1.c

Lines changed: 160 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ open_line(dir, flags, old_indent)
671671
ptr = saved_line;
672672
# ifdef FEAT_COMMENTS
673673
if (flags & OPENLINE_DO_COM)
674-
lead_len = get_leader_len(ptr, NULL, FALSE);
674+
lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
675675
else
676676
lead_len = 0;
677677
# endif
@@ -693,7 +693,7 @@ open_line(dir, flags, old_indent)
693693
}
694694
# ifdef FEAT_COMMENTS
695695
if (flags & OPENLINE_DO_COM)
696-
lead_len = get_leader_len(ptr, NULL, FALSE);
696+
lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
697697
else
698698
lead_len = 0;
699699
if (lead_len > 0)
@@ -836,7 +836,7 @@ open_line(dir, flags, old_indent)
836836
*/
837837
end_comment_pending = NUL;
838838
if (flags & OPENLINE_DO_COM)
839-
lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD);
839+
lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
840840
else
841841
lead_len = 0;
842842
if (lead_len > 0)
@@ -1548,14 +1548,18 @@ open_line(dir, flags, old_indent)
15481548
* When "flags" is not NULL, it is set to point to the flags of the recognized
15491549
* comment leader.
15501550
* "backward" must be true for the "O" command.
1551+
* If "include_space" is set, include trailing whitespace while calculating the
1552+
* length.
15511553
*/
15521554
int
1553-
get_leader_len(line, flags, backward)
1555+
get_leader_len(line, flags, backward, include_space)
15541556
char_u *line;
15551557
char_u **flags;
15561558
int backward;
1559+
int include_space;
15571560
{
15581561
int i, j;
1562+
int result;
15591563
int got_com = FALSE;
15601564
int found_one;
15611565
char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
@@ -1565,7 +1569,7 @@ get_leader_len(line, flags, backward)
15651569
char_u *prev_list;
15661570
char_u *saved_flags = NULL;
15671571

1568-
i = 0;
1572+
result = i = 0;
15691573
while (vim_iswhite(line[i])) /* leading white space is ignored */
15701574
++i;
15711575

@@ -1668,17 +1672,167 @@ get_leader_len(line, flags, backward)
16681672
if (!found_one)
16691673
break;
16701674

1675+
result = i;
1676+
16711677
/* Include any trailing white space. */
16721678
while (vim_iswhite(line[i]))
16731679
++i;
16741680

1681+
if (include_space)
1682+
result = i;
1683+
16751684
/* If this comment doesn't nest, stop here. */
16761685
got_com = TRUE;
16771686
if (vim_strchr(part_buf, COM_NEST) == NULL)
16781687
break;
16791688
}
1689+
return result;
1690+
}
1691+
1692+
/*
1693+
* Return the offset at which the last comment in line starts. If there is no
1694+
* comment in the whole line, -1 is returned.
1695+
*
1696+
* When "flags" is not null, it is set to point to the flags describing the
1697+
* recognized comment leader.
1698+
*/
1699+
int
1700+
get_last_leader_offset(line, flags)
1701+
char_u *line;
1702+
char_u **flags;
1703+
{
1704+
int result = -1;
1705+
int i, j;
1706+
int lower_check_bound = 0;
1707+
char_u *string;
1708+
char_u *com_leader;
1709+
char_u *com_flags;
1710+
char_u *list;
1711+
int found_one;
1712+
char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1713+
1714+
/*
1715+
* Repeat to match several nested comment strings.
1716+
*/
1717+
i = (int)STRLEN(line);
1718+
while (--i >= lower_check_bound)
1719+
{
1720+
/*
1721+
* scan through the 'comments' option for a match
1722+
*/
1723+
found_one = FALSE;
1724+
for (list = curbuf->b_p_com; *list; )
1725+
{
1726+
char_u *flags_save = list;
1727+
1728+
/*
1729+
* Get one option part into part_buf[]. Advance list to next one.
1730+
* put string at start of string.
1731+
*/
1732+
(void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1733+
string = vim_strchr(part_buf, ':');
1734+
if (string == NULL) /* If everything is fine, this cannot actually
1735+
* happen. */
1736+
{
1737+
continue;
1738+
}
1739+
*string++ = NUL; /* Isolate flags from string. */
1740+
com_leader = string;
16801741

1681-
return (got_com ? i : 0);
1742+
/*
1743+
* Line contents and string must match.
1744+
* When string starts with white space, must have some white space
1745+
* (but the amount does not need to match, there might be a mix of
1746+
* TABs and spaces).
1747+
*/
1748+
if (vim_iswhite(string[0]))
1749+
{
1750+
if (i == 0 || !vim_iswhite(line[i - 1]))
1751+
continue;
1752+
while (vim_iswhite(string[0]))
1753+
++string;
1754+
}
1755+
for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1756+
/* do nothing */;
1757+
if (string[j] != NUL)
1758+
continue;
1759+
1760+
/*
1761+
* When 'b' flag used, there must be white space or an
1762+
* end-of-line after the string in the line.
1763+
*/
1764+
if (vim_strchr(part_buf, COM_BLANK) != NULL
1765+
&& !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1766+
{
1767+
continue;
1768+
}
1769+
1770+
/*
1771+
* We have found a match, stop searching.
1772+
*/
1773+
found_one = TRUE;
1774+
1775+
if (flags)
1776+
*flags = flags_save;
1777+
com_flags = flags_save;
1778+
1779+
break;
1780+
}
1781+
1782+
if (found_one)
1783+
{
1784+
char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1785+
int len1, len2, off;
1786+
1787+
result = i;
1788+
/*
1789+
* If this comment nests, continue searching.
1790+
*/
1791+
if (vim_strchr(part_buf, COM_NEST) != NULL)
1792+
continue;
1793+
1794+
lower_check_bound = i;
1795+
1796+
/* Let's verify whether the comment leader found is a substring
1797+
* of other comment leaders. If it is, let's adjust the
1798+
* lower_check_bound so that we make sure that we have determined
1799+
* the comment leader correctly.
1800+
*/
1801+
1802+
while (vim_iswhite(*com_leader))
1803+
++com_leader;
1804+
len1 = (int)STRLEN(com_leader);
1805+
1806+
for (list = curbuf->b_p_com; *list; )
1807+
{
1808+
char_u *flags_save = list;
1809+
1810+
(void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1811+
if (flags_save == com_flags)
1812+
continue;
1813+
string = vim_strchr(part_buf2, ':');
1814+
++string;
1815+
while (vim_iswhite(*string))
1816+
++string;
1817+
len2 = (int)STRLEN(string);
1818+
if (len2 == 0)
1819+
continue;
1820+
1821+
/* Now we have to verify whether string ends with a substring
1822+
* beginning the com_leader. */
1823+
for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1824+
{
1825+
--off;
1826+
if (!STRNCMP(string + off, com_leader, len2 - off))
1827+
{
1828+
if (i - off < lower_check_bound)
1829+
lower_check_bound = i - off;
1830+
}
1831+
}
1832+
}
1833+
}
1834+
}
1835+
return result;
16821836
}
16831837
#endif
16841838

src/normal.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,7 @@ do_pending_operator(cap, old_col, gui_yank)
19681968
beep_flush();
19691969
else
19701970
{
1971-
(void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE);
1971+
(void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE, TRUE);
19721972
auto_format(FALSE, TRUE);
19731973
}
19741974
break;
@@ -4426,7 +4426,7 @@ find_decl(ptr, len, locally, thisblock, searchflags)
44264426
break;
44274427
}
44284428
#ifdef FEAT_COMMENTS
4429-
if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4429+
if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
44304430
{
44314431
/* Ignore this line, continue at start of next line. */
44324432
++curwin->w_cursor.lnum;
@@ -9324,7 +9324,7 @@ nv_join(cap)
93249324
{
93259325
prep_redo(cap->oap->regname, cap->count0,
93269326
NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9327-
(void)do_join(cap->count0, cap->nchar == NUL, TRUE);
9327+
(void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
93289328
}
93299329
}
93309330
}

0 commit comments

Comments
 (0)