forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceManagement.pb
2777 lines (2204 loc) · 93.3 KB
/
SourceManagement.pb
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
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
Global IsIDEConfigPresent ; ugly way to do this, but works. to test if the source was ever loaded by the IDE
Global MarkerLines$
; Only for ChangeActiveSourceCode() to hide the previously visible gadget,
; even when *ActiveSource does not actually represent the currently visible one.
; (which is done while opening a new source file)
;
Global VisibleScintillaGadget
; only needed during file loading:
; as there is a UpdateCursorPosition() somewhere in the process, we cannot use
; the SourceFile fields dring this time
Global Loading_FirstVisibleLine, Loading_CurrentLine, Loading_CurrentColumn
Global Loading_FoldingState$
Procedure UpdateCursorPosition()
GetCursorPosition() ; sets the SourceFile fields also used by other functions
StartPosition = SendEditorMessage(#SCI_GETSELECTIONSTART, 0, 0)
EndPosition = SendEditorMessage(#SCI_GETSELECTIONEND, 0, 0)
If StartPosition = EndPosition
Text$ = Language("Misc","Line")+": "+Str(*ActiveSource\CurrentLine)+" "+Language("Misc","Column")+": "+Str(*ActiveSource\CurrentColumnDisplay)
Else
Text$ = Language("Misc","Line")+": "+Str(*ActiveSource\CurrentLine)+" "+Language("Misc","Column")+": "+Str(*ActiveSource\CurrentColumnDisplay) + " - ["+Str(CountCharacters(*ActiveSource\EditorGadget, StartPosition, EndPosition))+"]" ; Use a short 'selection' marker, or the text line+column+selection will be too big for the statusbar field (need #SCI_COUNTCHARACTERS to handle UTF8 properly)
EndIf
StatusBarText(#STATUSBAR, 0, Text$, #PB_StatusBar_Center)
EndProcedure
Procedure RefreshSourceTitle(*Source.SourceFile)
PushListPosition(FileList())
ChangeCurrentElement(FileList(), *Source)
Index = ListIndex(FileList())
PopListPosition(FileList())
SetTabBarGadgetItemText(#GADGET_FilesPanel, Index, GetSourceTitle(*Source))
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
If *Source = *ProjectInfo
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, GetCocoaColor("textColor"))
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, GetCocoaColor("controlAccentColor"))
ElseIf *Source\IsForm And *Source\ProjectFile
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, GetCocoaColor("textColor"))
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, GetCocoaColor("controlAccentColor"))
ElseIf *Source\IsForm
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, GetCocoaColor("textColor"))
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, GetCocoaColor("controlAccentColor"))
ElseIf *Source\ProjectFile
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, GetCocoaColor("textColor"))
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, GetCocoaColor("controlAccentColor"))
Else
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, GetCocoaColor("textColor"))
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, GetCocoaColor("controlBackgroundColor"))
EndIf
CompilerElse
If *Source = *ProjectInfo
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, #COLOR_FilePanelFront)
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, #COLOR_ProjectInfo)
ElseIf *Source\IsForm And *Source\ProjectFile
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, #COLOR_FilePanelFront)
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, #COLOR_FormProjectFile)
ElseIf *Source\IsForm
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, #COLOR_FilePanelFront)
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, #COLOR_FormFile)
ElseIf *Source\ProjectFile
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, #COLOR_FilePanelFront)
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, #COLOR_ProjectFile)
Else
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_FrontColor, #COLOR_FilePanelFront)
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Index, #PB_Gadget_BackColor, #PB_Default)
EndIf
CompilerEndIf
EndProcedure
; get the title string for the current element in FileList()
Procedure.s GetSourceTitle(*Source.SourceFile)
If *Source = *ProjectInfo
;Title$ = "> " + Language("Project","TabTitle")
Title$ = Language("Project","TabTitle")
Else
Modified = GetSourceModified(*Source)
If *Source\FileName$ = "" And *Source\IsForm
Title$ = Language("FileStuff","NewForm")
ElseIf *Source\FileName$ = ""
Title$ = Language("FileStuff","NewSource")
Else
Title$ = GetFilePart(*Source\FileName$)
EndIf
If Modified
Title$ + "*"
EndIf
; If *Source\ProjectFile
; Title$ = "> " + Title$
; EndIf
EndIf
ProcedureReturn Title$
EndProcedure
Procedure UpdateSourceStatus(Modified)
If Modified = -1
Modified = GetSourceModified()
ElseIf Modified <> GetSourceModified()
SetSourceModified(Modified)
EndIf
; only do an actual refresh if needed.
; this procedure is called many times during folding as it seems,
; so limit the number of updates if nothing changed
If Modified <> *ActiveSource\DisplayModified
*ActiveSource\DisplayModified = Modified
RefreshSourceTitle(*ActiveSource)
UpdateMenuStates()
EndIf
EndProcedure
Procedure ChangeActiveSourcecode(*OldSource.SourceFile = 0)
If *OldSource = 0
*OldSource = *ActiveSource
EndIf
; Make sure the sorted data for the old source is up to date to ensure
; a quick access. Does nothing if the data is up to date
If *OldSource And *OldSource <> *ProjectInfo And *OldSource\IsForm = 0
SortParserData(*OldSource\Parser, *OldSource)
EndIf
AutoComplete_Close()
If *ActiveSource And IsWindow(#WINDOW_Option) ; make sure the options are closed
If (*ActiveSource <> *ProjectInfo And *ActiveSource\ProjectFile = 0) Or (@FileList() <> *ProjectInfo And FileList()\ProjectFile = 0)
; close only if either the old or the new code do not belong to the project
OptionWindowEvents(#PB_Event_CloseWindow)
EndIf
EndIf
CompilerIf #SpiderBasic
If *ActiveSource And IsWindow(#WINDOW_CreateApp) ; make sure the CreateApp window is closed
If (*ActiveSource <> *ProjectInfo And *ActiveSource\ProjectFile = 0) Or (@FileList() <> *ProjectInfo And FileList()\ProjectFile = 0)
; close only if either the old or the new code do not belong to the project
CreateAppWindowEvents(#PB_Event_CloseWindow)
EndIf
EndIf
CompilerEndIf
*ActiveSource = @FileList()
SetTabBarGadgetState(#GADGET_FilesPanel, ListIndex(FileList()))
HideLineNumbers(*ActiveSource, 1-EnableLineNumbers)
UpdateMainWindowTitle()
ClearList(BlockSelectionStack())
BlockSelectionUpdated = #False
ErrorLog_Refresh() ; always update, even if hidden
ErrorLog_SyncState(#False) ; update the display state
ResizeMainWindow() ; make sure the EditorGadget is correctly sized
If *ActiveSource = *ProjectInfo
HideGadget(#GADGET_ProjectInfo, 0)
Else
If *ProjectInfo
HideGadget(#GADGET_ProjectInfo, 1)
EndIf
SetActiveGadget(*ActiveSource\EditorGadget)
HideEditorGadget(*ActiveSource\EditorGadget, 0) ; Show only when the resize is done
CompilerIf #CompileWindows
;
; Some weird touchpad driver has a problem with invisible windows being on top
; in the z-order, so make sure the newly visible gadget is the topmost one
; This is not a PB bug, its just a compatibility hack for that driver
;
SetWindowPos_(GadgetID(*ActiveSource\EditorGadget), #HWND_TOP, 0, 0, 0, 0, #SWP_NOMOVE|#SWP_NOOWNERZORDER|#SWP_NOSIZE)
CompilerEndIf
EndIf
If *ActiveSource = *ProjectInfo
If VisibleScintillaGadget
HideEditorGadget(VisibleScintillaGadget, 1)
EndIf
VisibleScintillaGadget = 0
Else
; We hide the previous editor gadget only when the new one is displayed, to remove flickering
; NOTE: While a new source is created, the actually displayed gadget is not
; from *ActiveSource (to avoid some flicker), so use a special variable for this check.
;
If VisibleScintillaGadget And VisibleScintillaGadget <> *ActiveSource\EditorGadget
HideEditorGadget(VisibleScintillaGadget, 1)
EndIf
VisibleScintillaGadget = *ActiveSource\EditorGadget
EndIf
; show up the canvas for form drawing it the source is actually a form otherwise hide it
If *ActiveSource\IsForm
currentwindow = *ActiveSource\IsForm
FD_SelectWindow(currentwindow)
FD_UpdateObjList()
redraw = 1
If FormWindows()\current_view = 0
HideGadget(#GADGET_Form, 0)
HideEditorGadget(*ActiveSource\EditorGadget, 1)
RemoveKeyboardShortcut(#WINDOW_Main, #PB_Shortcut_Return)
RemoveKeyboardShortcut(#WINDOW_Main, #PB_Shortcut_Tab)
RemoveKeyboardShortcut(#WINDOW_Main, #PB_Shortcut_Shift | #PB_Shortcut_Tab)
Else
HideGadget(#GADGET_Form, 1)
HideEditorGadget(*ActiveSource\EditorGadget, 0)
SetActiveGadget(*ActiveSource\EditorGadget)
FD_SelectNone()
CompilerIf #CompileWindows | #CompileMac
AddKeyboardShortcut(#WINDOW_Main, #PB_Shortcut_Return, #MENU_Scintilla_Enter)
AddKeyboardShortcut(#WINDOW_Main, #PB_Shortcut_Tab, #MENU_Scintilla_Tab)
AddKeyboardShortcut(#WINDOW_Main, #PB_Shortcut_Shift | #PB_Shortcut_Tab, #MENU_Scintilla_ShiftTab)
CompilerEndIf
EndIf
Else
currentwindow = 0 ; no more active form
CompilerIf #CompileWindows | #CompileMac
AddKeyboardShortcut(#WINDOW_Main, #PB_Shortcut_Return, #MENU_Scintilla_Enter)
AddKeyboardShortcut(#WINDOW_Main, #PB_Shortcut_Tab, #MENU_Scintilla_Tab)
AddKeyboardShortcut(#WINDOW_Main, #PB_Shortcut_Shift | #PB_Shortcut_Tab, #MENU_Scintilla_ShiftTab)
CompilerEndIf
FD_SelectNone()
If IsGadget(#Form_PropObjList)
ClearGadgetItems(#Form_PropObjList)
EndIf
If IsGadget(#GADGET_Form)
HideGadget(#GADGET_Form, 1)
EndIf
EndIf
; Note:
; For some odd reason, the ResizeMainWindow() above has no effect as long as the ScintillaGadget
; is not the topmost visible one on Linux. The result is that when you close all tabs, you end up
; with a newly created '<New>' source that is not visible (because its still sized 0,0)
;
; This is a Linux only problem and i have no idea why, but just trying another
; resize after the proper gadget is visible and events are flushed fixes the trouble.
;
CompilerIf #CompileLinux
FlushEvents() ; this still dispatches all events, so its not problematic
ResizeMainWindow()
CompilerEndIf
UpdateCursorPosition()
; enabled the folding update again, as strangely the fold mark in first line disappears otherwise !?
If *ActiveSource <> *ProjectInfo And Not *ActiveSource\IsForm
FullSourceScan(*ActiveSource)
UpdateFolding(*ActiveSource, 0, -1)
EndIf
UpdateProcedureList()
UpdateVariableViewer()
UpdateMenuStates()
SetDebuggerMenuStates()
; update quickhelp
If *ActiveSource = *ProjectInfo
ChangeStatus("", 0)
ElseIf SendEditorMessage(#SCI_GETREADONLY, 0, 0) = 0 ; do not update quickhelp when in debugger mode
ChangeStatus("", 0)
UpdateCursorPosition()
selStart = SendEditorMessage(#SCI_GETSELECTIONSTART, 0, 0)
selEnd = SendEditorMessage(#SCI_GETSELECTIONEND , 0, 0)
If selStart = selEnd
QuickHelpFromLine(*ActiveSource\CurrentLine-1, *ActiveSource\CurrentColumnChars-1)
EndIf
EndIf
UpdateSelectionRepeat()
EndProcedure
Procedure NewSource(FileName$, ExecuteTool)
*OldSource = *ActiveSource
LastElement(FileList())
AddElement(FileList())
; Generate a unique ID for the target in this structure
;
FileList()\ID = GetUniqueID()
If FileName$ = ""
Title$ = Language("FileStuff","NewSource")
Else
Title$ = GetFilePart(FileName$)
EndIf
OpenGadgetList(#GADGET_SourceContainer)
CreateEditorGadget()
CloseGadgetList()
If FileName$ = ""
FileList()\IsCode = #True ; assume it is a code file until it is saved
Else
FileList()\IsCode = IsCodeFile(FileName$)
EndIf
FileList()\FileName$ = FileName$
FileList()\Debugger = OptionDebugger ; set the default values
FileList()\EnablePurifier = OptionPurifier
FileList()\EnableASM = OptionInlineASM
FileList()\EnableXP = OptionXPSkin
FileList()\EnableAdmin = OptionVistaAdmin
FileList()\EnableUser = OptionVistaUser
FileList()\DPIAware = OptionDPIAware
FileList()\EnableThread = OptionThread
FileList()\EnableOnError = OptionOnError
FileList()\ExecutableFormat = OptionExeFormat
FileList()\CPU = OptionCPU
FileList()\NewLineType = OptionNewLineType
FileList()\SubSystem$ = OptionSubSystem$
FileList()\ErrorLog = OptionErrorLog
FileList()\Parser\Encoding = OptionEncoding
FileList()\UseCreateExe = OptionUseCreateExe
FileList()\UseBuildCount = OptionUseBuildCount
FileList()\UseCompileCount = OptionUseCompileCount
FileList()\TemporaryExePlace= OptionTemporaryExe
FileList()\CurrentDirectory$= ""
FileList()\ToggleFolds = 1
FileList()\CustomCompiler = 0
FileList()\PurifierGranularity$ = ""
FileList()\ExistsOnDisk = #False
If OptionEncoding = 0
ScintillaSendMessage(FileList()\EditorGadget, #SCI_SETCODEPAGE, 0, 0)
Else
ScintillaSendMessage(FileList()\EditorGadget, #SCI_SETCODEPAGE, #SC_CP_UTF8, 0)
EndIf
AddTabBarGadgetItem(#GADGET_FilesPanel, #PB_Default, Title$)
ChangeActiveSourcecode(*OldSource)
; Link to project (if any)
; Do it as soon as possible, so the panel tab gets the right color very quickly.
LinkSourceToProject(*ActiveSource)
If EnableColoring
SetBackgroundColor()
EndIf
SetTabBarGadgetState(#GADGET_FilesPanel, CountTabBarGadgetItems(#GADGET_FilesPanel)-1)
UpdateSourceStatus(0)
ResizeMainWindow()
; if configured and needed, execute tool for new sources
If ExecuteTool
AddTools_Execute(#TRIGGER_NewSource, *ActiveSource)
; reset the modified flag so this code can be closed without saving if nothing is changed
UpdateSourceStatus(#False)
; place cursor at end of file (usually such tools add headers to a file)
Pos = SendEditorMessage(#SCI_GETLENGTH, 0, 0)
SendEditorMessage(#SCI_SETSEL, Pos, Pos)
UpdateCursorPosition()
EndIf
EndProcedure
Procedure DetectNewLineType(*Buffer, BufferSize)
*Pointer.HighlightPTR = *Buffer
*BufferEnd = *Buffer + BufferSize
DetectedType = -1
While *Pointer < *BufferEnd
If *Pointer\b = 13 And *Pointer\a[1] = 10
; windows newline
If DetectedType <> 0 And DetectedType <> -1 ; oops, a mixed up file, use os standard
ProcedureReturn #DEFAULT_NewLineType
EndIf
DetectedType = 0
*Pointer + 1
ElseIf *Pointer\b = 10 And *Pointer\a[1] = 13
; unknown type, use os standard
ProcedureReturn #DEFAULT_NewLineType
ElseIf *Pointer\b = 10
; linux newline
If DetectedType <> 1 And DetectedType <> -1 ; oops, a mixed up file, use os standard
ProcedureReturn #DEFAULT_NewLineType
EndIf
DetectedType = 1
ElseIf *Pointer\b = 13
; mac newline
If DetectedType <> 2 And DetectedType <> -1 ; oops, a mixed up file, use os standard
ProcedureReturn #DEFAULT_NewLineType
EndIf
DetectedType = 2
EndIf
*Pointer + 1
Wend
If DetectedType = -1
DetectedType = #DEFAULT_NewLineType
EndIf
ProcedureReturn DetectedType
EndProcedure
Procedure ChangeNewLineType(*ptrBuffer.INTEGER, *ptrBufferSize.INTEGER, NewLineType)
*NewBuffer = AllocateMemory(*ptrBufferSize\i + 1000000)
*BufferEnd = *ptrBuffer\i + *ptrBufferSize\i
*ReadCursor.HighlightPTR = *ptrBuffer\i
*WriteCursor.HighlightPTR = *NewBuffer
CopyMemoryString("", @*WriteCursor)
If *NewBuffer
While *ReadCursor < *BufferEnd
If (*ReadCursor\b = 13 And *ReadCursor\a[1] = 10) Or (*ReadCursor\b = 10 And *ReadCursor\a[1] = 13)
If NewLineType = 0 ; to crlf
*WriteCursor\b = 13
*WriteCursor\a[1] = 10
*ReadCursor + 1
*WriteCursor + 1
ElseIf NewLineType = 1 ; to lf
*WriteCursor\b = 10
*ReadCursor + 1
Else ; to cr
*WriteCursor\b = 13
*ReadCursor + 1
EndIf
ElseIf *ReadCursor\b = 13 Or *ReadCursor\b = 10
If NewLineType = 0 ; crlf
*WriteCursor\b = 13
*WriteCursor\a[1] = 10
*WriteCursor + 1
ElseIf NewLineType = 1 ; to lf
*WriteCursor\b = 10
Else ; to cr
*WriteCursor\b = 13
EndIf
Else
*WriteCursor\b = *ReadCursor\b
EndIf
*ReadCursor + 1
*WriteCursor + 1
Wend
FreeMemory(*ptrBuffer\i) ; free the old buffer
*ptrBuffer\i = *NewBuffer
*ptrBufferSize\i = *WriteCursor - *NewBuffer
EndIf
EndProcedure
; Note: We do not use the SYS_AsciiToUTF8 etc anymore because:
; The "�" and "`" Characters ($91, $92) are not displayed by Scintilla.
; Scintilla instead uses U+2018 and U+2019 characters when they get pasted.
; So to avoid any confustion from a disappearing character we handle this conversion
; as well when doing the Source encoding change.
;
; Lets hope there are no more such spechial characters.
;
; Some notes:
; - unrepresentable chars become '?'
;
Procedure AsciiToUTF8(*out.ASCII, *outlen.LONG, *in.ASCII, *inlen.LONG)
*in_end = *in + *inlen\l ; copy to local vars for access speed
*out_start = *out
While *in < *in_end ; ascii range
If *in\a < $80
*out\a = *in\a
*out + 1
ElseIf *in\a = $91 ; `-char. Turn this into U+2018
*out\a = $E2
*out + 1
*out\a = $80
*out + 1
*out\a = $98
*out + 1
ElseIf *in\a = $92 ; �-char. Turn this into U+2019
*out\a = $E2
*out + 1
*out\a = $80
*out + 1
*out\a = $99
*out + 1
Else ; turn it into a 2byte sequence
*out\a = ((*in\a >> 6) & %00011111) | %11000000
*out + 1
*out\a = (*in\a & %00111111) | %10000000
*out + 1
EndIf
*in + 1
Wend
*outlen\l = *out - *out_start
EndProcedure
Procedure UTF8ToAscii(*out.ASCII, *outlen.LONG, *in.ASCII, *inlen.LONG)
*in_end = *in + *inlen\l ; copy to local vars for access speed
*out_start = *out
While *in < *in_end
c = *in\a
If c & %10000000 = 0 ; 1-byte char
*out\a = c
*out + 1
*in + 1
ElseIf c & %11100000 = %11000000 And *in+1 < *in_end ; 2-byte char
*in + 1
If *in\a & %11000000 = %10000000 ; check if the next is a followup byte
c = ((c & %00011111) << 6) | (*in\a & %00111111)
If c < 256
*out\a = c
Else
*out\a = '?' ; unicode char outside of ascii range
EndIf
*out + 1
*in + 1
Else
*out\a = '?' ; invalid utf8
*out + 1
EndIf
ElseIf c & %11110000 = %11100000 And *in+2 < *in_end ; 3-byte char, not representable in ascii
If c = $E2 And PeekC(*in+1) = $80 And PeekC(*in+2) = $98
*out\a = $91
*out + 1
*in + 3
ElseIf c = $E2 And PeekC(*in+1) = $80 And PeekC(*in+2) = $99
*out\a = $92
*out + 1
*in + 3
Else
*out\a = '?'
*out + 1
; skip the next two bytes only if they are correct followup bytes
If PeekC(*in+1) & %11000000 = %10000000 And PeekC(*in+2) & %11000000 = %10000000
*in + 3
ElseIf PeekC(*in+1) & %11000000 = %10000000
*in + 2 ; incomplete sequence
Else
*in + 1 ; only start byte of sequence
EndIf
EndIf
ElseIf c & %11111000 = %11110000 And *in+3 < *in_end ; 4-byte char, not representable in ascii
*out\a = '?'
*out + 1
; skip the next three bytes only if they are correct followup bytes
If PeekC(*in+1) & %11000000 = %10000000 And PeekC(*in+2) & %11000000 = %10000000 And PeekC(*in+3) & %11000000 = %10000000
*in + 4
ElseIf PeekC(*in+1) & %11000000 = %10000000 And PeekC(*in+2) & %11000000 = %10000000
*in + 3 ; incomplete sequence
ElseIf PeekC(*in+1) & %11000000 = %10000000
*in + 2 ; incomplete sequence
Else
*in + 1 ; only start byte of sequence
EndIf
Else
*in + 1 ; invalid UTF-8, just skip it
EndIf
Wend
*outlen\l = *out - *out_start
EndProcedure
Procedure ChangeTextEncoding(*Source.SourceFile, NewEncoding)
If NewEncoding <> *Source\Parser\Encoding
; Its .l as the SYS function takes an int *
OldLength.l = ScintillaSendMessage(*Source\EditorGadget, #SCI_GETLENGTH, 0, 0)
*OldBuffer = AllocateMemory(OldLength+1)
If *OldBuffer
ScintillaSendMessage(*Source\EditorGadget, #SCI_GETTEXT, OldLength+1, *OldBuffer) ; #SCI_GETTEXT returns length-1 bytes... very inconsistent of scintilla
If NewEncoding = 1
NewLength.l = OldLength*4 ; Utf8 can only be 4x as big as Ascii
Else
NewLength.l = OldLength ; Buffer can only get smaller for Utf8-Ascii
EndIf
*NewBuffer = AllocateMemory(NewLength+1)
If *NewBuffer
If NewEncoding = 1
AsciiToUTF8(*NewBuffer, @NewLength, *OldBuffer, @OldLength)
Else
UTF8ToAscii(*NewBuffer, @NewLength, *OldBuffer, @OldLength)
EndIf
ScintillaSendMessage(*Source\EditorGadget, #SCI_CLEARALL, 0, 0) ; should completely erase the old document and create a new one
If NewEncoding = 0
ScintillaSendMessage(*Source\EditorGadget, #SCI_SETCODEPAGE, 0, 0)
Else
ScintillaSendMessage(*Source\EditorGadget, #SCI_SETCODEPAGE, #SC_CP_UTF8, 0)
EndIf
ScintillaSendMessage(*Source\EditorGadget, #SCI_SETTEXT, 0, *NewBuffer)
*Source\Parser\Encoding = NewEncoding ; finally update the flag in the structure
FreeMemory(*NewBuffer)
EndIf
FreeMemory(*OldBuffer)
EndIf
EndIf
EndProcedure
; Use macro to ease the typing
;
Macro AddStringConfigLine(Key, Value)
If Value And IsCodeFile ; Don't save empty value, as it's the default
NbLines + 1
ConfigLines$(NbLines) = Key + " = " + Value
EndIf
EndMacro
Macro AddFlagConfigLine(Key, Value)
If Value And IsCodeFile ; Don't save empty value, as it's the default
NbLines + 1
ConfigLines$(NbLines) = Key
EndIf
EndMacro
; Also used to append Project settings to a temp file, so it must handle a CompileTarget input
;
Procedure SaveProjectSettings(*Target.CompileTarget, IsCodeFile, IsTempFile, ReportErrors)
If SaveProjectSettings = 3 And IsTempFile = 0 ; don't save anything
ProcedureReturn
EndIf
If SaveProjectSettings = 0 And IsCodeFile = 0
; Do not save settings at the end of non-code files
; We do however save settings when they are not appended to the file to memorize cursor position etc
ProcedureReturn
EndIf
; If its not a target, we have more info from the extended sourcefile structure
If *Target\IsProject = 0
*Source.SourceFile = *Target
Else
*Source = 0
EndIf
; generate the config lines
;
If CommandlineBuild = 0 And *Target = *ActiveSource
UpdateCursorPosition()
EndIf
; Note: All entries with a fixed number of lines come first
; (no array bounds check needed then)
;
NbLines = 1
ConfigLines$(NbLines) = "IDE Options = "+DefaultCompiler\VersionString$
If IsCodeFile
If *Target\ExecutableFormat = 1
NbLines + 1
ConfigLines$(NbLines) = "ExecutableFormat = Console"
ElseIf *Target\ExecutableFormat = 2
NbLines + 1
; Note: when writing, we make a difference, on reading we allow both for compatibility
;
CompilerIf #CompileWindows
ConfigLines$(NbLines) = "ExecutableFormat = Shared dll"
CompilerElseIf #CompileMac
ConfigLines$(NbLines) = "ExecutableFormat = Shared .dylib"
CompilerElse ; Linux
ConfigLines$(NbLines) = "ExecutableFormat = Shared .so"
CompilerEndIf
EndIf ; no need to write it when it's 0.. it will default to that anyway
EndIf
If (MemorizeCursor Or IsTempFile) And *Source
If *Source\CurrentLine > 1
NbLines + 1
ConfigLines$(NbLines) = "CursorPosition = "+Str(*Source\CurrentLine-1)
EndIf
If IsTempFile ; this is saved for tempfiles only
NbLines + 1
ConfigLines$(NbLines) = "CursorColumn = "+Str(*Source\CurrentColumnBytes)
EndIf
If *Source = *ActiveSource
FirstLine = GetFirstVisibleLine()
EndIf
If FirstLine > 0
NbLines + 1
ConfigLines$(NbLines) = "FirstLine = "+Str(FirstLine)
EndIf
EndIf
If *Source And *Source = *ActiveSource And EnableFolding And IsCodeFile
FoldingInfo$ = CreateFoldingInformation()
If FoldingInfo$ <> ""
NbLines + 1
ConfigLines$(NbLines) = "Folding = "+FoldingInfo$
EndIf
EndIf
If CommandlineBuild = 0 And *Source = *ActiveSource And (MemorizeMarkers Or IsTempFile)
Markers$ = GetMarkerString()
If Markers$ <> ""
NbLines + 1
ConfigLines$(NbLines) = "Markers = "+Markers$
EndIf
EndIf
CompilerIf #SpiderBasic
AddStringConfigLine("WindowTheme", *Target\WindowTheme$)
AddStringConfigLine("GadgetTheme", *Target\GadgetTheme$)
AddStringConfigLine("WebServerAddress", *Target\WebServerAddress$)
; WebApp
;
AddStringConfigLine("WebAppName" , *Target\WebAppName$)
AddStringConfigLine("WebAppIcon" , *Target\WebAppIcon$)
AddStringConfigLine("HtmlFilename" , *Target\HtmlFilename$)
AddStringConfigLine("JavaScriptFilename", *Target\JavaScriptFilename$)
AddStringConfigLine("JavaScriptPath" , *Target\JavaScriptPath$)
AddStringConfigLine("ExportCommandLine" , *Target\ExportCommandLine$)
AddStringConfigLine("ExportArguments" , *Target\ExportArguments$)
AddStringConfigLine("ResourceDirectory" , *Target\ResourceDirectory$)
AddFlagConfigLine("EnableResourceDirectory", *Target\EnableResourceDirectory)
AddFlagConfigLine("OptimizeJS" , *Target\OptimizeJS)
AddFlagConfigLine("CopyJavaScriptLibrary", *Target\CopyJavaScriptLibrary)
AddFlagConfigLine("WebAppEnableDebugger" , *Target\WebAppEnableDebugger)
; iOSApp
;
AddStringConfigLine("iOSAppName" , *Target\iOSAppName$)
AddStringConfigLine("iOSAppIcon" , *Target\iOSAppIcon$)
AddStringConfigLine("iOSAppVersion" , *Target\iOSAppVersion$)
AddStringConfigLine("iOSAppPackageID" , *Target\iOSAppPackageID$)
AddStringConfigLine("iOSAppStartupImage", *Target\iOSAppStartupImage$)
AddStringConfigLine("iOSAppOutput" , *Target\iOSAppOutput$)
AddStringConfigLine("iOSAppResourceDirectory" , *Target\iOSAppResourceDirectory$)
AddFlagConfigLine("iOSAppEnableResourceDirectory", *Target\iOSAppEnableResourceDirectory)
AddStringConfigLine("iOSAppOrientation", Str(*Target\iOSAppOrientation))
AddFlagConfigLine("iOSAppGeolocation", *Target\iOSAppGeolocation)
AddFlagConfigLine("iOSAppFullScreen" , *Target\iOSAppFullScreen)
AddFlagConfigLine("iOSAppAutoUpload" , *Target\iOSAppAutoUpload)
AddFlagConfigLine("iOSAppEnableDebugger" , *Target\iOSAppEnableDebugger)
; AndroidApp
;
AddStringConfigLine("AndroidAppName" , *Target\AndroidAppName$)
AddStringConfigLine("AndroidAppIcon" , *Target\AndroidAppIcon$)
AddStringConfigLine("AndroidAppVersion" , *Target\AndroidAppVersion$)
AddStringConfigLine("AndroidAppPackageID" , *Target\AndroidAppPackageID$)
AddStringConfigLine("AndroidAppIAPKey" , *Target\AndroidAppIAPKey$)
AddStringConfigLine("AndroidAppStartupImage", *Target\AndroidAppStartupImage$)
AddStringConfigLine("AndroidAppOutput" , *Target\AndroidAppOutput$)
AddStringConfigLine("AndroidAppResourceDirectory" , *Target\AndroidAppResourceDirectory$)
AddFlagConfigLine("AndroidAppEnableResourceDirectory", *Target\AndroidAppEnableResourceDirectory)
AddStringConfigLine("AndroidAppOrientation" , Str(*Target\AndroidAppOrientation))
AddFlagConfigLine("AndroidAppGeolocation" , *Target\AndroidAppGeolocation)
AddFlagConfigLine("AndroidAppFullScreen" , *Target\AndroidAppFullScreen)
AddFlagConfigLine("AndroidAppAutoUpload" , *Target\AndroidAppAutoUpload)
AddFlagConfigLine("AndroidAppEnableDebugger", *Target\AndroidAppEnableDebugger)
CompilerEndIf
If *Target\EnableASM And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnableAsm"
EndIf
If *Target\EnableThread And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnableThread"
EndIf
If *Target\EnableXP And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnableXP"
EndIf
If *Target\EnableAdmin And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnableAdmin"
EndIf
If *Target\EnableUser And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnableUser"
EndIf
If *Target\DPIAware And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "DPIAware"
EndIf
If *Target\EnableOnError And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnableOnError"
EndIf
If *Target\UseIcon And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "UseIcon = " + *Target\IconName$
EndIf
If *Target\UseMainFile And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "UseMainFile = " + *Target\MainFile$
EndIf
If *Target\ExecutableName$ And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "Executable = " + CreateRelativePath(GetPathPart(*Target\FileName$), *Target\ExecutableName$)
EndIf
If *Target\CPU And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "CPU = " + Str(*Target\CPU)
EndIf
If *Target\SubSystem$ And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "SubSystem = " + *Target\Subsystem$
EndIf
If *Target\LinkerOptions$ And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "LinkerOptions = " + *Target\LinkerOptions$
EndIf
If *Target\Debugger = 0 And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "DisableDebugger"
EndIf
If *Source And *Source\ErrorLog = 0 And IsCodeFile ; this is only for source files
NbLines + 1
ConfigLines$(NbLines) = "HideErrorLog"
EndIf
If *Target\CommandLine$ And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "CommandLine = " + *Target\CommandLine$
EndIf
If *Target\CurrentDirectory$ And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "CurrentDirectory = " + *Target\CurrentDirectory$
EndIf
If *Target\TemporaryExePlace And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "CompileSourceDirectory"
EndIf
If *Target\EnabledTools$ And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnabledTools = " + *Target\EnabledTools$
EndIf
If *Target\CustomCompiler And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "Compiler = "+*Target\CompilerVersion$
EndIf
If *Target\CustomDebugger And IsCodeFile; do not save any of this if disabled
; CompilerIf #CompileMac ; not supported on OSX yet OSX-debug
; If *Source\DebuggerType = 1 Or *Source\DebuggerType = 2
; Type = *Source\DebuggerType + 1
; Else
; Type = 0
; EndIf
; CompilerElse
Type = *Target\DebuggerType
; CompilerEndIf
If Type = 1
NbLines + 1
ConfigLines$(NbLines) = "Debugger = IDE"
ElseIf Type = 2
NbLines + 1
ConfigLines$(NbLines) = "Debugger = Standalone"
ElseIf Type = 3
NbLines + 1
ConfigLines$(NbLines) = "Debugger = Console"
EndIf
EndIf
If *Target\CustomWarning And IsCodeFile
If *Target\WarningMode = 0
NbLines + 1
ConfigLines$(NbLines) = "Warnings = Ignore"
ElseIf *Target\WarningMode = 1
NbLines + 1
ConfigLines$(NbLines) = "Warnings = Display"
ElseIf *Target\WarningMode = 2
NbLines + 1
ConfigLines$(NbLines) = "Warnings = Error"
EndIf
EndIf
; Save the granularity options even if the purifier is disabled
;
If *Target\EnablePurifier And *Target\PurifierGranularity$ And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnablePurifier = " + *Target\PurifierGranularity$
ElseIf *Target\EnablePurifier And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnablePurifier"
ElseIf *Target\PurifierGranularity$ And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "DisablePurifier = " + *Target\PurifierGranularity$
EndIf
If *Target\UseCompileCount And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnableCompileCount = " + Str(*Target\CompileCount)
ElseIf *Target\CompileCount > 0 And IsCodeFile ; only save when <> 0 in disabled mode
NbLines + 1
ConfigLines$(NbLines) = "DisableCompileCount = " + Str(*Target\CompileCount)
EndIf
If *Target\UseBuildCount And IsCodeFile
NbLines + 1
ConfigLines$(NbLines) = "EnableBuildCount = " + Str(*Target\BuildCount)
ElseIf *Target\BuildCount > 0 And IsCodeFile ; only save when <> 0 in disabled mode