-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUMainForm.pas
604 lines (513 loc) · 17.9 KB
/
UMainForm.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
unit UMainForm;
{ =========================================================================
NAME FacilSyntaxTool
Demo unit is part of a FacilSyntaxTool to test the capabilities of
SynFacil as a proof of concept, based on the work of
Tito Hinostroza units:
SynFacilBasic
SynFacilHighlighter
ORIGINAL SOURCE: https://github.com/t-edson/SynFacilSyn
COPYRIGHT (c) 12/2023 - Alexander Weidauer; [email protected]
This program is free software; you can redistribute it and/or modify
it under the same terms as SynFacilSyn itself, either version 1.21 or,
at your option, any later version of SynFacilSyn you may have available.
ADVISE: The units SynFacilBasic and SynFacilHighlighter are embedded as
original source code form https://github.com/t-edson/SynFacilSyn. No
modifications are made.
}
{$MODE OBJFPC}
{$LONGSTRINGS ON}
interface
{ === UNITS ============================================================== }
uses
Buttons,
Classes,
Controls,
Dialogs,
ExtCtrls,
Forms,
Graphics,
LCLType,
Menus,
StdCtrls,
SynEdit,
SynFacilHighlighter,
SynHighlighterXML,
SysUtils,
XMLPropStorage;
{ === CONSTANTS ========================================================= }
const
CFG_NAME = 'syn-facil-tool.xml';
{$IFDEF UNIX}
CFG_DIR = '.syn-facil';
{$ENDIF}
{$IFDEF WINDOWS}
CFG_DIR = 'Facil-Tool';
{$ENDIF}
{ === RESSOURCES ======================================================== }
resourcestring
RS_CODE = 'Code';
RS_CONFIG = 'Configuration';
RS_EXT_ALL = '*.*';
RS_EXT_XML = '*.xml';
RS_FMT_MODLINE = 'LINE: %d COLUMN: %d FILE: %s';
RS_MSG_MODIFY = '%s file %s was modified!%sDo you want to save the file?';
RS_SYNTAX = 'Syntax';
type
{ === TFrmMain -- Main Form Definition ================================= }
{ TFrmMain }
TFrmMain = class(TForm)
{ Button to end the application. }
BtnQuit: TBitBtn;
{ Button to save the application configuration. }
BtnStoreConfig: TBitBtn;
{ Button to call the syntax popoup menu. }
BtnMenuSyntax: TBitBtn;
{ Button to open the application configuration. }
BtnOpenConfig: TBitBtn;
{ Button to call the example code popoup menu. }
BtnMenuCode: TBitBtn;
{ Checkox to enable automatic storage of the syntax file if,
the syntax rules are applied to the highlighter. }
ChkAsSyntax: TCheckBox;
{ Checkox to enable automatic storage of the example code file if,
the syntax rules are applied to the highlighter. }
ChkAsCode: TCheckBox;
{ Not used yet }
DlgColor: TColorDialog;
{ OpenDialog to open files }
DlgOpen: TOpenDialog;
{ SaveDialog to save files }
DlgSave: TSaveDialog;
{ Edit to hold or change the configuration file }
EdtFileConfig: TEdit;
{ Edit to hold or change the example code file }
EdtFileCode: TEdit;
{ Edit to hold or change the xml syntax file }
EdtFileSyntax: TEdit;
{ SynEditor for the example code file }
EdtCode: TSynEdit;
{ SynEditor for the xml syntax file }
EdtSyntax: TSynEdit;
{ GroupBox to group code related stuff }
GbxCode: TGroupBox;
{ GroupBox to group syntax related stuff }
GbxSyntax: TGroupBox;
{ Label for EdtConfig }
LabConfig: TLabel;
{ Menu entry to store example code as ...calls DlgStore }
MnuCodeStoreAs: TMenuItem;
{ Menu entry to store example code using the current name }
MnuStoreCode: TMenuItem;
{ Menu entry to open example code as ...calls DlgOpen }
MnuOpenCode: TMenuItem;
{ Menu entry to applay the syntax rules to hilight the example code }
MnuSyntaxApply: TMenuItem;
{ Menu entry to store xml syntax rules as ...calls DlgStore }
MnuSyntaxStoreAs: TMenuItem;
{ Menu entry to store xml syntax using the current name }
MnuSyntaxStore: TMenuItem;
{ Menu entry to open xml syntax rules ...calls DlgOpen }
MnuSyntaxOpen: TMenuItem;
{ Menu separator }
MnuSepApply: TMenuItem;
{ Panel to hold the config elements }
PnlTop: TPanel;
{ Panel to hold the editors }
PnlEditors: TPanel;
{ Panel to hold code config related components }
PnlCodeTop: TPanel;
{ Panel to hold code syntax related components }
PnlSyntaxTop: TPanel;
{ Panel code editor modeline }
PnlCodeModLine: TPanel;
{ Panel syntax editor modeline }
PnlSyntaxModline: TPanel;
{ Popup menu code }
PopCode: TPopupMenu;
{ Popup menu syntax }
PopSyntax: TPopupMenu;
{ Splitter between editor group boxes }
SplitEditors: TSplitter;
{ Highighter fir the xml syntax file }
HlXml: TSynXMLSyn;
{ Property storage for the app in ~/.syn-facil/syn-facil-tool.xml }
XMLConfig: TXMLPropStorage;
{ === Event declarations ========================================== }
{ Open popup menu code example editor }
procedure BtnMenuCodeClick(Sender: TObject);
{ Open popup menu syntax editor }
procedure BtnMenuSyntaxClick(Sender: TObject);
{ Open the application configuration ..LoadEditDialog }
procedure BtnOpenConfigClick(Sender: TObject);
{ Sores the current configuration }
procedure BtnStoreConfigClick(Sender: TObject);
{ End the application ..FormCloseQuery }
procedure BtnQuitClick(Sender: TObject);
{ Open the example code ..LoadEditDialog }
procedure MnuOpenCodeClick(Sender: TObject);
{ Stores the example code editor ..SaveEdit }
procedure MnuStoreCodeClick(Sender: TObject);
{ Stores the example code editor ..SaveEditAs }
procedure MnuStoreAsCodeClick(Sender: TObject);
{ Apply the syntax rules to the example code editor }
procedure MnuApplySyntaxClick(Sender: TObject);
{ Open the xml syntax rules ..LoadEditDialog }
procedure MnuOpenSyntaxClick(Sender: TObject);
{ Stores the xml rules ..SaveEdit }
procedure MnuStoreSyntaxClick(Sender: TObject);
{ Stores the xml rules editor ..SaveEditAs }
procedure MnuStoreAsSyntaxClick(Sender: TObject);
{ Check updates to trigger modline modifications for code editor }
procedure EdtCodeChangeUpdating(ASender: TObject; AnUpdating: Boolean);
{ Check updates to trigger modline modifications for code editor }
procedure EdtSyntaxChangeUpdating(ASender: TObject; AnUpdating: Boolean);
{ Init application when window is open load config }
procedure FormActivate(Sender: TObject);
{ Check and store if editors are modified }
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
{ Init application allocate basic objects }
procedure FormCreate(Sender: TObject);
{ Destroy allocated basic }
procedure FormDestroy(Sender: TObject);
public
{ === Public variables ============================================ }
{ The "generic" hilighter applied to the code example }
HlGeneric: TSynFacilSyn;
{ Home directory }
DirUser: String;
{ Configuration directory ~/.syn-facil }
DirConfig: String;
{ Configuration file ~/.syn-facil/syn-facil-tool.xml }
FileConfig: String;
{ File name abbrevation code example file for modeline }
FileCode: String;
{ File name abbrevation xml rule file for modeline }
FileSyntax: String;
{ Form.Activate and initialization was visited }
Activated: Boolean;
{ === Service routines ============================================ }
{ Applys the xml rules to the example config }
procedure ApplyGeneric;
{ Loads an Code or Syntax into the editor using the config.
Parameter:
aType -- Code, Config or Syntax
aEdt -- Edit holding the file name ..will be changed ..TEXT
aFile -- abbrevated name
aSyn -- The editor
Returns: TRUE if successful.
}
function LoadEdit(const aType: String;
aEdt: TEdit;
var aFile: String;
aSyn: TSynEdit): Boolean;
{ Loads an Code, Config or Syntax into the editor/ xml properties
using the open dialog.
Parameter:
aType -- Code, Config or Syntax
aExt -- *.* or *.xml
aDlg -- The open dialog
aEdt -- Edit holding the file name ..will be changed ..TEXT
aFile -- abbrevated name
aSyn -- The editor or NIL
Returns: TRUE if successful.
}
function LoadEditDlg(const aType, aExt: String;
aDlg: TOpenDialog;
aEdt: TEdit;
var aFile: String;
aSyn: TSynEdit): Boolean;
{ Save an edtor file with current filename
Parameter:
aType -- Code or Syntax
aName -- Filename
aSyn -- Editor
aAsk -- TRUE Ask to perform and overwrite or
FALSE operate silently when auto-save is checked
}
procedure SaveEdit(const aType, aName: String;
aSyn: TSynEdit; aAsk: Boolean = TRUE);
{ Save an editor file with current name whit dialog
Parameter:
aType -- Code, Config, Syntax
aExt -- Extensions *.*, *.xml
aDlg -- The save dialog
aEdit -- Edit holhing the file name ..can be changed (VAR)
aFile -- aFilename short
aSyn -- SynEdit -- for Code and Syntax or NIL for Config
Returns: TRUE whe successful
}
function SaveEditAs(const aType, aExt: String;
aDlg: TSaveDialog;
aEdt: TEdit;
var aFile: String;
aSyn: TSynEdit): Boolean;
end;
{ == Main Form ========================================================== }
var
FrmMain: TFrmMain;
{ === Implementation ==================================================== }
implementation
{$R *.lfm}
{ TFrmMain }
procedure TFrmMain.ApplyGeneric;
var
lStream: TStringStream;
begin
if (FileCode <> '') and EdtCode.Modified then begin
if ChkAsCode.Checked then
SaveEdit(RS_CODE, EdtFileCode.Text, EdtCode, FALSE);
end;
if (FileSyntax <> '') and EdtSyntax.Modified then begin
if ChkAsSyntax.Checked then
SaveEdit(RS_SYNTAX, EdtFileSyntax.Text, EdtSyntax, FALSE);
end;
lStream:= TStringStream.Create(EdtSyntax.Text);
try
try
HlGeneric.LoadFromStream(lStream);
EdtCode.Invalidate;
except
on e: Exception do begin
MessageDlg(e.Message, mtError, [mbOK], 0);
exit;
end;
end;
finally
lStream.Free;
end;
end;
{ === Button events ===================================================== }
procedure TFrmMain.BtnOpenConfigClick(Sender: TObject);
begin
if not LoadEditDlg(RS_CONFIG, RS_EXT_XML, DlgOpen,
EdtFileConfig, FileConfig, NIL) then Exit;
XMLConfig.FileName := EdtFileConfig.Text;
FileConfig:= EdtFileConfig.Text;
try
XMLConfig.Restore;
except
on e: Exception do begin
MessageDlg(e.Message, mtError, [mbOK], 0);
exit;
end;
end;
end;
procedure TFrmMain.BtnStoreConfigClick(Sender: TObject);
begin
if not SaveEditAs(RS_CONFIG, RS_EXT_ALL, DlgSave, EdtFileConfig,
FileConfig, NIL ) then exit;
end;
procedure TFrmMain.BtnMenuSyntaxClick(Sender: TObject);
begin
PopSyntax.PopUp( FrmMain.Left
+ EdtFileSyntax.Width
+ BtnMenuSyntax.Width
- 175,
FrmMain.Top
+ PnlTop.Height
+ PnlSyntaxTop.Height
+ 20);
end;
procedure TFrmMain.BtnMenuCodeClick(Sender: TObject);
begin
PopCode.PopUp( FrmMain.Left
+ GbxSyntax.Width
+ EdtFileCode.Width
+ BtnMenuCode.Width
- 158,
FrmMain.Top
+ PnlTop.Height
+ PnlCodeTop.Height
+ 20);
end;
procedure TFrmMain.BtnQuitClick(Sender: TObject);
begin
Close;
end;
{ === SynEdit events =================================================== }
procedure TFrmMain.EdtCodeChangeUpdating(ASender: TObject;
AnUpdating: Boolean);
var
lPos: TPoint;
begin
PnlCodeModline.Caption:='.';
if FileCode='' then Exit;
lPos:=EdtCode.LogicalCaretXY;
PnlCodeModline.Caption := Format(RS_FMT_MODLINE,
[lPos.Y, lPos.X, FileCode]) ;
end;
procedure TFrmMain.EdtSyntaxChangeUpdating(ASender: TObject;
AnUpdating: Boolean);
var
lPos: TPoint;
begin
PnlSyntaxModline.Caption:='.';
if FileSyntax='' then Exit;
lPos:=EdtSyntax.LogicalCaretXY;
PnlSyntaxModline.Caption := Format(RS_FMT_MODLINE,
[lPos.Y, lPos.X, FileSyntax]) ;
end;
{ === Form events ===================================================== }
procedure TFrmMain.FormActivate(Sender: TObject);
begin
if Activated then exit;
Activated := true;
if EdtFileCode.Text <> '' then begin
if not LoadEdit(RS_CODE, EdtFileCode, FileCode, EdtCode) then
exit;
end;
if EdtFileSyntax.Text <> '' then begin
if not LoadEdit(RS_SYNTAX, EdtFileSyntax, FileSyntax, EdtSyntax) then
exit;
ApplyGeneric;
end;
end;
procedure TFrmMain.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
if (FileCode <> '') and EdtCode.Modified then begin
SaveEdit(RS_CODE, EdtFileCode.Text, EdtCode, not ChkAsCode.Checked)
end;
if (FileSyntax <> '') and EdtSyntax.Modified then begin
SaveEdit(RS_SYNTAX, EdtFileSyntax.Text, EdtSyntax,
not ChkAsSyntax.Checked)
end;
CanClose := True;
end;
procedure TFrmMain.FormCreate(Sender: TObject);
begin
HlGeneric := TSynFacilSyn.Create(self);
EdtCode.Highlighter := HlGeneric;
DirUser:= GetUserDir;
DirConfig:= ConcatPaths([DirUser, CFG_DIR]);
FileConfig:= ConcatPaths([DirConfig, CFG_NAME]);
if not DirectoryExists(DirConfig) then CreateDir(DirConfig);
XMLConfig.FileName:=FileConfig;
XMLConfig.Restore;
EdtFileConfig.Text := FileConfig;
DlgOpen.Filename :='.';
FileSyntax:='';
FileCode:='';
Activated:=False;
end;
procedure TFrmMain.FormDestroy(Sender: TObject);
begin
HlGeneric.Free;
end;
{ === Loading routines ================================================== }
function TFrmMain.LoadEdit(const aType: String;
aEdt: TEdit;
var aFile: String;
aSyn: TSynEdit): Boolean;
begin
try
aSyn.Lines.LoadFromFile(aEdt.Text);
aFile := ExtractFileName(aEdt.Text);
Result := TRUE;
except
on e: Exception do begin
MessageDlg(e.Message, mtError, [mbOK], 0);
Result:= FALSE;
end;
end;
end;
function TFrmMain.LoadEditDlg(const aType, aExt: String;
aDlg: TOpenDialog;
aEdt: TEdit;
var aFile: String;
aSyn: TSynEdit): Boolean;
begin
aDlg.Filter := aType+'|'+aExt;
aDlg.FileName := aEdt.Text;
if not aDlg.Execute then exit;
aFile := '';
aEdt.Text:='';
try
if Assigned(aSyn) then aSyn.Lines.LoadFromFile(aDlg.FileName);
aEdt.Text:=aDlg.FileName;
if Assigned(aSyn) then aFile:= ExtractFileName(aDlg.FileName)
else aFile:= aDlg.FileName;
Result := TRUE;
except
on e: Exception do begin
MessageDlg(e.Message, mtError, [mbOK], 0);
Result:= FALSE;
end;
end;
end;
{ === Popup Menu events ================================================= }
procedure TFrmMain.MnuApplySyntaxClick(Sender: TObject);
begin
ApplyGeneric;
end;
procedure TFrmMain.MnuOpenCodeClick(Sender: TObject);
begin
if not LoadEditDlg(RS_CODE, RS_EXT_ALL, DlgOpen,
EdtFileCode, FileCode, EdtCode) then Exit;
end;
procedure TFrmMain.MnuOpenSyntaxClick(Sender: TObject);
begin
if not LoadEditDlg(RS_SYNTAX, RS_EXT_XML, DlgOpen,
EdtFileSyntax, FileSyntax, EdtSyntax) then Exit;
ApplyGeneric;
end;
procedure TFrmMain.MnuStoreCodeClick(Sender: TObject);
begin
SaveEdit(RS_CODE,EdtFileCode.Text,EdtCode, FALSE);
end;
procedure TFrmMain.MnuStoreAsCodeClick(Sender: TObject);
begin
if not SaveEditAs(RS_CODE, RS_EXT_ALL, DlgSave, EdtFileCode,
FileCode, EdtCode ) then exit;
end;
procedure TFrmMain.MnuStoreSyntaxClick(Sender: TObject);
begin
SaveEdit(RS_SYNTAX,EdtFileSyntax.Text,EdtSyntax, FALSE);
end;
procedure TFrmMain.MnuStoreAsSyntaxClick(Sender: TObject);
begin
if not SaveEditAs(RS_SYNTAX, RS_EXT_XML, DlgSave, EdtFileSyntax,
FileSyntax, EdtSyntax ) then exit;
end;
{ === Store routines ==================================================== }
function TFrmMain.SaveEditAs(const aType, aExt: String;
aDlg: TSaveDialog;
aEdt: TEdit;
var aFile: String;
aSyn: TSynEdit): Boolean;
begin
aDlg.Filter := aType+'|'+aExt;
aDlg.FileName := aEdt.Text;
if not aDlg.Execute then exit;
aFile := '';
aEdt.Text:='';
try
if Assigned(aSyn) then aSyn.Lines.SaveToFile(aDlg.FileName);
aEdt.Text:=aDlg.FileName;
if Assigned(aSyn) then aFile:= ExtractFileName(aDlg.FileName)
else aFile:= aDlg.FileName;
Result := TRUE;
except
on e: Exception do begin
MessageDlg(e.Message, mtError, [mbOK], 0);
Result:= FALSE;
end;
end;
end;
procedure TFrmMain.SaveEdit(const aType, aName: String;
aSyn: TSynEdit; aAsk: Boolean);
begin
if aAsk then begin
if MessageDlg(Format(RS_MSG_MODIFY, [aType, aName, LineEnding]),
mtConfirmation, [mbYes, mbNo], 0) = mrNo then Exit;
end;
try
aSyn.Lines.SaveToFile(aName);
except
on e: Exception do begin
MessageDlg(e.Message, mtError, [mbOK], 0);
end;
end;
end;
end.