summaryrefslogtreecommitdiffstats
path: root/src/textformat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/textformat.c')
-rw-r--r--src/textformat.c46
1 files changed, 31 insertions, 15 deletions
diff --git a/src/textformat.c b/src/textformat.c
index a1a3e16..14acc53 100644
--- a/src/textformat.c
+++ b/src/textformat.c
@@ -90,11 +90,18 @@ internal_format(
colnr_T end_col;
int wcc; // counter for whitespace chars
int did_do_comment = FALSE;
+ int first_pass;
- virtcol = get_nolist_virtcol()
- + char2cells(c != NUL ? c : gchar_cursor());
- if (virtcol <= (colnr_T)textwidth)
- break;
+ // Cursor is currently at the end of line. No need to format
+ // if line length is less than textwidth (8 * textwidth for
+ // utf safety)
+ if (curwin->w_cursor.col < 8 * textwidth)
+ {
+ virtcol = get_nolist_virtcol()
+ + char2cells(c != NUL ? c : gchar_cursor());
+ if (virtcol <= (colnr_T)textwidth)
+ break;
+ }
if (no_leader)
do_comments = FALSE;
@@ -144,9 +151,17 @@ internal_format(
coladvance((colnr_T)textwidth);
wantcol = curwin->w_cursor.col;
- curwin->w_cursor.col = startcol;
+ // If startcol is large (a long line), formatting takes too much
+ // time. The algorithm is O(n^2), it walks from the end of the
+ // line to textwidth border every time for each line break.
+ //
+ // Ceil to 8 * textwidth to optimize.
+ curwin->w_cursor.col = startcol < 8 * textwidth ? startcol :
+ 8 * textwidth;
+
foundcol = 0;
skip_pos = 0;
+ first_pass = TRUE;
// Find position to break at.
// Stop at first entered white when 'formatoptions' has 'v'
@@ -155,8 +170,11 @@ internal_format(
|| curwin->w_cursor.lnum != Insstart.lnum
|| curwin->w_cursor.col >= Insstart.col)
{
- if (curwin->w_cursor.col == startcol && c != NUL)
+ if (first_pass && c != NUL)
+ {
cc = c;
+ first_pass = FALSE;
+ }
else
cc = gchar_cursor();
if (WHITECHAR(cc))
@@ -437,7 +455,7 @@ internal_format(
// Check if cursor is not past the NUL off the line, cindent
// may have added or removed indent.
curwin->w_cursor.col += startcol;
- len = (colnr_T)STRLEN(ml_get_curline());
+ len = ml_get_curline_len();
if (curwin->w_cursor.col > len)
curwin->w_cursor.col = len;
}
@@ -513,9 +531,7 @@ ends_in_white(linenr_T lnum)
if (*s == NUL)
return FALSE;
- // Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
- // invocation may call function multiple times".
- l = STRLEN(s) - 1;
+ l = ml_get_len(lnum) - 1;
return VIM_ISWHITE(s[l]);
}
@@ -555,7 +571,7 @@ same_leader(
return FALSE;
if (*p == COM_START)
{
- int line_len = (int)STRLEN(ml_get(lnum));
+ int line_len = ml_get_len(lnum);
if (line_len <= leader1_len)
return FALSE;
if (leader2_flags == NULL || leader2_len == 0)
@@ -666,7 +682,7 @@ auto_format(
// in 'formatoptions' and there is a single character before the cursor.
// Otherwise the line would be broken and when typing another non-white
// next they are not joined back together.
- wasatend = (pos.col == (colnr_T)STRLEN(old));
+ wasatend = (pos.col == ml_get_curline_len());
if (*old != NUL && !trailblank && wasatend)
{
dec_cursor();
@@ -722,7 +738,7 @@ auto_format(
if (!wasatend && has_format_option(FO_WHITE_PAR))
{
new = ml_get_curline();
- len = (colnr_T)STRLEN(new);
+ len = ml_get_curline_len();
if (curwin->w_cursor.col == len)
{
pnew = vim_strnsave(new, len + 2);
@@ -795,7 +811,7 @@ comp_textwidth(
// The width is the window width minus 'wrapmargin' minus all the
// things that add to the margin.
textwidth = curwin->w_width - curbuf->b_p_wm;
- if (cmdwin_type != 0)
+ if (curbuf == cmdwin_buf)
textwidth -= 1;
#ifdef FEAT_FOLDING
textwidth -= curwin->w_p_fdc;
@@ -1199,7 +1215,7 @@ format_lines(
}
first_par_line = FALSE;
// If the line is getting long, format it next time
- if (STRLEN(ml_get_curline()) > (size_t)max_len)
+ if (ml_get_curline_len() > max_len)
force_format = TRUE;
else
force_format = FALSE;