/
option.c
7173 lines (6681 loc) · 176 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 below (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 entry to the enum below.
* - Add a variable to the window or buffer struct in structs.h.
* - For a window option, add some code to copy_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 do_set().
* - If it's a list of flags, add some code in do_set(), 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, 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 opt_flags);
static char_u *get_varp(struct vimoption *);
static void check_win_options(win_T *win);
static void option_value2string(struct vimoption *, int opt_flags);
static void check_winopt(winopt_T *wop);
static int wc_use_keyname(char_u *varp, long *wcp);
static void paste_option_changed(void);
static void compatible_set(void);
/*
* 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)
{
char_u *p;
int opt_idx;
long_u n;
#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);
}
/*
* 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
#ifdef FEAT_WILDIGN
/*
* Set the default for 'backupskip' to include environment variables for
* temp files.
*/
{
# ifdef UNIX
static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
# else
static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
# endif
int len;
garray_T ga;
int mustfree;
char_u *item;
opt_idx = findoption((char_u *)"backupskip");
ga_init2(&ga, 1, 100);
for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
{
mustfree = FALSE;
# ifdef UNIX
if (*names[n] == NUL)
# ifdef MACOS_X
p = (char_u *)"/private/tmp";
# else
p = (char_u *)"/tmp";
# endif
else
# endif
p = vim_getenv((char_u *)names[n], &mustfree);
if (p != NULL && *p != NUL)
{
// First time count the NUL, otherwise count the ','.
len = (int)STRLEN(p) + 3;
item = alloc(len);
STRCPY(item, p);
add_pathsep(item);
STRCAT(item, "*");
if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
== NULL
&& ga_grow(&ga, len) == OK)
{
if (ga.ga_len > 0)
STRCAT(ga.ga_data, ",");
STRCAT(ga.ga_data, item);
ga.ga_len += len;
}
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);
}
}
#endif
/*
* 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
*/
opt_idx = findoption((char_u *)"maxmemtot");
if (opt_idx >= 0)
{
#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
#endif
{
#ifdef HAVE_AVAIL_MEM
// Use amount of memory available at this moment.
n = (mch_avail_mem(FALSE) >> 1);
#else
# ifdef HAVE_TOTAL_MEM
// Use amount of memory available to Vim.
n = (mch_total_mem(FALSE) >> 1);
# else
n = (0x7fffffff >> 11);
# endif
#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;
}
}
}
#ifdef FEAT_SEARCHPATH
{
char_u *cdpath;
char_u *buf;
int i;
int j;
int mustfree = FALSE;
// Initialize the 'cdpath' option's default value.
cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
if (cdpath != NULL)
{
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);
}
}
#endif
#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || 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"
# else
# ifdef VMS
(char_u *)"dec-mcs"
# else
# ifdef EBCDIC
(char_u *)"ebcdic-uk"
# else
# ifdef MAC
(char_u *)"mac-roman"
# else // HPUX
(char_u *)"hp-roman8"
# endif
# endif
# endif
# endif
);
#endif
#ifdef FEAT_POSTSCRIPT
// '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)"
# else
# ifdef 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
);
#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 CLEAN_RUNTIMEPATH
if (clean_arg)
{
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)
{
options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
p_pp = (char_u *)CLEAN_RUNTIMEPATH;
}
}
#endif
#ifdef FEAT_GUI
if (found_reverse_arg)
set_option_value((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
/*
* 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.
*/
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;
}
}
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((char_u *)"tbidi", 1L, NULL, 0);
#endif
didset_options2();
# 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];
// 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);
}
}
# else
# ifdef MACOS_CONVERT
// Moved to os_mac_conv.c to avoid dependency problems.
mac_lang_init();
# endif
# endif
# ifdef MSWIN
// MS-Windows has builtin support for conversion to and from Unicode, using
// "utf-8" for 'encoding' should work best for most users.
p = vim_strsave((char_u *)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)
{
char_u *save_enc;
// Try setting 'encoding' and check if the value is valid.
// If not, go back to the default encoding.
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_strsave((char_u *)"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];
// Win32 console: In ConPTY, GetConsoleCP() returns zero.
// Use an alternative value.
if (GetConsoleCP() == 0)
sprintf(buf, "cp%ld", (long)GetACP());
else
sprintf(buf, "cp%ld", (long)GetConsoleCP());
p_tenc = vim_strsave((char_u *)buf);
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;
}
}
#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()
{
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);
#ifdef FEAT_CINDENT
parse_cino(curbuf);
#endif
}
/*
* 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
{
opt_idx = findoption((char_u *)name);
if (opt_idx >= 0)
{
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, long_u flags)
{
int bs = 0;
size_t newlen;
char_u *s;
if (origval == NULL)
return NULL;
newlen = STRLEN(newval);
for (s = origval; *s != NUL; ++s)
{
if ((!(flags & P_COMMA)
|| s == origval
|| (s[-1] == ',' && !(bs & 1)))
&& STRNCMP(s, newval, newlen) == 0
&& (!(flags & P_COMMA)
|| s[newlen] == ','
|| s[newlen] == 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;
}
/*
* 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;
}
#if defined(EXITFREE) || defined(PROTO)
/*
* Free all options.
*/
void
free_all_options(void)
{
int i;
for (i = 0; !istermoption_idx(i); i++)
{
if (options[i].indir == PV_NONE)
{
// global option: free value and default value.
if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
free_string_option(*(char_u **)options[i].var);
if (options[i].flags & P_DEF_ALLOCED)
free_string_option(options[i].def_val[VI_DEFAULT]);
}
else if (options[i].var != VAR_WIN
&& (options[i].flags & P_STRING))
// buffer-local option: free global value
clear_string_option((char_u **)options[i].var);
}
}
#endif
/*
* Initialize the options, part two: After getting Rows and Columns and
* setting 'term'.
*/
void
set_init_2(void)
{
int idx;
/*
* 'scroll' defaults to half the window height. The stored default is zero,
* which results in the actual value computed from the window height.
*/
idx = findoption((char_u *)"scroll");
if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
set_option_default(idx, OPT_LOCAL, p_cp);
comp_col();
/*
* 'window' is only for backwards compatibility with Vi.
* Default is Rows - 1.
*/
if (!option_was_set((char_u *)"window"))
p_window = Rows - 1;
set_number_default("window", Rows - 1);
// For DOS console the default is always black.
#if !((defined(MSWIN)) && !defined(FEAT_GUI))
/*
* If 'background' wasn't set by the user, try guessing the value,
* depending on the terminal name. Only need to check for terminals
* with a dark background, that can handle color.
*/
idx = findoption((char_u *)"bg");
if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
&& *term_bg_default() == 'd')
{
set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
// don't mark it as set, when starting the GUI it may be
// changed again
options[idx].flags &= ~P_WAS_SET;
}
#endif
#ifdef CURSOR_SHAPE
parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
#endif
#ifdef FEAT_MOUSESHAPE
parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
#endif
#ifdef FEAT_PRINTER
(void)parse_printoptions(); // parse 'printoptions' default value
#endif
}
/*
* Initialize the options, part three: After reading the .vimrc
*/
void
set_init_3(void)
{
#if defined(UNIX) || defined(MSWIN)
/*
* Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
* This is done after other initializations, where 'shell' might have been
* set, but only if they have not been set before.
*/
char_u *p;
int idx_srr;
int do_srr;
# ifdef FEAT_QUICKFIX
int idx_sp;
int do_sp;
# endif
idx_srr = findoption((char_u *)"srr");
if (idx_srr < 0)
do_srr = FALSE;
else
do_srr = !(options[idx_srr].flags & P_WAS_SET);
# ifdef FEAT_QUICKFIX
idx_sp = findoption((char_u *)"sp");
if (idx_sp < 0)
do_sp = FALSE;
else
do_sp = !(options[idx_sp].flags & P_WAS_SET);
# endif
p = get_isolated_shell_name();
if (p != NULL)
{
/*
* Default for p_sp is "| tee", for p_srr is ">".
* For known shells it is changed here to include stderr.
*/
if ( fnamecmp(p, "csh") == 0
|| fnamecmp(p, "tcsh") == 0
# if defined(MSWIN) // also check with .exe extension
|| fnamecmp(p, "csh.exe") == 0
|| fnamecmp(p, "tcsh.exe") == 0
# endif
)
{
# if defined(FEAT_QUICKFIX)
if (do_sp)
{
# ifdef MSWIN
p_sp = (char_u *)">&";
# else
p_sp = (char_u *)"|& tee";
# endif
options[idx_sp].def_val[VI_DEFAULT] = p_sp;
}
# endif
if (do_srr)
{
p_srr = (char_u *)">&";
options[idx_srr].def_val[VI_DEFAULT] = p_srr;
}
}
# ifdef MSWIN
// Windows PowerShell output is UTF-16 with BOM so re-encode to the
// current codepage.
else if ( fnamecmp(p, "powershell") == 0
|| fnamecmp(p, "powershell.exe") == 0
)
{
# if defined(FEAT_QUICKFIX)
if (do_sp)
{
p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
options[idx_sp].def_val[VI_DEFAULT] = p_sp;
}
# endif
if (do_srr)
{
p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
options[idx_srr].def_val[VI_DEFAULT] = p_srr;
}
}
#endif
else
// Always use POSIX shell style redirection if we reach this
if ( fnamecmp(p, "sh") == 0
|| fnamecmp(p, "ksh") == 0
|| fnamecmp(p, "mksh") == 0
|| fnamecmp(p, "pdksh") == 0
|| fnamecmp(p, "zsh") == 0
|| fnamecmp(p, "zsh-beta") == 0
|| fnamecmp(p, "bash") == 0
|| fnamecmp(p, "fish") == 0
|| fnamecmp(p, "ash") == 0
|| fnamecmp(p, "dash") == 0
|| fnamecmp(p, "pwsh") == 0
# ifdef MSWIN
|| fnamecmp(p, "cmd") == 0
|| fnamecmp(p, "sh.exe") == 0
|| fnamecmp(p, "ksh.exe") == 0
|| fnamecmp(p, "mksh.exe") == 0
|| fnamecmp(p, "pdksh.exe") == 0
|| fnamecmp(p, "zsh.exe") == 0
|| fnamecmp(p, "zsh-beta.exe") == 0
|| fnamecmp(p, "bash.exe") == 0
|| fnamecmp(p, "cmd.exe") == 0
|| fnamecmp(p, "dash.exe") == 0
|| fnamecmp(p, "pwsh.exe") == 0
# endif
)
{
# if defined(FEAT_QUICKFIX)
if (do_sp)
{
# ifdef MSWIN
p_sp = (char_u *)">%s 2>&1";
# else
if (fnamecmp(p, "pwsh") == 0)
p_sp = (char_u *)">%s 2>&1";
else
p_sp = (char_u *)"2>&1| tee";
# endif
options[idx_sp].def_val[VI_DEFAULT] = p_sp;
}
# endif
if (do_srr)
{
p_srr = (char_u *)">%s 2>&1";