Jump to content

How to obtain function's name?


Recommended Posts

Posted

Hi,

 

I need to convert current function's name (defined by defun) into string and use this string within the function... Is it possible?

 

Thanks a lot for answer!

Posted

Using atoms-family is the only thing I can think of right now - why would you want to do this?

Posted

Could you post a sample code of what you are trying to achieve?

Posted

Something using reactors but I am not sure if its what you are after..

 

Catch is it only returns the inital function name

 

;Create a command reactor to watch for lisp functions
(vlr-lisp-reactor nil '((:vlr-lispWillStart . storeFunctionName)))

;Store the command name in a global variable called *currentLispFunction*

(defun storeFunctionName (<Reactor> <Command>)
  
(setq *currentLispFunction*  (car <Command>))
 )


(defun c:test ()

 (alert  (strcat "\nFunction [" *currentLispFunction* "] started..."))

   )

Posted

I am just beginner in AutoLISP and I thought there is easier way than reactors. Maybe I'll try to find different solution...

 

Thanks a lot for your answers!

Posted

Here's what I have:

 

[color=#8b4513];converts a quoted symbol to a string[/color]
[b][color=BLACK]([/color][/b]defun symstr [b][color=FUCHSIA]([/color][/b]name[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]if [b][color=NAVY]([/color][/b]= [b][color=MAROON]([/color][/b]type name[b][color=MAROON])[/color][/b] 'SYM[b][color=NAVY])[/color][/b]
   [b][color=NAVY]([/color][/b]
     [b][color=MAROON]([/color][/b]list
       [b][color=GREEN]([/color][/b]if [b][color=BLUE]([/color][/b]boundp name[b][color=BLUE])[/color][/b]
         [b][color=BLUE]([/color][/b]cons '/ [b][color=RED]([/color][/b]atoms-family 0[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
         [b][color=BLUE]([/color][/b]cons '/ [b][color=RED]([/color][/b]cons name [b][color=PURPLE]([/color][/b]atoms-family 0[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
       [b][color=GREEN])[/color][/b]
       [b][color=GREEN]([/color][/b]list setq name 0[b][color=GREEN])[/color][/b]
       [b][color=GREEN]([/color][/b]list car [b][color=BLUE]([/color][/b]list atoms-family 1[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
     [b][color=MAROON])[/color][/b]
   [b][color=NAVY])[/color][/b]
   nil
 [b][color=FUCHSIA])[/color][/b]
[b][color=BLACK])[/color][/b]

 

But your profile says LT?

 

-David

Posted

If you have the Symbol, could you not just use:

 

(vl-princ-to-string sym)

Posted
If you have the Symbol, could you not just use:

 

(vl-princ-to-string sym)

 

Lee,

 

You've got me. :cry: -David

Posted

Nah - I know you don't have access to the VL functions David, it was a bit unfair on you.

 

But, going back to your example - I can't quite understand the method that you are using, if you have a moment, would you mind explaining the thought process please? :)

Posted

Lee,

 

I didn't write this one. But I think it first verifies the argument to be a symbol. Then check to see if it is bound to a value, if not simply add the symbol to atoms-family. I don't quite undertstand the setq name 0 statement. The last returns the newly added symbol as a string. -David

Posted

OK I think I understand it now, this is how I understand it:

 

;converts a quoted symbol to a string

(defun symstr (name)
 
 (if (= (type name) 'SYM)
   
   (
     (list
       
       (if (boundp name)
         
         (cons '/ (atoms-family 0))

         ; ==>   (/ sym1 sym2 sym3 ... symN)
         
         (cons '/ (cons name (atoms-family 0)))

         ; ==>   (/ name sym1 sym2 sym3 ... symN)
       )
       
       (list setq name 0)
       
       (list car (list atoms-family 1))
       
     )

     ; If symbol has value:

     ; (  (lambda (/ sym1 sym2 sym3 ... symN)
     ;    (setq name 0)
     ;    (car (atoms-family 1))

     ; Else:

     ; (  (lambda (/ name sym1 sym2 sym3 ... symN)
     ;    (setq name 0)
     ;    (car (atoms-family 1))

   )
   nil
 )
)

The list structure is that of a lambda function, and it sets all symbols to nil, then bounds the value 0 to the desired symbol - hence atoms-family will only return that symbol.

 

However it seems to fail as "/" is contained in the atoms-family list as a symbol, and hence there is an error as two "/" symbols will be found in the arguments list.

 

Lee

Posted

(snvalid "/") returns nil in 2000. Has that changed?

 

(type '/) returns sym

 

So I guess it needs a some additional parameter checking. -David

Posted

(snvalid "/") ==> nil 

 

But '/' is a protected symbol and so will appear in the atoms-family list, so perhaps a vl-remove may be needed.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...