Jump to content

same defun names (but deference codes)in two programs


Recommended Posts

Posted

Two lisp programs loaded.There are same defun names (but deference codes)in those programs.

When I use the command of first program it working. But when I use command of second program, it running as first program.

If I load and run the second program, it is ok.

Can you give me an idea to solve this.

Posted

What are the defun names? Are they both (defun c:???) is so then rename one of them.

Posted

It can do as you say. But I need some other method. Because too meany name I have to change.

Posted

There is no other option. With two functions named the same, the last one loaded will replace the first one loaded. So you need to rename one or continually reload the one you need.

 

Find a decent notepad replacement that has a "Replace" command (Notepad++). This will allow you to highlight the first occurance of the string, specify the string to replace it (whole word only option) and replace every occurance of that string in the file in one go or on a string by string basis. Even using string by string it's a whole lot faster than finding each occurance and manually changing each one.

Posted

Alternatively you could define separate scopes -

 

For instance you have defined these functions:

(defun C:test ( / )
  (alert (strcat (alerting) ", guys"))
  (princ)
)

(defun alerting nil
  "Hello"
)



(defun C:test ( / )
  (alert (strcat (alerting) ", there"))
  (princ)
)

(defun alerting nil
  "Hi"
)

 

You can wrap them in defuns with different names, but localise all the functions and subfunctions within:


(defun C:test1 ( / C:test alerting )
  
  (defun C:test ( / )
    (alert (strcat (alerting) ", guys"))
    (princ)
  )
  
  (defun alerting nil
    "Hello"
  )
  
  (C:test)
  (princ)
)


(defun C:test2 ( / C:test alerting )
  
  (defun C:test ( / )
    (alert (strcat (alerting) ", there"))
    (princ)
  )
  
  (defun alerting nil
    "Hi"
  )
  
  (C:test)
  (princ)
)

 

Thats one of the reasons I keep my smaller subs localised, since they are generic and I might have been used similar in other .lsp file - like this one:

(setq fill_listbox (lambda (k L) (start_list k) (mapcar 'add_list L) (end_list)))

And other subfunctions of mine that are 200+ rows and almost no risk of me to overwrite them, as global - such as "MergePDFs" or "AddMyLayers".

 

  • Like 2
Posted (edited)

Its maybe time to think about a index list what defun is used with which program. You could use a lisp to look for "defun" in  a list of lisps, reading each line as it is a text file then write filename and defun name and so on. 

 

Do this first from Windows type CMD open the command window use the old fashioned DOS and change to your lisp directory 

Type findstr defun *.lsp >defunlist.txt and all should be revealed when you open defunlist.txt.

 

image.png.2656363135fc125e39c2d644277f1777.pngimage.png.2656363135fc125e39c2d644277f1777.png

Edited by BIGAL
  • Like 1
Posted (edited)

One could use a wrapper like this, although It won't make any sense because if one knows how to properly use it will also know how to properly define his functions.

 

(defun DefineScope ( fn call body / args )
  (setq args (cons '/ (mapcar 'eval body)))
  (foreach x (cdr args)
    (if (/= (substr (vl-symbol-name x) 1 2) "C:")
      (set x nil)
    )
  )
  (eval (cons 'defun (append (list fn args) body call '((princ)))))
); defun DefineScope

 

Sample usage:

(DefineScope 'C:test1 '( (alert "Now calling (test1) and (test2) \nand defining (C:testfoo)") (test1)  (test2) )
  '( 
    (defun C:testfoo nil
      (alert "I'm testfoo,\n and I'm not dependent on the internal subfunctions") 
      (princ)
    )
    (defun test1 ( / )
      (mySub1 "Hi")
    )
    (defun test2 ( / )
      (mySub2 "Hello")
    )
    (defun mySub1 (x) (alert x) )
    (defun mySub2 (x) (alert (strcat x ", there")) )
  ); list
); DefineScope


(DefineScope 'C:test2 '( (alert "Now calling (test1) and (test2)") (test1)  (test2) )
  '( 
    (defun test1 ( / )
      (mySub1 "Hi")
    )
    (defun test2 ( / )
      (mySub2 "Hello")
    )
    (defun mySub1 (x) (alert (strcat x " guys")) )
    (defun mySub2 (x) (alert (strcat x ", all")) )
  ); list
); DefineScope

 

So basically whats defined within the body that doesn't have the "C:" prefix will be private -

test1 >> nil
test2 >> nil
mySub1 >> nil
mySub2 >> nil
c:test1 >> #<USUBR @0000008065e772f0 C:TEST1>
c:test2 >> #<USUBR @0000008065e77548 C:TEST2>
C:testfoo >> #<USUBR @0000008065e77098 C:TESTFOO>

 

Edited by Grrr
Posted

Ideally you would rename one or the other LISP.

If the LISP with the duplicated name is called / run from another LISP as  a subroutine you could simply reload the file containing the duplicate named LISP as required and make it the most recent one to be loaded.

Use a line similar to this.

(load "-filepath and LISP file name remember to use \\ insted of single \ as required - " "LISP Failed to Load")

So long as the file name and location won't change this should work - not ideal mind,

  • Like 1

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...