]> granicus.if.org Git - vim/commitdiff
patch 8.2.2993: 'fileencodings' default value should depend on 'encoding' v8.2.2993
authorBram Moolenaar <Bram@vim.org>
Sun, 13 Jun 2021 18:27:36 +0000 (20:27 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 13 Jun 2021 18:27:36 +0000 (20:27 +0200)
Problem:    'fileencodings' default value should depend on 'encoding'. (Gary
            Johnson)
Solution:   When 'encoding' is "utf-8" use a different default value for
            'fileencodings'.

src/mbyte.c
src/option.c
src/proto/option.pro
src/testdir/test_options.vim
src/version.c

index 4b78eee0e5c10072d8f0e5e3090b696931e48c5f..fb0c4820ba7640cfc7904af35cbf8f44a93e463e 100644 (file)
@@ -733,8 +733,7 @@ codepage_invalid:
 
     // When using Unicode, set default for 'fileencodings'.
     if (enc_utf8 && !option_was_set((char_u *)"fencs"))
-       set_string_option_direct((char_u *)"fencs", -1,
-                      (char_u *)"ucs-bom,utf-8,default,latin1", OPT_FREE, 0);
+       set_fencs_unicode();
 
 #if defined(HAVE_BIND_TEXTDOMAIN_CODESET) && defined(FEAT_GETTEXT)
     // GNU gettext 0.10.37 supports this feature: set the codeset used for
index 33d29a1fc43eebbf8a7e4e2f20689889bbf92442..dd44fe4ac4441b0828a928c486b563b5befff570 100644 (file)
@@ -535,6 +535,19 @@ set_init_1(int clean_arg)
 #endif
 }
 
+static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
+
+/*
+ * Set the "fileencodings" option to the default value for when 'encoding' is
+ * utf-8.
+ */
+    void
+set_fencs_unicode()
+{
+    set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
+                                                                 OPT_FREE, 0);
+}
+
 /*
  * Set an option to its default value.
  * This does not take care of side effects!
@@ -558,9 +571,12 @@ set_option_default(
        dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
        if (flags & P_STRING)
        {
+           // 'fencs' default value depends on 'encoding'
+           if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
+               set_fencs_unicode();
            // Use set_string_option_direct() for local options to handle
            // freeing and allocating the value.
-           if (options[opt_idx].indir != PV_NONE)
+           else if (options[opt_idx].indir != PV_NONE)
                set_string_option_direct(NULL, opt_idx,
                                 options[opt_idx].def_val[dvi], opt_flags, 0);
            else
@@ -1684,6 +1700,8 @@ do_set(
 #endif
                                    newval = term_bg_default();
                            }
+                           else if ((char_u **)varp == &p_fencs && enc_utf8)
+                               newval = fencs_utf8_default;
 
                            // expand environment variables and ~ (since the
                            // default value was already expanded, only
index 8d672aac0504a4a6b566986855fe3735c0dc45ec..25cfb6541fb53b5461ef1a963186d21de98ede28 100644 (file)
@@ -1,5 +1,6 @@
 /* option.c */
 void set_init_1(int clean_arg);
+void set_fencs_unicode(void);
 void set_string_default(char *name, char_u *val);
 void set_number_default(char *name, long val);
 void set_local_options_default(win_T *wp, int do_buffer);
index d640426453b0acfac9c4aef9b8dd38bc82a53f7a..2f64d128752284aa5f1b138dde67bfd1488bcb6f 100644 (file)
@@ -1140,6 +1140,14 @@ func Test_opt_default()
   call assert_equal('vt', &formatoptions)
   set formatoptions&vim
   call assert_equal('tcq', &formatoptions)
+
+  call assert_equal('ucs-bom,utf-8,default,latin1', &fencs)
+  set fencs=latin1
+  set fencs&
+  call assert_equal('ucs-bom,utf-8,default,latin1', &fencs)
+  set fencs=latin1
+  set all&
+  call assert_equal('ucs-bom,utf-8,default,latin1', &fencs)
 endfunc
 
 " Test for the 'cmdheight' option
index 441826b95b11cff1eefcd2ad68da863652fe276b..b685257e57265efc6c8e15290c245bd06d88db7e 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2993,
 /**/
     2992,
 /**/