-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
option.c
8848 lines (8108 loc) · 214 KB
/
option.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* Code to handle user-settable options. This is all pretty much table-
* driven. Checklist for adding a new option:
* - Put it in the options array in optiondefs.h (copy an existing entry).
* - For a global option: Add a variable for it in option.h.
* - For a buffer or window local option:
* - Add a PV_XX macro definition to the optiondefs.h file.
* - Add a variable to the window or buffer struct in structs.h.
* - For a window option, add some code to copy_winopt().
* - For a window string option, add code to check_win_options() and
* clear_winopt().
* - For a buffer option, add some code to buf_copy_options().
* - For a buffer string option, add code to check_buf_options().
* - If it's a numeric option, add any necessary bounds checks to
* set_num_option().
* - If it's a list of flags, add some code in did_set_string_option(), search
* for WW_ALL.
* - When adding an option with expansion (P_EXPAND), but with a different
* default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
* - Add documentation! One line in doc/quickref.txt, full description in
* options.txt, and any other related places.
* - Add an entry in runtime/optwin.vim.
* When making changes:
* - Adjust the help for the option in doc/option.txt.
* - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
* comment at the help for the 'compatible' option.
*/
#define IN_OPTION_C
#include "vim.h"
#include "optiondefs.h"
static void set_options_default(int opt_flags);
static void set_string_default_esc(char *name, char_u *val, int escape);
static char_u *find_dup_item(char_u *origval, char_u *newval, size_t newvallen, long_u flags);
static char_u *option_expand(int opt_idx, char_u *val);
static void didset_options(void);
static void didset_options2(void);
#if defined(FEAT_EVAL) || defined(PROTO)
static long_u *insecure_flag(int opt_idx, int opt_flags);
#else
# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
#endif
static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
static int find_key_option(char_u *arg_arg, int has_lt);
static void showoptions(int all, int opt_flags);
static int optval_default(struct vimoption *, char_u *varp, int compatible);
static void showoneopt(struct vimoption *, int opt_flags);
static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
static int put_setbool(FILE *fd, char *cmd, char *name, int value);
static int istermoption(struct vimoption *p);
static char_u *get_varp_scope(struct vimoption *p, int scope);
static char_u *get_varp(struct vimoption *);
static void check_win_options(win_T *win);
static void option_value2string(struct vimoption *, int scope);
static void check_winopt(winopt_T *wop);
static int wc_use_keyname(char_u *varp, long *wcp);
static void compatible_set(void);
#if defined(FEAT_EVAL) || defined(PROTO)
static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
"softtabstop", "textwidth", "wrapmargin",
#ifdef FEAT_RIGHTLEFT
"hkmap", "revins",
#endif
#ifdef FEAT_VARTABS
"varsofttabstop",
#endif
NULL};
static void didset_options_sctx(int opt_flags, char **buf);
#endif
/*
* Initialize the 'shell' option to a default value.
*/
static void
set_init_default_shell(void)
{
char_u *p;
// Find default value for 'shell' option.
// Don't use it if it is empty.
if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
#if defined(MSWIN)
|| ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
|| ((p = (char_u *)default_shell()) != NULL && *p != NUL)
#endif
)
#if defined(MSWIN)
{
// For MS-Windows put the path in quotes instead of escaping spaces.
char_u *cmd;
size_t len;
if (vim_strchr(p, ' ') != NULL)
{
len = STRLEN(p) + 3; // two quotes and a trailing NUL
cmd = alloc(len);
if (cmd != NULL)
{
vim_snprintf((char *)cmd, len, "\"%s\"", p);
set_string_default("sh", cmd);
vim_free(cmd);
}
}
else
set_string_default("sh", p);
}
#else
set_string_default_esc("sh", p, TRUE);
#endif
}
/*
* Set the default for 'backupskip' to include environment variables for
* temp files.
*/
static void
set_init_default_backupskip(void)
{
int opt_idx;
int i;
char_u *p;
int plen;
#ifdef UNIX
static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
#else
static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
#endif
garray_T ga;
opt_idx = findoption((char_u *)"backupskip");
ga_init2(&ga, 1, 100);
for (i = 0; i < (int)ARRAY_LENGTH(names); ++i)
{
int mustfree = FALSE;
#ifdef UNIX
if (*names[i] == NUL)
{
# ifdef MACOS_X
p = (char_u *)"/private/tmp";
plen = (int)STRLEN_LITERAL("/private/tmp");
# else
p = (char_u *)"/tmp";
plen = (int)STRLEN_LITERAL("/tmp");
# endif
}
else
#endif
{
p = vim_getenv((char_u *)names[i], &mustfree);
plen = 0; // will be calculated below
}
if (p != NULL && *p != NUL)
{
char_u *item;
size_t itemsize;
int has_trailing_path_sep = FALSE;
if (plen == 0)
{
// the value was retrieved from the environment
plen = (int)STRLEN(p);
// does the value include a trailing path separator?
if (after_pathsep(p, p + plen))
has_trailing_path_sep = TRUE;
}
// item size needs to be large enough to include "/*" and a trailing NUL
// note: the value (and therefore plen) may already include a path separator
itemsize = plen + (has_trailing_path_sep ? 0 : 1) + 2;
item = alloc(itemsize);
if (item != NULL)
{
// add a preceding comma as a separator after the first item
size_t itemseplen = (ga.ga_len == 0) ? 0 : 1;
size_t itemlen;
itemlen = vim_snprintf((char *)item, itemsize, "%s%s*", p, (has_trailing_path_sep) ? "" : PATHSEPSTR);
if (find_dup_item(ga.ga_data, item, itemlen, options[opt_idx].flags) == NULL
&& ga_grow(&ga, itemseplen + itemlen + 1) == OK)
{
ga.ga_len += vim_snprintf((char *)ga.ga_data + ga.ga_len,
itemseplen + itemlen + 1,
"%s%s", (itemseplen > 0) ? "," : "", item);
}
vim_free(item);
}
}
if (mustfree)
vim_free(p);
}
if (ga.ga_data != NULL)
{
set_string_default("bsk", ga.ga_data);
vim_free(ga.ga_data);
}
}
/*
* Initialize the 'maxmemtot' and 'maxmem' options to a default value.
* 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
*/
static void
set_init_default_maxmemtot(void)
{
int opt_idx;
long_u n;
opt_idx = findoption((char_u *)"maxmemtot");
if (opt_idx < 0)
return;
#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
#endif
{
#if defined(HAVE_AVAIL_MEM)
// Use amount of memory available at this moment.
n = (mch_avail_mem(FALSE) >> 1);
#elif defined(HAVE_TOTAL_MEM)
// Use amount of memory available to Vim.
n = (mch_total_mem(FALSE) >> 1);
#else
n = (0x7fffffff >> 11);
#endif
options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
opt_idx = findoption((char_u *)"maxmem");
if (opt_idx >= 0)
{
#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
|| (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
#endif
options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
}
}
}
/*
* Initialize the 'cdpath' option to a default value.
*/
static void
set_init_default_cdpath(void)
{
int opt_idx;
char_u *cdpath;
char_u *buf;
int i;
int j;
int mustfree = FALSE;
cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
if (cdpath == NULL)
return;
buf = alloc((STRLEN(cdpath) << 1) + 2);
if (buf != NULL)
{
buf[0] = ','; // start with ",", current dir first
j = 1;
for (i = 0; cdpath[i] != NUL; ++i)
{
if (vim_ispathlistsep(cdpath[i]))
buf[j++] = ',';
else
{
if (cdpath[i] == ' ' || cdpath[i] == ',')
buf[j++] = '\\';
buf[j++] = cdpath[i];
}
}
buf[j] = NUL;
opt_idx = findoption((char_u *)"cdpath");
if (opt_idx >= 0)
{
options[opt_idx].def_val[VI_DEFAULT] = buf;
options[opt_idx].flags |= P_DEF_ALLOCED;
}
else
vim_free(buf); // cannot happen
}
if (mustfree)
vim_free(cdpath);
}
/*
* Initialize the 'printencoding' option to a default value.
*/
static void
set_init_default_printencoding(void)
{
#if defined(FEAT_POSTSCRIPT) && \
(defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
// Set print encoding on platforms that don't default to latin1
set_string_default("penc",
# if defined(MSWIN)
(char_u *)"cp1252"
# elif defined(VMS)
(char_u *)"dec-mcs"
# elif defined(MAC)
(char_u *)"mac-roman"
# else // HPUX
(char_u *)"hp-roman8"
# endif
);
#endif
}
#ifdef FEAT_POSTSCRIPT
/*
* Initialize the 'printexpr' option to a default value.
*/
static void
set_init_default_printexpr(void)
{
// 'printexpr' must be allocated to be able to evaluate it.
set_string_default("pexpr",
# if defined(MSWIN)
(char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
# elif defined(VMS)
(char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
# else
(char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
# endif
);
}
#endif
#ifdef UNIX
/*
* Force restricted-mode on for "nologin" or "false" $SHELL
*/
static void
set_init_restricted_mode(void)
{
char_u *p;
p = get_isolated_shell_name();
if (p == NULL)
return;
if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
restricted = TRUE;
vim_free(p);
}
#endif
#ifdef CLEAN_RUNTIMEPATH
/*
* When Vim is started with the "--clean" argument, set the default value
* for the 'runtimepath' and 'packpath' options.
*/
static void
set_init_clean_rtp(void)
{
int opt_idx;
opt_idx = findoption((char_u *)"runtimepath");
if (opt_idx >= 0)
{
options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
}
opt_idx = findoption((char_u *)"packpath");
if (opt_idx < 0)
return;
options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
p_pp = (char_u *)CLEAN_RUNTIMEPATH;
}
#endif
#ifdef UNIX
/*
* Change 'runtimepath' and 'packdir' to '$XDG_CONFIG_HOME/vim' if the only
* vimrc found is located in '$XDG_CONFIG_HOME/vim/vimrc'.
* In case the '$XDG_CONFIG_HOME' variable is not set, '$HOME/.config' is used
* as a fallback as is defined in the XDG base dir specification:
* <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>
*/
static void
set_init_xdg_rtp(void)
{
int opt_idx;
int has_xdg_env = TRUE;
int should_free_xdg_dir = FALSE;
char_u *vimrc1 = NULL;
char_u *vimrc2 = NULL;
char_u *xdg_dir = NULL;
char_u *xdg_rtp = NULL;
char_u *vimrc_xdg = NULL;
// initialize chartab, so we can expand $HOME
(void)init_chartab();
vimrc1 = expand_env_save((char_u *)USR_VIMRC_FILE);
vimrc2 = expand_env_save((char_u *)USR_VIMRC_FILE2);
xdg_dir = mch_getenv("XDG_CONFIG_HOME");
if (!xdg_dir)
{
xdg_dir = expand_env_save((char_u *)"~/.config");
should_free_xdg_dir = TRUE;
has_xdg_env = FALSE;
}
vimrc_xdg = concat_fnames(xdg_dir, (char_u *)"vim/vimrc", TRUE);
if (file_is_readable(vimrc1) || file_is_readable(vimrc2) ||
!file_is_readable(vimrc_xdg))
goto theend;
xdg_rtp = has_xdg_env ? (char_u *)XDG_RUNTIMEPATH
: (char_u *)XDG_RUNTIMEPATH_FB;
if ((opt_idx = findoption((char_u *)"runtimepath")) < 0)
goto theend;
options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
p_rtp = xdg_rtp;
if ((opt_idx = findoption((char_u *)"packpath")) < 0)
goto theend;
options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
p_pp = xdg_rtp;
#if defined(XDG_VDIR) && defined(FEAT_SESSION)
if ((opt_idx = findoption((char_u *)"viewdir")) < 0)
goto theend;
options[opt_idx].def_val[VI_DEFAULT] = (char_u *)XDG_VDIR;
p_vdir = (char_u *)XDG_VDIR;
#endif
theend:
vim_free(vimrc1);
vim_free(vimrc2);
vim_free(vimrc_xdg);
if (should_free_xdg_dir)
vim_free(xdg_dir);
}
#endif
/*
* Expand environment variables and things like "~" for the defaults.
* If option_expand() returns non-NULL the variable is expanded. This can
* only happen for non-indirect options.
* Also set the default to the expanded value, so ":set" does not list
* them.
* Don't set the P_ALLOCED flag, because we don't want to free the
* default.
*/
static void
set_init_expand_env(void)
{
int opt_idx;
char_u *p;
for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
{
if ((options[opt_idx].flags & P_GETTEXT)
&& options[opt_idx].var != NULL)
p = (char_u *)_(*(char **)options[opt_idx].var);
else
p = option_expand(opt_idx, NULL);
if (p != NULL && (p = vim_strsave(p)) != NULL)
{
*(char_u **)options[opt_idx].var = p;
// VIMEXP
// Defaults for all expanded options are currently the same for Vi
// and Vim. When this changes, add some code here! Also need to
// split P_DEF_ALLOCED in two.
if (options[opt_idx].flags & P_DEF_ALLOCED)
vim_free(options[opt_idx].def_val[VI_DEFAULT]);
options[opt_idx].def_val[VI_DEFAULT] = p;
options[opt_idx].flags |= P_DEF_ALLOCED;
}
}
}
/*
* Initialize the 'LANG' environment variable to a default value.
*/
static void
set_init_lang_env(void)
{
#if defined(MSWIN) && defined(FEAT_GETTEXT)
// If $LANG isn't set, try to get a good value for it. This makes the
// right language be used automatically. Don't do this for English.
if (mch_getenv((char_u *)"LANG") == NULL)
{
char buf[20];
long_u n;
// Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
// LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
// only the first two.
n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
(LPTSTR)buf, 20);
if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
{
// There are a few exceptions (probably more)
if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
STRCPY(buf, "zh_TW");
else if (STRNICMP(buf, "chs", 3) == 0
|| STRNICMP(buf, "zhc", 3) == 0)
STRCPY(buf, "zh_CN");
else if (STRNICMP(buf, "jp", 2) == 0)
STRCPY(buf, "ja");
else
buf[2] = NUL; // truncate to two-letter code
vim_setenv((char_u *)"LANG", (char_u *)buf);
}
}
#elif defined(MACOS_CONVERT)
// Moved to os_mac_conv.c to avoid dependency problems.
mac_lang_init();
#endif
}
/*
* Initialize the 'encoding' option to a default value.
*/
static void
set_init_default_encoding(void)
{
char_u *p;
int opt_idx;
# if defined(MSWIN) || defined(__MVS__)
// MS-Windows has builtin support for conversion to and from Unicode, using
// "utf-8" for 'encoding' should work best for most users.
// z/OS built should default to UTF-8 mode as setlocale does not respect utf-8 environment variable locales
p = vim_strnsave((char_u *)ENC_DFLT, STRLEN_LITERAL(ENC_DFLT));
# else
// enc_locale() will try to find the encoding of the current locale.
// This works best for properly configured systems, old and new.
p = enc_locale();
# endif
if (p == NULL)
return;
// Try setting 'encoding' and check if the value is valid.
// If not, go back to the default encoding.
char_u *save_enc = p_enc;
p_enc = p;
if (STRCMP(p_enc, "gb18030") == 0)
{
// We don't support "gb18030", but "cp936" is a good substitute
// for practical purposes, thus use that. It's not an alias to
// still support conversion between gb18030 and utf-8.
p_enc = vim_strnsave((char_u *)"cp936", STRLEN_LITERAL("cp936"));
vim_free(p);
}
if (mb_init() == NULL)
{
opt_idx = findoption((char_u *)"encoding");
if (opt_idx >= 0)
{
options[opt_idx].def_val[VI_DEFAULT] = p_enc;
options[opt_idx].flags |= P_DEF_ALLOCED;
}
#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
{
// Adjust the default for 'isprint' and 'iskeyword' to match
// latin1. Also set the defaults for when 'nocompatible' is
// set.
set_string_option_direct((char_u *)"isp", -1,
ISP_LATIN1, OPT_FREE, SID_NONE);
set_string_option_direct((char_u *)"isk", -1,
ISK_LATIN1, OPT_FREE, SID_NONE);
opt_idx = findoption((char_u *)"isp");
if (opt_idx >= 0)
options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
opt_idx = findoption((char_u *)"isk");
if (opt_idx >= 0)
options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
(void)init_chartab();
}
#endif
#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
// Win32 console: When GetACP() returns a different value from
// GetConsoleCP() set 'termencoding'.
if (
# ifdef VIMDLL
(!gui.in_use && !gui.starting) &&
# endif
GetACP() != GetConsoleCP())
{
char buf[50];
size_t buflen;
// Win32 console: In ConPTY, GetConsoleCP() returns zero.
// Use an alternative value.
if (GetConsoleCP() == 0)
buflen = vim_snprintf(buf, sizeof(buf), "cp%ld", (long)GetACP());
else
buflen = vim_snprintf(buf, sizeof(buf), "cp%ld", (long)GetConsoleCP());
p_tenc = vim_strnsave((char_u *)buf, buflen);
if (p_tenc != NULL)
{
opt_idx = findoption((char_u *)"termencoding");
if (opt_idx >= 0)
{
options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
options[opt_idx].flags |= P_DEF_ALLOCED;
}
convert_setup(&input_conv, p_tenc, p_enc);
convert_setup(&output_conv, p_enc, p_tenc);
}
else
p_tenc = empty_option;
}
#endif
#if defined(MSWIN)
// $HOME may have characters in active code page.
init_homedir();
#endif
}
else
{
vim_free(p_enc);
p_enc = save_enc;
}
}
/*
* Initialize the options, first part.
*
* Called only once from main(), just after creating the first buffer.
* If "clean_arg" is TRUE Vim was started with --clean.
*/
void
set_init_1(int clean_arg)
{
#ifdef FEAT_LANGMAP
langmap_init();
#endif
// Be Vi compatible by default
p_cp = TRUE;
// Use POSIX compatibility when $VIM_POSIX is set.
if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
{
set_string_default("cpo", (char_u *)CPO_ALL);
set_string_default("shm", (char_u *)SHM_POSIX);
}
set_init_default_shell();
set_init_default_backupskip();
set_init_default_maxmemtot();
set_init_default_cdpath();
set_init_default_printencoding();
#ifdef FEAT_POSTSCRIPT
set_init_default_printexpr();
#endif
/*
* Set all the options (except the terminal options) to their default
* value. Also set the global value for local options.
*/
set_options_default(0);
#ifdef UNIX
set_init_xdg_rtp();
set_init_restricted_mode();
#endif
#ifdef CLEAN_RUNTIMEPATH
if (clean_arg)
set_init_clean_rtp();
#endif
#ifdef FEAT_GUI
if (found_reverse_arg)
set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
#endif
curbuf->b_p_initialized = TRUE;
curbuf->b_p_ar = -1; // no local 'autoread' value
curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
check_buf_options(curbuf);
check_win_options(curwin);
check_options();
// Must be before option_expand(), because that one needs vim_isIDc()
didset_options();
#ifdef FEAT_SPELL
// Use the current chartab for the generic chartab. This is not in
// didset_options() because it only depends on 'encoding'.
init_spell_chartab();
#endif
set_init_default_encoding();
// Expand environment variables and things like "~" for the defaults.
set_init_expand_env();
save_file_ff(curbuf); // Buffer is unchanged
#if defined(FEAT_ARABIC)
// Detect use of mlterm.
// Mlterm is a terminal emulator akin to xterm that has some special
// abilities (bidi namely).
// NOTE: mlterm's author is being asked to 'set' a variable
// instead of an environment variable due to inheritance.
if (mch_getenv((char_u *)"MLTERM") != NULL)
set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
#endif
didset_options2();
set_init_lang_env();
#ifdef FEAT_MULTI_LANG
// Set the default for 'helplang'.
set_helplang_default(get_mess_lang());
#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(void)
{
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!
*/
static void
set_option_default(
int opt_idx,
int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
int compatible) // use Vi default value
{
char_u *varp; // pointer to variable for current option
int dvi; // index in def_val[]
long_u flags;
long_u *flagsp;
int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
flags = options[opt_idx].flags;
if (varp != NULL) // skip hidden option, nothing to do for it
{
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.
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
{
if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
free_string_option(*(char_u **)(varp));
*(char_u **)varp = options[opt_idx].def_val[dvi];
options[opt_idx].flags &= ~P_ALLOCED;
}
}
else if (flags & P_NUM)
{
if (options[opt_idx].indir == PV_SCROLL)
win_comp_scroll(curwin);
else
{
long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
if ((long *)varp == &curwin->w_p_so
|| (long *)varp == &curwin->w_p_siso)
// 'scrolloff' and 'sidescrolloff' local values have a
// different default value than the global default.
*(long *)varp = -1;
else
*(long *)varp = def_val;
// May also set global value for local option.
if (both)
*(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
def_val;
}
}
else // P_BOOL
{
// the cast to long is required for Manx C, long_i is needed for
// MSVC
*(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
#ifdef UNIX
// 'modeline' defaults to off for root
if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
*(int *)varp = FALSE;
#endif
// May also set global value for local option.
if (both)
*(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
*(int *)varp;
}
// The default value is not insecure.
flagsp = insecure_flag(opt_idx, opt_flags);
*flagsp = *flagsp & ~P_INSECURE;
}
#ifdef FEAT_EVAL
set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
#endif
}
/*
* Set all options (except terminal options) to their default value.
* When "opt_flags" is non-zero skip 'encoding'.
*/
static void
set_options_default(
int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
{
int i;
win_T *wp;
tabpage_T *tp;
for (i = 0; !istermoption_idx(i); i++)
if (!(options[i].flags & P_NODEFAULT)
&& (opt_flags == 0
|| (options[i].var != (char_u *)&p_enc
# if defined(FEAT_CRYPT)
&& options[i].var != (char_u *)&p_cm
&& options[i].var != (char_u *)&p_key
# endif
)))
set_option_default(i, opt_flags, p_cp);
// The 'scroll' option must be computed for all windows.
FOR_ALL_TAB_WINDOWS(tp, wp)
win_comp_scroll(wp);
parse_cino(curbuf);
}
/*
* Set the Vi-default value of a string option.
* Used for 'sh', 'backupskip' and 'term'.
* When "escape" is TRUE escape spaces with a backslash.
*/
static void
set_string_default_esc(char *name, char_u *val, int escape)
{
char_u *p;
int opt_idx;
if (escape && vim_strchr(val, ' ') != NULL)
p = vim_strsave_escaped(val, (char_u *)" ");
else
p = vim_strsave(val);
if (p == NULL) // we don't want a NULL
return;
opt_idx = findoption((char_u *)name);
if (opt_idx < 0)
{
vim_free(p);
return;
}
if (options[opt_idx].flags & P_DEF_ALLOCED)
vim_free(options[opt_idx].def_val[VI_DEFAULT]);
options[opt_idx].def_val[VI_DEFAULT] = p;
options[opt_idx].flags |= P_DEF_ALLOCED;
}
void
set_string_default(char *name, char_u *val)
{
set_string_default_esc(name, val, FALSE);
}
/*
* For an option value that contains comma separated items, find "newval" in
* "origval". Return NULL if not found.
*/
static char_u *
find_dup_item(char_u *origval, char_u *newval, size_t newvallen, long_u flags)
{
int bs = 0;
char_u *s;
if (origval == NULL)
return NULL;
for (s = origval; *s != NUL; ++s)
{
if ((!(flags & P_COMMA)
|| s == origval
|| (s[-1] == ',' && !(bs & 1)))
&& STRNCMP(s, newval, newvallen) == 0
&& (!(flags & P_COMMA)
|| s[newvallen] == ','
|| s[newvallen] == NUL))
return s;
// Count backslashes. Only a comma with an even number of backslashes
// or a single backslash preceded by a comma before it is recognized as
// a separator.
if ((s > origval + 1
&& s[-1] == '\\'
&& s[-2] != ',')
|| (s == origval + 1
&& s[-1] == '\\'))
++bs;
else
bs = 0;
}
return NULL;
}
/*
* Set the Vi-default value of a number option.
* Used for 'lines' and 'columns'.
*/
void
set_number_default(char *name, long val)
{
int opt_idx;
opt_idx = findoption((char_u *)name);
if (opt_idx >= 0)
options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
}
#if defined(FEAT_PROP_POPUP) || defined(PROTO)
/*
* Set all window-local and buffer-local options to the Vim default.
* local-global options will use the global value.
* When "do_buffer" is FALSE don't set buffer-local options.
*/
void
set_local_options_default(win_T *wp, int do_buffer)
{
win_T *save_curwin = curwin;
int i;
curwin = wp;
curbuf = curwin->w_buffer;
block_autocmds();
for (i = 0; !istermoption_idx(i); i++)
{
struct vimoption *p = &(options[i]);
char_u *varp = get_varp_scope(p, OPT_LOCAL);
if (p->indir != PV_NONE
&& (do_buffer || (p->indir & PV_BUF) == 0)
&& !(options[i].flags & P_NODEFAULT)
&& !optval_default(p, varp, FALSE))
set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
}
unblock_autocmds();
curwin = save_curwin;
curbuf = curwin->w_buffer;
}
#endif
#if defined(EXITFREE) || defined(PROTO)
/*
* Free all options.
*/
void
free_all_options(void)
{
int i;
for (i = 0; !istermoption_idx(i); i++)