aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/runtime.c
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2022-08-20 07:54:14 +0800
committerzeertzjq <zeertzjq@outlook.com>2022-08-23 17:47:46 +0800
commitffa1335047047ac00280ac742bcc6dfcc7fa3589 (patch)
treeb71be77bcee6dfb6e3f96f154c2e8a77cdf4170d /src/nvim/runtime.c
parent42e9fe7d958e0ba025034c330d8e29293d828b60 (diff)
downloadrneovim-ffa1335047047ac00280ac742bcc6dfcc7fa3589.tar.gz
rneovim-ffa1335047047ac00280ac742bcc6dfcc7fa3589.tar.bz2
rneovim-ffa1335047047ac00280ac742bcc6dfcc7fa3589.zip
vim-patch:8.2.4726: cannot use expand() to get the script name
Problem: Cannot use expand() to get the script name. Solution: Support expand('<script>'). (closes vim/vim#10121) https://github.com/vim/vim/commit/6013d0045dec7ca7c0068fbe186c42d754a7368b Use `.sn_name` instead of `->sn_name` as v8.2.0154 hasn't been ported. Cherry-pick builtin.txt expand() doc from latest Vim.
Diffstat (limited to 'src/nvim/runtime.c')
-rw-r--r--src/nvim/runtime.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/nvim/runtime.c b/src/nvim/runtime.c
index 537c496ae2..412b78728e 100644
--- a/src/nvim/runtime.c
+++ b/src/nvim/runtime.c
@@ -100,7 +100,8 @@ void estack_pop(void)
}
/// Get the current value for <sfile> in allocated memory.
-/// @param which ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
+/// @param which ESTACK_SFILE for <sfile>, ESTACK_STACK for <stack> or
+/// ESTACK_SCRIPT for <script>.
char *estack_sfile(estack_arg_T which)
{
estack_T *entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
@@ -111,6 +112,27 @@ char *estack_sfile(estack_arg_T which)
return xstrdup(entry->es_name);
}
+ // If evaluated in a function return the path of the script where the
+ // function is defined, at script level the current script path is returned
+ // instead.
+ if (which == ESTACK_SCRIPT) {
+ if (entry->es_type == ETYPE_UFUNC) {
+ sctx_T *def_ctx = &entry->es_info.ufunc->uf_script_ctx;
+ if (def_ctx->sc_sid > 0) {
+ return xstrdup((char *)(SCRIPT_ITEM(def_ctx->sc_sid).sn_name));
+ }
+ } else if (exestack.ga_len > 0) {
+ // Walk the stack backwards, starting from the current frame.
+ for (int idx = exestack.ga_len - 1; idx; idx--) {
+ entry = ((estack_T *)exestack.ga_data) + idx;
+ if (entry->es_type == ETYPE_SCRIPT) {
+ return xstrdup(entry->es_name);
+ }
+ }
+ }
+ return NULL;
+ }
+
// Give information about each stack entry up to the root.
// For a function we compose the call stack, as it was done in the past:
// "function One[123]..Two[456]..Three"