Sign in Join
Wiki copytemporary

IDE Window: Code Templates

Deutsch · English · Español · Français · Suomi

What are Code Templates?

A Code Template is a section of 'boiler plate' code with an associated abbreviation which invokes its insertion. For example, you can type 'cl' in the Source Editor, and then press the Ctrl+J keyboard shortcut to invoke the code template(s) starting with 'cl'. This feature is a great time saver.

The Code Templates dialog (accessed via the Tools|Code Templates... menu option) allows you to edit existing templates, or add your own templates.

Code template text can contain macros, and you can even add your own macros to the list of those already available by creating a design time package and using the macrointf.pas of the IDEIntf package.

Example 1 - ifb

type

pascal
ifb|

Here the pipe symbol (|) represents the blinking screen cursor. Press Ctrl+j. The three characters you typed ('ifb') will be expanded to

pascal
if | then begin
pascal
end;

Again the pipe represents the cursor blinking onscreen. You will see that the cursor has been moved. This template is called 'ifb' and it is defined as follows:

pascal
if | then begin
pascal
end;

This time the pipe '|' is an actual character you type as part of this code template's definition. It stipulates where the cursor will be positioned after the template has been inserted.

Example 2 - Using macros

This second example shows you how to create a code template 'w' which repeats the name of the current procedure somewhere in your code. For long names this can be quite a time saver, and it also types it for you without any spelling mistakes!

text
 writeln(' ',|);
text
 writeln('$ProcedureName() ',|);
pascal
 procedure TForm1.FormDestroy(Sender: TObject);
 begin
   w|
 end;

(Here the pipe represents the blinking cursor, not a character).

pascal
 procedure TForm1.FormDestroy(Sender: TObject);
 begin
   writeln('TForm1.FormDestroy ',|);
 end;

Example 3 - Editing macros parameters

pascal
 $Param(i):=0;
 while($Param(i,Sync=1)<$Param(Count))do
 begin
   |
 inc($Param(i,Sync=1));
 end;

Template Autocompletion

Code templates are invoked either by using the Ctrl+j shortcut, or (if you set this option) automatically following particular events. For each template you can specify the event (if any) that automatically invokes it. You can enable none, some or all of the following events:

Macros

Macros have the form $(MacroName) or $MacroName(Param1,...). Macros can be nested.

Define your own macros

You can register your own macros by writing a design time package and installing it. See ide/codetemplatesdlg.pas procedure CreateStandardCodeMacros for examples.

DCI file format

The default dci file of the IDE is pcp/lazarus.dci, where pcp is your primary config path (e.g. under Linux $HOME/.lazarus). If this file is not present the IDE will copy the file ide/lazarus_dci_file.dci.

A dci file is a text file with a list of templates.

Each template starts with line [name | description]. The template's value are the following lines.


From the Free Pascal wiki, page "IDE Window: Code Templates", as it stood on 2016-07-14 after a change by Arent, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.