Skip to content

Commit 9be7c04

Browse files
committed
patch 8.0.0179: cannot have a local value for 'formatprg'
Problem: 'formatprg' is a global option but the value may depend on the type of buffer. (Sung Pae) Solution: Make 'formatprg' global-local. (closes #1380)
1 parent 9b73c4a commit 9be7c04

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

runtime/doc/options.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3417,7 +3417,7 @@ A jump table for the options with a short description can be found at |Q_op|.
34173417

34183418
*'formatprg'* *'fp'*
34193419
'formatprg' 'fp' string (default "")
3420-
global
3420+
global or local to buffer |global-local|
34213421
{not in Vi}
34223422
The name of an external program that will be used to format the lines
34233423
selected with the |gq| operator. The program must take the input on

src/normal.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,7 @@ do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
19841984
op_formatexpr(oap); /* use expression */
19851985
else
19861986
#endif
1987-
if (*p_fp != NUL)
1987+
if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
19881988
op_colon(oap); /* use external command */
19891989
else
19901990
op_format(oap, FALSE); /* use internal function */
@@ -2197,10 +2197,12 @@ op_colon(oparg_T *oap)
21972197
}
21982198
else if (oap->op_type == OP_FORMAT)
21992199
{
2200-
if (*p_fp == NUL)
2201-
stuffReadbuff((char_u *)"fmt");
2202-
else
2200+
if (*curbuf->b_p_fp != NUL)
2201+
stuffReadbuff(curbuf->b_p_fp);
2202+
else if (*p_fp != NUL)
22032203
stuffReadbuff(p_fp);
2204+
else
2205+
stuffReadbuff((char_u *)"fmt");
22042206
stuffReadbuff((char_u *)"\n']");
22052207
}
22062208

