Obtain name and module of function running

Is there a way, from basic script, to obtain at runtime the name and module of the function running and possibly, after a function call, the name and module of the calling function?

1 Like

No. It is impossible in StarBasic. Not even in debug mode where the list of calls only shows the routine names without library and module.

Good question. I’ll create an enhancement request (to determine the library, module, macro name of the script being executed). Something like GetScriptURI().

1 Like

Thank you both. With “traditional” tools I was already quite convinced that it was not possible. I was hoping for some lesser known API.
Even in the latest releases of ScriptForge, I haven’t found anything like it.

Better ask about the problem you want to solve, NOT about how you think it can be solved!

You could use another macro language (such as Python) with any IDE you prefer.

Thanks everyone. I don’t know Python, I work in basic scripts. In the management of “on error goto” I always call a parameterized function to display errors and messages, and I wanted to integrate it with the name of the function and of the module that had generated the error.
For this, as a first attempt, I was looking for the solution in basic script.

I write the name of function to the bug calling

Sub myBigFunction
	on local error goto bug
	rem code
	exit sub
	bug(Err, Erl, Error, "myBigFunction") 'name of function written as parameter
End Sub

Sub bug(sErr$, sErl$, sError$, sFce$)
	msgbox("line: " & sErl & chr(13) & sErr & ": " & sError, 16, sFce)
	stop 'don't show bug alerts from more functions 
End Sub
1 Like

Thanks. That is exactly what originated my request. I also have a similar user function (err_handler) called from the label L (… on error goto L …)
Each of my modules contains a module constant (sModName) with the name of the module. Currently I call “err_handler” passing as fixed parameters sModName and the name of the function, and as variable parameters specific messages related to the specific function.
But when you have a library with many modules each with more than 20-30 constantly evolving functions, having two more parameters to add each time makes the code heavier
If your “bug” function from the calling code could have found by itself the path of the function (function-module-library name) it was much more convenient

In addition to the above, sometimes macros are called using the invoke method, in which case you need to know the names of the library, module, macro…
For such cases, I am forced to have a “special” module in the library (below is a fragment):

' --------------------------------------------------------------------------------------------------------------------------------------
' lang:en
' Returns the name of this library.
Function ThisLib_Name() As String
End Function

' --------------------------------------------------------------------------------------------------------------------------------------
' lang:en
' Returns the type of this library.
' Types:
' `0` document library.
' `1` user library (My Macros & Dialogs).
' `2` LO application library (LibreOffice Macros & Dialogs).
Function ThisLib_Type() As Long
End Function

Unfortunately there isn’t a possibility how to do it in Basic :-(.

I use extension BasicIDETools Basic IDE Tools | Apache OpenOffice Extensions with some my fixes and modifications, and inspired by this extension I add one icon to the Basic Toolbar with macro VlozFci that inserts the code for new function. Maybe you can modified it and create a dilaog window also with listbox of Basic Libraries instead only simple msgbox.

Sub VlozFci 'put the code to the Basic Editor
	dim s$, p()
	s=inputbox("F name = Function name"& chr(13) &"S name = Sub name", "Function or Sub", "F ")
	if s="" then exit sub
	p=split(s, " ")
	if p(0)="S" then p(0)="Sub" else p(0)="Function"
	s=chr(13) & p(0) &" "& p(1) & iif(p(0)="Sub", "", "()") & chr(13) & chr(9) &"on local error goto bug"& chr(13) & chr(9) & chr(13) & chr(9) & "exit "& LCase(p(0)) & chr(13) &"bug:" & chr(13) & chr(9) & "bug(Erl, Erl, Error, """& p(1) &""")" & chr(13) & "End " & p(0) & chr(13)
	dim document as object, dispatcher as object
	dispatcher.executeDispatch(document, ".uno:Paste", "", 0, array())
End Sub

rem ----- operations with system clipboard
global gsClipboard as string

Function LclipboardgetTransferData( aFlavor as com.sun.star.datatransfer.DataFlavor)
	if (aFlavor.MimeType="text/plain;charset=utf-16") then
	end if
End Function

Function LclipboardgetTransferDataFlavors()
	dim aFlavor as new com.sun.star.datatransfer.DataFlavor
End Function

Function LclipboardisDataFlavorSupported( aFlavor as com.sun.star.datatransfer.DataFlavor) as boolean
	if aFlavor.MimeType="text/plain;charset=utf-16" then
	end if
End Function

Sub vynulujSchranku 'delete clipboard
	dim oClip as object
	oClip=createUNOService( "com.sun.star.datatransfer.clipboard.SystemClipboard")
	oClip.setContents(createUNOListener("Lclipboard", "com.sun.star.datatransfer.XTransferable"), Null)
	wait 50
End Sub

Sub vlozDoSchranky(optional s$) 'put text to clipboard
	if isMissing(s) then s=""
	dim oClip as object
	oClip=createUNOService( "com.sun.star.datatransfer.clipboard.SystemClipboard")
	oClip.setContents(createUNOListener("Lclipboard", "com.sun.star.datatransfer.XTransferable") ,Null)
End Sub

Function vypisTextSchranky() as string 'show text from clipboard
	dim oClip, oConv, oCont, oTyps, i%
	for i=lbound(oTyps) to ubound(oTyps)
		if oTyps(i).MimeType="text/plain;charset=utf-16" then
			msgbox oConv.convertToSimpleType(oCont.getTransferData(oTyps(i)), com.sun.star.uno.TypeClass.STRING)
			exit for
		end if
End Function

Function oDocBasicIDE() as object 'get window of Basic Editor
	dim oComponents as object, oComponent as object
	if (NOT isNull(oComponents)) then
		do while oComponents.hasMoreElements()
			on local error goto preskoc
			if oComponent.supportsService("com.sun.star.script.BasicIDE") then
				exit do
			end if
			on local error goto 0
	end if
End Function