Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
brammool committed Jun 6, 2012
1 parent bc256d9 commit 8134039
Show file tree
Hide file tree
Showing 13 changed files with 482 additions and 27 deletions.
6 changes: 6 additions & 0 deletions runtime/doc/change.txt
Expand Up @@ -1522,6 +1522,12 @@ B When joining lines, don't insert a space between two multi-byte
characters. Overruled by the 'M' flag.
1 Don't break a line after a one-letter word. It's broken before it
instead (if possible).
j Where it makes sense, remove a comment leader when joining lines. For
example, joining:
int i; // the index ~
// in the list ~
Becomes:
int i; // the index in the list ~


With 't' and 'c' you can specify when Vim performs auto-wrapping:
Expand Down
10 changes: 5 additions & 5 deletions src/edit.c
Expand Up @@ -5847,7 +5847,7 @@ insertchar(c, flags, second_indent)
* Need to remove existing (middle) comment leader and insert end
* comment leader. First, check what comment leader we can find.
*/
i = get_leader_len(line = ml_get_curline(), &p, FALSE);
i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
{
/* Skip middle-comment string */
Expand Down Expand Up @@ -6085,7 +6085,7 @@ internal_format(textwidth, second_indent, flags, format_only, c)

/* Don't break until after the comment leader */
if (do_comments)
leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
else
leader_len = 0;

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

Expand Down Expand Up @@ -8565,7 +8565,7 @@ ins_del()
{
temp = curwin->w_cursor.col;
if (!can_bs(BS_EOL) /* only if "eol" included */
|| do_join(2, FALSE, TRUE) == FAIL)
|| do_join(2, FALSE, TRUE, FALSE) == FAIL)
vim_beep();
else
curwin->w_cursor.col = temp;
Expand Down Expand Up @@ -8746,7 +8746,7 @@ ins_bs(c, mode, inserted_space_p)
ptr[len - 1] = NUL;
}

(void)do_join(2, FALSE, FALSE);
(void)do_join(2, FALSE, FALSE, FALSE);
if (temp == NUL && gchar_cursor() != NUL)
inc_cursor();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ex_docmd.c
Expand Up @@ -8545,7 +8545,7 @@ ex_join(eap)
}
++eap->line2;
}
(void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
(void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
beginline(BL_WHITE | BL_FIX);
ex_may_print(eap);
}
Expand Down
166 changes: 160 additions & 6 deletions src/misc1.c
Expand Up @@ -671,7 +671,7 @@ open_line(dir, flags, old_indent)
ptr = saved_line;
# ifdef FEAT_COMMENTS
if (flags & OPENLINE_DO_COM)
lead_len = get_leader_len(ptr, NULL, FALSE);
lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
else
lead_len = 0;
# endif
Expand All @@ -693,7 +693,7 @@ open_line(dir, flags, old_indent)
}
# ifdef FEAT_COMMENTS
if (flags & OPENLINE_DO_COM)
lead_len = get_leader_len(ptr, NULL, FALSE);
lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
else
lead_len = 0;
if (lead_len > 0)
Expand Down Expand Up @@ -836,7 +836,7 @@ open_line(dir, flags, old_indent)
*/
end_comment_pending = NUL;
if (flags & OPENLINE_DO_COM)
lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD);
lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
else
lead_len = 0;
if (lead_len > 0)
Expand Down Expand Up @@ -1548,14 +1548,18 @@ open_line(dir, flags, old_indent)
* When "flags" is not NULL, it is set to point to the flags of the recognized
* comment leader.
* "backward" must be true for the "O" command.
* If "include_space" is set, include trailing whitespace while calculating the
* length.
*/
int
get_leader_len(line, flags, backward)
get_leader_len(line, flags, backward, include_space)
char_u *line;
char_u **flags;
int backward;
int include_space;
{
int i, j;
int result;
int got_com = FALSE;
int found_one;
char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
Expand All @@ -1565,7 +1569,7 @@ get_leader_len(line, flags, backward)
char_u *prev_list;
char_u *saved_flags = NULL;

i = 0;
result = i = 0;
while (vim_iswhite(line[i])) /* leading white space is ignored */
++i;

Expand Down Expand Up @@ -1668,17 +1672,167 @@ get_leader_len(line, flags, backward)
if (!found_one)
break;

result = i;

/* Include any trailing white space. */
while (vim_iswhite(line[i]))
++i;

if (include_space)
result = i;

/* If this comment doesn't nest, stop here. */
got_com = TRUE;
if (vim_strchr(part_buf, COM_NEST) == NULL)
break;
}
return result;
}

