In order to save user settings a certain way if a routine is loaded and executed- and specifically, if it fails during running, as OP mentioned "escaping" out...
These all require use of an error handler from within the autolisp program.
Typically error handlers are defined just under the main function for the routine and will accept one argument, that for MSG. Most of these error handlers will prompt the message when an error occurs, displaying it to the user, as well as execute any additional code found within the error handler, which is where you'd put such code to restore the clayer to a specific layer, it's where you'd ensure that system variables are reset as well.
You can see in tharwats example that is exactly what he has done
- (defun *error* (msg) (if cl (setvar 'clayer cl) ) (if (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ msg) (princ (strcat "\n** Error: " msg " **")) ) )
Can see this code to do exactly two things. One is to display a message using (WCMATCH), (princ) and (strcat), and the other is to check if the variable exists in the routine called CL
If this variable is found to have a value then set the clayer system variable to that value. Cursory glances would have me think that upon changing the layer in the non-error-handling part of that code, just before the clayer is changed that there is a call to (setq cl (getvar "clayer")) in order to establish that variable into the systems memory for later use or for restoring via the error handler.
HTH
Edit: Yes in fact it's the very first line after the error handler. Guess he got right to the point |