Using Macros in Delphi

Computer programmers often need to transform a piece of code in a repetitive way. For example, consider transforming the following fields declaration into a fields initialization [1], as shown below:

Delphi macros to transform source code

You can do that by hand of course, but you can also use macros to streamline your work, specially if you need to convert more than just a few lines of code.

Macros allow to record a sequence of keystrokes and playback such compilation with just one saved keystroke.

To record a macro in Delphi you need to:
  1. Press Ctrl + Shift + R to start recording your macro. [2]
  2. Use as many keystrokes as you want to transform your code.
  3. Press Ctrl + Shift + R (again) to finish recording your macro. [3] 
To playback a saved (recorded) macro you just need to press Ctrl + Shift + P each time you want to run it. [4]

At this point you might be a little lost, hopefully the following video will clarify all your doubts:


In the previous example I recorded (and played) a macro containing 20 steps (keystrokes), but I am sure you can do the same with less keystrokes. If you do, please, write you keystrokes in the comments section at the bottom of this post.  

In conclusion, macros can make your life easier when writing code; besides, you can use them to impress your boss or colleagues with your super fast typing skills ;-)

Notes:
[1]: The unit containing the full source code of this example is at the end of this post.
[2]: In the latest Delphi versions you can click the following button to Record Macro:
Record Macro button in Delphi
[3]: In the latest Delphi versions you can click the following button to Stop Recording Macro:
Stop Recording Macro button in Delphi
[4]: In the latest Delphi versions you can click the following button to Playback Macro:
Playback Macro button in Delphi
Source code:

unit Person;

interface

type
  TPerson = class
  private
    FGivenName: string;
    FFamilyName: string;
    FPhoneNumber: string;
    FDateOfBirth: TDateTime;
    FAddress: string;
    FEMail: string;
  public
    constructor Create(aGivenName, aFamilyName, 

                       aPhoneNumber: string;
                       aDateOfBirth: TDateTime;
                       aAddress, aEMail: string);
    //Other methods and properties here...
  end;


implementation

{ TPerson }

constructor TPerson.Create(aGivenName, aFamilyName, 

                           aPhoneNumber: string;
                           aDateOfBirth: TDateTime; 

                           aAddress, aEMail: string);
begin
  FGivenName:= aGivenName;
  FFamilyName:= aFamilyName;
  FPhoneNumber:= aPhoneNumber;
  FDateOfBirth:= aDateOfBirth;
  FAddress:= aAddress;
  FEMail:= aEMail;

  //Step  1: Ctrl + Shift + ArrowRight
  //Step  2: Ctrl + C
  //Step  3: ArrowLeft
  //Step  4: =
  //Step  5: Spacebar
  //Step  6: Ctrl + V
  //Step  7: Ctrl + ArrowLeft
  //Step  8: a
  //Step  9: Del
  //Step 10: Ctrl + ArrowRight
  //Step 11: Ctrl + ArrowRight
  //Step 12: Ctrl + Shift + ArrowLeft
  //Step 13: Backspace
  //Step 14: Backspace
  //Step 15: Backspace
  //Step 16: Backspace
  //Step 17: ;
  //Step 18: Ctrl + ArrowLeft
  //Step 19: Ctrl + ArrowLeft
  //Step 20: ArrowDown;
end;

end.

No comments:

Post a Comment