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 #10121)
diff --git a/src/scriptfile.c b/src/scriptfile.c
index ec47edf..7e14cdc 100644
--- a/src/scriptfile.c
+++ b/src/scriptfile.c
@@ -118,7 +118,8 @@
/*
* Get the current value for <sfile> in allocated memory.
- * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
+ * "which" is ESTACK_SFILE for <sfile>, ESTACK_STACK for <stack> or
+ * ESTACK_SCRIPT for <script>.
*/
char_u *
estack_sfile(estack_arg_T which UNUSED)
@@ -156,6 +157,32 @@
return NULL;
}
+ // 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 vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name);
+ }
+ else if (exestack.ga_len > 0)
+ {
+ // Walk the stack backwards, starting from the current frame.
+ for (idx = exestack.ga_len - 1; idx; --idx)
+ {
+ entry = ((estack_T *)exestack.ga_data) + idx;
+
+ if (entry->es_type == ETYPE_SCRIPT)
+ return vim_strsave(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"