-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainunit.pas
1219 lines (1057 loc) · 34.2 KB
/
mainunit.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit mainunit;
{$mode objfpc}{$H+}
interface
uses
Classes, SynEdit, Forms, Controls, Graphics, Dialogs, StdCtrls,
Menus, ExtCtrls, ComCtrls, ActnList, UniqueInstance, SynEditMiscClasses,
SynEditKeyCmds, LCLType;
const
UndoCloseTabHistoryMaxSize = 100;
type
{ TForm1 }
TForm1 = class(TForm)
UndoCloseTabAction: TAction;
BeginFindAction: TAction;
Suggestions: TListBox;
StatusBar: TStatusBar;
TextQueryStatusLabel: TLabel;
TextQueryPanel: TPanel;
SaveNotesAction: TAction;
MoveTabForwardAction: TAction;
MoveTabBackwardAction: TAction;
PrevTabAction: TAction;
NextTabAction: TAction;
NewTabAction: TAction;
CloseTabAction: TAction;
MainTabsActionList: TActionList;
AwesomeBar: TEdit;
MainTabs: TTabControl;
TextQuery: TEdit;
TextEditor: TSynEdit;
DeselectSuggestionsTimer: TTimer;
UniqueInstance1: TUniqueInstance;
procedure AwesomeBarChange(Sender: TObject);
procedure AwesomeBarEnter(Sender: TObject);
procedure AwesomeBarKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
procedure BeginFindActionExecute(Sender: TObject);
procedure CloseTabActionExecute(Sender: TObject);
procedure DeselectSuggestionsTimerTimer(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure MainTabsChange(Sender: TObject);
procedure MainTabsChanging(Sender: TObject; var AllowChange: boolean);
procedure MoveTabBackwardActionExecute(Sender: TObject);
procedure MoveTabForwardActionExecute(Sender: TObject);
procedure NewTabActionExecute(Sender: TObject);
procedure NextTabActionExecute(Sender: TObject);
procedure PrevTabActionExecute(Sender: TObject);
procedure SaveNotesActionExecute(Sender: TObject);
procedure StatusBarDblClick(Sender: TObject);
procedure SuggestionsKeyDown(Sender: TObject; var Key: word;
Shift: TShiftState);
procedure TextEditorCommandProcessed(Sender: TObject;
var Command: TSynEditorCommand; var AChar: TUTF8Char; Data: pointer);
procedure TextEditorEnter(Sender: TObject);
procedure TextEditorExit(Sender: TObject);
procedure TextEditorKeyPress(Sender: TObject; var Key: char);
procedure TextEditorSpecialLineMarkup(Sender: TObject; Line: integer;
var Special: boolean; Markup: TSynSelectedColor);
procedure TextQueryChange(Sender: TObject);
procedure TextQueryEnter(Sender: TObject);
procedure TextQueryExit(Sender: TObject);
procedure TextQueryKeyPress(Sender: TObject; var Key: char);
procedure UndoCloseTabActionExecute(Sender: TObject);
procedure UniqueInstance1OtherInstance(Sender: TObject;
ParamCount: integer; const Parameters: array of string);
procedure SelectOrAddTab(const query: string);
procedure OpenShortcutFile(const fname: string);
private
FAllLines, FDiscardedLines, FAllTags, FTabCaretsAndViews: TStringList;
FFoundTextPoints: array of TPoint; //xy of caret for each found text
FAllTagCount, FSelectedTagCount: integer;
FUndoCloseTabHistory: array [0..UndoCloseTabHistoryMaxSize] of string;
FUndoCloseTabHistoryUsed: integer;
FBaseTitle: string;
procedure CollectAllTags;
procedure FilterTextByAwesomeBar;
procedure UpdateSuggestions;
function SaveNotes: boolean;
function QueryUnsavedChanges: TModalResult;
procedure RefreshFoundPoints;
procedure MoveCursorToNextFind;
procedure SaveCaretAndView(tabindex: integer);
procedure LoadCaretAndView(tabindex: integer);
procedure SortTodoListAroundCurrentLine;
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses
StrUtils, dateutils, Math, LCLIntf, SynEditMarkupHighAll, SysUtils,
SynHighlighterAny, FileUtil, SynGutterLineNumber;
const
LineCountPanel = 0;
TagCountPanel = 1;
QueryTimePanel = 2;
OldSizePanel = 3;
NotesPathSizePanel = 4;
{ TForm1 }
function IsTagLine(const line: string): boolean;
var
i: integer;
begin
//a tag line must start with # and be longer than 2
if (not line.StartsWith('#')) or (Length(line) < 2) then
Exit(False);
//a tagline must not have hashes inside words
for i := 2 to Length(line) do
if (line[i] = '#') and (line[i - 1] <> ' ') then
Exit(False);
//a tagline must not contain words without hash in front
for i := 2 to Length(line) do
if (line[i] <> '#') and (line[i] <> ' ') and (line[i - 1] = ' ') then
Exit(False);
Exit(True);
end;
function TagLineWithTag(const line, tag: string): boolean;
begin
Result := IsTagLine(line) and (line + ' ').Contains('#' + tag + ' ');
end;
function LoadQueryFromShortcutFile(const fpath: string): string;
const
prefix = 'Query ';
var
Lines: TStringList;
line: string;
begin
try
Lines := TStringList.Create;
Lines.LoadFromFile(fpath);
for line in Lines do
if line.StartsWith(prefix) then
Exit(line.Substring(Length(prefix)));
finally
Lines.Free;
end;
Result := '';
end;
function GetFileNearExe(const fname: string): string;
begin
Result := ExtractFilePath(Application.ExeName) + fname;
end;
function PrettyPrintFileSize(size: int64): string;
begin
if size < 0 then
Exit('Error');
if size < intpower(1024.0, 1) then
Exit(IntToStr(size) + ' Bytes');
if size < intpower(1024.0, 2) then
Exit(FloatToStr(RoundTo(size / intpower(1024.0, 1), -3)) + ' KiB');
if size < intpower(1024.0, 3) then
Exit(FloatToStr(RoundTo(size / intpower(1024.0, 2), -3)) + ' MiB');
if size < intpower(1024.0, 4) then
Exit(FloatToStr(RoundTo(size / intpower(1024.0, 3), -3)) + ' GiB');
Result := FloatToStr(RoundTo(size / intpower(1024.0, 4), -3)) + ' TiB';
end;
function GetOldDirSize: string;
var
oname, fname: string;
list: TStringList;
fsize, tsize: int64;
begin
oname := GetFileNearExe('old');
if not DirectoryExists(oname) then
Exit('Old: no directory.');
list := FindAllFiles(oname);
tsize := 0;
for fname in list do
begin
fsize := FileSize(fname);
if fsize > 0 then
tsize += fsize;
end;
Result := 'Old: ' + IntToStr(list.Count) + ' files, ' + PrettyPrintFileSize(tsize);
list.Free;
end;
function GetWordTouchingCursor(const line: string; idx: integer): string;
var
tmp: string;
start, ending: integer;
begin
tmp := ' ' + line + ' ';
start := idx + 1;
ending := idx + 1;
while tmp[start - 1] <> ' ' do
start := start - 1;
while tmp[ending] <> ' ' do
ending := ending + 1;
Result := Copy(tmp, start, ending - start);
end;
procedure EnsureNotesExist(const notefile: string);
var
initialnotefile: string;
content: TStringList;
begin
if FileExists(notefile) then
Exit;
initialnotefile := GetFileNearExe('botes-initial-allnotes.txt');
if FileExists(initialnotefile) then
begin
CopyFile(initialnotefile, notefile);
end;
content := TStringList.Create;
try
content.LineBreak := #10;
// below was auto generated
content.Add('#demo');
content.Add('Hello, controlls are:');
content.Add('Ctrl + S to save changes.');
content.Add(
'Esc - switch between this text area and the bar at the top that now says ''demo''.');
content.Add('Ctrl + Page Up/Down - move between tabs.');
content.Add('Ctrl + Shift + Page Up/Down - move the current tab around on the tab bar.');
content.Add('Ctrl + N - open a new tab.');
content.Add('Ctrl + T - open a new tab.');
content.Add('Ctrl + Shift + T - undo closing last tab.');
content.Add('Ctrl + W - close current tab.');
content.Add(
'Ctrl + D - (for TODO lists), toggle between [ ] and [x] if current line starts with either.');
content.Add(
'Ctrl + O - open URL or hashtag that''s selected or (if nothing''s selected) under cursor');
content.Add('');
content.Add('Ctrl + F when in main text area will open a simple search');
content.Add('');
content.Add('Ctrl + K - sorts a TODO list (putting done at the end), try:');
content.Add('[ ] item 1');
content.Add('[x] item 2');
content.Add('[x] item 3');
content.Add('[ ] item 4');
content.Add('[x] item 5');
content.Add('');
content.Add('Double click on status bar to open the directory Botes is in.');
content.Add('');
content.Add('Create old directory there to get a duplicate of every saved file.');
content.Add('See the included README.md for more details about this feature.');
content.Add('');
content.Add(
'Create a file with a single line (case sensitive) in the form of ''Query YOURTAG''');
content.Add(
'and pass it as first and only argument when starting Botes to create a shortcut');
content.Add(
'to open/select a tab with ''YOURTAG'' in it. You can further use a unique extension');
content.Add(
'(for example .bs, Botes shortcut) and associate it with Botes to create a file');
content.Add(
'that when opened with default app from explorer or command line will open Botes.');
content.Add('');
content.Add(
'There can only be one instance of Botes running, if you attempt to open another');
content.Add(
'it will instead focus the already opened one. This feature works properly with');
content.Add(
'the above shortcut file feature as well (it''ll open or focus a tab specified in');
content.Add('the file you attempted to open in the already existing instance).');
content.Add('');
content.Add(
'Check https://github.com/FRex/Botes for the latest version and more information.');
content.Add('');
content.Add('When the suggestions list box is visible pressing down will go into it');
content.Add('and pressing up when having first item selected in it or escape at');
content.Add('any time will go back to editing the query edit box.');
content.Add('');
content.Add(
'Pressing enter will select the item from list box, query it and go to text area.');
content.Add('Pressing backspace will go back to query edit box and put in it');
content.Add('the item minus the last character.');
content.Add('');
content.Add('');
content.SaveToFile(notefile);
finally
content.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
caretmarkup: TSynEditMarkupHighlightAllCaret;
matchmarkup: TSynEditMarkupHighlightAll;
i: integer;
allnotespath: string;
notespanelstr: string;
begin
WindowState := wsMaximized;
FBaseTitle := Caption;
FAllLines := TStringList.Create;
FAllLines.LineBreak := #10;
FDiscardedLines := TStringList.Create;
FDiscardedLines.LineBreak := #10;
FAllTags := TStringList.Create;
FAllLines.LineBreak := #10;
FTabCaretsAndViews := TStringList.Create;
FTabCaretsAndViews.LineBreak := #10;
MainTabs.Tabs.LineBreak := #10;
MainTabs.Options := MainTabs.Options + [nboDoChangeOnSetIndex];
StatusBar.Panels[OldSizePanel].Text := GetOldDirSize;
FUndoCloseTabHistoryUsed := 0;
allnotespath := GetFileNearExe('allnotes.txt');
EnsureNotesExist(allnotespath);
try
FAllLines.LoadFromFile(allnotespath);
notespanelstr := allnotespath + ' - ' + PrettyPrintFileSize(FileSize(allnotespath));
StatusBar.Panels[NotesPathSizePanel].Text := notespanelstr;
if TextEditor.Gutter.Parts.Part[1] is TSynGutterLineNumber then
(TextEditor.Gutter.Parts.Part[1] as TSynGutterLineNumber).DigitCount :=
Length(IntToStr(FAllLines.Count));
except
on EFOpenError do
MessageDlg('File not found', Format('Failed to open file: %s', [allnotespath]),
mtError, [mbOK], 0);
end;
FAllTags.Sorted := True;
FAllTags.Duplicates := dupIgnore;
CollectAllTags;
TextEditor.Lines.Assign(FAllLines);
caretmarkup := TSynEditMarkupHighlightAllCaret(
TextEditor.MarkupByClass[TSynEditMarkupHighlightAllCaret]);
caretmarkup.MarkupInfo.Background := clTeal;
caretmarkup.WaitTime := 100;
caretmarkup.Trim := True;
caretmarkup.FullWord := True;
matchmarkup := TSynEditMarkupHighlightAll(
TextEditor.MarkupByClass[TSynEditMarkupHighlightAll]);
matchmarkup.Enabled := False;
try
FTabCaretsAndViews.LoadFromFile(GetFileNearExe('tabcaretsandviews.txt'));
except
//ignore this error, since this is a file added in later version of the program
on EFOpenError do ;
end;
try
MainTabs.Tabs.LoadFromFile(GetFileNearExe('tabs.txt'));
for i := 0 to MainTabs.Tabs.Count - 1 do
if (length(MainTabs.Tabs[i]) > 0) and (MainTabs.Tabs[i][1] = '@') then
begin
MainTabs.Tabs[i] := Copy(MainTabs.Tabs[i], 2, length(MainTabs.Tabs[i]));
MainTabs.TabIndex := i;
end;
if ParamCount > 0 then
OpenShortcutFile(ParamStr(1));
except
//ignore EFOpenError - we start with one empty tab set in designer so its ok
on EFOpenError do ;
end;
//make sure we have entry for each tab, if there were more tabs than line numbers
while FTabCaretsAndViews.Count < MainTabs.Tabs.Count do
FTabCaretsAndViews.Add('');
//set first tab awesome bar
MainTabsChange(MainTabs);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FAllLines.Free;
FDiscardedLines.Free;
FAllTags.Free;
FTabCaretsAndViews.Free;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
//set focus to text if there is a tag query or to awesome bar if there isnt
if Length(MainTabs.Tabs[MainTabs.TabIndex]) = 0 then
AwesomeBar.SetFocus
else
TextEditor.SetFocus;
end;
procedure TForm1.MainTabsChange(Sender: TObject);
begin
AwesomeBar.Text := MainTabs.Tabs[MainTabs.TabIndex];
LoadCaretAndView(MainTabs.TabIndex);
end;
procedure TForm1.MainTabsChanging(Sender: TObject; var AllowChange: boolean);
var
ans: TModalResult;
begin
if TextEditor.Modified then
begin
ans := QueryUnsavedChanges;
if ans = mrCancel then
AllowChange := False;
if ans = mrYes then
AllowChange := SaveNotes;
if ans = mrNo then
TextEditor.Modified := False;
end; //if
end;
procedure TForm1.MoveTabBackwardActionExecute(Sender: TObject);
var
onchanging: TTabChangingEvent;
begin
if MainTabs.TabIndex = 0 then
Exit;
MainTabs.Tabs.Exchange(MainTabs.TabIndex, MainTabs.TabIndex - 1);
FTabCaretsAndViews.Exchange(MainTabs.TabIndex, MainTabs.TabIndex - 1);
onchanging := MainTabs.OnChanging;
MainTabs.OnChanging := nil;
MainTabs.TabIndex := MainTabs.TabIndex - 1;
MainTabs.OnChanging := onchanging;
end;
procedure TForm1.MoveTabForwardActionExecute(Sender: TObject);
var
onchanging: TTabChangingEvent;
begin
if (MainTabs.TabIndex + 1) = MainTabs.Tabs.Count then
Exit;
MainTabs.Tabs.Exchange(MainTabs.TabIndex, MainTabs.TabIndex + 1);
FTabCaretsAndViews.Exchange(MainTabs.TabIndex, MainTabs.TabIndex + 1);
onchanging := MainTabs.OnChanging;
MainTabs.OnChanging := nil;
MainTabs.TabIndex := MainTabs.TabIndex + 1;
MainTabs.OnChanging := onchanging;
end;
procedure TForm1.NewTabActionExecute(Sender: TObject);
begin
SaveCaretAndView(MainTabs.TabIndex);
MainTabs.Tabs.Append('');
FTabCaretsAndViews.Append('');
MainTabs.TabIndex := MainTabs.Tabs.Count - 1;
AwesomeBar.SetFocus;
end;
procedure TForm1.NextTabActionExecute(Sender: TObject);
var
tab: integer;
begin
tab := MainTabs.TabIndex + 1;
if tab = MainTabs.Tabs.Count then
tab := 0;
SaveCaretAndView(MainTabs.TabIndex);
MainTabs.TabIndex := tab;
LoadCaretAndView(tab);
end;
procedure TForm1.PrevTabActionExecute(Sender: TObject);
var
tab: integer;
begin
tab := MainTabs.TabIndex;
if tab = 0 then
tab := MainTabs.Tabs.Count;
SaveCaretAndView(MainTabs.TabIndex);
MainTabs.TabIndex := tab - 1;
LoadCaretAndView(tab - 1);
end;
procedure TForm1.SaveNotesActionExecute(Sender: TObject);
begin
SaveNotes;
end;
procedure TForm1.StatusBarDblClick(Sender: TObject);
begin
OpenDocument(GetFileNearExe(''));
end;
procedure TForm1.SuggestionsKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
var
str: string;
begin
if Key = VK_ESCAPE then
AwesomeBar.SetFocus;
if (Key = VK_RETURN) and (Suggestions.ItemIndex <> -1) then
begin
AwesomeBar.Text := Suggestions.Items[Suggestions.ItemIndex];
TextEditor.SetFocus;
end;
if (Key = VK_UP) and (Suggestions.ItemIndex = 0) then
begin
AwesomeBar.SetFocus;
AwesomeBar.SelStart := Length(AwesomeBar.Text);
AwesomeBar.SelLength := 0;
DeselectSuggestionsTimer.Enabled := True;
end;
if (Key = VK_BACK) and (Suggestions.ItemIndex <> -1) then
begin
str := Suggestions.Items[Suggestions.ItemIndex];
AwesomeBar.Text := str.Substring(0, Length(str) - 1);
AwesomeBar.SetFocus;
AwesomeBar.SelStart := Length(AwesomeBar.Text);
AwesomeBar.SelLength := 0;
DeselectSuggestionsTimer.Enabled := True;
end;
end;
procedure TForm1.TextEditorCommandProcessed(Sender: TObject;
var Command: TSynEditorCommand; var AChar: TUTF8Char; Data: pointer);
var
mark, word, line, url: string;
a, b: TPoint;
begin
if Command = (ecUserDefinedFirst + 0) then
begin
a.Create(1, TextEditor.CaretY);
b.Create(4, TextEditor.CaretY);
//save original text to not keep querying it and set the [x]/[ ] etc. more than once
mark := TextEditor.TextBetweenPoints[a, b];
//use TextBetweenPoints so this change is in editing history and can be ctrl-z'd
if mark = '[ ]' then
TextEditor.TextBetweenPoints[a, b] := '[x]';
if mark = '[x]' then
TextEditor.TextBetweenPoints[a, b] := '[ ]';
end;
if Command = (ecUserDefinedFirst + 1) then
begin
url := '';
//if anything is selected try to open it, otherwise open 'word' near cursor
if TextEditor.SelText <> '' then
url := TextEditor.SelText
else
begin
line := TextEditor.Lines[TextEditor.CaretY - 1];
if (Length(line) + 1) >= TextEditor.CaretX then
begin
word := GetWordTouchingCursor(line, TextEditor.CaretX);
//looking for substring :/ catches absolute Windows paths (with forward
//slashes) and URLs, looking for starting / catches absolute Linux paths
//starting with hashtag is to catch links to other notes within the app
if (Pos(':/', word) <> 0) or word.StartsWith('/') or word.StartsWith('#') then
url := word;
end;
end;
if url.StartsWith('#') then
SelectOrAddTab(Copy(url, 2, Length(url) + 100))
else if url <> '' then
OpenUrl(url);
end; //if command is user defined first + 1
if Command = (ecUserDefinedFirst + 2) then
SortTodoListAroundCurrentLine;
end;
function IsTodoLine(line: string): boolean;
begin
Exit((line.Substring(0, 3) = '[ ]') or (line.Substring(0, 3) = '[x]'));
end;
procedure TForm1.SortTodoListAroundCurrentLine;
var
firstTodo, lastTodo, cur: integer;
completed, pending: TStringList;
line: string;
begin
if not IsTodoLine(TextEditor.Lines[TextEditor.CaretY - 1]) then
Exit;
for cur := TextEditor.CaretY - 1 downto 0 do
begin
if not IsTodoLine(TextEditor.Lines[cur]) then
break;
firstTodo := cur;
end;
for cur := TextEditor.CaretY - 1 to TextEditor.Lines.Count - 1 do
begin
if not IsTodoLine(TextEditor.Lines[cur]) then
break;
lastTodo := cur;
end;
completed := TStringList.Create;
pending := TStringList.Create;
for cur := firstTodo to lastTodo do
if TextEditor.Lines[cur][2] = 'x' then
completed.Append(TextEditor.Lines[cur])
else
pending.Append(TextEditor.Lines[cur]);
TextEditor.BeginUpdate;
for cur := firstTodo to lastTodo do
begin
if (cur - firstTodo) < pending.Count then
line := pending[cur - firstTodo]
else
line := completed[cur - firstTodo - pending.Count];
TextEditor.TextBetweenPoints[TPoint.Create(0, cur + 1),
TPoint.Create(maxLongint, cur + 1)] := line;
end;
TextEditor.EndUpdate;
completed.Free;
pending.Free;
end;
procedure TForm1.TextEditorEnter(Sender: TObject);
begin
Suggestions.Visible := False;
TextEditor.MarkupByClass[TSynEditMarkupHighlightAllCaret].Enabled := True;
end;
procedure TForm1.TextEditorExit(Sender: TObject);
begin
TextEditor.MarkupByClass[TSynEditMarkupHighlightAllCaret].Enabled := False;
end;
procedure TForm1.TextEditorKeyPress(Sender: TObject; var Key: char);
begin
if Ord(Key) = 27 then
AwesomeBar.SetFocus;
end;
procedure TForm1.TextEditorSpecialLineMarkup(Sender: TObject;
Line: integer; var Special: boolean; Markup: TSynSelectedColor);
var
notempty: boolean;
begin
if IsTagLine(TextEditor.Lines[Line - 1]) then
begin
Special := True;
if TagLineWithTag(TextEditor.Lines[Line - 1], AwesomeBar.Text) then
begin
Markup.Background := clMoneyGreen;
Markup.Foreground := clBlack;
end
else
begin
Markup.Background := clMaroon;
Markup.Foreground := clCream;
end;
end; //if IsTagLine(linestr)
//special case for an UNSAVEABLE edit: highlight first line if it's not a tag
//line except if it's empty AND the only line in the editor which can be saved
//because it's a special case when deleting all notes in a given tag
notempty := not ((TextEditor.Lines.Count = 1) and (TextEditor.Lines[0] = ''));
if (Line = 1) and (not IsTagLine(TextEditor.Lines[0])) and notempty then
begin
Special := True;
Markup.Background := clMaroon;
Markup.Foreground := clCream;
end;
end;
procedure TForm1.TextQueryChange(Sender: TObject);
var
matchmarkup: TSynEditMarkupHighlightAll;
begin
RefreshFoundPoints;
matchmarkup := TSynEditMarkupHighlightAll(
TextEditor.MarkupByClass[TSynEditMarkupHighlightAll]);
matchmarkup.SearchString := TextQuery.Text;
TextQueryStatusLabel.Caption :=
Format('Find: %d results', [Length(FFoundTextPoints)]);
end;
procedure TForm1.TextQueryEnter(Sender: TObject);
begin
Suggestions.Visible := False;
RefreshFoundPoints;
MoveCursorToNextFind;
TextEditor.MarkupByClass[TSynEditMarkupHighlightAll].Enabled := True;
end;
procedure TForm1.TextQueryExit(Sender: TObject);
begin
TextQueryPanel.Hide;
TextEditor.MarkupByClass[TSynEditMarkupHighlightAll].Enabled := False;
end;
procedure TForm1.TextQueryKeyPress(Sender: TObject; var Key: char);
begin
if Ord(Key) = 27 then
TextEditor.SetFocus;
if Ord(Key) = 13 then
MoveCursorToNextFind;
end;
procedure TForm1.UndoCloseTabActionExecute(Sender: TObject);
begin
//FUndoCloseTabHistoryUsed < 0 should NEVER happen but just to be safe
if FUndoCloseTabHistoryUsed <= 0 then
Exit;
//pop element from history and open a tab with it
FUndoCloseTabHistoryUsed := FUndoCloseTabHistoryUsed - 1;
MainTabs.Tabs.Append(FUndoCloseTabHistory[FUndoCloseTabHistoryUsed]);
MainTabs.TabIndex := MainTabs.Tabs.Count - 1;
//deselect and move cursor to end of string
if AwesomeBar.Focused then
begin
AwesomeBar.SelLength := 0;
AwesomeBar.SelStart := Length(AwesomeBar.Text);
end;
end;
procedure TForm1.UniqueInstance1OtherInstance(Sender: TObject;
ParamCount: integer; const Parameters: array of string);
begin
if ParamCount > 0 then
OpenShortcutFile(Parameters[0]);
Application.Restore;
Application.BringToFront;
end;
procedure TForm1.SelectOrAddTab(const query: string);
var
i: integer;
begin
if query = '' then
Exit;
//backwards to select rightmost tab if there are duplicates
for i := (MainTabs.Tabs.Count - 1) downto 0 do
begin
if MainTabs.Tabs[i] = query then
begin
MainTabs.TabIndex := i;
Exit;
end;
end; //for
//if above loop didn't find and select a tab we must add it
MainTabs.Tabs.Append(query);
MainTabs.TabIndex := MainTabs.Tabs.Count - 1;
end;
procedure TForm1.OpenShortcutFile(const fname: string);
var
shortcutquery: string;
begin
try
shortcutquery := LoadQueryFromShortcutFile(fname);
SelectOrAddTab(shortcutquery);
except
on EFOpenError do
MessageDlg('File not found', Format('Failed to open shortcut file: %s', [fname]),
mtError, [mbOK], 0);
end;
end;
function PrettyPrintMilliSeconds(totalms: int64): string;
var
s, ms: integer;
begin
s := totalms div 1000;
ms := totalms mod 1000;
Result := Format('%d.%.3ds', [s, ms]);
end;
procedure TForm1.AwesomeBarChange(Sender: TObject);
var
starttime: TDateTime;
tmp: string;
alllines: boolean;
begin
starttime := Now;
MainTabs.Tabs[MainTabs.TabIndex] := AwesomeBar.Text;
UpdateSuggestions;
alllines := False;
if AwesomeBar.Text = '' then
begin
FDiscardedLines.Clear;
TextEditor.Lines.Assign(FAllLines);
alllines := True;
Caption := FBaseTitle;
end
else
begin
Caption := AwesomeBar.Text + ' - ' + FBaseTitle;
Application.Title := Caption;
FilterTextByAwesomeBar;
end;
tmp := Format('Lines: %d/%d', [TextEditor.Lines.Count, FAllLines.Count]);
StatusBar.Panels[LineCountPanel].Text := tmp;
if alllines then
begin
StatusBar.Panels[TagCountPanel].Text := 'Tags: all';
end
else
begin
tmp := Format('Tags: %d/%d', [FSelectedTagCount, FAllTagCount]);
StatusBar.Panels[TagCountPanel].Text := tmp;
end;
tmp := 'Time: ' + PrettyPrintMilliSeconds(MilliSecondsBetween(Now, starttime));
StatusBar.Panels[QueryTimePanel].Text := tmp;
end;
procedure TForm1.AwesomeBarEnter(Sender: TObject);
var
ans: TModalResult;
begin
if TextEditor.Modified then
begin
ans := QueryUnsavedChanges;
if ans = mrCancel then
Exit;
if ans = mrYes then
SaveNotes;
end;
UpdateSuggestions;
Suggestions.Visible := True;
Suggestions.ItemIndex := -1;
end;
procedure TForm1.AwesomeBarKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
begin
if (Key = VK_ESCAPE) or (Key = VK_RETURN) then
TextEditor.SetFocus;
if Key = VK_DOWN then
begin
if Suggestions.Items.Count > 0 then
Suggestions.ItemIndex := 0;
Suggestions.SetFocus;
end;
end;
procedure TForm1.BeginFindActionExecute(Sender: TObject);
begin
TextQueryPanel.Show;
TextQuery.SetFocus;
end;
procedure TForm1.CloseTabActionExecute(Sender: TObject);
var
ans: TModalResult;
begin
//check for modification first
if TextEditor.Modified then
begin
ans := QueryUnsavedChanges;
if ans = mrCancel then
Exit;
if ans = mrYes then
SaveNotes;
if ans = mrNo then
TextEditor.Modified := False;
end; //if
//only close if unmodified or cancel wasn't picked above
//add to history first if we have space
if FUndoCloseTabHistoryUsed < UndoCloseTabHistoryMaxSize then
begin
FUndoCloseTabHistory[FUndoCloseTabHistoryUsed] := MainTabs.Tabs[MainTabs.TabIndex];
FUndoCloseTabHistoryUsed := FUndoCloseTabHistoryUsed + 1;
end;
MainTabs.Tabs.Delete(MainTabs.TabIndex);
if MainTabs.Tabs.Count = 0 then
begin
MainTabs.Tabs.Append('');
MainTabsChange(MainTabs);
end;
FilterTextByAwesomeBar;
LoadCaretAndView(MainTabs.TabIndex);
end;
procedure TForm1.DeselectSuggestionsTimerTimer(Sender: TObject);
begin
Suggestions.ItemIndex := -1;
DeselectSuggestionsTimer.Enabled := False;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
var
strs: TStringList;
begin
strs := TStringList.Create;
strs.Assign(MainTabs.Tabs);
strs[MainTabs.TabIndex] := '@' + strs[MainTabs.TabIndex];
strs.SaveToFile(GetFileNearExe('tabs.txt'));
strs.Free;
SaveCaretAndView(MainTabs.TabIndex);
FTabCaretsAndViews.SaveToFile(GetFileNearExe('tabcaretsandviews.txt'));
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
var
ans: TModalResult;
begin
if not TextEditor.Modified then
begin
CanClose := True;
Exit;
end;
ans := QueryUnsavedChanges;
if ans = mrYes then
CanClose := SaveNotes;
if ans = mrNo then
CanClose := True;
if ans = mrCancel then
CanClose := False;
end;
procedure TForm1.CollectAllTags;
var
str: string;
split: TStringList;
begin
FAllTags.Clear;
split := TStringList.Create;
try
split.Delimiter := ' ';
for str in FAllLines do
if IsTagLine(str) then
begin
split.Clear;
split.DelimitedText := str.Replace('#', '');
FAllTags.AddStrings(split);
end;