A Shortcut to Localising Variables


When writing AutoLISP programs, declaring variables local to a program, or colloquially, 'localising variables' is an important practice to follow.

Failure to declare a variable local to a program can, in some cases, lead to unforeseen consequences as described in my tutorial on Localising Variables.

However, those who have written programs of reasonable length will know that variable localisation can be a long and tedious process, often resulting in some variables being mistyped or omitted completely.

Thankfully, the Visual LISP IDE (VLIDE) offers a much quicker way to declare variables local. This tutorial will demonstrate a method to automatically report a list of symbols defined in your program code, reducing the task of localisation to a simple copy & paste operation.

Preliminary Setup

Before using the Checking utility offered by the Visual LISP IDE to report a list of variables, first ensure the following settings are enabled in your environment. This procedure will only need to be performed once.

First, open the Visual LISP IDE by typing 'VLIDE' at the AutoCAD command-line.

In the VLIDE, go to Tools » Environment Options » General Options

Tools, General Options

On the dialog that appears, navigate to the Diagnostic tab.

Ensure the Report statistics during syntax checking toggle is enabled.

Report Statistics

Click OK on the dialog to submit these settings.

Checking the Code

Now that the necessary settings are enabled, we can proceed to use the Checking utility to generate a list of variables for a program.

To serve as an example for this tutorial, open a New File in the VLIDE (File » New File or Ctrl+N) and copy the following code into the editor window:

(defun c:test ( / )
  (setq var1 1.0
        var2 "abc"
        var3 '(0 1 2 3 4)

This very simple code defines three global variables, (var1, var2 & var3), which we intend to declare local to the function c:test.

In the VLIDE, go to Tools » Check Text in Editor (alternatively, click the Check Edit Window button on the Tools toolbar, or press Ctrl+Alt+C).

Check Text in Editor

Check Edit Window

This utility will check the code for syntax errors and also return a list of global variables defined in the code, the output may look something like this:

[CHECKING TEXT <Untitled-1> loading...]
; === Top statistic:
; Global variables: (VAR1 VAR2 VAR3)
; Function definition (with number of arguments): ((C:TEST . 0))
; Check done.

Now localising the variables becomes a simple task of copying the list of global variables into the defun expression of the function definition.


The list returned by the VLIDE checking utility will list all defined symbols in the code. This list may include protected symbols, such as functions or protected constants. If such symbols are inadvertently localised, your program may not run as expected.

When copying the list of global variables ensure you remove the following items:

  • Protected symbols (highlighted blue), such as functions or protected constants (e.g. :vlax-true).
  • DCL Symbols defined at run-time, such as $key, $reason, $value, $data.
  • The pause symbol, as may be used in command expressions.
  • Variables that you intend to remain global.

Hint: The list of variables returned by the checking utility is sorted alphabetically, so by prefixing global variable names with asterisks (*), these are likely to appear at the front of the list and are easily removed.

Finishing Touches

If, like me, you are obsessed with the look and layout of your code, you may not like to have a capitalised list of local variables.

If you fall into this category of individuals, consider the following function:

(defun VarList ( lst )
  (vl-princ-to-string (mapcar '(lambda ( x ) (strcase (vl-princ-to-string x) t)) lst))
_$ (VarList '(VAR1 VAR2 VAR3))
"(var1 var2 var3)"

Much nicer.

Further Information

The Checking utility available in the Visual LISP IDE has many more uses than merely listing global variables; for more information about the full power of this tool, read the following section of the Visual LISP IDE Help Documentation:

AutoLISP Developer's Guide » Using the Visual LISP Environment » Developing Programs with Visual LISP » Checking for Syntax Errors » Using the Check Command to Look for Syntax Errors


increase · reset · decrease

Designed & Created by Lee Mac © 2010