📄 syneditplugins.pas
字号:
{ TAbstractSynHookerPlugin }
procedure TAbstractSynHookerPlugin.HookEditor(aEditor: TCustomSynEdit;
aCommandID: TSynEditorCommand; aOldShortCut, aNewShortCut: TShortCut);
var
iIndex: integer;
iKeystroke: TSynEditKeyStroke;
begin
Assert( aNewShortCut <> 0 );
{ shortcurts aren't created while in design-time }
if [csDesigning] * ComponentState = [csDesigning] then
begin
if TSynEdit(aEditor).Keystrokes.FindShortcut( aNewShortCut ) >= 0 then
raise ESynKeyError.Create(SYNS_EDuplicateShortCut)
else
Exit;
end;
{ tries to update old Keystroke }
if aOldShortCut <> 0 then
begin
iIndex := TSynEdit(aEditor).Keystrokes.FindShortcut( aOldShortCut );
if (iIndex >= 0) then
begin
iKeystroke := TSynEdit(aEditor).Keystrokes[iIndex];
if iKeystroke.Command = aCommandID then
begin
iKeystroke.ShortCut := aNewShortCut;
Exit;
end;
end;
end;
{ new Keystroke }
iKeystroke := TSynEdit(aEditor).Keystrokes.Add;
try
iKeystroke.ShortCut := aNewShortCut;
except
iKeystroke.Free;
raise;
end;
iKeystroke.Command := aCommandID;
aEditor.RegisterCommandHandler( OnCommand, Self );
end;
procedure TAbstractSynHookerPlugin.UnHookEditor(aEditor: TCustomSynEdit;
aCommandID: TSynEditorCommand; aShortCut: TShortCut);
var
iIndex: integer;
begin
aEditor.UnregisterCommandHandler( OnCommand );
iIndex := TSynEdit(aEditor).Keystrokes.FindShortcut( aShortCut );
if (iIndex >= 0) and
(TSynEdit(aEditor).Keystrokes[iIndex].Command = aCommandID) then
TSynEdit(aEditor).Keystrokes[iIndex].Free;
end;
{ TAbstractSynHookerPlugin }
procedure TAbstractSynSingleHookPlugin.Accept;
begin
fState := psAccepting;
try
DoAccept;
finally
fCurrentEditor := nil;
fState := psNone;
end;
end;
procedure TAbstractSynSingleHookPlugin.Cancel;
begin
fState := psCancelling;
try
DoCancel;
finally
fCurrentEditor := nil;
fState := psNone;
end;
end;
constructor TAbstractSynSingleHookPlugin.Create(aOwner: TComponent);
begin
inherited;
fCommandID := NewPluginCommand;
fShortCut := DefaultShortCut;
end;
class function TAbstractSynSingleHookPlugin.DefaultShortCut: TShortCut;
begin
Result := 0;
end;
destructor TAbstractSynSingleHookPlugin.Destroy;
begin
if Executing then
Cancel;
ReleasePluginCommand( CommandID );
inherited;
end;
procedure TAbstractSynSingleHookPlugin.DoAddEditor(
aEditor: TCustomSynEdit);
begin
if ShortCut <> 0 then
HookEditor( aEditor, CommandID, 0, ShortCut );
end;
procedure TAbstractSynSingleHookPlugin.Execute(aEditor: TCustomSynEdit);
begin
if Executing then
Cancel;
Assert( fCurrentEditor = nil );
fCurrentEditor := aEditor;
Assert( fState = psNone );
fState := psExecuting;
try
DoExecute;
except
Cancel;
raise;
end;
end;
function TAbstractSynSingleHookPlugin.Executing: boolean;
begin
Result := fState = psExecuting;
end;
function TAbstractSynSingleHookPlugin.IsShortCutStored: Boolean;
begin
Result := fShortCut <> DefaultShortCut;
end;
procedure TAbstractSynSingleHookPlugin.DoRemoveEditor(aEditor: TCustomSynEdit);
begin
if ShortCut <> 0 then
UnHookEditor( aEditor, CommandID, ShortCut );
if Executing and (CurrentEditor = aEditor) then
Cancel;
end;
procedure TAbstractSynSingleHookPlugin.SetShortCut(const Value: TShortCut);
var
cEditor: integer;
begin
if fShortCut <> Value then
begin
if Assigned(fEditors) then
if Value <> 0 then
begin
for cEditor := 0 to fEditors.Count -1 do
HookEditor( Editors[cEditor], CommandID, fShortCut, Value );
end
else
begin
for cEditor := 0 to fEditors.Count -1 do
UnHookEditor( Editors[cEditor], CommandID, fShortCut );
end;
fShortCut := Value;
end;
end;
{ TAbstractSynCompletion }
function TAbstractSynCompletion.GetCurrentEditorString: String;
var
iString: String;
cCol: integer;
iIdentChars: TSynIdentChars;
begin
iString := CurrentEditor.LineText;
if (CurrentEditor.CaretX > 1) and
(CurrentEditor.CaretX -1 <= Length(iString)) then
begin
iIdentChars := CurrentEditor.IdentChars;
for cCol := CurrentEditor.CaretX -1 downto 1 do
if not (iString[cCol] in iIdentChars) then
break;
Result := Copy( iString, cCol +1, CurrentEditor.CaretX - cCol -1);
end;
end;
procedure TAbstractSynCompletion.DoAccept;
begin
fCurrentString := '';
end;
procedure TAbstractSynCompletion.DoCancel;
begin
fCurrentString := '';
end;
procedure TAbstractSynCompletion.DoExecute;
begin
CurrentString := GetCurrentEditorString;
end;
procedure TAbstractSynCompletion.OnCommand(Sender: TObject;
AfterProcessing: boolean; var Handled: boolean;
var Command: TSynEditorCommand; var aChar: char; Data,
HandlerData: pointer);
var
iString: String;
begin
if not Executing then
begin
if (Command = CommandID) then
begin
Execute( Sender as TCustomSynEdit );
Handled := True;
end;
end
else { Executing }
if Sender = CurrentEditor then
begin
if not AfterProcessing then
begin
case Command of
ecChar:
if aChar = #27 then
begin
Cancel;
Handled := True;
end
else
begin
if not(aChar in CurrentEditor.IdentChars) then
Accept;
{don't handle the char}
end;
ecLineBreak:
begin
Accept;
Handled := True;
end;
ecLeft, ecSelLeft:
if CurrentString = '' then
Handled := True;
ecDeleteLastChar:
if CurrentString = '' then
Handled := True;
ecTab:
Accept;
ecDeleteChar,
ecRight, ecSelRight,
ecLostFocus, ecGotFocus:
; {processed on AfterProcessing}
else
Cancel;
end;
end
else { AfterProcessing }
case Command of
ecLostFocus, ecGotFocus,
ecDeleteChar:
;
ecDeleteLastChar,
ecLeft, ecSelLeft,
ecChar:
CurrentString := GetCurrentEditorString;
ecRight, ecSelRight: begin
iString := GetCurrentEditorString;
if iString = '' then
Cancel
else
CurrentString := iString;
end;
else
if CurrentString <> GetCurrentEditorString then
Cancel;
end;
end; {endif Sender = CurrentEditor}
end;
procedure TAbstractSynCompletion.SetCurrentString(const Value: String);
begin
fCurrentString := Value;
end;
procedure TAbstractSynCompletion.AddEditor(aEditor: TCustomSynEdit);
begin
inherited AddEditor(aEditor);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -