comparison src/option.c @ 18679:fd95d4dbeb37 v8.1.2331

patch 8.1.2331: the option.c file is still very big Commit: https://github.com/vim/vim/commit/7bae0b1bc84a95d565ffab38cf7f82ad21c656b6 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Nov 21 22:14:18 2019 +0100 patch 8.1.2331: the option.c file is still very big Problem: The option.c file is still very big. Solution: Move a few functions to where they fit better. (Yegappan Lakshmanan, closes #4895)
author Bram Moolenaar <Bram@vim.org>
date Thu, 21 Nov 2019 22:15:03 +0100
parents f249b44039e0
children 7982f65d8f54
comparison
equal deleted inserted replaced
18678:cb4a4b71df4a 18679:fd95d4dbeb37
35 #include "vim.h" 35 #include "vim.h"
36 #include "optiondefs.h" 36 #include "optiondefs.h"
37 37
38 static void set_options_default(int opt_flags); 38 static void set_options_default(int opt_flags);
39 static void set_string_default_esc(char *name, char_u *val, int escape); 39 static void set_string_default_esc(char *name, char_u *val, int escape);
40 static char_u *term_bg_default(void);
41 static char_u *option_expand(int opt_idx, char_u *val); 40 static char_u *option_expand(int opt_idx, char_u *val);
42 static void didset_options(void); 41 static void didset_options(void);
43 static void didset_options2(void); 42 static void didset_options2(void);
44 #if defined(FEAT_EVAL) || defined(PROTO) 43 #if defined(FEAT_EVAL) || defined(PROTO)
45 static long_u *insecure_flag(int opt_idx, int opt_flags); 44 static long_u *insecure_flag(int opt_idx, int opt_flags);
798 (void)parse_printoptions(); /* parse 'printoptions' default value */ 797 (void)parse_printoptions(); /* parse 'printoptions' default value */
799 #endif 798 #endif
800 } 799 }
801 800
802 /* 801 /*
803 * Return "dark" or "light" depending on the kind of terminal.
804 * This is just guessing! Recognized are:
805 * "linux" Linux console
806 * "screen.linux" Linux console with screen
807 * "cygwin.*" Cygwin shell
808 * "putty.*" Putty program
809 * We also check the COLORFGBG environment variable, which is set by
810 * rxvt and derivatives. This variable contains either two or three
811 * values separated by semicolons; we want the last value in either
812 * case. If this value is 0-6 or 8, our background is dark.
813 */
814 static char_u *
815 term_bg_default(void)
816 {
817 #if defined(MSWIN)
818 /* DOS console is nearly always black */
819 return (char_u *)"dark";
820 #else
821 char_u *p;
822
823 if (STRCMP(T_NAME, "linux") == 0
824 || STRCMP(T_NAME, "screen.linux") == 0
825 || STRNCMP(T_NAME, "cygwin", 6) == 0
826 || STRNCMP(T_NAME, "putty", 5) == 0
827 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
828 && (p = vim_strrchr(p, ';')) != NULL
829 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
830 && p[2] == NUL))
831 return (char_u *)"dark";
832 return (char_u *)"light";
833 #endif
834 }
835
836 /*
837 * Initialize the options, part three: After reading the .vimrc 802 * Initialize the options, part three: After reading the .vimrc
838 */ 803 */
839 void 804 void
840 set_init_3(void) 805 set_init_3(void)
841 { 806 {
1051 p_hlg[1] = 'n'; 1016 p_hlg[1] = 'n';
1052 } 1017 }
1053 p_hlg[2] = NUL; 1018 p_hlg[2] = NUL;
1054 } 1019 }
1055 options[idx].flags |= P_ALLOCED; 1020 options[idx].flags |= P_ALLOCED;
1056 }
1057 }
1058 #endif
1059
1060 #ifdef FEAT_GUI
1061 static char_u *
1062 gui_bg_default(void)
1063 {
1064 if (gui_get_lightness(gui.back_pixel) < 127)
1065 return (char_u *)"dark";
1066 return (char_u *)"light";
1067 }
1068
1069 /*
1070 * Option initializations that can only be done after opening the GUI window.
1071 */
1072 void
1073 init_gui_options(void)
1074 {
1075 /* Set the 'background' option according to the lightness of the
1076 * background color, unless the user has set it already. */
1077 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
1078 {
1079 set_option_value((char_u *)"bg", 0L, gui_bg_default(), 0);
1080 highlight_changed();
1081 } 1021 }
1082 } 1022 }
1083 #endif 1023 #endif
1084 1024
1085 #ifdef FEAT_TITLE 1025 #ifdef FEAT_TITLE
2175 if (multi_byte) 2115 if (multi_byte)
2176 return PTR2CHAR(arg); 2116 return PTR2CHAR(arg);
2177 return *arg; 2117 return *arg;
2178 } 2118 }
2179 2119
2180 #if defined(FEAT_CMDWIN) || defined(PROTO)
2181 /*
2182 * Check value of 'cedit' and set cedit_key.
2183 * Returns NULL if value is OK, error message otherwise.
2184 */
2185 char *
2186 check_cedit(void)
2187 {
2188 int n;
2189
2190 if (*p_cedit == NUL)
2191 cedit_key = -1;
2192 else
2193 {
2194 n = string_to_key(p_cedit, FALSE);
2195 if (vim_isprintc(n))
2196 return e_invarg;
2197 cedit_key = n;
2198 }
2199 return NULL;
2200 }
2201 #endif
2202
2203 #ifdef FEAT_TITLE 2120 #ifdef FEAT_TITLE
2204 /* 2121 /*
2205 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call 2122 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2206 * maketitle() to create and display it. 2123 * maketitle() to create and display it.
2207 * When switching the title or icon off, call mch_restore_title() to get 2124 * When switching the title or icon off, call mch_restore_title() to get
2505 for (s = val; *s != NUL; ++s) 2422 for (s = val; *s != NUL; ++s)
2506 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL) 2423 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2507 return FALSE; 2424 return FALSE;
2508 return TRUE; 2425 return TRUE;
2509 } 2426 }
2510
2511 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
2512 /*
2513 * Extract the items in the 'clipboard' option and set global values.
2514 * Return an error message or NULL for success.
2515 */
2516 char *
2517 check_clipboard_option(void)
2518 {
2519 int new_unnamed = 0;
2520 int new_autoselect_star = FALSE;
2521 int new_autoselect_plus = FALSE;
2522 int new_autoselectml = FALSE;
2523 int new_html = FALSE;
2524 regprog_T *new_exclude_prog = NULL;
2525 char *errmsg = NULL;
2526 char_u *p;
2527
2528 for (p = p_cb; *p != NUL; )
2529 {
2530 if (STRNCMP(p, "unnamed", 7) == 0 && (p[7] == ',' || p[7] == NUL))
2531 {
2532 new_unnamed |= CLIP_UNNAMED;
2533 p += 7;
2534 }
2535 else if (STRNCMP(p, "unnamedplus", 11) == 0
2536 && (p[11] == ',' || p[11] == NUL))
2537 {
2538 new_unnamed |= CLIP_UNNAMED_PLUS;
2539 p += 11;
2540 }
2541 else if (STRNCMP(p, "autoselect", 10) == 0
2542 && (p[10] == ',' || p[10] == NUL))
2543 {
2544 new_autoselect_star = TRUE;
2545 p += 10;
2546 }
2547 else if (STRNCMP(p, "autoselectplus", 14) == 0
2548 && (p[14] == ',' || p[14] == NUL))
2549 {
2550 new_autoselect_plus = TRUE;
2551 p += 14;
2552 }
2553 else if (STRNCMP(p, "autoselectml", 12) == 0
2554 && (p[12] == ',' || p[12] == NUL))
2555 {
2556 new_autoselectml = TRUE;
2557 p += 12;
2558 }
2559 else if (STRNCMP(p, "html", 4) == 0 && (p[4] == ',' || p[4] == NUL))
2560 {
2561 new_html = TRUE;
2562 p += 4;
2563 }
2564 else if (STRNCMP(p, "exclude:", 8) == 0 && new_exclude_prog == NULL)
2565 {
2566 p += 8;
2567 new_exclude_prog = vim_regcomp(p, RE_MAGIC);
2568 if (new_exclude_prog == NULL)
2569 errmsg = e_invarg;
2570 break;
2571 }
2572 else
2573 {
2574 errmsg = e_invarg;
2575 break;
2576 }
2577 if (*p == ',')
2578 ++p;
2579 }
2580 if (errmsg == NULL)
2581 {
2582 clip_unnamed = new_unnamed;
2583 clip_autoselect_star = new_autoselect_star;
2584 clip_autoselect_plus = new_autoselect_plus;
2585 clip_autoselectml = new_autoselectml;
2586 clip_html = new_html;
2587 vim_regfree(clip_exclude_prog);
2588 clip_exclude_prog = new_exclude_prog;
2589 #ifdef FEAT_GUI_GTK
2590 if (gui.in_use)
2591 {
2592 gui_gtk_set_selection_targets();
2593 gui_gtk_set_dnd_targets();
2594 }
2595 #endif
2596 }
2597 else
2598 vim_regfree(new_exclude_prog);
2599
2600 return errmsg;
2601 }
2602 #endif
2603 2427
2604 #if defined(FEAT_EVAL) || defined(PROTO) 2428 #if defined(FEAT_EVAL) || defined(PROTO)
2605 /* 2429 /*
2606 * Set the script_ctx for an option, taking care of setting the buffer- or 2430 * Set the script_ctx for an option, taking care of setting the buffer- or
2607 * window-local value. 2431 * window-local value.
6981 breakat_flags[*p] = TRUE; 6805 breakat_flags[*p] = TRUE;
6982 } 6806 }
6983 #endif 6807 #endif
6984 6808
6985 /* 6809 /*
6986 * Read the 'wildmode' option, fill wim_flags[].
6987 */
6988 int
6989 check_opt_wim(void)
6990 {
6991 char_u new_wim_flags[4];
6992 char_u *p;
6993 int i;
6994 int idx = 0;
6995
6996 for (i = 0; i < 4; ++i)
6997 new_wim_flags[i] = 0;
6998
6999 for (p = p_wim; *p; ++p)
7000 {
7001 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
7002 ;
7003 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
7004 return FAIL;
7005 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
7006 new_wim_flags[idx] |= WIM_LONGEST;
7007 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
7008 new_wim_flags[idx] |= WIM_FULL;
7009 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
7010 new_wim_flags[idx] |= WIM_LIST;
7011 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
7012 new_wim_flags[idx] |= WIM_BUFLASTUSED;
7013 else
7014 return FAIL;
7015 p += i;
7016 if (*p == NUL)
7017 break;
7018 if (*p == ',')
7019 {
7020 if (idx == 3)
7021 return FAIL;
7022 ++idx;
7023 }
7024 }
7025
7026 /* fill remaining entries with last flag */
7027 while (idx < 3)
7028 {
7029 new_wim_flags[idx + 1] = new_wim_flags[idx];
7030 ++idx;
7031 }
7032
7033 /* only when there are no errors, wim_flags[] is changed */
7034 for (i = 0; i < 4; ++i)
7035 wim_flags[i] = new_wim_flags[i];
7036 return OK;
7037 }
7038
7039 /*
7040 * Check if backspacing over something is allowed. 6810 * Check if backspacing over something is allowed.
7041 */ 6811 */
7042 int 6812 int
7043 can_bs( 6813 can_bs(
7044 int what) /* BS_INDENT, BS_EOL or BS_START */ 6814 int what) /* BS_INDENT, BS_EOL or BS_START */
7055 } 6825 }
7056 return vim_strchr(p_bs, what) != NULL; 6826 return vim_strchr(p_bs, what) != NULL;
7057 } 6827 }
7058 6828
7059 /* 6829 /*
7060 * Save the current values of 'fileformat' and 'fileencoding', so that we know
7061 * the file must be considered changed when the value is different.
7062 */
7063 void
7064 save_file_ff(buf_T *buf)
7065 {
7066 buf->b_start_ffc = *buf->b_p_ff;
7067 buf->b_start_eol = buf->b_p_eol;
7068 buf->b_start_bomb = buf->b_p_bomb;
7069
7070 /* Only use free/alloc when necessary, they take time. */
7071 if (buf->b_start_fenc == NULL
7072 || STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0)
7073 {
7074 vim_free(buf->b_start_fenc);
7075 buf->b_start_fenc = vim_strsave(buf->b_p_fenc);
7076 }
7077 }
7078
7079 /*
7080 * Return TRUE if 'fileformat' and/or 'fileencoding' has a different value
7081 * from when editing started (save_file_ff() called).
7082 * Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was
7083 * changed and 'binary' is not set.
7084 * Also when 'endofline' was changed and 'fixeol' is not set.
7085 * When "ignore_empty" is true don't consider a new, empty buffer to be
7086 * changed.
7087 */
7088 int
7089 file_ff_differs(buf_T *buf, int ignore_empty)
7090 {
7091 /* In a buffer that was never loaded the options are not valid. */
7092 if (buf->b_flags & BF_NEVERLOADED)
7093 return FALSE;
7094 if (ignore_empty
7095 && (buf->b_flags & BF_NEW)
7096 && buf->b_ml.ml_line_count == 1
7097 && *ml_get_buf(buf, (linenr_T)1, FALSE) == NUL)
7098 return FALSE;
7099 if (buf->b_start_ffc != *buf->b_p_ff)
7100 return TRUE;
7101 if ((buf->b_p_bin || !buf->b_p_fixeol) && buf->b_start_eol != buf->b_p_eol)
7102 return TRUE;
7103 if (!buf->b_p_bin && buf->b_start_bomb != buf->b_p_bomb)
7104 return TRUE;
7105 if (buf->b_start_fenc == NULL)
7106 return (*buf->b_p_fenc != NUL);
7107 return (STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0);
7108 }
7109
7110 /*
7111 * Return the effective 'scrolloff' value for the current window, using the 6830 * Return the effective 'scrolloff' value for the current window, using the
7112 * global value when appropriate. 6831 * global value when appropriate.
7113 */ 6832 */
7114 long 6833 long
7115 get_scrolloff_value(void) 6834 get_scrolloff_value(void)
7124 long 6843 long
7125 get_sidescrolloff_value(void) 6844 get_sidescrolloff_value(void)
7126 { 6845 {
7127 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso; 6846 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7128 } 6847 }
7129
7130 /*
7131 * Check matchpairs option for "*initc".
7132 * If there is a match set "*initc" to the matching character and "*findc" to
7133 * the opposite character. Set "*backwards" to the direction.
7134 * When "switchit" is TRUE swap the direction.
7135 */
7136 void
7137 find_mps_values(
7138 int *initc,
7139 int *findc,
7140 int *backwards,
7141 int switchit)
7142 {
7143 char_u *ptr;
7144
7145 ptr = curbuf->b_p_mps;
7146 while (*ptr != NUL)
7147 {
7148 if (has_mbyte)
7149 {
7150 char_u *prev;
7151
7152 if (mb_ptr2char(ptr) == *initc)
7153 {
7154 if (switchit)
7155 {
7156 *findc = *initc;
7157 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
7158 *backwards = TRUE;
7159 }
7160 else
7161 {
7162 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
7163 *backwards = FALSE;
7164 }
7165 return;
7166 }
7167 prev = ptr;
7168 ptr += mb_ptr2len(ptr) + 1;
7169 if (mb_ptr2char(ptr) == *initc)
7170 {
7171 if (switchit)
7172 {
7173 *findc = *initc;
7174 *initc = mb_ptr2char(prev);
7175 *backwards = FALSE;
7176 }
7177 else
7178 {
7179 *findc = mb_ptr2char(prev);
7180 *backwards = TRUE;
7181 }
7182 return;
7183 }
7184 ptr += mb_ptr2len(ptr);
7185 }
7186 else
7187 {
7188 if (*ptr == *initc)
7189 {
7190 if (switchit)
7191 {
7192 *backwards = TRUE;
7193 *findc = *initc;
7194 *initc = ptr[2];
7195 }
7196 else
7197 {
7198 *backwards = FALSE;
7199 *findc = ptr[2];
7200 }
7201 return;
7202 }
7203 ptr += 2;
7204 if (*ptr == *initc)
7205 {
7206 if (switchit)
7207 {
7208 *backwards = FALSE;
7209 *findc = *initc;
7210 *initc = ptr[-2];
7211 }
7212 else
7213 {
7214 *backwards = TRUE;
7215 *findc = ptr[-2];
7216 }
7217 return;
7218 }
7219 ++ptr;
7220 }
7221 if (*ptr == ',')
7222 ++ptr;
7223 }
7224 }
7225
7226 #if defined(FEAT_LINEBREAK) || defined(PROTO)
7227 /*
7228 * This is called when 'breakindentopt' is changed and when a window is
7229 * initialized.
7230 */
7231 int
7232 briopt_check(win_T *wp)
7233 {
7234 char_u *p;
7235 int bri_shift = 0;
7236 long bri_min = 20;
7237 int bri_sbr = FALSE;
7238
7239 p = wp->w_p_briopt;
7240 while (*p != NUL)
7241 {
7242 if (STRNCMP(p, "shift:", 6) == 0
7243 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
7244 {
7245 p += 6;
7246 bri_shift = getdigits(&p);
7247 }
7248 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
7249 {
7250 p += 4;
7251 bri_min = getdigits(&p);
7252 }
7253 else if (STRNCMP(p, "sbr", 3) == 0)
7254 {
7255 p += 3;
7256 bri_sbr = TRUE;
7257 }
7258 if (*p != ',' && *p != NUL)
7259 return FAIL;
7260 if (*p == ',')
7261 ++p;
7262 }
7263
7264 wp->w_p_brishift = bri_shift;
7265 wp->w_p_brimin = bri_min;
7266 wp->w_p_brisbr = bri_sbr;
7267
7268 return OK;
7269 }
7270 #endif
7271 6848
7272 /* 6849 /*
7273 * Get the local or global value of 'backupcopy'. 6850 * Get the local or global value of 'backupcopy'.
7274 */ 6851 */
7275 unsigned int 6852 unsigned int