Skip to content

Commit 5da901b

Browse files
yegappanbrammool
authored andcommitted
patch 9.0.1359: too many "else if" statements in handling options
Problem: Too many "else if" statements in handling options. Solution: Add more functions for handling option changes. (Yegappan Lakshmanan, closes #12060)
1 parent 30a8447 commit 5da901b

8 files changed

Lines changed: 188 additions & 61 deletions

File tree

src/map.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,11 +3088,10 @@ did_set_langmap(optset_T *args UNUSED)
30883088
}
30893089
if (to == NUL)
30903090
{
3091-
// TODO: Need to use errbuf argument for this error message
3092-
// and return it.
3093-
semsg(_(e_langmap_matching_character_missing_for_str),
3094-
transchar(from));
3095-
return NULL;
3091+
sprintf(args->os_errbuf,
3092+
_(e_langmap_matching_character_missing_for_str),
3093+
transchar(from));
3094+
return args->os_errbuf;
30963095
}
30973096

30983097
if (from >= 256)
@@ -3112,10 +3111,10 @@ did_set_langmap(optset_T *args UNUSED)
31123111
{
31133112
if (p[0] != ',')
31143113
{
3115-
// TODO: Need to use errbuf argument for this error
3116-
// message and return it.
3117-
semsg(_(e_langmap_extra_characters_after_semicolon_str), p);
3118-
return NULL;
3114+
sprintf(args->os_errbuf,
3115+
_(e_langmap_extra_characters_after_semicolon_str),
3116+
p);
3117+
return args->os_errbuf;
31193118
}
31203119
++p;
31213120
}

