This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ueditorfile.pas
325 lines (272 loc) · 9.42 KB
/
ueditorfile.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
unit uEditorFile;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ComCtrls, SynEdit, Controls, uObjects, uExternalVariables, FileUtil, uTokens,SynEditMarkup,
uMarkup;
type
{ TMySynEdit }
TMySynEdit = class(TSynEdit)
private
function GetMarkupManager: TSynEditMarkupManager;
public
property MarkupMgr: TSynEditMarkupManager read GetMarkupManager;
end;
type
{ TEditorFile }
TEditorFile = class(TTabSheet)
private
Markup: TMarkup;
fFileName: string;
fUntitled: boolean;
fEditor: TMySynEdit;
fTimestamp: longint;
fDontAskReloadForTimestamp: longint;
CurrentlyUpdating: boolean;
procedure SetFileName(AValue: string);
procedure OnChangeEvent(Sender: TObject);
function GetTimestamp: longint;
public
//First line the syntax checker processed that didn't have any errors.
LineIndex_Syntax: integer;
//First line the highlighter parsed
LineIndex_HL: integer;
UntitledIndex: integer;
IsFromArchive: boolean;
KnownVariables: TVariablesTable;
UsedPrograms: TUsedProgramsList;
TokenLines: TEditorLines;
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
property FileName: string read fFileName write SetFileName;
property Untitled: boolean read fUntitled;
procedure SaveFile;
procedure SaveFileAs(const SaveAs: string);
procedure ReadFile(const FileToRead: string);
procedure Reload;
procedure OnShowEvent(Sender: TObject);
procedure OnEnterEvent(Sender: TObject);
procedure OnShowHintEvent(Sender: TObject; HintInfo: PHintInfo);
procedure OnClickEvent(Sender: TObject);
property Editor: TMySynEdit read fEditor;
procedure Cut;
procedure Copy;
procedure Paste;
function ExternallyChanged: boolean;
function AskToReload: boolean;
procedure SetDontAskReloadForTimestamp;
procedure ClearMarkups;
procedure AddMarkup(const StartPoint, EndPoint: TTokenPoint; const Message: string);
procedure StartSyntaxCheck;
procedure FinishSyntaxCheck;
end;
implementation
{ TEditorFile }
uses uKerboscriptHighlighter, uMain, Graphics, SynGutterLineNumber, SynEditKeyCmds, uVariableFetcher,
Dialogs, Menus, SynEditPointClasses, uSettings, SynEditTypes, Forms;
{ TMySynEdit }
function TMySynEdit.GetMarkupManager: TSynEditMarkupManager;
begin
Result := TSynEditMarkupManager(GetMarkupMgr);
end;
procedure TEditorFile.SetFileName(AValue: string);
begin
fFileName := AValue;
Caption := ExtractFileName(AValue);
OnShow(self);
end;
procedure TEditorFile.OnChangeEvent(Sender: TObject);
begin
Caption := '*' + ExtractFileName(FileName);
end;
function TEditorFile.GetTimestamp: longint;
var
TheFile: longint;
begin
TheFile := -1;
try
TheFile := FileOpen(Utf8ToSys(FileName), fmOpenRead + fmShareDenyNone);
Result := FileGetDate(TheFile);
finally
if TheFile > 0 then
FileClose(TheFile);
end;
end;
constructor TEditorFile.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
CurrentlyUpdating := True;
OnShow := @OnShowEvent;
parent := frmMain.pcMDIFaker;
fEditor := TMySynEdit.Create(self);
fEditor.Parent := self;
fEditor.Align := alClient;
fEditor.Highlighter := frmMain.Highlighter;
TKerboscriptHighlighter(fEditor.Highlighter).SetEditor(Self);
fEditor.BracketMatchColor.Assign(TKerboscriptHighlighter(fEditor.Highlighter).BracketMatchColor);
fEditor.Options := fEditor.Options + [eoKeepCaretX, eoEnhanceHomeKey, eoBracketHighlight,
eoGroupUndo, eoTabsToSpaces, eoTrimTrailingSpaces, eoHideRightMargin] - [eoScrollPastEol, eoSmartTabs];
fEditor.Color := TKerboscriptHighlighter(fEditor.Highlighter).SpaceAttr.Background;
fEditor.Font.Assign(frmMain.seHidden.Font);
fEditor.ExtraCharSpacing := frmMain.seHidden.ExtraCharSpacing;
fEditor.OnStatusChange := @frmMain.seHiddenStatusChange;
fEditor.OnReplaceText := @frmMain.seHiddenReplaceText;
fEditor.OnChange := @OnChangeEvent;
fEditor.OnEnter := @OnEnterEvent;
fEditor.OnShowHint := @OnShowHintEvent;
fEditor.OnClick := @OnClickEvent;
fEditor.ShowHint := True;
TSynGutterLineNumber(fEditor.Gutter.Parts.Part[1]).ZeroStart := True;
fEditor.TabWidth := Settings.GetValue('editor/tabwidth', 2);
fEditor.PopupMenu := frmMain.popmEditor;
fEditor.Keystrokes.Delete(fEditor.Keystrokes.FindCommand(ecInsertLine)); //Ctrl-N
fEditor.Keystrokes.Delete(Editor.Keystrokes.FindCommand(ecPageBottom)); //Ctrl-PgDwn
fEditor.Keystrokes.Delete(Editor.Keystrokes.FindCommand(ecPageTop)); //Ctrl-PgUp
fEditor.BookMarkOptions.BookmarkImages := frmMain.imlstBookmarks;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker0)].Command := ecToggleMarker0;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker1)].Command := ecToggleMarker1;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker2)].Command := ecToggleMarker2;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker3)].Command := ecToggleMarker3;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker4)].Command := ecToggleMarker4;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker5)].Command := ecToggleMarker5;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker6)].Command := ecToggleMarker6;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker7)].Command := ecToggleMarker7;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker8)].Command := ecToggleMarker8;
fEditor.Keystrokes[fEditor.Keystrokes.FindCommand(ecSetMarker9)].Command := ecToggleMarker9;
if Settings.GetValue('editor/oldcursor', True) then
fEditor.InsertCaret := ctHorizontalLine;
fUntitled := True;
fEditor.Modified := False;
KnownVariables := TVariablesTable.Create;
KnownVariables.Sorted := True;
KnownVariables.OnCompare := @KeyCompare;
UsedPrograms := TUsedProgramsList.Create;
frmMain.SynCompletion1.AddEditor(fEditor);
CurrentlyUpdating := False;
TokenLines := TEditorLines.Create;
Markup := TMarkup.Create(fEditor);
Markup.MarkupInfo.FrameEdges := sfeBottom;
Markup.MarkupInfo.FrameStyle := slsWaved;
Markup.MarkupInfo.FrameColor := clred;
Markup.MarkupInfo.Foreground := clnone;
Markup.MarkupInfo.Background := clnone;
fEditor.MarkupMgr.AddMarkUp(Markup);
LineIndex_Syntax := 0;
end;
destructor TEditorFile.Destroy;
begin
CurrentlyUpdating := True;
KnownVariables.Free;
UsedPrograms.Free;
inherited Destroy;
end;
procedure TEditorFile.SaveFile;
begin
SaveFileAs(FileName);
end;
procedure TEditorFile.SaveFileAs(const SaveAs: string);
begin
CurrentlyUpdating := True;
try
fEditor.Lines.SaveToFile(SaveAs);
FileName := SaveAs;
fTimestamp := GetTimestamp;
fEditor.Modified := False;
fUntitled := False;
UntitledIndex := -1;
Caption := ExtractFileName(FileName);
finally
CurrentlyUpdating := False;
end;
end;
procedure TEditorFile.ReadFile(const FileToRead: string);
begin
CurrentlyUpdating := True;
try
FileName := FileToRead;
fEditor.Modified := False;
fUntitled := False;
UntitledIndex := -1;
fEditor.Lines.LoadFromFile(FileToRead);
fTimestamp := GetTimestamp;
fDontAskReloadForTimestamp := fTimestamp;
finally
CurrentlyUpdating := False;
end;
end;
procedure TEditorFile.Reload;
begin
if not Untitled then
ReadFile(FileName);
end;
procedure TEditorFile.OnShowEvent(Sender: TObject);
begin
frmMain.StatusBar1.Panels[3].Text := FileName;
if Editor <> nil then
frmMain.StatusBar1.Panels[1].Text := 'Length: ' + IntToStr(Length(Editor.Text) -
(Length(sLineBreak) * Editor.Lines.Count));
end;
procedure TEditorFile.OnEnterEvent(Sender: TObject);
begin
if AskToReload and frmMain.AskToReloadFile then
Reload;
end;
procedure TEditorFile.OnShowHintEvent(Sender: TObject; HintInfo: PHintInfo);
var Aux: TPoint;
begin
Aux := Editor.PixelsToLogicalPos(hintinfo^.CursorPos);
{if Editor.GetWordAtRowCol(Aux) = '' then
hintinfo^.HintStr := ''
else}
begin
Dec(Aux.Y);
hintinfo^.HintStr := Markup.GetTextForMarkup(Aux);
end;
end;
procedure TEditorFile.OnClickEvent(Sender: TObject);
begin
{Editor.Hint := Markup.GetTextForMarkup(Point(Editor.CaretX, Editor.CaretY - 1));
Application.ActivateHint(Mouse.CursorPos);}
end;
procedure TEditorFile.Cut;
begin
fEditor.CutToClipboard;
end;
procedure TEditorFile.Copy;
begin
fEditor.CopyToClipboard;
end;
procedure TEditorFile.Paste;
begin
fEditor.PasteFromClipboard;
end;
function TEditorFile.ExternallyChanged: boolean;
begin
Result := not Untitled and (fTimestamp <> GetTimestamp);
end;
function TEditorFile.AskToReload: boolean;
begin
Result := not CurrentlyUpdating and ExternallyChanged and (GetTimestamp <> fDontAskReloadForTimestamp);
end;
procedure TEditorFile.SetDontAskReloadForTimestamp;
begin
fDontAskReloadForTimestamp := GetTimestamp;
end;
procedure TEditorFile.ClearMarkups;
begin
Markup.ClearMarks;
end;
procedure TEditorFile.AddMarkup(const StartPoint, EndPoint: TTokenPoint; const Message: string);
begin
Markup.AddMark(StartPoint, EndPoint, Message);
end;
procedure TEditorFile.StartSyntaxCheck;
begin
Markup.BeginUpdates;
end;
procedure TEditorFile.FinishSyntaxCheck;
begin
if Markup.FinishedUpdates then
Editor.Refresh;
end;
end.