Author Topic: Redefine defun?  (Read 380 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 352
Redefine defun?
« on: February 15, 2024, 03:29:24 PM »
Is it possible to redefine the defun function?  I would like to redefine defun so I could add a function call at the beginning of each procedure.


PKENEWELL

  • Bull Frog
  • Posts: 319
Re: Redefine defun?
« Reply #1 on: February 15, 2024, 04:52:57 PM »
I'm not exactly sure what you mean. If you literally mean changing the "defun" subr to some other name, then no. But you can redefine function names on the fly in AutoLisp. A great example of that is with built in "autoload" function. It writes a stub function that loads the Lisp file associated with the command, then rewrites itself when the lisp file is loaded.

This is pulled from the "acad2024doc.lsp" file showing how it is written. Warning, it's pretty advanced stuff:
Code - Auto/Visual Lisp: [Select]
  1. (defun autoload (app cmdliste)
  2.   (_autoqload "" app cmdliste)
  3. )
  4.  
  5. (defun autoarxload (app cmdliste)
  6.   (_autoqload "arx" app cmdliste)
  7. )
  8.  
  9. (defun _autoqload (quoi app cmdliste / qapp symnam)
  10.   (setq qapp (strcat "\"" app "\""))
  11.   (setq initstring "\nInitializing...")
  12.   (mapcar
  13.    '(lambda (cmd / nom_cmd)
  14.       (progn
  15.         (setq nom_cmd (strcat "C:" cmd))
  16.         (if (not (eval (read nom_cmd)))
  17.             (eval
  18.              (read (strcat
  19.                     "(defun " nom_cmd "( / rtn)"
  20.                     "(setq m:err *error* *error* *merrmsg*)"
  21.                     "(if (ai_ffile " qapp ")"
  22.                     "(progn (princ initstring)"
  23.                     "(_auto" quoi "load " qapp ") (setq rtn (" nom_cmd ")))"
  24.                     "(ai_nofile " qapp "))"
  25.                     "(setq *error* m:err m:err nil)"
  26.                     "rtn)"
  27.                     ))))))
  28.    cmdliste)
  29.   nil
  30. )
  31.  
  32. (defun _autoload (app)
  33. ; (princ "Auto:(load ") (princ app) (princ ")") (terpri)
  34.   (load app)
  35. )
  36.  
  37. ; (princ "Auto:(arxload ") (princ app) (princ ")") (terpri)
  38.   (arxload app)
  39. )
  40.  
  41. (defun ai_nofile (filename)
  42.   (princ
  43.     (strcat "\nThe file "
  44.             filename
  45.             "(.lsp/.exe/.arx) was not found in your search path folders."
  46.     )
  47.   )
  48.   (princ "\nCheck the installation of the support files and try again.")
  49.   (princ)
  50. )
  51.  
  52. (defun ai_ffile (app)
  53.   (or (findfile (strcat app ".lsp"))
  54.       (findfile (strcat app ".exp"))
  55.       (findfile (strcat app ".exe"))
  56.       (findfile (strcat app ".arx"))
  57.       (findfile app)
  58.   )
  59. )
  60.  
  61.   (princ msg)
  62.   (setq *error* m:err m:err nil)
  63.   (princ)
  64. )
  65.  
« Last Edit: February 15, 2024, 05:08:30 PM by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Redefine defun?
« Reply #2 on: February 15, 2024, 08:42:45 PM »
I'm not exactly sure what you mean. If you literally mean changing the "defun" subr to some other name, then no. But you can redefine function names on the fly in AutoLisp. A great example of that is with built in "autoload" function. It writes a stub function that loads the Lisp file associated with the command, then rewrites itself when the lisp file is loaded.
...>%

I didn't spend any time looking at the code but that sounds interesting. Is this a new feature (I am still using 2022 at the moment)? The description also sounds just like something I wrote for myself, but I called mine JIT (just in time) load.  Thanks for the tip, I'll try to find some time to look into `autoload`.

REF:
https://www.theswamp.org/index.php?topic=57707.msg610694#msg610694
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Redefine defun?
« Reply #3 on: February 15, 2024, 08:46:42 PM »
Is it possible to redefine the defun function?  I would like to redefine defun so I could add a function call at the beginning of each procedure.
No you can not redefine DEFUN but you can create your own "function builder". I give the basic framework/idea/concept in my Bottom-Up design writeup.
https://www.theswamp.org/index.php?topic=58123.msg613398#msg613398

And a more crazy/wacky idea in a "shortie template" concept post. Works fairly well.
https://www.theswamp.org/index.php?topic=58213.msg614022#msg614022
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

PKENEWELL

  • Bull Frog
  • Posts: 319
Re: Redefine defun?
« Reply #4 on: February 16, 2024, 09:17:39 AM »
I didn't spend any time looking at the code but that sounds interesting. Is this a new feature (I am still using 2022 at the moment)? The description also sounds just like something I wrote for myself, but I called mine JIT (just in time) load.  Thanks for the tip, I'll try to find some time to look into `autoload`.

REF:
https://www.theswamp.org/index.php?topic=57707.msg610694#msg610694

Actually John, the (autoload) function has been a function with AutoCAD as far back as I can remember (At least back to R12). It was never built into the syntax, but was written into the AutoCAD default lisp files, similar to the ai_utils.lsp. It has evolved with AutoCAD to include ARX, VLX, and FAS files, but the basic function was there all along.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

PKENEWELL

  • Bull Frog
  • Posts: 319
Re: Redefine defun?
« Reply #5 on: February 16, 2024, 09:20:54 AM »
No you can not redefine DEFUN but you can create your own "function builder". I give the basic framework/idea/concept in my Bottom-Up design writeup.
https://www.theswamp.org/index.php?topic=58123.msg613398#msg613398

And a more crazy/wacky idea in a "shortie template" concept post. Works fairly well.
https://www.theswamp.org/index.php?topic=58213.msg614022#msg614022

Great Examples!
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt