Sign in Join
Wiki copytemporary

IDE Window: Code Templates: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## What are Code Templates?
22
33A 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.
44
55The Code Templates dialog (accessed via the Tools\|Code Templates... menu option) allows you to edit existing templates, or add your own templates.
66
77Code 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.
88
99## Example 1 - ifb
1010
1111type
1212
1313 ifb|
1414
1515Here the pipe symbol (\|) represents the blinking screen cursor. Press Ctrl+j. The three characters you typed ('ifb') will be expanded to
1616
1717 if | then begin
1818
1919 end;
2020
2121Again 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:
2222
2323 if | then begin
2424
2525 end;
2626
2727This 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.
2828
2929## Example 2 - Using macros
3030
3131This 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!
3232
3333- In the Code Templates dialog, click on the \[Add\] button. In the dialog which pops up set Token to 'w', and Comment to 'writeln(ProcName)', then click \[OK\].
3434- Your new code template item has been added to the list box.
3535- In the source below fill in
3636
37-` writeln(' ',|);`
37+```text
38+ writeln(' ',|);
39+```
3840
3941- Here pipe is an actual pipe character defining the new cursor position. Now place the cursor after the first apostrophe (') and click on the 'Insert macro' button. A dialog pops up listing all available code macros. Choose 'ProcedureName' and click 'Insert Macro'. You should now have:
4042
41-` writeln('$ProcedureName() ',|);`
43+```text
44+ writeln('$ProcedureName() ',|);
45+```
4246
4347- Click \[OK\] to save and close the Code Templates dialog.
4448- Move the cursor into the body of a method, and type 'w'. For instance:
4549
46-` procedure TForm1.FormDestroy(Sender: TObject);`\
47-` begin`\
48-`   w|`\
49-` end;`
50+``` pascal
51+ procedure TForm1.FormDestroy(Sender: TObject);
52+ begin
53+ w|
54+ end;
55+```
5056
5157(Here the pipe represents the blinking cursor, not a character).
5258
5359- Press Ctrl+j. You will get:
5460
55-` procedure TForm1.FormDestroy(Sender: TObject);`\
56-` begin`\
57-`   writeln('TForm1.FormDestroy ',|);`\
58-` end;`
61+``` pascal
62+ procedure TForm1.FormDestroy(Sender: TObject);
63+ begin
64+ writeln('TForm1.FormDestroy ',|);
65+ end;
66+```
5967
6068## Example 3 - Editing macros parameters
6169
6270- Go: Tools - Code Templates, in group box Templates press button Add
6371- Input Token: w
6472- Input Comment: While i\<count do
6573- Press Ok
6674- In field below Copy/Paste next text:
6775
68-` $Param(i):=0;`\
69-` while($Param(i,Sync=1)<$Param(Count))do`\
70-` begin`\
71-`   |`\
72-` inc($Param(i,Sync=1));`\
73-` end;     `
76+``` pascal
77+ $Param(i):=0;
78+ while($Param(i,Sync=1)<$Param(Count))do
79+ begin
80+ |
81+ inc($Param(i,Sync=1));
82+ end;
83+```
7484
7585- Set next check-boxes: Enable Macros, line break, do not add character.
7686- Somewhere in editor where you want cycle type letter 'w' (without brackets) and press Enter
7787- Now you can type name of counter used instead of 'i' (simultaneously in several places) and press Tab - for edit next parameter. You can press Esc any time to exit from Macros
7888
7989## Template Autocompletion
8090
8191Code 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:
8292
8393- line break - insert the template whenever the user presses the \[Return\] or \[Enter\] key
8494- space - insert the template whenever the user presses the \[Space bar\].
8595- word end - whenever the user presses a word-ending key. For example, when typing 'b;' the code template 'b' can be executed.
8696- do not add character - this option can be combined with any of the above. When enabled the triggering key itself will not be inserted. For example when the option 'space' is enabled and the user triggers a template insertion by pressing the space bar, the space character itself will not be inserted in the source editor.
8797
8898## Macros
8999
90100Macros have the form **\$(MacroName)** or **\$MacroName(Param1,...)**. Macros can be nested.
91101
92102### Define your own macros
93103
94104You can register your own macros by writing a design time package and installing it. See ide/codetemplatesdlg.pas procedure CreateStandardCodeMacros for examples.
95105
96106## DCI file format
97107
98108The 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.
99109
100110A dci file is a text file with a list of templates.
101111
102112Each template starts with line \[name \| description\]. The template's value are the following lines.
103113
104114---
105115*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](/wiki-contact).*

Back to history