robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
8.9 KiB202 linesraw
1#pragma once
2
3#include <stdbool.h>
4#include <stddef.h>
5
6#include "nvim/api/private/defs.h"
7#include "nvim/cmdexpand_defs.h"
8#include "nvim/regexp_defs.h"
9#include "nvim/types_defs.h"
10
11#include "options_enum.generated.h"
12
13/// Option flags.
14typedef enum {
15  kOptFlagExpand    = 1 << 0,  ///< Environment expansion.
16                               ///< NOTE: kOptFlagExpand can never be used for local or hidden options.
17  kOptFlagNoDefExp  = 1 << 1,  ///< Don't expand default value.
18  kOptFlagNoDefault = 1 << 2,  ///< Don't set to default value.
19  kOptFlagWasSet    = 1 << 3,  ///< Option has been set/reset.
20  kOptFlagNoMkrc    = 1 << 4,  ///< Don't include in :mkvimrc output.
21  kOptFlagUIOption  = 1 << 5,  ///< Send option to remote UI.
22  kOptFlagRedrTabl  = 1 << 6,  ///< Redraw tabline.
23  kOptFlagRedrStat  = 1 << 7,  ///< Redraw status lines.
24  kOptFlagRedrWin   = 1 << 8,  ///< Redraw current window and recompute text.
25  kOptFlagRedrBuf   = 1 << 9,  ///< Redraw current buffer and recompute text.
26  kOptFlagRedrAll   = kOptFlagRedrBuf | kOptFlagRedrWin,  ///< Redraw all windows and recompute text.
27  kOptFlagRedrClear = kOptFlagRedrAll | kOptFlagRedrStat,  ///< Clear and redraw all and recompute text.
28  kOptFlagComma     = 1 << 10,  ///< Comma-separated list.
29  kOptFlagOneComma  = (1 << 11) | kOptFlagComma,  ///< Comma-separated list that cannot have two consecutive commas.
30  kOptFlagNoDup     = 1 << 12,  ///< Don't allow duplicate strings.
31  kOptFlagFlagList  = 1 << 13,  ///< List of single-char flags.
32  kOptFlagSecure    = 1 << 14,  ///< Cannot change in modelines, secure mode, or the sandbox.
33  kOptFlagGettext   = 1 << 15,  ///< Expand default value with _().
34  kOptFlagNoGlob    = 1 << 16,  ///< Do not use local value for global vimrc.
35  kOptFlagNFname    = 1 << 17,  ///< Only normal file name chars allowed.
36  kOptFlagInsecure  = 1 << 18,  ///< Value contains untrusted content; evaluate it in the sandbox.
37  kOptFlagPriMkrc   = 1 << 19,  ///< Priority for :mkvimrc (setting option has side effects).
38  kOptFlagCurswant  = 1 << 20,  ///< Update curswant required; not needed when there is a redraw flag.
39  kOptFlagNDname    = 1 << 21,  ///< Only normal directory name chars allowed.
40  kOptFlagHLOnly    = 1 << 22,  ///< Option only changes highlight, not text.
41  kOptFlagMLE       = 1 << 23,  ///< Under control of 'modelineexpr'.
42  kOptFlagFunc      = 1 << 24,  ///< Callback option: Lua function, or Vimscript funcref.
43  kOptFlagColon     = 1 << 25,  ///< Values use colons to create sublists.
44  kOptFlagExpr      = 1 << 26,  ///< Callback option: Vimscript expr, e.g. 'foldexpr'.
45} OptFlags;
46
47/// Scopes that an option can support.
48typedef enum {
49  kOptScopeGlobal = 0,  ///< Request global option value
50  kOptScopeWin,      ///< Request window-local option value
51  kOptScopeBuf,      ///< Request buffer-local option value
52  kOptScopeTab,      ///< Request tabpage-local option value
53} OptScope;
54/// Always update this whenever a new option scope is added.
55#define kOptScopeSize (kOptScopeTab + 1)
56typedef uint8_t OptScopeFlags;
57
58/// Value kind of one key in a dict option (see "schema" in options.lua).
59typedef enum {
60  kOptSchemaFlag,   ///< bare flag, e.g. 'diffopt' "filler"
61  kOptSchemaNum,    ///< "key:N" non-negative number, e.g. 'diffopt' "context:4"
62  kOptSchemaSNum,   ///< "key:N" signed number, e.g. 'breakindentopt' "shift:-2"
63  kOptSchemaEnum,   ///< "key:val" where val is one of "enum_values", e.g. "algorithm:patience"
64  kOptSchemaStr,    ///< "key:val" where val is a free string, e.g. 'previewpopup' "highlight:Foo"
65} OptSchemaKind;
66
67/// One key of a dict option, generated from its `dict` schema in options.lua.
68/// Consumed by opt_strings_check(). A NULL "name" terminates the array.
69typedef struct {
70  const char *name;           ///< key name, without a trailing ':'
71  OptSchemaKind kind;
72  const char **enum_values;    ///< kOptSchemaEnum: NULL-terminated valid values; else NULL
73} OptSchemaItem;
74
75/// Schema for a dict option (`schema.dict` in options.lua).
76typedef struct {
77  FieldHashfn get_field;        ///< Keyset perfect-hash lookup, for opt_fill().
78  const OptSchemaItem *schema;  ///< Grammar, for opt_strings_check() validation.
79  size_t size;                  ///< sizeof the keyset, for opt_keyset() storage.
80} OptDictSchema;
81
82/// :set operator types
83typedef enum {
84  OP_NONE = 0,
85  OP_ADDING,      ///< "opt+=arg"
86  OP_PREPENDING,  ///< "opt^=arg"
87  OP_REMOVING,    ///< "opt-=arg"
88} set_op_T;
89
90/// Arguments for validating an option value or applying it after storage.
91typedef struct {
92  /// Pointer to the option variable.  The variable can be an OptInt (numeric
93  /// option), an int (boolean option), a char pointer (string option), or a Callback.
94  /// Holds the old value during validation and the new value when applying it.
95  void *os_varp;
96  OptIndex os_idx;
97  int os_flags;
98
99  /// Old value of the option.
100  Object os_oldval;
101  /// New value of the option (not yet stored during validation).
102  Object os_newval;
103
104  /// Option value was checked to be safe, no need to set kOptFlagInsecure
105  /// Used for the 'keymap', 'filetype' and 'syntax' options.
106  bool os_value_checked;
107  /// Option value changed.  Used for the 'filetype' and 'syntax' options.
108  bool os_value_changed;
109
110  /// Used by the 'isident', 'iskeyword', 'isprint' and 'isfname' options.
111  /// Set to true if the character table is modified when processing the
112  /// option and need to be restored because of a failure.
113  bool os_restore_chartab;
114
115  /// If the value specified for an option is not valid and the error message
116  /// is parameterized, then the "os_errbuf" buffer is used to store the error
117  /// message (when it is not NULL).
118  const CharBuf *os_errbuf;
119
120  win_T *os_win;
121  buf_T *os_buf;
122} optset_T;
123
124/// Check a candidate value without changing option variables or derived state,
125/// or evaluating user code. Return an error message, or NULL on success.
126typedef const char *(*opt_validate_cb_T)(const optset_T *args);
127
128/// Type for the callback function invoked after storing an option value to
129/// apply it and update derived state.
130///
131/// Returns NULL on success, or an error message if the value could not be applied.
132typedef const char *(*opt_did_set_cb_T)(optset_T *args);
133
134/// Argument for the callback function (opt_expand_cb_T) invoked after a string
135/// option value is expanded for cmdline completion.
136typedef struct {
137  /// Pointer to the option variable. It's always a string.
138  char *oe_varp;
139  OptIndex oe_idx;
140  /// The original option value, escaped.
141  char *oe_opt_value;
142
143  /// true if using set+= instead of set=
144  bool oe_append;
145  /// true if we would like to add the original option value as the first choice.
146  bool oe_include_orig_val;
147
148  /// Regex from the cmdline, for matching potential options against.
149  regmatch_T *oe_regmatch;
150  /// The expansion context.
151  expand_T *oe_xp;
152
153  /// The full argument passed to :set. For example, if the user inputs
154  /// ":set dip=icase,algorithm:my<Tab>", oe_xp->xp_pattern will only have
155  /// "my", but oe_set_arg will contain the whole "icase,algorithm:my".
156  char *oe_set_arg;
157} optexpand_T;
158
159/// Type for the callback function that is invoked when expanding possible
160/// string option values during cmdline completion.
161///
162/// Strings in returned matches will be managed and freed by caller.
163///
164/// Returns OK if the expansion succeeded (numMatches and matches have to be
165/// set). Otherwise returns FAIL.
166///
167/// Note: If returned FAIL or *numMatches is 0, *matches will NOT be freed by
168/// caller.
169typedef int (*opt_expand_cb_T)(optexpand_T *args, int *numMatches, char ***matches);
170
171typedef struct {
172  char *fullname;                    ///< full option name
173  char *shortname;                   ///< permissible abbreviation
174  uint32_t flags;                    ///< see above
175  ObjectType type;                   ///< option type
176  OptScopeFlags scope_flags;         ///< option scope flags, see OptScope
177  void *var;                         ///< global option: pointer to variable;
178                                     ///< window-local option: NULL;
179                                     ///< buffer-local option: global value
180  unsigned *flags_var;
181  ssize_t scope_idx[kOptScopeSize];  ///< index of option at every scope.
182  bool immutable;                    ///< option is immutable, trying to set it will give an error.
183
184  const char **values;               ///< possible values for string options
185  const size_t values_len;           ///< length of values array
186
187  /// Grammar of a dict option ("schema.dict" in options.lua); NULL otherwise.
188  const OptSchemaItem *schema;
189
190  /// Validate a candidate value before storing it, without side effects.
191  opt_validate_cb_T opt_validate_cb;
192
193  /// Apply the stored value. Some handlers still perform option-specific validation.
194  opt_did_set_cb_T opt_did_set_cb;
195
196  /// callback function to invoke when expanding possible values on the
197  /// cmdline. Only useful for string options.
198  opt_expand_cb_T opt_expand_cb;
199
200  Object def_val;                    ///< default value
201  sctx_T script_ctx;                 ///< script in which the option was last set
202} vimoption_T;