diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-08-23 18:12:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-23 18:12:28 +0800 |
commit | df4709ddf6d1ed524adae9373ecb762b9db11814 (patch) | |
tree | d65ac06e9560138c20f09d6f8269b4d9e2d29713 /src/nvim/runtime.c | |
parent | 42e9fe7d958e0ba025034c330d8e29293d828b60 (diff) | |
parent | d459b6687704b7d1f230d0b14c0d59f87cf5f67d (diff) | |
download | rneovim-df4709ddf6d1ed524adae9373ecb762b9db11814.tar.gz rneovim-df4709ddf6d1ed524adae9373ecb762b9db11814.tar.bz2 rneovim-df4709ddf6d1ed524adae9373ecb762b9db11814.zip |
Merge pull request #19905 from zeertzjq/vim-8.2.4726
vim-patch:8.2.{4726,4740,4741,4749,4841,4842}: expand('<script>')
Diffstat (limited to 'src/nvim/runtime.c')
-rw-r--r-- | src/nvim/runtime.c | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/nvim/runtime.c b/src/nvim/runtime.c index 537c496ae2..038aad12a4 100644 --- a/src/nvim/runtime.c +++ b/src/nvim/runtime.c @@ -100,10 +100,11 @@ 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; + const estack_T *entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1; if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC) { if (entry->es_name == NULL) { return NULL; @@ -111,6 +112,36 @@ char *estack_sfile(estack_arg_T which) return xstrdup(entry->es_name); } + // If evaluated in a function or autocommand, return the path of the script + // where it is defined, at script level the current script path is returned + // instead. + if (which == ESTACK_SCRIPT) { + assert(entry == ((estack_T *)exestack.ga_data) + exestack.ga_len - 1); + // Walk the stack backwards, starting from the current frame. + for (int idx = exestack.ga_len - 1; idx >= 0; idx--, entry--) { + if (entry->es_type == ETYPE_UFUNC) { + const sctx_T *const 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 { + return NULL; + } + } else if (entry->es_type == ETYPE_AUCMD) { + const sctx_T *const def_ctx = &entry->es_info.aucmd->script_ctx; + + if (def_ctx->sc_sid > 0) { + return xstrdup((char *)(SCRIPT_ITEM(def_ctx->sc_sid).sn_name)); + } else { + return NULL; + } + } else 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" |