Skip to content

Commit c3c3158

Browse files
committed
patch 8.1.0728: cannot avoid breaking after a single space.
Problem: Cannot avoid breaking after a single space. Solution: Add the 'p' flag to 'formatoptions'. (Tom Ryder)
1 parent 44a7db4 commit c3c3158

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

runtime/doc/change.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,17 @@ j Where it makes sense, remove a comment leader when joining lines. For
17201720
// in the list ~
17211721
Becomes:
17221722
int i; // the index in the list ~
1723+
p Don't break lines at single spaces that follow periods. This is
1724+
intended to complement 'joinspaces' and |cpo-J|, for prose with
1725+
sentences separated by two spaces. For example, with 'textwidth' set
1726+
to 28: >
1727+
Surely you're joking, Mr. Feynman!
1728+
< Becomes: >
1729+
Surely you're joking,
1730+
Mr. Feynman!
1731+
< Instead of: >
1732+
Surely you're joking, Mr.
1733+
Feynman!
17231734
17241735
17251736
With 't' and 'c' you can specify when Vim performs auto-wrapping:

src/edit.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6498,6 +6498,7 @@ internal_format(
64986498
char_u *saved_text = NULL;
64996499
colnr_T col;
65006500
colnr_T end_col;
6501+
int wcc; // counter for whitespace chars
65016502

65026503
virtcol = get_nolist_virtcol()
65036504
+ char2cells(c != NUL ? c : gchar_cursor());
@@ -6559,14 +6560,26 @@ internal_format(
65596560
/* remember position of blank just before text */
65606561
end_col = curwin->w_cursor.col;
65616562

6562-
/* find start of sequence of blanks */
6563+
// find start of sequence of blanks
6564+
wcc = 0;
65636565
while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
65646566
{
65656567
dec_cursor();
65666568
cc = gchar_cursor();
6569+
6570+
// Increment count of how many whitespace chars in this
6571+
// group; we only need to know if it's more than one.
6572+
if (wcc < 2)
6573+
wcc++;
65676574
}
65686575
if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
65696576
break; /* only spaces in front of text */
6577+
6578+
// Don't break after a period when 'formatoptions' has 'p' and
6579+
// there are less than two spaces.
6580+
if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
6581+
continue;
6582+
65706583
#ifdef FEAT_COMMENTS
65716584
/* Don't break until after the comment leader */
65726585
if (curwin->w_cursor.col < leader_len)

src/option.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@
101101
#define FO_WHITE_PAR 'w' /* trailing white space continues paragr. */
102102
#define FO_AUTO 'a' /* automatic formatting */
103103
#define FO_REMOVE_COMS 'j' /* remove comment leaders when joining lines */
104+
#define FO_PERIOD_ABBR 'p' /* don't break a single space after a period */
104105

105106
#define DFLT_FO_VI "vt"
106107
#define DFLT_FO_VIM "tcq"
107-
#define FO_ALL "tcroq2vlb1mMBn,awj" /* for do_set() */
108+
#define FO_ALL "tcroq2vlb1mMBn,awjp" /* for do_set() */
108109

109110
/* characters for the p_cpo option: */
110111
#define CPO_ALTREAD 'a' /* ":read" sets alternate file name */

src/testdir/test_textformat.vim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,32 @@ func Test_text_format()
163163
\ '# 1 xxxxx',
164164
\ '# foobar'], getline(1, 2))
165165

166+
" Test the 'p' flag for 'formatoptions'
167+
" First test without the flag: that it will break "Mr. Feynman" at the space
168+
normal ggdG
169+
setl tw=28 fo=tcq
170+
call setline('.', 'Surely you''re joking, Mr. Feynman!')
171+
normal gqq
172+
call assert_equal([
173+
\ 'Surely you''re joking, Mr.',
174+
\ 'Feynman!'], getline(1, 2))
175+
" Now test with the flag: that it will push the name with the title onto the
176+
" next line
177+
normal ggdG
178+
setl fo+=p
179+
call setline('.', 'Surely you''re joking, Mr. Feynman!')
180+
normal gqq
181+
call assert_equal([
182+
\ 'Surely you''re joking,',
183+
\ 'Mr. Feynman!'], getline(1, 2))
184+
" Ensure that it will still break if two spaces are entered
185+
normal ggdG
186+
call setline('.', 'Surely you''re joking, Mr. Feynman!')
187+
normal gqq
188+
call assert_equal([
189+
\ 'Surely you''re joking, Mr.',
190+
\ 'Feynman!'], getline(1, 2))
191+
166192
setl ai& tw& fo& si& comments&
167193
enew!
168194
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ static char *(features[]) =
795795

796796
static int included_patches[] =
797797
{ /* Add new patch number below this line */
798+
/**/
799+
728,
798800
/**/
799801
727,
800802
/**/

0 commit comments

Comments
 (0)