In the AutoCAD 2015 - AutoLisp Help both the *push-error-using-command* and *push-error-using-stack* make the following statement in reference to the command function in an Error Handler function.
Note: This funciton cannot be used when the command function is used within a custom *error* handler.
I assume this note was only supposed to be on the *push-error-using-stack* function help page.
Also, on the help page for the AutoLisp command function it makes the following statement.
Also, if you use the command function in an .lsp or .mnl file, it should be called only from within a defun statement. Use theS::STARTUP function to define commands that need to be issued immediately when you begin a drawing session.
What happens if you call the command function outside of a Defun statement?
Ever since we started writing AutoLisp code with AutoCAD version 2.52 we have written the majority of our code without using the Defun statement. In the early days AutoLisp memory was very limited and a program could take a minute or two to start executing after calling the load function. To save memory we create AutoLisp files that just contained a series of AutoLisp function calls. This reduce the memory requirements and allow the program to begin executing right after it read in the first statement.
So in this new fiberless version of AutoCAD what would happen if I execute a command function outside of a Defun statement?
Anything bad???
During startup we don't use any command functions unless they are in the S::Startup function. However, once AutoCAD is fully initialized and a user selects a program from a menu, AutoLisp loads an .lsp file that in most cases does not define any functions using the Defun statement.
The part of the command function help that I am concerned with is:
Also, if you use the command function in an .lsp or .mnl file, it should be called only from within a defun statement.
In the past, loading an AutoLisp file that was created in this manner has not caused any problems and so far I haven't seen any problems running the code in AutoCAD 2015. However, I don't know if it is doing something behind the scenes that may cause problems later or not.
Additional note: I went back into AutoCAD 2010 & 2012 and looked at the help for the AutoLisp command function and it makes the same remarks about using the command function only in a defun statement. Since our code has been running without any problems in the last few years I guess I won't worry about it unless someone tells me that it will cause problems in the new fiberless AutoCAD enviroment.
True we are using lisp to script. In addition, I didn't say that we don't define any functions using defun. For functions used in multiple files or by DCL dialogs we definitely define functions that can be called. In other cases we may put the common code in a separate .lsp file that can be loaded when necessary.
As a stated earlier most of these programs were written when we started using AutoCAD version 2.52 when memory was limited and they have not really changed since then. I think the last major change to the AutoLisp code was to add DCL dialogs to some of the programs. As for the AutoLisp variables we have functions we used to initialize and clean them up so that we simulate a local stack.
Typically, our client wants to minimize the time and expense migrating from one version of AutoCAD to another. So if the program works in the new version without any modifications, then the program is never changed. We have around 334 AutoLisp files which would have to be rework to define everything using defun statements. That is something I don't think the client would pay for unless I can give them a very good reason.