vim-patch:8.0.0179

Problem:    'formatprg' is a global option but the value may depend on the
            type of buffer. (Sung Pae)
Solution:   Make 'formatprg' global-local. (closes vim/vim#1380)

9be7c04e6c
This commit is contained in:
raichoo 2017-03-09 17:33:51 +01:00
parent 2ad25c0466
commit 0f5c3f111a
7 changed files with 47 additions and 21 deletions

View File

@ -2731,7 +2731,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|
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
stdin and produce the output on stdout. The Unix program "fmt" is stdin and produce the output on stdout. The Unix program "fmt" is

View File

@ -640,6 +640,7 @@ struct file_buffer {
char_u *b_p_inde; ///< 'indentexpr' char_u *b_p_inde; ///< 'indentexpr'
uint32_t b_p_inde_flags; ///< flags for 'indentexpr' uint32_t b_p_inde_flags; ///< flags for 'indentexpr'
char_u *b_p_indk; ///< 'indentkeys' char_u *b_p_indk; ///< 'indentkeys'
char_u *b_p_fp; ///< 'formatprg'
char_u *b_p_fex; ///< 'formatexpr' char_u *b_p_fex; ///< 'formatexpr'
uint32_t b_p_fex_flags; ///< flags for 'formatexpr' uint32_t b_p_fex_flags; ///< flags for 'formatexpr'
char_u *b_p_kp; ///< 'keywordprg' char_u *b_p_kp; ///< 'keywordprg'

View File

@ -1901,12 +1901,13 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank)
break; break;
case OP_FORMAT: case OP_FORMAT:
if (*curbuf->b_p_fex != NUL) if (*curbuf->b_p_fex != NUL) {
op_formatexpr(oap); /* use expression */ op_formatexpr(oap); // use expression
else if (*p_fp != NUL) } else 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
}
break; break;
case OP_FORMAT2: case OP_FORMAT2:
@ -2064,10 +2065,13 @@ static void op_colon(oparg_T *oap)
stuffReadbuff(get_equalprg()); stuffReadbuff(get_equalprg());
stuffReadbuff((char_u *)"\n"); stuffReadbuff((char_u *)"\n");
} 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

@ -2150,6 +2150,7 @@ void check_buf_options(buf_T *buf)
check_string_option(&buf->b_p_inex); check_string_option(&buf->b_p_inex);
check_string_option(&buf->b_p_inde); check_string_option(&buf->b_p_inde);
check_string_option(&buf->b_p_indk); check_string_option(&buf->b_p_indk);
check_string_option(&buf->b_p_fp);
check_string_option(&buf->b_p_fex); check_string_option(&buf->b_p_fex);
check_string_option(&buf->b_p_kp); check_string_option(&buf->b_p_kp);
check_string_option(&buf->b_p_mps); check_string_option(&buf->b_p_mps);
@ -5255,6 +5256,9 @@ void unset_global_local_option(char *name, void *from)
case PV_TSR: case PV_TSR:
clear_string_option(&buf->b_p_tsr); clear_string_option(&buf->b_p_tsr);
break; break;
case PV_FP:
clear_string_option(&buf->b_p_fp);
break;
case PV_EFM: case PV_EFM:
clear_string_option(&buf->b_p_efm); clear_string_option(&buf->b_p_efm);
break; break;
@ -5288,6 +5292,7 @@ static char_u *get_varp_scope(vimoption_T *p, int opt_flags)
} }
if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH)) { if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH)) {
switch ((int)p->indir) { switch ((int)p->indir) {
case PV_FP: return (char_u *)&(curbuf->b_p_fp);
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);
case PV_MP: return (char_u *)&(curbuf->b_p_mp); case PV_MP: return (char_u *)&(curbuf->b_p_mp);
@ -5346,6 +5351,8 @@ static char_u *get_varp(vimoption_T *p)
? (char_u *)&(curbuf->b_p_dict) : p->var; ? (char_u *)&(curbuf->b_p_dict) : p->var;
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;
case PV_FP: return *curbuf->b_p_fp != NUL
? (char_u *)&(curbuf->b_p_fp) : p->var;
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;
case PV_GP: return *curbuf->b_p_gp != NUL case PV_GP: return *curbuf->b_p_gp != NUL
@ -5694,6 +5701,7 @@ void buf_copy_options(buf_T *buf, int flags)
buf->b_s.b_p_spl = vim_strsave(p_spl); buf->b_s.b_p_spl = vim_strsave(p_spl);
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);
buf->b_p_fp = empty_option;
buf->b_p_fex = vim_strsave(p_fex); buf->b_p_fex = vim_strsave(p_fex);
buf->b_p_sua = vim_strsave(p_sua); buf->b_p_sua = vim_strsave(p_sua);
buf->b_p_keymap = vim_strsave(p_keymap); buf->b_p_keymap = vim_strsave(p_keymap);

View File

@ -713,6 +713,7 @@ enum {
, BV_EP , BV_EP
, BV_ET , BV_ET
, BV_FENC , BV_FENC
, BV_FP
, BV_BEXPR , BV_BEXPR
, BV_FEX , BV_FEX
, BV_FF , BV_FF

View File

@ -948,7 +948,7 @@ return {
}, },
{ {
full_name='formatprg', abbreviation='fp', full_name='formatprg', abbreviation='fp',
type='string', scope={'global'}, type='string', scope={'global', 'buffer'},
secure=true, secure=true,
vi_def=true, vi_def=true,
expand=true, expand=true,

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