1
0
forked from aniani/vim

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)
This commit is contained in:
Bram Moolenaar
2017-01-14 14:28:30 +01:00
parent 9b73c4a215
commit 9be7c04e6c
7 changed files with 43 additions and 16 deletions

View File

@@ -3417,7 +3417,7 @@ A jump table for the options with a short description can be found at |Q_op|.
*'formatprg'* *'fp'* *'formatprg'* *'fp'*
'formatprg' 'fp' string (default "") 'formatprg' 'fp' string (default "")
global global or local to buffer |global-local|
{not in Vi} {not in Vi}
The name of an external program that will be used to format the lines The name of an external program that will be used to format the lines
selected with the |gq| operator. The program must take the input on selected with the |gq| operator. The program must take the input on

View File

@@ -1984,7 +1984,7 @@ do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
op_formatexpr(oap); /* use expression */ op_formatexpr(oap); /* use expression */
else else
#endif #endif
if (*p_fp != NUL) if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
op_colon(oap); /* use external command */ op_colon(oap); /* use external command */
else else
op_format(oap, FALSE); /* use internal function */ op_format(oap, FALSE); /* use internal function */
@@ -2197,10 +2197,12 @@ op_colon(oparg_T *oap)
} }
else if (oap->op_type == OP_FORMAT) else if (oap->op_type == OP_FORMAT)
{ {
if (*p_fp == NUL) if (*curbuf->b_p_fp != NUL)
stuffReadbuff((char_u *)"fmt"); stuffReadbuff(curbuf->b_p_fp);
else else if (*p_fp != NUL)
stuffReadbuff(p_fp); stuffReadbuff(p_fp);
else
stuffReadbuff((char_u *)"fmt");
stuffReadbuff((char_u *)"\n']"); stuffReadbuff((char_u *)"\n']");
} }

View File

@@ -107,6 +107,7 @@
#if defined(FEAT_BEVAL) && defined(FEAT_EVAL) #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
# define PV_BEXPR OPT_BOTH(OPT_BUF(BV_BEXPR)) # define PV_BEXPR OPT_BOTH(OPT_BUF(BV_BEXPR))
#endif #endif
#define PV_FP OPT_BOTH(OPT_BUF(BV_FP))
#ifdef FEAT_EVAL #ifdef FEAT_EVAL
# define PV_FEX OPT_BUF(BV_FEX) # define PV_FEX OPT_BUF(BV_FEX)
#endif #endif
@@ -1258,7 +1259,7 @@ static struct vimoption options[] =
{(char_u *)"^\\s*\\d\\+[\\]:.)}\\t ]\\s*", {(char_u *)"^\\s*\\d\\+[\\]:.)}\\t ]\\s*",
(char_u *)0L} SCRIPTID_INIT}, (char_u *)0L} SCRIPTID_INIT},
{"formatprg", "fp", P_STRING|P_EXPAND|P_VI_DEF|P_SECURE, {"formatprg", "fp", P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
(char_u *)&p_fp, PV_NONE, (char_u *)&p_fp, PV_FP,
{(char_u *)"", (char_u *)0L} SCRIPTID_INIT}, {(char_u *)"", (char_u *)0L} SCRIPTID_INIT},
{"fsync", "fs", P_BOOL|P_SECURE|P_VI_DEF, {"fsync", "fs", P_BOOL|P_SECURE|P_VI_DEF,
#ifdef HAVE_FSYNC #ifdef HAVE_FSYNC
@@ -5481,6 +5482,7 @@ check_buf_options(buf_T *buf)
#if defined(FEAT_CRYPT) #if defined(FEAT_CRYPT)
check_string_option(&buf->b_p_cm); check_string_option(&buf->b_p_cm);
#endif #endif
check_string_option(&buf->b_p_fp);
#if defined(FEAT_EVAL) #if defined(FEAT_EVAL)
check_string_option(&buf->b_p_fex); check_string_option(&buf->b_p_fex);
#endif #endif
@@ -10175,6 +10177,9 @@ unset_global_local_option(char_u *name, void *from)
clear_string_option(&buf->b_p_tsr); clear_string_option(&buf->b_p_tsr);
break; break;
#endif #endif
case PV_FP:
clear_string_option(&buf->b_p_fp);
break;
#ifdef FEAT_QUICKFIX #ifdef FEAT_QUICKFIX
case PV_EFM: case PV_EFM:
clear_string_option(&buf->b_p_efm); clear_string_option(&buf->b_p_efm);
@@ -10228,6 +10233,7 @@ get_varp_scope(struct vimoption *p, int opt_flags)
{ {
switch ((int)p->indir) switch ((int)p->indir)
{ {
case PV_FP: return (char_u *)&(curbuf->b_p_fp);
#ifdef FEAT_QUICKFIX #ifdef FEAT_QUICKFIX
case PV_EFM: return (char_u *)&(curbuf->b_p_efm); case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
case PV_GP: return (char_u *)&(curbuf->b_p_gp); case PV_GP: return (char_u *)&(curbuf->b_p_gp);
@@ -10308,6 +10314,8 @@ get_varp(struct vimoption *p)
case PV_TSR: return *curbuf->b_p_tsr != NUL case PV_TSR: return *curbuf->b_p_tsr != NUL
? (char_u *)&(curbuf->b_p_tsr) : p->var; ? (char_u *)&(curbuf->b_p_tsr) : p->var;
#endif #endif
case PV_FP: return *curbuf->b_p_fp != NUL
? (char_u *)&(curbuf->b_p_fp) : p->var;
#ifdef FEAT_QUICKFIX #ifdef FEAT_QUICKFIX
case PV_EFM: return *curbuf->b_p_efm != NUL case PV_EFM: return *curbuf->b_p_efm != NUL
? (char_u *)&(curbuf->b_p_efm) : p->var; ? (char_u *)&(curbuf->b_p_efm) : p->var;
@@ -10873,6 +10881,7 @@ buf_copy_options(buf_T *buf, int flags)
buf->b_p_inde = vim_strsave(p_inde); buf->b_p_inde = vim_strsave(p_inde);
buf->b_p_indk = vim_strsave(p_indk); buf->b_p_indk = vim_strsave(p_indk);
#endif #endif
buf->b_p_fp = empty_option;
#if defined(FEAT_EVAL) #if defined(FEAT_EVAL)
buf->b_p_fex = vim_strsave(p_fex); buf->b_p_fex = vim_strsave(p_fex);
#endif #endif

View File

@@ -1029,6 +1029,7 @@ enum
, BV_EP , BV_EP
, BV_ET , BV_ET
, BV_FENC , BV_FENC
, BV_FP
#ifdef FEAT_EVAL #ifdef FEAT_EVAL
, BV_BEXPR , BV_BEXPR
, BV_FEX , BV_FEX

View File

@@ -2097,6 +2097,7 @@ struct file_buffer
long_u b_p_inde_flags; /* flags for 'indentexpr' */ long_u b_p_inde_flags; /* flags for 'indentexpr' */
char_u *b_p_indk; /* 'indentkeys' */ char_u *b_p_indk; /* 'indentkeys' */
#endif #endif
char_u *b_p_fp; /* 'formatprg' */
#if defined(FEAT_EVAL) #if defined(FEAT_EVAL)
char_u *b_p_fex; /* 'formatexpr' */ char_u *b_p_fex; /* 'formatexpr' */
long_u b_p_fex_flags; /* flags for 'formatexpr' */ long_u b_p_fex_flags; /* flags for 'formatexpr' */

View File

@@ -224,21 +224,33 @@ func! Test_normal06_formatprg()
" only test on non windows platform " only test on non windows platform
if has('win32') if has('win32')
return return
else
" uses sed to number non-empty lines
call writefile(['#!/bin/sh', 'sed ''/./=''|sed ''/./{', 'N', 's/\n/ /', '}'''], 'Xsed_format.sh')
call system('chmod +x ./Xsed_format.sh')
endif endif
call Setup_NewWindow()
%d " uses sed to number non-empty lines
call setline(1, ['a', '', 'c', '', ' ', 'd', 'e']) call writefile(['#!/bin/sh', 'sed ''/./=''|sed ''/./{', 'N', 's/\n/ /', '}'''], 'Xsed_format.sh')
call system('chmod +x ./Xsed_format.sh')
let text = ['a', '', 'c', '', ' ', 'd', 'e']
let expected = ['1 a', '', '3 c', '', '5 ', '6 d', '7 e']
10new
call setline(1, text)
set formatprg=./Xsed_format.sh set formatprg=./Xsed_format.sh
norm! gggqG norm! gggqG
call assert_equal(['1 a', '', '3 c', '', '5 ', '6 d', '7 e'], getline(1, '$')) call assert_equal(expected, getline(1, '$'))
bw!
10new
call setline(1, text)
set formatprg=donothing
setlocal formatprg=./Xsed_format.sh
norm! gggqG
call assert_equal(expected, getline(1, '$'))
bw!
" clean up " clean up
set formatprg= set formatprg=
setlocal formatprg=
call delete('Xsed_format.sh') call delete('Xsed_format.sh')
bw!
endfunc endfunc
func! Test_normal07_internalfmt() func! Test_normal07_internalfmt()
@@ -251,7 +263,7 @@ func! Test_normal07_internalfmt()
norm! gggqG norm! gggqG
call assert_equal(['1 2 3', '4 5 6', '7 8 9', '10 11 '], getline(1, '$')) call assert_equal(['1 2 3', '4 5 6', '7 8 9', '10 11 '], getline(1, '$'))
" clean up " clean up
set formatprg= tw=0 set tw=0
bw! bw!
endfunc endfunc

View File

@@ -764,6 +764,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
179,
/**/ /**/
178, 178,
/**/ /**/