mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
feat(api): add filetype option nvim_get_option_value
- Also adjust the expr-mapping behaviour so normal commands and text changes are allowed in internal dummy buffers.
This commit is contained in:
parent
84027f7515
commit
e1db0e35e4
@ -1969,6 +1969,10 @@ nvim_get_option_value({name}, {*opts}) *nvim_get_option_value()*
|
|||||||
• win: |window-ID|. Used for getting window local options.
|
• win: |window-ID|. Used for getting window local options.
|
||||||
• buf: Buffer number. Used for getting buffer local options.
|
• buf: Buffer number. Used for getting buffer local options.
|
||||||
Implies {scope} is "local".
|
Implies {scope} is "local".
|
||||||
|
• filetype: |filetype|. Used to get the default option for a
|
||||||
|
specific filetype. Cannot be used with any other option.
|
||||||
|
Note: this is expensive, it is recommended to cache this
|
||||||
|
value.
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
Option value
|
Option value
|
||||||
|
@ -210,6 +210,9 @@ The following new APIs or features were added.
|
|||||||
https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection .
|
https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection .
|
||||||
Support for the previous format will be removed in a future release.
|
Support for the previous format will be removed in a future release.
|
||||||
|
|
||||||
|
• |nvim_get_option_value()| now has a `filetype` option so it can return the
|
||||||
|
default option for a specific filetype.
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
CHANGED FEATURES *news-changes*
|
CHANGED FEATURES *news-changes*
|
||||||
|
|
||||||
|
@ -102,6 +102,7 @@ return {
|
|||||||
"scope";
|
"scope";
|
||||||
"win";
|
"win";
|
||||||
"buf";
|
"buf";
|
||||||
|
"filetype";
|
||||||
}};
|
}};
|
||||||
{ 'highlight', {
|
{ 'highlight', {
|
||||||
"bold";
|
"bold";
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int validate_option_value_args(Dict(option) *opts, int *scope, int *opt_type, void **from,
|
static int validate_option_value_args(Dict(option) *opts, int *scope, int *opt_type, void **from,
|
||||||
Error *err)
|
char **filetype, Error *err)
|
||||||
{
|
{
|
||||||
if (HAS_KEY(opts->scope)) {
|
if (HAS_KEY(opts->scope)) {
|
||||||
VALIDATE_T("scope", kObjectTypeString, opts->scope.type, {
|
VALIDATE_T("scope", kObjectTypeString, opts->scope.type, {
|
||||||
@ -44,6 +44,14 @@ static int validate_option_value_args(Dict(option) *opts, int *scope, int *opt_t
|
|||||||
|
|
||||||
*opt_type = SREQ_GLOBAL;
|
*opt_type = SREQ_GLOBAL;
|
||||||
|
|
||||||
|
if (filetype != NULL && HAS_KEY(opts->filetype)) {
|
||||||
|
VALIDATE_T("scope", kObjectTypeString, opts->filetype.type, {
|
||||||
|
return FAIL;
|
||||||
|
});
|
||||||
|
|
||||||
|
*filetype = opts->filetype.data.string.data;
|
||||||
|
}
|
||||||
|
|
||||||
if (HAS_KEY(opts->win)) {
|
if (HAS_KEY(opts->win)) {
|
||||||
VALIDATE_T("win", kObjectTypeInteger, opts->win.type, {
|
VALIDATE_T("win", kObjectTypeInteger, opts->win.type, {
|
||||||
return FAIL;
|
return FAIL;
|
||||||
@ -69,10 +77,17 @@ static int validate_option_value_args(Dict(option) *opts, int *scope, int *opt_t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALIDATE((!HAS_KEY(opts->filetype)
|
||||||
|
|| !(HAS_KEY(opts->buf) || HAS_KEY(opts->scope) || HAS_KEY(opts->win))),
|
||||||
|
"%s", "cannot use 'filetype' with 'scope', 'buf' or 'win'", {
|
||||||
|
return FAIL;
|
||||||
|
});
|
||||||
|
|
||||||
VALIDATE((!HAS_KEY(opts->scope) || !HAS_KEY(opts->buf)), "%s",
|
VALIDATE((!HAS_KEY(opts->scope) || !HAS_KEY(opts->buf)), "%s",
|
||||||
"cannot use both 'scope' and 'buf'", {
|
"cannot use both 'scope' and 'buf'", {
|
||||||
return FAIL;
|
return FAIL;
|
||||||
});
|
});
|
||||||
|
|
||||||
VALIDATE((!HAS_KEY(opts->win) || !HAS_KEY(opts->buf)), "%s", "cannot use both 'buf' and 'win'", {
|
VALIDATE((!HAS_KEY(opts->win) || !HAS_KEY(opts->buf)), "%s", "cannot use both 'buf' and 'win'", {
|
||||||
return FAIL;
|
return FAIL;
|
||||||
});
|
});
|
||||||
@ -80,6 +95,30 @@ static int validate_option_value_args(Dict(option) *opts, int *scope, int *opt_t
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a dummy buffer and run the FileType autocmd on it.
|
||||||
|
static buf_T *do_ft_buf(char *filetype, aco_save_T *aco, Error *err)
|
||||||
|
{
|
||||||
|
if (filetype == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate a buffer without putting it in the buffer list.
|
||||||
|
buf_T *ftbuf = buflist_new(NULL, NULL, 1, BLN_DUMMY);
|
||||||
|
if (ftbuf == NULL) {
|
||||||
|
api_set_error(err, kErrorTypeException, "Could not create internal buffer");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set curwin/curbuf to buf and save a few things.
|
||||||
|
aucmd_prepbuf(aco, ftbuf);
|
||||||
|
|
||||||
|
ftbuf->b_p_ft = xstrdup(filetype);
|
||||||
|
|
||||||
|
apply_autocmds(EVENT_FILETYPE, ftbuf->b_p_ft, ftbuf->b_fname, true, ftbuf);
|
||||||
|
|
||||||
|
return ftbuf;
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets the value of an option. The behavior of this function matches that of
|
/// Gets the value of an option. The behavior of this function matches that of
|
||||||
/// |:set|: the local value of an option is returned if it exists; otherwise,
|
/// |:set|: the local value of an option is returned if it exists; otherwise,
|
||||||
/// the global value is returned. Local values always correspond to the current
|
/// the global value is returned. Local values always correspond to the current
|
||||||
@ -92,6 +131,10 @@ static int validate_option_value_args(Dict(option) *opts, int *scope, int *opt_t
|
|||||||
/// - win: |window-ID|. Used for getting window local options.
|
/// - win: |window-ID|. Used for getting window local options.
|
||||||
/// - buf: Buffer number. Used for getting buffer local options.
|
/// - buf: Buffer number. Used for getting buffer local options.
|
||||||
/// Implies {scope} is "local".
|
/// Implies {scope} is "local".
|
||||||
|
/// - filetype: |filetype|. Used to get the default option for a
|
||||||
|
/// specific filetype. Cannot be used with any other option.
|
||||||
|
/// Note: this is expensive, it is recommended to cache this
|
||||||
|
/// value.
|
||||||
/// @param[out] err Error details, if any
|
/// @param[out] err Error details, if any
|
||||||
/// @return Option value
|
/// @return Option value
|
||||||
Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
|
Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
|
||||||
@ -102,14 +145,37 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
|
|||||||
int scope = 0;
|
int scope = 0;
|
||||||
int opt_type = SREQ_GLOBAL;
|
int opt_type = SREQ_GLOBAL;
|
||||||
void *from = NULL;
|
void *from = NULL;
|
||||||
if (!validate_option_value_args(opts, &scope, &opt_type, &from, err)) {
|
char *filetype = NULL;
|
||||||
|
|
||||||
|
if (!validate_option_value_args(opts, &scope, &opt_type, &from, &filetype, err)) {
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
aco_save_T aco;
|
||||||
|
|
||||||
|
buf_T *ftbuf = do_ft_buf(filetype, &aco, err);
|
||||||
|
if (ERROR_SET(err)) {
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ftbuf != NULL) {
|
||||||
|
assert(!from);
|
||||||
|
from = ftbuf;
|
||||||
|
}
|
||||||
|
|
||||||
long numval = 0;
|
long numval = 0;
|
||||||
char *stringval = NULL;
|
char *stringval = NULL;
|
||||||
getoption_T result = access_option_value_for(name.data, &numval, &stringval, scope, opt_type,
|
getoption_T result = access_option_value_for(name.data, &numval, &stringval, scope, opt_type,
|
||||||
from, true, err);
|
from, true, err);
|
||||||
|
|
||||||
|
if (ftbuf != NULL) {
|
||||||
|
// restore curwin/curbuf and a few other things
|
||||||
|
aucmd_restbuf(&aco);
|
||||||
|
|
||||||
|
assert(curbuf != ftbuf); // safety check
|
||||||
|
wipe_buffer(ftbuf, false);
|
||||||
|
}
|
||||||
|
|
||||||
if (ERROR_SET(err)) {
|
if (ERROR_SET(err)) {
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
@ -164,7 +230,7 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
|
|||||||
int scope = 0;
|
int scope = 0;
|
||||||
int opt_type = SREQ_GLOBAL;
|
int opt_type = SREQ_GLOBAL;
|
||||||
void *to = NULL;
|
void *to = NULL;
|
||||||
if (!validate_option_value_args(opts, &scope, &opt_type, &to, err)) {
|
if (!validate_option_value_args(opts, &scope, &opt_type, &to, NULL, err)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1245,7 +1245,9 @@ bool edit(int cmdchar, bool startln, long count)
|
|||||||
// Don't allow changes in the buffer while editing the cmdline. The
|
// Don't allow changes in the buffer while editing the cmdline. The
|
||||||
// caller of getcmdline() may get confused.
|
// caller of getcmdline() may get confused.
|
||||||
// Don't allow recursive insert mode when busy with completion.
|
// Don't allow recursive insert mode when busy with completion.
|
||||||
if (textlock != 0 || ins_compl_active() || compl_busy || pum_visible()) {
|
// Allow in dummy buffers since they are only used internally
|
||||||
|
if (textlock != 0 || ins_compl_active() || compl_busy || pum_visible()
|
||||||
|
|| expr_map_locked()) {
|
||||||
emsg(_(e_textlock));
|
emsg(_(e_textlock));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -6326,6 +6326,11 @@ void restore_current_state(save_state_T *sst)
|
|||||||
ui_cursor_shape(); // may show different cursor shape
|
ui_cursor_shape(); // may show different cursor shape
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool expr_map_locked(void)
|
||||||
|
{
|
||||||
|
return expr_map_lock > 0 && !(curbuf->b_flags & BF_DUMMY);
|
||||||
|
}
|
||||||
|
|
||||||
/// ":normal[!] {commands}": Execute normal mode commands.
|
/// ":normal[!] {commands}": Execute normal mode commands.
|
||||||
static void ex_normal(exarg_T *eap)
|
static void ex_normal(exarg_T *eap)
|
||||||
{
|
{
|
||||||
@ -6335,10 +6340,11 @@ static void ex_normal(exarg_T *eap)
|
|||||||
}
|
}
|
||||||
char *arg = NULL;
|
char *arg = NULL;
|
||||||
|
|
||||||
if (ex_normal_lock > 0) {
|
if (expr_map_locked()) {
|
||||||
emsg(_(e_secure));
|
emsg(_(e_secure));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ex_normal_busy >= p_mmd) {
|
if (ex_normal_busy >= p_mmd) {
|
||||||
emsg(_("E192: Recursive use of :normal too deep"));
|
emsg(_("E192: Recursive use of :normal too deep"));
|
||||||
return;
|
return;
|
||||||
|
@ -2740,6 +2740,9 @@ bool text_locked(void)
|
|||||||
if (cmdwin_type != 0) {
|
if (cmdwin_type != 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (expr_map_locked()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return textlock != 0;
|
return textlock != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -715,7 +715,7 @@ EXTERN typebuf_T typebuf INIT(= { NULL, NULL, 0, 0, 0, 0, 0, 0, 0 });
|
|||||||
EXTERN bool typebuf_was_empty INIT(= false);
|
EXTERN bool typebuf_was_empty INIT(= false);
|
||||||
|
|
||||||
EXTERN int ex_normal_busy INIT(= 0); // recursiveness of ex_normal()
|
EXTERN int ex_normal_busy INIT(= 0); // recursiveness of ex_normal()
|
||||||
EXTERN int ex_normal_lock INIT(= 0); // forbid use of ex_normal()
|
EXTERN int expr_map_lock INIT(= 0); // running expr mapping, prevent use of ex_normal() and text changes
|
||||||
EXTERN int ignore_script INIT(= false); // ignore script input
|
EXTERN int ignore_script INIT(= false); // ignore script input
|
||||||
EXTERN int stop_insert_mode; // for ":stopinsert"
|
EXTERN int stop_insert_mode; // for ":stopinsert"
|
||||||
EXTERN bool KeyTyped; // true if user typed current char
|
EXTERN bool KeyTyped; // true if user typed current char
|
||||||
|
@ -1615,8 +1615,7 @@ char *eval_map_expr(mapblock_T *mp, int c)
|
|||||||
|
|
||||||
// Forbid changing text or using ":normal" to avoid most of the bad side
|
// Forbid changing text or using ":normal" to avoid most of the bad side
|
||||||
// effects. Also restore the cursor position.
|
// effects. Also restore the cursor position.
|
||||||
textlock++;
|
expr_map_lock++;
|
||||||
ex_normal_lock++;
|
|
||||||
set_vim_var_char(c); // set v:char to the typed character
|
set_vim_var_char(c); // set v:char to the typed character
|
||||||
const pos_T save_cursor = curwin->w_cursor;
|
const pos_T save_cursor = curwin->w_cursor;
|
||||||
const int save_msg_col = msg_col;
|
const int save_msg_col = msg_col;
|
||||||
@ -1637,8 +1636,7 @@ char *eval_map_expr(mapblock_T *mp, int c)
|
|||||||
p = eval_to_string(expr, NULL, false);
|
p = eval_to_string(expr, NULL, false);
|
||||||
xfree(expr);
|
xfree(expr);
|
||||||
}
|
}
|
||||||
textlock--;
|
expr_map_lock--;
|
||||||
ex_normal_lock--;
|
|
||||||
curwin->w_cursor = save_cursor;
|
curwin->w_cursor = save_cursor;
|
||||||
msg_col = save_msg_col;
|
msg_col = save_msg_col;
|
||||||
msg_row = save_msg_row;
|
msg_row = save_msg_row;
|
||||||
|
@ -96,6 +96,7 @@
|
|||||||
#include "nvim/eval/typval.h"
|
#include "nvim/eval/typval.h"
|
||||||
#include "nvim/eval/typval_defs.h"
|
#include "nvim/eval/typval_defs.h"
|
||||||
#include "nvim/ex_cmds_defs.h"
|
#include "nvim/ex_cmds_defs.h"
|
||||||
|
#include "nvim/ex_docmd.h"
|
||||||
#include "nvim/ex_getln.h"
|
#include "nvim/ex_getln.h"
|
||||||
#include "nvim/extmark.h"
|
#include "nvim/extmark.h"
|
||||||
#include "nvim/fileio.h"
|
#include "nvim/fileio.h"
|
||||||
@ -300,7 +301,7 @@ bool undo_allowed(buf_T *buf)
|
|||||||
|
|
||||||
// Don't allow changes in the buffer while editing the cmdline. The
|
// Don't allow changes in the buffer while editing the cmdline. The
|
||||||
// caller of getcmdline() may get confused.
|
// caller of getcmdline() may get confused.
|
||||||
if (textlock != 0) {
|
if (textlock != 0 || expr_map_locked()) {
|
||||||
emsg(_(e_textlock));
|
emsg(_(e_textlock));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user