/*
* Return the offset at which the last comment in line starts. If there is no
* comment in the whole line, -1 is returned.
*
* When "flags" is not null, it is set to point to the flags describing the
* recognized comment leader.
*/
int
get_last_leader_offset(line, flags)
char_u *line;
char_u **flags;
{
int result = -1;
int i, j;
int lower_check_bound = 0;
char_u *string;
char_u *com_leader;
char_u *com_flags;
char_u *list;
int found_one;
char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */

/*
* Repeat to match several nested comment strings.
*/
i = (int)STRLEN(line);
while (--i >= lower_check_bound)
{
/*
* scan through the 'comments' option for a match
*/
found_one = FALSE;
for (list = curbuf->b_p_com; *list; )
{
char_u *flags_save = list;

/*
* Get one option part into part_buf[]. Advance list to next one.
* put string at start of string.
*/
(void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
string = vim_strchr(part_buf, ':');
if (string == NULL) /* If everything is fine, this cannot actually
* happen. */
{
continue;
}
*string++ = NUL; /* Isolate flags from string. */
com_leader = string;

return (got_com ? i : 0);
/*
* Line contents and string must match.
* When string starts with white space, must have some white space
* (but the amount does not need to match, there might be a mix of
* TABs and spaces).
*/
if (vim_iswhite(string[0]))
{
if (i == 0 || !vim_iswhite(line[i - 1]))
continue;
while (vim_iswhite(string[0]))
++string;
}
for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
/* do nothing */;
if (string[j] != NUL)
continue;

/*
* When 'b' flag used, there must be white space or an
* end-of-line after the string in the line.
*/
if (vim_strchr(part_buf, COM_BLANK) != NULL
&& !vim_iswhite(line[i + j]) && line[i + j] != NUL)
{
continue;
}

/*
* We have found a match, stop searching.
*/
found_one = TRUE;

if (flags)
*flags = flags_save;
com_flags = flags_save;

break;
}

if (found_one)
{
char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
int len1, len2, off;

result = i;
/*
* If this comment nests, continue searching.
*/
if (vim_strchr(part_buf, COM_NEST) != NULL)
continue;

lower_check_bound = i;

/* Let's verify whether the comment leader found is a substring
* of other comment leaders. If it is, let's adjust the
* lower_check_bound so that we make sure that we have determined
* the comment leader correctly.
*/

while (vim_iswhite(*com_leader))
++com_leader;
len1 = (int)STRLEN(com_leader);

for (list = curbuf->b_p_com; *list; )
{
char_u *flags_save = list;

(void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
if (flags_save == com_flags)
continue;
string = vim_strchr(part_buf2, ':');
++string;
while (vim_iswhite(*string))
++string;
len2 = (int)STRLEN(string);
if (len2 == 0)
continue;

/* Now we have to verify whether string ends with a substring
* beginning the com_leader. */
for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
{
--off;
if (!STRNCMP(string + off, com_leader, len2 - off))
{
if (i - off < lower_check_bound)
lower_check_bound = i - off;
}
}
}
}
}
return result;
}
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/normal.c
Expand Up @@ -1968,7 +1968,7 @@ do_pending_operator(cap, old_col, gui_yank)
beep_flush();
else
{
(void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE);
(void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE, TRUE);
auto_format(FALSE, TRUE);
}
break;
Expand Down Expand Up @@ -4426,7 +4426,7 @@ find_decl(ptr, len, locally, thisblock, searchflags)
break;
}
#ifdef FEAT_COMMENTS
if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
{
/* Ignore this line, continue at start of next line. */
++curwin->w_cursor.lnum;
Expand Down Expand Up @@ -9324,7 +9324,7 @@ nv_join(cap)
{
prep_redo(cap->oap->regname, cap->count0,
NUL, cap->cmdchar, NUL, NUL, cap->nchar);
(void)do_join(cap->count0, cap->nchar == NUL, TRUE);
(void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
}
}
}
Expand Down

0 comments on commit 8134039

Please sign in to comment.