Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Code to handle user-settable options. This is all pretty much table- |
| 12 | * driven. Checklist for adding a new option: |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 13 | * - Put it in the options array in optiondefs.h (copy an existing entry). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 14 | * - For a global option: Add a variable for it in option.h. |
| 15 | * - For a buffer or window local option: |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 16 | * - Add a PV_XX macro definition to the optiondefs.h file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | * - Add a variable to the window or buffer struct in structs.h. |
| 18 | * - For a window option, add some code to copy_winopt(). |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 19 | * - For a window string option, add code to check_win_options() and |
| 20 | * clear_winopt(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | * - For a buffer option, add some code to buf_copy_options(). |
| 22 | * - For a buffer string option, add code to check_buf_options(). |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 23 | * - If it's a numeric option, add any necessary bounds checks to |
| 24 | * set_num_option(). |
| 25 | * - If it's a list of flags, add some code in did_set_string_option(), search |
| 26 | * for WW_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | * - When adding an option with expansion (P_EXPAND), but with a different |
| 28 | * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP. |
Bram Moolenaar | cea912a | 2016-10-12 14:20:24 +0200 | [diff] [blame] | 29 | * - Add documentation! One line in doc/quickref.txt, full description in |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | * options.txt, and any other related places. |
| 31 | * - Add an entry in runtime/optwin.vim. |
| 32 | * When making changes: |
| 33 | * - Adjust the help for the option in doc/option.txt. |
| 34 | * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a |
| 35 | * comment at the help for the 'compatible' option. |
| 36 | */ |
| 37 | |
| 38 | #define IN_OPTION_C |
| 39 | #include "vim.h" |
Bram Moolenaar | 0eddca4 | 2019-09-12 22:26:43 +0200 | [diff] [blame] | 40 | #include "optiondefs.h" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 42 | static void set_options_default(int opt_flags); |
Bram Moolenaar | 4bfa8af | 2018-02-03 15:14:46 +0100 | [diff] [blame] | 43 | static void set_string_default_esc(char *name, char_u *val, int escape); |
Bram Moolenaar | b00ef05 | 2020-09-12 14:53:53 +0200 | [diff] [blame] | 44 | static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 45 | static char_u *option_expand(int opt_idx, char_u *val); |
| 46 | static void didset_options(void); |
| 47 | static void didset_options2(void); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 48 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 49 | static long_u *insecure_flag(int opt_idx, int opt_flags); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 50 | #else |
| 51 | # define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags) |
| 52 | #endif |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 53 | static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags); |
| 54 | static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags); |
Bram Moolenaar | 9cf4b50 | 2018-07-23 04:12:03 +0200 | [diff] [blame] | 55 | static int find_key_option(char_u *arg_arg, int has_lt); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 56 | static void showoptions(int all, int opt_flags); |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 57 | static int optval_default(struct vimoption *, char_u *varp, int compatible); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 58 | static void showoneopt(struct vimoption *, int opt_flags); |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 59 | static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 60 | static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep); |
| 61 | static int put_setbool(FILE *fd, char *cmd, char *name, int value); |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 62 | static int istermoption(struct vimoption *p); |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 63 | static char_u *get_varp_scope(struct vimoption *p, int scope); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 64 | static char_u *get_varp(struct vimoption *); |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 65 | static void check_win_options(win_T *win); |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 66 | static void option_value2string(struct vimoption *, int scope); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 67 | static void check_winopt(winopt_T *wop); |
| 68 | static int wc_use_keyname(char_u *varp, long *wcp); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 69 | static void compatible_set(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | |
Christian Brabandt | 757593c | 2023-08-22 21:44:10 +0200 | [diff] [blame] | 71 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 72 | static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL}; |
| 73 | static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab", |
| 74 | "softtabstop", "textwidth", "wrapmargin", |
| 75 | #ifdef FEAT_RIGHTLEFT |
| 76 | "hkmap", "revins", |
| 77 | #endif |
| 78 | #ifdef FEAT_VARTABS |
| 79 | "varsofttabstop", |
| 80 | #endif |
| 81 | NULL}; |
| 82 | static void didset_options_sctx(int opt_flags, char **buf); |
| 83 | #endif |
| 84 | |
| 85 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 86 | /* |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 87 | * Initialize the 'shell' option to a default value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | */ |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 89 | static void |
| 90 | set_init_default_shell(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | { |
| 92 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 93 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 94 | // Find default value for 'shell' option. |
| 95 | // Don't use it if it is empty. |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 96 | if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 97 | #if defined(MSWIN) |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 98 | || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 99 | || ((p = (char_u *)default_shell()) != NULL && *p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 100 | #endif |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 101 | ) |
Bram Moolenaar | 2efc44b | 2019-10-05 12:09:32 +0200 | [diff] [blame] | 102 | #if defined(MSWIN) |
| 103 | { |
| 104 | // For MS-Windows put the path in quotes instead of escaping spaces. |
| 105 | char_u *cmd; |
| 106 | size_t len; |
| 107 | |
| 108 | if (vim_strchr(p, ' ') != NULL) |
| 109 | { |
| 110 | len = STRLEN(p) + 3; // two quotes and a trailing NUL |
| 111 | cmd = alloc(len); |
Bram Moolenaar | 1671de3 | 2019-10-05 21:35:16 +0200 | [diff] [blame] | 112 | if (cmd != NULL) |
| 113 | { |
| 114 | vim_snprintf((char *)cmd, len, "\"%s\"", p); |
| 115 | set_string_default("sh", cmd); |
| 116 | vim_free(cmd); |
| 117 | } |
Bram Moolenaar | 2efc44b | 2019-10-05 12:09:32 +0200 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | set_string_default("sh", p); |
| 121 | } |
| 122 | #else |
Bram Moolenaar | 4bfa8af | 2018-02-03 15:14:46 +0100 | [diff] [blame] | 123 | set_string_default_esc("sh", p, TRUE); |
Bram Moolenaar | 2efc44b | 2019-10-05 12:09:32 +0200 | [diff] [blame] | 124 | #endif |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 125 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 126 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 127 | /* |
| 128 | * Set the default for 'backupskip' to include environment variables for |
| 129 | * temp files. |
| 130 | */ |
| 131 | static void |
| 132 | set_init_default_backupskip(void) |
| 133 | { |
| 134 | int opt_idx; |
| 135 | long_u n; |
| 136 | char_u *p; |
Bram Moolenaar | 074fbd4 | 2022-08-26 16:41:14 +0100 | [diff] [blame] | 137 | #ifdef UNIX |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 138 | static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"}; |
Bram Moolenaar | 074fbd4 | 2022-08-26 16:41:14 +0100 | [diff] [blame] | 139 | #else |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 140 | static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"}; |
Bram Moolenaar | 074fbd4 | 2022-08-26 16:41:14 +0100 | [diff] [blame] | 141 | #endif |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 142 | int len; |
| 143 | garray_T ga; |
Bram Moolenaar | 1433802 | 2023-03-15 22:05:44 +0000 | [diff] [blame] | 144 | char_u *item; |
Bram Moolenaar | b00ef05 | 2020-09-12 14:53:53 +0200 | [diff] [blame] | 145 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 146 | opt_idx = findoption((char_u *)"backupskip"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 147 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 148 | ga_init2(&ga, 1, 100); |
| 149 | for (n = 0; n < (long)ARRAY_LENGTH(names); ++n) |
| 150 | { |
Bram Moolenaar | 1433802 | 2023-03-15 22:05:44 +0000 | [diff] [blame] | 151 | int mustfree = FALSE; |
Bram Moolenaar | 074fbd4 | 2022-08-26 16:41:14 +0100 | [diff] [blame] | 152 | #ifdef UNIX |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 153 | if (*names[n] == NUL) |
Bram Moolenaar | 074fbd4 | 2022-08-26 16:41:14 +0100 | [diff] [blame] | 154 | # ifdef MACOS_X |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 155 | p = (char_u *)"/private/tmp"; |
Bram Moolenaar | 074fbd4 | 2022-08-26 16:41:14 +0100 | [diff] [blame] | 156 | # else |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 157 | p = (char_u *)"/tmp"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 158 | # endif |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 159 | else |
Bram Moolenaar | 074fbd4 | 2022-08-26 16:41:14 +0100 | [diff] [blame] | 160 | #endif |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 161 | p = vim_getenv((char_u *)names[n], &mustfree); |
| 162 | if (p != NULL && *p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | { |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 164 | // First time count the NUL, otherwise count the ','. |
| 165 | len = (int)STRLEN(p) + 3; |
| 166 | item = alloc(len); |
Bram Moolenaar | 1433802 | 2023-03-15 22:05:44 +0000 | [diff] [blame] | 167 | if (item != NULL) |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 168 | { |
Bram Moolenaar | 1433802 | 2023-03-15 22:05:44 +0000 | [diff] [blame] | 169 | STRCPY(item, p); |
| 170 | add_pathsep(item); |
| 171 | STRCAT(item, "*"); |
| 172 | if (find_dup_item(ga.ga_data, item, options[opt_idx].flags) |
| 173 | == NULL |
| 174 | && ga_grow(&ga, len) == OK) |
| 175 | { |
| 176 | if (ga.ga_len > 0) |
| 177 | STRCAT(ga.ga_data, ","); |
| 178 | STRCAT(ga.ga_data, item); |
| 179 | ga.ga_len += len; |
| 180 | } |
| 181 | vim_free(item); |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 182 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 183 | } |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 184 | if (mustfree) |
| 185 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 186 | } |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 187 | if (ga.ga_data != NULL) |
| 188 | { |
| 189 | set_string_default("bsk", ga.ga_data); |
| 190 | vim_free(ga.ga_data); |
| 191 | } |
| 192 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 194 | /* |
| 195 | * Initialize the 'maxmemtot' and 'maxmem' options to a default value. |
| 196 | * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory. |
| 197 | */ |
| 198 | static void |
| 199 | set_init_default_maxmemtot(void) |
| 200 | { |
| 201 | int opt_idx; |
| 202 | long_u n; |
| 203 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 204 | opt_idx = findoption((char_u *)"maxmemtot"); |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 205 | if (opt_idx < 0) |
| 206 | return; |
| 207 | |
| 208 | #if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM) |
| 209 | if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L) |
| 210 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 211 | { |
ichizok | 0256042 | 2022-04-05 14:18:44 +0100 | [diff] [blame] | 212 | #if defined(HAVE_AVAIL_MEM) |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 213 | // Use amount of memory available at this moment. |
| 214 | n = (mch_avail_mem(FALSE) >> 1); |
ichizok | 0256042 | 2022-04-05 14:18:44 +0100 | [diff] [blame] | 215 | #elif defined(HAVE_TOTAL_MEM) |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 216 | // Use amount of memory available to Vim. |
| 217 | n = (mch_total_mem(FALSE) >> 1); |
ichizok | 0256042 | 2022-04-05 14:18:44 +0100 | [diff] [blame] | 218 | #else |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 219 | n = (0x7fffffff >> 11); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 220 | #endif |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 221 | options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n; |
| 222 | opt_idx = findoption((char_u *)"maxmem"); |
| 223 | if (opt_idx >= 0) |
| 224 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 225 | #if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM) |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 226 | if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n |
| 227 | || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L) |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 228 | #endif |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 229 | options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n; |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 230 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 231 | } |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 232 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 233 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 234 | /* |
| 235 | * Initialize the 'cdpath' option to a default value. |
| 236 | */ |
| 237 | static void |
| 238 | set_init_default_cdpath(void) |
| 239 | { |
| 240 | int opt_idx; |
| 241 | char_u *cdpath; |
| 242 | char_u *buf; |
| 243 | int i; |
| 244 | int j; |
| 245 | int mustfree = FALSE; |
| 246 | |
| 247 | cdpath = vim_getenv((char_u *)"CDPATH", &mustfree); |
| 248 | if (cdpath == NULL) |
| 249 | return; |
| 250 | |
| 251 | buf = alloc((STRLEN(cdpath) << 1) + 2); |
| 252 | if (buf != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 253 | { |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 254 | buf[0] = ','; // start with ",", current dir first |
| 255 | j = 1; |
| 256 | for (i = 0; cdpath[i] != NUL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 257 | { |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 258 | if (vim_ispathlistsep(cdpath[i])) |
| 259 | buf[j++] = ','; |
| 260 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 261 | { |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 262 | if (cdpath[i] == ' ' || cdpath[i] == ',') |
| 263 | buf[j++] = '\\'; |
| 264 | buf[j++] = cdpath[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 267 | buf[j] = NUL; |
| 268 | opt_idx = findoption((char_u *)"cdpath"); |
| 269 | if (opt_idx >= 0) |
| 270 | { |
| 271 | options[opt_idx].def_val[VI_DEFAULT] = buf; |
| 272 | options[opt_idx].flags |= P_DEF_ALLOCED; |
| 273 | } |
| 274 | else |
| 275 | vim_free(buf); // cannot happen |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 276 | } |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 277 | if (mustfree) |
| 278 | vim_free(cdpath); |
| 279 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 280 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 281 | /* |
| 282 | * Initialize the 'printencoding' option to a default value. |
| 283 | */ |
| 284 | static void |
| 285 | set_init_default_printencoding(void) |
| 286 | { |
ichizok | 0256042 | 2022-04-05 14:18:44 +0100 | [diff] [blame] | 287 | #if defined(FEAT_POSTSCRIPT) && \ |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 288 | (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux)) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 289 | // Set print encoding on platforms that don't default to latin1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 290 | set_string_default("penc", |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 291 | # if defined(MSWIN) |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 292 | (char_u *)"cp1252" |
ichizok | 0256042 | 2022-04-05 14:18:44 +0100 | [diff] [blame] | 293 | # elif defined(VMS) |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 294 | (char_u *)"dec-mcs" |
ichizok | 0256042 | 2022-04-05 14:18:44 +0100 | [diff] [blame] | 295 | # elif defined(MAC) |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 296 | (char_u *)"mac-roman" |
ichizok | 0256042 | 2022-04-05 14:18:44 +0100 | [diff] [blame] | 297 | # else // HPUX |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 298 | (char_u *)"hp-roman8" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 299 | # endif |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 300 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 301 | #endif |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 302 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 303 | |
| 304 | #ifdef FEAT_POSTSCRIPT |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 305 | /* |
| 306 | * Initialize the 'printexpr' option to a default value. |
| 307 | */ |
| 308 | static void |
| 309 | set_init_default_printexpr(void) |
| 310 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 311 | // 'printexpr' must be allocated to be able to evaluate it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 312 | set_string_default("pexpr", |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 313 | # if defined(MSWIN) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 314 | (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)" |
ichizok | 0256042 | 2022-04-05 14:18:44 +0100 | [diff] [blame] | 315 | # elif defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 316 | (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)" |
| 317 | |
ichizok | 0256042 | 2022-04-05 14:18:44 +0100 | [diff] [blame] | 318 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 319 | (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 320 | # endif |
| 321 | ); |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 322 | } |
| 323 | #endif |
| 324 | |
| 325 | #ifdef UNIX |
| 326 | /* |
| 327 | * Force restricted-mode on for "nologin" or "false" $SHELL |
| 328 | */ |
| 329 | static void |
| 330 | set_init_restricted_mode(void) |
| 331 | { |
| 332 | char_u *p; |
| 333 | |
| 334 | p = get_isolated_shell_name(); |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 335 | if (p == NULL) |
| 336 | return; |
| 337 | if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0) |
| 338 | restricted = TRUE; |
| 339 | vim_free(p); |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 340 | } |
| 341 | #endif |
| 342 | |
| 343 | #ifdef CLEAN_RUNTIMEPATH |
| 344 | /* |
| 345 | * When Vim is started with the "--clean" argument, set the default value |
| 346 | * for the 'runtimepath' and 'packpath' options. |
| 347 | */ |
| 348 | static void |
| 349 | set_init_clean_rtp(void) |
| 350 | { |
| 351 | int opt_idx; |
| 352 | |
| 353 | opt_idx = findoption((char_u *)"runtimepath"); |
| 354 | if (opt_idx >= 0) |
| 355 | { |
| 356 | options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH; |
| 357 | p_rtp = (char_u *)CLEAN_RUNTIMEPATH; |
| 358 | } |
| 359 | opt_idx = findoption((char_u *)"packpath"); |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 360 | if (opt_idx < 0) |
| 361 | return; |
| 362 | options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH; |
| 363 | p_pp = (char_u *)CLEAN_RUNTIMEPATH; |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 364 | } |
| 365 | #endif |
| 366 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 367 | #ifdef UNIX |
| 368 | /* |
| 369 | * Change 'runtimepath' and 'packdir' to '$XDG_CONFIG_HOME/vim' if the only |
| 370 | * vimrc found is located in '$XDG_CONFIG_HOME/vim/vimrc'. |
| 371 | * In case the '$XDG_CONFIG_HOME' variable is not set, '$HOME/.config' is used |
| 372 | * as a fallback as is defined in the XDG base dir specification: |
| 373 | * <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html> |
| 374 | */ |
| 375 | static void |
| 376 | set_init_xdg_rtp(void) |
| 377 | { |
| 378 | int opt_idx; |
| 379 | int has_xdg_env = TRUE; |
| 380 | int should_free_xdg_dir = FALSE; |
| 381 | char_u *vimrc1 = NULL; |
| 382 | char_u *vimrc2 = NULL; |
| 383 | char_u *xdg_dir = NULL; |
| 384 | char_u *xdg_rtp = NULL; |
| 385 | char_u *vimrc_xdg = NULL; |
| 386 | |
Christian Brabandt | b09fa35 | 2024-04-21 14:52:20 +0200 | [diff] [blame] | 387 | // initialize chartab, so we can expand $HOME |
| 388 | (void)init_chartab(); |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 389 | vimrc1 = expand_env_save((char_u *)USR_VIMRC_FILE); |
| 390 | vimrc2 = expand_env_save((char_u *)USR_VIMRC_FILE2); |
| 391 | |
| 392 | xdg_dir = mch_getenv("XDG_CONFIG_HOME"); |
| 393 | if (!xdg_dir) |
| 394 | { |
| 395 | xdg_dir = expand_env_save((char_u *)"~/.config"); |
| 396 | should_free_xdg_dir = TRUE; |
| 397 | has_xdg_env = FALSE; |
| 398 | } |
| 399 | vimrc_xdg = concat_fnames(xdg_dir, (char_u *)"vim/vimrc", TRUE); |
| 400 | |
| 401 | if (file_is_readable(vimrc1) || file_is_readable(vimrc2) || |
| 402 | !file_is_readable(vimrc_xdg)) |
| 403 | goto theend; |
| 404 | |
| 405 | xdg_rtp = has_xdg_env ? (char_u *)XDG_RUNTIMEPATH |
| 406 | : (char_u *)XDG_RUNTIMEPATH_FB; |
| 407 | |
| 408 | if ((opt_idx = findoption((char_u *)"runtimepath")) < 0) |
| 409 | goto theend; |
| 410 | |
| 411 | options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp; |
| 412 | p_rtp = xdg_rtp; |
| 413 | |
| 414 | if ((opt_idx = findoption((char_u *)"packpath")) < 0) |
| 415 | goto theend; |
| 416 | |
| 417 | options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp; |
| 418 | p_pp = xdg_rtp; |
| 419 | |
Christian Brabandt | c3e6e39 | 2024-05-04 09:48:15 +0200 | [diff] [blame] | 420 | #if defined(XDG_VDIR) && defined(FEAT_SESSION) |
| 421 | if ((opt_idx = findoption((char_u *)"viewdir")) < 0) |
| 422 | goto theend; |
| 423 | |
| 424 | options[opt_idx].def_val[VI_DEFAULT] = (char_u *)XDG_VDIR; |
| 425 | p_vdir = (char_u *)XDG_VDIR; |
| 426 | #endif |
| 427 | |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 428 | theend: |
| 429 | vim_free(vimrc1); |
| 430 | vim_free(vimrc2); |
| 431 | vim_free(vimrc_xdg); |
| 432 | if (should_free_xdg_dir) |
| 433 | vim_free(xdg_dir); |
| 434 | } |
| 435 | #endif |
| 436 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 437 | /* |
| 438 | * Expand environment variables and things like "~" for the defaults. |
| 439 | * If option_expand() returns non-NULL the variable is expanded. This can |
| 440 | * only happen for non-indirect options. |
| 441 | * Also set the default to the expanded value, so ":set" does not list |
| 442 | * them. |
| 443 | * Don't set the P_ALLOCED flag, because we don't want to free the |
| 444 | * default. |
| 445 | */ |
| 446 | static void |
| 447 | set_init_expand_env(void) |
| 448 | { |
| 449 | int opt_idx; |
| 450 | char_u *p; |
| 451 | |
| 452 | for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++) |
| 453 | { |
| 454 | if ((options[opt_idx].flags & P_GETTEXT) |
| 455 | && options[opt_idx].var != NULL) |
| 456 | p = (char_u *)_(*(char **)options[opt_idx].var); |
| 457 | else |
| 458 | p = option_expand(opt_idx, NULL); |
| 459 | if (p != NULL && (p = vim_strsave(p)) != NULL) |
| 460 | { |
| 461 | *(char_u **)options[opt_idx].var = p; |
| 462 | // VIMEXP |
| 463 | // Defaults for all expanded options are currently the same for Vi |
| 464 | // and Vim. When this changes, add some code here! Also need to |
| 465 | // split P_DEF_ALLOCED in two. |
| 466 | if (options[opt_idx].flags & P_DEF_ALLOCED) |
| 467 | vim_free(options[opt_idx].def_val[VI_DEFAULT]); |
| 468 | options[opt_idx].def_val[VI_DEFAULT] = p; |
| 469 | options[opt_idx].flags |= P_DEF_ALLOCED; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Initialize the 'LANG' environment variable to a default value. |
| 476 | */ |
| 477 | static void |
| 478 | set_init_lang_env(void) |
| 479 | { |
| 480 | #if defined(MSWIN) && defined(FEAT_GETTEXT) |
| 481 | // If $LANG isn't set, try to get a good value for it. This makes the |
| 482 | // right language be used automatically. Don't do this for English. |
| 483 | if (mch_getenv((char_u *)"LANG") == NULL) |
| 484 | { |
| 485 | char buf[20]; |
| 486 | long_u n; |
| 487 | |
| 488 | // Could use LOCALE_SISO639LANGNAME, but it's not in Win95. |
| 489 | // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use |
| 490 | // only the first two. |
| 491 | n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, |
| 492 | (LPTSTR)buf, 20); |
| 493 | if (n >= 2 && STRNICMP(buf, "en", 2) != 0) |
| 494 | { |
| 495 | // There are a few exceptions (probably more) |
| 496 | if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0) |
| 497 | STRCPY(buf, "zh_TW"); |
| 498 | else if (STRNICMP(buf, "chs", 3) == 0 |
| 499 | || STRNICMP(buf, "zhc", 3) == 0) |
| 500 | STRCPY(buf, "zh_CN"); |
| 501 | else if (STRNICMP(buf, "jp", 2) == 0) |
| 502 | STRCPY(buf, "ja"); |
| 503 | else |
| 504 | buf[2] = NUL; // truncate to two-letter code |
| 505 | vim_setenv((char_u *)"LANG", (char_u *)buf); |
| 506 | } |
| 507 | } |
| 508 | #elif defined(MACOS_CONVERT) |
| 509 | // Moved to os_mac_conv.c to avoid dependency problems. |
| 510 | mac_lang_init(); |
| 511 | #endif |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Initialize the 'encoding' option to a default value. |
| 516 | */ |
| 517 | static void |
| 518 | set_init_default_encoding(void) |
| 519 | { |
| 520 | char_u *p; |
| 521 | int opt_idx; |
| 522 | |
Igor Todorovski | 497e528 | 2024-01-12 17:59:18 +0100 | [diff] [blame] | 523 | # if defined(MSWIN) || defined(__MVS__) |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 524 | // MS-Windows has builtin support for conversion to and from Unicode, using |
| 525 | // "utf-8" for 'encoding' should work best for most users. |
Igor Todorovski | 497e528 | 2024-01-12 17:59:18 +0100 | [diff] [blame] | 526 | // z/OS built should default to UTF-8 mode as setlocale does not respect utf-8 environment variable locales |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 527 | p = vim_strsave((char_u *)ENC_DFLT); |
| 528 | # else |
| 529 | // enc_locale() will try to find the encoding of the current locale. |
| 530 | // This works best for properly configured systems, old and new. |
| 531 | p = enc_locale(); |
| 532 | # endif |
| 533 | if (p == NULL) |
| 534 | return; |
| 535 | |
| 536 | // Try setting 'encoding' and check if the value is valid. |
| 537 | // If not, go back to the default encoding. |
| 538 | char_u *save_enc = p_enc; |
| 539 | p_enc = p; |
| 540 | if (STRCMP(p_enc, "gb18030") == 0) |
| 541 | { |
| 542 | // We don't support "gb18030", but "cp936" is a good substitute |
| 543 | // for practical purposes, thus use that. It's not an alias to |
| 544 | // still support conversion between gb18030 and utf-8. |
| 545 | p_enc = vim_strsave((char_u *)"cp936"); |
| 546 | vim_free(p); |
| 547 | } |
| 548 | if (mb_init() == NULL) |
| 549 | { |
| 550 | opt_idx = findoption((char_u *)"encoding"); |
| 551 | if (opt_idx >= 0) |
| 552 | { |
| 553 | options[opt_idx].def_val[VI_DEFAULT] = p_enc; |
| 554 | options[opt_idx].flags |= P_DEF_ALLOCED; |
| 555 | } |
| 556 | |
| 557 | #if defined(MSWIN) || defined(MACOS_X) || defined(VMS) |
| 558 | if (STRCMP(p_enc, "latin1") == 0 || enc_utf8) |
| 559 | { |
| 560 | // Adjust the default for 'isprint' and 'iskeyword' to match |
| 561 | // latin1. Also set the defaults for when 'nocompatible' is |
| 562 | // set. |
| 563 | set_string_option_direct((char_u *)"isp", -1, |
| 564 | ISP_LATIN1, OPT_FREE, SID_NONE); |
| 565 | set_string_option_direct((char_u *)"isk", -1, |
| 566 | ISK_LATIN1, OPT_FREE, SID_NONE); |
| 567 | opt_idx = findoption((char_u *)"isp"); |
| 568 | if (opt_idx >= 0) |
| 569 | options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1; |
| 570 | opt_idx = findoption((char_u *)"isk"); |
| 571 | if (opt_idx >= 0) |
| 572 | options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1; |
| 573 | (void)init_chartab(); |
| 574 | } |
| 575 | #endif |
| 576 | |
| 577 | #if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL)) |
| 578 | // Win32 console: When GetACP() returns a different value from |
| 579 | // GetConsoleCP() set 'termencoding'. |
| 580 | if ( |
| 581 | # ifdef VIMDLL |
| 582 | (!gui.in_use && !gui.starting) && |
| 583 | # endif |
| 584 | GetACP() != GetConsoleCP()) |
| 585 | { |
| 586 | char buf[50]; |
| 587 | |
| 588 | // Win32 console: In ConPTY, GetConsoleCP() returns zero. |
| 589 | // Use an alternative value. |
| 590 | if (GetConsoleCP() == 0) |
| 591 | sprintf(buf, "cp%ld", (long)GetACP()); |
| 592 | else |
| 593 | sprintf(buf, "cp%ld", (long)GetConsoleCP()); |
| 594 | p_tenc = vim_strsave((char_u *)buf); |
| 595 | if (p_tenc != NULL) |
| 596 | { |
| 597 | opt_idx = findoption((char_u *)"termencoding"); |
| 598 | if (opt_idx >= 0) |
| 599 | { |
| 600 | options[opt_idx].def_val[VI_DEFAULT] = p_tenc; |
| 601 | options[opt_idx].flags |= P_DEF_ALLOCED; |
| 602 | } |
| 603 | convert_setup(&input_conv, p_tenc, p_enc); |
| 604 | convert_setup(&output_conv, p_enc, p_tenc); |
| 605 | } |
| 606 | else |
| 607 | p_tenc = empty_option; |
| 608 | } |
| 609 | #endif |
| 610 | #if defined(MSWIN) |
| 611 | // $HOME may have characters in active code page. |
| 612 | init_homedir(); |
| 613 | #endif |
| 614 | } |
| 615 | else |
| 616 | { |
| 617 | vim_free(p_enc); |
| 618 | p_enc = save_enc; |
| 619 | } |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Initialize the options, first part. |
| 624 | * |
| 625 | * Called only once from main(), just after creating the first buffer. |
| 626 | * If "clean_arg" is TRUE Vim was started with --clean. |
| 627 | */ |
| 628 | void |
| 629 | set_init_1(int clean_arg) |
| 630 | { |
| 631 | #ifdef FEAT_LANGMAP |
| 632 | langmap_init(); |
| 633 | #endif |
| 634 | |
| 635 | // Be Vi compatible by default |
| 636 | p_cp = TRUE; |
| 637 | |
| 638 | // Use POSIX compatibility when $VIM_POSIX is set. |
| 639 | if (mch_getenv((char_u *)"VIM_POSIX") != NULL) |
| 640 | { |
| 641 | set_string_default("cpo", (char_u *)CPO_ALL); |
| 642 | set_string_default("shm", (char_u *)SHM_POSIX); |
| 643 | } |
| 644 | |
| 645 | set_init_default_shell(); |
| 646 | set_init_default_backupskip(); |
| 647 | set_init_default_maxmemtot(); |
| 648 | set_init_default_cdpath(); |
| 649 | set_init_default_printencoding(); |
| 650 | #ifdef FEAT_POSTSCRIPT |
| 651 | set_init_default_printexpr(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 652 | #endif |
| 653 | |
| 654 | /* |
| 655 | * Set all the options (except the terminal options) to their default |
| 656 | * value. Also set the global value for local options. |
| 657 | */ |
| 658 | set_options_default(0); |
| 659 | |
matveyt | adbb1bf | 2022-02-01 17:26:12 +0000 | [diff] [blame] | 660 | #ifdef UNIX |
Luca Saccarola | c9df1fb | 2024-04-14 22:53:22 +0200 | [diff] [blame] | 661 | set_init_xdg_rtp(); |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 662 | set_init_restricted_mode(); |
matveyt | adbb1bf | 2022-02-01 17:26:12 +0000 | [diff] [blame] | 663 | #endif |
| 664 | |
Bram Moolenaar | 0726870 | 2018-03-01 21:57:32 +0100 | [diff] [blame] | 665 | #ifdef CLEAN_RUNTIMEPATH |
| 666 | if (clean_arg) |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 667 | set_init_clean_rtp(); |
Bram Moolenaar | 0726870 | 2018-03-01 21:57:32 +0100 | [diff] [blame] | 668 | #endif |
| 669 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 670 | #ifdef FEAT_GUI |
| 671 | if (found_reverse_arg) |
Bram Moolenaar | 31e5c60 | 2022-04-15 13:53:33 +0100 | [diff] [blame] | 672 | set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 673 | #endif |
| 674 | |
| 675 | curbuf->b_p_initialized = TRUE; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 676 | curbuf->b_p_ar = -1; // no local 'autoread' value |
Bram Moolenaar | f5a2fd8 | 2013-11-06 05:26:15 +0100 | [diff] [blame] | 677 | curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 678 | check_buf_options(curbuf); |
| 679 | check_win_options(curwin); |
| 680 | check_options(); |
| 681 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 682 | // Must be before option_expand(), because that one needs vim_isIDc() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 683 | didset_options(); |
| 684 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 685 | #ifdef FEAT_SPELL |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 686 | // Use the current chartab for the generic chartab. This is not in |
| 687 | // didset_options() because it only depends on 'encoding'. |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 688 | init_spell_chartab(); |
| 689 | #endif |
| 690 | |
K.Takata | ce3189d | 2023-02-15 19:13:43 +0000 | [diff] [blame] | 691 | set_init_default_encoding(); |
| 692 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 693 | // Expand environment variables and things like "~" for the defaults. |
| 694 | set_init_expand_env(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 695 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 696 | save_file_ff(curbuf); // Buffer is unchanged |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 697 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 698 | #if defined(FEAT_ARABIC) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 699 | // Detect use of mlterm. |
| 700 | // Mlterm is a terminal emulator akin to xterm that has some special |
| 701 | // abilities (bidi namely). |
| 702 | // NOTE: mlterm's author is being asked to 'set' a variable |
| 703 | // instead of an environment variable due to inheritance. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 704 | if (mch_getenv((char_u *)"MLTERM") != NULL) |
Bram Moolenaar | 31e5c60 | 2022-04-15 13:53:33 +0100 | [diff] [blame] | 705 | set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | #endif |
| 707 | |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 708 | didset_options2(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | |
Yegappan Lakshmanan | 6c41bed | 2023-02-10 14:50:31 +0000 | [diff] [blame] | 710 | set_init_lang_env(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 711 | |
| 712 | #ifdef FEAT_MULTI_LANG |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 713 | // Set the default for 'helplang'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 714 | set_helplang_default(get_mess_lang()); |
| 715 | #endif |
| 716 | } |
| 717 | |
Bram Moolenaar | 5ffefbb | 2021-06-13 20:27:36 +0200 | [diff] [blame] | 718 | static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1"; |
| 719 | |
| 720 | /* |
| 721 | * Set the "fileencodings" option to the default value for when 'encoding' is |
| 722 | * utf-8. |
| 723 | */ |
| 724 | void |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 725 | set_fencs_unicode(void) |
Bram Moolenaar | 5ffefbb | 2021-06-13 20:27:36 +0200 | [diff] [blame] | 726 | { |
| 727 | set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default, |
| 728 | OPT_FREE, 0); |
| 729 | } |
| 730 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 731 | /* |
| 732 | * Set an option to its default value. |
| 733 | * This does not take care of side effects! |
| 734 | */ |
| 735 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 736 | set_option_default( |
| 737 | int opt_idx, |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 738 | int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL |
| 739 | int compatible) // use Vi default value |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 740 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 741 | char_u *varp; // pointer to variable for current option |
| 742 | int dvi; // index in def_val[] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 743 | long_u flags; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 744 | long_u *flagsp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 745 | int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0; |
| 746 | |
| 747 | varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags); |
| 748 | flags = options[opt_idx].flags; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 749 | if (varp != NULL) // skip hidden option, nothing to do for it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 750 | { |
| 751 | dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT; |
| 752 | if (flags & P_STRING) |
| 753 | { |
Bram Moolenaar | 5ffefbb | 2021-06-13 20:27:36 +0200 | [diff] [blame] | 754 | // 'fencs' default value depends on 'encoding' |
| 755 | if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8) |
| 756 | set_fencs_unicode(); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 757 | // Use set_string_option_direct() for local options to handle |
| 758 | // freeing and allocating the value. |
Bram Moolenaar | 5ffefbb | 2021-06-13 20:27:36 +0200 | [diff] [blame] | 759 | else if (options[opt_idx].indir != PV_NONE) |
Bram Moolenaar | b833c1e | 2018-05-05 16:36:06 +0200 | [diff] [blame] | 760 | set_string_option_direct(NULL, opt_idx, |
| 761 | options[opt_idx].def_val[dvi], opt_flags, 0); |
| 762 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 763 | { |
Bram Moolenaar | b833c1e | 2018-05-05 16:36:06 +0200 | [diff] [blame] | 764 | if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED)) |
| 765 | free_string_option(*(char_u **)(varp)); |
| 766 | *(char_u **)varp = options[opt_idx].def_val[dvi]; |
| 767 | options[opt_idx].flags &= ~P_ALLOCED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | else if (flags & P_NUM) |
| 771 | { |
Bram Moolenaar | 5fc1a8b | 2006-10-17 16:34:24 +0000 | [diff] [blame] | 772 | if (options[opt_idx].indir == PV_SCROLL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 773 | win_comp_scroll(curwin); |
| 774 | else |
| 775 | { |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 776 | long def_val = (long)(long_i)options[opt_idx].def_val[dvi]; |
| 777 | |
| 778 | if ((long *)varp == &curwin->w_p_so |
| 779 | || (long *)varp == &curwin->w_p_siso) |
| 780 | // 'scrolloff' and 'sidescrolloff' local values have a |
| 781 | // different default value than the global default. |
| 782 | *(long *)varp = -1; |
| 783 | else |
| 784 | *(long *)varp = def_val; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 785 | // May also set global value for local option. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 786 | if (both) |
| 787 | *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 788 | def_val; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 791 | else // P_BOOL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 792 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 793 | // the cast to long is required for Manx C, long_i is needed for |
| 794 | // MSVC |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 795 | *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi]; |
Bram Moolenaar | 8243a79 | 2007-05-01 17:05:03 +0000 | [diff] [blame] | 796 | #ifdef UNIX |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 797 | // 'modeline' defaults to off for root |
Bram Moolenaar | 8243a79 | 2007-05-01 17:05:03 +0000 | [diff] [blame] | 798 | if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID) |
| 799 | *(int *)varp = FALSE; |
| 800 | #endif |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 801 | // May also set global value for local option. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 802 | if (both) |
| 803 | *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = |
| 804 | *(int *)varp; |
| 805 | } |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 806 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 807 | // The default value is not insecure. |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 808 | flagsp = insecure_flag(opt_idx, opt_flags); |
| 809 | *flagsp = *flagsp & ~P_INSECURE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | #ifdef FEAT_EVAL |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 813 | set_option_sctx_idx(opt_idx, opt_flags, current_sctx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 814 | #endif |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Set all options (except terminal options) to their default value. |
Bram Moolenaar | b341dda | 2015-08-25 12:56:31 +0200 | [diff] [blame] | 819 | * When "opt_flags" is non-zero skip 'encoding'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 820 | */ |
| 821 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 822 | set_options_default( |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 823 | int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 824 | { |
| 825 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 826 | win_T *wp; |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 827 | tabpage_T *tp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 828 | |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 829 | for (i = 0; !istermoption_idx(i); i++) |
Bram Moolenaar | b341dda | 2015-08-25 12:56:31 +0200 | [diff] [blame] | 830 | if (!(options[i].flags & P_NODEFAULT) |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 831 | && (opt_flags == 0 |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 832 | || (options[i].var != (char_u *)&p_enc |
Bram Moolenaar | 5ea87a0 | 2015-08-26 23:24:09 +0200 | [diff] [blame] | 833 | # if defined(FEAT_CRYPT) |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 834 | && options[i].var != (char_u *)&p_cm |
Bram Moolenaar | 8060687 | 2015-08-25 21:27:35 +0200 | [diff] [blame] | 835 | && options[i].var != (char_u *)&p_key |
Bram Moolenaar | 5ea87a0 | 2015-08-26 23:24:09 +0200 | [diff] [blame] | 836 | # endif |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 837 | ))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 838 | set_option_default(i, opt_flags, p_cp); |
| 839 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 840 | // The 'scroll' option must be computed for all windows. |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 841 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 842 | win_comp_scroll(wp); |
Bram Moolenaar | 5a4eceb | 2014-09-09 17:33:07 +0200 | [diff] [blame] | 843 | parse_cino(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Set the Vi-default value of a string option. |
| 848 | * Used for 'sh', 'backupskip' and 'term'. |
Bram Moolenaar | 4bfa8af | 2018-02-03 15:14:46 +0100 | [diff] [blame] | 849 | * When "escape" is TRUE escape spaces with a backslash. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 850 | */ |
Bram Moolenaar | 4bfa8af | 2018-02-03 15:14:46 +0100 | [diff] [blame] | 851 | static void |
| 852 | set_string_default_esc(char *name, char_u *val, int escape) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 853 | { |
| 854 | char_u *p; |
| 855 | int opt_idx; |
| 856 | |
Bram Moolenaar | 4bfa8af | 2018-02-03 15:14:46 +0100 | [diff] [blame] | 857 | if (escape && vim_strchr(val, ' ') != NULL) |
| 858 | p = vim_strsave_escaped(val, (char_u *)" "); |
| 859 | else |
| 860 | p = vim_strsave(val); |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 861 | if (p == NULL) // we don't want a NULL |
| 862 | return; |
| 863 | |
| 864 | opt_idx = findoption((char_u *)name); |
| 865 | if (opt_idx < 0) |
Christian Brabandt | 29269a7 | 2024-04-16 22:44:31 +0200 | [diff] [blame] | 866 | { |
| 867 | vim_free(p); |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 868 | return; |
Christian Brabandt | 29269a7 | 2024-04-16 22:44:31 +0200 | [diff] [blame] | 869 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 870 | |
| 871 | if (options[opt_idx].flags & P_DEF_ALLOCED) |
| 872 | vim_free(options[opt_idx].def_val[VI_DEFAULT]); |
| 873 | options[opt_idx].def_val[VI_DEFAULT] = p; |
| 874 | options[opt_idx].flags |= P_DEF_ALLOCED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Bram Moolenaar | 4bfa8af | 2018-02-03 15:14:46 +0100 | [diff] [blame] | 877 | void |
| 878 | set_string_default(char *name, char_u *val) |
| 879 | { |
| 880 | set_string_default_esc(name, val, FALSE); |
| 881 | } |
| 882 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | /* |
Bram Moolenaar | b00ef05 | 2020-09-12 14:53:53 +0200 | [diff] [blame] | 884 | * For an option value that contains comma separated items, find "newval" in |
| 885 | * "origval". Return NULL if not found. |
| 886 | */ |
| 887 | static char_u * |
| 888 | find_dup_item(char_u *origval, char_u *newval, long_u flags) |
| 889 | { |
Bram Moolenaar | 8f13d82 | 2020-09-12 21:04:23 +0200 | [diff] [blame] | 890 | int bs = 0; |
Bram Moolenaar | b00ef05 | 2020-09-12 14:53:53 +0200 | [diff] [blame] | 891 | size_t newlen; |
| 892 | char_u *s; |
| 893 | |
| 894 | if (origval == NULL) |
| 895 | return NULL; |
| 896 | |
| 897 | newlen = STRLEN(newval); |
| 898 | for (s = origval; *s != NUL; ++s) |
| 899 | { |
| 900 | if ((!(flags & P_COMMA) |
| 901 | || s == origval |
| 902 | || (s[-1] == ',' && !(bs & 1))) |
| 903 | && STRNCMP(s, newval, newlen) == 0 |
| 904 | && (!(flags & P_COMMA) |
| 905 | || s[newlen] == ',' |
| 906 | || s[newlen] == NUL)) |
| 907 | return s; |
| 908 | // Count backslashes. Only a comma with an even number of backslashes |
| 909 | // or a single backslash preceded by a comma before it is recognized as |
| 910 | // a separator. |
| 911 | if ((s > origval + 1 |
| 912 | && s[-1] == '\\' |
| 913 | && s[-2] != ',') |
| 914 | || (s == origval + 1 |
| 915 | && s[-1] == '\\')) |
| 916 | ++bs; |
| 917 | else |
| 918 | bs = 0; |
| 919 | } |
| 920 | return NULL; |
| 921 | } |
| 922 | |
| 923 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | * Set the Vi-default value of a number option. |
| 925 | * Used for 'lines' and 'columns'. |
| 926 | */ |
| 927 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 928 | set_number_default(char *name, long val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 930 | int opt_idx; |
| 931 | |
| 932 | opt_idx = findoption((char_u *)name); |
| 933 | if (opt_idx >= 0) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 934 | options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 937 | #if defined(FEAT_PROP_POPUP) || defined(PROTO) |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 938 | /* |
| 939 | * Set all window-local and buffer-local options to the Vim default. |
| 940 | * local-global options will use the global value. |
Bram Moolenaar | 4645104 | 2019-08-24 15:50:46 +0200 | [diff] [blame] | 941 | * When "do_buffer" is FALSE don't set buffer-local options. |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 942 | */ |
| 943 | void |
Bram Moolenaar | 4645104 | 2019-08-24 15:50:46 +0200 | [diff] [blame] | 944 | set_local_options_default(win_T *wp, int do_buffer) |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 945 | { |
| 946 | win_T *save_curwin = curwin; |
| 947 | int i; |
| 948 | |
| 949 | curwin = wp; |
| 950 | curbuf = curwin->w_buffer; |
| 951 | block_autocmds(); |
| 952 | |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 953 | for (i = 0; !istermoption_idx(i); i++) |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 954 | { |
| 955 | struct vimoption *p = &(options[i]); |
| 956 | char_u *varp = get_varp_scope(p, OPT_LOCAL); |
| 957 | |
| 958 | if (p->indir != PV_NONE |
Bram Moolenaar | 4645104 | 2019-08-24 15:50:46 +0200 | [diff] [blame] | 959 | && (do_buffer || (p->indir & PV_BUF) == 0) |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 960 | && !(options[i].flags & P_NODEFAULT) |
| 961 | && !optval_default(p, varp, FALSE)) |
Bram Moolenaar | 8617348 | 2019-10-01 17:02:16 +0200 | [diff] [blame] | 962 | set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE); |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | unblock_autocmds(); |
| 966 | curwin = save_curwin; |
| 967 | curbuf = curwin->w_buffer; |
| 968 | } |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 969 | #endif |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 970 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 971 | #if defined(EXITFREE) || defined(PROTO) |
| 972 | /* |
| 973 | * Free all options. |
| 974 | */ |
| 975 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 976 | free_all_options(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 977 | { |
| 978 | int i; |
| 979 | |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 980 | for (i = 0; !istermoption_idx(i); i++) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 981 | { |
| 982 | if (options[i].indir == PV_NONE) |
| 983 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 984 | // global option: free value and default value. |
Bram Moolenaar | 6739114 | 2017-02-19 21:07:04 +0100 | [diff] [blame] | 985 | if ((options[i].flags & P_ALLOCED) && options[i].var != NULL) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 986 | free_string_option(*(char_u **)options[i].var); |
| 987 | if (options[i].flags & P_DEF_ALLOCED) |
| 988 | free_string_option(options[i].def_val[VI_DEFAULT]); |
| 989 | } |
| 990 | else if (options[i].var != VAR_WIN |
| 991 | && (options[i].flags & P_STRING)) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 992 | // buffer-local option: free global value |
Bram Moolenaar | 77111e2 | 2021-07-29 21:11:30 +0200 | [diff] [blame] | 993 | clear_string_option((char_u **)options[i].var); |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 994 | } |
Yegappan Lakshmanan | 777175b | 2021-11-18 22:08:57 +0000 | [diff] [blame] | 995 | free_operatorfunc_option(); |
Yegappan Lakshmanan | 19916a8 | 2021-11-24 16:32:55 +0000 | [diff] [blame] | 996 | free_tagfunc_option(); |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 997 | } |
| 998 | #endif |
| 999 | |
| 1000 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1001 | /* |
| 1002 | * Initialize the options, part two: After getting Rows and Columns and |
| 1003 | * setting 'term'. |
| 1004 | */ |
| 1005 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1006 | set_init_2(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1007 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 1008 | int idx; |
| 1009 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 1010 | // 'scroll' defaults to half the window height. The stored default is zero, |
| 1011 | // which results in the actual value computed from the window height. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1012 | idx = findoption((char_u *)"scroll"); |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 1013 | if (idx >= 0 && !(options[idx].flags & P_WAS_SET)) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1014 | set_option_default(idx, OPT_LOCAL, p_cp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1015 | comp_col(); |
| 1016 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 1017 | // 'window' is only for backwards compatibility with Vi. |
| 1018 | // Default is Rows - 1. |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 1019 | if (!option_was_set((char_u *)"window")) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 1020 | p_window = Rows - 1; |
| 1021 | set_number_default("window", Rows - 1); |
| 1022 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 1023 | // For DOS console the default is always black. |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1024 | #if !((defined(MSWIN)) && !defined(FEAT_GUI)) |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 1025 | // If 'background' wasn't set by the user, try guessing the value, |
| 1026 | // depending on the terminal name. Only need to check for terminals |
| 1027 | // with a dark background, that can handle color. |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 1028 | idx = findoption((char_u *)"bg"); |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 1029 | if (idx >= 0 && !(options[idx].flags & P_WAS_SET) |
| 1030 | && *term_bg_default() == 'd') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1031 | { |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 1032 | set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 1033 | // don't mark it as set, when starting the GUI it may be |
| 1034 | // changed again |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 1035 | options[idx].flags &= ~P_WAS_SET; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1036 | } |
| 1037 | #endif |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1038 | |
| 1039 | #ifdef CURSOR_SHAPE |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 1040 | parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor' |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1041 | #endif |
| 1042 | #ifdef FEAT_MOUSESHAPE |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 1043 | parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape' |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1044 | #endif |
| 1045 | #ifdef FEAT_PRINTER |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 1046 | (void)parse_printoptions(NULL); // parse 'printoptions' default value |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1047 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Initialize the options, part three: After reading the .vimrc |
| 1052 | */ |
| 1053 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1054 | set_init_3(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1055 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1056 | #if defined(UNIX) || defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1057 | /* |
| 1058 | * Set 'shellpipe' and 'shellredir', depending on the 'shell' option. |
| 1059 | * This is done after other initializations, where 'shell' might have been |
| 1060 | * set, but only if they have not been set before. |
| 1061 | */ |
| 1062 | char_u *p; |
| 1063 | int idx_srr; |
| 1064 | int do_srr; |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1065 | # ifdef FEAT_QUICKFIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1066 | int idx_sp; |
| 1067 | int do_sp; |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1068 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1069 | |
| 1070 | idx_srr = findoption((char_u *)"srr"); |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 1071 | if (idx_srr < 0) |
| 1072 | do_srr = FALSE; |
| 1073 | else |
| 1074 | do_srr = !(options[idx_srr].flags & P_WAS_SET); |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1075 | # ifdef FEAT_QUICKFIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1076 | idx_sp = findoption((char_u *)"sp"); |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 1077 | if (idx_sp < 0) |
| 1078 | do_sp = FALSE; |
| 1079 | else |
| 1080 | do_sp = !(options[idx_sp].flags & P_WAS_SET); |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1081 | # endif |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1082 | p = get_isolated_shell_name(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1083 | if (p != NULL) |
| 1084 | { |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 1085 | // Default for p_sp is "| tee", for p_srr is ">". |
| 1086 | // For known shells it is changed here to include stderr. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1087 | if ( fnamecmp(p, "csh") == 0 |
| 1088 | || fnamecmp(p, "tcsh") == 0 |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1089 | # if defined(MSWIN) // also check with .exe extension |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1090 | || fnamecmp(p, "csh.exe") == 0 |
| 1091 | || fnamecmp(p, "tcsh.exe") == 0 |
| 1092 | # endif |
| 1093 | ) |
| 1094 | { |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1095 | # if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | if (do_sp) |
| 1097 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1098 | # ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1099 | p_sp = (char_u *)">&"; |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1100 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1101 | p_sp = (char_u *)"|& tee"; |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1102 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1103 | options[idx_sp].def_val[VI_DEFAULT] = p_sp; |
| 1104 | } |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1105 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1106 | if (do_srr) |
| 1107 | { |
| 1108 | p_srr = (char_u *)">&"; |
| 1109 | options[idx_srr].def_val[VI_DEFAULT] = p_srr; |
| 1110 | } |
| 1111 | } |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 1112 | # ifdef MSWIN |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 1113 | // Windows PowerShell output is UTF-16 with BOM so re-encode to the |
| 1114 | // current codepage. |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 1115 | else if ( fnamecmp(p, "powershell") == 0 |
| 1116 | || fnamecmp(p, "powershell.exe") == 0 |
| 1117 | ) |
| 1118 | { |
| 1119 | # if defined(FEAT_QUICKFIX) |
| 1120 | if (do_sp) |
| 1121 | { |
| 1122 | p_sp = (char_u *)"2>&1 | Out-File -Encoding default"; |
| 1123 | options[idx_sp].def_val[VI_DEFAULT] = p_sp; |
| 1124 | } |
| 1125 | # endif |
| 1126 | if (do_srr) |
| 1127 | { |
| 1128 | p_srr = (char_u *)"2>&1 | Out-File -Encoding default"; |
| 1129 | options[idx_srr].def_val[VI_DEFAULT] = p_srr; |
| 1130 | } |
| 1131 | } |
| 1132 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1133 | else |
Natanael Copa | 5631836 | 2021-05-06 18:46:35 +0200 | [diff] [blame] | 1134 | // Always use POSIX shell style redirection if we reach this |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1135 | if ( fnamecmp(p, "sh") == 0 |
| 1136 | || fnamecmp(p, "ksh") == 0 |
Bram Moolenaar | f1fda2d | 2011-04-28 12:57:36 +0200 | [diff] [blame] | 1137 | || fnamecmp(p, "mksh") == 0 |
| 1138 | || fnamecmp(p, "pdksh") == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1139 | || fnamecmp(p, "zsh") == 0 |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 1140 | || fnamecmp(p, "zsh-beta") == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1141 | || fnamecmp(p, "bash") == 0 |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1142 | || fnamecmp(p, "fish") == 0 |
Natanael Copa | 5631836 | 2021-05-06 18:46:35 +0200 | [diff] [blame] | 1143 | || fnamecmp(p, "ash") == 0 |
| 1144 | || fnamecmp(p, "dash") == 0 |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 1145 | || fnamecmp(p, "pwsh") == 0 |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1146 | # ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1147 | || fnamecmp(p, "cmd") == 0 |
| 1148 | || fnamecmp(p, "sh.exe") == 0 |
| 1149 | || fnamecmp(p, "ksh.exe") == 0 |
Bram Moolenaar | f1fda2d | 2011-04-28 12:57:36 +0200 | [diff] [blame] | 1150 | || fnamecmp(p, "mksh.exe") == 0 |
| 1151 | || fnamecmp(p, "pdksh.exe") == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1152 | || fnamecmp(p, "zsh.exe") == 0 |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 1153 | || fnamecmp(p, "zsh-beta.exe") == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1154 | || fnamecmp(p, "bash.exe") == 0 |
| 1155 | || fnamecmp(p, "cmd.exe") == 0 |
Natanael Copa | 5631836 | 2021-05-06 18:46:35 +0200 | [diff] [blame] | 1156 | || fnamecmp(p, "dash.exe") == 0 |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 1157 | || fnamecmp(p, "pwsh.exe") == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | # endif |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1159 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1160 | { |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1161 | # if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1162 | if (do_sp) |
| 1163 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1164 | # ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1165 | p_sp = (char_u *)">%s 2>&1"; |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1166 | # else |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 1167 | if (fnamecmp(p, "pwsh") == 0) |
| 1168 | p_sp = (char_u *)">%s 2>&1"; |
| 1169 | else |
| 1170 | p_sp = (char_u *)"2>&1| tee"; |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1171 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1172 | options[idx_sp].def_val[VI_DEFAULT] = p_sp; |
| 1173 | } |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 1174 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1175 | if (do_srr) |
| 1176 | { |
| 1177 | p_srr = (char_u *)">%s 2>&1"; |
| 1178 | options[idx_srr].def_val[VI_DEFAULT] = p_srr; |
| 1179 | } |
| 1180 | } |
| 1181 | vim_free(p); |
| 1182 | } |
| 1183 | #endif |
| 1184 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1185 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1186 | /* |
Bram Moolenaar | a64ba22 | 2012-02-12 23:23:31 +0100 | [diff] [blame] | 1187 | * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the |
| 1188 | * 'shell' option. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1189 | * This is done after other initializations, where 'shell' might have been |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 1190 | * set, but only if they have not been set before. |
| 1191 | * Default values depend on shell (cmd.exe is default shell): |
| 1192 | * |
| 1193 | * p_shcf p_sxq |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 1194 | * cmd.exe - "/c" "(" |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 1195 | * powershell.exe - "-Command" "\"" |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 1196 | * pwsh.exe - "-c" "\"" |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 1197 | * "sh" like shells - "-c" "\"" |
| 1198 | * |
| 1199 | * For Win32 p_sxq is set instead of p_shq to include shell redirection. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1200 | */ |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 1201 | if (strstr((char *)gettail(p_sh), "powershell") != NULL) |
| 1202 | { |
| 1203 | int idx_opt; |
| 1204 | |
| 1205 | idx_opt = findoption((char_u *)"shcf"); |
| 1206 | if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET)) |
| 1207 | { |
| 1208 | p_shcf = (char_u*)"-Command"; |
| 1209 | options[idx_opt].def_val[VI_DEFAULT] = p_shcf; |
| 1210 | } |
| 1211 | |
| 1212 | idx_opt = findoption((char_u *)"sxq"); |
| 1213 | if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET)) |
| 1214 | { |
| 1215 | p_sxq = (char_u*)"\""; |
| 1216 | options[idx_opt].def_val[VI_DEFAULT] = p_sxq; |
| 1217 | } |
| 1218 | } |
| 1219 | else if (strstr((char *)gettail(p_sh), "sh") != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1220 | { |
| 1221 | int idx3; |
| 1222 | |
| 1223 | idx3 = findoption((char_u *)"shcf"); |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 1224 | if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1225 | { |
| 1226 | p_shcf = (char_u *)"-c"; |
| 1227 | options[idx3].def_val[VI_DEFAULT] = p_shcf; |
| 1228 | } |
| 1229 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 1230 | // Somehow Win32 requires the quotes around the redirection too |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1231 | idx3 = findoption((char_u *)"sxq"); |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 1232 | if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1233 | { |
| 1234 | p_sxq = (char_u *)"\""; |
| 1235 | options[idx3].def_val[VI_DEFAULT] = p_sxq; |
| 1236 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | } |
Bram Moolenaar | a64ba22 | 2012-02-12 23:23:31 +0100 | [diff] [blame] | 1238 | else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL) |
| 1239 | { |
| 1240 | int idx3; |
| 1241 | |
| 1242 | /* |
| 1243 | * cmd.exe on Windows will strip the first and last double quote given |
| 1244 | * on the command line, e.g. most of the time things like: |
| 1245 | * cmd /c "my path/to/echo" "my args to echo" |
| 1246 | * become: |
| 1247 | * my path/to/echo" "my args to echo |
| 1248 | * when executed. |
| 1249 | * |
Bram Moolenaar | 034b115 | 2012-02-19 18:19:30 +0100 | [diff] [blame] | 1250 | * To avoid this, set shellxquote to surround the command in |
| 1251 | * parenthesis. This appears to make most commands work, without |
| 1252 | * breaking commands that worked previously, such as |
| 1253 | * '"path with spaces/cmd" "a&b"'. |
Bram Moolenaar | a64ba22 | 2012-02-12 23:23:31 +0100 | [diff] [blame] | 1254 | */ |
Bram Moolenaar | a64ba22 | 2012-02-12 23:23:31 +0100 | [diff] [blame] | 1255 | idx3 = findoption((char_u *)"sxq"); |
| 1256 | if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET)) |
| 1257 | { |
Bram Moolenaar | 034b115 | 2012-02-19 18:19:30 +0100 | [diff] [blame] | 1258 | p_sxq = (char_u *)"("; |
Bram Moolenaar | a64ba22 | 2012-02-12 23:23:31 +0100 | [diff] [blame] | 1259 | options[idx3].def_val[VI_DEFAULT] = p_sxq; |
| 1260 | } |
| 1261 | |
Bram Moolenaar | a64ba22 | 2012-02-12 23:23:31 +0100 | [diff] [blame] | 1262 | idx3 = findoption((char_u *)"shcf"); |
| 1263 | if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET)) |
| 1264 | { |
Bram Moolenaar | 034b115 | 2012-02-19 18:19:30 +0100 | [diff] [blame] | 1265 | p_shcf = (char_u *)"/c"; |
Bram Moolenaar | a64ba22 | 2012-02-12 23:23:31 +0100 | [diff] [blame] | 1266 | options[idx3].def_val[VI_DEFAULT] = p_shcf; |
| 1267 | } |
| 1268 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | #endif |
| 1270 | |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1271 | if (BUFEMPTY()) |
Bram Moolenaar | 364fa5c | 2016-03-20 17:53:25 +0100 | [diff] [blame] | 1272 | { |
| 1273 | int idx_ffs = findoption((char_u *)"ffs"); |
| 1274 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 1275 | // Apply the first entry of 'fileformats' to the initial buffer. |
Bram Moolenaar | 364fa5c | 2016-03-20 17:53:25 +0100 | [diff] [blame] | 1276 | if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET)) |
| 1277 | set_fileformat(default_fileformat(), OPT_LOCAL); |
| 1278 | } |
| 1279 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1280 | set_title_defaults(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | #if defined(FEAT_MULTI_LANG) || defined(PROTO) |
| 1284 | /* |
| 1285 | * When 'helplang' is still at its default value, set it to "lang". |
| 1286 | * Only the first two characters of "lang" are used. |
| 1287 | */ |
| 1288 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1289 | set_helplang_default(char_u *lang) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1290 | { |
| 1291 | int idx; |
| 1292 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 1293 | if (lang == NULL || STRLEN(lang) < 2) // safety check |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1294 | return; |
| 1295 | idx = findoption((char_u *)"hlg"); |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 1296 | if (idx < 0 || (options[idx].flags & P_WAS_SET)) |
| 1297 | return; |
| 1298 | |
| 1299 | if (options[idx].flags & P_ALLOCED) |
| 1300 | free_string_option(p_hlg); |
| 1301 | p_hlg = vim_strsave(lang); |
| 1302 | if (p_hlg == NULL) |
| 1303 | p_hlg = empty_option; |
| 1304 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1305 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 1306 | // zh_CN becomes "cn", zh_TW becomes "tw" |
| 1307 | if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5) |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1308 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 1309 | p_hlg[0] = TOLOWER_ASC(p_hlg[3]); |
| 1310 | p_hlg[1] = TOLOWER_ASC(p_hlg[4]); |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1311 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 1312 | // any C like setting, such as C.UTF-8, becomes "en" |
| 1313 | else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C') |
| 1314 | { |
| 1315 | p_hlg[0] = 'e'; |
| 1316 | p_hlg[1] = 'n'; |
| 1317 | } |
| 1318 | p_hlg[2] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1319 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 1320 | options[idx].flags |= P_ALLOCED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | } |
| 1322 | #endif |
| 1323 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1324 | /* |
| 1325 | * 'title' and 'icon' only default to true if they have not been set or reset |
| 1326 | * in .vimrc and we can read the old value. |
| 1327 | * When 'title' and 'icon' have been reset in .vimrc, we won't even check if |
| 1328 | * they can be reset. This reduces startup time when using X on a remote |
| 1329 | * machine. |
| 1330 | */ |
| 1331 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1332 | set_title_defaults(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1333 | { |
| 1334 | int idx1; |
| 1335 | long val; |
| 1336 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 1337 | // If GUI is (going to be) used, we can always set the window title and |
| 1338 | // icon name. Saves a bit of time, because the X11 display server does |
| 1339 | // not need to be contacted. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1340 | idx1 = findoption((char_u *)"title"); |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 1341 | if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1342 | { |
| 1343 | #ifdef FEAT_GUI |
| 1344 | if (gui.starting || gui.in_use) |
| 1345 | val = TRUE; |
| 1346 | else |
| 1347 | #endif |
| 1348 | val = mch_can_restore_title(); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 1349 | options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1350 | p_title = val; |
| 1351 | } |
| 1352 | idx1 = findoption((char_u *)"icon"); |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 1353 | if (idx1 < 0 || (options[idx1].flags & P_WAS_SET)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1354 | { |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 1355 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1356 | } |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 1357 | |
| 1358 | #ifdef FEAT_GUI |
| 1359 | if (gui.starting || gui.in_use) |
| 1360 | val = TRUE; |
| 1361 | else |
| 1362 | #endif |
| 1363 | val = mch_can_restore_icon(); |
| 1364 | options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val; |
| 1365 | p_icon = val; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1366 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1367 | |
Bram Moolenaar | 6b915c0 | 2020-01-18 15:53:19 +0100 | [diff] [blame] | 1368 | void |
| 1369 | ex_set(exarg_T *eap) |
| 1370 | { |
| 1371 | int flags = 0; |
| 1372 | |
| 1373 | if (eap->cmdidx == CMD_setlocal) |
| 1374 | flags = OPT_LOCAL; |
| 1375 | else if (eap->cmdidx == CMD_setglobal) |
| 1376 | flags = OPT_GLOBAL; |
| 1377 | #if defined(FEAT_EVAL) && defined(FEAT_BROWSE) |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 1378 | if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0) |
Bram Moolenaar | 6b915c0 | 2020-01-18 15:53:19 +0100 | [diff] [blame] | 1379 | ex_options(eap); |
| 1380 | else |
| 1381 | #endif |
| 1382 | { |
| 1383 | if (eap->forceit) |
| 1384 | flags |= OPT_ONECOLUMN; |
| 1385 | (void)do_set(eap->arg, flags); |
| 1386 | } |
| 1387 | } |
| 1388 | |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 1389 | /* |
Yee Cheng Chin | 6d11347 | 2023-10-02 21:38:39 +0200 | [diff] [blame] | 1390 | * :set boolean option prefix |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 1391 | */ |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 1392 | typedef enum { |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 1393 | PREFIX_NO = 0, // "no" prefix |
| 1394 | PREFIX_NONE, // no prefix |
| 1395 | PREFIX_INV, // "inv" prefix |
| 1396 | } set_prefix_T; |
| 1397 | |
| 1398 | /* |
| 1399 | * Return the prefix type for the option name in *argp. |
| 1400 | */ |
| 1401 | static set_prefix_T |
| 1402 | get_option_prefix(char_u **argp) |
| 1403 | { |
| 1404 | int prefix = PREFIX_NONE; |
| 1405 | char_u *arg = *argp; |
| 1406 | |
| 1407 | if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0) |
| 1408 | { |
| 1409 | prefix = PREFIX_NO; |
| 1410 | arg += 2; |
| 1411 | } |
| 1412 | else if (STRNCMP(arg, "inv", 3) == 0) |
| 1413 | { |
| 1414 | prefix = PREFIX_INV; |
| 1415 | arg += 3; |
| 1416 | } |
| 1417 | |
| 1418 | *argp = arg; |
| 1419 | return prefix; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * Parse the option name in "arg" and return the option index in "*opt_idxp", |
| 1424 | * and the option name length in "*lenp". For a <t_xx> option, return the key |
| 1425 | * number in "*keyp". |
| 1426 | * |
| 1427 | * Returns FAIL if an option starting with "<" doesn't end with a ">", |
| 1428 | * otherwise returns OK. |
| 1429 | */ |
| 1430 | static int |
| 1431 | parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp) |
| 1432 | { |
| 1433 | int key = 0; |
| 1434 | int len; |
| 1435 | int opt_idx; |
| 1436 | |
| 1437 | if (*arg == '<') |
| 1438 | { |
| 1439 | opt_idx = -1; |
| 1440 | // look out for <t_>;> |
| 1441 | if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4]) |
| 1442 | len = 5; |
| 1443 | else |
| 1444 | { |
| 1445 | len = 1; |
| 1446 | while (arg[len] != NUL && arg[len] != '>') |
| 1447 | ++len; |
| 1448 | } |
| 1449 | if (arg[len] != '>') |
| 1450 | return FAIL; |
| 1451 | |
| 1452 | arg[len] = NUL; // put NUL after name |
| 1453 | if (arg[1] == 't' && arg[2] == '_') // could be term code |
| 1454 | opt_idx = findoption(arg + 1); |
| 1455 | arg[len++] = '>'; // restore '>' |
| 1456 | if (opt_idx == -1) |
| 1457 | key = find_key_option(arg + 1, TRUE); |
| 1458 | } |
| 1459 | else |
| 1460 | { |
| 1461 | int nextchar; // next non-white char after option name |
| 1462 | |
| 1463 | len = 0; |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 1464 | // The two characters after "t_" may not be alphanumeric. |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 1465 | if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3]) |
| 1466 | len = 4; |
| 1467 | else |
| 1468 | while (ASCII_ISALNUM(arg[len]) || arg[len] == '_') |
| 1469 | ++len; |
| 1470 | nextchar = arg[len]; |
| 1471 | arg[len] = NUL; // put NUL after name |
| 1472 | opt_idx = findoption(arg); |
| 1473 | arg[len] = nextchar; // restore nextchar |
| 1474 | if (opt_idx == -1) |
| 1475 | key = find_key_option(arg, FALSE); |
| 1476 | } |
| 1477 | |
| 1478 | *keyp = key; |
| 1479 | *lenp = len; |
| 1480 | *opt_idxp = opt_idx; |
| 1481 | |
| 1482 | return OK; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * Get the option operator (+=, ^=, -=). |
| 1487 | */ |
| 1488 | static set_op_T |
| 1489 | get_opt_op(char_u *arg) |
| 1490 | { |
| 1491 | set_op_T op = OP_NONE; |
| 1492 | |
| 1493 | if (*arg != NUL && *(arg + 1) == '=') |
| 1494 | { |
| 1495 | if (*arg == '+') |
| 1496 | op = OP_ADDING; // "+=" |
| 1497 | else if (*arg == '^') |
| 1498 | op = OP_PREPENDING; // "^=" |
| 1499 | else if (*arg == '-') |
| 1500 | op = OP_REMOVING; // "-=" |
| 1501 | } |
| 1502 | |
| 1503 | return op; |
| 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Validate whether the value of the option in "opt_idx" can be changed. |
| 1508 | * Returns FAIL if the option can be skipped or cannot be changed. Returns OK |
| 1509 | * if it can be changed. |
| 1510 | */ |
| 1511 | static int |
| 1512 | validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg) |
| 1513 | { |
| 1514 | // Skip all options that are not window-local (used when showing |
| 1515 | // an already loaded buffer in a window). |
| 1516 | if ((opt_flags & OPT_WINONLY) |
| 1517 | && (opt_idx < 0 || options[opt_idx].var != VAR_WIN)) |
| 1518 | return FAIL; |
| 1519 | |
| 1520 | // Skip all options that are window-local (used for :vimgrep). |
| 1521 | if ((opt_flags & OPT_NOWIN) && opt_idx >= 0 |
| 1522 | && options[opt_idx].var == VAR_WIN) |
| 1523 | return FAIL; |
| 1524 | |
| 1525 | // Disallow changing some options from modelines. |
| 1526 | if (opt_flags & OPT_MODELINE) |
| 1527 | { |
| 1528 | if (flags & (P_SECURE | P_NO_ML)) |
| 1529 | { |
| 1530 | *errmsg = e_not_allowed_in_modeline; |
| 1531 | return FAIL; |
| 1532 | } |
| 1533 | if ((flags & P_MLE) && !p_mle) |
| 1534 | { |
| 1535 | *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off; |
| 1536 | return FAIL; |
| 1537 | } |
| 1538 | #ifdef FEAT_DIFF |
| 1539 | // In diff mode some options are overruled. This avoids that |
| 1540 | // 'foldmethod' becomes "marker" instead of "diff" and that |
| 1541 | // "wrap" gets set. |
| 1542 | if (curwin->w_p_diff |
| 1543 | && opt_idx >= 0 // shut up coverity warning |
| 1544 | && ( |
| 1545 | # ifdef FEAT_FOLDING |
| 1546 | options[opt_idx].indir == PV_FDM || |
| 1547 | # endif |
| 1548 | options[opt_idx].indir == PV_WRAP)) |
| 1549 | return FAIL; |
| 1550 | #endif |
| 1551 | } |
| 1552 | |
| 1553 | #ifdef HAVE_SANDBOX |
| 1554 | // Disallow changing some options in the sandbox |
| 1555 | if (sandbox != 0 && (flags & P_SECURE)) |
| 1556 | { |
| 1557 | *errmsg = e_not_allowed_in_sandbox; |
| 1558 | return FAIL; |
| 1559 | } |
| 1560 | #endif |
| 1561 | |
| 1562 | return OK; |
| 1563 | } |
| 1564 | |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 1565 | /* |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 1566 | * Get the Vim/Vi default value for a string option. |
| 1567 | */ |
| 1568 | static char_u * |
| 1569 | stropt_get_default_val( |
| 1570 | int opt_idx, |
| 1571 | char_u *varp, |
| 1572 | int flags, |
| 1573 | int cp_val) |
| 1574 | { |
| 1575 | char_u *newval; |
| 1576 | |
| 1577 | newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val) |
| 1578 | ? VI_DEFAULT : VIM_DEFAULT]; |
| 1579 | if ((char_u **)varp == &p_bg) |
| 1580 | { |
| 1581 | // guess the value of 'background' |
| 1582 | #ifdef FEAT_GUI |
| 1583 | if (gui.in_use) |
| 1584 | newval = gui_bg_default(); |
| 1585 | else |
| 1586 | #endif |
| 1587 | newval = term_bg_default(); |
| 1588 | } |
| 1589 | else if ((char_u **)varp == &p_fencs && enc_utf8) |
| 1590 | newval = fencs_utf8_default; |
| 1591 | |
| 1592 | // expand environment variables and ~ since the default value was |
| 1593 | // already expanded, only required when an environment variable was set |
| 1594 | // later |
| 1595 | if (newval == NULL) |
| 1596 | newval = empty_option; |
| 1597 | else |
| 1598 | { |
| 1599 | char_u *s = option_expand(opt_idx, newval); |
| 1600 | if (s == NULL) |
| 1601 | s = newval; |
| 1602 | newval = vim_strsave(s); |
| 1603 | } |
| 1604 | |
| 1605 | return newval; |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * Convert the 'backspace' option number value to a string: for adding, |
| 1610 | * prepending and removing string. |
| 1611 | */ |
| 1612 | static void |
| 1613 | opt_backspace_nr2str( |
| 1614 | char_u *varp, |
| 1615 | char_u **origval_p, |
| 1616 | char_u **origval_l_p, |
| 1617 | char_u **origval_g_p, |
| 1618 | char_u **oldval_p) |
| 1619 | { |
| 1620 | int i = getdigits((char_u **)varp); |
| 1621 | |
| 1622 | switch (i) |
| 1623 | { |
| 1624 | case 0: |
| 1625 | *(char_u **)varp = empty_option; |
| 1626 | break; |
| 1627 | case 1: |
| 1628 | *(char_u **)varp = vim_strsave((char_u *)"indent,eol"); |
| 1629 | break; |
| 1630 | case 2: |
| 1631 | *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start"); |
| 1632 | break; |
| 1633 | case 3: |
| 1634 | *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop"); |
| 1635 | break; |
| 1636 | } |
| 1637 | vim_free(*oldval_p); |
| 1638 | if (*origval_p == *oldval_p) |
| 1639 | *origval_p = *(char_u **)varp; |
| 1640 | if (*origval_l_p == *oldval_p) |
| 1641 | *origval_l_p = *(char_u **)varp; |
| 1642 | if (*origval_g_p == *oldval_p) |
| 1643 | *origval_g_p = *(char_u **)varp; |
| 1644 | *oldval_p = *(char_u **)varp; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
| 1648 | * Convert the 'whichwrap' option number value to a string, for backwards |
| 1649 | * compatibility with Vim 3.0. |
| 1650 | * Note: 'argp' is a pointer to a char_u pointer and is updated. |
| 1651 | */ |
| 1652 | static char_u * |
| 1653 | opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap) |
| 1654 | { |
| 1655 | *whichwrap = NUL; |
| 1656 | int i = getdigits(argp); |
| 1657 | if (i & 1) |
| 1658 | STRCAT(whichwrap, "b,"); |
| 1659 | if (i & 2) |
| 1660 | STRCAT(whichwrap, "s,"); |
| 1661 | if (i & 4) |
| 1662 | STRCAT(whichwrap, "h,l,"); |
| 1663 | if (i & 8) |
| 1664 | STRCAT(whichwrap, "<,>,"); |
| 1665 | if (i & 16) |
| 1666 | STRCAT(whichwrap, "[,],"); |
| 1667 | if (*whichwrap != NUL) // remove trailing , |
| 1668 | whichwrap[STRLEN(whichwrap) - 1] = NUL; |
| 1669 | |
| 1670 | return whichwrap; |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * Copy the new string value into allocated memory for the option. |
| 1675 | * Can't use set_string_option_direct(), because we need to remove the |
| 1676 | * backslashes. |
| 1677 | */ |
| 1678 | static char_u * |
| 1679 | stropt_copy_value( |
| 1680 | char_u *origval, |
| 1681 | char_u **argp, |
| 1682 | set_op_T op, |
| 1683 | int flags UNUSED) |
| 1684 | { |
| 1685 | char_u *arg = *argp; |
| 1686 | unsigned newlen; |
| 1687 | char_u *newval; |
| 1688 | char_u *s = NULL; |
| 1689 | |
| 1690 | // get a bit too much |
| 1691 | newlen = (unsigned)STRLEN(arg) + 1; |
| 1692 | if (op != OP_NONE) |
| 1693 | newlen += (unsigned)STRLEN(origval) + 1; |
| 1694 | newval = alloc(newlen); |
| 1695 | if (newval == NULL) // out of mem, don't change |
| 1696 | return NULL; |
| 1697 | s = newval; |
| 1698 | |
| 1699 | // Copy the string, skip over escaped chars. |
| 1700 | // For MS-DOS and WIN32 backslashes before normal file name characters |
| 1701 | // are not removed, and keep backslash at start, for "\\machine\path", |
| 1702 | // but do remove it for "\\\\machine\\path". |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 1703 | // The reverse is found in escape_option_str_cmdline(). |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 1704 | while (*arg != NUL && !VIM_ISWHITE(*arg)) |
| 1705 | { |
| 1706 | int i; |
| 1707 | |
| 1708 | if (*arg == '\\' && arg[1] != NUL |
| 1709 | #ifdef BACKSLASH_IN_FILENAME |
| 1710 | && !((flags & P_EXPAND) |
| 1711 | && vim_isfilec(arg[1]) |
| 1712 | && !VIM_ISWHITE(arg[1]) |
| 1713 | && (arg[1] != '\\' |
| 1714 | || (s == newval && arg[2] != '\\'))) |
| 1715 | #endif |
| 1716 | ) |
| 1717 | ++arg; // remove backslash |
| 1718 | if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1) |
| 1719 | { |
| 1720 | // copy multibyte char |
| 1721 | mch_memmove(s, arg, (size_t)i); |
| 1722 | arg += i; |
| 1723 | s += i; |
| 1724 | } |
| 1725 | else |
| 1726 | *s++ = *arg++; |
| 1727 | } |
| 1728 | *s = NUL; |
| 1729 | |
| 1730 | *argp = arg; |
| 1731 | return newval; |
| 1732 | } |
| 1733 | |
| 1734 | /* |
| 1735 | * Expand environment variables and ~ in string option value 'newval'. |
| 1736 | */ |
| 1737 | static char_u * |
| 1738 | stropt_expand_envvar( |
| 1739 | int opt_idx, |
| 1740 | char_u *origval, |
| 1741 | char_u *newval, |
| 1742 | set_op_T op) |
| 1743 | { |
| 1744 | char_u *s = option_expand(opt_idx, newval); |
| 1745 | if (s == NULL) |
| 1746 | return newval; |
| 1747 | |
| 1748 | vim_free(newval); |
| 1749 | unsigned newlen = (unsigned)STRLEN(s) + 1; |
| 1750 | if (op != OP_NONE) |
| 1751 | newlen += (unsigned)STRLEN(origval) + 1; |
| 1752 | |
| 1753 | newval = alloc(newlen); |
| 1754 | if (newval == NULL) |
| 1755 | return NULL; |
| 1756 | |
| 1757 | STRCPY(newval, s); |
| 1758 | |
| 1759 | return newval; |
| 1760 | } |
| 1761 | |
| 1762 | /* |
| 1763 | * Concatenate the original and new values of a string option, adding a "," if |
| 1764 | * needed. |
| 1765 | */ |
| 1766 | static void |
| 1767 | stropt_concat_with_comma( |
| 1768 | char_u *origval, |
| 1769 | char_u *newval, |
| 1770 | set_op_T op, |
| 1771 | int flags) |
| 1772 | { |
| 1773 | int len = 0; |
| 1774 | |
| 1775 | int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL); |
| 1776 | if (op == OP_ADDING) |
| 1777 | { |
| 1778 | len = (int)STRLEN(origval); |
| 1779 | // strip a trailing comma, would get 2 |
| 1780 | if (comma && len > 1 |
| 1781 | && (flags & P_ONECOMMA) == P_ONECOMMA |
| 1782 | && origval[len - 1] == ',' |
| 1783 | && origval[len - 2] != '\\') |
| 1784 | len--; |
| 1785 | mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1); |
| 1786 | mch_memmove(newval, origval, (size_t)len); |
| 1787 | } |
| 1788 | else |
| 1789 | { |
| 1790 | len = (int)STRLEN(newval); |
| 1791 | STRMOVE(newval + len + comma, origval); |
| 1792 | } |
| 1793 | if (comma) |
| 1794 | newval[len] = ','; |
| 1795 | } |
| 1796 | |
| 1797 | /* |
| 1798 | * Remove a value from a string option. Copy string option value in "origval" |
| 1799 | * to "newval" and then remove the string "strval" of length "len". |
| 1800 | */ |
| 1801 | static void |
| 1802 | stropt_remove_val( |
| 1803 | char_u *origval, |
| 1804 | char_u *newval, |
| 1805 | int flags, |
| 1806 | char_u *strval, |
| 1807 | int len) |
| 1808 | { |
| 1809 | // Remove newval[] from origval[]. (Note: "len" has been set above |
| 1810 | // and is used here). |
| 1811 | STRCPY(newval, origval); |
| 1812 | if (*strval) |
| 1813 | { |
| 1814 | // may need to remove a comma |
| 1815 | if (flags & P_COMMA) |
| 1816 | { |
| 1817 | if (strval == origval) |
| 1818 | { |
| 1819 | // include comma after string |
| 1820 | if (strval[len] == ',') |
| 1821 | ++len; |
| 1822 | } |
| 1823 | else |
| 1824 | { |
| 1825 | // include comma before string |
| 1826 | --strval; |
| 1827 | ++len; |
| 1828 | } |
| 1829 | } |
| 1830 | STRMOVE(newval + (strval - origval), strval + len); |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * Remove flags that appear twice in the string option value 'newval'. |
| 1836 | */ |
| 1837 | static void |
| 1838 | stropt_remove_dupflags(char_u *newval, int flags) |
| 1839 | { |
| 1840 | char_u *s = newval; |
| 1841 | |
| 1842 | // Remove flags that appear twice. |
| 1843 | while (*s) |
| 1844 | { |
| 1845 | // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap' |
| 1846 | if (flags & P_ONECOMMA) |
| 1847 | { |
| 1848 | if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL) |
| 1849 | { |
| 1850 | // Remove the duplicated value and the next comma. |
| 1851 | STRMOVE(s, s + 2); |
| 1852 | continue; |
| 1853 | } |
| 1854 | } |
| 1855 | else |
| 1856 | { |
| 1857 | if ((!(flags & P_COMMA) || *s != ',') |
| 1858 | && vim_strchr(s + 1, *s) != NULL) |
| 1859 | { |
| 1860 | STRMOVE(s, s + 1); |
| 1861 | continue; |
| 1862 | } |
| 1863 | } |
| 1864 | ++s; |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | * Get the string value specified for a ":set" command. The following set |
| 1870 | * options are supported: |
| 1871 | * set {opt}& |
| 1872 | * set {opt}< |
| 1873 | * set {opt}={val} |
| 1874 | * set {opt}:{val} |
| 1875 | */ |
| 1876 | static char_u * |
| 1877 | stropt_get_newval( |
| 1878 | int nextchar, |
| 1879 | int opt_idx, |
| 1880 | char_u **argp, |
| 1881 | char_u *varp, |
| 1882 | char_u **origval_arg, |
| 1883 | char_u **origval_l_arg, |
| 1884 | char_u **origval_g_arg, |
| 1885 | char_u **oldval_arg, |
| 1886 | set_op_T *op_arg, |
| 1887 | int flags, |
| 1888 | int cp_val) |
| 1889 | { |
| 1890 | char_u *arg = *argp; |
| 1891 | char_u *origval = *origval_arg; |
| 1892 | char_u *origval_l = *origval_l_arg; |
| 1893 | char_u *origval_g = *origval_g_arg; |
| 1894 | char_u *oldval = *oldval_arg; |
| 1895 | set_op_T op = *op_arg; |
| 1896 | char_u *save_arg = NULL; |
| 1897 | char_u *newval; |
| 1898 | char_u *s = NULL; |
| 1899 | char_u whichwrap[80]; |
| 1900 | |
| 1901 | if (nextchar == '&') // set to default val |
| 1902 | newval = stropt_get_default_val(opt_idx, varp, flags, cp_val); |
| 1903 | else if (nextchar == '<') // set to global val |
| 1904 | newval = vim_strsave(*(char_u **)get_varp_scope( |
| 1905 | &(options[opt_idx]), OPT_GLOBAL)); |
| 1906 | else |
| 1907 | { |
Yee Cheng Chin | 6d11347 | 2023-10-02 21:38:39 +0200 | [diff] [blame] | 1908 | ++arg; // jump to after the '=' or ':' |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 1909 | |
| 1910 | // Set 'keywordprg' to ":help" if an empty |
| 1911 | // value was passed to :set by the user. |
| 1912 | if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' ')) |
| 1913 | { |
| 1914 | save_arg = arg; |
| 1915 | arg = (char_u *)":help"; |
| 1916 | } |
| 1917 | // Convert 'backspace' number to string |
| 1918 | else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp)) |
| 1919 | opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g, |
| 1920 | &oldval); |
| 1921 | else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg)) |
| 1922 | { |
| 1923 | // Convert 'whichwrap' number to string, for backwards |
| 1924 | // compatibility with Vim 3.0. |
| 1925 | char_u *t = opt_whichwrap_nr2str(&arg, whichwrap); |
| 1926 | save_arg = arg; |
| 1927 | arg = t; |
| 1928 | } |
| 1929 | // Remove '>' before 'dir' and 'bdir', for backwards compatibility with |
| 1930 | // version 3.0 |
| 1931 | else if (*arg == '>' && (varp == (char_u *)&p_dir |
| 1932 | || varp == (char_u *)&p_bdir)) |
| 1933 | ++arg; |
| 1934 | |
| 1935 | // Copy the new string into allocated memory. |
| 1936 | newval = stropt_copy_value(origval, &arg, op, flags); |
| 1937 | if (newval == NULL) |
| 1938 | goto done; |
| 1939 | |
| 1940 | // Expand environment variables and ~. |
| 1941 | // Don't do it when adding without inserting a comma. |
| 1942 | if (op == OP_NONE || (flags & P_COMMA)) |
| 1943 | { |
| 1944 | newval = stropt_expand_envvar(opt_idx, origval, newval, op); |
| 1945 | if (newval == NULL) |
| 1946 | goto done; |
| 1947 | } |
| 1948 | |
| 1949 | // locate newval[] in origval[] when removing it and when adding to |
| 1950 | // avoid duplicates |
| 1951 | int len = 0; |
| 1952 | if (op == OP_REMOVING || (flags & P_NODUP)) |
| 1953 | { |
| 1954 | len = (int)STRLEN(newval); |
| 1955 | s = find_dup_item(origval, newval, flags); |
| 1956 | |
| 1957 | // do not add if already there |
| 1958 | if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL) |
| 1959 | { |
| 1960 | op = OP_NONE; |
| 1961 | STRCPY(newval, origval); |
| 1962 | } |
| 1963 | |
| 1964 | // if no duplicate, move pointer to end of original value |
| 1965 | if (s == NULL) |
| 1966 | s = origval + (int)STRLEN(origval); |
| 1967 | } |
| 1968 | |
| 1969 | // concatenate the two strings; add a ',' if needed |
| 1970 | if (op == OP_ADDING || op == OP_PREPENDING) |
| 1971 | stropt_concat_with_comma(origval, newval, op, flags); |
| 1972 | else if (op == OP_REMOVING) |
| 1973 | // Remove newval[] from origval[]. (Note: "len" has been set above |
| 1974 | // and is used here). |
| 1975 | stropt_remove_val(origval, newval, flags, s, len); |
| 1976 | |
| 1977 | if (flags & P_FLAGLIST) |
| 1978 | // Remove flags that appear twice. |
| 1979 | stropt_remove_dupflags(newval, flags); |
| 1980 | } |
| 1981 | |
| 1982 | done: |
| 1983 | if (save_arg != NULL) |
| 1984 | arg = save_arg; // arg was temporarily changed, restore it |
| 1985 | *argp = arg; |
| 1986 | *origval_arg = origval; |
| 1987 | *origval_l_arg = origval_l; |
| 1988 | *origval_g_arg = origval_g; |
| 1989 | *oldval_arg = oldval; |
| 1990 | *op_arg = op; |
| 1991 | |
| 1992 | return newval; |
| 1993 | } |
| 1994 | |
| 1995 | /* |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 1996 | * Part of do_set() for string options. |
| 1997 | * Returns FAIL on failure, do not process further options. |
| 1998 | */ |
| 1999 | static int |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 2000 | do_set_option_string( |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2001 | int opt_idx, |
| 2002 | int opt_flags, |
Bram Moolenaar | 6f98114 | 2022-09-22 12:48:58 +0100 | [diff] [blame] | 2003 | char_u **argp, |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2004 | int nextchar, |
| 2005 | set_op_T op_arg, |
Yee Cheng Chin | 6ee7b52 | 2023-10-01 09:13:22 +0200 | [diff] [blame] | 2006 | long_u flags, |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2007 | int cp_val, |
| 2008 | char_u *varp_arg, |
| 2009 | char *errbuf, |
Mike Williams | 620f011 | 2023-12-05 15:36:06 +0100 | [diff] [blame] | 2010 | size_t errbuflen, |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2011 | int *value_checked, |
| 2012 | char **errmsg) |
| 2013 | { |
Bram Moolenaar | 6f98114 | 2022-09-22 12:48:58 +0100 | [diff] [blame] | 2014 | char_u *arg = *argp; |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2015 | set_op_T op = op_arg; |
| 2016 | char_u *varp = varp_arg; |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2017 | char_u *oldval = NULL; // previous value if *varp |
| 2018 | char_u *newval; |
| 2019 | char_u *origval = NULL; |
| 2020 | char_u *origval_l = NULL; |
| 2021 | char_u *origval_g = NULL; |
| 2022 | #if defined(FEAT_EVAL) |
| 2023 | char_u *saved_origval = NULL; |
| 2024 | char_u *saved_origval_l = NULL; |
| 2025 | char_u *saved_origval_g = NULL; |
| 2026 | char_u *saved_newval = NULL; |
| 2027 | #endif |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2028 | |
| 2029 | // When using ":set opt=val" for a global option |
| 2030 | // with a local value the local value will be |
| 2031 | // reset, use the global value here. |
| 2032 | if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0 |
| 2033 | && ((int)options[opt_idx].indir & PV_BOTH)) |
| 2034 | varp = options[opt_idx].var; |
| 2035 | |
| 2036 | // The old value is kept until we are sure that the new value is valid. |
| 2037 | oldval = *(char_u **)varp; |
| 2038 | |
| 2039 | if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) |
| 2040 | { |
| 2041 | origval_l = *(char_u **)get_varp_scope( |
| 2042 | &(options[opt_idx]), OPT_LOCAL); |
| 2043 | origval_g = *(char_u **)get_varp_scope( |
| 2044 | &(options[opt_idx]), OPT_GLOBAL); |
| 2045 | |
| 2046 | // A global-local string option might have an empty option as value to |
| 2047 | // indicate that the global value should be used. |
| 2048 | if (((int)options[opt_idx].indir & PV_BOTH) |
| 2049 | && origval_l == empty_option) |
| 2050 | origval_l = origval_g; |
| 2051 | } |
| 2052 | |
| 2053 | // When setting the local value of a global option, the old value may be |
| 2054 | // the global value. |
| 2055 | if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL)) |
| 2056 | origval = *(char_u **)get_varp(&options[opt_idx]); |
| 2057 | else |
| 2058 | origval = oldval; |
| 2059 | |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 2060 | // Get the new value for the option |
| 2061 | newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval, |
| 2062 | &origval_l, &origval_g, &oldval, &op, flags, |
| 2063 | cp_val); |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2064 | |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 2065 | // Set the new value. |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2066 | *(char_u **)(varp) = newval; |
Bram Moolenaar | 339e114 | 2023-02-15 14:26:25 +0000 | [diff] [blame] | 2067 | if (newval == NULL) |
| 2068 | *(char_u **)(varp) = empty_option; |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2069 | |
| 2070 | #if defined(FEAT_EVAL) |
| 2071 | if (!starting |
| 2072 | # ifdef FEAT_CRYPT |
| 2073 | && options[opt_idx].indir != PV_KEY |
| 2074 | # endif |
| 2075 | && origval != NULL && newval != NULL) |
| 2076 | { |
| 2077 | // origval may be freed by did_set_string_option(), make a copy. |
| 2078 | saved_origval = vim_strsave(origval); |
| 2079 | // newval (and varp) may become invalid if the buffer is closed by |
| 2080 | // autocommands. |
| 2081 | saved_newval = vim_strsave(newval); |
| 2082 | if (origval_l != NULL) |
| 2083 | saved_origval_l = vim_strsave(origval_l); |
| 2084 | if (origval_g != NULL) |
| 2085 | saved_origval_g = vim_strsave(origval_g); |
| 2086 | } |
| 2087 | #endif |
| 2088 | |
| 2089 | { |
| 2090 | long_u *p = insecure_flag(opt_idx, opt_flags); |
| 2091 | int secure_saved = secure; |
| 2092 | |
| 2093 | // When an option is set in the sandbox, from a modeline or in secure |
| 2094 | // mode, then deal with side effects in secure mode. Also when the |
| 2095 | // value was set with the P_INSECURE flag and is not completely |
| 2096 | // replaced. |
| 2097 | if ((opt_flags & OPT_MODELINE) |
| 2098 | #ifdef HAVE_SANDBOX |
| 2099 | || sandbox != 0 |
| 2100 | #endif |
| 2101 | || (op != OP_NONE && (*p & P_INSECURE))) |
| 2102 | secure = 1; |
| 2103 | |
| 2104 | // Handle side effects, and set the global value for ":set" on local |
| 2105 | // options. Note: when setting 'syntax' or 'filetype' autocommands may |
| 2106 | // be triggered that can cause havoc. |
| 2107 | *errmsg = did_set_string_option( |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 2108 | opt_idx, (char_u **)varp, oldval, newval, errbuf, |
Christian Brabandt | b39b240 | 2023-11-29 11:34:05 +0100 | [diff] [blame] | 2109 | errbuflen, opt_flags, op, value_checked); |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2110 | |
| 2111 | secure = secure_saved; |
| 2112 | } |
| 2113 | |
| 2114 | #if defined(FEAT_EVAL) |
| 2115 | if (*errmsg == NULL) |
zeertzjq | 269aa2b | 2022-11-28 11:36:50 +0000 | [diff] [blame] | 2116 | trigger_optionset_string(opt_idx, opt_flags, saved_origval, |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2117 | saved_origval_l, saved_origval_g, saved_newval); |
| 2118 | vim_free(saved_origval); |
| 2119 | vim_free(saved_origval_l); |
| 2120 | vim_free(saved_origval_g); |
| 2121 | vim_free(saved_newval); |
| 2122 | #endif |
| 2123 | |
Bram Moolenaar | 6f98114 | 2022-09-22 12:48:58 +0100 | [diff] [blame] | 2124 | *argp = arg; |
Bram Moolenaar | 4740394 | 2022-09-21 21:12:53 +0100 | [diff] [blame] | 2125 | return *errmsg == NULL ? OK : FAIL; |
| 2126 | } |
| 2127 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2128 | /* |
Bram Moolenaar | 546933f | 2023-02-06 16:40:49 +0000 | [diff] [blame] | 2129 | * Set a boolean option. |
| 2130 | * Returns an untranslated error message or NULL. |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2131 | */ |
| 2132 | static char * |
| 2133 | do_set_option_bool( |
| 2134 | int opt_idx, |
| 2135 | int opt_flags, |
| 2136 | set_prefix_T prefix, |
| 2137 | long_u flags, |
| 2138 | char_u *varp, |
| 2139 | int nextchar, |
| 2140 | int afterchar, |
| 2141 | int cp_val) |
| 2142 | |
| 2143 | { |
| 2144 | varnumber_T value; |
| 2145 | |
| 2146 | if (nextchar == '=' || nextchar == ':') |
| 2147 | return e_invalid_argument; |
Bram Moolenaar | 546933f | 2023-02-06 16:40:49 +0000 | [diff] [blame] | 2148 | if (opt_idx < 0 || varp == NULL) |
| 2149 | return NULL; // "cannot happen" |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2150 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 2151 | // ":set opt!": invert |
| 2152 | // ":set opt&": reset to default value |
| 2153 | // ":set opt<": reset to global value |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2154 | if (nextchar == '!') |
| 2155 | value = *(int *)(varp) ^ 1; |
| 2156 | else if (nextchar == '&') |
| 2157 | value = (int)(long)(long_i)options[opt_idx].def_val[ |
| 2158 | ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT]; |
| 2159 | else if (nextchar == '<') |
| 2160 | { |
| 2161 | // For 'autoread' -1 means to use global value. |
| 2162 | if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL) |
| 2163 | value = -1; |
| 2164 | else |
| 2165 | value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL); |
| 2166 | } |
| 2167 | else |
| 2168 | { |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 2169 | // ":set invopt": invert |
| 2170 | // ":set opt" or ":set noopt": set or reset |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2171 | if (nextchar != NUL && !VIM_ISWHITE(afterchar)) |
| 2172 | return e_trailing_characters; |
| 2173 | if (prefix == PREFIX_INV) |
| 2174 | value = *(int *)(varp) ^ 1; |
| 2175 | else |
| 2176 | value = prefix == PREFIX_NO ? 0 : 1; |
| 2177 | } |
| 2178 | |
| 2179 | return set_bool_option(opt_idx, varp, (int)value, opt_flags); |
| 2180 | } |
| 2181 | |
| 2182 | /* |
Bram Moolenaar | 546933f | 2023-02-06 16:40:49 +0000 | [diff] [blame] | 2183 | * Set a numeric option. |
| 2184 | * Returns an untranslated error message or NULL. |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2185 | */ |
| 2186 | static char * |
| 2187 | do_set_option_numeric( |
| 2188 | int opt_idx, |
| 2189 | int opt_flags, |
| 2190 | char_u **argp, |
| 2191 | int nextchar, |
| 2192 | set_op_T op, |
| 2193 | long_u flags, |
| 2194 | int cp_val, |
| 2195 | char_u *varp, |
| 2196 | char *errbuf, |
| 2197 | size_t errbuflen) |
| 2198 | { |
| 2199 | char_u *arg = *argp; |
| 2200 | varnumber_T value; |
| 2201 | int i; |
| 2202 | char *errmsg = NULL; |
| 2203 | |
Bram Moolenaar | 546933f | 2023-02-06 16:40:49 +0000 | [diff] [blame] | 2204 | if (opt_idx < 0 || varp == NULL) |
| 2205 | return NULL; // "cannot happen" |
| 2206 | // |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2207 | /* |
| 2208 | * Different ways to set a number option: |
| 2209 | * & set to default value |
| 2210 | * < set to global value |
| 2211 | * <xx> accept special key codes for 'wildchar' |
| 2212 | * c accept any non-digit for 'wildchar' |
| 2213 | * [-]0-9 set number |
| 2214 | * other error |
| 2215 | */ |
| 2216 | ++arg; |
| 2217 | if (nextchar == '&') |
| 2218 | value = (long)(long_i)options[opt_idx].def_val[ |
| 2219 | ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT]; |
| 2220 | else if (nextchar == '<') |
| 2221 | { |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2222 | if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL) |
Bram Moolenaar | bf5f189 | 2023-06-27 21:51:07 +0100 | [diff] [blame] | 2223 | // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2224 | value = NO_LOCAL_UNDOLEVEL; |
Bram Moolenaar | bf5f189 | 2023-06-27 21:51:07 +0100 | [diff] [blame] | 2225 | else if (opt_flags == OPT_LOCAL |
| 2226 | && ((long *)varp == &curwin->w_p_siso |
| 2227 | || (long *)varp == &curwin->w_p_so)) |
| 2228 | // for 'scrolloff'/'sidescrolloff' -1 means using the global value |
| 2229 | value = -1; |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2230 | else |
| 2231 | value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL); |
| 2232 | } |
| 2233 | else if (((long *)varp == &p_wc || (long *)varp == &p_wcm) |
| 2234 | && (*arg == '<' |
| 2235 | || *arg == '^' |
| 2236 | || (*arg != NUL |
| 2237 | && (!arg[1] || VIM_ISWHITE(arg[1])) |
| 2238 | && !VIM_ISDIGIT(*arg)))) |
| 2239 | { |
| 2240 | value = string_to_key(arg, FALSE); |
| 2241 | if (value == 0 && (long *)varp != &p_wcm) |
| 2242 | { |
| 2243 | errmsg = e_invalid_argument; |
| 2244 | goto skip; |
| 2245 | } |
| 2246 | } |
| 2247 | else if (*arg == '-' || VIM_ISDIGIT(*arg)) |
| 2248 | { |
| 2249 | // Allow negative (for 'undolevels'), octal and hex numbers. |
Bram Moolenaar | 5fb78c3 | 2023-03-04 20:47:39 +0000 | [diff] [blame] | 2250 | vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL); |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2251 | if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i]))) |
| 2252 | { |
| 2253 | errmsg = e_number_required_after_equal; |
| 2254 | goto skip; |
| 2255 | } |
| 2256 | } |
| 2257 | else |
| 2258 | { |
| 2259 | errmsg = e_number_required_after_equal; |
| 2260 | goto skip; |
| 2261 | } |
| 2262 | |
| 2263 | if (op == OP_ADDING) |
| 2264 | value = *(long *)varp + value; |
| 2265 | else if (op == OP_PREPENDING) |
| 2266 | value = *(long *)varp * value; |
| 2267 | else if (op == OP_REMOVING) |
| 2268 | value = *(long *)varp - value; |
| 2269 | |
| 2270 | errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen, |
| 2271 | opt_flags); |
| 2272 | |
| 2273 | skip: |
| 2274 | *argp = arg; |
| 2275 | return errmsg; |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * Set a key code (t_xx) option |
| 2280 | */ |
| 2281 | static char * |
| 2282 | do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar) |
| 2283 | { |
| 2284 | char_u *arg = *argp; |
| 2285 | char_u *p; |
| 2286 | |
| 2287 | if (nextchar == '&') |
| 2288 | { |
| 2289 | if (add_termcap_entry(key_name, TRUE) == FAIL) |
| 2290 | return e_not_found_in_termcap; |
| 2291 | } |
| 2292 | else |
| 2293 | { |
| 2294 | ++arg; // jump to after the '=' or ':' |
| 2295 | for (p = arg; *p && !VIM_ISWHITE(*p); ++p) |
| 2296 | if (*p == '\\' && p[1] != NUL) |
| 2297 | ++p; |
| 2298 | nextchar = *p; |
| 2299 | *p = NUL; |
| 2300 | add_termcode(key_name, arg, FALSE); |
| 2301 | *p = nextchar; |
| 2302 | } |
| 2303 | if (full_screen) |
| 2304 | ttest(FALSE); |
| 2305 | redraw_all_later(UPD_CLEAR); |
| 2306 | |
| 2307 | *argp = arg; |
| 2308 | return NULL; |
| 2309 | } |
| 2310 | |
| 2311 | /* |
| 2312 | * Set an option to a new value. |
| 2313 | */ |
| 2314 | static char * |
| 2315 | do_set_option_value( |
| 2316 | int opt_idx, |
| 2317 | int opt_flags, |
| 2318 | char_u **argp, |
| 2319 | set_prefix_T prefix, |
| 2320 | set_op_T op, |
| 2321 | long_u flags, |
| 2322 | char_u *varp, |
| 2323 | char_u *key_name, |
| 2324 | int nextchar, |
| 2325 | int afterchar, |
| 2326 | int cp_val, |
| 2327 | int *stopopteval, |
| 2328 | char *errbuf, |
| 2329 | size_t errbuflen) |
| 2330 | { |
| 2331 | int value_checked = FALSE; |
| 2332 | char *errmsg = NULL; |
| 2333 | char_u *arg = *argp; |
| 2334 | |
| 2335 | if (flags & P_BOOL) |
| 2336 | { |
| 2337 | // boolean option |
| 2338 | errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp, |
| 2339 | nextchar, afterchar, cp_val); |
| 2340 | if (errmsg != NULL) |
| 2341 | goto skip; |
| 2342 | } |
| 2343 | else |
| 2344 | { |
| 2345 | // numeric or string option |
| 2346 | if (vim_strchr((char_u *)"=:&<", nextchar) == NULL |
| 2347 | || prefix != PREFIX_NONE) |
| 2348 | { |
| 2349 | errmsg = e_invalid_argument; |
| 2350 | goto skip; |
| 2351 | } |
| 2352 | |
| 2353 | if (flags & P_NUM) |
| 2354 | { |
| 2355 | // numeric option |
| 2356 | errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar, |
| 2357 | op, flags, cp_val, varp, |
| 2358 | errbuf, errbuflen); |
| 2359 | if (errmsg != NULL) |
| 2360 | goto skip; |
| 2361 | } |
| 2362 | else if (opt_idx >= 0) |
| 2363 | { |
| 2364 | // string option |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 2365 | if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op, |
Christian Brabandt | b39b240 | 2023-11-29 11:34:05 +0100 | [diff] [blame] | 2366 | flags, cp_val, varp, errbuf, errbuflen, |
Yegappan Lakshmanan | 1a64764 | 2023-02-14 13:07:18 +0000 | [diff] [blame] | 2367 | &value_checked, &errmsg) == FAIL) |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2368 | { |
| 2369 | if (errmsg != NULL) |
| 2370 | goto skip; |
| 2371 | *stopopteval = TRUE; |
| 2372 | goto skip; |
| 2373 | } |
| 2374 | } |
| 2375 | else |
| 2376 | { |
| 2377 | // key code option |
| 2378 | errmsg = do_set_option_keycode(&arg, key_name, nextchar); |
| 2379 | if (errmsg != NULL) |
| 2380 | goto skip; |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | if (opt_idx >= 0) |
| 2385 | did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked); |
| 2386 | |
| 2387 | skip: |
| 2388 | *argp = arg; |
| 2389 | return errmsg; |
| 2390 | } |
| 2391 | |
| 2392 | /* |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2393 | * Set an option to a new value. |
| 2394 | * Return NULL if OK, return an untranslated error message when something is |
| 2395 | * wrong. "errbuf[errbuflen]" can be used to create the error message. |
| 2396 | */ |
| 2397 | static char * |
| 2398 | do_set_option( |
| 2399 | int opt_flags, |
| 2400 | char_u **argp, |
| 2401 | char_u *arg_start, |
| 2402 | char_u **startarg, |
| 2403 | int *did_show, |
| 2404 | int *stopopteval, |
| 2405 | char *errbuf, |
| 2406 | size_t errbuflen) |
| 2407 | { |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2408 | int opt_idx; |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2409 | char_u *arg; |
| 2410 | set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix |
| 2411 | set_op_T op; |
| 2412 | long_u flags; // flags for current option |
| 2413 | char_u *varp; // pointer to variable for current option |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2414 | char_u key_name[2]; |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2415 | int nextchar; // next non-white char after option name |
| 2416 | int afterchar; // character just after option name |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2417 | char *errmsg = NULL; |
| 2418 | int key; |
| 2419 | int len; |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2420 | |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2421 | prefix = get_option_prefix(argp); |
| 2422 | arg = *argp; |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2423 | |
| 2424 | // find end of name |
| 2425 | key = 0; |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2426 | if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL) |
| 2427 | return e_invalid_argument; |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2428 | |
| 2429 | // remember character after option name |
| 2430 | afterchar = arg[len]; |
| 2431 | |
| 2432 | if (in_vim9script()) |
| 2433 | { |
| 2434 | char_u *p = skipwhite(arg + len); |
| 2435 | |
| 2436 | // disallow white space before =val, +=val, -=val, ^=val |
| 2437 | if (p > arg + len && (p[0] == '=' |
| 2438 | || (vim_strchr((char_u *)"+-^", p[0]) != NULL |
| 2439 | && p[1] == '='))) |
| 2440 | { |
| 2441 | errmsg = e_no_white_space_allowed_between_option_and; |
| 2442 | arg = p; |
| 2443 | *startarg = p; |
| 2444 | goto skip; |
| 2445 | } |
| 2446 | } |
| 2447 | else |
| 2448 | // skip white space, allow ":set ai ?", ":set hlsearch !" |
| 2449 | while (VIM_ISWHITE(arg[len])) |
| 2450 | ++len; |
| 2451 | |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2452 | op = get_opt_op(arg + len); |
| 2453 | if (op != OP_NONE) |
| 2454 | len++; |
| 2455 | |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2456 | nextchar = arg[len]; |
| 2457 | |
| 2458 | if (opt_idx == -1 && key == 0) // found a mismatch: skip |
| 2459 | { |
| 2460 | if (in_vim9script() && arg > arg_start |
| 2461 | && vim_strchr((char_u *)"!&<", *arg) != NULL) |
| 2462 | errmsg = e_no_white_space_allowed_between_option_and; |
| 2463 | else |
| 2464 | errmsg = e_unknown_option; |
| 2465 | goto skip; |
| 2466 | } |
| 2467 | |
| 2468 | if (opt_idx >= 0) |
| 2469 | { |
| 2470 | if (options[opt_idx].var == NULL) // hidden option: skip |
| 2471 | { |
| 2472 | // Only give an error message when requesting the value of |
| 2473 | // a hidden option, ignore setting it. |
| 2474 | if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL |
| 2475 | && (!(options[opt_idx].flags & P_BOOL) |
| 2476 | || nextchar == '?')) |
| 2477 | errmsg = e_option_not_supported; |
| 2478 | goto skip; |
| 2479 | } |
| 2480 | |
| 2481 | flags = options[opt_idx].flags; |
| 2482 | varp = get_varp_scope(&(options[opt_idx]), opt_flags); |
| 2483 | } |
| 2484 | else |
| 2485 | { |
| 2486 | flags = P_STRING; |
Bram Moolenaar | 40b4872 | 2023-02-05 17:04:50 +0000 | [diff] [blame] | 2487 | varp = NULL; |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2488 | if (key < 0) |
| 2489 | { |
| 2490 | key_name[0] = KEY2TERMCAP0(key); |
| 2491 | key_name[1] = KEY2TERMCAP1(key); |
| 2492 | } |
| 2493 | else |
| 2494 | { |
| 2495 | key_name[0] = KS_KEY; |
| 2496 | key_name[1] = (key & 0xff); |
| 2497 | } |
| 2498 | } |
| 2499 | |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2500 | // Make sure the option value can be changed. |
| 2501 | if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL) |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2502 | goto skip; |
| 2503 | |
Bram Moolenaar | 40b4872 | 2023-02-05 17:04:50 +0000 | [diff] [blame] | 2504 | int cp_val = p_cp; |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2505 | if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL) |
| 2506 | { |
| 2507 | arg += len; |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2508 | if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i') |
| 2509 | { |
| 2510 | if (arg[3] == 'm') // "opt&vim": set to Vim default |
| 2511 | { |
| 2512 | cp_val = FALSE; |
| 2513 | arg += 3; |
| 2514 | } |
| 2515 | else // "opt&vi": set to Vi default |
| 2516 | { |
| 2517 | cp_val = TRUE; |
| 2518 | arg += 2; |
| 2519 | } |
| 2520 | } |
| 2521 | if (vim_strchr((char_u *)"?!&<", nextchar) != NULL |
| 2522 | && arg[1] != NUL && !VIM_ISWHITE(arg[1])) |
| 2523 | { |
| 2524 | errmsg = e_trailing_characters; |
| 2525 | goto skip; |
| 2526 | } |
| 2527 | } |
| 2528 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 2529 | // Allow '=' and ':' for historical reasons (MSDOS command.com). |
| 2530 | // Allows only one '=' character per "set" command line. grrr. (jw) |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2531 | if (nextchar == '?' |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2532 | || (prefix == PREFIX_NONE |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2533 | && vim_strchr((char_u *)"=:&<", nextchar) == NULL |
| 2534 | && !(flags & P_BOOL))) |
| 2535 | { |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 2536 | // print value |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2537 | if (*did_show) |
| 2538 | msg_putchar('\n'); // cursor below last one |
| 2539 | else |
| 2540 | { |
| 2541 | gotocmdline(TRUE); // cursor at status line |
| 2542 | *did_show = TRUE; // remember that we did a line |
| 2543 | } |
| 2544 | if (opt_idx >= 0) |
| 2545 | { |
| 2546 | showoneopt(&options[opt_idx], opt_flags); |
| 2547 | #ifdef FEAT_EVAL |
| 2548 | if (p_verbose > 0) |
| 2549 | { |
| 2550 | // Mention where the option was last set. |
| 2551 | if (varp == options[opt_idx].var) |
| 2552 | last_set_msg(options[opt_idx].script_ctx); |
| 2553 | else if ((int)options[opt_idx].indir & PV_WIN) |
| 2554 | last_set_msg(curwin->w_p_script_ctx[ |
| 2555 | (int)options[opt_idx].indir & PV_MASK]); |
| 2556 | else if ((int)options[opt_idx].indir & PV_BUF) |
| 2557 | last_set_msg(curbuf->b_p_script_ctx[ |
| 2558 | (int)options[opt_idx].indir & PV_MASK]); |
| 2559 | } |
| 2560 | #endif |
| 2561 | } |
| 2562 | else |
| 2563 | { |
| 2564 | char_u *p; |
| 2565 | |
| 2566 | p = find_termcode(key_name); |
| 2567 | if (p == NULL) |
| 2568 | { |
| 2569 | errmsg = e_key_code_not_set; |
| 2570 | goto skip; |
| 2571 | } |
| 2572 | else |
| 2573 | (void)show_one_termcode(key_name, p, TRUE); |
| 2574 | } |
| 2575 | if (nextchar != '?' |
| 2576 | && nextchar != NUL && !VIM_ISWHITE(afterchar)) |
| 2577 | errmsg = e_trailing_characters; |
| 2578 | } |
| 2579 | else |
| 2580 | { |
Yegappan Lakshmanan | c72078b | 2023-02-05 16:02:35 +0000 | [diff] [blame] | 2581 | errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op, |
| 2582 | flags, varp, key_name, nextchar, |
| 2583 | afterchar, cp_val, stopopteval, errbuf, |
| 2584 | errbuflen); |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | skip: |
| 2588 | *argp = arg; |
| 2589 | return errmsg; |
| 2590 | } |
| 2591 | |
| 2592 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2593 | * Parse 'arg' for option settings. |
| 2594 | * |
| 2595 | * 'arg' may be IObuff, but only when no errors can be present and option |
| 2596 | * does not need to be expanded with option_expand(). |
| 2597 | * "opt_flags": |
| 2598 | * 0 for ":set" |
Bram Moolenaar | 15a24f0 | 2021-12-03 20:43:24 +0000 | [diff] [blame] | 2599 | * OPT_GLOBAL for ":setglobal" |
| 2600 | * OPT_LOCAL for ":setlocal" and a modeline |
| 2601 | * OPT_MODELINE for a modeline |
| 2602 | * OPT_WINONLY to only set window-local options |
| 2603 | * OPT_NOWIN to skip setting window-local options |
| 2604 | * OPT_ONECOLUMN do not use multiple columns |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2605 | * |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2606 | * Returns FAIL if an error is detected, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2607 | */ |
| 2608 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2609 | do_set( |
Bram Moolenaar | 1594f31 | 2021-07-08 16:40:13 +0200 | [diff] [blame] | 2610 | char_u *arg_start, // option string (may be written to!) |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2611 | int opt_flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2612 | { |
Bram Moolenaar | 1594f31 | 2021-07-08 16:40:13 +0200 | [diff] [blame] | 2613 | char_u *arg = arg_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2614 | int i; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2615 | int did_show = FALSE; // already showed one value |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2616 | |
| 2617 | if (*arg == NUL) |
| 2618 | { |
| 2619 | showoptions(0, opt_flags); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2620 | did_show = TRUE; |
| 2621 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2624 | while (*arg != NUL) // loop to process all options |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2625 | { |
zeertzjq | 0ef9a5c | 2023-01-17 21:38:25 +0000 | [diff] [blame] | 2626 | if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3]) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 2627 | && !(opt_flags & OPT_MODELINE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2628 | { |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 2629 | // ":set all" show all options. |
| 2630 | // ":set all&" set all options to their default value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2631 | arg += 3; |
| 2632 | if (*arg == '&') |
| 2633 | { |
| 2634 | ++arg; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2635 | // Only for :set command set global value of local options. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2636 | set_options_default(OPT_FREE | opt_flags); |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 2637 | didset_options(); |
| 2638 | didset_options2(); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 2639 | redraw_all_later(UPD_CLEAR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2640 | } |
| 2641 | else |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2642 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2643 | showoptions(1, opt_flags); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2644 | did_show = TRUE; |
| 2645 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2646 | } |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 2647 | else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2648 | { |
| 2649 | showoptions(2, opt_flags); |
Bram Moolenaar | 15a24f0 | 2021-12-03 20:43:24 +0000 | [diff] [blame] | 2650 | show_termcodes(opt_flags); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2651 | did_show = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2652 | arg += 7; |
| 2653 | } |
| 2654 | else |
| 2655 | { |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2656 | int stopopteval = FALSE; |
| 2657 | char *errmsg = NULL; |
Christian Brabandt | b39b240 | 2023-11-29 11:34:05 +0100 | [diff] [blame] | 2658 | char errbuf[ERR_BUFLEN]; |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2659 | char_u *startarg = arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2660 | |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2661 | errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg, |
| 2662 | &did_show, &stopopteval, errbuf, |
Christian Brabandt | b39b240 | 2023-11-29 11:34:05 +0100 | [diff] [blame] | 2663 | ERR_BUFLEN); |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2664 | if (stopopteval) |
| 2665 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2666 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 2667 | // Advance to next argument. |
| 2668 | // - skip until a blank found, taking care of backslashes |
| 2669 | // - skip blanks |
| 2670 | // - skip one "=val" argument (for hidden options ":set gfn =xx") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2671 | for (i = 0; i < 2 ; ++i) |
| 2672 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2673 | while (*arg != NUL && !VIM_ISWHITE(*arg)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2674 | if (*arg++ == '\\' && *arg != NUL) |
| 2675 | ++arg; |
| 2676 | arg = skipwhite(arg); |
| 2677 | if (*arg != '=') |
| 2678 | break; |
| 2679 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2680 | |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2681 | if (errmsg != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2682 | { |
Yegappan Lakshmanan | 78012f5 | 2023-02-02 16:34:11 +0000 | [diff] [blame] | 2683 | vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1); |
| 2684 | i = (int)STRLEN(IObuff) + 2; |
| 2685 | if (i + (arg - startarg) < IOSIZE) |
| 2686 | { |
| 2687 | // append the argument with the error |
| 2688 | STRCAT(IObuff, ": "); |
| 2689 | mch_memmove(IObuff + i, startarg, (arg - startarg)); |
| 2690 | IObuff[i + (arg - startarg)] = NUL; |
| 2691 | } |
| 2692 | // make sure all characters are printable |
| 2693 | trans_characters(IObuff, IOSIZE); |
| 2694 | |
| 2695 | ++no_wait_return; // wait_return() done later |
| 2696 | emsg((char *)IObuff); // show error highlighted |
| 2697 | --no_wait_return; |
| 2698 | |
| 2699 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2700 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | arg = skipwhite(arg); |
| 2704 | } |
| 2705 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2706 | theend: |
| 2707 | if (silent_mode && did_show) |
| 2708 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2709 | // After displaying option values in silent mode. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2710 | silent_mode = FALSE; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2711 | info_message = TRUE; // use mch_msg(), not mch_errmsg() |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2712 | msg_putchar('\n'); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2713 | cursor_on(); // msg_start() switches it off |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2714 | out_flush(); |
| 2715 | silent_mode = TRUE; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2716 | info_message = FALSE; // use mch_msg(), not mch_errmsg() |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2719 | return OK; |
| 2720 | } |
| 2721 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2722 | /* |
| 2723 | * Call this when an option has been given a new value through a user command. |
| 2724 | * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag. |
| 2725 | */ |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 2726 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2727 | did_set_option( |
| 2728 | int opt_idx, |
Bram Moolenaar | 916a818 | 2018-11-25 02:18:29 +0100 | [diff] [blame] | 2729 | int opt_flags, // possibly with OPT_MODELINE |
| 2730 | int new_value, // value was replaced completely |
| 2731 | int value_checked) // value was checked to be safe, no need to set the |
| 2732 | // P_INSECURE flag. |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2733 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2734 | long_u *p; |
| 2735 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2736 | options[opt_idx].flags |= P_WAS_SET; |
| 2737 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2738 | // When an option is set in the sandbox, from a modeline or in secure mode |
| 2739 | // set the P_INSECURE flag. Otherwise, if a new value is stored reset the |
| 2740 | // flag. |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2741 | p = insecure_flag(opt_idx, opt_flags); |
Bram Moolenaar | 916a818 | 2018-11-25 02:18:29 +0100 | [diff] [blame] | 2742 | if (!value_checked && (secure |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2743 | #ifdef HAVE_SANDBOX |
| 2744 | || sandbox != 0 |
| 2745 | #endif |
Bram Moolenaar | 916a818 | 2018-11-25 02:18:29 +0100 | [diff] [blame] | 2746 | || (opt_flags & OPT_MODELINE))) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2747 | *p = *p | P_INSECURE; |
| 2748 | else if (new_value) |
| 2749 | *p = *p & ~P_INSECURE; |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2752 | /* |
| 2753 | * Convert a key name or string into a key value. |
| 2754 | * Used for 'wildchar' and 'cedit' options. |
Bram Moolenaar | dbe948d | 2017-07-23 22:50:51 +0200 | [diff] [blame] | 2755 | * When "multi_byte" is TRUE allow for multi-byte characters. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2756 | */ |
Bram Moolenaar | dbe948d | 2017-07-23 22:50:51 +0200 | [diff] [blame] | 2757 | int |
| 2758 | string_to_key(char_u *arg, int multi_byte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2759 | { |
| 2760 | if (*arg == '<') |
Bram Moolenaar | 9cf4b50 | 2018-07-23 04:12:03 +0200 | [diff] [blame] | 2761 | return find_key_option(arg + 1, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2762 | if (*arg == '^') |
| 2763 | return Ctrl_chr(arg[1]); |
Bram Moolenaar | dbe948d | 2017-07-23 22:50:51 +0200 | [diff] [blame] | 2764 | if (multi_byte) |
| 2765 | return PTR2CHAR(arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2766 | return *arg; |
| 2767 | } |
| 2768 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2769 | /* |
| 2770 | * When changing 'title', 'titlestring', 'icon' or 'iconstring', call |
| 2771 | * maketitle() to create and display it. |
| 2772 | * When switching the title or icon off, call mch_restore_title() to get |
| 2773 | * the old value back. |
| 2774 | */ |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 2775 | void |
Bram Moolenaar | 84a9308 | 2018-06-16 22:58:15 +0200 | [diff] [blame] | 2776 | did_set_title(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2777 | { |
| 2778 | if (starting != NO_SCREEN |
| 2779 | #ifdef FEAT_GUI |
| 2780 | && !gui.starting |
| 2781 | #endif |
| 2782 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2783 | maketitle(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2784 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2785 | |
| 2786 | /* |
| 2787 | * set_options_bin - called when 'bin' changes value. |
| 2788 | */ |
| 2789 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2790 | set_options_bin( |
| 2791 | int oldval, |
| 2792 | int newval, |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2793 | int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2794 | { |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 2795 | // The option values that are changed when 'bin' changes are |
| 2796 | // copied when 'bin is set and restored when 'bin' is reset. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2797 | if (newval) |
| 2798 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2799 | if (!oldval) // switched on |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2800 | { |
| 2801 | if (!(opt_flags & OPT_GLOBAL)) |
| 2802 | { |
| 2803 | curbuf->b_p_tw_nobin = curbuf->b_p_tw; |
| 2804 | curbuf->b_p_wm_nobin = curbuf->b_p_wm; |
| 2805 | curbuf->b_p_ml_nobin = curbuf->b_p_ml; |
| 2806 | curbuf->b_p_et_nobin = curbuf->b_p_et; |
| 2807 | } |
| 2808 | if (!(opt_flags & OPT_LOCAL)) |
| 2809 | { |
| 2810 | p_tw_nobin = p_tw; |
| 2811 | p_wm_nobin = p_wm; |
| 2812 | p_ml_nobin = p_ml; |
| 2813 | p_et_nobin = p_et; |
| 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | if (!(opt_flags & OPT_GLOBAL)) |
| 2818 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2819 | curbuf->b_p_tw = 0; // no automatic line wrap |
| 2820 | curbuf->b_p_wm = 0; // no automatic line wrap |
| 2821 | curbuf->b_p_ml = 0; // no modelines |
| 2822 | curbuf->b_p_et = 0; // no expandtab |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2823 | } |
| 2824 | if (!(opt_flags & OPT_LOCAL)) |
| 2825 | { |
| 2826 | p_tw = 0; |
| 2827 | p_wm = 0; |
| 2828 | p_ml = FALSE; |
| 2829 | p_et = FALSE; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2830 | p_bin = TRUE; // needed when called for the "-b" argument |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2831 | } |
| 2832 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2833 | else if (oldval) // switched off |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2834 | { |
| 2835 | if (!(opt_flags & OPT_GLOBAL)) |
| 2836 | { |
| 2837 | curbuf->b_p_tw = curbuf->b_p_tw_nobin; |
| 2838 | curbuf->b_p_wm = curbuf->b_p_wm_nobin; |
| 2839 | curbuf->b_p_ml = curbuf->b_p_ml_nobin; |
| 2840 | curbuf->b_p_et = curbuf->b_p_et_nobin; |
| 2841 | } |
| 2842 | if (!(opt_flags & OPT_LOCAL)) |
| 2843 | { |
| 2844 | p_tw = p_tw_nobin; |
| 2845 | p_wm = p_wm_nobin; |
| 2846 | p_ml = p_ml_nobin; |
| 2847 | p_et = p_et_nobin; |
| 2848 | } |
| 2849 | } |
Christian Brabandt | 757593c | 2023-08-22 21:44:10 +0200 | [diff] [blame] | 2850 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2851 | // Remember where the dependent option were reset |
| 2852 | didset_options_sctx(opt_flags, p_bin_dep_opts); |
| 2853 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2856 | /* |
| 2857 | * Expand environment variables for some string options. |
| 2858 | * These string options cannot be indirect! |
| 2859 | * If "val" is NULL expand the current value of the option. |
| 2860 | * Return pointer to NameBuff, or NULL when not expanded. |
| 2861 | */ |
| 2862 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2863 | option_expand(int opt_idx, char_u *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2864 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2865 | // if option doesn't need expansion nothing to do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2866 | if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL) |
| 2867 | return NULL; |
| 2868 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2869 | // If val is longer than MAXPATHL no meaningful expansion can be done, |
| 2870 | // expand_env() would truncate the string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2871 | if (val != NULL && STRLEN(val) > MAXPATHL) |
| 2872 | return NULL; |
| 2873 | |
| 2874 | if (val == NULL) |
| 2875 | val = *(char_u **)options[opt_idx].var; |
| 2876 | |
| 2877 | /* |
| 2878 | * Expanding this with NameBuff, expand_env() must not be passed IObuff. |
| 2879 | * Escape spaces when expanding 'tags', they are used to separate file |
| 2880 | * names. |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 2881 | * For 'spellsuggest' expand after "file:". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2882 | */ |
| 2883 | expand_env_esc(val, NameBuff, MAXPATHL, |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 2884 | (char_u **)options[opt_idx].var == &p_tags, FALSE, |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2885 | #ifdef FEAT_SPELL |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 2886 | (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" : |
| 2887 | #endif |
| 2888 | NULL); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2889 | if (STRCMP(NameBuff, val) == 0) // they are the same |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2890 | return NULL; |
| 2891 | |
| 2892 | return NameBuff; |
| 2893 | } |
| 2894 | |
| 2895 | /* |
| 2896 | * After setting various option values: recompute variables that depend on |
| 2897 | * option values. |
| 2898 | */ |
| 2899 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2900 | didset_options(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2901 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2902 | // initialize the table for 'iskeyword' et.al. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2903 | (void)init_chartab(); |
| 2904 | |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 2905 | didset_string_options(); |
| 2906 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2907 | #ifdef FEAT_SPELL |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 2908 | (void)spell_check_msm(); |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 2909 | (void)spell_check_sps(); |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 2910 | (void)compile_cap_prog(curwin->w_s); |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 2911 | (void)did_set_spell_option(TRUE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2912 | #endif |
Bram Moolenaar | 010ee96 | 2019-09-25 20:37:36 +0200 | [diff] [blame] | 2913 | // set cedit_key |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 2914 | (void)did_set_cedit(NULL); |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2915 | #ifdef FEAT_LINEBREAK |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2916 | // initialize the table for 'breakat'. |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 2917 | did_set_breakat(NULL); |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 2918 | #endif |
Bram Moolenaar | 010ee96 | 2019-09-25 20:37:36 +0200 | [diff] [blame] | 2919 | after_copy_winopt(curwin); |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | /* |
| 2923 | * More side effects of setting options. |
| 2924 | */ |
| 2925 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2926 | didset_options2(void) |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 2927 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2928 | // Initialize the highlight_attr[] table. |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 2929 | (void)highlight_changed(); |
| 2930 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2931 | // Parse default for 'wildmode' |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 2932 | check_opt_wim(); |
| 2933 | |
Bram Moolenaar | eed9d46 | 2021-02-15 20:38:25 +0100 | [diff] [blame] | 2934 | // Parse default for 'listchars'. |
zeertzjq | 6a8d2e1 | 2024-01-17 20:54:49 +0100 | [diff] [blame] | 2935 | (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE, NULL, 0); |
Bram Moolenaar | eed9d46 | 2021-02-15 20:38:25 +0100 | [diff] [blame] | 2936 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2937 | // Parse default for 'fillchars'. |
zeertzjq | 6a8d2e1 | 2024-01-17 20:54:49 +0100 | [diff] [blame] | 2938 | (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE, NULL, 0); |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 2939 | |
| 2940 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 2941 | // Parse default for 'clipboard' |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 2942 | (void)did_set_clipboard(NULL); |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 2943 | #endif |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 2944 | #ifdef FEAT_VARTABS |
Bram Moolenaar | 55c77cf | 2019-02-16 19:05:11 +0100 | [diff] [blame] | 2945 | vim_free(curbuf->b_p_vsts_array); |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 2946 | (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array); |
Bram Moolenaar | 55c77cf | 2019-02-16 19:05:11 +0100 | [diff] [blame] | 2947 | vim_free(curbuf->b_p_vts_array); |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 2948 | (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 2949 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | /* |
| 2953 | * Check for string options that are NULL (normally only termcap options). |
| 2954 | */ |
| 2955 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2956 | check_options(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2957 | { |
| 2958 | int opt_idx; |
| 2959 | |
| 2960 | for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++) |
| 2961 | if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL) |
| 2962 | check_string_option((char_u **)get_varp(&(options[opt_idx]))); |
| 2963 | } |
| 2964 | |
| 2965 | /* |
Bram Moolenaar | 35bc7d6 | 2018-10-02 14:45:10 +0200 | [diff] [blame] | 2966 | * Return the option index found by a pointer into term_strings[]. |
| 2967 | * Return -1 if not found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2968 | */ |
Bram Moolenaar | 35bc7d6 | 2018-10-02 14:45:10 +0200 | [diff] [blame] | 2969 | int |
| 2970 | get_term_opt_idx(char_u **p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2971 | { |
Bram Moolenaar | 35bc7d6 | 2018-10-02 14:45:10 +0200 | [diff] [blame] | 2972 | int opt_idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2973 | |
| 2974 | for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++) |
| 2975 | if (options[opt_idx].var == (char_u *)p) |
Bram Moolenaar | 35bc7d6 | 2018-10-02 14:45:10 +0200 | [diff] [blame] | 2976 | return opt_idx; |
| 2977 | return -1; // cannot happen: didn't find it! |
| 2978 | } |
| 2979 | |
| 2980 | /* |
| 2981 | * Mark a terminal option as allocated, found by a pointer into term_strings[]. |
| 2982 | * Return the option index or -1 if not found. |
| 2983 | */ |
| 2984 | int |
| 2985 | set_term_option_alloced(char_u **p) |
| 2986 | { |
| 2987 | int opt_idx = get_term_opt_idx(p); |
| 2988 | |
| 2989 | if (opt_idx >= 0) |
| 2990 | options[opt_idx].flags |= P_ALLOCED; |
| 2991 | return opt_idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2994 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2995 | /* |
| 2996 | * Return TRUE when option "opt" was set from a modeline or in secure mode. |
| 2997 | * Return FALSE when it wasn't. |
| 2998 | * Return -1 for an unknown option. |
| 2999 | */ |
| 3000 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3001 | was_set_insecurely(char_u *opt, int opt_flags) |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3002 | { |
| 3003 | int idx = findoption(opt); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3004 | long_u *flagp; |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3005 | |
| 3006 | if (idx >= 0) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3007 | { |
| 3008 | flagp = insecure_flag(idx, opt_flags); |
| 3009 | return (*flagp & P_INSECURE) != 0; |
| 3010 | } |
Bram Moolenaar | 95f0960 | 2016-11-10 20:01:45 +0100 | [diff] [blame] | 3011 | internal_error("was_set_insecurely()"); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3012 | return -1; |
| 3013 | } |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3014 | |
| 3015 | /* |
| 3016 | * Get a pointer to the flags used for the P_INSECURE flag of option |
| 3017 | * "opt_idx". For some local options a local flags field is used. |
Bram Moolenaar | d5e8c92 | 2021-02-02 21:10:01 +0100 | [diff] [blame] | 3018 | * NOTE: Caller must make sure that "curwin" is set to the window from which |
| 3019 | * the option is used. |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3020 | */ |
| 3021 | static long_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3022 | insecure_flag(int opt_idx, int opt_flags) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3023 | { |
| 3024 | if (opt_flags & OPT_LOCAL) |
| 3025 | switch ((int)options[opt_idx].indir) |
| 3026 | { |
| 3027 | #ifdef FEAT_STL_OPT |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3028 | case PV_STL: return &curwin->w_p_stl_flags; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3029 | #endif |
| 3030 | #ifdef FEAT_EVAL |
Bram Moolenaar | 2e97890 | 2006-05-13 12:37:50 +0000 | [diff] [blame] | 3031 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3032 | case PV_FDE: return &curwin->w_p_fde_flags; |
| 3033 | case PV_FDT: return &curwin->w_p_fdt_flags; |
Bram Moolenaar | 2e97890 | 2006-05-13 12:37:50 +0000 | [diff] [blame] | 3034 | # endif |
Bram Moolenaar | 9b2200a | 2006-03-20 21:55:45 +0000 | [diff] [blame] | 3035 | # ifdef FEAT_BEVAL |
| 3036 | case PV_BEXPR: return &curbuf->b_p_bexpr_flags; |
| 3037 | # endif |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3038 | case PV_INDE: return &curbuf->b_p_inde_flags; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3039 | case PV_FEX: return &curbuf->b_p_fex_flags; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3040 | # ifdef FEAT_FIND_ID |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3041 | case PV_INEX: return &curbuf->b_p_inex_flags; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3042 | # endif |
| 3043 | #endif |
| 3044 | } |
| 3045 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 3046 | // Nothing special, return global flags field. |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3047 | return &options[opt_idx].flags; |
| 3048 | } |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3049 | #endif |
| 3050 | |
Bram Moolenaar | 1f26d2f | 2009-02-11 10:35:36 +0000 | [diff] [blame] | 3051 | /* |
| 3052 | * Redraw the window title and/or tab page text later. |
| 3053 | */ |
Yegappan Lakshmanan | eb91e24 | 2023-08-31 18:10:46 +0200 | [diff] [blame] | 3054 | void |
| 3055 | redraw_titles(void) |
Bram Moolenaar | 1f26d2f | 2009-02-11 10:35:36 +0000 | [diff] [blame] | 3056 | { |
| 3057 | need_maketitle = TRUE; |
Bram Moolenaar | 1f26d2f | 2009-02-11 10:35:36 +0000 | [diff] [blame] | 3058 | redraw_tabline = TRUE; |
Bram Moolenaar | 1f26d2f | 2009-02-11 10:35:36 +0000 | [diff] [blame] | 3059 | } |
Bram Moolenaar | 1f26d2f | 2009-02-11 10:35:36 +0000 | [diff] [blame] | 3060 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3061 | /* |
Bram Moolenaar | 8f130ed | 2019-04-10 22:15:19 +0200 | [diff] [blame] | 3062 | * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII |
| 3063 | * characters or characters in "allowed". |
| 3064 | */ |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 3065 | int |
Bram Moolenaar | 8f130ed | 2019-04-10 22:15:19 +0200 | [diff] [blame] | 3066 | valid_name(char_u *val, char *allowed) |
| 3067 | { |
| 3068 | char_u *s; |
| 3069 | |
| 3070 | for (s = val; *s != NUL; ++s) |
| 3071 | if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL) |
| 3072 | return FALSE; |
| 3073 | return TRUE; |
| 3074 | } |
| 3075 | |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3076 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3077 | /* |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 3078 | * Set the script_ctx for an option, taking care of setting the buffer- or |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3079 | * window-local value. |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3080 | */ |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 3081 | void |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 3082 | set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3083 | { |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3084 | int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0; |
| 3085 | int indir = (int)options[opt_idx].indir; |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 3086 | sctx_T new_script_ctx = script_ctx; |
| 3087 | |
Bram Moolenaar | 5125874 | 2020-05-03 17:19:33 +0200 | [diff] [blame] | 3088 | // Modeline already has the line number set. |
| 3089 | if (!(opt_flags & OPT_MODELINE)) |
| 3090 | new_script_ctx.sc_lnum += SOURCING_LNUM; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3091 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 3092 | // Remember where the option was set. For local options need to do that |
| 3093 | // in the buffer or window structure. |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3094 | if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 3095 | options[opt_idx].script_ctx = new_script_ctx; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3096 | if (both || (opt_flags & OPT_LOCAL)) |
| 3097 | { |
| 3098 | if (indir & PV_BUF) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 3099 | curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3100 | else if (indir & PV_WIN) |
Bram Moolenaar | e70dd11 | 2022-01-21 16:31:11 +0000 | [diff] [blame] | 3101 | { |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 3102 | curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx; |
Bram Moolenaar | e70dd11 | 2022-01-21 16:31:11 +0000 | [diff] [blame] | 3103 | if (both) |
| 3104 | // also setting the "all buffers" value |
| 3105 | curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] = |
| 3106 | new_script_ctx; |
| 3107 | } |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3108 | } |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3109 | } |
Bram Moolenaar | 35bc7d6 | 2018-10-02 14:45:10 +0200 | [diff] [blame] | 3110 | |
| 3111 | /* |
Bram Moolenaar | 5600a70 | 2022-01-22 15:09:36 +0000 | [diff] [blame] | 3112 | * Get the script context of global option "name". |
| 3113 | * |
| 3114 | */ |
| 3115 | sctx_T * |
| 3116 | get_option_sctx(char *name) |
| 3117 | { |
| 3118 | int idx = findoption((char_u *)name); |
| 3119 | |
| 3120 | if (idx >= 0) |
| 3121 | return &options[idx].script_ctx; |
| 3122 | siemsg("no such option: %s", name); |
| 3123 | return NULL; |
| 3124 | } |
| 3125 | |
| 3126 | /* |
Bram Moolenaar | 35bc7d6 | 2018-10-02 14:45:10 +0200 | [diff] [blame] | 3127 | * Set the script_ctx for a termcap option. |
| 3128 | * "name" must be the two character code, e.g. "RV". |
| 3129 | * When "name" is NULL use "opt_idx". |
| 3130 | */ |
| 3131 | void |
| 3132 | set_term_option_sctx_idx(char *name, int opt_idx) |
| 3133 | { |
| 3134 | char_u buf[5]; |
| 3135 | int idx; |
| 3136 | |
| 3137 | if (name == NULL) |
| 3138 | idx = opt_idx; |
| 3139 | else |
| 3140 | { |
| 3141 | buf[0] = 't'; |
| 3142 | buf[1] = '_'; |
| 3143 | buf[2] = name[0]; |
| 3144 | buf[3] = name[1]; |
| 3145 | buf[4] = 0; |
| 3146 | idx = findoption(buf); |
| 3147 | } |
| 3148 | if (idx >= 0) |
| 3149 | set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx); |
| 3150 | } |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3151 | #endif |
| 3152 | |
Bram Moolenaar | f414048 | 2020-02-15 23:06:45 +0100 | [diff] [blame] | 3153 | #if defined(FEAT_EVAL) |
| 3154 | /* |
| 3155 | * Apply the OptionSet autocommand. |
| 3156 | */ |
| 3157 | static void |
| 3158 | apply_optionset_autocmd( |
| 3159 | int opt_idx, |
| 3160 | long opt_flags, |
| 3161 | long oldval, |
| 3162 | long oldval_g, |
| 3163 | long newval, |
| 3164 | char *errmsg) |
| 3165 | { |
| 3166 | char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12]; |
| 3167 | |
| 3168 | // Don't do this while starting up, failure or recursively. |
| 3169 | if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL) |
| 3170 | return; |
| 3171 | |
| 3172 | vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval); |
| 3173 | vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld", |
| 3174 | oldval_g); |
| 3175 | vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval); |
| 3176 | vim_snprintf((char *)buf_type, sizeof(buf_type), "%s", |
| 3177 | (opt_flags & OPT_LOCAL) ? "local" : "global"); |
| 3178 | set_vim_var_string(VV_OPTION_NEW, buf_new, -1); |
| 3179 | set_vim_var_string(VV_OPTION_OLD, buf_old, -1); |
| 3180 | set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); |
| 3181 | if (opt_flags & OPT_LOCAL) |
| 3182 | { |
| 3183 | set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1); |
| 3184 | set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1); |
| 3185 | } |
| 3186 | if (opt_flags & OPT_GLOBAL) |
| 3187 | { |
| 3188 | set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1); |
| 3189 | set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1); |
| 3190 | } |
| 3191 | if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) |
| 3192 | { |
| 3193 | set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1); |
| 3194 | set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1); |
| 3195 | set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1); |
| 3196 | } |
| 3197 | if (opt_flags & OPT_MODELINE) |
| 3198 | { |
| 3199 | set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1); |
| 3200 | set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1); |
| 3201 | } |
| 3202 | apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname, |
| 3203 | NULL, FALSE, NULL); |
| 3204 | reset_v_option_vars(); |
| 3205 | } |
| 3206 | #endif |
| 3207 | |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3208 | #if defined(FEAT_ARABIC) || defined(PROTO) |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3209 | /* |
| 3210 | * Process the updated 'arabic' option value. |
| 3211 | */ |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3212 | char * |
| 3213 | did_set_arabic(optset_T *args UNUSED) |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3214 | { |
| 3215 | char *errmsg = NULL; |
| 3216 | |
| 3217 | if (curwin->w_p_arab) |
| 3218 | { |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 3219 | // 'arabic' is set, handle various sub-settings. |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3220 | if (!p_tbidi) |
| 3221 | { |
| 3222 | // set rightleft mode |
| 3223 | if (!curwin->w_p_rl) |
| 3224 | { |
| 3225 | curwin->w_p_rl = TRUE; |
| 3226 | changed_window_setting(); |
| 3227 | } |
| 3228 | |
| 3229 | // Enable Arabic shaping (major part of what Arabic requires) |
| 3230 | if (!p_arshape) |
| 3231 | { |
| 3232 | p_arshape = TRUE; |
| 3233 | redraw_later_clear(); |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | // Arabic requires a utf-8 encoding, inform the user if it's not |
| 3238 | // set. |
| 3239 | if (STRCMP(p_enc, "utf-8") != 0) |
| 3240 | { |
| 3241 | static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'"); |
| 3242 | |
| 3243 | msg_source(HL_ATTR(HLF_W)); |
| 3244 | msg_attr(_(w_arabic), HL_ATTR(HLF_W)); |
| 3245 | #ifdef FEAT_EVAL |
| 3246 | set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1); |
| 3247 | #endif |
| 3248 | } |
| 3249 | |
| 3250 | // set 'delcombine' |
| 3251 | p_deco = TRUE; |
| 3252 | |
| 3253 | # ifdef FEAT_KEYMAP |
| 3254 | // Force-set the necessary keymap for arabic |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3255 | errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic", |
| 3256 | OPT_LOCAL); |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3257 | # endif |
| 3258 | } |
| 3259 | else |
| 3260 | { |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 3261 | // 'arabic' is reset, handle various sub-settings. |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3262 | if (!p_tbidi) |
| 3263 | { |
| 3264 | // reset rightleft mode |
| 3265 | if (curwin->w_p_rl) |
| 3266 | { |
| 3267 | curwin->w_p_rl = FALSE; |
| 3268 | changed_window_setting(); |
| 3269 | } |
| 3270 | |
| 3271 | // 'arabicshape' isn't reset, it is a global option and |
| 3272 | // another window may still need it "on". |
| 3273 | } |
| 3274 | |
| 3275 | // 'delcombine' isn't reset, it is a global option and another |
| 3276 | // window may still want it "on". |
| 3277 | |
| 3278 | # ifdef FEAT_KEYMAP |
| 3279 | // Revert to the default keymap |
| 3280 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 3281 | curbuf->b_p_imsearch = B_IMODE_USE_INSERT; |
| 3282 | # endif |
| 3283 | } |
| 3284 | |
| 3285 | return errmsg; |
| 3286 | } |
| 3287 | #endif |
| 3288 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3289 | #if defined(FEAT_AUTOCHDIR) || defined(PROTO) |
| 3290 | /* |
| 3291 | * Process the updated 'autochdir' option value. |
| 3292 | */ |
| 3293 | char * |
| 3294 | did_set_autochdir(optset_T *args UNUSED) |
| 3295 | { |
| 3296 | // Change directories when the 'acd' option is set now. |
| 3297 | DO_AUTOCHDIR; |
| 3298 | return NULL; |
| 3299 | } |
| 3300 | #endif |
| 3301 | |
| 3302 | #if defined(FEAT_BEVAL_GUI) || defined(PROTO) |
| 3303 | /* |
| 3304 | * Process the updated 'ballooneval' option value. |
| 3305 | */ |
| 3306 | char * |
| 3307 | did_set_ballooneval(optset_T *args) |
| 3308 | { |
| 3309 | if (balloonEvalForTerm) |
| 3310 | return NULL; |
| 3311 | |
| 3312 | if (p_beval && !args->os_oldval.boolean) |
| 3313 | gui_mch_enable_beval_area(balloonEval); |
| 3314 | else if (!p_beval && args->os_oldval.boolean) |
| 3315 | gui_mch_disable_beval_area(balloonEval); |
| 3316 | |
| 3317 | return NULL; |
| 3318 | } |
| 3319 | #endif |
| 3320 | |
| 3321 | #if defined(FEAT_BEVAL_TERM) || defined(PROTO) |
| 3322 | /* |
| 3323 | * Process the updated 'balloonevalterm' option value. |
| 3324 | */ |
| 3325 | char * |
| 3326 | did_set_balloonevalterm(optset_T *args UNUSED) |
| 3327 | { |
| 3328 | mch_bevalterm_changed(); |
| 3329 | return NULL; |
| 3330 | } |
| 3331 | #endif |
| 3332 | |
| 3333 | /* |
| 3334 | * Process the updated 'binary' option value. |
| 3335 | */ |
| 3336 | char * |
| 3337 | did_set_binary(optset_T *args) |
| 3338 | { |
| 3339 | // when 'bin' is set also set some other options |
| 3340 | set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags); |
| 3341 | redraw_titles(); |
| 3342 | |
| 3343 | return NULL; |
| 3344 | } |
| 3345 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3346 | /* |
| 3347 | * Process the updated 'buflisted' option value. |
| 3348 | */ |
| 3349 | char * |
| 3350 | did_set_buflisted(optset_T *args) |
| 3351 | { |
| 3352 | // when 'buflisted' changes, trigger autocommands |
| 3353 | if (args->os_oldval.boolean != curbuf->b_p_bl) |
| 3354 | apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE, |
| 3355 | NULL, NULL, TRUE, curbuf); |
| 3356 | return NULL; |
| 3357 | } |
| 3358 | |
| 3359 | /* |
| 3360 | * Process the new 'cmdheight' option value. |
| 3361 | */ |
| 3362 | char * |
| 3363 | did_set_cmdheight(optset_T *args) |
| 3364 | { |
| 3365 | long old_value = args->os_oldval.number; |
| 3366 | char *errmsg = NULL; |
| 3367 | |
| 3368 | // if p_ch changed value, change the command line height |
| 3369 | if (p_ch < 1) |
| 3370 | { |
| 3371 | errmsg = e_argument_must_be_positive; |
| 3372 | p_ch = 1; |
| 3373 | } |
| 3374 | if (p_ch > Rows - min_rows() + 1) |
| 3375 | p_ch = Rows - min_rows() + 1; |
| 3376 | |
| 3377 | // Only compute the new window layout when startup has been |
| 3378 | // completed. Otherwise the frame sizes may be wrong. |
| 3379 | if ((p_ch != old_value |
| 3380 | || tabline_height() + topframe->fr_height != Rows - p_ch) |
| 3381 | && full_screen |
| 3382 | #ifdef FEAT_GUI |
| 3383 | && !gui.starting |
| 3384 | #endif |
| 3385 | ) |
| 3386 | command_height(); |
| 3387 | |
| 3388 | return errmsg; |
| 3389 | } |
| 3390 | |
| 3391 | /* |
| 3392 | * Process the updated 'compatible' option value. |
| 3393 | */ |
| 3394 | char * |
| 3395 | did_set_compatible(optset_T *args UNUSED) |
| 3396 | { |
| 3397 | compatible_set(); |
| 3398 | return NULL; |
| 3399 | } |
| 3400 | |
| 3401 | #if defined(FEAT_CONCEAL) || defined(PROTO) |
| 3402 | /* |
| 3403 | * Process the new 'conceallevel' option value. |
| 3404 | */ |
| 3405 | char * |
| 3406 | did_set_conceallevel(optset_T *args UNUSED) |
| 3407 | { |
| 3408 | char *errmsg = NULL; |
| 3409 | |
| 3410 | if (curwin->w_p_cole < 0) |
| 3411 | { |
| 3412 | errmsg = e_argument_must_be_positive; |
| 3413 | curwin->w_p_cole = 0; |
| 3414 | } |
| 3415 | else if (curwin->w_p_cole > 3) |
| 3416 | { |
| 3417 | errmsg = e_invalid_argument; |
| 3418 | curwin->w_p_cole = 3; |
| 3419 | } |
| 3420 | |
| 3421 | return errmsg; |
| 3422 | } |
| 3423 | #endif |
| 3424 | |
| 3425 | #if defined(FEAT_DIFF) || defined(PROTO) |
| 3426 | /* |
| 3427 | * Process the updated 'diff' option value. |
| 3428 | */ |
| 3429 | char * |
| 3430 | did_set_diff(optset_T *args UNUSED) |
| 3431 | { |
| 3432 | // May add or remove the buffer from the list of diff buffers. |
| 3433 | diff_buf_adjust(curwin); |
| 3434 | # ifdef FEAT_FOLDING |
| 3435 | if (foldmethodIsDiff(curwin)) |
| 3436 | foldUpdateAll(curwin); |
| 3437 | # endif |
| 3438 | return NULL; |
| 3439 | } |
| 3440 | #endif |
| 3441 | |
| 3442 | /* |
| 3443 | * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb' |
| 3444 | * option value. |
| 3445 | */ |
| 3446 | char * |
| 3447 | did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED) |
| 3448 | { |
| 3449 | // redraw the window title and tab page text |
| 3450 | redraw_titles(); |
| 3451 | return NULL; |
| 3452 | } |
| 3453 | |
| 3454 | /* |
| 3455 | * Process the updated 'equalalways' option value. |
| 3456 | */ |
| 3457 | char * |
| 3458 | did_set_equalalways(optset_T *args) |
| 3459 | { |
| 3460 | if (p_ea && !args->os_oldval.boolean) |
| 3461 | win_equal(curwin, FALSE, 0); |
| 3462 | |
| 3463 | return NULL; |
| 3464 | } |
| 3465 | |
| 3466 | #if defined(FEAT_FOLDING) || defined(PROTO) |
| 3467 | /* |
| 3468 | * Process the new 'foldcolumn' option value. |
| 3469 | */ |
| 3470 | char * |
| 3471 | did_set_foldcolumn(optset_T *args UNUSED) |
| 3472 | { |
| 3473 | char *errmsg = NULL; |
| 3474 | |
| 3475 | if (curwin->w_p_fdc < 0) |
| 3476 | { |
| 3477 | errmsg = e_argument_must_be_positive; |
| 3478 | curwin->w_p_fdc = 0; |
| 3479 | } |
| 3480 | else if (curwin->w_p_fdc > 12) |
| 3481 | { |
| 3482 | errmsg = e_invalid_argument; |
| 3483 | curwin->w_p_fdc = 12; |
| 3484 | } |
| 3485 | |
| 3486 | return errmsg; |
| 3487 | } |
| 3488 | |
| 3489 | /* |
| 3490 | * Process the new 'foldlevel' option value. |
| 3491 | */ |
| 3492 | char * |
| 3493 | did_set_foldlevel(optset_T *args UNUSED) |
| 3494 | { |
| 3495 | if (curwin->w_p_fdl < 0) |
| 3496 | curwin->w_p_fdl = 0; |
| 3497 | newFoldLevel(); |
| 3498 | return NULL; |
| 3499 | } |
| 3500 | |
| 3501 | /* |
| 3502 | * Process the new 'foldminlines' option value. |
| 3503 | */ |
| 3504 | char * |
| 3505 | did_set_foldminlines(optset_T *args UNUSED) |
| 3506 | { |
| 3507 | foldUpdateAll(curwin); |
| 3508 | return NULL; |
| 3509 | } |
| 3510 | |
| 3511 | /* |
| 3512 | * Process the new 'foldnestmax' option value. |
| 3513 | */ |
| 3514 | char * |
| 3515 | did_set_foldnestmax(optset_T *args UNUSED) |
| 3516 | { |
| 3517 | if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin)) |
| 3518 | foldUpdateAll(curwin); |
| 3519 | return NULL; |
| 3520 | } |
| 3521 | #endif |
| 3522 | |
| 3523 | #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO) |
| 3524 | /* |
| 3525 | * Process the updated 'hlsearch' option value. |
| 3526 | */ |
| 3527 | char * |
| 3528 | did_set_hlsearch(optset_T *args UNUSED) |
| 3529 | { |
| 3530 | // when 'hlsearch' is set or reset: reset no_hlsearch |
| 3531 | set_no_hlsearch(FALSE); |
| 3532 | return NULL; |
| 3533 | } |
| 3534 | #endif |
| 3535 | |
| 3536 | /* |
| 3537 | * Process the updated 'ignorecase' option value. |
| 3538 | */ |
| 3539 | char * |
| 3540 | did_set_ignorecase(optset_T *args UNUSED) |
| 3541 | { |
| 3542 | // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw |
| 3543 | if (p_hls) |
| 3544 | redraw_all_later(UPD_SOME_VALID); |
| 3545 | return NULL; |
| 3546 | } |
| 3547 | |
| 3548 | #if defined(HAVE_INPUT_METHOD) || defined(PROTO) |
| 3549 | /* |
| 3550 | * Process the updated 'imdisable' option value. |
| 3551 | */ |
| 3552 | char * |
| 3553 | did_set_imdisable(optset_T *args UNUSED) |
| 3554 | { |
| 3555 | // Only de-activate it here, it will be enabled when changing mode. |
| 3556 | if (p_imdisable) |
| 3557 | im_set_active(FALSE); |
| 3558 | else if (State & MODE_INSERT) |
| 3559 | // When the option is set from an autocommand, it may need to take |
| 3560 | // effect right away. |
| 3561 | im_set_active(curbuf->b_p_iminsert == B_IMODE_IM); |
| 3562 | return NULL; |
| 3563 | } |
| 3564 | #endif |
| 3565 | |
| 3566 | /* |
| 3567 | * Process the new 'iminsert' option value. |
| 3568 | */ |
| 3569 | char * |
| 3570 | did_set_iminsert(optset_T *args UNUSED) |
| 3571 | { |
| 3572 | char *errmsg = NULL; |
| 3573 | |
| 3574 | if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST) |
| 3575 | { |
| 3576 | errmsg = e_invalid_argument; |
| 3577 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 3578 | } |
| 3579 | p_iminsert = curbuf->b_p_iminsert; |
| 3580 | if (termcap_active) // don't do this in the alternate screen |
| 3581 | showmode(); |
| 3582 | #if defined(FEAT_KEYMAP) |
| 3583 | // Show/unshow value of 'keymap' in status lines. |
| 3584 | status_redraw_curbuf(); |
| 3585 | #endif |
| 3586 | |
| 3587 | return errmsg; |
| 3588 | } |
| 3589 | |
| 3590 | /* |
| 3591 | * Process the new 'imsearch' option value. |
| 3592 | */ |
| 3593 | char * |
| 3594 | did_set_imsearch(optset_T *args UNUSED) |
| 3595 | { |
| 3596 | char *errmsg = NULL; |
| 3597 | |
| 3598 | if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST) |
| 3599 | { |
| 3600 | errmsg = e_invalid_argument; |
| 3601 | curbuf->b_p_imsearch = B_IMODE_NONE; |
| 3602 | } |
| 3603 | p_imsearch = curbuf->b_p_imsearch; |
| 3604 | |
| 3605 | return errmsg; |
| 3606 | } |
| 3607 | |
| 3608 | #if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO) |
| 3609 | /* |
| 3610 | * Process the new 'imstyle' option value. |
| 3611 | */ |
| 3612 | char * |
| 3613 | did_set_imstyle(optset_T *args UNUSED) |
| 3614 | { |
| 3615 | char *errmsg = NULL; |
| 3616 | |
| 3617 | if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT) |
| 3618 | errmsg = e_invalid_argument; |
| 3619 | |
| 3620 | return errmsg; |
| 3621 | } |
| 3622 | #endif |
| 3623 | |
| 3624 | /* |
| 3625 | * Process the updated 'insertmode' option value. |
| 3626 | */ |
| 3627 | char * |
| 3628 | did_set_insertmode(optset_T *args) |
| 3629 | { |
| 3630 | // when 'insertmode' is set from an autocommand need to do work here |
| 3631 | if (p_im) |
| 3632 | { |
| 3633 | if ((State & MODE_INSERT) == 0) |
| 3634 | need_start_insertmode = TRUE; |
| 3635 | stop_insert_mode = FALSE; |
| 3636 | } |
| 3637 | // only reset if it was set previously |
| 3638 | else if (args->os_oldval.boolean) |
| 3639 | { |
| 3640 | need_start_insertmode = FALSE; |
| 3641 | stop_insert_mode = TRUE; |
| 3642 | if (restart_edit != 0 && mode_displayed) |
| 3643 | clear_cmdline = TRUE; // remove "(insert)" |
| 3644 | restart_edit = 0; |
| 3645 | } |
| 3646 | |
| 3647 | return NULL; |
| 3648 | } |
| 3649 | |
| 3650 | #if defined(FEAT_LANGMAP) || defined(PROTO) |
| 3651 | /* |
| 3652 | * Process the updated 'langnoremap' option value. |
| 3653 | */ |
| 3654 | char * |
| 3655 | did_set_langnoremap(optset_T *args UNUSED) |
| 3656 | { |
| 3657 | // 'langnoremap' -> !'langremap' |
| 3658 | p_lrm = !p_lnr; |
| 3659 | return NULL; |
| 3660 | } |
| 3661 | |
| 3662 | /* |
| 3663 | * Process the updated 'langremap' option value. |
| 3664 | */ |
| 3665 | char * |
| 3666 | did_set_langremap(optset_T *args UNUSED) |
| 3667 | { |
| 3668 | // 'langremap' -> !'langnoremap' |
| 3669 | p_lnr = !p_lrm; |
| 3670 | return NULL; |
| 3671 | } |
| 3672 | #endif |
| 3673 | |
| 3674 | /* |
| 3675 | * Process the new 'laststatus' option value. |
| 3676 | */ |
| 3677 | char * |
| 3678 | did_set_laststatus(optset_T *args UNUSED) |
| 3679 | { |
| 3680 | last_status(FALSE); // (re)set last window status line |
| 3681 | return NULL; |
| 3682 | } |
| 3683 | |
| 3684 | #if defined(FEAT_GUI) || defined(PROTO) |
| 3685 | /* |
| 3686 | * Process the new 'linespace' option value. |
| 3687 | */ |
| 3688 | char * |
| 3689 | did_set_linespace(optset_T *args UNUSED) |
| 3690 | { |
| 3691 | // Recompute gui.char_height and resize the Vim window to keep the |
| 3692 | // same number of lines. |
| 3693 | if (gui.in_use && gui_mch_adjust_charheight() == OK) |
| 3694 | gui_set_shellsize(FALSE, FALSE, RESIZE_VERT); |
| 3695 | return NULL; |
| 3696 | } |
| 3697 | #endif |
| 3698 | |
| 3699 | /* |
| 3700 | * Process the updated 'lisp' option value. |
| 3701 | */ |
| 3702 | char * |
| 3703 | did_set_lisp(optset_T *args UNUSED) |
| 3704 | { |
| 3705 | // When 'lisp' option changes include/exclude '-' in keyword characters. |
| 3706 | (void)buf_init_chartab(curbuf, FALSE); // ignore errors |
| 3707 | return NULL; |
| 3708 | } |
| 3709 | |
| 3710 | /* |
| 3711 | * Process the new 'maxcombine' option value. |
| 3712 | */ |
| 3713 | char * |
| 3714 | did_set_maxcombine(optset_T *args UNUSED) |
| 3715 | { |
| 3716 | if (p_mco > MAX_MCO) |
| 3717 | p_mco = MAX_MCO; |
| 3718 | else if (p_mco < 0) |
| 3719 | p_mco = 0; |
| 3720 | screenclear(); // will re-allocate the screen |
| 3721 | return NULL; |
| 3722 | } |
| 3723 | |
| 3724 | /* |
| 3725 | * Process the updated 'modifiable' option value. |
| 3726 | */ |
| 3727 | char * |
| 3728 | did_set_modifiable(optset_T *args UNUSED) |
| 3729 | { |
| 3730 | // when 'modifiable' is changed, redraw the window title |
| 3731 | |
| 3732 | # ifdef FEAT_TERMINAL |
| 3733 | // Cannot set 'modifiable' when in Terminal mode. |
| 3734 | if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf) |
| 3735 | && curbuf->b_term != NULL && !term_is_finished(curbuf)))) |
| 3736 | { |
| 3737 | curbuf->b_p_ma = FALSE; |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3738 | return e_cannot_make_terminal_with_running_job_modifiable; |
| 3739 | } |
| 3740 | # endif |
| 3741 | redraw_titles(); |
| 3742 | |
| 3743 | return NULL; |
| 3744 | } |
| 3745 | |
| 3746 | /* |
| 3747 | * Process the updated 'modified' option value. |
| 3748 | */ |
| 3749 | char * |
| 3750 | did_set_modified(optset_T *args) |
| 3751 | { |
| 3752 | if (!args->os_newval.boolean) |
| 3753 | save_file_ff(curbuf); // Buffer is unchanged |
| 3754 | redraw_titles(); |
zeertzjq | 5bf6c21 | 2024-03-31 18:41:27 +0200 | [diff] [blame] | 3755 | curbuf->b_modified_was_set = args->os_newval.boolean; |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3756 | return NULL; |
| 3757 | } |
| 3758 | |
| 3759 | #if defined(FEAT_GUI) || defined(PROTO) |
| 3760 | /* |
| 3761 | * Process the updated 'mousehide' option value. |
| 3762 | */ |
| 3763 | char * |
| 3764 | did_set_mousehide(optset_T *args UNUSED) |
| 3765 | { |
| 3766 | if (!p_mh) |
| 3767 | gui_mch_mousehide(FALSE); |
| 3768 | return NULL; |
| 3769 | } |
| 3770 | #endif |
| 3771 | |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3772 | /* |
| 3773 | * Process the updated 'number' or 'relativenumber' option value. |
| 3774 | */ |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3775 | char * |
| 3776 | did_set_number_relativenumber(optset_T *args UNUSED) |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3777 | { |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3778 | #if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO) |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3779 | if (gui.in_use |
| 3780 | && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u') |
| 3781 | && curbuf->b_signlist != NULL) |
| 3782 | { |
| 3783 | // If the 'number' or 'relativenumber' options are modified and |
| 3784 | // 'signcolumn' is set to 'number', then clear the screen for a full |
| 3785 | // refresh. Otherwise the sign icons are not displayed properly in the |
| 3786 | // number column. If the 'number' option is set and only the |
| 3787 | // 'relativenumber' option is toggled, then don't refresh the screen |
| 3788 | // (optimization). |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3789 | if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu))) |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3790 | redraw_all_later(UPD_CLEAR); |
| 3791 | } |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3792 | #endif |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 3793 | return NULL; |
| 3794 | } |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 3795 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3796 | #if defined(FEAT_LINEBREAK) || defined(PROTO) |
| 3797 | /* |
| 3798 | * Process the new 'numberwidth' option value. |
| 3799 | */ |
| 3800 | char * |
| 3801 | did_set_numberwidth(optset_T *args UNUSED) |
| 3802 | { |
| 3803 | char *errmsg = NULL; |
| 3804 | |
| 3805 | // 'numberwidth' must be positive |
| 3806 | if (curwin->w_p_nuw < 1) |
| 3807 | { |
| 3808 | errmsg = e_argument_must_be_positive; |
| 3809 | curwin->w_p_nuw = 1; |
| 3810 | } |
| 3811 | if (curwin->w_p_nuw > 20) |
| 3812 | { |
| 3813 | errmsg = e_invalid_argument; |
| 3814 | curwin->w_p_nuw = 20; |
| 3815 | } |
| 3816 | curwin->w_nrwidth_line_count = 0; // trigger a redraw |
| 3817 | |
| 3818 | return errmsg; |
| 3819 | } |
| 3820 | #endif |
| 3821 | |
| 3822 | /* |
| 3823 | * Process the updated 'paste' option value. Called after p_paste was set or |
| 3824 | * reset. When 'paste' is set or reset also change other options. |
| 3825 | */ |
| 3826 | char * |
| 3827 | did_set_paste(optset_T *args UNUSED) |
| 3828 | { |
| 3829 | static int old_p_paste = FALSE; |
| 3830 | static int save_sm = 0; |
| 3831 | static int save_sta = 0; |
| 3832 | static int save_ru = 0; |
| 3833 | #ifdef FEAT_RIGHTLEFT |
| 3834 | static int save_ri = 0; |
| 3835 | static int save_hkmap = 0; |
| 3836 | #endif |
| 3837 | buf_T *buf; |
| 3838 | |
| 3839 | if (p_paste) |
| 3840 | { |
| 3841 | // Paste switched from off to on. |
| 3842 | // Save the current values, so they can be restored later. |
| 3843 | if (!old_p_paste) |
| 3844 | { |
| 3845 | // save options for each buffer |
| 3846 | FOR_ALL_BUFFERS(buf) |
| 3847 | { |
| 3848 | buf->b_p_tw_nopaste = buf->b_p_tw; |
| 3849 | buf->b_p_wm_nopaste = buf->b_p_wm; |
| 3850 | buf->b_p_sts_nopaste = buf->b_p_sts; |
| 3851 | buf->b_p_ai_nopaste = buf->b_p_ai; |
| 3852 | buf->b_p_et_nopaste = buf->b_p_et; |
| 3853 | #ifdef FEAT_VARTABS |
| 3854 | if (buf->b_p_vsts_nopaste) |
| 3855 | vim_free(buf->b_p_vsts_nopaste); |
| 3856 | buf->b_p_vsts_nopaste = |
| 3857 | buf->b_p_vsts && buf->b_p_vsts != empty_option |
| 3858 | ? vim_strsave(buf->b_p_vsts) : NULL; |
| 3859 | #endif |
| 3860 | } |
| 3861 | |
| 3862 | // save global options |
| 3863 | save_sm = p_sm; |
| 3864 | save_sta = p_sta; |
| 3865 | save_ru = p_ru; |
| 3866 | #ifdef FEAT_RIGHTLEFT |
| 3867 | save_ri = p_ri; |
| 3868 | save_hkmap = p_hkmap; |
| 3869 | #endif |
| 3870 | // save global values for local buffer options |
| 3871 | p_ai_nopaste = p_ai; |
| 3872 | p_et_nopaste = p_et; |
| 3873 | p_sts_nopaste = p_sts; |
| 3874 | p_tw_nopaste = p_tw; |
| 3875 | p_wm_nopaste = p_wm; |
| 3876 | #ifdef FEAT_VARTABS |
| 3877 | if (p_vsts_nopaste) |
| 3878 | vim_free(p_vsts_nopaste); |
| 3879 | p_vsts_nopaste = p_vsts && p_vsts != empty_option |
| 3880 | ? vim_strsave(p_vsts) : NULL; |
| 3881 | #endif |
| 3882 | } |
| 3883 | |
| 3884 | // Always set the option values, also when 'paste' is set when it is |
| 3885 | // already on. Set options for each buffer. |
| 3886 | FOR_ALL_BUFFERS(buf) |
| 3887 | { |
| 3888 | buf->b_p_tw = 0; // textwidth is 0 |
| 3889 | buf->b_p_wm = 0; // wrapmargin is 0 |
| 3890 | buf->b_p_sts = 0; // softtabstop is 0 |
| 3891 | buf->b_p_ai = 0; // no auto-indent |
| 3892 | buf->b_p_et = 0; // no expandtab |
| 3893 | #ifdef FEAT_VARTABS |
| 3894 | if (buf->b_p_vsts) |
| 3895 | free_string_option(buf->b_p_vsts); |
| 3896 | buf->b_p_vsts = empty_option; |
| 3897 | VIM_CLEAR(buf->b_p_vsts_array); |
| 3898 | #endif |
| 3899 | } |
| 3900 | |
| 3901 | // set global options |
| 3902 | p_sm = 0; // no showmatch |
| 3903 | p_sta = 0; // no smarttab |
| 3904 | if (p_ru) |
| 3905 | status_redraw_all(); // redraw to remove the ruler |
| 3906 | p_ru = 0; // no ruler |
| 3907 | #ifdef FEAT_RIGHTLEFT |
| 3908 | p_ri = 0; // no reverse insert |
| 3909 | p_hkmap = 0; // no Hebrew keyboard |
| 3910 | #endif |
| 3911 | // set global values for local buffer options |
| 3912 | p_tw = 0; |
| 3913 | p_wm = 0; |
| 3914 | p_sts = 0; |
| 3915 | p_ai = 0; |
Christian Brabandt | 757593c | 2023-08-22 21:44:10 +0200 | [diff] [blame] | 3916 | p_et = 0; |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3917 | #ifdef FEAT_VARTABS |
| 3918 | if (p_vsts) |
| 3919 | free_string_option(p_vsts); |
| 3920 | p_vsts = empty_option; |
| 3921 | #endif |
| 3922 | } |
| 3923 | |
| 3924 | // Paste switched from on to off: Restore saved values. |
| 3925 | else if (old_p_paste) |
| 3926 | { |
| 3927 | // restore options for each buffer |
| 3928 | FOR_ALL_BUFFERS(buf) |
| 3929 | { |
| 3930 | buf->b_p_tw = buf->b_p_tw_nopaste; |
| 3931 | buf->b_p_wm = buf->b_p_wm_nopaste; |
| 3932 | buf->b_p_sts = buf->b_p_sts_nopaste; |
| 3933 | buf->b_p_ai = buf->b_p_ai_nopaste; |
| 3934 | buf->b_p_et = buf->b_p_et_nopaste; |
| 3935 | #ifdef FEAT_VARTABS |
| 3936 | if (buf->b_p_vsts) |
| 3937 | free_string_option(buf->b_p_vsts); |
| 3938 | buf->b_p_vsts = buf->b_p_vsts_nopaste |
| 3939 | ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option; |
| 3940 | vim_free(buf->b_p_vsts_array); |
| 3941 | if (buf->b_p_vsts && buf->b_p_vsts != empty_option) |
| 3942 | (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array); |
| 3943 | else |
| 3944 | buf->b_p_vsts_array = NULL; |
| 3945 | #endif |
| 3946 | } |
| 3947 | |
| 3948 | // restore global options |
| 3949 | p_sm = save_sm; |
| 3950 | p_sta = save_sta; |
| 3951 | if (p_ru != save_ru) |
| 3952 | status_redraw_all(); // redraw to draw the ruler |
| 3953 | p_ru = save_ru; |
| 3954 | #ifdef FEAT_RIGHTLEFT |
| 3955 | p_ri = save_ri; |
| 3956 | p_hkmap = save_hkmap; |
| 3957 | #endif |
| 3958 | // set global values for local buffer options |
| 3959 | p_ai = p_ai_nopaste; |
| 3960 | p_et = p_et_nopaste; |
| 3961 | p_sts = p_sts_nopaste; |
| 3962 | p_tw = p_tw_nopaste; |
| 3963 | p_wm = p_wm_nopaste; |
| 3964 | #ifdef FEAT_VARTABS |
| 3965 | if (p_vsts) |
| 3966 | free_string_option(p_vsts); |
| 3967 | p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option; |
| 3968 | #endif |
| 3969 | } |
| 3970 | |
| 3971 | old_p_paste = p_paste; |
| 3972 | |
Christian Brabandt | 757593c | 2023-08-22 21:44:10 +0200 | [diff] [blame] | 3973 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3974 | // Remember where the dependent options were reset |
| 3975 | didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts); |
| 3976 | #endif |
| 3977 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3978 | return NULL; |
| 3979 | } |
| 3980 | |
| 3981 | |
| 3982 | #ifdef FEAT_QUICKFIX |
| 3983 | /* |
| 3984 | * Process the updated 'previewwindow' option value. |
| 3985 | */ |
| 3986 | char * |
Yegappan Lakshmanan | 3ee2596 | 2023-12-04 20:31:14 +0100 | [diff] [blame] | 3987 | did_set_previewwindow(optset_T *args UNUSED) |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3988 | { |
| 3989 | if (!curwin->w_p_pvw) |
| 3990 | return NULL; |
| 3991 | |
| 3992 | // There can be only one window with 'previewwindow' set. |
| 3993 | win_T *win; |
| 3994 | |
| 3995 | FOR_ALL_WINDOWS(win) |
| 3996 | if (win->w_p_pvw && win != curwin) |
| 3997 | { |
| 3998 | curwin->w_p_pvw = FALSE; |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 3999 | return e_preview_window_already_exists; |
| 4000 | } |
| 4001 | |
| 4002 | return NULL; |
| 4003 | } |
| 4004 | #endif |
| 4005 | |
| 4006 | #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO) |
| 4007 | /* |
| 4008 | * Process the new 'pyxversion' option value. |
| 4009 | */ |
| 4010 | char * |
| 4011 | did_set_pyxversion(optset_T *args UNUSED) |
| 4012 | { |
| 4013 | char *errmsg = NULL; |
| 4014 | |
| 4015 | if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3) |
| 4016 | errmsg = e_invalid_argument; |
| 4017 | |
| 4018 | return errmsg; |
| 4019 | } |
| 4020 | #endif |
| 4021 | |
| 4022 | /* |
| 4023 | * Process the updated 'readonly' option value. |
| 4024 | */ |
| 4025 | char * |
| 4026 | did_set_readonly(optset_T *args) |
| 4027 | { |
| 4028 | // when 'readonly' is reset globally, also reset readonlymode |
| 4029 | if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0) |
| 4030 | readonlymode = FALSE; |
| 4031 | |
| 4032 | // when 'readonly' is set may give W10 again |
| 4033 | if (curbuf->b_p_ro) |
| 4034 | curbuf->b_did_warn = FALSE; |
| 4035 | |
| 4036 | redraw_titles(); |
| 4037 | |
| 4038 | return NULL; |
| 4039 | } |
| 4040 | |
| 4041 | /* |
| 4042 | * Process the updated 'scrollbind' option value. |
| 4043 | */ |
| 4044 | char * |
| 4045 | did_set_scrollbind(optset_T *args UNUSED) |
| 4046 | { |
| 4047 | // when 'scrollbind' is set: snapshot the current position to avoid a jump |
| 4048 | // at the end of normal_cmd() |
| 4049 | if (!curwin->w_p_scb) |
| 4050 | return NULL; |
| 4051 | |
| 4052 | do_check_scrollbind(FALSE); |
| 4053 | curwin->w_scbind_pos = curwin->w_topline; |
| 4054 | return NULL; |
| 4055 | } |
| 4056 | |
| 4057 | #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) |
| 4058 | /* |
| 4059 | * Process the updated 'shellslash' option value. |
| 4060 | */ |
| 4061 | char * |
| 4062 | did_set_shellslash(optset_T *args UNUSED) |
| 4063 | { |
| 4064 | if (p_ssl) |
| 4065 | { |
| 4066 | psepc = '/'; |
| 4067 | psepcN = '\\'; |
| 4068 | pseps[0] = '/'; |
| 4069 | } |
| 4070 | else |
| 4071 | { |
| 4072 | psepc = '\\'; |
| 4073 | psepcN = '/'; |
| 4074 | pseps[0] = '\\'; |
| 4075 | } |
| 4076 | |
| 4077 | // need to adjust the file name arguments and buffer names. |
| 4078 | buflist_slash_adjust(); |
| 4079 | alist_slash_adjust(); |
| 4080 | # ifdef FEAT_EVAL |
| 4081 | scriptnames_slash_adjust(); |
| 4082 | # endif |
| 4083 | return NULL; |
| 4084 | } |
| 4085 | #endif |
| 4086 | |
| 4087 | /* |
| 4088 | * Process the new 'shiftwidth' or the 'tabstop' option value. |
| 4089 | */ |
| 4090 | char * |
| 4091 | did_set_shiftwidth_tabstop(optset_T *args) |
| 4092 | { |
| 4093 | long *pp = (long *)args->os_varp; |
| 4094 | char *errmsg = NULL; |
| 4095 | |
| 4096 | if (curbuf->b_p_sw < 0) |
| 4097 | { |
| 4098 | errmsg = e_argument_must_be_positive; |
| 4099 | #ifdef FEAT_VARTABS |
| 4100 | // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use. |
| 4101 | curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0 |
| 4102 | ? tabstop_first(curbuf->b_p_vts_array) |
| 4103 | : curbuf->b_p_ts; |
| 4104 | #else |
| 4105 | curbuf->b_p_sw = curbuf->b_p_ts; |
| 4106 | #endif |
| 4107 | } |
| 4108 | |
| 4109 | #ifdef FEAT_FOLDING |
| 4110 | if (foldmethodIsIndent(curwin)) |
| 4111 | foldUpdateAll(curwin); |
| 4112 | #endif |
| 4113 | // When 'shiftwidth' changes, or it's zero and 'tabstop' changes: |
| 4114 | // parse 'cinoptions'. |
| 4115 | if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0) |
| 4116 | parse_cino(curbuf); |
| 4117 | |
| 4118 | return errmsg; |
| 4119 | } |
| 4120 | |
| 4121 | /* |
| 4122 | * Process the new 'showtabline' option value. |
| 4123 | */ |
| 4124 | char * |
| 4125 | did_set_showtabline(optset_T *args UNUSED) |
| 4126 | { |
| 4127 | shell_new_rows(); // recompute window positions and heights |
| 4128 | return NULL; |
| 4129 | } |
| 4130 | |
| 4131 | /* |
| 4132 | * Process the updated 'smoothscroll' option value. |
| 4133 | */ |
| 4134 | char * |
| 4135 | did_set_smoothscroll(optset_T *args UNUSED) |
| 4136 | { |
Luuk van Baal | 1bf1bf5 | 2023-10-28 21:43:31 +0200 | [diff] [blame] | 4137 | if (!curwin->w_p_sms) |
| 4138 | curwin->w_skipcol = 0; |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4139 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4140 | return NULL; |
| 4141 | } |
| 4142 | |
| 4143 | #if defined(FEAT_SPELL) || defined(PROTO) |
| 4144 | /* |
| 4145 | * Process the updated 'spell' option value. |
| 4146 | */ |
| 4147 | char * |
| 4148 | did_set_spell(optset_T *args UNUSED) |
| 4149 | { |
| 4150 | if (curwin->w_p_spell) |
| 4151 | return parse_spelllang(curwin); |
| 4152 | |
| 4153 | return NULL; |
| 4154 | } |
| 4155 | #endif |
| 4156 | |
| 4157 | /* |
| 4158 | * Process the updated 'swapfile' option value. |
| 4159 | */ |
| 4160 | char * |
| 4161 | did_set_swapfile(optset_T *args UNUSED) |
| 4162 | { |
| 4163 | // when 'swf' is set, create swapfile, when reset remove swapfile |
| 4164 | if (curbuf->b_p_swf && p_uc) |
| 4165 | ml_open_file(curbuf); // create the swap file |
| 4166 | else |
| 4167 | // no need to reset curbuf->b_may_swap, ml_open_file() will check |
| 4168 | // buf->b_p_swf |
| 4169 | mf_close_file(curbuf, TRUE); // remove the swap file |
| 4170 | return NULL; |
| 4171 | } |
| 4172 | |
| 4173 | #if defined(FEAT_TERMGUICOLORS) || defined(PROTO) |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4174 | char * |
| 4175 | did_set_termguicolors(optset_T *args UNUSED) |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 4176 | { |
| 4177 | # ifdef FEAT_VTP |
| 4178 | // Do not turn on 'tgc' when 24-bit colors are not supported. |
| 4179 | if ( |
| 4180 | # ifdef VIMDLL |
| 4181 | !gui.in_use && !gui.starting && |
| 4182 | # endif |
| 4183 | !has_vtp_working()) |
| 4184 | { |
| 4185 | p_tgc = 0; |
Yegappan Lakshmanan | 80b817b | 2023-02-09 22:08:52 +0000 | [diff] [blame] | 4186 | return e_24_bit_colors_are_not_supported_on_this_environment; |
| 4187 | } |
| 4188 | if (is_term_win32()) |
| 4189 | swap_tcap(); |
| 4190 | # endif |
| 4191 | # ifdef FEAT_GUI |
| 4192 | if (!gui.in_use && !gui.starting) |
| 4193 | # endif |
| 4194 | highlight_gui_started(); |
| 4195 | # ifdef FEAT_VTP |
| 4196 | // reset t_Co |
| 4197 | if (is_term_win32()) |
| 4198 | { |
| 4199 | control_console_color_rgb(); |
| 4200 | set_termname(T_NAME); |
| 4201 | init_highlight(TRUE, FALSE); |
| 4202 | } |
| 4203 | # endif |
| 4204 | # ifdef FEAT_TERMINAL |
| 4205 | term_update_colors_all(); |
| 4206 | term_update_palette_all(); |
| 4207 | term_update_wincolor_all(); |
| 4208 | # endif |
| 4209 | |
| 4210 | return NULL; |
| 4211 | } |
| 4212 | #endif |
| 4213 | |
| 4214 | /* |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4215 | * Process the updated 'terse' option value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4216 | */ |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4217 | char * |
| 4218 | did_set_terse(optset_T *args UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4219 | { |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4220 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4221 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4222 | // when 'terse' is set change 'shortmess' |
| 4223 | p = vim_strchr(p_shm, SHM_SEARCH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4224 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4225 | // insert 's' in p_shm |
| 4226 | if (p_terse && p == NULL) |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4227 | { |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4228 | STRCPY(IObuff, p_shm); |
| 4229 | STRCAT(IObuff, "s"); |
| 4230 | set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0); |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4231 | } |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4232 | // remove 's' from p_shm |
| 4233 | else if (!p_terse && p != NULL) |
| 4234 | STRMOVE(p, p + 1); |
| 4235 | return NULL; |
| 4236 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4237 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4238 | /* |
| 4239 | * Process the updated 'textauto' option value. |
| 4240 | */ |
| 4241 | char * |
| 4242 | did_set_textauto(optset_T *args) |
| 4243 | { |
| 4244 | // when 'textauto' is set or reset also change 'fileformats' |
| 4245 | set_string_option_direct((char_u *)"ffs", -1, |
| 4246 | p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"", |
| 4247 | OPT_FREE | args->os_flags, 0); |
Bram Moolenaar | 5374430 | 2015-07-17 17:38:22 +0200 | [diff] [blame] | 4248 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4249 | return NULL; |
| 4250 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4251 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4252 | /* |
| 4253 | * Process the updated 'textmode' option value. |
| 4254 | */ |
| 4255 | char * |
| 4256 | did_set_textmode(optset_T *args) |
| 4257 | { |
| 4258 | // when 'textmode' is set or reset also change 'fileformat' |
| 4259 | set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags); |
| 4260 | |
| 4261 | return NULL; |
| 4262 | } |
| 4263 | |
| 4264 | /* |
| 4265 | * Process the new 'textwidth' option value. |
| 4266 | */ |
| 4267 | char * |
| 4268 | did_set_textwidth(optset_T *args UNUSED) |
| 4269 | { |
| 4270 | char *errmsg = NULL; |
| 4271 | |
| 4272 | if (curbuf->b_p_tw < 0) |
| 4273 | { |
| 4274 | errmsg = e_argument_must_be_positive; |
| 4275 | curbuf->b_p_tw = 0; |
| 4276 | } |
| 4277 | #ifdef FEAT_SYN_HL |
| 4278 | { |
| 4279 | win_T *wp; |
| 4280 | tabpage_T *tp; |
| 4281 | |
| 4282 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 4283 | check_colorcolumn(wp); |
| 4284 | } |
Bram Moolenaar | 5374430 | 2015-07-17 17:38:22 +0200 | [diff] [blame] | 4285 | #endif |
| 4286 | |
Bram Moolenaar | 31e5c60 | 2022-04-15 13:53:33 +0100 | [diff] [blame] | 4287 | return errmsg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | /* |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4291 | * Process the updated 'title' or the 'icon' option value. |
| 4292 | */ |
| 4293 | char * |
| 4294 | did_set_title_icon(optset_T *args UNUSED) |
| 4295 | { |
| 4296 | // when 'title' changed, may need to change the title; same for 'icon' |
| 4297 | did_set_title(); |
| 4298 | return NULL; |
| 4299 | } |
| 4300 | |
| 4301 | /* |
| 4302 | * Process the new 'titlelen' option value. |
| 4303 | */ |
| 4304 | char * |
| 4305 | did_set_titlelen(optset_T *args) |
| 4306 | { |
| 4307 | long old_value = args->os_oldval.number; |
| 4308 | char *errmsg = NULL; |
| 4309 | |
| 4310 | // if 'titlelen' has changed, redraw the title |
| 4311 | if (p_titlelen < 0) |
| 4312 | { |
| 4313 | errmsg = e_argument_must_be_positive; |
| 4314 | p_titlelen = 85; |
| 4315 | } |
| 4316 | if (starting != NO_SCREEN && old_value != p_titlelen) |
| 4317 | need_maketitle = TRUE; |
| 4318 | |
| 4319 | return errmsg; |
| 4320 | } |
| 4321 | |
| 4322 | #if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO) |
| 4323 | /* |
| 4324 | * Process the updated 'undofile' option value. |
| 4325 | */ |
| 4326 | char * |
| 4327 | did_set_undofile(optset_T *args) |
| 4328 | { |
| 4329 | // Only take action when the option was set. |
| 4330 | if (!curbuf->b_p_udf && !p_udf) |
| 4331 | return NULL; |
| 4332 | |
| 4333 | // When reset we do not delete the undo file, the option may be set again |
| 4334 | // without making any changes in between. |
| 4335 | char_u hash[UNDO_HASH_SIZE]; |
| 4336 | buf_T *save_curbuf = curbuf; |
| 4337 | |
| 4338 | FOR_ALL_BUFFERS(curbuf) |
| 4339 | { |
| 4340 | // When 'undofile' is set globally: for every buffer, otherwise |
| 4341 | // only for the current buffer: Try to read in the undofile, |
| 4342 | // if one exists, the buffer wasn't changed and the buffer was |
| 4343 | // loaded |
| 4344 | if ((curbuf == save_curbuf |
| 4345 | || (args->os_flags & OPT_GLOBAL) |
| 4346 | || args->os_flags == 0) |
| 4347 | && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL) |
| 4348 | { |
| 4349 | #ifdef FEAT_CRYPT |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 4350 | if (crypt_method_is_sodium(crypt_get_method_nr(curbuf))) |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4351 | continue; |
| 4352 | #endif |
| 4353 | u_compute_hash(hash); |
| 4354 | u_read_undo(NULL, hash, curbuf->b_fname); |
| 4355 | } |
| 4356 | } |
| 4357 | curbuf = save_curbuf; |
| 4358 | |
| 4359 | return NULL; |
| 4360 | } |
| 4361 | #endif |
| 4362 | |
| 4363 | /* |
| 4364 | * Process the new global 'undolevels' option value. |
| 4365 | */ |
| 4366 | static void |
| 4367 | update_global_undolevels(long value, long old_value) |
| 4368 | { |
| 4369 | // sync undo before 'undolevels' changes |
| 4370 | |
| 4371 | // use the old value, otherwise u_sync() may not work properly |
| 4372 | p_ul = old_value; |
| 4373 | u_sync(TRUE); |
| 4374 | p_ul = value; |
| 4375 | } |
| 4376 | |
| 4377 | /* |
| 4378 | * Process the new buffer local 'undolevels' option value. |
| 4379 | */ |
| 4380 | static void |
| 4381 | update_buflocal_undolevels(long value, long old_value) |
| 4382 | { |
| 4383 | // use the old value, otherwise u_sync() may not work properly |
| 4384 | curbuf->b_p_ul = old_value; |
| 4385 | u_sync(TRUE); |
| 4386 | curbuf->b_p_ul = value; |
| 4387 | } |
| 4388 | |
| 4389 | /* |
| 4390 | * Process the new 'undolevels' option value. |
| 4391 | */ |
| 4392 | char * |
| 4393 | did_set_undolevels(optset_T *args) |
| 4394 | { |
| 4395 | long *pp = (long *)args->os_varp; |
| 4396 | |
| 4397 | if (pp == &p_ul) // global 'undolevels' |
| 4398 | update_global_undolevels(args->os_newval.number, |
| 4399 | args->os_oldval.number); |
| 4400 | else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels' |
| 4401 | update_buflocal_undolevels(args->os_newval.number, |
| 4402 | args->os_oldval.number); |
| 4403 | |
| 4404 | return NULL; |
| 4405 | } |
| 4406 | |
| 4407 | /* |
| 4408 | * Process the new 'updatecount' option value. |
| 4409 | */ |
| 4410 | char * |
| 4411 | did_set_updatecount(optset_T *args) |
| 4412 | { |
| 4413 | long old_value = args->os_oldval.number; |
| 4414 | char *errmsg = NULL; |
| 4415 | |
| 4416 | // when 'updatecount' changes from zero to non-zero, open swap files |
| 4417 | if (p_uc < 0) |
| 4418 | { |
| 4419 | errmsg = e_argument_must_be_positive; |
| 4420 | p_uc = 100; |
| 4421 | } |
| 4422 | if (p_uc && !old_value) |
| 4423 | ml_open_files(); |
| 4424 | |
| 4425 | return errmsg; |
| 4426 | } |
| 4427 | |
| 4428 | /* |
| 4429 | * Process the updated 'weirdinvert' option value. |
| 4430 | */ |
| 4431 | char * |
| 4432 | did_set_weirdinvert(optset_T *args) |
| 4433 | { |
| 4434 | // When 'weirdinvert' changed, set/reset 't_xs'. |
| 4435 | // Then set 'weirdinvert' according to value of 't_xs'. |
| 4436 | if (p_wiv && !args->os_oldval.boolean) |
| 4437 | T_XS = (char_u *)"y"; |
| 4438 | else if (!p_wiv && args->os_oldval.boolean) |
| 4439 | T_XS = empty_option; |
| 4440 | p_wiv = (*T_XS != NUL); |
| 4441 | |
| 4442 | return NULL; |
| 4443 | } |
| 4444 | |
| 4445 | /* |
Yee Cheng Chin | 8f4fb00 | 2023-10-17 10:06:56 +0200 | [diff] [blame] | 4446 | * Process the new 'wildchar' / 'wildcharm' option value. |
| 4447 | */ |
| 4448 | char * |
| 4449 | did_set_wildchar(optset_T *args) |
| 4450 | { |
| 4451 | long c = *(long *)args->os_varp; |
| 4452 | |
| 4453 | // Don't allow key values that wouldn't work as wildchar. |
| 4454 | if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER) |
| 4455 | return e_invalid_argument; |
| 4456 | |
| 4457 | return NULL; |
| 4458 | } |
| 4459 | |
| 4460 | /* |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4461 | * Process the new 'window' option value. |
| 4462 | */ |
| 4463 | char * |
| 4464 | did_set_window(optset_T *args UNUSED) |
| 4465 | { |
| 4466 | if (p_window < 1) |
| 4467 | p_window = 1; |
| 4468 | else if (p_window >= Rows) |
| 4469 | p_window = Rows - 1; |
| 4470 | return NULL; |
| 4471 | } |
| 4472 | |
| 4473 | /* |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4474 | * Process the new 'winheight' or the 'helpheight' option value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4475 | */ |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4476 | char * |
| 4477 | did_set_winheight_helpheight(optset_T *args) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4478 | { |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4479 | long *pp = (long *)args->os_varp; |
| 4480 | char *errmsg = NULL; |
| 4481 | |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4482 | if (p_wh < 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4483 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4484 | errmsg = e_argument_must_be_positive; |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4485 | p_wh = 1; |
| 4486 | } |
| 4487 | if (p_wmh > p_wh) |
| 4488 | { |
| 4489 | errmsg = e_winheight_cannot_be_smaller_than_winminheight; |
| 4490 | p_wh = p_wmh; |
| 4491 | } |
| 4492 | if (p_hh < 0) |
| 4493 | { |
| 4494 | errmsg = e_argument_must_be_positive; |
| 4495 | p_hh = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4498 | // Change window height NOW |
| 4499 | if (!ONE_WINDOW) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4500 | { |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4501 | if (pp == &p_wh && curwin->w_height < p_wh) |
| 4502 | win_setheight((int)p_wh); |
| 4503 | if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh) |
| 4504 | win_setheight((int)p_hh); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4505 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4506 | |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4507 | return errmsg; |
| 4508 | } |
| 4509 | |
| 4510 | /* |
| 4511 | * Process the new 'winminheight' option value. |
| 4512 | */ |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4513 | char * |
| 4514 | did_set_winminheight(optset_T *args UNUSED) |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4515 | { |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4516 | char *errmsg = NULL; |
| 4517 | |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4518 | if (p_wmh < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4519 | { |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4520 | errmsg = e_argument_must_be_positive; |
| 4521 | p_wmh = 0; |
| 4522 | } |
| 4523 | if (p_wmh > p_wh) |
| 4524 | { |
| 4525 | errmsg = e_winheight_cannot_be_smaller_than_winminheight; |
| 4526 | p_wmh = p_wh; |
| 4527 | } |
| 4528 | win_setminheight(); |
| 4529 | |
| 4530 | return errmsg; |
| 4531 | } |
| 4532 | |
| 4533 | /* |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4534 | * Process the new 'winminwidth' option value. |
| 4535 | */ |
| 4536 | char * |
| 4537 | did_set_winminwidth(optset_T *args UNUSED) |
| 4538 | { |
| 4539 | char *errmsg = NULL; |
| 4540 | |
| 4541 | if (p_wmw < 0) |
| 4542 | { |
| 4543 | errmsg = e_argument_must_be_positive; |
| 4544 | p_wmw = 0; |
| 4545 | } |
| 4546 | if (p_wmw > p_wiw) |
| 4547 | { |
| 4548 | errmsg = e_winwidth_cannot_be_smaller_than_winminwidth; |
| 4549 | p_wmw = p_wiw; |
| 4550 | } |
| 4551 | win_setminwidth(); |
| 4552 | |
| 4553 | return errmsg; |
| 4554 | } |
| 4555 | |
| 4556 | /* |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4557 | * Process the new 'winwidth' option value. |
| 4558 | */ |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4559 | char * |
| 4560 | did_set_winwidth(optset_T *args UNUSED) |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4561 | { |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4562 | char *errmsg = NULL; |
| 4563 | |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4564 | if (p_wiw < 1) |
| 4565 | { |
| 4566 | errmsg = e_argument_must_be_positive; |
| 4567 | p_wiw = 1; |
| 4568 | } |
| 4569 | if (p_wmw > p_wiw) |
| 4570 | { |
| 4571 | errmsg = e_winwidth_cannot_be_smaller_than_winminwidth; |
| 4572 | p_wiw = p_wmw; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4573 | } |
Bram Moolenaar | 4c7ed46 | 2006-02-15 22:18:42 +0000 | [diff] [blame] | 4574 | |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4575 | // Change window width NOW |
| 4576 | if (!ONE_WINDOW && curwin->w_width < p_wiw) |
| 4577 | win_setwidth((int)p_wiw); |
| 4578 | |
| 4579 | return errmsg; |
| 4580 | } |
| 4581 | |
| 4582 | /* |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4583 | * Process the updated 'wrap' option value. |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4584 | */ |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4585 | char * |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4586 | did_set_wrap(optset_T *args UNUSED) |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4587 | { |
Luuk van Baal | 1bf1bf5 | 2023-10-28 21:43:31 +0200 | [diff] [blame] | 4588 | // Set w_leftcol or w_skipcol to zero. |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4589 | if (curwin->w_p_wrap) |
| 4590 | curwin->w_leftcol = 0; |
Luuk van Baal | 1bf1bf5 | 2023-10-28 21:43:31 +0200 | [diff] [blame] | 4591 | else |
| 4592 | curwin->w_skipcol = 0; |
| 4593 | |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4594 | return NULL; |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | /* |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4598 | * Set the value of a boolean option, and take care of side effects. |
| 4599 | * Returns NULL for success, or an error message for an error. |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4600 | */ |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4601 | static char * |
| 4602 | set_bool_option( |
| 4603 | int opt_idx, // index in options[] table |
| 4604 | char_u *varp, // pointer to the option variable |
| 4605 | int value, // new value |
| 4606 | int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4607 | { |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4608 | int old_value = *(int *)varp; |
| 4609 | #if defined(FEAT_EVAL) |
| 4610 | int old_global_value = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4611 | #endif |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4612 | char *errmsg = NULL; |
| 4613 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4614 | // Disallow changing some options from secure mode |
| 4615 | if ((secure |
| 4616 | #ifdef HAVE_SANDBOX |
| 4617 | || sandbox != 0 |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4618 | #endif |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4619 | ) && (options[opt_idx].flags & P_SECURE)) |
| 4620 | return e_not_allowed_here; |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4621 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4622 | #if defined(FEAT_EVAL) |
| 4623 | // Save the global value before changing anything. This is needed as for |
| 4624 | // a global-only option setting the "local value" in fact sets the global |
| 4625 | // value (since there is only one value). |
| 4626 | if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) |
| 4627 | old_global_value = *(int *)get_varp_scope(&(options[opt_idx]), |
| 4628 | OPT_GLOBAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4629 | #endif |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4630 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4631 | *(int *)varp = value; // set the new value |
| 4632 | #ifdef FEAT_EVAL |
| 4633 | // Remember where the option was set. |
| 4634 | set_option_sctx_idx(opt_idx, opt_flags, current_sctx); |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 4635 | #endif |
| 4636 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4637 | #ifdef FEAT_GUI |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4638 | need_mouse_correct = TRUE; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 4639 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4640 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4641 | // May set global value for local option. |
| 4642 | if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) |
| 4643 | *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value; |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4644 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4645 | // Handle side effects of changing a bool option. |
| 4646 | if (options[opt_idx].opt_did_set_cb != NULL) |
| 4647 | { |
| 4648 | optset_T args; |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4649 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4650 | CLEAR_FIELD(args); |
| 4651 | args.os_varp = varp; |
| 4652 | args.os_flags = opt_flags; |
| 4653 | args.os_oldval.boolean = old_value; |
| 4654 | args.os_newval.boolean = value; |
| 4655 | args.os_errbuf = NULL; |
| 4656 | errmsg = options[opt_idx].opt_did_set_cb(&args); |
zeertzjq | deba02d | 2023-11-02 21:01:19 +0100 | [diff] [blame] | 4657 | if (errmsg != NULL) |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4658 | return errmsg; |
| 4659 | } |
| 4660 | |
| 4661 | // after handling side effects, call autocommand |
| 4662 | |
| 4663 | options[opt_idx].flags |= P_WAS_SET; |
| 4664 | |
| 4665 | #if defined(FEAT_EVAL) |
| 4666 | apply_optionset_autocmd(opt_idx, opt_flags, |
| 4667 | (long)(old_value ? TRUE : FALSE), |
| 4668 | (long)(old_global_value ? TRUE : FALSE), |
| 4669 | (long)(value ? TRUE : FALSE), NULL); |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 4670 | #endif |
| 4671 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4672 | comp_col(); // in case 'ruler' or 'showcmd' changed |
zeertzjq | fcaed6a | 2024-02-18 09:33:54 +0100 | [diff] [blame] | 4673 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4674 | if (curwin->w_curswant != MAXCOL |
zeertzjq | fcaed6a | 2024-02-18 09:33:54 +0100 | [diff] [blame] | 4675 | && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0 |
| 4676 | && (options[opt_idx].flags & P_HLONLY) == 0) |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4677 | curwin->w_set_curswant = TRUE; |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4678 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4679 | if ((opt_flags & OPT_NO_REDRAW) == 0) |
| 4680 | check_redraw(options[opt_idx].flags); |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4681 | |
| 4682 | return errmsg; |
| 4683 | } |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4684 | |
| 4685 | /* |
| 4686 | * Check the bounds of numeric options. |
| 4687 | */ |
| 4688 | static char * |
| 4689 | check_num_option_bounds( |
| 4690 | long *pp, |
| 4691 | long old_value, |
| 4692 | long old_Rows, |
| 4693 | long old_Columns, |
| 4694 | char *errbuf, |
| 4695 | size_t errbuflen, |
| 4696 | char *errmsg) |
| 4697 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4698 | if (Rows < min_rows() && full_screen) |
| 4699 | { |
| 4700 | if (errbuf != NULL) |
| 4701 | { |
=?UTF-8?q?Dundar=20G=C3=B6c?= | 420fabc | 2022-01-28 15:28:04 +0000 | [diff] [blame] | 4702 | vim_snprintf(errbuf, errbuflen, |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4703 | _(e_need_at_least_nr_lines), min_rows()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4704 | errmsg = errbuf; |
| 4705 | } |
| 4706 | Rows = min_rows(); |
| 4707 | } |
| 4708 | if (Columns < MIN_COLUMNS && full_screen) |
| 4709 | { |
| 4710 | if (errbuf != NULL) |
| 4711 | { |
=?UTF-8?q?Dundar=20G=C3=B6c?= | 420fabc | 2022-01-28 15:28:04 +0000 | [diff] [blame] | 4712 | vim_snprintf(errbuf, errbuflen, |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4713 | _(e_need_at_least_nr_columns), MIN_COLUMNS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4714 | errmsg = errbuf; |
| 4715 | } |
| 4716 | Columns = MIN_COLUMNS; |
| 4717 | } |
Bram Moolenaar | e057d40 | 2013-06-30 17:51:51 +0200 | [diff] [blame] | 4718 | limit_screen_size(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4719 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 4720 | // If the screen (shell) height has been changed, assume it is the |
| 4721 | // physical screenheight. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4722 | if (old_Rows != Rows || old_Columns != Columns) |
| 4723 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4724 | // Changing the screen size is not allowed while updating the screen. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4725 | if (updating_screen) |
| 4726 | *pp = old_value; |
| 4727 | else if (full_screen |
| 4728 | #ifdef FEAT_GUI |
| 4729 | && !gui.starting |
| 4730 | #endif |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4731 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4732 | set_shellsize((int)Columns, (int)Rows, TRUE); |
| 4733 | else |
| 4734 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4735 | // Postpone the resizing; check the size and cmdline position for |
| 4736 | // messages. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4737 | check_shellsize(); |
| 4738 | if (cmdline_row > Rows - p_ch && Rows > p_ch) |
| 4739 | cmdline_row = Rows - p_ch; |
| 4740 | } |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4741 | if (p_window >= Rows || !option_was_set((char_u *)"window")) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 4742 | p_window = Rows - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4745 | if (curbuf->b_p_ts <= 0) |
| 4746 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4747 | errmsg = e_argument_must_be_positive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4748 | curbuf->b_p_ts = 8; |
| 4749 | } |
Bram Moolenaar | 652dee4 | 2022-01-28 20:47:49 +0000 | [diff] [blame] | 4750 | else if (curbuf->b_p_ts > TABSTOP_MAX) |
| 4751 | { |
| 4752 | errmsg = e_invalid_argument; |
| 4753 | curbuf->b_p_ts = 8; |
| 4754 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4755 | if (p_tm < 0) |
| 4756 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4757 | errmsg = e_argument_must_be_positive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4758 | p_tm = 0; |
| 4759 | } |
| 4760 | if ((curwin->w_p_scr <= 0 |
| 4761 | || (curwin->w_p_scr > curwin->w_height |
| 4762 | && curwin->w_height > 0)) |
| 4763 | && full_screen) |
| 4764 | { |
| 4765 | if (pp == &(curwin->w_p_scr)) |
| 4766 | { |
| 4767 | if (curwin->w_p_scr != 0) |
Bram Moolenaar | d8e4447 | 2021-07-21 22:20:33 +0200 | [diff] [blame] | 4768 | errmsg = e_invalid_scroll_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4769 | win_comp_scroll(curwin); |
| 4770 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4771 | // If 'scroll' became invalid because of a side effect silently adjust |
| 4772 | // it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4773 | else if (curwin->w_p_scr <= 0) |
| 4774 | curwin->w_p_scr = 1; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4775 | else // curwin->w_p_scr > curwin->w_height |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4776 | curwin->w_p_scr = curwin->w_height; |
| 4777 | } |
Bram Moolenaar | 991e10f | 2008-10-02 20:48:41 +0000 | [diff] [blame] | 4778 | if (p_hi < 0) |
| 4779 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4780 | errmsg = e_argument_must_be_positive; |
Bram Moolenaar | 991e10f | 2008-10-02 20:48:41 +0000 | [diff] [blame] | 4781 | p_hi = 0; |
| 4782 | } |
Bram Moolenaar | 78159bb | 2014-06-25 11:48:54 +0200 | [diff] [blame] | 4783 | else if (p_hi > 10000) |
| 4784 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4785 | errmsg = e_invalid_argument; |
Bram Moolenaar | 78159bb | 2014-06-25 11:48:54 +0200 | [diff] [blame] | 4786 | p_hi = 10000; |
| 4787 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4788 | if (p_re < 0 || p_re > 2) |
| 4789 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4790 | errmsg = e_invalid_argument; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4791 | p_re = 0; |
| 4792 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4793 | if (p_report < 0) |
| 4794 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4795 | errmsg = e_argument_must_be_positive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4796 | p_report = 1; |
| 4797 | } |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 4798 | if ((p_sj < -100 || p_sj >= Rows) && full_screen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4799 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4800 | if (Rows != old_Rows) // Rows changed, just adjust p_sj |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4801 | p_sj = Rows / 2; |
| 4802 | else |
| 4803 | { |
Bram Moolenaar | d8e4447 | 2021-07-21 22:20:33 +0200 | [diff] [blame] | 4804 | errmsg = e_invalid_scroll_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4805 | p_sj = 1; |
| 4806 | } |
| 4807 | } |
| 4808 | if (p_so < 0 && full_screen) |
| 4809 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4810 | errmsg = e_argument_must_be_positive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4811 | p_so = 0; |
| 4812 | } |
| 4813 | if (p_siso < 0 && full_screen) |
| 4814 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4815 | errmsg = e_argument_must_be_positive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4816 | p_siso = 0; |
| 4817 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4818 | if (p_cwh < 1) |
| 4819 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4820 | errmsg = e_argument_must_be_positive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4821 | p_cwh = 1; |
| 4822 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4823 | if (p_ut < 0) |
| 4824 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4825 | errmsg = e_argument_must_be_positive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | p_ut = 2000; |
| 4827 | } |
| 4828 | if (p_ss < 0) |
| 4829 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 4830 | errmsg = e_argument_must_be_positive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4831 | p_ss = 0; |
| 4832 | } |
| 4833 | |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4834 | return errmsg; |
| 4835 | } |
| 4836 | |
| 4837 | /* |
| 4838 | * Set the value of a number option, and take care of side effects. |
| 4839 | * Returns NULL for success, or an error message for an error. |
| 4840 | */ |
| 4841 | static char * |
| 4842 | set_num_option( |
| 4843 | int opt_idx, // index in options[] table |
| 4844 | char_u *varp, // pointer to the option variable |
| 4845 | long value, // new value |
| 4846 | char *errbuf, // buffer for error messages |
| 4847 | size_t errbuflen, // length of "errbuf" |
| 4848 | int opt_flags) // OPT_LOCAL, OPT_GLOBAL, |
| 4849 | // OPT_MODELINE, etc. |
| 4850 | { |
| 4851 | char *errmsg = NULL; |
| 4852 | long old_value = *(long *)varp; |
| 4853 | #if defined(FEAT_EVAL) |
| 4854 | long old_global_value = 0; // only used when setting a local and |
| 4855 | // global option |
| 4856 | #endif |
| 4857 | long old_Rows = Rows; // remember old Rows |
| 4858 | long old_Columns = Columns; // remember old Columns |
| 4859 | long *pp = (long *)varp; |
| 4860 | |
| 4861 | // Disallow changing some options from secure mode. |
| 4862 | if ((secure |
| 4863 | #ifdef HAVE_SANDBOX |
| 4864 | || sandbox != 0 |
| 4865 | #endif |
| 4866 | ) && (options[opt_idx].flags & P_SECURE)) |
| 4867 | return e_not_allowed_here; |
| 4868 | |
| 4869 | #if defined(FEAT_EVAL) |
| 4870 | // Save the global value before changing anything. This is needed as for |
| 4871 | // a global-only option setting the "local value" in fact sets the global |
| 4872 | // value (since there is only one value). |
| 4873 | if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) |
| 4874 | old_global_value = *(long *)get_varp_scope(&(options[opt_idx]), |
| 4875 | OPT_GLOBAL); |
| 4876 | #endif |
| 4877 | |
| 4878 | *pp = value; |
| 4879 | #ifdef FEAT_EVAL |
| 4880 | // Remember where the option was set. |
| 4881 | set_option_sctx_idx(opt_idx, opt_flags, current_sctx); |
| 4882 | #endif |
| 4883 | #ifdef FEAT_GUI |
| 4884 | need_mouse_correct = TRUE; |
| 4885 | #endif |
| 4886 | |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4887 | // Invoke the option specific callback function to validate and apply the |
| 4888 | // new value. |
| 4889 | if (options[opt_idx].opt_did_set_cb != NULL) |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4890 | { |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4891 | optset_T args; |
| 4892 | |
Yegappan Lakshmanan | 5284b23 | 2023-03-04 19:57:32 +0000 | [diff] [blame] | 4893 | CLEAR_FIELD(args); |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4894 | args.os_varp = varp; |
| 4895 | args.os_flags = opt_flags; |
| 4896 | args.os_oldval.number = old_value; |
| 4897 | args.os_newval.number = value; |
Yegappan Lakshmanan | 6d611de | 2023-02-25 11:59:33 +0000 | [diff] [blame] | 4898 | args.os_errbuf = NULL; |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4899 | errmsg = options[opt_idx].opt_did_set_cb(&args); |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 4902 | // Check the bounds for numeric options here |
Yegappan Lakshmanan | 0caaf1e | 2023-02-09 12:23:17 +0000 | [diff] [blame] | 4903 | errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns, |
| 4904 | errbuf, errbuflen, errmsg); |
| 4905 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4906 | // May set global value for local option. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4907 | if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) |
| 4908 | *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp; |
| 4909 | |
| 4910 | options[opt_idx].flags |= P_WAS_SET; |
| 4911 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 4912 | #if defined(FEAT_EVAL) |
Bram Moolenaar | f414048 | 2020-02-15 23:06:45 +0100 | [diff] [blame] | 4913 | apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value, |
| 4914 | value, errmsg); |
Bram Moolenaar | 5374430 | 2015-07-17 17:38:22 +0200 | [diff] [blame] | 4915 | #endif |
| 4916 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4917 | comp_col(); // in case 'columns' or 'ls' changed |
zeertzjq | fcaed6a | 2024-02-18 09:33:54 +0100 | [diff] [blame] | 4918 | |
Bram Moolenaar | 913077c | 2012-03-28 19:59:04 +0200 | [diff] [blame] | 4919 | if (curwin->w_curswant != MAXCOL |
zeertzjq | fcaed6a | 2024-02-18 09:33:54 +0100 | [diff] [blame] | 4920 | && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0 |
| 4921 | && (options[opt_idx].flags & P_HLONLY) == 0) |
Bram Moolenaar | 913077c | 2012-03-28 19:59:04 +0200 | [diff] [blame] | 4922 | curwin->w_set_curswant = TRUE; |
zeertzjq | fcaed6a | 2024-02-18 09:33:54 +0100 | [diff] [blame] | 4923 | |
Bram Moolenaar | 37294bd | 2021-03-10 13:40:08 +0100 | [diff] [blame] | 4924 | if ((opt_flags & OPT_NO_REDRAW) == 0) |
| 4925 | check_redraw(options[opt_idx].flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4926 | |
| 4927 | return errmsg; |
| 4928 | } |
| 4929 | |
| 4930 | /* |
| 4931 | * Called after an option changed: check if something needs to be redrawn. |
| 4932 | */ |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 4933 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4934 | check_redraw(long_u flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4935 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4936 | // Careful: P_RCLR and P_RALL are a combination of other P_ flags |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4937 | int doclear = (flags & P_RCLR) == P_RCLR; |
| 4938 | int all = ((flags & P_RALL) == P_RALL || doclear); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4939 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4940 | if ((flags & P_RSTAT) || all) // mark all status lines dirty |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4941 | status_redraw_all(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4942 | |
| 4943 | if ((flags & P_RBUF) || (flags & P_RWIN) || all) |
zeertzjq | fcaed6a | 2024-02-18 09:33:54 +0100 | [diff] [blame] | 4944 | { |
| 4945 | if (flags & P_HLONLY) |
| 4946 | redraw_later(UPD_NOT_VALID); |
| 4947 | else |
| 4948 | changed_window_setting(); |
| 4949 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4950 | if (flags & P_RBUF) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 4951 | redraw_curbuf_later(UPD_NOT_VALID); |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4952 | if (doclear) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 4953 | redraw_all_later(UPD_CLEAR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4954 | else if (all) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 4955 | redraw_all_later(UPD_NOT_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
| 4958 | /* |
| 4959 | * Find index for option 'arg'. |
| 4960 | * Return -1 if not found. |
| 4961 | */ |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 4962 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4963 | findoption(char_u *arg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4964 | { |
| 4965 | int opt_idx; |
| 4966 | char *s, *p; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 4967 | static short quick_tab[27] = {0, 0}; // quick access table |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4968 | int is_term_opt; |
| 4969 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 4970 | // For first call: Initialize the quick-access table. |
| 4971 | // It contains the index for the first option that starts with a certain |
| 4972 | // letter. There are 26 letters, plus the first "t_" option. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4973 | if (quick_tab[1] == 0) |
| 4974 | { |
| 4975 | p = options[0].fullname; |
| 4976 | for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++) |
| 4977 | { |
| 4978 | if (s[0] != p[0]) |
| 4979 | { |
| 4980 | if (s[0] == 't' && s[1] == '_') |
| 4981 | quick_tab[26] = opt_idx; |
| 4982 | else |
| 4983 | quick_tab[CharOrdLow(s[0])] = opt_idx; |
| 4984 | } |
| 4985 | p = s; |
| 4986 | } |
| 4987 | } |
| 4988 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 4989 | // Check for name starting with an illegal character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4990 | if (arg[0] < 'a' || arg[0] > 'z') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4991 | return -1; |
| 4992 | |
| 4993 | is_term_opt = (arg[0] == 't' && arg[1] == '_'); |
| 4994 | if (is_term_opt) |
| 4995 | opt_idx = quick_tab[26]; |
| 4996 | else |
| 4997 | opt_idx = quick_tab[CharOrdLow(arg[0])]; |
zeertzjq | f48558e | 2023-12-08 21:34:31 +0100 | [diff] [blame] | 4998 | for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4999 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5000 | if (STRCMP(arg, s) == 0) // match full name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5001 | break; |
| 5002 | } |
zeertzjq | f48558e | 2023-12-08 21:34:31 +0100 | [diff] [blame] | 5003 | if (s != NULL && s[0] != arg[0]) |
| 5004 | s = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5005 | if (s == NULL && !is_term_opt) |
| 5006 | { |
| 5007 | opt_idx = quick_tab[CharOrdLow(arg[0])]; |
| 5008 | for ( ; options[opt_idx].fullname != NULL; opt_idx++) |
| 5009 | { |
| 5010 | s = options[opt_idx].shortname; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5011 | if (s != NULL && STRCMP(arg, s) == 0) // match short name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5012 | break; |
| 5013 | s = NULL; |
| 5014 | } |
| 5015 | } |
| 5016 | if (s == NULL) |
| 5017 | opt_idx = -1; |
| 5018 | return opt_idx; |
| 5019 | } |
| 5020 | |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 5021 | #if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \ |
| 5022 | || defined(FEAT_SPELL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5023 | /* |
| 5024 | * Get the value for an option. |
| 5025 | * |
| 5026 | * Returns: |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5027 | * Number option: gov_number, *numval gets value. |
Bram Moolenaar | a725149 | 2021-01-02 16:53:13 +0100 | [diff] [blame] | 5028 | * Toggle option: gov_bool, *numval gets value. |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5029 | * String option: gov_string, *stringval gets allocated string. |
| 5030 | * Hidden Number option: gov_hidden_number. |
| 5031 | * Hidden Toggle option: gov_hidden_bool. |
| 5032 | * Hidden String option: gov_hidden_string. |
| 5033 | * Unknown option: gov_unknown. |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 5034 | * |
| 5035 | * "flagsp" (if not NULL) is set to the option flags (P_xxxx). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5036 | */ |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5037 | getoption_T |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5038 | get_option_value( |
| 5039 | char_u *name, |
| 5040 | long *numval, |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5041 | char_u **stringval, // NULL when only checking existence |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 5042 | int *flagsp, |
| 5043 | int scope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5044 | { |
| 5045 | int opt_idx; |
| 5046 | char_u *varp; |
| 5047 | |
| 5048 | opt_idx = findoption(name); |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5049 | if (opt_idx < 0) // option not in the table |
Bram Moolenaar | e353c40 | 2017-02-04 19:49:16 +0100 | [diff] [blame] | 5050 | { |
| 5051 | int key; |
| 5052 | |
| 5053 | if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_' |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5054 | && (key = find_key_option(name, FALSE)) != 0) |
Bram Moolenaar | e353c40 | 2017-02-04 19:49:16 +0100 | [diff] [blame] | 5055 | { |
| 5056 | char_u key_name[2]; |
| 5057 | char_u *p; |
| 5058 | |
Bram Moolenaar | 10c75c4 | 2021-12-28 20:53:30 +0000 | [diff] [blame] | 5059 | if (flagsp != NULL) |
| 5060 | *flagsp = 0; // terminal option has no flags |
| 5061 | |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5062 | // check for a terminal option |
Bram Moolenaar | e353c40 | 2017-02-04 19:49:16 +0100 | [diff] [blame] | 5063 | if (key < 0) |
| 5064 | { |
| 5065 | key_name[0] = KEY2TERMCAP0(key); |
| 5066 | key_name[1] = KEY2TERMCAP1(key); |
| 5067 | } |
| 5068 | else |
| 5069 | { |
| 5070 | key_name[0] = KS_KEY; |
| 5071 | key_name[1] = (key & 0xff); |
| 5072 | } |
| 5073 | p = find_termcode(key_name); |
| 5074 | if (p != NULL) |
| 5075 | { |
| 5076 | if (stringval != NULL) |
| 5077 | *stringval = vim_strsave(p); |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5078 | return gov_string; |
Bram Moolenaar | e353c40 | 2017-02-04 19:49:16 +0100 | [diff] [blame] | 5079 | } |
| 5080 | } |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5081 | return gov_unknown; |
Bram Moolenaar | e353c40 | 2017-02-04 19:49:16 +0100 | [diff] [blame] | 5082 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5083 | |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 5084 | varp = get_varp_scope(&(options[opt_idx]), scope); |
| 5085 | |
| 5086 | if (flagsp != NULL) |
| 5087 | // Return the P_xxxx option flags. |
| 5088 | *flagsp = options[opt_idx].flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5089 | |
| 5090 | if (options[opt_idx].flags & P_STRING) |
| 5091 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5092 | if (varp == NULL) // hidden option |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5093 | return gov_hidden_string; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5094 | if (stringval != NULL) |
| 5095 | { |
zeertzjq | 51f0bc3 | 2022-05-09 13:33:39 +0100 | [diff] [blame] | 5096 | if ((char_u **)varp == &p_pt) // 'pastetoggle' |
zeertzjq | cdc8393 | 2022-09-12 13:38:41 +0100 | [diff] [blame] | 5097 | *stringval = str2special_save(*(char_u **)(varp), FALSE, |
| 5098 | FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5099 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5100 | // never return the value of the crypt key |
zeertzjq | 51f0bc3 | 2022-05-09 13:33:39 +0100 | [diff] [blame] | 5101 | else if ((char_u **)varp == &curbuf->b_p_key |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 5102 | && **(char_u **)(varp) != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5103 | *stringval = vim_strsave((char_u *)"*****"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5104 | #endif |
zeertzjq | 51f0bc3 | 2022-05-09 13:33:39 +0100 | [diff] [blame] | 5105 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5106 | *stringval = vim_strsave(*(char_u **)(varp)); |
| 5107 | } |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5108 | return gov_string; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5111 | if (varp == NULL) // hidden option |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5112 | return (options[opt_idx].flags & P_NUM) |
| 5113 | ? gov_hidden_number : gov_hidden_bool; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5114 | if (options[opt_idx].flags & P_NUM) |
| 5115 | *numval = *(long *)varp; |
| 5116 | else |
| 5117 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5118 | // Special case: 'modified' is b_changed, but we also want to consider |
| 5119 | // it set when 'ff' or 'fenc' changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5120 | if ((int *)varp == &curbuf->b_changed) |
| 5121 | *numval = curbufIsChanged(); |
| 5122 | else |
Bram Moolenaar | 2acfbed | 2016-07-01 23:14:02 +0200 | [diff] [blame] | 5123 | *numval = (long) *(int *)varp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5124 | } |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5125 | return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5126 | } |
| 5127 | #endif |
| 5128 | |
Bram Moolenaar | 1028f4d | 2014-01-14 16:55:00 +0100 | [diff] [blame] | 5129 | #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5130 | /* |
| 5131 | * Returns the option attributes and its value. Unlike the above function it |
| 5132 | * will return either global value or local value of the option depending on |
| 5133 | * what was requested, but it will never return global value if it was |
| 5134 | * requested to return local one and vice versa. Neither it will return |
| 5135 | * buffer-local value if it was requested to return window-local one. |
| 5136 | * |
| 5137 | * Pretends that option is absent if it is not present in the requested scope |
| 5138 | * (i.e. has no global, window-local or buffer-local value depending on |
| 5139 | * opt_type). Uses |
| 5140 | * |
| 5141 | * Returned flags: |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 5142 | * 0 hidden or unknown option, also option that does not have requested |
| 5143 | * type (see SREQ_* in vim.h) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5144 | * see SOPT_* in vim.h for other flags |
| 5145 | * |
| 5146 | * Possible opt_type values: see SREQ_* in vim.h |
| 5147 | */ |
| 5148 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5149 | get_option_value_strict( |
| 5150 | char_u *name, |
| 5151 | long *numval, |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5152 | char_u **stringval, // NULL when only obtaining attributes |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5153 | int opt_type, |
| 5154 | void *from) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5155 | { |
| 5156 | int opt_idx; |
Bram Moolenaar | 6800186 | 2013-05-11 13:45:05 +0200 | [diff] [blame] | 5157 | char_u *varp = NULL; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5158 | struct vimoption *p; |
| 5159 | int r = 0; |
| 5160 | |
| 5161 | opt_idx = findoption(name); |
| 5162 | if (opt_idx < 0) |
| 5163 | return 0; |
| 5164 | |
| 5165 | p = &(options[opt_idx]); |
| 5166 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5167 | // Hidden option |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5168 | if (p->var == NULL) |
| 5169 | return 0; |
| 5170 | |
| 5171 | if (p->flags & P_BOOL) |
| 5172 | r |= SOPT_BOOL; |
| 5173 | else if (p->flags & P_NUM) |
| 5174 | r |= SOPT_NUM; |
| 5175 | else if (p->flags & P_STRING) |
| 5176 | r |= SOPT_STRING; |
| 5177 | |
| 5178 | if (p->indir == PV_NONE) |
| 5179 | { |
| 5180 | if (opt_type == SREQ_GLOBAL) |
| 5181 | r |= SOPT_GLOBAL; |
| 5182 | else |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5183 | return 0; // Did not request global-only option |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5184 | } |
| 5185 | else |
| 5186 | { |
| 5187 | if (p->indir & PV_BOTH) |
| 5188 | r |= SOPT_GLOBAL; |
| 5189 | else if (opt_type == SREQ_GLOBAL) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5190 | return 0; // Requested global option |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5191 | |
| 5192 | if (p->indir & PV_WIN) |
| 5193 | { |
| 5194 | if (opt_type == SREQ_BUF) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5195 | return 0; // Did not request window-local option |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5196 | else |
| 5197 | r |= SOPT_WIN; |
| 5198 | } |
| 5199 | else if (p->indir & PV_BUF) |
| 5200 | { |
| 5201 | if (opt_type == SREQ_WIN) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5202 | return 0; // Did not request buffer-local option |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5203 | else |
| 5204 | r |= SOPT_BUF; |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | if (stringval == NULL) |
| 5209 | return r; |
| 5210 | |
| 5211 | if (opt_type == SREQ_GLOBAL) |
| 5212 | varp = p->var; |
| 5213 | else |
| 5214 | { |
| 5215 | if (opt_type == SREQ_BUF) |
| 5216 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5217 | // Special case: 'modified' is b_changed, but we also want to |
| 5218 | // consider it set when 'ff' or 'fenc' changed. |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5219 | if (p->indir == PV_MOD) |
| 5220 | { |
Bram Moolenaar | defe642 | 2018-06-24 15:14:07 +0200 | [diff] [blame] | 5221 | *numval = bufIsChanged((buf_T *)from); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5222 | varp = NULL; |
| 5223 | } |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 5224 | # ifdef FEAT_CRYPT |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5225 | else if (p->indir == PV_KEY) |
| 5226 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5227 | // never return the value of the crypt key |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5228 | *stringval = NULL; |
| 5229 | varp = NULL; |
| 5230 | } |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 5231 | # endif |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5232 | else |
| 5233 | { |
Bram Moolenaar | defe642 | 2018-06-24 15:14:07 +0200 | [diff] [blame] | 5234 | buf_T *save_curbuf = curbuf; |
| 5235 | |
| 5236 | // only getting a pointer, no need to use aucmd_prepbuf() |
| 5237 | curbuf = (buf_T *)from; |
| 5238 | curwin->w_buffer = curbuf; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5239 | varp = get_varp(p); |
Bram Moolenaar | defe642 | 2018-06-24 15:14:07 +0200 | [diff] [blame] | 5240 | curbuf = save_curbuf; |
| 5241 | curwin->w_buffer = curbuf; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5242 | } |
| 5243 | } |
| 5244 | else if (opt_type == SREQ_WIN) |
| 5245 | { |
Bram Moolenaar | defe642 | 2018-06-24 15:14:07 +0200 | [diff] [blame] | 5246 | win_T *save_curwin = curwin; |
| 5247 | |
| 5248 | curwin = (win_T *)from; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5249 | curbuf = curwin->w_buffer; |
| 5250 | varp = get_varp(p); |
| 5251 | curwin = save_curwin; |
| 5252 | curbuf = curwin->w_buffer; |
| 5253 | } |
| 5254 | if (varp == p->var) |
| 5255 | return (r | SOPT_UNSET); |
| 5256 | } |
| 5257 | |
| 5258 | if (varp != NULL) |
| 5259 | { |
| 5260 | if (p->flags & P_STRING) |
| 5261 | *stringval = vim_strsave(*(char_u **)(varp)); |
| 5262 | else if (p->flags & P_NUM) |
| 5263 | *numval = *(long *) varp; |
| 5264 | else |
| 5265 | *numval = *(int *)varp; |
| 5266 | } |
| 5267 | |
| 5268 | return r; |
| 5269 | } |
Bram Moolenaar | 1028f4d | 2014-01-14 16:55:00 +0100 | [diff] [blame] | 5270 | |
| 5271 | /* |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 5272 | * Iterate over options. First argument is a pointer to a pointer to a |
| 5273 | * structure inside options[] array, second is option type like in the above |
| 5274 | * function. |
Bram Moolenaar | 1028f4d | 2014-01-14 16:55:00 +0100 | [diff] [blame] | 5275 | * |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 5276 | * If first argument points to NULL it is assumed that iteration just started |
Bram Moolenaar | 1028f4d | 2014-01-14 16:55:00 +0100 | [diff] [blame] | 5277 | * and caller needs the very first value. |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 5278 | * If first argument points to the end marker function returns NULL and sets |
Bram Moolenaar | 1028f4d | 2014-01-14 16:55:00 +0100 | [diff] [blame] | 5279 | * first argument to NULL. |
| 5280 | * |
| 5281 | * Returns full option name for current option on each call. |
| 5282 | */ |
| 5283 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5284 | option_iter_next(void **option, int opt_type) |
Bram Moolenaar | 1028f4d | 2014-01-14 16:55:00 +0100 | [diff] [blame] | 5285 | { |
| 5286 | struct vimoption *ret = NULL; |
| 5287 | do |
| 5288 | { |
| 5289 | if (*option == NULL) |
| 5290 | *option = (void *) options; |
| 5291 | else if (((struct vimoption *) (*option))->fullname == NULL) |
| 5292 | { |
| 5293 | *option = NULL; |
| 5294 | return NULL; |
| 5295 | } |
| 5296 | else |
| 5297 | *option = (void *) (((struct vimoption *) (*option)) + 1); |
| 5298 | |
| 5299 | ret = ((struct vimoption *) (*option)); |
| 5300 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5301 | // Hidden option |
Bram Moolenaar | 1028f4d | 2014-01-14 16:55:00 +0100 | [diff] [blame] | 5302 | if (ret->var == NULL) |
| 5303 | { |
| 5304 | ret = NULL; |
| 5305 | continue; |
| 5306 | } |
| 5307 | |
| 5308 | switch (opt_type) |
| 5309 | { |
| 5310 | case SREQ_GLOBAL: |
| 5311 | if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH)) |
| 5312 | ret = NULL; |
| 5313 | break; |
| 5314 | case SREQ_BUF: |
| 5315 | if (!(ret->indir & PV_BUF)) |
| 5316 | ret = NULL; |
| 5317 | break; |
| 5318 | case SREQ_WIN: |
| 5319 | if (!(ret->indir & PV_WIN)) |
| 5320 | ret = NULL; |
| 5321 | break; |
| 5322 | default: |
Bram Moolenaar | 95f0960 | 2016-11-10 20:01:45 +0100 | [diff] [blame] | 5323 | internal_error("option_iter_next()"); |
Bram Moolenaar | 1028f4d | 2014-01-14 16:55:00 +0100 | [diff] [blame] | 5324 | return NULL; |
| 5325 | } |
| 5326 | } |
| 5327 | while (ret == NULL); |
| 5328 | |
| 5329 | return (char_u *)ret->fullname; |
| 5330 | } |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 5331 | #endif |
| 5332 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5333 | /* |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 5334 | * Return the flags for the option at 'opt_idx'. |
| 5335 | */ |
| 5336 | long_u |
| 5337 | get_option_flags(int opt_idx) |
| 5338 | { |
| 5339 | return options[opt_idx].flags; |
| 5340 | } |
| 5341 | |
| 5342 | /* |
| 5343 | * Set a flag for the option at 'opt_idx'. |
| 5344 | */ |
| 5345 | void |
| 5346 | set_option_flag(int opt_idx, long_u flag) |
| 5347 | { |
| 5348 | options[opt_idx].flags |= flag; |
| 5349 | } |
| 5350 | |
| 5351 | /* |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 5352 | * Returns TRUE if the option at 'opt_idx' is a global option |
| 5353 | */ |
| 5354 | int |
| 5355 | is_global_option(int opt_idx) |
| 5356 | { |
| 5357 | return options[opt_idx].indir == PV_NONE; |
| 5358 | } |
| 5359 | |
| 5360 | /* |
| 5361 | * Returns TRUE if the option at 'opt_idx' is a global option which also has a |
| 5362 | * local value. |
| 5363 | */ |
| 5364 | int |
| 5365 | is_global_local_option(int opt_idx) |
| 5366 | { |
| 5367 | return options[opt_idx].indir & PV_BOTH; |
| 5368 | } |
| 5369 | |
| 5370 | /* |
| 5371 | * Returns TRUE if the option at 'opt_idx' is a window-local option |
| 5372 | */ |
| 5373 | int |
| 5374 | is_window_local_option(int opt_idx) |
| 5375 | { |
| 5376 | return options[opt_idx].var == VAR_WIN; |
| 5377 | } |
| 5378 | |
| 5379 | /* |
| 5380 | * Returns TRUE if the option at 'opt_idx' is a hidden option |
| 5381 | */ |
| 5382 | int |
| 5383 | is_hidden_option(int opt_idx) |
| 5384 | { |
| 5385 | return options[opt_idx].var == NULL; |
| 5386 | } |
| 5387 | |
| 5388 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 5389 | /* |
| 5390 | * Returns TRUE if the option at 'opt_idx' is a crypt key option |
| 5391 | */ |
| 5392 | int |
| 5393 | is_crypt_key_option(int opt_idx) |
| 5394 | { |
| 5395 | return options[opt_idx].indir == PV_KEY; |
| 5396 | } |
| 5397 | #endif |
| 5398 | |
| 5399 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5400 | * Set the value of option "name". |
| 5401 | * Use "string" for string options, use "number" for other options. |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 5402 | * |
Bram Moolenaar | 31e5c60 | 2022-04-15 13:53:33 +0100 | [diff] [blame] | 5403 | * Returns NULL on success or an untranslated error message on error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5404 | */ |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5405 | char * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5406 | set_option_value( |
| 5407 | char_u *name, |
| 5408 | long number, |
| 5409 | char_u *string, |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5410 | int opt_flags) // OPT_LOCAL or 0 (both) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5411 | { |
| 5412 | int opt_idx; |
| 5413 | char_u *varp; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5414 | long_u flags; |
Christian Brabandt | b39b240 | 2023-11-29 11:34:05 +0100 | [diff] [blame] | 5415 | static char errbuf[ERR_BUFLEN]; |
| 5416 | int errbuflen = ERR_BUFLEN; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5417 | |
| 5418 | opt_idx = findoption(name); |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 5419 | if (opt_idx < 0) |
Bram Moolenaar | e353c40 | 2017-02-04 19:49:16 +0100 | [diff] [blame] | 5420 | { |
| 5421 | int key; |
| 5422 | |
| 5423 | if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_' |
Bram Moolenaar | 9cf4b50 | 2018-07-23 04:12:03 +0200 | [diff] [blame] | 5424 | && (key = find_key_option(name, FALSE)) != 0) |
Bram Moolenaar | e353c40 | 2017-02-04 19:49:16 +0100 | [diff] [blame] | 5425 | { |
| 5426 | char_u key_name[2]; |
| 5427 | |
| 5428 | if (key < 0) |
| 5429 | { |
| 5430 | key_name[0] = KEY2TERMCAP0(key); |
| 5431 | key_name[1] = KEY2TERMCAP1(key); |
| 5432 | } |
| 5433 | else |
| 5434 | { |
| 5435 | key_name[0] = KS_KEY; |
| 5436 | key_name[1] = (key & 0xff); |
| 5437 | } |
| 5438 | add_termcode(key_name, string, FALSE); |
| 5439 | if (full_screen) |
| 5440 | ttest(FALSE); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 5441 | redraw_all_later(UPD_CLEAR); |
Bram Moolenaar | e353c40 | 2017-02-04 19:49:16 +0100 | [diff] [blame] | 5442 | return NULL; |
| 5443 | } |
| 5444 | |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 5445 | semsg(_(e_unknown_option_str_2), name); |
Bram Moolenaar | e353c40 | 2017-02-04 19:49:16 +0100 | [diff] [blame] | 5446 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5447 | else |
| 5448 | { |
| 5449 | flags = options[opt_idx].flags; |
| 5450 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5451 | // Disallow changing some options in the sandbox |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5452 | if (sandbox > 0 && (flags & P_SECURE)) |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 5453 | { |
Bram Moolenaar | d8e4447 | 2021-07-21 22:20:33 +0200 | [diff] [blame] | 5454 | emsg(_(e_not_allowed_in_sandbox)); |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 5455 | return NULL; |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 5456 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5457 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 5458 | if (flags & P_STRING) |
Christian Brabandt | b39b240 | 2023-11-29 11:34:05 +0100 | [diff] [blame] | 5459 | return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen); |
=?UTF-8?q?Dundar=20G=C3=B6c?= | f26c161 | 2022-04-07 13:26:34 +0100 | [diff] [blame] | 5460 | |
| 5461 | varp = get_varp_scope(&(options[opt_idx]), opt_flags); |
| 5462 | if (varp != NULL) // hidden option is not changed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5463 | { |
=?UTF-8?q?Dundar=20G=C3=B6c?= | f26c161 | 2022-04-07 13:26:34 +0100 | [diff] [blame] | 5464 | if (number == 0 && string != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5465 | { |
=?UTF-8?q?Dundar=20G=C3=B6c?= | f26c161 | 2022-04-07 13:26:34 +0100 | [diff] [blame] | 5466 | int idx; |
| 5467 | |
| 5468 | // Either we are given a string or we are setting option |
| 5469 | // to zero. |
| 5470 | for (idx = 0; string[idx] == '0'; ++idx) |
| 5471 | ; |
| 5472 | if (string[idx] != NUL || idx == 0) |
Bram Moolenaar | 96bb621 | 2007-06-19 18:52:53 +0000 | [diff] [blame] | 5473 | { |
=?UTF-8?q?Dundar=20G=C3=B6c?= | f26c161 | 2022-04-07 13:26:34 +0100 | [diff] [blame] | 5474 | // There's another character after zeros or the string |
| 5475 | // is empty. In both cases, we are trying to set a |
| 5476 | // num option using a string. |
| 5477 | semsg(_(e_number_required_after_str_equal_str), |
| 5478 | name, string); |
| 5479 | return NULL; // do nothing as we hit an error |
Bram Moolenaar | 96bb621 | 2007-06-19 18:52:53 +0000 | [diff] [blame] | 5480 | |
Bram Moolenaar | 96bb621 | 2007-06-19 18:52:53 +0000 | [diff] [blame] | 5481 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5482 | } |
=?UTF-8?q?Dundar=20G=C3=B6c?= | f26c161 | 2022-04-07 13:26:34 +0100 | [diff] [blame] | 5483 | if (flags & P_NUM) |
Yegappan Lakshmanan | 32ff96e | 2023-02-13 16:10:04 +0000 | [diff] [blame] | 5484 | { |
=?UTF-8?q?Dundar=20G=C3=B6c?= | f26c161 | 2022-04-07 13:26:34 +0100 | [diff] [blame] | 5485 | return set_num_option(opt_idx, varp, number, |
Yegappan Lakshmanan | 32ff96e | 2023-02-13 16:10:04 +0000 | [diff] [blame] | 5486 | errbuf, sizeof(errbuf), opt_flags); |
| 5487 | } |
=?UTF-8?q?Dundar=20G=C3=B6c?= | f26c161 | 2022-04-07 13:26:34 +0100 | [diff] [blame] | 5488 | else |
| 5489 | return set_bool_option(opt_idx, varp, (int)number, opt_flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5490 | } |
=?UTF-8?q?Dundar=20G=C3=B6c?= | f26c161 | 2022-04-07 13:26:34 +0100 | [diff] [blame] | 5491 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5492 | } |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 5493 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | /* |
Bram Moolenaar | 31e5c60 | 2022-04-15 13:53:33 +0100 | [diff] [blame] | 5497 | * Call set_option_value() and when an error is returned report it. |
| 5498 | */ |
| 5499 | void |
| 5500 | set_option_value_give_err( |
| 5501 | char_u *name, |
| 5502 | long number, |
| 5503 | char_u *string, |
| 5504 | int opt_flags) // OPT_LOCAL or 0 (both) |
| 5505 | { |
| 5506 | char *errmsg = set_option_value(name, number, string, opt_flags); |
| 5507 | |
| 5508 | if (errmsg != NULL) |
| 5509 | emsg(_(errmsg)); |
| 5510 | } |
| 5511 | |
| 5512 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5513 | * Get the terminal code for a terminal option. |
| 5514 | * Returns NULL when not found. |
| 5515 | */ |
| 5516 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5517 | get_term_code(char_u *tname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5518 | { |
| 5519 | int opt_idx; |
| 5520 | char_u *varp; |
| 5521 | |
| 5522 | if (tname[0] != 't' || tname[1] != '_' || |
| 5523 | tname[2] == NUL || tname[3] == NUL) |
| 5524 | return NULL; |
| 5525 | if ((opt_idx = findoption(tname)) >= 0) |
| 5526 | { |
| 5527 | varp = get_varp(&(options[opt_idx])); |
| 5528 | if (varp != NULL) |
| 5529 | varp = *(char_u **)(varp); |
| 5530 | return varp; |
| 5531 | } |
| 5532 | return find_termcode(tname + 2); |
| 5533 | } |
| 5534 | |
| 5535 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5536 | get_highlight_default(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5537 | { |
| 5538 | int i; |
| 5539 | |
| 5540 | i = findoption((char_u *)"hl"); |
| 5541 | if (i >= 0) |
| 5542 | return options[i].def_val[VI_DEFAULT]; |
| 5543 | return (char_u *)NULL; |
| 5544 | } |
| 5545 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5546 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5547 | get_encoding_default(void) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5548 | { |
| 5549 | int i; |
| 5550 | |
| 5551 | i = findoption((char_u *)"enc"); |
| 5552 | if (i >= 0) |
| 5553 | return options[i].def_val[VI_DEFAULT]; |
| 5554 | return (char_u *)NULL; |
| 5555 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5556 | |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 5557 | #if defined(FEAT_QUICKFIX) || defined(PROTO) |
Bram Moolenaar | 4791fcd | 2022-02-23 12:06:00 +0000 | [diff] [blame] | 5558 | int |
| 5559 | is_option_allocated(char *name) |
| 5560 | { |
| 5561 | int idx = findoption((char_u *)name); |
| 5562 | |
| 5563 | return idx >= 0 && (options[idx].flags & P_ALLOCED); |
| 5564 | } |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 5565 | #endif |
Bram Moolenaar | 4791fcd | 2022-02-23 12:06:00 +0000 | [diff] [blame] | 5566 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5567 | /* |
| 5568 | * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number. |
Bram Moolenaar | 9cf4b50 | 2018-07-23 04:12:03 +0200 | [diff] [blame] | 5569 | * When "has_lt" is true there is a '<' before "*arg_arg". |
| 5570 | * Returns 0 when the key is not recognized. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5571 | */ |
| 5572 | static int |
Bram Moolenaar | 9cf4b50 | 2018-07-23 04:12:03 +0200 | [diff] [blame] | 5573 | find_key_option(char_u *arg_arg, int has_lt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5574 | { |
Bram Moolenaar | 9cf4b50 | 2018-07-23 04:12:03 +0200 | [diff] [blame] | 5575 | int key = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5576 | int modifiers; |
Bram Moolenaar | 9cf4b50 | 2018-07-23 04:12:03 +0200 | [diff] [blame] | 5577 | char_u *arg = arg_arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5578 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 5579 | // Don't use get_special_key_code() for t_xx, we don't want it to call |
| 5580 | // add_termcap_entry(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5581 | if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3]) |
| 5582 | key = TERMCAP2KEY(arg[2], arg[3]); |
Bram Moolenaar | 9cf4b50 | 2018-07-23 04:12:03 +0200 | [diff] [blame] | 5583 | else if (has_lt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5584 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5585 | --arg; // put arg at the '<' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5586 | modifiers = 0; |
Bram Moolenaar | ebe9d34 | 2020-05-30 21:52:54 +0200 | [diff] [blame] | 5587 | key = find_special_key(&arg, &modifiers, |
| 5588 | FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5589 | if (modifiers) // can't handle modifiers here |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5590 | key = 0; |
| 5591 | } |
| 5592 | return key; |
| 5593 | } |
| 5594 | |
| 5595 | /* |
| 5596 | * if 'all' == 0: show changed options |
| 5597 | * if 'all' == 1: show all normal options |
| 5598 | * if 'all' == 2: show all terminal options |
| 5599 | */ |
| 5600 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5601 | showoptions( |
| 5602 | int all, |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5603 | int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5604 | { |
| 5605 | struct vimoption *p; |
| 5606 | int col; |
| 5607 | int isterm; |
| 5608 | char_u *varp; |
| 5609 | struct vimoption **items; |
| 5610 | int item_count; |
| 5611 | int run; |
| 5612 | int row, rows; |
| 5613 | int cols; |
| 5614 | int i; |
| 5615 | int len; |
| 5616 | |
| 5617 | #define INC 20 |
| 5618 | #define GAP 3 |
| 5619 | |
Bram Moolenaar | 6b915c0 | 2020-01-18 15:53:19 +0100 | [diff] [blame] | 5620 | items = ALLOC_MULT(struct vimoption *, OPTION_COUNT); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5621 | if (items == NULL) |
| 5622 | return; |
| 5623 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5624 | // Highlight title |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5625 | if (all == 2) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5626 | msg_puts_title(_("\n--- Terminal codes ---")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5627 | else if (opt_flags & OPT_GLOBAL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5628 | msg_puts_title(_("\n--- Global option values ---")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5629 | else if (opt_flags & OPT_LOCAL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5630 | msg_puts_title(_("\n--- Local option values ---")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5631 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5632 | msg_puts_title(_("\n--- Options ---")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5633 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 5634 | // Do the loop two times: |
| 5635 | // 1. display the short items |
| 5636 | // 2. display the long items (only strings and numbers) |
| 5637 | // When "opt_flags" has OPT_ONECOLUMN do everything in run 2. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5638 | for (run = 1; run <= 2 && !got_int; ++run) |
| 5639 | { |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 5640 | // collect the items in items[] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5641 | item_count = 0; |
| 5642 | for (p = &options[0]; p->fullname != NULL; p++) |
| 5643 | { |
Bram Moolenaar | f86db78 | 2018-10-25 13:31:37 +0200 | [diff] [blame] | 5644 | // apply :filter /pat/ |
Bram Moolenaar | 6b915c0 | 2020-01-18 15:53:19 +0100 | [diff] [blame] | 5645 | if (message_filtered((char_u *)p->fullname)) |
Bram Moolenaar | f86db78 | 2018-10-25 13:31:37 +0200 | [diff] [blame] | 5646 | continue; |
| 5647 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5648 | varp = NULL; |
| 5649 | isterm = istermoption(p); |
Bram Moolenaar | 6b915c0 | 2020-01-18 15:53:19 +0100 | [diff] [blame] | 5650 | if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5651 | { |
| 5652 | if (p->indir != PV_NONE && !isterm) |
| 5653 | varp = get_varp_scope(p, opt_flags); |
| 5654 | } |
| 5655 | else |
| 5656 | varp = get_varp(p); |
| 5657 | if (varp != NULL |
| 5658 | && ((all == 2 && isterm) |
| 5659 | || (all == 1 && !isterm) |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 5660 | || (all == 0 && !optval_default(p, varp, p_cp)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5661 | { |
Bram Moolenaar | 6b915c0 | 2020-01-18 15:53:19 +0100 | [diff] [blame] | 5662 | if (opt_flags & OPT_ONECOLUMN) |
| 5663 | len = Columns; |
| 5664 | else if (p->flags & P_BOOL) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5665 | len = 1; // a toggle option fits always |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5666 | else |
| 5667 | { |
| 5668 | option_value2string(p, opt_flags); |
| 5669 | len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1; |
| 5670 | } |
| 5671 | if ((len <= INC - GAP && run == 1) || |
| 5672 | (len > INC - GAP && run == 2)) |
| 5673 | items[item_count++] = p; |
| 5674 | } |
| 5675 | } |
| 5676 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 5677 | // display the items |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5678 | if (run == 1) |
| 5679 | { |
| 5680 | cols = (Columns + GAP - 3) / INC; |
| 5681 | if (cols == 0) |
| 5682 | cols = 1; |
| 5683 | rows = (item_count + cols - 1) / cols; |
| 5684 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5685 | else // run == 2 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5686 | rows = item_count; |
| 5687 | for (row = 0; row < rows && !got_int; ++row) |
| 5688 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5689 | msg_putchar('\n'); // go to next line |
| 5690 | if (got_int) // 'q' typed in more |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5691 | break; |
| 5692 | col = 0; |
| 5693 | for (i = row; i < item_count; i += rows) |
| 5694 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5695 | msg_col = col; // make columns |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5696 | showoneopt(items[i], opt_flags); |
| 5697 | col += INC; |
| 5698 | } |
| 5699 | out_flush(); |
| 5700 | ui_breakcheck(); |
| 5701 | } |
| 5702 | } |
| 5703 | vim_free(items); |
| 5704 | } |
| 5705 | |
| 5706 | /* |
| 5707 | * Return TRUE if option "p" has its default value. |
| 5708 | */ |
| 5709 | static int |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 5710 | optval_default(struct vimoption *p, char_u *varp, int compatible) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5711 | { |
| 5712 | int dvi; |
| 5713 | |
| 5714 | if (varp == NULL) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5715 | return TRUE; // hidden option is always at default |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 5716 | dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5717 | if (p->flags & P_NUM) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 5718 | return (*(long *)varp == (long)(long_i)p->def_val[dvi]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5719 | if (p->flags & P_BOOL) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5720 | // the cast to long is required for Manx C, long_i is |
| 5721 | // needed for MSVC |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 5722 | return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5723 | // P_STRING |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5724 | return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0); |
| 5725 | } |
| 5726 | |
| 5727 | /* |
| 5728 | * showoneopt: show the value of one option |
| 5729 | * must not be called with a hidden option! |
| 5730 | */ |
| 5731 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5732 | showoneopt( |
| 5733 | struct vimoption *p, |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5734 | int opt_flags) // OPT_LOCAL or OPT_GLOBAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5735 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 5736 | char_u *varp; |
| 5737 | int save_silent = silent_mode; |
| 5738 | |
| 5739 | silent_mode = FALSE; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5740 | info_message = TRUE; // use mch_msg(), not mch_errmsg() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5741 | |
| 5742 | varp = get_varp_scope(p, opt_flags); |
| 5743 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5744 | // for 'modified' we also need to check if 'ff' or 'fenc' changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5745 | if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed |
| 5746 | ? !curbufIsChanged() : !*(int *)varp)) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5747 | msg_puts("no"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5748 | else if ((p->flags & P_BOOL) && *(int *)varp < 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5749 | msg_puts("--"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5750 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5751 | msg_puts(" "); |
| 5752 | msg_puts(p->fullname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5753 | if (!(p->flags & P_BOOL)) |
| 5754 | { |
| 5755 | msg_putchar('='); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5756 | // put value string in NameBuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5757 | option_value2string(p, opt_flags); |
| 5758 | msg_outtrans(NameBuff); |
| 5759 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 5760 | |
| 5761 | silent_mode = save_silent; |
| 5762 | info_message = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | /* |
| 5766 | * Write modified options as ":set" commands to a file. |
| 5767 | * |
| 5768 | * There are three values for "opt_flags": |
| 5769 | * OPT_GLOBAL: Write global option values and fresh values of |
| 5770 | * buffer-local options (used for start of a session |
| 5771 | * file). |
| 5772 | * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for |
| 5773 | * curwin (used for a vimrc file). |
| 5774 | * OPT_LOCAL: Write buffer-local option values for curbuf, fresh |
| 5775 | * and local values for window-local options of |
| 5776 | * curwin. Local values are also written when at the |
| 5777 | * default value, because a modeline or autocommand |
| 5778 | * may have set them when doing ":edit file" and the |
| 5779 | * user has set them back at the default or fresh |
| 5780 | * value. |
| 5781 | * When "local_only" is TRUE, don't write fresh |
| 5782 | * values, only local values (for ":mkview"). |
| 5783 | * (fresh value = value used for a new buffer or window for a local option). |
| 5784 | * |
| 5785 | * Return FAIL on error, OK otherwise. |
| 5786 | */ |
| 5787 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5788 | makeset(FILE *fd, int opt_flags, int local_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5789 | { |
| 5790 | struct vimoption *p; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5791 | char_u *varp; // currently used value |
| 5792 | char_u *varp_fresh; // local value |
| 5793 | char_u *varp_local = NULL; // fresh value |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5794 | char *cmd; |
| 5795 | int round; |
Bram Moolenaar | 7fd1602 | 2007-09-06 14:35:35 +0000 | [diff] [blame] | 5796 | int pri; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5797 | |
| 5798 | /* |
| 5799 | * The options that don't have a default (terminal name, columns, lines) |
| 5800 | * are never written. Terminal options are also not written. |
Bram Moolenaar | 7fd1602 | 2007-09-06 14:35:35 +0000 | [diff] [blame] | 5801 | * Do the loop over "options[]" twice: once for options with the |
| 5802 | * P_PRI_MKRC flag and once without. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5803 | */ |
Bram Moolenaar | 7fd1602 | 2007-09-06 14:35:35 +0000 | [diff] [blame] | 5804 | for (pri = 1; pri >= 0; --pri) |
| 5805 | { |
| 5806 | for (p = &options[0]; !istermoption(p); p++) |
| 5807 | if (!(p->flags & P_NO_MKRC) |
| 5808 | && !istermoption(p) |
| 5809 | && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5810 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5811 | // skip global option when only doing locals |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5812 | if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL)) |
| 5813 | continue; |
| 5814 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5815 | // Do not store options like 'bufhidden' and 'syntax' in a vimrc |
| 5816 | // file, they are always buffer-specific. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5817 | if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB)) |
| 5818 | continue; |
| 5819 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5820 | // Global values are only written when not at the default value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5821 | varp = get_varp_scope(p, opt_flags); |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 5822 | if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5823 | continue; |
| 5824 | |
Bram Moolenaar | d23b714 | 2021-04-17 21:04:34 +0200 | [diff] [blame] | 5825 | if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp |
| 5826 | || p->var == (char_u *)&p_pp)) |
Bram Moolenaar | 635bd60 | 2021-04-16 19:58:22 +0200 | [diff] [blame] | 5827 | continue; |
| 5828 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5829 | round = 2; |
| 5830 | if (p->indir != PV_NONE) |
| 5831 | { |
| 5832 | if (p->var == VAR_WIN) |
| 5833 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5834 | // skip window-local option when only doing globals |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5835 | if (!(opt_flags & OPT_LOCAL)) |
| 5836 | continue; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5837 | // When fresh value of window-local option is not at the |
| 5838 | // default, need to write it too. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5839 | if (!(opt_flags & OPT_GLOBAL) && !local_only) |
| 5840 | { |
| 5841 | varp_fresh = get_varp_scope(p, OPT_GLOBAL); |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 5842 | if (!optval_default(p, varp_fresh, p_cp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5843 | { |
| 5844 | round = 1; |
| 5845 | varp_local = varp; |
| 5846 | varp = varp_fresh; |
| 5847 | } |
| 5848 | } |
| 5849 | } |
| 5850 | } |
| 5851 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5852 | // Round 1: fresh value for window-local options. |
| 5853 | // Round 2: other values |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5854 | for ( ; round <= 2; varp = varp_local, ++round) |
| 5855 | { |
| 5856 | if (round == 1 || (opt_flags & OPT_GLOBAL)) |
| 5857 | cmd = "set"; |
| 5858 | else |
| 5859 | cmd = "setlocal"; |
| 5860 | |
| 5861 | if (p->flags & P_BOOL) |
| 5862 | { |
| 5863 | if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL) |
| 5864 | return FAIL; |
| 5865 | } |
| 5866 | else if (p->flags & P_NUM) |
| 5867 | { |
| 5868 | if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL) |
| 5869 | return FAIL; |
| 5870 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5871 | else // P_STRING |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5872 | { |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 5873 | int do_endif = FALSE; |
| 5874 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5875 | // Don't set 'syntax' and 'filetype' again if the value is |
| 5876 | // already right, avoids reloading the syntax file. |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 5877 | if ( |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 5878 | #if defined(FEAT_SYN_HL) |
| 5879 | p->indir == PV_SYN || |
| 5880 | #endif |
| 5881 | p->indir == PV_FT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5882 | { |
| 5883 | if (fprintf(fd, "if &%s != '%s'", p->fullname, |
| 5884 | *(char_u **)(varp)) < 0 |
| 5885 | || put_eol(fd) < 0) |
| 5886 | return FAIL; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 5887 | do_endif = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5888 | } |
| 5889 | if (put_setstring(fd, cmd, p->fullname, (char_u **)varp, |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5890 | p->flags) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5891 | return FAIL; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 5892 | if (do_endif) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5893 | { |
| 5894 | if (put_line(fd, "endif") == FAIL) |
| 5895 | return FAIL; |
| 5896 | } |
| 5897 | } |
| 5898 | } |
| 5899 | } |
Bram Moolenaar | 7fd1602 | 2007-09-06 14:35:35 +0000 | [diff] [blame] | 5900 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5901 | return OK; |
| 5902 | } |
| 5903 | |
| 5904 | #if defined(FEAT_FOLDING) || defined(PROTO) |
| 5905 | /* |
| 5906 | * Generate set commands for the local fold options only. Used when |
| 5907 | * 'sessionoptions' or 'viewoptions' contains "folds" but not "options". |
| 5908 | */ |
| 5909 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5910 | makefoldset(FILE *fd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5911 | { |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5912 | if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5913 | # ifdef FEAT_EVAL |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5914 | || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5915 | == FAIL |
| 5916 | # endif |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5917 | || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5918 | == FAIL |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5919 | || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5920 | == FAIL |
| 5921 | || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL |
| 5922 | || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL |
| 5923 | || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL |
| 5924 | || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL |
| 5925 | ) |
| 5926 | return FAIL; |
| 5927 | |
| 5928 | return OK; |
| 5929 | } |
| 5930 | #endif |
| 5931 | |
| 5932 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5933 | put_setstring( |
| 5934 | FILE *fd, |
| 5935 | char *cmd, |
| 5936 | char *name, |
| 5937 | char_u **valuep, |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5938 | long_u flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5939 | { |
| 5940 | char_u *s; |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5941 | char_u *buf = NULL; |
| 5942 | char_u *part = NULL; |
| 5943 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5944 | |
| 5945 | if (fprintf(fd, "%s %s=", cmd, name) < 0) |
| 5946 | return FAIL; |
| 5947 | if (*valuep != NULL) |
| 5948 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 5949 | // Output 'pastetoggle' as key names. For other |
| 5950 | // options some characters have to be escaped with |
| 5951 | // CTRL-V or backslash |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5952 | if (valuep == &p_pt) |
| 5953 | { |
| 5954 | s = *valuep; |
| 5955 | while (*s != NUL) |
zeertzjq | cdc8393 | 2022-09-12 13:38:41 +0100 | [diff] [blame] | 5956 | if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5957 | return FAIL; |
| 5958 | } |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5959 | // expand the option value, replace $HOME by ~ |
| 5960 | else if ((flags & P_EXPAND) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5961 | { |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5962 | int size = (int)STRLEN(*valuep) + 1; |
| 5963 | |
| 5964 | // replace home directory in the whole option value into "buf" |
| 5965 | buf = alloc(size); |
Bram Moolenaar | f844147 | 2011-04-28 17:24:58 +0200 | [diff] [blame] | 5966 | if (buf == NULL) |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5967 | goto fail; |
| 5968 | home_replace(NULL, *valuep, buf, size, FALSE); |
| 5969 | |
| 5970 | // If the option value is longer than MAXPATHL, we need to append |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 5971 | // each comma separated part of the option separately, so that it |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 5972 | // can be expanded when read back. |
| 5973 | if (size >= MAXPATHL && (flags & P_COMMA) != 0 |
| 5974 | && vim_strchr(*valuep, ',') != NULL) |
| 5975 | { |
| 5976 | part = alloc(size); |
| 5977 | if (part == NULL) |
| 5978 | goto fail; |
| 5979 | |
| 5980 | // write line break to clear the option, e.g. ':set rtp=' |
| 5981 | if (put_eol(fd) == FAIL) |
| 5982 | goto fail; |
| 5983 | |
| 5984 | p = buf; |
| 5985 | while (*p != NUL) |
| 5986 | { |
| 5987 | // for each comma separated option part, append value to |
| 5988 | // the option, :set rtp+=value |
| 5989 | if (fprintf(fd, "%s %s+=", cmd, name) < 0) |
| 5990 | goto fail; |
| 5991 | (void)copy_option_part(&p, part, size, ","); |
| 5992 | if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL) |
| 5993 | goto fail; |
| 5994 | } |
| 5995 | vim_free(buf); |
| 5996 | vim_free(part); |
| 5997 | return OK; |
| 5998 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5999 | if (put_escstr(fd, buf, 2) == FAIL) |
Bram Moolenaar | f844147 | 2011-04-28 17:24:58 +0200 | [diff] [blame] | 6000 | { |
| 6001 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6002 | return FAIL; |
Bram Moolenaar | f844147 | 2011-04-28 17:24:58 +0200 | [diff] [blame] | 6003 | } |
| 6004 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6005 | } |
| 6006 | else if (put_escstr(fd, *valuep, 2) == FAIL) |
| 6007 | return FAIL; |
| 6008 | } |
| 6009 | if (put_eol(fd) < 0) |
| 6010 | return FAIL; |
| 6011 | return OK; |
Bram Moolenaar | ed18f2c | 2019-01-24 20:30:52 +0100 | [diff] [blame] | 6012 | fail: |
| 6013 | vim_free(buf); |
| 6014 | vim_free(part); |
| 6015 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
| 6018 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6019 | put_setnum( |
| 6020 | FILE *fd, |
| 6021 | char *cmd, |
| 6022 | char *name, |
| 6023 | long *valuep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6024 | { |
| 6025 | long wc; |
| 6026 | |
| 6027 | if (fprintf(fd, "%s %s=", cmd, name) < 0) |
| 6028 | return FAIL; |
| 6029 | if (wc_use_keyname((char_u *)valuep, &wc)) |
| 6030 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6031 | // print 'wildchar' and 'wildcharm' as a key name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6032 | if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0) |
| 6033 | return FAIL; |
| 6034 | } |
| 6035 | else if (fprintf(fd, "%ld", *valuep) < 0) |
| 6036 | return FAIL; |
| 6037 | if (put_eol(fd) < 0) |
| 6038 | return FAIL; |
| 6039 | return OK; |
| 6040 | } |
| 6041 | |
| 6042 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6043 | put_setbool( |
| 6044 | FILE *fd, |
| 6045 | char *cmd, |
| 6046 | char *name, |
| 6047 | int value) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6048 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6049 | if (value < 0) // global/local option using global value |
Bram Moolenaar | 893de92 | 2007-10-02 18:40:57 +0000 | [diff] [blame] | 6050 | return OK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6051 | if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0 |
| 6052 | || put_eol(fd) < 0) |
| 6053 | return FAIL; |
| 6054 | return OK; |
| 6055 | } |
| 6056 | |
| 6057 | /* |
| 6058 | * Clear all the terminal options. |
| 6059 | * If the option has been allocated, free the memory. |
| 6060 | * Terminal options are never hidden or indirect. |
| 6061 | */ |
| 6062 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6063 | clear_termoptions(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6064 | { |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 6065 | // Reset a few things before clearing the old options. This may cause |
| 6066 | // outputting a few things that the terminal doesn't understand, but the |
| 6067 | // screen will be cleared later, so this is OK. |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 6068 | mch_setmouse(FALSE); // switch mouse off |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6069 | mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6070 | #if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6071 | // When starting the GUI close the display opened for the clipboard. |
| 6072 | // After restoring the title, because that will need the display. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6073 | if (gui.starting) |
| 6074 | clear_xterm_clip(); |
| 6075 | #endif |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6076 | stoptermcap(); // stop termcap mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6077 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 6078 | free_termoptions(); |
| 6079 | } |
| 6080 | |
| 6081 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6082 | free_termoptions(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 6083 | { |
| 6084 | struct vimoption *p; |
| 6085 | |
Bram Moolenaar | 35bc7d6 | 2018-10-02 14:45:10 +0200 | [diff] [blame] | 6086 | for (p = options; p->fullname != NULL; p++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6087 | if (istermoption(p)) |
| 6088 | { |
| 6089 | if (p->flags & P_ALLOCED) |
| 6090 | free_string_option(*(char_u **)(p->var)); |
| 6091 | if (p->flags & P_DEF_ALLOCED) |
| 6092 | free_string_option(p->def_val[VI_DEFAULT]); |
| 6093 | *(char_u **)(p->var) = empty_option; |
| 6094 | p->def_val[VI_DEFAULT] = empty_option; |
| 6095 | p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED); |
Bram Moolenaar | 35bc7d6 | 2018-10-02 14:45:10 +0200 | [diff] [blame] | 6096 | #ifdef FEAT_EVAL |
| 6097 | // remember where the option was cleared |
| 6098 | set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx); |
| 6099 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6100 | } |
| 6101 | clear_termcodes(); |
| 6102 | } |
| 6103 | |
| 6104 | /* |
Bram Moolenaar | 363cb67 | 2009-07-22 12:28:17 +0000 | [diff] [blame] | 6105 | * Free the string for one term option, if it was allocated. |
| 6106 | * Set the string to empty_option and clear allocated flag. |
| 6107 | * "var" points to the option value. |
| 6108 | */ |
| 6109 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6110 | free_one_termoption(char_u *var) |
Bram Moolenaar | 363cb67 | 2009-07-22 12:28:17 +0000 | [diff] [blame] | 6111 | { |
| 6112 | struct vimoption *p; |
| 6113 | |
| 6114 | for (p = &options[0]; p->fullname != NULL; p++) |
| 6115 | if (p->var == var) |
| 6116 | { |
| 6117 | if (p->flags & P_ALLOCED) |
| 6118 | free_string_option(*(char_u **)(p->var)); |
| 6119 | *(char_u **)(p->var) = empty_option; |
| 6120 | p->flags &= ~P_ALLOCED; |
| 6121 | break; |
| 6122 | } |
| 6123 | } |
| 6124 | |
| 6125 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6126 | * Set the terminal option defaults to the current value. |
| 6127 | * Used after setting the terminal name. |
| 6128 | */ |
| 6129 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6130 | set_term_defaults(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6131 | { |
| 6132 | struct vimoption *p; |
| 6133 | |
| 6134 | for (p = &options[0]; p->fullname != NULL; p++) |
| 6135 | { |
| 6136 | if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var)) |
| 6137 | { |
| 6138 | if (p->flags & P_DEF_ALLOCED) |
| 6139 | { |
| 6140 | free_string_option(p->def_val[VI_DEFAULT]); |
| 6141 | p->flags &= ~P_DEF_ALLOCED; |
| 6142 | } |
| 6143 | p->def_val[VI_DEFAULT] = *(char_u **)(p->var); |
| 6144 | if (p->flags & P_ALLOCED) |
| 6145 | { |
| 6146 | p->flags |= P_DEF_ALLOCED; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6147 | p->flags &= ~P_ALLOCED; // don't free the value now |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6148 | } |
| 6149 | } |
| 6150 | } |
| 6151 | } |
| 6152 | |
| 6153 | /* |
| 6154 | * return TRUE if 'p' starts with 't_' |
| 6155 | */ |
| 6156 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6157 | istermoption(struct vimoption *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6158 | { |
| 6159 | return (p->fullname[0] == 't' && p->fullname[1] == '_'); |
| 6160 | } |
| 6161 | |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 6162 | /* |
| 6163 | * Returns TRUE if the option at 'opt_idx' starts with 't_' |
| 6164 | */ |
| 6165 | int |
| 6166 | istermoption_idx(int opt_idx) |
| 6167 | { |
| 6168 | return istermoption(&options[opt_idx]); |
| 6169 | } |
| 6170 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 6171 | #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6172 | /* |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6173 | * Unset local option value, similar to ":set opt<". |
| 6174 | */ |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6175 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6176 | unset_global_local_option(char_u *name, void *from) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6177 | { |
| 6178 | struct vimoption *p; |
| 6179 | int opt_idx; |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6180 | buf_T *buf = (buf_T *)from; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6181 | |
| 6182 | opt_idx = findoption(name); |
Bram Moolenaar | bd8539a | 2015-08-11 18:53:03 +0200 | [diff] [blame] | 6183 | if (opt_idx < 0) |
| 6184 | return; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6185 | p = &(options[opt_idx]); |
| 6186 | |
| 6187 | switch ((int)p->indir) |
| 6188 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6189 | // global option with local value: use local value if it's been set |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6190 | case PV_EP: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6191 | clear_string_option(&buf->b_p_ep); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6192 | break; |
| 6193 | case PV_KP: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6194 | clear_string_option(&buf->b_p_kp); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6195 | break; |
| 6196 | case PV_PATH: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6197 | clear_string_option(&buf->b_p_path); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6198 | break; |
| 6199 | case PV_AR: |
| 6200 | buf->b_p_ar = -1; |
| 6201 | break; |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 6202 | case PV_BKC: |
| 6203 | clear_string_option(&buf->b_p_bkc); |
| 6204 | buf->b_bkc_flags = 0; |
| 6205 | break; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6206 | case PV_TAGS: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6207 | clear_string_option(&buf->b_p_tags); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6208 | break; |
Bram Moolenaar | 0f6562e | 2015-11-24 18:48:14 +0100 | [diff] [blame] | 6209 | case PV_TC: |
| 6210 | clear_string_option(&buf->b_p_tc); |
| 6211 | buf->b_tc_flags = 0; |
| 6212 | break; |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 6213 | case PV_SISO: |
| 6214 | curwin->w_p_siso = -1; |
| 6215 | break; |
| 6216 | case PV_SO: |
| 6217 | curwin->w_p_so = -1; |
| 6218 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6219 | # ifdef FEAT_FIND_ID |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6220 | case PV_DEF: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6221 | clear_string_option(&buf->b_p_def); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6222 | break; |
| 6223 | case PV_INC: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6224 | clear_string_option(&buf->b_p_inc); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6225 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6226 | # endif |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 6227 | case PV_COT: |
| 6228 | clear_string_option(&buf->b_p_cot); |
| 6229 | buf->b_cot_flags = 0; |
| 6230 | break; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6231 | case PV_DICT: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6232 | clear_string_option(&buf->b_p_dict); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6233 | break; |
| 6234 | case PV_TSR: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6235 | clear_string_option(&buf->b_p_tsr); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6236 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6237 | # ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 6238 | case PV_TSRFU: |
| 6239 | clear_string_option(&buf->b_p_tsrfu); |
| 6240 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6241 | # endif |
Bram Moolenaar | 9be7c04 | 2017-01-14 14:28:30 +0100 | [diff] [blame] | 6242 | case PV_FP: |
| 6243 | clear_string_option(&buf->b_p_fp); |
| 6244 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6245 | # ifdef FEAT_QUICKFIX |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6246 | case PV_EFM: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6247 | clear_string_option(&buf->b_p_efm); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6248 | break; |
| 6249 | case PV_GP: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6250 | clear_string_option(&buf->b_p_gp); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6251 | break; |
| 6252 | case PV_MP: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6253 | clear_string_option(&buf->b_p_mp); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6254 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6255 | # endif |
| 6256 | # if defined(FEAT_BEVAL) && defined(FEAT_EVAL) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6257 | case PV_BEXPR: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6258 | clear_string_option(&buf->b_p_bexpr); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6259 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6260 | # endif |
| 6261 | # if defined(FEAT_CRYPT) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6262 | case PV_CM: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6263 | clear_string_option(&buf->b_p_cm); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6264 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6265 | # endif |
| 6266 | # ifdef FEAT_LINEBREAK |
Bram Moolenaar | ee85702 | 2019-11-09 23:26:40 +0100 | [diff] [blame] | 6267 | case PV_SBR: |
| 6268 | clear_string_option(&((win_T *)from)->w_p_sbr); |
| 6269 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6270 | # endif |
| 6271 | # ifdef FEAT_STL_OPT |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6272 | case PV_STL: |
Bram Moolenaar | 51ac8a2 | 2013-05-06 06:45:47 +0200 | [diff] [blame] | 6273 | clear_string_option(&((win_T *)from)->w_p_stl); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6274 | break; |
Bram Moolenaar | 9af2ea8 | 2022-11-22 18:18:38 +0000 | [diff] [blame] | 6275 | # endif |
Bram Moolenaar | f5a2fd8 | 2013-11-06 05:26:15 +0100 | [diff] [blame] | 6276 | case PV_UL: |
| 6277 | buf->b_p_ul = NO_LOCAL_UNDOLEVEL; |
| 6278 | break; |
Bram Moolenaar | af6c131 | 2014-03-12 18:55:58 +0100 | [diff] [blame] | 6279 | case PV_LW: |
| 6280 | clear_string_option(&buf->b_p_lw); |
| 6281 | break; |
Bram Moolenaar | 2c7292d | 2017-03-05 17:43:31 +0100 | [diff] [blame] | 6282 | case PV_MENC: |
| 6283 | clear_string_option(&buf->b_p_menc); |
| 6284 | break; |
Bram Moolenaar | eed9d46 | 2021-02-15 20:38:25 +0100 | [diff] [blame] | 6285 | case PV_LCS: |
| 6286 | clear_string_option(&((win_T *)from)->w_p_lcs); |
zeertzjq | 6a8d2e1 | 2024-01-17 20:54:49 +0100 | [diff] [blame] | 6287 | set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE, |
| 6288 | NULL, 0); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 6289 | redraw_later(UPD_NOT_VALID); |
Bram Moolenaar | eed9d46 | 2021-02-15 20:38:25 +0100 | [diff] [blame] | 6290 | break; |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6291 | case PV_FCS: |
| 6292 | clear_string_option(&((win_T *)from)->w_p_fcs); |
zeertzjq | 6a8d2e1 | 2024-01-17 20:54:49 +0100 | [diff] [blame] | 6293 | set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE, |
| 6294 | NULL, 0); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 6295 | redraw_later(UPD_NOT_VALID); |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6296 | break; |
Gary Johnson | 53ba05b | 2021-07-26 22:19:10 +0200 | [diff] [blame] | 6297 | case PV_VE: |
Gary Johnson | 51ad850 | 2021-08-03 18:33:08 +0200 | [diff] [blame] | 6298 | clear_string_option(&((win_T *)from)->w_p_ve); |
| 6299 | ((win_T *)from)->w_ve_flags = 0; |
Gary Johnson | 53ba05b | 2021-07-26 22:19:10 +0200 | [diff] [blame] | 6300 | break; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6301 | } |
| 6302 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 6303 | #endif |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 6304 | |
| 6305 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6306 | * Get pointer to option variable, depending on local or global scope. |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 6307 | * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6308 | */ |
| 6309 | static char_u * |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 6310 | get_varp_scope(struct vimoption *p, int scope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6311 | { |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 6312 | if ((scope & OPT_GLOBAL) && p->indir != PV_NONE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6313 | { |
| 6314 | if (p->var == VAR_WIN) |
| 6315 | return (char_u *)GLOBAL_WO(get_varp(p)); |
| 6316 | return p->var; |
| 6317 | } |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 6318 | if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6319 | { |
| 6320 | switch ((int)p->indir) |
| 6321 | { |
Bram Moolenaar | 9be7c04 | 2017-01-14 14:28:30 +0100 | [diff] [blame] | 6322 | case PV_FP: return (char_u *)&(curbuf->b_p_fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6323 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6324 | case PV_EFM: return (char_u *)&(curbuf->b_p_efm); |
| 6325 | case PV_GP: return (char_u *)&(curbuf->b_p_gp); |
| 6326 | case PV_MP: return (char_u *)&(curbuf->b_p_mp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6327 | #endif |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6328 | case PV_EP: return (char_u *)&(curbuf->b_p_ep); |
| 6329 | case PV_KP: return (char_u *)&(curbuf->b_p_kp); |
| 6330 | case PV_PATH: return (char_u *)&(curbuf->b_p_path); |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 6331 | case PV_AR: return (char_u *)&(curbuf->b_p_ar); |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6332 | case PV_TAGS: return (char_u *)&(curbuf->b_p_tags); |
Bram Moolenaar | 0f6562e | 2015-11-24 18:48:14 +0100 | [diff] [blame] | 6333 | case PV_TC: return (char_u *)&(curbuf->b_p_tc); |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 6334 | case PV_SISO: return (char_u *)&(curwin->w_p_siso); |
| 6335 | case PV_SO: return (char_u *)&(curwin->w_p_so); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6336 | #ifdef FEAT_FIND_ID |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6337 | case PV_DEF: return (char_u *)&(curbuf->b_p_def); |
| 6338 | case PV_INC: return (char_u *)&(curbuf->b_p_inc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6339 | #endif |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 6340 | case PV_COT: return (char_u *)&(curbuf->b_p_cot); |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6341 | case PV_DICT: return (char_u *)&(curbuf->b_p_dict); |
| 6342 | case PV_TSR: return (char_u *)&(curbuf->b_p_tsr); |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 6343 | #ifdef FEAT_COMPL_FUNC |
| 6344 | case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu); |
| 6345 | #endif |
Bram Moolenaar | 9b2200a | 2006-03-20 21:55:45 +0000 | [diff] [blame] | 6346 | #if defined(FEAT_BEVAL) && defined(FEAT_EVAL) |
| 6347 | case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr); |
| 6348 | #endif |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 6349 | #if defined(FEAT_CRYPT) |
| 6350 | case PV_CM: return (char_u *)&(curbuf->b_p_cm); |
| 6351 | #endif |
Bram Moolenaar | ee85702 | 2019-11-09 23:26:40 +0100 | [diff] [blame] | 6352 | #ifdef FEAT_LINEBREAK |
| 6353 | case PV_SBR: return (char_u *)&(curwin->w_p_sbr); |
| 6354 | #endif |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6355 | #ifdef FEAT_STL_OPT |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6356 | case PV_STL: return (char_u *)&(curwin->w_p_stl); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6357 | #endif |
Bram Moolenaar | f5a2fd8 | 2013-11-06 05:26:15 +0100 | [diff] [blame] | 6358 | case PV_UL: return (char_u *)&(curbuf->b_p_ul); |
Bram Moolenaar | af6c131 | 2014-03-12 18:55:58 +0100 | [diff] [blame] | 6359 | case PV_LW: return (char_u *)&(curbuf->b_p_lw); |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 6360 | case PV_BKC: return (char_u *)&(curbuf->b_p_bkc); |
Bram Moolenaar | 2c7292d | 2017-03-05 17:43:31 +0100 | [diff] [blame] | 6361 | case PV_MENC: return (char_u *)&(curbuf->b_p_menc); |
Gary Johnson | 53ba05b | 2021-07-26 22:19:10 +0200 | [diff] [blame] | 6362 | case PV_LCS: return (char_u *)&(curwin->w_p_lcs); |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6363 | case PV_FCS: return (char_u *)&(curwin->w_p_fcs); |
Gary Johnson | 51ad850 | 2021-08-03 18:33:08 +0200 | [diff] [blame] | 6364 | case PV_VE: return (char_u *)&(curwin->w_p_ve); |
Bram Moolenaar | eed9d46 | 2021-02-15 20:38:25 +0100 | [diff] [blame] | 6365 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6366 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6367 | return NULL; // "cannot happen" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6368 | } |
| 6369 | return get_varp(p); |
| 6370 | } |
| 6371 | |
| 6372 | /* |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 6373 | * Get pointer to option variable at 'opt_idx', depending on local or global |
| 6374 | * scope. |
| 6375 | */ |
| 6376 | char_u * |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 6377 | get_option_varp_scope(int opt_idx, int scope) |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 6378 | { |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 6379 | return get_varp_scope(&(options[opt_idx]), scope); |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 6380 | } |
| 6381 | |
| 6382 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6383 | * Get pointer to option variable. |
| 6384 | */ |
| 6385 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6386 | get_varp(struct vimoption *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6387 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6388 | // hidden option, always return NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6389 | if (p->var == NULL) |
| 6390 | return NULL; |
| 6391 | |
| 6392 | switch ((int)p->indir) |
| 6393 | { |
| 6394 | case PV_NONE: return p->var; |
| 6395 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6396 | // global option with local value: use local value if it's been set |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6397 | case PV_EP: return *curbuf->b_p_ep != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6398 | ? (char_u *)&curbuf->b_p_ep : p->var; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6399 | case PV_KP: return *curbuf->b_p_kp != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6400 | ? (char_u *)&curbuf->b_p_kp : p->var; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6401 | case PV_PATH: return *curbuf->b_p_path != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6402 | ? (char_u *)&(curbuf->b_p_path) : p->var; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 6403 | case PV_AR: return curbuf->b_p_ar >= 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6404 | ? (char_u *)&(curbuf->b_p_ar) : p->var; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6405 | case PV_TAGS: return *curbuf->b_p_tags != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6406 | ? (char_u *)&(curbuf->b_p_tags) : p->var; |
Bram Moolenaar | 0f6562e | 2015-11-24 18:48:14 +0100 | [diff] [blame] | 6407 | case PV_TC: return *curbuf->b_p_tc != NUL |
| 6408 | ? (char_u *)&(curbuf->b_p_tc) : p->var; |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 6409 | case PV_BKC: return *curbuf->b_p_bkc != NUL |
| 6410 | ? (char_u *)&(curbuf->b_p_bkc) : p->var; |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 6411 | case PV_SISO: return curwin->w_p_siso >= 0 |
| 6412 | ? (char_u *)&(curwin->w_p_siso) : p->var; |
| 6413 | case PV_SO: return curwin->w_p_so >= 0 |
| 6414 | ? (char_u *)&(curwin->w_p_so) : p->var; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6415 | #ifdef FEAT_FIND_ID |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6416 | case PV_DEF: return *curbuf->b_p_def != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6417 | ? (char_u *)&(curbuf->b_p_def) : p->var; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6418 | case PV_INC: return *curbuf->b_p_inc != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6419 | ? (char_u *)&(curbuf->b_p_inc) : p->var; |
| 6420 | #endif |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 6421 | case PV_COT: return *curbuf->b_p_cot != NUL |
| 6422 | ? (char_u *)&(curbuf->b_p_cot) : p->var; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6423 | case PV_DICT: return *curbuf->b_p_dict != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6424 | ? (char_u *)&(curbuf->b_p_dict) : p->var; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6425 | case PV_TSR: return *curbuf->b_p_tsr != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6426 | ? (char_u *)&(curbuf->b_p_tsr) : p->var; |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 6427 | #ifdef FEAT_COMPL_FUNC |
| 6428 | case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL |
| 6429 | ? (char_u *)&(curbuf->b_p_tsrfu) : p->var; |
| 6430 | #endif |
Bram Moolenaar | 9be7c04 | 2017-01-14 14:28:30 +0100 | [diff] [blame] | 6431 | case PV_FP: return *curbuf->b_p_fp != NUL |
| 6432 | ? (char_u *)&(curbuf->b_p_fp) : p->var; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6433 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6434 | case PV_EFM: return *curbuf->b_p_efm != NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6435 | ? (char_u *)&(curbuf->b_p_efm) : p->var; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6436 | case PV_GP: return *curbuf->b_p_gp != NUL |
| 6437 | ? (char_u *)&(curbuf->b_p_gp) : p->var; |
| 6438 | case PV_MP: return *curbuf->b_p_mp != NUL |
| 6439 | ? (char_u *)&(curbuf->b_p_mp) : p->var; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6440 | #endif |
Bram Moolenaar | 9b2200a | 2006-03-20 21:55:45 +0000 | [diff] [blame] | 6441 | #if defined(FEAT_BEVAL) && defined(FEAT_EVAL) |
| 6442 | case PV_BEXPR: return *curbuf->b_p_bexpr != NUL |
| 6443 | ? (char_u *)&(curbuf->b_p_bexpr) : p->var; |
| 6444 | #endif |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 6445 | #if defined(FEAT_CRYPT) |
| 6446 | case PV_CM: return *curbuf->b_p_cm != NUL |
| 6447 | ? (char_u *)&(curbuf->b_p_cm) : p->var; |
| 6448 | #endif |
Bram Moolenaar | ee85702 | 2019-11-09 23:26:40 +0100 | [diff] [blame] | 6449 | #ifdef FEAT_LINEBREAK |
| 6450 | case PV_SBR: return *curwin->w_p_sbr != NUL |
| 6451 | ? (char_u *)&(curwin->w_p_sbr) : p->var; |
| 6452 | #endif |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6453 | #ifdef FEAT_STL_OPT |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6454 | case PV_STL: return *curwin->w_p_stl != NUL |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6455 | ? (char_u *)&(curwin->w_p_stl) : p->var; |
| 6456 | #endif |
Bram Moolenaar | f5a2fd8 | 2013-11-06 05:26:15 +0100 | [diff] [blame] | 6457 | case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL |
| 6458 | ? (char_u *)&(curbuf->b_p_ul) : p->var; |
Bram Moolenaar | af6c131 | 2014-03-12 18:55:58 +0100 | [diff] [blame] | 6459 | case PV_LW: return *curbuf->b_p_lw != NUL |
| 6460 | ? (char_u *)&(curbuf->b_p_lw) : p->var; |
Bram Moolenaar | 2c7292d | 2017-03-05 17:43:31 +0100 | [diff] [blame] | 6461 | case PV_MENC: return *curbuf->b_p_menc != NUL |
| 6462 | ? (char_u *)&(curbuf->b_p_menc) : p->var; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6463 | #ifdef FEAT_ARABIC |
| 6464 | case PV_ARAB: return (char_u *)&(curwin->w_p_arab); |
| 6465 | #endif |
| 6466 | case PV_LIST: return (char_u *)&(curwin->w_p_list); |
Bram Moolenaar | eed9d46 | 2021-02-15 20:38:25 +0100 | [diff] [blame] | 6467 | case PV_LCS: return *curwin->w_p_lcs != NUL |
| 6468 | ? (char_u *)&(curwin->w_p_lcs) : p->var; |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6469 | case PV_FCS: return *curwin->w_p_fcs != NUL |
| 6470 | ? (char_u *)&(curwin->w_p_fcs) : p->var; |
Gary Johnson | 51ad850 | 2021-08-03 18:33:08 +0200 | [diff] [blame] | 6471 | case PV_VE: return *curwin->w_p_ve != NUL |
| 6472 | ? (char_u *)&(curwin->w_p_ve) : p->var; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6473 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6474 | case PV_SPELL: return (char_u *)&(curwin->w_p_spell); |
| 6475 | #endif |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6476 | #ifdef FEAT_SYN_HL |
| 6477 | case PV_CUC: return (char_u *)&(curwin->w_p_cuc); |
| 6478 | case PV_CUL: return (char_u *)&(curwin->w_p_cul); |
Bram Moolenaar | 410e98a | 2019-09-09 22:05:49 +0200 | [diff] [blame] | 6479 | case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt); |
Bram Moolenaar | 1a38442 | 2010-07-14 19:53:30 +0200 | [diff] [blame] | 6480 | case PV_CC: return (char_u *)&(curwin->w_p_cc); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6481 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6482 | #ifdef FEAT_DIFF |
| 6483 | case PV_DIFF: return (char_u *)&(curwin->w_p_diff); |
| 6484 | #endif |
| 6485 | #ifdef FEAT_FOLDING |
| 6486 | case PV_FDC: return (char_u *)&(curwin->w_p_fdc); |
| 6487 | case PV_FEN: return (char_u *)&(curwin->w_p_fen); |
| 6488 | case PV_FDI: return (char_u *)&(curwin->w_p_fdi); |
| 6489 | case PV_FDL: return (char_u *)&(curwin->w_p_fdl); |
| 6490 | case PV_FDM: return (char_u *)&(curwin->w_p_fdm); |
| 6491 | case PV_FML: return (char_u *)&(curwin->w_p_fml); |
| 6492 | case PV_FDN: return (char_u *)&(curwin->w_p_fdn); |
| 6493 | # ifdef FEAT_EVAL |
| 6494 | case PV_FDE: return (char_u *)&(curwin->w_p_fde); |
| 6495 | case PV_FDT: return (char_u *)&(curwin->w_p_fdt); |
| 6496 | # endif |
| 6497 | case PV_FMR: return (char_u *)&(curwin->w_p_fmr); |
| 6498 | #endif |
| 6499 | case PV_NU: return (char_u *)&(curwin->w_p_nu); |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 6500 | case PV_RNU: return (char_u *)&(curwin->w_p_rnu); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 6501 | #ifdef FEAT_LINEBREAK |
| 6502 | case PV_NUW: return (char_u *)&(curwin->w_p_nuw); |
| 6503 | #endif |
Colin Kennedy | 2157035 | 2024-03-03 16:16:47 +0100 | [diff] [blame] | 6504 | case PV_WFB: return (char_u *)&(curwin->w_p_wfb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6505 | case PV_WFH: return (char_u *)&(curwin->w_p_wfh); |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 6506 | case PV_WFW: return (char_u *)&(curwin->w_p_wfw); |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 6507 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6508 | case PV_PVW: return (char_u *)&(curwin->w_p_pvw); |
| 6509 | #endif |
| 6510 | #ifdef FEAT_RIGHTLEFT |
| 6511 | case PV_RL: return (char_u *)&(curwin->w_p_rl); |
| 6512 | case PV_RLC: return (char_u *)&(curwin->w_p_rlc); |
| 6513 | #endif |
| 6514 | case PV_SCROLL: return (char_u *)&(curwin->w_p_scr); |
Bram Moolenaar | f6196f4 | 2022-10-02 21:29:55 +0100 | [diff] [blame] | 6515 | case PV_SMS: return (char_u *)&(curwin->w_p_sms); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6516 | case PV_WRAP: return (char_u *)&(curwin->w_p_wrap); |
| 6517 | #ifdef FEAT_LINEBREAK |
| 6518 | case PV_LBR: return (char_u *)&(curwin->w_p_lbr); |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 6519 | case PV_BRI: return (char_u *)&(curwin->w_p_bri); |
| 6520 | case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6521 | #endif |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 6522 | case PV_WCR: return (char_u *)&(curwin->w_p_wcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6523 | case PV_SCBIND: return (char_u *)&(curwin->w_p_scb); |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 6524 | case PV_CRBIND: return (char_u *)&(curwin->w_p_crb); |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 6525 | #ifdef FEAT_CONCEAL |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 6526 | case PV_COCU: return (char_u *)&(curwin->w_p_cocu); |
| 6527 | case PV_COLE: return (char_u *)&(curwin->w_p_cole); |
| 6528 | #endif |
| 6529 | #ifdef FEAT_TERMINAL |
Bram Moolenaar | 6d150f7 | 2018-04-21 20:03:20 +0200 | [diff] [blame] | 6530 | case PV_TWK: return (char_u *)&(curwin->w_p_twk); |
| 6531 | case PV_TWS: return (char_u *)&(curwin->w_p_tws); |
| 6532 | case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl); |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 6533 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6534 | |
| 6535 | case PV_AI: return (char_u *)&(curbuf->b_p_ai); |
| 6536 | case PV_BIN: return (char_u *)&(curbuf->b_p_bin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6537 | case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6538 | case PV_BH: return (char_u *)&(curbuf->b_p_bh); |
| 6539 | case PV_BT: return (char_u *)&(curbuf->b_p_bt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6540 | case PV_BL: return (char_u *)&(curbuf->b_p_bl); |
| 6541 | case PV_CI: return (char_u *)&(curbuf->b_p_ci); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6542 | case PV_CIN: return (char_u *)&(curbuf->b_p_cin); |
| 6543 | case PV_CINK: return (char_u *)&(curbuf->b_p_cink); |
| 6544 | case PV_CINO: return (char_u *)&(curbuf->b_p_cino); |
Tom Praschan | 3506cf3 | 2022-04-07 12:39:08 +0100 | [diff] [blame] | 6545 | case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6546 | case PV_CINW: return (char_u *)&(curbuf->b_p_cinw); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6547 | case PV_COM: return (char_u *)&(curbuf->b_p_com); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6548 | #ifdef FEAT_FOLDING |
| 6549 | case PV_CMS: return (char_u *)&(curbuf->b_p_cms); |
| 6550 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6551 | case PV_CPT: return (char_u *)&(curbuf->b_p_cpt); |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 6552 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 6553 | case PV_CSL: return (char_u *)&(curbuf->b_p_csl); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6554 | #endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6555 | #ifdef FEAT_COMPL_FUNC |
| 6556 | case PV_CFU: return (char_u *)&(curbuf->b_p_cfu); |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 6557 | case PV_OFU: return (char_u *)&(curbuf->b_p_ofu); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6558 | #endif |
Bram Moolenaar | 45e18cb | 2019-04-28 18:05:35 +0200 | [diff] [blame] | 6559 | #ifdef FEAT_EVAL |
| 6560 | case PV_TFU: return (char_u *)&(curbuf->b_p_tfu); |
| 6561 | #endif |
Bram Moolenaar | 3f68a41 | 2022-10-28 17:04:21 +0100 | [diff] [blame] | 6562 | case PV_EOF: return (char_u *)&(curbuf->b_p_eof); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6563 | case PV_EOL: return (char_u *)&(curbuf->b_p_eol); |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 6564 | case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6565 | case PV_ET: return (char_u *)&(curbuf->b_p_et); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6566 | case PV_FENC: return (char_u *)&(curbuf->b_p_fenc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6567 | case PV_FF: return (char_u *)&(curbuf->b_p_ff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6568 | case PV_FT: return (char_u *)&(curbuf->b_p_ft); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6569 | case PV_FO: return (char_u *)&(curbuf->b_p_fo); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6570 | case PV_FLP: return (char_u *)&(curbuf->b_p_flp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6571 | case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert); |
| 6572 | case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch); |
| 6573 | case PV_INF: return (char_u *)&(curbuf->b_p_inf); |
| 6574 | case PV_ISK: return (char_u *)&(curbuf->b_p_isk); |
| 6575 | #ifdef FEAT_FIND_ID |
| 6576 | # ifdef FEAT_EVAL |
| 6577 | case PV_INEX: return (char_u *)&(curbuf->b_p_inex); |
| 6578 | # endif |
| 6579 | #endif |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 6580 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6581 | case PV_INDE: return (char_u *)&(curbuf->b_p_inde); |
| 6582 | case PV_INDK: return (char_u *)&(curbuf->b_p_indk); |
| 6583 | #endif |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6584 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6585 | case PV_FEX: return (char_u *)&(curbuf->b_p_fex); |
| 6586 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6587 | #ifdef FEAT_CRYPT |
| 6588 | case PV_KEY: return (char_u *)&(curbuf->b_p_key); |
| 6589 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6590 | case PV_LISP: return (char_u *)&(curbuf->b_p_lisp); |
Bram Moolenaar | 49846fb | 2022-10-15 16:05:33 +0100 | [diff] [blame] | 6591 | case PV_LOP: return (char_u *)&(curbuf->b_p_lop); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6592 | case PV_ML: return (char_u *)&(curbuf->b_p_ml); |
| 6593 | case PV_MPS: return (char_u *)&(curbuf->b_p_mps); |
| 6594 | case PV_MA: return (char_u *)&(curbuf->b_p_ma); |
| 6595 | case PV_MOD: return (char_u *)&(curbuf->b_changed); |
| 6596 | case PV_NF: return (char_u *)&(curbuf->b_p_nf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6597 | case PV_PI: return (char_u *)&(curbuf->b_p_pi); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 6598 | case PV_QE: return (char_u *)&(curbuf->b_p_qe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6599 | case PV_RO: return (char_u *)&(curbuf->b_p_ro); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6600 | case PV_SI: return (char_u *)&(curbuf->b_p_si); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6601 | case PV_SN: return (char_u *)&(curbuf->b_p_sn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6602 | case PV_STS: return (char_u *)&(curbuf->b_p_sts); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6603 | case PV_SUA: return (char_u *)&(curbuf->b_p_sua); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6604 | case PV_SWF: return (char_u *)&(curbuf->b_p_swf); |
| 6605 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 6606 | case PV_SMC: return (char_u *)&(curbuf->b_p_smc); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6607 | case PV_SYN: return (char_u *)&(curbuf->b_p_syn); |
| 6608 | #endif |
| 6609 | #ifdef FEAT_SPELL |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 6610 | case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc); |
| 6611 | case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf); |
| 6612 | case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl); |
Bram Moolenaar | 362b44b | 2020-06-10 21:47:00 +0200 | [diff] [blame] | 6613 | case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6614 | #endif |
| 6615 | case PV_SW: return (char_u *)&(curbuf->b_p_sw); |
| 6616 | case PV_TS: return (char_u *)&(curbuf->b_p_ts); |
| 6617 | case PV_TW: return (char_u *)&(curbuf->b_p_tw); |
| 6618 | case PV_TX: return (char_u *)&(curbuf->b_p_tx); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 6619 | #ifdef FEAT_PERSISTENT_UNDO |
| 6620 | case PV_UDF: return (char_u *)&(curbuf->b_p_udf); |
| 6621 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6622 | case PV_WM: return (char_u *)&(curbuf->b_p_wm); |
| 6623 | #ifdef FEAT_KEYMAP |
| 6624 | case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap); |
| 6625 | #endif |
Bram Moolenaar | 95ec9d6 | 2016-08-12 18:29:59 +0200 | [diff] [blame] | 6626 | #ifdef FEAT_SIGNS |
| 6627 | case PV_SCL: return (char_u *)&(curwin->w_p_scl); |
| 6628 | #endif |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 6629 | #ifdef FEAT_VARTABS |
| 6630 | case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts); |
| 6631 | case PV_VTS: return (char_u *)&(curbuf->b_p_vts); |
| 6632 | #endif |
RestorerZ | 68ebcee | 2023-05-31 17:12:14 +0100 | [diff] [blame] | 6633 | default: iemsg(e_get_varp_error); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6634 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6635 | // always return a valid pointer to avoid a crash! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6636 | return (char_u *)&(curbuf->b_p_wm); |
| 6637 | } |
| 6638 | |
| 6639 | /* |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 6640 | * Return a pointer to the variable for option at 'opt_idx' |
| 6641 | */ |
| 6642 | char_u * |
| 6643 | get_option_var(int opt_idx) |
| 6644 | { |
| 6645 | return options[opt_idx].var; |
| 6646 | } |
| 6647 | |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 6648 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 6649 | /* |
| 6650 | * Return the full name of the option at 'opt_idx' |
| 6651 | */ |
| 6652 | char_u * |
| 6653 | get_option_fullname(int opt_idx) |
| 6654 | { |
| 6655 | return (char_u *)options[opt_idx].fullname; |
| 6656 | } |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 6657 | #endif |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 6658 | |
| 6659 | /* |
Yegappan Lakshmanan | af93691 | 2023-02-20 12:16:39 +0000 | [diff] [blame] | 6660 | * Return the did_set callback function for the option at 'opt_idx' |
| 6661 | */ |
| 6662 | opt_did_set_cb_T |
| 6663 | get_option_did_set_cb(int opt_idx) |
| 6664 | { |
| 6665 | return options[opt_idx].opt_did_set_cb; |
| 6666 | } |
| 6667 | |
| 6668 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6669 | * Get the value of 'equalprg', either the buffer-local one or the global one. |
| 6670 | */ |
| 6671 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6672 | get_equalprg(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6673 | { |
| 6674 | if (*curbuf->b_p_ep == NUL) |
| 6675 | return p_ep; |
| 6676 | return curbuf->b_p_ep; |
| 6677 | } |
| 6678 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6679 | /* |
| 6680 | * Copy options from one window to another. |
| 6681 | * Used when splitting a window. |
| 6682 | */ |
| 6683 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6684 | win_copy_options(win_T *wp_from, win_T *wp_to) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6685 | { |
| 6686 | copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt); |
| 6687 | copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt); |
Bram Moolenaar | 010ee96 | 2019-09-25 20:37:36 +0200 | [diff] [blame] | 6688 | after_copy_winopt(wp_to); |
| 6689 | } |
| 6690 | |
| 6691 | /* |
| 6692 | * After copying window options: update variables depending on options. |
| 6693 | */ |
| 6694 | void |
Sean Dewar | 0f7ff85 | 2022-02-12 11:51:25 +0000 | [diff] [blame] | 6695 | after_copy_winopt(win_T *wp) |
Bram Moolenaar | 010ee96 | 2019-09-25 20:37:36 +0200 | [diff] [blame] | 6696 | { |
| 6697 | #ifdef FEAT_LINEBREAK |
| 6698 | briopt_check(wp); |
Bram Moolenaar | 285ed7e | 2014-08-24 21:39:49 +0200 | [diff] [blame] | 6699 | #endif |
Bram Moolenaar | 017ba07 | 2019-09-14 21:01:23 +0200 | [diff] [blame] | 6700 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 010ee96 | 2019-09-25 20:37:36 +0200 | [diff] [blame] | 6701 | fill_culopt_flags(NULL, wp); |
| 6702 | check_colorcolumn(wp); |
Bram Moolenaar | 017ba07 | 2019-09-14 21:01:23 +0200 | [diff] [blame] | 6703 | #endif |
zeertzjq | 6a8d2e1 | 2024-01-17 20:54:49 +0100 | [diff] [blame] | 6704 | set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0); |
| 6705 | set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0); |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6706 | } |
| 6707 | |
| 6708 | static char_u * |
| 6709 | copy_option_val(char_u *val) |
| 6710 | { |
| 6711 | if (val == empty_option) |
| 6712 | return empty_option; // no need to allocate memory |
| 6713 | return vim_strsave(val); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6714 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6715 | |
| 6716 | /* |
| 6717 | * Copy the options from one winopt_T to another. |
| 6718 | * Doesn't free the old option values in "to", use clear_winopt() for that. |
| 6719 | * The 'scroll' option is not copied, because it depends on the window height. |
| 6720 | * The 'previewwindow' option is reset, there can be only one preview window. |
| 6721 | */ |
| 6722 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6723 | copy_winopt(winopt_T *from, winopt_T *to) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6724 | { |
| 6725 | #ifdef FEAT_ARABIC |
| 6726 | to->wo_arab = from->wo_arab; |
| 6727 | #endif |
| 6728 | to->wo_list = from->wo_list; |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6729 | to->wo_lcs = copy_option_val(from->wo_lcs); |
| 6730 | to->wo_fcs = copy_option_val(from->wo_fcs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6731 | to->wo_nu = from->wo_nu; |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 6732 | to->wo_rnu = from->wo_rnu; |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6733 | to->wo_ve = copy_option_val(from->wo_ve); |
Gary Johnson | 51ad850 | 2021-08-03 18:33:08 +0200 | [diff] [blame] | 6734 | to->wo_ve_flags = from->wo_ve_flags; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 6735 | #ifdef FEAT_LINEBREAK |
| 6736 | to->wo_nuw = from->wo_nuw; |
| 6737 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6738 | #ifdef FEAT_RIGHTLEFT |
| 6739 | to->wo_rl = from->wo_rl; |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6740 | to->wo_rlc = copy_option_val(from->wo_rlc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6741 | #endif |
Bram Moolenaar | ee85702 | 2019-11-09 23:26:40 +0100 | [diff] [blame] | 6742 | #ifdef FEAT_LINEBREAK |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6743 | to->wo_sbr = copy_option_val(from->wo_sbr); |
Bram Moolenaar | ee85702 | 2019-11-09 23:26:40 +0100 | [diff] [blame] | 6744 | #endif |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6745 | #ifdef FEAT_STL_OPT |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6746 | to->wo_stl = copy_option_val(from->wo_stl); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6747 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6748 | to->wo_wrap = from->wo_wrap; |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6749 | #ifdef FEAT_DIFF |
| 6750 | to->wo_wrap_save = from->wo_wrap_save; |
| 6751 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6752 | #ifdef FEAT_LINEBREAK |
| 6753 | to->wo_lbr = from->wo_lbr; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 6754 | to->wo_bri = from->wo_bri; |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6755 | to->wo_briopt = copy_option_val(from->wo_briopt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6756 | #endif |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6757 | to->wo_wcr = copy_option_val(from->wo_wcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6758 | to->wo_scb = from->wo_scb; |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6759 | to->wo_scb_save = from->wo_scb_save; |
Bram Moolenaar | b1fd26d | 2022-10-03 11:23:02 +0100 | [diff] [blame] | 6760 | to->wo_sms = from->wo_sms; |
Bram Moolenaar | 4161dcc | 2010-12-02 15:33:21 +0100 | [diff] [blame] | 6761 | to->wo_crb = from->wo_crb; |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6762 | to->wo_crb_save = from->wo_crb_save; |
Christian Brabandt | 4a8eb6e | 2023-08-13 19:43:42 +0200 | [diff] [blame] | 6763 | to->wo_siso = from->wo_siso; |
| 6764 | to->wo_so = from->wo_so; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6765 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6766 | to->wo_spell = from->wo_spell; |
| 6767 | #endif |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6768 | #ifdef FEAT_SYN_HL |
| 6769 | to->wo_cuc = from->wo_cuc; |
| 6770 | to->wo_cul = from->wo_cul; |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6771 | to->wo_culopt = copy_option_val(from->wo_culopt); |
| 6772 | to->wo_cc = copy_option_val(from->wo_cc); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6773 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6774 | #ifdef FEAT_DIFF |
| 6775 | to->wo_diff = from->wo_diff; |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6776 | to->wo_diff_saved = from->wo_diff_saved; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6777 | #endif |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 6778 | #ifdef FEAT_CONCEAL |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6779 | to->wo_cocu = copy_option_val(from->wo_cocu); |
Bram Moolenaar | d497a30 | 2010-07-23 22:27:03 +0200 | [diff] [blame] | 6780 | to->wo_cole = from->wo_cole; |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 6781 | #endif |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 6782 | #ifdef FEAT_TERMINAL |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6783 | to->wo_twk = copy_option_val(from->wo_twk); |
| 6784 | to->wo_tws = copy_option_val(from->wo_tws); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 6785 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6786 | #ifdef FEAT_FOLDING |
| 6787 | to->wo_fdc = from->wo_fdc; |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6788 | to->wo_fdc_save = from->wo_fdc_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6789 | to->wo_fen = from->wo_fen; |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6790 | to->wo_fen_save = from->wo_fen_save; |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6791 | to->wo_fdi = copy_option_val(from->wo_fdi); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6792 | to->wo_fml = from->wo_fml; |
| 6793 | to->wo_fdl = from->wo_fdl; |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6794 | to->wo_fdl_save = from->wo_fdl_save; |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6795 | to->wo_fdm = copy_option_val(from->wo_fdm); |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6796 | to->wo_fdm_save = from->wo_diff_saved |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6797 | ? vim_strsave(from->wo_fdm_save) : empty_option; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6798 | to->wo_fdn = from->wo_fdn; |
| 6799 | # ifdef FEAT_EVAL |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6800 | to->wo_fde = copy_option_val(from->wo_fde); |
| 6801 | to->wo_fdt = copy_option_val(from->wo_fdt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6802 | # endif |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6803 | to->wo_fmr = copy_option_val(from->wo_fmr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6804 | #endif |
Bram Moolenaar | 95ec9d6 | 2016-08-12 18:29:59 +0200 | [diff] [blame] | 6805 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6806 | to->wo_scl = copy_option_val(from->wo_scl); |
Bram Moolenaar | 95ec9d6 | 2016-08-12 18:29:59 +0200 | [diff] [blame] | 6807 | #endif |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 6808 | |
| 6809 | #ifdef FEAT_EVAL |
| 6810 | // Copy the script context so that we know where the value was last set. |
| 6811 | mch_memmove(to->wo_script_ctx, from->wo_script_ctx, |
| 6812 | sizeof(to->wo_script_ctx)); |
| 6813 | #endif |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6814 | check_winopt(to); // don't want NULL pointers |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6815 | } |
| 6816 | |
| 6817 | /* |
| 6818 | * Check string options in a window for a NULL value. |
| 6819 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 6820 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6821 | check_win_options(win_T *win) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6822 | { |
| 6823 | check_winopt(&win->w_onebuf_opt); |
| 6824 | check_winopt(&win->w_allbuf_opt); |
| 6825 | } |
| 6826 | |
| 6827 | /* |
| 6828 | * Check for NULL pointers in a winopt_T and replace them with empty_option. |
| 6829 | */ |
Bram Moolenaar | 8dc907d | 2014-06-25 14:44:10 +0200 | [diff] [blame] | 6830 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6831 | check_winopt(winopt_T *wop UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6832 | { |
| 6833 | #ifdef FEAT_FOLDING |
| 6834 | check_string_option(&wop->wo_fdi); |
| 6835 | check_string_option(&wop->wo_fdm); |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6836 | check_string_option(&wop->wo_fdm_save); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6837 | # ifdef FEAT_EVAL |
| 6838 | check_string_option(&wop->wo_fde); |
| 6839 | check_string_option(&wop->wo_fdt); |
| 6840 | # endif |
| 6841 | check_string_option(&wop->wo_fmr); |
| 6842 | #endif |
Bram Moolenaar | 95ec9d6 | 2016-08-12 18:29:59 +0200 | [diff] [blame] | 6843 | #ifdef FEAT_SIGNS |
| 6844 | check_string_option(&wop->wo_scl); |
| 6845 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6846 | #ifdef FEAT_RIGHTLEFT |
| 6847 | check_string_option(&wop->wo_rlc); |
| 6848 | #endif |
Bram Moolenaar | ee85702 | 2019-11-09 23:26:40 +0100 | [diff] [blame] | 6849 | #ifdef FEAT_LINEBREAK |
| 6850 | check_string_option(&wop->wo_sbr); |
| 6851 | #endif |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6852 | #ifdef FEAT_STL_OPT |
| 6853 | check_string_option(&wop->wo_stl); |
| 6854 | #endif |
Bram Moolenaar | 1a38442 | 2010-07-14 19:53:30 +0200 | [diff] [blame] | 6855 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 410e98a | 2019-09-09 22:05:49 +0200 | [diff] [blame] | 6856 | check_string_option(&wop->wo_culopt); |
Bram Moolenaar | 1a38442 | 2010-07-14 19:53:30 +0200 | [diff] [blame] | 6857 | check_string_option(&wop->wo_cc); |
| 6858 | #endif |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 6859 | #ifdef FEAT_CONCEAL |
| 6860 | check_string_option(&wop->wo_cocu); |
| 6861 | #endif |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 6862 | #ifdef FEAT_TERMINAL |
Bram Moolenaar | 6d150f7 | 2018-04-21 20:03:20 +0200 | [diff] [blame] | 6863 | check_string_option(&wop->wo_twk); |
| 6864 | check_string_option(&wop->wo_tws); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 6865 | #endif |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 6866 | #ifdef FEAT_LINEBREAK |
| 6867 | check_string_option(&wop->wo_briopt); |
| 6868 | #endif |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 6869 | check_string_option(&wop->wo_wcr); |
Bram Moolenaar | eed9d46 | 2021-02-15 20:38:25 +0100 | [diff] [blame] | 6870 | check_string_option(&wop->wo_lcs); |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6871 | check_string_option(&wop->wo_fcs); |
Gary Johnson | 51ad850 | 2021-08-03 18:33:08 +0200 | [diff] [blame] | 6872 | check_string_option(&wop->wo_ve); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6873 | } |
| 6874 | |
| 6875 | /* |
| 6876 | * Free the allocated memory inside a winopt_T. |
| 6877 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6878 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6879 | clear_winopt(winopt_T *wop UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6880 | { |
| 6881 | #ifdef FEAT_FOLDING |
| 6882 | clear_string_option(&wop->wo_fdi); |
| 6883 | clear_string_option(&wop->wo_fdm); |
Bram Moolenaar | a87aa80 | 2013-07-03 15:47:03 +0200 | [diff] [blame] | 6884 | clear_string_option(&wop->wo_fdm_save); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6885 | # ifdef FEAT_EVAL |
| 6886 | clear_string_option(&wop->wo_fde); |
| 6887 | clear_string_option(&wop->wo_fdt); |
| 6888 | # endif |
| 6889 | clear_string_option(&wop->wo_fmr); |
| 6890 | #endif |
Bram Moolenaar | 95ec9d6 | 2016-08-12 18:29:59 +0200 | [diff] [blame] | 6891 | #ifdef FEAT_SIGNS |
| 6892 | clear_string_option(&wop->wo_scl); |
| 6893 | #endif |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 6894 | #ifdef FEAT_LINEBREAK |
| 6895 | clear_string_option(&wop->wo_briopt); |
| 6896 | #endif |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 6897 | clear_string_option(&wop->wo_wcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6898 | #ifdef FEAT_RIGHTLEFT |
| 6899 | clear_string_option(&wop->wo_rlc); |
| 6900 | #endif |
Bram Moolenaar | ee85702 | 2019-11-09 23:26:40 +0100 | [diff] [blame] | 6901 | #ifdef FEAT_LINEBREAK |
| 6902 | clear_string_option(&wop->wo_sbr); |
| 6903 | #endif |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6904 | #ifdef FEAT_STL_OPT |
| 6905 | clear_string_option(&wop->wo_stl); |
| 6906 | #endif |
Bram Moolenaar | 1a38442 | 2010-07-14 19:53:30 +0200 | [diff] [blame] | 6907 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 410e98a | 2019-09-09 22:05:49 +0200 | [diff] [blame] | 6908 | clear_string_option(&wop->wo_culopt); |
Bram Moolenaar | 1a38442 | 2010-07-14 19:53:30 +0200 | [diff] [blame] | 6909 | clear_string_option(&wop->wo_cc); |
| 6910 | #endif |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 6911 | #ifdef FEAT_CONCEAL |
| 6912 | clear_string_option(&wop->wo_cocu); |
| 6913 | #endif |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 6914 | #ifdef FEAT_TERMINAL |
Bram Moolenaar | 6d150f7 | 2018-04-21 20:03:20 +0200 | [diff] [blame] | 6915 | clear_string_option(&wop->wo_twk); |
| 6916 | clear_string_option(&wop->wo_tws); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 6917 | #endif |
Bram Moolenaar | eed9d46 | 2021-02-15 20:38:25 +0100 | [diff] [blame] | 6918 | clear_string_option(&wop->wo_lcs); |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 6919 | clear_string_option(&wop->wo_fcs); |
Gary Johnson | 51ad850 | 2021-08-03 18:33:08 +0200 | [diff] [blame] | 6920 | clear_string_option(&wop->wo_ve); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 6923 | #ifdef FEAT_EVAL |
| 6924 | // Index into the options table for a buffer-local option enum. |
| 6925 | static int buf_opt_idx[BV_COUNT]; |
| 6926 | # define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx |
| 6927 | |
| 6928 | /* |
| 6929 | * Initialize buf_opt_idx[] if not done already. |
| 6930 | */ |
| 6931 | static void |
| 6932 | init_buf_opt_idx(void) |
| 6933 | { |
| 6934 | static int did_init_buf_opt_idx = FALSE; |
| 6935 | int i; |
| 6936 | |
| 6937 | if (did_init_buf_opt_idx) |
| 6938 | return; |
| 6939 | did_init_buf_opt_idx = TRUE; |
| 6940 | for (i = 0; !istermoption_idx(i); i++) |
| 6941 | if (options[i].indir & PV_BUF) |
| 6942 | buf_opt_idx[options[i].indir & PV_MASK] = i; |
| 6943 | } |
| 6944 | #else |
| 6945 | # define COPY_OPT_SCTX(buf, bv) |
| 6946 | #endif |
| 6947 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6948 | /* |
| 6949 | * Copy global option values to local options for one buffer. |
| 6950 | * Used when creating a new buffer and sometimes when entering a buffer. |
| 6951 | * flags: |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 6952 | * BCO_ENTER We will enter the buffer "buf". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6953 | * BCO_ALWAYS Always copy the options, but only set b_p_initialized when |
| 6954 | * appropriate. |
| 6955 | * BCO_NOHELP Don't copy the values to a help buffer. |
| 6956 | */ |
| 6957 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6958 | buf_copy_options(buf_T *buf, int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6959 | { |
| 6960 | int should_copy = TRUE; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 6961 | char_u *save_p_isk = NULL; // init for GCC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6962 | int dont_do_help; |
| 6963 | int did_isk = FALSE; |
| 6964 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 6965 | // Skip this when the option defaults have not been set yet. Happens when |
| 6966 | // main() allocates the first buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6967 | if (p_cpo != NULL) |
| 6968 | { |
| 6969 | /* |
| 6970 | * Always copy when entering and 'cpo' contains 'S'. |
| 6971 | * Don't copy when already initialized. |
| 6972 | * Don't copy when 'cpo' contains 's' and not entering. |
| 6973 | * 'S' BCO_ENTER initialized 's' should_copy |
| 6974 | * yes yes X X TRUE |
| 6975 | * yes no yes X FALSE |
| 6976 | * no X yes X FALSE |
| 6977 | * X no no yes FALSE |
| 6978 | * X no no no TRUE |
| 6979 | * no yes no X TRUE |
| 6980 | */ |
| 6981 | if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER)) |
| 6982 | && (buf->b_p_initialized |
| 6983 | || (!(flags & BCO_ENTER) |
| 6984 | && vim_strchr(p_cpo, CPO_BUFOPT) != NULL))) |
| 6985 | should_copy = FALSE; |
| 6986 | |
| 6987 | if (should_copy || (flags & BCO_ALWAYS)) |
| 6988 | { |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 6989 | #ifdef FEAT_EVAL |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6990 | CLEAR_FIELD(buf->b_p_script_ctx); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 6991 | init_buf_opt_idx(); |
| 6992 | #endif |
| 6993 | // Don't copy the options specific to a help buffer when |
| 6994 | // BCO_NOHELP is given or the options were initialized already |
| 6995 | // (jumping back to a help file with CTRL-T or CTRL-O) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6996 | dont_do_help = ((flags & BCO_NOHELP) && buf->b_help) |
| 6997 | || buf->b_p_initialized; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 6998 | if (dont_do_help) // don't free b_p_isk |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6999 | { |
| 7000 | save_p_isk = buf->b_p_isk; |
| 7001 | buf->b_p_isk = NULL; |
| 7002 | } |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 7003 | // Always free the allocated strings. If not already initialized, |
| 7004 | // reset 'readonly' and copy 'fileformat'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7005 | if (!buf->b_p_initialized) |
| 7006 | { |
| 7007 | free_buf_options(buf, TRUE); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7008 | buf->b_p_ro = FALSE; // don't copy readonly |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7009 | buf->b_p_tx = p_tx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7010 | buf->b_p_fenc = vim_strsave(p_fenc); |
Bram Moolenaar | e8ef3a0 | 2016-10-12 17:45:29 +0200 | [diff] [blame] | 7011 | switch (*p_ffs) |
| 7012 | { |
| 7013 | case 'm': |
| 7014 | buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break; |
| 7015 | case 'd': |
| 7016 | buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break; |
| 7017 | case 'u': |
| 7018 | buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break; |
| 7019 | default: |
| 7020 | buf->b_p_ff = vim_strsave(p_ff); |
| 7021 | } |
| 7022 | if (buf->b_p_ff != NULL) |
| 7023 | buf->b_start_ffc = *buf->b_p_ff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7024 | buf->b_p_bh = empty_option; |
| 7025 | buf->b_p_bt = empty_option; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7026 | } |
| 7027 | else |
| 7028 | free_buf_options(buf, FALSE); |
| 7029 | |
| 7030 | buf->b_p_ai = p_ai; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7031 | COPY_OPT_SCTX(buf, BV_AI); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7032 | buf->b_p_ai_nopaste = p_ai_nopaste; |
| 7033 | buf->b_p_sw = p_sw; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7034 | COPY_OPT_SCTX(buf, BV_SW); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7035 | buf->b_p_tw = p_tw; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7036 | COPY_OPT_SCTX(buf, BV_TW); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7037 | buf->b_p_tw_nopaste = p_tw_nopaste; |
| 7038 | buf->b_p_tw_nobin = p_tw_nobin; |
| 7039 | buf->b_p_wm = p_wm; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7040 | COPY_OPT_SCTX(buf, BV_WM); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7041 | buf->b_p_wm_nopaste = p_wm_nopaste; |
| 7042 | buf->b_p_wm_nobin = p_wm_nobin; |
| 7043 | buf->b_p_bin = p_bin; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7044 | COPY_OPT_SCTX(buf, BV_BIN); |
Bram Moolenaar | e8bb255 | 2005-07-08 22:26:47 +0000 | [diff] [blame] | 7045 | buf->b_p_bomb = p_bomb; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7046 | COPY_OPT_SCTX(buf, BV_BOMB); |
Bram Moolenaar | b388be0 | 2015-07-22 22:19:38 +0200 | [diff] [blame] | 7047 | buf->b_p_fixeol = p_fixeol; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7048 | COPY_OPT_SCTX(buf, BV_FIXEOL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7049 | buf->b_p_et = p_et; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7050 | COPY_OPT_SCTX(buf, BV_ET); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7051 | buf->b_p_et_nobin = p_et_nobin; |
Bram Moolenaar | 54f018c | 2015-09-15 17:30:40 +0200 | [diff] [blame] | 7052 | buf->b_p_et_nopaste = p_et_nopaste; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7053 | buf->b_p_ml = p_ml; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7054 | COPY_OPT_SCTX(buf, BV_ML); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7055 | buf->b_p_ml_nobin = p_ml_nobin; |
| 7056 | buf->b_p_inf = p_inf; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7057 | COPY_OPT_SCTX(buf, BV_INF); |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 7058 | if (cmdmod.cmod_flags & CMOD_NOSWAPFILE) |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7059 | buf->b_p_swf = FALSE; |
| 7060 | else |
| 7061 | { |
| 7062 | buf->b_p_swf = p_swf; |
Bram Moolenaar | 6206877 | 2021-12-14 09:01:38 +0000 | [diff] [blame] | 7063 | COPY_OPT_SCTX(buf, BV_SWF); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7064 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7065 | buf->b_p_cpt = vim_strsave(p_cpt); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7066 | COPY_OPT_SCTX(buf, BV_CPT); |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 7067 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 7068 | buf->b_p_csl = vim_strsave(p_csl); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7069 | COPY_OPT_SCTX(buf, BV_CSL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7070 | #endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7071 | #ifdef FEAT_COMPL_FUNC |
| 7072 | buf->b_p_cfu = vim_strsave(p_cfu); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7073 | COPY_OPT_SCTX(buf, BV_CFU); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 7074 | set_buflocal_cfu_callback(buf); |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 7075 | buf->b_p_ofu = vim_strsave(p_ofu); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7076 | COPY_OPT_SCTX(buf, BV_OFU); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 7077 | set_buflocal_ofu_callback(buf); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7078 | #endif |
Bram Moolenaar | 45e18cb | 2019-04-28 18:05:35 +0200 | [diff] [blame] | 7079 | #ifdef FEAT_EVAL |
| 7080 | buf->b_p_tfu = vim_strsave(p_tfu); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7081 | COPY_OPT_SCTX(buf, BV_TFU); |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 7082 | set_buflocal_tfu_callback(buf); |
Bram Moolenaar | 45e18cb | 2019-04-28 18:05:35 +0200 | [diff] [blame] | 7083 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7084 | buf->b_p_sts = p_sts; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7085 | COPY_OPT_SCTX(buf, BV_STS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7086 | buf->b_p_sts_nopaste = p_sts_nopaste; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 7087 | #ifdef FEAT_VARTABS |
| 7088 | buf->b_p_vsts = vim_strsave(p_vsts); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7089 | COPY_OPT_SCTX(buf, BV_VSTS); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 7090 | if (p_vsts && p_vsts != empty_option) |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 7091 | (void)tabstop_set(p_vsts, &buf->b_p_vsts_array); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 7092 | else |
Bram Moolenaar | 652dee4 | 2022-01-28 20:47:49 +0000 | [diff] [blame] | 7093 | buf->b_p_vsts_array = NULL; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 7094 | buf->b_p_vsts_nopaste = p_vsts_nopaste |
| 7095 | ? vim_strsave(p_vsts_nopaste) : NULL; |
| 7096 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7097 | buf->b_p_sn = p_sn; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7098 | COPY_OPT_SCTX(buf, BV_SN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7099 | buf->b_p_com = vim_strsave(p_com); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7100 | COPY_OPT_SCTX(buf, BV_COM); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7101 | #ifdef FEAT_FOLDING |
| 7102 | buf->b_p_cms = vim_strsave(p_cms); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7103 | COPY_OPT_SCTX(buf, BV_CMS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7104 | #endif |
| 7105 | buf->b_p_fo = vim_strsave(p_fo); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7106 | COPY_OPT_SCTX(buf, BV_FO); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 7107 | buf->b_p_flp = vim_strsave(p_flp); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7108 | COPY_OPT_SCTX(buf, BV_FLP); |
Bram Moolenaar | 473952e | 2019-09-28 16:30:04 +0200 | [diff] [blame] | 7109 | // NOTE: Valgrind may report a bogus memory leak for 'nrformats' |
| 7110 | // when it is set to 8 bytes in defaults.vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7111 | buf->b_p_nf = vim_strsave(p_nf); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7112 | COPY_OPT_SCTX(buf, BV_NF); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7113 | buf->b_p_mps = vim_strsave(p_mps); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7114 | COPY_OPT_SCTX(buf, BV_MPS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7115 | buf->b_p_si = p_si; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7116 | COPY_OPT_SCTX(buf, BV_SI); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7117 | buf->b_p_ci = p_ci; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7118 | COPY_OPT_SCTX(buf, BV_CI); |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 7119 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7120 | buf->b_p_cin = p_cin; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7121 | COPY_OPT_SCTX(buf, BV_CIN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7122 | buf->b_p_cink = vim_strsave(p_cink); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7123 | COPY_OPT_SCTX(buf, BV_CINK); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7124 | buf->b_p_cino = vim_strsave(p_cino); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7125 | COPY_OPT_SCTX(buf, BV_CINO); |
Tom Praschan | 3506cf3 | 2022-04-07 12:39:08 +0100 | [diff] [blame] | 7126 | buf->b_p_cinsd = vim_strsave(p_cinsd); |
| 7127 | COPY_OPT_SCTX(buf, BV_CINSD); |
Bram Moolenaar | 49846fb | 2022-10-15 16:05:33 +0100 | [diff] [blame] | 7128 | buf->b_p_lop = vim_strsave(p_lop); |
| 7129 | COPY_OPT_SCTX(buf, BV_LOP); |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 7130 | |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7131 | // Don't copy 'filetype', it must be detected |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7132 | buf->b_p_ft = empty_option; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7133 | buf->b_p_pi = p_pi; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7134 | COPY_OPT_SCTX(buf, BV_PI); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7135 | buf->b_p_cinw = vim_strsave(p_cinw); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7136 | COPY_OPT_SCTX(buf, BV_CINW); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7137 | buf->b_p_lisp = p_lisp; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7138 | COPY_OPT_SCTX(buf, BV_LISP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7139 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7140 | // Don't copy 'syntax', it must be set |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7141 | buf->b_p_syn = empty_option; |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 7142 | buf->b_p_smc = p_smc; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7143 | COPY_OPT_SCTX(buf, BV_SMC); |
Bram Moolenaar | b8060fe | 2016-01-19 22:29:28 +0100 | [diff] [blame] | 7144 | buf->b_s.b_syn_isk = empty_option; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 7145 | #endif |
| 7146 | #ifdef FEAT_SPELL |
Bram Moolenaar | d5784f9 | 2010-10-13 14:05:35 +0200 | [diff] [blame] | 7147 | buf->b_s.b_p_spc = vim_strsave(p_spc); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7148 | COPY_OPT_SCTX(buf, BV_SPC); |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 7149 | (void)compile_cap_prog(&buf->b_s); |
| 7150 | buf->b_s.b_p_spf = vim_strsave(p_spf); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7151 | COPY_OPT_SCTX(buf, BV_SPF); |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 7152 | buf->b_s.b_p_spl = vim_strsave(p_spl); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7153 | COPY_OPT_SCTX(buf, BV_SPL); |
Bram Moolenaar | 362b44b | 2020-06-10 21:47:00 +0200 | [diff] [blame] | 7154 | buf->b_s.b_p_spo = vim_strsave(p_spo); |
| 7155 | COPY_OPT_SCTX(buf, BV_SPO); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7156 | #endif |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 7157 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7158 | buf->b_p_inde = vim_strsave(p_inde); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7159 | COPY_OPT_SCTX(buf, BV_INDE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7160 | buf->b_p_indk = vim_strsave(p_indk); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7161 | COPY_OPT_SCTX(buf, BV_INDK); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7162 | #endif |
Bram Moolenaar | 9be7c04 | 2017-01-14 14:28:30 +0100 | [diff] [blame] | 7163 | buf->b_p_fp = empty_option; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7164 | #if defined(FEAT_EVAL) |
| 7165 | buf->b_p_fex = vim_strsave(p_fex); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7166 | COPY_OPT_SCTX(buf, BV_FEX); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7167 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7168 | #ifdef FEAT_CRYPT |
| 7169 | buf->b_p_key = vim_strsave(p_key); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7170 | COPY_OPT_SCTX(buf, BV_KEY); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7171 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7172 | buf->b_p_sua = vim_strsave(p_sua); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7173 | COPY_OPT_SCTX(buf, BV_SUA); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7174 | #ifdef FEAT_KEYMAP |
| 7175 | buf->b_p_keymap = vim_strsave(p_keymap); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7176 | COPY_OPT_SCTX(buf, BV_KMAP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7177 | buf->b_kmap_state |= KEYMAP_INIT; |
| 7178 | #endif |
Bram Moolenaar | 6d150f7 | 2018-04-21 20:03:20 +0200 | [diff] [blame] | 7179 | #ifdef FEAT_TERMINAL |
| 7180 | buf->b_p_twsl = p_twsl; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7181 | COPY_OPT_SCTX(buf, BV_TWSL); |
Bram Moolenaar | 6d150f7 | 2018-04-21 20:03:20 +0200 | [diff] [blame] | 7182 | #endif |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7183 | // This isn't really an option, but copying the langmap and IME |
| 7184 | // state from the current buffer is better than resetting it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7185 | buf->b_p_iminsert = p_iminsert; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7186 | COPY_OPT_SCTX(buf, BV_IMI); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7187 | buf->b_p_imsearch = p_imsearch; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7188 | COPY_OPT_SCTX(buf, BV_IMS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7189 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7190 | // options that are normally global but also have a local value |
| 7191 | // are not copied, start using the global value |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7192 | buf->b_p_ar = -1; |
Bram Moolenaar | f5a2fd8 | 2013-11-06 05:26:15 +0100 | [diff] [blame] | 7193 | buf->b_p_ul = NO_LOCAL_UNDOLEVEL; |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 7194 | buf->b_p_bkc = empty_option; |
| 7195 | buf->b_bkc_flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7196 | #ifdef FEAT_QUICKFIX |
| 7197 | buf->b_p_gp = empty_option; |
| 7198 | buf->b_p_mp = empty_option; |
| 7199 | buf->b_p_efm = empty_option; |
| 7200 | #endif |
| 7201 | buf->b_p_ep = empty_option; |
| 7202 | buf->b_p_kp = empty_option; |
| 7203 | buf->b_p_path = empty_option; |
| 7204 | buf->b_p_tags = empty_option; |
Bram Moolenaar | 0f6562e | 2015-11-24 18:48:14 +0100 | [diff] [blame] | 7205 | buf->b_p_tc = empty_option; |
| 7206 | buf->b_tc_flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7207 | #ifdef FEAT_FIND_ID |
| 7208 | buf->b_p_def = empty_option; |
| 7209 | buf->b_p_inc = empty_option; |
| 7210 | # ifdef FEAT_EVAL |
| 7211 | buf->b_p_inex = vim_strsave(p_inex); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7212 | COPY_OPT_SCTX(buf, BV_INEX); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7213 | # endif |
| 7214 | #endif |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 7215 | buf->b_p_cot = empty_option; |
| 7216 | buf->b_cot_flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7217 | buf->b_p_dict = empty_option; |
| 7218 | buf->b_p_tsr = empty_option; |
Bram Moolenaar | f4d8b76 | 2021-10-17 14:13:09 +0100 | [diff] [blame] | 7219 | #ifdef FEAT_COMPL_FUNC |
| 7220 | buf->b_p_tsrfu = empty_option; |
| 7221 | #endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7222 | buf->b_p_qe = vim_strsave(p_qe); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7223 | COPY_OPT_SCTX(buf, BV_QE); |
Bram Moolenaar | 9b2200a | 2006-03-20 21:55:45 +0000 | [diff] [blame] | 7224 | #if defined(FEAT_BEVAL) && defined(FEAT_EVAL) |
| 7225 | buf->b_p_bexpr = empty_option; |
| 7226 | #endif |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 7227 | #if defined(FEAT_CRYPT) |
| 7228 | buf->b_p_cm = empty_option; |
| 7229 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 7230 | #ifdef FEAT_PERSISTENT_UNDO |
| 7231 | buf->b_p_udf = p_udf; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7232 | COPY_OPT_SCTX(buf, BV_UDF); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 7233 | #endif |
Bram Moolenaar | af6c131 | 2014-03-12 18:55:58 +0100 | [diff] [blame] | 7234 | buf->b_p_lw = empty_option; |
Bram Moolenaar | 2c7292d | 2017-03-05 17:43:31 +0100 | [diff] [blame] | 7235 | buf->b_p_menc = empty_option; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7236 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 7237 | // Don't copy the options set by ex_help(), use the saved values, |
| 7238 | // when going from a help buffer to a non-help buffer. |
| 7239 | // Don't touch these at all when BCO_NOHELP is used and going from |
| 7240 | // or to a help buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7241 | if (dont_do_help) |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 7242 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7243 | buf->b_p_isk = save_p_isk; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 7244 | #ifdef FEAT_VARTABS |
Yegappan Lakshmanan | 960dcbd | 2023-03-07 17:45:11 +0000 | [diff] [blame] | 7245 | if (p_vts && *p_vts != NUL && !buf->b_p_vts_array) |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 7246 | (void)tabstop_set(p_vts, &buf->b_p_vts_array); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 7247 | else |
| 7248 | buf->b_p_vts_array = NULL; |
| 7249 | #endif |
| 7250 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7251 | else |
| 7252 | { |
| 7253 | buf->b_p_isk = vim_strsave(p_isk); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7254 | COPY_OPT_SCTX(buf, BV_ISK); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7255 | did_isk = TRUE; |
| 7256 | buf->b_p_ts = p_ts; |
Bram Moolenaar | 6206877 | 2021-12-14 09:01:38 +0000 | [diff] [blame] | 7257 | COPY_OPT_SCTX(buf, BV_TS); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 7258 | #ifdef FEAT_VARTABS |
| 7259 | buf->b_p_vts = vim_strsave(p_vts); |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7260 | COPY_OPT_SCTX(buf, BV_VTS); |
Yegappan Lakshmanan | 960dcbd | 2023-03-07 17:45:11 +0000 | [diff] [blame] | 7261 | if (p_vts && *p_vts != NUL && !buf->b_p_vts_array) |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 7262 | (void)tabstop_set(p_vts, &buf->b_p_vts_array); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 7263 | else |
| 7264 | buf->b_p_vts_array = NULL; |
| 7265 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7266 | buf->b_help = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7267 | if (buf->b_p_bt[0] == 'h') |
| 7268 | clear_string_option(&buf->b_p_bt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7269 | buf->b_p_ma = p_ma; |
Bram Moolenaar | cfb3814 | 2019-10-19 20:18:47 +0200 | [diff] [blame] | 7270 | COPY_OPT_SCTX(buf, BV_MA); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7271 | } |
| 7272 | } |
| 7273 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 7274 | // When the options should be copied (ignoring BCO_ALWAYS), set the |
| 7275 | // flag that indicates that the options have been initialized. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7276 | if (should_copy) |
| 7277 | buf->b_p_initialized = TRUE; |
| 7278 | } |
| 7279 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7280 | check_buf_options(buf); // make sure we don't have NULLs |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7281 | if (did_isk) |
| 7282 | (void)buf_init_chartab(buf, FALSE); |
| 7283 | } |
| 7284 | |
| 7285 | /* |
| 7286 | * Reset the 'modifiable' option and its default value. |
| 7287 | */ |
| 7288 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7289 | reset_modifiable(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7290 | { |
| 7291 | int opt_idx; |
| 7292 | |
| 7293 | curbuf->b_p_ma = FALSE; |
| 7294 | p_ma = FALSE; |
| 7295 | opt_idx = findoption((char_u *)"ma"); |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 7296 | if (opt_idx >= 0) |
| 7297 | options[opt_idx].def_val[VI_DEFAULT] = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7298 | } |
| 7299 | |
| 7300 | /* |
| 7301 | * Set the global value for 'iminsert' to the local value. |
| 7302 | */ |
| 7303 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7304 | set_iminsert_global(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7305 | { |
| 7306 | p_iminsert = curbuf->b_p_iminsert; |
| 7307 | } |
| 7308 | |
| 7309 | /* |
| 7310 | * Set the global value for 'imsearch' to the local value. |
| 7311 | */ |
| 7312 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7313 | set_imsearch_global(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7314 | { |
| 7315 | p_imsearch = curbuf->b_p_imsearch; |
| 7316 | } |
| 7317 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7318 | static int expand_option_idx = -1; |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7319 | static int expand_option_start_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7320 | static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL}; |
| 7321 | static int expand_option_flags = 0; |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7322 | static int expand_option_append = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7323 | |
| 7324 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7325 | set_context_in_set_cmd( |
| 7326 | expand_T *xp, |
| 7327 | char_u *arg, |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7328 | int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7329 | { |
| 7330 | int nextchar; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7331 | long_u flags = 0; // init for GCC |
| 7332 | int opt_idx = 0; // init for GCC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7333 | char_u *p; |
| 7334 | char_u *s; |
| 7335 | int is_term_option = FALSE; |
| 7336 | int key; |
| 7337 | |
| 7338 | expand_option_flags = opt_flags; |
| 7339 | |
| 7340 | xp->xp_context = EXPAND_SETTINGS; |
| 7341 | if (*arg == NUL) |
| 7342 | { |
| 7343 | xp->xp_pattern = arg; |
| 7344 | return; |
| 7345 | } |
| 7346 | p = arg + STRLEN(arg) - 1; |
| 7347 | if (*p == ' ' && *(p - 1) != '\\') |
| 7348 | { |
| 7349 | xp->xp_pattern = p + 1; |
| 7350 | return; |
| 7351 | } |
| 7352 | while (p > arg) |
| 7353 | { |
| 7354 | s = p; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7355 | // count number of backslashes before ' ' or ',' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7356 | if (*p == ' ' || *p == ',') |
| 7357 | { |
| 7358 | while (s > arg && *(s - 1) == '\\') |
| 7359 | --s; |
| 7360 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7361 | // break at a space with an even number of backslashes |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7362 | if (*p == ' ' && ((p - s) & 1) == 0) |
| 7363 | { |
| 7364 | ++p; |
| 7365 | break; |
| 7366 | } |
| 7367 | --p; |
| 7368 | } |
Bram Moolenaar | 2a7b9ee | 2009-06-16 15:50:33 +0000 | [diff] [blame] | 7369 | if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7370 | { |
| 7371 | xp->xp_context = EXPAND_BOOL_SETTINGS; |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 7372 | xp->xp_prefix = XP_PREFIX_NO; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7373 | p += 2; |
| 7374 | } |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 7375 | else if (STRNCMP(p, "inv", 3) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7376 | { |
| 7377 | xp->xp_context = EXPAND_BOOL_SETTINGS; |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 7378 | xp->xp_prefix = XP_PREFIX_INV; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7379 | p += 3; |
| 7380 | } |
| 7381 | xp->xp_pattern = arg = p; |
| 7382 | if (*arg == '<') |
| 7383 | { |
| 7384 | while (*p != '>') |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7385 | if (*p++ == NUL) // expand terminal option name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7386 | return; |
| 7387 | key = get_special_key_code(arg + 1); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7388 | if (key == 0) // unknown name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7389 | { |
| 7390 | xp->xp_context = EXPAND_NOTHING; |
| 7391 | return; |
| 7392 | } |
| 7393 | nextchar = *++p; |
| 7394 | is_term_option = TRUE; |
| 7395 | expand_option_name[2] = KEY2TERMCAP0(key); |
| 7396 | expand_option_name[3] = KEY2TERMCAP1(key); |
| 7397 | } |
| 7398 | else |
| 7399 | { |
| 7400 | if (p[0] == 't' && p[1] == '_') |
| 7401 | { |
| 7402 | p += 2; |
| 7403 | if (*p != NUL) |
| 7404 | ++p; |
| 7405 | if (*p == NUL) |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7406 | return; // expand option name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7407 | nextchar = *++p; |
| 7408 | is_term_option = TRUE; |
| 7409 | expand_option_name[2] = p[-2]; |
| 7410 | expand_option_name[3] = p[-1]; |
| 7411 | } |
| 7412 | else |
| 7413 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7414 | // Allow * wildcard |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7415 | while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*') |
| 7416 | p++; |
| 7417 | if (*p == NUL) |
| 7418 | return; |
| 7419 | nextchar = *p; |
| 7420 | *p = NUL; |
| 7421 | opt_idx = findoption(arg); |
| 7422 | *p = nextchar; |
| 7423 | if (opt_idx == -1 || options[opt_idx].var == NULL) |
| 7424 | { |
| 7425 | xp->xp_context = EXPAND_NOTHING; |
| 7426 | return; |
| 7427 | } |
| 7428 | flags = options[opt_idx].flags; |
| 7429 | if (flags & P_BOOL) |
| 7430 | { |
| 7431 | xp->xp_context = EXPAND_NOTHING; |
| 7432 | return; |
| 7433 | } |
| 7434 | } |
| 7435 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7436 | // handle "-=" and "+=" |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7437 | expand_option_append = FALSE; |
| 7438 | int expand_option_subtract = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7439 | if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=') |
| 7440 | { |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7441 | if (nextchar == '-') |
| 7442 | expand_option_subtract = TRUE; |
| 7443 | if (nextchar == '+' || nextchar == '^') |
| 7444 | expand_option_append = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7445 | ++p; |
| 7446 | nextchar = '='; |
| 7447 | } |
| 7448 | if ((nextchar != '=' && nextchar != ':') |
| 7449 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 7450 | { |
| 7451 | xp->xp_context = EXPAND_UNSUCCESSFUL; |
| 7452 | return; |
| 7453 | } |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7454 | |
| 7455 | // Below are for handling expanding a specific option's value after the '=' |
| 7456 | // or ':' |
| 7457 | |
| 7458 | if (is_term_option) |
| 7459 | expand_option_idx = -1; |
| 7460 | else |
| 7461 | expand_option_idx = opt_idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7462 | |
Yee Cheng Chin | 6ee7b52 | 2023-10-01 09:13:22 +0200 | [diff] [blame] | 7463 | if (!is_term_option) |
| 7464 | { |
| 7465 | if (options[opt_idx].flags & P_NO_CMD_EXPAND) |
| 7466 | { |
| 7467 | xp->xp_context=EXPAND_UNSUCCESSFUL; |
| 7468 | return; |
| 7469 | } |
| 7470 | } |
| 7471 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7472 | xp->xp_pattern = p + 1; |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7473 | expand_option_start_col = (int)(p + 1 - xp->xp_line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7474 | |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7475 | // Certain options currently have special case handling to reuse the |
| 7476 | // expansion logic with other commands. |
Doug Kearns | 6dfdff3 | 2023-08-27 18:48:51 +0200 | [diff] [blame] | 7477 | #ifdef FEAT_SYN_HL |
| 7478 | if (options[opt_idx].var == (char_u *)&p_syn) |
| 7479 | { |
| 7480 | xp->xp_context = EXPAND_OWNSYNTAX; |
| 7481 | return; |
| 7482 | } |
| 7483 | #endif |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7484 | if (options[opt_idx].var == (char_u *)&p_ft) |
| 7485 | { |
| 7486 | xp->xp_context = EXPAND_FILETYPE; |
| 7487 | return; |
| 7488 | } |
Doug Kearns | 81642d9 | 2024-01-04 22:37:44 +0100 | [diff] [blame] | 7489 | #ifdef FEAT_KEYMAP |
| 7490 | if (options[opt_idx].var == (char_u *)&p_keymap) |
| 7491 | { |
| 7492 | xp->xp_context = EXPAND_KEYMAP; |
| 7493 | return; |
| 7494 | } |
| 7495 | #endif |
Doug Kearns | 6dfdff3 | 2023-08-27 18:48:51 +0200 | [diff] [blame] | 7496 | |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7497 | // Now pick. If the option has a custom expander, use that. Otherwise, just |
| 7498 | // fill with the existing option value. |
| 7499 | if (expand_option_subtract) |
| 7500 | { |
| 7501 | xp->xp_context = EXPAND_SETTING_SUBTRACT; |
| 7502 | return; |
| 7503 | } |
| 7504 | else if (expand_option_idx >= 0 && |
| 7505 | options[expand_option_idx].opt_expand_cb != NULL) |
| 7506 | { |
| 7507 | xp->xp_context = EXPAND_STRING_SETTING; |
| 7508 | } |
| 7509 | else if (*xp->xp_pattern == NUL) |
| 7510 | { |
| 7511 | xp->xp_context = EXPAND_OLD_SETTING; |
| 7512 | return; |
| 7513 | } |
| 7514 | else |
| 7515 | xp->xp_context = EXPAND_NOTHING; |
| 7516 | |
| 7517 | if (is_term_option || (flags & P_NUM)) |
| 7518 | return; |
| 7519 | |
| 7520 | // Only string options below |
| 7521 | |
| 7522 | // Options that have P_EXPAND are considered to all use file/dir expansion. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7523 | if (flags & P_EXPAND) |
| 7524 | { |
| 7525 | p = options[opt_idx].var; |
| 7526 | if (p == (char_u *)&p_bdir |
| 7527 | || p == (char_u *)&p_dir |
| 7528 | || p == (char_u *)&p_path |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 7529 | || p == (char_u *)&p_pp |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7530 | || p == (char_u *)&p_rtp |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7531 | || p == (char_u *)&p_cdpath |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7532 | #ifdef FEAT_SESSION |
| 7533 | || p == (char_u *)&p_vdir |
| 7534 | #endif |
| 7535 | ) |
| 7536 | { |
| 7537 | xp->xp_context = EXPAND_DIRECTORIES; |
Bram Moolenaar | f80f40a | 2022-08-25 16:02:23 +0100 | [diff] [blame] | 7538 | if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7539 | xp->xp_backslash = XP_BS_THREE; |
| 7540 | else |
| 7541 | xp->xp_backslash = XP_BS_ONE; |
| 7542 | } |
| 7543 | else |
| 7544 | { |
| 7545 | xp->xp_context = EXPAND_FILES; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7546 | // for 'tags' need three backslashes for a space |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7547 | if (p == (char_u *)&p_tags) |
| 7548 | xp->xp_backslash = XP_BS_THREE; |
| 7549 | else |
| 7550 | xp->xp_backslash = XP_BS_ONE; |
| 7551 | } |
Yee Cheng Chin | 5484485 | 2023-10-09 18:12:31 +0200 | [diff] [blame] | 7552 | if (flags & P_COMMA) |
| 7553 | xp->xp_backslash |= XP_BS_COMMA; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7554 | } |
| 7555 | |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7556 | // For an option that is a list of file names, or comma/colon-separated |
| 7557 | // values, split it by the delimiter and find the start of the current |
| 7558 | // pattern, while accounting for backslash-escaped space/commas/colons. |
| 7559 | // Triple-backslashed escaped file names (e.g. 'path') can also be |
| 7560 | // delimited by space. |
| 7561 | if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7562 | { |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7563 | for (p = arg + STRLEN(arg) - 1; p >= xp->xp_pattern; --p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7564 | { |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7565 | // count number of backslashes before ' ' or ',' or ':' |
| 7566 | if (*p == ' ' || *p == ',' || |
| 7567 | (*p == ':' && (flags & P_COLON))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7568 | { |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7569 | s = p; |
| 7570 | while (s > xp->xp_pattern && *(s - 1) == '\\') |
| 7571 | --s; |
Yee Cheng Chin | 5484485 | 2023-10-09 18:12:31 +0200 | [diff] [blame] | 7572 | if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3)) |
| 7573 | #if defined(BACKSLASH_IN_FILENAME) |
| 7574 | || (*p == ',' && (flags & P_COMMA) && (p - s) < 1) |
| 7575 | #else |
| 7576 | || (*p == ',' && (flags & P_COMMA) && (p - s) < 2) |
| 7577 | #endif |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7578 | || (*p == ':' && (flags & P_COLON))) |
| 7579 | { |
| 7580 | xp->xp_pattern = p + 1; |
| 7581 | break; |
| 7582 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7583 | } |
| 7584 | } |
| 7585 | } |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7586 | |
| 7587 | // An option that is a list of single-character flags should always start |
| 7588 | // at the end as we don't complete words. |
| 7589 | if (flags & P_FLAGLIST) |
| 7590 | xp->xp_pattern = arg + STRLEN(arg); |
| 7591 | |
| 7592 | // Some options can either be using file/dir expansions, or custom value |
| 7593 | // expansion depending on what the user typed. Unfortunately we have to |
| 7594 | // manually handle it here to make sure we have the correct xp_context set. |
| 7595 | #ifdef FEAT_SPELL |
| 7596 | if (options[opt_idx].var == (char_u *)&p_sps) |
| 7597 | { |
| 7598 | if (STRNCMP(xp->xp_pattern, "file:", 5) == 0) |
| 7599 | { |
| 7600 | xp->xp_pattern += 5; |
| 7601 | return; |
| 7602 | } |
| 7603 | else if (options[expand_option_idx].opt_expand_cb != NULL) |
| 7604 | { |
| 7605 | xp->xp_context = EXPAND_STRING_SETTING; |
| 7606 | } |
| 7607 | } |
| 7608 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7609 | } |
| 7610 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7611 | /* |
| 7612 | * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'. |
| 7613 | * |
| 7614 | * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular |
| 7615 | * expression 'regmatch', then returns TRUE. Otherwise returns FALSE. |
| 7616 | * |
| 7617 | * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the |
| 7618 | * regular expression 'regmatch', then stores the match in matches[idx] and |
| 7619 | * returns TRUE. |
| 7620 | * |
| 7621 | * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches |
| 7622 | * 'fuzzystr', then returns TRUE. Otherwise returns FALSE. |
| 7623 | * |
| 7624 | * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches |
| 7625 | * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE. |
| 7626 | */ |
Yee Cheng Chin | f7f746b | 2023-09-30 12:28:50 +0200 | [diff] [blame] | 7627 | static int |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7628 | match_str( |
| 7629 | char_u *str, |
| 7630 | regmatch_T *regmatch, |
| 7631 | char_u **matches, |
| 7632 | int idx, |
| 7633 | int test_only, |
| 7634 | int fuzzy, |
| 7635 | char_u *fuzzystr, |
| 7636 | fuzmatch_str_T *fuzmatch) |
| 7637 | { |
| 7638 | if (!fuzzy) |
| 7639 | { |
| 7640 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 7641 | { |
| 7642 | if (!test_only) |
| 7643 | matches[idx] = vim_strsave(str); |
| 7644 | return TRUE; |
| 7645 | } |
| 7646 | } |
| 7647 | else |
| 7648 | { |
| 7649 | int score; |
| 7650 | |
| 7651 | score = fuzzy_match_str(str, fuzzystr); |
| 7652 | if (score != 0) |
| 7653 | { |
| 7654 | if (!test_only) |
| 7655 | { |
| 7656 | fuzmatch[idx].idx = idx; |
| 7657 | fuzmatch[idx].str = vim_strsave(str); |
| 7658 | fuzmatch[idx].score = score; |
| 7659 | } |
| 7660 | |
| 7661 | return TRUE; |
| 7662 | } |
| 7663 | } |
| 7664 | |
| 7665 | return FALSE; |
| 7666 | } |
| 7667 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7668 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7669 | ExpandSettings( |
| 7670 | expand_T *xp, |
| 7671 | regmatch_T *regmatch, |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7672 | char_u *fuzzystr, |
| 7673 | int *numMatches, |
Christian Brabandt | cb74789 | 2022-05-08 21:10:56 +0100 | [diff] [blame] | 7674 | char_u ***matches, |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 7675 | int can_fuzzy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7676 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7677 | int num_normal = 0; // Nr of matching non-term-code settings |
| 7678 | int num_term = 0; // Nr of matching terminal code settings |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7679 | int opt_idx; |
| 7680 | int match; |
| 7681 | int count = 0; |
| 7682 | char_u *str; |
| 7683 | int loop; |
| 7684 | int is_term_opt; |
| 7685 | char_u name_buf[MAX_KEY_NAME_LEN]; |
| 7686 | static char *(names[]) = {"all", "termcap"}; |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7687 | int ic = regmatch->rm_ic; // remember the ignore-case flag |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7688 | int fuzzy; |
| 7689 | fuzmatch_str_T *fuzmatch = NULL; |
| 7690 | |
Christian Brabandt | cb74789 | 2022-05-08 21:10:56 +0100 | [diff] [blame] | 7691 | fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7692 | |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7693 | // do this loop twice: |
| 7694 | // loop == 0: count the number of matching options |
| 7695 | // loop == 1: copy the matching options into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7696 | for (loop = 0; loop <= 1; ++loop) |
| 7697 | { |
| 7698 | regmatch->rm_ic = ic; |
| 7699 | if (xp->xp_context != EXPAND_BOOL_SETTINGS) |
| 7700 | { |
K.Takata | eeec254 | 2021-06-02 13:28:16 +0200 | [diff] [blame] | 7701 | for (match = 0; match < (int)ARRAY_LENGTH(names); ++match) |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7702 | { |
| 7703 | if (match_str((char_u *)names[match], regmatch, *matches, |
| 7704 | count, (loop == 0), fuzzy, fuzzystr, fuzmatch)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7705 | { |
| 7706 | if (loop == 0) |
| 7707 | num_normal++; |
| 7708 | else |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7709 | count++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7710 | } |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7711 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7712 | } |
| 7713 | for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL; |
| 7714 | opt_idx++) |
| 7715 | { |
| 7716 | if (options[opt_idx].var == NULL) |
| 7717 | continue; |
| 7718 | if (xp->xp_context == EXPAND_BOOL_SETTINGS |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 7719 | && !(options[opt_idx].flags & P_BOOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7720 | continue; |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 7721 | is_term_opt = istermoption_idx(opt_idx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7722 | if (is_term_opt && num_normal > 0) |
| 7723 | continue; |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7724 | |
| 7725 | if (match_str(str, regmatch, *matches, count, (loop == 0), |
| 7726 | fuzzy, fuzzystr, fuzmatch)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7727 | { |
| 7728 | if (loop == 0) |
| 7729 | { |
| 7730 | if (is_term_opt) |
| 7731 | num_term++; |
| 7732 | else |
| 7733 | num_normal++; |
| 7734 | } |
| 7735 | else |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7736 | count++; |
| 7737 | } |
| 7738 | else if (!fuzzy && options[opt_idx].shortname != NULL |
| 7739 | && vim_regexec(regmatch, |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 7740 | (char_u *)options[opt_idx].shortname, (colnr_T)0)) |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7741 | { |
| 7742 | // Compare against the abbreviated option name (for regular |
| 7743 | // expression match). Fuzzy matching (previous if) already |
| 7744 | // matches against both the expanded and abbreviated names. |
| 7745 | if (loop == 0) |
| 7746 | { |
| 7747 | if (is_term_opt) |
| 7748 | num_term++; |
| 7749 | else |
| 7750 | num_normal++; |
| 7751 | } |
| 7752 | else |
| 7753 | (*matches)[count++] = vim_strsave(str); |
| 7754 | } |
| 7755 | else if (is_term_opt) |
| 7756 | { |
| 7757 | name_buf[0] = '<'; |
| 7758 | name_buf[1] = 't'; |
| 7759 | name_buf[2] = '_'; |
| 7760 | name_buf[3] = str[2]; |
| 7761 | name_buf[4] = str[3]; |
| 7762 | name_buf[5] = '>'; |
| 7763 | name_buf[6] = NUL; |
| 7764 | |
| 7765 | if (match_str(name_buf, regmatch, *matches, count, (loop == 0), |
| 7766 | fuzzy, fuzzystr, fuzmatch)) |
| 7767 | { |
| 7768 | if (loop == 0) |
| 7769 | num_term++; |
| 7770 | else |
| 7771 | count++; |
| 7772 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7773 | } |
| 7774 | } |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7775 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 7776 | // Check terminal key codes, these are not in the option table |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7777 | if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0) |
| 7778 | { |
| 7779 | for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++) |
| 7780 | { |
Keith Thompson | 184f71c | 2024-01-04 21:19:04 +0100 | [diff] [blame] | 7781 | if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7782 | continue; |
| 7783 | |
| 7784 | name_buf[0] = 't'; |
| 7785 | name_buf[1] = '_'; |
| 7786 | name_buf[2] = str[0]; |
| 7787 | name_buf[3] = str[1]; |
| 7788 | name_buf[4] = NUL; |
| 7789 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7790 | if (match_str(name_buf, regmatch, *matches, count, |
Bram Moolenaar | 048d9d2 | 2023-05-06 22:21:11 +0100 | [diff] [blame] | 7791 | (loop == 0), fuzzy, fuzzystr, fuzmatch)) |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7792 | { |
| 7793 | if (loop == 0) |
| 7794 | num_term++; |
| 7795 | else |
| 7796 | count++; |
| 7797 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7798 | else |
| 7799 | { |
| 7800 | name_buf[0] = '<'; |
| 7801 | name_buf[1] = 't'; |
| 7802 | name_buf[2] = '_'; |
| 7803 | name_buf[3] = str[0]; |
| 7804 | name_buf[4] = str[1]; |
| 7805 | name_buf[5] = '>'; |
| 7806 | name_buf[6] = NUL; |
| 7807 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7808 | if (match_str(name_buf, regmatch, *matches, count, |
| 7809 | (loop == 0), fuzzy, fuzzystr, |
| 7810 | fuzmatch)) |
| 7811 | { |
| 7812 | if (loop == 0) |
| 7813 | num_term++; |
| 7814 | else |
| 7815 | count++; |
| 7816 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7817 | } |
| 7818 | } |
| 7819 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 7820 | // Check special key names. |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7821 | regmatch->rm_ic = TRUE; // ignore case here |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7822 | for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++) |
| 7823 | { |
| 7824 | name_buf[0] = '<'; |
| 7825 | STRCPY(name_buf + 1, str); |
| 7826 | STRCAT(name_buf, ">"); |
| 7827 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7828 | if (match_str(name_buf, regmatch, *matches, count, (loop == 0), |
| 7829 | fuzzy, fuzzystr, fuzmatch)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7830 | { |
| 7831 | if (loop == 0) |
| 7832 | num_term++; |
| 7833 | else |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7834 | count++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7835 | } |
| 7836 | } |
| 7837 | } |
| 7838 | if (loop == 0) |
| 7839 | { |
| 7840 | if (num_normal > 0) |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7841 | *numMatches = num_normal; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7842 | else if (num_term > 0) |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7843 | *numMatches = num_term; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7844 | else |
| 7845 | return OK; |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7846 | if (!fuzzy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7847 | { |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7848 | *matches = ALLOC_MULT(char_u *, *numMatches); |
| 7849 | if (*matches == NULL) |
| 7850 | { |
| 7851 | *matches = (char_u **)""; |
| 7852 | return FAIL; |
| 7853 | } |
| 7854 | } |
| 7855 | else |
| 7856 | { |
| 7857 | fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches); |
| 7858 | if (fuzmatch == NULL) |
| 7859 | { |
| 7860 | *matches = (char_u **)""; |
| 7861 | return FAIL; |
| 7862 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7863 | } |
| 7864 | } |
| 7865 | } |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 7866 | |
| 7867 | if (fuzzy && |
| 7868 | fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL) |
| 7869 | return FAIL; |
| 7870 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7871 | return OK; |
| 7872 | } |
| 7873 | |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7874 | // Escape an option value that can be used on the command-line with :set. |
| 7875 | // Caller needs to free the returned string, unless NULL is returned. |
| 7876 | static char_u* |
| 7877 | escape_option_str_cmdline(char_u *var) |
| 7878 | { |
| 7879 | char_u *buf; |
| 7880 | |
| 7881 | // A backslash is required before some characters. This is the reverse of |
| 7882 | // what happens in do_set(). |
| 7883 | buf = vim_strsave_escaped(var, escape_chars); |
| 7884 | if (buf == NULL) |
| 7885 | return NULL; |
| 7886 | |
| 7887 | #ifdef BACKSLASH_IN_FILENAME |
| 7888 | // For MS-Windows et al. we don't double backslashes at the start and |
| 7889 | // before a file name character. |
| 7890 | // The reverse is found at stropt_copy_value(). |
| 7891 | for (var = buf; *var != NUL; MB_PTR_ADV(var)) |
| 7892 | if (var[0] == '\\' && var[1] == '\\' |
| 7893 | && expand_option_idx >= 0 |
| 7894 | && (options[expand_option_idx].flags & P_EXPAND) |
| 7895 | && vim_isfilec(var[2]) |
| 7896 | && (var[2] != '\\' || (var == buf && var[4] != '\\'))) |
| 7897 | STRMOVE(var, var + 1); |
| 7898 | #endif |
| 7899 | return buf; |
| 7900 | } |
| 7901 | |
| 7902 | /* |
| 7903 | * Expansion handler for :set= when we just want to fill in with the existing value. |
| 7904 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7905 | int |
zeertzjq | b0d45ec | 2023-01-25 15:04:22 +0000 | [diff] [blame] | 7906 | ExpandOldSetting(int *numMatches, char_u ***matches) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7907 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7908 | char_u *var = NULL; // init for GCC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7909 | char_u *buf; |
| 7910 | |
zeertzjq | b0d45ec | 2023-01-25 15:04:22 +0000 | [diff] [blame] | 7911 | *numMatches = 0; |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7912 | *matches = ALLOC_MULT(char_u *, 1); |
zeertzjq | b0d45ec | 2023-01-25 15:04:22 +0000 | [diff] [blame] | 7913 | if (*matches == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7914 | return FAIL; |
| 7915 | |
Yegappan Lakshmanan | c6ff21e | 2023-03-02 14:46:48 +0000 | [diff] [blame] | 7916 | // For a terminal key code expand_option_idx is < 0. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7917 | if (expand_option_idx < 0) |
| 7918 | { |
| 7919 | var = find_termcode(expand_option_name + 2); |
| 7920 | if (var == NULL) |
| 7921 | expand_option_idx = findoption(expand_option_name); |
| 7922 | } |
| 7923 | |
| 7924 | if (expand_option_idx >= 0) |
| 7925 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 7926 | // put string of option value in NameBuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7927 | option_value2string(&options[expand_option_idx], expand_option_flags); |
| 7928 | var = NameBuff; |
| 7929 | } |
| 7930 | else if (var == NULL) |
| 7931 | var = (char_u *)""; |
| 7932 | |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7933 | buf = escape_option_str_cmdline(var); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7934 | if (buf == NULL) |
| 7935 | { |
zeertzjq | b0d45ec | 2023-01-25 15:04:22 +0000 | [diff] [blame] | 7936 | VIM_CLEAR(*matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7937 | return FAIL; |
| 7938 | } |
| 7939 | |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7940 | (*matches)[0] = buf; |
zeertzjq | b0d45ec | 2023-01-25 15:04:22 +0000 | [diff] [blame] | 7941 | *numMatches = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7942 | return OK; |
| 7943 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7944 | |
| 7945 | /* |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 7946 | * Expansion handler for :set=/:set+= when the option has a custom expansion handler. |
| 7947 | */ |
| 7948 | int |
| 7949 | ExpandStringSetting( |
| 7950 | expand_T *xp, |
| 7951 | regmatch_T *regmatch, |
| 7952 | int *numMatches, |
| 7953 | char_u ***matches) |
| 7954 | { |
| 7955 | char_u *var = NULL; // init for GCC |
| 7956 | char_u *buf; |
| 7957 | |
| 7958 | if (expand_option_idx < 0 || |
| 7959 | options[expand_option_idx].opt_expand_cb == NULL) |
| 7960 | { |
| 7961 | // Not supposed to reach this. This function is only for options with |
| 7962 | // custom expansion callbacks. |
| 7963 | return FAIL; |
| 7964 | } |
| 7965 | |
| 7966 | optexpand_T args; |
| 7967 | args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags); |
| 7968 | args.oe_append = expand_option_append; |
| 7969 | args.oe_regmatch = regmatch; |
| 7970 | args.oe_xp = xp; |
| 7971 | args.oe_set_arg = xp->xp_line + expand_option_start_col; |
| 7972 | args.oe_include_orig_val = |
| 7973 | !expand_option_append && |
| 7974 | (*args.oe_set_arg == NUL); |
| 7975 | |
| 7976 | // Retrieve the existing value, but escape it as a reverse of setting it. |
| 7977 | // We technically only need to do this when oe_append or |
| 7978 | // oe_include_orig_val is true. |
| 7979 | option_value2string(&options[expand_option_idx], expand_option_flags); |
| 7980 | var = NameBuff; |
| 7981 | buf = escape_option_str_cmdline(var); |
| 7982 | if (buf == NULL) |
| 7983 | return FAIL; |
| 7984 | |
| 7985 | args.oe_opt_value = buf; |
| 7986 | |
| 7987 | int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches); |
| 7988 | |
| 7989 | vim_free(buf); |
| 7990 | return num_ret; |
| 7991 | } |
| 7992 | |
| 7993 | /* |
| 7994 | * Expansion handler for :set-= |
| 7995 | */ |
| 7996 | int |
| 7997 | ExpandSettingSubtract( |
| 7998 | expand_T *xp, |
| 7999 | regmatch_T *regmatch, |
| 8000 | int *numMatches, |
| 8001 | char_u ***matches) |
| 8002 | { |
| 8003 | if (expand_option_idx < 0) |
| 8004 | // term option |
| 8005 | return ExpandOldSetting(numMatches, matches); |
| 8006 | |
| 8007 | char_u *option_val = *(char_u**)get_option_varp_scope( |
| 8008 | expand_option_idx, expand_option_flags); |
| 8009 | |
| 8010 | long_u option_flags = options[expand_option_idx].flags; |
| 8011 | |
| 8012 | if (option_flags & P_NUM) |
| 8013 | return ExpandOldSetting(numMatches, matches); |
| 8014 | else if (option_flags & P_COMMA) |
| 8015 | { |
| 8016 | // Split the option by comma, then present each option to the user if |
| 8017 | // it matches the pattern. |
| 8018 | // This condition needs to go first, because 'whichwrap' has both |
| 8019 | // P_COMMA and P_FLAGLIST. |
| 8020 | garray_T ga; |
| 8021 | |
| 8022 | char_u *item; |
| 8023 | char_u *option_copy; |
| 8024 | char_u *next_val; |
| 8025 | char_u *comma; |
| 8026 | |
| 8027 | if (*option_val == NUL) |
| 8028 | return FAIL; |
| 8029 | |
| 8030 | // Make a copy as we need to inject null characters destructively. |
| 8031 | option_copy = vim_strsave(option_val); |
| 8032 | if (option_copy == NULL) |
| 8033 | return FAIL; |
| 8034 | next_val = option_copy; |
| 8035 | |
| 8036 | ga_init2(&ga, sizeof(char_u *), 10); |
| 8037 | |
| 8038 | do |
| 8039 | { |
| 8040 | item = next_val; |
| 8041 | comma = vim_strchr(next_val, ','); |
| 8042 | while (comma != NULL && comma != next_val && *(comma - 1) == '\\') |
| 8043 | { |
| 8044 | // "\," is interpreted as a literal comma rather than option |
| 8045 | // separator when reading options in copy_option_part(). Skip |
| 8046 | // it. |
| 8047 | comma = vim_strchr(comma + 1, ','); |
| 8048 | } |
| 8049 | if (comma != NULL) |
| 8050 | { |
| 8051 | *comma = NUL; // null-terminate this value, required by later functions |
| 8052 | next_val = comma + 1; |
| 8053 | } |
| 8054 | else |
| 8055 | next_val = NULL; |
| 8056 | |
| 8057 | if (*item == NUL) |
| 8058 | // empty value, don't add to list |
| 8059 | continue; |
| 8060 | |
| 8061 | if (!vim_regexec(regmatch, item, (colnr_T)0)) |
| 8062 | continue; |
| 8063 | |
| 8064 | char_u *buf = escape_option_str_cmdline(item); |
| 8065 | if (buf == NULL) |
| 8066 | { |
| 8067 | vim_free(option_copy); |
| 8068 | ga_clear_strings(&ga); |
| 8069 | return FAIL; |
| 8070 | } |
| 8071 | if (ga_add_string(&ga, buf) != OK) |
| 8072 | { |
| 8073 | vim_free(buf); |
| 8074 | break; |
| 8075 | } |
| 8076 | } while (next_val != NULL); |
| 8077 | |
| 8078 | vim_free(option_copy); |
| 8079 | |
| 8080 | *matches = ga.ga_data; |
| 8081 | *numMatches = ga.ga_len; |
| 8082 | return OK; |
| 8083 | } |
| 8084 | else if (option_flags & P_FLAGLIST) |
| 8085 | { |
| 8086 | // Only present the flags that are set on the option as the other flags |
| 8087 | // are not meaningful to do set-= on. |
| 8088 | |
| 8089 | if (*xp->xp_pattern != NUL) |
| 8090 | { |
| 8091 | // Don't suggest anything if cmdline is non-empty. Vim's set-= |
| 8092 | // behavior requires consecutive strings and it's usually |
Viktor Szépe | dbf749b | 2023-10-16 09:53:37 +0200 | [diff] [blame] | 8093 | // unintuitive to users if they try to subtract multiple flags at |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 8094 | // once. |
| 8095 | return FAIL; |
| 8096 | } |
| 8097 | |
Yee Cheng Chin | 6d11347 | 2023-10-02 21:38:39 +0200 | [diff] [blame] | 8098 | size_t num_flags = STRLEN(option_val); |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 8099 | if (num_flags == 0) |
| 8100 | return FAIL; |
| 8101 | |
| 8102 | *matches = ALLOC_MULT(char_u *, num_flags + 1); |
| 8103 | if (*matches == NULL) |
| 8104 | return FAIL; |
| 8105 | |
| 8106 | int count = 0; |
| 8107 | char_u *p; |
| 8108 | |
| 8109 | p = vim_strsave(option_val); |
| 8110 | if (p == NULL) |
| 8111 | { |
| 8112 | VIM_CLEAR(*matches); |
| 8113 | return FAIL; |
| 8114 | } |
| 8115 | (*matches)[count++] = p; |
| 8116 | |
| 8117 | if (num_flags > 1) |
| 8118 | { |
| 8119 | // If more than one flags, split the flags up and expose each |
| 8120 | // character as individual choice. |
| 8121 | for (char_u *flag = option_val; *flag != NUL; flag++) |
| 8122 | { |
Yee Cheng Chin | f7f746b | 2023-09-30 12:28:50 +0200 | [diff] [blame] | 8123 | p = vim_strnsave(flag, 1); |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 8124 | if (p == NULL) |
| 8125 | break; |
| 8126 | (*matches)[count++] = p; |
| 8127 | } |
| 8128 | } |
| 8129 | |
| 8130 | *numMatches = count; |
| 8131 | return OK; |
| 8132 | } |
| 8133 | |
| 8134 | return ExpandOldSetting(numMatches, matches); |
| 8135 | } |
| 8136 | |
| 8137 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8138 | * Get the value for the numeric or string option *opp in a nice format into |
| 8139 | * NameBuff[]. Must not be called with a hidden option! |
| 8140 | */ |
| 8141 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 8142 | option_value2string( |
| 8143 | struct vimoption *opp, |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 8144 | int scope) // OPT_GLOBAL and/or OPT_LOCAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8145 | { |
| 8146 | char_u *varp; |
| 8147 | |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 8148 | varp = get_varp_scope(opp, scope); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8149 | |
| 8150 | if (opp->flags & P_NUM) |
| 8151 | { |
| 8152 | long wc = 0; |
| 8153 | |
| 8154 | if (wc_use_keyname(varp, &wc)) |
| 8155 | STRCPY(NameBuff, get_special_key_name((int)wc, 0)); |
| 8156 | else if (wc != 0) |
| 8157 | STRCPY(NameBuff, transchar((int)wc)); |
| 8158 | else |
| 8159 | sprintf((char *)NameBuff, "%ld", *(long *)varp); |
| 8160 | } |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 8161 | else // P_STRING |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8162 | { |
| 8163 | varp = *(char_u **)(varp); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 8164 | if (varp == NULL) // just in case |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8165 | NameBuff[0] = NUL; |
| 8166 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 8167 | // don't show the actual value of 'key', only that it's set |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8168 | else if (opp->var == (char_u *)&p_key && *varp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8169 | STRCPY(NameBuff, "*****"); |
| 8170 | #endif |
| 8171 | else if (opp->flags & P_EXPAND) |
| 8172 | home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 8173 | // Translate 'pastetoggle' into special key names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8174 | else if ((char_u **)opp->var == &p_pt) |
| 8175 | str2specialbuf(p_pt, NameBuff, MAXPATHL); |
| 8176 | else |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 8177 | vim_strncpy(NameBuff, varp, MAXPATHL - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8178 | } |
| 8179 | } |
| 8180 | |
| 8181 | /* |
| 8182 | * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be |
| 8183 | * printed as a keyname. |
| 8184 | * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'. |
| 8185 | */ |
| 8186 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 8187 | wc_use_keyname(char_u *varp, long *wcp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8188 | { |
| 8189 | if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm)) |
| 8190 | { |
| 8191 | *wcp = *(long *)varp; |
| 8192 | if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0) |
| 8193 | return TRUE; |
| 8194 | } |
| 8195 | return FALSE; |
| 8196 | } |
| 8197 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8198 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8199 | * Return TRUE if "x" is present in 'shortmess' option, or |
| 8200 | * 'shortmess' contains 'a' and "x" is present in SHM_A. |
| 8201 | */ |
| 8202 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 8203 | shortmess(int x) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8204 | { |
Bram Moolenaar | 7f29f7a | 2012-02-29 13:51:37 +0100 | [diff] [blame] | 8205 | return p_shm != NULL && |
| 8206 | ( vim_strchr(p_shm, x) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8207 | || (vim_strchr(p_shm, 'a') != NULL |
| 8208 | && vim_strchr((char_u *)SHM_A, x) != NULL)); |
| 8209 | } |
| 8210 | |
| 8211 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8212 | * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found. |
| 8213 | * |
| 8214 | * Reset 'compatible' and set the values for options that didn't get set yet |
| 8215 | * to the Vim defaults. |
| 8216 | * Don't do this if the 'compatible' option has been set or reset before. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 8217 | * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8218 | */ |
| 8219 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 8220 | vimrc_found(char_u *fname, char_u *envname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8221 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 8222 | int opt_idx; |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 8223 | int dofree = FALSE; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 8224 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8225 | |
| 8226 | if (!option_was_set((char_u *)"cp")) |
| 8227 | { |
| 8228 | p_cp = FALSE; |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 8229 | for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8230 | if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF))) |
| 8231 | set_option_default(opt_idx, OPT_FREE, FALSE); |
| 8232 | didset_options(); |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 8233 | didset_options2(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8234 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 8235 | |
| 8236 | if (fname != NULL) |
| 8237 | { |
| 8238 | p = vim_getenv(envname, &dofree); |
| 8239 | if (p == NULL) |
| 8240 | { |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 8241 | // Set $MYVIMRC to the first vimrc file found. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 8242 | p = FullName_save(fname, FALSE); |
| 8243 | if (p != NULL) |
| 8244 | { |
| 8245 | vim_setenv(envname, p); |
| 8246 | vim_free(p); |
| 8247 | } |
| 8248 | } |
| 8249 | else if (dofree) |
| 8250 | vim_free(p); |
| 8251 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8252 | } |
| 8253 | |
| 8254 | /* |
| 8255 | * Set 'compatible' on or off. Called for "-C" and "-N" command line arg. |
| 8256 | */ |
| 8257 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 8258 | change_compatible(int on) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8259 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 8260 | int opt_idx; |
| 8261 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8262 | if (p_cp != on) |
| 8263 | { |
| 8264 | p_cp = on; |
| 8265 | compatible_set(); |
| 8266 | } |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 8267 | opt_idx = findoption((char_u *)"cp"); |
| 8268 | if (opt_idx >= 0) |
| 8269 | options[opt_idx].flags |= P_WAS_SET; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8270 | } |
| 8271 | |
| 8272 | /* |
| 8273 | * Return TRUE when option "name" has been set. |
Bram Moolenaar | 1a4a75c | 2013-07-28 16:03:06 +0200 | [diff] [blame] | 8274 | * Only works correctly for global options. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8275 | */ |
| 8276 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 8277 | option_was_set(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8278 | { |
| 8279 | int idx; |
| 8280 | |
| 8281 | idx = findoption(name); |
Bram Moolenaar | 6e0ce17 | 2019-12-05 20:12:41 +0100 | [diff] [blame] | 8282 | if (idx < 0) // unknown option |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8283 | return FALSE; |
| 8284 | if (options[idx].flags & P_WAS_SET) |
| 8285 | return TRUE; |
| 8286 | return FALSE; |
| 8287 | } |
| 8288 | |
| 8289 | /* |
Bram Moolenaar | 15d55de | 2012-12-05 14:43:02 +0100 | [diff] [blame] | 8290 | * Reset the flag indicating option "name" was set. |
| 8291 | */ |
Bram Moolenaar | fe8ef98 | 2018-09-13 20:31:54 +0200 | [diff] [blame] | 8292 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 8293 | reset_option_was_set(char_u *name) |
Bram Moolenaar | 15d55de | 2012-12-05 14:43:02 +0100 | [diff] [blame] | 8294 | { |
| 8295 | int idx = findoption(name); |
| 8296 | |
Yegappan Lakshmanan | a41e221 | 2023-01-16 18:19:05 +0000 | [diff] [blame] | 8297 | if (idx < 0) |
| 8298 | return FAIL; |
| 8299 | |
| 8300 | options[idx].flags &= ~P_WAS_SET; |
| 8301 | return OK; |
Bram Moolenaar | 15d55de | 2012-12-05 14:43:02 +0100 | [diff] [blame] | 8302 | } |
| 8303 | |
| 8304 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8305 | * compatible_set() - Called when 'compatible' has been set or unset. |
| 8306 | * |
| 8307 | * When 'compatible' set: Set all relevant options (those that have the P_VIM) |
| 8308 | * flag) to a Vi compatible value. |
| 8309 | * When 'compatible' is unset: Set all options that have a different default |
| 8310 | * for Vim (without the P_VI_DEF flag) to that default. |
| 8311 | */ |
| 8312 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 8313 | compatible_set(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8314 | { |
| 8315 | int opt_idx; |
| 8316 | |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 8317 | for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8318 | if ( ((options[opt_idx].flags & P_VIM) && p_cp) |
| 8319 | || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp)) |
| 8320 | set_option_default(opt_idx, OPT_FREE, p_cp); |
| 8321 | didset_options(); |
Bram Moolenaar | e68c25c | 2015-08-25 15:39:55 +0200 | [diff] [blame] | 8322 | didset_options2(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8323 | } |
| 8324 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8325 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8326 | * Check if backspacing over something is allowed. |
| 8327 | */ |
| 8328 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 8329 | can_bs( |
Bram Moolenaar | aa0489e | 2020-04-17 19:41:21 +0200 | [diff] [blame] | 8330 | int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8331 | { |
Bram Moolenaar | 6b810d9 | 2018-06-04 17:28:44 +0200 | [diff] [blame] | 8332 | #ifdef FEAT_JOB_CHANNEL |
| 8333 | if (what == BS_START && bt_prompt(curbuf)) |
| 8334 | return FALSE; |
| 8335 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8336 | switch (*p_bs) |
| 8337 | { |
Bram Moolenaar | aa0489e | 2020-04-17 19:41:21 +0200 | [diff] [blame] | 8338 | case '3': return TRUE; |
| 8339 | case '2': return (what != BS_NOSTOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8340 | case '1': return (what != BS_START); |
| 8341 | case '0': return FALSE; |
| 8342 | } |
| 8343 | return vim_strchr(p_bs, what) != NULL; |
| 8344 | } |
| 8345 | |
| 8346 | /* |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 8347 | * Return the effective 'scrolloff' value for the current window, using the |
| 8348 | * global value when appropriate. |
| 8349 | */ |
| 8350 | long |
| 8351 | get_scrolloff_value(void) |
| 8352 | { |
| 8353 | return curwin->w_p_so < 0 ? p_so : curwin->w_p_so; |
| 8354 | } |
| 8355 | |
| 8356 | /* |
| 8357 | * Return the effective 'sidescrolloff' value for the current window, using the |
| 8358 | * global value when appropriate. |
| 8359 | */ |
| 8360 | long |
| 8361 | get_sidescrolloff_value(void) |
| 8362 | { |
| 8363 | return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso; |
| 8364 | } |
| 8365 | |
| 8366 | /* |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 8367 | * Get the local or global value of 'backupcopy' flags. |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 8368 | */ |
| 8369 | unsigned int |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 8370 | get_bkc_flags(buf_T *buf) |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 8371 | { |
| 8372 | return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags; |
| 8373 | } |
Bram Moolenaar | 95ec9d6 | 2016-08-12 18:29:59 +0200 | [diff] [blame] | 8374 | |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 8375 | #if defined(FEAT_LINEBREAK) || defined(PROTO) |
Gary Johnson | 53ba05b | 2021-07-26 22:19:10 +0200 | [diff] [blame] | 8376 | /* |
Christian Brabandt | c53b467 | 2022-01-15 10:01:05 +0000 | [diff] [blame] | 8377 | * Get the local or global value of 'formatlistpat'. |
| 8378 | */ |
| 8379 | char_u * |
| 8380 | get_flp_value(buf_T *buf) |
| 8381 | { |
Christian Brabandt | c53b467 | 2022-01-15 10:01:05 +0000 | [diff] [blame] | 8382 | if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL) |
| 8383 | return p_flp; |
| 8384 | return buf->b_p_flp; |
| 8385 | } |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 8386 | #endif |
Christian Brabandt | c53b467 | 2022-01-15 10:01:05 +0000 | [diff] [blame] | 8387 | |
| 8388 | /* |
zeertzjq | aa925ee | 2024-06-09 18:24:05 +0200 | [diff] [blame] | 8389 | * Get the local or global value of 'virtualedit' flags. |
Gary Johnson | 53ba05b | 2021-07-26 22:19:10 +0200 | [diff] [blame] | 8390 | */ |
| 8391 | unsigned int |
| 8392 | get_ve_flags(void) |
| 8393 | { |
Gary Johnson | 51ad850 | 2021-08-03 18:33:08 +0200 | [diff] [blame] | 8394 | return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags) |
| 8395 | & ~(VE_NONE | VE_NONEU); |
Gary Johnson | 53ba05b | 2021-07-26 22:19:10 +0200 | [diff] [blame] | 8396 | } |
| 8397 | |
Bram Moolenaar | ee85702 | 2019-11-09 23:26:40 +0100 | [diff] [blame] | 8398 | #if defined(FEAT_LINEBREAK) || defined(PROTO) |
| 8399 | /* |
| 8400 | * Get the local or global value of 'showbreak'. |
| 8401 | */ |
| 8402 | char_u * |
| 8403 | get_showbreak_value(win_T *win) |
| 8404 | { |
| 8405 | if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL) |
| 8406 | return p_sbr; |
| 8407 | if (STRCMP(win->w_p_sbr, "NONE") == 0) |
| 8408 | return empty_option; |
| 8409 | return win->w_p_sbr; |
| 8410 | } |
| 8411 | #endif |
| 8412 | |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 8413 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 8414 | /* |
| 8415 | * Get window or buffer local options. |
| 8416 | */ |
| 8417 | dict_T * |
| 8418 | get_winbuf_options(int bufopt) |
| 8419 | { |
| 8420 | dict_T *d; |
| 8421 | int opt_idx; |
| 8422 | |
| 8423 | d = dict_alloc(); |
| 8424 | if (d == NULL) |
| 8425 | return NULL; |
| 8426 | |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 8427 | for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++) |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 8428 | { |
| 8429 | struct vimoption *opt = &options[opt_idx]; |
| 8430 | |
| 8431 | if ((bufopt && (opt->indir & PV_BUF)) |
| 8432 | || (!bufopt && (opt->indir & PV_WIN))) |
| 8433 | { |
| 8434 | char_u *varp = get_varp(opt); |
| 8435 | |
| 8436 | if (varp != NULL) |
| 8437 | { |
| 8438 | if (opt->flags & P_STRING) |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 8439 | dict_add_string(d, opt->fullname, *(char_u **)varp); |
Bram Moolenaar | 789a5c0 | 2016-09-12 19:51:11 +0200 | [diff] [blame] | 8440 | else if (opt->flags & P_NUM) |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 8441 | dict_add_number(d, opt->fullname, *(long *)varp); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 8442 | else |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 8443 | dict_add_number(d, opt->fullname, *(int *)varp); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 8444 | } |
| 8445 | } |
| 8446 | } |
| 8447 | |
| 8448 | return d; |
| 8449 | } |
| 8450 | #endif |
Bram Moolenaar | 017ba07 | 2019-09-14 21:01:23 +0200 | [diff] [blame] | 8451 | |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 8452 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
Bram Moolenaar | 017ba07 | 2019-09-14 21:01:23 +0200 | [diff] [blame] | 8453 | /* |
| 8454 | * This is called when 'culopt' is changed |
| 8455 | */ |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 8456 | int |
Bram Moolenaar | 017ba07 | 2019-09-14 21:01:23 +0200 | [diff] [blame] | 8457 | fill_culopt_flags(char_u *val, win_T *wp) |
| 8458 | { |
| 8459 | char_u *p; |
| 8460 | char_u culopt_flags_new = 0; |
| 8461 | |
| 8462 | if (val == NULL) |
| 8463 | p = wp->w_p_culopt; |
| 8464 | else |
| 8465 | p = val; |
| 8466 | while (*p != NUL) |
| 8467 | { |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 8468 | // Note: Keep this in sync with p_culopt_values. |
Bram Moolenaar | 017ba07 | 2019-09-14 21:01:23 +0200 | [diff] [blame] | 8469 | if (STRNCMP(p, "line", 4) == 0) |
| 8470 | { |
| 8471 | p += 4; |
| 8472 | culopt_flags_new |= CULOPT_LINE; |
| 8473 | } |
| 8474 | else if (STRNCMP(p, "both", 4) == 0) |
| 8475 | { |
| 8476 | p += 4; |
| 8477 | culopt_flags_new |= CULOPT_LINE | CULOPT_NBR; |
| 8478 | } |
| 8479 | else if (STRNCMP(p, "number", 6) == 0) |
| 8480 | { |
| 8481 | p += 6; |
| 8482 | culopt_flags_new |= CULOPT_NBR; |
| 8483 | } |
| 8484 | else if (STRNCMP(p, "screenline", 10) == 0) |
| 8485 | { |
| 8486 | p += 10; |
| 8487 | culopt_flags_new |= CULOPT_SCRLINE; |
| 8488 | } |
| 8489 | |
| 8490 | if (*p != ',' && *p != NUL) |
| 8491 | return FAIL; |
| 8492 | if (*p == ',') |
| 8493 | ++p; |
| 8494 | } |
| 8495 | |
| 8496 | // Can't have both "line" and "screenline". |
| 8497 | if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE)) |
| 8498 | return FAIL; |
| 8499 | wp->w_p_culopt_flags = culopt_flags_new; |
| 8500 | |
| 8501 | return OK; |
| 8502 | } |
| 8503 | #endif |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 8504 | |
| 8505 | /* |
| 8506 | * Get the value of 'magic' adjusted for Vim9 script. |
| 8507 | */ |
| 8508 | int |
| 8509 | magic_isset(void) |
| 8510 | { |
| 8511 | switch (magic_overruled) |
| 8512 | { |
Bram Moolenaar | d93a7fc | 2021-01-04 12:42:13 +0100 | [diff] [blame] | 8513 | case OPTION_MAGIC_ON: return TRUE; |
| 8514 | case OPTION_MAGIC_OFF: return FALSE; |
| 8515 | case OPTION_MAGIC_NOT_SET: break; |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 8516 | } |
| 8517 | #ifdef FEAT_EVAL |
| 8518 | if (in_vim9script()) |
| 8519 | return TRUE; |
| 8520 | #endif |
| 8521 | return p_magic; |
| 8522 | } |
Yegappan Lakshmanan | 777175b | 2021-11-18 22:08:57 +0000 | [diff] [blame] | 8523 | |
| 8524 | /* |
| 8525 | * Set the callback function value for an option that accepts a function name, |
| 8526 | * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.) |
| 8527 | * Returns OK if the option is successfully set to a function, otherwise |
| 8528 | * returns FAIL. |
| 8529 | */ |
| 8530 | int |
| 8531 | option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED) |
| 8532 | { |
| 8533 | #ifdef FEAT_EVAL |
| 8534 | typval_T *tv; |
| 8535 | callback_T cb; |
| 8536 | |
| 8537 | if (optval == NULL || *optval == NUL) |
| 8538 | { |
| 8539 | free_callback(optcb); |
| 8540 | return OK; |
| 8541 | } |
| 8542 | |
Yegappan Lakshmanan | 05e59e3 | 2021-12-01 10:30:07 +0000 | [diff] [blame] | 8543 | if (*optval == '{' || (in_vim9script() && *optval == '(') |
Yegappan Lakshmanan | 777175b | 2021-11-18 22:08:57 +0000 | [diff] [blame] | 8544 | || (STRNCMP(optval, "function(", 9) == 0) |
| 8545 | || (STRNCMP(optval, "funcref(", 8) == 0)) |
| 8546 | // Lambda expression or a funcref |
| 8547 | tv = eval_expr(optval, NULL); |
| 8548 | else |
| 8549 | // treat everything else as a function name string |
Yegappan Lakshmanan | e7f4abd | 2021-12-24 20:47:38 +0000 | [diff] [blame] | 8550 | tv = alloc_string_tv(vim_strsave(optval)); |
Yegappan Lakshmanan | 777175b | 2021-11-18 22:08:57 +0000 | [diff] [blame] | 8551 | if (tv == NULL) |
| 8552 | return FAIL; |
| 8553 | |
| 8554 | cb = get_callback(tv); |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 8555 | if (cb.cb_name == NULL || *cb.cb_name == NUL) |
Yegappan Lakshmanan | 777175b | 2021-11-18 22:08:57 +0000 | [diff] [blame] | 8556 | { |
| 8557 | free_tv(tv); |
| 8558 | return FAIL; |
| 8559 | } |
| 8560 | |
| 8561 | free_callback(optcb); |
| 8562 | set_callback(optcb, &cb); |
Bram Moolenaar | c96b7f5 | 2022-12-02 15:58:38 +0000 | [diff] [blame] | 8563 | if (cb.cb_free_name) |
| 8564 | vim_free(cb.cb_name); |
Yegappan Lakshmanan | 777175b | 2021-11-18 22:08:57 +0000 | [diff] [blame] | 8565 | free_tv(tv); |
Bram Moolenaar | f0e7e63 | 2022-01-21 13:29:56 +0000 | [diff] [blame] | 8566 | |
| 8567 | // when using Vim9 style "import.funcname" it needs to be expanded to |
| 8568 | // "import#funcname". |
| 8569 | expand_autload_callback(optcb); |
| 8570 | |
Yegappan Lakshmanan | 777175b | 2021-11-18 22:08:57 +0000 | [diff] [blame] | 8571 | return OK; |
| 8572 | #else |
| 8573 | return FAIL; |
| 8574 | #endif |
| 8575 | } |
Christian Brabandt | 757593c | 2023-08-22 21:44:10 +0200 | [diff] [blame] | 8576 | |
| 8577 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 8578 | static void |
| 8579 | didset_options_sctx(int opt_flags, char **buf) |
| 8580 | { |
| 8581 | for (int i = 0; ; ++i) |
| 8582 | { |
| 8583 | if (buf[i] == NULL) |
| 8584 | break; |
| 8585 | |
| 8586 | int idx = findoption((char_u *)buf[i]); |
| 8587 | if (idx >= 0) |
| 8588 | set_option_sctx_idx(idx, opt_flags, current_sctx); |
| 8589 | } |
| 8590 | } |
| 8591 | #endif |