src/option.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
108108
# define PV_BEXPR OPT_BOTH(OPT_BUF(BV_BEXPR))
109109
#endif
110+
#define PV_FP OPT_BOTH(OPT_BUF(BV_FP))
110111
#ifdef FEAT_EVAL
111112
# define PV_FEX OPT_BUF(BV_FEX)
112113
#endif
@@ -1258,7 +1259,7 @@ static struct vimoption options[] =
12581259
{(char_u *)"^\\s*\\d\\+[\\]:.)}\\t ]\\s*",
12591260
(char_u *)0L} SCRIPTID_INIT},
12601261
{"formatprg", "fp", P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
1261-
(char_u *)&p_fp, PV_NONE,
1262+
(char_u *)&p_fp, PV_FP,
12621263
{(char_u *)"", (char_u *)0L} SCRIPTID_INIT},
12631264
{"fsync", "fs", P_BOOL|P_SECURE|P_VI_DEF,
12641265
#ifdef HAVE_FSYNC
@@ -5481,6 +5482,7 @@ check_buf_options(buf_T *buf)
54815482
#if defined(FEAT_CRYPT)
54825483
check_string_option(&buf->b_p_cm);
54835484
#endif
5485+
check_string_option(&buf->b_p_fp);
54845486
#if defined(FEAT_EVAL)
54855487
check_string_option(&buf->b_p_fex);
54865488
#endif
@@ -10175,6 +10177,9 @@ unset_global_local_option(char_u *name, void *from)
1017510177
clear_string_option(&buf->b_p_tsr);
1017610178
break;
1017710179
#endif
10180+
case PV_FP:
10181+
clear_string_option(&buf->b_p_fp);
10182+
break;
1017810183
#ifdef FEAT_QUICKFIX
1017910184
case PV_EFM:
1018010185
clear_string_option(&buf->b_p_efm);
@@ -10228,6 +10233,7 @@ get_varp_scope(struct vimoption *p, int opt_flags)
1022810233
{
1022910234
switch ((int)p->indir)
1023010235
{
10236+
case PV_FP: return (char_u *)&(curbuf->b_p_fp);
1023110237
#ifdef FEAT_QUICKFIX
1023210238
case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
1023310239
case PV_GP: return (char_u *)&(curbuf->b_p_gp);
@@ -10308,6 +10314,8 @@ get_varp(struct vimoption *p)
1030810314
case PV_TSR: return *curbuf->b_p_tsr != NUL
1030910315
? (char_u *)&(curbuf->b_p_tsr) : p->var;
1031010316
#endif
10317+
case PV_FP: return *curbuf->b_p_fp != NUL
10318+
? (char_u *)&(curbuf->b_p_fp) : p->var;
1031110319
#ifdef FEAT_QUICKFIX
1031210320
case PV_EFM: return *curbuf->b_p_efm != NUL
1031310321
? (char_u *)&(curbuf->b_p_efm) : p->var;
@@ -10873,6 +10881,7 @@ buf_copy_options(buf_T *buf, int flags)
1087310881
buf->b_p_inde = vim_strsave(p_inde);
1087410882
buf->b_p_indk = vim_strsave(p_indk);
1087510883
#endif
10884+
buf->b_p_fp = empty_option;
1087610885
#if defined(FEAT_EVAL)
1087710886
buf->b_p_fex = vim_strsave(p_fex);
1087810887
#endif

src/option.h

+1
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ enum
10291029
, BV_EP
10301030
, BV_ET
10311031
, BV_FENC
1032+
, BV_FP
10321033
#ifdef FEAT_EVAL
10331034
, BV_BEXPR
10341035
, BV_FEX

src/structs.h

+1
Original file line numberDiff line numberDiff line change
@@ -2097,6 +2097,7 @@ struct file_buffer
20972097
long_u b_p_inde_flags; /* flags for 'indentexpr' */
20982098
char_u *b_p_indk; /* 'indentkeys' */
20992099
#endif
2100+
char_u *b_p_fp; /* 'formatprg' */
21002101
#if defined(FEAT_EVAL)
21012102
char_u *b_p_fex; /* 'formatexpr' */
21022103
long_u b_p_fex_flags; /* flags for 'formatexpr' */

src/testdir/test_normal.vim

+22-10
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,33 @@ func! Test_normal06_formatprg()
224224
" only test on non windows platform
225225
if has('win32')
226226
return
227-
else
228-
" uses sed to number non-empty lines
229-
call writefile(['#!/bin/sh', 'sed ''/./=''|sed ''/./{', 'N', 's/\n/ /', '}'''], 'Xsed_format.sh')
230-
call system('chmod +x ./Xsed_format.sh')
231227
endif
232-
call Setup_NewWindow()
233-
%d
234-
call setline(1, ['a', '', 'c', '', ' ', 'd', 'e'])
228+
229+
" uses sed to number non-empty lines
230+
call writefile(['#!/bin/sh', 'sed ''/./=''|sed ''/./{', 'N', 's/\n/ /', '}'''], 'Xsed_format.sh')
231+
call system('chmod +x ./Xsed_format.sh')
232+
let text = ['a', '', 'c', '', ' ', 'd', 'e']
233+
let expected = ['1 a', '', '3 c', '', '5 ', '6 d', '7 e']
234+
235+
10new
236+
call setline(1, text)
235237
set formatprg=./Xsed_format.sh
236238
norm! gggqG
237-
call assert_equal(['1 a', '', '3 c', '', '5 ', '6 d', '7 e'], getline(1, '$'))
239+
call assert_equal(expected, getline(1, '$'))
240+
bw!
241+
242+
10new
243+
call setline(1, text)
244+
set formatprg=donothing
245+
setlocal formatprg=./Xsed_format.sh
246+
norm! gggqG
247+
call assert_equal(expected, getline(1, '$'))
248+
bw!
249+
238250
" clean up
239251
set formatprg=
252+
setlocal formatprg=
240253
call delete('Xsed_format.sh')
241-
bw!
242254
endfunc
243255

244256
func! Test_normal07_internalfmt()
@@ -251,7 +263,7 @@ func! Test_normal07_internalfmt()
251263
norm! gggqG
252264
call assert_equal(['1 2 3', '4 5 6', '7 8 9', '10 11 '], getline(1, '$'))
253265
" clean up
254-
set formatprg= tw=0
266+
set tw=0
255267
bw!
256268
endfunc
257269

src/version.c

+2
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
179,
767769
/**/
768770
178,
769771
/**/

0 commit comments

Comments
 (0)