The AutoLISP Extension provides tools to format select AutoLISP code statements, define the indent and alignment of new statements as they are typed, and mark a statement as a comment.
Prerequisites
Topics in this Tutorial
Format AutoLISP Code Statements
Visual Studio Code automatically applies some formatting to new AutoLISP code statements as they are typed, but code statements pasted from another source are not automatically formatted. Visual Studio Code does allow you to format select or all code statements in the current editor window.
The following steps explain how to format code statements in an LSP file:
- In Visual Studio Code, open the Create-LSP-Tutorial.lsp file or make it current.
- In the editor window, select the following statements that define the
hello function:
(defun c:hello ( / msg) (setq msg (getstring T "\nEnter a message: ")) (alert msg) )
- Right-click and choose Format Selection.
The code statements are reformatted with the nested statements now indented and aligned. The statements should look like the following:
(defun c:hello (/ msg) (setq msg (getstring T "\nEnter a message: ")) (alert msg) )
- Right-click in the editor window and choose Format Document.
All the code statements in the LSP file are formatted based on the settings of the AutoLISP Extension.
- Save the changes to the Create-LSP-Tutorial.lsp file.
The current settings used to indent and format AutoLISP code statements can be viewed and changed by doing the following:
- In Visual Studio Code, on the Activity Bar, click Extensions.
- On the Extensions view, click More Actions > Show Installed Extensions.
- On the AutoCAD AutoLISP Extension item, click Manage (
) > Extension Settings.
- Adjust the extension settings as desired, changes are automatically saved.
If you want to restore the default value of a setting, move the cursor over the name of the setting, and click More Actions (
) > Reset Setting.
Comment and Uncomment AutoLISP Code Statements
Comments are great for describing what a program or specific code statement does in an LSP file. You indicate a comment in an LSP file by adding one or more semi-colons in front of the text that you want to mark as a comment.
The following steps explain how to use the tools in Visual Studio Code to mark lines as comments or uncomment lines in an LSP file:
- In Visual Studio Code, open the Create-LSP-Tutorial.lsp file or make it current.
- In the editor window, select the following lines that define the
hello function:
(defun c:hello (/ msg) (setq msg (getstring T "\nEnter a message: ")) (alert msg) )
- On the menu bar, click Edit menu > Toggle Line Comment.
A semi-colon is added to the beginning of each line. The statements should now look like the following:
; (defun c:hello (/ msg) ; (setq msg (getstring T "\nEnter a message: ")) ; (alert msg) ; )
- Select the lines again and click Edit menu > Toggle Line Comment.
The semi-colon is removed from each line.
The Toggle Block Comment tool on the Edit menu can also be used to mark a large number of lines as comments. This type of comment is known as a block comment. A block comment starts with the character sequence ;| and ends with |; rather than each line beginning with a semi-colon. The following shows the results of a block comment being applied to the hello function.
;| (defun c:hello (/ msg) (setq msg (getstring T "\nEnter a message: ")) (alert msg) ) |;
A comment can also be placed after a code statement, this is commonly known as an inline comment.
(alert msg) ; Displays a string in a message box