According to “:help 'formatexpr'”,
The expression is also evaluated when 'textwidth' is set and adding
text beyond that limit.
However, even when 'formatoptions' doesn't contain "a", one can see that
a set 'formatexpr' is called every time a character is typed, regardless
of whether 'textwidth' is exceeded. In fact, most of the (rather few)
uses of 'formatexpr' which explicitly handle insert-mode reformatting
have added the check that the help claims to already be doing.
In order to reduce the amount of boilerplate in scripts and conform
better to the help, the attached patch performs this check for the user.
Note, 'formatexpr' will still be called if the cursor is < 'textwidth'
but other conditions necessitate formatting (like the "a" formatoption).
Cheers,
--
James
GPG Key: 4096R/331BA3DB 2011-12-05 James McCoy <[email protected]>
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/src/edit.c b/src/edit.c
index 2e5d317..6d7bce0 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -5874,8 +5874,9 @@ insertchar(c, flags, second_indent)
char_u *p;
#endif
int fo_ins_blank;
+ int force_format = flags & INSCHAR_FORMAT;
- textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
+ textwidth = comp_textwidth(force_format);
fo_ins_blank = has_format_option(FO_INS_BLANK);
/*
@@ -5894,7 +5895,7 @@ insertchar(c, flags, second_indent)
* before 'textwidth'
*/
if (textwidth > 0
- && ((flags & INSCHAR_FORMAT)
+ && (force_format
|| (!vim_iswhite(c)
&& !((State & REPLACE_FLAG)
#ifdef FEAT_VREPLACE
@@ -5912,8 +5913,11 @@ insertchar(c, flags, second_indent)
* when 'formatexpr' isn't set or it returns non-zero. */
#if defined(FEAT_EVAL)
int do_internal = TRUE;
+ colnr_T virtcol = get_nolist_virtcol()
+ + char2cells(c != NUL ? c : gchar_cursor());
- if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
+ if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
+ && (force_format || virtcol > (colnr_T)textwidth))
{
do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
/* It may be required to save for undo again, e.g. when setline()