I find very inapropiate to use the editor commands (ecXXX) to execute macros.
Depending on the editor options (auto identation, auto trim trailing spaces), the result change. Is there a better way to handle the problem than to save and restore the options before and after the macro execution ?
Example:
procedure curlyBraceCloseAndIndent(editor: TSynEdit);
var
beg: string;
i: integer = 1;
j: integer;
opts: TSynEditorOptions;
const
blk = [' ', #9];
begin
beg := editor.LineText;
if beg.isEmpty then exit;
beg := beg[1..editor.CaretX];
if beg.isEmpty then exit;
while true do
begin
if (i > beg.length) or not (beg[i] in blk) then
break;
i += 1;
end;
i -= 1;
opts := editor.Options;
editor.Options := opts - [eoAutoIndent, eoTrimTrailingSpaces];
editor.BeginUndoBlock;
editor.CommandProcessor(ecInsertLine, '', nil);
editor.CommandProcessor(ecDown, '', nil);
editor.CommandProcessor(ecInsertLine, '', nil);
editor.CommandProcessor(ecDown, '', nil);
for j := 1 to i do editor.CommandProcessor(ecChar, beg[j], nil);
editor.CommandProcessor(ecChar, '}', nil);
editor.CommandProcessor(ecUp, '', nil);
for j := 1 to i do editor.CommandProcessor(ecChar, beg[j], nil);
editor.CommandProcessor(ecTab, '', nil);
editor.EndUndoBlock;
editor.Options := opts;
end;
If I don't save-deactivate-restore the options I have to handle a lot of special cases.
I could use the .Lines but the problem is they don't work with the editor history.
Do I miss a better to do these kind of stuff ?