-
Notifications
You must be signed in to change notification settings - Fork 0
/
pEditorPlus.pas
431 lines (385 loc) · 9.15 KB
/
pEditorPlus.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
{$mode objfpc}{$H+}{$J-}
program PascalEditor;
uses
Crt, SysUtils, StrUtils, Classes;
const
ProgramName = 'p-Editor';
Version = '1.0';
Copyright = '© 2023 Morales Research Inc';
DictionaryFileName = 'dictionary.txt';
type
TTextStyles = (Normal, Heading, Subtitle, Title, Bold, Italics);
var
FileName: string;
FileText: TStringList;
CursorX, CursorY: Integer;
Command: char;
Dictionary: TStringList;
ScrollOffset: Integer;
RulerVisible: Boolean = True;
procedure SetTextColor(Color: Byte);
begin
TextColor(Color);
end;
function LoadDictionary(FileName: string): TStringList;
begin
Result := TStringList.Create;
try
Result.LoadFromFile(FileName);
except
on E: Exception do
writeln('Error loading dictionary: ', E.Message);
end;
end;
procedure SaveDictionary(FileName: string; Dictionary: TStringList);
begin
try
Dictionary.SaveToFile(FileName);
writeln('Dictionary updated and saved.');
except
on E: Exception do
writeln('Error saving dictionary: ', E.Message);
end;
end;
function IsWordInDictionary(Word: string): Boolean;
begin
Result := Dictionary.IndexOf(Word) >= 0;
end;
procedure DisplayInformation;
begin
SetTextColor(LightCyan);
WriteLn(ProgramName, ' ', Version);
SetTextColor(LightGray);
WriteLn(Copyright);
end;
procedure DisplayRuler;
var
i: Integer;
begin
SetTextColor(LightGray);
write('1');
for i := 1 to WindowWidth - 1 do
write('0');
writeln;
end;
procedure DisplayFileContents;
var
Line: string;
i: Integer;
LineNumber: Integer;
DisplayedLines: Integer;
begin
ClrScr;
GotoXY(1, 1);
LineNumber := ScrollOffset + 1;
DisplayedLines := 0;
if RulerVisible then
begin
DisplayRuler;
Inc(DisplayedLines);
end;
for i := ScrollOffset to FileText.Count - 1 do
begin
if DisplayedLines >= WindowHeight then
Break;
Line := FileText[i];
if Pos('# ', Line) = 1 then
begin
// Heading style
SetTextColor(LightCyan);
writeln(Copy(Line, 3, MaxInt));
end
else if Pos('## ', Line) = 1 then
begin
// Subtitle style
SetTextColor(Yellow);
writeln(Copy(Line, 4, MaxInt));
end
else if Pos('### ', Line) = 1 then
begin
// Title style
SetTextColor(Yellow);
writeln(Copy(Line, 5, MaxInt));
end
else if Pos('* ', Line) = 1 then
begin
// Bold style
SetTextColor(LightGray + Blink);
writeln(Copy(Line, 3, MaxInt));
end
else if Pos('_ ', Line) = 1 then
begin
// Italics style
SetTextColor(LightGray);
writeln(Copy(Line, 3, MaxInt));
end
else
writeln(LineNumber, ': ', Line);
Inc(LineNumber);
Inc(DisplayedLines);
end;
GotoXY(CursorX, CursorY - ScrollOffset);
end;
function AskToAddToDictionary(Word: string): Boolean;
var
Response: char;
begin
writeln('The word "', Word, '" is not in the dictionary.');
write('Do you want to add it to the dictionary? (Y/N): ');
readln(Response);
Result := UpCase(Response) = 'Y';
end;
procedure InsertTextAtCursor(Text: string);
var
Line: string;
Words: TStringList;
i: Integer;
begin
Words := TStringList.Create;
try
Line := FileText[CursorY - 1];
Insert(Text, Line, CursorX);
// Split the line into words
ExtractStrings([' ', ',', '.', ';', ':', '!', '?'], [], PChar(Line), Words);
// Check each word for spelling
for i := 0 to Words.Count - 1 do
begin
if not IsWordInDictionary(Words[i]) then
begin
if AskToAddToDictionary(Words[i]) then
begin
Dictionary.Add(Words[i]);
SaveDictionary(DictionaryFileName, Dictionary);
end;
end;
end;
FileText[CursorY - 1] := Line;
Inc(CursorX, Length(Text));
finally
Words.Free;
end;
end;
procedure ApplyStyleToLine(var LineToStyle: string; Style: TTextStyles);
begin
case Style of
Heading: LineToStyle := '# ' + LineToStyle;
Subtitle: LineToStyle := '## ' + LineToStyle;
Title: LineToStyle := '### ' + LineToStyle;
Bold: LineToStyle := '* ' + LineToStyle;
Italics: LineToStyle := '_ ' + LineToStyle;
end;
end;
procedure InsertStyledTextAtCursor(Text: string; Style: TTextStyles);
var
Line: string;
begin
Line := Text;
if Style <> Normal then
ApplyStyleToLine(Line, Style);
InsertTextAtCursor(Line);
end;
procedure MoveCursorLeft;
begin
if CursorX > 1 then
Dec(CursorX);
end;
procedure MoveCursorRight;
var
MaxX: Integer;
begin
MaxX := Length(FileText[CursorY - 1]) + 1;
if CursorX < MaxX then
Inc(CursorX);
end;
procedure MoveCursorUp;
begin
if CursorY > 1 then
begin
Dec(CursorY);
CursorX := 1;
if CursorY < ScrollOffset + 1 then
Dec(ScrollOffset);
end;
end;
procedure MoveCursorDown;
var
MaxY: Integer;
begin
MaxY := FileText.Count;
if CursorY < MaxY then
begin
Inc(CursorY);
CursorX := 1;
if CursorY > ScrollOffset + WindowHeight then
Inc(ScrollOffset);
end;
end;
procedure DeleteCharAtCursor;
var
Line: string;
begin
if CursorX > 1 then
begin
Line := FileText[CursorY - 1];
Delete(Line, CursorX, 1);
FileText[CursorY - 1] := Line;
MoveCursorLeft;
end;
end;
procedure ToggleRuler;
begin
RulerVisible := not RulerVisible;
end;
procedure CreateDictionaryFile;
begin
writeln('The dictionary file ("', DictionaryFileName, '") is missing.');
write('Do you want to generate a new empty dictionary file? (Y/N): ');
if UpCase(ReadKey) = 'Y' then
begin
Dictionary := TStringList.Create;
SaveDictionary(DictionaryFileName, Dictionary);
writeln('Empty dictionary file ("', DictionaryFileName, '") generated.');
end
else
writeln('Bypassing dictionary requirement.');
end;
procedure CreateNewFile;
begin
writeln('Creating a new empty file.');
FileText.Clear;
FileText.Add('');
CursorX := 1;
CursorY := 1;
ScrollOffset := 0;
end;
procedure OpenFile;
var
NewFileName: string;
begin
writeln('Enter the name of the file to open (leave empty to cancel):');
write('> ');
readln(NewFileName);
if NewFileName <> '' then
begin
try
FileText.LoadFromFile(NewFileName);
FileName := NewFileName;
CursorX := 1;
CursorY := 1;
ScrollOffset := 0;
except
on E: Exception do
writeln('Error opening file: ', E.Message);
end;
end;
end;
procedure SaveFile;
begin
if FileName <> '' then
begin
try
FileText.SaveToFile(FileName);
writeln('File saved: ', FileName);
except
on E: Exception do
writeln('Error saving file: ', E.Message);
end;
end
else
writeln('No file is currently open. Use "Save As" to specify a new file.');
end;
procedure SaveFileAs;
var
NewFileName: string;
begin
writeln('Enter the name of the file to save as:');
write('> ');
readln(NewFileName);
if NewFileName <> '' then
begin
try
FileText.SaveToFile(NewFileName);
writeln('File saved as: ', NewFileName);
FileName := NewFileName;
except
on E: Exception do
writeln('Error saving file: ', E.Message);
end;
end;
end;
procedure PerformSpellCheck;
var
Line: string;
Words: TStringList;
i: Integer;
begin
writeln('Performing spell check...');
Words := TStringList.Create;
try
for Line in FileText do
begin
// Split the line into words
ExtractStrings([' ', ',', '.', ';', ':', '!', '?'], [], PChar(Line), Words);
// Check each word for spelling
for i := 0 to Words.Count - 1 do
begin
if not IsWordInDictionary(Words[i]) then
writeln('Spelling error: "', Words[i], '" on line ', FileText.IndexOf(Line) + 1);
end;
end;
finally
Words.Free;
end;
writeln('Spell check completed.');
end;
begin
// Load the dictionary
Dictionary := LoadDictionary(DictionaryFileName);
if Dictionary.Count = 0 then
CreateDictionaryFile;
FileText := TStringList.Create;
DisplayInformation;
writeln('Commands:');
writeln(' N - New File');
writeln(' O - Open File');
writeln(' S - Save File');
writeln(' A - Save File As');
writeln(' Q - Quit');
writeln(' Arrow keys - Move cursor');
writeln(' I - Insert text at cursor');
writeln(' DEL - Delete character at cursor');
writeln(' B - Bold style');
writeln(' I - Italics style');
writeln(' T - Title style');
writeln(' S - Subtitle style');
writeln(' H - Heading style');
writeln(' C - Perform Spell Check');
writeln(' R - Toggle Ruler');
writeln('');
Command := #0;
CreateNewFile;
while Command <> 'Q' do
begin
DisplayFileContents;
Command := UpCase(ReadKey);
case Command of
'N': CreateNewFile;
'O': OpenFile;
'S': SaveFile;
'A': SaveFileAs;
'C': PerformSpellCheck;
'R': ToggleRuler;
#72: MoveCursorUp; // Up arrow
#80: MoveCursorDown; // Down arrow
#75: MoveCursorLeft; // Left arrow
#77: MoveCursorRight; // Right arrow
'I': InsertTextAtCursor('Hello, world!');
#8: DeleteCharAtCursor; // DEL key
'B': InsertStyledTextAtCursor('Bold text', Bold);
'T': InsertStyledTextAtCursor('Title', Title);
// 'S': InsertStyledTextAtCursor('Subtitle', Subtitle);
// 'H': InsertStyledTextAtCursor('Heading', Heading);
end;
end;
writeln('Goodbye!');
end.