vim9 Equivalence of __FILE__, __LINE__, and __FUNCTION__ usage in C++

36 views
Skip to first unread message

Nicolas

unread,
Jul 4, 2023, 10:06:05 AM7/4/23
to vim_use
Hi,

Is there an equivalence in vim9 of  __FILE__, __LINE__, and __FUNCTION__ usage in C++ ?

Thank you
Nicolas

Lifepillar

unread,
Jul 4, 2023, 4:11:31 PM7/4/23
to vim...@googlegroups.com
On 2023-07-04, Nicolas <niva...@gmail.com> wrote:
> Hi,
>
> Is there an equivalence in vim9 of __FILE__, __LINE__, and __FUNCTION__
> usage in C++
><https://stackoverflow.com/questions/597078/file-line-and-function-usage-in-c>

The closest to __FILE__ is likely <sfile> (`:help <sfile>`). I use this
snippet to get the path of the current script:

const PATH = resolve(expand('<sfile>:p'))

Or to source a file in the same directory as the current script:

source <sfile>:h/some_other_script.vim

For __LINE__ and __FUNCTION__, I don't know what "equivalent"
alternative to suggest, but if you elaborate on what your goal is, it
will be easier to help.

Life.

Nicolas

unread,
Jul 4, 2023, 4:30:17 PM7/4/23
to vim_use
Hi Life,

My Main goal is to add prefix of vim9script current '__FUNCTION__' to my debug message.

Thank you for help
Nicolas

Lifepillar

unread,
Jul 4, 2023, 4:47:37 PM7/4/23
to vim...@googlegroups.com
On 2023-07-04, Nicolas <niva...@gmail.com> wrote:
> Hi Life,
>
> My Main goal is to add prefix of vim9script current '__FUNCTION__' to my
> debug message.

Something along these lines, perhaps?

vim9script

def Debug(F: func)
echo string(F)
enddef

def Foo()
Debug(Foo)
enddef

def Bar()
Foo()
Debug(Bar)
enddef

Bar()

I don't think Vim 9 script has the kind of introspection you are asking
for.

Life.

Yegappan Lakshmanan

unread,
Jul 4, 2023, 7:08:01 PM7/4/23
to vim...@googlegroups.com
On Tue, Jul 4, 2023 at 1:11 PM Lifepillar <lifep...@lifepillar.me> wrote:
>
> On 2023-07-04, Nicolas <niva...@gmail.com> wrote:
> > Hi,
> >
> > Is there an equivalence in vim9 of __FILE__, __LINE__, and __FUNCTION__
> > usage in C++
> ><https://stackoverflow.com/questions/597078/file-line-and-function-usage-in-c>
>
>
> For __LINE__ and __FUNCTION__, I don't know what "equivalent"
> alternative to suggest, but if you elaborate on what your goal is, it
> will be easier to help.
>

You can try using <script>, <sflnum> or <stack>:

https://vimhelp.org/cmdline.txt.html#%3Cscript%3E
https://vimhelp.org/cmdline.txt.html#%3Csflnum%3E
https://vimhelp.org/cmdline.txt.html#%3Cstack%3E

- Yegappan

Bram Moolenaar

unread,
Jul 6, 2023, 10:19:01 PM7/6/23
to vim...@googlegroups.com, Yegappan Lakshmanan
Expanding "<stack>" gives you the most information. But the form is a
bit hard to parse, because it was kept compact. With some effort you
can parse it back into a list of function calls and script commands.
You can use "getscriptinfo()" to get the script file name from the
script ID, which is inside the function name after <SNR>:
<SNR>74_OuterFunc[7]

Here "74" is the script ID. Inside the script you may find
s:OuterFunc() or just OuterFunc() (esp. in Vim 9 script).

"[7]" indicates line 7 of the function. To see what script line that is
you can go to the line where the function is defined with ":func" or
":def" and then go that many lines down: "7j". When line continuation
is used it may be off by a few lines. And line insert/delete need to be
taken into account.

For interactive debugging the "where" command can be used. You need to
set a breakpoint or use ":debug command args". See ":help debug-mode".

--
Why is it that pizza is made round, packed in a square box,
and eaten as a triangle?

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Nicolas

unread,
Jul 7, 2023, 3:57:59 PM7/7/23
to vim...@googlegroups.com
Oh thank you a lot Bram and all of you. It's helpful to know where we come from.
In code, in Life too ;)

Thank you
Nicolas

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/ocLlQ9TpSXI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20230707021852.1BCCC1C09A9%40moolenaar.net.

Nicolas

unread,
Jul 7, 2023, 4:30:42 PM7/7/23
to vim...@googlegroups.com
This deal with <stack> is displaying last tree called function and line.

echomsg expand('<stack>')->split('<SNR>')[-1]->substitute('\d\+_\(\w\+\)\[\(\d\+\)\]', '\1: \2', "")

Thank you Bram.
Nicolas

Tom M

unread,
Jul 8, 2023, 4:28:16 PM7/8/23
to vim_use
I don't think Vim 9 script has the kind of introspection you are asking
for.

The v:throwpoint variable might help, as mentioned in :help throw-variables.

Tom

Nicolas

unread,
Jul 9, 2023, 2:49:04 PM7/9/23
to vim_use
Thank you Tom.
Reply all
Reply to author
Forward
0 new messages