src/optiondefs.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,8 @@ static struct vimoption options[] =
939939
#endif
940940
(char_u *)0L} SCTX_INIT},
941941
{"filetype", "ft", P_STRING|P_EXPAND|P_ALLOCED|P_VI_DEF|P_NOGLOB|P_NFNAME,
942-
(char_u *)&p_ft, PV_FT, NULL,
942+
(char_u *)&p_ft, PV_FT,
943+
did_set_filetype_or_syntax,
943944
{(char_u *)"", (char_u *)0L}
944945
SCTX_INIT},
945946
{"fillchars", "fcs", P_STRING|P_VI_DEF|P_RALL|P_ONECOMMA|P_NODUP,
@@ -1411,7 +1412,7 @@ static struct vimoption options[] =
14111412
(char_u *)&p_im, PV_NONE, did_set_insertmode,
14121413
{(char_u *)FALSE, (char_u *)0L} SCTX_INIT},
14131414
{"isfname", "isf", P_STRING|P_VI_DEF|P_COMMA|P_NODUP,
1414-
(char_u *)&p_isf, PV_NONE, NULL,
1415+
(char_u *)&p_isf, PV_NONE, did_set_isopt,
14151416
{
14161417
#ifdef BACKSLASH_IN_FILENAME
14171418
// Excluded are: & and ^ are special in cmd.exe
@@ -1428,7 +1429,7 @@ static struct vimoption options[] =
14281429
#endif
14291430
(char_u *)0L} SCTX_INIT},
14301431
{"isident", "isi", P_STRING|P_VI_DEF|P_COMMA|P_NODUP,
1431-
(char_u *)&p_isi, PV_NONE, NULL,
1432+
(char_u *)&p_isi, PV_NONE, did_set_isopt,
14321433
{
14331434
#if defined(MSWIN)
14341435
(char_u *)"@,48-57,_,128-167,224-235",
@@ -1437,7 +1438,7 @@ static struct vimoption options[] =
14371438
#endif
14381439
(char_u *)0L} SCTX_INIT},
14391440
{"iskeyword", "isk", P_STRING|P_ALLOCED|P_VIM|P_COMMA|P_NODUP,
1440-
(char_u *)&p_isk, PV_ISK, NULL,
1441+
(char_u *)&p_isk, PV_ISK, did_set_isopt,
14411442
{
14421443
(char_u *)"@,48-57,_",
14431444
#if defined(MSWIN)
@@ -1447,7 +1448,7 @@ static struct vimoption options[] =
14471448
#endif
14481449
} SCTX_INIT},
14491450
{"isprint", "isp", P_STRING|P_VI_DEF|P_RALL|P_COMMA|P_NODUP,
1450-
(char_u *)&p_isp, PV_NONE, NULL,
1451+
(char_u *)&p_isp, PV_NONE, did_set_isopt,
14511452
{
14521453
#if defined(MSWIN) || defined(VMS)
14531454
(char_u *)"@,~-255",
@@ -1469,7 +1470,7 @@ static struct vimoption options[] =
14691470
SCTX_INIT},
14701471
{"keymap", "kmp", P_STRING|P_ALLOCED|P_VI_DEF|P_RBUF|P_RSTAT|P_NFNAME|P_PRI_MKRC,
14711472
#ifdef FEAT_KEYMAP
1472-
(char_u *)&p_keymap, PV_KMAP, NULL,
1473+
(char_u *)&p_keymap, PV_KMAP, did_set_keymap,
14731474
{(char_u *)"", (char_u *)0L}
14741475
#else
14751476
(char_u *)NULL, PV_NONE, NULL,
@@ -2095,7 +2096,7 @@ static struct vimoption options[] =
20952096
{(char_u *)FALSE, (char_u *)0L} SCTX_INIT},
20962097
{"rightleftcmd", "rlc", P_STRING|P_ALLOCED|P_VI_DEF|P_RWIN,
20972098
#ifdef FEAT_RIGHTLEFT
2098-
(char_u *)VAR_WIN, PV_RLC, NULL,
2099+
(char_u *)VAR_WIN, PV_RLC, did_set_rightleftcmd,
20992100
{(char_u *)"search", (char_u *)NULL}
21002101
#else
21012102
(char_u *)NULL, PV_NONE, NULL,
@@ -2419,7 +2420,7 @@ static struct vimoption options[] =
24192420
(char_u *)&p_swf, PV_SWF, did_set_swapfile,
24202421
{(char_u *)TRUE, (char_u *)0L} SCTX_INIT},
24212422
{"swapsync", "sws", P_STRING|P_VI_DEF,
2422-
(char_u *)&p_sws, PV_NONE, NULL,
2423+
(char_u *)&p_sws, PV_NONE, did_set_swapsync,
24232424
{(char_u *)"fsync", (char_u *)0L} SCTX_INIT},
24242425
{"switchbuf", "swb", P_STRING|P_VI_DEF|P_ONECOMMA|P_NODUP,
24252426
(char_u *)&p_swb, PV_NONE, did_set_switchbuf,
@@ -2435,7 +2436,8 @@ static struct vimoption options[] =
24352436
SCTX_INIT},
24362437
{"syntax", "syn", P_STRING|P_ALLOCED|P_VI_DEF|P_NOGLOB|P_NFNAME,
24372438
#ifdef FEAT_SYN_HL
2438-
(char_u *)&p_syn, PV_SYN, NULL,
2439+
(char_u *)&p_syn, PV_SYN,
2440+
did_set_filetype_or_syntax,
24392441
{(char_u *)"", (char_u *)0L}
24402442
#else
24412443
(char_u *)NULL, PV_NONE, NULL,

src/optionstr.c

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
9797
static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
9898
#endif
9999
static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
100+
static char *(p_sws_values[]) = {"fsync", "sync", NULL};
100101

101102
static int check_opt_strings(char_u *val, char **values, int list);
102103
static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
@@ -761,15 +762,15 @@ did_set_breakindentopt(optset_T *args UNUSED)
761762
* The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
762763
* changed.
763764
*/
764-
static char *
765-
did_set_isopt(int *did_chartab)
765+
char *
766+
did_set_isopt(optset_T *args)
766767
{
767768
// 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
768769
// If the new option is invalid, use old value.
769770
// 'lisp' option: refill g_chartab[] for '-' char.
770771
if (init_chartab() == FAIL)
771772
{
772-
*did_chartab = TRUE; // need to restore it below
773+
args->os_restore_chartab = TRUE;// need to restore the chartab.
773774
return e_invalid_argument; // error in value
774775
}
775776

@@ -928,6 +929,15 @@ did_set_splitkeep(optset_T *args UNUSED)
928929
return did_set_opt_strings(p_spk, p_spk_values, FALSE);
929930
}
930931

932+
/*
933+
* The 'swapsync' option is changed.
934+
*/
935+
char *
936+
did_set_swapsync(optset_T *args UNUSED)
937+
{
938+
return did_set_opt_strings(p_sws, p_sws_values, FALSE);
939+
}
940+
931941
/*
932942
* The 'switchbuf' option is changed.
933943
*/
@@ -1224,16 +1234,16 @@ did_set_imactivatekey(optset_T *args UNUSED)
12241234
}
12251235
#endif
12261236

1227-
#ifdef FEAT_KEYMAP
1237+
#if defined(FEAT_KEYMAP) || defined(PROTO)
12281238
/*
12291239
* The 'keymap' option is changed.
12301240
*/
1231-
static char *
1232-
did_set_keymap(char_u **varp, int opt_flags, int *value_checked)
1241+
char *
1242+
did_set_keymap(optset_T *args)
12331243
{
12341244
char *errmsg = NULL;
12351245

1236-
if (!valid_filetype(*varp))
1246+
if (!valid_filetype(args->os_varp))
12371247
errmsg = e_invalid_argument;
12381248
else
12391249
{
@@ -1250,7 +1260,7 @@ did_set_keymap(char_u **varp, int opt_flags, int *value_checked)
12501260

12511261
// Since we check the value, there is no need to set P_INSECURE,
12521262
// even when the value comes from a modeline.
1253-
*value_checked = TRUE;
1263+
args->os_value_checked = TRUE;
12541264
}
12551265

12561266
if (errmsg == NULL)
@@ -1270,7 +1280,7 @@ did_set_keymap(char_u **varp, int opt_flags, int *value_checked)
12701280
if (curbuf->b_p_imsearch == B_IMODE_LMAP)
12711281
curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
12721282
}
1273-
if ((opt_flags & OPT_LOCAL) == 0)
1283+
if ((args->os_flags & OPT_LOCAL) == 0)
12741284
{
12751285
set_iminsert_global();
12761286
set_imsearch_global();
@@ -2573,7 +2583,8 @@ did_set_cinoptions(optset_T *args UNUSED)
25732583
char *
25742584
did_set_lispoptions(optset_T *args)
25752585
{
2576-
if (*args->os_varp != NUL && STRCMP(args->os_varp, "expr:0") != 0
2586+
if (*args->os_varp != NUL
2587+
&& STRCMP(args->os_varp, "expr:0") != 0
25772588
&& STRCMP(args->os_varp, "expr:1") != 0)
25782589
return e_invalid_argument;
25792590

@@ -2594,24 +2605,36 @@ did_set_renderoptions(optset_T *args UNUSED)
25942605
}
25952606
#endif
25962607

2608+
#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
2609+
/*
2610+
* The 'rightleftcmd' option is changed.
2611+
*/
2612+
char *
2613+
did_set_rightleftcmd(optset_T *args)
2614+
{
2615+
// Currently only "search" is a supported value.
2616+
if (*args->os_varp != NUL && STRCMP(args->os_varp, "search") != 0)
2617+
return e_invalid_argument;
2618+
2619+
return NULL;
2620+
}
2621+
#endif
2622+
25972623
/*
25982624
* The 'filetype' or the 'syntax' option is changed.
25992625
*/
2600-
static char *
2601-
did_set_filetype_or_syntax(
2602-
char_u **varp,
2603-
char_u *oldval,
2604-
int *value_checked,
2605-
int *value_changed)
2626+
char *
2627+
did_set_filetype_or_syntax(optset_T *args)
26062628
{
2607-
if (!valid_filetype(*varp))
2629+
if (!valid_filetype(args->os_varp))
26082630
return e_invalid_argument;
26092631

2610-
*value_changed = STRCMP(oldval, *varp) != 0;
2632+
args->os_value_changed =
2633+
STRCMP(args->os_oldval.string, args->os_varp) != 0;
26112634

26122635
// Since we check the value, there is no need to set P_INSECURE,
26132636
// even when the value comes from a modeline.
2614-
*value_checked = TRUE;
2637+
args->os_value_checked = TRUE;
26152638

26162639
return NULL;
26172640
}
@@ -3008,7 +3031,7 @@ did_set_string_option(
30083031
// need to set P_INSECURE
30093032
{
30103033
char *errmsg = NULL;
3011-
int did_chartab = FALSE;
3034+
int restore_chartab = FALSE;
30123035
char_u **gvarp;
30133036
long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
30143037
int value_changed = FALSE;
@@ -3037,31 +3060,37 @@ did_set_string_option(
30373060
args.os_flags = opt_flags;
30383061
args.os_oldval.string = oldval;
30393062
args.os_newval.string = value;
3063+
args.os_value_checked = FALSE;
3064+
args.os_value_changed = FALSE;
3065+
args.os_restore_chartab = FALSE;
30403066
args.os_errbuf = errbuf;
3067+
// Invoke the option specific callback function to validate and apply
3068+
// the new option value.
30413069
errmsg = did_set_cb(&args);
3070+
30423071
#ifdef FEAT_EVAL
3043-
// When processing the '*expr' options (e.g. diffexpr, foldexpr, etc.),
3044-
// the did_set_cb() function may modify '*varp'.
3072+
// The '*expr' option (e.g. diffexpr, foldexpr, etc.), callback
3073+
// functions may modify '*varp'.
30453074
if (errmsg == NULL && is_expr_option(varp, gvarp))
30463075
*varp = args.os_varp;
30473076
#endif
3077+
// The 'filetype' and 'syntax' option callback functions may change
3078+
// the os_value_changed field.
3079+
value_changed = args.os_value_changed;
3080+
// The 'keymap', 'filetype' and 'syntax' option callback functions
3081+
// may change the os_value_checked field.
3082+
*value_checked = args.os_value_checked;
3083+
// The 'isident', 'iskeyword', 'isprint' and 'isfname' options may
3084+
// change the character table. On failure, this needs to be restored.
3085+
restore_chartab = args.os_restore_chartab;
30483086
}
30493087
else if (varp == &T_NAME) // 'term'
30503088
errmsg = did_set_term(&opt_idx, &free_oldval);
3051-
else if ( varp == &p_isi // 'isident'
3052-
|| varp == &(curbuf->b_p_isk) // 'iskeyword'
3053-
|| varp == &p_isp // 'isprint'
3054-
|| varp == &p_isf) // 'isfname'
3055-
errmsg = did_set_isopt(&did_chartab);
30563089
else if ( varp == &p_enc // 'encoding'
30573090
|| gvarp == &p_fenc // 'fileencoding'
30583091
|| varp == &p_tenc // 'termencoding'
30593092
|| gvarp == &p_menc) // 'makeencoding'
30603093
errmsg = did_set_encoding(varp, gvarp, opt_flags);
3061-
#ifdef FEAT_KEYMAP
3062-
else if (varp == &curbuf->b_p_keymap) // 'keymap'
3063-
errmsg = did_set_keymap(varp, opt_flags, value_checked);
3064-
#endif
30653094
else if ( varp == &p_lcs // global 'listchars'
30663095
|| varp == &p_fcs) // global 'fillchars'
30673096
errmsg = did_set_global_listfillchars(varp, opt_flags);
@@ -3072,22 +3101,14 @@ did_set_string_option(
30723101
// terminal options
30733102
else if (istermoption_idx(opt_idx) && full_screen)
30743103
did_set_term_option(varp, &did_swaptcap);
3075-
else if (gvarp == &p_ft) // 'filetype'
3076-
errmsg = did_set_filetype_or_syntax(varp, oldval, value_checked,
3077-
&value_changed);
3078-
#ifdef FEAT_SYN_HL
3079-
else if (gvarp == &p_syn) // 'syntax'
3080-
errmsg = did_set_filetype_or_syntax(varp, oldval, value_checked,
3081-
&value_changed);
3082-
#endif
30833104

30843105
// If an error is detected, restore the previous value.
30853106
if (errmsg != NULL)
30863107
{
30873108
free_string_option(*varp);
30883109
*varp = oldval;
30893110
// When resetting some values, need to act on it.
3090-
if (did_chartab)
3111+
if (restore_chartab)
30913112
(void)init_chartab();
30923113
if (varp == &p_hl)
30933114
(void)highlight_changed();

src/proto/optionstr.pro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ char *set_string_option(int opt_idx, char_u *value, int opt_flags, char *errbuf)
1212
char *did_set_backupcopy(optset_T *args);
1313
char *did_set_backupext_or_patchmode(optset_T *args);
1414
char *did_set_breakindentopt(optset_T *args);
15+
char *did_set_isopt(optset_T *args);
1516
char *did_set_helpfile(optset_T *args);
1617
char *did_set_colorcolumn(optset_T *args);
1718
char *did_set_cursorlineopt(optset_T *args);
@@ -23,6 +24,7 @@ char *did_set_scrollopt(optset_T *args);
2324
char *did_set_selectmode(optset_T *args);
2425
char *did_set_showcmdloc(optset_T *args);
2526
char *did_set_splitkeep(optset_T *args);
27+
char *did_set_swapsync(optset_T *args);
2628
char *did_set_switchbuf(optset_T *args);
2729
char *did_set_sessionoptions(optset_T *args);
2830
char *did_set_viewoptions(optset_T *args);
@@ -36,6 +38,7 @@ char *did_set_eadirection(optset_T *args);
3638
char *did_set_eventignore(optset_T *args);
3739
char *did_set_printencoding(optset_T *args);
3840
char *did_set_imactivatekey(optset_T *args);
41+
char *did_set_keymap(optset_T *args);
3942
char *did_set_fileformat(optset_T *args);
4043
char *did_set_fileformats(optset_T *args);
4144
char *did_set_cryptkey(optset_T *args);
@@ -96,6 +99,8 @@ char *did_set_cscopequickfix(optset_T *args);
9699
char *did_set_cinoptions(optset_T *args);
97100
char *did_set_lispoptions(optset_T *args);
98101
char *did_set_renderoptions(optset_T *args);
102+
char *did_set_rightleftcmd(optset_T *args);
103+
char *did_set_filetype_or_syntax(optset_T *args);
99104
char *did_set_termwinkey(optset_T *args);
100105
char *did_set_termwinsize(optset_T *args);
101106
char *did_set_termwintype(optset_T *args);

src/structs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4820,6 +4820,17 @@ typedef struct
48204820
// Currently only used for boolean options.
48214821
int os_doskip;
48224822

4823+
// Option value was checked to be safe, no need to set P_INSECURE
4824+
// Used for the 'keymap', 'filetype' and 'syntax' options.
4825+
int os_value_checked;
4826+
// Option value changed. Used for the 'filetype' and 'syntax' options.
4827+
int os_value_changed;
4828+
4829+
// Used by the 'isident', 'iskeyword', 'isprint' and 'isfname' options.
4830+
// Set to TRUE if the character table is modified when processing the
4831+
// option and need to be restored because of a failure.
4832+
int os_restore_chartab;
4833+
48234834
// If the value specified for an option is not valid and the error message
48244835
// is parameterized, then the "os_errbuf" buffer is used to store the error
48254836
// message (when it is not NULL).

0 commit comments

Comments
 (0)